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