user32/tests: Avoid fix blurry apps popup in test_DisplayConfigSetDeviceInfo().
[wine.git] / server / process.c
blob58ae97cdb2ffa4bd8da6e70bee5679129fa0c214
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 #include <sys/socket.h>
35 #include <unistd.h>
36 #include <poll.h>
37 #ifdef HAVE_SYS_PARAM_H
38 # include <sys/param.h>
39 #endif
40 #ifdef HAVE_SYS_QUEUE_H
41 # include <sys/queue.h>
42 #endif
43 #ifdef HAVE_SYS_SYSCTL_H
44 # include <sys/sysctl.h>
45 #endif
46 #ifdef HAVE_SYS_USER_H
47 # define thread __unix_thread
48 # include <sys/user.h>
49 # undef thread
50 #endif
51 #ifdef HAVE_LIBPROCSTAT
52 # include <libprocstat.h>
53 #endif
55 #include "ntstatus.h"
56 #define WIN32_NO_STATUS
57 #include "winternl.h"
59 #include "file.h"
60 #include "handle.h"
61 #include "process.h"
62 #include "thread.h"
63 #include "request.h"
64 #include "user.h"
65 #include "security.h"
67 /* process object */
69 static struct list process_list = LIST_INIT(process_list);
70 static int running_processes, user_processes;
71 static struct event *shutdown_event; /* signaled when shutdown starts */
72 static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
73 static int shutdown_stage; /* current stage in the shutdown process */
75 static const WCHAR process_name[] = {'P','r','o','c','e','s','s'};
77 struct type_descr process_type =
79 { process_name, sizeof(process_name) }, /* name */
80 PROCESS_ALL_ACCESS, /* valid_access */
81 { /* mapping */
82 STANDARD_RIGHTS_READ | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
83 STANDARD_RIGHTS_WRITE | PROCESS_SUSPEND_RESUME | PROCESS_SET_INFORMATION | PROCESS_SET_QUOTA
84 | PROCESS_CREATE_PROCESS | PROCESS_DUP_HANDLE | PROCESS_VM_WRITE | PROCESS_VM_OPERATION
85 | PROCESS_CREATE_THREAD,
86 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE,
87 PROCESS_ALL_ACCESS
91 static void process_dump( struct object *obj, int verbose );
92 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
93 static unsigned int process_map_access( struct object *obj, unsigned int access );
94 static struct security_descriptor *process_get_sd( struct object *obj );
95 static void process_poll_event( struct fd *fd, int event );
96 static struct list *process_get_kernel_obj_list( struct object *obj );
97 static void process_destroy( struct object *obj );
98 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
100 static const struct object_ops process_ops =
102 sizeof(struct process), /* size */
103 &process_type, /* type */
104 process_dump, /* dump */
105 add_queue, /* add_queue */
106 remove_queue, /* remove_queue */
107 process_signaled, /* signaled */
108 no_satisfied, /* satisfied */
109 no_signal, /* signal */
110 no_get_fd, /* get_fd */
111 process_map_access, /* map_access */
112 process_get_sd, /* get_sd */
113 default_set_sd, /* set_sd */
114 no_get_full_name, /* get_full_name */
115 no_lookup_name, /* lookup_name */
116 no_link_name, /* link_name */
117 NULL, /* unlink_name */
118 no_open_file, /* open_file */
119 process_get_kernel_obj_list, /* get_kernel_obj_list */
120 no_close_handle, /* close_handle */
121 process_destroy /* destroy */
124 static const struct fd_ops process_fd_ops =
126 NULL, /* get_poll_events */
127 process_poll_event, /* poll_event */
128 NULL, /* flush */
129 NULL, /* get_fd_type */
130 NULL, /* ioctl */
131 NULL, /* queue_async */
132 NULL, /* reselect_async */
133 NULL /* cancel async */
136 /* process startup info */
138 struct startup_info
140 struct object obj; /* object header */
141 struct process *process; /* created process */
142 data_size_t info_size; /* size of startup info */
143 data_size_t data_size; /* size of whole startup data */
144 startup_info_t *data; /* data for startup info */
147 static void startup_info_dump( struct object *obj, int verbose );
148 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
149 static void startup_info_destroy( struct object *obj );
151 static const struct object_ops startup_info_ops =
153 sizeof(struct startup_info), /* size */
154 &no_type, /* type */
155 startup_info_dump, /* dump */
156 add_queue, /* add_queue */
157 remove_queue, /* remove_queue */
158 startup_info_signaled, /* signaled */
159 no_satisfied, /* satisfied */
160 no_signal, /* signal */
161 no_get_fd, /* get_fd */
162 default_map_access, /* map_access */
163 default_get_sd, /* get_sd */
164 default_set_sd, /* set_sd */
165 no_get_full_name, /* get_full_name */
166 no_lookup_name, /* lookup_name */
167 no_link_name, /* link_name */
168 NULL, /* unlink_name */
169 no_open_file, /* open_file */
170 no_kernel_obj_list, /* get_kernel_obj_list */
171 no_close_handle, /* close_handle */
172 startup_info_destroy /* destroy */
175 /* job object */
177 static const WCHAR job_name[] = {'J','o','b'};
179 struct type_descr job_type =
181 { job_name, sizeof(job_name) }, /* name */
182 JOB_OBJECT_ALL_ACCESS, /* valid_access */
183 { /* mapping */
184 STANDARD_RIGHTS_READ | JOB_OBJECT_QUERY,
185 STANDARD_RIGHTS_WRITE | JOB_OBJECT_TERMINATE | JOB_OBJECT_SET_ATTRIBUTES | JOB_OBJECT_ASSIGN_PROCESS,
186 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
187 JOB_OBJECT_ALL_ACCESS
191 static void job_dump( struct object *obj, int verbose );
192 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
193 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
194 static void job_destroy( struct object *obj );
196 struct job
198 struct object obj; /* object header */
199 struct list process_list; /* list of processes */
200 int num_processes; /* count of running processes */
201 int total_processes; /* count of processes which have been assigned */
202 unsigned int limit_flags; /* limit flags */
203 int terminating; /* job is terminating */
204 int signaled; /* job is signaled */
205 struct completion *completion_port; /* associated completion port */
206 apc_param_t completion_key; /* key to send with completion messages */
207 struct job *parent;
208 struct list parent_job_entry; /* list entry for parent job */
209 struct list child_job_list; /* list of child jobs */
212 static const struct object_ops job_ops =
214 sizeof(struct job), /* size */
215 &job_type, /* type */
216 job_dump, /* dump */
217 add_queue, /* add_queue */
218 remove_queue, /* remove_queue */
219 job_signaled, /* signaled */
220 no_satisfied, /* satisfied */
221 no_signal, /* signal */
222 no_get_fd, /* get_fd */
223 default_map_access, /* map_access */
224 default_get_sd, /* get_sd */
225 default_set_sd, /* set_sd */
226 default_get_full_name, /* get_full_name */
227 no_lookup_name, /* lookup_name */
228 directory_link_name, /* link_name */
229 default_unlink_name, /* unlink_name */
230 no_open_file, /* open_file */
231 no_kernel_obj_list, /* get_kernel_obj_list */
232 job_close_handle, /* close_handle */
233 job_destroy /* destroy */
236 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
237 unsigned int attr, const struct security_descriptor *sd )
239 struct job *job;
241 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
243 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
245 /* initialize it if it didn't already exist */
246 list_init( &job->process_list );
247 list_init( &job->child_job_list );
248 job->num_processes = 0;
249 job->total_processes = 0;
250 job->limit_flags = 0;
251 job->terminating = 0;
252 job->signaled = 0;
253 job->completion_port = NULL;
254 job->completion_key = 0;
255 job->parent = NULL;
258 return job;
261 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
263 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
266 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
268 if (job->completion_port)
269 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
272 static void add_job_completion_existing_processes( struct job *job, struct job *completion_job )
274 struct process *process;
275 struct job *j;
277 assert( completion_job->obj.ops == &job_ops );
279 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
281 add_job_completion_existing_processes( j, completion_job );
284 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
286 if (process->running_threads)
287 add_job_completion( completion_job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id( process ));
291 static int process_in_job( struct job *job, struct process *process )
293 struct job *j;
295 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
297 assert( j->parent == job );
298 if (process_in_job( j, process )) return 1;
300 return process->job == job;
303 static process_id_t *get_job_pids( struct job *job, process_id_t *pids, process_id_t *end )
305 struct process *process;
306 struct job *j;
308 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
309 pids = get_job_pids( j, pids, end );
311 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
313 if (pids == end) break;
314 if (process->end_time) continue; /* skip processes that ended */
315 *pids++ = process->id;
318 return pids;
321 static void add_job_process( struct job *job, struct process *process )
323 struct job *j, *common_parent;
324 process_id_t pid;
326 if (job == process->job) return;
328 if ((common_parent = process->job))
330 if (job->parent)
332 for (j = job->parent; j; j = j->parent)
333 if (j == common_parent) break;
335 if (j != common_parent)
337 /* Job already has parent and the process is not in the job's chain. */
338 set_error( STATUS_ACCESS_DENIED );
339 return;
341 /* process->job is referenced in the job->parent chain. */
342 release_object( process->job );
344 else
346 if (job->total_processes)
348 set_error( STATUS_ACCESS_DENIED );
349 return;
351 /* transfer reference. */
352 job->parent = process->job;
353 list_add_tail( &job->parent->child_job_list, &job->parent_job_entry );
355 list_remove( &process->job_entry );
357 process->job = (struct job *)grab_object( job );
358 list_add_tail( &job->process_list, &process->job_entry );
360 pid = get_process_id( process );
361 for (j = job; j != common_parent; j = j->parent)
363 j->num_processes++;
364 j->total_processes++;
365 add_job_completion( j, JOB_OBJECT_MSG_NEW_PROCESS, pid );
369 /* called when a process has terminated, allow one additional process */
370 static void release_job_process( struct process *process )
372 struct job *job = process->job;
374 while (job)
376 assert( job->num_processes );
377 job->num_processes--;
379 if (!job->terminating)
380 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
382 if (!job->num_processes)
383 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
385 job = job->parent;
389 static void terminate_job( struct job *job, int exit_code )
391 struct process *process, *next_process;
392 struct job *j, *next_job;
394 LIST_FOR_EACH_ENTRY_SAFE( j, next_job, &job->child_job_list, struct job, parent_job_entry )
396 assert( j->parent == job );
398 grab_object( j );
399 terminate_job( j, exit_code );
400 release_object( j );
403 job->terminating = 1;
404 LIST_FOR_EACH_ENTRY_SAFE( process, next_process, &job->process_list, struct process, job_entry )
406 assert( process->job == job );
407 if (process->running_threads) terminate_process( process, NULL, exit_code );
409 job->terminating = 0;
410 job->signaled = 1;
411 wake_up( &job->obj, 0 );
414 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
416 struct job *job = (struct job *)obj;
417 assert( obj->ops == &job_ops );
419 if (obj->handle_count == 1) /* last handle */
421 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
422 terminate_job( job, 0 );
424 return 1;
427 static void job_destroy( struct object *obj )
429 struct job *job = (struct job *)obj;
430 assert( obj->ops == &job_ops );
432 assert( !job->num_processes );
433 assert( list_empty( &job->process_list ));
434 assert( list_empty( &job->child_job_list ));
436 if (job->completion_port) release_object( job->completion_port );
437 if (job->parent)
439 list_remove( &job->parent_job_entry );
440 release_object( job->parent );
444 static void job_dump( struct object *obj, int verbose )
446 struct job *job = (struct job *)obj;
447 assert( obj->ops == &job_ops );
448 fprintf( stderr, "Job processes=%d child_jobs=%d parent=%p\n",
449 list_count(&job->process_list), list_count(&job->child_job_list), job->parent );
452 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
454 struct job *job = (struct job *)obj;
455 return job->signaled;
458 struct ptid_entry
460 void *ptr; /* entry ptr */
461 unsigned int next; /* next free entry */
464 static struct ptid_entry *ptid_entries; /* array of ptid entries */
465 static unsigned int used_ptid_entries; /* number of entries in use */
466 static unsigned int alloc_ptid_entries; /* number of allocated entries */
467 static unsigned int next_free_ptid; /* next free entry */
468 static unsigned int last_free_ptid; /* last free entry */
469 static unsigned int num_free_ptids; /* number of free ptids */
471 static void kill_all_processes(void);
473 #define PTID_OFFSET 8 /* offset for first ptid value */
475 static unsigned int index_from_ptid(unsigned int id) { return id / 4; }
476 static unsigned int ptid_from_index(unsigned int index) { return index * 4; }
478 /* allocate a new process or thread id */
479 unsigned int alloc_ptid( void *ptr )
481 struct ptid_entry *entry;
482 unsigned int index;
484 if (used_ptid_entries < alloc_ptid_entries)
486 index = used_ptid_entries + PTID_OFFSET;
487 entry = &ptid_entries[used_ptid_entries++];
489 else if (next_free_ptid && num_free_ptids >= 256)
491 index = next_free_ptid;
492 entry = &ptid_entries[index - PTID_OFFSET];
493 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
494 num_free_ptids--;
496 else /* need to grow the array */
498 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
499 if (!count) count = 512;
500 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
502 set_error( STATUS_NO_MEMORY );
503 return 0;
505 ptid_entries = entry;
506 alloc_ptid_entries = count;
507 index = used_ptid_entries + PTID_OFFSET;
508 entry = &ptid_entries[used_ptid_entries++];
511 entry->ptr = ptr;
512 return ptid_from_index( index );
515 /* free a process or thread id */
516 void free_ptid( unsigned int id )
518 unsigned int index = index_from_ptid( id );
519 struct ptid_entry *entry = &ptid_entries[index - PTID_OFFSET];
521 entry->ptr = NULL;
522 entry->next = 0;
524 /* append to end of free list so that we don't reuse it too early */
525 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = index;
526 else next_free_ptid = index;
527 last_free_ptid = index;
528 num_free_ptids++;
531 /* retrieve the pointer corresponding to a process or thread id */
532 void *get_ptid_entry( unsigned int id )
534 unsigned int index = index_from_ptid( id );
535 if (index < PTID_OFFSET) return NULL;
536 if (index - PTID_OFFSET >= used_ptid_entries) return NULL;
537 return ptid_entries[index - PTID_OFFSET].ptr;
540 /* return the main thread of the process */
541 struct thread *get_process_first_thread( struct process *process )
543 struct list *ptr = list_head( &process->thread_list );
544 if (!ptr) return NULL;
545 return LIST_ENTRY( ptr, struct thread, proc_entry );
548 /* set the state of the process startup info */
549 static void set_process_startup_state( struct process *process, enum startup_state state )
551 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
552 if (process->startup_info)
554 wake_up( &process->startup_info->obj, 0 );
555 release_object( process->startup_info );
556 process->startup_info = NULL;
560 /* callback for server shutdown */
561 static void server_shutdown_timeout( void *arg )
563 shutdown_timeout = NULL;
564 if (!running_processes)
566 close_master_socket( 0 );
567 return;
569 switch(++shutdown_stage)
571 case 1: /* signal system processes to exit */
572 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
573 if (shutdown_event) set_event( shutdown_event );
574 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
575 close_master_socket( 4 * -TICKS_PER_SEC );
576 break;
577 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
578 kill_all_processes();
579 break;
583 /* forced shutdown, used for wineserver -k */
584 void shutdown_master_socket(void)
586 kill_all_processes();
587 shutdown_stage = 2;
588 if (shutdown_timeout)
590 remove_timeout_user( shutdown_timeout );
591 shutdown_timeout = NULL;
593 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
596 /* final cleanup once we are sure a process is really dead */
597 static void process_died( struct process *process )
599 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
600 if (!process->is_system)
602 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
603 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
605 release_object( process );
606 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
609 /* callback for process sigkill timeout */
610 static void process_sigkill( void *private )
612 struct process *process = private;
613 int signal = 0;
615 process->sigkill_delay *= 2;
616 if (process->sigkill_delay >= TICKS_PER_SEC / 2)
617 signal = SIGKILL;
619 if (!kill( process->unix_pid, signal ) && !signal)
620 process->sigkill_timeout = add_timeout_user( -process->sigkill_delay, process_sigkill, process );
621 else
623 process->sigkill_delay = TICKS_PER_SEC / 64;
624 process->sigkill_timeout = NULL;
625 process_died( process );
629 /* start the sigkill timer for a process upon exit */
630 static void start_sigkill_timer( struct process *process )
632 grab_object( process );
633 if (process->unix_pid != -1)
634 process->sigkill_timeout = add_timeout_user( -process->sigkill_delay, process_sigkill, process );
635 else
636 process_died( process );
639 /* create a new process */
640 /* if the function fails the fd is closed */
641 struct process *create_process( int fd, struct process *parent, unsigned int flags, const startup_info_t *info,
642 const struct security_descriptor *sd, const obj_handle_t *handles,
643 unsigned int handle_count, struct token *token )
645 struct process *process;
647 if (!(process = alloc_object( &process_ops )))
649 close( fd );
650 goto error;
652 process->parent_id = 0;
653 process->debug_obj = NULL;
654 process->debug_event = NULL;
655 process->handles = NULL;
656 process->msg_fd = NULL;
657 process->sigkill_timeout = NULL;
658 process->sigkill_delay = TICKS_PER_SEC / 64;
659 process->unix_pid = -1;
660 process->exit_code = STILL_ACTIVE;
661 process->running_threads = 0;
662 process->priority = PROCESS_PRIOCLASS_NORMAL;
663 process->suspend = 0;
664 process->is_system = 0;
665 process->debug_children = 1;
666 process->is_terminating = 0;
667 process->imagelen = 0;
668 process->image = NULL;
669 process->job = NULL;
670 process->console = NULL;
671 process->startup_state = STARTUP_IN_PROGRESS;
672 process->startup_info = NULL;
673 process->idle_event = NULL;
674 process->peb = 0;
675 process->ldt_copy = 0;
676 process->dir_cache = NULL;
677 process->winstation = 0;
678 process->desktop = 0;
679 process->token = NULL;
680 process->trace_data = 0;
681 process->rawinput_mouse = NULL;
682 process->rawinput_kbd = NULL;
683 memset( &process->image_info, 0, sizeof(process->image_info) );
684 list_init( &process->kernel_object );
685 list_init( &process->thread_list );
686 list_init( &process->locks );
687 list_init( &process->asyncs );
688 list_init( &process->classes );
689 list_init( &process->views );
690 list_init( &process->rawinput_devices );
692 process->end_time = 0;
694 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
695 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
697 close( fd );
698 goto error;
700 if (!(process->id = process->group_id = alloc_ptid( process )))
702 close( fd );
703 goto error;
705 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
707 /* create the handle table */
708 if (!parent)
710 process->handles = alloc_handle_table( process, 0 );
711 process->token = token_create_admin( TRUE, -1, TokenElevationTypeFull, default_session_id );
712 process->affinity = ~0;
714 else
716 obj_handle_t std_handles[3];
718 std_handles[0] = info->hstdin;
719 std_handles[1] = info->hstdout;
720 std_handles[2] = info->hstderr;
722 process->parent_id = parent->id;
723 if (flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES)
724 process->handles = copy_handle_table( process, parent, handles, handle_count, std_handles );
725 else
726 process->handles = alloc_handle_table( process, 0 );
727 /* Note: for security reasons, starting a new process does not attempt
728 * to use the current impersonation token for the new process */
729 process->token = token_duplicate( token ? token : parent->token, TRUE, 0, NULL, NULL, 0, NULL, 0 );
730 process->affinity = parent->affinity;
732 if (!process->handles || !process->token) goto error;
733 process->session_id = token_get_session_id( process->token );
735 /* Assign a high security label to the token. The default would be medium
736 * but Wine provides admin access to all applications right now so high
737 * makes more sense for the time being. */
738 if (!token_assign_label( process->token, security_high_label_sid ))
739 goto error;
741 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
742 return process;
744 error:
745 if (process) release_object( process );
746 /* if we failed to start our first process, close everything down */
747 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
748 return NULL;
751 /* get the process data size */
752 data_size_t get_process_startup_info_size( struct process *process )
754 struct startup_info *info = process->startup_info;
756 if (!info) return 0;
757 return info->data_size;
760 /* destroy a process when its refcount is 0 */
761 static void process_destroy( struct object *obj )
763 struct process *process = (struct process *)obj;
764 assert( obj->ops == &process_ops );
766 /* we can't have a thread remaining */
767 assert( list_empty( &process->thread_list ));
768 assert( list_empty( &process->asyncs ));
770 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
772 close_process_handles( process );
773 set_process_startup_state( process, STARTUP_ABORTED );
775 if (process->job)
777 list_remove( &process->job_entry );
778 release_object( process->job );
780 if (process->console) release_object( process->console );
781 if (process->msg_fd) release_object( process->msg_fd );
782 if (process->idle_event) release_object( process->idle_event );
783 if (process->id) free_ptid( process->id );
784 if (process->token) release_object( process->token );
785 free( process->dir_cache );
786 free( process->image );
789 /* dump a process on stdout for debugging purposes */
790 static void process_dump( struct object *obj, int verbose )
792 struct process *process = (struct process *)obj;
793 assert( obj->ops == &process_ops );
795 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
798 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
800 struct process *process = (struct process *)obj;
801 return !process->running_threads;
804 static unsigned int process_map_access( struct object *obj, unsigned int access )
806 access = default_map_access( obj, access );
807 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
808 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
809 return access;
812 static struct list *process_get_kernel_obj_list( struct object *obj )
814 struct process *process = (struct process *)obj;
815 return &process->kernel_object;
818 static struct security_descriptor *process_get_sd( struct object *obj )
820 static struct security_descriptor *process_default_sd;
822 if (obj->sd) return obj->sd;
824 if (!process_default_sd)
826 size_t users_sid_len = security_sid_len( security_domain_users_sid );
827 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
828 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
829 + users_sid_len + admins_sid_len;
830 ACCESS_ALLOWED_ACE *aaa;
831 ACL *dacl;
833 process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
834 + dacl_len );
835 process_default_sd->control = SE_DACL_PRESENT;
836 process_default_sd->owner_len = admins_sid_len;
837 process_default_sd->group_len = users_sid_len;
838 process_default_sd->sacl_len = 0;
839 process_default_sd->dacl_len = dacl_len;
840 memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
841 memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
843 dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
844 dacl->AclRevision = ACL_REVISION;
845 dacl->Sbz1 = 0;
846 dacl->AclSize = dacl_len;
847 dacl->AceCount = 2;
848 dacl->Sbz2 = 0;
849 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
850 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
851 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
852 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
853 aaa->Mask = GENERIC_READ;
854 memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
855 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
856 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
857 aaa->Header.AceFlags = 0;
858 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
859 aaa->Mask = PROCESS_ALL_ACCESS;
860 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
862 return process_default_sd;
865 static void process_poll_event( struct fd *fd, int event )
867 struct process *process = get_fd_user( fd );
868 assert( process->obj.ops == &process_ops );
870 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
871 else if (event & POLLIN) receive_fd( process );
874 static void startup_info_destroy( struct object *obj )
876 struct startup_info *info = (struct startup_info *)obj;
877 assert( obj->ops == &startup_info_ops );
878 free( info->data );
879 if (info->process) release_object( info->process );
882 static void startup_info_dump( struct object *obj, int verbose )
884 struct startup_info *info = (struct startup_info *)obj;
885 assert( obj->ops == &startup_info_ops );
887 fputs( "Startup info", stderr );
888 if (info->data)
889 fprintf( stderr, " in=%04x out=%04x err=%04x",
890 info->data->hstdin, info->data->hstdout, info->data->hstderr );
891 fputc( '\n', stderr );
894 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
896 struct startup_info *info = (struct startup_info *)obj;
897 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
900 /* get a process from an id (and increment the refcount) */
901 struct process *get_process_from_id( process_id_t id )
903 struct object *obj = get_ptid_entry( id );
905 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
906 set_error( STATUS_INVALID_CID );
907 return NULL;
910 /* get a process from a handle (and increment the refcount) */
911 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
913 return (struct process *)get_handle_obj( current->process, handle,
914 access, &process_ops );
917 /* terminate a process with the given exit code */
918 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
920 struct thread *thread;
922 grab_object( process ); /* make sure it doesn't get freed when threads die */
923 process->is_terminating = 1;
925 restart:
926 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
928 if (exit_code) thread->exit_code = exit_code;
929 if (thread == skip) continue;
930 if (thread->state == TERMINATED) continue;
931 kill_thread( thread, 1 );
932 goto restart;
934 release_object( process );
937 /* kill all processes */
938 static void kill_all_processes(void)
940 struct list *ptr;
942 while ((ptr = list_head( &process_list )))
944 struct process *process = LIST_ENTRY( ptr, struct process, entry );
945 terminate_process( process, NULL, 1 );
949 /* kill all processes being attached to a console renderer */
950 void kill_console_processes( struct thread *renderer, int exit_code )
952 for (;;) /* restart from the beginning of the list every time */
954 struct process *process;
956 /* find the first process being attached to 'renderer' and still running */
957 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
959 if (process == renderer->process) continue;
960 if (process->console && console_get_renderer( process->console ) == renderer) break;
962 if (&process->entry == &process_list) break; /* no process found */
963 terminate_process( process, NULL, exit_code );
967 /* a process has been killed (i.e. its last thread died) */
968 static void process_killed( struct process *process )
970 struct list *ptr;
972 assert( list_empty( &process->thread_list ));
973 process->end_time = current_time;
974 close_process_desktop( process );
975 process->winstation = 0;
976 process->desktop = 0;
977 cancel_process_asyncs( process );
978 close_process_handles( process );
979 if (process->idle_event) release_object( process->idle_event );
980 process->idle_event = NULL;
981 assert( !process->console );
983 while ((ptr = list_head( &process->rawinput_devices )))
985 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
986 list_remove( &entry->entry );
987 free( entry );
989 destroy_process_classes( process );
990 free_mapped_views( process );
991 free_process_user_handles( process );
992 remove_process_locks( process );
993 set_process_startup_state( process, STARTUP_ABORTED );
994 finish_process_tracing( process );
995 release_job_process( process );
996 start_sigkill_timer( process );
997 wake_up( &process->obj, 0 );
1000 /* add a thread to a process running threads list */
1001 void add_process_thread( struct process *process, struct thread *thread )
1003 list_add_tail( &process->thread_list, &thread->proc_entry );
1004 if (!process->running_threads++)
1006 list_add_tail( &process_list, &process->entry );
1007 running_processes++;
1008 if (!process->is_system)
1010 if (!user_processes++ && shutdown_timeout)
1012 remove_timeout_user( shutdown_timeout );
1013 shutdown_timeout = NULL;
1017 grab_object( thread );
1020 /* remove a thread from a process running threads list */
1021 void remove_process_thread( struct process *process, struct thread *thread )
1023 assert( process->running_threads > 0 );
1024 assert( !list_empty( &process->thread_list ));
1026 list_remove( &thread->proc_entry );
1028 if (!--process->running_threads)
1030 /* we have removed the last running thread, exit the process */
1031 process->exit_code = thread->exit_code;
1032 generate_debug_event( thread, DbgExitProcessStateChange, process );
1033 list_remove( &process->entry );
1034 process_killed( process );
1036 else generate_debug_event( thread, DbgExitThreadStateChange, thread );
1037 release_object( thread );
1040 /* suspend all the threads of a process */
1041 void suspend_process( struct process *process )
1043 if (!process->suspend++)
1045 struct list *ptr, *next;
1047 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1049 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1050 if (!thread->suspend) stop_thread( thread );
1055 /* resume all the threads of a process */
1056 void resume_process( struct process *process )
1058 assert (process->suspend > 0);
1059 if (!--process->suspend)
1061 struct list *ptr, *next;
1063 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1065 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1066 if (!thread->suspend) wake_thread( thread );
1071 /* kill a process on the spot */
1072 void kill_process( struct process *process, int violent_death )
1074 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
1076 release_object( process->msg_fd );
1077 process->msg_fd = NULL;
1080 if (process->sigkill_timeout) return; /* already waiting for it to die */
1082 if (violent_death) terminate_process( process, NULL, 1 );
1083 else
1085 struct list *ptr;
1087 grab_object( process ); /* make sure it doesn't get freed when threads die */
1088 while ((ptr = list_head( &process->thread_list )))
1090 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1091 kill_thread( thread, 0 );
1093 release_object( process );
1097 /* detach all processes being debugged by a given thread */
1098 void detach_debugged_processes( struct debug_obj *debug_obj, int exit_code )
1100 for (;;) /* restart from the beginning of the list every time */
1102 struct process *process;
1104 /* find the first process being debugged by 'debugger' and still running */
1105 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1106 if (process->debug_obj == debug_obj) break;
1108 if (&process->entry == &process_list) break; /* no process found */
1109 if (exit_code)
1111 process->debug_obj = NULL;
1112 terminate_process( process, NULL, exit_code );
1114 else debugger_detach( process, debug_obj );
1119 void enum_processes( int (*cb)(struct process*, void*), void *user )
1121 struct list *ptr, *next;
1123 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1125 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1126 if ((cb)(process, user)) break;
1130 /* set the debugged flag in the process PEB */
1131 int set_process_debug_flag( struct process *process, int flag )
1133 char data = (flag != 0);
1134 client_ptr_t peb32 = 0;
1136 if (!is_machine_64bit( process->machine ) && is_machine_64bit( native_machine ))
1137 peb32 = process->peb + 0x1000;
1139 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1140 if (peb32 && !write_process_memory( process, peb32 + 2, 1, &data )) return 0;
1141 return write_process_memory( process, process->peb + 2, 1, &data );
1144 /* create a new process */
1145 DECL_HANDLER(new_process)
1147 struct startup_info *info;
1148 const void *info_ptr;
1149 struct unicode_str name;
1150 const struct security_descriptor *sd;
1151 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1152 struct process *process = NULL;
1153 struct token *token = NULL;
1154 struct debug_obj *debug_obj = NULL;
1155 struct process *parent;
1156 struct thread *parent_thread = current;
1157 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1158 const obj_handle_t *handles = NULL;
1159 const obj_handle_t *job_handles = NULL;
1160 unsigned int i, job_handle_count;
1161 struct job *job;
1163 if (socket_fd == -1)
1165 set_error( STATUS_INVALID_PARAMETER );
1166 return;
1168 if (!objattr)
1170 set_error( STATUS_INVALID_PARAMETER );
1171 close( socket_fd );
1172 return;
1174 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1176 set_error( STATUS_INVALID_HANDLE );
1177 close( socket_fd );
1178 return;
1180 if (shutdown_stage)
1182 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1183 close( socket_fd );
1184 return;
1187 if (req->parent_process)
1189 if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
1191 close( socket_fd );
1192 return;
1194 parent_thread = NULL;
1196 else parent = (struct process *)grab_object( current->process );
1198 /* If a job further in the job chain does not permit breakaway process creation
1199 * succeeds and the process which is trying to breakaway is assigned to that job. */
1200 if (parent->job && (req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY) &&
1201 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1203 set_error( STATUS_ACCESS_DENIED );
1204 close( socket_fd );
1205 release_object( parent );
1206 return;
1209 /* build the startup info for a new process */
1210 if (!(info = alloc_object( &startup_info_ops )))
1212 close( socket_fd );
1213 release_object( parent );
1214 return;
1216 info->process = NULL;
1217 info->data = NULL;
1219 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1221 if ((req->handles_size & 3) || req->handles_size > info->data_size)
1223 set_error( STATUS_INVALID_PARAMETER );
1224 close( socket_fd );
1225 goto done;
1227 if (req->handles_size)
1229 handles = info_ptr;
1230 info_ptr = (const char *)info_ptr + req->handles_size;
1231 info->data_size -= req->handles_size;
1234 if ((req->jobs_size & 3) || req->jobs_size > info->data_size)
1236 set_error( STATUS_INVALID_PARAMETER );
1237 close( socket_fd );
1238 goto done;
1240 if (req->jobs_size)
1242 job_handles = info_ptr;
1243 info_ptr = (const char *)info_ptr + req->jobs_size;
1244 info->data_size -= req->jobs_size;
1247 job_handle_count = req->jobs_size / sizeof(*handles);
1248 for (i = 0; i < job_handle_count; ++i)
1250 if (!(job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS )))
1252 close( socket_fd );
1253 goto done;
1255 release_object( job );
1258 info->info_size = min( req->info_size, info->data_size );
1260 if (req->info_size < sizeof(*info->data))
1262 /* make sure we have a full startup_info_t structure */
1263 data_size_t env_size = info->data_size - info->info_size;
1264 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1266 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1268 close( socket_fd );
1269 goto done;
1271 memcpy( info->data, info_ptr, info_size );
1272 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1273 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1274 info->info_size = sizeof(startup_info_t);
1275 info->data_size = info->info_size + env_size;
1277 else
1279 data_size_t pos = sizeof(*info->data);
1281 if (!(info->data = memdup( info_ptr, info->data_size )))
1283 close( socket_fd );
1284 goto done;
1286 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1287 FIXUP_LEN( info->data->curdir_len );
1288 FIXUP_LEN( info->data->dllpath_len );
1289 FIXUP_LEN( info->data->imagepath_len );
1290 FIXUP_LEN( info->data->cmdline_len );
1291 FIXUP_LEN( info->data->title_len );
1292 FIXUP_LEN( info->data->desktop_len );
1293 FIXUP_LEN( info->data->shellinfo_len );
1294 FIXUP_LEN( info->data->runtime_len );
1295 #undef FIXUP_LEN
1298 if (req->token && !(token = get_token_obj( current->process, req->token, TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY )))
1300 close( socket_fd );
1301 goto done;
1303 if (req->debug && !(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
1305 close( socket_fd );
1306 goto done;
1309 if (!(process = create_process( socket_fd, parent, req->flags, info->data, sd,
1310 handles, req->handles_size / sizeof(*handles), token )))
1311 goto done;
1313 process->startup_info = (struct startup_info *)grab_object( info );
1315 job = parent->job;
1316 while (job)
1318 if (!(job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
1319 && !(req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY
1320 && job->limit_flags & JOB_OBJECT_LIMIT_BREAKAWAY_OK))
1322 add_job_process( job, process );
1323 assert( !get_error() );
1324 break;
1326 job = job->parent;
1329 for (i = 0; i < job_handle_count; ++i)
1331 job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS );
1332 add_job_process( job, process );
1333 release_object( job );
1334 if (get_error())
1336 release_job_process( process );
1337 goto done;
1341 /* connect to the window station */
1342 connect_process_winstation( process, parent_thread, parent );
1344 /* set the process console */
1345 if (info->data->console > 3)
1346 info->data->console = duplicate_handle( parent, info->data->console, process,
1347 0, 0, DUPLICATE_SAME_ACCESS );
1349 if (!(req->flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES) && info->data->console != 1)
1351 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1352 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1353 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1354 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1355 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1356 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1357 /* some handles above may have been invalid; this is not an error */
1358 if (get_error() == STATUS_INVALID_HANDLE ||
1359 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1362 /* attach to the debugger */
1363 if (debug_obj)
1365 process->debug_obj = debug_obj;
1366 process->debug_children = !(req->flags & PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT);
1368 else if (parent->debug_children)
1370 process->debug_obj = parent->debug_obj;
1371 /* debug_children is set to 1 by default */
1374 if (!info->data->console_flags) process->group_id = parent->group_id;
1376 info->process = (struct process *)grab_object( process );
1377 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1378 reply->pid = get_process_id( process );
1379 reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
1381 done:
1382 if (process) release_object( process );
1383 if (debug_obj) release_object( debug_obj );
1384 if (token) release_object( token );
1385 release_object( parent );
1386 release_object( info );
1389 /* Retrieve information about a newly started process */
1390 DECL_HANDLER(get_new_process_info)
1392 struct startup_info *info;
1394 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1395 0, &startup_info_ops )))
1397 reply->success = is_process_init_done( info->process );
1398 reply->exit_code = info->process->exit_code;
1399 release_object( info );
1403 /* Retrieve the new process startup info */
1404 DECL_HANDLER(get_startup_info)
1406 struct process *process = current->process;
1407 struct startup_info *info = process->startup_info;
1408 data_size_t size;
1410 if (!info) return;
1412 /* we return the data directly without making a copy so this can only be called once */
1413 reply->info_size = info->info_size;
1414 size = info->data_size;
1415 if (size > get_reply_max_size()) size = get_reply_max_size();
1416 set_reply_data_ptr( info->data, size );
1417 info->data = NULL;
1418 info->data_size = 0;
1421 /* signal the end of the process initialization */
1422 DECL_HANDLER(init_process_done)
1424 struct process *process = current->process;
1425 struct memory_view *view;
1426 client_ptr_t base;
1427 const pe_image_info_t *image_info;
1429 if (is_process_init_done(process))
1431 set_error( STATUS_INVALID_PARAMETER );
1432 return;
1434 if (!(view = get_exe_view( process )))
1436 set_error( STATUS_DLL_NOT_FOUND );
1437 return;
1439 if (!(image_info = get_view_image_info( view, &base ))) return;
1441 current->teb = req->teb;
1442 process->peb = req->peb;
1443 process->ldt_copy = req->ldt_copy;
1445 process->start_time = current_time;
1446 current->entry_point = base + image_info->entry_point;
1448 init_process_tracing( process );
1449 generate_startup_debug_events( process );
1450 set_process_startup_state( process, STARTUP_DONE );
1452 if (image_info->subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI)
1453 process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1454 if (process->debug_obj) set_process_debug_flag( process, 1 );
1455 reply->entry = current->entry_point;
1456 reply->suspend = (current->suspend || process->suspend);
1459 /* open a handle to a process */
1460 DECL_HANDLER(open_process)
1462 struct process *process = get_process_from_id( req->pid );
1463 reply->handle = 0;
1464 if (process)
1466 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1467 release_object( process );
1471 /* terminate a process */
1472 DECL_HANDLER(terminate_process)
1474 struct process *process;
1476 if (req->handle)
1478 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1479 if (!process) return;
1481 else process = (struct process *)grab_object( current->process );
1483 reply->self = (current->process == process);
1484 terminate_process( process, current, req->exit_code );
1485 release_object( process );
1488 /* fetch information about a process */
1489 DECL_HANDLER(get_process_info)
1491 struct process *process;
1493 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1495 reply->pid = get_process_id( process );
1496 reply->ppid = process->parent_id;
1497 reply->exit_code = process->exit_code;
1498 reply->priority = process->priority;
1499 reply->affinity = process->affinity;
1500 reply->peb = process->peb;
1501 reply->start_time = process->start_time;
1502 reply->end_time = process->end_time;
1503 reply->session_id = process->session_id;
1504 reply->machine = process->machine;
1505 if (get_reply_max_size())
1507 if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
1508 else set_reply_data( &process->image_info, min( sizeof(process->image_info), get_reply_max_size() ));
1510 release_object( process );
1514 /* retrieve debug information about a process */
1515 DECL_HANDLER(get_process_debug_info)
1517 struct process *process;
1519 if (!(process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION ))) return;
1521 reply->debug_children = process->debug_children;
1522 if (!process->debug_obj) set_error( STATUS_PORT_NOT_SET );
1523 else reply->debug = alloc_handle( current->process, process->debug_obj, MAXIMUM_ALLOWED, 0 );
1524 release_object( process );
1527 /* fetch the name of the process image */
1528 DECL_HANDLER(get_process_image_name)
1530 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1532 if (!process) return;
1533 if (process->image)
1535 struct unicode_str name = { process->image, process->imagelen };
1536 /* skip the \??\ prefix */
1537 if (req->win32 && name.len > 6 * sizeof(WCHAR) && name.str[5] == ':')
1539 name.str += 4;
1540 name.len -= 4 * sizeof(WCHAR);
1542 /* FIXME: else resolve symlinks in NT path */
1544 reply->len = name.len;
1545 if (name.len <= get_reply_max_size())
1547 WCHAR *ptr = set_reply_data( name.str, name.len );
1548 /* change \??\ to \\?\ */
1549 if (req->win32 && name.len > sizeof(WCHAR) && ptr[1] == '?') ptr[1] = '\\';
1551 else set_error( STATUS_BUFFER_TOO_SMALL );
1553 release_object( process );
1556 /* retrieve information about a process memory usage */
1557 DECL_HANDLER(get_process_vm_counters)
1559 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1561 if (!process) return;
1562 if (process->unix_pid != -1)
1564 #ifdef linux
1565 FILE *f;
1566 char proc_path[32], line[256];
1567 unsigned long value;
1569 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1570 if ((f = fopen( proc_path, "r" )))
1572 while (fgets( line, sizeof(line), f ))
1574 if (sscanf( line, "VmPeak: %lu", &value ))
1575 reply->peak_virtual_size = (mem_size_t)value * 1024;
1576 else if (sscanf( line, "VmSize: %lu", &value ))
1577 reply->virtual_size = (mem_size_t)value * 1024;
1578 else if (sscanf( line, "VmHWM: %lu", &value ))
1579 reply->peak_working_set_size = (mem_size_t)value * 1024;
1580 else if (sscanf( line, "VmRSS: %lu", &value ))
1581 reply->working_set_size = (mem_size_t)value * 1024;
1582 else if (sscanf( line, "RssAnon: %lu", &value ))
1583 reply->pagefile_usage += (mem_size_t)value * 1024;
1584 else if (sscanf( line, "VmSwap: %lu", &value ))
1585 reply->pagefile_usage += (mem_size_t)value * 1024;
1587 reply->peak_pagefile_usage = reply->pagefile_usage;
1588 fclose( f );
1590 else set_error( STATUS_ACCESS_DENIED );
1591 #elif defined(HAVE_LIBPROCSTAT)
1592 struct procstat *procstat;
1593 unsigned int count;
1595 if ((procstat = procstat_open_sysctl()))
1597 struct kinfo_proc *kp = procstat_getprocs( procstat, KERN_PROC_PID, process->unix_pid, &count );
1598 if (kp)
1600 reply->virtual_size = kp->ki_size;
1601 reply->peak_virtual_size = reply->virtual_size;
1602 reply->working_set_size = kp->ki_rssize << PAGE_SHIFT;
1603 reply->peak_working_set_size = kp->ki_rusage.ru_maxrss * 1024;
1604 procstat_freeprocs( procstat, kp );
1606 else set_error( STATUS_ACCESS_DENIED );
1607 procstat_close( procstat );
1609 else set_error( STATUS_ACCESS_DENIED );
1610 #endif
1612 else set_error( STATUS_ACCESS_DENIED );
1613 release_object( process );
1616 static void set_process_affinity( struct process *process, affinity_t affinity )
1618 struct thread *thread;
1620 if (!process->running_threads)
1622 set_error( STATUS_PROCESS_IS_TERMINATING );
1623 return;
1626 process->affinity = affinity;
1628 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1630 set_thread_affinity( thread, affinity );
1634 /* set information about a process */
1635 DECL_HANDLER(set_process_info)
1637 struct process *process;
1639 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1641 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1642 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1643 release_object( process );
1647 /* read data from a process address space */
1648 DECL_HANDLER(read_process_memory)
1650 struct process *process;
1651 data_size_t len = get_reply_max_size();
1653 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1655 if (len)
1657 char *buffer = mem_alloc( len );
1658 if (buffer)
1660 if (read_process_memory( process, req->addr, len, buffer ))
1661 set_reply_data_ptr( buffer, len );
1662 else
1663 free( buffer );
1666 release_object( process );
1669 /* write data to a process address space */
1670 DECL_HANDLER(write_process_memory)
1672 struct process *process;
1674 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1676 data_size_t len = get_req_data_size();
1677 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1678 else set_error( STATUS_INVALID_PARAMETER );
1679 release_object( process );
1683 /* retrieve the process idle event */
1684 DECL_HANDLER(get_process_idle_event)
1686 struct process *process;
1688 reply->event = 0;
1689 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1691 if (process->idle_event && process != current->process)
1692 reply->event = alloc_handle( current->process, process->idle_event,
1693 EVENT_ALL_ACCESS, 0 );
1694 release_object( process );
1698 /* make the current process a system process */
1699 DECL_HANDLER(make_process_system)
1701 struct process *process;
1702 struct thread *thread;
1704 if (!shutdown_event)
1706 if (!(shutdown_event = create_event( NULL, NULL, OBJ_PERMANENT, 1, 0, NULL ))) return;
1707 release_object( shutdown_event );
1710 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION ))) return;
1712 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1714 release_object( process );
1715 return;
1718 if (!process->is_system)
1720 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1721 release_thread_desktop( thread, 0 );
1722 process->is_system = 1;
1723 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1724 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1726 release_object( process );
1729 /* create a new job object */
1730 DECL_HANDLER(create_job)
1732 struct job *job;
1733 struct unicode_str name;
1734 struct object *root;
1735 const struct security_descriptor *sd;
1736 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1738 if (!objattr) return;
1740 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1742 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1743 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1744 else
1745 reply->handle = alloc_handle_no_access_check( current->process, job,
1746 req->access, objattr->attributes );
1747 release_object( job );
1749 if (root) release_object( root );
1752 /* open a job object */
1753 DECL_HANDLER(open_job)
1755 struct unicode_str name = get_req_unicode_str();
1757 reply->handle = open_object( current->process, req->rootdir, req->access,
1758 &job_ops, &name, req->attributes );
1761 /* assign a job object to a process */
1762 DECL_HANDLER(assign_job)
1764 struct process *process;
1765 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1767 if (!job) return;
1769 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1771 if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
1772 else add_job_process( job, process );
1773 release_object( process );
1775 release_object( job );
1779 /* check if a process is associated with a job */
1780 DECL_HANDLER(process_in_job)
1782 struct process *process;
1783 struct job *job;
1785 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1786 return;
1788 if (!req->job)
1790 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1792 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1794 set_error( process_in_job( job, process ) ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1795 release_object( job );
1797 release_object( process );
1800 /* retrieve information about a job */
1801 DECL_HANDLER(get_job_info)
1803 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_QUERY );
1804 process_id_t *pids;
1805 data_size_t len;
1807 if (!job) return;
1809 reply->total_processes = job->total_processes;
1810 reply->active_processes = job->num_processes;
1812 len = min( get_reply_max_size(), reply->active_processes * sizeof(*pids) );
1813 if (len && ((pids = set_reply_data_size( len ))))
1814 get_job_pids( job, pids, pids + len / sizeof(*pids) );
1816 release_object( job );
1819 /* terminate all processes associated with the job */
1820 DECL_HANDLER(terminate_job)
1822 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1824 if (!job) return;
1826 terminate_job( job, req->status );
1827 release_object( job );
1830 /* update limits of the job object */
1831 DECL_HANDLER(set_job_limits)
1833 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1835 if (!job) return;
1837 job->limit_flags = req->limit_flags;
1838 release_object( job );
1841 /* set the jobs completion port */
1842 DECL_HANDLER(set_job_completion_port)
1844 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1846 if (!job) return;
1848 if (!job->completion_port)
1850 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1851 job->completion_key = req->key;
1853 add_job_completion_existing_processes( job, job );
1855 else
1856 set_error( STATUS_INVALID_PARAMETER );
1858 release_object( job );
1861 /* Suspend a process */
1862 DECL_HANDLER(suspend_process)
1864 struct process *process;
1866 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1868 struct list *ptr, *next;
1870 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1872 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1873 suspend_thread( thread );
1876 release_object( process );
1880 /* Resume a process */
1881 DECL_HANDLER(resume_process)
1883 struct process *process;
1885 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1887 struct list *ptr, *next;
1889 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1891 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1892 resume_thread( thread );
1895 release_object( process );
1899 /* Get a list of processes and threads currently running */
1900 DECL_HANDLER(list_processes)
1902 struct process *process;
1903 struct thread *thread;
1904 unsigned int pos = 0;
1905 char *buffer;
1907 reply->process_count = 0;
1908 reply->total_thread_count = 0;
1909 reply->total_name_len = 0;
1910 reply->info_size = 0;
1912 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1914 reply->info_size = (reply->info_size + 7) & ~7;
1915 reply->info_size += sizeof(struct process_info) + process->imagelen;
1916 reply->info_size = (reply->info_size + 7) & ~7;
1917 reply->info_size += process->running_threads * sizeof(struct thread_info);
1918 reply->process_count++;
1919 reply->total_thread_count += process->running_threads;
1920 reply->total_name_len += process->imagelen;
1923 if (reply->info_size > get_reply_max_size())
1925 set_error( STATUS_INFO_LENGTH_MISMATCH );
1926 return;
1929 if (!(buffer = set_reply_data_size( reply->info_size ))) return;
1931 memset( buffer, 0, reply->info_size );
1932 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1934 struct process_info *process_info;
1936 pos = (pos + 7) & ~7;
1937 process_info = (struct process_info *)(buffer + pos);
1938 process_info->start_time = process->start_time;
1939 process_info->name_len = process->imagelen;
1940 process_info->thread_count = process->running_threads;
1941 process_info->priority = process->priority;
1942 process_info->pid = process->id;
1943 process_info->parent_pid = process->parent_id;
1944 process_info->session_id = process->session_id;
1945 process_info->handle_count = get_handle_table_count(process);
1946 process_info->unix_pid = process->unix_pid;
1947 pos += sizeof(*process_info);
1948 memcpy( buffer + pos, process->image, process->imagelen );
1949 pos += process->imagelen;
1950 pos = (pos + 7) & ~7;
1951 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1953 struct thread_info *thread_info = (struct thread_info *)(buffer + pos);
1955 thread_info->start_time = thread->creation_time;
1956 thread_info->tid = thread->id;
1957 thread_info->base_priority = thread->priority;
1958 thread_info->current_priority = thread->priority; /* FIXME */
1959 thread_info->unix_tid = thread->unix_tid;
1960 thread_info->entry_point = thread->entry_point;
1961 thread_info->teb = thread->teb;
1962 pos += sizeof(*thread_info);