push fff380ca74633e3441fcec4153e8b8fd9a4ac57f
[wine/hacks.git] / server / process.c
blobaee351ae068aab88e0c596991cf882ea9df3f21f
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 "console.h"
50 #include "user.h"
51 #include "security.h"
53 /* process structure */
55 static struct list process_list = LIST_INIT(process_list);
56 static int running_processes;
58 /* process operations */
60 static void process_dump( struct object *obj, int verbose );
61 static int process_signaled( struct object *obj, struct thread *thread );
62 static 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 no_flush, /* flush */
88 no_get_file_info, /* get_file_info */
89 no_queue_async, /* queue_async */
90 no_cancel_async /* cancel async */
93 /* process startup info */
95 struct startup_info
97 struct object obj; /* object header */
98 obj_handle_t hstdin; /* handle for stdin */
99 obj_handle_t hstdout; /* handle for stdout */
100 obj_handle_t hstderr; /* handle for stderr */
101 struct file *exe_file; /* file handle for main exe */
102 struct process *process; /* created process */
103 data_size_t data_size; /* size of startup data */
104 void *data; /* data for startup info */
107 static void startup_info_dump( struct object *obj, int verbose );
108 static int startup_info_signaled( struct object *obj, struct thread *thread );
109 static void startup_info_destroy( struct object *obj );
111 static const struct object_ops startup_info_ops =
113 sizeof(struct startup_info), /* size */
114 startup_info_dump, /* dump */
115 add_queue, /* add_queue */
116 remove_queue, /* remove_queue */
117 startup_info_signaled, /* signaled */
118 no_satisfied, /* satisfied */
119 no_signal, /* signal */
120 no_get_fd, /* get_fd */
121 no_map_access, /* map_access */
122 no_lookup_name, /* lookup_name */
123 no_open_file, /* open_file */
124 no_close_handle, /* close_handle */
125 startup_info_destroy /* destroy */
129 struct ptid_entry
131 void *ptr; /* entry ptr */
132 unsigned int next; /* next free entry */
135 static struct ptid_entry *ptid_entries; /* array of ptid entries */
136 static unsigned int used_ptid_entries; /* number of entries in use */
137 static unsigned int alloc_ptid_entries; /* number of allocated entries */
138 static unsigned int next_free_ptid; /* next free entry */
139 static unsigned int last_free_ptid; /* last free entry */
141 #define PTID_OFFSET 8 /* offset for first ptid value */
143 /* allocate a new process or thread id */
144 unsigned int alloc_ptid( void *ptr )
146 struct ptid_entry *entry;
147 unsigned int id;
149 if (used_ptid_entries < alloc_ptid_entries)
151 id = used_ptid_entries + PTID_OFFSET;
152 entry = &ptid_entries[used_ptid_entries++];
154 else if (next_free_ptid)
156 id = next_free_ptid;
157 entry = &ptid_entries[id - PTID_OFFSET];
158 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
160 else /* need to grow the array */
162 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
163 if (!count) count = 64;
164 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
166 set_error( STATUS_NO_MEMORY );
167 return 0;
169 ptid_entries = entry;
170 alloc_ptid_entries = count;
171 id = used_ptid_entries + PTID_OFFSET;
172 entry = &ptid_entries[used_ptid_entries++];
175 entry->ptr = ptr;
176 return id;
179 /* free a process or thread id */
180 void free_ptid( unsigned int id )
182 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
184 entry->ptr = NULL;
185 entry->next = 0;
187 /* append to end of free list so that we don't reuse it too early */
188 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
189 else next_free_ptid = id;
191 last_free_ptid = id;
194 /* retrieve the pointer corresponding to a process or thread id */
195 void *get_ptid_entry( unsigned int id )
197 if (id < PTID_OFFSET) return NULL;
198 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
199 return ptid_entries[id - PTID_OFFSET].ptr;
202 /* return the main thread of the process */
203 struct thread *get_process_first_thread( struct process *process )
205 struct list *ptr = list_head( &process->thread_list );
206 if (!ptr) return NULL;
207 return LIST_ENTRY( ptr, struct thread, proc_entry );
210 /* set the state of the process startup info */
211 static void set_process_startup_state( struct process *process, enum startup_state state )
213 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
214 if (process->startup_info)
216 wake_up( &process->startup_info->obj, 0 );
217 release_object( process->startup_info );
218 process->startup_info = NULL;
222 /* final cleanup once we are sure a process is really dead */
223 static void process_died( struct process *process )
225 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
226 release_object( process );
227 if (!--running_processes) close_master_socket();
230 /* callback for process sigkill timeout */
231 static void process_sigkill( void *private )
233 struct process *process = private;
235 process->sigkill_timeout = NULL;
236 kill( process->unix_pid, SIGKILL );
237 process_died( process );
240 /* start the sigkill timer for a process upon exit */
241 static void start_sigkill_timer( struct process *process )
243 grab_object( process );
244 if (process->unix_pid != -1 && process->msg_fd)
246 struct timeval when = current_time;
248 add_timeout( &when, 1000 );
249 process->sigkill_timeout = add_timeout_user( &when, process_sigkill, process );
251 else process_died( process );
254 /* create a new process and its main thread */
255 /* if the function fails the fd is closed */
256 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
258 struct process *process;
259 struct thread *thread = NULL;
260 int request_pipe[2];
262 if (!(process = alloc_object( &process_ops )))
264 close( fd );
265 goto error;
267 process->parent = NULL;
268 process->debugger = NULL;
269 process->handles = NULL;
270 process->msg_fd = NULL;
271 process->sigkill_timeout = NULL;
272 process->unix_pid = -1;
273 process->exit_code = STILL_ACTIVE;
274 process->running_threads = 0;
275 process->priority = PROCESS_PRIOCLASS_NORMAL;
276 process->affinity = 1;
277 process->suspend = 0;
278 process->create_flags = 0;
279 process->console = NULL;
280 process->startup_state = STARTUP_IN_PROGRESS;
281 process->startup_info = NULL;
282 process->idle_event = NULL;
283 process->queue = NULL;
284 process->peb = NULL;
285 process->ldt_copy = NULL;
286 process->winstation = 0;
287 process->desktop = 0;
288 process->token = token_create_admin();
289 process->trace_data = 0;
290 list_init( &process->thread_list );
291 list_init( &process->locks );
292 list_init( &process->classes );
293 list_init( &process->dlls );
295 process->start_time = current_time;
296 process->end_time.tv_sec = process->end_time.tv_usec = 0;
297 list_add_head( &process_list, &process->entry );
299 if (!(process->id = process->group_id = alloc_ptid( process )))
301 close( fd );
302 goto error;
304 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj ))) goto error;
306 /* create the handle table */
307 if (!parent_thread) process->handles = alloc_handle_table( process, 0 );
308 else
310 struct process *parent = parent_thread->process;
311 process->parent = (struct process *)grab_object( parent );
312 process->handles = inherit_all ? copy_handle_table( process, parent )
313 : alloc_handle_table( process, 0 );
315 if (!process->handles) goto error;
317 /* create the main thread */
318 if (pipe( request_pipe ) == -1)
320 file_set_error();
321 goto error;
323 if (send_client_fd( process, request_pipe[1], 0 ) == -1)
325 close( request_pipe[0] );
326 close( request_pipe[1] );
327 goto error;
329 close( request_pipe[1] );
330 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
332 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
333 release_object( process );
334 return thread;
336 error:
337 if (process) release_object( process );
338 /* if we failed to start our first process, close everything down */
339 if (!running_processes) close_master_socket();
340 return NULL;
343 /* initialize the current process and fill in the request */
344 data_size_t init_process( struct thread *thread )
346 struct process *process = thread->process;
347 struct startup_info *info = process->startup_info;
349 init_process_tracing( process );
350 if (!info) return 0;
351 return info->data_size;
354 /* destroy a process when its refcount is 0 */
355 static void process_destroy( struct object *obj )
357 struct process *process = (struct process *)obj;
358 assert( obj->ops == &process_ops );
360 /* we can't have a thread remaining */
361 assert( list_empty( &process->thread_list ));
363 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
365 set_process_startup_state( process, STARTUP_ABORTED );
366 if (process->console) release_object( process->console );
367 if (process->parent) release_object( process->parent );
368 if (process->msg_fd) release_object( process->msg_fd );
369 list_remove( &process->entry );
370 if (process->idle_event) release_object( process->idle_event );
371 if (process->queue) release_object( process->queue );
372 if (process->id) free_ptid( process->id );
373 if (process->token) release_object( process->token );
376 /* dump a process on stdout for debugging purposes */
377 static void process_dump( struct object *obj, int verbose )
379 struct process *process = (struct process *)obj;
380 assert( obj->ops == &process_ops );
382 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
385 static int process_signaled( struct object *obj, struct thread *thread )
387 struct process *process = (struct process *)obj;
388 return !process->running_threads;
391 static unsigned int process_map_access( struct object *obj, unsigned int access )
393 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE;
394 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SYNCHRONIZE;
395 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
396 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
397 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
400 static void process_poll_event( struct fd *fd, int event )
402 struct process *process = get_fd_user( fd );
403 assert( process->obj.ops == &process_ops );
405 if (event & (POLLERR | POLLHUP))
407 release_object( process->msg_fd );
408 process->msg_fd = NULL;
409 if (process->sigkill_timeout) /* already waiting for it to die */
411 remove_timeout_user( process->sigkill_timeout );
412 process->sigkill_timeout = NULL;
413 process_died( process );
415 else kill_process( process, 0 );
417 else if (event & POLLIN) receive_fd( process );
420 static void startup_info_destroy( struct object *obj )
422 struct startup_info *info = (struct startup_info *)obj;
423 assert( obj->ops == &startup_info_ops );
424 free( info->data );
425 if (info->exe_file) release_object( info->exe_file );
426 if (info->process) release_object( info->process );
429 static void startup_info_dump( struct object *obj, int verbose )
431 struct startup_info *info = (struct startup_info *)obj;
432 assert( obj->ops == &startup_info_ops );
434 fprintf( stderr, "Startup info in=%p out=%p err=%p\n",
435 info->hstdin, info->hstdout, info->hstderr );
438 static int startup_info_signaled( struct object *obj, struct thread *thread )
440 struct startup_info *info = (struct startup_info *)obj;
441 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
444 /* get a process from an id (and increment the refcount) */
445 struct process *get_process_from_id( process_id_t id )
447 struct object *obj = get_ptid_entry( id );
449 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
450 set_error( STATUS_INVALID_PARAMETER );
451 return NULL;
454 /* get a process from a handle (and increment the refcount) */
455 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
457 return (struct process *)get_handle_obj( current->process, handle,
458 access, &process_ops );
461 /* find a dll from its base address */
462 static inline struct process_dll *find_process_dll( struct process *process, void *base )
464 struct process_dll *dll;
466 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
468 if (dll->base == base) return dll;
470 return NULL;
473 /* add a dll to a process list */
474 static struct process_dll *process_load_dll( struct process *process, struct file *file,
475 void *base, const WCHAR *filename, data_size_t name_len )
477 struct process_dll *dll;
479 /* make sure we don't already have one with the same base address */
480 if (find_process_dll( process, base ))
482 set_error( STATUS_INVALID_PARAMETER );
483 return NULL;
486 if ((dll = mem_alloc( sizeof(*dll) )))
488 dll->file = NULL;
489 dll->base = base;
490 dll->filename = NULL;
491 dll->namelen = name_len;
492 if (name_len && !(dll->filename = memdup( filename, name_len )))
494 free( dll );
495 return NULL;
497 if (file) dll->file = grab_file_unless_removable( file );
498 list_add_tail( &process->dlls, &dll->entry );
500 return dll;
503 /* remove a dll from a process list */
504 static void process_unload_dll( struct process *process, void *base )
506 struct process_dll *dll = find_process_dll( process, base );
508 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
510 if (dll->file) release_object( dll->file );
511 free( dll->filename );
512 list_remove( &dll->entry );
513 free( dll );
514 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, base );
516 else set_error( STATUS_INVALID_PARAMETER );
519 /* terminate a process with the given exit code */
520 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
522 struct list *ptr;
524 if (skip && skip->process == process) /* move it to the end of the list */
526 assert( skip->state != TERMINATED );
527 list_remove( &skip->proc_entry );
528 list_add_tail( &process->thread_list, &skip->proc_entry );
531 grab_object( process ); /* make sure it doesn't get freed when threads die */
532 while ((ptr = list_head( &process->thread_list )))
534 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
536 if (exit_code) thread->exit_code = exit_code;
537 if (thread == skip) break;
538 kill_thread( thread, 1 );
540 release_object( process );
543 /* kill all processes */
544 void kill_all_processes( struct process *skip, int exit_code )
546 for (;;)
548 struct process *process;
550 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
552 if (process == skip) continue;
553 if (process->running_threads) break;
555 if (&process->entry == &process_list) break; /* no process found */
556 terminate_process( process, NULL, exit_code );
560 /* kill all processes being attached to a console renderer */
561 void kill_console_processes( struct thread *renderer, int exit_code )
563 for (;;) /* restart from the beginning of the list every time */
565 struct process *process;
567 /* find the first process being attached to 'renderer' and still running */
568 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
570 if (process == renderer->process) continue;
571 if (!process->running_threads) continue;
572 if (process->console && process->console->renderer == renderer) break;
574 if (&process->entry == &process_list) break; /* no process found */
575 terminate_process( process, NULL, exit_code );
579 /* a process has been killed (i.e. its last thread died) */
580 static void process_killed( struct process *process )
582 struct handle_table *handles;
583 struct list *ptr;
585 assert( list_empty( &process->thread_list ));
586 process->end_time = current_time;
587 close_process_desktop( process );
588 handles = process->handles;
589 process->handles = NULL;
590 if (handles) release_object( handles );
592 /* close the console attached to this process, if any */
593 free_console( process );
595 while ((ptr = list_head( &process->dlls )))
597 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
598 if (dll->file) release_object( dll->file );
599 free( dll->filename );
600 list_remove( &dll->entry );
601 free( dll );
603 destroy_process_classes( process );
604 destroy_process_cursors( process );
605 remove_process_locks( process );
606 set_process_startup_state( process, STARTUP_ABORTED );
607 finish_process_tracing( process );
608 start_sigkill_timer( process );
609 wake_up( &process->obj, 0 );
612 /* add a thread to a process running threads list */
613 void add_process_thread( struct process *process, struct thread *thread )
615 list_add_tail( &process->thread_list, &thread->proc_entry );
616 if (!process->running_threads++) running_processes++;
617 grab_object( thread );
620 /* remove a thread from a process running threads list */
621 void remove_process_thread( struct process *process, struct thread *thread )
623 assert( process->running_threads > 0 );
624 assert( !list_empty( &process->thread_list ));
626 list_remove( &thread->proc_entry );
628 if (!--process->running_threads)
630 /* we have removed the last running thread, exit the process */
631 process->exit_code = thread->exit_code;
632 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
633 process_killed( process );
635 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
636 release_object( thread );
639 /* suspend all the threads of a process */
640 void suspend_process( struct process *process )
642 if (!process->suspend++)
644 struct list *ptr, *next;
646 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
648 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
649 if (!thread->suspend) stop_thread( thread );
654 /* resume all the threads of a process */
655 void resume_process( struct process *process )
657 assert (process->suspend > 0);
658 if (!--process->suspend)
660 struct list *ptr, *next;
662 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
664 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
665 if (!thread->suspend) wake_thread( thread );
670 /* kill a process on the spot */
671 void kill_process( struct process *process, int violent_death )
673 if (violent_death) terminate_process( process, NULL, 1 );
674 else
676 struct list *ptr;
678 grab_object( process ); /* make sure it doesn't get freed when threads die */
679 while ((ptr = list_head( &process->thread_list )))
681 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
682 kill_thread( thread, 0 );
684 release_object( process );
688 /* kill all processes being debugged by a given thread */
689 void kill_debugged_processes( struct thread *debugger, int exit_code )
691 for (;;) /* restart from the beginning of the list every time */
693 struct process *process;
695 /* find the first process being debugged by 'debugger' and still running */
696 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
698 if (!process->running_threads) continue;
699 if (process->debugger == debugger) break;
701 if (&process->entry == &process_list) break; /* no process found */
702 process->debugger = NULL;
703 terminate_process( process, NULL, exit_code );
708 /* trigger a breakpoint event in a given process */
709 void break_process( struct process *process )
711 struct thread *thread;
713 suspend_process( process );
715 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
717 if (thread->context) /* inside an exception event already */
719 break_thread( thread );
720 goto done;
723 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
724 else set_error( STATUS_ACCESS_DENIED );
725 done:
726 resume_process( process );
730 /* detach a debugger from all its debuggees */
731 void detach_debugged_processes( struct thread *debugger )
733 struct process *process;
735 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
737 if (process->debugger == debugger && process->running_threads)
739 debugger_detach( process, debugger );
745 void enum_processes( int (*cb)(struct process*, void*), void *user )
747 struct list *ptr, *next;
749 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
751 struct process *process = LIST_ENTRY( ptr, struct process, entry );
752 if ((cb)(process, user)) break;
756 /* set the debugged flag in the process PEB */
757 int set_process_debug_flag( struct process *process, int flag )
759 char data = (flag != 0);
761 /* BeingDebugged flag is the byte at offset 2 in the PEB */
762 return write_process_memory( process, (char *)process->peb + 2, 1, &data );
765 /* take a snapshot of currently running processes */
766 struct process_snapshot *process_snap( int *count )
768 struct process_snapshot *snapshot, *ptr;
769 struct process *process;
771 if (!running_processes) return NULL;
772 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
773 return NULL;
774 ptr = snapshot;
775 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
777 if (!process->running_threads) continue;
778 ptr->process = process;
779 ptr->threads = process->running_threads;
780 ptr->count = process->obj.refcount;
781 ptr->priority = process->priority;
782 ptr->handles = get_handle_table_count(process);
783 grab_object( process );
784 ptr++;
787 if (!(*count = ptr - snapshot))
789 free( snapshot );
790 snapshot = NULL;
792 return snapshot;
795 /* take a snapshot of the modules of a process */
796 struct module_snapshot *module_snap( struct process *process, int *count )
798 struct module_snapshot *snapshot, *ptr;
799 struct process_dll *dll;
800 int total = 0;
802 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry ) total++;
803 if (!(snapshot = mem_alloc( sizeof(*snapshot) * total ))) return NULL;
805 ptr = snapshot;
806 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
808 ptr->base = dll->base;
809 ptr->size = dll->size;
810 ptr->namelen = dll->namelen;
811 ptr->filename = memdup( dll->filename, dll->namelen );
812 ptr++;
814 *count = total;
815 return snapshot;
819 /* create a new process */
820 DECL_HANDLER(new_process)
822 struct startup_info *info;
823 struct thread *thread;
824 struct process *process;
825 struct process *parent = current->process;
826 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
828 if (socket_fd == -1)
830 set_error( STATUS_INVALID_PARAMETER );
831 return;
833 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
835 set_error( STATUS_INVALID_HANDLE );
836 close( socket_fd );
837 return;
840 /* build the startup info for a new process */
841 if (!(info = alloc_object( &startup_info_ops ))) return;
842 info->hstdin = req->hstdin;
843 info->hstdout = req->hstdout;
844 info->hstderr = req->hstderr;
845 info->exe_file = NULL;
846 info->process = NULL;
847 info->data_size = get_req_data_size();
848 info->data = NULL;
850 if (req->exe_file &&
851 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
852 goto done;
854 if (!(info->data = memdup( get_req_data(), info->data_size ))) goto done;
856 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
857 process = thread->process;
858 process->create_flags = req->create_flags;
859 process->startup_info = (struct startup_info *)grab_object( info );
861 /* connect to the window station */
862 connect_process_winstation( process, current );
864 /* thread will be actually suspended in init_done */
865 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
867 /* set the process console */
868 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
870 /* FIXME: some better error checking should be done...
871 * like if hConOut and hConIn are console handles, then they should be on the same
872 * physical console
874 inherit_console( current, process, req->inherit_all ? req->hstdin : 0 );
877 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
879 info->hstdin = duplicate_handle( parent, req->hstdin, process,
880 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
881 info->hstdout = duplicate_handle( parent, req->hstdout, process,
882 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
883 info->hstderr = duplicate_handle( parent, req->hstderr, process,
884 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
885 /* some handles above may have been invalid; this is not an error */
886 if (get_error() == STATUS_INVALID_HANDLE ||
887 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
890 /* attach to the debugger if requested */
891 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
892 set_process_debugger( process, current );
893 else if (parent->debugger && !(parent->create_flags & DEBUG_ONLY_THIS_PROCESS))
894 set_process_debugger( process, parent->debugger );
896 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
897 process->group_id = parent->group_id;
899 info->process = (struct process *)grab_object( process );
900 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
901 reply->pid = get_process_id( process );
902 reply->tid = get_thread_id( thread );
903 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
904 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
906 done:
907 release_object( info );
910 /* Retrieve information about a newly started process */
911 DECL_HANDLER(get_new_process_info)
913 struct startup_info *info;
915 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
916 0, &startup_info_ops )))
918 reply->success = is_process_init_done( info->process );
919 reply->exit_code = info->process->exit_code;
920 release_object( info );
924 /* Retrieve the new process startup info */
925 DECL_HANDLER(get_startup_info)
927 struct process *process = current->process;
928 struct startup_info *info = process->startup_info;
929 data_size_t size;
931 if (!info) return;
933 if (info->exe_file &&
934 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
936 reply->hstdin = info->hstdin;
937 reply->hstdout = info->hstdout;
938 reply->hstderr = info->hstderr;
940 /* we return the data directly without making a copy so this can only be called once */
941 size = info->data_size;
942 if (size > get_reply_max_size()) size = get_reply_max_size();
943 set_reply_data_ptr( info->data, size );
944 info->data = NULL;
945 info->data_size = 0;
948 /* signal the end of the process initialization */
949 DECL_HANDLER(init_process_done)
951 struct process_dll *dll;
952 struct process *process = current->process;
954 if (is_process_init_done(process))
956 set_error( STATUS_INVALID_PARAMETER );
957 return;
959 if (!(dll = find_process_dll( process, req->module )))
961 set_error( STATUS_DLL_NOT_FOUND );
962 return;
965 /* main exe is the first in the dll list */
966 list_remove( &dll->entry );
967 list_add_head( &process->dlls, &dll->entry );
969 generate_startup_debug_events( process, req->entry );
970 set_process_startup_state( process, STARTUP_DONE );
972 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0 );
973 if (current->suspend + process->suspend > 0) stop_thread( current );
974 if (process->debugger) set_process_debug_flag( process, 1 );
977 /* open a handle to a process */
978 DECL_HANDLER(open_process)
980 struct process *process = get_process_from_id( req->pid );
981 reply->handle = 0;
982 if (process)
984 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
985 release_object( process );
989 /* terminate a process */
990 DECL_HANDLER(terminate_process)
992 struct process *process;
994 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
996 reply->self = (current->process == process);
997 terminate_process( process, current, req->exit_code );
998 release_object( process );
1002 /* fetch information about a process */
1003 DECL_HANDLER(get_process_info)
1005 struct process *process;
1007 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1009 reply->pid = get_process_id( process );
1010 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1011 reply->exit_code = process->exit_code;
1012 reply->priority = process->priority;
1013 reply->affinity = process->affinity;
1014 reply->peb = process->peb;
1015 reply->start_time.sec = process->start_time.tv_sec;
1016 reply->start_time.usec = process->start_time.tv_usec;
1017 reply->end_time.sec = process->end_time.tv_sec;
1018 reply->end_time.usec = process->end_time.tv_usec;
1019 release_object( process );
1023 /* set information about a process */
1024 DECL_HANDLER(set_process_info)
1026 struct process *process;
1028 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1030 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1031 if (req->mask & SET_PROCESS_INFO_AFFINITY)
1033 if (req->affinity != 1) set_error( STATUS_INVALID_PARAMETER );
1034 else process->affinity = req->affinity;
1036 release_object( process );
1040 /* read data from a process address space */
1041 DECL_HANDLER(read_process_memory)
1043 struct process *process;
1044 data_size_t len = get_reply_max_size();
1046 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1048 if (len)
1050 char *buffer = mem_alloc( len );
1051 if (buffer)
1053 if (read_process_memory( process, req->addr, len, buffer ))
1054 set_reply_data_ptr( buffer, len );
1055 else
1056 free( buffer );
1059 release_object( process );
1062 /* write data to a process address space */
1063 DECL_HANDLER(write_process_memory)
1065 struct process *process;
1067 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1069 data_size_t len = get_req_data_size();
1070 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1071 else set_error( STATUS_INVALID_PARAMETER );
1072 release_object( process );
1076 /* notify the server that a dll has been loaded */
1077 DECL_HANDLER(load_dll)
1079 struct process_dll *dll;
1080 struct file *file = NULL;
1082 if (req->handle && !(file = get_file_obj( current->process, req->handle, FILE_READ_DATA )))
1083 return;
1085 if ((dll = process_load_dll( current->process, file, req->base,
1086 get_req_data(), get_req_data_size() )))
1088 dll->size = req->size;
1089 dll->dbg_offset = req->dbg_offset;
1090 dll->dbg_size = req->dbg_size;
1091 dll->name = req->name;
1092 /* only generate event if initialization is done */
1093 if (is_process_init_done( current->process ))
1094 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1096 if (file) release_object( file );
1099 /* notify the server that a dll is being unloaded */
1100 DECL_HANDLER(unload_dll)
1102 process_unload_dll( current->process, req->base );
1105 /* retrieve information about a module in a process */
1106 DECL_HANDLER(get_dll_info)
1108 struct process *process;
1110 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1112 struct process_dll *dll = find_process_dll( process, req->base_address );
1114 if (dll)
1116 reply->size = dll->size;
1117 reply->entry_point = NULL; /* FIXME */
1118 if (dll->filename)
1120 data_size_t len = min( dll->namelen, get_reply_max_size() );
1121 set_reply_data( dll->filename, len );
1124 else
1125 set_error( STATUS_DLL_NOT_FOUND );
1127 release_object( process );
1131 /* retrieve the process idle event */
1132 DECL_HANDLER(get_process_idle_event)
1134 struct process *process;
1136 reply->event = 0;
1137 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1139 if (process->idle_event && process != current->process)
1140 reply->event = alloc_handle( current->process, process->idle_event,
1141 EVENT_ALL_ACCESS, 0 );
1142 release_object( process );