winealsa: Use the symbolic name for MIDI_CTL_ALL_SOUNDS_OFF.
[wine.git] / server / process.c
blobd14072047185689e9fb26f429a8498db3f9bf6be
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, &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 struct ace *ace;
827 struct acl *dacl;
828 struct sid *sid;
829 size_t users_sid_len = sid_len( &domain_users_sid );
830 size_t admins_sid_len = sid_len( &builtin_admins_sid );
831 size_t dacl_len = sizeof(*dacl) + 2 * sizeof(*ace) + users_sid_len + admins_sid_len;
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 sid = (struct sid *)(process_default_sd + 1);
841 sid = copy_sid( sid, &builtin_admins_sid );
842 sid = copy_sid( sid, &domain_users_sid );
844 dacl = (struct acl *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
845 dacl->revision = ACL_REVISION;
846 dacl->pad1 = 0;
847 dacl->size = dacl_len;
848 dacl->count = 2;
849 dacl->pad2 = 0;
850 ace = set_ace( ace_first( dacl ), &domain_users_sid, ACCESS_ALLOWED_ACE_TYPE,
851 INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, GENERIC_READ );
852 set_ace( ace_next( ace ), &builtin_admins_sid, ACCESS_ALLOWED_ACE_TYPE, 0, PROCESS_ALL_ACCESS );
854 return process_default_sd;
857 static void process_poll_event( struct fd *fd, int event )
859 struct process *process = get_fd_user( fd );
860 assert( process->obj.ops == &process_ops );
862 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
863 else if (event & POLLIN) receive_fd( process );
866 static void startup_info_destroy( struct object *obj )
868 struct startup_info *info = (struct startup_info *)obj;
869 assert( obj->ops == &startup_info_ops );
870 free( info->data );
871 if (info->process) release_object( info->process );
874 static void startup_info_dump( struct object *obj, int verbose )
876 struct startup_info *info = (struct startup_info *)obj;
877 assert( obj->ops == &startup_info_ops );
879 fputs( "Startup info", stderr );
880 if (info->data)
881 fprintf( stderr, " in=%04x out=%04x err=%04x",
882 info->data->hstdin, info->data->hstdout, info->data->hstderr );
883 fputc( '\n', stderr );
886 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
888 struct startup_info *info = (struct startup_info *)obj;
889 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
892 /* get a process from an id (and increment the refcount) */
893 struct process *get_process_from_id( process_id_t id )
895 struct object *obj = get_ptid_entry( id );
897 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
898 set_error( STATUS_INVALID_CID );
899 return NULL;
902 /* get a process from a handle (and increment the refcount) */
903 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
905 return (struct process *)get_handle_obj( current->process, handle,
906 access, &process_ops );
909 /* terminate a process with the given exit code */
910 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
912 struct thread *thread;
914 grab_object( process ); /* make sure it doesn't get freed when threads die */
915 process->is_terminating = 1;
917 restart:
918 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
920 if (exit_code) thread->exit_code = exit_code;
921 if (thread == skip) continue;
922 if (thread->state == TERMINATED) continue;
923 kill_thread( thread, 1 );
924 goto restart;
926 release_object( process );
929 /* kill all processes */
930 static void kill_all_processes(void)
932 struct list *ptr;
934 while ((ptr = list_head( &process_list )))
936 struct process *process = LIST_ENTRY( ptr, struct process, entry );
937 terminate_process( process, NULL, 1 );
941 /* kill all processes being attached to a console renderer */
942 void kill_console_processes( struct thread *renderer, int exit_code )
944 for (;;) /* restart from the beginning of the list every time */
946 struct process *process;
948 /* find the first process being attached to 'renderer' and still running */
949 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
951 if (process == renderer->process) continue;
952 if (process->console && console_get_renderer( process->console ) == renderer) break;
954 if (&process->entry == &process_list) break; /* no process found */
955 terminate_process( process, NULL, exit_code );
959 /* a process has been killed (i.e. its last thread died) */
960 static void process_killed( struct process *process )
962 struct list *ptr;
964 assert( list_empty( &process->thread_list ));
965 process->end_time = current_time;
966 close_process_desktop( process );
967 process->winstation = 0;
968 process->desktop = 0;
969 cancel_process_asyncs( process );
970 close_process_handles( process );
971 if (process->idle_event) release_object( process->idle_event );
972 process->idle_event = NULL;
973 assert( !process->console );
975 while ((ptr = list_head( &process->rawinput_devices )))
977 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
978 list_remove( &entry->entry );
979 free( entry );
981 destroy_process_classes( process );
982 free_mapped_views( process );
983 free_process_user_handles( process );
984 remove_process_locks( process );
985 set_process_startup_state( process, STARTUP_ABORTED );
986 finish_process_tracing( process );
987 release_job_process( process );
988 start_sigkill_timer( process );
989 wake_up( &process->obj, 0 );
992 /* add a thread to a process running threads list */
993 void add_process_thread( struct process *process, struct thread *thread )
995 list_add_tail( &process->thread_list, &thread->proc_entry );
996 if (!process->running_threads++)
998 list_add_tail( &process_list, &process->entry );
999 running_processes++;
1000 if (!process->is_system)
1002 if (!user_processes++ && shutdown_timeout)
1004 remove_timeout_user( shutdown_timeout );
1005 shutdown_timeout = NULL;
1009 grab_object( thread );
1012 /* remove a thread from a process running threads list */
1013 void remove_process_thread( struct process *process, struct thread *thread )
1015 assert( process->running_threads > 0 );
1016 assert( !list_empty( &process->thread_list ));
1018 list_remove( &thread->proc_entry );
1020 if (!--process->running_threads)
1022 /* we have removed the last running thread, exit the process */
1023 process->exit_code = thread->exit_code;
1024 generate_debug_event( thread, DbgExitProcessStateChange, process );
1025 list_remove( &process->entry );
1026 process_killed( process );
1028 else generate_debug_event( thread, DbgExitThreadStateChange, thread );
1029 release_object( thread );
1032 /* suspend all the threads of a process */
1033 void suspend_process( struct process *process )
1035 if (!process->suspend++)
1037 struct list *ptr, *next;
1039 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1041 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1042 if (!thread->suspend) stop_thread( thread );
1047 /* resume all the threads of a process */
1048 void resume_process( struct process *process )
1050 assert (process->suspend > 0);
1051 if (!--process->suspend)
1053 struct list *ptr, *next;
1055 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1057 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1058 if (!thread->suspend) wake_thread( thread );
1063 /* kill a process on the spot */
1064 void kill_process( struct process *process, int violent_death )
1066 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
1068 release_object( process->msg_fd );
1069 process->msg_fd = NULL;
1072 if (process->sigkill_timeout) return; /* already waiting for it to die */
1074 if (violent_death) terminate_process( process, NULL, 1 );
1075 else
1077 struct list *ptr;
1079 grab_object( process ); /* make sure it doesn't get freed when threads die */
1080 while ((ptr = list_head( &process->thread_list )))
1082 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1083 kill_thread( thread, 0 );
1085 release_object( process );
1089 /* detach all processes being debugged by a given thread */
1090 void detach_debugged_processes( struct debug_obj *debug_obj, int exit_code )
1092 for (;;) /* restart from the beginning of the list every time */
1094 struct process *process;
1096 /* find the first process being debugged by 'debugger' and still running */
1097 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1098 if (process->debug_obj == debug_obj) break;
1100 if (&process->entry == &process_list) break; /* no process found */
1101 if (exit_code)
1103 process->debug_obj = NULL;
1104 terminate_process( process, NULL, exit_code );
1106 else debugger_detach( process, debug_obj );
1111 void enum_processes( int (*cb)(struct process*, void*), void *user )
1113 struct list *ptr, *next;
1115 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1117 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1118 if ((cb)(process, user)) break;
1122 /* set the debugged flag in the process PEB */
1123 int set_process_debug_flag( struct process *process, int flag )
1125 char data = (flag != 0);
1126 client_ptr_t peb32 = 0;
1128 if (!is_machine_64bit( process->machine ) && is_machine_64bit( native_machine ))
1129 peb32 = process->peb + 0x1000;
1131 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1132 if (peb32 && !write_process_memory( process, peb32 + 2, 1, &data )) return 0;
1133 return write_process_memory( process, process->peb + 2, 1, &data );
1136 /* create a new process */
1137 DECL_HANDLER(new_process)
1139 struct startup_info *info;
1140 const void *info_ptr;
1141 struct unicode_str name;
1142 const struct security_descriptor *sd;
1143 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1144 struct process *process = NULL;
1145 struct token *token = NULL;
1146 struct debug_obj *debug_obj = NULL;
1147 struct process *parent;
1148 struct thread *parent_thread = current;
1149 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1150 const obj_handle_t *handles = NULL;
1151 const obj_handle_t *job_handles = NULL;
1152 unsigned int i, job_handle_count;
1153 struct job *job;
1155 if (socket_fd == -1)
1157 set_error( STATUS_INVALID_PARAMETER );
1158 return;
1160 if (!objattr)
1162 set_error( STATUS_INVALID_PARAMETER );
1163 close( socket_fd );
1164 return;
1166 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1168 set_error( STATUS_INVALID_HANDLE );
1169 close( socket_fd );
1170 return;
1172 if (shutdown_stage)
1174 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1175 close( socket_fd );
1176 return;
1179 if (req->parent_process)
1181 if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
1183 close( socket_fd );
1184 return;
1186 parent_thread = NULL;
1188 else parent = (struct process *)grab_object( current->process );
1190 /* If a job further in the job chain does not permit breakaway process creation
1191 * succeeds and the process which is trying to breakaway is assigned to that job. */
1192 if (parent->job && (req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY) &&
1193 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1195 set_error( STATUS_ACCESS_DENIED );
1196 close( socket_fd );
1197 release_object( parent );
1198 return;
1201 /* build the startup info for a new process */
1202 if (!(info = alloc_object( &startup_info_ops )))
1204 close( socket_fd );
1205 release_object( parent );
1206 return;
1208 info->process = NULL;
1209 info->data = NULL;
1211 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1213 if ((req->handles_size & 3) || req->handles_size > info->data_size)
1215 set_error( STATUS_INVALID_PARAMETER );
1216 close( socket_fd );
1217 goto done;
1219 if (req->handles_size)
1221 handles = info_ptr;
1222 info_ptr = (const char *)info_ptr + req->handles_size;
1223 info->data_size -= req->handles_size;
1226 if ((req->jobs_size & 3) || req->jobs_size > info->data_size)
1228 set_error( STATUS_INVALID_PARAMETER );
1229 close( socket_fd );
1230 goto done;
1232 if (req->jobs_size)
1234 job_handles = info_ptr;
1235 info_ptr = (const char *)info_ptr + req->jobs_size;
1236 info->data_size -= req->jobs_size;
1239 job_handle_count = req->jobs_size / sizeof(*handles);
1240 for (i = 0; i < job_handle_count; ++i)
1242 if (!(job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS )))
1244 close( socket_fd );
1245 goto done;
1247 release_object( job );
1250 info->info_size = min( req->info_size, info->data_size );
1252 if (req->info_size < sizeof(*info->data))
1254 /* make sure we have a full startup_info_t structure */
1255 data_size_t env_size = info->data_size - info->info_size;
1256 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1258 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1260 close( socket_fd );
1261 goto done;
1263 memcpy( info->data, info_ptr, info_size );
1264 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1265 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1266 info->info_size = sizeof(startup_info_t);
1267 info->data_size = info->info_size + env_size;
1269 else
1271 data_size_t pos = sizeof(*info->data);
1273 if (!(info->data = memdup( info_ptr, info->data_size )))
1275 close( socket_fd );
1276 goto done;
1278 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1279 FIXUP_LEN( info->data->curdir_len );
1280 FIXUP_LEN( info->data->dllpath_len );
1281 FIXUP_LEN( info->data->imagepath_len );
1282 FIXUP_LEN( info->data->cmdline_len );
1283 FIXUP_LEN( info->data->title_len );
1284 FIXUP_LEN( info->data->desktop_len );
1285 FIXUP_LEN( info->data->shellinfo_len );
1286 FIXUP_LEN( info->data->runtime_len );
1287 #undef FIXUP_LEN
1290 if (req->token && !(token = get_token_obj( current->process, req->token, TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY )))
1292 close( socket_fd );
1293 goto done;
1295 if (req->debug && !(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
1297 close( socket_fd );
1298 goto done;
1301 if (!(process = create_process( socket_fd, parent, req->flags, info->data, sd,
1302 handles, req->handles_size / sizeof(*handles), token )))
1303 goto done;
1305 process->startup_info = (struct startup_info *)grab_object( info );
1307 job = parent->job;
1308 while (job)
1310 if (!(job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
1311 && !(req->flags & PROCESS_CREATE_FLAGS_BREAKAWAY
1312 && job->limit_flags & JOB_OBJECT_LIMIT_BREAKAWAY_OK))
1314 add_job_process( job, process );
1315 assert( !get_error() );
1316 break;
1318 job = job->parent;
1321 for (i = 0; i < job_handle_count; ++i)
1323 job = get_job_obj( current->process, job_handles[i], JOB_OBJECT_ASSIGN_PROCESS );
1324 add_job_process( job, process );
1325 release_object( job );
1326 if (get_error())
1328 release_job_process( process );
1329 goto done;
1333 /* connect to the window station */
1334 connect_process_winstation( process, parent_thread, parent );
1336 /* inherit the process console, but keep pseudo handles (< 0), and 0 (= not attached to a console) as is */
1337 if ((int)info->data->console > 0)
1338 info->data->console = duplicate_handle( parent, info->data->console, process,
1339 0, 0, DUPLICATE_SAME_ACCESS );
1341 if (!(req->flags & PROCESS_CREATE_FLAGS_INHERIT_HANDLES) && info->data->console != 1)
1343 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1344 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1345 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1346 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1347 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1348 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1349 /* some handles above may have been invalid; this is not an error */
1350 if (get_error() == STATUS_INVALID_HANDLE ||
1351 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1354 /* attach to the debugger */
1355 if (debug_obj)
1357 process->debug_obj = debug_obj;
1358 process->debug_children = !(req->flags & PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT);
1360 else if (parent->debug_children)
1362 process->debug_obj = parent->debug_obj;
1363 /* debug_children is set to 1 by default */
1366 if (!info->data->console_flags) process->group_id = parent->group_id;
1368 info->process = (struct process *)grab_object( process );
1369 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1370 reply->pid = get_process_id( process );
1371 reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
1373 done:
1374 if (process) release_object( process );
1375 if (debug_obj) release_object( debug_obj );
1376 if (token) release_object( token );
1377 release_object( parent );
1378 release_object( info );
1381 /* Retrieve information about a newly started process */
1382 DECL_HANDLER(get_new_process_info)
1384 struct startup_info *info;
1386 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1387 0, &startup_info_ops )))
1389 reply->success = is_process_init_done( info->process );
1390 reply->exit_code = info->process->exit_code;
1391 release_object( info );
1395 /* Retrieve the new process startup info */
1396 DECL_HANDLER(get_startup_info)
1398 struct process *process = current->process;
1399 struct startup_info *info = process->startup_info;
1400 data_size_t size;
1402 if (!info) return;
1404 /* we return the data directly without making a copy so this can only be called once */
1405 reply->info_size = info->info_size;
1406 size = info->data_size;
1407 if (size > get_reply_max_size()) size = get_reply_max_size();
1408 set_reply_data_ptr( info->data, size );
1409 info->data = NULL;
1410 info->data_size = 0;
1413 /* signal the end of the process initialization */
1414 DECL_HANDLER(init_process_done)
1416 struct process *process = current->process;
1417 struct memory_view *view;
1418 client_ptr_t base;
1419 const pe_image_info_t *image_info;
1421 if (is_process_init_done(process))
1423 set_error( STATUS_INVALID_PARAMETER );
1424 return;
1426 if (!(view = get_exe_view( process )))
1428 set_error( STATUS_DLL_NOT_FOUND );
1429 return;
1431 if (!(image_info = get_view_image_info( view, &base ))) return;
1433 current->teb = req->teb;
1434 process->peb = req->peb;
1435 process->ldt_copy = req->ldt_copy;
1437 process->start_time = current_time;
1438 current->entry_point = base + image_info->entry_point;
1440 init_process_tracing( process );
1441 generate_startup_debug_events( process );
1442 set_process_startup_state( process, STARTUP_DONE );
1444 if (image_info->subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI)
1445 process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1446 if (process->debug_obj) set_process_debug_flag( process, 1 );
1447 reply->entry = current->entry_point;
1448 reply->suspend = (current->suspend || process->suspend);
1451 /* open a handle to a process */
1452 DECL_HANDLER(open_process)
1454 struct process *process = get_process_from_id( req->pid );
1455 reply->handle = 0;
1456 if (process)
1458 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1459 release_object( process );
1463 /* terminate a process */
1464 DECL_HANDLER(terminate_process)
1466 struct process *process;
1468 if (req->handle)
1470 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1471 if (!process) return;
1473 else process = (struct process *)grab_object( current->process );
1475 reply->self = (current->process == process);
1476 terminate_process( process, current, req->exit_code );
1477 release_object( process );
1480 /* fetch information about a process */
1481 DECL_HANDLER(get_process_info)
1483 struct process *process;
1485 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1487 reply->pid = get_process_id( process );
1488 reply->ppid = process->parent_id;
1489 reply->exit_code = process->exit_code;
1490 reply->priority = process->priority;
1491 reply->affinity = process->affinity;
1492 reply->peb = process->peb;
1493 reply->start_time = process->start_time;
1494 reply->end_time = process->end_time;
1495 reply->session_id = process->session_id;
1496 reply->machine = process->machine;
1497 if (get_reply_max_size())
1499 if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
1500 else set_reply_data( &process->image_info, min( sizeof(process->image_info), get_reply_max_size() ));
1502 release_object( process );
1506 /* retrieve debug information about a process */
1507 DECL_HANDLER(get_process_debug_info)
1509 struct process *process;
1511 if (!(process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION ))) return;
1513 reply->debug_children = process->debug_children;
1514 if (!process->debug_obj) set_error( STATUS_PORT_NOT_SET );
1515 else reply->debug = alloc_handle( current->process, process->debug_obj, MAXIMUM_ALLOWED, 0 );
1516 release_object( process );
1519 /* fetch the name of the process image */
1520 DECL_HANDLER(get_process_image_name)
1522 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1524 if (!process) return;
1525 if (process->image)
1527 struct unicode_str name = { process->image, process->imagelen };
1528 /* skip the \??\ prefix */
1529 if (req->win32 && name.len > 6 * sizeof(WCHAR) && name.str[5] == ':')
1531 name.str += 4;
1532 name.len -= 4 * sizeof(WCHAR);
1534 /* FIXME: else resolve symlinks in NT path */
1536 reply->len = name.len;
1537 if (name.len <= get_reply_max_size())
1539 WCHAR *ptr = set_reply_data( name.str, name.len );
1540 /* change \??\ to \\?\ */
1541 if (req->win32 && name.len > sizeof(WCHAR) && ptr[1] == '?') ptr[1] = '\\';
1543 else set_error( STATUS_BUFFER_TOO_SMALL );
1545 release_object( process );
1548 /* retrieve information about a process memory usage */
1549 DECL_HANDLER(get_process_vm_counters)
1551 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1553 if (!process) return;
1554 if (process->unix_pid != -1)
1556 #ifdef linux
1557 FILE *f;
1558 char proc_path[32], line[256];
1559 unsigned long value;
1561 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1562 if ((f = fopen( proc_path, "r" )))
1564 while (fgets( line, sizeof(line), f ))
1566 if (sscanf( line, "VmPeak: %lu", &value ))
1567 reply->peak_virtual_size = (mem_size_t)value * 1024;
1568 else if (sscanf( line, "VmSize: %lu", &value ))
1569 reply->virtual_size = (mem_size_t)value * 1024;
1570 else if (sscanf( line, "VmHWM: %lu", &value ))
1571 reply->peak_working_set_size = (mem_size_t)value * 1024;
1572 else if (sscanf( line, "VmRSS: %lu", &value ))
1573 reply->working_set_size = (mem_size_t)value * 1024;
1574 else if (sscanf( line, "RssAnon: %lu", &value ))
1575 reply->pagefile_usage += (mem_size_t)value * 1024;
1576 else if (sscanf( line, "VmSwap: %lu", &value ))
1577 reply->pagefile_usage += (mem_size_t)value * 1024;
1579 reply->peak_pagefile_usage = reply->pagefile_usage;
1580 fclose( f );
1582 else set_error( STATUS_ACCESS_DENIED );
1583 #elif defined(HAVE_LIBPROCSTAT)
1584 struct procstat *procstat;
1585 unsigned int count;
1587 if ((procstat = procstat_open_sysctl()))
1589 struct kinfo_proc *kp = procstat_getprocs( procstat, KERN_PROC_PID, process->unix_pid, &count );
1590 if (kp)
1592 reply->virtual_size = kp->ki_size;
1593 reply->peak_virtual_size = reply->virtual_size;
1594 reply->working_set_size = kp->ki_rssize << PAGE_SHIFT;
1595 reply->peak_working_set_size = kp->ki_rusage.ru_maxrss * 1024;
1596 procstat_freeprocs( procstat, kp );
1598 else set_error( STATUS_ACCESS_DENIED );
1599 procstat_close( procstat );
1601 else set_error( STATUS_ACCESS_DENIED );
1602 #endif
1604 else set_error( STATUS_ACCESS_DENIED );
1605 release_object( process );
1608 static void set_process_affinity( struct process *process, affinity_t affinity )
1610 struct thread *thread;
1612 if (!process->running_threads)
1614 set_error( STATUS_PROCESS_IS_TERMINATING );
1615 return;
1618 process->affinity = affinity;
1620 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1622 set_thread_affinity( thread, affinity );
1626 /* set information about a process */
1627 DECL_HANDLER(set_process_info)
1629 struct process *process;
1631 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1633 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1634 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1635 release_object( process );
1639 /* read data from a process address space */
1640 DECL_HANDLER(read_process_memory)
1642 struct process *process;
1643 data_size_t len = get_reply_max_size();
1645 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1647 if (len)
1649 char *buffer = mem_alloc( len );
1650 if (buffer)
1652 if (read_process_memory( process, req->addr, len, buffer ))
1653 set_reply_data_ptr( buffer, len );
1654 else
1655 free( buffer );
1658 release_object( process );
1661 /* write data to a process address space */
1662 DECL_HANDLER(write_process_memory)
1664 struct process *process;
1666 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1668 data_size_t len = get_req_data_size();
1669 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1670 else set_error( STATUS_INVALID_PARAMETER );
1671 release_object( process );
1675 /* retrieve the process idle event */
1676 DECL_HANDLER(get_process_idle_event)
1678 struct process *process;
1680 reply->event = 0;
1681 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1683 if (process->idle_event && process != current->process)
1684 reply->event = alloc_handle( current->process, process->idle_event,
1685 EVENT_ALL_ACCESS, 0 );
1686 release_object( process );
1690 /* make the current process a system process */
1691 DECL_HANDLER(make_process_system)
1693 struct process *process;
1694 struct thread *thread;
1696 if (!shutdown_event)
1698 if (!(shutdown_event = create_event( NULL, NULL, OBJ_PERMANENT, 1, 0, NULL ))) return;
1699 release_object( shutdown_event );
1702 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION ))) return;
1704 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1706 release_object( process );
1707 return;
1710 if (!process->is_system)
1712 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1713 release_thread_desktop( thread, 0 );
1714 process->is_system = 1;
1715 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1716 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1718 release_object( process );
1721 /* create a new job object */
1722 DECL_HANDLER(create_job)
1724 struct job *job;
1725 struct unicode_str name;
1726 struct object *root;
1727 const struct security_descriptor *sd;
1728 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1730 if (!objattr) return;
1732 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1734 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1735 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1736 else
1737 reply->handle = alloc_handle_no_access_check( current->process, job,
1738 req->access, objattr->attributes );
1739 release_object( job );
1741 if (root) release_object( root );
1744 /* open a job object */
1745 DECL_HANDLER(open_job)
1747 struct unicode_str name = get_req_unicode_str();
1749 reply->handle = open_object( current->process, req->rootdir, req->access,
1750 &job_ops, &name, req->attributes );
1753 /* assign a job object to a process */
1754 DECL_HANDLER(assign_job)
1756 struct process *process;
1757 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1759 if (!job) return;
1761 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1763 if (!process->running_threads) set_error( STATUS_PROCESS_IS_TERMINATING );
1764 else add_job_process( job, process );
1765 release_object( process );
1767 release_object( job );
1771 /* check if a process is associated with a job */
1772 DECL_HANDLER(process_in_job)
1774 struct process *process;
1775 struct job *job;
1777 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1778 return;
1780 if (!req->job)
1782 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1784 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1786 set_error( process_in_job( job, process ) ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1787 release_object( job );
1789 release_object( process );
1792 /* retrieve information about a job */
1793 DECL_HANDLER(get_job_info)
1795 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_QUERY );
1796 process_id_t *pids;
1797 data_size_t len;
1799 if (!job) return;
1801 reply->total_processes = job->total_processes;
1802 reply->active_processes = job->num_processes;
1804 len = min( get_reply_max_size(), reply->active_processes * sizeof(*pids) );
1805 if (len && ((pids = set_reply_data_size( len ))))
1806 get_job_pids( job, pids, pids + len / sizeof(*pids) );
1808 release_object( job );
1811 /* terminate all processes associated with the job */
1812 DECL_HANDLER(terminate_job)
1814 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1816 if (!job) return;
1818 terminate_job( job, req->status );
1819 release_object( job );
1822 /* update limits of the job object */
1823 DECL_HANDLER(set_job_limits)
1825 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1827 if (!job) return;
1829 job->limit_flags = req->limit_flags;
1830 release_object( job );
1833 /* set the jobs completion port */
1834 DECL_HANDLER(set_job_completion_port)
1836 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1838 if (!job) return;
1840 if (!job->completion_port)
1842 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1843 job->completion_key = req->key;
1845 add_job_completion_existing_processes( job, job );
1847 else
1848 set_error( STATUS_INVALID_PARAMETER );
1850 release_object( job );
1853 /* Suspend a process */
1854 DECL_HANDLER(suspend_process)
1856 struct process *process;
1858 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1860 struct list *ptr, *next;
1862 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1864 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1865 suspend_thread( thread );
1868 release_object( process );
1872 /* Resume a process */
1873 DECL_HANDLER(resume_process)
1875 struct process *process;
1877 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1879 struct list *ptr, *next;
1881 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1883 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1884 resume_thread( thread );
1887 release_object( process );
1891 /* Get a list of processes and threads currently running */
1892 DECL_HANDLER(list_processes)
1894 struct process *process;
1895 struct thread *thread;
1896 unsigned int pos = 0;
1897 char *buffer;
1899 reply->process_count = 0;
1900 reply->total_thread_count = 0;
1901 reply->total_name_len = 0;
1902 reply->info_size = 0;
1904 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1906 reply->info_size = (reply->info_size + 7) & ~7;
1907 reply->info_size += sizeof(struct process_info) + process->imagelen;
1908 reply->info_size = (reply->info_size + 7) & ~7;
1909 reply->info_size += process->running_threads * sizeof(struct thread_info);
1910 reply->process_count++;
1911 reply->total_thread_count += process->running_threads;
1912 reply->total_name_len += process->imagelen;
1915 if (reply->info_size > get_reply_max_size())
1917 set_error( STATUS_INFO_LENGTH_MISMATCH );
1918 return;
1921 if (!(buffer = set_reply_data_size( reply->info_size ))) return;
1923 memset( buffer, 0, reply->info_size );
1924 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1926 struct process_info *process_info;
1928 pos = (pos + 7) & ~7;
1929 process_info = (struct process_info *)(buffer + pos);
1930 process_info->start_time = process->start_time;
1931 process_info->name_len = process->imagelen;
1932 process_info->thread_count = process->running_threads;
1933 process_info->priority = process->priority;
1934 process_info->pid = process->id;
1935 process_info->parent_pid = process->parent_id;
1936 process_info->session_id = process->session_id;
1937 process_info->handle_count = get_handle_table_count(process);
1938 process_info->unix_pid = process->unix_pid;
1939 pos += sizeof(*process_info);
1940 memcpy( buffer + pos, process->image, process->imagelen );
1941 pos += process->imagelen;
1942 pos = (pos + 7) & ~7;
1943 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1945 struct thread_info *thread_info = (struct thread_info *)(buffer + pos);
1947 thread_info->start_time = thread->creation_time;
1948 thread_info->tid = thread->id;
1949 thread_info->base_priority = thread->priority;
1950 thread_info->current_priority = thread->priority; /* FIXME */
1951 thread_info->unix_tid = thread->unix_tid;
1952 thread_info->entry_point = thread->entry_point;
1953 thread_info->teb = thread->teb;
1954 pos += sizeof(*thread_info);