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