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