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