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