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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_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 */
87 no_get_file_info, /* get_file_info */
88 no_queue_async, /* queue_async */
89 no_cancel_async /* cancel async */
92 /* process startup info */
96 struct object obj; /* object header */
97 struct list entry; /* entry in list of startup infos */
98 int inherit_all; /* inherit all handles from parent */
99 unsigned int create_flags; /* creation flags */
100 int unix_pid; /* Unix pid of new process */
101 obj_handle_t hstdin; /* handle for stdin */
102 obj_handle_t hstdout; /* handle for stdout */
103 obj_handle_t hstderr; /* handle for stderr */
104 struct file *exe_file; /* file handle for main exe */
105 struct thread *owner; /* owner thread (the one that created the new process) */
106 struct process *process; /* created process */
107 struct thread *thread; /* created thread */
108 size_t data_size; /* size of startup data */
109 void *data; /* data for startup info */
112 static void startup_info_dump( struct object *obj, int verbose );
113 static int startup_info_signaled( struct object *obj, struct thread *thread );
114 static void startup_info_destroy( struct object *obj );
116 static const struct object_ops startup_info_ops =
118 sizeof(struct startup_info), /* size */
119 startup_info_dump, /* dump */
120 add_queue, /* add_queue */
121 remove_queue, /* remove_queue */
122 startup_info_signaled, /* signaled */
123 no_satisfied, /* satisfied */
124 no_signal, /* signal */
125 no_get_fd, /* get_fd */
126 no_map_access, /* map_access */
127 no_lookup_name, /* lookup_name */
128 no_close_handle, /* close_handle */
129 startup_info_destroy /* destroy */
133 static struct list startup_info_list = LIST_INIT(startup_info_list);
137 void *ptr; /* entry ptr */
138 unsigned int next; /* next free entry */
141 static struct ptid_entry *ptid_entries; /* array of ptid entries */
142 static unsigned int used_ptid_entries; /* number of entries in use */
143 static unsigned int alloc_ptid_entries; /* number of allocated entries */
144 static unsigned int next_free_ptid; /* next free entry */
145 static unsigned int last_free_ptid; /* last free entry */
147 #define PTID_OFFSET 8 /* offset for first ptid value */
149 /* allocate a new process or thread id */
150 unsigned int alloc_ptid( void *ptr )
152 struct ptid_entry *entry;
155 if (used_ptid_entries < alloc_ptid_entries)
157 id = used_ptid_entries + PTID_OFFSET;
158 entry = &ptid_entries[used_ptid_entries++];
160 else if (next_free_ptid)
163 entry = &ptid_entries[id - PTID_OFFSET];
164 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
166 else /* need to grow the array */
168 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
169 if (!count) count = 64;
170 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
172 set_error( STATUS_NO_MEMORY );
175 ptid_entries = entry;
176 alloc_ptid_entries = count;
177 id = used_ptid_entries + PTID_OFFSET;
178 entry = &ptid_entries[used_ptid_entries++];
185 /* free a process or thread id */
186 void free_ptid( unsigned int id )
188 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
193 /* append to end of free list so that we don't reuse it too early */
194 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
195 else next_free_ptid = id;
200 /* retrieve the pointer corresponding to a process or thread id */
201 void *get_ptid_entry( unsigned int id )
203 if (id < PTID_OFFSET) return NULL;
204 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
205 return ptid_entries[id - PTID_OFFSET].ptr;
208 /* return the main thread of the process */
209 struct thread *get_process_first_thread( struct process *process )
211 struct list *ptr = list_head( &process->thread_list );
212 if (!ptr) return NULL;
213 return LIST_ENTRY( ptr, struct thread, proc_entry );
216 /* set the state of the process startup info */
217 static void set_process_startup_state( struct process *process, enum startup_state state )
219 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
220 if (process->startup_info)
222 wake_up( &process->startup_info->obj, 0 );
223 release_object( process->startup_info );
224 process->startup_info = NULL;
228 /* create a new process and its main thread */
229 struct thread *create_process( int fd )
231 struct process *process;
232 struct thread *thread = NULL;
235 if (!(process = alloc_object( &process_ops ))) goto error;
236 process->parent = NULL;
237 process->debugger = NULL;
238 process->handles = NULL;
239 process->msg_fd = NULL;
240 process->exit_code = STILL_ACTIVE;
241 process->running_threads = 0;
242 process->priority = PROCESS_PRIOCLASS_NORMAL;
243 process->affinity = 1;
244 process->suspend = 0;
245 process->create_flags = 0;
246 process->console = NULL;
247 process->startup_state = STARTUP_IN_PROGRESS;
248 process->startup_info = NULL;
249 process->idle_event = NULL;
250 process->queue = NULL;
252 process->ldt_copy = NULL;
253 process->winstation = 0;
254 process->desktop = 0;
255 process->token = token_create_admin();
256 list_init( &process->thread_list );
257 list_init( &process->locks );
258 list_init( &process->classes );
259 list_init( &process->dlls );
261 gettimeofday( &process->start_time, NULL );
262 list_add_head( &process_list, &process->entry );
264 if (!(process->id = process->group_id = alloc_ptid( process ))) goto error;
265 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
267 /* create the main thread */
268 if (pipe( request_pipe ) == -1)
273 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
275 close( request_pipe[0] );
276 close( request_pipe[1] );
279 close( request_pipe[1] );
280 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
282 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
283 release_object( process );
287 if (process) release_object( process );
288 /* if we failed to start our first process, close everything down */
289 if (!running_processes) close_master_socket();
293 /* find the startup info for a given Unix process */
294 inline static struct startup_info *find_startup_info( int unix_pid )
298 LIST_FOR_EACH( ptr, &startup_info_list )
300 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
301 if (info->unix_pid == unix_pid) return info;
306 /* initialize the current process and fill in the request */
307 size_t init_process( struct thread *thread )
309 struct process *process = thread->process;
310 struct thread *parent_thread = NULL;
311 struct process *parent = NULL;
312 struct startup_info *info;
314 if (process->startup_info) return process->startup_info->data_size; /* already initialized */
316 if ((info = find_startup_info( thread->unix_pid )))
318 if (info->thread) return info->data_size; /* already initialized */
320 info->thread = (struct thread *)grab_object( thread );
321 info->process = (struct process *)grab_object( process );
322 process->startup_info = (struct startup_info *)grab_object( info );
324 parent_thread = info->owner;
325 parent = parent_thread->process;
326 process->parent = (struct process *)grab_object( parent );
328 /* set the process flags */
329 process->create_flags = info->create_flags;
331 if (info->inherit_all) process->handles = copy_handle_table( process, parent );
334 /* create the handle table */
335 if (!process->handles) process->handles = alloc_handle_table( process, 0 );
336 if (!process->handles)
338 fatal_protocol_error( thread, "Failed to allocate handle table\n" );
342 /* connect to the window station */
343 connect_process_winstation( process, parent_thread );
347 /* thread will be actually suspended in init_done */
348 if (info->create_flags & CREATE_SUSPENDED) thread->suspend++;
350 /* set the process console */
351 if (!(info->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
353 /* FIXME: some better error checking should be done...
354 * like if hConOut and hConIn are console handles, then they should be on the same
357 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
360 /* attach to the debugger if requested */
361 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
362 set_process_debugger( process, parent_thread );
363 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
364 set_process_debugger( process, parent->debugger );
366 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
367 process->group_id = parent->group_id;
369 return info->data_size;
372 /* destroy a process when its refcount is 0 */
373 static void process_destroy( struct object *obj )
375 struct process *process = (struct process *)obj;
376 assert( obj->ops == &process_ops );
378 /* we can't have a thread remaining */
379 assert( list_empty( &process->thread_list ));
381 set_process_startup_state( process, STARTUP_ABORTED );
382 if (process->console) release_object( process->console );
383 if (process->parent) release_object( process->parent );
384 if (process->msg_fd) release_object( process->msg_fd );
385 list_remove( &process->entry );
386 if (process->idle_event) release_object( process->idle_event );
387 if (process->queue) release_object( process->queue );
388 if (process->id) free_ptid( process->id );
389 if (process->token) release_object( process->token );
392 /* dump a process on stdout for debugging purposes */
393 static void process_dump( struct object *obj, int verbose )
395 struct process *process = (struct process *)obj;
396 assert( obj->ops == &process_ops );
398 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
401 static int process_signaled( struct object *obj, struct thread *thread )
403 struct process *process = (struct process *)obj;
404 return !process->running_threads;
407 static unsigned int process_map_access( struct object *obj, unsigned int access )
409 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
410 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
411 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
412 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
413 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
416 static void process_poll_event( struct fd *fd, int event )
418 struct process *process = get_fd_user( fd );
419 assert( process->obj.ops == &process_ops );
421 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
422 else if (event & POLLIN) receive_fd( process );
425 static void startup_info_destroy( struct object *obj )
427 struct startup_info *info = (struct startup_info *)obj;
428 assert( obj->ops == &startup_info_ops );
429 list_remove( &info->entry );
430 if (info->data) free( info->data );
431 if (info->exe_file) release_object( info->exe_file );
432 if (info->process) release_object( info->process );
433 if (info->thread) release_object( info->thread );
434 if (info->owner) release_object( info->owner );
437 static void startup_info_dump( struct object *obj, int verbose )
439 struct startup_info *info = (struct startup_info *)obj;
440 assert( obj->ops == &startup_info_ops );
442 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
443 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
446 static int startup_info_signaled( struct object *obj, struct thread *thread )
448 struct startup_info *info = (struct startup_info *)obj;
449 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
452 /* get a process from an id (and increment the refcount) */
453 struct process *get_process_from_id( process_id_t id )
455 struct object *obj = get_ptid_entry( id );
457 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
458 set_error( STATUS_INVALID_PARAMETER );
462 /* get a process from a handle (and increment the refcount) */
463 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
465 return (struct process *)get_handle_obj( current->process, handle,
466 access, &process_ops );
469 /* find a dll from its base address */
470 static inline struct process_dll *find_process_dll( struct process *process, void *base )
472 struct process_dll *dll;
474 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
476 if (dll->base == base) return dll;
481 /* add a dll to a process list */
482 static struct process_dll *process_load_dll( struct process *process, struct file *file,
483 void *base, const WCHAR *filename, size_t name_len )
485 struct process_dll *dll;
487 /* make sure we don't already have one with the same base address */
488 if (find_process_dll( process, base ))
490 set_error( STATUS_INVALID_PARAMETER );
494 if ((dll = mem_alloc( sizeof(*dll) )))
498 dll->filename = NULL;
499 dll->namelen = name_len;
500 if (name_len && !(dll->filename = memdup( filename, name_len )))
505 if (file) dll->file = (struct file *)grab_object( file );
506 list_add_tail( &process->dlls, &dll->entry );
511 /* remove a dll from a process list */
512 static void process_unload_dll( struct process *process, void *base )
514 struct process_dll *dll = find_process_dll( process, base );
516 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
518 if (dll->file) release_object( dll->file );
519 if (dll->filename) free( dll->filename );
520 list_remove( &dll->entry );
522 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
524 else set_error( STATUS_INVALID_PARAMETER );
527 /* kill all processes */
528 void kill_all_processes( struct process *skip, int exit_code )
532 struct process *process;
534 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
536 if (process == skip) continue;
537 if (process->running_threads) break;
539 if (&process->entry == &process_list) break; /* no process found */
540 kill_process( process, NULL, exit_code );
544 /* kill all processes being attached to a console renderer */
545 void kill_console_processes( struct thread *renderer, int exit_code )
547 for (;;) /* restart from the beginning of the list every time */
549 struct process *process;
551 /* find the first process being attached to 'renderer' and still running */
552 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
554 if (process == renderer->process) continue;
555 if (!process->running_threads) continue;
556 if (process->console && process->console->renderer == renderer) break;
558 if (&process->entry == &process_list) break; /* no process found */
559 kill_process( process, NULL, exit_code );
563 /* a process has been killed (i.e. its last thread died) */
564 static void process_killed( struct process *process )
566 struct handle_table *handles;
569 assert( list_empty( &process->thread_list ));
570 gettimeofday( &process->end_time, NULL );
571 close_process_desktop( process );
572 handles = process->handles;
573 process->handles = NULL;
574 if (handles) release_object( handles );
576 /* close the console attached to this process, if any */
577 free_console( process );
579 while ((ptr = list_head( &process->dlls )))
581 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
582 if (dll->file) release_object( dll->file );
583 if (dll->filename) free( dll->filename );
584 list_remove( &dll->entry );
587 destroy_process_classes( process );
588 remove_process_locks( process );
589 set_process_startup_state( process, STARTUP_ABORTED );
590 wake_up( &process->obj, 0 );
591 if (!--running_processes) close_master_socket();
594 /* add a thread to a process running threads list */
595 void add_process_thread( struct process *process, struct thread *thread )
597 list_add_head( &process->thread_list, &thread->proc_entry );
598 if (!process->running_threads++) running_processes++;
599 grab_object( thread );
602 /* remove a thread from a process running threads list */
603 void remove_process_thread( struct process *process, struct thread *thread )
605 assert( process->running_threads > 0 );
606 assert( !list_empty( &process->thread_list ));
608 list_remove( &thread->proc_entry );
610 if (!--process->running_threads)
612 /* we have removed the last running thread, exit the process */
613 process->exit_code = thread->exit_code;
614 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
615 process_killed( process );
617 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
618 release_object( thread );
621 /* suspend all the threads of a process */
622 void suspend_process( struct process *process )
624 if (!process->suspend++)
626 struct list *ptr, *next;
628 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
630 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
631 if (!thread->suspend) stop_thread( thread );
636 /* resume all the threads of a process */
637 void resume_process( struct process *process )
639 assert (process->suspend > 0);
640 if (!--process->suspend)
642 struct list *ptr, *next;
644 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
646 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
647 if (!thread->suspend) wake_thread( thread );
652 /* kill a process on the spot */
653 void kill_process( struct process *process, struct thread *skip, int exit_code )
655 struct list *ptr, *next;
657 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
659 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
661 if (exit_code) thread->exit_code = exit_code;
662 if (thread != skip) kill_thread( thread, 1 );
666 /* kill all processes being debugged by a given thread */
667 void kill_debugged_processes( struct thread *debugger, int exit_code )
669 for (;;) /* restart from the beginning of the list every time */
671 struct process *process;
673 /* find the first process being debugged by 'debugger' and still running */
674 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
676 if (!process->running_threads) continue;
677 if (process->debugger == debugger) break;
679 if (&process->entry == &process_list) break; /* no process found */
680 process->debugger = NULL;
681 kill_process( process, NULL, exit_code );
686 /* detach a debugger from all its debuggees */
687 void detach_debugged_processes( struct thread *debugger )
689 struct process *process;
691 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
693 if (process->debugger == debugger && process->running_threads)
695 debugger_detach( process, debugger );
701 void enum_processes( int (*cb)(struct process*, void*), void *user )
703 struct list *ptr, *next;
705 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
707 struct process *process = LIST_ENTRY( ptr, struct process, entry );
708 if ((cb)(process, user)) break;
712 /* set the debugged flag in the process PEB */
713 int set_process_debug_flag( struct process *process, int flag )
715 char data = (flag != 0);
717 /* BeingDebugged flag is the byte at offset 2 in the PEB */
718 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
721 /* take a snapshot of currently running processes */
722 struct process_snapshot *process_snap( int *count )
724 struct process_snapshot *snapshot, *ptr;
725 struct process *process;
726 if (!running_processes) return NULL;
727 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
730 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
732 if (!process->running_threads) continue;
733 ptr->process = process;
734 ptr->threads = process->running_threads;
735 ptr->count = process->obj.refcount;
736 ptr->priority = process->priority;
737 ptr->handles = get_handle_table_count(process);
738 grab_object( process );
741 *count = running_processes;
745 /* take a snapshot of the modules of a process */
746 struct module_snapshot *module_snap( struct process *process, int *count )
748 struct module_snapshot *snapshot, *ptr;
749 struct process_dll *dll;
752 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
753 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
756 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
758 ptr->base = dll->base;
759 ptr->size = dll->size;
760 ptr->namelen = dll->namelen;
761 ptr->filename = memdup( dll->filename, dll->namelen );
769 /* create a new process */
770 DECL_HANDLER(new_process)
772 struct startup_info *info;
774 /* build the startup info for a new process */
775 if (!(info = alloc_object( &startup_info_ops ))) return;
776 list_add_head( &startup_info_list, &info->entry );
777 info->inherit_all = req->inherit_all;
778 info->create_flags = req->create_flags;
779 info->unix_pid = req->unix_pid;
780 info->hstdin = req->hstdin;
781 info->hstdout = req->hstdout;
782 info->hstderr = req->hstderr;
783 info->exe_file = NULL;
784 info->owner = (struct thread *)grab_object( current );
785 info->process = NULL;
787 info->data_size = get_req_data_size();
791 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
794 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
795 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
798 release_object( info );
801 /* Retrieve information about a newly started process */
802 DECL_HANDLER(get_new_process_info)
804 struct startup_info *info;
806 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
807 0, &startup_info_ops )))
809 reply->pid = get_process_id( info->process );
810 reply->tid = get_thread_id( info->thread );
811 reply->phandle = alloc_handle( current->process, info->process,
812 req->process_access, req->process_attr );
813 reply->thandle = alloc_handle( current->process, info->thread,
814 req->thread_access, req->thread_attr );
815 reply->success = is_process_init_done( info->process );
816 release_object( info );
828 /* Retrieve the new process startup info */
829 DECL_HANDLER(get_startup_info)
831 struct process *process = current->process;
832 struct startup_info *info = process->startup_info;
837 if (info->exe_file &&
838 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
840 if (!info->inherit_all && !(info->create_flags & CREATE_NEW_CONSOLE))
842 struct process *parent_process = info->owner->process;
843 reply->hstdin = duplicate_handle( parent_process, info->hstdin, process,
844 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
845 reply->hstdout = duplicate_handle( parent_process, info->hstdout, process,
846 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
847 reply->hstderr = duplicate_handle( parent_process, info->hstderr, process,
848 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
849 /* some handles above may have been invalid; this is not an error */
850 if (get_error() == STATUS_INVALID_HANDLE ||
851 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
855 reply->hstdin = info->hstdin;
856 reply->hstdout = info->hstdout;
857 reply->hstderr = info->hstderr;
860 /* we return the data directly without making a copy so this can only be called once */
861 size = info->data_size;
862 if (size > get_reply_max_size()) size = get_reply_max_size();
863 set_reply_data_ptr( info->data, size );
868 /* signal the end of the process initialization */
869 DECL_HANDLER(init_process_done)
871 struct process_dll *dll;
872 struct process *process = current->process;
874 if (is_process_init_done(process))
876 fatal_protocol_error( current, "init_process_done: called twice\n" );
879 if (!(dll = find_process_dll( process, req->module )))
881 set_error( STATUS_DLL_NOT_FOUND );
885 /* main exe is the first in the dll list */
886 list_remove( &dll->entry );
887 list_add_head( &process->dlls, &dll->entry );
889 generate_startup_debug_events( process, req->entry );
890 set_process_startup_state( process, STARTUP_DONE );
892 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
893 if (current->suspend + process->suspend > 0) stop_thread( current );
894 if (process->debugger) set_process_debug_flag( process, 1 );
897 /* open a handle to a process */
898 DECL_HANDLER(open_process)
900 struct process *process = get_process_from_id( req->pid );
904 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
905 release_object( process );
909 /* terminate a process */
910 DECL_HANDLER(terminate_process)
912 struct process *process;
914 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
916 reply->self = (current->process == process);
917 kill_process( process, current, req->exit_code );
918 release_object( process );
922 /* fetch information about a process */
923 DECL_HANDLER(get_process_info)
925 struct process *process;
927 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
929 reply->pid = get_process_id( process );
930 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
931 reply->exit_code = process->exit_code;
932 reply->priority = process->priority;
933 reply->affinity = process->affinity;
934 reply->peb = process->peb;
935 release_object( process );
939 /* set information about a process */
940 DECL_HANDLER(set_process_info)
942 struct process *process;
944 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
946 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
947 if (req->mask & SET_PROCESS_INFO_AFFINITY)
949 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
950 else process->affinity = req->affinity;
952 release_object( process );
956 /* read data from a process address space */
957 DECL_HANDLER(read_process_memory)
959 struct process *process;
960 size_t len = get_reply_max_size();
962 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
966 char *buffer = mem_alloc( len );
969 if (read_process_memory( process, req->addr, len, buffer ))
970 set_reply_data_ptr( buffer, len );
975 release_object( process );
978 /* write data to a process address space */
979 DECL_HANDLER(write_process_memory)
981 struct process *process;
983 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
985 size_t len = get_req_data_size();
986 if (len) write_process_memory( process, req->addr, len, get_req_data() );
987 else set_error( STATUS_INVALID_PARAMETER );
988 release_object( process );
992 /* notify the server that a dll has been loaded */
993 DECL_HANDLER(load_dll)
995 struct process_dll *dll;
996 struct file *file = NULL;
998 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1001 if ((dll = process_load_dll( current->process, file, req->base,
1002 get_req_data(), get_req_data_size() )))
1004 dll->size = req->size;
1005 dll->dbg_offset = req->dbg_offset;
1006 dll->dbg_size = req->dbg_size;
1007 dll->name = req->name;
1008 /* only generate event if initialization is done */
1009 if (is_process_init_done( current->process ))
1010 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1012 if (file) release_object( file );
1015 /* notify the server that a dll is being unloaded */
1016 DECL_HANDLER(unload_dll)
1018 process_unload_dll( current->process, req->base );
1021 /* retrieve information about a module in a process */
1022 DECL_HANDLER(get_dll_info)
1024 struct process *process;
1026 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1028 struct process_dll *dll = find_process_dll( process, req->base_address );
1032 reply->size = dll->size;
1033 reply->entry_point = NULL; /* FIXME */
1036 size_t len = min( dll->namelen, get_reply_max_size() );
1037 set_reply_data( dll->filename, len );
1041 set_error( STATUS_DLL_NOT_FOUND );
1043 release_object( process );
1047 /* wait for a process to start waiting on input */
1048 /* FIXME: only returns event for now, wait is done in the client */
1049 DECL_HANDLER(wait_input_idle)
1051 struct process *process;
1054 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1056 if (process->idle_event && process != current->process)
1057 reply->event = alloc_handle( current->process, process->idle_event,
1058 EVENT_ALL_ACCESS, 0 );
1059 release_object( process );