Modify ok("xxx") calls into ok("xxx\n") as is now expected.
[wine.git] / server / process.c
blob15ab052e936a25c6157fbd1db3c746fdc0b92015
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 <limits.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <stdarg.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 "windef.h"
38 #include "winbase.h"
39 #include "winnt.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "process.h"
44 #include "thread.h"
45 #include "request.h"
46 #include "console.h"
48 /* process structure */
50 static struct process *first_process;
51 static int running_processes;
53 /* process operations */
55 static void process_dump( struct object *obj, int verbose );
56 static int process_signaled( struct object *obj, struct thread *thread );
57 static void process_poll_event( struct fd *fd, int event );
58 static void process_destroy( struct object *obj );
60 static const struct object_ops process_ops =
62 sizeof(struct process), /* size */
63 process_dump, /* dump */
64 add_queue, /* add_queue */
65 remove_queue, /* remove_queue */
66 process_signaled, /* signaled */
67 no_satisfied, /* satisfied */
68 no_get_fd, /* get_fd */
69 process_destroy /* destroy */
72 static const struct fd_ops process_fd_ops =
74 NULL, /* get_poll_events */
75 process_poll_event, /* poll_event */
76 no_flush, /* flush */
77 no_get_file_info, /* get_file_info */
78 no_queue_async /* queue_async */
81 /* process startup info */
83 struct startup_info
85 struct object obj; /* object header */
86 struct list entry; /* entry in list of startup infos */
87 int inherit_all; /* inherit all handles from parent */
88 int create_flags; /* creation flags */
89 int unix_pid; /* Unix pid of new process */
90 obj_handle_t hstdin; /* handle for stdin */
91 obj_handle_t hstdout; /* handle for stdout */
92 obj_handle_t hstderr; /* handle for stderr */
93 struct file *exe_file; /* file handle for main exe */
94 struct thread *owner; /* owner thread (the one that created the new process) */
95 struct process *process; /* created process */
96 struct thread *thread; /* created thread */
97 size_t data_size; /* size of startup data */
98 startup_info_t *data; /* data for startup info */
101 static void startup_info_dump( struct object *obj, int verbose );
102 static int startup_info_signaled( struct object *obj, struct thread *thread );
103 static void startup_info_destroy( struct object *obj );
105 static const struct object_ops startup_info_ops =
107 sizeof(struct startup_info), /* size */
108 startup_info_dump, /* dump */
109 add_queue, /* add_queue */
110 remove_queue, /* remove_queue */
111 startup_info_signaled, /* signaled */
112 no_satisfied, /* satisfied */
113 no_get_fd, /* get_fd */
114 startup_info_destroy /* destroy */
118 static struct list startup_info_list = LIST_INIT(startup_info_list);
120 struct ptid_entry
122 void *ptr; /* entry ptr */
123 unsigned int next; /* next free entry */
126 static struct ptid_entry *ptid_entries; /* array of ptid entries */
127 static unsigned int used_ptid_entries; /* number of entries in use */
128 static unsigned int alloc_ptid_entries; /* number of allocated entries */
129 static unsigned int next_free_ptid; /* next free entry */
130 static unsigned int last_free_ptid; /* last free entry */
132 #define PTID_OFFSET 8 /* offset for first ptid value */
134 /* allocate a new process or thread id */
135 unsigned int alloc_ptid( void *ptr )
137 struct ptid_entry *entry;
138 unsigned int id;
140 if (used_ptid_entries < alloc_ptid_entries)
142 id = used_ptid_entries + PTID_OFFSET;
143 entry = &ptid_entries[used_ptid_entries++];
145 else if (next_free_ptid)
147 id = next_free_ptid;
148 entry = &ptid_entries[id - PTID_OFFSET];
149 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
151 else /* need to grow the array */
153 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
154 if (!count) count = 64;
155 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
157 set_error( STATUS_NO_MEMORY );
158 return 0;
160 ptid_entries = entry;
161 alloc_ptid_entries = count;
162 id = used_ptid_entries + PTID_OFFSET;
163 entry = &ptid_entries[used_ptid_entries++];
166 entry->ptr = ptr;
167 return id;
170 /* free a process or thread id */
171 void free_ptid( unsigned int id )
173 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
175 entry->ptr = NULL;
176 entry->next = 0;
178 /* append to end of free list so that we don't reuse it too early */
179 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
180 else next_free_ptid = id;
182 last_free_ptid = id;
185 /* retrieve the pointer corresponding to a process or thread id */
186 void *get_ptid_entry( unsigned int id )
188 if (id < PTID_OFFSET) return NULL;
189 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
190 return ptid_entries[id - PTID_OFFSET].ptr;
193 /* set the state of the process startup info */
194 static void set_process_startup_state( struct process *process, enum startup_state state )
196 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
197 if (process->startup_info)
199 wake_up( &process->startup_info->obj, 0 );
200 release_object( process->startup_info );
201 process->startup_info = NULL;
205 /* set the console and stdio handles for a newly created process */
206 static int set_process_console( struct process *process, struct thread *parent_thread,
207 struct startup_info *info, struct init_process_reply *reply )
209 if (process->create_flags & CREATE_NEW_CONSOLE)
211 /* let the process init do the allocation */
212 return 1;
214 else if (info && !(process->create_flags & DETACHED_PROCESS))
216 /* FIXME: some better error checking should be done...
217 * like if hConOut and hConIn are console handles, then they should be on the same
218 * physical console
220 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
222 if (info)
224 if (!info->inherit_all)
226 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
227 0, TRUE, DUPLICATE_SAME_ACCESS );
228 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
229 0, TRUE, DUPLICATE_SAME_ACCESS );
230 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
231 0, TRUE, DUPLICATE_SAME_ACCESS );
233 else
235 reply->hstdin = info->hstdin;
236 reply->hstdout = info->hstdout;
237 reply->hstderr = info->hstderr;
240 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
241 /* some handles above may have been invalid; this is not an error */
242 if (get_error() == STATUS_INVALID_HANDLE ||
243 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
244 return 1;
247 /* create a new process and its main thread */
248 struct thread *create_process( int fd )
250 struct process *process;
251 struct thread *thread = NULL;
252 int request_pipe[2];
254 if (!(process = alloc_object( &process_ops ))) goto error;
255 process->next = NULL;
256 process->prev = NULL;
257 process->parent = NULL;
258 process->thread_list = NULL;
259 process->debugger = NULL;
260 process->handles = NULL;
261 process->msg_fd = NULL;
262 process->exit_code = STILL_ACTIVE;
263 process->running_threads = 0;
264 process->priority = NORMAL_PRIORITY_CLASS;
265 process->affinity = 1;
266 process->suspend = 0;
267 process->create_flags = 0;
268 process->console = NULL;
269 process->startup_state = STARTUP_IN_PROGRESS;
270 process->startup_info = NULL;
271 process->idle_event = NULL;
272 process->queue = NULL;
273 process->atom_table = NULL;
274 process->ldt_copy = NULL;
275 process->exe.next = NULL;
276 process->exe.prev = NULL;
277 process->exe.file = NULL;
278 process->exe.dbg_offset = 0;
279 process->exe.dbg_size = 0;
280 process->exe.namelen = 0;
281 process->exe.filename = NULL;
282 process->group_id = 0;
283 process->token = create_token();
284 list_init( &process->locks );
286 gettimeofday( &process->start_time, NULL );
287 if ((process->next = first_process) != NULL) process->next->prev = process;
288 first_process = process;
290 if (!(process->id = alloc_ptid( process ))) goto error;
291 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
293 /* create the main thread */
294 if (pipe( request_pipe ) == -1)
296 file_set_error();
297 goto error;
299 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
301 close( request_pipe[0] );
302 close( request_pipe[1] );
303 goto error;
305 close( request_pipe[1] );
306 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
308 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
309 release_object( process );
310 return thread;
312 error:
313 if (process) release_object( process );
314 /* if we failed to start our first process, close everything down */
315 if (!running_processes) close_master_socket();
316 return NULL;
319 /* find the startup info for a given Unix process */
320 inline static struct startup_info *find_startup_info( int unix_pid )
322 struct list *ptr;
324 LIST_FOR_EACH( ptr, &startup_info_list )
326 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
327 if (info->unix_pid == unix_pid) return info;
329 return NULL;
332 /* initialize the current process and fill in the request */
333 static struct startup_info *init_process( struct init_process_reply *reply )
335 struct process *process = current->process;
336 struct thread *parent_thread = NULL;
337 struct process *parent = NULL;
338 struct startup_info *info = find_startup_info( current->unix_pid );
340 if (info)
342 if (info->thread)
344 fatal_protocol_error( current, "init_process: called twice?\n" );
345 return NULL;
347 parent_thread = info->owner;
348 parent = parent_thread->process;
349 process->parent = (struct process *)grab_object( parent );
352 /* set the process flags */
353 process->create_flags = info ? info->create_flags : 0;
355 /* create the handle table */
356 if (info && info->inherit_all)
357 process->handles = copy_handle_table( process, parent );
358 else
359 process->handles = alloc_handle_table( process, 0 );
360 if (!process->handles) return NULL;
362 /* retrieve the main exe file */
363 reply->exe_file = 0;
364 if (info && info->exe_file)
366 process->exe.file = (struct file *)grab_object( info->exe_file );
367 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
368 return NULL;
371 /* set the process console */
372 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
374 process->group_id = get_process_id( process );
375 if (parent)
377 /* attach to the debugger if requested */
378 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
379 set_process_debugger( process, parent_thread );
380 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
381 set_process_debugger( process, parent->debugger );
382 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
383 process->group_id = parent->group_id;
386 /* thread will be actually suspended in init_done */
387 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
389 if (info)
391 reply->info_size = info->data_size;
392 info->process = (struct process *)grab_object( process );
393 info->thread = (struct thread *)grab_object( current );
395 reply->create_flags = process->create_flags;
396 reply->server_start = server_start_ticks;
397 return info ? (struct startup_info *)grab_object( info ) : NULL;
400 /* destroy a process when its refcount is 0 */
401 static void process_destroy( struct object *obj )
403 struct process *process = (struct process *)obj;
404 assert( obj->ops == &process_ops );
406 /* we can't have a thread remaining */
407 assert( !process->thread_list );
409 set_process_startup_state( process, STARTUP_ABORTED );
410 if (process->console) release_object( process->console );
411 if (process->parent) release_object( process->parent );
412 if (process->msg_fd) release_object( process->msg_fd );
413 if (process->next) process->next->prev = process->prev;
414 if (process->prev) process->prev->next = process->next;
415 else first_process = process->next;
416 if (process->idle_event) release_object( process->idle_event );
417 if (process->queue) release_object( process->queue );
418 if (process->atom_table) release_object( process->atom_table );
419 if (process->exe.file) release_object( process->exe.file );
420 if (process->exe.filename) free( process->exe.filename );
421 if (process->id) free_ptid( process->id );
422 if (process->token) release_object( process->token );
425 /* dump a process on stdout for debugging purposes */
426 static void process_dump( struct object *obj, int verbose )
428 struct process *process = (struct process *)obj;
429 assert( obj->ops == &process_ops );
431 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
432 process->id, process->next, process->prev, process->handles );
435 static int process_signaled( struct object *obj, struct thread *thread )
437 struct process *process = (struct process *)obj;
438 return !process->running_threads;
441 static void process_poll_event( struct fd *fd, int event )
443 struct process *process = get_fd_user( fd );
444 assert( process->obj.ops == &process_ops );
446 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
447 else if (event & POLLIN) receive_fd( process );
450 static void startup_info_destroy( struct object *obj )
452 struct startup_info *info = (struct startup_info *)obj;
453 assert( obj->ops == &startup_info_ops );
454 list_remove( &info->entry );
455 if (info->data) free( info->data );
456 if (info->exe_file) release_object( info->exe_file );
457 if (info->process) release_object( info->process );
458 if (info->thread) release_object( info->thread );
459 if (info->owner) release_object( info->owner );
462 static void startup_info_dump( struct object *obj, int verbose )
464 struct startup_info *info = (struct startup_info *)obj;
465 assert( obj->ops == &startup_info_ops );
467 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
468 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
471 static int startup_info_signaled( struct object *obj, struct thread *thread )
473 struct startup_info *info = (struct startup_info *)obj;
474 return info->process && is_process_init_done(info->process);
477 /* get a process from an id (and increment the refcount) */
478 struct process *get_process_from_id( process_id_t id )
480 struct object *obj = get_ptid_entry( id );
482 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
483 set_error( STATUS_INVALID_PARAMETER );
484 return NULL;
487 /* get a process from a handle (and increment the refcount) */
488 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
490 return (struct process *)get_handle_obj( current->process, handle,
491 access, &process_ops );
494 /* add a dll to a process list */
495 static struct process_dll *process_load_dll( struct process *process, struct file *file,
496 void *base, const char *filename, size_t name_len )
498 struct process_dll *dll;
500 /* make sure we don't already have one with the same base address */
501 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
503 set_error( STATUS_INVALID_PARAMETER );
504 return NULL;
507 if ((dll = mem_alloc( sizeof(*dll) )))
509 dll->prev = &process->exe;
510 dll->file = NULL;
511 dll->base = base;
512 dll->filename = NULL;
513 dll->namelen = name_len;
514 if (name_len && !(dll->filename = memdup( filename, name_len )))
516 free( dll );
517 return NULL;
519 if (file) dll->file = (struct file *)grab_object( file );
520 if ((dll->next = process->exe.next)) dll->next->prev = dll;
521 process->exe.next = dll;
523 return dll;
526 /* remove a dll from a process list */
527 static void process_unload_dll( struct process *process, void *base )
529 struct process_dll *dll;
531 for (dll = process->exe.next; dll; dll = dll->next)
533 if (dll->base == base)
535 if (dll->file) release_object( dll->file );
536 if (dll->next) dll->next->prev = dll->prev;
537 if (dll->prev) dll->prev->next = dll->next;
538 if (dll->filename) free( dll->filename );
539 free( dll );
540 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
541 return;
544 set_error( STATUS_INVALID_PARAMETER );
547 /* kill all processes */
548 void kill_all_processes( struct process *skip, int exit_code )
550 for (;;)
552 struct process *process = first_process;
554 while (process && (!process->running_threads || process == skip))
555 process = process->next;
556 if (!process) break;
557 kill_process( process, NULL, exit_code );
561 /* kill all processes being attached to a console renderer */
562 void kill_console_processes( struct thread *renderer, int exit_code )
564 for (;;) /* restart from the beginning of the list every time */
566 struct process *process = first_process;
568 /* find the first process being attached to 'renderer' and still running */
569 while (process &&
570 (process == renderer->process || !process->console ||
571 process->console->renderer != renderer || !process->running_threads))
573 process = process->next;
575 if (!process) break;
576 kill_process( process, NULL, exit_code );
580 /* a process has been killed (i.e. its last thread died) */
581 static void process_killed( struct process *process )
583 assert( !process->thread_list );
584 gettimeofday( &process->end_time, NULL );
585 if (process->handles) release_object( process->handles );
586 process->handles = NULL;
588 /* close the console attached to this process, if any */
589 free_console( process );
591 while (process->exe.next)
593 struct process_dll *dll = process->exe.next;
594 process->exe.next = dll->next;
595 if (dll->file) release_object( dll->file );
596 if (dll->filename) free( dll->filename );
597 free( dll );
599 set_process_startup_state( process, STARTUP_ABORTED );
600 if (process->exe.file) release_object( process->exe.file );
601 process->exe.file = NULL;
602 wake_up( &process->obj, 0 );
603 if (!--running_processes) close_master_socket();
606 /* add a thread to a process running threads list */
607 void add_process_thread( struct process *process, struct thread *thread )
609 thread->proc_next = process->thread_list;
610 thread->proc_prev = NULL;
611 if (thread->proc_next) thread->proc_next->proc_prev = thread;
612 process->thread_list = thread;
613 if (!process->running_threads++) running_processes++;
614 grab_object( thread );
617 /* remove a thread from a process running threads list */
618 void remove_process_thread( struct process *process, struct thread *thread )
620 assert( process->running_threads > 0 );
621 assert( process->thread_list );
623 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
624 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
625 else process->thread_list = thread->proc_next;
627 if (!--process->running_threads)
629 /* we have removed the last running thread, exit the process */
630 process->exit_code = thread->exit_code;
631 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
632 process_killed( process );
634 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
635 release_object( thread );
638 /* suspend all the threads of a process */
639 void suspend_process( struct process *process )
641 if (!process->suspend++)
643 struct thread *thread = process->thread_list;
644 while (thread)
646 struct thread *next = thread->proc_next;
647 if (!thread->suspend) stop_thread( thread );
648 thread = next;
653 /* resume all the threads of a process */
654 void resume_process( struct process *process )
656 assert (process->suspend > 0);
657 if (!--process->suspend)
659 struct thread *thread = process->thread_list;
660 while (thread)
662 struct thread *next = thread->proc_next;
663 if (!thread->suspend) wake_thread( thread );
664 thread = next;
669 /* kill a process on the spot */
670 void kill_process( struct process *process, struct thread *skip, int exit_code )
672 struct thread *thread = process->thread_list;
674 while (thread)
676 struct thread *next = thread->proc_next;
677 thread->exit_code = exit_code;
678 if (thread != skip) kill_thread( thread, 1 );
679 thread = next;
683 /* kill all processes being debugged by a given thread */
684 void kill_debugged_processes( struct thread *debugger, int exit_code )
686 for (;;) /* restart from the beginning of the list every time */
688 struct process *process = first_process;
689 /* find the first process being debugged by 'debugger' and still running */
690 while (process && (process->debugger != debugger || !process->running_threads))
691 process = process->next;
692 if (!process) return;
693 process->debugger = NULL;
694 kill_process( process, NULL, exit_code );
699 /* detach a debugger from all its debuggees */
700 void detach_debugged_processes( struct thread *debugger )
702 struct process *process;
703 for (process = first_process; process; process = process->next)
705 if (process->debugger == debugger && process->running_threads)
707 debugger_detach( process, debugger );
713 void enum_processes( int (*cb)(struct process*, void*), void *user )
715 struct process *process;
716 for (process = first_process; process; process = process->next)
718 if ((cb)(process, user)) break;
723 /* get all information about a process */
724 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
726 reply->pid = get_process_id( process );
727 reply->debugged = (process->debugger != 0);
728 reply->exit_code = process->exit_code;
729 reply->priority = process->priority;
730 reply->process_affinity = process->affinity;
731 reply->system_affinity = 1;
734 /* set all information about a process */
735 static void set_process_info( struct process *process,
736 const struct set_process_info_request *req )
738 if (req->mask & SET_PROCESS_INFO_PRIORITY)
739 process->priority = req->priority;
740 if (req->mask & SET_PROCESS_INFO_AFFINITY)
742 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
743 else process->affinity = req->affinity;
747 /* read data from a process memory space */
748 /* len is the total size (in ints) */
749 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
751 struct thread *thread = process->thread_list;
753 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
755 if (!thread) /* process is dead */
757 set_error( STATUS_ACCESS_DENIED );
758 return 0;
760 if (suspend_for_ptrace( thread ))
762 while (len > 0)
764 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
765 len--;
767 resume_after_ptrace( thread );
769 return !len;
772 /* make sure we can write to the whole address range */
773 /* len is the total size (in ints) */
774 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
776 int page = get_page_size() / sizeof(int);
778 for (;;)
780 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
781 if (len <= page) break;
782 addr += page;
783 len -= page;
785 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
788 /* write data to a process memory space */
789 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
790 /* we check the total size for write permissions */
791 static void write_process_memory( struct process *process, int *addr, size_t len,
792 unsigned int first_mask, unsigned int last_mask, const int *src )
794 struct thread *thread = process->thread_list;
796 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
798 if (!thread) /* process is dead */
800 set_error( STATUS_ACCESS_DENIED );
801 return;
803 if (suspend_for_ptrace( thread ))
805 if (!check_process_write_access( thread, addr, len ))
807 set_error( STATUS_ACCESS_DENIED );
808 goto done;
810 /* first word is special */
811 if (len > 1)
813 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
814 len--;
816 else last_mask &= first_mask;
818 while (len > 1)
820 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
821 len--;
824 /* last word is special too */
825 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
827 done:
828 resume_after_ptrace( thread );
832 /* take a snapshot of currently running processes */
833 struct process_snapshot *process_snap( int *count )
835 struct process_snapshot *snapshot, *ptr;
836 struct process *process;
837 if (!running_processes) return NULL;
838 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
839 return NULL;
840 ptr = snapshot;
841 for (process = first_process; process; process = process->next)
843 if (!process->running_threads) continue;
844 ptr->process = process;
845 ptr->threads = process->running_threads;
846 ptr->count = process->obj.refcount;
847 ptr->priority = process->priority;
848 grab_object( process );
849 ptr++;
851 *count = running_processes;
852 return snapshot;
855 /* take a snapshot of the modules of a process */
856 struct module_snapshot *module_snap( struct process *process, int *count )
858 struct module_snapshot *snapshot, *ptr;
859 struct process_dll *dll;
860 int total = 0;
862 for (dll = &process->exe; dll; dll = dll->next) total++;
863 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
865 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
867 ptr->base = dll->base;
868 ptr->size = dll->size;
869 ptr->namelen = dll->namelen;
870 ptr->filename = memdup( dll->filename, dll->namelen );
872 *count = total;
873 return snapshot;
877 /* create a new process */
878 DECL_HANDLER(new_process)
880 struct startup_info *info;
882 /* build the startup info for a new process */
883 if (!(info = alloc_object( &startup_info_ops ))) return;
884 list_add_head( &startup_info_list, &info->entry );
885 info->inherit_all = req->inherit_all;
886 info->create_flags = req->create_flags;
887 info->unix_pid = req->unix_pid;
888 info->hstdin = req->hstdin;
889 info->hstdout = req->hstdout;
890 info->hstderr = req->hstderr;
891 info->exe_file = NULL;
892 info->owner = (struct thread *)grab_object( current );
893 info->process = NULL;
894 info->thread = NULL;
895 info->data_size = get_req_data_size();
896 info->data = NULL;
898 if (req->exe_file &&
899 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
900 goto done;
902 if (!(info->data = mem_alloc( info->data_size ))) goto done;
903 memcpy( info->data, get_req_data(), info->data_size );
904 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
906 done:
907 release_object( info );
910 /* Retrieve information about a newly started process */
911 DECL_HANDLER(get_new_process_info)
913 struct startup_info *info;
915 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
916 0, &startup_info_ops )))
918 reply->pid = get_process_id( info->process );
919 reply->tid = get_thread_id( info->thread );
920 reply->phandle = alloc_handle( current->process, info->process,
921 PROCESS_ALL_ACCESS, req->pinherit );
922 reply->thandle = alloc_handle( current->process, info->thread,
923 THREAD_ALL_ACCESS, req->tinherit );
924 reply->success = is_process_init_done( info->process );
925 release_object( info );
927 else
929 reply->pid = 0;
930 reply->tid = 0;
931 reply->phandle = 0;
932 reply->thandle = 0;
933 reply->success = 0;
937 /* Retrieve the new process startup info */
938 DECL_HANDLER(get_startup_info)
940 struct startup_info *info;
942 if ((info = current->process->startup_info))
944 size_t size = info->data_size;
945 if (size > get_reply_max_size()) size = get_reply_max_size();
947 /* we return the data directly without making a copy so this can only be called once */
948 set_reply_data_ptr( info->data, size );
949 info->data = NULL;
950 info->data_size = 0;
955 /* initialize a new process */
956 DECL_HANDLER(init_process)
958 if (current->unix_pid == -1)
960 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
961 return;
963 if (current->process->startup_info)
965 fatal_protocol_error( current, "init_process: called twice\n" );
966 return;
968 reply->info_size = 0;
969 current->process->ldt_copy = req->ldt_copy;
970 current->process->startup_info = init_process( reply );
973 /* signal the end of the process initialization */
974 DECL_HANDLER(init_process_done)
976 struct file *file = NULL;
977 struct process *process = current->process;
979 if (is_process_init_done(process))
981 fatal_protocol_error( current, "init_process_done: called twice\n" );
982 return;
984 if (!req->module)
986 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
987 return;
989 process->exe.base = req->module;
990 process->exe.size = req->module_size;
991 process->exe.name = req->name;
993 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
994 if (process->exe.file) release_object( process->exe.file );
995 process->exe.file = file;
997 if ((process->exe.namelen = get_req_data_size()))
998 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1000 generate_startup_debug_events( process, req->entry );
1001 set_process_startup_state( process, STARTUP_DONE );
1003 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1004 if (current->suspend + process->suspend > 0) stop_thread( current );
1005 reply->debugged = (process->debugger != 0);
1008 /* open a handle to a process */
1009 DECL_HANDLER(open_process)
1011 struct process *process = get_process_from_id( req->pid );
1012 reply->handle = 0;
1013 if (process)
1015 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1016 release_object( process );
1020 /* terminate a process */
1021 DECL_HANDLER(terminate_process)
1023 struct process *process;
1025 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1027 reply->self = (current->process == process);
1028 kill_process( process, current, req->exit_code );
1029 release_object( process );
1033 /* fetch information about a process */
1034 DECL_HANDLER(get_process_info)
1036 struct process *process;
1038 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1040 get_process_info( process, reply );
1041 release_object( process );
1045 /* set information about a process */
1046 DECL_HANDLER(set_process_info)
1048 struct process *process;
1050 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1052 set_process_info( process, req );
1053 release_object( process );
1057 /* read data from a process address space */
1058 DECL_HANDLER(read_process_memory)
1060 struct process *process;
1061 size_t len = get_reply_max_size();
1063 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1065 if (len)
1067 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1068 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1069 const int *start = (int *)((char *)req->addr - start_offset);
1070 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1071 if (buffer)
1073 if (read_process_memory( process, start, nb_ints, buffer ))
1075 /* move start of requested data to start of buffer */
1076 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1077 set_reply_data_ptr( buffer, len );
1079 else len = 0;
1082 release_object( process );
1085 /* write data to a process address space */
1086 DECL_HANDLER(write_process_memory)
1088 struct process *process;
1090 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1092 size_t len = get_req_data_size();
1093 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1094 set_error( STATUS_INVALID_PARAMETER );
1095 else
1097 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1098 req->first_mask, req->last_mask, get_req_data() );
1100 release_object( process );
1104 /* notify the server that a dll has been loaded */
1105 DECL_HANDLER(load_dll)
1107 struct process_dll *dll;
1108 struct file *file = NULL;
1110 if (req->handle &&
1111 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1113 if ((dll = process_load_dll( current->process, file, req->base,
1114 get_req_data(), get_req_data_size() )))
1116 dll->size = req->size;
1117 dll->dbg_offset = req->dbg_offset;
1118 dll->dbg_size = req->dbg_size;
1119 dll->name = req->name;
1120 /* only generate event if initialization is done */
1121 if (is_process_init_done( current->process ))
1122 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1124 if (file) release_object( file );
1127 /* notify the server that a dll is being unloaded */
1128 DECL_HANDLER(unload_dll)
1130 process_unload_dll( current->process, req->base );
1133 /* retrieve information about a module in a process */
1134 DECL_HANDLER(get_dll_info)
1136 struct process *process;
1138 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1140 struct process_dll *dll;
1142 for (dll = &process->exe; dll; dll = dll->next)
1144 if (dll->base == req->base_address)
1146 reply->size = dll->size;
1147 reply->entry_point = 0; /* FIXME */
1148 if (dll->filename)
1150 size_t len = min( dll->namelen, get_reply_max_size() );
1151 set_reply_data( dll->filename, len );
1153 break;
1156 if (!dll)
1157 set_error(STATUS_DLL_NOT_FOUND);
1158 release_object( process );
1162 /* wait for a process to start waiting on input */
1163 /* FIXME: only returns event for now, wait is done in the client */
1164 DECL_HANDLER(wait_input_idle)
1166 struct process *process;
1168 reply->event = 0;
1169 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1171 if (process->idle_event && process != current->process && process->queue != current->queue)
1172 reply->event = alloc_handle( current->process, process->idle_event,
1173 EVENT_ALL_ACCESS, 0 );
1174 release_object( process );