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