ddraw: Hold the lock in IDirect3DMaterial methods.
[wine/dibdrv.git] / server / process.c
blobd733031d72842619f2e76dbf5373e8d634062a4f
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;
57 /* process operations */
59 static void process_dump( struct object *obj, int verbose );
60 static int process_signaled( struct object *obj, struct thread *thread );
61 static unsigned int process_map_access( struct object *obj, unsigned int access );
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_map_access, /* map_access */
76 no_lookup_name, /* lookup_name */
77 no_open_file, /* open_file */
78 no_close_handle, /* close_handle */
79 process_destroy /* destroy */
82 static const struct fd_ops process_fd_ops =
84 NULL, /* get_poll_events */
85 process_poll_event, /* poll_event */
86 NULL, /* flush */
87 NULL, /* get_fd_type */
88 NULL, /* ioctl */
89 NULL, /* queue_async */
90 NULL, /* reselect_async */
91 NULL /* cancel async */
94 /* process startup info */
96 struct startup_info
98 struct object obj; /* object header */
99 obj_handle_t hstdin; /* handle for stdin */
100 obj_handle_t hstdout; /* handle for stdout */
101 obj_handle_t hstderr; /* handle for stderr */
102 struct file *exe_file; /* file handle for main exe */
103 struct process *process; /* created process */
104 data_size_t data_size; /* size of startup data */
105 void *data; /* data for startup info */
108 static void startup_info_dump( struct object *obj, int verbose );
109 static int startup_info_signaled( struct object *obj, struct thread *thread );
110 static void startup_info_destroy( struct object *obj );
112 static const struct object_ops startup_info_ops =
114 sizeof(struct startup_info), /* size */
115 startup_info_dump, /* dump */
116 add_queue, /* add_queue */
117 remove_queue, /* remove_queue */
118 startup_info_signaled, /* signaled */
119 no_satisfied, /* satisfied */
120 no_signal, /* signal */
121 no_get_fd, /* get_fd */
122 no_map_access, /* map_access */
123 no_lookup_name, /* lookup_name */
124 no_open_file, /* open_file */
125 no_close_handle, /* close_handle */
126 startup_info_destroy /* destroy */
130 struct ptid_entry
132 void *ptr; /* entry ptr */
133 unsigned int next; /* next free entry */
136 static struct ptid_entry *ptid_entries; /* array of ptid entries */
137 static unsigned int used_ptid_entries; /* number of entries in use */
138 static unsigned int alloc_ptid_entries; /* number of allocated entries */
139 static unsigned int next_free_ptid; /* next free entry */
140 static unsigned int last_free_ptid; /* last free entry */
142 #define PTID_OFFSET 8 /* offset for first ptid value */
144 /* allocate a new process or thread id */
145 unsigned int alloc_ptid( void *ptr )
147 struct ptid_entry *entry;
148 unsigned int id;
150 if (used_ptid_entries < alloc_ptid_entries)
152 id = used_ptid_entries + PTID_OFFSET;
153 entry = &ptid_entries[used_ptid_entries++];
155 else if (next_free_ptid)
157 id = next_free_ptid;
158 entry = &ptid_entries[id - PTID_OFFSET];
159 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
161 else /* need to grow the array */
163 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
164 if (!count) count = 64;
165 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
167 set_error( STATUS_NO_MEMORY );
168 return 0;
170 ptid_entries = entry;
171 alloc_ptid_entries = count;
172 id = used_ptid_entries + PTID_OFFSET;
173 entry = &ptid_entries[used_ptid_entries++];
176 entry->ptr = ptr;
177 return id;
180 /* free a process or thread id */
181 void free_ptid( unsigned int id )
183 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
185 entry->ptr = NULL;
186 entry->next = 0;
188 /* append to end of free list so that we don't reuse it too early */
189 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
190 else next_free_ptid = id;
192 last_free_ptid = id;
195 /* retrieve the pointer corresponding to a process or thread id */
196 void *get_ptid_entry( unsigned int id )
198 if (id < PTID_OFFSET) return NULL;
199 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
200 return ptid_entries[id - PTID_OFFSET].ptr;
203 /* return the main thread of the process */
204 struct thread *get_process_first_thread( struct process *process )
206 struct list *ptr = list_head( &process->thread_list );
207 if (!ptr) return NULL;
208 return LIST_ENTRY( ptr, struct thread, proc_entry );
211 /* set the state of the process startup info */
212 static void set_process_startup_state( struct process *process, enum startup_state state )
214 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
215 if (process->startup_info)
217 wake_up( &process->startup_info->obj, 0 );
218 release_object( process->startup_info );
219 process->startup_info = NULL;
223 /* final cleanup once we are sure a process is really dead */
224 static void process_died( struct process *process )
226 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
227 release_object( process );
228 if (!--running_processes) close_master_socket();
231 /* callback for process sigkill timeout */
232 static void process_sigkill( void *private )
234 struct process *process = private;
236 process->sigkill_timeout = NULL;
237 kill( process->unix_pid, SIGKILL );
238 process_died( process );
241 /* start the sigkill timer for a process upon exit */
242 static void start_sigkill_timer( struct process *process )
244 grab_object( process );
245 if (process->unix_pid != -1 && process->msg_fd)
246 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
247 else
248 process_died( process );
251 /* create a new process and its main thread */
252 /* if the function fails the fd is closed */
253 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
255 struct process *process;
256 struct thread *thread = NULL;
257 int request_pipe[2];
259 if (!(process = alloc_object( &process_ops )))
261 close( fd );
262 goto error;
264 process->parent = NULL;
265 process->debugger = NULL;
266 process->handles = NULL;
267 process->msg_fd = NULL;
268 process->sigkill_timeout = NULL;
269 process->unix_pid = -1;
270 process->exit_code = STILL_ACTIVE;
271 process->running_threads = 0;
272 process->priority = PROCESS_PRIOCLASS_NORMAL;
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->token = NULL;
286 process->trace_data = 0;
287 list_init( &process->thread_list );
288 list_init( &process->locks );
289 list_init( &process->classes );
290 list_init( &process->dlls );
292 process->start_time = current_time;
293 process->end_time = 0;
294 list_add_head( &process_list, &process->entry );
296 if (!(process->id = process->group_id = alloc_ptid( process )))
298 close( fd );
299 goto error;
301 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
303 /* create the handle table */
304 if (!parent_thread)
306 process->handles = alloc_handle_table( process, 0 );
307 process->token = token_create_admin();
309 else
311 struct process *parent = parent_thread->process;
312 process->parent = (struct process *)grab_object( parent );
313 process->handles = inherit_all ? copy_handle_table( process, parent )
314 : alloc_handle_table( process, 0 );
315 /* Note: for security reasons, starting a new process does not attempt
316 * to use the current impersonation token for the new process */
317 process->token = token_duplicate( parent->token, TRUE, 0 );
319 if (!process->handles || !process->token) goto error;
321 /* create the main thread */
322 if (pipe( request_pipe ) == -1)
324 file_set_error();
325 goto error;
327 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
329 close( request_pipe[0] );
330 close( request_pipe[1] );
331 goto error;
333 close( request_pipe[1] );
334 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
336 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
337 release_object( process );
338 return thread;
340 error:
341 if (process) release_object( process );
342 /* if we failed to start our first process, close everything down */
343 if (!running_processes) close_master_socket();
344 return NULL;
347 /* initialize the current process and fill in the request */
348 data_size_t init_process( struct thread *thread )
350 struct process *process = thread->process;
351 struct startup_info *info = process->startup_info;
353 init_process_tracing( process );
354 if (!info) return 0;
355 return info->data_size;
358 /* destroy a process when its refcount is 0 */
359 static void process_destroy( struct object *obj )
361 struct process *process = (struct process *)obj;
362 assert( obj->ops == &process_ops );
364 /* we can't have a thread remaining */
365 assert( list_empty( &process->thread_list ));
367 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
369 set_process_startup_state( process, STARTUP_ABORTED );
370 if (process->console) release_object( process->console );
371 if (process->parent) release_object( process->parent );
372 if (process->msg_fd) release_object( process->msg_fd );
373 list_remove( &process->entry );
374 if (process->idle_event) release_object( process->idle_event );
375 if (process->queue) release_object( process->queue );
376 if (process->id) free_ptid( process->id );
377 if (process->token) release_object( process->token );
380 /* dump a process on stdout for debugging purposes */
381 static void process_dump( struct object *obj, int verbose )
383 struct process *process = (struct process *)obj;
384 assert( obj->ops == &process_ops );
386 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
389 static int process_signaled( struct object *obj, struct thread *thread )
391 struct process *process = (struct process *)obj;
392 return !process->running_threads;
395 static unsigned int process_map_access( struct object *obj, unsigned int access )
397 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
398 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
399 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
400 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
401 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
404 static void process_poll_event( struct fd *fd, int event )
406 struct process *process = get_fd_user( fd );
407 assert( process->obj.ops == &process_ops );
409 if (event & (POLLERR | POLLHUP))
411 release_object( process->msg_fd );
412 process->msg_fd = NULL;
413 if (process->sigkill_timeout) /* already waiting for it to die */
415 remove_timeout_user( process->sigkill_timeout );
416 process->sigkill_timeout = NULL;
417 process_died( process );
419 else kill_process( process, 0 );
421 else if (event & POLLIN) receive_fd( process );
424 static void startup_info_destroy( struct object *obj )
426 struct startup_info *info = (struct startup_info *)obj;
427 assert( obj->ops == &startup_info_ops );
428 free( info->data );
429 if (info->exe_file) release_object( info->exe_file );
430 if (info->process) release_object( info->process );
433 static void startup_info_dump( struct object *obj, int verbose )
435 struct startup_info *info = (struct startup_info *)obj;
436 assert( obj->ops == &startup_info_ops );
438 fprintf( stderr, "Startup info in=%p out=%p err=%p\n",
439 info->hstdin, info->hstdout, info->hstderr );
442 static int startup_info_signaled( struct object *obj, struct thread *thread )
444 struct startup_info *info = (struct startup_info *)obj;
445 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
448 /* get a process from an id (and increment the refcount) */
449 struct process *get_process_from_id( process_id_t id )
451 struct object *obj = get_ptid_entry( id );
453 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
454 set_error( STATUS_INVALID_PARAMETER );
455 return NULL;
458 /* get a process from a handle (and increment the refcount) */
459 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
461 return (struct process *)get_handle_obj( current->process, handle,
462 access, &process_ops );
465 /* find a dll from its base address */
466 static inline struct process_dll *find_process_dll( struct process *process, void *base )
468 struct process_dll *dll;
470 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
472 if (dll->base == base) return dll;
474 return NULL;
477 /* add a dll to a process list */
478 static struct process_dll *process_load_dll( struct process *process, struct file *file,
479 void *base, const WCHAR *filename, data_size_t name_len )
481 struct process_dll *dll;
483 /* make sure we don't already have one with the same base address */
484 if (find_process_dll( process, base ))
486 set_error( STATUS_INVALID_PARAMETER );
487 return NULL;
490 if ((dll = mem_alloc( sizeof(*dll) )))
492 dll->file = NULL;
493 dll->base = base;
494 dll->filename = NULL;
495 dll->namelen = name_len;
496 if (name_len && !(dll->filename = memdup( filename, name_len )))
498 free( dll );
499 return NULL;
501 if (file) dll->file = grab_file_unless_removable( file );
502 list_add_tail( &process->dlls, &dll->entry );
504 return dll;
507 /* remove a dll from a process list */
508 static void process_unload_dll( struct process *process, void *base )
510 struct process_dll *dll = find_process_dll( process, base );
512 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
514 if (dll->file) release_object( dll->file );
515 free( dll->filename );
516 list_remove( &dll->entry );
517 free( dll );
518 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
520 else set_error( STATUS_INVALID_PARAMETER );
523 /* terminate a process with the given exit code */
524 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
526 struct list *ptr;
528 if (skip && skip->process == process) /* move it to the end of the list */
530 assert( skip->state != TERMINATED );
531 list_remove( &skip->proc_entry );
532 list_add_tail( &process->thread_list, &skip->proc_entry );
535 grab_object( process ); /* make sure it doesn't get freed when threads die */
536 while ((ptr = list_head( &process->thread_list )))
538 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
540 if (exit_code) thread->exit_code = exit_code;
541 if (thread == skip) break;
542 kill_thread( thread, 1 );
544 release_object( process );
547 /* kill all processes */
548 void kill_all_processes( struct process *skip, int exit_code )
550 for (;;)
552 struct process *process;
554 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
556 if (process == skip) continue;
557 if (process->running_threads) break;
559 if (&process->entry == &process_list) break; /* no process found */
560 terminate_process( process, NULL, exit_code );
564 /* kill all processes being attached to a console renderer */
565 void kill_console_processes( struct thread *renderer, int exit_code )
567 for (;;) /* restart from the beginning of the list every time */
569 struct process *process;
571 /* find the first process being attached to 'renderer' and still running */
572 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
574 if (process == renderer->process) continue;
575 if (!process->running_threads) continue;
576 if (process->console && console_get_renderer( process->console ) == renderer) break;
578 if (&process->entry == &process_list) break; /* no process found */
579 terminate_process( process, NULL, exit_code );
583 /* a process has been killed (i.e. its last thread died) */
584 static void process_killed( struct process *process )
586 struct handle_table *handles;
587 struct list *ptr;
589 assert( list_empty( &process->thread_list ));
590 process->end_time = current_time;
591 close_process_desktop( process );
592 handles = process->handles;
593 process->handles = NULL;
594 if (handles) release_object( handles );
596 /* close the console attached to this process, if any */
597 free_console( process );
599 while ((ptr = list_head( &process->dlls )))
601 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
602 if (dll->file) release_object( dll->file );
603 free( dll->filename );
604 list_remove( &dll->entry );
605 free( dll );
607 destroy_process_classes( process );
608 remove_process_locks( process );
609 set_process_startup_state( process, STARTUP_ABORTED );
610 finish_process_tracing( process );
611 start_sigkill_timer( process );
612 wake_up( &process->obj, 0 );
615 /* add a thread to a process running threads list */
616 void add_process_thread( struct process *process, struct thread *thread )
618 list_add_tail( &process->thread_list, &thread->proc_entry );
619 if (!process->running_threads++) running_processes++;
620 grab_object( thread );
623 /* remove a thread from a process running threads list */
624 void remove_process_thread( struct process *process, struct thread *thread )
626 assert( process->running_threads > 0 );
627 assert( !list_empty( &process->thread_list ));
629 list_remove( &thread->proc_entry );
631 if (!--process->running_threads)
633 /* we have removed the last running thread, exit the process */
634 process->exit_code = thread->exit_code;
635 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
636 process_killed( process );
638 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
639 release_object( thread );
642 /* suspend all the threads of a process */
643 void suspend_process( struct process *process )
645 if (!process->suspend++)
647 struct list *ptr, *next;
649 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
651 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
652 if (!thread->suspend) stop_thread( thread );
657 /* resume all the threads of a process */
658 void resume_process( struct process *process )
660 assert (process->suspend > 0);
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) wake_thread( thread );
673 /* kill a process on the spot */
674 void kill_process( struct process *process, int violent_death )
676 if (violent_death) terminate_process( process, NULL, 1 );
677 else
679 struct list *ptr;
681 grab_object( process ); /* make sure it doesn't get freed when threads die */
682 while ((ptr = list_head( &process->thread_list )))
684 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
685 kill_thread( thread, 0 );
687 release_object( process );
691 /* kill all processes being debugged by a given thread */
692 void kill_debugged_processes( struct thread *debugger, int exit_code )
694 for (;;) /* restart from the beginning of the list every time */
696 struct process *process;
698 /* find the first process being debugged by 'debugger' and still running */
699 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
701 if (!process->running_threads) continue;
702 if (process->debugger == debugger) break;
704 if (&process->entry == &process_list) break; /* no process found */
705 process->debugger = NULL;
706 terminate_process( process, NULL, exit_code );
711 /* trigger a breakpoint event in a given process */
712 void break_process( struct process *process )
714 struct thread *thread;
716 suspend_process( process );
718 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
720 if (thread->context) /* inside an exception event already */
722 break_thread( thread );
723 goto done;
726 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
727 else set_error( STATUS_ACCESS_DENIED );
728 done:
729 resume_process( process );
733 /* detach a debugger from all its debuggees */
734 void detach_debugged_processes( struct thread *debugger )
736 struct process *process;
738 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
740 if (process->debugger == debugger && process->running_threads)
742 debugger_detach( process, debugger );
748 void enum_processes( int (*cb)(struct process*, void*), void *user )
750 struct list *ptr, *next;
752 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
754 struct process *process = LIST_ENTRY( ptr, struct process, entry );
755 if ((cb)(process, user)) break;
759 /* set the debugged flag in the process PEB */
760 int set_process_debug_flag( struct process *process, int flag )
762 char data = (flag != 0);
764 /* BeingDebugged flag is the byte at offset 2 in the PEB */
765 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
768 /* take a snapshot of currently running processes */
769 struct process_snapshot *process_snap( int *count )
771 struct process_snapshot *snapshot, *ptr;
772 struct process *process;
774 if (!running_processes) return NULL;
775 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
776 return NULL;
777 ptr = snapshot;
778 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
780 if (!process->running_threads) continue;
781 ptr->process = process;
782 ptr->threads = process->running_threads;
783 ptr->count = process->obj.refcount;
784 ptr->priority = process->priority;
785 ptr->handles = get_handle_table_count(process);
786 grab_object( process );
787 ptr++;
790 if (!(*count = ptr - snapshot))
792 free( snapshot );
793 snapshot = NULL;
795 return snapshot;
798 /* take a snapshot of the modules of a process */
799 struct module_snapshot *module_snap( struct process *process, int *count )
801 struct module_snapshot *snapshot, *ptr;
802 struct process_dll *dll;
803 int total = 0;
805 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
806 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
808 ptr = snapshot;
809 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
811 ptr->base = dll->base;
812 ptr->size = dll->size;
813 ptr->namelen = dll->namelen;
814 ptr->filename = memdup( dll->filename, dll->namelen );
815 ptr++;
817 *count = total;
818 return snapshot;
822 /* create a new process */
823 DECL_HANDLER(new_process)
825 struct startup_info *info;
826 struct thread *thread;
827 struct process *process;
828 struct process *parent = current->process;
829 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
831 if (socket_fd == -1)
833 set_error( STATUS_INVALID_PARAMETER );
834 return;
836 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
838 set_error( STATUS_INVALID_HANDLE );
839 close( socket_fd );
840 return;
843 /* build the startup info for a new process */
844 if (!(info = alloc_object( &startup_info_ops ))) return;
845 info->hstdin = req->hstdin;
846 info->hstdout = req->hstdout;
847 info->hstderr = req->hstderr;
848 info->exe_file = NULL;
849 info->process = NULL;
850 info->data_size = get_req_data_size();
851 info->data = NULL;
853 if (req->exe_file &&
854 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
855 goto done;
857 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
859 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
860 process = thread->process;
861 process->create_flags = req->create_flags;
862 process->startup_info = (struct startup_info *)grab_object( info );
864 /* connect to the window station */
865 connect_process_winstation( process, current );
867 /* thread will be actually suspended in init_done */
868 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
870 /* set the process console */
871 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
873 /* FIXME: some better error checking should be done...
874 * like if hConOut and hConIn are console handles, then they should be on the same
875 * physical console
877 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
880 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
882 info->hstdin = duplicate_handle( parent, req->hstdin, process,
883 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
884 info->hstdout = duplicate_handle( parent, req->hstdout, process,
885 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
886 info->hstderr = duplicate_handle( parent, req->hstderr, process,
887 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
888 /* some handles above may have been invalid; this is not an error */
889 if (get_error() == STATUS_INVALID_HANDLE ||
890 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
893 /* attach to the debugger if requested */
894 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
895 set_process_debugger( process, current );
896 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
897 set_process_debugger( process, parent->debugger );
899 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
900 process->group_id = parent->group_id;
902 info->process = (struct process *)grab_object( process );
903 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
904 reply->pid = get_process_id( process );
905 reply->tid = get_thread_id( thread );
906 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
907 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
909 done:
910 release_object( info );
913 /* Retrieve information about a newly started process */
914 DECL_HANDLER(get_new_process_info)
916 struct startup_info *info;
918 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
919 0, &startup_info_ops )))
921 reply->success = is_process_init_done( info->process );
922 reply->exit_code = info->process->exit_code;
923 release_object( info );
927 /* Retrieve the new process startup info */
928 DECL_HANDLER(get_startup_info)
930 struct process *process = current->process;
931 struct startup_info *info = process->startup_info;
932 data_size_t size;
934 if (!info) return;
936 if (info->exe_file &&
937 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
939 reply->hstdin = info->hstdin;
940 reply->hstdout = info->hstdout;
941 reply->hstderr = info->hstderr;
943 /* we return the data directly without making a copy so this can only be called once */
944 size = info->data_size;
945 if (size > get_reply_max_size()) size = get_reply_max_size();
946 set_reply_data_ptr( info->data, size );
947 info->data = NULL;
948 info->data_size = 0;
951 /* signal the end of the process initialization */
952 DECL_HANDLER(init_process_done)
954 struct process_dll *dll;
955 struct process *process = current->process;
957 if (is_process_init_done(process))
959 set_error( STATUS_INVALID_PARAMETER );
960 return;
962 if (!(dll = find_process_dll( process, req->module )))
964 set_error( STATUS_DLL_NOT_FOUND );
965 return;
968 /* main exe is the first in the dll list */
969 list_remove( &dll->entry );
970 list_add_head( &process->dlls, &dll->entry );
972 generate_startup_debug_events( process, req->entry );
973 set_process_startup_state( process, STARTUP_DONE );
975 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
976 if (current->suspend + process->suspend > 0) stop_thread( current );
977 if (process->debugger) set_process_debug_flag( process, 1 );
980 /* open a handle to a process */
981 DECL_HANDLER(open_process)
983 struct process *process = get_process_from_id( req->pid );
984 reply->handle = 0;
985 if (process)
987 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
988 release_object( process );
992 /* terminate a process */
993 DECL_HANDLER(terminate_process)
995 struct process *process;
997 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
999 reply->self = (current->process == process);
1000 terminate_process( process, current, req->exit_code );
1001 release_object( process );
1005 /* fetch information about a process */
1006 DECL_HANDLER(get_process_info)
1008 struct process *process;
1010 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1012 reply->pid = get_process_id( process );
1013 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1014 reply->exit_code = process->exit_code;
1015 reply->priority = process->priority;
1016 reply->affinity = process->affinity;
1017 reply->peb = process->peb;
1018 reply->start_time = process->start_time;
1019 reply->end_time = process->end_time;
1020 release_object( process );
1024 /* set information about a process */
1025 DECL_HANDLER(set_process_info)
1027 struct process *process;
1029 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1031 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1032 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1034 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1035 else process->affinity = req->affinity;
1037 release_object( process );
1041 /* read data from a process address space */
1042 DECL_HANDLER(read_process_memory)
1044 struct process *process;
1045 data_size_t len = get_reply_max_size();
1047 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1049 if (len)
1051 char *buffer = mem_alloc( len );
1052 if (buffer)
1054 if (read_process_memory( process, req->addr, len, buffer ))
1055 set_reply_data_ptr( buffer, len );
1056 else
1057 free( buffer );
1060 release_object( process );
1063 /* write data to a process address space */
1064 DECL_HANDLER(write_process_memory)
1066 struct process *process;
1068 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1070 data_size_t len = get_req_data_size();
1071 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1072 else set_error( STATUS_INVALID_PARAMETER );
1073 release_object( process );
1077 /* notify the server that a dll has been loaded */
1078 DECL_HANDLER(load_dll)
1080 struct process_dll *dll;
1081 struct file *file = NULL;
1083 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1084 return;
1086 if ((dll = process_load_dll( current->process, file, req->base,
1087 get_req_data(), get_req_data_size() )))
1089 dll->size = req->size;
1090 dll->dbg_offset = req->dbg_offset;
1091 dll->dbg_size = req->dbg_size;
1092 dll->name = req->name;
1093 /* only generate event if initialization is done */
1094 if (is_process_init_done( current->process ))
1095 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1097 if (file) release_object( file );
1100 /* notify the server that a dll is being unloaded */
1101 DECL_HANDLER(unload_dll)
1103 process_unload_dll( current->process, req->base );
1106 /* retrieve information about a module in a process */
1107 DECL_HANDLER(get_dll_info)
1109 struct process *process;
1111 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1113 struct process_dll *dll = find_process_dll( process, req->base_address );
1115 if (dll)
1117 reply->size = dll->size;
1118 reply->entry_point = NULL; /* FIXME */
1119 if (dll->filename)
1121 data_size_t len = min( dll->namelen, get_reply_max_size() );
1122 set_reply_data( dll->filename, len );
1125 else
1126 set_error( STATUS_DLL_NOT_FOUND );
1128 release_object( process );
1132 /* retrieve the process idle event */
1133 DECL_HANDLER(get_process_idle_event)
1135 struct process *process;
1137 reply->event = 0;
1138 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1140 if (process->idle_event && process != current->process)
1141 reply->event = alloc_handle( current->process, process->idle_event,
1142 EVENT_ALL_ACCESS, 0 );
1143 release_object( process );