Various cosmetic changes.
[wine/wine64.git] / server / process.c
blob43b5555e7ce61812bec3352735c05a225b744245
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <signal.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/time.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
19 #endif
20 #include <unistd.h>
22 #include "winbase.h"
23 #include "winnt.h"
25 #include "handle.h"
26 #include "process.h"
27 #include "thread.h"
28 #include "request.h"
29 #include "console.h"
31 /* process structure */
33 static struct process *first_process;
34 static int running_processes;
36 /* process operations */
38 static void process_dump( struct object *obj, int verbose );
39 static int process_signaled( struct object *obj, struct thread *thread );
40 static void process_poll_event( struct object *obj, int event );
41 static void process_destroy( struct object *obj );
43 static const struct object_ops process_ops =
45 sizeof(struct process), /* size */
46 process_dump, /* dump */
47 add_queue, /* add_queue */
48 remove_queue, /* remove_queue */
49 process_signaled, /* signaled */
50 no_satisfied, /* satisfied */
51 NULL, /* get_poll_events */
52 process_poll_event, /* poll_event */
53 no_get_fd, /* get_fd */
54 no_flush, /* flush */
55 no_get_file_info, /* get_file_info */
56 NULL, /* queue_async */
57 process_destroy /* destroy */
60 /* process startup info */
62 struct startup_info
64 struct object obj; /* object header */
65 int inherit_all; /* inherit all handles from parent */
66 int create_flags; /* creation flags */
67 int start_flags; /* flags from startup info */
68 handle_t hstdin; /* handle for stdin */
69 handle_t hstdout; /* handle for stdout */
70 handle_t hstderr; /* handle for stderr */
71 int cmd_show; /* main window show mode */
72 struct file *exe_file; /* file handle for main exe */
73 char *filename; /* file name for main exe */
74 struct thread *owner; /* owner thread (the one that created the new process) */
75 struct process *process; /* created process */
76 struct thread *thread; /* created thread */
79 static void startup_info_dump( struct object *obj, int verbose );
80 static int startup_info_signaled( struct object *obj, struct thread *thread );
81 static void startup_info_destroy( struct object *obj );
83 static const struct object_ops startup_info_ops =
85 sizeof(struct startup_info), /* size */
86 startup_info_dump, /* dump */
87 add_queue, /* add_queue */
88 remove_queue, /* remove_queue */
89 startup_info_signaled, /* signaled */
90 no_satisfied, /* satisfied */
91 NULL, /* get_poll_events */
92 NULL, /* poll_event */
93 no_get_fd, /* get_fd */
94 no_flush, /* flush */
95 no_get_file_info, /* get_file_info */
96 NULL, /* queue_async */
97 startup_info_destroy /* destroy */
101 /* set the console and stdio handles for a newly created process */
102 static int set_process_console( struct process *process, struct thread *parent_thread,
103 struct startup_info *info, struct init_process_reply *reply )
105 if (process->create_flags & CREATE_NEW_CONSOLE)
107 /* let the process init do the allocation */
108 return 1;
110 else if (parent_thread && !(process->create_flags & DETACHED_PROCESS))
112 /* FIXME: some better error checking should be done...
113 * like if hConOut and hConIn are console handles, then they should be on the same
114 * physical console
116 inherit_console( parent_thread, process,
117 (info->inherit_all || (info->start_flags & STARTF_USESTDHANDLES)) ?
118 info->hstdin : 0 );
120 if (parent_thread)
122 if (!info->inherit_all && !(info->start_flags & STARTF_USESTDHANDLES))
124 /* duplicate the handle from the parent into this process */
125 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
126 0, TRUE, DUPLICATE_SAME_ACCESS );
127 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
128 0, TRUE, DUPLICATE_SAME_ACCESS );
129 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
130 0, TRUE, DUPLICATE_SAME_ACCESS );
132 else
134 reply->hstdin = info->hstdin;
135 reply->hstdout = info->hstdout;
136 reply->hstderr = info->hstderr;
139 else
141 if (process->console)
143 reply->hstdin = alloc_handle( process, process->console,
144 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
145 reply->hstdout = alloc_handle( process, process->console->active,
146 GENERIC_READ | GENERIC_WRITE, 1 );
147 reply->hstderr = alloc_handle( process, process->console->active,
148 GENERIC_READ | GENERIC_WRITE, 1 );
150 else
152 /* no parent, let the caller decide what to do */
153 reply->hstdin = reply->hstdout = reply->hstderr = 0;
156 /* some handles above may have been invalid; this is not an error */
157 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
158 return 1;
161 /* create a new process and its main thread */
162 struct thread *create_process( int fd )
164 struct process *process;
165 struct thread *thread = NULL;
166 int request_pipe[2];
168 if (!(process = alloc_object( &process_ops, fd ))) return NULL;
169 process->next = NULL;
170 process->prev = NULL;
171 process->parent = NULL;
172 process->thread_list = NULL;
173 process->debugger = NULL;
174 process->handles = NULL;
175 process->exit_code = STILL_ACTIVE;
176 process->running_threads = 0;
177 process->priority = NORMAL_PRIORITY_CLASS;
178 process->affinity = 1;
179 process->suspend = 0;
180 process->create_flags = 0;
181 process->console = NULL;
182 process->init_event = NULL;
183 process->idle_event = NULL;
184 process->queue = NULL;
185 process->atom_table = NULL;
186 process->ldt_copy = NULL;
187 process->exe.next = NULL;
188 process->exe.prev = NULL;
189 process->exe.file = NULL;
190 process->exe.dbg_offset = 0;
191 process->exe.dbg_size = 0;
193 gettimeofday( &process->start_time, NULL );
194 if ((process->next = first_process) != NULL) process->next->prev = process;
195 first_process = process;
197 /* create the init done event */
198 if (!(process->init_event = create_event( NULL, 0, 1, 0 ))) goto error;
200 /* create the main thread */
201 if (pipe( request_pipe ) == -1)
203 file_set_error();
204 goto error;
206 send_client_fd( process, request_pipe[1], 0 );
207 close( request_pipe[1] );
208 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
210 set_select_events( &process->obj, POLLIN ); /* start listening to events */
211 release_object( process );
212 return thread;
214 error:
215 if (thread) release_object( thread );
216 release_object( process );
217 return NULL;
220 /* initialize the current process and fill in the request */
221 static void init_process( int ppid, struct init_process_reply *reply )
223 struct process *process = current->process;
224 struct thread *parent_thread = get_thread_from_pid( ppid );
225 struct process *parent = NULL;
226 struct startup_info *info = NULL;
228 if (parent_thread)
230 parent = parent_thread->process;
231 info = parent_thread->info;
232 if (!info)
234 fatal_protocol_error( current, "init_process: parent but no info\n" );
235 return;
237 if (info->thread)
239 fatal_protocol_error( current, "init_process: called twice?\n" );
240 return;
242 process->parent = (struct process *)grab_object( parent );
245 /* set the process flags */
246 process->create_flags = info ? info->create_flags : 0;
248 /* create the handle table */
249 if (parent && info->inherit_all)
250 process->handles = copy_handle_table( process, parent );
251 else
252 process->handles = alloc_handle_table( process, 0 );
253 if (!process->handles) return;
255 /* retrieve the main exe file */
256 reply->exe_file = 0;
257 if (parent && info->exe_file)
259 process->exe.file = (struct file *)grab_object( info->exe_file );
260 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
261 return;
264 /* set the process console */
265 if (!set_process_console( process, parent_thread, info, reply )) return;
267 if (parent)
269 /* attach to the debugger if requested */
270 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
271 set_process_debugger( process, parent_thread );
272 else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
273 set_process_debugger( process, parent->debugger );
276 /* thread will be actually suspended in init_done */
277 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
279 if (info)
281 size_t size = strlen(info->filename);
282 if (size > get_reply_max_size()) size = get_reply_max_size();
283 reply->start_flags = info->start_flags;
284 reply->cmd_show = info->cmd_show;
285 set_reply_data( info->filename, size );
286 info->process = (struct process *)grab_object( process );
287 info->thread = (struct thread *)grab_object( current );
288 wake_up( &info->obj, 0 );
290 else
292 reply->start_flags = STARTF_USESTDHANDLES;
293 reply->cmd_show = 0;
295 reply->create_flags = process->create_flags;
296 reply->server_start = server_start_ticks;
299 /* destroy a process when its refcount is 0 */
300 static void process_destroy( struct object *obj )
302 struct process *process = (struct process *)obj;
303 assert( obj->ops == &process_ops );
305 /* we can't have a thread remaining */
306 assert( !process->thread_list );
307 if (process->console) release_object( process->console );
308 if (process->parent) release_object( process->parent );
309 if (process->next) process->next->prev = process->prev;
310 if (process->prev) process->prev->next = process->next;
311 else first_process = process->next;
312 if (process->init_event) release_object( process->init_event );
313 if (process->idle_event) release_object( process->idle_event );
314 if (process->queue) release_object( process->queue );
315 if (process->atom_table) release_object( process->atom_table );
316 if (process->exe.file) release_object( process->exe.file );
319 /* dump a process on stdout for debugging purposes */
320 static void process_dump( struct object *obj, int verbose )
322 struct process *process = (struct process *)obj;
323 assert( obj->ops == &process_ops );
325 fprintf( stderr, "Process next=%p prev=%p handles=%p\n",
326 process->next, process->prev, process->handles );
329 static int process_signaled( struct object *obj, struct thread *thread )
331 struct process *process = (struct process *)obj;
332 return !process->running_threads;
336 static void process_poll_event( struct object *obj, int event )
338 struct process *process = (struct process *)obj;
339 assert( obj->ops == &process_ops );
341 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
342 else if (event & POLLIN) receive_fd( process );
345 static void startup_info_destroy( struct object *obj )
347 struct startup_info *info = (struct startup_info *)obj;
348 assert( obj->ops == &startup_info_ops );
349 if (info->filename) free( info->filename );
350 if (info->exe_file) release_object( info->exe_file );
351 if (info->process) release_object( info->process );
352 if (info->thread) release_object( info->thread );
353 if (info->owner)
355 info->owner->info = NULL;
356 release_object( info->owner );
360 static void startup_info_dump( struct object *obj, int verbose )
362 struct startup_info *info = (struct startup_info *)obj;
363 assert( obj->ops == &startup_info_ops );
365 fprintf( stderr, "Startup info flags=%x in=%d out=%d err=%d name='%s'\n",
366 info->start_flags, info->hstdin, info->hstdout, info->hstderr, info->filename );
369 static int startup_info_signaled( struct object *obj, struct thread *thread )
371 struct startup_info *info = (struct startup_info *)obj;
372 return (info->thread != NULL);
376 /* get a process from an id (and increment the refcount) */
377 struct process *get_process_from_id( void *id )
379 struct process *p = first_process;
380 while (p && (p != id)) p = p->next;
381 if (p) grab_object( p );
382 else set_error( STATUS_INVALID_PARAMETER );
383 return p;
386 /* get a process from a handle (and increment the refcount) */
387 struct process *get_process_from_handle( handle_t handle, unsigned int access )
389 return (struct process *)get_handle_obj( current->process, handle,
390 access, &process_ops );
393 /* add a dll to a process list */
394 static struct process_dll *process_load_dll( struct process *process, struct file *file,
395 void *base )
397 struct process_dll *dll;
399 /* make sure we don't already have one with the same base address */
400 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
402 set_error( STATUS_INVALID_PARAMETER );
403 return NULL;
406 if ((dll = mem_alloc( sizeof(*dll) )))
408 dll->prev = &process->exe;
409 dll->file = NULL;
410 dll->base = base;
411 if (file) dll->file = (struct file *)grab_object( file );
412 if ((dll->next = process->exe.next)) dll->next->prev = dll;
413 process->exe.next = dll;
415 return dll;
418 /* remove a dll from a process list */
419 static void process_unload_dll( struct process *process, void *base )
421 struct process_dll *dll;
423 for (dll = process->exe.next; dll; dll = dll->next)
425 if (dll->base == base)
427 if (dll->file) release_object( dll->file );
428 if (dll->next) dll->next->prev = dll->prev;
429 if (dll->prev) dll->prev->next = dll->next;
430 free( dll );
431 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
432 return;
435 set_error( STATUS_INVALID_PARAMETER );
438 /* kill all processes being attached to a console renderer */
439 void kill_console_processes( struct thread *renderer, int exit_code )
441 for (;;) /* restart from the beginning of the list every time */
443 struct process *process = first_process;
445 /* find the first process being attached to 'renderer' and still running */
446 while (process &&
447 (process == renderer->process || !process->console ||
448 process->console->renderer != renderer || !process->running_threads))
450 process = process->next;
452 if (!process) break;
453 kill_process( process, NULL, exit_code );
457 /* a process has been killed (i.e. its last thread died) */
458 static void process_killed( struct process *process )
460 assert( !process->thread_list );
461 gettimeofday( &process->end_time, NULL );
462 if (process->handles) release_object( process->handles );
463 process->handles = NULL;
465 /* close the console attached to this process, if any */
466 free_console( process );
468 while (process->exe.next)
470 struct process_dll *dll = process->exe.next;
471 process->exe.next = dll->next;
472 if (dll->file) release_object( dll->file );
473 free( dll );
475 if (process->exe.file) release_object( process->exe.file );
476 process->exe.file = NULL;
477 wake_up( &process->obj, 0 );
478 if (!--running_processes)
480 /* last process died, close global handles */
481 close_global_handles();
482 /* this will cause the select loop to terminate */
483 if (!persistent_server) close_master_socket();
487 /* add a thread to a process running threads list */
488 void add_process_thread( struct process *process, struct thread *thread )
490 thread->proc_next = process->thread_list;
491 thread->proc_prev = NULL;
492 if (thread->proc_next) thread->proc_next->proc_prev = thread;
493 process->thread_list = thread;
494 if (!process->running_threads++) running_processes++;
495 grab_object( thread );
498 /* remove a thread from a process running threads list */
499 void remove_process_thread( struct process *process, struct thread *thread )
501 assert( process->running_threads > 0 );
502 assert( process->thread_list );
504 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
505 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
506 else process->thread_list = thread->proc_next;
508 if (!--process->running_threads)
510 /* we have removed the last running thread, exit the process */
511 process->exit_code = thread->exit_code;
512 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
513 process_killed( process );
515 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
516 release_object( thread );
519 /* suspend all the threads of a process */
520 void suspend_process( struct process *process )
522 if (!process->suspend++)
524 struct thread *thread = process->thread_list;
525 for (; thread; thread = thread->proc_next)
527 if (!thread->suspend) stop_thread( thread );
532 /* resume all the threads of a process */
533 void resume_process( struct process *process )
535 assert (process->suspend > 0);
536 if (!--process->suspend)
538 struct thread *thread = process->thread_list;
539 for (; thread; thread = thread->proc_next)
541 if (!thread->suspend) continue_thread( thread );
546 /* kill a process on the spot */
547 void kill_process( struct process *process, struct thread *skip, int exit_code )
549 struct thread *thread = process->thread_list;
551 while (thread)
553 struct thread *next = thread->proc_next;
554 thread->exit_code = exit_code;
555 if (thread != skip) kill_thread( thread, 1 );
556 thread = next;
560 /* kill all processes being debugged by a given thread */
561 void kill_debugged_processes( struct thread *debugger, int exit_code )
563 for (;;) /* restart from the beginning of the list every time */
565 struct process *process = first_process;
566 /* find the first process being debugged by 'debugger' and still running */
567 while (process && (process->debugger != debugger || !process->running_threads))
568 process = process->next;
569 if (!process) return;
570 process->debugger = NULL;
571 kill_process( process, NULL, exit_code );
576 /* detach a debugger from all its debuggees */
577 void detach_debugged_processes( struct thread *debugger )
579 struct process *process;
580 for (process = first_process; process; process = process->next)
582 if (process->debugger == debugger && process->running_threads)
584 debugger_detach( process, debugger );
590 /* get all information about a process */
591 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
593 reply->pid = get_process_id( process );
594 reply->debugged = (process->debugger != 0);
595 reply->exit_code = process->exit_code;
596 reply->priority = process->priority;
597 reply->process_affinity = process->affinity;
598 reply->system_affinity = 1;
601 /* set all information about a process */
602 static void set_process_info( struct process *process,
603 const struct set_process_info_request *req )
605 if (req->mask & SET_PROCESS_INFO_PRIORITY)
606 process->priority = req->priority;
607 if (req->mask & SET_PROCESS_INFO_AFFINITY)
609 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
610 else process->affinity = req->affinity;
614 /* read data from a process memory space */
615 /* len is the total size (in ints) */
616 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
618 struct thread *thread = process->thread_list;
620 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
622 if (!thread) /* process is dead */
624 set_error( STATUS_ACCESS_DENIED );
625 return 0;
627 if (suspend_for_ptrace( thread ))
629 while (len > 0)
631 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
632 len--;
634 resume_thread( thread );
636 return !len;
639 /* make sure we can write to the whole address range */
640 /* len is the total size (in ints) */
641 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
643 int page = get_page_size() / sizeof(int);
645 for (;;)
647 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
648 if (len <= page) break;
649 addr += page;
650 len -= page;
652 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
655 /* write data to a process memory space */
656 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
657 /* we check the total size for write permissions */
658 static void write_process_memory( struct process *process, int *addr, size_t len,
659 unsigned int first_mask, unsigned int last_mask, const int *src )
661 struct thread *thread = process->thread_list;
663 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
665 if (!thread) /* process is dead */
667 set_error( STATUS_ACCESS_DENIED );
668 return;
670 if (suspend_for_ptrace( thread ))
672 if (!check_process_write_access( thread, addr, len ))
674 set_error( STATUS_ACCESS_DENIED );
675 return;
677 /* first word is special */
678 if (len > 1)
680 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
681 len--;
683 else last_mask &= first_mask;
685 while (len > 1)
687 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
688 len--;
691 /* last word is special too */
692 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
694 done:
695 resume_thread( thread );
699 /* take a snapshot of currently running processes */
700 struct process_snapshot *process_snap( int *count )
702 struct process_snapshot *snapshot, *ptr;
703 struct process *process;
704 if (!running_processes) return NULL;
705 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
706 return NULL;
707 ptr = snapshot;
708 for (process = first_process; process; process = process->next)
710 if (!process->running_threads) continue;
711 ptr->process = process;
712 ptr->threads = process->running_threads;
713 ptr->count = process->obj.refcount;
714 ptr->priority = process->priority;
715 grab_object( process );
716 ptr++;
718 *count = running_processes;
719 return snapshot;
722 /* take a snapshot of the modules of a process */
723 struct module_snapshot *module_snap( struct process *process, int *count )
725 struct module_snapshot *snapshot, *ptr;
726 struct process_dll *dll;
727 int total = 0;
729 for (dll = &process->exe; dll; dll = dll->next) total++;
730 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
732 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
734 ptr->base = dll->base;
736 *count = total;
737 return snapshot;
741 /* create a new process */
742 DECL_HANDLER(new_process)
744 size_t len = get_req_data_size();
745 struct startup_info *info;
747 if (current->info)
749 fatal_protocol_error( current, "new_process: another process is being created\n" );
750 return;
753 /* build the startup info for a new process */
754 if (!(info = alloc_object( &startup_info_ops, -1 ))) return;
755 info->inherit_all = req->inherit_all;
756 info->create_flags = req->create_flags;
757 info->start_flags = req->start_flags;
758 info->hstdin = req->hstdin;
759 info->hstdout = req->hstdout;
760 info->hstderr = req->hstderr;
761 info->cmd_show = req->cmd_show;
762 info->exe_file = NULL;
763 info->filename = NULL;
764 info->owner = (struct thread *)grab_object( current );
765 info->process = NULL;
766 info->thread = NULL;
768 if (req->exe_file &&
769 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
770 goto done;
772 if (!(info->filename = mem_alloc( len + 1 ))) goto done;
774 memcpy( info->filename, get_req_data(), len );
775 info->filename[len] = 0;
776 current->info = info;
777 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
779 done:
780 release_object( info );
783 /* Retrieve information about a newly started process */
784 DECL_HANDLER(get_new_process_info)
786 struct startup_info *info;
788 reply->event = 0;
790 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
791 0, &startup_info_ops )))
793 reply->pid = get_process_id( info->process );
794 reply->tid = get_thread_id( info->thread );
795 reply->phandle = alloc_handle( current->process, info->process,
796 PROCESS_ALL_ACCESS, req->pinherit );
797 reply->thandle = alloc_handle( current->process, info->thread,
798 THREAD_ALL_ACCESS, req->tinherit );
799 if (info->process->init_event)
800 reply->event = alloc_handle( current->process, info->process->init_event,
801 EVENT_ALL_ACCESS, 0 );
802 release_object( info );
804 else
806 reply->pid = 0;
807 reply->tid = 0;
808 reply->phandle = 0;
809 reply->thandle = 0;
813 /* initialize a new process */
814 DECL_HANDLER(init_process)
816 if (!current->unix_pid)
818 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
819 return;
821 current->process->ldt_copy = req->ldt_copy;
822 init_process( req->ppid, reply );
825 /* signal the end of the process initialization */
826 DECL_HANDLER(init_process_done)
828 struct file *file = NULL;
829 struct process *process = current->process;
831 if (!process->init_event)
833 fatal_protocol_error( current, "init_process_done: no event\n" );
834 return;
836 process->exe.base = req->module;
837 process->exe.name = req->name;
839 if (req->exe_file) file = get_file_obj( current->process, req->exe_file, GENERIC_READ );
840 if (process->exe.file) release_object( process->exe.file );
841 process->exe.file = file;
843 generate_startup_debug_events( current->process, req->entry );
844 set_event( process->init_event );
845 release_object( process->init_event );
846 process->init_event = NULL;
847 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
848 if (current->suspend + current->process->suspend > 0) stop_thread( current );
849 reply->debugged = (current->process->debugger != 0);
852 /* open a handle to a process */
853 DECL_HANDLER(open_process)
855 struct process *process = get_process_from_id( req->pid );
856 reply->handle = 0;
857 if (process)
859 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
860 release_object( process );
864 /* terminate a process */
865 DECL_HANDLER(terminate_process)
867 struct process *process;
869 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
871 reply->self = (current->process == process);
872 kill_process( process, current, req->exit_code );
873 release_object( process );
877 /* fetch information about a process */
878 DECL_HANDLER(get_process_info)
880 struct process *process;
882 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
884 get_process_info( process, reply );
885 release_object( process );
889 /* set information about a process */
890 DECL_HANDLER(set_process_info)
892 struct process *process;
894 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
896 set_process_info( process, req );
897 release_object( process );
901 /* read data from a process address space */
902 DECL_HANDLER(read_process_memory)
904 struct process *process;
905 size_t len = get_reply_max_size();
907 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
909 if (len)
911 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
912 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
913 const int *start = (int *)((char *)req->addr - start_offset);
914 int *buffer = mem_alloc( nb_ints * sizeof(int) );
915 if (buffer)
917 if (read_process_memory( process, start, nb_ints, buffer ))
919 /* move start of requested data to start of buffer */
920 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
921 set_reply_data_ptr( buffer, len );
923 else len = 0;
926 release_object( process );
929 /* write data to a process address space */
930 DECL_HANDLER(write_process_memory)
932 struct process *process;
934 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
936 size_t len = get_req_data_size();
937 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
938 set_error( STATUS_INVALID_PARAMETER );
939 else
941 if (len) write_process_memory( process, req->addr, len / sizeof(int),
942 req->first_mask, req->last_mask, get_req_data() );
944 release_object( process );
948 /* notify the server that a dll has been loaded */
949 DECL_HANDLER(load_dll)
951 struct process_dll *dll;
952 struct file *file = NULL;
954 if (req->handle &&
955 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
957 if ((dll = process_load_dll( current->process, file, req->base )))
959 dll->dbg_offset = req->dbg_offset;
960 dll->dbg_size = req->dbg_size;
961 dll->name = req->name;
962 /* only generate event if initialization is done */
963 if (!current->process->init_event)
964 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
966 if (file) release_object( file );
969 /* notify the server that a dll is being unloaded */
970 DECL_HANDLER(unload_dll)
972 process_unload_dll( current->process, req->base );
975 /* wait for a process to start waiting on input */
976 /* FIXME: only returns event for now, wait is done in the client */
977 DECL_HANDLER(wait_input_idle)
979 struct process *process;
981 reply->event = 0;
982 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
984 if (process->idle_event && process != current->process && process->queue != current->queue)
985 reply->event = alloc_handle( current->process, process->idle_event,
986 EVENT_ALL_ACCESS, 0 );
987 release_object( process );