winebuild: Add IMAGE_FILE_32BIT_MACHINE flag for a 32-bit build.
[wine/wine64.git] / server / process.c
blob623d2ae9914615ea73f8a24faee38e68f0d1059d
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 remove_process_locks( process );
616 set_process_startup_state( process, STARTUP_ABORTED );
617 finish_process_tracing( process );
618 start_sigkill_timer( process );
619 wake_up( &process->obj, 0 );
622 /* add a thread to a process running threads list */
623 void add_process_thread( struct process *process, struct thread *thread )
625 list_add_tail( &process->thread_list, &thread->proc_entry );
626 if (!process->running_threads++)
628 running_processes++;
629 if (!process->is_system)
631 if (!user_processes++ && user_process_event)
632 reset_event( user_process_event );
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, int violent_death )
691 if (violent_death) terminate_process( process, NULL, 1 );
692 else
694 struct list *ptr;
696 grab_object( process ); /* make sure it doesn't get freed when threads die */
697 while ((ptr = list_head( &process->thread_list )))
699 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
700 kill_thread( thread, 0 );
702 release_object( process );
706 /* kill all processes being debugged by a given thread */
707 void kill_debugged_processes( struct thread *debugger, int exit_code )
709 for (;;) /* restart from the beginning of the list every time */
711 struct process *process;
713 /* find the first process being debugged by 'debugger' and still running */
714 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
716 if (!process->running_threads) continue;
717 if (process->debugger == debugger) break;
719 if (&process->entry == &process_list) break; /* no process found */
720 process->debugger = NULL;
721 terminate_process( process, NULL, exit_code );
726 /* trigger a breakpoint event in a given process */
727 void break_process( struct process *process )
729 struct thread *thread;
731 suspend_process( process );
733 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
735 if (thread->context) /* inside an exception event already */
737 break_thread( thread );
738 goto done;
741 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
742 else set_error( STATUS_ACCESS_DENIED );
743 done:
744 resume_process( process );
748 /* detach a debugger from all its debuggees */
749 void detach_debugged_processes( struct thread *debugger )
751 struct process *process;
753 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
755 if (process->debugger == debugger && process->running_threads)
757 debugger_detach( process, debugger );
763 void enum_processes( int (*cb)(struct process*, void*), void *user )
765 struct list *ptr, *next;
767 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
769 struct process *process = LIST_ENTRY( ptr, struct process, entry );
770 if ((cb)(process, user)) break;
774 /* set the debugged flag in the process PEB */
775 int set_process_debug_flag( struct process *process, int flag )
777 char data = (flag != 0);
779 /* BeingDebugged flag is the byte at offset 2 in the PEB */
780 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
783 /* take a snapshot of currently running processes */
784 struct process_snapshot *process_snap( int *count )
786 struct process_snapshot *snapshot, *ptr;
787 struct process *process;
789 if (!running_processes) return NULL;
790 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
791 return NULL;
792 ptr = snapshot;
793 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
795 if (!process->running_threads) continue;
796 ptr->process = process;
797 ptr->threads = process->running_threads;
798 ptr->count = process->obj.refcount;
799 ptr->priority = process->priority;
800 ptr->handles = get_handle_table_count(process);
801 grab_object( process );
802 ptr++;
805 if (!(*count = ptr - snapshot))
807 free( snapshot );
808 snapshot = NULL;
810 return snapshot;
813 /* take a snapshot of the modules of a process */
814 struct module_snapshot *module_snap( struct process *process, int *count )
816 struct module_snapshot *snapshot, *ptr;
817 struct process_dll *dll;
818 int total = 0;
820 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
821 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
823 ptr = snapshot;
824 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
826 ptr->base = dll->base;
827 ptr->size = dll->size;
828 ptr->namelen = dll->namelen;
829 ptr->filename = memdup( dll->filename, dll->namelen );
830 ptr++;
832 *count = total;
833 return snapshot;
837 /* create a new process */
838 DECL_HANDLER(new_process)
840 struct startup_info *info;
841 struct thread *thread;
842 struct process *process;
843 struct process *parent = current->process;
844 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
846 if (socket_fd == -1)
848 set_error( STATUS_INVALID_PARAMETER );
849 return;
851 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
853 set_error( STATUS_INVALID_HANDLE );
854 close( socket_fd );
855 return;
858 /* build the startup info for a new process */
859 if (!(info = alloc_object( &startup_info_ops ))) return;
860 info->hstdin = req->hstdin;
861 info->hstdout = req->hstdout;
862 info->hstderr = req->hstderr;
863 info->exe_file = NULL;
864 info->process = NULL;
865 info->data_size = get_req_data_size();
866 info->data = NULL;
868 if (req->exe_file &&
869 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
870 goto done;
872 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
874 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
875 process = thread->process;
876 process->create_flags = req->create_flags;
877 process->startup_info = (struct startup_info *)grab_object( info );
879 /* connect to the window station */
880 connect_process_winstation( process, current );
882 /* thread will be actually suspended in init_done */
883 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
885 /* set the process console */
886 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
888 /* FIXME: some better error checking should be done...
889 * like if hConOut and hConIn are console handles, then they should be on the same
890 * physical console
892 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
895 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
897 info->hstdin = duplicate_handle( parent, req->hstdin, process,
898 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
899 info->hstdout = duplicate_handle( parent, req->hstdout, process,
900 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
901 info->hstderr = duplicate_handle( parent, req->hstderr, process,
902 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
903 /* some handles above may have been invalid; this is not an error */
904 if (get_error() == STATUS_INVALID_HANDLE ||
905 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
908 /* attach to the debugger if requested */
909 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
910 set_process_debugger( process, current );
911 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
912 set_process_debugger( process, parent->debugger );
914 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
915 process->group_id = parent->group_id;
917 info->process = (struct process *)grab_object( process );
918 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
919 reply->pid = get_process_id( process );
920 reply->tid = get_thread_id( thread );
921 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
922 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
924 done:
925 release_object( info );
928 /* Retrieve information about a newly started process */
929 DECL_HANDLER(get_new_process_info)
931 struct startup_info *info;
933 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
934 0, &startup_info_ops )))
936 reply->success = is_process_init_done( info->process );
937 reply->exit_code = info->process->exit_code;
938 release_object( info );
942 /* Retrieve the new process startup info */
943 DECL_HANDLER(get_startup_info)
945 struct process *process = current->process;
946 struct startup_info *info = process->startup_info;
947 data_size_t size;
949 if (!info) return;
951 if (info->exe_file &&
952 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
954 reply->hstdin = info->hstdin;
955 reply->hstdout = info->hstdout;
956 reply->hstderr = info->hstderr;
958 /* we return the data directly without making a copy so this can only be called once */
959 size = info->data_size;
960 if (size > get_reply_max_size()) size = get_reply_max_size();
961 set_reply_data_ptr( info->data, size );
962 info->data = NULL;
963 info->data_size = 0;
966 /* signal the end of the process initialization */
967 DECL_HANDLER(init_process_done)
969 struct process_dll *dll;
970 struct process *process = current->process;
972 if (is_process_init_done(process))
974 set_error( STATUS_INVALID_PARAMETER );
975 return;
977 if (!(dll = find_process_dll( process, req->module )))
979 set_error( STATUS_DLL_NOT_FOUND );
980 return;
983 /* main exe is the first in the dll list */
984 list_remove( &dll->entry );
985 list_add_head( &process->dlls, &dll->entry );
987 generate_startup_debug_events( process, req->entry );
988 set_process_startup_state( process, STARTUP_DONE );
990 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
991 if (current->suspend + process->suspend > 0) stop_thread( current );
992 if (process->debugger) set_process_debug_flag( process, 1 );
995 /* open a handle to a process */
996 DECL_HANDLER(open_process)
998 struct process *process = get_process_from_id( req->pid );
999 reply->handle = 0;
1000 if (process)
1002 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1003 release_object( process );
1007 /* terminate a process */
1008 DECL_HANDLER(terminate_process)
1010 struct process *process;
1012 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
1014 reply->self = (current->process == process);
1015 terminate_process( process, current, req->exit_code );
1016 release_object( process );
1020 /* fetch information about a process */
1021 DECL_HANDLER(get_process_info)
1023 struct process *process;
1025 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1027 reply->pid = get_process_id( process );
1028 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1029 reply->exit_code = process->exit_code;
1030 reply->priority = process->priority;
1031 reply->affinity = process->affinity;
1032 reply->peb = process->peb;
1033 reply->start_time = process->start_time;
1034 reply->end_time = process->end_time;
1035 release_object( process );
1039 /* set information about a process */
1040 DECL_HANDLER(set_process_info)
1042 struct process *process;
1044 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1046 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1047 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1049 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1050 else process->affinity = req->affinity;
1052 release_object( process );
1056 /* read data from a process address space */
1057 DECL_HANDLER(read_process_memory)
1059 struct process *process;
1060 data_size_t len = get_reply_max_size();
1062 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1064 if (len)
1066 char *buffer = mem_alloc( len );
1067 if (buffer)
1069 if (read_process_memory( process, req->addr, len, buffer ))
1070 set_reply_data_ptr( buffer, len );
1071 else
1072 free( buffer );
1075 release_object( process );
1078 /* write data to a process address space */
1079 DECL_HANDLER(write_process_memory)
1081 struct process *process;
1083 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1085 data_size_t len = get_req_data_size();
1086 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1087 else set_error( STATUS_INVALID_PARAMETER );
1088 release_object( process );
1092 /* notify the server that a dll has been loaded */
1093 DECL_HANDLER(load_dll)
1095 struct process_dll *dll;
1096 struct file *file = NULL;
1098 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1099 return;
1101 if ((dll = process_load_dll( current->process, file, req->base,
1102 get_req_data(), get_req_data_size() )))
1104 dll->size = req->size;
1105 dll->dbg_offset = req->dbg_offset;
1106 dll->dbg_size = req->dbg_size;
1107 dll->name = req->name;
1108 /* only generate event if initialization is done */
1109 if (is_process_init_done( current->process ))
1110 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1112 if (file) release_object( file );
1115 /* notify the server that a dll is being unloaded */
1116 DECL_HANDLER(unload_dll)
1118 process_unload_dll( current->process, req->base );
1121 /* retrieve information about a module in a process */
1122 DECL_HANDLER(get_dll_info)
1124 struct process *process;
1126 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1128 struct process_dll *dll = find_process_dll( process, req->base_address );
1130 if (dll)
1132 reply->size = dll->size;
1133 reply->entry_point = NULL; /* FIXME */
1134 if (dll->filename)
1136 data_size_t len = min( dll->namelen, get_reply_max_size() );
1137 set_reply_data( dll->filename, len );
1140 else
1141 set_error( STATUS_DLL_NOT_FOUND );
1143 release_object( process );
1147 /* retrieve the process idle event */
1148 DECL_HANDLER(get_process_idle_event)
1150 struct process *process;
1152 reply->event = 0;
1153 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1155 if (process->idle_event && process != current->process)
1156 reply->event = alloc_handle( current->process, process->idle_event,
1157 EVENT_ALL_ACCESS, 0 );
1158 release_object( process );
1162 /* make the current process a system process */
1163 DECL_HANDLER(make_process_system)
1165 struct process *process = current->process;
1167 if (!user_process_event)
1169 if (!(user_process_event = create_event( NULL, NULL, 0, 1, 0 ))) return;
1170 make_object_static( (struct object *)user_process_event );
1173 if (!(reply->event = alloc_handle( current->process, user_process_event, EVENT_ALL_ACCESS, 0 )))
1174 return;
1176 if (!process->is_system)
1178 process->is_system = 1;
1179 if (!--user_processes) set_event( user_process_event );