Release 20031118.
[wine/multimedia.git] / server / process.c
blobfd96614dc7d16845e4f81674a72df2672abcc3d1
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnt.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "process.h"
44 #include "thread.h"
45 #include "request.h"
46 #include "console.h"
48 /* process structure */
50 static struct process *first_process;
51 static int running_processes;
53 /* process operations */
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 );
60 static const struct object_ops process_ops =
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 */
72 static const struct fd_ops process_fd_ops =
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 */
81 /* process startup info */
83 struct startup_info
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 void *data; /* data for startup info */
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 );
105 static const struct object_ops startup_info_ops =
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 */
118 static struct list startup_info_list = LIST_INIT(startup_info_list);
120 struct ptid_entry
122 void *ptr; /* entry ptr */
123 unsigned int next; /* next free entry */
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 */
132 #define PTID_OFFSET 8 /* offset for first ptid value */
134 /* allocate a new process or thread id */
135 unsigned int alloc_ptid( void *ptr )
137 struct ptid_entry *entry;
138 unsigned int id;
140 if (used_ptid_entries < alloc_ptid_entries)
142 id = used_ptid_entries + PTID_OFFSET;
143 entry = &ptid_entries[used_ptid_entries++];
145 else if (next_free_ptid)
147 id = next_free_ptid;
148 entry = &ptid_entries[id - PTID_OFFSET];
149 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
151 else /* need to grow the array */
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) )))
157 set_error( STATUS_NO_MEMORY );
158 return 0;
160 ptid_entries = entry;
161 alloc_ptid_entries = count;
162 id = used_ptid_entries + PTID_OFFSET;
163 entry = &ptid_entries[used_ptid_entries++];
166 entry->ptr = ptr;
167 return id;
170 /* free a process or thread id */
171 void free_ptid( unsigned int id )
173 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
175 entry->ptr = NULL;
176 entry->next = 0;
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;
182 last_free_ptid = id;
185 /* retrieve the pointer corresponding to a process or thread id */
186 void *get_ptid_entry( unsigned int id )
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;
193 /* set the state of the process startup info */
194 static void set_process_startup_state( struct process *process, enum startup_state state )
196 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
197 if (process->startup_info)
199 wake_up( &process->startup_info->obj, 0 );
200 release_object( process->startup_info );
201 process->startup_info = NULL;
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 )
209 if (process->create_flags & CREATE_NEW_CONSOLE)
211 /* let the process init do the allocation */
212 return 1;
214 else if (info && !(process->create_flags & DETACHED_PROCESS))
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
220 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
222 if (info)
224 if (!info->inherit_all)
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 );
233 else
235 reply->hstdin = info->hstdin;
236 reply->hstdout = info->hstdout;
237 reply->hstderr = info->hstderr;
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;
247 /* create a new process and its main thread */
248 struct thread *create_process( int fd )
250 struct process *process;
251 struct thread *thread = NULL;
252 int request_pipe[2];
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->peb = NULL;
275 process->ldt_copy = NULL;
276 process->exe.next = NULL;
277 process->exe.prev = NULL;
278 process->exe.file = NULL;
279 process->exe.dbg_offset = 0;
280 process->exe.dbg_size = 0;
281 process->exe.namelen = 0;
282 process->exe.filename = NULL;
283 process->group_id = 0;
284 process->token = create_token();
285 list_init( &process->locks );
287 gettimeofday( &process->start_time, NULL );
288 if ((process->next = first_process) != NULL) process->next->prev = process;
289 first_process = process;
291 if (!(process->id = alloc_ptid( process ))) goto error;
292 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
294 /* create the main thread */
295 if (pipe( request_pipe ) == -1)
297 file_set_error();
298 goto error;
300 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
302 close( request_pipe[0] );
303 close( request_pipe[1] );
304 goto error;
306 close( request_pipe[1] );
307 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
309 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
310 release_object( process );
311 return thread;
313 error:
314 if (process) release_object( process );
315 /* if we failed to start our first process, close everything down */
316 if (!running_processes) close_master_socket();
317 return NULL;
320 /* find the startup info for a given Unix process */
321 inline static struct startup_info *find_startup_info( int unix_pid )
323 struct list *ptr;
325 LIST_FOR_EACH( ptr, &startup_info_list )
327 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
328 if (info->unix_pid == unix_pid) return info;
330 return NULL;
333 /* initialize the current process and fill in the request */
334 static struct startup_info *init_process( struct init_process_reply *reply )
336 struct process *process = current->process;
337 struct thread *parent_thread = NULL;
338 struct process *parent = NULL;
339 struct startup_info *info = find_startup_info( current->unix_pid );
341 if (info)
343 if (info->thread)
345 fatal_protocol_error( current, "init_process: called twice?\n" );
346 return NULL;
348 parent_thread = info->owner;
349 parent = parent_thread->process;
350 process->parent = (struct process *)grab_object( parent );
353 /* set the process flags */
354 process->create_flags = info ? info->create_flags : 0;
356 /* create the handle table */
357 if (info && info->inherit_all)
358 process->handles = copy_handle_table( process, parent );
359 else
360 process->handles = alloc_handle_table( process, 0 );
361 if (!process->handles) return NULL;
363 /* retrieve the main exe file */
364 reply->exe_file = 0;
365 if (info && info->exe_file)
367 process->exe.file = (struct file *)grab_object( info->exe_file );
368 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
369 return NULL;
372 /* set the process console */
373 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
375 process->group_id = get_process_id( process );
376 if (parent)
378 /* attach to the debugger if requested */
379 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
380 set_process_debugger( process, parent_thread );
381 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
382 set_process_debugger( process, parent->debugger );
383 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
384 process->group_id = parent->group_id;
387 /* thread will be actually suspended in init_done */
388 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
390 if (info)
392 reply->info_size = info->data_size;
393 info->process = (struct process *)grab_object( process );
394 info->thread = (struct thread *)grab_object( current );
396 reply->create_flags = process->create_flags;
397 reply->server_start = server_start_ticks;
398 return info ? (struct startup_info *)grab_object( info ) : NULL;
401 /* destroy a process when its refcount is 0 */
402 static void process_destroy( struct object *obj )
404 struct process *process = (struct process *)obj;
405 assert( obj->ops == &process_ops );
407 /* we can't have a thread remaining */
408 assert( !process->thread_list );
410 set_process_startup_state( process, STARTUP_ABORTED );
411 if (process->console) release_object( process->console );
412 if (process->parent) release_object( process->parent );
413 if (process->msg_fd) release_object( process->msg_fd );
414 if (process->next) process->next->prev = process->prev;
415 if (process->prev) process->prev->next = process->next;
416 else first_process = process->next;
417 if (process->idle_event) release_object( process->idle_event );
418 if (process->queue) release_object( process->queue );
419 if (process->atom_table) release_object( process->atom_table );
420 if (process->exe.file) release_object( process->exe.file );
421 if (process->exe.filename) free( process->exe.filename );
422 if (process->id) free_ptid( process->id );
423 if (process->token) release_object( process->token );
426 /* dump a process on stdout for debugging purposes */
427 static void process_dump( struct object *obj, int verbose )
429 struct process *process = (struct process *)obj;
430 assert( obj->ops == &process_ops );
432 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
433 process->id, process->next, process->prev, process->handles );
436 static int process_signaled( struct object *obj, struct thread *thread )
438 struct process *process = (struct process *)obj;
439 return !process->running_threads;
442 static void process_poll_event( struct fd *fd, int event )
444 struct process *process = get_fd_user( fd );
445 assert( process->obj.ops == &process_ops );
447 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
448 else if (event & POLLIN) receive_fd( process );
451 static void startup_info_destroy( struct object *obj )
453 struct startup_info *info = (struct startup_info *)obj;
454 assert( obj->ops == &startup_info_ops );
455 list_remove( &info->entry );
456 if (info->data) free( info->data );
457 if (info->exe_file) release_object( info->exe_file );
458 if (info->process) release_object( info->process );
459 if (info->thread) release_object( info->thread );
460 if (info->owner) release_object( info->owner );
463 static void startup_info_dump( struct object *obj, int verbose )
465 struct startup_info *info = (struct startup_info *)obj;
466 assert( obj->ops == &startup_info_ops );
468 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
469 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
472 static int startup_info_signaled( struct object *obj, struct thread *thread )
474 struct startup_info *info = (struct startup_info *)obj;
475 return info->process && is_process_init_done(info->process);
478 /* get a process from an id (and increment the refcount) */
479 struct process *get_process_from_id( process_id_t id )
481 struct object *obj = get_ptid_entry( id );
483 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
484 set_error( STATUS_INVALID_PARAMETER );
485 return NULL;
488 /* get a process from a handle (and increment the refcount) */
489 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
491 return (struct process *)get_handle_obj( current->process, handle,
492 access, &process_ops );
495 /* add a dll to a process list */
496 static struct process_dll *process_load_dll( struct process *process, struct file *file,
497 void *base, const WCHAR *filename, size_t name_len )
499 struct process_dll *dll;
501 /* make sure we don't already have one with the same base address */
502 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
504 set_error( STATUS_INVALID_PARAMETER );
505 return NULL;
508 if ((dll = mem_alloc( sizeof(*dll) )))
510 dll->prev = &process->exe;
511 dll->file = NULL;
512 dll->base = base;
513 dll->filename = NULL;
514 dll->namelen = name_len;
515 if (name_len && !(dll->filename = memdup( filename, name_len )))
517 free( dll );
518 return NULL;
520 if (file) dll->file = (struct file *)grab_object( file );
521 if ((dll->next = process->exe.next)) dll->next->prev = dll;
522 process->exe.next = dll;
524 return dll;
527 /* remove a dll from a process list */
528 static void process_unload_dll( struct process *process, void *base )
530 struct process_dll *dll;
532 for (dll = process->exe.next; dll; dll = dll->next)
534 if (dll->base == base)
536 if (dll->file) release_object( dll->file );
537 if (dll->next) dll->next->prev = dll->prev;
538 if (dll->prev) dll->prev->next = dll->next;
539 if (dll->filename) free( dll->filename );
540 free( dll );
541 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
542 return;
545 set_error( STATUS_INVALID_PARAMETER );
548 /* kill all processes */
549 void kill_all_processes( struct process *skip, int exit_code )
551 for (;;)
553 struct process *process = first_process;
555 while (process && (!process->running_threads || process == skip))
556 process = process->next;
557 if (!process) break;
558 kill_process( process, NULL, exit_code );
562 /* kill all processes being attached to a console renderer */
563 void kill_console_processes( struct thread *renderer, int exit_code )
565 for (;;) /* restart from the beginning of the list every time */
567 struct process *process = first_process;
569 /* find the first process being attached to 'renderer' and still running */
570 while (process &&
571 (process == renderer->process || !process->console ||
572 process->console->renderer != renderer || !process->running_threads))
574 process = process->next;
576 if (!process) break;
577 kill_process( process, NULL, exit_code );
581 /* a process has been killed (i.e. its last thread died) */
582 static void process_killed( struct process *process )
584 assert( !process->thread_list );
585 gettimeofday( &process->end_time, NULL );
586 if (process->handles) release_object( process->handles );
587 process->handles = NULL;
589 /* close the console attached to this process, if any */
590 free_console( process );
592 while (process->exe.next)
594 struct process_dll *dll = process->exe.next;
595 process->exe.next = dll->next;
596 if (dll->file) release_object( dll->file );
597 if (dll->filename) free( dll->filename );
598 free( dll );
600 set_process_startup_state( process, STARTUP_ABORTED );
601 if (process->exe.file) release_object( process->exe.file );
602 process->exe.file = NULL;
603 wake_up( &process->obj, 0 );
604 if (!--running_processes) close_master_socket();
607 /* add a thread to a process running threads list */
608 void add_process_thread( struct process *process, struct thread *thread )
610 thread->proc_next = process->thread_list;
611 thread->proc_prev = NULL;
612 if (thread->proc_next) thread->proc_next->proc_prev = thread;
613 process->thread_list = thread;
614 if (!process->running_threads++) running_processes++;
615 grab_object( thread );
618 /* remove a thread from a process running threads list */
619 void remove_process_thread( struct process *process, struct thread *thread )
621 assert( process->running_threads > 0 );
622 assert( process->thread_list );
624 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
625 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
626 else process->thread_list = thread->proc_next;
628 if (!--process->running_threads)
630 /* we have removed the last running thread, exit the process */
631 process->exit_code = thread->exit_code;
632 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
633 process_killed( process );
635 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
636 release_object( thread );
639 /* suspend all the threads of a process */
640 void suspend_process( struct process *process )
642 if (!process->suspend++)
644 struct thread *thread = process->thread_list;
645 while (thread)
647 struct thread *next = thread->proc_next;
648 if (!thread->suspend) stop_thread( thread );
649 thread = next;
654 /* resume all the threads of a process */
655 void resume_process( struct process *process )
657 assert (process->suspend > 0);
658 if (!--process->suspend)
660 struct thread *thread = process->thread_list;
661 while (thread)
663 struct thread *next = thread->proc_next;
664 if (!thread->suspend) wake_thread( thread );
665 thread = next;
670 /* kill a process on the spot */
671 void kill_process( struct process *process, struct thread *skip, int exit_code )
673 struct thread *thread = process->thread_list;
675 while (thread)
677 struct thread *next = thread->proc_next;
678 thread->exit_code = exit_code;
679 if (thread != skip) kill_thread( thread, 1 );
680 thread = next;
684 /* kill all processes being debugged by a given thread */
685 void kill_debugged_processes( struct thread *debugger, int exit_code )
687 for (;;) /* restart from the beginning of the list every time */
689 struct process *process = first_process;
690 /* find the first process being debugged by 'debugger' and still running */
691 while (process && (process->debugger != debugger || !process->running_threads))
692 process = process->next;
693 if (!process) return;
694 process->debugger = NULL;
695 kill_process( process, NULL, exit_code );
700 /* detach a debugger from all its debuggees */
701 void detach_debugged_processes( struct thread *debugger )
703 struct process *process;
704 for (process = first_process; process; process = process->next)
706 if (process->debugger == debugger && process->running_threads)
708 debugger_detach( process, debugger );
714 void enum_processes( int (*cb)(struct process*, void*), void *user )
716 struct process *process;
717 for (process = first_process; process; process = process->next)
719 if ((cb)(process, user)) break;
724 /* read data from a process memory space */
725 /* len is the total size (in ints) */
726 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
728 struct thread *thread = process->thread_list;
730 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
732 if (!thread) /* process is dead */
734 set_error( STATUS_ACCESS_DENIED );
735 return 0;
737 if (suspend_for_ptrace( thread ))
739 while (len > 0)
741 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
742 len--;
744 resume_after_ptrace( thread );
746 return !len;
749 /* make sure we can write to the whole address range */
750 /* len is the total size (in ints) */
751 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
753 int page = get_page_size() / sizeof(int);
755 for (;;)
757 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
758 if (len <= page) break;
759 addr += page;
760 len -= page;
762 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
765 /* write data to a process memory space */
766 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
767 /* we check the total size for write permissions */
768 static int write_process_memory( struct process *process, int *addr, size_t len,
769 unsigned int first_mask, unsigned int last_mask, const int *src )
771 struct thread *thread = process->thread_list;
772 int ret = 0;
774 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
776 if (!thread) /* process is dead */
778 set_error( STATUS_ACCESS_DENIED );
779 return 0;
781 if (suspend_for_ptrace( thread ))
783 if (!check_process_write_access( thread, addr, len ))
785 set_error( STATUS_ACCESS_DENIED );
786 goto done;
788 /* first word is special */
789 if (len > 1)
791 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
792 len--;
794 else last_mask &= first_mask;
796 while (len > 1)
798 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
799 len--;
802 /* last word is special too */
803 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
804 ret = 1;
806 done:
807 resume_after_ptrace( thread );
809 return ret;
812 /* set the debugged flag in the process PEB */
813 int set_process_debug_flag( struct process *process, int flag )
815 int mask = 0, data = 0;
817 /* BeingDebugged flag is the byte at offset 2 in the PEB */
818 memset( (char *)&mask + 2, 0xff, 1 );
819 memset( (char *)&data + 2, flag, 1 );
820 return write_process_memory( process, process->peb, 1, mask, mask, &data );
823 /* take a snapshot of currently running processes */
824 struct process_snapshot *process_snap( int *count )
826 struct process_snapshot *snapshot, *ptr;
827 struct process *process;
828 if (!running_processes) return NULL;
829 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
830 return NULL;
831 ptr = snapshot;
832 for (process = first_process; process; process = process->next)
834 if (!process->running_threads) continue;
835 ptr->process = process;
836 ptr->threads = process->running_threads;
837 ptr->count = process->obj.refcount;
838 ptr->priority = process->priority;
839 ptr->handles = get_handle_table_count(process);
840 grab_object( process );
841 ptr++;
843 *count = running_processes;
844 return snapshot;
847 /* take a snapshot of the modules of a process */
848 struct module_snapshot *module_snap( struct process *process, int *count )
850 struct module_snapshot *snapshot, *ptr;
851 struct process_dll *dll;
852 int total = 0;
854 for (dll = &process->exe; dll; dll = dll->next) total++;
855 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
857 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
859 ptr->base = dll->base;
860 ptr->size = dll->size;
861 ptr->namelen = dll->namelen;
862 ptr->filename = memdup( dll->filename, dll->namelen );
864 *count = total;
865 return snapshot;
869 /* create a new process */
870 DECL_HANDLER(new_process)
872 struct startup_info *info;
874 /* build the startup info for a new process */
875 if (!(info = alloc_object( &startup_info_ops ))) return;
876 list_add_head( &startup_info_list, &info->entry );
877 info->inherit_all = req->inherit_all;
878 info->create_flags = req->create_flags;
879 info->unix_pid = req->unix_pid;
880 info->hstdin = req->hstdin;
881 info->hstdout = req->hstdout;
882 info->hstderr = req->hstderr;
883 info->exe_file = NULL;
884 info->owner = (struct thread *)grab_object( current );
885 info->process = NULL;
886 info->thread = NULL;
887 info->data_size = get_req_data_size();
888 info->data = NULL;
890 if (req->exe_file &&
891 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
892 goto done;
894 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
895 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
897 done:
898 release_object( info );
901 /* Retrieve information about a newly started process */
902 DECL_HANDLER(get_new_process_info)
904 struct startup_info *info;
906 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
907 0, &startup_info_ops )))
909 reply->pid = get_process_id( info->process );
910 reply->tid = get_thread_id( info->thread );
911 reply->phandle = alloc_handle( current->process, info->process,
912 PROCESS_ALL_ACCESS, req->pinherit );
913 reply->thandle = alloc_handle( current->process, info->thread,
914 THREAD_ALL_ACCESS, req->tinherit );
915 reply->success = is_process_init_done( info->process );
916 release_object( info );
918 else
920 reply->pid = 0;
921 reply->tid = 0;
922 reply->phandle = 0;
923 reply->thandle = 0;
924 reply->success = 0;
928 /* Retrieve the new process startup info */
929 DECL_HANDLER(get_startup_info)
931 struct startup_info *info;
933 if ((info = current->process->startup_info))
935 size_t size = info->data_size;
936 if (size > get_reply_max_size()) size = get_reply_max_size();
938 /* we return the data directly without making a copy so this can only be called once */
939 set_reply_data_ptr( info->data, size );
940 info->data = NULL;
941 info->data_size = 0;
946 /* initialize a new process */
947 DECL_HANDLER(init_process)
949 if (current->unix_pid == -1)
951 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
952 return;
954 if (current->process->startup_info)
956 fatal_protocol_error( current, "init_process: called twice\n" );
957 return;
959 if (!req->peb || (unsigned int)req->peb % sizeof(int))
961 fatal_protocol_error( current, "init_process: bad peb address\n" );
962 return;
964 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
966 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
967 return;
969 reply->info_size = 0;
970 current->process->peb = req->peb;
971 current->process->ldt_copy = req->ldt_copy;
972 current->process->startup_info = init_process( reply );
975 /* signal the end of the process initialization */
976 DECL_HANDLER(init_process_done)
978 struct file *file = NULL;
979 struct process *process = current->process;
981 if (is_process_init_done(process))
983 fatal_protocol_error( current, "init_process_done: called twice\n" );
984 return;
986 if (!req->module)
988 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
989 return;
991 process->exe.base = req->module;
992 process->exe.size = req->module_size;
993 process->exe.name = req->name;
995 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
996 if (process->exe.file) release_object( process->exe.file );
997 process->exe.file = file;
999 if ((process->exe.namelen = get_req_data_size()))
1000 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1002 generate_startup_debug_events( process, req->entry );
1003 set_process_startup_state( process, STARTUP_DONE );
1005 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1006 if (current->suspend + process->suspend > 0) stop_thread( current );
1007 if (process->debugger) set_process_debug_flag( process, 1 );
1010 /* open a handle to a process */
1011 DECL_HANDLER(open_process)
1013 struct process *process = get_process_from_id( req->pid );
1014 reply->handle = 0;
1015 if (process)
1017 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1018 release_object( process );
1022 /* terminate a process */
1023 DECL_HANDLER(terminate_process)
1025 struct process *process;
1027 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1029 reply->self = (current->process == process);
1030 kill_process( process, current, req->exit_code );
1031 release_object( process );
1035 /* fetch information about a process */
1036 DECL_HANDLER(get_process_info)
1038 struct process *process;
1040 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1042 reply->pid = get_process_id( process );
1043 reply->exit_code = process->exit_code;
1044 reply->priority = process->priority;
1045 reply->process_affinity = process->affinity;
1046 reply->system_affinity = 1;
1047 release_object( process );
1051 /* set information about a process */
1052 DECL_HANDLER(set_process_info)
1054 struct process *process;
1056 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1058 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1059 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1061 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1062 else process->affinity = req->affinity;
1064 release_object( process );
1068 /* read data from a process address space */
1069 DECL_HANDLER(read_process_memory)
1071 struct process *process;
1072 size_t len = get_reply_max_size();
1074 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1076 if (len)
1078 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1079 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1080 const int *start = (int *)((char *)req->addr - start_offset);
1081 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1082 if (buffer)
1084 if (read_process_memory( process, start, nb_ints, buffer ))
1086 /* move start of requested data to start of buffer */
1087 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1088 set_reply_data_ptr( buffer, len );
1090 else len = 0;
1093 release_object( process );
1096 /* write data to a process address space */
1097 DECL_HANDLER(write_process_memory)
1099 struct process *process;
1101 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1103 size_t len = get_req_data_size();
1104 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1105 set_error( STATUS_INVALID_PARAMETER );
1106 else
1108 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1109 req->first_mask, req->last_mask, get_req_data() );
1111 release_object( process );
1115 /* notify the server that a dll has been loaded */
1116 DECL_HANDLER(load_dll)
1118 struct process_dll *dll;
1119 struct file *file = NULL;
1121 if (req->handle)
1123 if (!(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1124 if (is_file_removable( file )) /* don't keep the file open on removable media */
1126 release_object( file );
1127 file = NULL;
1131 if ((dll = process_load_dll( current->process, file, req->base,
1132 get_req_data(), get_req_data_size() )))
1134 dll->size = req->size;
1135 dll->dbg_offset = req->dbg_offset;
1136 dll->dbg_size = req->dbg_size;
1137 dll->name = req->name;
1138 /* only generate event if initialization is done */
1139 if (is_process_init_done( current->process ))
1140 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1142 if (file) release_object( file );
1145 /* notify the server that a dll is being unloaded */
1146 DECL_HANDLER(unload_dll)
1148 process_unload_dll( current->process, req->base );
1151 /* retrieve information about a module in a process */
1152 DECL_HANDLER(get_dll_info)
1154 struct process *process;
1156 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1158 struct process_dll *dll;
1160 for (dll = &process->exe; dll; dll = dll->next)
1162 if (dll->base == req->base_address)
1164 reply->size = dll->size;
1165 reply->entry_point = 0; /* FIXME */
1166 if (dll->filename)
1168 size_t len = min( dll->namelen, get_reply_max_size() );
1169 set_reply_data( dll->filename, len );
1171 break;
1174 if (!dll)
1175 set_error(STATUS_DLL_NOT_FOUND);
1176 release_object( process );
1180 /* wait for a process to start waiting on input */
1181 /* FIXME: only returns event for now, wait is done in the client */
1182 DECL_HANDLER(wait_input_idle)
1184 struct process *process;
1186 reply->event = 0;
1187 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1189 if (process->idle_event && process != current->process && process->queue != current->queue)
1190 reply->event = alloc_handle( current->process, process->idle_event,
1191 EVENT_ALL_ACCESS, 0 );
1192 release_object( process );