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
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
45 /* process structure */
47 static struct process *first_process;
48 static int running_processes;
50 /* process operations */
52 static void process_dump( struct object *obj, int verbose );
53 static int process_signaled( struct object *obj, struct thread *thread );
54 static void process_poll_event( struct object *obj, int event );
55 static void process_destroy( struct object *obj );
57 static const struct object_ops process_ops =
59 sizeof(struct process), /* size */
60 process_dump, /* dump */
61 add_queue, /* add_queue */
62 remove_queue, /* remove_queue */
63 process_signaled, /* signaled */
64 no_satisfied, /* satisfied */
65 NULL, /* get_poll_events */
66 process_poll_event, /* poll_event */
67 no_get_fd, /* get_fd */
69 no_get_file_info, /* get_file_info */
70 NULL, /* queue_async */
71 process_destroy /* destroy */
74 /* process startup info */
78 struct object obj; /* object header */
79 int inherit_all; /* inherit all handles from parent */
80 int create_flags; /* creation flags */
81 int start_flags; /* flags from startup info */
82 handle_t hstdin; /* handle for stdin */
83 handle_t hstdout; /* handle for stdout */
84 handle_t hstderr; /* handle for stderr */
85 int cmd_show; /* main window show mode */
86 struct file *exe_file; /* file handle for main exe */
87 char *filename; /* file name for main exe */
88 struct thread *owner; /* owner thread (the one that created the new process) */
89 struct process *process; /* created process */
90 struct thread *thread; /* created thread */
93 static void startup_info_dump( struct object *obj, int verbose );
94 static int startup_info_signaled( struct object *obj, struct thread *thread );
95 static void startup_info_destroy( struct object *obj );
97 static const struct object_ops startup_info_ops =
99 sizeof(struct startup_info), /* size */
100 startup_info_dump, /* dump */
101 add_queue, /* add_queue */
102 remove_queue, /* remove_queue */
103 startup_info_signaled, /* signaled */
104 no_satisfied, /* satisfied */
105 NULL, /* get_poll_events */
106 NULL, /* poll_event */
107 no_get_fd, /* get_fd */
108 no_flush, /* flush */
109 no_get_file_info, /* get_file_info */
110 NULL, /* queue_async */
111 startup_info_destroy /* destroy */
115 /* set the console and stdio handles for a newly created process */
116 static int set_process_console( struct process *process, struct thread *parent_thread,
117 struct startup_info *info, struct init_process_reply *reply )
119 if (process->create_flags & CREATE_NEW_CONSOLE)
121 /* let the process init do the allocation */
124 else if (parent_thread && !(process->create_flags & DETACHED_PROCESS))
126 /* FIXME: some better error checking should be done...
127 * like if hConOut and hConIn are console handles, then they should be on the same
130 inherit_console( parent_thread, process,
131 (info->inherit_all || (info->start_flags & STARTF_USESTDHANDLES)) ?
136 if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
138 /* duplicate the handle from the parent into this process */
139 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
140 0, TRUE, DUPLICATE_SAME_ACCESS );
141 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
142 0, TRUE, DUPLICATE_SAME_ACCESS );
143 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
144 0, TRUE, DUPLICATE_SAME_ACCESS );
148 reply->hstdin = info->hstdin;
149 reply->hstdout = info->hstdout;
150 reply->hstderr = info->hstderr;
155 if (process->console)
157 reply->hstdin = alloc_handle( process, process->console,
158 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
159 reply->hstdout = alloc_handle( process, process->console->active,
160 GENERIC_READ | GENERIC_WRITE, 1 );
161 reply->hstderr = alloc_handle( process, process->console->active,
162 GENERIC_READ | GENERIC_WRITE, 1 );
166 /* no parent, let the caller decide what to do */
167 reply->hstdin = reply->hstdout = reply->hstderr = 0;
170 /* some handles above may have been invalid; this is not an error */
171 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
175 /* create a new process and its main thread */
176 struct thread *create_process( int fd )
178 struct process *process;
179 struct thread *thread = NULL;
182 if (!(process = alloc_object( &process_ops, fd ))) return NULL;
183 process->next = NULL;
184 process->prev = NULL;
185 process->parent = NULL;
186 process->thread_list = NULL;
187 process->debugger = NULL;
188 process->handles = NULL;
189 process->exit_code = STILL_ACTIVE;
190 process->running_threads = 0;
191 process->priority = NORMAL_PRIORITY_CLASS;
192 process->affinity = 1;
193 process->suspend = 0;
194 process->create_flags = 0;
195 process->console = NULL;
196 process->init_event = NULL;
197 process->idle_event = NULL;
198 process->queue = NULL;
199 process->atom_table = NULL;
200 process->ldt_copy = NULL;
201 process->exe.next = NULL;
202 process->exe.prev = NULL;
203 process->exe.file = NULL;
204 process->exe.dbg_offset = 0;
205 process->exe.dbg_size = 0;
207 gettimeofday( &process->start_time, NULL );
208 if ((process->next = first_process) != NULL) process->next->prev = process;
209 first_process = process;
211 /* create the init done event */
212 if (!(process->init_event = create_event( NULL, 0, 1, 0 ))) goto error;
214 /* create the main thread */
215 if (pipe( request_pipe ) == -1)
220 send_client_fd( process, request_pipe[1], 0 );
221 close( request_pipe[1] );
222 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
224 set_select_events( &process->obj, POLLIN ); /* start listening to events */
225 release_object( process );
229 if (thread) release_object( thread );
230 release_object( process );
234 /* initialize the current process and fill in the request */
235 static void init_process( int ppid, struct init_process_reply *reply )
237 struct process *process = current->process;
238 struct thread *parent_thread = get_thread_from_pid( ppid );
239 struct process *parent = NULL;
240 struct startup_info *info = NULL;
244 parent = parent_thread->process;
245 info = parent_thread->info;
248 fatal_protocol_error( current, "init_process: parent but no info\n" );
253 fatal_protocol_error( current, "init_process: called twice?\n" );
256 process->parent = (struct process *)grab_object( parent );
259 /* set the process flags */
260 process->create_flags = info ? info->create_flags : 0;
262 /* create the handle table */
263 if (parent && info->inherit_all)
264 process->handles = copy_handle_table( process, parent );
266 process->handles = alloc_handle_table( process, 0 );
267 if (!process->handles) return;
269 /* retrieve the main exe file */
271 if (parent && info->exe_file)
273 process->exe.file = (struct file *)grab_object( info->exe_file );
274 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
278 /* set the process console */
279 if (!set_process_console( process, parent_thread, info, reply )) return;
283 /* attach to the debugger if requested */
284 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
285 set_process_debugger( process, parent_thread );
286 else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
287 set_process_debugger( process, parent->debugger );
290 /* thread will be actually suspended in init_done */
291 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
295 size_t size = strlen(info->filename);
296 if (size > get_reply_max_size()) size = get_reply_max_size();
297 reply->start_flags = info->start_flags;
298 reply->cmd_show = info->cmd_show;
299 set_reply_data( info->filename, size );
300 info->process = (struct process *)grab_object( process );
301 info->thread = (struct thread *)grab_object( current );
302 wake_up( &info->obj, 0 );
306 reply->start_flags = STARTF_USESTDHANDLES;
309 reply->create_flags = process->create_flags;
310 reply->server_start = server_start_ticks;
313 /* destroy a process when its refcount is 0 */
314 static void process_destroy( struct object *obj )
316 struct process *process = (struct process *)obj;
317 assert( obj->ops == &process_ops );
319 /* we can't have a thread remaining */
320 assert( !process->thread_list );
321 if (process->console) release_object( process->console );
322 if (process->parent) release_object( process->parent );
323 if (process->next) process->next->prev = process->prev;
324 if (process->prev) process->prev->next = process->next;
325 else first_process = process->next;
326 if (process->init_event) release_object( process->init_event );
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 );
333 /* dump a process on stdout for debugging purposes */
334 static void process_dump( struct object *obj, int verbose )
336 struct process *process = (struct process *)obj;
337 assert( obj->ops == &process_ops );
339 fprintf( stderr, "Process next=%p prev=%p handles=%p\n",
340 process->next, process->prev, process->handles );
343 static int process_signaled( struct object *obj, struct thread *thread )
345 struct process *process = (struct process *)obj;
346 return !process->running_threads;
350 static void process_poll_event( struct object *obj, int event )
352 struct process *process = (struct process *)obj;
353 assert( obj->ops == &process_ops );
355 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
356 else if (event & POLLIN) receive_fd( process );
359 static void startup_info_destroy( struct object *obj )
361 struct startup_info *info = (struct startup_info *)obj;
362 assert( obj->ops == &startup_info_ops );
363 if (info->filename) free( info->filename );
364 if (info->exe_file) release_object( info->exe_file );
365 if (info->process) release_object( info->process );
366 if (info->thread) release_object( info->thread );
369 info->owner->info = NULL;
370 release_object( info->owner );
374 static void startup_info_dump( struct object *obj, int verbose )
376 struct startup_info *info = (struct startup_info *)obj;
377 assert( obj->ops == &startup_info_ops );
379 fprintf( stderr, "Startup info flags=%x in=%d out=%d err=%d name='%s'\n",
380 info->start_flags, info->hstdin, info->hstdout, info->hstderr, info->filename );
383 static int startup_info_signaled( struct object *obj, struct thread *thread )
385 struct startup_info *info = (struct startup_info *)obj;
386 return (info->thread != NULL);
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,
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 if (file) dll->file = (struct file *)grab_object( file );
426 if ((dll->next = process->exe.next)) dll->next->prev = dll;
427 process->exe.next = dll;
432 /* remove a dll from a process list */
433 static void process_unload_dll( struct process *process, void *base )
435 struct process_dll *dll;
437 for (dll = process->exe.next; dll; dll = dll->next)
439 if (dll->base == base)
441 if (dll->file) release_object( dll->file );
442 if (dll->next) dll->next->prev = dll->prev;
443 if (dll->prev) dll->prev->next = dll->next;
445 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
449 set_error( STATUS_INVALID_PARAMETER );
452 /* kill all processes being attached to a console renderer */
453 void kill_console_processes( struct thread *renderer, int exit_code )
455 for (;;) /* restart from the beginning of the list every time */
457 struct process *process = first_process;
459 /* find the first process being attached to 'renderer' and still running */
461 (process == renderer->process || !process->console ||
462 process->console->renderer != renderer || !process->running_threads))
464 process = process->next;
467 kill_process( process, NULL, exit_code );
471 /* a process has been killed (i.e. its last thread died) */
472 static void process_killed( struct process *process )
474 assert( !process->thread_list );
475 gettimeofday( &process->end_time, NULL );
476 if (process->handles) release_object( process->handles );
477 process->handles = NULL;
479 /* close the console attached to this process, if any */
480 free_console( process );
482 while (process->exe.next)
484 struct process_dll *dll = process->exe.next;
485 process->exe.next = dll->next;
486 if (dll->file) release_object( dll->file );
489 if (process->exe.file) release_object( process->exe.file );
490 process->exe.file = NULL;
491 wake_up( &process->obj, 0 );
492 if (!--running_processes)
494 /* last process died, close global handles */
495 close_global_handles();
496 /* this will cause the select loop to terminate */
497 if (!persistent_server) close_master_socket();
501 /* add a thread to a process running threads list */
502 void add_process_thread( struct process *process, struct thread *thread )
504 thread->proc_next = process->thread_list;
505 thread->proc_prev = NULL;
506 if (thread->proc_next) thread->proc_next->proc_prev = thread;
507 process->thread_list = thread;
508 if (!process->running_threads++) running_processes++;
509 grab_object( thread );
512 /* remove a thread from a process running threads list */
513 void remove_process_thread( struct process *process, struct thread *thread )
515 assert( process->running_threads > 0 );
516 assert( process->thread_list );
518 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
519 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
520 else process->thread_list = thread->proc_next;
522 if (!--process->running_threads)
524 /* we have removed the last running thread, exit the process */
525 process->exit_code = thread->exit_code;
526 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
527 process_killed( process );
529 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
530 release_object( thread );
533 /* suspend all the threads of a process */
534 void suspend_process( struct process *process )
536 if (!process->suspend++)
538 struct thread *thread = process->thread_list;
539 for (; thread; thread = thread->proc_next)
541 if (!thread->suspend) stop_thread( thread );
546 /* resume all the threads of a process */
547 void resume_process( struct process *process )
549 assert (process->suspend > 0);
550 if (!--process->suspend)
552 struct thread *thread = process->thread_list;
553 for (; thread; thread = thread->proc_next)
555 if (!thread->suspend) continue_thread( thread );
560 /* kill a process on the spot */
561 void kill_process( struct process *process, struct thread *skip, int exit_code )
563 struct thread *thread = process->thread_list;
567 struct thread *next = thread->proc_next;
568 thread->exit_code = exit_code;
569 if (thread != skip) kill_thread( thread, 1 );
574 /* kill all processes being debugged by a given thread */
575 void kill_debugged_processes( struct thread *debugger, int exit_code )
577 for (;;) /* restart from the beginning of the list every time */
579 struct process *process = first_process;
580 /* find the first process being debugged by 'debugger' and still running */
581 while (process && (process->debugger != debugger || !process->running_threads))
582 process = process->next;
583 if (!process) return;
584 process->debugger = NULL;
585 kill_process( process, NULL, exit_code );
590 /* detach a debugger from all its debuggees */
591 void detach_debugged_processes( struct thread *debugger )
593 struct process *process;
594 for (process = first_process; process; process = process->next)
596 if (process->debugger == debugger && process->running_threads)
598 debugger_detach( process, debugger );
604 /* get all information about a process */
605 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
607 reply->pid = get_process_id( process );
608 reply->debugged = (process->debugger != 0);
609 reply->exit_code = process->exit_code;
610 reply->priority = process->priority;
611 reply->process_affinity = process->affinity;
612 reply->system_affinity = 1;
615 /* set all information about a process */
616 static void set_process_info( struct process *process,
617 const struct set_process_info_request *req )
619 if (req->mask & SET_PROCESS_INFO_PRIORITY)
620 process->priority = req->priority;
621 if (req->mask & SET_PROCESS_INFO_AFFINITY)
623 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
624 else process->affinity = req->affinity;
628 /* read data from a process memory space */
629 /* len is the total size (in ints) */
630 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
632 struct thread *thread = process->thread_list;
634 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
636 if (!thread) /* process is dead */
638 set_error( STATUS_ACCESS_DENIED );
641 if (suspend_for_ptrace( thread ))
645 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
648 resume_thread( thread );
653 /* make sure we can write to the whole address range */
654 /* len is the total size (in ints) */
655 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
657 int page = get_page_size() / sizeof(int);
661 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
662 if (len <= page) break;
666 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
669 /* write data to a process memory space */
670 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
671 /* we check the total size for write permissions */
672 static void write_process_memory( struct process *process, int *addr, size_t len,
673 unsigned int first_mask, unsigned int last_mask, const int *src )
675 struct thread *thread = process->thread_list;
677 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
679 if (!thread) /* process is dead */
681 set_error( STATUS_ACCESS_DENIED );
684 if (suspend_for_ptrace( thread ))
686 if (!check_process_write_access( thread, addr, len ))
688 set_error( STATUS_ACCESS_DENIED );
691 /* first word is special */
694 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
697 else last_mask &= first_mask;
701 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
705 /* last word is special too */
706 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
709 resume_thread( thread );
713 /* take a snapshot of currently running processes */
714 struct process_snapshot *process_snap( int *count )
716 struct process_snapshot *snapshot, *ptr;
717 struct process *process;
718 if (!running_processes) return NULL;
719 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
722 for (process = first_process; process; process = process->next)
724 if (!process->running_threads) continue;
725 ptr->process = process;
726 ptr->threads = process->running_threads;
727 ptr->count = process->obj.refcount;
728 ptr->priority = process->priority;
729 grab_object( process );
732 *count = running_processes;
736 /* take a snapshot of the modules of a process */
737 struct module_snapshot *module_snap( struct process *process, int *count )
739 struct module_snapshot *snapshot, *ptr;
740 struct process_dll *dll;
743 for (dll = &process->exe; dll; dll = dll->next) total++;
744 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
746 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
748 ptr->base = dll->base;
755 /* create a new process */
756 DECL_HANDLER(new_process)
758 size_t len = get_req_data_size();
759 struct startup_info *info;
763 fatal_protocol_error( current, "new_process: another process is being created\n" );
767 /* build the startup info for a new process */
768 if (!(info = alloc_object( &startup_info_ops, -1 ))) return;
769 info->inherit_all = req->inherit_all;
770 info->create_flags = req->create_flags;
771 info->start_flags = req->start_flags;
772 info->hstdin = req->hstdin;
773 info->hstdout = req->hstdout;
774 info->hstderr = req->hstderr;
775 info->cmd_show = req->cmd_show;
776 info->exe_file = NULL;
777 info->filename = NULL;
778 info->owner = (struct thread *)grab_object( current );
779 info->process = NULL;
783 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
786 if (!(info->filename = mem_alloc( len + 1 ))) goto done;
788 memcpy( info->filename, get_req_data(), len );
789 info->filename[len] = 0;
790 current->info = info;
791 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
794 release_object( info );
797 /* Retrieve information about a newly started process */
798 DECL_HANDLER(get_new_process_info)
800 struct startup_info *info;
804 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
805 0, &startup_info_ops )))
807 reply->pid = get_process_id( info->process );
808 reply->tid = get_thread_id( info->thread );
809 reply->phandle = alloc_handle( current->process, info->process,
810 PROCESS_ALL_ACCESS, req->pinherit );
811 reply->thandle = alloc_handle( current->process, info->thread,
812 THREAD_ALL_ACCESS, req->tinherit );
813 if (info->process->init_event)
814 reply->event = alloc_handle( current->process, info->process->init_event,
815 EVENT_ALL_ACCESS, 0 );
816 release_object( info );
827 /* initialize a new process */
828 DECL_HANDLER(init_process)
830 if (!current->unix_pid)
832 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
835 current->process->ldt_copy = req->ldt_copy;
836 init_process( req->ppid, reply );
839 /* signal the end of the process initialization */
840 DECL_HANDLER(init_process_done)
842 struct file *file = NULL;
843 struct process *process = current->process;
845 if (!process->init_event)
847 fatal_protocol_error( current, "init_process_done: no event\n" );
850 process->exe.base = req->module;
851 process->exe.name = req->name;
853 if (req->exe_file) file = get_file_obj( current->process, req->exe_file, GENERIC_READ );
854 if (process->exe.file) release_object( process->exe.file );
855 process->exe.file = file;
857 generate_startup_debug_events( current->process, req->entry );
858 set_event( process->init_event );
859 release_object( process->init_event );
860 process->init_event = NULL;
861 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
862 if (current->suspend + current->process->suspend > 0) stop_thread( current );
863 reply->debugged = (current->process->debugger != 0);
866 /* open a handle to a process */
867 DECL_HANDLER(open_process)
869 struct process *process = get_process_from_id( req->pid );
873 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
874 release_object( process );
878 /* terminate a process */
879 DECL_HANDLER(terminate_process)
881 struct process *process;
883 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
885 reply->self = (current->process == process);
886 kill_process( process, current, req->exit_code );
887 release_object( process );
891 /* fetch information about a process */
892 DECL_HANDLER(get_process_info)
894 struct process *process;
896 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
898 get_process_info( process, reply );
899 release_object( process );
903 /* set information about a process */
904 DECL_HANDLER(set_process_info)
906 struct process *process;
908 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
910 set_process_info( process, req );
911 release_object( process );
915 /* read data from a process address space */
916 DECL_HANDLER(read_process_memory)
918 struct process *process;
919 size_t len = get_reply_max_size();
921 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
925 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
926 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
927 const int *start = (int *)((char *)req->addr - start_offset);
928 int *buffer = mem_alloc( nb_ints * sizeof(int) );
931 if (read_process_memory( process, start, nb_ints, buffer ))
933 /* move start of requested data to start of buffer */
934 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
935 set_reply_data_ptr( buffer, len );
940 release_object( process );
943 /* write data to a process address space */
944 DECL_HANDLER(write_process_memory)
946 struct process *process;
948 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
950 size_t len = get_req_data_size();
951 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
952 set_error( STATUS_INVALID_PARAMETER );
955 if (len) write_process_memory( process, req->addr, len / sizeof(int),
956 req->first_mask, req->last_mask, get_req_data() );
958 release_object( process );
962 /* notify the server that a dll has been loaded */
963 DECL_HANDLER(load_dll)
965 struct process_dll *dll;
966 struct file *file = NULL;
969 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
971 if ((dll = process_load_dll( current->process, file, req->base )))
973 dll->dbg_offset = req->dbg_offset;
974 dll->dbg_size = req->dbg_size;
975 dll->name = req->name;
976 /* only generate event if initialization is done */
977 if (!current->process->init_event)
978 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
980 if (file) release_object( file );
983 /* notify the server that a dll is being unloaded */
984 DECL_HANDLER(unload_dll)
986 process_unload_dll( current->process, req->base );
989 /* wait for a process to start waiting on input */
990 /* FIXME: only returns event for now, wait is done in the client */
991 DECL_HANDLER(wait_input_idle)
993 struct process *process;
996 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
998 if (process->idle_event && process != current->process && process->queue != current->queue)
999 reply->event = alloc_handle( current->process, process->idle_event,
1000 EVENT_ALL_ACCESS, 0 );
1001 release_object( process );