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