The desktop of a new thread should be set from the process initial
[wine/testsucceed.git] / server / process.c
blobf2b4012d5adaff5304c969bd398d0b3be666ada5
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"
51 #include "security.h"
53 /* process structure */
55 static struct list process_list = LIST_INIT(process_list);
56 static int running_processes;
58 /* process operations */
60 static void process_dump( struct object *obj, int verbose );
61 static int process_signaled( struct object *obj, struct thread *thread );
62 static void process_poll_event( struct fd *fd, int event );
63 static void process_destroy( struct object *obj );
65 static const struct object_ops process_ops =
67 sizeof(struct process), /* size */
68 process_dump, /* dump */
69 add_queue, /* add_queue */
70 remove_queue, /* remove_queue */
71 process_signaled, /* signaled */
72 no_satisfied, /* satisfied */
73 no_signal, /* signal */
74 no_get_fd, /* get_fd */
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 startup_info_destroy /* destroy */
126 static struct list startup_info_list = LIST_INIT(startup_info_list);
128 struct ptid_entry
130 void *ptr; /* entry ptr */
131 unsigned int next; /* next free entry */
134 static struct ptid_entry *ptid_entries; /* array of ptid entries */
135 static unsigned int used_ptid_entries; /* number of entries in use */
136 static unsigned int alloc_ptid_entries; /* number of allocated entries */
137 static unsigned int next_free_ptid; /* next free entry */
138 static unsigned int last_free_ptid; /* last free entry */
140 #define PTID_OFFSET 8 /* offset for first ptid value */
142 /* allocate a new process or thread id */
143 unsigned int alloc_ptid( void *ptr )
145 struct ptid_entry *entry;
146 unsigned int id;
148 if (used_ptid_entries < alloc_ptid_entries)
150 id = used_ptid_entries + PTID_OFFSET;
151 entry = &ptid_entries[used_ptid_entries++];
153 else if (next_free_ptid)
155 id = next_free_ptid;
156 entry = &ptid_entries[id - PTID_OFFSET];
157 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
159 else /* need to grow the array */
161 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
162 if (!count) count = 64;
163 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
165 set_error( STATUS_NO_MEMORY );
166 return 0;
168 ptid_entries = entry;
169 alloc_ptid_entries = count;
170 id = used_ptid_entries + PTID_OFFSET;
171 entry = &ptid_entries[used_ptid_entries++];
174 entry->ptr = ptr;
175 return id;
178 /* free a process or thread id */
179 void free_ptid( unsigned int id )
181 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
183 entry->ptr = NULL;
184 entry->next = 0;
186 /* append to end of free list so that we don't reuse it too early */
187 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
188 else next_free_ptid = id;
190 last_free_ptid = id;
193 /* retrieve the pointer corresponding to a process or thread id */
194 void *get_ptid_entry( unsigned int id )
196 if (id < PTID_OFFSET) return NULL;
197 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
198 return ptid_entries[id - PTID_OFFSET].ptr;
201 /* return the main thread of the process */
202 struct thread *get_process_first_thread( struct process *process )
204 struct list *ptr = list_head( &process->thread_list );
205 if (!ptr) return NULL;
206 return LIST_ENTRY( ptr, struct thread, proc_entry );
209 /* set the state of the process startup info */
210 static void set_process_startup_state( struct process *process, enum startup_state state )
212 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
213 if (process->startup_info)
215 wake_up( &process->startup_info->obj, 0 );
216 release_object( process->startup_info );
217 process->startup_info = NULL;
221 /* set the console and stdio handles for a newly created process */
222 static int set_process_console( struct process *process, struct thread *parent_thread,
223 struct startup_info *info, struct init_process_reply *reply )
225 if (info)
227 if (!(process->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
229 /* FIXME: some better error checking should be done...
230 * like if hConOut and hConIn are console handles, then they should be on the same
231 * physical console
233 inherit_console( parent_thread, process, info->inherit_all ? info->hstdin : 0 );
235 if (!info->inherit_all && !(process->create_flags & CREATE_NEW_CONSOLE))
237 reply->hstdin = duplicate_handle( parent_thread->process, info->hstdin, process,
238 0, TRUE, DUPLICATE_SAME_ACCESS );
239 reply->hstdout = duplicate_handle( parent_thread->process, info->hstdout, process,
240 0, TRUE, DUPLICATE_SAME_ACCESS );
241 reply->hstderr = duplicate_handle( parent_thread->process, info->hstderr, process,
242 0, TRUE, DUPLICATE_SAME_ACCESS );
244 else
246 reply->hstdin = info->hstdin;
247 reply->hstdout = info->hstdout;
248 reply->hstderr = info->hstderr;
251 else reply->hstdin = reply->hstdout = reply->hstderr = 0;
252 /* some handles above may have been invalid; this is not an error */
253 if (get_error() == STATUS_INVALID_HANDLE ||
254 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
255 return 1;
258 /* create a new process and its main thread */
259 struct thread *create_process( int fd )
261 struct process *process;
262 struct thread *thread = NULL;
263 int request_pipe[2];
265 if (!(process = alloc_object( &process_ops ))) goto error;
266 process->parent = NULL;
267 process->debugger = NULL;
268 process->handles = NULL;
269 process->msg_fd = NULL;
270 process->exit_code = STILL_ACTIVE;
271 process->running_threads = 0;
272 process->priority = NORMAL_PRIORITY_CLASS;
273 process->affinity = 1;
274 process->suspend = 0;
275 process->create_flags = 0;
276 process->console = NULL;
277 process->startup_state = STARTUP_IN_PROGRESS;
278 process->startup_info = NULL;
279 process->idle_event = NULL;
280 process->queue = NULL;
281 process->peb = NULL;
282 process->ldt_copy = NULL;
283 process->winstation = 0;
284 process->desktop = 0;
285 process->exe.file = NULL;
286 process->exe.dbg_offset = 0;
287 process->exe.dbg_size = 0;
288 process->exe.namelen = 0;
289 process->exe.filename = NULL;
290 process->group_id = 0;
291 process->token = token_create_admin();
292 list_init( &process->thread_list );
293 list_init( &process->locks );
294 list_init( &process->classes );
295 list_init( &process->dlls );
297 gettimeofday( &process->start_time, NULL );
298 list_add_head( &process_list, &process->entry );
300 if (!(process->id = alloc_ptid( process ))) goto error;
301 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
303 /* create the main thread */
304 if (pipe( request_pipe ) == -1)
306 file_set_error();
307 goto error;
309 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
311 close( request_pipe[0] );
312 close( request_pipe[1] );
313 goto error;
315 close( request_pipe[1] );
316 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
318 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
319 release_object( process );
320 return thread;
322 error:
323 if (process) release_object( process );
324 /* if we failed to start our first process, close everything down */
325 if (!running_processes) close_master_socket();
326 return NULL;
329 /* find the startup info for a given Unix process */
330 inline static struct startup_info *find_startup_info( int unix_pid )
332 struct list *ptr;
334 LIST_FOR_EACH( ptr, &startup_info_list )
336 struct startup_info *info = LIST_ENTRY( ptr, struct startup_info, entry );
337 if (info->unix_pid == unix_pid) return info;
339 return NULL;
342 /* initialize the current process and fill in the request */
343 static struct startup_info *init_process( struct init_process_reply *reply )
345 struct process *process = current->process;
346 struct thread *parent_thread = NULL;
347 struct process *parent = NULL;
348 struct startup_info *info = find_startup_info( current->unix_pid );
350 if (info)
352 if (info->thread)
354 fatal_protocol_error( current, "init_process: called twice?\n" );
355 return NULL;
357 parent_thread = info->owner;
358 parent = parent_thread->process;
359 process->parent = (struct process *)grab_object( parent );
362 /* set the process flags */
363 process->create_flags = info ? info->create_flags : 0;
365 /* create the handle table */
366 if (info && info->inherit_all)
367 process->handles = copy_handle_table( process, parent );
368 else
369 process->handles = alloc_handle_table( process, 0 );
370 if (!process->handles) return NULL;
372 /* retrieve the main exe file */
373 reply->exe_file = 0;
374 if (info && info->exe_file)
376 process->exe.file = (struct file *)grab_object( info->exe_file );
377 if (!(reply->exe_file = alloc_handle( process, process->exe.file, GENERIC_READ, 0 )))
378 return NULL;
381 /* connect to the window station and desktop */
382 connect_process_winstation( process, NULL, 0 );
383 connect_process_desktop( process, NULL, 0 );
384 current->desktop = process->desktop;
386 /* set the process console */
387 if (!set_process_console( process, parent_thread, info, reply )) return NULL;
389 process->group_id = get_process_id( process );
390 if (parent)
392 /* attach to the debugger if requested */
393 if (process->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
394 set_process_debugger( process, parent_thread );
395 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
396 set_process_debugger( process, parent->debugger );
397 if (!(process->create_flags & CREATE_NEW_PROCESS_GROUP))
398 process->group_id = parent->group_id;
401 /* thread will be actually suspended in init_done */
402 if (process->create_flags & CREATE_SUSPENDED) current->suspend++;
404 if (info)
406 reply->info_size = info->data_size;
407 info->process = (struct process *)grab_object( process );
408 info->thread = (struct thread *)grab_object( current );
410 reply->create_flags = process->create_flags;
411 reply->server_start = server_start_ticks;
412 return info ? (struct startup_info *)grab_object( info ) : NULL;
415 /* destroy a process when its refcount is 0 */
416 static void process_destroy( struct object *obj )
418 struct process *process = (struct process *)obj;
419 assert( obj->ops == &process_ops );
421 /* we can't have a thread remaining */
422 assert( list_empty( &process->thread_list ));
424 set_process_startup_state( process, STARTUP_ABORTED );
425 if (process->console) release_object( process->console );
426 if (process->parent) release_object( process->parent );
427 if (process->msg_fd) release_object( process->msg_fd );
428 list_remove( &process->entry );
429 if (process->idle_event) release_object( process->idle_event );
430 if (process->queue) release_object( process->queue );
431 if (process->exe.file) release_object( process->exe.file );
432 if (process->exe.filename) free( process->exe.filename );
433 if (process->id) free_ptid( process->id );
434 if (process->token) release_object( process->token );
437 /* dump a process on stdout for debugging purposes */
438 static void process_dump( struct object *obj, int verbose )
440 struct process *process = (struct process *)obj;
441 assert( obj->ops == &process_ops );
443 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
446 static int process_signaled( struct object *obj, struct thread *thread )
448 struct process *process = (struct process *)obj;
449 return !process->running_threads;
452 static void process_poll_event( struct fd *fd, int event )
454 struct process *process = get_fd_user( fd );
455 assert( process->obj.ops == &process_ops );
457 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
458 else if (event & POLLIN) receive_fd( process );
461 static void startup_info_destroy( struct object *obj )
463 struct startup_info *info = (struct startup_info *)obj;
464 assert( obj->ops == &startup_info_ops );
465 list_remove( &info->entry );
466 if (info->data) free( info->data );
467 if (info->exe_file) release_object( info->exe_file );
468 if (info->process) release_object( info->process );
469 if (info->thread) release_object( info->thread );
470 if (info->owner) release_object( info->owner );
473 static void startup_info_dump( struct object *obj, int verbose )
475 struct startup_info *info = (struct startup_info *)obj;
476 assert( obj->ops == &startup_info_ops );
478 fprintf( stderr, "Startup info flags=%x in=%p out=%p err=%p\n",
479 info->create_flags, info->hstdin, info->hstdout, info->hstderr );
482 static int startup_info_signaled( struct object *obj, struct thread *thread )
484 struct startup_info *info = (struct startup_info *)obj;
485 return info->process && is_process_init_done(info->process);
488 /* get a process from an id (and increment the refcount) */
489 struct process *get_process_from_id( process_id_t id )
491 struct object *obj = get_ptid_entry( id );
493 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
494 set_error( STATUS_INVALID_PARAMETER );
495 return NULL;
498 /* get a process from a handle (and increment the refcount) */
499 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
501 return (struct process *)get_handle_obj( current->process, handle,
502 access, &process_ops );
505 /* find a dll from its base address */
506 static inline struct process_dll *find_process_dll( struct process *process, void *base )
508 struct process_dll *dll;
510 if (process->exe.base == base) return &process->exe;
512 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
514 if (dll->base == base) return dll;
516 return NULL;
519 /* add a dll to a process list */
520 static struct process_dll *process_load_dll( struct process *process, struct file *file,
521 void *base, const WCHAR *filename, size_t name_len )
523 struct process_dll *dll;
525 /* make sure we don't already have one with the same base address */
526 if (find_process_dll( process, base ))
528 set_error( STATUS_INVALID_PARAMETER );
529 return NULL;
532 if ((dll = mem_alloc( sizeof(*dll) )))
534 dll->file = NULL;
535 dll->base = base;
536 dll->filename = NULL;
537 dll->namelen = name_len;
538 if (name_len && !(dll->filename = memdup( filename, name_len )))
540 free( dll );
541 return NULL;
543 if (file) dll->file = (struct file *)grab_object( file );
544 list_add_head( &process->dlls, &dll->entry );
546 return dll;
549 /* remove a dll from a process list */
550 static void process_unload_dll( struct process *process, void *base )
552 struct process_dll *dll = find_process_dll( process, base );
554 if (dll && dll != &process->exe)
556 if (dll->file) release_object( dll->file );
557 if (dll->filename) free( dll->filename );
558 list_remove( &dll->entry );
559 free( dll );
560 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
562 else set_error( STATUS_INVALID_PARAMETER );
565 /* kill all processes */
566 void kill_all_processes( struct process *skip, int exit_code )
568 for (;;)
570 struct process *process;
572 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
574 if (process == skip) continue;
575 if (process->running_threads) break;
577 if (&process->entry == &process_list) break; /* no process found */
578 kill_process( process, NULL, exit_code );
582 /* kill all processes being attached to a console renderer */
583 void kill_console_processes( struct thread *renderer, int exit_code )
585 for (;;) /* restart from the beginning of the list every time */
587 struct process *process;
589 /* find the first process being attached to 'renderer' and still running */
590 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
592 if (process == renderer->process) continue;
593 if (!process->running_threads) continue;
594 if (process->console && process->console->renderer == renderer) break;
596 if (&process->entry == &process_list) break; /* no process found */
597 kill_process( process, NULL, exit_code );
601 /* a process has been killed (i.e. its last thread died) */
602 static void process_killed( struct process *process )
604 struct list *ptr;
606 assert( list_empty( &process->thread_list ));
607 gettimeofday( &process->end_time, NULL );
608 if (process->handles) release_object( process->handles );
609 process->handles = NULL;
611 /* close the console attached to this process, if any */
612 free_console( process );
614 while ((ptr = list_head( &process->dlls )))
616 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
617 if (dll->file) release_object( dll->file );
618 if (dll->filename) free( dll->filename );
619 list_remove( &dll->entry );
620 free( dll );
622 destroy_process_classes( process );
623 set_process_startup_state( process, STARTUP_ABORTED );
624 if (process->exe.file) release_object( process->exe.file );
625 process->exe.file = NULL;
626 wake_up( &process->obj, 0 );
627 if (!--running_processes) close_master_socket();
630 /* add a thread to a process running threads list */
631 void add_process_thread( struct process *process, struct thread *thread )
633 list_add_head( &process->thread_list, &thread->proc_entry );
634 if (!process->running_threads++) running_processes++;
635 grab_object( thread );
638 /* remove a thread from a process running threads list */
639 void remove_process_thread( struct process *process, struct thread *thread )
641 assert( process->running_threads > 0 );
642 assert( !list_empty( &process->thread_list ));
644 list_remove( &thread->proc_entry );
646 if (!--process->running_threads)
648 /* we have removed the last running thread, exit the process */
649 process->exit_code = thread->exit_code;
650 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
651 process_killed( process );
653 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
654 release_object( thread );
657 /* suspend all the threads of a process */
658 void suspend_process( struct process *process )
660 if (!process->suspend++)
662 struct list *ptr, *next;
664 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
666 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
667 if (!thread->suspend) stop_thread( thread );
672 /* resume all the threads of a process */
673 void resume_process( struct process *process )
675 assert (process->suspend > 0);
676 if (!--process->suspend)
678 struct list *ptr, *next;
680 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
682 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
683 if (!thread->suspend) wake_thread( thread );
688 /* kill a process on the spot */
689 void kill_process( struct process *process, struct thread *skip, int exit_code )
691 struct list *ptr, *next;
693 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
695 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
697 thread->exit_code = exit_code;
698 if (thread != skip) kill_thread( thread, 1 );
702 /* kill all processes being debugged by a given thread */
703 void kill_debugged_processes( struct thread *debugger, int exit_code )
705 for (;;) /* restart from the beginning of the list every time */
707 struct process *process;
709 /* find the first process being debugged by 'debugger' and still running */
710 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
712 if (!process->running_threads) continue;
713 if (process->debugger == debugger) break;
715 if (&process->entry == &process_list) break; /* no process found */
716 process->debugger = NULL;
717 kill_process( process, NULL, exit_code );
722 /* detach a debugger from all its debuggees */
723 void detach_debugged_processes( struct thread *debugger )
725 struct process *process;
727 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
729 if (process->debugger == debugger && process->running_threads)
731 debugger_detach( process, debugger );
737 void enum_processes( int (*cb)(struct process*, void*), void *user )
739 struct list *ptr, *next;
741 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
743 struct process *process = LIST_ENTRY( ptr, struct process, entry );
744 if ((cb)(process, user)) break;
749 /* read data from a process memory space */
750 /* len is the total size (in ints) */
751 static int read_process_memory( struct process *process, const int *addr, size_t len, int *dest )
753 struct thread *thread = get_process_first_thread( process );
755 assert( !((unsigned int)addr % sizeof(int)) ); /* address must be aligned */
757 if (!thread) /* process is dead */
759 set_error( STATUS_ACCESS_DENIED );
760 return 0;
762 if (suspend_for_ptrace( thread ))
764 while (len > 0)
766 if (read_thread_int( thread, addr++, dest++ ) == -1) break;
767 len--;
769 resume_after_ptrace( thread );
771 return !len;
774 /* make sure we can write to the whole address range */
775 /* len is the total size (in ints) */
776 static int check_process_write_access( struct thread *thread, int *addr, size_t len )
778 int page = get_page_size() / sizeof(int);
780 for (;;)
782 if (write_thread_int( thread, addr, 0, 0 ) == -1) return 0;
783 if (len <= page) break;
784 addr += page;
785 len -= page;
787 return (write_thread_int( thread, addr + len - 1, 0, 0 ) != -1);
790 /* write data to a process memory space */
791 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
792 /* we check the total size for write permissions */
793 static int write_process_memory( struct process *process, int *addr, size_t len,
794 unsigned int first_mask, unsigned int last_mask, const int *src )
796 struct thread *thread = get_process_first_thread( process );
797 int ret = 0;
799 assert( !((unsigned int)addr % sizeof(int) )); /* address must be aligned */
801 if (!thread) /* process is dead */
803 set_error( STATUS_ACCESS_DENIED );
804 return 0;
806 if (suspend_for_ptrace( thread ))
808 if (!check_process_write_access( thread, addr, len ))
810 set_error( STATUS_ACCESS_DENIED );
811 goto done;
813 /* first word is special */
814 if (len > 1)
816 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
817 len--;
819 else last_mask &= first_mask;
821 while (len > 1)
823 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
824 len--;
827 /* last word is special too */
828 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
829 ret = 1;
831 done:
832 resume_after_ptrace( thread );
834 return ret;
837 /* set the debugged flag in the process PEB */
838 int set_process_debug_flag( struct process *process, int flag )
840 int mask = 0, data = 0;
842 /* BeingDebugged flag is the byte at offset 2 in the PEB */
843 memset( (char *)&mask + 2, 0xff, 1 );
844 memset( (char *)&data + 2, flag, 1 );
845 return write_process_memory( process, process->peb, 1, mask, mask, &data );
848 /* take a snapshot of currently running processes */
849 struct process_snapshot *process_snap( int *count )
851 struct process_snapshot *snapshot, *ptr;
852 struct process *process;
853 if (!running_processes) return NULL;
854 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
855 return NULL;
856 ptr = snapshot;
857 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
859 if (!process->running_threads) continue;
860 ptr->process = process;
861 ptr->threads = process->running_threads;
862 ptr->count = process->obj.refcount;
863 ptr->priority = process->priority;
864 ptr->handles = get_handle_table_count(process);
865 grab_object( process );
866 ptr++;
868 *count = running_processes;
869 return snapshot;
872 /* take a snapshot of the modules of a process */
873 struct module_snapshot *module_snap( struct process *process, int *count )
875 struct module_snapshot *snapshot, *ptr;
876 struct process_dll *dll;
877 int total = 1;
879 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
880 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
882 /* first entry is main exe */
883 snapshot->base = process->exe.base;
884 snapshot->size = process->exe.size;
885 snapshot->namelen = process->exe.namelen;
886 snapshot->filename = memdup( process->exe.filename, process->exe.namelen );
887 ptr = snapshot + 1;
889 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
891 ptr->base = dll->base;
892 ptr->size = dll->size;
893 ptr->namelen = dll->namelen;
894 ptr->filename = memdup( dll->filename, dll->namelen );
895 ptr++;
897 *count = total;
898 return snapshot;
902 /* create a new process */
903 DECL_HANDLER(new_process)
905 struct startup_info *info;
907 /* build the startup info for a new process */
908 if (!(info = alloc_object( &startup_info_ops ))) return;
909 list_add_head( &startup_info_list, &info->entry );
910 info->inherit_all = req->inherit_all;
911 info->create_flags = req->create_flags;
912 info->unix_pid = req->unix_pid;
913 info->hstdin = req->hstdin;
914 info->hstdout = req->hstdout;
915 info->hstderr = req->hstderr;
916 info->exe_file = NULL;
917 info->owner = (struct thread *)grab_object( current );
918 info->process = NULL;
919 info->thread = NULL;
920 info->data_size = get_req_data_size();
921 info->data = NULL;
923 if (req->exe_file &&
924 !(info->exe_file = get_file_obj( current->process, req->exe_file, GENERIC_READ )))
925 goto done;
927 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
928 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, FALSE );
930 done:
931 release_object( info );
934 /* Retrieve information about a newly started process */
935 DECL_HANDLER(get_new_process_info)
937 struct startup_info *info;
939 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
940 0, &startup_info_ops )))
942 reply->pid = get_process_id( info->process );
943 reply->tid = get_thread_id( info->thread );
944 reply->phandle = alloc_handle( current->process, info->process,
945 PROCESS_ALL_ACCESS, req->pinherit );
946 reply->thandle = alloc_handle( current->process, info->thread,
947 THREAD_ALL_ACCESS, req->tinherit );
948 reply->success = is_process_init_done( info->process );
949 release_object( info );
951 else
953 reply->pid = 0;
954 reply->tid = 0;
955 reply->phandle = 0;
956 reply->thandle = 0;
957 reply->success = 0;
961 /* Retrieve the new process startup info */
962 DECL_HANDLER(get_startup_info)
964 struct startup_info *info;
966 if ((info = current->process->startup_info))
968 size_t size = info->data_size;
969 if (size > get_reply_max_size()) size = get_reply_max_size();
971 /* we return the data directly without making a copy so this can only be called once */
972 set_reply_data_ptr( info->data, size );
973 info->data = NULL;
974 info->data_size = 0;
979 /* initialize a new process */
980 DECL_HANDLER(init_process)
982 if (current->unix_pid == -1)
984 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
985 return;
987 if (current->process->startup_info)
989 fatal_protocol_error( current, "init_process: called twice\n" );
990 return;
992 if (!req->peb || (unsigned int)req->peb % sizeof(int))
994 fatal_protocol_error( current, "init_process: bad peb address\n" );
995 return;
997 if (!req->ldt_copy || (unsigned int)req->ldt_copy % sizeof(int))
999 fatal_protocol_error( current, "init_process: bad ldt_copy address\n" );
1000 return;
1002 reply->info_size = 0;
1003 current->process->peb = req->peb;
1004 current->process->ldt_copy = req->ldt_copy;
1005 current->process->startup_info = init_process( reply );
1008 /* signal the end of the process initialization */
1009 DECL_HANDLER(init_process_done)
1011 struct file *file = NULL;
1012 struct process *process = current->process;
1014 if (is_process_init_done(process))
1016 fatal_protocol_error( current, "init_process_done: called twice\n" );
1017 return;
1019 if (!req->module)
1021 fatal_protocol_error( current, "init_process_done: module base address cannot be 0\n" );
1022 return;
1024 process->exe.base = req->module;
1025 process->exe.size = req->module_size;
1026 process->exe.name = req->name;
1028 if (req->exe_file) file = get_file_obj( process, req->exe_file, GENERIC_READ );
1029 if (process->exe.file) release_object( process->exe.file );
1030 process->exe.file = file;
1032 if ((process->exe.namelen = get_req_data_size()))
1033 process->exe.filename = memdup( get_req_data(), process->exe.namelen );
1035 generate_startup_debug_events( process, req->entry );
1036 set_process_startup_state( process, STARTUP_DONE );
1038 if (req->gui) process->idle_event = create_event( NULL, 0, 1, 0 );
1039 if (current->suspend + process->suspend > 0) stop_thread( current );
1040 if (process->debugger) set_process_debug_flag( process, 1 );
1043 /* open a handle to a process */
1044 DECL_HANDLER(open_process)
1046 struct process *process = get_process_from_id( req->pid );
1047 reply->handle = 0;
1048 if (process)
1050 reply->handle = alloc_handle( current->process, process, req->access, req->inherit );
1051 release_object( process );
1055 /* terminate a process */
1056 DECL_HANDLER(terminate_process)
1058 struct process *process;
1060 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1062 reply->self = (current->process == process);
1063 kill_process( process, current, req->exit_code );
1064 release_object( process );
1068 /* fetch information about a process */
1069 DECL_HANDLER(get_process_info)
1071 struct process *process;
1073 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1075 reply->pid = get_process_id( process );
1076 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1077 reply->exit_code = process->exit_code;
1078 reply->priority = process->priority;
1079 reply->process_affinity = process->affinity;
1080 reply->system_affinity = 1;
1081 reply->peb = process->peb;
1082 release_object( process );
1086 /* set information about a process */
1087 DECL_HANDLER(set_process_info)
1089 struct process *process;
1091 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1093 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1094 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1096 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1097 else process->affinity = req->affinity;
1099 release_object( process );
1103 /* read data from a process address space */
1104 DECL_HANDLER(read_process_memory)
1106 struct process *process;
1107 size_t len = get_reply_max_size();
1109 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1111 if (len)
1113 unsigned int start_offset = (unsigned int)req->addr % sizeof(int);
1114 unsigned int nb_ints = (len + start_offset + sizeof(int) - 1) / sizeof(int);
1115 const int *start = (int *)((char *)req->addr - start_offset);
1116 int *buffer = mem_alloc( nb_ints * sizeof(int) );
1117 if (buffer)
1119 if (read_process_memory( process, start, nb_ints, buffer ))
1121 /* move start of requested data to start of buffer */
1122 if (start_offset) memmove( buffer, (char *)buffer + start_offset, len );
1123 set_reply_data_ptr( buffer, len );
1125 else len = 0;
1128 release_object( process );
1131 /* write data to a process address space */
1132 DECL_HANDLER(write_process_memory)
1134 struct process *process;
1136 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1138 size_t len = get_req_data_size();
1139 if ((len % sizeof(int)) || ((unsigned int)req->addr % sizeof(int)))
1140 set_error( STATUS_INVALID_PARAMETER );
1141 else
1143 if (len) write_process_memory( process, req->addr, len / sizeof(int),
1144 req->first_mask, req->last_mask, get_req_data() );
1146 release_object( process );
1150 /* notify the server that a dll has been loaded */
1151 DECL_HANDLER(load_dll)
1153 struct process_dll *dll;
1154 struct file *file = NULL;
1156 if (req->handle && !(file = get_file_obj( current->process, req->handle, GENERIC_READ )))
1157 return;
1159 if ((dll = process_load_dll( current->process, file, req->base,
1160 get_req_data(), get_req_data_size() )))
1162 dll->size = req->size;
1163 dll->dbg_offset = req->dbg_offset;
1164 dll->dbg_size = req->dbg_size;
1165 dll->name = req->name;
1166 /* only generate event if initialization is done */
1167 if (is_process_init_done( current->process ))
1168 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1170 if (file) release_object( file );
1173 /* notify the server that a dll is being unloaded */
1174 DECL_HANDLER(unload_dll)
1176 process_unload_dll( current->process, req->base );
1179 /* retrieve information about a module in a process */
1180 DECL_HANDLER(get_dll_info)
1182 struct process *process;
1184 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1186 struct process_dll *dll = find_process_dll( process, req->base_address );
1188 if (dll)
1190 reply->size = dll->size;
1191 reply->entry_point = NULL; /* FIXME */
1192 if (dll->filename)
1194 size_t len = min( dll->namelen, get_reply_max_size() );
1195 set_reply_data( dll->filename, len );
1198 else
1199 set_error( STATUS_DLL_NOT_FOUND );
1201 release_object( process );
1205 /* wait for a process to start waiting on input */
1206 /* FIXME: only returns event for now, wait is done in the client */
1207 DECL_HANDLER(wait_input_idle)
1209 struct process *process;
1211 reply->event = 0;
1212 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1214 if (process->idle_event && process != current->process && process->queue != current->queue)
1215 reply->event = alloc_handle( current->process, process->idle_event,
1216 EVENT_ALL_ACCESS, 0 );
1217 release_object( process );