Check always if destination surface is locked.
[wine/multimedia.git] / server / process.c
blobc89fa1ce87cf15006a97e4c810e38655291228e8
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 "winbase.h"
42 #include "winnt.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "console.h"
50 #include "user.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_get_fd, /* get_fd */
73 process_destroy /* destroy */
76 static const struct fd_ops process_fd_ops =
78 NULL, /* get_poll_events */
79 process_poll_event, /* poll_event */
80 no_flush, /* flush */
81 no_get_file_info, /* get_file_info */
82 no_queue_async, /* queue_async */
83 no_cancel_async /* cancel async */
86 /* process startup info */
88 struct startup_info
90 struct object obj; /* object header */
91 struct list entry; /* entry in list of startup infos */
92 int inherit_all; /* inherit all handles from parent */
93 int create_flags; /* creation flags */
94 int unix_pid; /* Unix pid of new process */
95 obj_handle_t hstdin; /* handle for stdin */
96 obj_handle_t hstdout; /* handle for stdout */
97 obj_handle_t hstderr; /* handle for stderr */
98 struct file *exe_file; /* file handle for main exe */
99 struct thread *owner; /* owner thread (the one that created the new process) */
100 struct process *process; /* created process */
101 struct thread *thread; /* created thread */
102 size_t data_size; /* size of startup data */
103 void *data; /* data for startup info */
106 static void startup_info_dump( struct object *obj, int verbose );
107 static int startup_info_signaled( struct object *obj, struct thread *thread );
108 static void startup_info_destroy( struct object *obj );
110 static const struct object_ops startup_info_ops =
112 sizeof(struct startup_info), /* size */
113 startup_info_dump, /* dump */
114 add_queue, /* add_queue */
115 remove_queue, /* remove_queue */
116 startup_info_signaled, /* signaled */
117 no_satisfied, /* satisfied */
118 no_get_fd, /* get_fd */
119 startup_info_destroy /* destroy */
123 static struct list startup_info_list = LIST_INIT(startup_info_list);
125 struct ptid_entry
127 void *ptr; /* entry ptr */
128 unsigned int next; /* next free entry */
131 static struct ptid_entry *ptid_entries; /* array of ptid entries */
132 static unsigned int used_ptid_entries; /* number of entries in use */
133 static unsigned int alloc_ptid_entries; /* number of allocated entries */
134 static unsigned int next_free_ptid; /* next free entry */
135 static unsigned int last_free_ptid; /* last free entry */
137 #define PTID_OFFSET 8 /* offset for first ptid value */
139 /* allocate a new process or thread id */
140 unsigned int alloc_ptid( void *ptr )
142 struct ptid_entry *entry;
143 unsigned int id;
145 if (used_ptid_entries < alloc_ptid_entries)
147 id = used_ptid_entries + PTID_OFFSET;
148 entry = &ptid_entries[used_ptid_entries++];
150 else if (next_free_ptid)
152 id = next_free_ptid;
153 entry = &ptid_entries[id - PTID_OFFSET];
154 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
156 else /* need to grow the array */
158 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
159 if (!count) count = 64;
160 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
162 set_error( STATUS_NO_MEMORY );
163 return 0;
165 ptid_entries = entry;
166 alloc_ptid_entries = count;
167 id = used_ptid_entries + PTID_OFFSET;
168 entry = &ptid_entries[used_ptid_entries++];
171 entry->ptr = ptr;
172 return id;
175 /* free a process or thread id */
176 void free_ptid( unsigned int id )
178 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
180 entry->ptr = NULL;
181 entry->next = 0;
183 /* append to end of free list so that we don't reuse it too early */
184 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
185 else next_free_ptid = id;
187 last_free_ptid = id;
190 /* retrieve the pointer corresponding to a process or thread id */
191 void *get_ptid_entry( unsigned int id )
193 if (id < PTID_OFFSET) return NULL;
194 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
195 return ptid_entries[id - PTID_OFFSET].ptr;
198 /* return the main thread of the process */
199 struct thread *get_process_first_thread( struct process *process )
201 struct list *ptr = list_head( &process->thread_list );
202 if (!ptr) return NULL;
203 return LIST_ENTRY( ptr, struct thread, proc_entry );
206 /* set the state of the process startup info */
207 static void set_process_startup_state( struct process *process, enum startup_state state )
209 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
210 if (process->startup_info)
212 wake_up( &process->startup_info->obj, 0 );
213 release_object( process->startup_info );
214 process->startup_info = NULL;
218 /* set the console and stdio handles for a newly created process */
219 static int set_process_console( struct process *process, struct thread *parent_thread,
220 struct startup_info *info, struct init_process_reply *reply )
222 if (info)
224 if (!(process->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
226 /* FIXME: some better error checking should be done...
227 * like if hConOut and hConIn are console handles, then they should be on the same
228 * physical console
230 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
232 if (!info->inherit_all && !(process->create_flags & CREATE_NEW_CONSOLE))
234 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
235 0, TRUE, DUPLICATE_SAME_ACCESS );
236 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
237 0, TRUE, DUPLICATE_SAME_ACCESS );
238 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
239 0, TRUE, DUPLICATE_SAME_ACCESS );
241 else
243 reply->hstdin = info->hstdin;
244 reply->hstdout = info->hstdout;
245 reply->hstderr = info->hstderr;
248 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
249 /* some handles above may have been invalid; this is not an error */
250 if (get_error() == STATUS_INVALID_HANDLE ||
251 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
252 return 1;
255 /* create a new process and its main thread */
256 struct thread *create_process( int fd )
258 struct process *process;
259 struct thread *thread = NULL;
260 int request_pipe[2];
262 if (!(process = alloc_object( &process_ops ))) goto error;
263 process->parent = NULL;
264 process->debugger = NULL;
265 process->handles = NULL;
266 process->msg_fd = NULL;
267 process->exit_code = STILL_ACTIVE;
268 process->running_threads = 0;
269 process->priority = NORMAL_PRIORITY_CLASS;
270 process->affinity = 1;
271 process->suspend = 0;
272 process->create_flags = 0;
273 process->console = NULL;
274 process->startup_state = STARTUP_IN_PROGRESS;
275 process->startup_info = NULL;
276 process->idle_event = NULL;
277 process->queue = NULL;
278 process->atom_table = NULL;
279 process->peb = NULL;
280 process->ldt_copy = NULL;
281 process->exe.file = NULL;
282 process->exe.dbg_offset = 0;
283 process->exe.dbg_size = 0;
284 process->exe.namelen = 0;
285 process->exe.filename = NULL;
286 process->group_id = 0;
287 process->token = create_admin_token();
288 list_init( &process->thread_list );
289 list_init( &process->locks );
290 list_init( &process->classes );
291 list_init( &process->dlls );
293 gettimeofday( &process->start_time, NULL );
294 list_add_head( &process_list, &process->entry );
296 if (!(process->id = alloc_ptid( process ))) goto error;
297 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
299 /* create the main thread */
300 if (pipe( request_pipe ) == -1)
302 file_set_error();
303 goto error;
305 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
307 close( request_pipe[0] );
308 close( request_pipe[1] );
309 goto error;
311 close( request_pipe[1] );
312 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
314 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
315 release_object( process );
316 return thread;
318 error:
319 if (process) release_object( process );
320 /* if we failed to start our first process, close everything down */
321 if (!running_processes) close_master_socket();
322 return NULL;
325 /* find the startup info for a given Unix process */
326 inline static struct startup_info *find_startup_info( int unix_pid )
328 struct list *ptr;
330 LIST_FOR_EACH( ptr, &startup_info_list )
332 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
333 if (info->unix_pid == unix_pid) return info;
335 return NULL;
338 /* initialize the current process and fill in the request */
339 static struct startup_info *init_process( struct init_process_reply *reply )
341 struct process *process = current->process;
342 struct thread *parent_thread = NULL;
343 struct process *parent = NULL;
344 struct startup_info *info = find_startup_info( current->unix_pid );
346 if (info)
348 if (info->thread)
350 fatal_protocol_error( current, "init_process: called twice?\n" );
351 return NULL;
353 parent_thread = info->owner;
354 parent = parent_thread->process;
355 process->parent = (struct process *)grab_object( parent );
358 /* set the process flags */
359 process->create_flags = info ? info->create_flags : 0;
361 /* create the handle table */
362 if (info && info->inherit_all)
363 process->handles = copy_handle_table( process, parent );
364 else
365 process->handles = alloc_handle_table( process, 0 );
366 if (!process->handles) return NULL;
368 /* retrieve the main exe file */
369 reply->exe_file = 0;
370 if (info && info->exe_file)
372 process->exe.file = (struct file *)grab_object( info->exe_file );
373 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
374 return NULL;
377 /* set the process console */
378 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
380 process->group_id = get_process_id( process );
381 if (parent)
383 /* attach to the debugger if requested */
384 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
385 set_process_debugger( process, parent_thread );
386 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
387 set_process_debugger( process, parent->debugger );
388 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
389 process->group_id = parent->group_id;
392 /* thread will be actually suspended in init_done */
393 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
395 if (info)
397 reply->info_size = info->data_size;
398 info->process = (struct process *)grab_object( process );
399 info->thread = (struct thread *)grab_object( current );
401 reply->create_flags = process->create_flags;
402 reply->server_start = server_start_ticks;
403 return info ? (struct startup_info *)grab_object( info ) : NULL;
406 /* destroy a process when its refcount is 0 */
407 static void process_destroy( struct object *obj )
409 struct process *process = (struct process *)obj;
410 assert( obj->ops == &process_ops );
412 /* we can't have a thread remaining */
413 assert( list_empty( &process->thread_list ));
415 set_process_startup_state( process, STARTUP_ABORTED );
416 if (process->console) release_object( process->console );
417 if (process->parent) release_object( process->parent );
418 if (process->msg_fd) release_object( process->msg_fd );
419 list_remove( &process->entry );
420 if (process->idle_event) release_object( process->idle_event );
421 if (process->queue) release_object( process->queue );
422 if (process->atom_table) release_object( process->atom_table );
423 if (process->exe.file) release_object( process->exe.file );
424 if (process->exe.filename) free( process->exe.filename );
425 if (process->id) free_ptid( process->id );
426 if (process->token) release_object( process->token );
429 /* dump a process on stdout for debugging purposes */
430 static void process_dump( struct object *obj, int verbose )
432 struct process *process = (struct process *)obj;
433 assert( obj->ops == &process_ops );
435 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
438 static int process_signaled( struct object *obj, struct thread *thread )
440 struct process *process = (struct process *)obj;
441 return !process->running_threads;
444 static void process_poll_event( struct fd *fd, int event )
446 struct process *process = get_fd_user( fd );
447 assert( process->obj.ops == &process_ops );
449 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
450 else if (event & POLLIN) receive_fd( process );
453 static void startup_info_destroy( struct object *obj )
455 struct startup_info *info = (struct startup_info *)obj;
456 assert( obj->ops == &startup_info_ops );
457 list_remove( &info->entry );
458 if (info->data) free( info->data );
459 if (info->exe_file) release_object( info->exe_file );
460 if (info->process) release_object( info->process );
461 if (info->thread) release_object( info->thread );
462 if (info->owner) release_object( info->owner );
465 static void startup_info_dump( struct object *obj, int verbose )
467 struct startup_info *info = (struct startup_info *)obj;
468 assert( obj->ops == &startup_info_ops );
470 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
471 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
474 static int startup_info_signaled( struct object *obj, struct thread *thread )
476 struct startup_info *info = (struct startup_info *)obj;
477 return info->process && is_process_init_done(info->process);
480 /* get a process from an id (and increment the refcount) */
481 struct process *get_process_from_id( process_id_t id )
483 struct object *obj = get_ptid_entry( id );
485 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
486 set_error( STATUS_INVALID_PARAMETER );
487 return NULL;
490 /* get a process from a handle (and increment the refcount) */
491 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
493 return (struct process *)get_handle_obj( current->process, handle,
494 access, &process_ops );
497 /* find a dll from its base address */
498 static inline struct process_dll *find_process_dll( struct process *process, void *base )
500 struct process_dll *dll;
502 if (process->exe.base == base) return &process->exe;
504 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
506 if (dll->base == base) return dll;
508 return NULL;
511 /* add a dll to a process list */
512 static struct process_dll *process_load_dll( struct process *process, struct file *file,
513 void *base, const WCHAR *filename, size_t name_len )
515 struct process_dll *dll;
517 /* make sure we don't already have one with the same base address */
518 if (find_process_dll( process, base ))
520 set_error( STATUS_INVALID_PARAMETER );
521 return NULL;
524 if ((dll = mem_alloc( sizeof(*dll) )))
526 dll->file = NULL;
527 dll->base = base;
528 dll->filename = NULL;
529 dll->namelen = name_len;
530 if (name_len && !(dll->filename = memdup( filename, name_len )))
532 free( dll );
533 return NULL;
535 if (file) dll->file = (struct file *)grab_object( file );
536 list_add_head( &process->dlls, &dll->entry );
538 return dll;
541 /* remove a dll from a process list */
542 static void process_unload_dll( struct process *process, void *base )
544 struct process_dll *dll = find_process_dll( process, base );
546 if (dll && dll != &process->exe)
548 if (dll->file) release_object( dll->file );
549 if (dll->filename) free( dll->filename );
550 list_remove( &dll->entry );
551 free( dll );
552 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
554 else set_error( STATUS_INVALID_PARAMETER );
557 /* kill all processes */
558 void kill_all_processes( struct process *skip, int exit_code )
560 for (;;)
562 struct process *process;
564 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
566 if (process == skip) continue;
567 if (process->running_threads) break;
569 if (&process->entry == &process_list) break; /* no process found */
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;
581 /* find the first process being attached to 'renderer' and still running */
582 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
584 if (process == renderer->process) continue;
585 if (!process->running_threads) continue;
586 if (process->console && process->console->renderer == renderer) break;
588 if (&process->entry == &process_list) break; /* no process found */
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 struct list *ptr;
598 assert( list_empty( &process->thread_list ));
599 gettimeofday( &process->end_time, NULL );
600 if (process->handles) release_object( process->handles );
601 process->handles = NULL;
603 /* close the console attached to this process, if any */
604 free_console( process );
606 while ((ptr = list_head( &process->dlls )))
608 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
609 if (dll->file) release_object( dll->file );
610 if (dll->filename) free( dll->filename );
611 list_remove( &dll->entry );
612 free( dll );
614 destroy_process_classes( process );
615 set_process_startup_state( process, STARTUP_ABORTED );
616 if (process->exe.file) release_object( process->exe.file );
617 process->exe.file = NULL;
618 wake_up( &process->obj, 0 );
619 if (!--running_processes) close_master_socket();
622 /* add a thread to a process running threads list */
623 void add_process_thread( struct process *process, struct thread *thread )
625 list_add_head( &process->thread_list, &thread->proc_entry );
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( !list_empty( &process->thread_list ));
636 list_remove( &thread->proc_entry );
638 if (!--process->running_threads)
640 /* we have removed the last running thread, exit the process */
641 process->exit_code = thread->exit_code;
642 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
643 process_killed( process );
645 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
646 release_object( thread );
649 /* suspend all the threads of a process */
650 void suspend_process( struct process *process )
652 if (!process->suspend++)
654 struct list *ptr, *next;
656 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
658 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
659 if (!thread->suspend) stop_thread( thread );
664 /* resume all the threads of a process */
665 void resume_process( struct process *process )
667 assert (process->suspend > 0);
668 if (!--process->suspend)
670 struct list *ptr, *next;
672 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
674 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
675 if (!thread->suspend) wake_thread( thread );
680 /* kill a process on the spot */
681 void kill_process( struct process *process, struct thread *skip, int exit_code )
683 struct list *ptr, *next;
685 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
687 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
689 thread->exit_code = exit_code;
690 if (thread != skip) kill_thread( thread, 1 );
694 /* kill all processes being debugged by a given thread */
695 void kill_debugged_processes( struct thread *debugger, int exit_code )
697 for (;;) /* restart from the beginning of the list every time */
699 struct process *process;
701 /* find the first process being debugged by 'debugger' and still running */
702 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
704 if (!process->running_threads) continue;
705 if (process->debugger == debugger) break;
707 if (&process->entry == &process_list) break; /* no process found */
708 process->debugger = NULL;
709 kill_process( process, NULL, exit_code );
714 /* detach a debugger from all its debuggees */
715 void detach_debugged_processes( struct thread *debugger )
717 struct process *process;
719 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
721 if (process->debugger == debugger && process->running_threads)
723 debugger_detach( process, debugger );
729 void enum_processes( int (*cb)(struct process*, void*), void *user )
731 struct list *ptr, *next;
733 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
735 struct process *process = LIST_ENTRY( ptr, struct process, entry );
736 if ((cb)(process, user)) break;
741 /* read data from a process memory space */
742 /* len is the total size (in ints) */
743 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
745 struct thread *thread = get_process_first_thread( process );
747 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
749 if (!thread) /* process is dead */
751 set_error( STATUS_ACCESS_DENIED );
752 return 0;
754 if (suspend_for_ptrace( thread ))
756 while (len > 0)
758 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
759 len--;
761 resume_after_ptrace( thread );
763 return !len;
766 /* make sure we can write to the whole address range */
767 /* len is the total size (in ints) */
768 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
770 int page = get_page_size() / sizeof(int);
772 for (;;)
774 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
775 if (len <= page) break;
776 addr += page;
777 len -= page;
779 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
782 /* write data to a process memory space */
783 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
784 /* we check the total size for write permissions */
785 static int write_process_memory( struct process *process, int *addr, size_t len,
786 unsigned int first_mask, unsigned int last_mask, const int *src )
788 struct thread *thread = get_process_first_thread( process );
789 int ret = 0;
791 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
793 if (!thread) /* process is dead */
795 set_error( STATUS_ACCESS_DENIED );
796 return 0;
798 if (suspend_for_ptrace( thread ))
800 if (!check_process_write_access( thread, addr, len ))
802 set_error( STATUS_ACCESS_DENIED );
803 goto done;
805 /* first word is special */
806 if (len > 1)
808 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
809 len--;
811 else last_mask &= first_mask;
813 while (len > 1)
815 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
816 len--;
819 /* last word is special too */
820 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
821 ret = 1;
823 done:
824 resume_after_ptrace( thread );
826 return ret;
829 /* set the debugged flag in the process PEB */
830 int set_process_debug_flag( struct process *process, int flag )
832 int mask = 0, data = 0;
834 /* BeingDebugged flag is the byte at offset 2 in the PEB */
835 memset( (char *)&mask + 2, 0xff, 1 );
836 memset( (char *)&data + 2, flag, 1 );
837 return write_process_memory( process, process->peb, 1, mask, mask, &data );
840 /* take a snapshot of currently running processes */
841 struct process_snapshot *process_snap( int *count )
843 struct process_snapshot *snapshot, *ptr;
844 struct process *process;
845 if (!running_processes) return NULL;
846 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
847 return NULL;
848 ptr = snapshot;
849 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
851 if (!process->running_threads) continue;
852 ptr->process = process;
853 ptr->threads = process->running_threads;
854 ptr->count = process->obj.refcount;
855 ptr->priority = process->priority;
856 ptr->handles = get_handle_table_count(process);
857 grab_object( process );
858 ptr++;
860 *count = running_processes;
861 return snapshot;
864 /* take a snapshot of the modules of a process */
865 struct module_snapshot *module_snap( struct process *process, int *count )
867 struct module_snapshot *snapshot, *ptr;
868 struct process_dll *dll;
869 int total = 1;
871 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
872 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
874 /* first entry is main exe */
875 snapshot->base = process->exe.base;
876 snapshot->size = process->exe.size;
877 snapshot->namelen = process->exe.namelen;
878 snapshot->filename = memdup( process->exe.filename, process->exe.namelen );
879 ptr = snapshot + 1;
881 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
883 ptr->base = dll->base;
884 ptr->size = dll->size;
885 ptr->namelen = dll->namelen;
886 ptr->filename = memdup( dll->filename, dll->namelen );
887 ptr++;
889 *count = total;
890 return snapshot;
894 /* create a new process */
895 DECL_HANDLER(new_process)
897 struct startup_info *info;
899 /* build the startup info for a new process */
900 if (!(info = alloc_object( &startup_info_ops ))) return;
901 list_add_head( &startup_info_list, &info->entry );
902 info->inherit_all = req->inherit_all;
903 info->create_flags = req->create_flags;
904 info->unix_pid = req->unix_pid;
905 info->hstdin = req->hstdin;
906 info->hstdout = req->hstdout;
907 info->hstderr = req->hstderr;
908 info->exe_file = NULL;
909 info->owner = (struct thread *)grab_object( current );
910 info->process = NULL;
911 info->thread = NULL;
912 info->data_size = get_req_data_size();
913 info->data = NULL;
915 if (req->exe_file &&
916 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
917 goto done;
919 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
920 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
922 done:
923 release_object( info );
926 /* Retrieve information about a newly started process */
927 DECL_HANDLER(get_new_process_info)
929 struct startup_info *info;
931 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
932 0, &startup_info_ops )))
934 reply->pid = get_process_id( info->process );
935 reply->tid = get_thread_id( info->thread );
936 reply->phandle = alloc_handle( current->process, info->process,
937 PROCESS_ALL_ACCESS, req->pinherit );
938 reply->thandle = alloc_handle( current->process, info->thread,
939 THREAD_ALL_ACCESS, req->tinherit );
940 reply->success = is_process_init_done( info->process );
941 release_object( info );
943 else
945 reply->pid = 0;
946 reply->tid = 0;
947 reply->phandle = 0;
948 reply->thandle = 0;
949 reply->success = 0;
953 /* Retrieve the new process startup info */
954 DECL_HANDLER(get_startup_info)
956 struct startup_info *info;
958 if ((info = current->process->startup_info))
960 size_t size = info->data_size;
961 if (size > get_reply_max_size()) size = get_reply_max_size();
963 /* we return the data directly without making a copy so this can only be called once */
964 set_reply_data_ptr( info->data, size );
965 info->data = NULL;
966 info->data_size = 0;
971 /* initialize a new process */
972 DECL_HANDLER(init_process)
974 if (current->unix_pid == -1)
976 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
977 return;
979 if (current->process->startup_info)
981 fatal_protocol_error( current, "init_process: called twice\n" );
982 return;
984 if (!req->peb || (unsigned int)req->peb % sizeof(int))
986 fatal_protocol_error( current, "init_process: bad peb address\n" );
987 return;
989 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
991 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
992 return;
994 reply->info_size = 0;
995 current->process->peb = req->peb;
996 current->process->ldt_copy = req->ldt_copy;
997 current->process->startup_info = init_process( reply );
1000 /* signal the end of the process initialization */
1001 DECL_HANDLER(init_process_done)
1003 struct file *file = NULL;
1004 struct process *process = current->process;
1006 if (is_process_init_done(process))
1008 fatal_protocol_error( current, "init_process_done: called twice\n" );
1009 return;
1011 if (!req->module)
1013 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
1014 return;
1016 process->exe.base = req->module;
1017 process->exe.size = req->module_size;
1018 process->exe.name = req->name;
1020 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
1021 if (process->exe.file) release_object( process->exe.file );
1022 process->exe.file = file;
1024 if ((process->exe.namelen = get_req_data_size()))
1025 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1027 generate_startup_debug_events( process, req->entry );
1028 set_process_startup_state( process, STARTUP_DONE );
1030 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1031 if (current->suspend + process->suspend > 0) stop_thread( current );
1032 if (process->debugger) set_process_debug_flag( process, 1 );
1035 /* open a handle to a process */
1036 DECL_HANDLER(open_process)
1038 struct process *process = get_process_from_id( req->pid );
1039 reply->handle = 0;
1040 if (process)
1042 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1043 release_object( process );
1047 /* terminate a process */
1048 DECL_HANDLER(terminate_process)
1050 struct process *process;
1052 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1054 reply->self = (current->process == process);
1055 kill_process( process, current, req->exit_code );
1056 release_object( process );
1060 /* fetch information about a process */
1061 DECL_HANDLER(get_process_info)
1063 struct process *process;
1065 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1067 reply->pid = get_process_id( process );
1068 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1069 reply->exit_code = process->exit_code;
1070 reply->priority = process->priority;
1071 reply->process_affinity = process->affinity;
1072 reply->system_affinity = 1;
1073 reply->peb = process->peb;
1074 release_object( process );
1078 /* set information about a process */
1079 DECL_HANDLER(set_process_info)
1081 struct process *process;
1083 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1085 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1086 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1088 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1089 else process->affinity = req->affinity;
1091 release_object( process );
1095 /* read data from a process address space */
1096 DECL_HANDLER(read_process_memory)
1098 struct process *process;
1099 size_t len = get_reply_max_size();
1101 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1103 if (len)
1105 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1106 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1107 const int *start = (int *)((char *)req->addr - start_offset);
1108 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1109 if (buffer)
1111 if (read_process_memory( process, start, nb_ints, buffer ))
1113 /* move start of requested data to start of buffer */
1114 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1115 set_reply_data_ptr( buffer, len );
1117 else len = 0;
1120 release_object( process );
1123 /* write data to a process address space */
1124 DECL_HANDLER(write_process_memory)
1126 struct process *process;
1128 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1130 size_t len = get_req_data_size();
1131 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1132 set_error( STATUS_INVALID_PARAMETER );
1133 else
1135 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1136 req->first_mask, req->last_mask, get_req_data() );
1138 release_object( process );
1142 /* notify the server that a dll has been loaded */
1143 DECL_HANDLER(load_dll)
1145 struct process_dll *dll;
1146 struct file *file = NULL;
1148 if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1149 return;
1151 if ((dll = process_load_dll( current->process, file, req->base,
1152 get_req_data(), get_req_data_size() )))
1154 dll->size = req->size;
1155 dll->dbg_offset = req->dbg_offset;
1156 dll->dbg_size = req->dbg_size;
1157 dll->name = req->name;
1158 /* only generate event if initialization is done */
1159 if (is_process_init_done( current->process ))
1160 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1162 if (file) release_object( file );
1165 /* notify the server that a dll is being unloaded */
1166 DECL_HANDLER(unload_dll)
1168 process_unload_dll( current->process, req->base );
1171 /* retrieve information about a module in a process */
1172 DECL_HANDLER(get_dll_info)
1174 struct process *process;
1176 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1178 struct process_dll *dll = find_process_dll( process, req->base_address );
1180 if (dll)
1182 reply->size = dll->size;
1183 reply->entry_point = 0; /* FIXME */
1184 if (dll->filename)
1186 size_t len = min( dll->namelen, get_reply_max_size() );
1187 set_reply_data( dll->filename, len );
1190 else
1191 set_error( STATUS_DLL_NOT_FOUND );
1193 release_object( process );
1197 /* wait for a process to start waiting on input */
1198 /* FIXME: only returns event for now, wait is done in the client */
1199 DECL_HANDLER(wait_input_idle)
1201 struct process *process;
1203 reply->event = 0;
1204 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1206 if (process->idle_event && process != current->process && process->queue != current->queue)
1207 reply->event = alloc_handle( current->process, process->idle_event,
1208 EVENT_ALL_ACCESS, 0 );
1209 release_object( process );