msi: Add a stub ALTER query.
[wine/wine64.git] / server / process.c
blobb36ca3708522f0d5c29dfa4f9a4451eecaa11008
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #include <unistd.h>
36 #ifdef HAVE_POLL_H
37 #include <poll.h>
38 #endif
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "console.h"
50 #include "user.h"
51 #include "security.h"
53 /* process structure */
55 static struct list process_list = LIST_INIT(process_list);
56 static int running_processes;
58 /* process operations */
60 static void process_dump( struct object *obj, int verbose );
61 static int process_signaled( struct object *obj, struct thread *thread );
62 static unsigned int process_map_access( struct object *obj, unsigned int access );
63 static void process_poll_event( struct fd *fd, int event );
64 static void process_destroy( struct object *obj );
66 static const struct object_ops process_ops =
68 sizeof(struct process), /* size */
69 process_dump, /* dump */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 process_signaled, /* signaled */
73 no_satisfied, /* satisfied */
74 no_signal, /* signal */
75 no_get_fd, /* get_fd */
76 process_map_access, /* map_access */
77 no_lookup_name, /* lookup_name */
78 no_close_handle, /* close_handle */
79 process_destroy /* destroy */
82 static const struct fd_ops process_fd_ops =
84 NULL, /* get_poll_events */
85 process_poll_event, /* poll_event */
86 no_flush, /* flush */
87 no_get_file_info, /* get_file_info */
88 no_queue_async, /* queue_async */
89 no_cancel_async /* cancel async */
92 /* process startup info */
94 struct startup_info
96 struct object obj; /* object header */
97 obj_handle_t hstdin; /* handle for stdin */
98 obj_handle_t hstdout; /* handle for stdout */
99 obj_handle_t hstderr; /* handle for stderr */
100 struct file *exe_file; /* file handle for main exe */
101 struct process *process; /* created process */
102 data_size_t data_size; /* size of startup data */
103 void *data; /* data for startup info */
106 static void startup_info_dump( struct object *obj, int verbose );
107 static int startup_info_signaled( struct object *obj, struct thread *thread );
108 static void startup_info_destroy( struct object *obj );
110 static const struct object_ops startup_info_ops =
112 sizeof(struct startup_info), /* size */
113 startup_info_dump, /* dump */
114 add_queue, /* add_queue */
115 remove_queue, /* remove_queue */
116 startup_info_signaled, /* signaled */
117 no_satisfied, /* satisfied */
118 no_signal, /* signal */
119 no_get_fd, /* get_fd */
120 no_map_access, /* map_access */
121 no_lookup_name, /* lookup_name */
122 no_close_handle, /* close_handle */
123 startup_info_destroy /* destroy */
127 struct ptid_entry
129 void *ptr; /* entry ptr */
130 unsigned int next; /* next free entry */
133 static struct ptid_entry *ptid_entries; /* array of ptid entries */
134 static unsigned int used_ptid_entries; /* number of entries in use */
135 static unsigned int alloc_ptid_entries; /* number of allocated entries */
136 static unsigned int next_free_ptid; /* next free entry */
137 static unsigned int last_free_ptid; /* last free entry */
139 #define PTID_OFFSET 8 /* offset for first ptid value */
141 /* allocate a new process or thread id */
142 unsigned int alloc_ptid( void *ptr )
144 struct ptid_entry *entry;
145 unsigned int id;
147 if (used_ptid_entries < alloc_ptid_entries)
149 id = used_ptid_entries + PTID_OFFSET;
150 entry = &ptid_entries[used_ptid_entries++];
152 else if (next_free_ptid)
154 id = next_free_ptid;
155 entry = &ptid_entries[id - PTID_OFFSET];
156 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
158 else /* need to grow the array */
160 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
161 if (!count) count = 64;
162 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
164 set_error( STATUS_NO_MEMORY );
165 return 0;
167 ptid_entries = entry;
168 alloc_ptid_entries = count;
169 id = used_ptid_entries + PTID_OFFSET;
170 entry = &ptid_entries[used_ptid_entries++];
173 entry->ptr = ptr;
174 return id;
177 /* free a process or thread id */
178 void free_ptid( unsigned int id )
180 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
182 entry->ptr = NULL;
183 entry->next = 0;
185 /* append to end of free list so that we don't reuse it too early */
186 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
187 else next_free_ptid = id;
189 last_free_ptid = id;
192 /* retrieve the pointer corresponding to a process or thread id */
193 void *get_ptid_entry( unsigned int id )
195 if (id < PTID_OFFSET) return NULL;
196 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
197 return ptid_entries[id - PTID_OFFSET].ptr;
200 /* return the main thread of the process */
201 struct thread *get_process_first_thread( struct process *process )
203 struct list *ptr = list_head( &process->thread_list );
204 if (!ptr) return NULL;
205 return LIST_ENTRY( ptr, struct thread, proc_entry );
208 /* set the state of the process startup info */
209 static void set_process_startup_state( struct process *process, enum startup_state state )
211 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
212 if (process->startup_info)
214 wake_up( &process->startup_info->obj, 0 );
215 release_object( process->startup_info );
216 process->startup_info = NULL;
220 /* final cleanup once we are sure a process is really dead */
221 static void process_died( struct process *process )
223 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
224 release_object( process );
225 if (!--running_processes) close_master_socket();
228 /* callback for process sigkill timeout */
229 static void process_sigkill( void *private )
231 struct process *process = private;
233 process->sigkill_timeout = NULL;
234 kill( process->unix_pid, SIGKILL );
235 process_died( process );
238 /* start the sigkill timer for a process upon exit */
239 static void start_sigkill_timer( struct process *process )
241 grab_object( process );
242 if (process->unix_pid != -1 && process->msg_fd)
244 struct timeval when = current_time;
246 add_timeout( &when, 1000 );
247 process->sigkill_timeout = add_timeout_user( &when, process_sigkill, process );
249 else process_died( process );
252 /* create a new process and its main thread */
253 /* if the function fails the fd is closed */
254 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
256 struct process *process;
257 struct thread *thread = NULL;
258 int request_pipe[2];
260 if (!(process = alloc_object( &process_ops )))
262 close( fd );
263 goto error;
265 process->parent = NULL;
266 process->debugger = NULL;
267 process->handles = NULL;
268 process->msg_fd = NULL;
269 process->sigkill_timeout = NULL;
270 process->unix_pid = -1;
271 process->exit_code = STILL_ACTIVE;
272 process->running_threads = 0;
273 process->priority = PROCESS_PRIOCLASS_NORMAL;
274 process->affinity = 1;
275 process->suspend = 0;
276 process->create_flags = 0;
277 process->console = NULL;
278 process->startup_state = STARTUP_IN_PROGRESS;
279 process->startup_info = NULL;
280 process->idle_event = NULL;
281 process->queue = NULL;
282 process->peb = NULL;
283 process->ldt_copy = NULL;
284 process->winstation = 0;
285 process->desktop = 0;
286 process->token = token_create_admin();
287 list_init( &process->thread_list );
288 list_init( &process->locks );
289 list_init( &process->classes );
290 list_init( &process->dlls );
292 process->start_time = current_time;
293 process->end_time.tv_sec = process->end_time.tv_usec = 0;
294 list_add_head( &process_list, &process->entry );
296 if (!(process->id = process->group_id = alloc_ptid( process )))
298 close( fd );
299 goto error;
301 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
303 /* create the handle table */
304 if (!parent_thread) process->handles = alloc_handle_table( process, 0 );
305 else
307 struct process *parent = parent_thread->process;
308 process->parent = (struct process *)grab_object( parent );
309 process->handles = inherit_all ? copy_handle_table( process, parent )
310 : alloc_handle_table( process, 0 );
312 if (!process->handles) goto error;
314 /* create the main thread */
315 if (pipe( request_pipe ) == -1)
317 file_set_error();
318 goto error;
320 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
322 close( request_pipe[0] );
323 close( request_pipe[1] );
324 goto error;
326 close( request_pipe[1] );
327 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
329 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
330 release_object( process );
331 return thread;
333 error:
334 if (process) release_object( process );
335 /* if we failed to start our first process, close everything down */
336 if (!running_processes) close_master_socket();
337 return NULL;
340 /* initialize the current process and fill in the request */
341 data_size_t init_process( struct thread *thread )
343 struct process *process = thread->process;
344 struct startup_info *info = process->startup_info;
346 if (!info) return 0;
347 return info->data_size;
350 /* destroy a process when its refcount is 0 */
351 static void process_destroy( struct object *obj )
353 struct process *process = (struct process *)obj;
354 assert( obj->ops == &process_ops );
356 /* we can't have a thread remaining */
357 assert( list_empty( &process->thread_list ));
359 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
361 set_process_startup_state( process, STARTUP_ABORTED );
362 if (process->console) release_object( process->console );
363 if (process->parent) release_object( process->parent );
364 if (process->msg_fd) release_object( process->msg_fd );
365 list_remove( &process->entry );
366 if (process->idle_event) release_object( process->idle_event );
367 if (process->queue) release_object( process->queue );
368 if (process->id) free_ptid( process->id );
369 if (process->token) release_object( process->token );
372 /* dump a process on stdout for debugging purposes */
373 static void process_dump( struct object *obj, int verbose )
375 struct process *process = (struct process *)obj;
376 assert( obj->ops == &process_ops );
378 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
381 static int process_signaled( struct object *obj, struct thread *thread )
383 struct process *process = (struct process *)obj;
384 return !process->running_threads;
387 static unsigned int process_map_access( struct object *obj, unsigned int access )
389 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
390 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
391 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
392 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
393 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
396 static void process_poll_event( struct fd *fd, int event )
398 struct process *process = get_fd_user( fd );
399 assert( process->obj.ops == &process_ops );
401 if (event & (POLLERR | POLLHUP))
403 release_object( process->msg_fd );
404 process->msg_fd = NULL;
405 if (process->sigkill_timeout) /* already waiting for it to die */
407 remove_timeout_user( process->sigkill_timeout );
408 process->sigkill_timeout = NULL;
409 process_died( process );
411 else kill_process( process, NULL, 0 );
413 else if (event & POLLIN) receive_fd( process );
416 static void startup_info_destroy( struct object *obj )
418 struct startup_info *info = (struct startup_info *)obj;
419 assert( obj->ops == &startup_info_ops );
420 if (info->data) free( info->data );
421 if (info->exe_file) release_object( info->exe_file );
422 if (info->process) release_object( info->process );
425 static void startup_info_dump( struct object *obj, int verbose )
427 struct startup_info *info = (struct startup_info *)obj;
428 assert( obj->ops == &startup_info_ops );
430 fprintf( stderr, "Startup info in=%p out=%p err=%p\n",
431 info->hstdin, info->hstdout, info->hstderr );
434 static int startup_info_signaled( struct object *obj, struct thread *thread )
436 struct startup_info *info = (struct startup_info *)obj;
437 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
440 /* get a process from an id (and increment the refcount) */
441 struct process *get_process_from_id( process_id_t id )
443 struct object *obj = get_ptid_entry( id );
445 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
446 set_error( STATUS_INVALID_PARAMETER );
447 return NULL;
450 /* get a process from a handle (and increment the refcount) */
451 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
453 return (struct process *)get_handle_obj( current->process, handle,
454 access, &process_ops );
457 /* find a dll from its base address */
458 static inline struct process_dll *find_process_dll( struct process *process, void *base )
460 struct process_dll *dll;
462 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
464 if (dll->base == base) return dll;
466 return NULL;
469 /* add a dll to a process list */
470 static struct process_dll *process_load_dll( struct process *process, struct file *file,
471 void *base, const WCHAR *filename, data_size_t name_len )
473 struct process_dll *dll;
475 /* make sure we don't already have one with the same base address */
476 if (find_process_dll( process, base ))
478 set_error( STATUS_INVALID_PARAMETER );
479 return NULL;
482 if ((dll = mem_alloc( sizeof(*dll) )))
484 dll->file = NULL;
485 dll->base = base;
486 dll->filename = NULL;
487 dll->namelen = name_len;
488 if (name_len && !(dll->filename = memdup( filename, name_len )))
490 free( dll );
491 return NULL;
493 if (file) dll->file = (struct file *)grab_object( file );
494 list_add_tail( &process->dlls, &dll->entry );
496 return dll;
499 /* remove a dll from a process list */
500 static void process_unload_dll( struct process *process, void *base )
502 struct process_dll *dll = find_process_dll( process, base );
504 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
506 if (dll->file) release_object( dll->file );
507 if (dll->filename) free( dll->filename );
508 list_remove( &dll->entry );
509 free( dll );
510 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
512 else set_error( STATUS_INVALID_PARAMETER );
515 /* kill all processes */
516 void kill_all_processes( struct process *skip, int exit_code )
518 for (;;)
520 struct process *process;
522 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
524 if (process == skip) continue;
525 if (process->running_threads) break;
527 if (&process->entry == &process_list) break; /* no process found */
528 kill_process( process, NULL, exit_code );
532 /* kill all processes being attached to a console renderer */
533 void kill_console_processes( struct thread *renderer, int exit_code )
535 for (;;) /* restart from the beginning of the list every time */
537 struct process *process;
539 /* find the first process being attached to 'renderer' and still running */
540 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
542 if (process == renderer->process) continue;
543 if (!process->running_threads) continue;
544 if (process->console && process->console->renderer == renderer) break;
546 if (&process->entry == &process_list) break; /* no process found */
547 kill_process( process, NULL, exit_code );
551 /* a process has been killed (i.e. its last thread died) */
552 static void process_killed( struct process *process )
554 struct handle_table *handles;
555 struct list *ptr;
557 assert( list_empty( &process->thread_list ));
558 process->end_time = current_time;
559 close_process_desktop( process );
560 handles = process->handles;
561 process->handles = NULL;
562 if (handles) release_object( handles );
564 /* close the console attached to this process, if any */
565 free_console( process );
567 while ((ptr = list_head( &process->dlls )))
569 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
570 if (dll->file) release_object( dll->file );
571 if (dll->filename) free( dll->filename );
572 list_remove( &dll->entry );
573 free( dll );
575 destroy_process_classes( process );
576 remove_process_locks( process );
577 set_process_startup_state( process, STARTUP_ABORTED );
578 start_sigkill_timer( process );
579 wake_up( &process->obj, 0 );
582 /* add a thread to a process running threads list */
583 void add_process_thread( struct process *process, struct thread *thread )
585 list_add_tail( &process->thread_list, &thread->proc_entry );
586 if (!process->running_threads++) running_processes++;
587 grab_object( thread );
590 /* remove a thread from a process running threads list */
591 void remove_process_thread( struct process *process, struct thread *thread )
593 assert( process->running_threads > 0 );
594 assert( !list_empty( &process->thread_list ));
596 list_remove( &thread->proc_entry );
598 if (!--process->running_threads)
600 /* we have removed the last running thread, exit the process */
601 process->exit_code = thread->exit_code;
602 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
603 process_killed( process );
605 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
606 release_object( thread );
609 /* suspend all the threads of a process */
610 void suspend_process( struct process *process )
612 if (!process->suspend++)
614 struct list *ptr, *next;
616 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
618 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
619 if (!thread->suspend) stop_thread( thread );
624 /* resume all the threads of a process */
625 void resume_process( struct process *process )
627 assert (process->suspend > 0);
628 if (!--process->suspend)
630 struct list *ptr, *next;
632 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
634 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
635 if (!thread->suspend) wake_thread( thread );
640 /* kill a process on the spot */
641 void kill_process( struct process *process, struct thread *skip, int exit_code )
643 struct list *ptr, *next;
645 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
647 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
649 if (exit_code) thread->exit_code = exit_code;
650 if (thread != skip) kill_thread( thread, 1 );
654 /* kill all processes being debugged by a given thread */
655 void kill_debugged_processes( struct thread *debugger, int exit_code )
657 for (;;) /* restart from the beginning of the list every time */
659 struct process *process;
661 /* find the first process being debugged by 'debugger' and still running */
662 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
664 if (!process->running_threads) continue;
665 if (process->debugger == debugger) break;
667 if (&process->entry == &process_list) break; /* no process found */
668 process->debugger = NULL;
669 kill_process( process, NULL, exit_code );
674 /* trigger a breakpoint event in a given process */
675 void break_process( struct process *process )
677 struct thread *thread;
679 suspend_process( process );
681 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
683 if (thread->context) /* inside an exception event already */
685 break_thread( thread );
686 goto done;
689 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
690 else set_error( STATUS_ACCESS_DENIED );
691 done:
692 resume_process( process );
696 /* detach a debugger from all its debuggees */
697 void detach_debugged_processes( struct thread *debugger )
699 struct process *process;
701 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
703 if (process->debugger == debugger && process->running_threads)
705 debugger_detach( process, debugger );
711 void enum_processes( int (*cb)(struct process*, void*), void *user )
713 struct list *ptr, *next;
715 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
717 struct process *process = LIST_ENTRY( ptr, struct process, entry );
718 if ((cb)(process, user)) break;
722 /* set the debugged flag in the process PEB */
723 int set_process_debug_flag( struct process *process, int flag )
725 char data = (flag != 0);
727 /* BeingDebugged flag is the byte at offset 2 in the PEB */
728 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
731 /* take a snapshot of currently running processes */
732 struct process_snapshot *process_snap( int *count )
734 struct process_snapshot *snapshot, *ptr;
735 struct process *process;
737 if (!running_processes) return NULL;
738 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
739 return NULL;
740 ptr = snapshot;
741 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
743 if (!process->running_threads) continue;
744 ptr->process = process;
745 ptr->threads = process->running_threads;
746 ptr->count = process->obj.refcount;
747 ptr->priority = process->priority;
748 ptr->handles = get_handle_table_count(process);
749 grab_object( process );
750 ptr++;
753 if (!(*count = ptr - snapshot))
755 free( snapshot );
756 snapshot = NULL;
758 return snapshot;
761 /* take a snapshot of the modules of a process */
762 struct module_snapshot *module_snap( struct process *process, int *count )
764 struct module_snapshot *snapshot, *ptr;
765 struct process_dll *dll;
766 int total = 0;
768 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
769 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
771 ptr = snapshot;
772 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
774 ptr->base = dll->base;
775 ptr->size = dll->size;
776 ptr->namelen = dll->namelen;
777 ptr->filename = memdup( dll->filename, dll->namelen );
778 ptr++;
780 *count = total;
781 return snapshot;
785 /* create a new process */
786 DECL_HANDLER(new_process)
788 struct startup_info *info;
789 struct thread *thread;
790 struct process *process;
791 struct process *parent = current->process;
792 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
794 if (socket_fd == -1)
796 set_error( STATUS_INVALID_PARAMETER );
797 return;
799 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
801 set_error( STATUS_INVALID_HANDLE );
802 close( socket_fd );
803 return;
806 /* build the startup info for a new process */
807 if (!(info = alloc_object( &startup_info_ops ))) return;
808 info->hstdin = req->hstdin;
809 info->hstdout = req->hstdout;
810 info->hstderr = req->hstderr;
811 info->exe_file = NULL;
812 info->process = NULL;
813 info->data_size = get_req_data_size();
814 info->data = NULL;
816 if (req->exe_file &&
817 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
818 goto done;
820 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
822 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
823 process = thread->process;
824 process->create_flags = req->create_flags;
825 process->startup_info = (struct startup_info *)grab_object( info );
827 /* connect to the window station */
828 connect_process_winstation( process, current );
830 /* thread will be actually suspended in init_done */
831 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
833 /* set the process console */
834 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
836 /* FIXME: some better error checking should be done...
837 * like if hConOut and hConIn are console handles, then they should be on the same
838 * physical console
840 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
843 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
845 info->hstdin = duplicate_handle( parent, req->hstdin, process,
846 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
847 info->hstdout = duplicate_handle( parent, req->hstdout, process,
848 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
849 info->hstderr = duplicate_handle( parent, req->hstderr, process,
850 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
851 /* some handles above may have been invalid; this is not an error */
852 if (get_error() == STATUS_INVALID_HANDLE ||
853 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
856 /* attach to the debugger if requested */
857 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
858 set_process_debugger( process, current );
859 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
860 set_process_debugger( process, parent->debugger );
862 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
863 process->group_id = parent->group_id;
865 info->process = (struct process *)grab_object( process );
866 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
867 reply->pid = get_process_id( process );
868 reply->tid = get_thread_id( thread );
869 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
870 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
872 done:
873 release_object( info );
876 /* Retrieve information about a newly started process */
877 DECL_HANDLER(get_new_process_info)
879 struct startup_info *info;
881 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
882 0, &startup_info_ops )))
884 reply->success = is_process_init_done( info->process );
885 reply->exit_code = info->process->exit_code;
886 release_object( info );
890 /* Retrieve the new process startup info */
891 DECL_HANDLER(get_startup_info)
893 struct process *process = current->process;
894 struct startup_info *info = process->startup_info;
895 data_size_t size;
897 if (!info) return;
899 if (info->exe_file &&
900 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
902 reply->hstdin = info->hstdin;
903 reply->hstdout = info->hstdout;
904 reply->hstderr = info->hstderr;
906 /* we return the data directly without making a copy so this can only be called once */
907 size = info->data_size;
908 if (size > get_reply_max_size()) size = get_reply_max_size();
909 set_reply_data_ptr( info->data, size );
910 info->data = NULL;
911 info->data_size = 0;
914 /* signal the end of the process initialization */
915 DECL_HANDLER(init_process_done)
917 struct process_dll *dll;
918 struct process *process = current->process;
920 if (is_process_init_done(process))
922 set_error( STATUS_INVALID_PARAMETER );
923 return;
925 if (!(dll = find_process_dll( process, req->module )))
927 set_error( STATUS_DLL_NOT_FOUND );
928 return;
931 /* main exe is the first in the dll list */
932 list_remove( &dll->entry );
933 list_add_head( &process->dlls, &dll->entry );
935 generate_startup_debug_events( process, req->entry );
936 set_process_startup_state( process, STARTUP_DONE );
938 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
939 if (current->suspend + process->suspend > 0) stop_thread( current );
940 if (process->debugger) set_process_debug_flag( process, 1 );
943 /* open a handle to a process */
944 DECL_HANDLER(open_process)
946 struct process *process = get_process_from_id( req->pid );
947 reply->handle = 0;
948 if (process)
950 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
951 release_object( process );
955 /* terminate a process */
956 DECL_HANDLER(terminate_process)
958 struct process *process;
960 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
962 reply->self = (current->process == process);
963 kill_process( process, current, req->exit_code );
964 release_object( process );
968 /* fetch information about a process */
969 DECL_HANDLER(get_process_info)
971 struct process *process;
973 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
975 reply->pid = get_process_id( process );
976 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
977 reply->exit_code = process->exit_code;
978 reply->priority = process->priority;
979 reply->affinity = process->affinity;
980 reply->peb = process->peb;
981 reply->start_time.sec = process->start_time.tv_sec;
982 reply->start_time.usec = process->start_time.tv_usec;
983 reply->end_time.sec = process->end_time.tv_sec;
984 reply->end_time.usec = process->end_time.tv_usec;
985 release_object( process );
989 /* set information about a process */
990 DECL_HANDLER(set_process_info)
992 struct process *process;
994 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
996 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
997 if (req->mask & SET_PROCESS_INFO_AFFINITY)
999 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1000 else process->affinity = req->affinity;
1002 release_object( process );
1006 /* read data from a process address space */
1007 DECL_HANDLER(read_process_memory)
1009 struct process *process;
1010 data_size_t len = get_reply_max_size();
1012 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1014 if (len)
1016 char *buffer = mem_alloc( len );
1017 if (buffer)
1019 if (read_process_memory( process, req->addr, len, buffer ))
1020 set_reply_data_ptr( buffer, len );
1021 else
1022 free( buffer );
1025 release_object( process );
1028 /* write data to a process address space */
1029 DECL_HANDLER(write_process_memory)
1031 struct process *process;
1033 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1035 data_size_t len = get_req_data_size();
1036 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1037 else set_error( STATUS_INVALID_PARAMETER );
1038 release_object( process );
1042 /* notify the server that a dll has been loaded */
1043 DECL_HANDLER(load_dll)
1045 struct process_dll *dll;
1046 struct file *file = NULL;
1048 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1049 return;
1051 if ((dll = process_load_dll( current->process, file, req->base,
1052 get_req_data(), get_req_data_size() )))
1054 dll->size = req->size;
1055 dll->dbg_offset = req->dbg_offset;
1056 dll->dbg_size = req->dbg_size;
1057 dll->name = req->name;
1058 /* only generate event if initialization is done */
1059 if (is_process_init_done( current->process ))
1060 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1062 if (file) release_object( file );
1065 /* notify the server that a dll is being unloaded */
1066 DECL_HANDLER(unload_dll)
1068 process_unload_dll( current->process, req->base );
1071 /* retrieve information about a module in a process */
1072 DECL_HANDLER(get_dll_info)
1074 struct process *process;
1076 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1078 struct process_dll *dll = find_process_dll( process, req->base_address );
1080 if (dll)
1082 reply->size = dll->size;
1083 reply->entry_point = NULL; /* FIXME */
1084 if (dll->filename)
1086 data_size_t len = min( dll->namelen, get_reply_max_size() );
1087 set_reply_data( dll->filename, len );
1090 else
1091 set_error( STATUS_DLL_NOT_FOUND );
1093 release_object( process );
1097 /* retrieve the process idle event */
1098 DECL_HANDLER(get_process_idle_event)
1100 struct process *process;
1102 reply->event = 0;
1103 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1105 if (process->idle_event && process != current->process)
1106 reply->event = alloc_handle( current->process, process->idle_event,
1107 EVENT_ALL_ACCESS, 0 );
1108 release_object( process );