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
22 #include "wine/port.h"
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
41 #define WIN32_NO_STATUS
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_open_file, /* open_file */
79 no_close_handle, /* close_handle */
80 process_destroy /* destroy */
83 static const struct fd_ops process_fd_ops =
85 NULL, /* get_poll_events */
86 process_poll_event, /* poll_event */
88 NULL, /* get_fd_type */
90 NULL, /* queue_async */
91 NULL, /* reselect_async */
92 NULL /* cancel async */
95 /* process startup info */
99 struct object obj; /* object header */
100 obj_handle_t hstdin; /* handle for stdin */
101 obj_handle_t hstdout; /* handle for stdout */
102 obj_handle_t hstderr; /* handle for stderr */
103 struct file *exe_file; /* file handle for main exe */
104 struct process *process; /* created process */
105 data_size_t data_size; /* size of startup data */
106 void *data; /* data for startup info */
109 static void startup_info_dump( struct object *obj, int verbose );
110 static int startup_info_signaled( struct object *obj, struct thread *thread );
111 static void startup_info_destroy( struct object *obj );
113 static const struct object_ops startup_info_ops =
115 sizeof(struct startup_info), /* size */
116 startup_info_dump, /* dump */
117 add_queue, /* add_queue */
118 remove_queue, /* remove_queue */
119 startup_info_signaled, /* signaled */
120 no_satisfied, /* satisfied */
121 no_signal, /* signal */
122 no_get_fd, /* get_fd */
123 no_map_access, /* map_access */
124 no_lookup_name, /* lookup_name */
125 no_open_file, /* open_file */
126 no_close_handle, /* close_handle */
127 startup_info_destroy /* destroy */
133 void *ptr; /* entry ptr */
134 unsigned int next; /* next free entry */
137 static struct ptid_entry *ptid_entries; /* array of ptid entries */
138 static unsigned int used_ptid_entries; /* number of entries in use */
139 static unsigned int alloc_ptid_entries; /* number of allocated entries */
140 static unsigned int next_free_ptid; /* next free entry */
141 static unsigned int last_free_ptid; /* last free entry */
143 #define PTID_OFFSET 8 /* offset for first ptid value */
145 /* allocate a new process or thread id */
146 unsigned int alloc_ptid( void *ptr )
148 struct ptid_entry *entry;
151 if (used_ptid_entries < alloc_ptid_entries)
153 id = used_ptid_entries + PTID_OFFSET;
154 entry = &ptid_entries[used_ptid_entries++];
156 else if (next_free_ptid)
159 entry = &ptid_entries[id - PTID_OFFSET];
160 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
162 else /* need to grow the array */
164 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
165 if (!count) count = 64;
166 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
168 set_error( STATUS_NO_MEMORY );
171 ptid_entries = entry;
172 alloc_ptid_entries = count;
173 id = used_ptid_entries + PTID_OFFSET;
174 entry = &ptid_entries[used_ptid_entries++];
181 /* free a process or thread id */
182 void free_ptid( unsigned int id )
184 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
189 /* append to end of free list so that we don't reuse it too early */
190 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
191 else next_free_ptid = id;
196 /* retrieve the pointer corresponding to a process or thread id */
197 void *get_ptid_entry( unsigned int id )
199 if (id < PTID_OFFSET) return NULL;
200 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
201 return ptid_entries[id - PTID_OFFSET].ptr;
204 /* return the main thread of the process */
205 struct thread *get_process_first_thread( struct process *process )
207 struct list *ptr = list_head( &process->thread_list );
208 if (!ptr) return NULL;
209 return LIST_ENTRY( ptr, struct thread, proc_entry );
212 /* set the state of the process startup info */
213 static void set_process_startup_state( struct process *process, enum startup_state state )
215 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
216 if (process->startup_info)
218 wake_up( &process->startup_info->obj, 0 );
219 release_object( process->startup_info );
220 process->startup_info = NULL;
224 /* final cleanup once we are sure a process is really dead */
225 static void process_died( struct process *process )
227 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
228 release_object( process );
229 if (!--running_processes) close_master_socket();
232 /* callback for process sigkill timeout */
233 static void process_sigkill( void *private )
235 struct process *process = private;
237 process->sigkill_timeout = NULL;
238 kill( process->unix_pid, SIGKILL );
239 process_died( process );
242 /* start the sigkill timer for a process upon exit */
243 static void start_sigkill_timer( struct process *process )
245 grab_object( process );
246 if (process->unix_pid != -1 && process->msg_fd)
248 struct timeval when = current_time;
250 add_timeout( &when, 1000 );
251 process->sigkill_timeout = add_timeout_user( &when, process_sigkill, process );
253 else process_died( process );
256 /* create a new process and its main thread */
257 /* if the function fails the fd is closed */
258 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
260 struct process *process;
261 struct thread *thread = NULL;
264 if (!(process = alloc_object( &process_ops )))
269 process->parent = NULL;
270 process->debugger = NULL;
271 process->handles = NULL;
272 process->msg_fd = NULL;
273 process->sigkill_timeout = NULL;
274 process->unix_pid = -1;
275 process->exit_code = STILL_ACTIVE;
276 process->running_threads = 0;
277 process->priority = PROCESS_PRIOCLASS_NORMAL;
278 process->affinity = 1;
279 process->suspend = 0;
280 process->create_flags = 0;
281 process->console = NULL;
282 process->startup_state = STARTUP_IN_PROGRESS;
283 process->startup_info = NULL;
284 process->idle_event = NULL;
285 process->queue = NULL;
287 process->ldt_copy = NULL;
288 process->winstation = 0;
289 process->desktop = 0;
290 process->token = token_create_admin();
291 process->trace_data = 0;
292 list_init( &process->thread_list );
293 list_init( &process->locks );
294 list_init( &process->classes );
295 list_init( &process->dlls );
297 process->start_time = current_time;
298 process->end_time.tv_sec = process->end_time.tv_usec = 0;
299 list_add_head( &process_list, &process->entry );
301 if (!(process->id = process->group_id = alloc_ptid( process )))
306 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
308 /* create the handle table */
309 if (!parent_thread) process->handles = alloc_handle_table( process, 0 );
312 struct process *parent = parent_thread->process;
313 process->parent = (struct process *)grab_object( parent );
314 process->handles = inherit_all ? copy_handle_table( process, parent )
315 : alloc_handle_table( process, 0 );
317 if (!process->handles) goto error;
319 /* create the main thread */
320 if (pipe( request_pipe ) == -1)
325 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
327 close( request_pipe[0] );
328 close( request_pipe[1] );
331 close( request_pipe[1] );
332 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
334 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
335 release_object( process );
339 if (process) release_object( process );
340 /* if we failed to start our first process, close everything down */
341 if (!running_processes) close_master_socket();
345 /* initialize the current process and fill in the request */
346 data_size_t init_process( struct thread *thread )
348 struct process *process = thread->process;
349 struct startup_info *info = process->startup_info;
351 init_process_tracing( process );
353 return info->data_size;
356 /* destroy a process when its refcount is 0 */
357 static void process_destroy( struct object *obj )
359 struct process *process = (struct process *)obj;
360 assert( obj->ops == &process_ops );
362 /* we can't have a thread remaining */
363 assert( list_empty( &process->thread_list ));
365 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
367 set_process_startup_state( process, STARTUP_ABORTED );
368 if (process->console) release_object( process->console );
369 if (process->parent) release_object( process->parent );
370 if (process->msg_fd) release_object( process->msg_fd );
371 list_remove( &process->entry );
372 if (process->idle_event) release_object( process->idle_event );
373 if (process->queue) release_object( process->queue );
374 if (process->id) free_ptid( process->id );
375 if (process->token) release_object( process->token );
378 /* dump a process on stdout for debugging purposes */
379 static void process_dump( struct object *obj, int verbose )
381 struct process *process = (struct process *)obj;
382 assert( obj->ops == &process_ops );
384 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
387 static int process_signaled( struct object *obj, struct thread *thread )
389 struct process *process = (struct process *)obj;
390 return !process->running_threads;
393 static unsigned int process_map_access( struct object *obj, unsigned int access )
395 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
396 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
397 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
398 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
399 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
402 static void process_poll_event( struct fd *fd, int event )
404 struct process *process = get_fd_user( fd );
405 assert( process->obj.ops == &process_ops );
407 if (event & (POLLERR | POLLHUP))
409 release_object( process->msg_fd );
410 process->msg_fd = NULL;
411 if (process->sigkill_timeout) /* already waiting for it to die */
413 remove_timeout_user( process->sigkill_timeout );
414 process->sigkill_timeout = NULL;
415 process_died( process );
417 else kill_process( process, 0 );
419 else if (event & POLLIN) receive_fd( process );
422 static void startup_info_destroy( struct object *obj )
424 struct startup_info *info = (struct startup_info *)obj;
425 assert( obj->ops == &startup_info_ops );
427 if (info->exe_file) release_object( info->exe_file );
428 if (info->process) release_object( info->process );
431 static void startup_info_dump( struct object *obj, int verbose )
433 struct startup_info *info = (struct startup_info *)obj;
434 assert( obj->ops == &startup_info_ops );
436 fprintf( stderr, "Startup info in=%p out=%p err=%p\n",
437 info->hstdin, info->hstdout, info->hstderr );
440 static int startup_info_signaled( struct object *obj, struct thread *thread )
442 struct startup_info *info = (struct startup_info *)obj;
443 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
446 /* get a process from an id (and increment the refcount) */
447 struct process *get_process_from_id( process_id_t id )
449 struct object *obj = get_ptid_entry( id );
451 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
452 set_error( STATUS_INVALID_PARAMETER );
456 /* get a process from a handle (and increment the refcount) */
457 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
459 return (struct process *)get_handle_obj( current->process, handle,
460 access, &process_ops );
463 /* find a dll from its base address */
464 static inline struct process_dll *find_process_dll( struct process *process, void *base )
466 struct process_dll *dll;
468 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
470 if (dll->base == base) return dll;
475 /* add a dll to a process list */
476 static struct process_dll *process_load_dll( struct process *process, struct file *file,
477 void *base, const WCHAR *filename, data_size_t name_len )
479 struct process_dll *dll;
481 /* make sure we don't already have one with the same base address */
482 if (find_process_dll( process, base ))
484 set_error( STATUS_INVALID_PARAMETER );
488 if ((dll = mem_alloc( sizeof(*dll) )))
492 dll->filename = NULL;
493 dll->namelen = name_len;
494 if (name_len && !(dll->filename = memdup( filename, name_len )))
499 if (file) dll->file = grab_file_unless_removable( file );
500 list_add_tail( &process->dlls, &dll->entry );
505 /* remove a dll from a process list */
506 static void process_unload_dll( struct process *process, void *base )
508 struct process_dll *dll = find_process_dll( process, base );
510 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
512 if (dll->file) release_object( dll->file );
513 free( dll->filename );
514 list_remove( &dll->entry );
516 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
518 else set_error( STATUS_INVALID_PARAMETER );
521 /* terminate a process with the given exit code */
522 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
526 if (skip && skip->process == process) /* move it to the end of the list */
528 assert( skip->state != TERMINATED );
529 list_remove( &skip->proc_entry );
530 list_add_tail( &process->thread_list, &skip->proc_entry );
533 grab_object( process ); /* make sure it doesn't get freed when threads die */
534 while ((ptr = list_head( &process->thread_list )))
536 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
538 if (exit_code) thread->exit_code = exit_code;
539 if (thread == skip) break;
540 kill_thread( thread, 1 );
542 release_object( process );
545 /* kill all processes */
546 void kill_all_processes( struct process *skip, int exit_code )
550 struct process *process;
552 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
554 if (process == skip) continue;
555 if (process->running_threads) break;
557 if (&process->entry == &process_list) break; /* no process found */
558 terminate_process( process, NULL, exit_code );
562 /* kill all processes being attached to a console renderer */
563 void kill_console_processes( struct thread *renderer, int exit_code )
565 for (;;) /* restart from the beginning of the list every time */
567 struct process *process;
569 /* find the first process being attached to 'renderer' and still running */
570 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
572 if (process == renderer->process) continue;
573 if (!process->running_threads) continue;
574 if (process->console && process->console->renderer == renderer) break;
576 if (&process->entry == &process_list) break; /* no process found */
577 terminate_process( process, NULL, exit_code );
581 /* a process has been killed (i.e. its last thread died) */
582 static void process_killed( struct process *process )
584 struct handle_table *handles;
587 assert( list_empty( &process->thread_list ));
588 process->end_time = current_time;
589 close_process_desktop( process );
590 handles = process->handles;
591 process->handles = NULL;
592 if (handles) release_object( handles );
594 /* close the console attached to this process, if any */
595 free_console( process );
597 while ((ptr = list_head( &process->dlls )))
599 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
600 if (dll->file) release_object( dll->file );
601 free( dll->filename );
602 list_remove( &dll->entry );
605 destroy_process_classes( process );
606 remove_process_locks( process );
607 set_process_startup_state( process, STARTUP_ABORTED );
608 finish_process_tracing( process );
609 start_sigkill_timer( process );
610 wake_up( &process->obj, 0 );
613 /* add a thread to a process running threads list */
614 void add_process_thread( struct process *process, struct thread *thread )
616 list_add_tail( &process->thread_list, &thread->proc_entry );
617 if (!process->running_threads++) running_processes++;
618 grab_object( thread );
621 /* remove a thread from a process running threads list */
622 void remove_process_thread( struct process *process, struct thread *thread )
624 assert( process->running_threads > 0 );
625 assert( !list_empty( &process->thread_list ));
627 list_remove( &thread->proc_entry );
629 if (!--process->running_threads)
631 /* we have removed the last running thread, exit the process */
632 process->exit_code = thread->exit_code;
633 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
634 process_killed( process );
636 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
637 release_object( thread );
640 /* suspend all the threads of a process */
641 void suspend_process( struct process *process )
643 if (!process->suspend++)
645 struct list *ptr, *next;
647 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
649 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
650 if (!thread->suspend) stop_thread( thread );
655 /* resume all the threads of a process */
656 void resume_process( struct process *process )
658 assert (process->suspend > 0);
659 if (!--process->suspend)
661 struct list *ptr, *next;
663 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
665 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
666 if (!thread->suspend) wake_thread( thread );
671 /* kill a process on the spot */
672 void kill_process( struct process *process, int violent_death )
674 if (violent_death) terminate_process( process, NULL, 1 );
679 grab_object( process ); /* make sure it doesn't get freed when threads die */
680 while ((ptr = list_head( &process->thread_list )))
682 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
683 kill_thread( thread, 0 );
685 release_object( process );
689 /* kill all processes being debugged by a given thread */
690 void kill_debugged_processes( struct thread *debugger, int exit_code )
692 for (;;) /* restart from the beginning of the list every time */
694 struct process *process;
696 /* find the first process being debugged by 'debugger' and still running */
697 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
699 if (!process->running_threads) continue;
700 if (process->debugger == debugger) break;
702 if (&process->entry == &process_list) break; /* no process found */
703 process->debugger = NULL;
704 terminate_process( process, NULL, exit_code );
709 /* trigger a breakpoint event in a given process */
710 void break_process( struct process *process )
712 struct thread *thread;
714 suspend_process( process );
716 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
718 if (thread->context) /* inside an exception event already */
720 break_thread( thread );
724 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
725 else set_error( STATUS_ACCESS_DENIED );
727 resume_process( process );
731 /* detach a debugger from all its debuggees */
732 void detach_debugged_processes( struct thread *debugger )
734 struct process *process;
736 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
738 if (process->debugger == debugger && process->running_threads)
740 debugger_detach( process, debugger );
746 void enum_processes( int (*cb)(struct process*, void*), void *user )
748 struct list *ptr, *next;
750 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
752 struct process *process = LIST_ENTRY( ptr, struct process, entry );
753 if ((cb)(process, user)) break;
757 /* set the debugged flag in the process PEB */
758 int set_process_debug_flag( struct process *process, int flag )
760 char data = (flag != 0);
762 /* BeingDebugged flag is the byte at offset 2 in the PEB */
763 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
766 /* take a snapshot of currently running processes */
767 struct process_snapshot *process_snap( int *count )
769 struct process_snapshot *snapshot, *ptr;
770 struct process *process;
772 if (!running_processes) return NULL;
773 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
776 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
778 if (!process->running_threads) continue;
779 ptr->process = process;
780 ptr->threads = process->running_threads;
781 ptr->count = process->obj.refcount;
782 ptr->priority = process->priority;
783 ptr->handles = get_handle_table_count(process);
784 grab_object( process );
788 if (!(*count = ptr - snapshot))
796 /* take a snapshot of the modules of a process */
797 struct module_snapshot *module_snap( struct process *process, int *count )
799 struct module_snapshot *snapshot, *ptr;
800 struct process_dll *dll;
803 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
804 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
807 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
809 ptr->base = dll->base;
810 ptr->size = dll->size;
811 ptr->namelen = dll->namelen;
812 ptr->filename = memdup( dll->filename, dll->namelen );
820 /* create a new process */
821 DECL_HANDLER(new_process)
823 struct startup_info *info;
824 struct thread *thread;
825 struct process *process;
826 struct process *parent = current->process;
827 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
831 set_error( STATUS_INVALID_PARAMETER );
834 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
836 set_error( STATUS_INVALID_HANDLE );
841 /* build the startup info for a new process */
842 if (!(info = alloc_object( &startup_info_ops ))) return;
843 info->hstdin = req->hstdin;
844 info->hstdout = req->hstdout;
845 info->hstderr = req->hstderr;
846 info->exe_file = NULL;
847 info->process = NULL;
848 info->data_size = get_req_data_size();
852 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
855 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
857 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
858 process = thread->process;
859 process->create_flags = req->create_flags;
860 process->startup_info = (struct startup_info *)grab_object( info );
862 /* connect to the window station */
863 connect_process_winstation( process, current );
865 /* thread will be actually suspended in init_done */
866 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
868 /* set the process console */
869 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
871 /* FIXME: some better error checking should be done...
872 * like if hConOut and hConIn are console handles, then they should be on the same
875 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
878 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
880 info->hstdin = duplicate_handle( parent, req->hstdin, process,
881 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
882 info->hstdout = duplicate_handle( parent, req->hstdout, process,
883 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
884 info->hstderr = duplicate_handle( parent, req->hstderr, process,
885 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
886 /* some handles above may have been invalid; this is not an error */
887 if (get_error() == STATUS_INVALID_HANDLE ||
888 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
891 /* attach to the debugger if requested */
892 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
893 set_process_debugger( process, current );
894 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
895 set_process_debugger( process, parent->debugger );
897 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
898 process->group_id = parent->group_id;
900 info->process = (struct process *)grab_object( process );
901 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
902 reply->pid = get_process_id( process );
903 reply->tid = get_thread_id( thread );
904 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
905 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
908 release_object( info );
911 /* Retrieve information about a newly started process */
912 DECL_HANDLER(get_new_process_info)
914 struct startup_info *info;
916 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
917 0, &startup_info_ops )))
919 reply->success = is_process_init_done( info->process );
920 reply->exit_code = info->process->exit_code;
921 release_object( info );
925 /* Retrieve the new process startup info */
926 DECL_HANDLER(get_startup_info)
928 struct process *process = current->process;
929 struct startup_info *info = process->startup_info;
934 if (info->exe_file &&
935 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
937 reply->hstdin = info->hstdin;
938 reply->hstdout = info->hstdout;
939 reply->hstderr = info->hstderr;
941 /* we return the data directly without making a copy so this can only be called once */
942 size = info->data_size;
943 if (size > get_reply_max_size()) size = get_reply_max_size();
944 set_reply_data_ptr( info->data, size );
949 /* signal the end of the process initialization */
950 DECL_HANDLER(init_process_done)
952 struct process_dll *dll;
953 struct process *process = current->process;
955 if (is_process_init_done(process))
957 set_error( STATUS_INVALID_PARAMETER );
960 if (!(dll = find_process_dll( process, req->module )))
962 set_error( STATUS_DLL_NOT_FOUND );
966 /* main exe is the first in the dll list */
967 list_remove( &dll->entry );
968 list_add_head( &process->dlls, &dll->entry );
970 generate_startup_debug_events( process, req->entry );
971 set_process_startup_state( process, STARTUP_DONE );
973 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
974 if (current->suspend + process->suspend > 0) stop_thread( current );
975 if (process->debugger) set_process_debug_flag( process, 1 );
978 /* open a handle to a process */
979 DECL_HANDLER(open_process)
981 struct process *process = get_process_from_id( req->pid );
985 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
986 release_object( process );
990 /* terminate a process */
991 DECL_HANDLER(terminate_process)
993 struct process *process;
995 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
997 reply->self = (current->process == process);
998 terminate_process( process, current, req->exit_code );
999 release_object( process );
1003 /* fetch information about a process */
1004 DECL_HANDLER(get_process_info)
1006 struct process *process;
1008 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1010 reply->pid = get_process_id( process );
1011 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1012 reply->exit_code = process->exit_code;
1013 reply->priority = process->priority;
1014 reply->affinity = process->affinity;
1015 reply->peb = process->peb;
1016 reply->start_time.sec = process->start_time.tv_sec;
1017 reply->start_time.usec = process->start_time.tv_usec;
1018 reply->end_time.sec = process->end_time.tv_sec;
1019 reply->end_time.usec = process->end_time.tv_usec;
1020 release_object( process );
1024 /* set information about a process */
1025 DECL_HANDLER(set_process_info)
1027 struct process *process;
1029 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1031 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1032 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1034 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1035 else process->affinity = req->affinity;
1037 release_object( process );
1041 /* read data from a process address space */
1042 DECL_HANDLER(read_process_memory)
1044 struct process *process;
1045 data_size_t len = get_reply_max_size();
1047 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1051 char *buffer = mem_alloc( len );
1054 if (read_process_memory( process, req->addr, len, buffer ))
1055 set_reply_data_ptr( buffer, len );
1060 release_object( process );
1063 /* write data to a process address space */
1064 DECL_HANDLER(write_process_memory)
1066 struct process *process;
1068 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1070 data_size_t len = get_req_data_size();
1071 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1072 else set_error( STATUS_INVALID_PARAMETER );
1073 release_object( process );
1077 /* notify the server that a dll has been loaded */
1078 DECL_HANDLER(load_dll)
1080 struct process_dll *dll;
1081 struct file *file = NULL;
1083 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1086 if ((dll = process_load_dll( current->process, file, req->base,
1087 get_req_data(), get_req_data_size() )))
1089 dll->size = req->size;
1090 dll->dbg_offset = req->dbg_offset;
1091 dll->dbg_size = req->dbg_size;
1092 dll->name = req->name;
1093 /* only generate event if initialization is done */
1094 if (is_process_init_done( current->process ))
1095 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1097 if (file) release_object( file );
1100 /* notify the server that a dll is being unloaded */
1101 DECL_HANDLER(unload_dll)
1103 process_unload_dll( current->process, req->base );
1106 /* retrieve information about a module in a process */
1107 DECL_HANDLER(get_dll_info)
1109 struct process *process;
1111 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1113 struct process_dll *dll = find_process_dll( process, req->base_address );
1117 reply->size = dll->size;
1118 reply->entry_point = NULL; /* FIXME */
1121 data_size_t len = min( dll->namelen, get_reply_max_size() );
1122 set_reply_data( dll->filename, len );
1126 set_error( STATUS_DLL_NOT_FOUND );
1128 release_object( process );
1132 /* retrieve the process idle event */
1133 DECL_HANDLER(get_process_idle_event)
1135 struct process *process;
1138 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1140 if (process->idle_event && process != current->process)
1141 reply->event = alloc_handle( current->process, process->idle_event,
1142 EVENT_ALL_ACCESS, 0 );
1143 release_object( process );