ksecdd.sys: Build with msvcrt.
[wine.git] / server / process.c
blobbeaa68603e8f7863e9decf8bdce962aa4e2c3293
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->handles = NULL;
506 process->msg_fd = NULL;
507 process->sigkill_timeout = NULL;
508 process->unix_pid = -1;
509 process->exit_code = STILL_ACTIVE;
510 process->running_threads = 0;
511 process->priority = PROCESS_PRIOCLASS_NORMAL;
512 process->suspend = 0;
513 process->is_system = 0;
514 process->debug_children = 1;
515 process->is_terminating = 0;
516 process->job = NULL;
517 process->console = NULL;
518 process->startup_state = STARTUP_IN_PROGRESS;
519 process->startup_info = NULL;
520 process->idle_event = NULL;
521 process->exe_file = NULL;
522 process->peb = 0;
523 process->ldt_copy = 0;
524 process->dir_cache = NULL;
525 process->winstation = 0;
526 process->desktop = 0;
527 process->token = NULL;
528 process->trace_data = 0;
529 process->rawinput_mouse = NULL;
530 process->rawinput_kbd = NULL;
531 list_init( &process->kernel_object );
532 list_init( &process->thread_list );
533 list_init( &process->locks );
534 list_init( &process->asyncs );
535 list_init( &process->classes );
536 list_init( &process->views );
537 list_init( &process->dlls );
538 list_init( &process->rawinput_devices );
540 process->end_time = 0;
541 list_add_tail( &process_list, &process->entry );
543 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
544 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
546 close( fd );
547 goto error;
549 if (!(process->id = process->group_id = alloc_ptid( process )))
551 close( fd );
552 goto error;
554 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
556 /* create the handle table */
557 if (!parent)
559 process->handles = alloc_handle_table( process, 0 );
560 process->token = token_create_admin();
561 process->affinity = ~0;
563 else
565 process->parent_id = parent->id;
566 process->handles = inherit_all ? copy_handle_table( process, parent )
567 : alloc_handle_table( process, 0 );
568 /* Note: for security reasons, starting a new process does not attempt
569 * to use the current impersonation token for the new process */
570 process->token = token_duplicate( parent->token, TRUE, 0, NULL );
571 process->affinity = parent->affinity;
573 if (!process->handles || !process->token) goto error;
575 /* Assign a high security label to the token. The default would be medium
576 * but Wine provides admin access to all applications right now so high
577 * makes more sense for the time being. */
578 if (!token_assign_label( process->token, security_high_label_sid ))
579 goto error;
581 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
582 return process;
584 error:
585 if (process) release_object( process );
586 /* if we failed to start our first process, close everything down */
587 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
588 return NULL;
591 /* initialize the current process and fill in the request */
592 data_size_t init_process( struct thread *thread )
594 struct process *process = thread->process;
595 struct startup_info *info = process->startup_info;
597 if (!info) return 0;
598 return info->data_size;
601 /* destroy a process when its refcount is 0 */
602 static void process_destroy( struct object *obj )
604 struct process *process = (struct process *)obj;
605 assert( obj->ops == &process_ops );
607 /* we can't have a thread remaining */
608 assert( list_empty( &process->thread_list ));
609 assert( list_empty( &process->asyncs ));
611 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
613 close_process_handles( process );
614 set_process_startup_state( process, STARTUP_ABORTED );
616 if (process->job)
618 list_remove( &process->job_entry );
619 release_object( process->job );
621 if (process->console) release_object( process->console );
622 if (process->msg_fd) release_object( process->msg_fd );
623 list_remove( &process->entry );
624 if (process->idle_event) release_object( process->idle_event );
625 if (process->exe_file) release_object( process->exe_file );
626 if (process->id) free_ptid( process->id );
627 if (process->token) release_object( process->token );
628 free( process->dir_cache );
631 /* dump a process on stdout for debugging purposes */
632 static void process_dump( struct object *obj, int verbose )
634 struct process *process = (struct process *)obj;
635 assert( obj->ops == &process_ops );
637 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
640 static struct object_type *process_get_type( struct object *obj )
642 static const WCHAR name[] = {'P','r','o','c','e','s','s'};
643 static const struct unicode_str str = { name, sizeof(name) };
644 return get_object_type( &str );
647 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
649 struct process *process = (struct process *)obj;
650 return !process->running_threads;
653 static unsigned int process_map_access( struct object *obj, unsigned int access )
655 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
656 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
657 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
658 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
659 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
661 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
662 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
664 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
667 static struct list *process_get_kernel_obj_list( struct object *obj )
669 struct process *process = (struct process *)obj;
670 return &process->kernel_object;
673 static struct security_descriptor *process_get_sd( struct object *obj )
675 static struct security_descriptor *process_default_sd;
677 if (obj->sd) return obj->sd;
679 if (!process_default_sd)
681 size_t users_sid_len = security_sid_len( security_domain_users_sid );
682 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
683 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
684 + users_sid_len + admins_sid_len;
685 ACCESS_ALLOWED_ACE *aaa;
686 ACL *dacl;
688 process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
689 + dacl_len );
690 process_default_sd->control = SE_DACL_PRESENT;
691 process_default_sd->owner_len = admins_sid_len;
692 process_default_sd->group_len = users_sid_len;
693 process_default_sd->sacl_len = 0;
694 process_default_sd->dacl_len = dacl_len;
695 memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
696 memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
698 dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
699 dacl->AclRevision = ACL_REVISION;
700 dacl->Sbz1 = 0;
701 dacl->AclSize = dacl_len;
702 dacl->AceCount = 2;
703 dacl->Sbz2 = 0;
704 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
705 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
706 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
707 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
708 aaa->Mask = GENERIC_READ;
709 memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
710 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
711 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
712 aaa->Header.AceFlags = 0;
713 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
714 aaa->Mask = PROCESS_ALL_ACCESS;
715 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
717 return process_default_sd;
720 static void process_poll_event( struct fd *fd, int event )
722 struct process *process = get_fd_user( fd );
723 assert( process->obj.ops == &process_ops );
725 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
726 else if (event & POLLIN) receive_fd( process );
729 static void startup_info_destroy( struct object *obj )
731 struct startup_info *info = (struct startup_info *)obj;
732 assert( obj->ops == &startup_info_ops );
733 free( info->data );
734 if (info->process) release_object( info->process );
737 static void startup_info_dump( struct object *obj, int verbose )
739 struct startup_info *info = (struct startup_info *)obj;
740 assert( obj->ops == &startup_info_ops );
742 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
743 info->data->hstdin, info->data->hstdout, info->data->hstderr );
746 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
748 struct startup_info *info = (struct startup_info *)obj;
749 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
752 /* get a process from an id (and increment the refcount) */
753 struct process *get_process_from_id( process_id_t id )
755 struct object *obj = get_ptid_entry( id );
757 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
758 set_error( STATUS_INVALID_PARAMETER );
759 return NULL;
762 /* get a process from a handle (and increment the refcount) */
763 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
765 return (struct process *)get_handle_obj( current->process, handle,
766 access, &process_ops );
769 /* find a dll from its base address */
770 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
772 struct process_dll *dll;
774 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
776 if (dll->base == base) return dll;
778 return NULL;
781 /* add a dll to a process list */
782 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
783 const WCHAR *filename, data_size_t name_len )
785 struct process_dll *dll;
787 /* make sure we don't already have one with the same base address */
788 if (find_process_dll( process, base ))
790 set_error( STATUS_INVALID_PARAMETER );
791 return NULL;
794 if ((dll = mem_alloc( sizeof(*dll) )))
796 dll->base = base;
797 dll->filename = NULL;
798 dll->namelen = name_len;
799 if (name_len && !(dll->filename = memdup( filename, name_len )))
801 free( dll );
802 return NULL;
804 list_add_tail( &process->dlls, &dll->entry );
806 return dll;
809 /* remove a dll from a process list */
810 static void process_unload_dll( struct process *process, mod_handle_t base )
812 struct process_dll *dll = find_process_dll( process, base );
814 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
816 free( dll->filename );
817 list_remove( &dll->entry );
818 free( dll );
819 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
821 else set_error( STATUS_INVALID_PARAMETER );
824 /* terminate a process with the given exit code */
825 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
827 struct thread *thread;
829 grab_object( process ); /* make sure it doesn't get freed when threads die */
830 process->is_terminating = 1;
832 restart:
833 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
835 if (exit_code) thread->exit_code = exit_code;
836 if (thread == skip) continue;
837 if (thread->state == TERMINATED) continue;
838 kill_thread( thread, 1 );
839 goto restart;
841 release_object( process );
844 /* kill all processes */
845 static void kill_all_processes(void)
847 for (;;)
849 struct process *process;
851 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
853 if (process->running_threads) break;
855 if (&process->entry == &process_list) break; /* no process found */
856 terminate_process( process, NULL, 1 );
860 /* kill all processes being attached to a console renderer */
861 void kill_console_processes( struct thread *renderer, int exit_code )
863 for (;;) /* restart from the beginning of the list every time */
865 struct process *process;
867 /* find the first process being attached to 'renderer' and still running */
868 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
870 if (process == renderer->process) continue;
871 if (!process->running_threads) continue;
872 if (process->console && console_get_renderer( process->console ) == renderer) break;
874 if (&process->entry == &process_list) break; /* no process found */
875 terminate_process( process, NULL, exit_code );
879 /* a process has been killed (i.e. its last thread died) */
880 static void process_killed( struct process *process )
882 struct list *ptr;
884 assert( list_empty( &process->thread_list ));
885 process->end_time = current_time;
886 if (!process->is_system) close_process_desktop( process );
887 process->winstation = 0;
888 process->desktop = 0;
889 close_process_handles( process );
890 cancel_process_asyncs( process );
891 if (process->idle_event) release_object( process->idle_event );
892 if (process->exe_file) release_object( process->exe_file );
893 process->idle_event = NULL;
894 process->exe_file = NULL;
896 /* close the console attached to this process, if any */
897 free_console( process );
899 while ((ptr = list_head( &process->rawinput_devices )))
901 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
902 list_remove( &entry->entry );
903 free( entry );
905 while ((ptr = list_head( &process->dlls )))
907 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
908 free( dll->filename );
909 list_remove( &dll->entry );
910 free( dll );
912 destroy_process_classes( process );
913 free_mapped_views( process );
914 free_process_user_handles( process );
915 remove_process_locks( process );
916 set_process_startup_state( process, STARTUP_ABORTED );
917 finish_process_tracing( process );
918 release_job_process( process );
919 start_sigkill_timer( process );
920 wake_up( &process->obj, 0 );
923 /* add a thread to a process running threads list */
924 void add_process_thread( struct process *process, struct thread *thread )
926 list_add_tail( &process->thread_list, &thread->proc_entry );
927 if (!process->running_threads++)
929 running_processes++;
930 if (!process->is_system)
932 if (!user_processes++ && shutdown_timeout)
934 remove_timeout_user( shutdown_timeout );
935 shutdown_timeout = NULL;
939 grab_object( thread );
942 /* remove a thread from a process running threads list */
943 void remove_process_thread( struct process *process, struct thread *thread )
945 assert( process->running_threads > 0 );
946 assert( !list_empty( &process->thread_list ));
948 list_remove( &thread->proc_entry );
950 if (!--process->running_threads)
952 /* we have removed the last running thread, exit the process */
953 process->exit_code = thread->exit_code;
954 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
955 process_killed( process );
957 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
958 release_object( thread );
961 /* suspend all the threads of a process */
962 void suspend_process( struct process *process )
964 if (!process->suspend++)
966 struct list *ptr, *next;
968 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
970 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
971 if (!thread->suspend) stop_thread( thread );
976 /* resume all the threads of a process */
977 void resume_process( struct process *process )
979 assert (process->suspend > 0);
980 if (!--process->suspend)
982 struct list *ptr, *next;
984 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
986 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
987 if (!thread->suspend) wake_thread( thread );
992 /* kill a process on the spot */
993 void kill_process( struct process *process, int violent_death )
995 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
997 release_object( process->msg_fd );
998 process->msg_fd = NULL;
1001 if (process->sigkill_timeout) /* already waiting for it to die */
1003 remove_timeout_user( process->sigkill_timeout );
1004 process->sigkill_timeout = NULL;
1005 process_died( process );
1006 return;
1009 if (violent_death) terminate_process( process, NULL, 1 );
1010 else
1012 struct list *ptr;
1014 grab_object( process ); /* make sure it doesn't get freed when threads die */
1015 while ((ptr = list_head( &process->thread_list )))
1017 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1018 kill_thread( thread, 0 );
1020 release_object( process );
1024 /* kill all processes being debugged by a given thread */
1025 void kill_debugged_processes( struct thread *debugger, int exit_code )
1027 for (;;) /* restart from the beginning of the list every time */
1029 struct process *process;
1031 /* find the first process being debugged by 'debugger' and still running */
1032 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1034 if (!process->running_threads) continue;
1035 if (process->debugger == debugger) break;
1037 if (&process->entry == &process_list) break; /* no process found */
1038 process->debugger = NULL;
1039 terminate_process( process, NULL, exit_code );
1044 /* trigger a breakpoint event in a given process */
1045 void break_process( struct process *process )
1047 struct thread *thread;
1049 suspend_process( process );
1051 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1053 if (thread->context) /* inside an exception event already */
1055 break_thread( thread );
1056 goto done;
1059 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1060 else set_error( STATUS_ACCESS_DENIED );
1061 done:
1062 resume_process( process );
1066 /* detach a debugger from all its debuggees */
1067 void detach_debugged_processes( struct thread *debugger )
1069 struct process *process;
1071 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1073 if (process->debugger == debugger && process->running_threads)
1075 debugger_detach( process, debugger );
1081 void enum_processes( int (*cb)(struct process*, void*), void *user )
1083 struct list *ptr, *next;
1085 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1087 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1088 if ((cb)(process, user)) break;
1092 /* set the debugged flag in the process PEB */
1093 int set_process_debug_flag( struct process *process, int flag )
1095 char data = (flag != 0);
1097 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1098 return write_process_memory( process, process->peb + 2, 1, &data );
1101 /* take a snapshot of currently running processes */
1102 struct process_snapshot *process_snap( int *count )
1104 struct process_snapshot *snapshot, *ptr;
1105 struct process *process;
1107 if (!running_processes) return NULL;
1108 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1109 return NULL;
1110 ptr = snapshot;
1111 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1113 if (!process->running_threads) continue;
1114 ptr->process = process;
1115 ptr->threads = process->running_threads;
1116 ptr->count = process->obj.refcount;
1117 ptr->priority = process->priority;
1118 ptr->handles = get_handle_table_count(process);
1119 grab_object( process );
1120 ptr++;
1123 if (!(*count = ptr - snapshot))
1125 free( snapshot );
1126 snapshot = NULL;
1128 return snapshot;
1131 /* create a new process */
1132 DECL_HANDLER(new_process)
1134 struct startup_info *info;
1135 const void *info_ptr;
1136 struct unicode_str name;
1137 const struct security_descriptor *sd;
1138 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1139 struct process *process = NULL;
1140 struct process *parent = current->process;
1141 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1143 if (socket_fd == -1)
1145 set_error( STATUS_INVALID_PARAMETER );
1146 return;
1148 if (!objattr)
1150 set_error( STATUS_INVALID_PARAMETER );
1151 close( socket_fd );
1152 return;
1154 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1156 set_error( STATUS_INVALID_HANDLE );
1157 close( socket_fd );
1158 return;
1160 if (shutdown_stage)
1162 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1163 close( socket_fd );
1164 return;
1166 if (!is_cpu_supported( req->cpu ))
1168 close( socket_fd );
1169 return;
1172 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1173 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1175 set_error( STATUS_ACCESS_DENIED );
1176 close( socket_fd );
1177 return;
1180 /* build the startup info for a new process */
1181 if (!(info = alloc_object( &startup_info_ops )))
1183 close( socket_fd );
1184 return;
1186 info->process = NULL;
1187 info->data = NULL;
1189 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1190 info->info_size = min( req->info_size, info->data_size );
1192 if (req->info_size < sizeof(*info->data))
1194 /* make sure we have a full startup_info_t structure */
1195 data_size_t env_size = info->data_size - info->info_size;
1196 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1198 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1200 close( socket_fd );
1201 goto done;
1203 memcpy( info->data, info_ptr, info_size );
1204 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1205 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1206 info->info_size = sizeof(startup_info_t);
1207 info->data_size = info->info_size + env_size;
1209 else
1211 data_size_t pos = sizeof(*info->data);
1213 if (!(info->data = memdup( info_ptr, info->data_size )))
1215 close( socket_fd );
1216 goto done;
1218 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1219 FIXUP_LEN( info->data->curdir_len );
1220 FIXUP_LEN( info->data->dllpath_len );
1221 FIXUP_LEN( info->data->imagepath_len );
1222 FIXUP_LEN( info->data->cmdline_len );
1223 FIXUP_LEN( info->data->title_len );
1224 FIXUP_LEN( info->data->desktop_len );
1225 FIXUP_LEN( info->data->shellinfo_len );
1226 FIXUP_LEN( info->data->runtime_len );
1227 #undef FIXUP_LEN
1230 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1232 process->startup_info = (struct startup_info *)grab_object( info );
1234 if (req->exe_file &&
1235 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1236 goto done;
1238 if (parent->job
1239 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1240 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1242 add_job_process( parent->job, process );
1245 /* connect to the window station */
1246 connect_process_winstation( process, current );
1248 /* set the process console */
1249 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1251 /* FIXME: some better error checking should be done...
1252 * like if hConOut and hConIn are console handles, then they should be on the same
1253 * physical console
1255 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1258 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1260 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1261 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1262 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1263 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1264 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1265 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1266 /* some handles above may have been invalid; this is not an error */
1267 if (get_error() == STATUS_INVALID_HANDLE ||
1268 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1271 /* attach to the debugger if requested */
1272 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1274 set_process_debugger( process, current );
1275 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1277 else if (parent->debugger && parent->debug_children)
1279 set_process_debugger( process, parent->debugger );
1280 /* debug_children is set to 1 by default */
1283 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1284 process->group_id = parent->group_id;
1286 info->process = (struct process *)grab_object( process );
1287 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1288 reply->pid = get_process_id( process );
1289 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1291 done:
1292 if (process) release_object( process );
1293 release_object( info );
1296 /* execute a new process, replacing the existing one */
1297 DECL_HANDLER(exec_process)
1299 struct process *process;
1300 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1302 if (socket_fd == -1)
1304 set_error( STATUS_INVALID_PARAMETER );
1305 return;
1307 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1309 set_error( STATUS_INVALID_HANDLE );
1310 close( socket_fd );
1311 return;
1313 if (shutdown_stage)
1315 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1316 close( socket_fd );
1317 return;
1319 if (!is_cpu_supported( req->cpu ))
1321 close( socket_fd );
1322 return;
1324 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1325 create_thread( -1, process, NULL );
1326 release_object( process );
1329 /* Retrieve information about a newly started process */
1330 DECL_HANDLER(get_new_process_info)
1332 struct startup_info *info;
1334 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1335 0, &startup_info_ops )))
1337 reply->success = is_process_init_done( info->process );
1338 reply->exit_code = info->process->exit_code;
1339 release_object( info );
1343 /* Retrieve the new process startup info */
1344 DECL_HANDLER(get_startup_info)
1346 struct process *process = current->process;
1347 struct startup_info *info = process->startup_info;
1348 data_size_t size;
1350 if (!info) return;
1352 /* we return the data directly without making a copy so this can only be called once */
1353 reply->info_size = info->info_size;
1354 size = info->data_size;
1355 if (size > get_reply_max_size()) size = get_reply_max_size();
1356 set_reply_data_ptr( info->data, size );
1357 info->data = NULL;
1358 info->data_size = 0;
1361 /* signal the end of the process initialization */
1362 DECL_HANDLER(init_process_done)
1364 struct process_dll *dll;
1365 struct process *process = current->process;
1367 if (is_process_init_done(process))
1369 set_error( STATUS_INVALID_PARAMETER );
1370 return;
1372 if (!(dll = find_process_dll( process, req->module )))
1374 set_error( STATUS_DLL_NOT_FOUND );
1375 return;
1378 /* main exe is the first in the dll list */
1379 list_remove( &dll->entry );
1380 list_add_head( &process->dlls, &dll->entry );
1382 process->ldt_copy = req->ldt_copy;
1383 process->start_time = current_time;
1384 current->entry_point = req->entry;
1385 if (process->exe_file) release_object( process->exe_file );
1386 process->exe_file = NULL;
1388 init_process_tracing( process );
1389 generate_startup_debug_events( process, req->entry );
1390 set_process_startup_state( process, STARTUP_DONE );
1392 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1393 if (process->debugger) set_process_debug_flag( process, 1 );
1394 reply->suspend = (current->suspend || process->suspend);
1397 /* open a handle to a process */
1398 DECL_HANDLER(open_process)
1400 struct process *process = get_process_from_id( req->pid );
1401 reply->handle = 0;
1402 if (process)
1404 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1405 release_object( process );
1409 /* terminate a process */
1410 DECL_HANDLER(terminate_process)
1412 struct process *process;
1414 if (req->handle)
1416 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1417 if (!process) return;
1419 else process = (struct process *)grab_object( current->process );
1421 reply->self = (current->process == process);
1422 terminate_process( process, current, req->exit_code );
1423 release_object( process );
1426 /* fetch information about a process */
1427 DECL_HANDLER(get_process_info)
1429 struct process *process;
1431 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1433 reply->pid = get_process_id( process );
1434 reply->ppid = process->parent_id;
1435 reply->exit_code = process->exit_code;
1436 reply->priority = process->priority;
1437 reply->affinity = process->affinity;
1438 reply->peb = process->peb;
1439 reply->start_time = process->start_time;
1440 reply->end_time = process->end_time;
1441 reply->cpu = process->cpu;
1442 reply->debugger_present = !!process->debugger;
1443 reply->debug_children = process->debug_children;
1444 release_object( process );
1448 /* retrieve information about a process memory usage */
1449 DECL_HANDLER(get_process_vm_counters)
1451 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1453 if (!process) return;
1454 #ifdef linux
1455 if (process->unix_pid != -1)
1457 FILE *f;
1458 char proc_path[32], line[256];
1459 unsigned long value;
1461 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1462 if ((f = fopen( proc_path, "r" )))
1464 while (fgets( line, sizeof(line), f ))
1466 if (sscanf( line, "VmPeak: %lu", &value ))
1467 reply->peak_virtual_size = (mem_size_t)value * 1024;
1468 else if (sscanf( line, "VmSize: %lu", &value ))
1469 reply->virtual_size = (mem_size_t)value * 1024;
1470 else if (sscanf( line, "VmHWM: %lu", &value ))
1471 reply->peak_working_set_size = (mem_size_t)value * 1024;
1472 else if (sscanf( line, "VmRSS: %lu", &value ))
1473 reply->working_set_size = (mem_size_t)value * 1024;
1474 else if (sscanf( line, "RssAnon: %lu", &value ))
1475 reply->pagefile_usage += (mem_size_t)value * 1024;
1476 else if (sscanf( line, "VmSwap: %lu", &value ))
1477 reply->pagefile_usage += (mem_size_t)value * 1024;
1479 reply->peak_pagefile_usage = reply->pagefile_usage;
1480 fclose( f );
1482 else set_error( STATUS_ACCESS_DENIED );
1484 else set_error( STATUS_ACCESS_DENIED );
1485 #endif
1486 release_object( process );
1489 static void set_process_affinity( struct process *process, affinity_t affinity )
1491 struct thread *thread;
1493 if (!process->running_threads)
1495 set_error( STATUS_PROCESS_IS_TERMINATING );
1496 return;
1499 process->affinity = affinity;
1501 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1503 set_thread_affinity( thread, affinity );
1507 /* set information about a process */
1508 DECL_HANDLER(set_process_info)
1510 struct process *process;
1512 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1514 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1515 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1516 release_object( process );
1520 /* read data from a process address space */
1521 DECL_HANDLER(read_process_memory)
1523 struct process *process;
1524 data_size_t len = get_reply_max_size();
1526 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1528 if (len)
1530 char *buffer = mem_alloc( len );
1531 if (buffer)
1533 if (read_process_memory( process, req->addr, len, buffer ))
1534 set_reply_data_ptr( buffer, len );
1535 else
1536 free( buffer );
1539 release_object( process );
1542 /* write data to a process address space */
1543 DECL_HANDLER(write_process_memory)
1545 struct process *process;
1547 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1549 data_size_t len = get_req_data_size();
1550 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1551 else set_error( STATUS_INVALID_PARAMETER );
1552 release_object( process );
1556 /* notify the server that a dll has been loaded */
1557 DECL_HANDLER(load_dll)
1559 struct process_dll *dll;
1561 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1563 dll->dbg_offset = req->dbg_offset;
1564 dll->dbg_size = req->dbg_size;
1565 dll->name = req->name;
1566 /* only generate event if initialization is done */
1567 if (is_process_init_done( current->process ))
1568 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1572 /* notify the server that a dll is being unloaded */
1573 DECL_HANDLER(unload_dll)
1575 process_unload_dll( current->process, req->base );
1578 /* retrieve information about a module in a process */
1579 DECL_HANDLER(get_dll_info)
1581 struct process *process;
1583 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1585 struct process_dll *dll;
1587 if (req->base_address)
1588 dll = find_process_dll( process, req->base_address );
1589 else /* NULL means main module */
1590 dll = list_head( &process->dlls ) ?
1591 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1593 if (dll)
1595 reply->entry_point = 0; /* FIXME */
1596 reply->filename_len = dll->namelen;
1597 if (dll->filename)
1599 if (dll->namelen <= get_reply_max_size())
1600 set_reply_data( dll->filename, dll->namelen );
1601 else
1602 set_error( STATUS_BUFFER_TOO_SMALL );
1605 else
1606 set_error( STATUS_DLL_NOT_FOUND );
1608 release_object( process );
1612 /* retrieve the process idle event */
1613 DECL_HANDLER(get_process_idle_event)
1615 struct process *process;
1617 reply->event = 0;
1618 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1620 if (process->idle_event && process != current->process)
1621 reply->event = alloc_handle( current->process, process->idle_event,
1622 EVENT_ALL_ACCESS, 0 );
1623 release_object( process );
1627 /* make the current process a system process */
1628 DECL_HANDLER(make_process_system)
1630 struct process *process = current->process;
1632 if (!shutdown_event)
1634 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1635 make_object_static( (struct object *)shutdown_event );
1638 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1639 return;
1641 if (!process->is_system)
1643 process->is_system = 1;
1644 close_process_desktop( process );
1645 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1646 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1650 /* create a new job object */
1651 DECL_HANDLER(create_job)
1653 struct job *job;
1654 struct unicode_str name;
1655 struct object *root;
1656 const struct security_descriptor *sd;
1657 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1659 if (!objattr) return;
1661 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1663 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1664 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1665 else
1666 reply->handle = alloc_handle_no_access_check( current->process, job,
1667 req->access, objattr->attributes );
1668 release_object( job );
1670 if (root) release_object( root );
1673 /* open a job object */
1674 DECL_HANDLER(open_job)
1676 struct unicode_str name = get_req_unicode_str();
1678 reply->handle = open_object( current->process, req->rootdir, req->access,
1679 &job_ops, &name, req->attributes );
1682 /* assign a job object to a process */
1683 DECL_HANDLER(assign_job)
1685 struct process *process;
1686 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1688 if (!job) return;
1690 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1692 if (!process->running_threads)
1693 set_error( STATUS_PROCESS_IS_TERMINATING );
1694 else if (process->job)
1695 set_error( STATUS_ACCESS_DENIED );
1696 else
1697 add_job_process( job, process );
1698 release_object( process );
1700 release_object( job );
1704 /* check if a process is associated with a job */
1705 DECL_HANDLER(process_in_job)
1707 struct process *process;
1708 struct job *job;
1710 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1711 return;
1713 if (!req->job)
1715 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1717 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1719 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1720 release_object( job );
1722 release_object( process );
1725 /* terminate all processes associated with the job */
1726 DECL_HANDLER(terminate_job)
1728 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1730 if (!job) return;
1732 terminate_job( job, req->status );
1733 release_object( job );
1736 /* update limits of the job object */
1737 DECL_HANDLER(set_job_limits)
1739 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1741 if (!job) return;
1743 job->limit_flags = req->limit_flags;
1744 release_object( job );
1747 /* set the jobs completion port */
1748 DECL_HANDLER(set_job_completion_port)
1750 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1752 if (!job) return;
1754 if (!job->completion_port)
1756 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1757 job->completion_key = req->key;
1759 else
1760 set_error( STATUS_INVALID_PARAMETER );
1762 release_object( job );
1765 /* Suspend a process */
1766 DECL_HANDLER(suspend_process)
1768 struct process *process;
1770 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1772 struct list *ptr, *next;
1774 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1776 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1777 suspend_thread( thread );
1780 release_object( process );
1784 /* Resume a process */
1785 DECL_HANDLER(resume_process)
1787 struct process *process;
1789 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1791 struct list *ptr, *next;
1793 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1795 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1796 resume_thread( thread );
1799 release_object( process );