push 150847dede558b39567907bd3738046f0ea247b2
[wine/hacks.git] / server / process.c
blob67445d2f45429a6cf3d85348b049e96706a5ebb0
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.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, user_processes;
56 static struct event *user_process_event; /* signaled when all user processes have exited */
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 unsigned int process_map_access( struct object *obj, unsigned int access );
63 static void process_poll_event( struct fd *fd, int event );
64 static void process_destroy( struct object *obj );
66 static const struct object_ops process_ops =
68 sizeof(struct process), /* size */
69 process_dump, /* dump */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 process_signaled, /* signaled */
73 no_satisfied, /* satisfied */
74 no_signal, /* signal */
75 no_get_fd, /* get_fd */
76 process_map_access, /* map_access */
77 no_lookup_name, /* lookup_name */
78 no_open_file, /* open_file */
79 no_close_handle, /* close_handle */
80 process_destroy /* destroy */
83 static const struct fd_ops process_fd_ops =
85 NULL, /* get_poll_events */
86 process_poll_event, /* poll_event */
87 NULL, /* flush */
88 NULL, /* get_fd_type */
89 NULL, /* ioctl */
90 NULL, /* queue_async */
91 NULL, /* reselect_async */
92 NULL /* cancel async */
95 /* process startup info */
97 struct startup_info
99 struct object obj; /* object header */
100 obj_handle_t hstdin; /* handle for stdin */
101 obj_handle_t hstdout; /* handle for stdout */
102 obj_handle_t hstderr; /* handle for stderr */
103 struct file *exe_file; /* file handle for main exe */
104 struct process *process; /* created process */
105 data_size_t data_size; /* size of startup data */
106 void *data; /* data for startup info */
109 static void startup_info_dump( struct object *obj, int verbose );
110 static int startup_info_signaled( struct object *obj, struct thread *thread );
111 static void startup_info_destroy( struct object *obj );
113 static const struct object_ops startup_info_ops =
115 sizeof(struct startup_info), /* size */
116 startup_info_dump, /* dump */
117 add_queue, /* add_queue */
118 remove_queue, /* remove_queue */
119 startup_info_signaled, /* signaled */
120 no_satisfied, /* satisfied */
121 no_signal, /* signal */
122 no_get_fd, /* get_fd */
123 no_map_access, /* map_access */
124 no_lookup_name, /* lookup_name */
125 no_open_file, /* open_file */
126 no_close_handle, /* close_handle */
127 startup_info_destroy /* destroy */
131 struct ptid_entry
133 void *ptr; /* entry ptr */
134 unsigned int next; /* next free entry */
137 static struct ptid_entry *ptid_entries; /* array of ptid entries */
138 static unsigned int used_ptid_entries; /* number of entries in use */
139 static unsigned int alloc_ptid_entries; /* number of allocated entries */
140 static unsigned int next_free_ptid; /* next free entry */
141 static unsigned int last_free_ptid; /* last free entry */
143 #define PTID_OFFSET 8 /* offset for first ptid value */
145 /* allocate a new process or thread id */
146 unsigned int alloc_ptid( void *ptr )
148 struct ptid_entry *entry;
149 unsigned int id;
151 if (used_ptid_entries < alloc_ptid_entries)
153 id = used_ptid_entries + PTID_OFFSET;
154 entry = &ptid_entries[used_ptid_entries++];
156 else if (next_free_ptid)
158 id = next_free_ptid;
159 entry = &ptid_entries[id - PTID_OFFSET];
160 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
162 else /* need to grow the array */
164 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
165 if (!count) count = 64;
166 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
168 set_error( STATUS_NO_MEMORY );
169 return 0;
171 ptid_entries = entry;
172 alloc_ptid_entries = count;
173 id = used_ptid_entries + PTID_OFFSET;
174 entry = &ptid_entries[used_ptid_entries++];
177 entry->ptr = ptr;
178 return id;
181 /* free a process or thread id */
182 void free_ptid( unsigned int id )
184 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
186 entry->ptr = NULL;
187 entry->next = 0;
189 /* append to end of free list so that we don't reuse it too early */
190 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
191 else next_free_ptid = id;
193 last_free_ptid = id;
196 /* retrieve the pointer corresponding to a process or thread id */
197 void *get_ptid_entry( unsigned int id )
199 if (id < PTID_OFFSET) return NULL;
200 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
201 return ptid_entries[id - PTID_OFFSET].ptr;
204 /* return the main thread of the process */
205 struct thread *get_process_first_thread( struct process *process )
207 struct list *ptr = list_head( &process->thread_list );
208 if (!ptr) return NULL;
209 return LIST_ENTRY( ptr, struct thread, proc_entry );
212 /* set the state of the process startup info */
213 static void set_process_startup_state( struct process *process, enum startup_state state )
215 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
216 if (process->startup_info)
218 wake_up( &process->startup_info->obj, 0 );
219 release_object( process->startup_info );
220 process->startup_info = NULL;
224 /* final cleanup once we are sure a process is really dead */
225 static void process_died( struct process *process )
227 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
228 if (!process->is_system)
230 if (!--user_processes && user_process_event)
231 set_event( user_process_event );
233 release_object( process );
234 if (!--running_processes) close_master_socket();
237 /* callback for process sigkill timeout */
238 static void process_sigkill( void *private )
240 struct process *process = private;
242 process->sigkill_timeout = NULL;
243 kill( process->unix_pid, SIGKILL );
244 process_died( process );
247 /* start the sigkill timer for a process upon exit */
248 static void start_sigkill_timer( struct process *process )
250 grab_object( process );
251 if (process->unix_pid != -1 && process->msg_fd)
252 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
253 else
254 process_died( process );
257 /* create a new process and its main thread */
258 /* if the function fails the fd is closed */
259 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
261 struct process *process;
262 struct thread *thread = NULL;
263 int request_pipe[2];
265 if (!(process = alloc_object( &process_ops )))
267 close( fd );
268 goto error;
270 process->parent = NULL;
271 process->debugger = NULL;
272 process->handles = NULL;
273 process->msg_fd = NULL;
274 process->sigkill_timeout = NULL;
275 process->unix_pid = -1;
276 process->exit_code = STILL_ACTIVE;
277 process->running_threads = 0;
278 process->priority = PROCESS_PRIOCLASS_NORMAL;
279 process->affinity = 1;
280 process->suspend = 0;
281 process->is_system = 0;
282 process->create_flags = 0;
283 process->console = NULL;
284 process->startup_state = STARTUP_IN_PROGRESS;
285 process->startup_info = NULL;
286 process->idle_event = NULL;
287 process->queue = NULL;
288 process->peb = NULL;
289 process->ldt_copy = NULL;
290 process->winstation = 0;
291 process->desktop = 0;
292 process->token = NULL;
293 process->trace_data = 0;
294 list_init( &process->thread_list );
295 list_init( &process->locks );
296 list_init( &process->classes );
297 list_init( &process->dlls );
299 process->start_time = current_time;
300 process->end_time = 0;
301 list_add_head( &process_list, &process->entry );
303 if (!(process->id = process->group_id = alloc_ptid( process )))
305 close( fd );
306 goto error;
308 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
310 /* create the handle table */
311 if (!parent_thread)
313 process->handles = alloc_handle_table( process, 0 );
314 process->token = token_create_admin();
316 else
318 struct process *parent = parent_thread->process;
319 process->parent = (struct process *)grab_object( parent );
320 process->handles = inherit_all ? copy_handle_table( process, parent )
321 : alloc_handle_table( process, 0 );
322 /* Note: for security reasons, starting a new process does not attempt
323 * to use the current impersonation token for the new process */
324 process->token = token_duplicate( parent->token, TRUE, 0 );
326 if (!process->handles || !process->token) goto error;
328 /* create the main thread */
329 if (pipe( request_pipe ) == -1)
331 file_set_error();
332 goto error;
334 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
336 close( request_pipe[0] );
337 close( request_pipe[1] );
338 goto error;
340 close( request_pipe[1] );
341 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
343 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
344 release_object( process );
345 return thread;
347 error:
348 if (process) release_object( process );
349 /* if we failed to start our first process, close everything down */
350 if (!running_processes) close_master_socket();
351 return NULL;
354 /* initialize the current process and fill in the request */
355 data_size_t init_process( struct thread *thread )
357 struct process *process = thread->process;
358 struct startup_info *info = process->startup_info;
360 init_process_tracing( process );
361 if (!info) return 0;
362 return info->data_size;
365 /* destroy a process when its refcount is 0 */
366 static void process_destroy( struct object *obj )
368 struct process *process = (struct process *)obj;
369 assert( obj->ops == &process_ops );
371 /* we can't have a thread remaining */
372 assert( list_empty( &process->thread_list ));
374 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
376 set_process_startup_state( process, STARTUP_ABORTED );
377 if (process->console) release_object( process->console );
378 if (process->parent) release_object( process->parent );
379 if (process->msg_fd) release_object( process->msg_fd );
380 list_remove( &process->entry );
381 if (process->idle_event) release_object( process->idle_event );
382 if (process->queue) release_object( process->queue );
383 if (process->id) free_ptid( process->id );
384 if (process->token) release_object( process->token );
387 /* dump a process on stdout for debugging purposes */
388 static void process_dump( struct object *obj, int verbose )
390 struct process *process = (struct process *)obj;
391 assert( obj->ops == &process_ops );
393 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
396 static int process_signaled( struct object *obj, struct thread *thread )
398 struct process *process = (struct process *)obj;
399 return !process->running_threads;
402 static unsigned int process_map_access( struct object *obj, unsigned int access )
404 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
405 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
406 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
407 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
408 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
411 static void process_poll_event( struct fd *fd, int event )
413 struct process *process = get_fd_user( fd );
414 assert( process->obj.ops == &process_ops );
416 if (event & (POLLERR | POLLHUP))
418 release_object( process->msg_fd );
419 process->msg_fd = NULL;
420 if (process->sigkill_timeout) /* already waiting for it to die */
422 remove_timeout_user( process->sigkill_timeout );
423 process->sigkill_timeout = NULL;
424 process_died( process );
426 else kill_process( process, 0 );
428 else if (event & POLLIN) receive_fd( process );
431 static void startup_info_destroy( struct object *obj )
433 struct startup_info *info = (struct startup_info *)obj;
434 assert( obj->ops == &startup_info_ops );
435 free( info->data );
436 if (info->exe_file) release_object( info->exe_file );
437 if (info->process) release_object( info->process );
440 static void startup_info_dump( struct object *obj, int verbose )
442 struct startup_info *info = (struct startup_info *)obj;
443 assert( obj->ops == &startup_info_ops );
445 fprintf( stderr, "Startup info in=%p out=%p err=%p\n",
446 info->hstdin, info->hstdout, info->hstderr );
449 static int startup_info_signaled( struct object *obj, struct thread *thread )
451 struct startup_info *info = (struct startup_info *)obj;
452 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
455 /* get a process from an id (and increment the refcount) */
456 struct process *get_process_from_id( process_id_t id )
458 struct object *obj = get_ptid_entry( id );
460 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
461 set_error( STATUS_INVALID_PARAMETER );
462 return NULL;
465 /* get a process from a handle (and increment the refcount) */
466 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
468 return (struct process *)get_handle_obj( current->process, handle,
469 access, &process_ops );
472 /* find a dll from its base address */
473 static inline struct process_dll *find_process_dll( struct process *process, void *base )
475 struct process_dll *dll;
477 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
479 if (dll->base == base) return dll;
481 return NULL;
484 /* add a dll to a process list */
485 static struct process_dll *process_load_dll( struct process *process, struct file *file,
486 void *base, const WCHAR *filename, data_size_t name_len )
488 struct process_dll *dll;
490 /* make sure we don't already have one with the same base address */
491 if (find_process_dll( process, base ))
493 set_error( STATUS_INVALID_PARAMETER );
494 return NULL;
497 if ((dll = mem_alloc( sizeof(*dll) )))
499 dll->file = NULL;
500 dll->base = base;
501 dll->filename = NULL;
502 dll->namelen = name_len;
503 if (name_len && !(dll->filename = memdup( filename, name_len )))
505 free( dll );
506 return NULL;
508 if (file) dll->file = grab_file_unless_removable( file );
509 list_add_tail( &process->dlls, &dll->entry );
511 return dll;
514 /* remove a dll from a process list */
515 static void process_unload_dll( struct process *process, void *base )
517 struct process_dll *dll = find_process_dll( process, base );
519 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
521 if (dll->file) release_object( dll->file );
522 free( dll->filename );
523 list_remove( &dll->entry );
524 free( dll );
525 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
527 else set_error( STATUS_INVALID_PARAMETER );
530 /* terminate a process with the given exit code */
531 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
533 struct list *ptr;
535 if (skip && skip->process == process) /* move it to the end of the list */
537 assert( skip->state != TERMINATED );
538 list_remove( &skip->proc_entry );
539 list_add_tail( &process->thread_list, &skip->proc_entry );
542 grab_object( process ); /* make sure it doesn't get freed when threads die */
543 while ((ptr = list_head( &process->thread_list )))
545 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
547 if (exit_code) thread->exit_code = exit_code;
548 if (thread == skip) break;
549 kill_thread( thread, 1 );
551 release_object( process );
554 /* kill all processes */
555 void kill_all_processes( struct process *skip, int exit_code )
557 for (;;)
559 struct process *process;
561 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
563 if (process == skip) continue;
564 if (process->running_threads) break;
566 if (&process->entry == &process_list) break; /* no process found */
567 terminate_process( process, NULL, exit_code );
571 /* kill all processes being attached to a console renderer */
572 void kill_console_processes( struct thread *renderer, int exit_code )
574 for (;;) /* restart from the beginning of the list every time */
576 struct process *process;
578 /* find the first process being attached to 'renderer' and still running */
579 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
581 if (process == renderer->process) continue;
582 if (!process->running_threads) continue;
583 if (process->console && console_get_renderer( process->console ) == renderer) break;
585 if (&process->entry == &process_list) break; /* no process found */
586 terminate_process( process, NULL, exit_code );
590 /* a process has been killed (i.e. its last thread died) */
591 static void process_killed( struct process *process )
593 struct handle_table *handles;
594 struct list *ptr;
596 assert( list_empty( &process->thread_list ));
597 process->end_time = current_time;
598 close_process_desktop( process );
599 handles = process->handles;
600 process->handles = NULL;
601 if (handles) release_object( handles );
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 free( dll->filename );
611 list_remove( &dll->entry );
612 free( dll );
614 destroy_process_classes( process );
615 destroy_process_cursors( process );
616 remove_process_locks( process );
617 set_process_startup_state( process, STARTUP_ABORTED );
618 finish_process_tracing( process );
619 start_sigkill_timer( process );
620 wake_up( &process->obj, 0 );
623 /* add a thread to a process running threads list */
624 void add_process_thread( struct process *process, struct thread *thread )
626 list_add_tail( &process->thread_list, &thread->proc_entry );
627 if (!process->running_threads++)
629 running_processes++;
630 if (!process->is_system)
632 if (!user_processes++ && user_process_event)
633 reset_event( user_process_event );
636 grab_object( thread );
639 /* remove a thread from a process running threads list */
640 void remove_process_thread( struct process *process, struct thread *thread )
642 assert( process->running_threads > 0 );
643 assert( !list_empty( &process->thread_list ));
645 list_remove( &thread->proc_entry );
647 if (!--process->running_threads)
649 /* we have removed the last running thread, exit the process */
650 process->exit_code = thread->exit_code;
651 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
652 process_killed( process );
654 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
655 release_object( thread );
658 /* suspend all the threads of a process */
659 void suspend_process( struct process *process )
661 if (!process->suspend++)
663 struct list *ptr, *next;
665 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
667 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
668 if (!thread->suspend) stop_thread( thread );
673 /* resume all the threads of a process */
674 void resume_process( struct process *process )
676 assert (process->suspend > 0);
677 if (!--process->suspend)
679 struct list *ptr, *next;
681 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
683 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
684 if (!thread->suspend) wake_thread( thread );
689 /* kill a process on the spot */
690 void kill_process( struct process *process, int violent_death )
692 if (violent_death) terminate_process( process, NULL, 1 );
693 else
695 struct list *ptr;
697 grab_object( process ); /* make sure it doesn't get freed when threads die */
698 while ((ptr = list_head( &process->thread_list )))
700 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
701 kill_thread( thread, 0 );
703 release_object( process );
707 /* kill all processes being debugged by a given thread */
708 void kill_debugged_processes( struct thread *debugger, int exit_code )
710 for (;;) /* restart from the beginning of the list every time */
712 struct process *process;
714 /* find the first process being debugged by 'debugger' and still running */
715 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
717 if (!process->running_threads) continue;
718 if (process->debugger == debugger) break;
720 if (&process->entry == &process_list) break; /* no process found */
721 process->debugger = NULL;
722 terminate_process( process, NULL, exit_code );
727 /* trigger a breakpoint event in a given process */
728 void break_process( struct process *process )
730 struct thread *thread;
732 suspend_process( process );
734 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
736 if (thread->context) /* inside an exception event already */
738 break_thread( thread );
739 goto done;
742 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
743 else set_error( STATUS_ACCESS_DENIED );
744 done:
745 resume_process( process );
749 /* detach a debugger from all its debuggees */
750 void detach_debugged_processes( struct thread *debugger )
752 struct process *process;
754 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
756 if (process->debugger == debugger && process->running_threads)
758 debugger_detach( process, debugger );
764 void enum_processes( int (*cb)(struct process*, void*), void *user )
766 struct list *ptr, *next;
768 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
770 struct process *process = LIST_ENTRY( ptr, struct process, entry );
771 if ((cb)(process, user)) break;
775 /* set the debugged flag in the process PEB */
776 int set_process_debug_flag( struct process *process, int flag )
778 char data = (flag != 0);
780 /* BeingDebugged flag is the byte at offset 2 in the PEB */
781 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
784 /* take a snapshot of currently running processes */
785 struct process_snapshot *process_snap( int *count )
787 struct process_snapshot *snapshot, *ptr;
788 struct process *process;
790 if (!running_processes) return NULL;
791 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
792 return NULL;
793 ptr = snapshot;
794 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
796 if (!process->running_threads) continue;
797 ptr->process = process;
798 ptr->threads = process->running_threads;
799 ptr->count = process->obj.refcount;
800 ptr->priority = process->priority;
801 ptr->handles = get_handle_table_count(process);
802 grab_object( process );
803 ptr++;
806 if (!(*count = ptr - snapshot))
808 free( snapshot );
809 snapshot = NULL;
811 return snapshot;
814 /* take a snapshot of the modules of a process */
815 struct module_snapshot *module_snap( struct process *process, int *count )
817 struct module_snapshot *snapshot, *ptr;
818 struct process_dll *dll;
819 int total = 0;
821 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
822 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
824 ptr = snapshot;
825 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
827 ptr->base = dll->base;
828 ptr->size = dll->size;
829 ptr->namelen = dll->namelen;
830 ptr->filename = memdup( dll->filename, dll->namelen );
831 ptr++;
833 *count = total;
834 return snapshot;
838 /* create a new process */
839 DECL_HANDLER(new_process)
841 struct startup_info *info;
842 struct thread *thread;
843 struct process *process;
844 struct process *parent = current->process;
845 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
847 if (socket_fd == -1)
849 set_error( STATUS_INVALID_PARAMETER );
850 return;
852 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
854 set_error( STATUS_INVALID_HANDLE );
855 close( socket_fd );
856 return;
859 /* build the startup info for a new process */
860 if (!(info = alloc_object( &startup_info_ops ))) return;
861 info->hstdin = req->hstdin;
862 info->hstdout = req->hstdout;
863 info->hstderr = req->hstderr;
864 info->exe_file = NULL;
865 info->process = NULL;
866 info->data_size = get_req_data_size();
867 info->data = NULL;
869 if (req->exe_file &&
870 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
871 goto done;
873 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
875 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
876 process = thread->process;
877 process->create_flags = req->create_flags;
878 process->startup_info = (struct startup_info *)grab_object( info );
880 /* connect to the window station */
881 connect_process_winstation( process, current );
883 /* thread will be actually suspended in init_done */
884 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
886 /* set the process console */
887 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
889 /* FIXME: some better error checking should be done...
890 * like if hConOut and hConIn are console handles, then they should be on the same
891 * physical console
893 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
896 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
898 info->hstdin = duplicate_handle( parent, req->hstdin, process,
899 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
900 info->hstdout = duplicate_handle( parent, req->hstdout, process,
901 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
902 info->hstderr = duplicate_handle( parent, req->hstderr, process,
903 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
904 /* some handles above may have been invalid; this is not an error */
905 if (get_error() == STATUS_INVALID_HANDLE ||
906 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
909 /* attach to the debugger if requested */
910 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
911 set_process_debugger( process, current );
912 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
913 set_process_debugger( process, parent->debugger );
915 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
916 process->group_id = parent->group_id;
918 info->process = (struct process *)grab_object( process );
919 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
920 reply->pid = get_process_id( process );
921 reply->tid = get_thread_id( thread );
922 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
923 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
925 done:
926 release_object( info );
929 /* Retrieve information about a newly started process */
930 DECL_HANDLER(get_new_process_info)
932 struct startup_info *info;
934 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
935 0, &startup_info_ops )))
937 reply->success = is_process_init_done( info->process );
938 reply->exit_code = info->process->exit_code;
939 release_object( info );
943 /* Retrieve the new process startup info */
944 DECL_HANDLER(get_startup_info)
946 struct process *process = current->process;
947 struct startup_info *info = process->startup_info;
948 data_size_t size;
950 if (!info) return;
952 if (info->exe_file &&
953 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
955 reply->hstdin = info->hstdin;
956 reply->hstdout = info->hstdout;
957 reply->hstderr = info->hstderr;
959 /* we return the data directly without making a copy so this can only be called once */
960 size = info->data_size;
961 if (size > get_reply_max_size()) size = get_reply_max_size();
962 set_reply_data_ptr( info->data, size );
963 info->data = NULL;
964 info->data_size = 0;
967 /* signal the end of the process initialization */
968 DECL_HANDLER(init_process_done)
970 struct process_dll *dll;
971 struct process *process = current->process;
973 if (is_process_init_done(process))
975 set_error( STATUS_INVALID_PARAMETER );
976 return;
978 if (!(dll = find_process_dll( process, req->module )))
980 set_error( STATUS_DLL_NOT_FOUND );
981 return;
984 /* main exe is the first in the dll list */
985 list_remove( &dll->entry );
986 list_add_head( &process->dlls, &dll->entry );
988 generate_startup_debug_events( process, req->entry );
989 set_process_startup_state( process, STARTUP_DONE );
991 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
992 if (current->suspend + process->suspend > 0) stop_thread( current );
993 if (process->debugger) set_process_debug_flag( process, 1 );
996 /* open a handle to a process */
997 DECL_HANDLER(open_process)
999 struct process *process = get_process_from_id( req->pid );
1000 reply->handle = 0;
1001 if (process)
1003 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1004 release_object( process );
1008 /* terminate a process */
1009 DECL_HANDLER(terminate_process)
1011 struct process *process;
1013 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1015 reply->self = (current->process == process);
1016 terminate_process( process, current, req->exit_code );
1017 release_object( process );
1021 /* fetch information about a process */
1022 DECL_HANDLER(get_process_info)
1024 struct process *process;
1026 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1028 reply->pid = get_process_id( process );
1029 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1030 reply->exit_code = process->exit_code;
1031 reply->priority = process->priority;
1032 reply->affinity = process->affinity;
1033 reply->peb = process->peb;
1034 reply->start_time = process->start_time;
1035 reply->end_time = process->end_time;
1036 release_object( process );
1040 /* set information about a process */
1041 DECL_HANDLER(set_process_info)
1043 struct process *process;
1045 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1047 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1048 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1050 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1051 else process->affinity = req->affinity;
1053 release_object( process );
1057 /* read data from a process address space */
1058 DECL_HANDLER(read_process_memory)
1060 struct process *process;
1061 data_size_t len = get_reply_max_size();
1063 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1065 if (len)
1067 char *buffer = mem_alloc( len );
1068 if (buffer)
1070 if (read_process_memory( process, req->addr, len, buffer ))
1071 set_reply_data_ptr( buffer, len );
1072 else
1073 free( buffer );
1076 release_object( process );
1079 /* write data to a process address space */
1080 DECL_HANDLER(write_process_memory)
1082 struct process *process;
1084 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1086 data_size_t len = get_req_data_size();
1087 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1088 else set_error( STATUS_INVALID_PARAMETER );
1089 release_object( process );
1093 /* notify the server that a dll has been loaded */
1094 DECL_HANDLER(load_dll)
1096 struct process_dll *dll;
1097 struct file *file = NULL;
1099 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1100 return;
1102 if ((dll = process_load_dll( current->process, file, req->base,
1103 get_req_data(), get_req_data_size() )))
1105 dll->size = req->size;
1106 dll->dbg_offset = req->dbg_offset;
1107 dll->dbg_size = req->dbg_size;
1108 dll->name = req->name;
1109 /* only generate event if initialization is done */
1110 if (is_process_init_done( current->process ))
1111 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1113 if (file) release_object( file );
1116 /* notify the server that a dll is being unloaded */
1117 DECL_HANDLER(unload_dll)
1119 process_unload_dll( current->process, req->base );
1122 /* retrieve information about a module in a process */
1123 DECL_HANDLER(get_dll_info)
1125 struct process *process;
1127 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1129 struct process_dll *dll = find_process_dll( process, req->base_address );
1131 if (dll)
1133 reply->size = dll->size;
1134 reply->entry_point = NULL; /* FIXME */
1135 if (dll->filename)
1137 data_size_t len = min( dll->namelen, get_reply_max_size() );
1138 set_reply_data( dll->filename, len );
1141 else
1142 set_error( STATUS_DLL_NOT_FOUND );
1144 release_object( process );
1148 /* retrieve the process idle event */
1149 DECL_HANDLER(get_process_idle_event)
1151 struct process *process;
1153 reply->event = 0;
1154 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1156 if (process->idle_event && process != current->process)
1157 reply->event = alloc_handle( current->process, process->idle_event,
1158 EVENT_ALL_ACCESS, 0 );
1159 release_object( process );
1163 /* make the current process a system process */
1164 DECL_HANDLER(make_process_system)
1166 struct process *process = current->process;
1168 if (!user_process_event)
1170 if (!(user_process_event = create_event( NULL, NULL, 0, 1, 0 ))) return;
1171 make_object_static( (struct object *)user_process_event );
1174 if (!(reply->event = alloc_handle( current->process, user_process_event, EVENT_ALL_ACCESS, 0 )))
1175 return;
1177 if (!process->is_system)
1179 process->is_system = 1;
1180 if (!--user_processes) set_event( user_process_event );