RDW_FRAME should be RDW_NOFRAME when validating (spotted by Duane
[wine/multimedia.git] / server / process.c
blob2474a98a746fe44fdaedde09e60db320d12246ef
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 obj_handle_t hstdin; /* handle for stdin */
84 obj_handle_t hstdout; /* handle for stdout */
85 obj_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 state of the process startup info */
117 static void set_process_startup_state( struct process *process, enum startup_state state )
119 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
120 if (process->startup_info)
122 wake_up( &process->startup_info->obj, 0 );
123 release_object( process->startup_info );
124 process->startup_info = NULL;
128 /* set the console and stdio handles for a newly created process */
129 static int set_process_console( struct process *process, struct thread *parent_thread,
130 struct startup_info *info, struct init_process_reply *reply )
132 if (process->create_flags & CREATE_NEW_CONSOLE)
134 /* let the process init do the allocation */
135 return 1;
137 else if (info && !(process->create_flags & DETACHED_PROCESS))
139 /* FIXME: some better error checking should be done...
140 * like if hConOut and hConIn are console handles, then they should be on the same
141 * physical console
143 inherit_console( parent_thread, process,
144 (info->inherit_all || info->use_handles) ? info->hstdin : 0 );
146 if (info)
148 if (!info->inherit_all && !info->use_handles)
150 /* duplicate the handle from the parent into this process */
151 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
152 0, TRUE, DUPLICATE_SAME_ACCESS );
153 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
154 0, TRUE, DUPLICATE_SAME_ACCESS );
155 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
156 0, TRUE, DUPLICATE_SAME_ACCESS );
158 else
160 reply->hstdin = info->hstdin;
161 reply->hstdout = info->hstdout;
162 reply->hstderr = info->hstderr;
165 else
167 if (process->console)
169 reply->hstdin = alloc_handle( process, process->console,
170 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
171 reply->hstdout = alloc_handle( process, process->console->active,
172 GENERIC_READ | GENERIC_WRITE, 1 );
173 reply->hstderr = alloc_handle( process, process->console->active,
174 GENERIC_READ | GENERIC_WRITE, 1 );
176 else
178 /* no parent, let the caller decide what to do */
179 reply->hstdin = reply->hstdout = reply->hstderr = 0;
182 /* some handles above may have been invalid; this is not an error */
183 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
184 return 1;
187 /* create a new process and its main thread */
188 struct thread *create_process( int fd )
190 struct process *process;
191 struct thread *thread = NULL;
192 int request_pipe[2];
194 if (!(process = alloc_object( &process_ops, fd ))) goto error;
195 process->next = NULL;
196 process->prev = NULL;
197 process->parent = NULL;
198 process->thread_list = NULL;
199 process->debugger = NULL;
200 process->handles = NULL;
201 process->exit_code = STILL_ACTIVE;
202 process->running_threads = 0;
203 process->priority = NORMAL_PRIORITY_CLASS;
204 process->affinity = 1;
205 process->suspend = 0;
206 process->create_flags = 0;
207 process->console = NULL;
208 process->startup_state = STARTUP_IN_PROGRESS;
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.dbg_offset = 0;
218 process->exe.dbg_size = 0;
219 process->exe.namelen = 0;
220 process->exe.filename = NULL;
221 process->group_id = 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)
230 file_set_error();
231 goto error;
233 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
235 close( request_pipe[0] );
236 close( request_pipe[1] );
237 goto error;
239 close( request_pipe[1] );
240 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
242 set_select_events( &process->obj, POLLIN ); /* start listening to events */
243 release_object( process );
244 return thread;
246 error:
247 if (process) release_object( process );
248 /* if we failed to start our first process, close everything down */
249 if (!running_processes) close_master_socket();
250 return NULL;
253 /* initialize the current process and fill in the request */
254 static struct startup_info *init_process( int ppid, struct init_process_reply *reply )
256 struct process *process = current->process;
257 struct thread *parent_thread = get_thread_from_pid( ppid );
258 struct process *parent = NULL;
259 struct startup_info *info = NULL;
261 if (parent_thread)
263 parent = parent_thread->process;
264 info = parent_thread->info;
265 if (info && info->thread)
267 fatal_protocol_error( current, "init_process: called twice?\n" );
268 return NULL;
270 process->parent = (struct process *)grab_object( parent );
273 /* set the process flags */
274 process->create_flags = info ? info->create_flags : 0;
276 /* create the handle table */
277 if (info && info->inherit_all)
278 process->handles = copy_handle_table( process, parent );
279 else
280 process->handles = alloc_handle_table( process, 0 );
281 if (!process->handles) return NULL;
283 /* retrieve the main exe file */
284 reply->exe_file = 0;
285 if (info && info->exe_file)
287 process->exe.file = (struct file *)grab_object( info->exe_file );
288 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
289 return NULL;
292 /* set the process console */
293 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
295 process->group_id = process;
296 if (parent)
298 /* attach to the debugger if requested */
299 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
300 set_process_debugger( process, parent_thread );
301 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
302 set_process_debugger( process, parent->debugger );
303 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
304 process->group_id = parent->group_id;
307 /* thread will be actually suspended in init_done */
308 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
310 if (info)
312 reply->info_size = info->data_size;
313 info->process = (struct process *)grab_object( process );
314 info->thread = (struct thread *)grab_object( current );
316 reply->create_flags = process->create_flags;
317 reply->server_start = server_start_ticks;
318 return info ? (struct startup_info *)grab_object( info ) : NULL;
321 /* destroy a process when its refcount is 0 */
322 static void process_destroy( struct object *obj )
324 struct process *process = (struct process *)obj;
325 assert( obj->ops == &process_ops );
327 /* we can't have a thread remaining */
328 assert( !process->thread_list );
330 set_process_startup_state( process, STARTUP_ABORTED );
331 if (process->console) release_object( process->console );
332 if (process->parent) release_object( process->parent );
333 if (process->next) process->next->prev = process->prev;
334 if (process->prev) process->prev->next = process->next;
335 else first_process = process->next;
336 if (process->idle_event) release_object( process->idle_event );
337 if (process->queue) release_object( process->queue );
338 if (process->atom_table) release_object( process->atom_table );
339 if (process->exe.file) release_object( process->exe.file );
340 if (process->exe.filename) free( process->exe.filename );
343 /* dump a process on stdout for debugging purposes */
344 static void process_dump( struct object *obj, int verbose )
346 struct process *process = (struct process *)obj;
347 assert( obj->ops == &process_ops );
349 fprintf( stderr, "Process next=%p prev=%p handles=%p\n",
350 process->next, process->prev, process->handles );
353 static int process_signaled( struct object *obj, struct thread *thread )
355 struct process *process = (struct process *)obj;
356 return !process->running_threads;
360 static void process_poll_event( struct object *obj, int event )
362 struct process *process = (struct process *)obj;
363 assert( obj->ops == &process_ops );
365 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
366 else if (event & POLLIN) receive_fd( process );
369 static void startup_info_destroy( struct object *obj )
371 struct startup_info *info = (struct startup_info *)obj;
372 assert( obj->ops == &startup_info_ops );
373 if (info->data) free( info->data );
374 if (info->exe_file) release_object( info->exe_file );
375 if (info->process) release_object( info->process );
376 if (info->thread) release_object( info->thread );
377 if (info->owner)
379 info->owner->info = NULL;
380 release_object( info->owner );
384 static void startup_info_dump( struct object *obj, int verbose )
386 struct startup_info *info = (struct startup_info *)obj;
387 assert( obj->ops == &startup_info_ops );
389 fprintf( stderr, "Startup info flags=%x in=%d out=%d err=%d\n",
390 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
393 static int startup_info_signaled( struct object *obj, struct thread *thread )
395 struct startup_info *info = (struct startup_info *)obj;
396 return info->process && is_process_init_done(info->process);
399 /* get a process from an id (and increment the refcount) */
400 struct process *get_process_from_id( void *id )
402 struct process *p = first_process;
403 while (p && (p != id)) p = p->next;
404 if (p) grab_object( p );
405 else set_error( STATUS_INVALID_PARAMETER );
406 return p;
409 /* get a process from a handle (and increment the refcount) */
410 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
412 return (struct process *)get_handle_obj( current->process, handle,
413 access, &process_ops );
416 /* add a dll to a process list */
417 static struct process_dll *process_load_dll( struct process *process, struct file *file,
418 void *base, const char *filename, size_t name_len )
420 struct process_dll *dll;
422 /* make sure we don't already have one with the same base address */
423 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
425 set_error( STATUS_INVALID_PARAMETER );
426 return NULL;
429 if ((dll = mem_alloc( sizeof(*dll) )))
431 dll->prev = &process->exe;
432 dll->file = NULL;
433 dll->base = base;
434 dll->filename = NULL;
435 dll->namelen = name_len;
436 if (name_len && !(dll->filename = memdup( filename, name_len )))
438 free( dll );
439 return NULL;
441 if (file) dll->file = (struct file *)grab_object( file );
442 if ((dll->next = process->exe.next)) dll->next->prev = dll;
443 process->exe.next = dll;
445 return dll;
448 /* remove a dll from a process list */
449 static void process_unload_dll( struct process *process, void *base )
451 struct process_dll *dll;
453 for (dll = process->exe.next; dll; dll = dll->next)
455 if (dll->base == base)
457 if (dll->file) release_object( dll->file );
458 if (dll->next) dll->next->prev = dll->prev;
459 if (dll->prev) dll->prev->next = dll->next;
460 if (dll->filename) free( dll->filename );
461 free( dll );
462 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
463 return;
466 set_error( STATUS_INVALID_PARAMETER );
469 /* kill all processes being attached to a console renderer */
470 void kill_console_processes( struct thread *renderer, int exit_code )
472 for (;;) /* restart from the beginning of the list every time */
474 struct process *process = first_process;
476 /* find the first process being attached to 'renderer' and still running */
477 while (process &&
478 (process == renderer->process || !process->console ||
479 process->console->renderer != renderer || !process->running_threads))
481 process = process->next;
483 if (!process) break;
484 kill_process( process, NULL, exit_code );
488 /* a process has been killed (i.e. its last thread died) */
489 static void process_killed( struct process *process )
491 assert( !process->thread_list );
492 gettimeofday( &process->end_time, NULL );
493 if (process->handles) release_object( process->handles );
494 process->handles = NULL;
496 /* close the console attached to this process, if any */
497 free_console( process );
499 while (process->exe.next)
501 struct process_dll *dll = process->exe.next;
502 process->exe.next = dll->next;
503 if (dll->file) release_object( dll->file );
504 if (dll->filename) free( dll->filename );
505 free( dll );
507 set_process_startup_state( process, STARTUP_ABORTED );
508 if (process->exe.file) release_object( process->exe.file );
509 process->exe.file = NULL;
510 wake_up( &process->obj, 0 );
511 if (!--running_processes) close_master_socket();
514 /* add a thread to a process running threads list */
515 void add_process_thread( struct process *process, struct thread *thread )
517 thread->proc_next = process->thread_list;
518 thread->proc_prev = NULL;
519 if (thread->proc_next) thread->proc_next->proc_prev = thread;
520 process->thread_list = thread;
521 if (!process->running_threads++) running_processes++;
522 grab_object( thread );
525 /* remove a thread from a process running threads list */
526 void remove_process_thread( struct process *process, struct thread *thread )
528 assert( process->running_threads > 0 );
529 assert( process->thread_list );
531 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
532 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
533 else process->thread_list = thread->proc_next;
535 if (!--process->running_threads)
537 /* we have removed the last running thread, exit the process */
538 process->exit_code = thread->exit_code;
539 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
540 process_killed( process );
542 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
543 release_object( thread );
546 /* suspend all the threads of a process */
547 void suspend_process( struct process *process )
549 if (!process->suspend++)
551 struct thread *thread = process->thread_list;
552 while (thread)
554 struct thread *next = thread->proc_next;
555 if (!thread->suspend) stop_thread( thread );
556 thread = next;
561 /* resume all the threads of a process */
562 void resume_process( struct process *process )
564 assert (process->suspend > 0);
565 if (!--process->suspend)
567 struct thread *thread = process->thread_list;
568 while (thread)
570 struct thread *next = thread->proc_next;
571 if (!thread->suspend) continue_thread( thread );
572 thread = next;
577 /* kill a process on the spot */
578 void kill_process( struct process *process, struct thread *skip, int exit_code )
580 struct thread *thread = process->thread_list;
582 while (thread)
584 struct thread *next = thread->proc_next;
585 thread->exit_code = exit_code;
586 if (thread != skip) kill_thread( thread, 1 );
587 thread = next;
591 /* kill all processes being debugged by a given thread */
592 void kill_debugged_processes( struct thread *debugger, int exit_code )
594 for (;;) /* restart from the beginning of the list every time */
596 struct process *process = first_process;
597 /* find the first process being debugged by 'debugger' and still running */
598 while (process && (process->debugger != debugger || !process->running_threads))
599 process = process->next;
600 if (!process) return;
601 process->debugger = NULL;
602 kill_process( process, NULL, exit_code );
607 /* detach a debugger from all its debuggees */
608 void detach_debugged_processes( struct thread *debugger )
610 struct process *process;
611 for (process = first_process; process; process = process->next)
613 if (process->debugger == debugger && process->running_threads)
615 debugger_detach( process, debugger );
621 void enum_processes( int (*cb)(struct process*, void*), void *user )
623 struct process *process;
624 for (process = first_process; process; process = process->next)
626 if ((cb)(process, user)) break;
631 /* get all information about a process */
632 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
634 reply->pid = get_process_id( process );
635 reply->debugged = (process->debugger != 0);
636 reply->exit_code = process->exit_code;
637 reply->priority = process->priority;
638 reply->process_affinity = process->affinity;
639 reply->system_affinity = 1;
642 /* set all information about a process */
643 static void set_process_info( struct process *process,
644 const struct set_process_info_request *req )
646 if (req->mask & SET_PROCESS_INFO_PRIORITY)
647 process->priority = req->priority;
648 if (req->mask & SET_PROCESS_INFO_AFFINITY)
650 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
651 else process->affinity = req->affinity;
655 /* read data from a process memory space */
656 /* len is the total size (in ints) */
657 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
659 struct thread *thread = process->thread_list;
661 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
663 if (!thread) /* process is dead */
665 set_error( STATUS_ACCESS_DENIED );
666 return 0;
668 if (suspend_for_ptrace( thread ))
670 while (len > 0)
672 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
673 len--;
675 resume_thread( thread );
677 return !len;
680 /* make sure we can write to the whole address range */
681 /* len is the total size (in ints) */
682 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
684 int page = get_page_size() / sizeof(int);
686 for (;;)
688 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
689 if (len <= page) break;
690 addr += page;
691 len -= page;
693 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
696 /* write data to a process memory space */
697 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
698 /* we check the total size for write permissions */
699 static void write_process_memory( struct process *process, int *addr, size_t len,
700 unsigned int first_mask, unsigned int last_mask, const int *src )
702 struct thread *thread = process->thread_list;
704 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
706 if (!thread) /* process is dead */
708 set_error( STATUS_ACCESS_DENIED );
709 return;
711 if (suspend_for_ptrace( thread ))
713 if (!check_process_write_access( thread, addr, len ))
715 set_error( STATUS_ACCESS_DENIED );
716 return;
718 /* first word is special */
719 if (len > 1)
721 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
722 len--;
724 else last_mask &= first_mask;
726 while (len > 1)
728 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
729 len--;
732 /* last word is special too */
733 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
735 done:
736 resume_thread( thread );
740 /* take a snapshot of currently running processes */
741 struct process_snapshot *process_snap( int *count )
743 struct process_snapshot *snapshot, *ptr;
744 struct process *process;
745 if (!running_processes) return NULL;
746 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
747 return NULL;
748 ptr = snapshot;
749 for (process = first_process; process; process = process->next)
751 if (!process->running_threads) continue;
752 ptr->process = process;
753 ptr->threads = process->running_threads;
754 ptr->count = process->obj.refcount;
755 ptr->priority = process->priority;
756 grab_object( process );
757 ptr++;
759 *count = running_processes;
760 return snapshot;
763 /* take a snapshot of the modules of a process */
764 struct module_snapshot *module_snap( struct process *process, int *count )
766 struct module_snapshot *snapshot, *ptr;
767 struct process_dll *dll;
768 int total = 0;
770 for (dll = &process->exe; dll; dll = dll->next) total++;
771 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
773 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
775 ptr->base = dll->base;
776 ptr->size = dll->size;
777 ptr->namelen = dll->namelen;
778 ptr->filename = memdup( dll->filename, dll->namelen );
780 *count = total;
781 return snapshot;
785 /* create a new process */
786 DECL_HANDLER(new_process)
788 struct startup_info *info;
790 if (current->info)
792 fatal_protocol_error( current, "new_process: another process is being created\n" );
793 return;
796 /* build the startup info for a new process */
797 if (!(info = alloc_object( &startup_info_ops, -1 ))) return;
798 info->inherit_all = req->inherit_all;
799 info->use_handles = req->use_handles;
800 info->create_flags = req->create_flags;
801 info->hstdin = req->hstdin;
802 info->hstdout = req->hstdout;
803 info->hstderr = req->hstderr;
804 info->exe_file = NULL;
805 info->owner = (struct thread *)grab_object( current );
806 info->process = NULL;
807 info->thread = NULL;
808 info->data_size = get_req_data_size();
809 info->data = NULL;
811 if (req->exe_file &&
812 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
813 goto done;
815 if (!(info->data = mem_alloc( info->data_size ))) goto done;
816 memcpy( info->data, get_req_data(), info->data_size );
817 current->info = info;
818 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
820 done:
821 release_object( info );
824 /* Retrieve information about a newly started process */
825 DECL_HANDLER(get_new_process_info)
827 struct startup_info *info;
829 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
830 0, &startup_info_ops )))
832 reply->pid = get_process_id( info->process );
833 reply->tid = get_thread_id( info->thread );
834 reply->phandle = alloc_handle( current->process, info->process,
835 PROCESS_ALL_ACCESS, req->pinherit );
836 reply->thandle = alloc_handle( current->process, info->thread,
837 THREAD_ALL_ACCESS, req->tinherit );
838 reply->success = is_process_init_done( info->process );
839 release_object( info );
841 else
843 reply->pid = 0;
844 reply->tid = 0;
845 reply->phandle = 0;
846 reply->thandle = 0;
847 reply->success = 0;
851 /* Retrieve the new process startup info */
852 DECL_HANDLER(get_startup_info)
854 struct startup_info *info;
856 if ((info = current->process->startup_info))
858 size_t size = info->data_size;
859 if (size > get_reply_max_size()) size = get_reply_max_size();
861 /* we return the data directly without making a copy so this can only be called once */
862 set_reply_data_ptr( info->data, size );
863 info->data = NULL;
864 info->data_size = 0;
869 /* initialize a new process */
870 DECL_HANDLER(init_process)
872 if (!current->unix_pid)
874 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
875 return;
877 if (current->process->startup_info)
879 fatal_protocol_error( current, "init_process: called twice\n" );
880 return;
882 reply->info_size = 0;
883 current->process->ldt_copy = req->ldt_copy;
884 current->process->startup_info = init_process( req->ppid, reply );
887 /* signal the end of the process initialization */
888 DECL_HANDLER(init_process_done)
890 struct file *file = NULL;
891 struct process *process = current->process;
893 if (is_process_init_done(process))
895 fatal_protocol_error( current, "init_process_done: called twice\n" );
896 return;
898 if (!req->module)
900 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
901 return;
903 process->exe.base = req->module;
904 process->exe.size = req->module_size;
905 process->exe.name = req->name;
907 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
908 if (process->exe.file) release_object( process->exe.file );
909 process->exe.file = file;
911 if ((process->exe.namelen = get_req_data_size()))
912 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
914 generate_startup_debug_events( process, req->entry );
915 set_process_startup_state( process, STARTUP_DONE );
917 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
918 if (current->suspend + process->suspend > 0) stop_thread( current );
919 reply->debugged = (process->debugger != 0);
922 /* open a handle to a process */
923 DECL_HANDLER(open_process)
925 struct process *process = get_process_from_id( req->pid );
926 reply->handle = 0;
927 if (process)
929 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
930 release_object( process );
934 /* terminate a process */
935 DECL_HANDLER(terminate_process)
937 struct process *process;
939 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
941 reply->self = (current->process == process);
942 kill_process( process, current, req->exit_code );
943 release_object( process );
947 /* fetch information about a process */
948 DECL_HANDLER(get_process_info)
950 struct process *process;
952 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
954 get_process_info( process, reply );
955 release_object( process );
959 /* set information about a process */
960 DECL_HANDLER(set_process_info)
962 struct process *process;
964 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
966 set_process_info( process, req );
967 release_object( process );
971 /* read data from a process address space */
972 DECL_HANDLER(read_process_memory)
974 struct process *process;
975 size_t len = get_reply_max_size();
977 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
979 if (len)
981 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
982 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
983 const int *start = (int *)((char *)req->addr - start_offset);
984 int *buffer = mem_alloc( nb_ints * sizeof(int) );
985 if (buffer)
987 if (read_process_memory( process, start, nb_ints, buffer ))
989 /* move start of requested data to start of buffer */
990 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
991 set_reply_data_ptr( buffer, len );
993 else len = 0;
996 release_object( process );
999 /* write data to a process address space */
1000 DECL_HANDLER(write_process_memory)
1002 struct process *process;
1004 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1006 size_t len = get_req_data_size();
1007 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1008 set_error( STATUS_INVALID_PARAMETER );
1009 else
1011 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1012 req->first_mask, req->last_mask, get_req_data() );
1014 release_object( process );
1018 /* notify the server that a dll has been loaded */
1019 DECL_HANDLER(load_dll)
1021 struct process_dll *dll;
1022 struct file *file = NULL;
1024 if (req->handle &&
1025 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1027 if ((dll = process_load_dll( current->process, file, req->base,
1028 get_req_data(), get_req_data_size() )))
1030 dll->size = req->size;
1031 dll->dbg_offset = req->dbg_offset;
1032 dll->dbg_size = req->dbg_size;
1033 dll->name = req->name;
1034 /* only generate event if initialization is done */
1035 if (is_process_init_done( current->process ))
1036 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1038 if (file) release_object( file );
1041 /* notify the server that a dll is being unloaded */
1042 DECL_HANDLER(unload_dll)
1044 process_unload_dll( current->process, req->base );
1047 /* wait for a process to start waiting on input */
1048 /* FIXME: only returns event for now, wait is done in the client */
1049 DECL_HANDLER(wait_input_idle)
1051 struct process *process;
1053 reply->event = 0;
1054 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1056 if (process->idle_event && process != current->process && process->queue != current->queue)
1057 reply->event = alloc_handle( current->process, process->idle_event,
1058 EVENT_ALL_ACCESS, 0 );
1059 release_object( process );