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>
49 /* process structure */
51 static struct list process_list = LIST_INIT(process_list);
52 static int running_processes;
54 /* process operations */
56 static void process_dump( struct object *obj, int verbose );
57 static int process_signaled( struct object *obj, struct thread *thread );
58 static void process_poll_event( struct fd *fd, int event );
59 static void process_destroy( struct object *obj );
61 static const struct object_ops process_ops =
63 sizeof(struct process), /* size */
64 process_dump, /* dump */
65 add_queue, /* add_queue */
66 remove_queue, /* remove_queue */
67 process_signaled, /* signaled */
68 no_satisfied, /* satisfied */
69 no_get_fd, /* get_fd */
70 process_destroy /* destroy */
73 static const struct fd_ops process_fd_ops =
75 NULL, /* get_poll_events */
76 process_poll_event, /* poll_event */
78 no_get_file_info, /* get_file_info */
79 no_queue_async, /* queue_async */
80 no_cancel_async /* cancel async */
83 /* process startup info */
87 struct object obj; /* object header */
88 struct list entry; /* entry in list of startup infos */
89 int inherit_all; /* inherit all handles from parent */
90 int create_flags; /* creation flags */
91 int unix_pid; /* Unix pid of new process */
92 obj_handle_t hstdin; /* handle for stdin */
93 obj_handle_t hstdout; /* handle for stdout */
94 obj_handle_t hstderr; /* handle for stderr */
95 struct file *exe_file; /* file handle for main exe */
96 struct thread *owner; /* owner thread (the one that created the new process) */
97 struct process *process; /* created process */
98 struct thread *thread; /* created thread */
99 size_t data_size; /* size of startup data */
100 void *data; /* data for startup info */
103 static void startup_info_dump( struct object *obj, int verbose );
104 static int startup_info_signaled( struct object *obj, struct thread *thread );
105 static void startup_info_destroy( struct object *obj );
107 static const struct object_ops startup_info_ops =
109 sizeof(struct startup_info), /* size */
110 startup_info_dump, /* dump */
111 add_queue, /* add_queue */
112 remove_queue, /* remove_queue */
113 startup_info_signaled, /* signaled */
114 no_satisfied, /* satisfied */
115 no_get_fd, /* get_fd */
116 startup_info_destroy /* destroy */
120 static struct list startup_info_list = LIST_INIT(startup_info_list);
124 void *ptr; /* entry ptr */
125 unsigned int next; /* next free entry */
128 static struct ptid_entry *ptid_entries; /* array of ptid entries */
129 static unsigned int used_ptid_entries; /* number of entries in use */
130 static unsigned int alloc_ptid_entries; /* number of allocated entries */
131 static unsigned int next_free_ptid; /* next free entry */
132 static unsigned int last_free_ptid; /* last free entry */
134 #define PTID_OFFSET 8 /* offset for first ptid value */
136 /* allocate a new process or thread id */
137 unsigned int alloc_ptid( void *ptr )
139 struct ptid_entry *entry;
142 if (used_ptid_entries < alloc_ptid_entries)
144 id = used_ptid_entries + PTID_OFFSET;
145 entry = &ptid_entries[used_ptid_entries++];
147 else if (next_free_ptid)
150 entry = &ptid_entries[id - PTID_OFFSET];
151 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
153 else /* need to grow the array */
155 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
156 if (!count) count = 64;
157 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
159 set_error( STATUS_NO_MEMORY );
162 ptid_entries = entry;
163 alloc_ptid_entries = count;
164 id = used_ptid_entries + PTID_OFFSET;
165 entry = &ptid_entries[used_ptid_entries++];
172 /* free a process or thread id */
173 void free_ptid( unsigned int id )
175 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
180 /* append to end of free list so that we don't reuse it too early */
181 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
182 else next_free_ptid = id;
187 /* retrieve the pointer corresponding to a process or thread id */
188 void *get_ptid_entry( unsigned int id )
190 if (id < PTID_OFFSET) return NULL;
191 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
192 return ptid_entries[id - PTID_OFFSET].ptr;
195 /* return the main thread of the process */
196 struct thread *get_process_first_thread( struct process *process )
198 struct list *ptr = list_head( &process->thread_list );
199 if (!ptr) return NULL;
200 return LIST_ENTRY( ptr, struct thread, proc_entry );
203 /* set the state of the process startup info */
204 static void set_process_startup_state( struct process *process, enum startup_state state )
206 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
207 if (process->startup_info)
209 wake_up( &process->startup_info->obj, 0 );
210 release_object( process->startup_info );
211 process->startup_info = NULL;
215 /* set the console and stdio handles for a newly created process */
216 static int set_process_console( struct process *process, struct thread *parent_thread,
217 struct startup_info *info, struct init_process_reply *reply )
221 if (!(process->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
223 /* FIXME: some better error checking should be done...
224 * like if hConOut and hConIn are console handles, then they should be on the same
227 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
229 if (!info->inherit_all && !(process->create_flags & CREATE_NEW_CONSOLE))
231 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
232 0, TRUE, DUPLICATE_SAME_ACCESS );
233 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
234 0, TRUE, DUPLICATE_SAME_ACCESS );
235 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
236 0, TRUE, DUPLICATE_SAME_ACCESS );
240 reply->hstdin = info->hstdin;
241 reply->hstdout = info->hstdout;
242 reply->hstderr = info->hstderr;
245 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
246 /* some handles above may have been invalid; this is not an error */
247 if (get_error() == STATUS_INVALID_HANDLE ||
248 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
252 /* create a new process and its main thread */
253 struct thread *create_process( int fd )
255 struct process *process;
256 struct thread *thread = NULL;
259 if (!(process = alloc_object( &process_ops ))) goto error;
260 process->parent = NULL;
261 process->debugger = NULL;
262 process->handles = NULL;
263 process->msg_fd = NULL;
264 process->exit_code = STILL_ACTIVE;
265 process->running_threads = 0;
266 process->priority = NORMAL_PRIORITY_CLASS;
267 process->affinity = 1;
268 process->suspend = 0;
269 process->create_flags = 0;
270 process->console = NULL;
271 process->startup_state = STARTUP_IN_PROGRESS;
272 process->startup_info = NULL;
273 process->idle_event = NULL;
274 process->queue = NULL;
275 process->atom_table = NULL;
277 process->ldt_copy = NULL;
278 process->exe.file = NULL;
279 process->exe.dbg_offset = 0;
280 process->exe.dbg_size = 0;
281 process->exe.namelen = 0;
282 process->exe.filename = NULL;
283 process->group_id = 0;
284 process->token = create_admin_token();
285 list_init( &process->thread_list );
286 list_init( &process->locks );
287 list_init( &process->classes );
288 list_init( &process->dlls );
290 gettimeofday( &process->start_time, NULL );
291 list_add_head( &process_list, &process->entry );
293 if (!(process->id = alloc_ptid( process ))) goto error;
294 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
296 /* create the main thread */
297 if (pipe( request_pipe ) == -1)
302 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
304 close( request_pipe[0] );
305 close( request_pipe[1] );
308 close( request_pipe[1] );
309 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
311 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
312 release_object( process );
316 if (process) release_object( process );
317 /* if we failed to start our first process, close everything down */
318 if (!running_processes) close_master_socket();
322 /* find the startup info for a given Unix process */
323 inline static struct startup_info *find_startup_info( int unix_pid )
327 LIST_FOR_EACH( ptr, &startup_info_list )
329 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
330 if (info->unix_pid == unix_pid) return info;
335 /* initialize the current process and fill in the request */
336 static struct startup_info *init_process( struct init_process_reply *reply )
338 struct process *process = current->process;
339 struct thread *parent_thread = NULL;
340 struct process *parent = NULL;
341 struct startup_info *info = find_startup_info( current->unix_pid );
347 fatal_protocol_error( current, "init_process: called twice?\n" );
350 parent_thread = info->owner;
351 parent = parent_thread->process;
352 process->parent = (struct process *)grab_object( parent );
355 /* set the process flags */
356 process->create_flags = info ? info->create_flags : 0;
358 /* create the handle table */
359 if (info && info->inherit_all)
360 process->handles = copy_handle_table( process, parent );
362 process->handles = alloc_handle_table( process, 0 );
363 if (!process->handles) return NULL;
365 /* retrieve the main exe file */
367 if (info && info->exe_file)
369 process->exe.file = (struct file *)grab_object( info->exe_file );
370 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
374 /* set the process console */
375 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
377 process->group_id = get_process_id( process );
380 /* attach to the debugger if requested */
381 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
382 set_process_debugger( process, parent_thread );
383 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
384 set_process_debugger( process, parent->debugger );
385 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
386 process->group_id = parent->group_id;
389 /* thread will be actually suspended in init_done */
390 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
394 reply->info_size = info->data_size;
395 info->process = (struct process *)grab_object( process );
396 info->thread = (struct thread *)grab_object( current );
398 reply->create_flags = process->create_flags;
399 reply->server_start = server_start_ticks;
400 return info ? (struct startup_info *)grab_object( info ) : NULL;
403 /* destroy a process when its refcount is 0 */
404 static void process_destroy( struct object *obj )
406 struct process *process = (struct process *)obj;
407 assert( obj->ops == &process_ops );
409 /* we can't have a thread remaining */
410 assert( list_empty( &process->thread_list ));
412 set_process_startup_state( process, STARTUP_ABORTED );
413 if (process->console) release_object( process->console );
414 if (process->parent) release_object( process->parent );
415 if (process->msg_fd) release_object( process->msg_fd );
416 list_remove( &process->entry );
417 if (process->idle_event) release_object( process->idle_event );
418 if (process->queue) release_object( process->queue );
419 if (process->atom_table) release_object( process->atom_table );
420 if (process->exe.file) release_object( process->exe.file );
421 if (process->exe.filename) free( process->exe.filename );
422 if (process->id) free_ptid( process->id );
423 if (process->token) release_object( process->token );
426 /* dump a process on stdout for debugging purposes */
427 static void process_dump( struct object *obj, int verbose )
429 struct process *process = (struct process *)obj;
430 assert( obj->ops == &process_ops );
432 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
435 static int process_signaled( struct object *obj, struct thread *thread )
437 struct process *process = (struct process *)obj;
438 return !process->running_threads;
441 static void process_poll_event( struct fd *fd, int event )
443 struct process *process = get_fd_user( fd );
444 assert( process->obj.ops == &process_ops );
446 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
447 else if (event & POLLIN) receive_fd( process );
450 static void startup_info_destroy( struct object *obj )
452 struct startup_info *info = (struct startup_info *)obj;
453 assert( obj->ops == &startup_info_ops );
454 list_remove( &info->entry );
455 if (info->data) free( info->data );
456 if (info->exe_file) release_object( info->exe_file );
457 if (info->process) release_object( info->process );
458 if (info->thread) release_object( info->thread );
459 if (info->owner) release_object( info->owner );
462 static void startup_info_dump( struct object *obj, int verbose )
464 struct startup_info *info = (struct startup_info *)obj;
465 assert( obj->ops == &startup_info_ops );
467 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
468 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
471 static int startup_info_signaled( struct object *obj, struct thread *thread )
473 struct startup_info *info = (struct startup_info *)obj;
474 return info->process && is_process_init_done(info->process);
477 /* get a process from an id (and increment the refcount) */
478 struct process *get_process_from_id( process_id_t id )
480 struct object *obj = get_ptid_entry( id );
482 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
483 set_error( STATUS_INVALID_PARAMETER );
487 /* get a process from a handle (and increment the refcount) */
488 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
490 return (struct process *)get_handle_obj( current->process, handle,
491 access, &process_ops );
494 /* find a dll from its base address */
495 static inline struct process_dll *find_process_dll( struct process *process, void *base )
497 struct process_dll *dll;
499 if (process->exe.base == base) return &process->exe;
501 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
503 if (dll->base == base) return dll;
508 /* add a dll to a process list */
509 static struct process_dll *process_load_dll( struct process *process, struct file *file,
510 void *base, const WCHAR *filename, size_t name_len )
512 struct process_dll *dll;
514 /* make sure we don't already have one with the same base address */
515 if (find_process_dll( process, base ))
517 set_error( STATUS_INVALID_PARAMETER );
521 if ((dll = mem_alloc( sizeof(*dll) )))
525 dll->filename = NULL;
526 dll->namelen = name_len;
527 if (name_len && !(dll->filename = memdup( filename, name_len )))
532 if (file) dll->file = (struct file *)grab_object( file );
533 list_add_head( &process->dlls, &dll->entry );
538 /* remove a dll from a process list */
539 static void process_unload_dll( struct process *process, void *base )
541 struct process_dll *dll = find_process_dll( process, base );
543 if (dll && dll != &process->exe)
545 if (dll->file) release_object( dll->file );
546 if (dll->filename) free( dll->filename );
547 list_remove( &dll->entry );
549 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
551 else set_error( STATUS_INVALID_PARAMETER );
554 /* kill all processes */
555 void kill_all_processes( struct process *skip, int exit_code )
559 struct process *process;
561 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
563 if (process == skip) continue;
564 if (process->running_threads) break;
566 if (&process->entry == &process_list) break; /* no process found */
567 kill_process( process, NULL, exit_code );
571 /* kill all processes being attached to a console renderer */
572 void kill_console_processes( struct thread *renderer, int exit_code )
574 for (;;) /* restart from the beginning of the list every time */
576 struct process *process;
578 /* find the first process being attached to 'renderer' and still running */
579 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
581 if (process == renderer->process) continue;
582 if (!process->running_threads) continue;
583 if (process->console && process->console->renderer == renderer) break;
585 if (&process->entry == &process_list) break; /* no process found */
586 kill_process( process, NULL, exit_code );
590 /* a process has been killed (i.e. its last thread died) */
591 static void process_killed( struct process *process )
595 assert( list_empty( &process->thread_list ));
596 gettimeofday( &process->end_time, NULL );
597 if (process->handles) release_object( process->handles );
598 process->handles = NULL;
600 /* close the console attached to this process, if any */
601 free_console( process );
603 while ((ptr = list_head( &process->dlls )))
605 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
606 if (dll->file) release_object( dll->file );
607 if (dll->filename) free( dll->filename );
608 list_remove( &dll->entry );
611 destroy_process_classes( process );
612 set_process_startup_state( process, STARTUP_ABORTED );
613 if (process->exe.file) release_object( process->exe.file );
614 process->exe.file = NULL;
615 wake_up( &process->obj, 0 );
616 if (!--running_processes) close_master_socket();
619 /* add a thread to a process running threads list */
620 void add_process_thread( struct process *process, struct thread *thread )
622 list_add_head( &process->thread_list, &thread->proc_entry );
623 if (!process->running_threads++) running_processes++;
624 grab_object( thread );
627 /* remove a thread from a process running threads list */
628 void remove_process_thread( struct process *process, struct thread *thread )
630 assert( process->running_threads > 0 );
631 assert( !list_empty( &process->thread_list ));
633 list_remove( &thread->proc_entry );
635 if (!--process->running_threads)
637 /* we have removed the last running thread, exit the process */
638 process->exit_code = thread->exit_code;
639 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
640 process_killed( process );
642 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
643 release_object( thread );
646 /* suspend all the threads of a process */
647 void suspend_process( struct process *process )
649 if (!process->suspend++)
651 struct list *ptr, *next;
653 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
655 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
656 if (!thread->suspend) stop_thread( thread );
661 /* resume all the threads of a process */
662 void resume_process( struct process *process )
664 assert (process->suspend > 0);
665 if (!--process->suspend)
667 struct list *ptr, *next;
669 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
671 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
672 if (!thread->suspend) wake_thread( thread );
677 /* kill a process on the spot */
678 void kill_process( struct process *process, struct thread *skip, int exit_code )
680 struct list *ptr, *next;
682 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
684 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
686 thread->exit_code = exit_code;
687 if (thread != skip) kill_thread( thread, 1 );
691 /* kill all processes being debugged by a given thread */
692 void kill_debugged_processes( struct thread *debugger, int exit_code )
694 for (;;) /* restart from the beginning of the list every time */
696 struct process *process;
698 /* find the first process being debugged by 'debugger' and still running */
699 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
701 if (!process->running_threads) continue;
702 if (process->debugger == debugger) break;
704 if (&process->entry == &process_list) break; /* no process found */
705 process->debugger = NULL;
706 kill_process( process, NULL, exit_code );
711 /* detach a debugger from all its debuggees */
712 void detach_debugged_processes( struct thread *debugger )
714 struct process *process;
716 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
718 if (process->debugger == debugger && process->running_threads)
720 debugger_detach( process, debugger );
726 void enum_processes( int (*cb)(struct process*, void*), void *user )
728 struct list *ptr, *next;
730 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
732 struct process *process = LIST_ENTRY( ptr, struct process, entry );
733 if ((cb)(process, user)) break;
738 /* read data from a process memory space */
739 /* len is the total size (in ints) */
740 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
742 struct thread *thread = get_process_first_thread( process );
744 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
746 if (!thread) /* process is dead */
748 set_error( STATUS_ACCESS_DENIED );
751 if (suspend_for_ptrace( thread ))
755 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
758 resume_after_ptrace( thread );
763 /* make sure we can write to the whole address range */
764 /* len is the total size (in ints) */
765 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
767 int page = get_page_size() / sizeof(int);
771 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
772 if (len <= page) break;
776 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
779 /* write data to a process memory space */
780 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
781 /* we check the total size for write permissions */
782 static int write_process_memory( struct process *process, int *addr, size_t len,
783 unsigned int first_mask, unsigned int last_mask, const int *src )
785 struct thread *thread = get_process_first_thread( process );
788 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
790 if (!thread) /* process is dead */
792 set_error( STATUS_ACCESS_DENIED );
795 if (suspend_for_ptrace( thread ))
797 if (!check_process_write_access( thread, addr, len ))
799 set_error( STATUS_ACCESS_DENIED );
802 /* first word is special */
805 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
808 else last_mask &= first_mask;
812 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
816 /* last word is special too */
817 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
821 resume_after_ptrace( thread );
826 /* set the debugged flag in the process PEB */
827 int set_process_debug_flag( struct process *process, int flag )
829 int mask = 0, data = 0;
831 /* BeingDebugged flag is the byte at offset 2 in the PEB */
832 memset( (char *)&mask + 2, 0xff, 1 );
833 memset( (char *)&data + 2, flag, 1 );
834 return write_process_memory( process, process->peb, 1, mask, mask, &data );
837 /* take a snapshot of currently running processes */
838 struct process_snapshot *process_snap( int *count )
840 struct process_snapshot *snapshot, *ptr;
841 struct process *process;
842 if (!running_processes) return NULL;
843 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
846 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
848 if (!process->running_threads) continue;
849 ptr->process = process;
850 ptr->threads = process->running_threads;
851 ptr->count = process->obj.refcount;
852 ptr->priority = process->priority;
853 ptr->handles = get_handle_table_count(process);
854 grab_object( process );
857 *count = running_processes;
861 /* take a snapshot of the modules of a process */
862 struct module_snapshot *module_snap( struct process *process, int *count )
864 struct module_snapshot *snapshot, *ptr;
865 struct process_dll *dll;
868 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
869 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
871 /* first entry is main exe */
872 snapshot->base = process->exe.base;
873 snapshot->size = process->exe.size;
874 snapshot->namelen = process->exe.namelen;
875 snapshot->filename = memdup( process->exe.filename, process->exe.namelen );
878 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
880 ptr->base = dll->base;
881 ptr->size = dll->size;
882 ptr->namelen = dll->namelen;
883 ptr->filename = memdup( dll->filename, dll->namelen );
891 /* create a new process */
892 DECL_HANDLER(new_process)
894 struct startup_info *info;
896 /* build the startup info for a new process */
897 if (!(info = alloc_object( &startup_info_ops ))) return;
898 list_add_head( &startup_info_list, &info->entry );
899 info->inherit_all = req->inherit_all;
900 info->create_flags = req->create_flags;
901 info->unix_pid = req->unix_pid;
902 info->hstdin = req->hstdin;
903 info->hstdout = req->hstdout;
904 info->hstderr = req->hstderr;
905 info->exe_file = NULL;
906 info->owner = (struct thread *)grab_object( current );
907 info->process = NULL;
909 info->data_size = get_req_data_size();
913 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
916 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
917 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
920 release_object( info );
923 /* Retrieve information about a newly started process */
924 DECL_HANDLER(get_new_process_info)
926 struct startup_info *info;
928 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
929 0, &startup_info_ops )))
931 reply->pid = get_process_id( info->process );
932 reply->tid = get_thread_id( info->thread );
933 reply->phandle = alloc_handle( current->process, info->process,
934 PROCESS_ALL_ACCESS, req->pinherit );
935 reply->thandle = alloc_handle( current->process, info->thread,
936 THREAD_ALL_ACCESS, req->tinherit );
937 reply->success = is_process_init_done( info->process );
938 release_object( info );
950 /* Retrieve the new process startup info */
951 DECL_HANDLER(get_startup_info)
953 struct startup_info *info;
955 if ((info = current->process->startup_info))
957 size_t size = info->data_size;
958 if (size > get_reply_max_size()) size = get_reply_max_size();
960 /* we return the data directly without making a copy so this can only be called once */
961 set_reply_data_ptr( info->data, size );
968 /* initialize a new process */
969 DECL_HANDLER(init_process)
971 if (current->unix_pid == -1)
973 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
976 if (current->process->startup_info)
978 fatal_protocol_error( current, "init_process: called twice\n" );
981 if (!req->peb || (unsigned int)req->peb % sizeof(int))
983 fatal_protocol_error( current, "init_process: bad peb address\n" );
986 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
988 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
991 reply->info_size = 0;
992 current->process->peb = req->peb;
993 current->process->ldt_copy = req->ldt_copy;
994 current->process->startup_info = init_process( reply );
997 /* signal the end of the process initialization */
998 DECL_HANDLER(init_process_done)
1000 struct file *file = NULL;
1001 struct process *process = current->process;
1003 if (is_process_init_done(process))
1005 fatal_protocol_error( current, "init_process_done: called twice\n" );
1010 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
1013 process->exe.base = req->module;
1014 process->exe.size = req->module_size;
1015 process->exe.name = req->name;
1017 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
1018 if (process->exe.file) release_object( process->exe.file );
1019 process->exe.file = file;
1021 if ((process->exe.namelen = get_req_data_size()))
1022 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1024 generate_startup_debug_events( process, req->entry );
1025 set_process_startup_state( process, STARTUP_DONE );
1027 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1028 if (current->suspend + process->suspend > 0) stop_thread( current );
1029 if (process->debugger) set_process_debug_flag( process, 1 );
1032 /* open a handle to a process */
1033 DECL_HANDLER(open_process)
1035 struct process *process = get_process_from_id( req->pid );
1039 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1040 release_object( process );
1044 /* terminate a process */
1045 DECL_HANDLER(terminate_process)
1047 struct process *process;
1049 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1051 reply->self = (current->process == process);
1052 kill_process( process, current, req->exit_code );
1053 release_object( process );
1057 /* fetch information about a process */
1058 DECL_HANDLER(get_process_info)
1060 struct process *process;
1062 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1064 reply->pid = get_process_id( process );
1065 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1066 reply->exit_code = process->exit_code;
1067 reply->priority = process->priority;
1068 reply->process_affinity = process->affinity;
1069 reply->system_affinity = 1;
1070 reply->peb = process->peb;
1071 release_object( process );
1075 /* set information about a process */
1076 DECL_HANDLER(set_process_info)
1078 struct process *process;
1080 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1082 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1083 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1085 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1086 else process->affinity = req->affinity;
1088 release_object( process );
1092 /* read data from a process address space */
1093 DECL_HANDLER(read_process_memory)
1095 struct process *process;
1096 size_t len = get_reply_max_size();
1098 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1102 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1103 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1104 const int *start = (int *)((char *)req->addr - start_offset);
1105 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1108 if (read_process_memory( process, start, nb_ints, buffer ))
1110 /* move start of requested data to start of buffer */
1111 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1112 set_reply_data_ptr( buffer, len );
1117 release_object( process );
1120 /* write data to a process address space */
1121 DECL_HANDLER(write_process_memory)
1123 struct process *process;
1125 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1127 size_t len = get_req_data_size();
1128 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1129 set_error( STATUS_INVALID_PARAMETER );
1132 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1133 req->first_mask, req->last_mask, get_req_data() );
1135 release_object( process );
1139 /* notify the server that a dll has been loaded */
1140 DECL_HANDLER(load_dll)
1142 struct process_dll *dll;
1143 struct file *file = NULL;
1145 if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1148 if ((dll = process_load_dll( current->process, file, req->base,
1149 get_req_data(), get_req_data_size() )))
1151 dll->size = req->size;
1152 dll->dbg_offset = req->dbg_offset;
1153 dll->dbg_size = req->dbg_size;
1154 dll->name = req->name;
1155 /* only generate event if initialization is done */
1156 if (is_process_init_done( current->process ))
1157 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1159 if (file) release_object( file );
1162 /* notify the server that a dll is being unloaded */
1163 DECL_HANDLER(unload_dll)
1165 process_unload_dll( current->process, req->base );
1168 /* retrieve information about a module in a process */
1169 DECL_HANDLER(get_dll_info)
1171 struct process *process;
1173 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1175 struct process_dll *dll = find_process_dll( process, req->base_address );
1179 reply->size = dll->size;
1180 reply->entry_point = 0; /* FIXME */
1183 size_t len = min( dll->namelen, get_reply_max_size() );
1184 set_reply_data( dll->filename, len );
1188 set_error( STATUS_DLL_NOT_FOUND );
1190 release_object( process );
1194 /* wait for a process to start waiting on input */
1195 /* FIXME: only returns event for now, wait is done in the client */
1196 DECL_HANDLER(wait_input_idle)
1198 struct process *process;
1201 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1203 if (process->idle_event && process != current->process && process->queue != current->queue)
1204 reply->event = alloc_handle( current->process, process->idle_event,
1205 EVENT_ALL_ACCESS, 0 );
1206 release_object( process );