makefiles: Add a separate variable to keep track of source test files.
[wine.git] / server / process.c
blob6d794ba5ead73975608cce42b8f8ddc3b527e71d
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"
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
36 #endif
37 #include <unistd.h>
38 #include <poll.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
41 #endif
42 #ifdef HAVE_SYS_QUEUE_H
43 # include <sys/queue.h>
44 #endif
45 #ifdef HAVE_SYS_SYSCTL_H
46 # include <sys/sysctl.h>
47 #endif
48 #ifdef HAVE_SYS_USER_H
49 # define thread __unix_thread
50 # include <sys/user.h>
51 # undef thread
52 #endif
53 #ifdef HAVE_LIBPROCSTAT
54 # include <libprocstat.h>
55 #endif
57 #include "ntstatus.h"
58 #define WIN32_NO_STATUS
59 #include "winternl.h"
61 #include "file.h"
62 #include "handle.h"
63 #include "process.h"
64 #include "thread.h"
65 #include "request.h"
66 #include "user.h"
67 #include "security.h"
69 /* process object */
71 static struct list process_list = LIST_INIT(process_list);
72 static int running_processes, user_processes;
73 static struct event *shutdown_event; /* signaled when shutdown starts */
74 static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
75 static int shutdown_stage; /* current stage in the shutdown process */
77 static const WCHAR process_name[] = {'P','r','o','c','e','s','s'};
79 struct type_descr process_type =
81 { process_name, sizeof(process_name) }, /* name */
82 PROCESS_ALL_ACCESS, /* valid_access */
83 { /* mapping */
84 STANDARD_RIGHTS_READ | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
85 STANDARD_RIGHTS_WRITE | PROCESS_SUSPEND_RESUME | PROCESS_SET_INFORMATION | PROCESS_SET_QUOTA
86 | PROCESS_CREATE_PROCESS | PROCESS_DUP_HANDLE | PROCESS_VM_WRITE | PROCESS_VM_OPERATION
87 | PROCESS_CREATE_THREAD,
88 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE,
89 PROCESS_ALL_ACCESS
93 static void process_dump( struct object *obj, int verbose );
94 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
95 static unsigned int process_map_access( struct object *obj, unsigned int access );
96 static struct security_descriptor *process_get_sd( struct object *obj );
97 static void process_poll_event( struct fd *fd, int event );
98 static struct list *process_get_kernel_obj_list( struct object *obj );
99 static void process_destroy( struct object *obj );
100 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
102 static const struct object_ops process_ops =
104 sizeof(struct process), /* size */
105 &process_type, /* type */
106 process_dump, /* dump */
107 add_queue, /* add_queue */
108 remove_queue, /* remove_queue */
109 process_signaled, /* signaled */
110 no_satisfied, /* satisfied */
111 no_signal, /* signal */
112 no_get_fd, /* get_fd */
113 process_map_access, /* map_access */
114 process_get_sd, /* get_sd */
115 default_set_sd, /* set_sd */
116 no_get_full_name, /* get_full_name */
117 no_lookup_name, /* lookup_name */
118 no_link_name, /* link_name */
119 NULL, /* unlink_name */
120 no_open_file, /* open_file */
121 process_get_kernel_obj_list, /* get_kernel_obj_list */
122 no_close_handle, /* close_handle */
123 process_destroy /* destroy */
126 static const struct fd_ops process_fd_ops =
128 NULL, /* get_poll_events */
129 process_poll_event, /* poll_event */
130 NULL, /* flush */
131 NULL, /* get_fd_type */
132 NULL, /* ioctl */
133 NULL, /* queue_async */
134 NULL, /* reselect_async */
135 NULL /* cancel async */
138 /* process startup info */
140 struct startup_info
142 struct object obj; /* object header */
143 struct process *process; /* created process */
144 data_size_t info_size; /* size of startup info */
145 data_size_t data_size; /* size of whole startup data */
146 startup_info_t *data; /* data for startup info */
149 static void startup_info_dump( struct object *obj, int verbose );
150 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
151 static void startup_info_destroy( struct object *obj );
153 static const struct object_ops startup_info_ops =
155 sizeof(struct startup_info), /* size */
156 &no_type, /* type */
157 startup_info_dump, /* dump */
158 add_queue, /* add_queue */
159 remove_queue, /* remove_queue */
160 startup_info_signaled, /* signaled */
161 no_satisfied, /* satisfied */
162 no_signal, /* signal */
163 no_get_fd, /* get_fd */
164 default_map_access, /* map_access */
165 default_get_sd, /* get_sd */
166 default_set_sd, /* set_sd */
167 no_get_full_name, /* get_full_name */
168 no_lookup_name, /* lookup_name */
169 no_link_name, /* link_name */
170 NULL, /* unlink_name */
171 no_open_file, /* open_file */
172 no_kernel_obj_list, /* get_kernel_obj_list */
173 no_close_handle, /* close_handle */
174 startup_info_destroy /* destroy */
177 /* job object */
179 static const WCHAR job_name[] = {'J','o','b'};
181 struct type_descr job_type =
183 { job_name, sizeof(job_name) }, /* name */
184 JOB_OBJECT_ALL_ACCESS, /* valid_access */
185 { /* mapping */
186 STANDARD_RIGHTS_READ | JOB_OBJECT_QUERY,
187 STANDARD_RIGHTS_WRITE | JOB_OBJECT_TERMINATE | JOB_OBJECT_SET_ATTRIBUTES | JOB_OBJECT_ASSIGN_PROCESS,
188 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
189 JOB_OBJECT_ALL_ACCESS
193 static void job_dump( struct object *obj, int verbose );
194 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
195 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
196 static void job_destroy( struct object *obj );
198 struct job
200 struct object obj; /* object header */
201 struct list process_list; /* list of processes */
202 int num_processes; /* count of running processes */
203 int total_processes; /* count of processes which have been assigned */
204 unsigned int limit_flags; /* limit flags */
205 int terminating; /* job is terminating */
206 int signaled; /* job is signaled */
207 struct completion *completion_port; /* associated completion port */
208 apc_param_t completion_key; /* key to send with completion messages */
209 struct job *parent;
210 struct list parent_job_entry; /* list entry for parent job */
211 struct list child_job_list; /* list of child jobs */
214 static const struct object_ops job_ops =
216 sizeof(struct job), /* size */
217 &job_type, /* type */
218 job_dump, /* dump */
219 add_queue, /* add_queue */
220 remove_queue, /* remove_queue */
221 job_signaled, /* signaled */
222 no_satisfied, /* satisfied */
223 no_signal, /* signal */
224 no_get_fd, /* get_fd */
225 default_map_access, /* map_access */
226 default_get_sd, /* get_sd */
227 default_set_sd, /* set_sd */
228 default_get_full_name, /* get_full_name */
229 no_lookup_name, /* lookup_name */
230 directory_link_name, /* link_name */
231 default_unlink_name, /* unlink_name */
232 no_open_file, /* open_file */
233 no_kernel_obj_list, /* get_kernel_obj_list */
234 job_close_handle, /* close_handle */
235 job_destroy /* destroy */
238 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
239 unsigned int attr, const struct security_descriptor *sd )
241 struct job *job;
243 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
245 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
247 /* initialize it if it didn't already exist */
248 list_init( &job->process_list );
249 list_init( &job->child_job_list );
250 job->num_processes = 0;
251 job->total_processes = 0;
252 job->limit_flags = 0;
253 job->terminating = 0;
254 job->signaled = 0;
255 job->completion_port = NULL;
256 job->completion_key = 0;
257 job->parent = NULL;
260 return job;
263 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
265 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
268 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
270 if (job->completion_port)
271 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
274 static void add_job_completion_existing_processes( struct job *job, struct job *completion_job )
276 struct process *process;
277 struct job *j;
279 assert( completion_job->obj.ops == &job_ops );
281 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
283 add_job_completion_existing_processes( j, completion_job );
286 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
288 if (process->running_threads)
289 add_job_completion( completion_job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id( process ));
293 static int process_in_job( struct job *job, struct process *process )
295 struct job *j;
297 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
299 assert( j->parent == job );
300 if (process_in_job( j, process )) return 1;
302 return process->job == job;
305 static process_id_t *get_job_pids( struct job *job, process_id_t *pids, process_id_t *end )
307 struct process *process;
308 struct job *j;
310 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
311 pids = get_job_pids( j, pids, end );
313 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
315 if (pids == end) break;
316 if (process->end_time) continue; /* skip processes that ended */
317 *pids++ = process->id;
320 return pids;
323 static void add_job_process( struct job *job, struct process *process )
325 struct job *j, *common_parent;
326 process_id_t pid;
328 if (job == process->job) return;
330 if ((common_parent = process->job))
332 if (job->parent)
334 for (j = job->parent; j; j = j->parent)
335 if (j == common_parent) break;
337 if (j != common_parent)
339 /* Job already has parent and the process is not in the job's chain. */
340 set_error( STATUS_ACCESS_DENIED );
341 return;
343 /* process->job is referenced in the job->parent chain. */
344 release_object( process->job );
346 else
348 if (job->total_processes)
350 set_error( STATUS_ACCESS_DENIED );
351 return;
353 /* transfer reference. */
354 job->parent = process->job;
355 list_add_tail( &job->parent->child_job_list, &job->parent_job_entry );
357 list_remove( &process->job_entry );
359 process->job = (struct job *)grab_object( job );
360 list_add_tail( &job->process_list, &process->job_entry );
362 pid = get_process_id( process );
363 for (j = job; j != common_parent; j = j->parent)
365 j->num_processes++;
366 j->total_processes++;
367 add_job_completion( j, JOB_OBJECT_MSG_NEW_PROCESS, pid );
371 /* called when a process has terminated, allow one additional process */
372 static void release_job_process( struct process *process )
374 struct job *job = process->job;
376 while (job)
378 assert( job->num_processes );
379 job->num_processes--;
381 if (!job->terminating)
382 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
384 if (!job->num_processes)
385 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
387 job = job->parent;
391 static void terminate_job( struct job *job, int exit_code )
393 struct process *process, *next_process;
394 struct job *j, *next_job;
396 LIST_FOR_EACH_ENTRY_SAFE( j, next_job, &job->child_job_list, struct job, parent_job_entry )
398 assert( j->parent == job );
400 grab_object( j );
401 terminate_job( j, exit_code );
402 release_object( j );
405 job->terminating = 1;
406 LIST_FOR_EACH_ENTRY_SAFE( process, next_process, &job->process_list, struct process, job_entry )
408 assert( process->job == job );
409 if (process->running_threads) terminate_process( process, NULL, exit_code );
411 job->terminating = 0;
412 job->signaled = 1;
413 wake_up( &job->obj, 0 );
416 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
418 struct job *job = (struct job *)obj;
419 assert( obj->ops == &job_ops );
421 if (obj->handle_count == 1) /* last handle */
423 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
424 terminate_job( job, 0 );
426 return 1;
429 static void job_destroy( struct object *obj )
431 struct job *job = (struct job *)obj;
432 assert( obj->ops == &job_ops );
434 assert( !job->num_processes );
435 assert( list_empty( &job->process_list ));
436 assert( list_empty( &job->child_job_list ));
438 if (job->completion_port) release_object( job->completion_port );
439 if (job->parent)
441 list_remove( &job->parent_job_entry );
442 release_object( job->parent );
446 static void job_dump( struct object *obj, int verbose )
448 struct job *job = (struct job *)obj;
449 assert( obj->ops == &job_ops );
450 fprintf( stderr, "Job processes=%d child_jobs=%d parent=%p\n",
451 list_count(&job->process_list), list_count(&job->child_job_list), job->parent );
454 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
456 struct job *job = (struct job *)obj;
457 return job->signaled;
460 struct ptid_entry
462 void *ptr; /* entry ptr */
463 unsigned int next; /* next free entry */
466 static struct ptid_entry *ptid_entries; /* array of ptid entries */
467 static unsigned int used_ptid_entries; /* number of entries in use */
468 static unsigned int alloc_ptid_entries; /* number of allocated entries */
469 static unsigned int next_free_ptid; /* next free entry */
470 static unsigned int last_free_ptid; /* last free entry */
471 static unsigned int num_free_ptids; /* number of free ptids */
473 static void kill_all_processes(void);
475 #define PTID_OFFSET 8 /* offset for first ptid value */
477 static unsigned int index_from_ptid(unsigned int id) { return id / 4; }
478 static unsigned int ptid_from_index(unsigned int index) { return index * 4; }
480 /* allocate a new process or thread id */
481 unsigned int alloc_ptid( void *ptr )
483 struct ptid_entry *entry;
484 unsigned int index;
486 if (used_ptid_entries < alloc_ptid_entries)
488 index = used_ptid_entries + PTID_OFFSET;
489 entry = &ptid_entries[used_ptid_entries++];
491 else if (next_free_ptid && num_free_ptids >= 256)
493 index = next_free_ptid;
494 entry = &ptid_entries[index - PTID_OFFSET];
495 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
496 num_free_ptids--;
498 else /* need to grow the array */
500 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
501 if (!count) count = 512;
502 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
504 set_error( STATUS_NO_MEMORY );
505 return 0;
507 ptid_entries = entry;
508 alloc_ptid_entries = count;
509 index = used_ptid_entries + PTID_OFFSET;
510 entry = &ptid_entries[used_ptid_entries++];
513 entry->ptr = ptr;
514 return ptid_from_index( index );
517 /* free a process or thread id */
518 void free_ptid( unsigned int id )
520 unsigned int index = index_from_ptid( id );
521 struct ptid_entry *entry = &ptid_entries[index - PTID_OFFSET];
523 entry->ptr = NULL;
524 entry->next = 0;
526 /* append to end of free list so that we don't reuse it too early */
527 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = index;
528 else next_free_ptid = index;
529 last_free_ptid = index;
530 num_free_ptids++;
533 /* retrieve the pointer corresponding to a process or thread id */
534 void *get_ptid_entry( unsigned int id )
536 unsigned int index = index_from_ptid( id );
537 if (index < PTID_OFFSET) return NULL;
538 if (index - PTID_OFFSET >= used_ptid_entries) return NULL;
539 return ptid_entries[index - PTID_OFFSET].ptr;
542 /* return the main thread of the process */
543 struct thread *get_process_first_thread( struct process *process )
545 struct list *ptr = list_head( &process->thread_list );
546 if (!ptr) return NULL;
547 return LIST_ENTRY( ptr, struct thread, proc_entry );
550 /* set the state of the process startup info */
551 static void set_process_startup_state( struct process *process, enum startup_state state )
553 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
554 if (process->startup_info)
556 wake_up( &process->startup_info->obj, 0 );
557 release_object( process->startup_info );
558 process->startup_info = NULL;
562 /* callback for server shutdown */
563 static void server_shutdown_timeout( void *arg )
565 shutdown_timeout = NULL;
566 if (!running_processes)
568 close_master_socket( 0 );
569 return;
571 switch(++shutdown_stage)
573 case 1: /* signal system processes to exit */
574 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
575 if (shutdown_event) set_event( shutdown_event );
576 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
577 close_master_socket( 4 * -TICKS_PER_SEC );
578 break;
579 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
580 kill_all_processes();
581 break;
585 /* forced shutdown, used for wineserver -k */
586 void shutdown_master_socket(void)
588 kill_all_processes();
589 shutdown_stage = 2;
590 if (shutdown_timeout)
592 remove_timeout_user( shutdown_timeout );
593 shutdown_timeout = NULL;
595 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
598 /* final cleanup once we are sure a process is really dead */
599 static void process_died( struct process *process )
601 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
602 if (!process->is_system)
604 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
605 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
607 release_object( process );
608 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
611 /* callback for process sigkill timeout */
612 static void process_sigkill( void *private )
614 struct process *process = private;
615 int signal = 0;
617 process->sigkill_delay *= 2;
618 if (process->sigkill_delay >= TICKS_PER_SEC / 2)
619 signal = SIGKILL;
621 if (!kill( process->unix_pid, signal ) && !signal)
622 process->sigkill_timeout = add_timeout_user( -process->sigkill_delay, process_sigkill, process );
623 else
625 process->sigkill_delay = TICKS_PER_SEC / 64;
626 process->sigkill_timeout = NULL;
627 process_died( process );
631 /* start the sigkill timer for a process upon exit */
632 static void start_sigkill_timer( struct process *process )
634 grab_object( process );
635 if (process->unix_pid != -1)
636 process->sigkill_timeout = add_timeout_user( -process->sigkill_delay, process_sigkill, process );
637 else
638 process_died( process );
641 /* create a new process */
642 /* if the function fails the fd is closed */
643 struct process *create_process( int fd, struct process *parent, unsigned int flags, const startup_info_t *info,
644 const struct security_descriptor *sd, const obj_handle_t *handles,
645 unsigned int handle_count, struct token *token )
647 struct process *process;
649 if (!(process = alloc_object( &process_ops )))
651 close( fd );
652 goto error;
654 process->parent_id = 0;
655 process->debug_obj = NULL;
656 process->debug_event = NULL;
657 process->handles = NULL;
658 process->msg_fd = NULL;
659 process->sigkill_timeout = NULL;
660 process->sigkill_delay = TICKS_PER_SEC / 64;
661 process->unix_pid = -1;
662 process->exit_code = STILL_ACTIVE;
663 process->running_threads = 0;
664 process->priority = PROCESS_PRIOCLASS_NORMAL;
665 process->suspend = 0;
666 process->is_system = 0;
667 process->debug_children = 1;
668 process->is_terminating = 0;
669 process->imagelen = 0;
670 process->image = NULL;
671 process->job = NULL;
672 process->console = NULL;
673 process->startup_state = STARTUP_IN_PROGRESS;
674 process->startup_info = NULL;
675 process->idle_event = NULL;
676 process->peb = 0;
677 process->ldt_copy = 0;
678 process->dir_cache = NULL;
679 process->winstation = 0;
680 process->desktop = 0;
681 process->token = NULL;
682 process->trace_data = 0;
683 process->rawinput_mouse = NULL;
684 process->rawinput_kbd = NULL;
685 list_init( &process->kernel_object );
686 list_init( &process->thread_list );
687 list_init( &process->locks );
688 list_init( &process->asyncs );
689 list_init( &process->classes );
690 list_init( &process->views );
691 list_init( &process->rawinput_devices );
693 process->end_time = 0;
695 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
696 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
698 close( fd );
699 goto error;
701 if (!(process->id = process->group_id = alloc_ptid( process )))
703 close( fd );
704 goto error;
706 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
708 /* create the handle table */
709 if (!parent)
711 process->handles = alloc_handle_table( process, 0 );
712 process->token = token_create_admin( TRUE, -1, TokenElevationTypeFull, default_session_id );
713 process->affinity = ~0;
715 else
717 obj_handle_t std_handles[3];
719 std_handles[0] = info->hstdin;
720 std_handles[1] = info->hstdout;
721 std_handles[2] = info->hstderr;
723 process->parent_id = parent->id;
724 if (flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES)
725 process->handles = copy_handle_table( process, parent, handles, handle_count, std_handles );
726 else
727 process->handles = alloc_handle_table( process, 0 );
728 /* Note: for security reasons, starting a new process does not attempt
729 * to use the current impersonation token for the new process */
730 process->token = token_duplicate( token ? token : parent->token, TRUE, 0, NULL, NULL, 0, NULL, 0 );
731 process->affinity = parent->affinity;
733 if (!process->handles || !process->token) goto error;
734 process->session_id = token_get_session_id( process->token );
736 /* Assign a high security label to the token. The default would be medium
737 * but Wine provides admin access to all applications right now so high
738 * makes more sense for the time being. */
739 if (!token_assign_label( process->token, security_high_label_sid ))
740 goto error;
742 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
743 return process;
745 error:
746 if (process) release_object( process );
747 /* if we failed to start our first process, close everything down */
748 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
749 return NULL;
752 /* get the process data size */
753 data_size_t get_process_startup_info_size( struct process *process )
755 struct startup_info *info = process->startup_info;
757 if (!info) return 0;
758 return info->data_size;
761 /* destroy a process when its refcount is 0 */
762 static void process_destroy( struct object *obj )
764 struct process *process = (struct process *)obj;
765 assert( obj->ops == &process_ops );
767 /* we can't have a thread remaining */
768 assert( list_empty( &process->thread_list ));
769 assert( list_empty( &process->asyncs ));
771 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
773 close_process_handles( process );
774 set_process_startup_state( process, STARTUP_ABORTED );
776 if (process->job)
778 list_remove( &process->job_entry );
779 release_object( process->job );
781 if (process->console) release_object( process->console );
782 if (process->msg_fd) release_object( process->msg_fd );
783 if (process->idle_event) release_object( process->idle_event );
784 if (process->id) free_ptid( process->id );
785 if (process->token) release_object( process->token );
786 free( process->dir_cache );
787 free( process->image );
790 /* dump a process on stdout for debugging purposes */
791 static void process_dump( struct object *obj, int verbose )
793 struct process *process = (struct process *)obj;
794 assert( obj->ops == &process_ops );
796 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
799 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
801 struct process *process = (struct process *)obj;
802 return !process->running_threads;
805 static unsigned int process_map_access( struct object *obj, unsigned int access )
807 access = default_map_access( obj, access );
808 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
809 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
810 return access;
813 static struct list *process_get_kernel_obj_list( struct object *obj )
815 struct process *process = (struct process *)obj;
816 return &process->kernel_object;
819 static struct security_descriptor *process_get_sd( struct object *obj )
821 static struct security_descriptor *process_default_sd;
823 if (obj->sd) return obj->sd;
825 if (!process_default_sd)
827 size_t users_sid_len = security_sid_len( security_domain_users_sid );
828 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
829 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
830 + users_sid_len + admins_sid_len;
831 ACCESS_ALLOWED_ACE *aaa;
832 ACL *dacl;
834 process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
835 + dacl_len );
836 process_default_sd->control = SE_DACL_PRESENT;
837 process_default_sd->owner_len = admins_sid_len;
838 process_default_sd->group_len = users_sid_len;
839 process_default_sd->sacl_len = 0;
840 process_default_sd->dacl_len = dacl_len;
841 memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
842 memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
844 dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
845 dacl->AclRevision = ACL_REVISION;
846 dacl->Sbz1 = 0;
847 dacl->AclSize = dacl_len;
848 dacl->AceCount = 2;
849 dacl->Sbz2 = 0;
850 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
851 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
852 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
853 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
854 aaa->Mask = GENERIC_READ;
855 memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
856 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
857 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
858 aaa->Header.AceFlags = 0;
859 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
860 aaa->Mask = PROCESS_ALL_ACCESS;
861 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
863 return process_default_sd;
866 static void process_poll_event( struct fd *fd, int event )
868 struct process *process = get_fd_user( fd );
869 assert( process->obj.ops == &process_ops );
871 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
872 else if (event & POLLIN) receive_fd( process );
875 static void startup_info_destroy( struct object *obj )
877 struct startup_info *info = (struct startup_info *)obj;
878 assert( obj->ops == &startup_info_ops );
879 free( info->data );
880 if (info->process) release_object( info->process );
883 static void startup_info_dump( struct object *obj, int verbose )
885 struct startup_info *info = (struct startup_info *)obj;
886 assert( obj->ops == &startup_info_ops );
888 fputs( "Startup info", stderr );
889 if (info->data)
890 fprintf( stderr, " in=%04x out=%04x err=%04x",
891 info->data->hstdin, info->data->hstdout, info->data->hstderr );
892 fputc( '\n', stderr );
895 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
897 struct startup_info *info = (struct startup_info *)obj;
898 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
901 /* get a process from an id (and increment the refcount) */
902 struct process *get_process_from_id( process_id_t id )
904 struct object *obj = get_ptid_entry( id );
906 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
907 set_error( STATUS_INVALID_CID );
908 return NULL;
911 /* get a process from a handle (and increment the refcount) */
912 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
914 return (struct process *)get_handle_obj( current->process, handle,
915 access, &process_ops );
918 /* terminate a process with the given exit code */
919 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
921 struct thread *thread;
923 grab_object( process ); /* make sure it doesn't get freed when threads die */
924 process->is_terminating = 1;
926 restart:
927 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
929 if (exit_code) thread->exit_code = exit_code;
930 if (thread == skip) continue;
931 if (thread->state == TERMINATED) continue;
932 kill_thread( thread, 1 );
933 goto restart;
935 release_object( process );
938 /* kill all processes */
939 static void kill_all_processes(void)
941 struct list *ptr;
943 while ((ptr = list_head( &process_list )))
945 struct process *process = LIST_ENTRY( ptr, struct process, entry );
946 terminate_process( process, NULL, 1 );
950 /* kill all processes being attached to a console renderer */
951 void kill_console_processes( struct thread *renderer, int exit_code )
953 for (;;) /* restart from the beginning of the list every time */
955 struct process *process;
957 /* find the first process being attached to 'renderer' and still running */
958 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
960 if (process == renderer->process) continue;
961 if (process->console && console_get_renderer( process->console ) == renderer) break;
963 if (&process->entry == &process_list) break; /* no process found */
964 terminate_process( process, NULL, exit_code );
968 /* a process has been killed (i.e. its last thread died) */
969 static void process_killed( struct process *process )
971 struct list *ptr;
973 assert( list_empty( &process->thread_list ));
974 process->end_time = current_time;
975 close_process_desktop( process );
976 process->winstation = 0;
977 process->desktop = 0;
978 cancel_process_asyncs( process );
979 close_process_handles( process );
980 if (process->idle_event) release_object( process->idle_event );
981 process->idle_event = NULL;
982 assert( !process->console );
984 while ((ptr = list_head( &process->rawinput_devices )))
986 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
987 list_remove( &entry->entry );
988 free( entry );
990 destroy_process_classes( process );
991 free_mapped_views( process );
992 free_process_user_handles( process );
993 remove_process_locks( process );
994 set_process_startup_state( process, STARTUP_ABORTED );
995 finish_process_tracing( process );
996 release_job_process( process );
997 start_sigkill_timer( process );
998 wake_up( &process->obj, 0 );
1001 /* add a thread to a process running threads list */
1002 void add_process_thread( struct process *process, struct thread *thread )
1004 list_add_tail( &process->thread_list, &thread->proc_entry );
1005 if (!process->running_threads++)
1007 list_add_tail( &process_list, &process->entry );
1008 running_processes++;
1009 if (!process->is_system)
1011 if (!user_processes++ && shutdown_timeout)
1013 remove_timeout_user( shutdown_timeout );
1014 shutdown_timeout = NULL;
1018 grab_object( thread );
1021 /* remove a thread from a process running threads list */
1022 void remove_process_thread( struct process *process, struct thread *thread )
1024 assert( process->running_threads > 0 );
1025 assert( !list_empty( &process->thread_list ));
1027 list_remove( &thread->proc_entry );
1029 if (!--process->running_threads)
1031 /* we have removed the last running thread, exit the process */
1032 process->exit_code = thread->exit_code;
1033 generate_debug_event( thread, DbgExitProcessStateChange, process );
1034 list_remove( &process->entry );
1035 process_killed( process );
1037 else generate_debug_event( thread, DbgExitThreadStateChange, thread );
1038 release_object( thread );
1041 /* suspend all the threads of a process */
1042 void suspend_process( struct process *process )
1044 if (!process->suspend++)
1046 struct list *ptr, *next;
1048 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1050 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1051 if (!thread->suspend) stop_thread( thread );
1056 /* resume all the threads of a process */
1057 void resume_process( struct process *process )
1059 assert (process->suspend > 0);
1060 if (!--process->suspend)
1062 struct list *ptr, *next;
1064 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1066 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1067 if (!thread->suspend) wake_thread( thread );
1072 /* kill a process on the spot */
1073 void kill_process( struct process *process, int violent_death )
1075 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
1077 release_object( process->msg_fd );
1078 process->msg_fd = NULL;
1081 if (process->sigkill_timeout) return; /* already waiting for it to die */
1083 if (violent_death) terminate_process( process, NULL, 1 );
1084 else
1086 struct list *ptr;
1088 grab_object( process ); /* make sure it doesn't get freed when threads die */
1089 while ((ptr = list_head( &process->thread_list )))
1091 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1092 kill_thread( thread, 0 );
1094 release_object( process );
1098 /* detach all processes being debugged by a given thread */
1099 void detach_debugged_processes( struct debug_obj *debug_obj, int exit_code )
1101 for (;;) /* restart from the beginning of the list every time */
1103 struct process *process;
1105 /* find the first process being debugged by 'debugger' and still running */
1106 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1107 if (process->debug_obj == debug_obj) break;
1109 if (&process->entry == &process_list) break; /* no process found */
1110 if (exit_code)
1112 process->debug_obj = NULL;
1113 terminate_process( process, NULL, exit_code );
1115 else debugger_detach( process, debug_obj );
1120 void enum_processes( int (*cb)(struct process*, void*), void *user )
1122 struct list *ptr, *next;
1124 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1126 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1127 if ((cb)(process, user)) break;
1131 /* set the debugged flag in the process PEB */
1132 int set_process_debug_flag( struct process *process, int flag )
1134 char data = (flag != 0);
1135 client_ptr_t peb32 = 0;
1137 if (!is_machine_64bit( process->machine ) && is_machine_64bit( native_machine ))
1138 peb32 = process->peb + 0x1000;
1140 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1141 if (peb32 && !write_process_memory( process, peb32 + 2, 1, &data )) return 0;
1142 return write_process_memory( process, process->peb + 2, 1, &data );
1145 /* create a new process */
1146 DECL_HANDLER(new_process)
1148 struct startup_info *info;
1149 const void *info_ptr;
1150 struct unicode_str name;
1151 const struct security_descriptor *sd;
1152 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1153 struct process *process = NULL;
1154 struct token *token = NULL;
1155 struct debug_obj *debug_obj = NULL;
1156 struct process *parent;
1157 struct thread *parent_thread = current;
1158 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1159 const obj_handle_t *handles = NULL;
1160 const obj_handle_t *job_handles = NULL;
1161 unsigned int i, job_handle_count;
1162 struct job *job;
1164 if (socket_fd == -1)
1166 set_error( STATUS_INVALID_PARAMETER );
1167 return;
1169 if (!objattr)
1171 set_error( STATUS_INVALID_PARAMETER );
1172 close( socket_fd );
1173 return;
1175 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1177 set_error( STATUS_INVALID_HANDLE );
1178 close( socket_fd );
1179 return;
1181 if (shutdown_stage)
1183 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1184 close( socket_fd );
1185 return;
1188 if (req->parent_process)
1190 if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
1192 close( socket_fd );
1193 return;
1195 parent_thread = NULL;
1197 else parent = (struct process *)grab_object( current->process );
1199 /* If a job further in the job chain does not permit breakaway process creation
1200 * succeeds and the process which is trying to breakaway is assigned to that job. */
1201 if (parent->job && (req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY) &&
1202 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1204 set_error( STATUS_ACCESS_DENIED );
1205 close( socket_fd );
1206 release_object( parent );
1207 return;
1210 /* build the startup info for a new process */
1211 if (!(info = alloc_object( &startup_info_ops )))
1213 close( socket_fd );
1214 release_object( parent );
1215 return;
1217 info->process = NULL;
1218 info->data = NULL;
1220 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1222 if ((req->handles_size & 3) || req->handles_size > info->data_size)
1224 set_error( STATUS_INVALID_PARAMETER );
1225 close( socket_fd );
1226 goto done;
1228 if (req->handles_size)
1230 handles = info_ptr;
1231 info_ptr = (const char *)info_ptr + req->handles_size;
1232 info->data_size -= req->handles_size;
1235 if ((req->jobs_size & 3) || req->jobs_size > info->data_size)
1237 set_error( STATUS_INVALID_PARAMETER );
1238 close( socket_fd );
1239 goto done;
1241 if (req->jobs_size)
1243 job_handles = info_ptr;
1244 info_ptr = (const char *)info_ptr + req->jobs_size;
1245 info->data_size -= req->jobs_size;
1248 job_handle_count = req->jobs_size / sizeof(*handles);
1249 for (i = 0; i < job_handle_count; ++i)
1251 if (!(job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS )))
1253 close( socket_fd );
1254 goto done;
1256 release_object( job );
1259 info->info_size = min( req->info_size, info->data_size );
1261 if (req->info_size < sizeof(*info->data))
1263 /* make sure we have a full startup_info_t structure */
1264 data_size_t env_size = info->data_size - info->info_size;
1265 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1267 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1269 close( socket_fd );
1270 goto done;
1272 memcpy( info->data, info_ptr, info_size );
1273 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1274 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1275 info->info_size = sizeof(startup_info_t);
1276 info->data_size = info->info_size + env_size;
1278 else
1280 data_size_t pos = sizeof(*info->data);
1282 if (!(info->data = memdup( info_ptr, info->data_size )))
1284 close( socket_fd );
1285 goto done;
1287 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1288 FIXUP_LEN( info->data->curdir_len );
1289 FIXUP_LEN( info->data->dllpath_len );
1290 FIXUP_LEN( info->data->imagepath_len );
1291 FIXUP_LEN( info->data->cmdline_len );
1292 FIXUP_LEN( info->data->title_len );
1293 FIXUP_LEN( info->data->desktop_len );
1294 FIXUP_LEN( info->data->shellinfo_len );
1295 FIXUP_LEN( info->data->runtime_len );
1296 #undef FIXUP_LEN
1299 if (req->token && !(token = get_token_obj( current->process, req->token, TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY )))
1301 close( socket_fd );
1302 goto done;
1304 if (req->debug && !(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
1306 close( socket_fd );
1307 goto done;
1310 if (!(process = create_process( socket_fd, parent, req->flags, info->data, sd,
1311 handles, req->handles_size / sizeof(*handles), token )))
1312 goto done;
1314 process->startup_info = (struct startup_info *)grab_object( info );
1316 job = parent->job;
1317 while (job)
1319 if (!(job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
1320 && !(req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY
1321 && job->limit_flags & JOB_OBJECT_LIMIT_BREAKAWAY_OK))
1323 add_job_process( job, process );
1324 assert( !get_error() );
1325 break;
1327 job = job->parent;
1330 for (i = 0; i < job_handle_count; ++i)
1332 job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS );
1333 add_job_process( job, process );
1334 release_object( job );
1335 if (get_error())
1337 release_job_process( process );
1338 goto done;
1342 /* connect to the window station */
1343 connect_process_winstation( process, parent_thread, parent );
1345 /* set the process console */
1346 if (info->data->console > 3)
1347 info->data->console = duplicate_handle( parent, info->data->console, process,
1348 0, 0, DUPLICATE_SAME_ACCESS );
1350 if (!(req->flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES) && info->data->console != 1)
1352 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1353 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1354 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1355 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1356 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1357 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1358 /* some handles above may have been invalid; this is not an error */
1359 if (get_error() == STATUS_INVALID_HANDLE ||
1360 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1363 /* attach to the debugger */
1364 if (debug_obj)
1366 process->debug_obj = debug_obj;
1367 process->debug_children = !(req->flags & PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT);
1369 else if (parent->debug_children)
1371 process->debug_obj = parent->debug_obj;
1372 /* debug_children is set to 1 by default */
1375 if (!info->data->console_flags) process->group_id = parent->group_id;
1377 info->process = (struct process *)grab_object( process );
1378 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1379 reply->pid = get_process_id( process );
1380 reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
1382 done:
1383 if (process) release_object( process );
1384 if (debug_obj) release_object( debug_obj );
1385 if (token) release_object( token );
1386 release_object( parent );
1387 release_object( info );
1390 /* Retrieve information about a newly started process */
1391 DECL_HANDLER(get_new_process_info)
1393 struct startup_info *info;
1395 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1396 0, &startup_info_ops )))
1398 reply->success = is_process_init_done( info->process );
1399 reply->exit_code = info->process->exit_code;
1400 release_object( info );
1404 /* Retrieve the new process startup info */
1405 DECL_HANDLER(get_startup_info)
1407 struct process *process = current->process;
1408 struct startup_info *info = process->startup_info;
1409 data_size_t size;
1411 if (!info) return;
1413 /* we return the data directly without making a copy so this can only be called once */
1414 reply->info_size = info->info_size;
1415 size = info->data_size;
1416 if (size > get_reply_max_size()) size = get_reply_max_size();
1417 set_reply_data_ptr( info->data, size );
1418 info->data = NULL;
1419 info->data_size = 0;
1422 /* signal the end of the process initialization */
1423 DECL_HANDLER(init_process_done)
1425 struct process *process = current->process;
1426 struct memory_view *view;
1427 client_ptr_t base;
1428 const pe_image_info_t *image_info;
1430 if (is_process_init_done(process))
1432 set_error( STATUS_INVALID_PARAMETER );
1433 return;
1435 if (!(view = get_exe_view( process )))
1437 set_error( STATUS_DLL_NOT_FOUND );
1438 return;
1440 if (!(image_info = get_view_image_info( view, &base ))) return;
1442 current->teb = req->teb;
1443 process->peb = req->peb;
1444 process->ldt_copy = req->ldt_copy;
1446 process->start_time = current_time;
1447 current->entry_point = base + image_info->entry_point;
1449 init_process_tracing( process );
1450 generate_startup_debug_events( process );
1451 set_process_startup_state( process, STARTUP_DONE );
1453 if (image_info->subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI)
1454 process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1455 if (process->debug_obj) set_process_debug_flag( process, 1 );
1456 reply->entry = current->entry_point;
1457 reply->suspend = (current->suspend || process->suspend);
1460 /* open a handle to a process */
1461 DECL_HANDLER(open_process)
1463 struct process *process = get_process_from_id( req->pid );
1464 reply->handle = 0;
1465 if (process)
1467 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1468 release_object( process );
1472 /* terminate a process */
1473 DECL_HANDLER(terminate_process)
1475 struct process *process;
1477 if (req->handle)
1479 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1480 if (!process) return;
1482 else process = (struct process *)grab_object( current->process );
1484 reply->self = (current->process == process);
1485 terminate_process( process, current, req->exit_code );
1486 release_object( process );
1489 /* fetch information about a process */
1490 DECL_HANDLER(get_process_info)
1492 struct process *process;
1494 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1496 reply->pid = get_process_id( process );
1497 reply->ppid = process->parent_id;
1498 reply->exit_code = process->exit_code;
1499 reply->priority = process->priority;
1500 reply->affinity = process->affinity;
1501 reply->peb = process->peb;
1502 reply->start_time = process->start_time;
1503 reply->end_time = process->end_time;
1504 reply->session_id = process->session_id;
1505 reply->machine = process->machine;
1506 if (get_reply_max_size())
1508 client_ptr_t base;
1509 const pe_image_info_t *info;
1510 struct memory_view *view = get_exe_view( process );
1511 if (view)
1513 if ((info = get_view_image_info( view, &base )))
1514 set_reply_data( info, min( sizeof(*info), get_reply_max_size() ));
1516 else set_error( STATUS_PROCESS_IS_TERMINATING );
1518 release_object( process );
1522 /* retrieve debug information about a process */
1523 DECL_HANDLER(get_process_debug_info)
1525 struct process *process;
1527 if (!(process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION ))) return;
1529 reply->debug_children = process->debug_children;
1530 if (!process->debug_obj) set_error( STATUS_PORT_NOT_SET );
1531 else reply->debug = alloc_handle( current->process, process->debug_obj, DEBUG_ALL_ACCESS, 0 );
1532 release_object( process );
1535 /* fetch the name of the process image */
1536 DECL_HANDLER(get_process_image_name)
1538 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1540 if (!process) return;
1541 if (process->image)
1543 struct unicode_str name = { process->image, process->imagelen };
1544 /* skip the \??\ prefix */
1545 if (req->win32 && name.len > 6 * sizeof(WCHAR) && name.str[5] == ':')
1547 name.str += 4;
1548 name.len -= 4 * sizeof(WCHAR);
1550 /* FIXME: else resolve symlinks in NT path */
1552 reply->len = name.len;
1553 if (name.len <= get_reply_max_size())
1555 WCHAR *ptr = set_reply_data( name.str, name.len );
1556 /* change \??\ to \\?\ */
1557 if (req->win32 && name.len > sizeof(WCHAR) && ptr[1] == '?') ptr[1] = '\\';
1559 else set_error( STATUS_BUFFER_TOO_SMALL );
1561 release_object( process );
1564 /* retrieve information about a process memory usage */
1565 DECL_HANDLER(get_process_vm_counters)
1567 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1569 if (!process) return;
1570 if (process->unix_pid != -1)
1572 #ifdef linux
1573 FILE *f;
1574 char proc_path[32], line[256];
1575 unsigned long value;
1577 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1578 if ((f = fopen( proc_path, "r" )))
1580 while (fgets( line, sizeof(line), f ))
1582 if (sscanf( line, "VmPeak: %lu", &value ))
1583 reply->peak_virtual_size = (mem_size_t)value * 1024;
1584 else if (sscanf( line, "VmSize: %lu", &value ))
1585 reply->virtual_size = (mem_size_t)value * 1024;
1586 else if (sscanf( line, "VmHWM: %lu", &value ))
1587 reply->peak_working_set_size = (mem_size_t)value * 1024;
1588 else if (sscanf( line, "VmRSS: %lu", &value ))
1589 reply->working_set_size = (mem_size_t)value * 1024;
1590 else if (sscanf( line, "RssAnon: %lu", &value ))
1591 reply->pagefile_usage += (mem_size_t)value * 1024;
1592 else if (sscanf( line, "VmSwap: %lu", &value ))
1593 reply->pagefile_usage += (mem_size_t)value * 1024;
1595 reply->peak_pagefile_usage = reply->pagefile_usage;
1596 fclose( f );
1598 else set_error( STATUS_ACCESS_DENIED );
1599 #elif defined(HAVE_LIBPROCSTAT)
1600 struct procstat *procstat;
1601 unsigned int count;
1603 if ((procstat = procstat_open_sysctl()))
1605 struct kinfo_proc *kp = procstat_getprocs( procstat, KERN_PROC_PID, process->unix_pid, &count );
1606 if (kp)
1608 reply->virtual_size = kp->ki_size;
1609 reply->peak_virtual_size = reply->virtual_size;
1610 reply->working_set_size = kp->ki_rssize << PAGE_SHIFT;
1611 reply->peak_working_set_size = kp->ki_rusage.ru_maxrss * 1024;
1612 procstat_freeprocs( procstat, kp );
1614 else set_error( STATUS_ACCESS_DENIED );
1615 procstat_close( procstat );
1617 else set_error( STATUS_ACCESS_DENIED );
1618 #endif
1620 else set_error( STATUS_ACCESS_DENIED );
1621 release_object( process );
1624 static void set_process_affinity( struct process *process, affinity_t affinity )
1626 struct thread *thread;
1628 if (!process->running_threads)
1630 set_error( STATUS_PROCESS_IS_TERMINATING );
1631 return;
1634 process->affinity = affinity;
1636 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1638 set_thread_affinity( thread, affinity );
1642 /* set information about a process */
1643 DECL_HANDLER(set_process_info)
1645 struct process *process;
1647 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1649 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1650 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1651 release_object( process );
1655 /* read data from a process address space */
1656 DECL_HANDLER(read_process_memory)
1658 struct process *process;
1659 data_size_t len = get_reply_max_size();
1661 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1663 if (len)
1665 char *buffer = mem_alloc( len );
1666 if (buffer)
1668 if (read_process_memory( process, req->addr, len, buffer ))
1669 set_reply_data_ptr( buffer, len );
1670 else
1671 free( buffer );
1674 release_object( process );
1677 /* write data to a process address space */
1678 DECL_HANDLER(write_process_memory)
1680 struct process *process;
1682 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1684 data_size_t len = get_req_data_size();
1685 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1686 else set_error( STATUS_INVALID_PARAMETER );
1687 release_object( process );
1691 /* retrieve the process idle event */
1692 DECL_HANDLER(get_process_idle_event)
1694 struct process *process;
1696 reply->event = 0;
1697 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1699 if (process->idle_event && process != current->process)
1700 reply->event = alloc_handle( current->process, process->idle_event,
1701 EVENT_ALL_ACCESS, 0 );
1702 release_object( process );
1706 /* make the current process a system process */
1707 DECL_HANDLER(make_process_system)
1709 struct process *process;
1710 struct thread *thread;
1712 if (!shutdown_event)
1714 if (!(shutdown_event = create_event( NULL, NULL, OBJ_PERMANENT, 1, 0, NULL ))) return;
1715 release_object( shutdown_event );
1718 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION ))) return;
1720 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1722 release_object( process );
1723 return;
1726 if (!process->is_system)
1728 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1729 release_thread_desktop( thread, 0 );
1730 process->is_system = 1;
1731 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1732 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1734 release_object( process );
1737 /* create a new job object */
1738 DECL_HANDLER(create_job)
1740 struct job *job;
1741 struct unicode_str name;
1742 struct object *root;
1743 const struct security_descriptor *sd;
1744 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1746 if (!objattr) return;
1748 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1750 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1751 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1752 else
1753 reply->handle = alloc_handle_no_access_check( current->process, job,
1754 req->access, objattr->attributes );
1755 release_object( job );
1757 if (root) release_object( root );
1760 /* open a job object */
1761 DECL_HANDLER(open_job)
1763 struct unicode_str name = get_req_unicode_str();
1765 reply->handle = open_object( current->process, req->rootdir, req->access,
1766 &job_ops, &name, req->attributes );
1769 /* assign a job object to a process */
1770 DECL_HANDLER(assign_job)
1772 struct process *process;
1773 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1775 if (!job) return;
1777 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1779 if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
1780 else add_job_process( job, process );
1781 release_object( process );
1783 release_object( job );
1787 /* check if a process is associated with a job */
1788 DECL_HANDLER(process_in_job)
1790 struct process *process;
1791 struct job *job;
1793 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1794 return;
1796 if (!req->job)
1798 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1800 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1802 set_error( process_in_job( job, process ) ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1803 release_object( job );
1805 release_object( process );
1808 /* retrieve information about a job */
1809 DECL_HANDLER(get_job_info)
1811 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_QUERY );
1812 process_id_t *pids;
1813 data_size_t len;
1815 if (!job) return;
1817 reply->total_processes = job->total_processes;
1818 reply->active_processes = job->num_processes;
1820 len = min( get_reply_max_size(), reply->active_processes * sizeof(*pids) );
1821 if (len && ((pids = set_reply_data_size( len ))))
1822 get_job_pids( job, pids, pids + len / sizeof(*pids) );
1824 release_object( job );
1827 /* terminate all processes associated with the job */
1828 DECL_HANDLER(terminate_job)
1830 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1832 if (!job) return;
1834 terminate_job( job, req->status );
1835 release_object( job );
1838 /* update limits of the job object */
1839 DECL_HANDLER(set_job_limits)
1841 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1843 if (!job) return;
1845 job->limit_flags = req->limit_flags;
1846 release_object( job );
1849 /* set the jobs completion port */
1850 DECL_HANDLER(set_job_completion_port)
1852 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1854 if (!job) return;
1856 if (!job->completion_port)
1858 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1859 job->completion_key = req->key;
1861 add_job_completion_existing_processes( job, job );
1863 else
1864 set_error( STATUS_INVALID_PARAMETER );
1866 release_object( job );
1869 /* Suspend a process */
1870 DECL_HANDLER(suspend_process)
1872 struct process *process;
1874 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1876 struct list *ptr, *next;
1878 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1880 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1881 suspend_thread( thread );
1884 release_object( process );
1888 /* Resume a process */
1889 DECL_HANDLER(resume_process)
1891 struct process *process;
1893 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1895 struct list *ptr, *next;
1897 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1899 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1900 resume_thread( thread );
1903 release_object( process );
1907 /* Get a list of processes and threads currently running */
1908 DECL_HANDLER(list_processes)
1910 struct process *process;
1911 struct thread *thread;
1912 unsigned int pos = 0;
1913 char *buffer;
1915 reply->process_count = 0;
1916 reply->total_thread_count = 0;
1917 reply->total_name_len = 0;
1918 reply->info_size = 0;
1920 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1922 reply->info_size = (reply->info_size + 7) & ~7;
1923 reply->info_size += sizeof(struct process_info) + process->imagelen;
1924 reply->info_size = (reply->info_size + 7) & ~7;
1925 reply->info_size += process->running_threads * sizeof(struct thread_info);
1926 reply->process_count++;
1927 reply->total_thread_count += process->running_threads;
1928 reply->total_name_len += process->imagelen;
1931 if (reply->info_size > get_reply_max_size())
1933 set_error( STATUS_INFO_LENGTH_MISMATCH );
1934 return;
1937 if (!(buffer = set_reply_data_size( reply->info_size ))) return;
1939 memset( buffer, 0, reply->info_size );
1940 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1942 struct process_info *process_info;
1944 pos = (pos + 7) & ~7;
1945 process_info = (struct process_info *)(buffer + pos);
1946 process_info->start_time = process->start_time;
1947 process_info->name_len = process->imagelen;
1948 process_info->thread_count = process->running_threads;
1949 process_info->priority = process->priority;
1950 process_info->pid = process->id;
1951 process_info->parent_pid = process->parent_id;
1952 process_info->session_id = process->session_id;
1953 process_info->handle_count = get_handle_table_count(process);
1954 process_info->unix_pid = process->unix_pid;
1955 pos += sizeof(*process_info);
1956 memcpy( buffer + pos, process->image, process->imagelen );
1957 pos += process->imagelen;
1958 pos = (pos + 7) & ~7;
1959 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1961 struct thread_info *thread_info = (struct thread_info *)(buffer + pos);
1963 thread_info->start_time = thread->creation_time;
1964 thread_info->tid = thread->id;
1965 thread_info->base_priority = thread->priority;
1966 thread_info->current_priority = thread->priority; /* FIXME */
1967 thread_info->unix_tid = thread->unix_tid;
1968 thread_info->entry_point = thread->entry_point;
1969 thread_info->teb = thread->teb;
1970 pos += sizeof(*thread_info);