Add definitions for the S_IS* macros.
[wine/multimedia.git] / server / process.c
blob333e11c2dd8d5e646b634b99b27582cae92f02eb
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 <stdio.h>
29 #include <stdlib.h>
30 #include <sys/time.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
33 #endif
34 #include <unistd.h>
36 #include "winbase.h"
37 #include "winnt.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "process.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "console.h"
46 /* process structure */
48 static struct process *first_process;
49 static int running_processes;
51 /* process operations */
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 fd *fd, int event );
56 static void process_destroy( struct object *obj );
58 static const struct object_ops process_ops =
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 no_get_fd, /* get_fd */
67 process_destroy /* destroy */
70 static const struct fd_ops process_fd_ops =
72 NULL, /* get_poll_events */
73 process_poll_event, /* poll_event */
74 no_flush, /* flush */
75 no_get_file_info, /* get_file_info */
76 no_queue_async /* queue_async */
79 /* process startup info */
81 struct startup_info
83 struct object obj; /* object header */
84 struct list entry; /* entry in list of startup infos */
85 int inherit_all; /* inherit all handles from parent */
86 int use_handles; /* use stdio handles */
87 int create_flags; /* creation flags */
88 int unix_pid; /* Unix pid of new process */
89 obj_handle_t hstdin; /* handle for stdin */
90 obj_handle_t hstdout; /* handle for stdout */
91 obj_handle_t hstderr; /* handle for stderr */
92 struct file *exe_file; /* file handle for main exe */
93 struct thread *owner; /* owner thread (the one that created the new process) */
94 struct process *process; /* created process */
95 struct thread *thread; /* created thread */
96 size_t data_size; /* size of startup data */
97 startup_info_t *data; /* data for startup info */
100 static void startup_info_dump( struct object *obj, int verbose );
101 static int startup_info_signaled( struct object *obj, struct thread *thread );
102 static void startup_info_destroy( struct object *obj );
104 static const struct object_ops startup_info_ops =
106 sizeof(struct startup_info), /* size */
107 startup_info_dump, /* dump */
108 add_queue, /* add_queue */
109 remove_queue, /* remove_queue */
110 startup_info_signaled, /* signaled */
111 no_satisfied, /* satisfied */
112 no_get_fd, /* get_fd */
113 startup_info_destroy /* destroy */
117 static struct list startup_info_list = LIST_INIT(startup_info_list);
119 struct ptid_entry
121 void *ptr; /* entry ptr */
122 unsigned int next; /* next free entry */
125 static struct ptid_entry *ptid_entries; /* array of ptid entries */
126 static unsigned int used_ptid_entries; /* number of entries in use */
127 static unsigned int alloc_ptid_entries; /* number of allocated entries */
128 static unsigned int next_free_ptid; /* next free entry */
129 static unsigned int last_free_ptid; /* last free entry */
131 #define PTID_OFFSET 8 /* offset for first ptid value */
133 /* allocate a new process or thread id */
134 unsigned int alloc_ptid( void *ptr )
136 struct ptid_entry *entry;
137 unsigned int id;
139 if (used_ptid_entries < alloc_ptid_entries)
141 id = used_ptid_entries + PTID_OFFSET;
142 entry = &ptid_entries[used_ptid_entries++];
144 else if (next_free_ptid)
146 id = next_free_ptid;
147 entry = &ptid_entries[id - PTID_OFFSET];
148 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
150 else /* need to grow the array */
152 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
153 if (!count) count = 64;
154 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
156 set_error( STATUS_NO_MEMORY );
157 return 0;
159 ptid_entries = entry;
160 alloc_ptid_entries = count;
161 id = used_ptid_entries + PTID_OFFSET;
162 entry = &ptid_entries[used_ptid_entries++];
165 entry->ptr = ptr;
166 return id;
169 /* free a process or thread id */
170 void free_ptid( unsigned int id )
172 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
174 entry->ptr = NULL;
175 entry->next = 0;
177 /* append to end of free list so that we don't reuse it too early */
178 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
179 else next_free_ptid = id;
181 last_free_ptid = id;
184 /* retrieve the pointer corresponding to a process or thread id */
185 void *get_ptid_entry( unsigned int id )
187 if (id < PTID_OFFSET) return NULL;
188 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
189 return ptid_entries[id - PTID_OFFSET].ptr;
192 /* set the state of the process startup info */
193 static void set_process_startup_state( struct process *process, enum startup_state state )
195 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
196 if (process->startup_info)
198 wake_up( &process->startup_info->obj, 0 );
199 release_object( process->startup_info );
200 process->startup_info = NULL;
204 /* set the console and stdio handles for a newly created process */
205 static int set_process_console( struct process *process, struct thread *parent_thread,
206 struct startup_info *info, struct init_process_reply *reply )
208 if (process->create_flags & CREATE_NEW_CONSOLE)
210 /* let the process init do the allocation */
211 return 1;
213 else if (info && !(process->create_flags & DETACHED_PROCESS))
215 /* FIXME: some better error checking should be done...
216 * like if hConOut and hConIn are console handles, then they should be on the same
217 * physical console
219 inherit_console( parent_thread, process,
220 (info->inherit_all || info->use_handles) ? info->hstdin : 0 );
222 if (info)
224 reply->hstdin = info->hstdin;
225 reply->hstdout = info->hstdout;
226 reply->hstderr = info->hstderr;
228 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
229 /* some handles above may have been invalid; this is not an error */
230 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
231 return 1;
234 /* create a new process and its main thread */
235 struct thread *create_process( int fd )
237 struct process *process;
238 struct thread *thread = NULL;
239 int request_pipe[2];
241 if (!(process = alloc_object( &process_ops ))) goto error;
242 process->next = NULL;
243 process->prev = NULL;
244 process->parent = NULL;
245 process->thread_list = NULL;
246 process->debugger = NULL;
247 process->handles = NULL;
248 process->msg_fd = NULL;
249 process->exit_code = STILL_ACTIVE;
250 process->running_threads = 0;
251 process->priority = NORMAL_PRIORITY_CLASS;
252 process->affinity = 1;
253 process->suspend = 0;
254 process->create_flags = 0;
255 process->console = NULL;
256 process->startup_state = STARTUP_IN_PROGRESS;
257 process->startup_info = NULL;
258 process->idle_event = NULL;
259 process->queue = NULL;
260 process->atom_table = NULL;
261 process->ldt_copy = NULL;
262 process->exe.next = NULL;
263 process->exe.prev = NULL;
264 process->exe.file = NULL;
265 process->exe.dbg_offset = 0;
266 process->exe.dbg_size = 0;
267 process->exe.namelen = 0;
268 process->exe.filename = NULL;
269 process->group_id = 0;
270 process->token = create_token();
271 list_init( &process->locks );
273 gettimeofday( &process->start_time, NULL );
274 if ((process->next = first_process) != NULL) process->next->prev = process;
275 first_process = process;
277 if (!(process->id = alloc_ptid( process ))) goto error;
278 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
280 /* create the main thread */
281 if (pipe( request_pipe ) == -1)
283 file_set_error();
284 goto error;
286 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
288 close( request_pipe[0] );
289 close( request_pipe[1] );
290 goto error;
292 close( request_pipe[1] );
293 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
295 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
296 release_object( process );
297 return thread;
299 error:
300 if (process) release_object( process );
301 /* if we failed to start our first process, close everything down */
302 if (!running_processes) close_master_socket();
303 return NULL;
306 /* find the startup info for a given Unix process */
307 inline static struct startup_info *find_startup_info( int unix_pid )
309 struct list *ptr;
311 LIST_FOR_EACH( ptr, &startup_info_list )
313 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
314 if (info->unix_pid == unix_pid) return info;
316 return NULL;
319 /* initialize the current process and fill in the request */
320 static struct startup_info *init_process( struct init_process_reply *reply )
322 struct process *process = current->process;
323 struct thread *parent_thread = NULL;
324 struct process *parent = NULL;
325 struct startup_info *info = find_startup_info( current->unix_pid );
327 if (info)
329 if (info->thread)
331 fatal_protocol_error( current, "init_process: called twice?\n" );
332 return NULL;
334 parent_thread = info->owner;
335 parent = parent_thread->process;
336 process->parent = (struct process *)grab_object( parent );
339 /* set the process flags */
340 process->create_flags = info ? info->create_flags : 0;
342 /* create the handle table */
343 if (info && info->inherit_all)
344 process->handles = copy_handle_table( process, parent );
345 else
346 process->handles = alloc_handle_table( process, 0 );
347 if (!process->handles) return NULL;
349 /* retrieve the main exe file */
350 reply->exe_file = 0;
351 if (info && info->exe_file)
353 process->exe.file = (struct file *)grab_object( info->exe_file );
354 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
355 return NULL;
358 /* set the process console */
359 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
361 process->group_id = get_process_id( process );
362 if (parent)
364 /* attach to the debugger if requested */
365 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
366 set_process_debugger( process, parent_thread );
367 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
368 set_process_debugger( process, parent->debugger );
369 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
370 process->group_id = parent->group_id;
373 /* thread will be actually suspended in init_done */
374 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
376 if (info)
378 reply->info_size = info->data_size;
379 info->process = (struct process *)grab_object( process );
380 info->thread = (struct thread *)grab_object( current );
382 reply->create_flags = process->create_flags;
383 reply->server_start = server_start_ticks;
384 return info ? (struct startup_info *)grab_object( info ) : NULL;
387 /* destroy a process when its refcount is 0 */
388 static void process_destroy( struct object *obj )
390 struct process *process = (struct process *)obj;
391 assert( obj->ops == &process_ops );
393 /* we can't have a thread remaining */
394 assert( !process->thread_list );
396 set_process_startup_state( process, STARTUP_ABORTED );
397 if (process->console) release_object( process->console );
398 if (process->parent) release_object( process->parent );
399 if (process->msg_fd) release_object( process->msg_fd );
400 if (process->next) process->next->prev = process->prev;
401 if (process->prev) process->prev->next = process->next;
402 else first_process = process->next;
403 if (process->idle_event) release_object( process->idle_event );
404 if (process->queue) release_object( process->queue );
405 if (process->atom_table) release_object( process->atom_table );
406 if (process->exe.file) release_object( process->exe.file );
407 if (process->exe.filename) free( process->exe.filename );
408 if (process->id) free_ptid( process->id );
409 if (process->token) release_object( process->token );
412 /* dump a process on stdout for debugging purposes */
413 static void process_dump( struct object *obj, int verbose )
415 struct process *process = (struct process *)obj;
416 assert( obj->ops == &process_ops );
418 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
419 process->id, process->next, process->prev, process->handles );
422 static int process_signaled( struct object *obj, struct thread *thread )
424 struct process *process = (struct process *)obj;
425 return !process->running_threads;
428 static void process_poll_event( struct fd *fd, int event )
430 struct process *process = get_fd_user( fd );
431 assert( process->obj.ops == &process_ops );
433 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
434 else if (event & POLLIN) receive_fd( process );
437 static void startup_info_destroy( struct object *obj )
439 struct startup_info *info = (struct startup_info *)obj;
440 assert( obj->ops == &startup_info_ops );
441 list_remove( &info->entry );
442 if (info->data) free( info->data );
443 if (info->exe_file) release_object( info->exe_file );
444 if (info->process) release_object( info->process );
445 if (info->thread) release_object( info->thread );
446 if (info->owner) release_object( info->owner );
449 static void startup_info_dump( struct object *obj, int verbose )
451 struct startup_info *info = (struct startup_info *)obj;
452 assert( obj->ops == &startup_info_ops );
454 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
455 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
458 static int startup_info_signaled( struct object *obj, struct thread *thread )
460 struct startup_info *info = (struct startup_info *)obj;
461 return info->process && is_process_init_done(info->process);
464 /* get a process from an id (and increment the refcount) */
465 struct process *get_process_from_id( process_id_t id )
467 struct object *obj = get_ptid_entry( id );
469 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
470 set_error( STATUS_INVALID_PARAMETER );
471 return NULL;
474 /* get a process from a handle (and increment the refcount) */
475 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
477 return (struct process *)get_handle_obj( current->process, handle,
478 access, &process_ops );
481 /* add a dll to a process list */
482 static struct process_dll *process_load_dll( struct process *process, struct file *file,
483 void *base, const char *filename, size_t name_len )
485 struct process_dll *dll;
487 /* make sure we don't already have one with the same base address */
488 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
490 set_error( STATUS_INVALID_PARAMETER );
491 return NULL;
494 if ((dll = mem_alloc( sizeof(*dll) )))
496 dll->prev = &process->exe;
497 dll->file = NULL;
498 dll->base = base;
499 dll->filename = NULL;
500 dll->namelen = name_len;
501 if (name_len && !(dll->filename = memdup( filename, name_len )))
503 free( dll );
504 return NULL;
506 if (file) dll->file = (struct file *)grab_object( file );
507 if ((dll->next = process->exe.next)) dll->next->prev = dll;
508 process->exe.next = dll;
510 return dll;
513 /* remove a dll from a process list */
514 static void process_unload_dll( struct process *process, void *base )
516 struct process_dll *dll;
518 for (dll = process->exe.next; dll; dll = dll->next)
520 if (dll->base == base)
522 if (dll->file) release_object( dll->file );
523 if (dll->next) dll->next->prev = dll->prev;
524 if (dll->prev) dll->prev->next = dll->next;
525 if (dll->filename) free( dll->filename );
526 free( dll );
527 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
528 return;
531 set_error( STATUS_INVALID_PARAMETER );
534 /* kill all processes */
535 void kill_all_processes( struct process *skip, int exit_code )
537 for (;;)
539 struct process *process = first_process;
541 while (process && (!process->running_threads || process == skip))
542 process = process->next;
543 if (!process) break;
544 kill_process( process, NULL, exit_code );
548 /* kill all processes being attached to a console renderer */
549 void kill_console_processes( struct thread *renderer, int exit_code )
551 for (;;) /* restart from the beginning of the list every time */
553 struct process *process = first_process;
555 /* find the first process being attached to 'renderer' and still running */
556 while (process &&
557 (process == renderer->process || !process->console ||
558 process->console->renderer != renderer || !process->running_threads))
560 process = process->next;
562 if (!process) break;
563 kill_process( process, NULL, exit_code );
567 /* a process has been killed (i.e. its last thread died) */
568 static void process_killed( struct process *process )
570 assert( !process->thread_list );
571 gettimeofday( &process->end_time, NULL );
572 if (process->handles) release_object( process->handles );
573 process->handles = NULL;
575 /* close the console attached to this process, if any */
576 free_console( process );
578 while (process->exe.next)
580 struct process_dll *dll = process->exe.next;
581 process->exe.next = dll->next;
582 if (dll->file) release_object( dll->file );
583 if (dll->filename) free( dll->filename );
584 free( dll );
586 set_process_startup_state( process, STARTUP_ABORTED );
587 if (process->exe.file) release_object( process->exe.file );
588 process->exe.file = NULL;
589 wake_up( &process->obj, 0 );
590 if (!--running_processes) close_master_socket();
593 /* add a thread to a process running threads list */
594 void add_process_thread( struct process *process, struct thread *thread )
596 thread->proc_next = process->thread_list;
597 thread->proc_prev = NULL;
598 if (thread->proc_next) thread->proc_next->proc_prev = thread;
599 process->thread_list = thread;
600 if (!process->running_threads++) running_processes++;
601 grab_object( thread );
604 /* remove a thread from a process running threads list */
605 void remove_process_thread( struct process *process, struct thread *thread )
607 assert( process->running_threads > 0 );
608 assert( process->thread_list );
610 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
611 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
612 else process->thread_list = thread->proc_next;
614 if (!--process->running_threads)
616 /* we have removed the last running thread, exit the process */
617 process->exit_code = thread->exit_code;
618 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
619 process_killed( process );
621 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
622 release_object( thread );
625 /* suspend all the threads of a process */
626 void suspend_process( struct process *process )
628 if (!process->suspend++)
630 struct thread *thread = process->thread_list;
631 while (thread)
633 struct thread *next = thread->proc_next;
634 if (!thread->suspend) stop_thread( thread );
635 thread = next;
640 /* resume all the threads of a process */
641 void resume_process( struct process *process )
643 assert (process->suspend > 0);
644 if (!--process->suspend)
646 struct thread *thread = process->thread_list;
647 while (thread)
649 struct thread *next = thread->proc_next;
650 if (!thread->suspend) wake_thread( thread );
651 thread = next;
656 /* kill a process on the spot */
657 void kill_process( struct process *process, struct thread *skip, int exit_code )
659 struct thread *thread = process->thread_list;
661 while (thread)
663 struct thread *next = thread->proc_next;
664 thread->exit_code = exit_code;
665 if (thread != skip) kill_thread( thread, 1 );
666 thread = next;
670 /* kill all processes being debugged by a given thread */
671 void kill_debugged_processes( struct thread *debugger, int exit_code )
673 for (;;) /* restart from the beginning of the list every time */
675 struct process *process = first_process;
676 /* find the first process being debugged by 'debugger' and still running */
677 while (process && (process->debugger != debugger || !process->running_threads))
678 process = process->next;
679 if (!process) return;
680 process->debugger = NULL;
681 kill_process( process, NULL, exit_code );
686 /* detach a debugger from all its debuggees */
687 void detach_debugged_processes( struct thread *debugger )
689 struct process *process;
690 for (process = first_process; process; process = process->next)
692 if (process->debugger == debugger && process->running_threads)
694 debugger_detach( process, debugger );
700 void enum_processes( int (*cb)(struct process*, void*), void *user )
702 struct process *process;
703 for (process = first_process; process; process = process->next)
705 if ((cb)(process, user)) break;
710 /* get all information about a process */
711 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
713 reply->pid = get_process_id( process );
714 reply->debugged = (process->debugger != 0);
715 reply->exit_code = process->exit_code;
716 reply->priority = process->priority;
717 reply->process_affinity = process->affinity;
718 reply->system_affinity = 1;
721 /* set all information about a process */
722 static void set_process_info( struct process *process,
723 const struct set_process_info_request *req )
725 if (req->mask & SET_PROCESS_INFO_PRIORITY)
726 process->priority = req->priority;
727 if (req->mask & SET_PROCESS_INFO_AFFINITY)
729 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
730 else process->affinity = req->affinity;
734 /* read data from a process memory space */
735 /* len is the total size (in ints) */
736 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
738 struct thread *thread = process->thread_list;
740 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
742 if (!thread) /* process is dead */
744 set_error( STATUS_ACCESS_DENIED );
745 return 0;
747 if (suspend_for_ptrace( thread ))
749 while (len > 0)
751 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
752 len--;
754 resume_after_ptrace( thread );
756 return !len;
759 /* make sure we can write to the whole address range */
760 /* len is the total size (in ints) */
761 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
763 int page = get_page_size() / sizeof(int);
765 for (;;)
767 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
768 if (len <= page) break;
769 addr += page;
770 len -= page;
772 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
775 /* write data to a process memory space */
776 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
777 /* we check the total size for write permissions */
778 static void write_process_memory( struct process *process, int *addr, size_t len,
779 unsigned int first_mask, unsigned int last_mask, const int *src )
781 struct thread *thread = process->thread_list;
783 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
785 if (!thread) /* process is dead */
787 set_error( STATUS_ACCESS_DENIED );
788 return;
790 if (suspend_for_ptrace( thread ))
792 if (!check_process_write_access( thread, addr, len ))
794 set_error( STATUS_ACCESS_DENIED );
795 goto done;
797 /* first word is special */
798 if (len > 1)
800 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
801 len--;
803 else last_mask &= first_mask;
805 while (len > 1)
807 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
808 len--;
811 /* last word is special too */
812 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
814 done:
815 resume_after_ptrace( thread );
819 /* take a snapshot of currently running processes */
820 struct process_snapshot *process_snap( int *count )
822 struct process_snapshot *snapshot, *ptr;
823 struct process *process;
824 if (!running_processes) return NULL;
825 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
826 return NULL;
827 ptr = snapshot;
828 for (process = first_process; process; process = process->next)
830 if (!process->running_threads) continue;
831 ptr->process = process;
832 ptr->threads = process->running_threads;
833 ptr->count = process->obj.refcount;
834 ptr->priority = process->priority;
835 grab_object( process );
836 ptr++;
838 *count = running_processes;
839 return snapshot;
842 /* take a snapshot of the modules of a process */
843 struct module_snapshot *module_snap( struct process *process, int *count )
845 struct module_snapshot *snapshot, *ptr;
846 struct process_dll *dll;
847 int total = 0;
849 for (dll = &process->exe; dll; dll = dll->next) total++;
850 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
852 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
854 ptr->base = dll->base;
855 ptr->size = dll->size;
856 ptr->namelen = dll->namelen;
857 ptr->filename = memdup( dll->filename, dll->namelen );
859 *count = total;
860 return snapshot;
864 /* create a new process */
865 DECL_HANDLER(new_process)
867 struct startup_info *info;
869 /* build the startup info for a new process */
870 if (!(info = alloc_object( &startup_info_ops ))) return;
871 list_add_head( &startup_info_list, &info->entry );
872 info->inherit_all = req->inherit_all;
873 info->use_handles = req->use_handles;
874 info->create_flags = req->create_flags;
875 info->unix_pid = req->unix_pid;
876 info->hstdin = req->hstdin;
877 info->hstdout = req->hstdout;
878 info->hstderr = req->hstderr;
879 info->exe_file = NULL;
880 info->owner = (struct thread *)grab_object( current );
881 info->process = NULL;
882 info->thread = NULL;
883 info->data_size = get_req_data_size();
884 info->data = NULL;
886 if (req->exe_file &&
887 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
888 goto done;
890 if (!(info->data = mem_alloc( info->data_size ))) goto done;
891 memcpy( info->data, get_req_data(), info->data_size );
892 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
894 done:
895 release_object( info );
898 /* Retrieve information about a newly started process */
899 DECL_HANDLER(get_new_process_info)
901 struct startup_info *info;
903 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
904 0, &startup_info_ops )))
906 reply->pid = get_process_id( info->process );
907 reply->tid = get_thread_id( info->thread );
908 reply->phandle = alloc_handle( current->process, info->process,
909 PROCESS_ALL_ACCESS, req->pinherit );
910 reply->thandle = alloc_handle( current->process, info->thread,
911 THREAD_ALL_ACCESS, req->tinherit );
912 reply->success = is_process_init_done( info->process );
913 release_object( info );
915 else
917 reply->pid = 0;
918 reply->tid = 0;
919 reply->phandle = 0;
920 reply->thandle = 0;
921 reply->success = 0;
925 /* Retrieve the new process startup info */
926 DECL_HANDLER(get_startup_info)
928 struct startup_info *info;
930 if ((info = current->process->startup_info))
932 size_t size = info->data_size;
933 if (size > get_reply_max_size()) size = get_reply_max_size();
935 /* we return the data directly without making a copy so this can only be called once */
936 set_reply_data_ptr( info->data, size );
937 info->data = NULL;
938 info->data_size = 0;
943 /* initialize a new process */
944 DECL_HANDLER(init_process)
946 if (current->unix_pid == -1)
948 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
949 return;
951 if (current->process->startup_info)
953 fatal_protocol_error( current, "init_process: called twice\n" );
954 return;
956 reply->info_size = 0;
957 current->process->ldt_copy = req->ldt_copy;
958 current->process->startup_info = init_process( reply );
961 /* signal the end of the process initialization */
962 DECL_HANDLER(init_process_done)
964 struct file *file = NULL;
965 struct process *process = current->process;
967 if (is_process_init_done(process))
969 fatal_protocol_error( current, "init_process_done: called twice\n" );
970 return;
972 if (!req->module)
974 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
975 return;
977 process->exe.base = req->module;
978 process->exe.size = req->module_size;
979 process->exe.name = req->name;
981 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
982 if (process->exe.file) release_object( process->exe.file );
983 process->exe.file = file;
985 if ((process->exe.namelen = get_req_data_size()))
986 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
988 generate_startup_debug_events( process, req->entry );
989 set_process_startup_state( process, STARTUP_DONE );
991 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
992 if (current->suspend + process->suspend > 0) stop_thread( current );
993 reply->debugged = (process->debugger != 0);
996 /* open a handle to a process */
997 DECL_HANDLER(open_process)
999 struct process *process = get_process_from_id( req->pid );
1000 reply->handle = 0;
1001 if (process)
1003 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1004 release_object( process );
1008 /* terminate a process */
1009 DECL_HANDLER(terminate_process)
1011 struct process *process;
1013 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1015 reply->self = (current->process == process);
1016 kill_process( process, current, req->exit_code );
1017 release_object( process );
1021 /* fetch information about a process */
1022 DECL_HANDLER(get_process_info)
1024 struct process *process;
1026 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1028 get_process_info( process, reply );
1029 release_object( process );
1033 /* set information about a process */
1034 DECL_HANDLER(set_process_info)
1036 struct process *process;
1038 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1040 set_process_info( process, req );
1041 release_object( process );
1045 /* read data from a process address space */
1046 DECL_HANDLER(read_process_memory)
1048 struct process *process;
1049 size_t len = get_reply_max_size();
1051 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1053 if (len)
1055 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1056 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1057 const int *start = (int *)((char *)req->addr - start_offset);
1058 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1059 if (buffer)
1061 if (read_process_memory( process, start, nb_ints, buffer ))
1063 /* move start of requested data to start of buffer */
1064 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1065 set_reply_data_ptr( buffer, len );
1067 else len = 0;
1070 release_object( process );
1073 /* write data to a process address space */
1074 DECL_HANDLER(write_process_memory)
1076 struct process *process;
1078 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1080 size_t len = get_req_data_size();
1081 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1082 set_error( STATUS_INVALID_PARAMETER );
1083 else
1085 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1086 req->first_mask, req->last_mask, get_req_data() );
1088 release_object( process );
1092 /* notify the server that a dll has been loaded */
1093 DECL_HANDLER(load_dll)
1095 struct process_dll *dll;
1096 struct file *file = NULL;
1098 if (req->handle &&
1099 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1101 if ((dll = process_load_dll( current->process, file, req->base,
1102 get_req_data(), get_req_data_size() )))
1104 dll->size = req->size;
1105 dll->dbg_offset = req->dbg_offset;
1106 dll->dbg_size = req->dbg_size;
1107 dll->name = req->name;
1108 /* only generate event if initialization is done */
1109 if (is_process_init_done( current->process ))
1110 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1112 if (file) release_object( file );
1115 /* notify the server that a dll is being unloaded */
1116 DECL_HANDLER(unload_dll)
1118 process_unload_dll( current->process, req->base );
1121 /* retrieve information about a module in a process */
1122 DECL_HANDLER(get_dll_info)
1124 struct process *process;
1126 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1128 struct process_dll *dll;
1130 for (dll = &process->exe; dll; dll = dll->next)
1132 if (dll->base == req->base_address)
1134 reply->size = dll->size;
1135 reply->entry_point = 0; /* FIXME */
1136 if (dll->filename)
1138 size_t len = min( dll->namelen, get_reply_max_size() );
1139 set_reply_data( dll->filename, len );
1141 break;
1144 if (!dll)
1145 set_error(STATUS_DLL_NOT_FOUND);
1146 release_object( process );
1150 /* wait for a process to start waiting on input */
1151 /* FIXME: only returns event for now, wait is done in the client */
1152 DECL_HANDLER(wait_input_idle)
1154 struct process *process;
1156 reply->event = 0;
1157 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1159 if (process->idle_event && process != current->process && process->queue != current->queue)
1160 reply->event = alloc_handle( current->process, process->idle_event,
1161 EVENT_ALL_ACCESS, 0 );
1162 release_object( process );