Added some tests.
[wine/multimedia.git] / server / process.c
blob55cd67e4fd25c0ad8b68d16f4652bac425f883e4
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 <errno.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <string.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 "winbase.h"
38 #include "winnt.h"
40 #include "handle.h"
41 #include "process.h"
42 #include "thread.h"
43 #include "request.h"
44 #include "console.h"
46 /* process structure */
48 static struct process *first_process;
49 static int running_processes;
51 /* process operations */
53 static void process_dump( struct object *obj, int verbose );
54 static int process_signaled( struct object *obj, struct thread *thread );
55 static void process_poll_event( struct 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 */
69 no_flush, /* flush */
70 no_get_file_info, /* get_file_info */
71 NULL, /* queue_async */
72 process_destroy /* destroy */
75 /* process startup info */
77 struct startup_info
79 struct object obj; /* object header */
80 int inherit_all; /* inherit all handles from parent */
81 int use_handles; /* use stdio handles */
82 int create_flags; /* creation flags */
83 handle_t hstdin; /* handle for stdin */
84 handle_t hstdout; /* handle for stdout */
85 handle_t hstderr; /* handle for stderr */
86 struct file *exe_file; /* file handle for main exe */
87 struct thread *owner; /* owner thread (the one that created the new process) */
88 struct process *process; /* created process */
89 struct thread *thread; /* created thread */
90 size_t data_size; /* size of startup data */
91 startup_info_t *data; /* data for startup info */
94 static void startup_info_dump( struct object *obj, int verbose );
95 static int startup_info_signaled( struct object *obj, struct thread *thread );
96 static void startup_info_destroy( struct object *obj );
98 static const struct object_ops startup_info_ops =
100 sizeof(struct startup_info), /* size */
101 startup_info_dump, /* dump */
102 add_queue, /* add_queue */
103 remove_queue, /* remove_queue */
104 startup_info_signaled, /* signaled */
105 no_satisfied, /* satisfied */
106 NULL, /* get_poll_events */
107 NULL, /* poll_event */
108 no_get_fd, /* get_fd */
109 no_flush, /* flush */
110 no_get_file_info, /* get_file_info */
111 NULL, /* queue_async */
112 startup_info_destroy /* destroy */
116 /* set the console and stdio handles for a newly created process */
117 static int set_process_console( struct process *process, struct thread *parent_thread,
118 struct startup_info *info, struct init_process_reply *reply )
120 if (process->create_flags & CREATE_NEW_CONSOLE)
122 /* let the process init do the allocation */
123 return 1;
125 else if (parent_thread && !(process->create_flags & DETACHED_PROCESS))
127 /* FIXME: some better error checking should be done...
128 * like if hConOut and hConIn are console handles, then they should be on the same
129 * physical console
131 inherit_console( parent_thread, process,
132 (info->inherit_all || info->use_handles) ? info->hstdin : 0 );
134 if (parent_thread)
136 if (!info->inherit_all && !info->use_handles)
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 );
146 else
148 reply->hstdin = info->hstdin;
149 reply->hstdout = info->hstdout;
150 reply->hstderr = info->hstderr;
153 else
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 );
164 else
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();
172 return 1;
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;
180 int request_pipe[2];
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;
206 process->exe.namelen = 0;
207 process->exe.filename = NULL;
209 gettimeofday( &process->start_time, NULL );
210 if ((process->next = first_process) != NULL) process->next->prev = process;
211 first_process = process;
213 /* create the init done event */
214 if (!(process->init_event = create_event( NULL, 0, 1, 0 ))) goto error;
216 /* create the main thread */
217 if (pipe( request_pipe ) == -1)
219 file_set_error();
220 goto error;
222 send_client_fd( process, request_pipe[1], 0 );
223 close( request_pipe[1] );
224 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
226 set_select_events( &process->obj, POLLIN ); /* start listening to events */
227 release_object( process );
228 return thread;
230 error:
231 if (thread) release_object( thread );
232 release_object( process );
233 return NULL;
236 /* initialize the current process and fill in the request */
237 static struct startup_info *init_process( int ppid, struct init_process_reply *reply )
239 struct process *process = current->process;
240 struct thread *parent_thread = get_thread_from_pid( ppid );
241 struct process *parent = NULL;
242 struct startup_info *info = NULL;
244 if (parent_thread)
246 parent = parent_thread->process;
247 info = parent_thread->info;
248 if (!info)
250 fatal_protocol_error( current, "init_process: parent but no info\n" );
251 return NULL;
253 if (info->thread)
255 fatal_protocol_error( current, "init_process: called twice?\n" );
256 return NULL;
258 process->parent = (struct process *)grab_object( parent );
261 /* set the process flags */
262 process->create_flags = info ? info->create_flags : 0;
264 /* create the handle table */
265 if (parent && info->inherit_all)
266 process->handles = copy_handle_table( process, parent );
267 else
268 process->handles = alloc_handle_table( process, 0 );
269 if (!process->handles) return NULL;
271 /* retrieve the main exe file */
272 reply->exe_file = 0;
273 if (parent && info->exe_file)
275 process->exe.file = (struct file *)grab_object( info->exe_file );
276 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
277 return NULL;
280 /* set the process console */
281 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
283 if (parent)
285 /* attach to the debugger if requested */
286 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
287 set_process_debugger( process, parent_thread );
288 else if (parent && parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
289 set_process_debugger( process, parent->debugger );
292 /* thread will be actually suspended in init_done */
293 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
295 if (info)
297 reply->info_size = info->data_size;
298 info->process = (struct process *)grab_object( process );
299 info->thread = (struct thread *)grab_object( current );
300 wake_up( &info->obj, 0 );
302 reply->create_flags = process->create_flags;
303 reply->server_start = server_start_ticks;
304 return info ? (struct startup_info *)grab_object( info ) : NULL;
307 /* destroy a process when its refcount is 0 */
308 static void process_destroy( struct object *obj )
310 struct process *process = (struct process *)obj;
311 assert( obj->ops == &process_ops );
313 /* we can't have a thread remaining */
314 assert( !process->thread_list );
315 if (process->console) release_object( process->console );
316 if (process->parent) release_object( process->parent );
317 if (process->next) process->next->prev = process->prev;
318 if (process->prev) process->prev->next = process->next;
319 else first_process = process->next;
320 if (process->init_event) release_object( process->init_event );
321 if (process->idle_event) release_object( process->idle_event );
322 if (process->queue) release_object( process->queue );
323 if (process->atom_table) release_object( process->atom_table );
324 if (process->exe.file) release_object( process->exe.file );
325 if (process->exe.filename) free( process->exe.filename );
328 /* dump a process on stdout for debugging purposes */
329 static void process_dump( struct object *obj, int verbose )
331 struct process *process = (struct process *)obj;
332 assert( obj->ops == &process_ops );
334 fprintf( stderr, "Process next=%p prev=%p handles=%p\n",
335 process->next, process->prev, process->handles );
338 static int process_signaled( struct object *obj, struct thread *thread )
340 struct process *process = (struct process *)obj;
341 return !process->running_threads;
345 static void process_poll_event( struct object *obj, int event )
347 struct process *process = (struct process *)obj;
348 assert( obj->ops == &process_ops );
350 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
351 else if (event & POLLIN) receive_fd( process );
354 static void startup_info_destroy( struct object *obj )
356 struct startup_info *info = (struct startup_info *)obj;
357 assert( obj->ops == &startup_info_ops );
358 if (info->data) free( info->data );
359 if (info->exe_file) release_object( info->exe_file );
360 if (info->process) release_object( info->process );
361 if (info->thread) release_object( info->thread );
362 if (info->owner)
364 info->owner->info = NULL;
365 release_object( info->owner );
369 static void startup_info_dump( struct object *obj, int verbose )
371 struct startup_info *info = (struct startup_info *)obj;
372 assert( obj->ops == &startup_info_ops );
374 fprintf( stderr, "Startup info flags=%x in=%d out=%d err=%d\n",
375 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
378 static int startup_info_signaled( struct object *obj, struct thread *thread )
380 struct startup_info *info = (struct startup_info *)obj;
381 return (info->thread != NULL);
385 /* get a process from an id (and increment the refcount) */
386 struct process *get_process_from_id( void *id )
388 struct process *p = first_process;
389 while (p && (p != id)) p = p->next;
390 if (p) grab_object( p );
391 else set_error( STATUS_INVALID_PARAMETER );
392 return p;
395 /* get a process from a handle (and increment the refcount) */
396 struct process *get_process_from_handle( handle_t handle, unsigned int access )
398 return (struct process *)get_handle_obj( current->process, handle,
399 access, &process_ops );
402 /* add a dll to a process list */
403 static struct process_dll *process_load_dll( struct process *process, struct file *file,
404 void *base, const char *filename, size_t name_len )
406 struct process_dll *dll;
408 /* make sure we don't already have one with the same base address */
409 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
411 set_error( STATUS_INVALID_PARAMETER );
412 return NULL;
415 if ((dll = mem_alloc( sizeof(*dll) )))
417 dll->prev = &process->exe;
418 dll->file = NULL;
419 dll->base = base;
420 dll->filename = NULL;
421 dll->namelen = name_len;
422 if (name_len && !(dll->filename = memdup( filename, name_len )))
424 free( dll );
425 return NULL;
427 if (file) dll->file = (struct file *)grab_object( file );
428 if ((dll->next = process->exe.next)) dll->next->prev = dll;
429 process->exe.next = dll;
431 return dll;
434 /* remove a dll from a process list */
435 static void process_unload_dll( struct process *process, void *base )
437 struct process_dll *dll;
439 for (dll = process->exe.next; dll; dll = dll->next)
441 if (dll->base == base)
443 if (dll->file) release_object( dll->file );
444 if (dll->next) dll->next->prev = dll->prev;
445 if (dll->prev) dll->prev->next = dll->next;
446 if (dll->filename) free( dll->filename );
447 free( dll );
448 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
449 return;
452 set_error( STATUS_INVALID_PARAMETER );
455 /* kill all processes being attached to a console renderer */
456 void kill_console_processes( struct thread *renderer, int exit_code )
458 for (;;) /* restart from the beginning of the list every time */
460 struct process *process = first_process;
462 /* find the first process being attached to 'renderer' and still running */
463 while (process &&
464 (process == renderer->process || !process->console ||
465 process->console->renderer != renderer || !process->running_threads))
467 process = process->next;
469 if (!process) break;
470 kill_process( process, NULL, exit_code );
474 /* a process has been killed (i.e. its last thread died) */
475 static void process_killed( struct process *process )
477 assert( !process->thread_list );
478 gettimeofday( &process->end_time, NULL );
479 if (process->handles) release_object( process->handles );
480 process->handles = NULL;
482 /* close the console attached to this process, if any */
483 free_console( process );
485 while (process->exe.next)
487 struct process_dll *dll = process->exe.next;
488 process->exe.next = dll->next;
489 if (dll->file) release_object( dll->file );
490 if (dll->filename) free( dll->filename );
491 free( dll );
493 if (process->exe.file) release_object( process->exe.file );
494 process->exe.file = NULL;
495 wake_up( &process->obj, 0 );
496 if (!--running_processes)
498 /* last process died, close global handles */
499 close_global_handles();
500 /* this will cause the select loop to terminate */
501 close_master_socket();
505 /* add a thread to a process running threads list */
506 void add_process_thread( struct process *process, struct thread *thread )
508 thread->proc_next = process->thread_list;
509 thread->proc_prev = NULL;
510 if (thread->proc_next) thread->proc_next->proc_prev = thread;
511 process->thread_list = thread;
512 if (!process->running_threads++) running_processes++;
513 grab_object( thread );
516 /* remove a thread from a process running threads list */
517 void remove_process_thread( struct process *process, struct thread *thread )
519 assert( process->running_threads > 0 );
520 assert( process->thread_list );
522 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
523 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
524 else process->thread_list = thread->proc_next;
526 if (!--process->running_threads)
528 /* we have removed the last running thread, exit the process */
529 process->exit_code = thread->exit_code;
530 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
531 process_killed( process );
533 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
534 release_object( thread );
537 /* suspend all the threads of a process */
538 void suspend_process( struct process *process )
540 if (!process->suspend++)
542 struct thread *thread = process->thread_list;
543 while (thread)
545 struct thread *next = thread->proc_next;
546 if (!thread->suspend) stop_thread( thread );
547 thread = next;
552 /* resume all the threads of a process */
553 void resume_process( struct process *process )
555 assert (process->suspend > 0);
556 if (!--process->suspend)
558 struct thread *thread = process->thread_list;
559 while (thread)
561 struct thread *next = thread->proc_next;
562 if (!thread->suspend) continue_thread( thread );
563 thread = next;
568 /* kill a process on the spot */
569 void kill_process( struct process *process, struct thread *skip, int exit_code )
571 struct thread *thread = process->thread_list;
573 while (thread)
575 struct thread *next = thread->proc_next;
576 thread->exit_code = exit_code;
577 if (thread != skip) kill_thread( thread, 1 );
578 thread = next;
582 /* kill all processes being debugged by a given thread */
583 void kill_debugged_processes( struct thread *debugger, int exit_code )
585 for (;;) /* restart from the beginning of the list every time */
587 struct process *process = first_process;
588 /* find the first process being debugged by 'debugger' and still running */
589 while (process && (process->debugger != debugger || !process->running_threads))
590 process = process->next;
591 if (!process) return;
592 process->debugger = NULL;
593 kill_process( process, NULL, exit_code );
598 /* detach a debugger from all its debuggees */
599 void detach_debugged_processes( struct thread *debugger )
601 struct process *process;
602 for (process = first_process; process; process = process->next)
604 if (process->debugger == debugger && process->running_threads)
606 debugger_detach( process, debugger );
612 /* get all information about a process */
613 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
615 reply->pid = get_process_id( process );
616 reply->debugged = (process->debugger != 0);
617 reply->exit_code = process->exit_code;
618 reply->priority = process->priority;
619 reply->process_affinity = process->affinity;
620 reply->system_affinity = 1;
623 /* set all information about a process */
624 static void set_process_info( struct process *process,
625 const struct set_process_info_request *req )
627 if (req->mask & SET_PROCESS_INFO_PRIORITY)
628 process->priority = req->priority;
629 if (req->mask & SET_PROCESS_INFO_AFFINITY)
631 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
632 else process->affinity = req->affinity;
636 /* read data from a process memory space */
637 /* len is the total size (in ints) */
638 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
640 struct thread *thread = process->thread_list;
642 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
644 if (!thread) /* process is dead */
646 set_error( STATUS_ACCESS_DENIED );
647 return 0;
649 if (suspend_for_ptrace( thread ))
651 while (len > 0)
653 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
654 len--;
656 resume_thread( thread );
658 return !len;
661 /* make sure we can write to the whole address range */
662 /* len is the total size (in ints) */
663 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
665 int page = get_page_size() / sizeof(int);
667 for (;;)
669 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
670 if (len <= page) break;
671 addr += page;
672 len -= page;
674 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
677 /* write data to a process memory space */
678 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
679 /* we check the total size for write permissions */
680 static void write_process_memory( struct process *process, int *addr, size_t len,
681 unsigned int first_mask, unsigned int last_mask, const int *src )
683 struct thread *thread = process->thread_list;
685 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
687 if (!thread) /* process is dead */
689 set_error( STATUS_ACCESS_DENIED );
690 return;
692 if (suspend_for_ptrace( thread ))
694 if (!check_process_write_access( thread, addr, len ))
696 set_error( STATUS_ACCESS_DENIED );
697 return;
699 /* first word is special */
700 if (len > 1)
702 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
703 len--;
705 else last_mask &= first_mask;
707 while (len > 1)
709 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
710 len--;
713 /* last word is special too */
714 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
716 done:
717 resume_thread( thread );
721 /* take a snapshot of currently running processes */
722 struct process_snapshot *process_snap( int *count )
724 struct process_snapshot *snapshot, *ptr;
725 struct process *process;
726 if (!running_processes) return NULL;
727 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
728 return NULL;
729 ptr = snapshot;
730 for (process = first_process; process; process = process->next)
732 if (!process->running_threads) continue;
733 ptr->process = process;
734 ptr->threads = process->running_threads;
735 ptr->count = process->obj.refcount;
736 ptr->priority = process->priority;
737 grab_object( process );
738 ptr++;
740 *count = running_processes;
741 return snapshot;
744 /* take a snapshot of the modules of a process */
745 struct module_snapshot *module_snap( struct process *process, int *count )
747 struct module_snapshot *snapshot, *ptr;
748 struct process_dll *dll;
749 int total = 0;
751 for (dll = &process->exe; dll; dll = dll->next) total++;
752 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
754 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
756 ptr->base = dll->base;
757 ptr->size = dll->size;
758 ptr->namelen = dll->namelen;
759 ptr->filename = memdup( dll->filename, dll->namelen );
761 *count = total;
762 return snapshot;
766 /* create a new process */
767 DECL_HANDLER(new_process)
769 struct startup_info *info;
771 if (current->info)
773 fatal_protocol_error( current, "new_process: another process is being created\n" );
774 return;
777 /* build the startup info for a new process */
778 if (!(info = alloc_object( &startup_info_ops, -1 ))) return;
779 info->inherit_all = req->inherit_all;
780 info->use_handles = req->use_handles;
781 info->create_flags = req->create_flags;
782 info->hstdin = req->hstdin;
783 info->hstdout = req->hstdout;
784 info->hstderr = req->hstderr;
785 info->exe_file = NULL;
786 info->owner = (struct thread *)grab_object( current );
787 info->process = NULL;
788 info->thread = NULL;
789 info->data_size = get_req_data_size();
790 info->data = NULL;
792 if (req->exe_file &&
793 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
794 goto done;
796 if (!(info->data = mem_alloc( info->data_size ))) goto done;
797 memcpy( info->data, get_req_data(), info->data_size );
798 current->info = info;
799 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
801 done:
802 release_object( info );
805 /* Retrieve information about a newly started process */
806 DECL_HANDLER(get_new_process_info)
808 struct startup_info *info;
810 reply->event = 0;
812 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
813 0, &startup_info_ops )))
815 reply->pid = get_process_id( info->process );
816 reply->tid = get_thread_id( info->thread );
817 reply->phandle = alloc_handle( current->process, info->process,
818 PROCESS_ALL_ACCESS, req->pinherit );
819 reply->thandle = alloc_handle( current->process, info->thread,
820 THREAD_ALL_ACCESS, req->tinherit );
821 if (info->process->init_event)
822 reply->event = alloc_handle( current->process, info->process->init_event,
823 EVENT_ALL_ACCESS, 0 );
824 release_object( info );
826 else
828 reply->pid = 0;
829 reply->tid = 0;
830 reply->phandle = 0;
831 reply->thandle = 0;
835 /* Retrieve the new process startup info */
836 DECL_HANDLER(get_startup_info)
838 struct startup_info *info;
840 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
841 0, &startup_info_ops )))
843 size_t size = info->data_size;
844 if (size > get_reply_max_size()) size = get_reply_max_size();
845 if (req->close)
847 set_reply_data_ptr( info->data, size );
848 info->data = NULL;
849 close_handle( current->process, req->info, NULL );
851 else set_reply_data( info->data, size );
852 release_object( info );
857 /* initialize a new process */
858 DECL_HANDLER(init_process)
860 struct startup_info *info;
862 reply->info = 0;
863 reply->info_size = 0;
864 if (!current->unix_pid)
866 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
867 return;
869 current->process->ldt_copy = req->ldt_copy;
870 if ((info = init_process( req->ppid, reply )))
872 reply->info = alloc_handle( current->process, info, 0, FALSE );
873 release_object( info );
877 /* signal the end of the process initialization */
878 DECL_HANDLER(init_process_done)
880 struct file *file = NULL;
881 struct process *process = current->process;
883 if (!process->init_event)
885 fatal_protocol_error( current, "init_process_done: no event\n" );
886 return;
888 process->exe.base = req->module;
889 process->exe.size = req->module_size;
890 process->exe.name = req->name;
892 if (req->exe_file) file = get_file_obj( current->process, req->exe_file, GENERIC_READ );
893 if (process->exe.file) release_object( process->exe.file );
894 process->exe.file = file;
896 if ((process->exe.namelen = get_req_data_size()))
897 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
899 generate_startup_debug_events( current->process, req->entry );
900 set_event( process->init_event );
901 release_object( process->init_event );
902 process->init_event = NULL;
903 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
904 if (current->suspend + current->process->suspend > 0) stop_thread( current );
905 reply->debugged = (current->process->debugger != 0);
908 /* open a handle to a process */
909 DECL_HANDLER(open_process)
911 struct process *process = get_process_from_id( req->pid );
912 reply->handle = 0;
913 if (process)
915 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
916 release_object( process );
920 /* terminate a process */
921 DECL_HANDLER(terminate_process)
923 struct process *process;
925 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
927 reply->self = (current->process == process);
928 kill_process( process, current, req->exit_code );
929 release_object( process );
933 /* fetch information about a process */
934 DECL_HANDLER(get_process_info)
936 struct process *process;
938 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
940 get_process_info( process, reply );
941 release_object( process );
945 /* set information about a process */
946 DECL_HANDLER(set_process_info)
948 struct process *process;
950 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
952 set_process_info( process, req );
953 release_object( process );
957 /* read data from a process address space */
958 DECL_HANDLER(read_process_memory)
960 struct process *process;
961 size_t len = get_reply_max_size();
963 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
965 if (len)
967 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
968 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
969 const int *start = (int *)((char *)req->addr - start_offset);
970 int *buffer = mem_alloc( nb_ints * sizeof(int) );
971 if (buffer)
973 if (read_process_memory( process, start, nb_ints, buffer ))
975 /* move start of requested data to start of buffer */
976 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
977 set_reply_data_ptr( buffer, len );
979 else len = 0;
982 release_object( process );
985 /* write data to a process address space */
986 DECL_HANDLER(write_process_memory)
988 struct process *process;
990 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
992 size_t len = get_req_data_size();
993 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
994 set_error( STATUS_INVALID_PARAMETER );
995 else
997 if (len) write_process_memory( process, req->addr, len / sizeof(int),
998 req->first_mask, req->last_mask, get_req_data() );
1000 release_object( process );
1004 /* notify the server that a dll has been loaded */
1005 DECL_HANDLER(load_dll)
1007 struct process_dll *dll;
1008 struct file *file = NULL;
1010 if (req->handle &&
1011 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1013 if ((dll = process_load_dll( current->process, file, req->base,
1014 get_req_data(), get_req_data_size() )))
1016 dll->size = req->size;
1017 dll->dbg_offset = req->dbg_offset;
1018 dll->dbg_size = req->dbg_size;
1019 dll->name = req->name;
1020 /* only generate event if initialization is done */
1021 if (!current->process->init_event)
1022 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1024 if (file) release_object( file );
1027 /* notify the server that a dll is being unloaded */
1028 DECL_HANDLER(unload_dll)
1030 process_unload_dll( current->process, req->base );
1033 /* wait for a process to start waiting on input */
1034 /* FIXME: only returns event for now, wait is done in the client */
1035 DECL_HANDLER(wait_input_idle)
1037 struct process *process;
1039 reply->event = 0;
1040 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1042 if (process->idle_event && process != current->process && process->queue != current->queue)
1043 reply->event = alloc_handle( current->process, process->idle_event,
1044 EVENT_ALL_ACCESS, 0 );
1045 release_object( process );