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