Wrappers must always be compiled in STRICT mode.
[wine] / server / process.c
1 /*
2  * Server-side process management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <signal.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/time.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
19 #endif
20 #include <unistd.h>
21
22 #include "winbase.h"
23 #include "winnt.h"
24
25 #include "handle.h"
26 #include "process.h"
27 #include "thread.h"
28 #include "request.h"
29
30 /* process structure */
31
32 static struct process *first_process;
33 static int running_processes;
34
35 /* process operations */
36
37 static void process_dump( struct object *obj, int verbose );
38 static int process_signaled( struct object *obj, struct thread *thread );
39 static void process_poll_event( struct object *obj, int event );
40 static void process_destroy( struct object *obj );
41
42 static const struct object_ops process_ops =
43 {
44     sizeof(struct process),      /* size */
45     process_dump,                /* dump */
46     add_queue,                   /* add_queue */
47     remove_queue,                /* remove_queue */
48     process_signaled,            /* signaled */
49     no_satisfied,                /* satisfied */
50     NULL,                        /* get_poll_events */
51     process_poll_event,          /* poll_event */
52     no_get_fd,                   /* get_fd */
53     no_flush,                    /* flush */
54     no_get_file_info,            /* get_file_info */
55     process_destroy              /* destroy */
56 };
57
58 /* process startup info */
59
60 struct startup_info
61 {
62     struct object       obj;          /* object header */
63     int                 inherit_all;  /* inherit all handles from parent */
64     int                 create_flags; /* creation flags */
65     int                 start_flags;  /* flags from startup info */
66     handle_t            hstdin;       /* handle for stdin */
67     handle_t            hstdout;      /* handle for stdout */
68     handle_t            hstderr;      /* handle for stderr */
69     int                 cmd_show;     /* main window show mode */
70     struct file        *exe_file;     /* file handle for main exe */
71     char               *filename;     /* file name for main exe */
72     struct thread      *owner;        /* owner thread (the one that created the new process) */
73     struct process     *process;      /* created process */
74     struct thread      *thread;       /* created thread */
75 };
76
77 static void startup_info_dump( struct object *obj, int verbose );
78 static int startup_info_signaled( struct object *obj, struct thread *thread );
79 static void startup_info_destroy( struct object *obj );
80
81 static const struct object_ops startup_info_ops =
82 {
83     sizeof(struct startup_info),   /* size */
84     startup_info_dump,             /* dump */
85     add_queue,                     /* add_queue */
86     remove_queue,                  /* remove_queue */
87     startup_info_signaled,         /* signaled */
88     no_satisfied,                  /* satisfied */
89     NULL,                          /* get_poll_events */
90     NULL,                          /* poll_event */
91     no_get_fd,                     /* get_fd */
92     no_flush,                      /* flush */
93     no_get_file_info,              /* get_file_info */
94     startup_info_destroy           /* destroy */
95 };
96
97
98 /* set the console and stdio handles for a newly created process */
99 static int set_process_console( struct process *process, struct process *parent,
100                                 struct startup_info *info, struct init_process_request *req )
101 {
102     if (process->create_flags & CREATE_NEW_CONSOLE)
103     {
104         if (!alloc_console( process )) return 0;
105     }
106     else if (parent && !(process->create_flags & DETACHED_PROCESS))
107     {
108         if (parent->console_in) process->console_in = grab_object( parent->console_in );
109         if (parent->console_out) process->console_out = grab_object( parent->console_out );
110     }
111     if (parent)
112     {
113         if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
114         {
115             /* duplicate the handle from the parent into this process */
116             req->hstdin  = duplicate_handle( parent, info->hstdin, process,
117                                              0, TRUE, DUPLICATE_SAME_ACCESS );
118             req->hstdout = duplicate_handle( parent, info->hstdout, process,
119                                              0, TRUE, DUPLICATE_SAME_ACCESS );
120             req->hstderr = duplicate_handle( parent, info->hstderr, process,
121                                              0, TRUE, DUPLICATE_SAME_ACCESS );
122         }
123         else
124         {
125             req->hstdin  = info->hstdin;
126             req->hstdout = info->hstdout;
127             req->hstderr = info->hstderr;
128         }
129     }
130     else
131     {
132         /* no parent, use handles to the console for stdio */
133         req->hstdin  = alloc_handle( process, process->console_in,
134                                      GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
135         req->hstdout = alloc_handle( process, process->console_out,
136                                      GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
137         req->hstderr = alloc_handle( process, process->console_out,
138                                      GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
139     }
140     /* some handles above may have been invalid; this is not an error */
141     if (get_error() == STATUS_INVALID_HANDLE) clear_error();
142     return 1;
143 }
144
145 /* create a new process and its main thread */
146 struct thread *create_process( int fd )
147 {
148     struct process *process;
149     struct thread *thread = NULL;
150     int request_pipe[2];
151
152     if (!(process = alloc_object( &process_ops, fd ))) return NULL;
153     process->next            = NULL;
154     process->prev            = NULL;
155     process->thread_list     = NULL;
156     process->debugger        = NULL;
157     process->handles         = NULL;
158     process->exit_code       = STILL_ACTIVE;
159     process->running_threads = 0;
160     process->priority        = NORMAL_PRIORITY_CLASS;
161     process->affinity        = 1;
162     process->suspend         = 0;
163     process->create_flags    = 0;
164     process->console_in      = NULL;
165     process->console_out     = NULL;
166     process->init_event      = NULL;
167     process->idle_event      = NULL;
168     process->queue           = NULL;
169     process->atom_table      = NULL;
170     process->ldt_copy        = NULL;
171     process->exe.next        = NULL;
172     process->exe.prev        = NULL;
173     process->exe.file        = NULL;
174     process->exe.dbg_offset  = 0;
175     process->exe.dbg_size    = 0;
176     gettimeofday( &process->start_time, NULL );
177     if ((process->next = first_process) != NULL) process->next->prev = process;
178     first_process = process;
179
180     /* create the init done event */
181     if (!(process->init_event = create_event( NULL, 0, 1, 0 ))) goto error;
182
183     /* create the main thread */
184     if (pipe( request_pipe ) == -1)
185     {
186         file_set_error();
187         goto error;
188     }
189     send_client_fd( process, request_pipe[1], 0 );
190     close( request_pipe[1] );
191     if (!(thread = create_thread( request_pipe[0], process ))) goto error;
192
193     set_select_events( &process->obj, POLLIN );  /* start listening to events */
194     release_object( process );
195     return thread;
196
197  error:
198     if (thread) release_object( thread );
199     release_object( process );
200     return NULL;
201 }
202
203 /* initialize the current process and fill in the request */
204 static void init_process( int ppid, struct init_process_request *req )
205 {
206     struct process *process = current->process;
207     struct thread *parent_thread = get_thread_from_pid( ppid );
208     struct process *parent = NULL;
209     struct startup_info *info = NULL;
210
211     if (parent_thread)
212     {
213         parent = parent_thread->process;
214         info = parent_thread->info;
215         if (!info)
216         {
217             fatal_protocol_error( current, "init_process: parent but no info\n" );
218             return;
219         }
220         if (info->thread)
221         {
222             fatal_protocol_error( current, "init_process: called twice?\n" );
223             return;
224         }
225     }
226
227     /* set the process flags */
228     process->create_flags = info ? info->create_flags : CREATE_NEW_CONSOLE;
229
230     /* create the handle table */
231     if (parent && info->inherit_all)
232         process->handles = copy_handle_table( process, parent );
233     else
234         process->handles = alloc_handle_table( process, 0 );
235     if (!process->handles) goto error;
236
237     /* retrieve the main exe file */
238     req->exe_file = 0;
239     if (parent && info->exe_file)
240     {
241         process->exe.file = (struct file *)grab_object( info->exe_file );
242         if (!(req->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
243             goto error;
244     }
245
246     /* set the process console */
247     if (!set_process_console( process, parent, info, req )) goto error;
248
249     if (parent)
250     {
251         /* attach to the debugger if requested */
252         if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
253             set_process_debugger( process, parent_thread );
254         else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
255             set_process_debugger( process, parent->debugger );
256     }
257
258     /* thread will be actually suspended in init_done */
259     if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
260
261     if (info)
262     {
263         size_t size = strlen(info->filename);
264         if (size > get_req_data_size(req)) size = get_req_data_size(req);
265         req->start_flags = info->start_flags;
266         req->cmd_show    = info->cmd_show;
267         memcpy( get_req_data(req), info->filename, size );
268         set_req_data_size( req, size );
269         info->process = (struct process *)grab_object( process );
270         info->thread  = (struct thread *)grab_object( current );
271         wake_up( &info->obj, 0 );
272     }
273     else
274     {
275         req->start_flags  = STARTF_USESTDHANDLES;
276         req->cmd_show     = 0;
277         set_req_data_size( req, 0 );
278     }
279     req->create_flags = process->create_flags;
280     req->server_start = server_start_ticks;
281  error:
282 }
283
284 /* destroy a process when its refcount is 0 */
285 static void process_destroy( struct object *obj )
286 {
287     struct process *process = (struct process *)obj;
288     assert( obj->ops == &process_ops );
289
290     /* we can't have a thread remaining */
291     assert( !process->thread_list );
292     if (process->next) process->next->prev = process->prev;
293     if (process->prev) process->prev->next = process->next;
294     else first_process = process->next;
295     if (process->init_event) release_object( process->init_event );
296     if (process->idle_event) release_object( process->idle_event );
297     if (process->queue) release_object( process->queue );
298     if (process->atom_table) release_object( process->atom_table );
299     if (process->exe.file) release_object( process->exe.file );
300 }
301
302 /* dump a process on stdout for debugging purposes */
303 static void process_dump( struct object *obj, int verbose )
304 {
305     struct process *process = (struct process *)obj;
306     assert( obj->ops == &process_ops );
307
308     fprintf( stderr, "Process next=%p prev=%p console=%p/%p handles=%p\n",
309              process->next, process->prev, process->console_in, process->console_out,
310              process->handles );
311 }
312
313 static int process_signaled( struct object *obj, struct thread *thread )
314 {
315     struct process *process = (struct process *)obj;
316     return !process->running_threads;
317 }
318
319
320 static void process_poll_event( struct object *obj, int event )
321 {
322     struct process *process = (struct process *)obj;
323     assert( obj->ops == &process_ops );
324
325     if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
326     else if (event & POLLIN) receive_fd( process );
327 }
328
329 static void startup_info_destroy( struct object *obj )
330 {
331     struct startup_info *info = (struct startup_info *)obj;
332     assert( obj->ops == &startup_info_ops );
333     if (info->filename) free( info->filename );
334     if (info->exe_file) release_object( info->exe_file );
335     if (info->process) release_object( info->process );
336     if (info->thread) release_object( info->thread );
337     if (info->owner)
338     {
339         info->owner->info = NULL;
340         release_object( info->owner );
341     }
342 }
343
344 static void startup_info_dump( struct object *obj, int verbose )
345 {
346     struct startup_info *info = (struct startup_info *)obj;
347     assert( obj->ops == &startup_info_ops );
348
349     fprintf( stderr, "Startup info flags=%x in=%d out=%d err=%d name='%s'\n",
350              info->start_flags, info->hstdin, info->hstdout, info->hstderr, info->filename );
351 }
352
353 static int startup_info_signaled( struct object *obj, struct thread *thread )
354 {
355     struct startup_info *info = (struct startup_info *)obj;
356     return (info->thread != NULL);
357 }
358
359
360 /* get a process from an id (and increment the refcount) */
361 struct process *get_process_from_id( void *id )
362 {
363     struct process *p = first_process;
364     while (p && (p != id)) p = p->next;
365     if (p) grab_object( p );
366     else set_error( STATUS_INVALID_PARAMETER );
367     return p;
368 }
369
370 /* get a process from a handle (and increment the refcount) */
371 struct process *get_process_from_handle( handle_t handle, unsigned int access )
372 {
373     return (struct process *)get_handle_obj( current->process, handle,
374                                              access, &process_ops );
375 }
376
377 /* add a dll to a process list */
378 static struct process_dll *process_load_dll( struct process *process, struct file *file,
379                                              void *base )
380 {
381     struct process_dll *dll;
382
383     /* make sure we don't already have one with the same base address */
384     for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
385     {
386         set_error( STATUS_INVALID_PARAMETER );
387         return NULL;
388     }
389
390     if ((dll = mem_alloc( sizeof(*dll) )))
391     {
392         dll->prev = &process->exe;
393         dll->file = NULL;
394         dll->base = base;
395         if (file) dll->file = (struct file *)grab_object( file );
396         if ((dll->next = process->exe.next)) dll->next->prev = dll;
397         process->exe.next = dll;
398     }
399     return dll;
400 }
401
402 /* remove a dll from a process list */
403 static void process_unload_dll( struct process *process, void *base )
404 {
405     struct process_dll *dll;
406
407     for (dll = process->exe.next; dll; dll = dll->next)
408     {
409         if (dll->base == base)
410         {
411             if (dll->file) release_object( dll->file );
412             if (dll->next) dll->next->prev = dll->prev;
413             if (dll->prev) dll->prev->next = dll->next;
414             free( dll );
415             generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
416             return;
417         }
418     }
419     set_error( STATUS_INVALID_PARAMETER );
420 }
421
422 /* a process has been killed (i.e. its last thread died) */
423 static void process_killed( struct process *process )
424 {
425     assert( !process->thread_list );
426     gettimeofday( &process->end_time, NULL );
427     if (process->handles) release_object( process->handles );
428     process->handles = NULL;
429     free_console( process );
430     while (process->exe.next)
431     {
432         struct process_dll *dll = process->exe.next;
433         process->exe.next = dll->next;
434         if (dll->file) release_object( dll->file );
435         free( dll );
436     }
437     if (process->exe.file) release_object( process->exe.file );
438     process->exe.file = NULL;
439     wake_up( &process->obj, 0 );
440     if (!--running_processes)
441     {
442         /* last process died, close global handles */
443         close_global_handles();
444         /* this will cause the select loop to terminate */
445         if (!persistent_server) close_master_socket();
446     }
447 }
448
449 /* add a thread to a process running threads list */
450 void add_process_thread( struct process *process, struct thread *thread )
451 {
452     thread->proc_next = process->thread_list;
453     thread->proc_prev = NULL;
454     if (thread->proc_next) thread->proc_next->proc_prev = thread;
455     process->thread_list = thread;
456     if (!process->running_threads++) running_processes++;
457     grab_object( thread );
458 }
459
460 /* remove a thread from a process running threads list */
461 void remove_process_thread( struct process *process, struct thread *thread )
462 {
463     assert( process->running_threads > 0 );
464     assert( process->thread_list );
465
466     if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
467     if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
468     else process->thread_list = thread->proc_next;
469
470     if (!--process->running_threads)
471     {
472         /* we have removed the last running thread, exit the process */
473         process->exit_code = thread->exit_code;
474         generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
475         process_killed( process );
476     }
477     else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
478     release_object( thread );
479 }
480
481 /* suspend all the threads of a process */
482 void suspend_process( struct process *process )
483 {
484     if (!process->suspend++)
485     {
486         struct thread *thread = process->thread_list;
487         for (; thread; thread = thread->proc_next)
488         {
489             if (!thread->suspend) stop_thread( thread );
490         }
491     }
492 }
493
494 /* resume all the threads of a process */
495 void resume_process( struct process *process )
496 {
497     assert (process->suspend > 0);
498     if (!--process->suspend)
499     {
500         struct thread *thread = process->thread_list;
501         for (; thread; thread = thread->proc_next)
502         {
503             if (!thread->suspend) continue_thread( thread );
504         }
505     }
506 }
507
508 /* kill a process on the spot */
509 void kill_process( struct process *process, struct thread *skip, int exit_code )
510 {
511     struct thread *thread = process->thread_list;
512     while (thread)
513     {
514         struct thread *next = thread->proc_next;
515         thread->exit_code = exit_code;
516         if (thread != skip) kill_thread( thread, 1 );
517         thread = next;
518     }
519 }
520
521 /* kill all processes being debugged by a given thread */
522 void kill_debugged_processes( struct thread *debugger, int exit_code )
523 {
524     for (;;)  /* restart from the beginning of the list every time */
525     {
526         struct process *process = first_process;
527         /* find the first process being debugged by 'debugger' and still running */
528         while (process && (process->debugger != debugger || !process->running_threads))
529             process = process->next;
530         if (!process) return;
531         process->debugger = NULL;
532         kill_process( process, NULL, exit_code );
533     }
534 }
535
536 /* get all information about a process */
537 static void get_process_info( struct process *process, struct get_process_info_request *req )
538 {
539     req->pid              = process;
540     req->debugged         = (process->debugger != 0);
541     req->exit_code        = process->exit_code;
542     req->priority         = process->priority;
543     req->process_affinity = process->affinity;
544     req->system_affinity  = 1;
545 }
546
547 /* set all information about a process */
548 static void set_process_info( struct process *process,
549                               struct set_process_info_request *req )
550 {
551     if (req->mask & SET_PROCESS_INFO_PRIORITY)
552         process->priority = req->priority;
553     if (req->mask & SET_PROCESS_INFO_AFFINITY)
554     {
555         if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
556         else process->affinity = req->affinity;
557     }
558 }
559
560 /* read data from a process memory space */
561 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
562 /* we read the total size in all cases to check for permissions */
563 static void read_process_memory( struct process *process, const int *addr,
564                                  size_t len, size_t max, int *dest )
565 {
566     struct thread *thread = process->thread_list;
567
568     if ((unsigned int)addr % sizeof(int))  /* address must be aligned */
569     {
570         set_error( STATUS_INVALID_PARAMETER );
571         return;
572     }
573     if (!thread)  /* process is dead */
574     {
575         set_error( STATUS_ACCESS_DENIED );
576         return;
577     }
578     if (suspend_for_ptrace( thread ))
579     {
580         while (len > 0 && max)
581         {
582             if (read_thread_int( thread, addr++, dest++ ) == -1) goto done;
583             max--;
584             len--;
585         }
586         /* check the rest for read permission */
587         if (len > 0)
588         {
589             int dummy, page = get_page_size() / sizeof(int);
590             while (len >= page)
591             {
592                 addr += page;
593                 len -= page;
594                 if (read_thread_int( thread, addr - 1, &dummy ) == -1) goto done;
595             }
596             if (len && (read_thread_int( thread, addr + len - 1, &dummy ) == -1)) goto done;
597         }
598     done:
599         resume_thread( thread );
600     }
601 }
602
603 /* write data to a process memory space */
604 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
605 /* we check the total size for write permissions */
606 static void write_process_memory( struct process *process, int *addr, size_t len,
607                                   size_t max, unsigned int first_mask,
608                                   unsigned int last_mask, const int *src )
609 {
610     struct thread *thread = process->thread_list;
611
612     if (!len || ((unsigned int)addr % sizeof(int)))  /* address must be aligned */
613     {
614         set_error( STATUS_INVALID_PARAMETER );
615         return;
616     }
617     if (!thread)  /* process is dead */
618     {
619         set_error( STATUS_ACCESS_DENIED );
620         return;
621     }
622     if (suspend_for_ptrace( thread ))
623     {
624         /* first word is special */
625         if (len > 1)
626         {
627             if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
628             len--;
629             max--;
630         }
631         else last_mask &= first_mask;
632
633         while (len > 1 && max)
634         {
635             if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
636             max--;
637             len--;
638         }
639
640         if (max)
641         {
642             /* last word is special too */
643             if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
644         }
645         else
646         {
647             /* check the rest for write permission */
648             int page = get_page_size() / sizeof(int);
649             while (len >= page)
650             {
651                 addr += page;
652                 len -= page;
653                 if (write_thread_int( thread, addr - 1, 0, 0 ) == -1) goto done;
654             }
655             if (len && (write_thread_int( thread, addr + len - 1, 0, 0 ) == -1)) goto done;
656         }
657     done:
658         resume_thread( thread );
659     }
660 }
661
662 /* take a snapshot of currently running processes */
663 struct process_snapshot *process_snap( int *count )
664 {
665     struct process_snapshot *snapshot, *ptr;
666     struct process *process;
667     if (!running_processes) return NULL;
668     if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
669         return NULL;
670     ptr = snapshot;
671     for (process = first_process; process; process = process->next)
672     {
673         if (!process->running_threads) continue;
674         ptr->process  = process;
675         ptr->threads  = process->running_threads;
676         ptr->count    = process->obj.refcount;
677         ptr->priority = process->priority;
678         grab_object( process );
679         ptr++;
680     }
681     *count = running_processes;
682     return snapshot;
683 }
684
685 /* take a snapshot of the modules of a process */
686 struct module_snapshot *module_snap( struct process *process, int *count )
687 {
688     struct module_snapshot *snapshot, *ptr;
689     struct process_dll *dll;
690     int total = 0;
691
692     for (dll = &process->exe; dll; dll = dll->next) total++;
693     if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
694
695     for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
696     {
697         ptr->base = dll->base;
698     }
699     *count = total;
700     return snapshot;
701 }
702
703
704 /* create a new process */
705 DECL_HANDLER(new_process)
706 {
707     size_t len = get_req_data_size( req );
708     struct startup_info *info;
709
710     if (current->info)
711     {
712         fatal_protocol_error( current, "new_process: another process is being created\n" );
713         return;
714     }
715
716     /* build the startup info for a new process */
717     if (!(info = alloc_object( &startup_info_ops, -1 ))) return;
718     info->inherit_all  = req->inherit_all;
719     info->create_flags = req->create_flags;
720     info->start_flags  = req->start_flags;
721     info->hstdin       = req->hstdin;
722     info->hstdout      = req->hstdout;
723     info->hstderr      = req->hstderr;
724     info->cmd_show     = req->cmd_show;
725     info->exe_file     = NULL;
726     info->filename     = NULL;
727     info->owner        = (struct thread *)grab_object( current );
728     info->process      = NULL;
729     info->thread       = NULL;
730
731     if (req->exe_file &&
732         !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
733         goto done;
734
735     if (!(info->filename = mem_alloc( len + 1 ))) goto done;
736
737     memcpy( info->filename, get_req_data(req), len );
738     info->filename[len] = 0;
739     current->info = info;
740     req->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
741
742  done:
743     release_object( info );
744 }
745
746 /* Retrieve information about a newly started process */
747 DECL_HANDLER(get_new_process_info)
748 {
749     struct startup_info *info;
750
751     req->event = 0;
752
753     if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
754                                                        0, &startup_info_ops )))
755     {
756         req->pid = get_process_id( info->process );
757         req->tid = get_thread_id( info->thread );
758         req->phandle = alloc_handle( current->process, info->process,
759                                      PROCESS_ALL_ACCESS, req->pinherit );
760         req->thandle = alloc_handle( current->process, info->thread,
761                                      THREAD_ALL_ACCESS, req->tinherit );
762         if (info->process->init_event)
763             req->event = alloc_handle( current->process, info->process->init_event,
764                                        EVENT_ALL_ACCESS, 0 );
765         release_object( info );
766     }
767     else
768     {
769         req->pid     = 0;
770         req->tid     = 0;
771         req->phandle = 0;
772         req->thandle = 0;
773     }
774 }
775
776 /* initialize a new process */
777 DECL_HANDLER(init_process)
778 {
779     if (!current->unix_pid)
780     {
781         fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
782         return;
783     }
784     current->process->ldt_copy  = req->ldt_copy;
785     init_process( req->ppid, req );
786 }
787
788 /* signal the end of the process initialization */
789 DECL_HANDLER(init_process_done)
790 {
791     struct file *file;
792     struct process *process = current->process;
793     if (!process->init_event)
794     {
795         fatal_protocol_error( current, "init_process_done: no event\n" );
796         return;
797     }
798     process->exe.base = req->module;
799     process->exe.name = req->name;
800
801     if (req->exe_file && (file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
802     {
803         if (process->exe.file) release_object( process->exe.file );
804         process->exe.file = file;
805     }
806     generate_startup_debug_events( current->process, req->entry );
807     set_event( process->init_event );
808     release_object( process->init_event );
809     process->init_event = NULL;
810     if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
811     if (current->suspend + current->process->suspend > 0) stop_thread( current );
812     req->debugged = (current->process->debugger != 0);
813 }
814
815 /* open a handle to a process */
816 DECL_HANDLER(open_process)
817 {
818     struct process *process = get_process_from_id( req->pid );
819     req->handle = 0;
820     if (process)
821     {
822         req->handle = alloc_handle( current->process, process, req->access, req->inherit );
823         release_object( process );
824     }
825 }
826
827 /* terminate a process */
828 DECL_HANDLER(terminate_process)
829 {
830     struct process *process;
831
832     if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
833     {
834         req->self = (current->process == process);
835         kill_process( process, current, req->exit_code );
836         release_object( process );
837     }
838 }
839
840 /* fetch information about a process */
841 DECL_HANDLER(get_process_info)
842 {
843     struct process *process;
844
845     if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
846     {
847         get_process_info( process, req );
848         release_object( process );
849     }
850 }
851
852 /* set information about a process */
853 DECL_HANDLER(set_process_info)
854 {
855     struct process *process;
856
857     if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
858     {
859         set_process_info( process, req );
860         release_object( process );
861     }
862 }
863
864 /* read data from a process address space */
865 DECL_HANDLER(read_process_memory)
866 {
867     struct process *process;
868
869     if ((process = get_process_from_handle( req->handle, PROCESS_VM_READ )))
870     {
871         size_t maxlen = get_req_data_size(req) / sizeof(int);
872         read_process_memory( process, req->addr, req->len, maxlen, get_req_data(req) );
873         release_object( process );
874     }
875 }
876
877 /* write data to a process address space */
878 DECL_HANDLER(write_process_memory)
879 {
880     struct process *process;
881
882     if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
883     {
884         size_t maxlen = get_req_data_size(req) / sizeof(int);
885         write_process_memory( process, req->addr, req->len, maxlen,
886                               req->first_mask, req->last_mask, get_req_data(req) );
887         release_object( process );
888     }
889 }
890
891 /* notify the server that a dll has been loaded */
892 DECL_HANDLER(load_dll)
893 {
894     struct process_dll *dll;
895     struct file *file = NULL;
896
897     if (req->handle &&
898         !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
899
900     if ((dll = process_load_dll( current->process, file, req->base )))
901     {
902         dll->dbg_offset = req->dbg_offset;
903         dll->dbg_size   = req->dbg_size;
904         dll->name       = req->name;
905         /* only generate event if initialization is done */
906         if (!current->process->init_event)
907             generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
908     }
909     if (file) release_object( file );
910 }
911
912 /* notify the server that a dll is being unloaded */
913 DECL_HANDLER(unload_dll)
914 {
915     process_unload_dll( current->process, req->base );
916 }
917
918 /* wait for a process to start waiting on input */
919 /* FIXME: only returns event for now, wait is done in the client */
920 DECL_HANDLER(wait_input_idle)
921 {
922     struct process *process;
923
924     req->event = 0;
925     if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
926     {
927         if (process->idle_event && process != current->process && process->queue != current->queue)
928             req->event = alloc_handle( current->process, process->idle_event,
929                                        EVENT_ALL_ACCESS, 0 );
930         release_object( process );
931     }
932 }