2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
52 /* process structure */
54 static struct list process_list = LIST_INIT(process_list);
55 static int running_processes;
57 /* process operations */
59 static void process_dump( struct object *obj, int verbose );
60 static int process_signaled( struct object *obj, struct thread *thread );
61 static void process_poll_event( struct fd *fd, int event );
62 static void process_destroy( struct object *obj );
64 static const struct object_ops process_ops =
66 sizeof(struct process), /* size */
67 process_dump, /* dump */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 process_signaled, /* signaled */
71 no_satisfied, /* satisfied */
72 no_signal, /* signal */
73 no_get_fd, /* get_fd */
74 no_close_handle, /* close_handle */
75 process_destroy /* destroy */
78 static const struct fd_ops process_fd_ops =
80 NULL, /* get_poll_events */
81 process_poll_event, /* poll_event */
83 no_get_file_info, /* get_file_info */
84 no_queue_async, /* queue_async */
85 no_cancel_async /* cancel async */
88 /* process startup info */
92 struct object obj; /* object header */
93 struct list entry; /* entry in list of startup infos */
94 int inherit_all; /* inherit all handles from parent */
95 unsigned int create_flags; /* creation flags */
96 int unix_pid; /* Unix pid of new process */
97 obj_handle_t hstdin; /* handle for stdin */
98 obj_handle_t hstdout; /* handle for stdout */
99 obj_handle_t hstderr; /* handle for stderr */
100 struct file *exe_file; /* file handle for main exe */
101 struct thread *owner; /* owner thread (the one that created the new process) */
102 struct process *process; /* created process */
103 struct thread *thread; /* created thread */
104 size_t data_size; /* size of startup data */
105 void *data; /* data for startup info */
108 static void startup_info_dump( struct object *obj, int verbose );
109 static int startup_info_signaled( struct object *obj, struct thread *thread );
110 static void startup_info_destroy( struct object *obj );
112 static const struct object_ops startup_info_ops =
114 sizeof(struct startup_info), /* size */
115 startup_info_dump, /* dump */
116 add_queue, /* add_queue */
117 remove_queue, /* remove_queue */
118 startup_info_signaled, /* signaled */
119 no_satisfied, /* satisfied */
120 no_signal, /* signal */
121 no_get_fd, /* get_fd */
122 no_close_handle, /* close_handle */
123 startup_info_destroy /* destroy */
127 static struct list startup_info_list = LIST_INIT(startup_info_list);
131 void *ptr; /* entry ptr */
132 unsigned int next; /* next free entry */
135 static struct ptid_entry *ptid_entries; /* array of ptid entries */
136 static unsigned int used_ptid_entries; /* number of entries in use */
137 static unsigned int alloc_ptid_entries; /* number of allocated entries */
138 static unsigned int next_free_ptid; /* next free entry */
139 static unsigned int last_free_ptid; /* last free entry */
141 #define PTID_OFFSET 8 /* offset for first ptid value */
143 /* allocate a new process or thread id */
144 unsigned int alloc_ptid( void *ptr )
146 struct ptid_entry *entry;
149 if (used_ptid_entries < alloc_ptid_entries)
151 id = used_ptid_entries + PTID_OFFSET;
152 entry = &ptid_entries[used_ptid_entries++];
154 else if (next_free_ptid)
157 entry = &ptid_entries[id - PTID_OFFSET];
158 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
160 else /* need to grow the array */
162 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
163 if (!count) count = 64;
164 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
166 set_error( STATUS_NO_MEMORY );
169 ptid_entries = entry;
170 alloc_ptid_entries = count;
171 id = used_ptid_entries + PTID_OFFSET;
172 entry = &ptid_entries[used_ptid_entries++];
179 /* free a process or thread id */
180 void free_ptid( unsigned int id )
182 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
187 /* append to end of free list so that we don't reuse it too early */
188 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
189 else next_free_ptid = id;
194 /* retrieve the pointer corresponding to a process or thread id */
195 void *get_ptid_entry( unsigned int id )
197 if (id < PTID_OFFSET) return NULL;
198 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
199 return ptid_entries[id - PTID_OFFSET].ptr;
202 /* return the main thread of the process */
203 struct thread *get_process_first_thread( struct process *process )
205 struct list *ptr = list_head( &process->thread_list );
206 if (!ptr) return NULL;
207 return LIST_ENTRY( ptr, struct thread, proc_entry );
210 /* set the state of the process startup info */
211 static void set_process_startup_state( struct process *process, enum startup_state state )
213 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
214 if (process->startup_info)
216 wake_up( &process->startup_info->obj, 0 );
217 release_object( process->startup_info );
218 process->startup_info = NULL;
222 /* create a new process and its main thread */
223 struct thread *create_process( int fd )
225 struct process *process;
226 struct thread *thread = NULL;
229 if (!(process = alloc_object( &process_ops ))) goto error;
230 process->parent = NULL;
231 process->debugger = NULL;
232 process->handles = NULL;
233 process->msg_fd = NULL;
234 process->exit_code = STILL_ACTIVE;
235 process->running_threads = 0;
236 process->priority = NORMAL_PRIORITY_CLASS;
237 process->affinity = 1;
238 process->suspend = 0;
239 process->create_flags = 0;
240 process->console = NULL;
241 process->startup_state = STARTUP_IN_PROGRESS;
242 process->startup_info = NULL;
243 process->idle_event = NULL;
244 process->queue = NULL;
246 process->ldt_copy = NULL;
247 process->winstation = 0;
248 process->desktop = 0;
249 process->exe.file = NULL;
250 process->exe.dbg_offset = 0;
251 process->exe.dbg_size = 0;
252 process->exe.namelen = 0;
253 process->exe.filename = NULL;
254 process->token = token_create_admin();
255 list_init( &process->thread_list );
256 list_init( &process->locks );
257 list_init( &process->classes );
258 list_init( &process->dlls );
260 gettimeofday( &process->start_time, NULL );
261 list_add_head( &process_list, &process->entry );
263 if (!(process->id = process->group_id = alloc_ptid( process ))) goto error;
264 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
266 /* create the main thread */
267 if (pipe( request_pipe ) == -1)
272 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
274 close( request_pipe[0] );
275 close( request_pipe[1] );
278 close( request_pipe[1] );
279 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
281 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
282 release_object( process );
286 if (process) release_object( process );
287 /* if we failed to start our first process, close everything down */
288 if (!running_processes) close_master_socket();
292 /* find the startup info for a given Unix process */
293 inline static struct startup_info *find_startup_info( int unix_pid )
297 LIST_FOR_EACH( ptr, &startup_info_list )
299 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
300 if (info->unix_pid == unix_pid) return info;
305 /* initialize the current process and fill in the request */
306 static size_t init_process(void)
308 struct process *process = current->process;
309 struct thread *parent_thread = NULL;
310 struct process *parent = NULL;
311 struct startup_info *info = find_startup_info( current->unix_pid );
317 fatal_protocol_error( current, "init_process: called twice?\n" );
320 parent_thread = info->owner;
321 parent = parent_thread->process;
322 process->parent = (struct process *)grab_object( parent );
325 /* set the process flags */
326 process->create_flags = info ? info->create_flags : 0;
328 /* create the handle table */
329 if (info && info->inherit_all)
330 process->handles = copy_handle_table( process, parent );
332 process->handles = alloc_handle_table( process, 0 );
333 if (!process->handles) return 0;
335 /* retrieve the main exe file */
336 if (info && info->exe_file) process->exe.file = (struct file *)grab_object( info->exe_file );
338 /* connect to the window station and desktop */
339 connect_process_winstation( process, NULL, 0 );
340 connect_process_desktop( process, NULL, 0 );
341 current->desktop = process->desktop;
343 /* set the process console */
344 if (info && !(info->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
346 /* FIXME: some better error checking should be done...
347 * like if hConOut and hConIn are console handles, then they should be on the same
350 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
355 /* attach to the debugger if requested */
356 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
357 set_process_debugger( process, parent_thread );
358 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
359 set_process_debugger( process, parent->debugger );
360 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
361 process->group_id = parent->group_id;
364 /* thread will be actually suspended in init_done */
365 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
369 info->process = (struct process *)grab_object( process );
370 info->thread = (struct thread *)grab_object( current );
371 process->startup_info = (struct startup_info *)grab_object( info );
372 return info->data_size;
377 /* destroy a process when its refcount is 0 */
378 static void process_destroy( struct object *obj )
380 struct process *process = (struct process *)obj;
381 assert( obj->ops == &process_ops );
383 /* we can't have a thread remaining */
384 assert( list_empty( &process->thread_list ));
386 set_process_startup_state( process, STARTUP_ABORTED );
387 if (process->console) release_object( process->console );
388 if (process->parent) release_object( process->parent );
389 if (process->msg_fd) release_object( process->msg_fd );
390 list_remove( &process->entry );
391 if (process->idle_event) release_object( process->idle_event );
392 if (process->queue) release_object( process->queue );
393 if (process->exe.file) release_object( process->exe.file );
394 if (process->exe.filename) free( process->exe.filename );
395 if (process->id) free_ptid( process->id );
396 if (process->token) release_object( process->token );
399 /* dump a process on stdout for debugging purposes */
400 static void process_dump( struct object *obj, int verbose )
402 struct process *process = (struct process *)obj;
403 assert( obj->ops == &process_ops );
405 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
408 static int process_signaled( struct object *obj, struct thread *thread )
410 struct process *process = (struct process *)obj;
411 return !process->running_threads;
414 static void process_poll_event( struct fd *fd, int event )
416 struct process *process = get_fd_user( fd );
417 assert( process->obj.ops == &process_ops );
419 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
420 else if (event & POLLIN) receive_fd( process );
423 static void startup_info_destroy( struct object *obj )
425 struct startup_info *info = (struct startup_info *)obj;
426 assert( obj->ops == &startup_info_ops );
427 list_remove( &info->entry );
428 if (info->data) free( info->data );
429 if (info->exe_file) release_object( info->exe_file );
430 if (info->process) release_object( info->process );
431 if (info->thread) release_object( info->thread );
432 if (info->owner) release_object( info->owner );
435 static void startup_info_dump( struct object *obj, int verbose )
437 struct startup_info *info = (struct startup_info *)obj;
438 assert( obj->ops == &startup_info_ops );
440 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
441 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
444 static int startup_info_signaled( struct object *obj, struct thread *thread )
446 struct startup_info *info = (struct startup_info *)obj;
447 return info->process && is_process_init_done(info->process);
450 /* get a process from an id (and increment the refcount) */
451 struct process *get_process_from_id( process_id_t id )
453 struct object *obj = get_ptid_entry( id );
455 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
456 set_error( STATUS_INVALID_PARAMETER );
460 /* get a process from a handle (and increment the refcount) */
461 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
463 return (struct process *)get_handle_obj( current->process, handle,
464 access, &process_ops );
467 /* find a dll from its base address */
468 static inline struct process_dll *find_process_dll( struct process *process, void *base )
470 struct process_dll *dll;
472 if (process->exe.base == base) return &process->exe;
474 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
476 if (dll->base == base) return dll;
481 /* add a dll to a process list */
482 static struct process_dll *process_load_dll( struct process *process, struct file *file,
483 void *base, const WCHAR *filename, size_t name_len )
485 struct process_dll *dll;
487 /* make sure we don't already have one with the same base address */
488 if (find_process_dll( process, base ))
490 set_error( STATUS_INVALID_PARAMETER );
494 if ((dll = mem_alloc( sizeof(*dll) )))
498 dll->filename = NULL;
499 dll->namelen = name_len;
500 if (name_len && !(dll->filename = memdup( filename, name_len )))
505 if (file) dll->file = (struct file *)grab_object( file );
506 list_add_head( &process->dlls, &dll->entry );
511 /* remove a dll from a process list */
512 static void process_unload_dll( struct process *process, void *base )
514 struct process_dll *dll = find_process_dll( process, base );
516 if (dll && dll != &process->exe)
518 if (dll->file) release_object( dll->file );
519 if (dll->filename) free( dll->filename );
520 list_remove( &dll->entry );
522 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
524 else set_error( STATUS_INVALID_PARAMETER );
527 /* kill all processes */
528 void kill_all_processes( struct process *skip, int exit_code )
532 struct process *process;
534 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
536 if (process == skip) continue;
537 if (process->running_threads) break;
539 if (&process->entry == &process_list) break; /* no process found */
540 kill_process( process, NULL, exit_code );
544 /* kill all processes being attached to a console renderer */
545 void kill_console_processes( struct thread *renderer, int exit_code )
547 for (;;) /* restart from the beginning of the list every time */
549 struct process *process;
551 /* find the first process being attached to 'renderer' and still running */
552 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
554 if (process == renderer->process) continue;
555 if (!process->running_threads) continue;
556 if (process->console && process->console->renderer == renderer) break;
558 if (&process->entry == &process_list) break; /* no process found */
559 kill_process( process, NULL, exit_code );
563 /* a process has been killed (i.e. its last thread died) */
564 static void process_killed( struct process *process )
566 struct handle_table *handles;
569 assert( list_empty( &process->thread_list ));
570 gettimeofday( &process->end_time, NULL );
571 handles = process->handles;
572 process->handles = NULL;
573 if (handles) release_object( handles );
575 /* close the console attached to this process, if any */
576 free_console( process );
578 while ((ptr = list_head( &process->dlls )))
580 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
581 if (dll->file) release_object( dll->file );
582 if (dll->filename) free( dll->filename );
583 list_remove( &dll->entry );
586 destroy_process_classes( process );
587 remove_process_locks( process );
588 set_process_startup_state( process, STARTUP_ABORTED );
589 if (process->exe.file) release_object( process->exe.file );
590 process->exe.file = NULL;
591 wake_up( &process->obj, 0 );
592 if (!--running_processes) close_master_socket();
595 /* add a thread to a process running threads list */
596 void add_process_thread( struct process *process, struct thread *thread )
598 list_add_head( &process->thread_list, &thread->proc_entry );
599 if (!process->running_threads++) running_processes++;
600 grab_object( thread );
603 /* remove a thread from a process running threads list */
604 void remove_process_thread( struct process *process, struct thread *thread )
606 assert( process->running_threads > 0 );
607 assert( !list_empty( &process->thread_list ));
609 list_remove( &thread->proc_entry );
611 if (!--process->running_threads)
613 /* we have removed the last running thread, exit the process */
614 process->exit_code = thread->exit_code;
615 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
616 process_killed( process );
618 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
619 release_object( thread );
622 /* suspend all the threads of a process */
623 void suspend_process( struct process *process )
625 if (!process->suspend++)
627 struct list *ptr, *next;
629 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
631 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
632 if (!thread->suspend) stop_thread( thread );
637 /* resume all the threads of a process */
638 void resume_process( struct process *process )
640 assert (process->suspend > 0);
641 if (!--process->suspend)
643 struct list *ptr, *next;
645 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
647 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
648 if (!thread->suspend) wake_thread( thread );
653 /* kill a process on the spot */
654 void kill_process( struct process *process, struct thread *skip, int exit_code )
656 struct list *ptr, *next;
658 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
660 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
662 thread->exit_code = exit_code;
663 if (thread != skip) kill_thread( thread, 1 );
667 /* kill all processes being debugged by a given thread */
668 void kill_debugged_processes( struct thread *debugger, int exit_code )
670 for (;;) /* restart from the beginning of the list every time */
672 struct process *process;
674 /* find the first process being debugged by 'debugger' and still running */
675 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
677 if (!process->running_threads) continue;
678 if (process->debugger == debugger) break;
680 if (&process->entry == &process_list) break; /* no process found */
681 process->debugger = NULL;
682 kill_process( process, NULL, exit_code );
687 /* detach a debugger from all its debuggees */
688 void detach_debugged_processes( struct thread *debugger )
690 struct process *process;
692 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
694 if (process->debugger == debugger && process->running_threads)
696 debugger_detach( process, debugger );
702 void enum_processes( int (*cb)(struct process*, void*), void *user )
704 struct list *ptr, *next;
706 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
708 struct process *process = LIST_ENTRY( ptr, struct process, entry );
709 if ((cb)(process, user)) break;
714 /* read data from a process memory space */
715 /* len is the total size (in ints) */
716 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
718 struct thread *thread = get_process_first_thread( process );
720 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
722 if (!thread) /* process is dead */
724 set_error( STATUS_ACCESS_DENIED );
727 if (suspend_for_ptrace( thread ))
731 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
734 resume_after_ptrace( thread );
739 /* make sure we can write to the whole address range */
740 /* len is the total size (in ints) */
741 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
743 int page = get_page_size() / sizeof(int);
747 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
748 if (len <= page) break;
752 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
755 /* write data to a process memory space */
756 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
757 /* we check the total size for write permissions */
758 static int write_process_memory( struct process *process, int *addr, size_t len,
759 unsigned int first_mask, unsigned int last_mask, const int *src )
761 struct thread *thread = get_process_first_thread( process );
764 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
766 if (!thread) /* process is dead */
768 set_error( STATUS_ACCESS_DENIED );
771 if (suspend_for_ptrace( thread ))
773 if (!check_process_write_access( thread, addr, len ))
775 set_error( STATUS_ACCESS_DENIED );
778 /* first word is special */
781 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
784 else last_mask &= first_mask;
788 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
792 /* last word is special too */
793 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
797 resume_after_ptrace( thread );
802 /* set the debugged flag in the process PEB */
803 int set_process_debug_flag( struct process *process, int flag )
805 int mask = 0, data = 0;
807 /* BeingDebugged flag is the byte at offset 2 in the PEB */
808 memset( (char *)&mask + 2, 0xff, 1 );
809 memset( (char *)&data + 2, flag, 1 );
810 return write_process_memory( process, process->peb, 1, mask, mask, &data );
813 /* take a snapshot of currently running processes */
814 struct process_snapshot *process_snap( int *count )
816 struct process_snapshot *snapshot, *ptr;
817 struct process *process;
818 if (!running_processes) return NULL;
819 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
822 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
824 if (!process->running_threads) continue;
825 ptr->process = process;
826 ptr->threads = process->running_threads;
827 ptr->count = process->obj.refcount;
828 ptr->priority = process->priority;
829 ptr->handles = get_handle_table_count(process);
830 grab_object( process );
833 *count = running_processes;
837 /* take a snapshot of the modules of a process */
838 struct module_snapshot *module_snap( struct process *process, int *count )
840 struct module_snapshot *snapshot, *ptr;
841 struct process_dll *dll;
844 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
845 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
847 /* first entry is main exe */
848 snapshot->base = process->exe.base;
849 snapshot->size = process->exe.size;
850 snapshot->namelen = process->exe.namelen;
851 snapshot->filename = memdup( process->exe.filename, process->exe.namelen );
854 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
856 ptr->base = dll->base;
857 ptr->size = dll->size;
858 ptr->namelen = dll->namelen;
859 ptr->filename = memdup( dll->filename, dll->namelen );
867 /* create a new process */
868 DECL_HANDLER(new_process)
870 struct startup_info *info;
872 /* build the startup info for a new process */
873 if (!(info = alloc_object( &startup_info_ops ))) return;
874 list_add_head( &startup_info_list, &info->entry );
875 info->inherit_all = req->inherit_all;
876 info->create_flags = req->create_flags;
877 info->unix_pid = req->unix_pid;
878 info->hstdin = req->hstdin;
879 info->hstdout = req->hstdout;
880 info->hstderr = req->hstderr;
881 info->exe_file = NULL;
882 info->owner = (struct thread *)grab_object( current );
883 info->process = NULL;
885 info->data_size = get_req_data_size();
889 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
892 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
893 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
896 release_object( info );
899 /* Retrieve information about a newly started process */
900 DECL_HANDLER(get_new_process_info)
902 struct startup_info *info;
904 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
905 0, &startup_info_ops )))
907 reply->pid = get_process_id( info->process );
908 reply->tid = get_thread_id( info->thread );
909 reply->phandle = alloc_handle( current->process, info->process,
910 PROCESS_ALL_ACCESS, req->pinherit );
911 reply->thandle = alloc_handle( current->process, info->thread,
912 THREAD_ALL_ACCESS, req->tinherit );
913 reply->success = is_process_init_done( info->process );
914 release_object( info );
926 /* Retrieve the new process startup info */
927 DECL_HANDLER(get_startup_info)
929 struct process *process = current->process;
930 struct startup_info *info = process->startup_info;
935 reply->create_flags = info->create_flags;
937 if (info->exe_file &&
938 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
940 if (!info->inherit_all && !(info->create_flags & CREATE_NEW_CONSOLE))
942 struct process *parent_process = info->owner->process;
943 reply->hstdin = duplicate_handle( parent_process, info->hstdin, process,
944 0, TRUE, DUPLICATE_SAME_ACCESS );
945 reply->hstdout = duplicate_handle( parent_process, info->hstdout, process,
946 0, TRUE, DUPLICATE_SAME_ACCESS );
947 reply->hstderr = duplicate_handle( parent_process, info->hstderr, process,
948 0, TRUE, DUPLICATE_SAME_ACCESS );
949 /* some handles above may have been invalid; this is not an error */
950 if (get_error() == STATUS_INVALID_HANDLE ||
951 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
955 reply->hstdin = info->hstdin;
956 reply->hstdout = info->hstdout;
957 reply->hstderr = info->hstderr;
960 /* we return the data directly without making a copy so this can only be called once */
961 size = info->data_size;
962 if (size > get_reply_max_size()) size = get_reply_max_size();
963 set_reply_data_ptr( info->data, size );
969 /* initialize a new process */
970 DECL_HANDLER(init_process)
972 if (current->unix_pid == -1)
974 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
977 if (current->process->startup_info)
979 fatal_protocol_error( current, "init_process: called twice\n" );
982 if (!req->peb || (unsigned int)req->peb % sizeof(int))
984 fatal_protocol_error( current, "init_process: bad peb address\n" );
987 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
989 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
992 current->process->peb = req->peb;
993 current->process->ldt_copy = req->ldt_copy;
994 reply->server_start = server_start_ticks;
995 reply->info_size = init_process();
998 /* signal the end of the process initialization */
999 DECL_HANDLER(init_process_done)
1001 struct file *file = NULL;
1002 struct process *process = current->process;
1004 if (is_process_init_done(process))
1006 fatal_protocol_error( current, "init_process_done: called twice\n" );
1011 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
1014 process->exe.base = req->module;
1015 process->exe.size = req->module_size;
1016 process->exe.name = req->name;
1018 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
1019 if (process->exe.file) release_object( process->exe.file );
1020 process->exe.file = file;
1022 if ((process->exe.namelen = get_req_data_size()))
1023 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1025 generate_startup_debug_events( process, req->entry );
1026 set_process_startup_state( process, STARTUP_DONE );
1028 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1029 if (current->suspend + process->suspend > 0) stop_thread( current );
1030 if (process->debugger) set_process_debug_flag( process, 1 );
1033 /* open a handle to a process */
1034 DECL_HANDLER(open_process)
1036 struct process *process = get_process_from_id( req->pid );
1040 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1041 release_object( process );
1045 /* terminate a process */
1046 DECL_HANDLER(terminate_process)
1048 struct process *process;
1050 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1052 reply->self = (current->process == process);
1053 kill_process( process, current, req->exit_code );
1054 release_object( process );
1058 /* fetch information about a process */
1059 DECL_HANDLER(get_process_info)
1061 struct process *process;
1063 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1065 reply->pid = get_process_id( process );
1066 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1067 reply->exit_code = process->exit_code;
1068 reply->priority = process->priority;
1069 reply->process_affinity = process->affinity;
1070 reply->system_affinity = 1;
1071 reply->peb = process->peb;
1072 release_object( process );
1076 /* set information about a process */
1077 DECL_HANDLER(set_process_info)
1079 struct process *process;
1081 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1083 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1084 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1086 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1087 else process->affinity = req->affinity;
1089 release_object( process );
1093 /* read data from a process address space */
1094 DECL_HANDLER(read_process_memory)
1096 struct process *process;
1097 size_t len = get_reply_max_size();
1099 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1103 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1104 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1105 const int *start = (int *)((char *)req->addr - start_offset);
1106 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1109 if (read_process_memory( process, start, nb_ints, buffer ))
1111 /* move start of requested data to start of buffer */
1112 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1113 set_reply_data_ptr( buffer, len );
1118 release_object( process );
1121 /* write data to a process address space */
1122 DECL_HANDLER(write_process_memory)
1124 struct process *process;
1126 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1128 size_t len = get_req_data_size();
1129 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1130 set_error( STATUS_INVALID_PARAMETER );
1133 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1134 req->first_mask, req->last_mask, get_req_data() );
1136 release_object( process );
1140 /* notify the server that a dll has been loaded */
1141 DECL_HANDLER(load_dll)
1143 struct process_dll *dll;
1144 struct file *file = NULL;
1146 if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1149 if ((dll = process_load_dll( current->process, file, req->base,
1150 get_req_data(), get_req_data_size() )))
1152 dll->size = req->size;
1153 dll->dbg_offset = req->dbg_offset;
1154 dll->dbg_size = req->dbg_size;
1155 dll->name = req->name;
1156 /* only generate event if initialization is done */
1157 if (is_process_init_done( current->process ))
1158 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1160 if (file) release_object( file );
1163 /* notify the server that a dll is being unloaded */
1164 DECL_HANDLER(unload_dll)
1166 process_unload_dll( current->process, req->base );
1169 /* retrieve information about a module in a process */
1170 DECL_HANDLER(get_dll_info)
1172 struct process *process;
1174 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1176 struct process_dll *dll = find_process_dll( process, req->base_address );
1180 reply->size = dll->size;
1181 reply->entry_point = NULL; /* FIXME */
1184 size_t len = min( dll->namelen, get_reply_max_size() );
1185 set_reply_data( dll->filename, len );
1189 set_error( STATUS_DLL_NOT_FOUND );
1191 release_object( process );
1195 /* wait for a process to start waiting on input */
1196 /* FIXME: only returns event for now, wait is done in the client */
1197 DECL_HANDLER(wait_input_idle)
1199 struct process *process;
1202 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1204 if (process->idle_event && process != current->process && process->queue != current->queue)
1205 reply->event = alloc_handle( current->process, process->idle_event,
1206 EVENT_ALL_ACCESS, 0 );
1207 release_object( process );