Fix _setmbcp behavior for unreal codepages.
[wine/multimedia.git] / server / process.c
blob863c41588c826db5e26b5c0233790c94ebc08df3
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>
36 #ifdef HAVE_POLL_H
37 #include <poll.h>
38 #endif
40 #include "windef.h"
41 #include "winnt.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "process.h"
46 #include "thread.h"
47 #include "request.h"
48 #include "console.h"
49 #include "user.h"
50 #include "security.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 */
82 no_flush, /* flush */
83 no_get_file_info, /* get_file_info */
84 no_queue_async, /* queue_async */
85 no_cancel_async /* cancel async */
88 /* process startup info */
90 struct 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 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);
129 struct ptid_entry
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;
147 unsigned int id;
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)
156 id = 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 );
167 return 0;
169 ptid_entries = entry;
170 alloc_ptid_entries = count;
171 id = used_ptid_entries + PTID_OFFSET;
172 entry = &ptid_entries[used_ptid_entries++];
175 entry->ptr = ptr;
176 return id;
179 /* free a process or thread id */
180 void free_ptid( unsigned int id )
182 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
184 entry->ptr = NULL;
185 entry->next = 0;
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;
191 last_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 /* set the console and stdio handles for a newly created process */
223 static int set_process_console( struct process *process, struct thread *parent_thread,
224 struct startup_info *info, struct init_process_reply *reply )
226 if (info)
228 if (!(process->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
230 /* FIXME: some better error checking should be done...
231 * like if hConOut and hConIn are console handles, then they should be on the same
232 * physical console
234 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
236 if (!info->inherit_all && !(process->create_flags & CREATE_NEW_CONSOLE))
238 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
239 0, TRUE, DUPLICATE_SAME_ACCESS );
240 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
241 0, TRUE, DUPLICATE_SAME_ACCESS );
242 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
243 0, TRUE, DUPLICATE_SAME_ACCESS );
245 else
247 reply->hstdin = info->hstdin;
248 reply->hstdout = info->hstdout;
249 reply->hstderr = info->hstderr;
252 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
253 /* some handles above may have been invalid; this is not an error */
254 if (get_error() == STATUS_INVALID_HANDLE ||
255 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
256 return 1;
259 /* create a new process and its main thread */
260 struct thread *create_process( int fd )
262 struct process *process;
263 struct thread *thread = NULL;
264 int request_pipe[2];
266 if (!(process = alloc_object( &process_ops ))) goto error;
267 process->parent = NULL;
268 process->debugger = NULL;
269 process->handles = NULL;
270 process->msg_fd = NULL;
271 process->exit_code = STILL_ACTIVE;
272 process->running_threads = 0;
273 process->priority = NORMAL_PRIORITY_CLASS;
274 process->affinity = 1;
275 process->suspend = 0;
276 process->create_flags = 0;
277 process->console = NULL;
278 process->startup_state = STARTUP_IN_PROGRESS;
279 process->startup_info = NULL;
280 process->idle_event = NULL;
281 process->queue = NULL;
282 process->peb = NULL;
283 process->ldt_copy = NULL;
284 process->winstation = 0;
285 process->desktop = 0;
286 process->exe.file = NULL;
287 process->exe.dbg_offset = 0;
288 process->exe.dbg_size = 0;
289 process->exe.namelen = 0;
290 process->exe.filename = NULL;
291 process->group_id = 0;
292 process->token = token_create_admin();
293 list_init( &process->thread_list );
294 list_init( &process->locks );
295 list_init( &process->classes );
296 list_init( &process->dlls );
298 gettimeofday( &process->start_time, NULL );
299 list_add_head( &process_list, &process->entry );
301 if (!(process->id = alloc_ptid( process ))) goto error;
302 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
304 /* create the main thread */
305 if (pipe( request_pipe ) == -1)
307 file_set_error();
308 goto error;
310 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
312 close( request_pipe[0] );
313 close( request_pipe[1] );
314 goto error;
316 close( request_pipe[1] );
317 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
319 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
320 release_object( process );
321 return thread;
323 error:
324 if (process) release_object( process );
325 /* if we failed to start our first process, close everything down */
326 if (!running_processes) close_master_socket();
327 return NULL;
330 /* find the startup info for a given Unix process */
331 inline static struct startup_info *find_startup_info( int unix_pid )
333 struct list *ptr;
335 LIST_FOR_EACH( ptr, &startup_info_list )
337 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
338 if (info->unix_pid == unix_pid) return info;
340 return NULL;
343 /* initialize the current process and fill in the request */
344 static struct startup_info *init_process( struct init_process_reply *reply )
346 struct process *process = current->process;
347 struct thread *parent_thread = NULL;
348 struct process *parent = NULL;
349 struct startup_info *info = find_startup_info( current->unix_pid );
351 if (info)
353 if (info->thread)
355 fatal_protocol_error( current, "init_process: called twice?\n" );
356 return NULL;
358 parent_thread = info->owner;
359 parent = parent_thread->process;
360 process->parent = (struct process *)grab_object( parent );
363 /* set the process flags */
364 process->create_flags = info ? info->create_flags : 0;
366 /* create the handle table */
367 if (info && info->inherit_all)
368 process->handles = copy_handle_table( process, parent );
369 else
370 process->handles = alloc_handle_table( process, 0 );
371 if (!process->handles) return NULL;
373 /* retrieve the main exe file */
374 reply->exe_file = 0;
375 if (info && info->exe_file)
377 process->exe.file = (struct file *)grab_object( info->exe_file );
378 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
379 return NULL;
382 /* connect to the window station and desktop */
383 connect_process_winstation( process, NULL, 0 );
384 connect_process_desktop( process, NULL, 0 );
385 current->desktop = process->desktop;
387 /* set the process console */
388 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
390 process->group_id = get_process_id( process );
391 if (parent)
393 /* attach to the debugger if requested */
394 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
395 set_process_debugger( process, parent_thread );
396 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
397 set_process_debugger( process, parent->debugger );
398 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
399 process->group_id = parent->group_id;
402 /* thread will be actually suspended in init_done */
403 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
405 if (info)
407 reply->info_size = info->data_size;
408 info->process = (struct process *)grab_object( process );
409 info->thread = (struct thread *)grab_object( current );
411 reply->create_flags = process->create_flags;
412 reply->server_start = server_start_ticks;
413 return info ? (struct startup_info *)grab_object( info ) : NULL;
416 /* destroy a process when its refcount is 0 */
417 static void process_destroy( struct object *obj )
419 struct process *process = (struct process *)obj;
420 assert( obj->ops == &process_ops );
422 /* we can't have a thread remaining */
423 assert( list_empty( &process->thread_list ));
425 set_process_startup_state( process, STARTUP_ABORTED );
426 if (process->console) release_object( process->console );
427 if (process->parent) release_object( process->parent );
428 if (process->msg_fd) release_object( process->msg_fd );
429 list_remove( &process->entry );
430 if (process->idle_event) release_object( process->idle_event );
431 if (process->queue) release_object( process->queue );
432 if (process->exe.file) release_object( process->exe.file );
433 if (process->exe.filename) free( process->exe.filename );
434 if (process->id) free_ptid( process->id );
435 if (process->token) release_object( process->token );
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 handles=%p\n", process->id, process->handles );
447 static int process_signaled( struct object *obj, struct thread *thread )
449 struct process *process = (struct process *)obj;
450 return !process->running_threads;
453 static void process_poll_event( struct fd *fd, int event )
455 struct process *process = get_fd_user( fd );
456 assert( process->obj.ops == &process_ops );
458 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
459 else if (event & POLLIN) receive_fd( process );
462 static void startup_info_destroy( struct object *obj )
464 struct startup_info *info = (struct startup_info *)obj;
465 assert( obj->ops == &startup_info_ops );
466 list_remove( &info->entry );
467 if (info->data) free( info->data );
468 if (info->exe_file) release_object( info->exe_file );
469 if (info->process) release_object( info->process );
470 if (info->thread) release_object( info->thread );
471 if (info->owner) release_object( info->owner );
474 static void startup_info_dump( struct object *obj, int verbose )
476 struct startup_info *info = (struct startup_info *)obj;
477 assert( obj->ops == &startup_info_ops );
479 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
480 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
483 static int startup_info_signaled( struct object *obj, struct thread *thread )
485 struct startup_info *info = (struct startup_info *)obj;
486 return info->process && is_process_init_done(info->process);
489 /* get a process from an id (and increment the refcount) */
490 struct process *get_process_from_id( process_id_t id )
492 struct object *obj = get_ptid_entry( id );
494 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
495 set_error( STATUS_INVALID_PARAMETER );
496 return NULL;
499 /* get a process from a handle (and increment the refcount) */
500 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
502 return (struct process *)get_handle_obj( current->process, handle,
503 access, &process_ops );
506 /* find a dll from its base address */
507 static inline struct process_dll *find_process_dll( struct process *process, void *base )
509 struct process_dll *dll;
511 if (process->exe.base == base) return &process->exe;
513 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
515 if (dll->base == base) return dll;
517 return NULL;
520 /* add a dll to a process list */
521 static struct process_dll *process_load_dll( struct process *process, struct file *file,
522 void *base, const WCHAR *filename, size_t name_len )
524 struct process_dll *dll;
526 /* make sure we don't already have one with the same base address */
527 if (find_process_dll( process, base ))
529 set_error( STATUS_INVALID_PARAMETER );
530 return NULL;
533 if ((dll = mem_alloc( sizeof(*dll) )))
535 dll->file = NULL;
536 dll->base = base;
537 dll->filename = NULL;
538 dll->namelen = name_len;
539 if (name_len && !(dll->filename = memdup( filename, name_len )))
541 free( dll );
542 return NULL;
544 if (file) dll->file = (struct file *)grab_object( file );
545 list_add_head( &process->dlls, &dll->entry );
547 return dll;
550 /* remove a dll from a process list */
551 static void process_unload_dll( struct process *process, void *base )
553 struct process_dll *dll = find_process_dll( process, base );
555 if (dll && dll != &process->exe)
557 if (dll->file) release_object( dll->file );
558 if (dll->filename) free( dll->filename );
559 list_remove( &dll->entry );
560 free( dll );
561 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
563 else set_error( STATUS_INVALID_PARAMETER );
566 /* kill all processes */
567 void kill_all_processes( struct process *skip, int exit_code )
569 for (;;)
571 struct process *process;
573 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
575 if (process == skip) continue;
576 if (process->running_threads) break;
578 if (&process->entry == &process_list) break; /* no process found */
579 kill_process( process, NULL, exit_code );
583 /* kill all processes being attached to a console renderer */
584 void kill_console_processes( struct thread *renderer, int exit_code )
586 for (;;) /* restart from the beginning of the list every time */
588 struct process *process;
590 /* find the first process being attached to 'renderer' and still running */
591 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
593 if (process == renderer->process) continue;
594 if (!process->running_threads) continue;
595 if (process->console && process->console->renderer == renderer) break;
597 if (&process->entry == &process_list) break; /* no process found */
598 kill_process( process, NULL, exit_code );
602 /* a process has been killed (i.e. its last thread died) */
603 static void process_killed( struct process *process )
605 struct handle_table *handles;
606 struct list *ptr;
608 assert( list_empty( &process->thread_list ));
609 gettimeofday( &process->end_time, NULL );
610 handles = process->handles;
611 process->handles = NULL;
612 if (handles) release_object( handles );
614 /* close the console attached to this process, if any */
615 free_console( process );
617 while ((ptr = list_head( &process->dlls )))
619 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
620 if (dll->file) release_object( dll->file );
621 if (dll->filename) free( dll->filename );
622 list_remove( &dll->entry );
623 free( dll );
625 destroy_process_classes( process );
626 remove_process_locks( process );
627 set_process_startup_state( process, STARTUP_ABORTED );
628 if (process->exe.file) release_object( process->exe.file );
629 process->exe.file = NULL;
630 wake_up( &process->obj, 0 );
631 if (!--running_processes) close_master_socket();
634 /* add a thread to a process running threads list */
635 void add_process_thread( struct process *process, struct thread *thread )
637 list_add_head( &process->thread_list, &thread->proc_entry );
638 if (!process->running_threads++) running_processes++;
639 grab_object( thread );
642 /* remove a thread from a process running threads list */
643 void remove_process_thread( struct process *process, struct thread *thread )
645 assert( process->running_threads > 0 );
646 assert( !list_empty( &process->thread_list ));
648 list_remove( &thread->proc_entry );
650 if (!--process->running_threads)
652 /* we have removed the last running thread, exit the process */
653 process->exit_code = thread->exit_code;
654 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
655 process_killed( process );
657 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
658 release_object( thread );
661 /* suspend all the threads of a process */
662 void suspend_process( struct process *process )
664 if (!process->suspend++)
666 struct list *ptr, *next;
668 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
670 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
671 if (!thread->suspend) stop_thread( thread );
676 /* resume all the threads of a process */
677 void resume_process( struct process *process )
679 assert (process->suspend > 0);
680 if (!--process->suspend)
682 struct list *ptr, *next;
684 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
686 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
687 if (!thread->suspend) wake_thread( thread );
692 /* kill a process on the spot */
693 void kill_process( struct process *process, struct thread *skip, int exit_code )
695 struct list *ptr, *next;
697 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
699 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
701 thread->exit_code = exit_code;
702 if (thread != skip) kill_thread( thread, 1 );
706 /* kill all processes being debugged by a given thread */
707 void kill_debugged_processes( struct thread *debugger, int exit_code )
709 for (;;) /* restart from the beginning of the list every time */
711 struct process *process;
713 /* find the first process being debugged by 'debugger' and still running */
714 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
716 if (!process->running_threads) continue;
717 if (process->debugger == debugger) break;
719 if (&process->entry == &process_list) break; /* no process found */
720 process->debugger = NULL;
721 kill_process( process, NULL, exit_code );
726 /* detach a debugger from all its debuggees */
727 void detach_debugged_processes( struct thread *debugger )
729 struct process *process;
731 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
733 if (process->debugger == debugger && process->running_threads)
735 debugger_detach( process, debugger );
741 void enum_processes( int (*cb)(struct process*, void*), void *user )
743 struct list *ptr, *next;
745 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
747 struct process *process = LIST_ENTRY( ptr, struct process, entry );
748 if ((cb)(process, user)) break;
753 /* read data from a process memory space */
754 /* len is the total size (in ints) */
755 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
757 struct thread *thread = get_process_first_thread( process );
759 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
761 if (!thread) /* process is dead */
763 set_error( STATUS_ACCESS_DENIED );
764 return 0;
766 if (suspend_for_ptrace( thread ))
768 while (len > 0)
770 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
771 len--;
773 resume_after_ptrace( thread );
775 return !len;
778 /* make sure we can write to the whole address range */
779 /* len is the total size (in ints) */
780 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
782 int page = get_page_size() / sizeof(int);
784 for (;;)
786 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
787 if (len <= page) break;
788 addr += page;
789 len -= page;
791 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
794 /* write data to a process memory space */
795 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
796 /* we check the total size for write permissions */
797 static int write_process_memory( struct process *process, int *addr, size_t len,
798 unsigned int first_mask, unsigned int last_mask, const int *src )
800 struct thread *thread = get_process_first_thread( process );
801 int ret = 0;
803 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
805 if (!thread) /* process is dead */
807 set_error( STATUS_ACCESS_DENIED );
808 return 0;
810 if (suspend_for_ptrace( thread ))
812 if (!check_process_write_access( thread, addr, len ))
814 set_error( STATUS_ACCESS_DENIED );
815 goto done;
817 /* first word is special */
818 if (len > 1)
820 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
821 len--;
823 else last_mask &= first_mask;
825 while (len > 1)
827 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
828 len--;
831 /* last word is special too */
832 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
833 ret = 1;
835 done:
836 resume_after_ptrace( thread );
838 return ret;
841 /* set the debugged flag in the process PEB */
842 int set_process_debug_flag( struct process *process, int flag )
844 int mask = 0, data = 0;
846 /* BeingDebugged flag is the byte at offset 2 in the PEB */
847 memset( (char *)&mask + 2, 0xff, 1 );
848 memset( (char *)&data + 2, flag, 1 );
849 return write_process_memory( process, process->peb, 1, mask, mask, &data );
852 /* take a snapshot of currently running processes */
853 struct process_snapshot *process_snap( int *count )
855 struct process_snapshot *snapshot, *ptr;
856 struct process *process;
857 if (!running_processes) return NULL;
858 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
859 return NULL;
860 ptr = snapshot;
861 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
863 if (!process->running_threads) continue;
864 ptr->process = process;
865 ptr->threads = process->running_threads;
866 ptr->count = process->obj.refcount;
867 ptr->priority = process->priority;
868 ptr->handles = get_handle_table_count(process);
869 grab_object( process );
870 ptr++;
872 *count = running_processes;
873 return snapshot;
876 /* take a snapshot of the modules of a process */
877 struct module_snapshot *module_snap( struct process *process, int *count )
879 struct module_snapshot *snapshot, *ptr;
880 struct process_dll *dll;
881 int total = 1;
883 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
884 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
886 /* first entry is main exe */
887 snapshot->base = process->exe.base;
888 snapshot->size = process->exe.size;
889 snapshot->namelen = process->exe.namelen;
890 snapshot->filename = memdup( process->exe.filename, process->exe.namelen );
891 ptr = snapshot + 1;
893 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
895 ptr->base = dll->base;
896 ptr->size = dll->size;
897 ptr->namelen = dll->namelen;
898 ptr->filename = memdup( dll->filename, dll->namelen );
899 ptr++;
901 *count = total;
902 return snapshot;
906 /* create a new process */
907 DECL_HANDLER(new_process)
909 struct startup_info *info;
911 /* build the startup info for a new process */
912 if (!(info = alloc_object( &startup_info_ops ))) return;
913 list_add_head( &startup_info_list, &info->entry );
914 info->inherit_all = req->inherit_all;
915 info->create_flags = req->create_flags;
916 info->unix_pid = req->unix_pid;
917 info->hstdin = req->hstdin;
918 info->hstdout = req->hstdout;
919 info->hstderr = req->hstderr;
920 info->exe_file = NULL;
921 info->owner = (struct thread *)grab_object( current );
922 info->process = NULL;
923 info->thread = NULL;
924 info->data_size = get_req_data_size();
925 info->data = NULL;
927 if (req->exe_file &&
928 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
929 goto done;
931 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
932 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
934 done:
935 release_object( info );
938 /* Retrieve information about a newly started process */
939 DECL_HANDLER(get_new_process_info)
941 struct startup_info *info;
943 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
944 0, &startup_info_ops )))
946 reply->pid = get_process_id( info->process );
947 reply->tid = get_thread_id( info->thread );
948 reply->phandle = alloc_handle( current->process, info->process,
949 PROCESS_ALL_ACCESS, req->pinherit );
950 reply->thandle = alloc_handle( current->process, info->thread,
951 THREAD_ALL_ACCESS, req->tinherit );
952 reply->success = is_process_init_done( info->process );
953 release_object( info );
955 else
957 reply->pid = 0;
958 reply->tid = 0;
959 reply->phandle = 0;
960 reply->thandle = 0;
961 reply->success = 0;
965 /* Retrieve the new process startup info */
966 DECL_HANDLER(get_startup_info)
968 struct startup_info *info;
970 if ((info = current->process->startup_info))
972 size_t size = info->data_size;
973 if (size > get_reply_max_size()) size = get_reply_max_size();
975 /* we return the data directly without making a copy so this can only be called once */
976 set_reply_data_ptr( info->data, size );
977 info->data = NULL;
978 info->data_size = 0;
983 /* initialize a new process */
984 DECL_HANDLER(init_process)
986 if (current->unix_pid == -1)
988 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
989 return;
991 if (current->process->startup_info)
993 fatal_protocol_error( current, "init_process: called twice\n" );
994 return;
996 if (!req->peb || (unsigned int)req->peb % sizeof(int))
998 fatal_protocol_error( current, "init_process: bad peb address\n" );
999 return;
1001 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
1003 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
1004 return;
1006 reply->info_size = 0;
1007 current->process->peb = req->peb;
1008 current->process->ldt_copy = req->ldt_copy;
1009 current->process->startup_info = init_process( reply );
1012 /* signal the end of the process initialization */
1013 DECL_HANDLER(init_process_done)
1015 struct file *file = NULL;
1016 struct process *process = current->process;
1018 if (is_process_init_done(process))
1020 fatal_protocol_error( current, "init_process_done: called twice\n" );
1021 return;
1023 if (!req->module)
1025 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
1026 return;
1028 process->exe.base = req->module;
1029 process->exe.size = req->module_size;
1030 process->exe.name = req->name;
1032 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
1033 if (process->exe.file) release_object( process->exe.file );
1034 process->exe.file = file;
1036 if ((process->exe.namelen = get_req_data_size()))
1037 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1039 generate_startup_debug_events( process, req->entry );
1040 set_process_startup_state( process, STARTUP_DONE );
1042 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1043 if (current->suspend + process->suspend > 0) stop_thread( current );
1044 if (process->debugger) set_process_debug_flag( process, 1 );
1047 /* open a handle to a process */
1048 DECL_HANDLER(open_process)
1050 struct process *process = get_process_from_id( req->pid );
1051 reply->handle = 0;
1052 if (process)
1054 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1055 release_object( process );
1059 /* terminate a process */
1060 DECL_HANDLER(terminate_process)
1062 struct process *process;
1064 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1066 reply->self = (current->process == process);
1067 kill_process( process, current, req->exit_code );
1068 release_object( process );
1072 /* fetch information about a process */
1073 DECL_HANDLER(get_process_info)
1075 struct process *process;
1077 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1079 reply->pid = get_process_id( process );
1080 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1081 reply->exit_code = process->exit_code;
1082 reply->priority = process->priority;
1083 reply->process_affinity = process->affinity;
1084 reply->system_affinity = 1;
1085 reply->peb = process->peb;
1086 release_object( process );
1090 /* set information about a process */
1091 DECL_HANDLER(set_process_info)
1093 struct process *process;
1095 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1097 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1098 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1100 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1101 else process->affinity = req->affinity;
1103 release_object( process );
1107 /* read data from a process address space */
1108 DECL_HANDLER(read_process_memory)
1110 struct process *process;
1111 size_t len = get_reply_max_size();
1113 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1115 if (len)
1117 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1118 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1119 const int *start = (int *)((char *)req->addr - start_offset);
1120 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1121 if (buffer)
1123 if (read_process_memory( process, start, nb_ints, buffer ))
1125 /* move start of requested data to start of buffer */
1126 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1127 set_reply_data_ptr( buffer, len );
1129 else len = 0;
1132 release_object( process );
1135 /* write data to a process address space */
1136 DECL_HANDLER(write_process_memory)
1138 struct process *process;
1140 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1142 size_t len = get_req_data_size();
1143 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1144 set_error( STATUS_INVALID_PARAMETER );
1145 else
1147 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1148 req->first_mask, req->last_mask, get_req_data() );
1150 release_object( process );
1154 /* notify the server that a dll has been loaded */
1155 DECL_HANDLER(load_dll)
1157 struct process_dll *dll;
1158 struct file *file = NULL;
1160 if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1161 return;
1163 if ((dll = process_load_dll( current->process, file, req->base,
1164 get_req_data(), get_req_data_size() )))
1166 dll->size = req->size;
1167 dll->dbg_offset = req->dbg_offset;
1168 dll->dbg_size = req->dbg_size;
1169 dll->name = req->name;
1170 /* only generate event if initialization is done */
1171 if (is_process_init_done( current->process ))
1172 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1174 if (file) release_object( file );
1177 /* notify the server that a dll is being unloaded */
1178 DECL_HANDLER(unload_dll)
1180 process_unload_dll( current->process, req->base );
1183 /* retrieve information about a module in a process */
1184 DECL_HANDLER(get_dll_info)
1186 struct process *process;
1188 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1190 struct process_dll *dll = find_process_dll( process, req->base_address );
1192 if (dll)
1194 reply->size = dll->size;
1195 reply->entry_point = NULL; /* FIXME */
1196 if (dll->filename)
1198 size_t len = min( dll->namelen, get_reply_max_size() );
1199 set_reply_data( dll->filename, len );
1202 else
1203 set_error( STATUS_DLL_NOT_FOUND );
1205 release_object( process );
1209 /* wait for a process to start waiting on input */
1210 /* FIXME: only returns event for now, wait is done in the client */
1211 DECL_HANDLER(wait_input_idle)
1213 struct process *process;
1215 reply->event = 0;
1216 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1218 if (process->idle_event && process != current->process && process->queue != current->queue)
1219 reply->event = alloc_handle( current->process, process->idle_event,
1220 EVENT_ALL_ACCESS, 0 );
1221 release_object( process );