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
22 #include "wine/port.h"
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.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 object *obj, 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 NULL, /* get_poll_events */
67 process_poll_event, /* poll_event */
68 no_get_fd, /* get_fd */
70 no_get_file_info, /* get_file_info */
71 NULL, /* queue_async */
72 process_destroy /* destroy */
75 /* process startup info */
77 enum startup_state { STARTUP_IN_PROGRESS, STARTUP_DONE, STARTUP_ABORTED };
81 struct object obj; /* object header */
82 enum startup_state state; /* child process startup state */
83 int inherit_all; /* inherit all handles from parent */
84 int use_handles; /* use stdio handles */
85 int create_flags; /* creation flags */
86 handle_t hstdin; /* handle for stdin */
87 handle_t hstdout; /* handle for stdout */
88 handle_t hstderr; /* handle for stderr */
89 struct file *exe_file; /* file handle for main exe */
90 struct thread *owner; /* owner thread (the one that created the new process) */
91 struct process *process; /* created process */
92 struct thread *thread; /* created thread */
93 size_t data_size; /* size of startup data */
94 startup_info_t *data; /* data for startup info */
97 static void startup_info_dump( struct object *obj, int verbose );
98 static int startup_info_signaled( struct object *obj, struct thread *thread );
99 static void startup_info_destroy( struct object *obj );
101 static const struct object_ops startup_info_ops =
103 sizeof(struct startup_info), /* size */
104 startup_info_dump, /* dump */
105 add_queue, /* add_queue */
106 remove_queue, /* remove_queue */
107 startup_info_signaled, /* signaled */
108 no_satisfied, /* satisfied */
109 NULL, /* get_poll_events */
110 NULL, /* poll_event */
111 no_get_fd, /* get_fd */
112 no_flush, /* flush */
113 no_get_file_info, /* get_file_info */
114 NULL, /* queue_async */
115 startup_info_destroy /* destroy */
119 /* set the state of the process startup info */
120 static void set_process_startup_state( struct process *process, enum startup_state state )
122 if (!process->startup_info) return;
123 process->startup_info->state = state;
124 wake_up( &process->startup_info->obj, 0 );
125 release_object( process->startup_info );
126 process->startup_info = NULL;
129 /* set the console and stdio handles for a newly created process */
130 static int set_process_console( struct process *process, struct thread *parent_thread,
131 struct startup_info *info, struct init_process_reply *reply )
133 if (process->create_flags & CREATE_NEW_CONSOLE)
135 /* let the process init do the allocation */
138 else if (info && !(process->create_flags & DETACHED_PROCESS))
140 /* FIXME: some better error checking should be done...
141 * like if hConOut and hConIn are console handles, then they should be on the same
144 inherit_console( parent_thread, process,
145 (info->inherit_all || info->use_handles) ? info->hstdin : 0 );
149 if (!info->inherit_all && !info->use_handles)
151 /* duplicate the handle from the parent into this process */
152 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
153 0, TRUE, DUPLICATE_SAME_ACCESS );
154 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
155 0, TRUE, DUPLICATE_SAME_ACCESS );
156 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
157 0, TRUE, DUPLICATE_SAME_ACCESS );
161 reply->hstdin = info->hstdin;
162 reply->hstdout = info->hstdout;
163 reply->hstderr = info->hstderr;
168 if (process->console)
170 reply->hstdin = alloc_handle( process, process->console,
171 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
172 reply->hstdout = alloc_handle( process, process->console->active,
173 GENERIC_READ | GENERIC_WRITE, 1 );
174 reply->hstderr = alloc_handle( process, process->console->active,
175 GENERIC_READ | GENERIC_WRITE, 1 );
179 /* no parent, let the caller decide what to do */
180 reply->hstdin = reply->hstdout = reply->hstderr = 0;
183 /* some handles above may have been invalid; this is not an error */
184 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
188 /* create a new process and its main thread */
189 struct thread *create_process( int fd )
191 struct process *process;
192 struct thread *thread = NULL;
195 if (!(process = alloc_object( &process_ops, fd ))) return NULL;
196 process->next = NULL;
197 process->prev = NULL;
198 process->parent = NULL;
199 process->thread_list = NULL;
200 process->debugger = NULL;
201 process->handles = NULL;
202 process->exit_code = STILL_ACTIVE;
203 process->running_threads = 0;
204 process->priority = NORMAL_PRIORITY_CLASS;
205 process->affinity = 1;
206 process->suspend = 0;
207 process->create_flags = 0;
208 process->console = NULL;
209 process->startup_info = NULL;
210 process->idle_event = NULL;
211 process->queue = NULL;
212 process->atom_table = NULL;
213 process->ldt_copy = NULL;
214 process->exe.next = NULL;
215 process->exe.prev = NULL;
216 process->exe.file = NULL;
217 process->exe.base = NULL;
218 process->exe.dbg_offset = 0;
219 process->exe.dbg_size = 0;
220 process->exe.namelen = 0;
221 process->exe.filename = NULL;
223 gettimeofday( &process->start_time, NULL );
224 if ((process->next = first_process) != NULL) process->next->prev = process;
225 first_process = process;
227 /* create the main thread */
228 if (pipe( request_pipe ) == -1)
233 send_client_fd( process, request_pipe[1], 0 );
234 close( request_pipe[1] );
235 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
237 set_select_events( &process->obj, POLLIN ); /* start listening to events */
238 release_object( process );
242 if (thread) release_object( thread );
243 release_object( process );
247 /* initialize the current process and fill in the request */
248 static struct startup_info *init_process( int ppid, struct init_process_reply *reply )
250 struct process *process = current->process;
251 struct thread *parent_thread = get_thread_from_pid( ppid );
252 struct process *parent = NULL;
253 struct startup_info *info = NULL;
257 parent = parent_thread->process;
258 info = parent_thread->info;
259 if (info && info->thread)
261 fatal_protocol_error( current, "init_process: called twice?\n" );
264 process->parent = (struct process *)grab_object( parent );
267 /* set the process flags */
268 process->create_flags = info ? info->create_flags : 0;
270 /* create the handle table */
271 if (info && info->inherit_all)
272 process->handles = copy_handle_table( process, parent );
274 process->handles = alloc_handle_table( process, 0 );
275 if (!process->handles) return NULL;
277 /* retrieve the main exe file */
279 if (info && info->exe_file)
281 process->exe.file = (struct file *)grab_object( info->exe_file );
282 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
286 /* set the process console */
287 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
291 /* attach to the debugger if requested */
292 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
293 set_process_debugger( process, parent_thread );
294 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
295 set_process_debugger( process, parent->debugger );
298 /* thread will be actually suspended in init_done */
299 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
303 reply->info_size = info->data_size;
304 info->process = (struct process *)grab_object( process );
305 info->thread = (struct thread *)grab_object( current );
307 reply->create_flags = process->create_flags;
308 reply->server_start = server_start_ticks;
309 return info ? (struct startup_info *)grab_object( info ) : NULL;
312 /* destroy a process when its refcount is 0 */
313 static void process_destroy( struct object *obj )
315 struct process *process = (struct process *)obj;
316 assert( obj->ops == &process_ops );
318 /* we can't have a thread remaining */
319 assert( !process->thread_list );
321 set_process_startup_state( process, STARTUP_ABORTED );
322 if (process->console) release_object( process->console );
323 if (process->parent) release_object( process->parent );
324 if (process->next) process->next->prev = process->prev;
325 if (process->prev) process->prev->next = process->next;
326 else first_process = process->next;
327 if (process->idle_event) release_object( process->idle_event );
328 if (process->queue) release_object( process->queue );
329 if (process->atom_table) release_object( process->atom_table );
330 if (process->exe.file) release_object( process->exe.file );
331 if (process->exe.filename) free( process->exe.filename );
334 /* dump a process on stdout for debugging purposes */
335 static void process_dump( struct object *obj, int verbose )
337 struct process *process = (struct process *)obj;
338 assert( obj->ops == &process_ops );
340 fprintf( stderr, "Process next=%p prev=%p handles=%p\n",
341 process->next, process->prev, process->handles );
344 static int process_signaled( struct object *obj, struct thread *thread )
346 struct process *process = (struct process *)obj;
347 return !process->running_threads;
351 static void process_poll_event( struct object *obj, int event )
353 struct process *process = (struct process *)obj;
354 assert( obj->ops == &process_ops );
356 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
357 else if (event & POLLIN) receive_fd( process );
360 static void startup_info_destroy( struct object *obj )
362 struct startup_info *info = (struct startup_info *)obj;
363 assert( obj->ops == &startup_info_ops );
364 if (info->data) free( info->data );
365 if (info->exe_file) release_object( info->exe_file );
366 if (info->process) release_object( info->process );
367 if (info->thread) release_object( info->thread );
370 info->owner->info = NULL;
371 release_object( info->owner );
375 static void startup_info_dump( struct object *obj, int verbose )
377 struct startup_info *info = (struct startup_info *)obj;
378 assert( obj->ops == &startup_info_ops );
380 fprintf( stderr, "Startup info flags=%x in=%d out=%d err=%d state=%d\n",
381 info->create_flags, info->hstdin, info->hstdout, info->hstderr, info->state );
384 static int startup_info_signaled( struct object *obj, struct thread *thread )
386 struct startup_info *info = (struct startup_info *)obj;
387 return info->state != STARTUP_IN_PROGRESS;
390 /* get a process from an id (and increment the refcount) */
391 struct process *get_process_from_id( void *id )
393 struct process *p = first_process;
394 while (p && (p != id)) p = p->next;
395 if (p) grab_object( p );
396 else set_error( STATUS_INVALID_PARAMETER );
400 /* get a process from a handle (and increment the refcount) */
401 struct process *get_process_from_handle( handle_t handle, unsigned int access )
403 return (struct process *)get_handle_obj( current->process, handle,
404 access, &process_ops );
407 /* add a dll to a process list */
408 static struct process_dll *process_load_dll( struct process *process, struct file *file,
409 void *base, const char *filename, size_t name_len )
411 struct process_dll *dll;
413 /* make sure we don't already have one with the same base address */
414 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
416 set_error( STATUS_INVALID_PARAMETER );
420 if ((dll = mem_alloc( sizeof(*dll) )))
422 dll->prev = &process->exe;
425 dll->filename = NULL;
426 dll->namelen = name_len;
427 if (name_len && !(dll->filename = memdup( filename, name_len )))
432 if (file) dll->file = (struct file *)grab_object( file );
433 if ((dll->next = process->exe.next)) dll->next->prev = dll;
434 process->exe.next = dll;
439 /* remove a dll from a process list */
440 static void process_unload_dll( struct process *process, void *base )
442 struct process_dll *dll;
444 for (dll = process->exe.next; dll; dll = dll->next)
446 if (dll->base == base)
448 if (dll->file) release_object( dll->file );
449 if (dll->next) dll->next->prev = dll->prev;
450 if (dll->prev) dll->prev->next = dll->next;
451 if (dll->filename) free( dll->filename );
453 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
457 set_error( STATUS_INVALID_PARAMETER );
460 /* kill all processes being attached to a console renderer */
461 void kill_console_processes( struct thread *renderer, int exit_code )
463 for (;;) /* restart from the beginning of the list every time */
465 struct process *process = first_process;
467 /* find the first process being attached to 'renderer' and still running */
469 (process == renderer->process || !process->console ||
470 process->console->renderer != renderer || !process->running_threads))
472 process = process->next;
475 kill_process( process, NULL, exit_code );
479 /* a process has been killed (i.e. its last thread died) */
480 static void process_killed( struct process *process )
482 assert( !process->thread_list );
483 gettimeofday( &process->end_time, NULL );
484 if (process->handles) release_object( process->handles );
485 process->handles = NULL;
487 /* close the console attached to this process, if any */
488 free_console( process );
490 while (process->exe.next)
492 struct process_dll *dll = process->exe.next;
493 process->exe.next = dll->next;
494 if (dll->file) release_object( dll->file );
495 if (dll->filename) free( dll->filename );
498 set_process_startup_state( process, STARTUP_ABORTED );
499 if (process->exe.file) release_object( process->exe.file );
500 process->exe.file = NULL;
501 wake_up( &process->obj, 0 );
502 if (!--running_processes)
504 /* last process died, close global handles */
505 close_global_handles();
506 /* this will cause the select loop to terminate */
507 close_master_socket();
511 /* add a thread to a process running threads list */
512 void add_process_thread( struct process *process, struct thread *thread )
514 thread->proc_next = process->thread_list;
515 thread->proc_prev = NULL;
516 if (thread->proc_next) thread->proc_next->proc_prev = thread;
517 process->thread_list = thread;
518 if (!process->running_threads++) running_processes++;
519 grab_object( thread );
522 /* remove a thread from a process running threads list */
523 void remove_process_thread( struct process *process, struct thread *thread )
525 assert( process->running_threads > 0 );
526 assert( process->thread_list );
528 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
529 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
530 else process->thread_list = thread->proc_next;
532 if (!--process->running_threads)
534 /* we have removed the last running thread, exit the process */
535 process->exit_code = thread->exit_code;
536 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
537 process_killed( process );
539 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
540 release_object( thread );
543 /* suspend all the threads of a process */
544 void suspend_process( struct process *process )
546 if (!process->suspend++)
548 struct thread *thread = process->thread_list;
551 struct thread *next = thread->proc_next;
552 if (!thread->suspend) stop_thread( thread );
558 /* resume all the threads of a process */
559 void resume_process( struct process *process )
561 assert (process->suspend > 0);
562 if (!--process->suspend)
564 struct thread *thread = process->thread_list;
567 struct thread *next = thread->proc_next;
568 if (!thread->suspend) continue_thread( thread );
574 /* kill a process on the spot */
575 void kill_process( struct process *process, struct thread *skip, int exit_code )
577 struct thread *thread = process->thread_list;
581 struct thread *next = thread->proc_next;
582 thread->exit_code = exit_code;
583 if (thread != skip) kill_thread( thread, 1 );
588 /* kill all processes being debugged by a given thread */
589 void kill_debugged_processes( struct thread *debugger, int exit_code )
591 for (;;) /* restart from the beginning of the list every time */
593 struct process *process = first_process;
594 /* find the first process being debugged by 'debugger' and still running */
595 while (process && (process->debugger != debugger || !process->running_threads))
596 process = process->next;
597 if (!process) return;
598 process->debugger = NULL;
599 kill_process( process, NULL, exit_code );
604 /* detach a debugger from all its debuggees */
605 void detach_debugged_processes( struct thread *debugger )
607 struct process *process;
608 for (process = first_process; process; process = process->next)
610 if (process->debugger == debugger && process->running_threads)
612 debugger_detach( process, debugger );
618 /* get all information about a process */
619 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
621 reply->pid = get_process_id( process );
622 reply->debugged = (process->debugger != 0);
623 reply->exit_code = process->exit_code;
624 reply->priority = process->priority;
625 reply->process_affinity = process->affinity;
626 reply->system_affinity = 1;
629 /* set all information about a process */
630 static void set_process_info( struct process *process,
631 const struct set_process_info_request *req )
633 if (req->mask & SET_PROCESS_INFO_PRIORITY)
634 process->priority = req->priority;
635 if (req->mask & SET_PROCESS_INFO_AFFINITY)
637 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
638 else process->affinity = req->affinity;
642 /* read data from a process memory space */
643 /* len is the total size (in ints) */
644 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
646 struct thread *thread = process->thread_list;
648 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
650 if (!thread) /* process is dead */
652 set_error( STATUS_ACCESS_DENIED );
655 if (suspend_for_ptrace( thread ))
659 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
662 resume_thread( thread );
667 /* make sure we can write to the whole address range */
668 /* len is the total size (in ints) */
669 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
671 int page = get_page_size() / sizeof(int);
675 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
676 if (len <= page) break;
680 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
683 /* write data to a process memory space */
684 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
685 /* we check the total size for write permissions */
686 static void write_process_memory( struct process *process, int *addr, size_t len,
687 unsigned int first_mask, unsigned int last_mask, const int *src )
689 struct thread *thread = process->thread_list;
691 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
693 if (!thread) /* process is dead */
695 set_error( STATUS_ACCESS_DENIED );
698 if (suspend_for_ptrace( thread ))
700 if (!check_process_write_access( thread, addr, len ))
702 set_error( STATUS_ACCESS_DENIED );
705 /* first word is special */
708 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
711 else last_mask &= first_mask;
715 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
719 /* last word is special too */
720 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
723 resume_thread( thread );
727 /* take a snapshot of currently running processes */
728 struct process_snapshot *process_snap( int *count )
730 struct process_snapshot *snapshot, *ptr;
731 struct process *process;
732 if (!running_processes) return NULL;
733 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
736 for (process = first_process; process; process = process->next)
738 if (!process->running_threads) continue;
739 ptr->process = process;
740 ptr->threads = process->running_threads;
741 ptr->count = process->obj.refcount;
742 ptr->priority = process->priority;
743 grab_object( process );
746 *count = running_processes;
750 /* take a snapshot of the modules of a process */
751 struct module_snapshot *module_snap( struct process *process, int *count )
753 struct module_snapshot *snapshot, *ptr;
754 struct process_dll *dll;
757 for (dll = &process->exe; dll; dll = dll->next) total++;
758 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
760 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
762 ptr->base = dll->base;
763 ptr->size = dll->size;
764 ptr->namelen = dll->namelen;
765 ptr->filename = memdup( dll->filename, dll->namelen );
772 /* create a new process */
773 DECL_HANDLER(new_process)
775 struct startup_info *info;
779 fatal_protocol_error( current, "new_process: another process is being created\n" );
783 /* build the startup info for a new process */
784 if (!(info = alloc_object( &startup_info_ops, -1 ))) return;
785 info->state = STARTUP_IN_PROGRESS;
786 info->inherit_all = req->inherit_all;
787 info->use_handles = req->use_handles;
788 info->create_flags = req->create_flags;
789 info->hstdin = req->hstdin;
790 info->hstdout = req->hstdout;
791 info->hstderr = req->hstderr;
792 info->exe_file = NULL;
793 info->owner = (struct thread *)grab_object( current );
794 info->process = NULL;
796 info->data_size = get_req_data_size();
800 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
803 if (!(info->data = mem_alloc( info->data_size ))) goto done;
804 memcpy( info->data, get_req_data(), info->data_size );
805 current->info = info;
806 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
809 release_object( info );
812 /* Retrieve information about a newly started process */
813 DECL_HANDLER(get_new_process_info)
815 struct startup_info *info;
817 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
818 0, &startup_info_ops )))
820 reply->pid = get_process_id( info->process );
821 reply->tid = get_thread_id( info->thread );
822 reply->phandle = alloc_handle( current->process, info->process,
823 PROCESS_ALL_ACCESS, req->pinherit );
824 reply->thandle = alloc_handle( current->process, info->thread,
825 THREAD_ALL_ACCESS, req->tinherit );
826 reply->success = (info->state == STARTUP_DONE);
827 release_object( info );
839 /* Retrieve the new process startup info */
840 DECL_HANDLER(get_startup_info)
842 struct startup_info *info;
844 if ((info = current->process->startup_info))
846 size_t size = info->data_size;
847 if (size > get_reply_max_size()) size = get_reply_max_size();
849 /* we return the data directly without making a copy so this can only be called once */
850 set_reply_data_ptr( info->data, size );
857 /* initialize a new process */
858 DECL_HANDLER(init_process)
860 if (!current->unix_pid)
862 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
865 if (current->process->startup_info)
867 fatal_protocol_error( current, "init_process: called twice\n" );
870 reply->info_size = 0;
871 current->process->ldt_copy = req->ldt_copy;
872 current->process->startup_info = init_process( req->ppid, reply );
875 /* signal the end of the process initialization */
876 DECL_HANDLER(init_process_done)
878 struct file *file = NULL;
879 struct process *process = current->process;
881 if (is_process_init_done(process))
883 fatal_protocol_error( current, "init_process_done: called twice\n" );
888 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
891 process->exe.base = req->module;
892 process->exe.size = req->module_size;
893 process->exe.name = req->name;
895 if (req->exe_file) file = get_file_obj( current->process, req->exe_file, GENERIC_READ );
896 if (process->exe.file) release_object( process->exe.file );
897 process->exe.file = file;
899 if ((process->exe.namelen = get_req_data_size()))
900 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
902 generate_startup_debug_events( process, req->entry );
903 set_process_startup_state( process, STARTUP_DONE );
905 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
906 if (current->suspend + current->process->suspend > 0) stop_thread( current );
907 reply->debugged = (current->process->debugger != 0);
910 /* open a handle to a process */
911 DECL_HANDLER(open_process)
913 struct process *process = get_process_from_id( req->pid );
917 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
918 release_object( process );
922 /* terminate a process */
923 DECL_HANDLER(terminate_process)
925 struct process *process;
927 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
929 reply->self = (current->process == process);
930 kill_process( process, current, req->exit_code );
931 release_object( process );
935 /* fetch information about a process */
936 DECL_HANDLER(get_process_info)
938 struct process *process;
940 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
942 get_process_info( process, reply );
943 release_object( process );
947 /* set information about a process */
948 DECL_HANDLER(set_process_info)
950 struct process *process;
952 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
954 set_process_info( process, req );
955 release_object( process );
959 /* read data from a process address space */
960 DECL_HANDLER(read_process_memory)
962 struct process *process;
963 size_t len = get_reply_max_size();
965 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
969 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
970 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
971 const int *start = (int *)((char *)req->addr - start_offset);
972 int *buffer = mem_alloc( nb_ints * sizeof(int) );
975 if (read_process_memory( process, start, nb_ints, buffer ))
977 /* move start of requested data to start of buffer */
978 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
979 set_reply_data_ptr( buffer, len );
984 release_object( process );
987 /* write data to a process address space */
988 DECL_HANDLER(write_process_memory)
990 struct process *process;
992 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
994 size_t len = get_req_data_size();
995 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
996 set_error( STATUS_INVALID_PARAMETER );
999 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1000 req->first_mask, req->last_mask, get_req_data() );
1002 release_object( process );
1006 /* notify the server that a dll has been loaded */
1007 DECL_HANDLER(load_dll)
1009 struct process_dll *dll;
1010 struct file *file = NULL;
1013 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1015 if ((dll = process_load_dll( current->process, file, req->base,
1016 get_req_data(), get_req_data_size() )))
1018 dll->size = req->size;
1019 dll->dbg_offset = req->dbg_offset;
1020 dll->dbg_size = req->dbg_size;
1021 dll->name = req->name;
1022 /* only generate event if initialization is done */
1023 if (is_process_init_done( current->process ))
1024 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1026 if (file) release_object( file );
1029 /* notify the server that a dll is being unloaded */
1030 DECL_HANDLER(unload_dll)
1032 process_unload_dll( current->process, req->base );
1035 /* wait for a process to start waiting on input */
1036 /* FIXME: only returns event for now, wait is done in the client */
1037 DECL_HANDLER(wait_input_idle)
1039 struct process *process;
1042 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1044 if (process->idle_event && process != current->process && process->queue != current->queue)
1045 reply->event = alloc_handle( current->process, process->idle_event,
1046 EVENT_ALL_ACCESS, 0 );
1047 release_object( process );