ntdll: Fix the Mac build with SDKs older than 10.14.
[wine.git] / server / process.c
blob16bb5d57e7295f69973727577e5f3f260d177dd0
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #include <unistd.h>
36 #ifdef HAVE_POLL_H
37 #include <poll.h>
38 #endif
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "user.h"
50 #include "security.h"
52 /* process structure */
54 static struct list process_list = LIST_INIT(process_list);
55 static int running_processes, user_processes;
56 static struct event *shutdown_event; /* signaled when shutdown starts */
57 static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
58 static int shutdown_stage; /* current stage in the shutdown process */
60 /* process operations */
62 static void process_dump( struct object *obj, int verbose );
63 static struct object_type *process_get_type( struct object *obj );
64 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
65 static unsigned int process_map_access( struct object *obj, unsigned int access );
66 static struct security_descriptor *process_get_sd( struct object *obj );
67 static void process_poll_event( struct fd *fd, int event );
68 static struct list *process_get_kernel_obj_list( struct object *obj );
69 static void process_destroy( struct object *obj );
70 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
72 static const struct object_ops process_ops =
74 sizeof(struct process), /* size */
75 process_dump, /* dump */
76 process_get_type, /* get_type */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 process_signaled, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 no_get_fd, /* get_fd */
83 process_map_access, /* map_access */
84 process_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_name */
89 no_open_file, /* open_file */
90 process_get_kernel_obj_list, /* get_kernel_obj_list */
91 no_close_handle, /* close_handle */
92 process_destroy /* destroy */
95 static const struct fd_ops process_fd_ops =
97 NULL, /* get_poll_events */
98 process_poll_event, /* poll_event */
99 NULL, /* flush */
100 NULL, /* get_fd_type */
101 NULL, /* ioctl */
102 NULL, /* queue_async */
103 NULL, /* reselect_async */
104 NULL /* cancel async */
107 /* process startup info */
109 struct startup_info
111 struct object obj; /* object header */
112 struct process *process; /* created process */
113 data_size_t info_size; /* size of startup info */
114 data_size_t data_size; /* size of whole startup data */
115 startup_info_t *data; /* data for startup info */
118 static void startup_info_dump( struct object *obj, int verbose );
119 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
120 static void startup_info_destroy( struct object *obj );
122 static const struct object_ops startup_info_ops =
124 sizeof(struct startup_info), /* size */
125 startup_info_dump, /* dump */
126 no_get_type, /* get_type */
127 add_queue, /* add_queue */
128 remove_queue, /* remove_queue */
129 startup_info_signaled, /* signaled */
130 no_satisfied, /* satisfied */
131 no_signal, /* signal */
132 no_get_fd, /* get_fd */
133 no_map_access, /* map_access */
134 default_get_sd, /* get_sd */
135 default_set_sd, /* set_sd */
136 no_lookup_name, /* lookup_name */
137 no_link_name, /* link_name */
138 NULL, /* unlink_name */
139 no_open_file, /* open_file */
140 no_kernel_obj_list, /* get_kernel_obj_list */
141 no_close_handle, /* close_handle */
142 startup_info_destroy /* destroy */
145 /* job object */
147 static void job_dump( struct object *obj, int verbose );
148 static struct object_type *job_get_type( struct object *obj );
149 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
150 static unsigned int job_map_access( struct object *obj, unsigned int access );
151 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
152 static void job_destroy( struct object *obj );
154 struct job
156 struct object obj; /* object header */
157 struct list process_list; /* list of all processes */
158 int num_processes; /* count of running processes */
159 unsigned int limit_flags; /* limit flags */
160 int terminating; /* job is terminating */
161 int signaled; /* job is signaled */
162 struct completion *completion_port; /* associated completion port */
163 apc_param_t completion_key; /* key to send with completion messages */
166 static const struct object_ops job_ops =
168 sizeof(struct job), /* size */
169 job_dump, /* dump */
170 job_get_type, /* get_type */
171 add_queue, /* add_queue */
172 remove_queue, /* remove_queue */
173 job_signaled, /* signaled */
174 no_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 job_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_lookup_name, /* lookup_name */
181 directory_link_name, /* link_name */
182 default_unlink_name, /* unlink_name */
183 no_open_file, /* open_file */
184 no_kernel_obj_list, /* get_kernel_obj_list */
185 job_close_handle, /* close_handle */
186 job_destroy /* destroy */
189 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
190 unsigned int attr, const struct security_descriptor *sd )
192 struct job *job;
194 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
196 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
198 /* initialize it if it didn't already exist */
199 list_init( &job->process_list );
200 job->num_processes = 0;
201 job->limit_flags = 0;
202 job->terminating = 0;
203 job->signaled = 0;
204 job->completion_port = NULL;
205 job->completion_key = 0;
208 return job;
211 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
213 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
216 static struct object_type *job_get_type( struct object *obj )
218 static const WCHAR name[] = {'J','o','b'};
219 static const struct unicode_str str = { name, sizeof(name) };
220 return get_object_type( &str );
223 static unsigned int job_map_access( struct object *obj, unsigned int access )
225 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
226 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
227 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
228 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
229 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
232 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
234 if (job->completion_port)
235 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
238 static void add_job_process( struct job *job, struct process *process )
240 process->job = (struct job *)grab_object( job );
241 list_add_tail( &job->process_list, &process->job_entry );
242 job->num_processes++;
244 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
247 /* called when a process has terminated, allow one additional process */
248 static void release_job_process( struct process *process )
250 struct job *job = process->job;
252 if (!job) return;
254 assert( job->num_processes );
255 job->num_processes--;
257 if (!job->terminating)
258 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
260 if (!job->num_processes)
261 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
264 static void terminate_job( struct job *job, int exit_code )
266 /* don't report completion events for terminated processes */
267 job->terminating = 1;
269 for (;;) /* restart from the beginning of the list every time */
271 struct process *process;
273 /* find the first process associated with this job and still running */
274 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
276 if (process->running_threads) break;
278 if (&process->job_entry == &job->process_list) break; /* no process found */
279 assert( process->job == job );
280 terminate_process( process, NULL, exit_code );
283 job->terminating = 0;
284 job->signaled = 1;
285 wake_up( &job->obj, 0 );
288 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
290 struct job *job = (struct job *)obj;
291 assert( obj->ops == &job_ops );
293 if (obj->handle_count == 1) /* last handle */
295 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
296 terminate_job( job, 0 );
298 return 1;
301 static void job_destroy( struct object *obj )
303 struct job *job = (struct job *)obj;
304 assert( obj->ops == &job_ops );
306 assert( !job->num_processes );
307 assert( list_empty(&job->process_list) );
309 if (job->completion_port) release_object( job->completion_port );
312 static void job_dump( struct object *obj, int verbose )
314 struct job *job = (struct job *)obj;
315 assert( obj->ops == &job_ops );
316 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
319 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
321 struct job *job = (struct job *)obj;
322 return job->signaled;
325 struct ptid_entry
327 void *ptr; /* entry ptr */
328 unsigned int next; /* next free entry */
331 static struct ptid_entry *ptid_entries; /* array of ptid entries */
332 static unsigned int used_ptid_entries; /* number of entries in use */
333 static unsigned int alloc_ptid_entries; /* number of allocated entries */
334 static unsigned int next_free_ptid; /* next free entry */
335 static unsigned int last_free_ptid; /* last free entry */
336 static unsigned int num_free_ptids; /* number of free ptids */
338 static void kill_all_processes(void);
340 #define PTID_OFFSET 8 /* offset for first ptid value */
342 /* allocate a new process or thread id */
343 unsigned int alloc_ptid( void *ptr )
345 struct ptid_entry *entry;
346 unsigned int id;
348 if (used_ptid_entries < alloc_ptid_entries)
350 id = used_ptid_entries + PTID_OFFSET;
351 entry = &ptid_entries[used_ptid_entries++];
353 else if (next_free_ptid && num_free_ptids >= 256)
355 id = next_free_ptid;
356 entry = &ptid_entries[id - PTID_OFFSET];
357 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
358 num_free_ptids--;
360 else /* need to grow the array */
362 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
363 if (!count) count = 512;
364 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
366 set_error( STATUS_NO_MEMORY );
367 return 0;
369 ptid_entries = entry;
370 alloc_ptid_entries = count;
371 id = used_ptid_entries + PTID_OFFSET;
372 entry = &ptid_entries[used_ptid_entries++];
375 entry->ptr = ptr;
376 return id;
379 /* free a process or thread id */
380 void free_ptid( unsigned int id )
382 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
384 entry->ptr = NULL;
385 entry->next = 0;
387 /* append to end of free list so that we don't reuse it too early */
388 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
389 else next_free_ptid = id;
390 last_free_ptid = id;
391 num_free_ptids++;
394 /* retrieve the pointer corresponding to a process or thread id */
395 void *get_ptid_entry( unsigned int id )
397 if (id < PTID_OFFSET) return NULL;
398 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
399 return ptid_entries[id - PTID_OFFSET].ptr;
402 /* return the main thread of the process */
403 struct thread *get_process_first_thread( struct process *process )
405 struct list *ptr = list_head( &process->thread_list );
406 if (!ptr) return NULL;
407 return LIST_ENTRY( ptr, struct thread, proc_entry );
410 /* set the state of the process startup info */
411 static void set_process_startup_state( struct process *process, enum startup_state state )
413 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
414 if (process->startup_info)
416 wake_up( &process->startup_info->obj, 0 );
417 release_object( process->startup_info );
418 process->startup_info = NULL;
422 /* callback for server shutdown */
423 static void server_shutdown_timeout( void *arg )
425 shutdown_timeout = NULL;
426 if (!running_processes)
428 close_master_socket( 0 );
429 return;
431 switch(++shutdown_stage)
433 case 1: /* signal system processes to exit */
434 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
435 if (shutdown_event) set_event( shutdown_event );
436 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
437 close_master_socket( 4 * -TICKS_PER_SEC );
438 break;
439 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
440 kill_all_processes();
441 break;
445 /* forced shutdown, used for wineserver -k */
446 void shutdown_master_socket(void)
448 kill_all_processes();
449 shutdown_stage = 2;
450 if (shutdown_timeout)
452 remove_timeout_user( shutdown_timeout );
453 shutdown_timeout = NULL;
455 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
458 /* final cleanup once we are sure a process is really dead */
459 static void process_died( struct process *process )
461 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
462 if (!process->is_system)
464 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
465 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
467 release_object( process );
468 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
471 /* callback for process sigkill timeout */
472 static void process_sigkill( void *private )
474 struct process *process = private;
476 process->sigkill_timeout = NULL;
477 kill( process->unix_pid, SIGKILL );
478 process_died( process );
481 /* start the sigkill timer for a process upon exit */
482 static void start_sigkill_timer( struct process *process )
484 grab_object( process );
485 if (process->unix_pid != -1 && process->msg_fd)
486 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
487 else
488 process_died( process );
491 /* create a new process */
492 /* if the function fails the fd is closed */
493 struct process *create_process( int fd, struct process *parent, int inherit_all,
494 const struct security_descriptor *sd )
496 struct process *process;
498 if (!(process = alloc_object( &process_ops )))
500 close( fd );
501 goto error;
503 process->parent_id = 0;
504 process->debugger = NULL;
505 process->debug_event = NULL;
506 process->handles = NULL;
507 process->msg_fd = NULL;
508 process->sigkill_timeout = NULL;
509 process->unix_pid = -1;
510 process->exit_code = STILL_ACTIVE;
511 process->running_threads = 0;
512 process->priority = PROCESS_PRIOCLASS_NORMAL;
513 process->suspend = 0;
514 process->is_system = 0;
515 process->debug_children = 1;
516 process->is_terminating = 0;
517 process->job = NULL;
518 process->console = NULL;
519 process->startup_state = STARTUP_IN_PROGRESS;
520 process->startup_info = NULL;
521 process->idle_event = NULL;
522 process->exe_file = NULL;
523 process->peb = 0;
524 process->ldt_copy = 0;
525 process->dir_cache = NULL;
526 process->winstation = 0;
527 process->desktop = 0;
528 process->token = NULL;
529 process->trace_data = 0;
530 process->rawinput_mouse = NULL;
531 process->rawinput_kbd = NULL;
532 list_init( &process->kernel_object );
533 list_init( &process->thread_list );
534 list_init( &process->locks );
535 list_init( &process->asyncs );
536 list_init( &process->classes );
537 list_init( &process->views );
538 list_init( &process->dlls );
539 list_init( &process->rawinput_devices );
541 process->end_time = 0;
542 list_add_tail( &process_list, &process->entry );
544 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
545 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
547 close( fd );
548 goto error;
550 if (!(process->id = process->group_id = alloc_ptid( process )))
552 close( fd );
553 goto error;
555 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
557 /* create the handle table */
558 if (!parent)
560 process->handles = alloc_handle_table( process, 0 );
561 process->token = token_create_admin();
562 process->affinity = ~0;
564 else
566 process->parent_id = parent->id;
567 process->handles = inherit_all ? copy_handle_table( process, parent )
568 : alloc_handle_table( process, 0 );
569 /* Note: for security reasons, starting a new process does not attempt
570 * to use the current impersonation token for the new process */
571 process->token = token_duplicate( parent->token, TRUE, 0, NULL );
572 process->affinity = parent->affinity;
574 if (!process->handles || !process->token) goto error;
576 /* Assign a high security label to the token. The default would be medium
577 * but Wine provides admin access to all applications right now so high
578 * makes more sense for the time being. */
579 if (!token_assign_label( process->token, security_high_label_sid ))
580 goto error;
582 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
583 return process;
585 error:
586 if (process) release_object( process );
587 /* if we failed to start our first process, close everything down */
588 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
589 return NULL;
592 /* initialize the current process and fill in the request */
593 data_size_t init_process( struct thread *thread )
595 struct process *process = thread->process;
596 struct startup_info *info = process->startup_info;
598 if (!info) return 0;
599 return info->data_size;
602 /* destroy a process when its refcount is 0 */
603 static void process_destroy( struct object *obj )
605 struct process *process = (struct process *)obj;
606 assert( obj->ops == &process_ops );
608 /* we can't have a thread remaining */
609 assert( list_empty( &process->thread_list ));
610 assert( list_empty( &process->asyncs ));
612 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
614 close_process_handles( process );
615 set_process_startup_state( process, STARTUP_ABORTED );
617 if (process->job)
619 list_remove( &process->job_entry );
620 release_object( process->job );
622 if (process->console) release_object( process->console );
623 if (process->msg_fd) release_object( process->msg_fd );
624 list_remove( &process->entry );
625 if (process->idle_event) release_object( process->idle_event );
626 if (process->exe_file) release_object( process->exe_file );
627 if (process->id) free_ptid( process->id );
628 if (process->token) release_object( process->token );
629 free( process->dir_cache );
632 /* dump a process on stdout for debugging purposes */
633 static void process_dump( struct object *obj, int verbose )
635 struct process *process = (struct process *)obj;
636 assert( obj->ops == &process_ops );
638 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
641 static struct object_type *process_get_type( struct object *obj )
643 static const WCHAR name[] = {'P','r','o','c','e','s','s'};
644 static const struct unicode_str str = { name, sizeof(name) };
645 return get_object_type( &str );
648 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
650 struct process *process = (struct process *)obj;
651 return !process->running_threads;
654 static unsigned int process_map_access( struct object *obj, unsigned int access )
656 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
657 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
658 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
659 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
660 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
662 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
663 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
665 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
668 static struct list *process_get_kernel_obj_list( struct object *obj )
670 struct process *process = (struct process *)obj;
671 return &process->kernel_object;
674 static struct security_descriptor *process_get_sd( struct object *obj )
676 static struct security_descriptor *process_default_sd;
678 if (obj->sd) return obj->sd;
680 if (!process_default_sd)
682 size_t users_sid_len = security_sid_len( security_domain_users_sid );
683 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
684 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
685 + users_sid_len + admins_sid_len;
686 ACCESS_ALLOWED_ACE *aaa;
687 ACL *dacl;
689 process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
690 + dacl_len );
691 process_default_sd->control = SE_DACL_PRESENT;
692 process_default_sd->owner_len = admins_sid_len;
693 process_default_sd->group_len = users_sid_len;
694 process_default_sd->sacl_len = 0;
695 process_default_sd->dacl_len = dacl_len;
696 memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
697 memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
699 dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
700 dacl->AclRevision = ACL_REVISION;
701 dacl->Sbz1 = 0;
702 dacl->AclSize = dacl_len;
703 dacl->AceCount = 2;
704 dacl->Sbz2 = 0;
705 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
706 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
707 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
708 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
709 aaa->Mask = GENERIC_READ;
710 memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
711 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
712 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
713 aaa->Header.AceFlags = 0;
714 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
715 aaa->Mask = PROCESS_ALL_ACCESS;
716 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
718 return process_default_sd;
721 static void process_poll_event( struct fd *fd, int event )
723 struct process *process = get_fd_user( fd );
724 assert( process->obj.ops == &process_ops );
726 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
727 else if (event & POLLIN) receive_fd( process );
730 static void startup_info_destroy( struct object *obj )
732 struct startup_info *info = (struct startup_info *)obj;
733 assert( obj->ops == &startup_info_ops );
734 free( info->data );
735 if (info->process) release_object( info->process );
738 static void startup_info_dump( struct object *obj, int verbose )
740 struct startup_info *info = (struct startup_info *)obj;
741 assert( obj->ops == &startup_info_ops );
743 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
744 info->data->hstdin, info->data->hstdout, info->data->hstderr );
747 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
749 struct startup_info *info = (struct startup_info *)obj;
750 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
753 /* get a process from an id (and increment the refcount) */
754 struct process *get_process_from_id( process_id_t id )
756 struct object *obj = get_ptid_entry( id );
758 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
759 set_error( STATUS_INVALID_PARAMETER );
760 return NULL;
763 /* get a process from a handle (and increment the refcount) */
764 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
766 return (struct process *)get_handle_obj( current->process, handle,
767 access, &process_ops );
770 /* find a dll from its base address */
771 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
773 struct process_dll *dll;
775 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
777 if (dll->base == base) return dll;
779 return NULL;
782 /* add a dll to a process list */
783 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
784 const WCHAR *filename, data_size_t name_len )
786 struct process_dll *dll;
788 /* make sure we don't already have one with the same base address */
789 if (find_process_dll( process, base ))
791 set_error( STATUS_INVALID_PARAMETER );
792 return NULL;
795 if ((dll = mem_alloc( sizeof(*dll) )))
797 dll->base = base;
798 dll->filename = NULL;
799 dll->namelen = name_len;
800 if (name_len && !(dll->filename = memdup( filename, name_len )))
802 free( dll );
803 return NULL;
805 list_add_tail( &process->dlls, &dll->entry );
807 return dll;
810 /* remove a dll from a process list */
811 static void process_unload_dll( struct process *process, mod_handle_t base )
813 struct process_dll *dll = find_process_dll( process, base );
815 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
817 free( dll->filename );
818 list_remove( &dll->entry );
819 free( dll );
820 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
822 else set_error( STATUS_INVALID_PARAMETER );
825 /* terminate a process with the given exit code */
826 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
828 struct thread *thread;
830 grab_object( process ); /* make sure it doesn't get freed when threads die */
831 process->is_terminating = 1;
833 restart:
834 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
836 if (exit_code) thread->exit_code = exit_code;
837 if (thread == skip) continue;
838 if (thread->state == TERMINATED) continue;
839 kill_thread( thread, 1 );
840 goto restart;
842 release_object( process );
845 /* kill all processes */
846 static void kill_all_processes(void)
848 for (;;)
850 struct process *process;
852 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
854 if (process->running_threads) break;
856 if (&process->entry == &process_list) break; /* no process found */
857 terminate_process( process, NULL, 1 );
861 /* kill all processes being attached to a console renderer */
862 void kill_console_processes( struct thread *renderer, int exit_code )
864 for (;;) /* restart from the beginning of the list every time */
866 struct process *process;
868 /* find the first process being attached to 'renderer' and still running */
869 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
871 if (process == renderer->process) continue;
872 if (!process->running_threads) continue;
873 if (process->console && console_get_renderer( process->console ) == renderer) break;
875 if (&process->entry == &process_list) break; /* no process found */
876 terminate_process( process, NULL, exit_code );
880 /* a process has been killed (i.e. its last thread died) */
881 static void process_killed( struct process *process )
883 struct list *ptr;
885 assert( list_empty( &process->thread_list ));
886 process->end_time = current_time;
887 if (!process->is_system) close_process_desktop( process );
888 process->winstation = 0;
889 process->desktop = 0;
890 close_process_handles( process );
891 cancel_process_asyncs( process );
892 if (process->idle_event) release_object( process->idle_event );
893 if (process->exe_file) release_object( process->exe_file );
894 process->idle_event = NULL;
895 process->exe_file = NULL;
897 /* close the console attached to this process, if any */
898 free_console( process );
900 while ((ptr = list_head( &process->rawinput_devices )))
902 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
903 list_remove( &entry->entry );
904 free( entry );
906 while ((ptr = list_head( &process->dlls )))
908 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
909 free( dll->filename );
910 list_remove( &dll->entry );
911 free( dll );
913 destroy_process_classes( process );
914 free_mapped_views( process );
915 free_process_user_handles( process );
916 remove_process_locks( process );
917 set_process_startup_state( process, STARTUP_ABORTED );
918 finish_process_tracing( process );
919 release_job_process( process );
920 start_sigkill_timer( process );
921 wake_up( &process->obj, 0 );
924 /* add a thread to a process running threads list */
925 void add_process_thread( struct process *process, struct thread *thread )
927 list_add_tail( &process->thread_list, &thread->proc_entry );
928 if (!process->running_threads++)
930 running_processes++;
931 if (!process->is_system)
933 if (!user_processes++ && shutdown_timeout)
935 remove_timeout_user( shutdown_timeout );
936 shutdown_timeout = NULL;
940 grab_object( thread );
943 /* remove a thread from a process running threads list */
944 void remove_process_thread( struct process *process, struct thread *thread )
946 assert( process->running_threads > 0 );
947 assert( !list_empty( &process->thread_list ));
949 list_remove( &thread->proc_entry );
951 if (!--process->running_threads)
953 /* we have removed the last running thread, exit the process */
954 process->exit_code = thread->exit_code;
955 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
956 process_killed( process );
958 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
959 release_object( thread );
962 /* suspend all the threads of a process */
963 void suspend_process( struct process *process )
965 if (!process->suspend++)
967 struct list *ptr, *next;
969 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
971 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
972 if (!thread->suspend) stop_thread( thread );
977 /* resume all the threads of a process */
978 void resume_process( struct process *process )
980 assert (process->suspend > 0);
981 if (!--process->suspend)
983 struct list *ptr, *next;
985 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
987 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
988 if (!thread->suspend) wake_thread( thread );
993 /* kill a process on the spot */
994 void kill_process( struct process *process, int violent_death )
996 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
998 release_object( process->msg_fd );
999 process->msg_fd = NULL;
1002 if (process->sigkill_timeout) /* already waiting for it to die */
1004 remove_timeout_user( process->sigkill_timeout );
1005 process->sigkill_timeout = NULL;
1006 process_died( process );
1007 return;
1010 if (violent_death) terminate_process( process, NULL, 1 );
1011 else
1013 struct list *ptr;
1015 grab_object( process ); /* make sure it doesn't get freed when threads die */
1016 while ((ptr = list_head( &process->thread_list )))
1018 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1019 kill_thread( thread, 0 );
1021 release_object( process );
1025 /* kill all processes being debugged by a given thread */
1026 void kill_debugged_processes( struct thread *debugger, int exit_code )
1028 for (;;) /* restart from the beginning of the list every time */
1030 struct process *process;
1032 /* find the first process being debugged by 'debugger' and still running */
1033 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1035 if (!process->running_threads) continue;
1036 if (process->debugger == debugger) break;
1038 if (&process->entry == &process_list) break; /* no process found */
1039 process->debugger = NULL;
1040 terminate_process( process, NULL, exit_code );
1045 /* detach a debugger from all its debuggees */
1046 void detach_debugged_processes( struct thread *debugger )
1048 struct process *process;
1050 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1052 if (process->debugger == debugger && process->running_threads)
1054 debugger_detach( process, debugger );
1060 void enum_processes( int (*cb)(struct process*, void*), void *user )
1062 struct list *ptr, *next;
1064 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1066 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1067 if ((cb)(process, user)) break;
1071 /* set the debugged flag in the process PEB */
1072 int set_process_debug_flag( struct process *process, int flag )
1074 char data = (flag != 0);
1076 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1077 return write_process_memory( process, process->peb + 2, 1, &data );
1080 /* take a snapshot of currently running processes */
1081 struct process_snapshot *process_snap( int *count )
1083 struct process_snapshot *snapshot, *ptr;
1084 struct process *process;
1086 if (!running_processes) return NULL;
1087 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1088 return NULL;
1089 ptr = snapshot;
1090 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1092 if (!process->running_threads) continue;
1093 ptr->process = process;
1094 ptr->threads = process->running_threads;
1095 ptr->count = process->obj.refcount;
1096 ptr->priority = process->priority;
1097 ptr->handles = get_handle_table_count(process);
1098 grab_object( process );
1099 ptr++;
1102 if (!(*count = ptr - snapshot))
1104 free( snapshot );
1105 snapshot = NULL;
1107 return snapshot;
1110 /* create a new process */
1111 DECL_HANDLER(new_process)
1113 struct startup_info *info;
1114 const void *info_ptr;
1115 struct unicode_str name;
1116 const struct security_descriptor *sd;
1117 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1118 struct process *process = NULL;
1119 struct process *parent = current->process;
1120 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1122 if (socket_fd == -1)
1124 set_error( STATUS_INVALID_PARAMETER );
1125 return;
1127 if (!objattr)
1129 set_error( STATUS_INVALID_PARAMETER );
1130 close( socket_fd );
1131 return;
1133 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1135 set_error( STATUS_INVALID_HANDLE );
1136 close( socket_fd );
1137 return;
1139 if (shutdown_stage)
1141 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1142 close( socket_fd );
1143 return;
1145 if (!is_cpu_supported( req->cpu ))
1147 close( socket_fd );
1148 return;
1151 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1152 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1154 set_error( STATUS_ACCESS_DENIED );
1155 close( socket_fd );
1156 return;
1159 /* build the startup info for a new process */
1160 if (!(info = alloc_object( &startup_info_ops )))
1162 close( socket_fd );
1163 return;
1165 info->process = NULL;
1166 info->data = NULL;
1168 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1169 info->info_size = min( req->info_size, info->data_size );
1171 if (req->info_size < sizeof(*info->data))
1173 /* make sure we have a full startup_info_t structure */
1174 data_size_t env_size = info->data_size - info->info_size;
1175 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1177 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1179 close( socket_fd );
1180 goto done;
1182 memcpy( info->data, info_ptr, info_size );
1183 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1184 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1185 info->info_size = sizeof(startup_info_t);
1186 info->data_size = info->info_size + env_size;
1188 else
1190 data_size_t pos = sizeof(*info->data);
1192 if (!(info->data = memdup( info_ptr, info->data_size )))
1194 close( socket_fd );
1195 goto done;
1197 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1198 FIXUP_LEN( info->data->curdir_len );
1199 FIXUP_LEN( info->data->dllpath_len );
1200 FIXUP_LEN( info->data->imagepath_len );
1201 FIXUP_LEN( info->data->cmdline_len );
1202 FIXUP_LEN( info->data->title_len );
1203 FIXUP_LEN( info->data->desktop_len );
1204 FIXUP_LEN( info->data->shellinfo_len );
1205 FIXUP_LEN( info->data->runtime_len );
1206 #undef FIXUP_LEN
1209 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1211 process->startup_info = (struct startup_info *)grab_object( info );
1213 if (req->exe_file &&
1214 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1215 goto done;
1217 if (parent->job
1218 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1219 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1221 add_job_process( parent->job, process );
1224 /* connect to the window station */
1225 connect_process_winstation( process, current );
1227 /* set the process console */
1228 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1230 /* FIXME: some better error checking should be done...
1231 * like if hConOut and hConIn are console handles, then they should be on the same
1232 * physical console
1234 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1237 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1239 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1240 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1241 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1242 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1243 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1244 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1245 /* some handles above may have been invalid; this is not an error */
1246 if (get_error() == STATUS_INVALID_HANDLE ||
1247 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1250 /* attach to the debugger if requested */
1251 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1253 set_process_debugger( process, current );
1254 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1256 else if (parent->debugger && parent->debug_children)
1258 set_process_debugger( process, parent->debugger );
1259 /* debug_children is set to 1 by default */
1262 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1263 process->group_id = parent->group_id;
1265 info->process = (struct process *)grab_object( process );
1266 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1267 reply->pid = get_process_id( process );
1268 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1270 done:
1271 if (process) release_object( process );
1272 release_object( info );
1275 /* execute a new process, replacing the existing one */
1276 DECL_HANDLER(exec_process)
1278 struct process *process;
1279 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1281 if (socket_fd == -1)
1283 set_error( STATUS_INVALID_PARAMETER );
1284 return;
1286 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1288 set_error( STATUS_INVALID_HANDLE );
1289 close( socket_fd );
1290 return;
1292 if (shutdown_stage)
1294 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1295 close( socket_fd );
1296 return;
1298 if (!is_cpu_supported( req->cpu ))
1300 close( socket_fd );
1301 return;
1303 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1304 create_thread( -1, process, NULL );
1305 release_object( process );
1308 /* Retrieve information about a newly started process */
1309 DECL_HANDLER(get_new_process_info)
1311 struct startup_info *info;
1313 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1314 0, &startup_info_ops )))
1316 reply->success = is_process_init_done( info->process );
1317 reply->exit_code = info->process->exit_code;
1318 release_object( info );
1322 /* Retrieve the new process startup info */
1323 DECL_HANDLER(get_startup_info)
1325 struct process *process = current->process;
1326 struct startup_info *info = process->startup_info;
1327 data_size_t size;
1329 if (!info) return;
1331 /* we return the data directly without making a copy so this can only be called once */
1332 reply->info_size = info->info_size;
1333 size = info->data_size;
1334 if (size > get_reply_max_size()) size = get_reply_max_size();
1335 set_reply_data_ptr( info->data, size );
1336 info->data = NULL;
1337 info->data_size = 0;
1340 /* signal the end of the process initialization */
1341 DECL_HANDLER(init_process_done)
1343 struct process_dll *dll;
1344 struct process *process = current->process;
1346 if (is_process_init_done(process))
1348 set_error( STATUS_INVALID_PARAMETER );
1349 return;
1351 if (!(dll = find_process_dll( process, req->module )))
1353 set_error( STATUS_DLL_NOT_FOUND );
1354 return;
1357 /* main exe is the first in the dll list */
1358 list_remove( &dll->entry );
1359 list_add_head( &process->dlls, &dll->entry );
1361 process->ldt_copy = req->ldt_copy;
1362 process->start_time = current_time;
1363 current->entry_point = req->entry;
1364 if (process->exe_file) release_object( process->exe_file );
1365 process->exe_file = NULL;
1367 init_process_tracing( process );
1368 generate_startup_debug_events( process, req->entry );
1369 set_process_startup_state( process, STARTUP_DONE );
1371 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1372 if (process->debugger) set_process_debug_flag( process, 1 );
1373 reply->suspend = (current->suspend || process->suspend);
1376 /* open a handle to a process */
1377 DECL_HANDLER(open_process)
1379 struct process *process = get_process_from_id( req->pid );
1380 reply->handle = 0;
1381 if (process)
1383 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1384 release_object( process );
1388 /* terminate a process */
1389 DECL_HANDLER(terminate_process)
1391 struct process *process;
1393 if (req->handle)
1395 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1396 if (!process) return;
1398 else process = (struct process *)grab_object( current->process );
1400 reply->self = (current->process == process);
1401 terminate_process( process, current, req->exit_code );
1402 release_object( process );
1405 /* fetch information about a process */
1406 DECL_HANDLER(get_process_info)
1408 struct process *process;
1410 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1412 reply->pid = get_process_id( process );
1413 reply->ppid = process->parent_id;
1414 reply->exit_code = process->exit_code;
1415 reply->priority = process->priority;
1416 reply->affinity = process->affinity;
1417 reply->peb = process->peb;
1418 reply->start_time = process->start_time;
1419 reply->end_time = process->end_time;
1420 reply->cpu = process->cpu;
1421 reply->debugger_present = !!process->debugger;
1422 reply->debug_children = process->debug_children;
1423 release_object( process );
1427 /* retrieve information about a process memory usage */
1428 DECL_HANDLER(get_process_vm_counters)
1430 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1432 if (!process) return;
1433 #ifdef linux
1434 if (process->unix_pid != -1)
1436 FILE *f;
1437 char proc_path[32], line[256];
1438 unsigned long value;
1440 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1441 if ((f = fopen( proc_path, "r" )))
1443 while (fgets( line, sizeof(line), f ))
1445 if (sscanf( line, "VmPeak: %lu", &value ))
1446 reply->peak_virtual_size = (mem_size_t)value * 1024;
1447 else if (sscanf( line, "VmSize: %lu", &value ))
1448 reply->virtual_size = (mem_size_t)value * 1024;
1449 else if (sscanf( line, "VmHWM: %lu", &value ))
1450 reply->peak_working_set_size = (mem_size_t)value * 1024;
1451 else if (sscanf( line, "VmRSS: %lu", &value ))
1452 reply->working_set_size = (mem_size_t)value * 1024;
1453 else if (sscanf( line, "RssAnon: %lu", &value ))
1454 reply->pagefile_usage += (mem_size_t)value * 1024;
1455 else if (sscanf( line, "VmSwap: %lu", &value ))
1456 reply->pagefile_usage += (mem_size_t)value * 1024;
1458 reply->peak_pagefile_usage = reply->pagefile_usage;
1459 fclose( f );
1461 else set_error( STATUS_ACCESS_DENIED );
1463 else set_error( STATUS_ACCESS_DENIED );
1464 #endif
1465 release_object( process );
1468 static void set_process_affinity( struct process *process, affinity_t affinity )
1470 struct thread *thread;
1472 if (!process->running_threads)
1474 set_error( STATUS_PROCESS_IS_TERMINATING );
1475 return;
1478 process->affinity = affinity;
1480 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1482 set_thread_affinity( thread, affinity );
1486 /* set information about a process */
1487 DECL_HANDLER(set_process_info)
1489 struct process *process;
1491 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1493 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1494 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1495 release_object( process );
1499 /* read data from a process address space */
1500 DECL_HANDLER(read_process_memory)
1502 struct process *process;
1503 data_size_t len = get_reply_max_size();
1505 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1507 if (len)
1509 char *buffer = mem_alloc( len );
1510 if (buffer)
1512 if (read_process_memory( process, req->addr, len, buffer ))
1513 set_reply_data_ptr( buffer, len );
1514 else
1515 free( buffer );
1518 release_object( process );
1521 /* write data to a process address space */
1522 DECL_HANDLER(write_process_memory)
1524 struct process *process;
1526 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1528 data_size_t len = get_req_data_size();
1529 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1530 else set_error( STATUS_INVALID_PARAMETER );
1531 release_object( process );
1535 /* notify the server that a dll has been loaded */
1536 DECL_HANDLER(load_dll)
1538 struct process_dll *dll;
1540 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1542 dll->dbg_offset = req->dbg_offset;
1543 dll->dbg_size = req->dbg_size;
1544 dll->name = req->name;
1545 /* only generate event if initialization is done */
1546 if (is_process_init_done( current->process ))
1547 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1551 /* notify the server that a dll is being unloaded */
1552 DECL_HANDLER(unload_dll)
1554 process_unload_dll( current->process, req->base );
1557 /* retrieve information about a module in a process */
1558 DECL_HANDLER(get_dll_info)
1560 struct process *process;
1562 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1564 struct process_dll *dll;
1566 if (req->base_address)
1567 dll = find_process_dll( process, req->base_address );
1568 else /* NULL means main module */
1569 dll = list_head( &process->dlls ) ?
1570 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1572 if (dll)
1574 reply->entry_point = 0; /* FIXME */
1575 reply->filename_len = dll->namelen;
1576 if (dll->filename)
1578 if (dll->namelen <= get_reply_max_size())
1579 set_reply_data( dll->filename, dll->namelen );
1580 else
1581 set_error( STATUS_BUFFER_TOO_SMALL );
1584 else
1585 set_error( STATUS_DLL_NOT_FOUND );
1587 release_object( process );
1591 /* retrieve the process idle event */
1592 DECL_HANDLER(get_process_idle_event)
1594 struct process *process;
1596 reply->event = 0;
1597 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1599 if (process->idle_event && process != current->process)
1600 reply->event = alloc_handle( current->process, process->idle_event,
1601 EVENT_ALL_ACCESS, 0 );
1602 release_object( process );
1606 /* make the current process a system process */
1607 DECL_HANDLER(make_process_system)
1609 struct process *process = current->process;
1611 if (!shutdown_event)
1613 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1614 make_object_static( (struct object *)shutdown_event );
1617 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1618 return;
1620 if (!process->is_system)
1622 process->is_system = 1;
1623 close_process_desktop( process );
1624 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1625 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1629 /* create a new job object */
1630 DECL_HANDLER(create_job)
1632 struct job *job;
1633 struct unicode_str name;
1634 struct object *root;
1635 const struct security_descriptor *sd;
1636 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1638 if (!objattr) return;
1640 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1642 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1643 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1644 else
1645 reply->handle = alloc_handle_no_access_check( current->process, job,
1646 req->access, objattr->attributes );
1647 release_object( job );
1649 if (root) release_object( root );
1652 /* open a job object */
1653 DECL_HANDLER(open_job)
1655 struct unicode_str name = get_req_unicode_str();
1657 reply->handle = open_object( current->process, req->rootdir, req->access,
1658 &job_ops, &name, req->attributes );
1661 /* assign a job object to a process */
1662 DECL_HANDLER(assign_job)
1664 struct process *process;
1665 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1667 if (!job) return;
1669 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1671 if (!process->running_threads)
1672 set_error( STATUS_PROCESS_IS_TERMINATING );
1673 else if (process->job)
1674 set_error( STATUS_ACCESS_DENIED );
1675 else
1676 add_job_process( job, process );
1677 release_object( process );
1679 release_object( job );
1683 /* check if a process is associated with a job */
1684 DECL_HANDLER(process_in_job)
1686 struct process *process;
1687 struct job *job;
1689 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1690 return;
1692 if (!req->job)
1694 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1696 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1698 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1699 release_object( job );
1701 release_object( process );
1704 /* terminate all processes associated with the job */
1705 DECL_HANDLER(terminate_job)
1707 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1709 if (!job) return;
1711 terminate_job( job, req->status );
1712 release_object( job );
1715 /* update limits of the job object */
1716 DECL_HANDLER(set_job_limits)
1718 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1720 if (!job) return;
1722 job->limit_flags = req->limit_flags;
1723 release_object( job );
1726 /* set the jobs completion port */
1727 DECL_HANDLER(set_job_completion_port)
1729 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1731 if (!job) return;
1733 if (!job->completion_port)
1735 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1736 job->completion_key = req->key;
1738 else
1739 set_error( STATUS_INVALID_PARAMETER );
1741 release_object( job );
1744 /* Suspend a process */
1745 DECL_HANDLER(suspend_process)
1747 struct process *process;
1749 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1751 struct list *ptr, *next;
1753 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1755 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1756 suspend_thread( thread );
1759 release_object( process );
1763 /* Resume a process */
1764 DECL_HANDLER(resume_process)
1766 struct process *process;
1768 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1770 struct list *ptr, *next;
1772 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1774 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1775 resume_thread( thread );
1778 release_object( process );