server: Check for system regs before suspending for ptrace.
[wine.git] / server / process.c
blob87ff3186bb552addada18cdabaefde1b43657438
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.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/time.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #include <unistd.h>
37 #ifdef HAVE_POLL_H
38 #include <poll.h>
39 #endif
41 #include "ntstatus.h"
42 #define WIN32_NO_STATUS
43 #include "winternl.h"
45 #include "file.h"
46 #include "handle.h"
47 #include "process.h"
48 #include "thread.h"
49 #include "request.h"
50 #include "user.h"
51 #include "security.h"
53 /* process object */
55 static struct list process_list = LIST_INIT(process_list);
56 static int running_processes, user_processes;
57 static struct event *shutdown_event; /* signaled when shutdown starts */
58 static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
59 static int shutdown_stage; /* current stage in the shutdown process */
61 static const WCHAR process_name[] = {'P','r','o','c','e','s','s'};
63 struct type_descr process_type =
65 { process_name, sizeof(process_name) }, /* name */
66 PROCESS_ALL_ACCESS, /* valid_access */
67 { /* mapping */
68 STANDARD_RIGHTS_READ | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
69 STANDARD_RIGHTS_WRITE | PROCESS_SUSPEND_RESUME | PROCESS_SET_INFORMATION | PROCESS_SET_QUOTA
70 | PROCESS_CREATE_PROCESS | PROCESS_DUP_HANDLE | PROCESS_VM_WRITE | PROCESS_VM_OPERATION
71 | PROCESS_CREATE_THREAD,
72 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE,
73 PROCESS_ALL_ACCESS
77 static void process_dump( struct object *obj, int verbose );
78 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
79 static unsigned int process_map_access( struct object *obj, unsigned int access );
80 static struct security_descriptor *process_get_sd( struct object *obj );
81 static void process_poll_event( struct fd *fd, int event );
82 static struct list *process_get_kernel_obj_list( struct object *obj );
83 static void process_destroy( struct object *obj );
84 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
86 static const struct object_ops process_ops =
88 sizeof(struct process), /* size */
89 &process_type, /* type */
90 process_dump, /* dump */
91 add_queue, /* add_queue */
92 remove_queue, /* remove_queue */
93 process_signaled, /* signaled */
94 no_satisfied, /* satisfied */
95 no_signal, /* signal */
96 no_get_fd, /* get_fd */
97 process_map_access, /* map_access */
98 process_get_sd, /* get_sd */
99 default_set_sd, /* set_sd */
100 no_get_full_name, /* get_full_name */
101 no_lookup_name, /* lookup_name */
102 no_link_name, /* link_name */
103 NULL, /* unlink_name */
104 no_open_file, /* open_file */
105 process_get_kernel_obj_list, /* get_kernel_obj_list */
106 no_close_handle, /* close_handle */
107 process_destroy /* destroy */
110 static const struct fd_ops process_fd_ops =
112 NULL, /* get_poll_events */
113 process_poll_event, /* poll_event */
114 NULL, /* flush */
115 NULL, /* get_fd_type */
116 NULL, /* ioctl */
117 NULL, /* queue_async */
118 NULL, /* reselect_async */
119 NULL /* cancel async */
122 /* process startup info */
124 struct startup_info
126 struct object obj; /* object header */
127 struct process *process; /* created process */
128 data_size_t info_size; /* size of startup info */
129 data_size_t data_size; /* size of whole startup data */
130 startup_info_t *data; /* data for startup info */
133 static void startup_info_dump( struct object *obj, int verbose );
134 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
135 static void startup_info_destroy( struct object *obj );
137 static const struct object_ops startup_info_ops =
139 sizeof(struct startup_info), /* size */
140 &no_type, /* type */
141 startup_info_dump, /* dump */
142 add_queue, /* add_queue */
143 remove_queue, /* remove_queue */
144 startup_info_signaled, /* signaled */
145 no_satisfied, /* satisfied */
146 no_signal, /* signal */
147 no_get_fd, /* get_fd */
148 default_map_access, /* map_access */
149 default_get_sd, /* get_sd */
150 default_set_sd, /* set_sd */
151 no_get_full_name, /* get_full_name */
152 no_lookup_name, /* lookup_name */
153 no_link_name, /* link_name */
154 NULL, /* unlink_name */
155 no_open_file, /* open_file */
156 no_kernel_obj_list, /* get_kernel_obj_list */
157 no_close_handle, /* close_handle */
158 startup_info_destroy /* destroy */
161 /* job object */
163 static const WCHAR job_name[] = {'J','o','b'};
165 struct type_descr job_type =
167 { job_name, sizeof(job_name) }, /* name */
168 JOB_OBJECT_ALL_ACCESS, /* valid_access */
169 { /* mapping */
170 STANDARD_RIGHTS_READ | JOB_OBJECT_QUERY,
171 STANDARD_RIGHTS_WRITE | JOB_OBJECT_TERMINATE | JOB_OBJECT_SET_ATTRIBUTES | JOB_OBJECT_ASSIGN_PROCESS,
172 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
173 JOB_OBJECT_ALL_ACCESS
177 static void job_dump( struct object *obj, int verbose );
178 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
179 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
180 static void job_destroy( struct object *obj );
182 struct job
184 struct object obj; /* object header */
185 struct list process_list; /* list of processes */
186 int num_processes; /* count of running processes */
187 int total_processes; /* count of processes which have been assigned */
188 unsigned int limit_flags; /* limit flags */
189 int terminating; /* job is terminating */
190 int signaled; /* job is signaled */
191 struct completion *completion_port; /* associated completion port */
192 apc_param_t completion_key; /* key to send with completion messages */
193 struct job *parent;
194 struct list parent_job_entry; /* list entry for parent job */
195 struct list child_job_list; /* list of child jobs */
198 static const struct object_ops job_ops =
200 sizeof(struct job), /* size */
201 &job_type, /* type */
202 job_dump, /* dump */
203 add_queue, /* add_queue */
204 remove_queue, /* remove_queue */
205 job_signaled, /* signaled */
206 no_satisfied, /* satisfied */
207 no_signal, /* signal */
208 no_get_fd, /* get_fd */
209 default_map_access, /* map_access */
210 default_get_sd, /* get_sd */
211 default_set_sd, /* set_sd */
212 default_get_full_name, /* get_full_name */
213 no_lookup_name, /* lookup_name */
214 directory_link_name, /* link_name */
215 default_unlink_name, /* unlink_name */
216 no_open_file, /* open_file */
217 no_kernel_obj_list, /* get_kernel_obj_list */
218 job_close_handle, /* close_handle */
219 job_destroy /* destroy */
222 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
223 unsigned int attr, const struct security_descriptor *sd )
225 struct job *job;
227 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
229 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
231 /* initialize it if it didn't already exist */
232 list_init( &job->process_list );
233 list_init( &job->child_job_list );
234 job->num_processes = 0;
235 job->total_processes = 0;
236 job->limit_flags = 0;
237 job->terminating = 0;
238 job->signaled = 0;
239 job->completion_port = NULL;
240 job->completion_key = 0;
241 job->parent = NULL;
244 return job;
247 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
249 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
252 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
254 if (job->completion_port)
255 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
258 static void add_job_completion_existing_processes( struct job *job, struct job *completion_job )
260 struct process *process;
261 struct job *j;
263 assert( completion_job->obj.ops == &job_ops );
265 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
267 add_job_completion_existing_processes( j, completion_job );
270 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
272 if (process->running_threads)
273 add_job_completion( completion_job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id( process ));
277 static int process_in_job( struct job *job, struct process *process )
279 struct job *j;
281 LIST_FOR_EACH_ENTRY( j, &job->child_job_list, struct job, parent_job_entry )
283 assert( j->parent == job );
284 if (process_in_job( j, process )) return 1;
286 return process->job == job;
289 static void add_job_process( struct job *job, struct process *process )
291 struct job *j, *common_parent;
292 process_id_t pid;
294 if (job == process->job) return;
296 if ((common_parent = process->job))
298 if (job->parent)
300 for (j = job->parent; j; j = j->parent)
301 if (j == common_parent) break;
303 if (j != common_parent)
305 /* Job already has parent and the process is not in the job's chain. */
306 set_error( STATUS_ACCESS_DENIED );
307 return;
309 /* process->job is referenced in the job->parent chain. */
310 release_object( process->job );
312 else
314 /* transfer reference. */
315 job->parent = process->job;
316 list_add_tail( &job->parent->child_job_list, &job->parent_job_entry );
318 list_remove( &process->job_entry );
320 process->job = (struct job *)grab_object( job );
321 list_add_tail( &job->process_list, &process->job_entry );
323 pid = get_process_id( process );
324 for (j = job; j != common_parent; j = j->parent)
326 j->num_processes++;
327 j->total_processes++;
328 add_job_completion( j, JOB_OBJECT_MSG_NEW_PROCESS, pid );
332 /* called when a process has terminated, allow one additional process */
333 static void release_job_process( struct process *process )
335 struct job *job = process->job;
337 while (job)
339 assert( job->num_processes );
340 job->num_processes--;
342 if (!job->terminating)
343 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
345 if (!job->num_processes)
346 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
348 job = job->parent;
352 static void terminate_job( struct job *job, int exit_code )
354 struct process *process, *next_process;
355 struct job *j, *next_job;
357 LIST_FOR_EACH_ENTRY_SAFE( j, next_job, &job->child_job_list, struct job, parent_job_entry )
359 assert( j->parent == job );
361 grab_object( j );
362 terminate_job( j, exit_code );
363 release_object( j );
366 job->terminating = 1;
367 LIST_FOR_EACH_ENTRY_SAFE( process, next_process, &job->process_list, struct process, job_entry )
369 assert( process->job == job );
370 if (process->running_threads) terminate_process( process, NULL, exit_code );
372 job->terminating = 0;
373 job->signaled = 1;
374 wake_up( &job->obj, 0 );
377 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
379 struct job *job = (struct job *)obj;
380 assert( obj->ops == &job_ops );
382 if (obj->handle_count == 1) /* last handle */
384 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
385 terminate_job( job, 0 );
387 return 1;
390 static void job_destroy( struct object *obj )
392 struct job *job = (struct job *)obj;
393 assert( obj->ops == &job_ops );
395 assert( !job->num_processes );
396 assert( list_empty( &job->process_list ));
397 assert( list_empty( &job->child_job_list ));
399 if (job->completion_port) release_object( job->completion_port );
400 if (job->parent)
402 list_remove( &job->parent_job_entry );
403 release_object( job->parent );
407 static void job_dump( struct object *obj, int verbose )
409 struct job *job = (struct job *)obj;
410 assert( obj->ops == &job_ops );
411 fprintf( stderr, "Job processes=%d child_jobs=%d parent=%p\n",
412 list_count(&job->process_list), list_count(&job->child_job_list), job->parent );
415 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
417 struct job *job = (struct job *)obj;
418 return job->signaled;
421 struct ptid_entry
423 void *ptr; /* entry ptr */
424 unsigned int next; /* next free entry */
427 static struct ptid_entry *ptid_entries; /* array of ptid entries */
428 static unsigned int used_ptid_entries; /* number of entries in use */
429 static unsigned int alloc_ptid_entries; /* number of allocated entries */
430 static unsigned int next_free_ptid; /* next free entry */
431 static unsigned int last_free_ptid; /* last free entry */
432 static unsigned int num_free_ptids; /* number of free ptids */
434 static void kill_all_processes(void);
436 #define PTID_OFFSET 8 /* offset for first ptid value */
438 static unsigned int index_from_ptid(unsigned int id) { return id / 4; }
439 static unsigned int ptid_from_index(unsigned int index) { return index * 4; }
441 /* allocate a new process or thread id */
442 unsigned int alloc_ptid( void *ptr )
444 struct ptid_entry *entry;
445 unsigned int index;
447 if (used_ptid_entries < alloc_ptid_entries)
449 index = used_ptid_entries + PTID_OFFSET;
450 entry = &ptid_entries[used_ptid_entries++];
452 else if (next_free_ptid && num_free_ptids >= 256)
454 index = next_free_ptid;
455 entry = &ptid_entries[index - PTID_OFFSET];
456 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
457 num_free_ptids--;
459 else /* need to grow the array */
461 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
462 if (!count) count = 512;
463 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
465 set_error( STATUS_NO_MEMORY );
466 return 0;
468 ptid_entries = entry;
469 alloc_ptid_entries = count;
470 index = used_ptid_entries + PTID_OFFSET;
471 entry = &ptid_entries[used_ptid_entries++];
474 entry->ptr = ptr;
475 return ptid_from_index( index );
478 /* free a process or thread id */
479 void free_ptid( unsigned int id )
481 unsigned int index = index_from_ptid( id );
482 struct ptid_entry *entry = &ptid_entries[index - PTID_OFFSET];
484 entry->ptr = NULL;
485 entry->next = 0;
487 /* append to end of free list so that we don't reuse it too early */
488 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = index;
489 else next_free_ptid = index;
490 last_free_ptid = index;
491 num_free_ptids++;
494 /* retrieve the pointer corresponding to a process or thread id */
495 void *get_ptid_entry( unsigned int id )
497 unsigned int index = index_from_ptid( id );
498 if (index < PTID_OFFSET) return NULL;
499 if (index - PTID_OFFSET >= used_ptid_entries) return NULL;
500 return ptid_entries[index - PTID_OFFSET].ptr;
503 /* return the main thread of the process */
504 struct thread *get_process_first_thread( struct process *process )
506 struct list *ptr = list_head( &process->thread_list );
507 if (!ptr) return NULL;
508 return LIST_ENTRY( ptr, struct thread, proc_entry );
511 /* set the state of the process startup info */
512 static void set_process_startup_state( struct process *process, enum startup_state state )
514 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
515 if (process->startup_info)
517 wake_up( &process->startup_info->obj, 0 );
518 release_object( process->startup_info );
519 process->startup_info = NULL;
523 /* callback for server shutdown */
524 static void server_shutdown_timeout( void *arg )
526 shutdown_timeout = NULL;
527 if (!running_processes)
529 close_master_socket( 0 );
530 return;
532 switch(++shutdown_stage)
534 case 1: /* signal system processes to exit */
535 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
536 if (shutdown_event) set_event( shutdown_event );
537 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
538 close_master_socket( 4 * -TICKS_PER_SEC );
539 break;
540 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
541 kill_all_processes();
542 break;
546 /* forced shutdown, used for wineserver -k */
547 void shutdown_master_socket(void)
549 kill_all_processes();
550 shutdown_stage = 2;
551 if (shutdown_timeout)
553 remove_timeout_user( shutdown_timeout );
554 shutdown_timeout = NULL;
556 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
559 /* final cleanup once we are sure a process is really dead */
560 static void process_died( struct process *process )
562 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
563 if (!process->is_system)
565 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
566 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
568 release_object( process );
569 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
572 /* callback for process sigkill timeout */
573 static void process_sigkill( void *private )
575 struct process *process = private;
577 process->sigkill_timeout = NULL;
578 kill( process->unix_pid, SIGKILL );
579 process_died( process );
582 /* start the sigkill timer for a process upon exit */
583 static void start_sigkill_timer( struct process *process )
585 grab_object( process );
586 if (process->unix_pid != -1)
587 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
588 else
589 process_died( process );
592 /* create a new process */
593 /* if the function fails the fd is closed */
594 struct process *create_process( int fd, struct process *parent, unsigned int flags, const startup_info_t *info,
595 const struct security_descriptor *sd, const obj_handle_t *handles,
596 unsigned int handle_count, struct token *token )
598 struct process *process;
600 if (!(process = alloc_object( &process_ops )))
602 close( fd );
603 goto error;
605 process->parent_id = 0;
606 process->debug_obj = NULL;
607 process->debug_event = NULL;
608 process->handles = NULL;
609 process->msg_fd = NULL;
610 process->sigkill_timeout = NULL;
611 process->unix_pid = -1;
612 process->exit_code = STILL_ACTIVE;
613 process->running_threads = 0;
614 process->priority = PROCESS_PRIOCLASS_NORMAL;
615 process->suspend = 0;
616 process->is_system = 0;
617 process->debug_children = 1;
618 process->is_terminating = 0;
619 process->imagelen = 0;
620 process->image = NULL;
621 process->job = NULL;
622 process->console = NULL;
623 process->startup_state = STARTUP_IN_PROGRESS;
624 process->startup_info = NULL;
625 process->idle_event = NULL;
626 process->peb = 0;
627 process->ldt_copy = 0;
628 process->dir_cache = NULL;
629 process->winstation = 0;
630 process->desktop = 0;
631 process->token = NULL;
632 process->trace_data = 0;
633 process->rawinput_mouse = NULL;
634 process->rawinput_kbd = NULL;
635 list_init( &process->kernel_object );
636 list_init( &process->thread_list );
637 list_init( &process->locks );
638 list_init( &process->asyncs );
639 list_init( &process->classes );
640 list_init( &process->views );
641 list_init( &process->rawinput_devices );
643 process->end_time = 0;
645 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
646 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
648 close( fd );
649 goto error;
651 if (!(process->id = process->group_id = alloc_ptid( process )))
653 close( fd );
654 goto error;
656 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
658 /* create the handle table */
659 if (!parent)
661 process->handles = alloc_handle_table( process, 0 );
662 process->token = token_create_admin( TokenElevationTypeFull );
663 process->affinity = ~0;
665 else
667 obj_handle_t std_handles[3];
669 std_handles[0] = info->hstdin;
670 std_handles[1] = info->hstdout;
671 std_handles[2] = info->hstderr;
673 process->parent_id = parent->id;
674 if (flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES)
675 process->handles = copy_handle_table( process, parent, handles, handle_count, std_handles );
676 else
677 process->handles = alloc_handle_table( process, 0 );
678 /* Note: for security reasons, starting a new process does not attempt
679 * to use the current impersonation token for the new process */
680 process->token = token_duplicate( token ? token : parent->token, TRUE, 0, NULL, NULL, 0, NULL, 0 );
681 process->affinity = parent->affinity;
683 if (!process->handles || !process->token) goto error;
685 /* Assign a high security label to the token. The default would be medium
686 * but Wine provides admin access to all applications right now so high
687 * makes more sense for the time being. */
688 if (!token_assign_label( process->token, security_high_label_sid ))
689 goto error;
691 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
692 return process;
694 error:
695 if (process) release_object( process );
696 /* if we failed to start our first process, close everything down */
697 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
698 return NULL;
701 /* get the process data size */
702 data_size_t get_process_startup_info_size( struct process *process )
704 struct startup_info *info = process->startup_info;
706 if (!info) return 0;
707 return info->data_size;
710 /* destroy a process when its refcount is 0 */
711 static void process_destroy( struct object *obj )
713 struct process *process = (struct process *)obj;
714 assert( obj->ops == &process_ops );
716 /* we can't have a thread remaining */
717 assert( list_empty( &process->thread_list ));
718 assert( list_empty( &process->asyncs ));
720 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
722 close_process_handles( process );
723 set_process_startup_state( process, STARTUP_ABORTED );
725 if (process->job)
727 list_remove( &process->job_entry );
728 release_object( process->job );
730 if (process->console) release_object( process->console );
731 if (process->msg_fd) release_object( process->msg_fd );
732 if (process->idle_event) release_object( process->idle_event );
733 if (process->id) free_ptid( process->id );
734 if (process->token) release_object( process->token );
735 free( process->dir_cache );
736 free( process->image );
739 /* dump a process on stdout for debugging purposes */
740 static void process_dump( struct object *obj, int verbose )
742 struct process *process = (struct process *)obj;
743 assert( obj->ops == &process_ops );
745 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
748 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
750 struct process *process = (struct process *)obj;
751 return !process->running_threads;
754 static unsigned int process_map_access( struct object *obj, unsigned int access )
756 access = default_map_access( obj, access );
757 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
758 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
759 return access;
762 static struct list *process_get_kernel_obj_list( struct object *obj )
764 struct process *process = (struct process *)obj;
765 return &process->kernel_object;
768 static struct security_descriptor *process_get_sd( struct object *obj )
770 static struct security_descriptor *process_default_sd;
772 if (obj->sd) return obj->sd;
774 if (!process_default_sd)
776 size_t users_sid_len = security_sid_len( security_domain_users_sid );
777 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
778 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
779 + users_sid_len + admins_sid_len;
780 ACCESS_ALLOWED_ACE *aaa;
781 ACL *dacl;
783 process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
784 + dacl_len );
785 process_default_sd->control = SE_DACL_PRESENT;
786 process_default_sd->owner_len = admins_sid_len;
787 process_default_sd->group_len = users_sid_len;
788 process_default_sd->sacl_len = 0;
789 process_default_sd->dacl_len = dacl_len;
790 memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
791 memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
793 dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
794 dacl->AclRevision = ACL_REVISION;
795 dacl->Sbz1 = 0;
796 dacl->AclSize = dacl_len;
797 dacl->AceCount = 2;
798 dacl->Sbz2 = 0;
799 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
800 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
801 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
802 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
803 aaa->Mask = GENERIC_READ;
804 memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
805 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
806 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
807 aaa->Header.AceFlags = 0;
808 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
809 aaa->Mask = PROCESS_ALL_ACCESS;
810 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
812 return process_default_sd;
815 static void process_poll_event( struct fd *fd, int event )
817 struct process *process = get_fd_user( fd );
818 assert( process->obj.ops == &process_ops );
820 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
821 else if (event & POLLIN) receive_fd( process );
824 static void startup_info_destroy( struct object *obj )
826 struct startup_info *info = (struct startup_info *)obj;
827 assert( obj->ops == &startup_info_ops );
828 free( info->data );
829 if (info->process) release_object( info->process );
832 static void startup_info_dump( struct object *obj, int verbose )
834 struct startup_info *info = (struct startup_info *)obj;
835 assert( obj->ops == &startup_info_ops );
837 fputs( "Startup info", stderr );
838 if (info->data)
839 fprintf( stderr, " in=%04x out=%04x err=%04x",
840 info->data->hstdin, info->data->hstdout, info->data->hstderr );
841 fputc( '\n', stderr );
844 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
846 struct startup_info *info = (struct startup_info *)obj;
847 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
850 /* get a process from an id (and increment the refcount) */
851 struct process *get_process_from_id( process_id_t id )
853 struct object *obj = get_ptid_entry( id );
855 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
856 set_error( STATUS_INVALID_CID );
857 return NULL;
860 /* get a process from a handle (and increment the refcount) */
861 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
863 return (struct process *)get_handle_obj( current->process, handle,
864 access, &process_ops );
867 /* terminate a process with the given exit code */
868 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
870 struct thread *thread;
872 grab_object( process ); /* make sure it doesn't get freed when threads die */
873 process->is_terminating = 1;
875 restart:
876 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
878 if (exit_code) thread->exit_code = exit_code;
879 if (thread == skip) continue;
880 if (thread->state == TERMINATED) continue;
881 kill_thread( thread, 1 );
882 goto restart;
884 release_object( process );
887 /* kill all processes */
888 static void kill_all_processes(void)
890 struct list *ptr;
892 while ((ptr = list_head( &process_list )))
894 struct process *process = LIST_ENTRY( ptr, struct process, entry );
895 terminate_process( process, NULL, 1 );
899 /* kill all processes being attached to a console renderer */
900 void kill_console_processes( struct thread *renderer, int exit_code )
902 for (;;) /* restart from the beginning of the list every time */
904 struct process *process;
906 /* find the first process being attached to 'renderer' and still running */
907 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
909 if (process == renderer->process) continue;
910 if (process->console && console_get_renderer( process->console ) == renderer) break;
912 if (&process->entry == &process_list) break; /* no process found */
913 terminate_process( process, NULL, exit_code );
917 /* a process has been killed (i.e. its last thread died) */
918 static void process_killed( struct process *process )
920 struct list *ptr;
922 assert( list_empty( &process->thread_list ));
923 process->end_time = current_time;
924 close_process_desktop( process );
925 process->winstation = 0;
926 process->desktop = 0;
927 cancel_process_asyncs( process );
928 close_process_handles( process );
929 if (process->idle_event) release_object( process->idle_event );
930 process->idle_event = NULL;
931 assert( !process->console );
933 while ((ptr = list_head( &process->rawinput_devices )))
935 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
936 list_remove( &entry->entry );
937 free( entry );
939 destroy_process_classes( process );
940 free_mapped_views( process );
941 free_process_user_handles( process );
942 remove_process_locks( process );
943 set_process_startup_state( process, STARTUP_ABORTED );
944 finish_process_tracing( process );
945 release_job_process( process );
946 start_sigkill_timer( process );
947 wake_up( &process->obj, 0 );
950 /* add a thread to a process running threads list */
951 void add_process_thread( struct process *process, struct thread *thread )
953 list_add_tail( &process->thread_list, &thread->proc_entry );
954 if (!process->running_threads++)
956 list_add_tail( &process_list, &process->entry );
957 running_processes++;
958 if (!process->is_system)
960 if (!user_processes++ && shutdown_timeout)
962 remove_timeout_user( shutdown_timeout );
963 shutdown_timeout = NULL;
967 grab_object( thread );
970 /* remove a thread from a process running threads list */
971 void remove_process_thread( struct process *process, struct thread *thread )
973 assert( process->running_threads > 0 );
974 assert( !list_empty( &process->thread_list ));
976 list_remove( &thread->proc_entry );
978 if (!--process->running_threads)
980 /* we have removed the last running thread, exit the process */
981 process->exit_code = thread->exit_code;
982 generate_debug_event( thread, DbgExitProcessStateChange, process );
983 list_remove( &process->entry );
984 process_killed( process );
986 else generate_debug_event( thread, DbgExitThreadStateChange, thread );
987 release_object( thread );
990 /* suspend all the threads of a process */
991 void suspend_process( struct process *process )
993 if (!process->suspend++)
995 struct list *ptr, *next;
997 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
999 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1000 if (!thread->suspend) stop_thread( thread );
1005 /* resume all the threads of a process */
1006 void resume_process( struct process *process )
1008 assert (process->suspend > 0);
1009 if (!--process->suspend)
1011 struct list *ptr, *next;
1013 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1015 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1016 if (!thread->suspend) wake_thread( thread );
1021 /* kill a process on the spot */
1022 void kill_process( struct process *process, int violent_death )
1024 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
1026 release_object( process->msg_fd );
1027 process->msg_fd = NULL;
1030 if (process->sigkill_timeout) return; /* already waiting for it to die */
1032 if (violent_death) terminate_process( process, NULL, 1 );
1033 else
1035 struct list *ptr;
1037 grab_object( process ); /* make sure it doesn't get freed when threads die */
1038 while ((ptr = list_head( &process->thread_list )))
1040 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1041 kill_thread( thread, 0 );
1043 release_object( process );
1047 /* detach all processes being debugged by a given thread */
1048 void detach_debugged_processes( struct debug_obj *debug_obj, int exit_code )
1050 for (;;) /* restart from the beginning of the list every time */
1052 struct process *process;
1054 /* find the first process being debugged by 'debugger' and still running */
1055 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1056 if (process->debug_obj == debug_obj) break;
1058 if (&process->entry == &process_list) break; /* no process found */
1059 if (exit_code)
1061 process->debug_obj = NULL;
1062 terminate_process( process, NULL, exit_code );
1064 else debugger_detach( process, debug_obj );
1069 void enum_processes( int (*cb)(struct process*, void*), void *user )
1071 struct list *ptr, *next;
1073 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1075 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1076 if ((cb)(process, user)) break;
1080 /* set the debugged flag in the process PEB */
1081 int set_process_debug_flag( struct process *process, int flag )
1083 char data = (flag != 0);
1084 client_ptr_t peb32 = 0;
1086 if (!is_machine_64bit( process->machine ) && is_machine_64bit( supported_machines[0] ))
1087 peb32 = process->peb + 0x1000;
1089 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1090 if (peb32 && !write_process_memory( process, peb32 + 2, 1, &data )) return 0;
1091 return write_process_memory( process, process->peb + 2, 1, &data );
1094 /* create a new process */
1095 DECL_HANDLER(new_process)
1097 struct startup_info *info;
1098 const void *info_ptr;
1099 struct unicode_str name;
1100 const struct security_descriptor *sd;
1101 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1102 struct process *process = NULL;
1103 struct token *token = NULL;
1104 struct debug_obj *debug_obj = NULL;
1105 struct process *parent;
1106 struct thread *parent_thread = current;
1107 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1108 const obj_handle_t *handles = NULL;
1109 const obj_handle_t *job_handles = NULL;
1110 unsigned int i, job_handle_count;
1111 struct job *job;
1113 if (socket_fd == -1)
1115 set_error( STATUS_INVALID_PARAMETER );
1116 return;
1118 if (!objattr)
1120 set_error( STATUS_INVALID_PARAMETER );
1121 close( socket_fd );
1122 return;
1124 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1126 set_error( STATUS_INVALID_HANDLE );
1127 close( socket_fd );
1128 return;
1130 if (shutdown_stage)
1132 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1133 close( socket_fd );
1134 return;
1137 if (req->parent_process)
1139 if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
1141 close( socket_fd );
1142 return;
1144 parent_thread = NULL;
1146 else parent = (struct process *)grab_object( current->process );
1148 /* If a job further in the job chain does not permit breakaway process creation
1149 * succeeds and the process which is trying to breakaway is assigned to that job. */
1150 if (parent->job && (req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY) &&
1151 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1153 set_error( STATUS_ACCESS_DENIED );
1154 close( socket_fd );
1155 release_object( parent );
1156 return;
1159 /* build the startup info for a new process */
1160 if (!(info = alloc_object( &startup_info_ops )))
1162 close( socket_fd );
1163 release_object( parent );
1164 return;
1166 info->process = NULL;
1167 info->data = NULL;
1169 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1171 if ((req->handles_size & 3) || req->handles_size > info->data_size)
1173 set_error( STATUS_INVALID_PARAMETER );
1174 close( socket_fd );
1175 goto done;
1177 if (req->handles_size)
1179 handles = info_ptr;
1180 info_ptr = (const char *)info_ptr + req->handles_size;
1181 info->data_size -= req->handles_size;
1184 if ((req->jobs_size & 3) || req->jobs_size > info->data_size)
1186 set_error( STATUS_INVALID_PARAMETER );
1187 close( socket_fd );
1188 goto done;
1190 if (req->jobs_size)
1192 job_handles = info_ptr;
1193 info_ptr = (const char *)info_ptr + req->jobs_size;
1194 info->data_size -= req->jobs_size;
1197 job_handle_count = req->jobs_size / sizeof(*handles);
1198 for (i = 0; i < job_handle_count; ++i)
1200 if (!(job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS )))
1202 close( socket_fd );
1203 goto done;
1205 release_object( job );
1208 info->info_size = min( req->info_size, info->data_size );
1210 if (req->info_size < sizeof(*info->data))
1212 /* make sure we have a full startup_info_t structure */
1213 data_size_t env_size = info->data_size - info->info_size;
1214 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1216 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1218 close( socket_fd );
1219 goto done;
1221 memcpy( info->data, info_ptr, info_size );
1222 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1223 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1224 info->info_size = sizeof(startup_info_t);
1225 info->data_size = info->info_size + env_size;
1227 else
1229 data_size_t pos = sizeof(*info->data);
1231 if (!(info->data = memdup( info_ptr, info->data_size )))
1233 close( socket_fd );
1234 goto done;
1236 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1237 FIXUP_LEN( info->data->curdir_len );
1238 FIXUP_LEN( info->data->dllpath_len );
1239 FIXUP_LEN( info->data->imagepath_len );
1240 FIXUP_LEN( info->data->cmdline_len );
1241 FIXUP_LEN( info->data->title_len );
1242 FIXUP_LEN( info->data->desktop_len );
1243 FIXUP_LEN( info->data->shellinfo_len );
1244 FIXUP_LEN( info->data->runtime_len );
1245 #undef FIXUP_LEN
1248 if (req->token && !(token = get_token_obj( current->process, req->token, TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY )))
1250 close( socket_fd );
1251 goto done;
1253 if (req->debug && !(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
1255 close( socket_fd );
1256 goto done;
1259 if (!(process = create_process( socket_fd, parent, req->flags, info->data, sd,
1260 handles, req->handles_size / sizeof(*handles), token )))
1261 goto done;
1263 process->startup_info = (struct startup_info *)grab_object( info );
1265 job = parent->job;
1266 while (job)
1268 if (!(job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
1269 && !(req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY
1270 && job->limit_flags & JOB_OBJECT_LIMIT_BREAKAWAY_OK))
1272 add_job_process( job, process );
1273 assert( !get_error() );
1274 break;
1276 job = job->parent;
1279 for (i = 0; i < job_handle_count; ++i)
1281 job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS );
1282 add_job_process( job, process );
1283 release_object( job );
1284 if (get_error())
1286 release_job_process( process );
1287 goto done;
1291 /* connect to the window station */
1292 connect_process_winstation( process, parent_thread, parent );
1294 /* set the process console */
1295 if (info->data->console > 3)
1296 info->data->console = duplicate_handle( parent, info->data->console, process,
1297 0, 0, DUPLICATE_SAME_ACCESS );
1299 if (!(req->flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES) && info->data->console != 1)
1301 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1302 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1303 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1304 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1305 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1306 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1307 /* some handles above may have been invalid; this is not an error */
1308 if (get_error() == STATUS_INVALID_HANDLE ||
1309 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1312 /* attach to the debugger */
1313 if (debug_obj)
1315 process->debug_obj = debug_obj;
1316 process->debug_children = !(req->flags & PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT);
1318 else if (parent->debug_children)
1320 process->debug_obj = parent->debug_obj;
1321 /* debug_children is set to 1 by default */
1324 if (!info->data->console_flags) process->group_id = parent->group_id;
1326 info->process = (struct process *)grab_object( process );
1327 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1328 reply->pid = get_process_id( process );
1329 reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
1331 done:
1332 if (process) release_object( process );
1333 if (debug_obj) release_object( debug_obj );
1334 if (token) release_object( token );
1335 release_object( parent );
1336 release_object( info );
1339 /* Retrieve information about a newly started process */
1340 DECL_HANDLER(get_new_process_info)
1342 struct startup_info *info;
1344 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1345 0, &startup_info_ops )))
1347 reply->success = is_process_init_done( info->process );
1348 reply->exit_code = info->process->exit_code;
1349 release_object( info );
1353 /* Retrieve the new process startup info */
1354 DECL_HANDLER(get_startup_info)
1356 struct process *process = current->process;
1357 struct startup_info *info = process->startup_info;
1358 data_size_t size;
1360 if (!info) return;
1362 /* we return the data directly without making a copy so this can only be called once */
1363 reply->info_size = info->info_size;
1364 size = info->data_size;
1365 if (size > get_reply_max_size()) size = get_reply_max_size();
1366 set_reply_data_ptr( info->data, size );
1367 info->data = NULL;
1368 info->data_size = 0;
1371 /* signal the end of the process initialization */
1372 DECL_HANDLER(init_process_done)
1374 struct process *process = current->process;
1375 struct memory_view *view;
1376 client_ptr_t base;
1377 const pe_image_info_t *image_info;
1379 if (is_process_init_done(process))
1381 set_error( STATUS_INVALID_PARAMETER );
1382 return;
1384 if (!(view = get_exe_view( process )))
1386 set_error( STATUS_DLL_NOT_FOUND );
1387 return;
1389 if (!(image_info = get_view_image_info( view, &base ))) return;
1391 current->teb = req->teb;
1392 process->peb = req->peb;
1393 process->ldt_copy = req->ldt_copy;
1395 process->start_time = current_time;
1396 current->entry_point = image_info->entry_point;
1398 init_process_tracing( process );
1399 generate_startup_debug_events( process );
1400 set_process_startup_state( process, STARTUP_DONE );
1402 if (image_info->subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI)
1403 process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1404 if (process->debug_obj) set_process_debug_flag( process, 1 );
1405 reply->entry = image_info->entry_point;
1406 reply->suspend = (current->suspend || process->suspend);
1409 /* open a handle to a process */
1410 DECL_HANDLER(open_process)
1412 struct process *process = get_process_from_id( req->pid );
1413 reply->handle = 0;
1414 if (process)
1416 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1417 release_object( process );
1421 /* terminate a process */
1422 DECL_HANDLER(terminate_process)
1424 struct process *process;
1426 if (req->handle)
1428 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1429 if (!process) return;
1431 else process = (struct process *)grab_object( current->process );
1433 reply->self = (current->process == process);
1434 terminate_process( process, current, req->exit_code );
1435 release_object( process );
1438 /* fetch information about a process */
1439 DECL_HANDLER(get_process_info)
1441 struct process *process;
1443 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1445 reply->pid = get_process_id( process );
1446 reply->ppid = process->parent_id;
1447 reply->exit_code = process->exit_code;
1448 reply->priority = process->priority;
1449 reply->affinity = process->affinity;
1450 reply->peb = process->peb;
1451 reply->start_time = process->start_time;
1452 reply->end_time = process->end_time;
1453 reply->machine = process->machine;
1454 if (get_reply_max_size())
1456 client_ptr_t base;
1457 const pe_image_info_t *info;
1458 struct memory_view *view = get_exe_view( process );
1459 if (view)
1461 if ((info = get_view_image_info( view, &base )))
1462 set_reply_data( info, min( sizeof(*info), get_reply_max_size() ));
1464 else set_error( STATUS_PROCESS_IS_TERMINATING );
1466 release_object( process );
1470 /* retrieve debug information about a process */
1471 DECL_HANDLER(get_process_debug_info)
1473 struct process *process;
1475 if (!(process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION ))) return;
1477 reply->debug_children = process->debug_children;
1478 if (!process->debug_obj) set_error( STATUS_PORT_NOT_SET );
1479 else reply->debug = alloc_handle( current->process, process->debug_obj, DEBUG_ALL_ACCESS, 0 );
1480 release_object( process );
1483 /* fetch the name of the process image */
1484 DECL_HANDLER(get_process_image_name)
1486 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1488 if (!process) return;
1489 if (process->image)
1491 struct unicode_str name = { process->image, process->imagelen };
1492 /* skip the \??\ prefix */
1493 if (req->win32 && name.len > 6 * sizeof(WCHAR) && name.str[5] == ':')
1495 name.str += 4;
1496 name.len -= 4 * sizeof(WCHAR);
1498 /* FIXME: else resolve symlinks in NT path */
1500 reply->len = name.len;
1501 if (name.len <= get_reply_max_size())
1503 WCHAR *ptr = set_reply_data( name.str, name.len );
1504 /* change \??\ to \\?\ */
1505 if (req->win32 && name.len > sizeof(WCHAR) && ptr[1] == '?') ptr[1] = '\\';
1507 else set_error( STATUS_BUFFER_TOO_SMALL );
1509 release_object( process );
1512 /* retrieve information about a process memory usage */
1513 DECL_HANDLER(get_process_vm_counters)
1515 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1517 if (!process) return;
1518 #ifdef linux
1519 if (process->unix_pid != -1)
1521 FILE *f;
1522 char proc_path[32], line[256];
1523 unsigned long value;
1525 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1526 if ((f = fopen( proc_path, "r" )))
1528 while (fgets( line, sizeof(line), f ))
1530 if (sscanf( line, "VmPeak: %lu", &value ))
1531 reply->peak_virtual_size = (mem_size_t)value * 1024;
1532 else if (sscanf( line, "VmSize: %lu", &value ))
1533 reply->virtual_size = (mem_size_t)value * 1024;
1534 else if (sscanf( line, "VmHWM: %lu", &value ))
1535 reply->peak_working_set_size = (mem_size_t)value * 1024;
1536 else if (sscanf( line, "VmRSS: %lu", &value ))
1537 reply->working_set_size = (mem_size_t)value * 1024;
1538 else if (sscanf( line, "RssAnon: %lu", &value ))
1539 reply->pagefile_usage += (mem_size_t)value * 1024;
1540 else if (sscanf( line, "VmSwap: %lu", &value ))
1541 reply->pagefile_usage += (mem_size_t)value * 1024;
1543 reply->peak_pagefile_usage = reply->pagefile_usage;
1544 fclose( f );
1546 else set_error( STATUS_ACCESS_DENIED );
1548 else set_error( STATUS_ACCESS_DENIED );
1549 #endif
1550 release_object( process );
1553 static void set_process_affinity( struct process *process, affinity_t affinity )
1555 struct thread *thread;
1557 if (!process->running_threads)
1559 set_error( STATUS_PROCESS_IS_TERMINATING );
1560 return;
1563 process->affinity = affinity;
1565 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1567 set_thread_affinity( thread, affinity );
1571 /* set information about a process */
1572 DECL_HANDLER(set_process_info)
1574 struct process *process;
1576 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1578 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1579 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1580 release_object( process );
1584 /* read data from a process address space */
1585 DECL_HANDLER(read_process_memory)
1587 struct process *process;
1588 data_size_t len = get_reply_max_size();
1590 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1592 if (len)
1594 char *buffer = mem_alloc( len );
1595 if (buffer)
1597 if (read_process_memory( process, req->addr, len, buffer ))
1598 set_reply_data_ptr( buffer, len );
1599 else
1600 free( buffer );
1603 release_object( process );
1606 /* write data to a process address space */
1607 DECL_HANDLER(write_process_memory)
1609 struct process *process;
1611 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1613 data_size_t len = get_req_data_size();
1614 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1615 else set_error( STATUS_INVALID_PARAMETER );
1616 release_object( process );
1620 /* retrieve the process idle event */
1621 DECL_HANDLER(get_process_idle_event)
1623 struct process *process;
1625 reply->event = 0;
1626 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1628 if (process->idle_event && process != current->process)
1629 reply->event = alloc_handle( current->process, process->idle_event,
1630 EVENT_ALL_ACCESS, 0 );
1631 release_object( process );
1635 /* make the current process a system process */
1636 DECL_HANDLER(make_process_system)
1638 struct process *process;
1639 struct thread *thread;
1641 if (!shutdown_event)
1643 if (!(shutdown_event = create_event( NULL, NULL, OBJ_PERMANENT, 1, 0, NULL ))) return;
1644 release_object( shutdown_event );
1647 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION ))) return;
1649 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1651 release_object( process );
1652 return;
1655 if (!process->is_system)
1657 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1658 release_thread_desktop( thread, 0 );
1659 process->is_system = 1;
1660 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1661 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1663 release_object( process );
1666 /* create a new job object */
1667 DECL_HANDLER(create_job)
1669 struct job *job;
1670 struct unicode_str name;
1671 struct object *root;
1672 const struct security_descriptor *sd;
1673 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1675 if (!objattr) return;
1677 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1679 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1680 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1681 else
1682 reply->handle = alloc_handle_no_access_check( current->process, job,
1683 req->access, objattr->attributes );
1684 release_object( job );
1686 if (root) release_object( root );
1689 /* open a job object */
1690 DECL_HANDLER(open_job)
1692 struct unicode_str name = get_req_unicode_str();
1694 reply->handle = open_object( current->process, req->rootdir, req->access,
1695 &job_ops, &name, req->attributes );
1698 /* assign a job object to a process */
1699 DECL_HANDLER(assign_job)
1701 struct process *process;
1702 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1704 if (!job) return;
1706 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1708 if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
1709 else add_job_process( job, process );
1710 release_object( process );
1712 release_object( job );
1716 /* check if a process is associated with a job */
1717 DECL_HANDLER(process_in_job)
1719 struct process *process;
1720 struct job *job;
1722 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1723 return;
1725 if (!req->job)
1727 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1729 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1731 set_error( process_in_job( job, process ) ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1732 release_object( job );
1734 release_object( process );
1737 /* retrieve information about a job */
1738 DECL_HANDLER(get_job_info)
1740 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_QUERY );
1742 if (!job) return;
1744 reply->total_processes = job->total_processes;
1745 reply->active_processes = job->num_processes;
1746 release_object( job );
1749 /* terminate all processes associated with the job */
1750 DECL_HANDLER(terminate_job)
1752 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1754 if (!job) return;
1756 terminate_job( job, req->status );
1757 release_object( job );
1760 /* update limits of the job object */
1761 DECL_HANDLER(set_job_limits)
1763 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1765 if (!job) return;
1767 job->limit_flags = req->limit_flags;
1768 release_object( job );
1771 /* set the jobs completion port */
1772 DECL_HANDLER(set_job_completion_port)
1774 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1776 if (!job) return;
1778 if (!job->completion_port)
1780 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1781 job->completion_key = req->key;
1783 add_job_completion_existing_processes( job, job );
1785 else
1786 set_error( STATUS_INVALID_PARAMETER );
1788 release_object( job );
1791 /* Suspend a process */
1792 DECL_HANDLER(suspend_process)
1794 struct process *process;
1796 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1798 struct list *ptr, *next;
1800 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1802 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1803 suspend_thread( thread );
1806 release_object( process );
1810 /* Resume a process */
1811 DECL_HANDLER(resume_process)
1813 struct process *process;
1815 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1817 struct list *ptr, *next;
1819 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1821 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1822 resume_thread( thread );
1825 release_object( process );
1829 /* Get a list of processes and threads currently running */
1830 DECL_HANDLER(list_processes)
1832 struct process *process;
1833 struct thread *thread;
1834 unsigned int pos = 0;
1835 char *buffer;
1837 reply->process_count = 0;
1838 reply->info_size = 0;
1840 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1842 reply->info_size = (reply->info_size + 7) & ~7;
1843 reply->info_size += sizeof(struct process_info) + process->imagelen;
1844 reply->info_size = (reply->info_size + 7) & ~7;
1845 reply->info_size += process->running_threads * sizeof(struct thread_info);
1846 reply->process_count++;
1849 if (reply->info_size > get_reply_max_size())
1851 set_error( STATUS_INFO_LENGTH_MISMATCH );
1852 return;
1855 if (!(buffer = set_reply_data_size( reply->info_size ))) return;
1857 memset( buffer, 0, reply->info_size );
1858 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1860 struct process_info *process_info;
1862 pos = (pos + 7) & ~7;
1863 process_info = (struct process_info *)(buffer + pos);
1864 process_info->start_time = process->start_time;
1865 process_info->name_len = process->imagelen;
1866 process_info->thread_count = process->running_threads;
1867 process_info->priority = process->priority;
1868 process_info->pid = process->id;
1869 process_info->parent_pid = process->parent_id;
1870 process_info->handle_count = get_handle_table_count(process);
1871 process_info->unix_pid = process->unix_pid;
1872 pos += sizeof(*process_info);
1873 memcpy( buffer + pos, process->image, process->imagelen );
1874 pos += process->imagelen;
1875 pos = (pos + 7) & ~7;
1876 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1878 struct thread_info *thread_info = (struct thread_info *)(buffer + pos);
1880 thread_info->start_time = thread->creation_time;
1881 thread_info->tid = thread->id;
1882 thread_info->base_priority = thread->priority;
1883 thread_info->current_priority = thread->priority; /* FIXME */
1884 thread_info->unix_tid = thread->unix_tid;
1885 pos += sizeof(*thread_info);