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