Add CSIDL_PROFILES. Improved logging for non-existent CSIDL values.
[wine/multimedia.git] / server / process.c
blobf26b62a47aa3b97daca2307f011ce7ce59134cf3
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 <stdio.h>
29 #include <stdlib.h>
30 #include <sys/time.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
33 #endif
34 #include <unistd.h>
36 #include "winbase.h"
37 #include "winnt.h"
39 #include "file.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 fd *fd, 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 no_get_fd, /* get_fd */
67 process_destroy /* destroy */
70 static const struct fd_ops process_fd_ops =
72 NULL, /* get_poll_events */
73 process_poll_event, /* poll_event */
74 no_flush, /* flush */
75 no_get_file_info, /* get_file_info */
76 no_queue_async /* queue_async */
79 /* process startup info */
81 struct startup_info
83 struct object obj; /* object header */
84 struct list entry; /* entry in list of startup infos */
85 int inherit_all; /* inherit all handles from parent */
86 int use_handles; /* use stdio handles */
87 int create_flags; /* creation flags */
88 int unix_pid; /* Unix pid of new process */
89 obj_handle_t hstdin; /* handle for stdin */
90 obj_handle_t hstdout; /* handle for stdout */
91 obj_handle_t hstderr; /* handle for stderr */
92 struct file *exe_file; /* file handle for main exe */
93 struct thread *owner; /* owner thread (the one that created the new process) */
94 struct process *process; /* created process */
95 struct thread *thread; /* created thread */
96 size_t data_size; /* size of startup data */
97 startup_info_t *data; /* data for startup info */
100 static void startup_info_dump( struct object *obj, int verbose );
101 static int startup_info_signaled( struct object *obj, struct thread *thread );
102 static void startup_info_destroy( struct object *obj );
104 static const struct object_ops startup_info_ops =
106 sizeof(struct startup_info), /* size */
107 startup_info_dump, /* dump */
108 add_queue, /* add_queue */
109 remove_queue, /* remove_queue */
110 startup_info_signaled, /* signaled */
111 no_satisfied, /* satisfied */
112 no_get_fd, /* get_fd */
113 startup_info_destroy /* destroy */
117 static struct list startup_info_list = LIST_INIT(startup_info_list);
119 struct ptid_entry
121 void *ptr; /* entry ptr */
122 unsigned int next; /* next free entry */
125 static struct ptid_entry *ptid_entries; /* array of ptid entries */
126 static unsigned int used_ptid_entries; /* number of entries in use */
127 static unsigned int alloc_ptid_entries; /* number of allocated entries */
128 static unsigned int next_free_ptid; /* next free entry */
129 static unsigned int last_free_ptid; /* last free entry */
131 #define PTID_OFFSET 8 /* offset for first ptid value */
133 /* allocate a new process or thread id */
134 unsigned int alloc_ptid( void *ptr )
136 struct ptid_entry *entry;
137 unsigned int id;
139 if (used_ptid_entries < alloc_ptid_entries)
141 id = used_ptid_entries + PTID_OFFSET;
142 entry = &ptid_entries[used_ptid_entries++];
144 else if (next_free_ptid)
146 id = next_free_ptid;
147 entry = &ptid_entries[id - PTID_OFFSET];
148 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
150 else /* need to grow the array */
152 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
153 if (!count) count = 64;
154 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
156 set_error( STATUS_NO_MEMORY );
157 return 0;
159 ptid_entries = entry;
160 alloc_ptid_entries = count;
161 id = used_ptid_entries + PTID_OFFSET;
162 entry = &ptid_entries[used_ptid_entries++];
165 entry->ptr = ptr;
166 return id;
169 /* free a process or thread id */
170 void free_ptid( unsigned int id )
172 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
174 entry->ptr = NULL;
175 entry->next = 0;
177 /* append to end of free list so that we don't reuse it too early */
178 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
179 else next_free_ptid = id;
181 last_free_ptid = id;
184 /* retrieve the pointer corresponding to a process or thread id */
185 void *get_ptid_entry( unsigned int id )
187 if (id < PTID_OFFSET) return NULL;
188 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
189 return ptid_entries[id - PTID_OFFSET].ptr;
192 /* set the state of the process startup info */
193 static void set_process_startup_state( struct process *process, enum startup_state state )
195 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
196 if (process->startup_info)
198 wake_up( &process->startup_info->obj, 0 );
199 release_object( process->startup_info );
200 process->startup_info = NULL;
204 /* set the console and stdio handles for a newly created process */
205 static int set_process_console( struct process *process, struct thread *parent_thread,
206 struct startup_info *info, struct init_process_reply *reply )
208 if (process->create_flags & CREATE_NEW_CONSOLE)
210 /* let the process init do the allocation */
211 return 1;
213 else if (info && !(process->create_flags & DETACHED_PROCESS))
215 /* FIXME: some better error checking should be done...
216 * like if hConOut and hConIn are console handles, then they should be on the same
217 * physical console
219 inherit_console( parent_thread, process,
220 (info->inherit_all || info->use_handles) ? info->hstdin : 0 );
222 if (info)
224 if (!info->inherit_all && !info->use_handles)
226 /* duplicate the handle from the parent into this process */
227 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
228 0, TRUE, DUPLICATE_SAME_ACCESS );
229 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
230 0, TRUE, DUPLICATE_SAME_ACCESS );
231 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
232 0, TRUE, DUPLICATE_SAME_ACCESS );
234 else
236 reply->hstdin = info->hstdin;
237 reply->hstdout = info->hstdout;
238 reply->hstderr = info->hstderr;
241 else
243 if (process->console)
245 reply->hstdin = alloc_handle( process, process->console,
246 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
247 reply->hstdout = alloc_handle( process, process->console->active,
248 GENERIC_READ | GENERIC_WRITE, 1 );
249 reply->hstderr = alloc_handle( process, process->console->active,
250 GENERIC_READ | GENERIC_WRITE, 1 );
252 else
254 /* no parent, let the caller decide what to do */
255 reply->hstdin = reply->hstdout = reply->hstderr = 0;
258 /* some handles above may have been invalid; this is not an error */
259 if (get_error() == STATUS_INVALID_HANDLE) clear_error();
260 return 1;
263 /* create a new process and its main thread */
264 struct thread *create_process( int fd )
266 struct process *process;
267 struct thread *thread = NULL;
268 int request_pipe[2];
270 if (!(process = alloc_object( &process_ops ))) goto error;
271 process->next = NULL;
272 process->prev = NULL;
273 process->parent = NULL;
274 process->thread_list = NULL;
275 process->debugger = NULL;
276 process->handles = NULL;
277 process->msg_fd = NULL;
278 process->exit_code = STILL_ACTIVE;
279 process->running_threads = 0;
280 process->priority = NORMAL_PRIORITY_CLASS;
281 process->affinity = 1;
282 process->suspend = 0;
283 process->create_flags = 0;
284 process->console = NULL;
285 process->startup_state = STARTUP_IN_PROGRESS;
286 process->startup_info = NULL;
287 process->idle_event = NULL;
288 process->queue = NULL;
289 process->atom_table = NULL;
290 process->ldt_copy = NULL;
291 process->exe.next = NULL;
292 process->exe.prev = NULL;
293 process->exe.file = NULL;
294 process->exe.dbg_offset = 0;
295 process->exe.dbg_size = 0;
296 process->exe.namelen = 0;
297 process->exe.filename = NULL;
298 process->group_id = 0;
300 gettimeofday( &process->start_time, NULL );
301 if ((process->next = first_process) != NULL) process->next->prev = process;
302 first_process = process;
304 if (!(process->id = alloc_ptid( process ))) goto error;
305 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
307 /* create the main thread */
308 if (pipe( request_pipe ) == -1)
310 file_set_error();
311 goto error;
313 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
315 close( request_pipe[0] );
316 close( request_pipe[1] );
317 goto error;
319 close( request_pipe[1] );
320 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
322 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
323 release_object( process );
324 return thread;
326 error:
327 if (process) release_object( process );
328 /* if we failed to start our first process, close everything down */
329 if (!running_processes) close_master_socket();
330 return NULL;
333 /* find the startup info for a given Unix process */
334 inline static struct startup_info *find_startup_info( int unix_pid )
336 struct list *ptr;
338 LIST_FOR_EACH( ptr, &startup_info_list )
340 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
341 if (info->unix_pid == unix_pid) return info;
343 return NULL;
346 /* initialize the current process and fill in the request */
347 static struct startup_info *init_process( struct init_process_reply *reply )
349 struct process *process = current->process;
350 struct thread *parent_thread = NULL;
351 struct process *parent = NULL;
352 struct startup_info *info = find_startup_info( current->unix_pid );
354 if (info)
356 if (info->thread)
358 fatal_protocol_error( current, "init_process: called twice?\n" );
359 return NULL;
361 parent_thread = info->owner;
362 parent = parent_thread->process;
363 process->parent = (struct process *)grab_object( parent );
366 /* set the process flags */
367 process->create_flags = info ? info->create_flags : 0;
369 /* create the handle table */
370 if (info && info->inherit_all)
371 process->handles = copy_handle_table( process, parent );
372 else
373 process->handles = alloc_handle_table( process, 0 );
374 if (!process->handles) return NULL;
376 /* retrieve the main exe file */
377 reply->exe_file = 0;
378 if (info && info->exe_file)
380 process->exe.file = (struct file *)grab_object( info->exe_file );
381 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
382 return NULL;
385 /* set the process console */
386 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
388 process->group_id = get_process_id( process );
389 if (parent)
391 /* attach to the debugger if requested */
392 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
393 set_process_debugger( process, parent_thread );
394 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
395 set_process_debugger( process, parent->debugger );
396 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
397 process->group_id = parent->group_id;
400 /* thread will be actually suspended in init_done */
401 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
403 if (info)
405 reply->info_size = info->data_size;
406 info->process = (struct process *)grab_object( process );
407 info->thread = (struct thread *)grab_object( current );
409 reply->create_flags = process->create_flags;
410 reply->server_start = server_start_ticks;
411 return info ? (struct startup_info *)grab_object( info ) : NULL;
414 /* destroy a process when its refcount is 0 */
415 static void process_destroy( struct object *obj )
417 struct process *process = (struct process *)obj;
418 assert( obj->ops == &process_ops );
420 /* we can't have a thread remaining */
421 assert( !process->thread_list );
423 set_process_startup_state( process, STARTUP_ABORTED );
424 if (process->console) release_object( process->console );
425 if (process->parent) release_object( process->parent );
426 if (process->msg_fd) release_object( process->msg_fd );
427 if (process->next) process->next->prev = process->prev;
428 if (process->prev) process->prev->next = process->next;
429 else first_process = process->next;
430 if (process->idle_event) release_object( process->idle_event );
431 if (process->queue) release_object( process->queue );
432 if (process->atom_table) release_object( process->atom_table );
433 if (process->exe.file) release_object( process->exe.file );
434 if (process->exe.filename) free( process->exe.filename );
435 if (process->id) free_ptid( process->id );
438 /* dump a process on stdout for debugging purposes */
439 static void process_dump( struct object *obj, int verbose )
441 struct process *process = (struct process *)obj;
442 assert( obj->ops == &process_ops );
444 fprintf( stderr, "Process id=%04x next=%p prev=%p handles=%p\n",
445 process->id, process->next, process->prev, process->handles );
448 static int process_signaled( struct object *obj, struct thread *thread )
450 struct process *process = (struct process *)obj;
451 return !process->running_threads;
454 static void process_poll_event( struct fd *fd, int event )
456 struct process *process = get_fd_user( fd );
457 assert( process->obj.ops == &process_ops );
459 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
460 else if (event & POLLIN) receive_fd( process );
463 static void startup_info_destroy( struct object *obj )
465 struct startup_info *info = (struct startup_info *)obj;
466 assert( obj->ops == &startup_info_ops );
467 list_remove( &info->entry );
468 if (info->data) free( info->data );
469 if (info->exe_file) release_object( info->exe_file );
470 if (info->process) release_object( info->process );
471 if (info->thread) release_object( info->thread );
472 if (info->owner) release_object( info->owner );
475 static void startup_info_dump( struct object *obj, int verbose )
477 struct startup_info *info = (struct startup_info *)obj;
478 assert( obj->ops == &startup_info_ops );
480 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
481 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
484 static int startup_info_signaled( struct object *obj, struct thread *thread )
486 struct startup_info *info = (struct startup_info *)obj;
487 return info->process && is_process_init_done(info->process);
490 /* get a process from an id (and increment the refcount) */
491 struct process *get_process_from_id( process_id_t id )
493 struct object *obj = get_ptid_entry( id );
495 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
496 set_error( STATUS_INVALID_PARAMETER );
497 return NULL;
500 /* get a process from a handle (and increment the refcount) */
501 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
503 return (struct process *)get_handle_obj( current->process, handle,
504 access, &process_ops );
507 /* add a dll to a process list */
508 static struct process_dll *process_load_dll( struct process *process, struct file *file,
509 void *base, const char *filename, size_t name_len )
511 struct process_dll *dll;
513 /* make sure we don't already have one with the same base address */
514 for (dll = process->exe.next; dll; dll = dll->next) if (dll->base == base)
516 set_error( STATUS_INVALID_PARAMETER );
517 return NULL;
520 if ((dll = mem_alloc( sizeof(*dll) )))
522 dll->prev = &process->exe;
523 dll->file = NULL;
524 dll->base = base;
525 dll->filename = NULL;
526 dll->namelen = name_len;
527 if (name_len && !(dll->filename = memdup( filename, name_len )))
529 free( dll );
530 return NULL;
532 if (file) dll->file = (struct file *)grab_object( file );
533 if ((dll->next = process->exe.next)) dll->next->prev = dll;
534 process->exe.next = dll;
536 return dll;
539 /* remove a dll from a process list */
540 static void process_unload_dll( struct process *process, void *base )
542 struct process_dll *dll;
544 for (dll = process->exe.next; dll; dll = dll->next)
546 if (dll->base == base)
548 if (dll->file) release_object( dll->file );
549 if (dll->next) dll->next->prev = dll->prev;
550 if (dll->prev) dll->prev->next = dll->next;
551 if (dll->filename) free( dll->filename );
552 free( dll );
553 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
554 return;
557 set_error( STATUS_INVALID_PARAMETER );
560 /* kill all processes */
561 void kill_all_processes( struct process *skip, int exit_code )
563 for (;;)
565 struct process *process = first_process;
567 while (process && (!process->running_threads || process == skip))
568 process = process->next;
569 if (!process) break;
570 kill_process( process, NULL, exit_code );
574 /* kill all processes being attached to a console renderer */
575 void kill_console_processes( struct thread *renderer, int exit_code )
577 for (;;) /* restart from the beginning of the list every time */
579 struct process *process = first_process;
581 /* find the first process being attached to 'renderer' and still running */
582 while (process &&
583 (process == renderer->process || !process->console ||
584 process->console->renderer != renderer || !process->running_threads))
586 process = process->next;
588 if (!process) break;
589 kill_process( process, NULL, exit_code );
593 /* a process has been killed (i.e. its last thread died) */
594 static void process_killed( struct process *process )
596 assert( !process->thread_list );
597 gettimeofday( &process->end_time, NULL );
598 if (process->handles) release_object( process->handles );
599 process->handles = NULL;
601 /* close the console attached to this process, if any */
602 free_console( process );
604 while (process->exe.next)
606 struct process_dll *dll = process->exe.next;
607 process->exe.next = dll->next;
608 if (dll->file) release_object( dll->file );
609 if (dll->filename) free( dll->filename );
610 free( dll );
612 set_process_startup_state( process, STARTUP_ABORTED );
613 if (process->exe.file) release_object( process->exe.file );
614 process->exe.file = NULL;
615 wake_up( &process->obj, 0 );
616 if (!--running_processes) close_master_socket();
619 /* add a thread to a process running threads list */
620 void add_process_thread( struct process *process, struct thread *thread )
622 thread->proc_next = process->thread_list;
623 thread->proc_prev = NULL;
624 if (thread->proc_next) thread->proc_next->proc_prev = thread;
625 process->thread_list = thread;
626 if (!process->running_threads++) running_processes++;
627 grab_object( thread );
630 /* remove a thread from a process running threads list */
631 void remove_process_thread( struct process *process, struct thread *thread )
633 assert( process->running_threads > 0 );
634 assert( process->thread_list );
636 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
637 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
638 else process->thread_list = thread->proc_next;
640 if (!--process->running_threads)
642 /* we have removed the last running thread, exit the process */
643 process->exit_code = thread->exit_code;
644 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
645 process_killed( process );
647 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
648 release_object( thread );
651 /* suspend all the threads of a process */
652 void suspend_process( struct process *process )
654 if (!process->suspend++)
656 struct thread *thread = process->thread_list;
657 while (thread)
659 struct thread *next = thread->proc_next;
660 if (!thread->suspend) stop_thread( thread );
661 thread = next;
666 /* resume all the threads of a process */
667 void resume_process( struct process *process )
669 assert (process->suspend > 0);
670 if (!--process->suspend)
672 struct thread *thread = process->thread_list;
673 while (thread)
675 struct thread *next = thread->proc_next;
676 if (!thread->suspend) wake_thread( thread );
677 thread = next;
682 /* kill a process on the spot */
683 void kill_process( struct process *process, struct thread *skip, int exit_code )
685 struct thread *thread = process->thread_list;
687 while (thread)
689 struct thread *next = thread->proc_next;
690 thread->exit_code = exit_code;
691 if (thread != skip) kill_thread( thread, 1 );
692 thread = next;
696 /* kill all processes being debugged by a given thread */
697 void kill_debugged_processes( struct thread *debugger, int exit_code )
699 for (;;) /* restart from the beginning of the list every time */
701 struct process *process = first_process;
702 /* find the first process being debugged by 'debugger' and still running */
703 while (process && (process->debugger != debugger || !process->running_threads))
704 process = process->next;
705 if (!process) return;
706 process->debugger = NULL;
707 kill_process( process, NULL, exit_code );
712 /* detach a debugger from all its debuggees */
713 void detach_debugged_processes( struct thread *debugger )
715 struct process *process;
716 for (process = first_process; process; process = process->next)
718 if (process->debugger == debugger && process->running_threads)
720 debugger_detach( process, debugger );
726 void enum_processes( int (*cb)(struct process*, void*), void *user )
728 struct process *process;
729 for (process = first_process; process; process = process->next)
731 if ((cb)(process, user)) break;
736 /* get all information about a process */
737 static void get_process_info( struct process *process, struct get_process_info_reply *reply )
739 reply->pid = get_process_id( process );
740 reply->debugged = (process->debugger != 0);
741 reply->exit_code = process->exit_code;
742 reply->priority = process->priority;
743 reply->process_affinity = process->affinity;
744 reply->system_affinity = 1;
747 /* set all information about a process */
748 static void set_process_info( struct process *process,
749 const struct set_process_info_request *req )
751 if (req->mask & SET_PROCESS_INFO_PRIORITY)
752 process->priority = req->priority;
753 if (req->mask & SET_PROCESS_INFO_AFFINITY)
755 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
756 else process->affinity = req->affinity;
760 /* read data from a process memory space */
761 /* len is the total size (in ints) */
762 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
764 struct thread *thread = process->thread_list;
766 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
768 if (!thread) /* process is dead */
770 set_error( STATUS_ACCESS_DENIED );
771 return 0;
773 if (suspend_for_ptrace( thread ))
775 while (len > 0)
777 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
778 len--;
780 resume_after_ptrace( thread );
782 return !len;
785 /* make sure we can write to the whole address range */
786 /* len is the total size (in ints) */
787 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
789 int page = get_page_size() / sizeof(int);
791 for (;;)
793 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
794 if (len <= page) break;
795 addr += page;
796 len -= page;
798 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
801 /* write data to a process memory space */
802 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
803 /* we check the total size for write permissions */
804 static void write_process_memory( struct process *process, int *addr, size_t len,
805 unsigned int first_mask, unsigned int last_mask, const int *src )
807 struct thread *thread = process->thread_list;
809 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
811 if (!thread) /* process is dead */
813 set_error( STATUS_ACCESS_DENIED );
814 return;
816 if (suspend_for_ptrace( thread ))
818 if (!check_process_write_access( thread, addr, len ))
820 set_error( STATUS_ACCESS_DENIED );
821 goto done;
823 /* first word is special */
824 if (len > 1)
826 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
827 len--;
829 else last_mask &= first_mask;
831 while (len > 1)
833 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
834 len--;
837 /* last word is special too */
838 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
840 done:
841 resume_after_ptrace( thread );
845 /* take a snapshot of currently running processes */
846 struct process_snapshot *process_snap( int *count )
848 struct process_snapshot *snapshot, *ptr;
849 struct process *process;
850 if (!running_processes) return NULL;
851 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
852 return NULL;
853 ptr = snapshot;
854 for (process = first_process; process; process = process->next)
856 if (!process->running_threads) continue;
857 ptr->process = process;
858 ptr->threads = process->running_threads;
859 ptr->count = process->obj.refcount;
860 ptr->priority = process->priority;
861 grab_object( process );
862 ptr++;
864 *count = running_processes;
865 return snapshot;
868 /* take a snapshot of the modules of a process */
869 struct module_snapshot *module_snap( struct process *process, int *count )
871 struct module_snapshot *snapshot, *ptr;
872 struct process_dll *dll;
873 int total = 0;
875 for (dll = &process->exe; dll; dll = dll->next) total++;
876 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
878 for (ptr = snapshot, dll = &process->exe; dll; dll = dll->next, ptr++)
880 ptr->base = dll->base;
881 ptr->size = dll->size;
882 ptr->namelen = dll->namelen;
883 ptr->filename = memdup( dll->filename, dll->namelen );
885 *count = total;
886 return snapshot;
890 /* create a new process */
891 DECL_HANDLER(new_process)
893 struct startup_info *info;
895 /* build the startup info for a new process */
896 if (!(info = alloc_object( &startup_info_ops ))) return;
897 list_add_head( &startup_info_list, &info->entry );
898 info->inherit_all = req->inherit_all;
899 info->use_handles = req->use_handles;
900 info->create_flags = req->create_flags;
901 info->unix_pid = req->unix_pid;
902 info->hstdin = req->hstdin;
903 info->hstdout = req->hstdout;
904 info->hstderr = req->hstderr;
905 info->exe_file = NULL;
906 info->owner = (struct thread *)grab_object( current );
907 info->process = NULL;
908 info->thread = NULL;
909 info->data_size = get_req_data_size();
910 info->data = NULL;
912 if (req->exe_file &&
913 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
914 goto done;
916 if (!(info->data = mem_alloc( info->data_size ))) goto done;
917 memcpy( info->data, get_req_data(), info->data_size );
918 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
920 done:
921 release_object( info );
924 /* Retrieve information about a newly started process */
925 DECL_HANDLER(get_new_process_info)
927 struct startup_info *info;
929 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
930 0, &startup_info_ops )))
932 reply->pid = get_process_id( info->process );
933 reply->tid = get_thread_id( info->thread );
934 reply->phandle = alloc_handle( current->process, info->process,
935 PROCESS_ALL_ACCESS, req->pinherit );
936 reply->thandle = alloc_handle( current->process, info->thread,
937 THREAD_ALL_ACCESS, req->tinherit );
938 reply->success = is_process_init_done( info->process );
939 release_object( info );
941 else
943 reply->pid = 0;
944 reply->tid = 0;
945 reply->phandle = 0;
946 reply->thandle = 0;
947 reply->success = 0;
951 /* Retrieve the new process startup info */
952 DECL_HANDLER(get_startup_info)
954 struct startup_info *info;
956 if ((info = current->process->startup_info))
958 size_t size = info->data_size;
959 if (size > get_reply_max_size()) size = get_reply_max_size();
961 /* we return the data directly without making a copy so this can only be called once */
962 set_reply_data_ptr( info->data, size );
963 info->data = NULL;
964 info->data_size = 0;
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" );
975 return;
977 if (current->process->startup_info)
979 fatal_protocol_error( current, "init_process: called twice\n" );
980 return;
982 reply->info_size = 0;
983 current->process->ldt_copy = req->ldt_copy;
984 current->process->startup_info = init_process( reply );
987 /* signal the end of the process initialization */
988 DECL_HANDLER(init_process_done)
990 struct file *file = NULL;
991 struct process *process = current->process;
993 if (is_process_init_done(process))
995 fatal_protocol_error( current, "init_process_done: called twice\n" );
996 return;
998 if (!req->module)
1000 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
1001 return;
1003 process->exe.base = req->module;
1004 process->exe.size = req->module_size;
1005 process->exe.name = req->name;
1007 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
1008 if (process->exe.file) release_object( process->exe.file );
1009 process->exe.file = file;
1011 if ((process->exe.namelen = get_req_data_size()))
1012 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1014 generate_startup_debug_events( process, req->entry );
1015 set_process_startup_state( process, STARTUP_DONE );
1017 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1018 if (current->suspend + process->suspend > 0) stop_thread( current );
1019 reply->debugged = (process->debugger != 0);
1022 /* open a handle to a process */
1023 DECL_HANDLER(open_process)
1025 struct process *process = get_process_from_id( req->pid );
1026 reply->handle = 0;
1027 if (process)
1029 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1030 release_object( process );
1034 /* terminate a process */
1035 DECL_HANDLER(terminate_process)
1037 struct process *process;
1039 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1041 reply->self = (current->process == process);
1042 kill_process( process, current, req->exit_code );
1043 release_object( process );
1047 /* fetch information about a process */
1048 DECL_HANDLER(get_process_info)
1050 struct process *process;
1052 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1054 get_process_info( process, reply );
1055 release_object( process );
1059 /* set information about a process */
1060 DECL_HANDLER(set_process_info)
1062 struct process *process;
1064 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1066 set_process_info( process, req );
1067 release_object( process );
1071 /* read data from a process address space */
1072 DECL_HANDLER(read_process_memory)
1074 struct process *process;
1075 size_t len = get_reply_max_size();
1077 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1079 if (len)
1081 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1082 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1083 const int *start = (int *)((char *)req->addr - start_offset);
1084 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1085 if (buffer)
1087 if (read_process_memory( process, start, nb_ints, buffer ))
1089 /* move start of requested data to start of buffer */
1090 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1091 set_reply_data_ptr( buffer, len );
1093 else len = 0;
1096 release_object( process );
1099 /* write data to a process address space */
1100 DECL_HANDLER(write_process_memory)
1102 struct process *process;
1104 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1106 size_t len = get_req_data_size();
1107 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1108 set_error( STATUS_INVALID_PARAMETER );
1109 else
1111 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1112 req->first_mask, req->last_mask, get_req_data() );
1114 release_object( process );
1118 /* notify the server that a dll has been loaded */
1119 DECL_HANDLER(load_dll)
1121 struct process_dll *dll;
1122 struct file *file = NULL;
1124 if (req->handle &&
1125 !(file = get_file_obj( current->process, req->handle, GENERIC_READ ))) return;
1127 if ((dll = process_load_dll( current->process, file, req->base,
1128 get_req_data(), get_req_data_size() )))
1130 dll->size = req->size;
1131 dll->dbg_offset = req->dbg_offset;
1132 dll->dbg_size = req->dbg_size;
1133 dll->name = req->name;
1134 /* only generate event if initialization is done */
1135 if (is_process_init_done( current->process ))
1136 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1138 if (file) release_object( file );
1141 /* notify the server that a dll is being unloaded */
1142 DECL_HANDLER(unload_dll)
1144 process_unload_dll( current->process, req->base );
1147 /* retrieve information about a module in a process */
1148 DECL_HANDLER(get_dll_info)
1150 struct process *process;
1152 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1154 struct process_dll *dll;
1156 for (dll = &process->exe; dll; dll = dll->next)
1158 if (dll->base == req->base_address)
1160 reply->size = dll->size;
1161 reply->entry_point = 0; /* FIXME */
1162 if (dll->filename)
1164 size_t len = min( dll->namelen, get_reply_max_size() );
1165 set_reply_data( dll->filename, len );
1167 break;
1170 if (!dll)
1171 set_error(STATUS_DLL_NOT_FOUND);
1172 release_object( process );
1176 /* wait for a process to start waiting on input */
1177 /* FIXME: only returns event for now, wait is done in the client */
1178 DECL_HANDLER(wait_input_idle)
1180 struct process *process;
1182 reply->event = 0;
1183 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1185 if (process->idle_event && process != current->process && process->queue != current->queue)
1186 reply->event = alloc_handle( current->process, process->idle_event,
1187 EVENT_ALL_ACCESS, 0 );
1188 release_object( process );