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