Added handling of SBM_GETSCROLLBARINFO message.
[wine/wine-gecko.git] / server / process.c
blob1c04ba712bcf1dd1fdf81720e3e9c06adae993e1
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"
47 #include "user.h"
49 /* process structure */
51 static struct process *first_process;
52 static int running_processes;
54 /* process operations */
56 static void process_dump( struct object *obj, int verbose );
57 static int process_signaled( struct object *obj, struct thread *thread );
58 static void process_poll_event( struct fd *fd, int event );
59 static void process_destroy( struct object *obj );
61 static const struct object_ops process_ops =
63 sizeof(struct process), /* size */
64 process_dump, /* dump */
65 add_queue, /* add_queue */
66 remove_queue, /* remove_queue */
67 process_signaled, /* signaled */
68 no_satisfied, /* satisfied */
69 no_get_fd, /* get_fd */
70 process_destroy /* destroy */
73 static const struct fd_ops process_fd_ops =
75 NULL, /* get_poll_events */
76 process_poll_event, /* poll_event */
77 no_flush, /* flush */
78 no_get_file_info, /* get_file_info */
79 no_queue_async /* queue_async */
82 /* process startup info */
84 struct startup_info
86 struct object obj; /* object header */
87 struct list entry; /* entry in list of startup infos */
88 int inherit_all; /* inherit all handles from parent */
89 int create_flags; /* creation flags */
90 int unix_pid; /* Unix pid of new process */
91 obj_handle_t hstdin; /* handle for stdin */
92 obj_handle_t hstdout; /* handle for stdout */
93 obj_handle_t hstderr; /* handle for stderr */
94 struct file *exe_file; /* file handle for main exe */
95 struct thread *owner; /* owner thread (the one that created the new process) */
96 struct process *process; /* created process */
97 struct thread *thread; /* created thread */
98 size_t data_size; /* size of startup data */
99 void *data; /* data for startup info */
102 static void startup_info_dump( struct object *obj, int verbose );
103 static int startup_info_signaled( struct object *obj, struct thread *thread );
104 static void startup_info_destroy( struct object *obj );
106 static const struct object_ops startup_info_ops =
108 sizeof(struct startup_info), /* size */
109 startup_info_dump, /* dump */
110 add_queue, /* add_queue */
111 remove_queue, /* remove_queue */
112 startup_info_signaled, /* signaled */
113 no_satisfied, /* satisfied */
114 no_get_fd, /* get_fd */
115 startup_info_destroy /* destroy */
119 static struct list startup_info_list = LIST_INIT(startup_info_list);
121 struct ptid_entry
123 void *ptr; /* entry ptr */
124 unsigned int next; /* next free entry */
127 static struct ptid_entry *ptid_entries; /* array of ptid entries */
128 static unsigned int used_ptid_entries; /* number of entries in use */
129 static unsigned int alloc_ptid_entries; /* number of allocated entries */
130 static unsigned int next_free_ptid; /* next free entry */
131 static unsigned int last_free_ptid; /* last free entry */
133 #define PTID_OFFSET 8 /* offset for first ptid value */
135 /* allocate a new process or thread id */
136 unsigned int alloc_ptid( void *ptr )
138 struct ptid_entry *entry;
139 unsigned int id;
141 if (used_ptid_entries < alloc_ptid_entries)
143 id = used_ptid_entries + PTID_OFFSET;
144 entry = &ptid_entries[used_ptid_entries++];
146 else if (next_free_ptid)
148 id = next_free_ptid;
149 entry = &ptid_entries[id - PTID_OFFSET];
150 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
152 else /* need to grow the array */
154 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
155 if (!count) count = 64;
156 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
158 set_error( STATUS_NO_MEMORY );
159 return 0;
161 ptid_entries = entry;
162 alloc_ptid_entries = count;
163 id = used_ptid_entries + PTID_OFFSET;
164 entry = &ptid_entries[used_ptid_entries++];
167 entry->ptr = ptr;
168 return id;
171 /* free a process or thread id */
172 void free_ptid( unsigned int id )
174 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
176 entry->ptr = NULL;
177 entry->next = 0;
179 /* append to end of free list so that we don't reuse it too early */
180 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
181 else next_free_ptid = id;
183 last_free_ptid = id;
186 /* retrieve the pointer corresponding to a process or thread id */
187 void *get_ptid_entry( unsigned int id )
189 if (id < PTID_OFFSET) return NULL;
190 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
191 return ptid_entries[id - PTID_OFFSET].ptr;
194 /* set the state of the process startup info */
195 static void set_process_startup_state( struct process *process, enum startup_state state )
197 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
198 if (process->startup_info)
200 wake_up( &process->startup_info->obj, 0 );
201 release_object( process->startup_info );
202 process->startup_info = NULL;
206 /* set the console and stdio handles for a newly created process */
207 static int set_process_console( struct process *process, struct thread *parent_thread,
208 struct startup_info *info, struct init_process_reply *reply )
210 if (info)
212 if (!(process->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
214 /* FIXME: some better error checking should be done...
215 * like if hConOut and hConIn are console handles, then they should be on the same
216 * physical console
218 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
220 if (!info->inherit_all && !(process->create_flags & CREATE_NEW_CONSOLE))
222 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
223 0, TRUE, DUPLICATE_SAME_ACCESS );
224 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
225 0, TRUE, DUPLICATE_SAME_ACCESS );
226 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
227 0, TRUE, DUPLICATE_SAME_ACCESS );
229 else
231 reply->hstdin = info->hstdin;
232 reply->hstdout = info->hstdout;
233 reply->hstderr = info->hstderr;
236 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
237 /* some handles above may have been invalid; this is not an error */
238 if (get_error() == STATUS_INVALID_HANDLE ||
239 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
240 return 1;
243 /* create a new process and its main thread */
244 struct thread *create_process( int fd )
246 struct process *process;
247 struct thread *thread = NULL;
248 int request_pipe[2];
250 if (!(process = alloc_object( &process_ops ))) goto error;
251 process->next = NULL;
252 process->prev = NULL;
253 process->parent = NULL;
254 process->thread_list = NULL;
255 process->debugger = NULL;
256 process->handles = NULL;
257 process->msg_fd = NULL;
258 process->exit_code = STILL_ACTIVE;
259 process->running_threads = 0;
260 process->priority = NORMAL_PRIORITY_CLASS;
261 process->affinity = 1;
262 process->suspend = 0;
263 process->create_flags = 0;
264 process->console = NULL;
265 process->startup_state = STARTUP_IN_PROGRESS;
266 process->startup_info = NULL;
267 process->idle_event = NULL;
268 process->queue = NULL;
269 process->atom_table = NULL;
270 process->peb = NULL;
271 process->ldt_copy = NULL;
272 process->exe.next = NULL;
273 process->exe.prev = NULL;
274 process->exe.file = NULL;
275 process->exe.dbg_offset = 0;
276 process->exe.dbg_size = 0;
277 process->exe.namelen = 0;
278 process->exe.filename = NULL;
279 process->group_id = 0;
280 process->token = create_token();
281 list_init( &process->locks );
282 list_init( &process->classes );
284 gettimeofday( &process->start_time, NULL );
285 if ((process->next = first_process) != NULL) process->next->prev = process;
286 first_process = process;
288 if (!(process->id = alloc_ptid( process ))) goto error;
289 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
291 /* create the main thread */
292 if (pipe( request_pipe ) == -1)
294 file_set_error();
295 goto error;
297 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
299 close( request_pipe[0] );
300 close( request_pipe[1] );
301 goto error;
303 close( request_pipe[1] );
304 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
306 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
307 release_object( process );
308 return thread;
310 error:
311 if (process) release_object( process );
312 /* if we failed to start our first process, close everything down */
313 if (!running_processes) close_master_socket();
314 return NULL;
317 /* find the startup info for a given Unix process */
318 inline static struct startup_info *find_startup_info( int unix_pid )
320 struct list *ptr;
322 LIST_FOR_EACH( ptr, &startup_info_list )
324 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
325 if (info->unix_pid == unix_pid) return info;
327 return NULL;
330 /* initialize the current process and fill in the request */
331 static struct startup_info *init_process( struct init_process_reply *reply )
333 struct process *process = current->process;
334 struct thread *parent_thread = NULL;
335 struct process *parent = NULL;
336 struct startup_info *info = find_startup_info( current->unix_pid );
338 if (info)
340 if (info->thread)
342 fatal_protocol_error( current, "init_process: called twice?\n" );
343 return NULL;
345 parent_thread = info->owner;
346 parent = parent_thread->process;
347 process->parent = (struct process *)grab_object( parent );
350 /* set the process flags */
351 process->create_flags = info ? info->create_flags : 0;
353 /* create the handle table */
354 if (info && info->inherit_all)
355 process->handles = copy_handle_table( process, parent );
356 else
357 process->handles = alloc_handle_table( process, 0 );
358 if (!process->handles) return NULL;
360 /* retrieve the main exe file */
361 reply->exe_file = 0;
362 if (info && info->exe_file)
364 process->exe.file = (struct file *)grab_object( info->exe_file );
365 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
366 return NULL;
369 /* set the process console */
370 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
372 process->group_id = get_process_id( process );
373 if (parent)
375 /* attach to the debugger if requested */
376 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
377 set_process_debugger( process, parent_thread );
378 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
379 set_process_debugger( process, parent->debugger );
380 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
381 process->group_id = parent->group_id;
384 /* thread will be actually suspended in init_done */
385 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
387 if (info)
389 reply->info_size = info->data_size;
390 info->process = (struct process *)grab_object( process );
391 info->thread = (struct thread *)grab_object( current );
393 reply->create_flags = process->create_flags;
394 reply->server_start = server_start_ticks;
395 return info ? (struct startup_info *)grab_object( info ) : NULL;
398 /* destroy a process when its refcount is 0 */
399 static void process_destroy( struct object *obj )
401 struct process *process = (struct process *)obj;
402 assert( obj->ops == &process_ops );
404 /* we can't have a thread remaining */
405 assert( !process->thread_list );
407 set_process_startup_state( process, STARTUP_ABORTED );
408 if (process->console) release_object( process->console );
409 if (process->parent) release_object( process->parent );
410 if (process->msg_fd) release_object( process->msg_fd );
411 if (process->next) process->next->prev = process->prev;
412 if (process->prev) process->prev->next = process->next;
413 else first_process = process->next;
414 if (process->idle_event) release_object( process->idle_event );
415 if (process->queue) release_object( process->queue );
416 if (process->atom_table) release_object( process->atom_table );
417 if (process->exe.file) release_object( process->exe.file );
418 if (process->exe.filename) free( process->exe.filename );
419 if (process->id) free_ptid( process->id );
420 if (process->token) release_object( process->token );
423 /* dump a process on stdout for debugging purposes */
424 static void process_dump( struct object *obj, int verbose )
426 struct process *process = (struct process *)obj;
427 assert( obj->ops == &process_ops );
429 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
430 process->id, process->next, process->prev, process->handles );
433 static int process_signaled( struct object *obj, struct thread *thread )
435 struct process *process = (struct process *)obj;
436 return !process->running_threads;
439 static void process_poll_event( struct fd *fd, int event )
441 struct process *process = get_fd_user( fd );
442 assert( process->obj.ops == &process_ops );
444 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
445 else if (event & POLLIN) receive_fd( process );
448 static void startup_info_destroy( struct object *obj )
450 struct startup_info *info = (struct startup_info *)obj;
451 assert( obj->ops == &startup_info_ops );
452 list_remove( &info->entry );
453 if (info->data) free( info->data );
454 if (info->exe_file) release_object( info->exe_file );
455 if (info->process) release_object( info->process );
456 if (info->thread) release_object( info->thread );
457 if (info->owner) release_object( info->owner );
460 static void startup_info_dump( struct object *obj, int verbose )
462 struct startup_info *info = (struct startup_info *)obj;
463 assert( obj->ops == &startup_info_ops );
465 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
466 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
469 static int startup_info_signaled( struct object *obj, struct thread *thread )
471 struct startup_info *info = (struct startup_info *)obj;
472 return info->process && is_process_init_done(info->process);
475 /* get a process from an id (and increment the refcount) */
476 struct process *get_process_from_id( process_id_t id )
478 struct object *obj = get_ptid_entry( id );
480 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
481 set_error( STATUS_INVALID_PARAMETER );
482 return NULL;
485 /* get a process from a handle (and increment the refcount) */
486 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
488 return (struct process *)get_handle_obj( current->process, handle,
489 access, &process_ops );
492 /* add a dll to a process list */
493 static struct process_dll *process_load_dll( struct process *process, struct file *file,
494 void *base, const WCHAR *filename, size_t name_len )
496 struct process_dll *dll;
498 /* make sure we don't already have one with the same base address */
499 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
501 set_error( STATUS_INVALID_PARAMETER );
502 return NULL;
505 if ((dll = mem_alloc( sizeof(*dll) )))
507 dll->prev = &process->exe;
508 dll->file = NULL;
509 dll->base = base;
510 dll->filename = NULL;
511 dll->namelen = name_len;
512 if (name_len && !(dll->filename = memdup( filename, name_len )))
514 free( dll );
515 return NULL;
517 if (file) dll->file = (struct file *)grab_object( file );
518 if ((dll->next = process->exe.next)) dll->next->prev = dll;
519 process->exe.next = dll;
521 return dll;
524 /* remove a dll from a process list */
525 static void process_unload_dll( struct process *process, void *base )
527 struct process_dll *dll;
529 for (dll = process->exe.next; dll; dll = dll->next)
531 if (dll->base == base)
533 if (dll->file) release_object( dll->file );
534 if (dll->next) dll->next->prev = dll->prev;
535 if (dll->prev) dll->prev->next = dll->next;
536 if (dll->filename) free( dll->filename );
537 free( dll );
538 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
539 return;
542 set_error( STATUS_INVALID_PARAMETER );
545 /* kill all processes */
546 void kill_all_processes( struct process *skip, int exit_code )
548 for (;;)
550 struct process *process = first_process;
552 while (process && (!process->running_threads || process == skip))
553 process = process->next;
554 if (!process) break;
555 kill_process( process, NULL, exit_code );
559 /* kill all processes being attached to a console renderer */
560 void kill_console_processes( struct thread *renderer, int exit_code )
562 for (;;) /* restart from the beginning of the list every time */
564 struct process *process = first_process;
566 /* find the first process being attached to 'renderer' and still running */
567 while (process &&
568 (process == renderer->process || !process->console ||
569 process->console->renderer != renderer || !process->running_threads))
571 process = process->next;
573 if (!process) break;
574 kill_process( process, NULL, exit_code );
578 /* a process has been killed (i.e. its last thread died) */
579 static void process_killed( struct process *process )
581 assert( !process->thread_list );
582 gettimeofday( &process->end_time, NULL );
583 if (process->handles) release_object( process->handles );
584 process->handles = NULL;
586 /* close the console attached to this process, if any */
587 free_console( process );
589 while (process->exe.next)
591 struct process_dll *dll = process->exe.next;
592 process->exe.next = dll->next;
593 if (dll->file) release_object( dll->file );
594 if (dll->filename) free( dll->filename );
595 free( dll );
597 destroy_process_classes( process );
598 set_process_startup_state( process, STARTUP_ABORTED );
599 if (process->exe.file) release_object( process->exe.file );
600 process->exe.file = NULL;
601 wake_up( &process->obj, 0 );
602 if (!--running_processes) close_master_socket();
605 /* add a thread to a process running threads list */
606 void add_process_thread( struct process *process, struct thread *thread )
608 thread->proc_next = process->thread_list;
609 thread->proc_prev = NULL;
610 if (thread->proc_next) thread->proc_next->proc_prev = thread;
611 process->thread_list = thread;
612 if (!process->running_threads++) running_processes++;
613 grab_object( thread );
616 /* remove a thread from a process running threads list */
617 void remove_process_thread( struct process *process, struct thread *thread )
619 assert( process->running_threads > 0 );
620 assert( process->thread_list );
622 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
623 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
624 else process->thread_list = thread->proc_next;
626 if (!--process->running_threads)
628 /* we have removed the last running thread, exit the process */
629 process->exit_code = thread->exit_code;
630 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
631 process_killed( process );
633 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
634 release_object( thread );
637 /* suspend all the threads of a process */
638 void suspend_process( struct process *process )
640 if (!process->suspend++)
642 struct thread *thread = process->thread_list;
643 while (thread)
645 struct thread *next = thread->proc_next;
646 if (!thread->suspend) stop_thread( thread );
647 thread = next;
652 /* resume all the threads of a process */
653 void resume_process( struct process *process )
655 assert (process->suspend > 0);
656 if (!--process->suspend)
658 struct thread *thread = process->thread_list;
659 while (thread)
661 struct thread *next = thread->proc_next;
662 if (!thread->suspend) wake_thread( thread );
663 thread = next;
668 /* kill a process on the spot */
669 void kill_process( struct process *process, struct thread *skip, int exit_code )
671 struct thread *thread = process->thread_list;
673 while (thread)
675 struct thread *next = thread->proc_next;
676 thread->exit_code = exit_code;
677 if (thread != skip) kill_thread( thread, 1 );
678 thread = next;
682 /* kill all processes being debugged by a given thread */
683 void kill_debugged_processes( struct thread *debugger, int exit_code )
685 for (;;) /* restart from the beginning of the list every time */
687 struct process *process = first_process;
688 /* find the first process being debugged by 'debugger' and still running */
689 while (process && (process->debugger != debugger || !process->running_threads))
690 process = process->next;
691 if (!process) return;
692 process->debugger = NULL;
693 kill_process( process, NULL, exit_code );
698 /* detach a debugger from all its debuggees */
699 void detach_debugged_processes( struct thread *debugger )
701 struct process *process;
702 for (process = first_process; process; process = process->next)
704 if (process->debugger == debugger && process->running_threads)
706 debugger_detach( process, debugger );
712 void enum_processes( int (*cb)(struct process*, void*), void *user )
714 struct process *process;
715 for (process = first_process; process; process = process->next)
717 if ((cb)(process, user)) break;
722 /* read data from a process memory space */
723 /* len is the total size (in ints) */
724 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
726 struct thread *thread = process->thread_list;
728 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
730 if (!thread) /* process is dead */
732 set_error( STATUS_ACCESS_DENIED );
733 return 0;
735 if (suspend_for_ptrace( thread ))
737 while (len > 0)
739 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
740 len--;
742 resume_after_ptrace( thread );
744 return !len;
747 /* make sure we can write to the whole address range */
748 /* len is the total size (in ints) */
749 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
751 int page = get_page_size() / sizeof(int);
753 for (;;)
755 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
756 if (len <= page) break;
757 addr += page;
758 len -= page;
760 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
763 /* write data to a process memory space */
764 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
765 /* we check the total size for write permissions */
766 static int write_process_memory( struct process *process, int *addr, size_t len,
767 unsigned int first_mask, unsigned int last_mask, const int *src )
769 struct thread *thread = process->thread_list;
770 int ret = 0;
772 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
774 if (!thread) /* process is dead */
776 set_error( STATUS_ACCESS_DENIED );
777 return 0;
779 if (suspend_for_ptrace( thread ))
781 if (!check_process_write_access( thread, addr, len ))
783 set_error( STATUS_ACCESS_DENIED );
784 goto done;
786 /* first word is special */
787 if (len > 1)
789 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
790 len--;
792 else last_mask &= first_mask;
794 while (len > 1)
796 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
797 len--;
800 /* last word is special too */
801 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
802 ret = 1;
804 done:
805 resume_after_ptrace( thread );
807 return ret;
810 /* set the debugged flag in the process PEB */
811 int set_process_debug_flag( struct process *process, int flag )
813 int mask = 0, data = 0;
815 /* BeingDebugged flag is the byte at offset 2 in the PEB */
816 memset( (char *)&mask + 2, 0xff, 1 );
817 memset( (char *)&data + 2, flag, 1 );
818 return write_process_memory( process, process->peb, 1, mask, mask, &data );
821 /* take a snapshot of currently running processes */
822 struct process_snapshot *process_snap( int *count )
824 struct process_snapshot *snapshot, *ptr;
825 struct process *process;
826 if (!running_processes) return NULL;
827 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
828 return NULL;
829 ptr = snapshot;
830 for (process = first_process; process; process = process->next)
832 if (!process->running_threads) continue;
833 ptr->process = process;
834 ptr->threads = process->running_threads;
835 ptr->count = process->obj.refcount;
836 ptr->priority = process->priority;
837 ptr->handles = get_handle_table_count(process);
838 grab_object( process );
839 ptr++;
841 *count = running_processes;
842 return snapshot;
845 /* take a snapshot of the modules of a process */
846 struct module_snapshot *module_snap( struct process *process, int *count )
848 struct module_snapshot *snapshot, *ptr;
849 struct process_dll *dll;
850 int total = 0;
852 for (dll = &process->exe; dll; dll = dll->next) total++;
853 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
855 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
857 ptr->base = dll->base;
858 ptr->size = dll->size;
859 ptr->namelen = dll->namelen;
860 ptr->filename = memdup( dll->filename, dll->namelen );
862 *count = total;
863 return snapshot;
867 /* create a new process */
868 DECL_HANDLER(new_process)
870 struct startup_info *info;
872 /* build the startup info for a new process */
873 if (!(info = alloc_object( &startup_info_ops ))) return;
874 list_add_head( &startup_info_list, &info->entry );
875 info->inherit_all = req->inherit_all;
876 info->create_flags = req->create_flags;
877 info->unix_pid = req->unix_pid;
878 info->hstdin = req->hstdin;
879 info->hstdout = req->hstdout;
880 info->hstderr = req->hstderr;
881 info->exe_file = NULL;
882 info->owner = (struct thread *)grab_object( current );
883 info->process = NULL;
884 info->thread = NULL;
885 info->data_size = get_req_data_size();
886 info->data = NULL;
888 if (req->exe_file &&
889 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
890 goto done;
892 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
893 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
895 done:
896 release_object( info );
899 /* Retrieve information about a newly started process */
900 DECL_HANDLER(get_new_process_info)
902 struct startup_info *info;
904 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
905 0, &startup_info_ops )))
907 reply->pid = get_process_id( info->process );
908 reply->tid = get_thread_id( info->thread );
909 reply->phandle = alloc_handle( current->process, info->process,
910 PROCESS_ALL_ACCESS, req->pinherit );
911 reply->thandle = alloc_handle( current->process, info->thread,
912 THREAD_ALL_ACCESS, req->tinherit );
913 reply->success = is_process_init_done( info->process );
914 release_object( info );
916 else
918 reply->pid = 0;
919 reply->tid = 0;
920 reply->phandle = 0;
921 reply->thandle = 0;
922 reply->success = 0;
926 /* Retrieve the new process startup info */
927 DECL_HANDLER(get_startup_info)
929 struct startup_info *info;
931 if ((info = current->process->startup_info))
933 size_t size = info->data_size;
934 if (size > get_reply_max_size()) size = get_reply_max_size();
936 /* we return the data directly without making a copy so this can only be called once */
937 set_reply_data_ptr( info->data, size );
938 info->data = NULL;
939 info->data_size = 0;
944 /* initialize a new process */
945 DECL_HANDLER(init_process)
947 if (current->unix_pid == -1)
949 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
950 return;
952 if (current->process->startup_info)
954 fatal_protocol_error( current, "init_process: called twice\n" );
955 return;
957 if (!req->peb || (unsigned int)req->peb % sizeof(int))
959 fatal_protocol_error( current, "init_process: bad peb address\n" );
960 return;
962 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
964 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
965 return;
967 reply->info_size = 0;
968 current->process->peb = req->peb;
969 current->process->ldt_copy = req->ldt_copy;
970 current->process->startup_info = init_process( reply );
973 /* signal the end of the process initialization */
974 DECL_HANDLER(init_process_done)
976 struct file *file = NULL;
977 struct process *process = current->process;
979 if (is_process_init_done(process))
981 fatal_protocol_error( current, "init_process_done: called twice\n" );
982 return;
984 if (!req->module)
986 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
987 return;
989 process->exe.base = req->module;
990 process->exe.size = req->module_size;
991 process->exe.name = req->name;
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;
997 if ((process->exe.namelen = get_req_data_size()))
998 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1000 generate_startup_debug_events( process, req->entry );
1001 set_process_startup_state( process, STARTUP_DONE );
1003 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1004 if (current->suspend + process->suspend > 0) stop_thread( current );
1005 if (process->debugger) set_process_debug_flag( process, 1 );
1008 /* open a handle to a process */
1009 DECL_HANDLER(open_process)
1011 struct process *process = get_process_from_id( req->pid );
1012 reply->handle = 0;
1013 if (process)
1015 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1016 release_object( process );
1020 /* terminate a process */
1021 DECL_HANDLER(terminate_process)
1023 struct process *process;
1025 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1027 reply->self = (current->process == process);
1028 kill_process( process, current, req->exit_code );
1029 release_object( process );
1033 /* fetch information about a process */
1034 DECL_HANDLER(get_process_info)
1036 struct process *process;
1038 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1040 reply->pid = get_process_id( process );
1041 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1042 reply->exit_code = process->exit_code;
1043 reply->priority = process->priority;
1044 reply->process_affinity = process->affinity;
1045 reply->system_affinity = 1;
1046 reply->peb = process->peb;
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 && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1122 return;
1124 if ((dll = process_load_dll( current->process, file, req->base,
1125 get_req_data(), get_req_data_size() )))
1127 dll->size = req->size;
1128 dll->dbg_offset = req->dbg_offset;
1129 dll->dbg_size = req->dbg_size;
1130 dll->name = req->name;
1131 /* only generate event if initialization is done */
1132 if (is_process_init_done( current->process ))
1133 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1135 if (file) release_object( file );
1138 /* notify the server that a dll is being unloaded */
1139 DECL_HANDLER(unload_dll)
1141 process_unload_dll( current->process, req->base );
1144 /* retrieve information about a module in a process */
1145 DECL_HANDLER(get_dll_info)
1147 struct process *process;
1149 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1151 struct process_dll *dll;
1153 for (dll = &process->exe; dll; dll = dll->next)
1155 if (dll->base == req->base_address)
1157 reply->size = dll->size;
1158 reply->entry_point = 0; /* FIXME */
1159 if (dll->filename)
1161 size_t len = min( dll->namelen, get_reply_max_size() );
1162 set_reply_data( dll->filename, len );
1164 break;
1167 if (!dll)
1168 set_error(STATUS_DLL_NOT_FOUND);
1169 release_object( process );
1173 /* wait for a process to start waiting on input */
1174 /* FIXME: only returns event for now, wait is done in the client */
1175 DECL_HANDLER(wait_input_idle)
1177 struct process *process;
1179 reply->event = 0;
1180 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1182 if (process->idle_event && process != current->process && process->queue != current->queue)
1183 reply->event = alloc_handle( current->process, process->idle_event,
1184 EVENT_ALL_ACCESS, 0 );
1185 release_object( process );