ntdll: Load .so builtin modules without using libwine.
[wine/zf.git] / server / process.c
blob73984f363f595dd5863e1052aa41a6fa574b884b
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;
1120 struct thread *parent_thread = current;
1121 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1123 if (socket_fd == -1)
1125 set_error( STATUS_INVALID_PARAMETER );
1126 return;
1128 if (!objattr)
1130 set_error( STATUS_INVALID_PARAMETER );
1131 close( socket_fd );
1132 return;
1134 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1136 set_error( STATUS_INVALID_HANDLE );
1137 close( socket_fd );
1138 return;
1140 if (shutdown_stage)
1142 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1143 close( socket_fd );
1144 return;
1146 if (!is_cpu_supported( req->cpu ))
1148 close( socket_fd );
1149 return;
1152 if (req->parent_process)
1154 if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
1156 close( socket_fd );
1157 return;
1159 parent_thread = NULL;
1161 else parent = (struct process *)grab_object( current->process );
1163 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1164 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1166 set_error( STATUS_ACCESS_DENIED );
1167 close( socket_fd );
1168 release_object( parent );
1169 return;
1172 /* build the startup info for a new process */
1173 if (!(info = alloc_object( &startup_info_ops )))
1175 close( socket_fd );
1176 release_object( parent );
1177 return;
1179 info->process = NULL;
1180 info->data = NULL;
1182 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1183 info->info_size = min( req->info_size, info->data_size );
1185 if (req->info_size < sizeof(*info->data))
1187 /* make sure we have a full startup_info_t structure */
1188 data_size_t env_size = info->data_size - info->info_size;
1189 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1191 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1193 close( socket_fd );
1194 goto done;
1196 memcpy( info->data, info_ptr, info_size );
1197 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1198 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1199 info->info_size = sizeof(startup_info_t);
1200 info->data_size = info->info_size + env_size;
1202 else
1204 data_size_t pos = sizeof(*info->data);
1206 if (!(info->data = memdup( info_ptr, info->data_size )))
1208 close( socket_fd );
1209 goto done;
1211 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1212 FIXUP_LEN( info->data->curdir_len );
1213 FIXUP_LEN( info->data->dllpath_len );
1214 FIXUP_LEN( info->data->imagepath_len );
1215 FIXUP_LEN( info->data->cmdline_len );
1216 FIXUP_LEN( info->data->title_len );
1217 FIXUP_LEN( info->data->desktop_len );
1218 FIXUP_LEN( info->data->shellinfo_len );
1219 FIXUP_LEN( info->data->runtime_len );
1220 #undef FIXUP_LEN
1223 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1225 process->startup_info = (struct startup_info *)grab_object( info );
1227 if (req->exe_file &&
1228 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1229 goto done;
1231 if (parent->job
1232 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1233 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1235 add_job_process( parent->job, process );
1238 /* connect to the window station */
1239 connect_process_winstation( process, parent_thread, parent );
1241 /* set the process console */
1242 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1244 /* FIXME: some better error checking should be done...
1245 * like if hConOut and hConIn are console handles, then they should be on the same
1246 * physical console
1248 inherit_console( parent_thread, parent, process, req->inherit_all ? info->data->hstdin : 0 );
1251 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1253 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1254 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1255 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1256 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1257 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1258 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1259 /* some handles above may have been invalid; this is not an error */
1260 if (get_error() == STATUS_INVALID_HANDLE ||
1261 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1263 /* attach to the debugger if requested */
1264 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1266 set_process_debugger( process, current );
1267 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1269 else if (current->process->debugger && current->process->debug_children)
1271 set_process_debugger( process, current->process->debugger );
1272 /* debug_children is set to 1 by default */
1275 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1276 process->group_id = parent->group_id;
1278 info->process = (struct process *)grab_object( process );
1279 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1280 reply->pid = get_process_id( process );
1281 reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
1283 done:
1284 if (process) release_object( process );
1285 release_object( parent );
1286 release_object( info );
1289 /* execute a new process, replacing the existing one */
1290 DECL_HANDLER(exec_process)
1292 struct process *process;
1293 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1295 if (socket_fd == -1)
1297 set_error( STATUS_INVALID_PARAMETER );
1298 return;
1300 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1302 set_error( STATUS_INVALID_HANDLE );
1303 close( socket_fd );
1304 return;
1306 if (shutdown_stage)
1308 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1309 close( socket_fd );
1310 return;
1312 if (!is_cpu_supported( req->cpu ))
1314 close( socket_fd );
1315 return;
1317 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1318 create_thread( -1, process, NULL );
1319 release_object( process );
1322 /* Retrieve information about a newly started process */
1323 DECL_HANDLER(get_new_process_info)
1325 struct startup_info *info;
1327 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1328 0, &startup_info_ops )))
1330 reply->success = is_process_init_done( info->process );
1331 reply->exit_code = info->process->exit_code;
1332 release_object( info );
1336 /* Retrieve the new process startup info */
1337 DECL_HANDLER(get_startup_info)
1339 struct process *process = current->process;
1340 struct startup_info *info = process->startup_info;
1341 data_size_t size;
1343 if (!info) return;
1345 /* we return the data directly without making a copy so this can only be called once */
1346 reply->info_size = info->info_size;
1347 size = info->data_size;
1348 if (size > get_reply_max_size()) size = get_reply_max_size();
1349 set_reply_data_ptr( info->data, size );
1350 info->data = NULL;
1351 info->data_size = 0;
1354 /* signal the end of the process initialization */
1355 DECL_HANDLER(init_process_done)
1357 struct process_dll *dll;
1358 struct process *process = current->process;
1360 if (is_process_init_done(process))
1362 set_error( STATUS_INVALID_PARAMETER );
1363 return;
1365 if (!(dll = find_process_dll( process, req->module )))
1367 set_error( STATUS_DLL_NOT_FOUND );
1368 return;
1371 /* main exe is the first in the dll list */
1372 list_remove( &dll->entry );
1373 list_add_head( &process->dlls, &dll->entry );
1375 process->ldt_copy = req->ldt_copy;
1376 process->start_time = current_time;
1377 current->entry_point = req->entry;
1378 if (process->exe_file) release_object( process->exe_file );
1379 process->exe_file = NULL;
1381 init_process_tracing( process );
1382 generate_startup_debug_events( process, req->entry );
1383 set_process_startup_state( process, STARTUP_DONE );
1385 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1386 if (process->debugger) set_process_debug_flag( process, 1 );
1387 reply->suspend = (current->suspend || process->suspend);
1390 /* open a handle to a process */
1391 DECL_HANDLER(open_process)
1393 struct process *process = get_process_from_id( req->pid );
1394 reply->handle = 0;
1395 if (process)
1397 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1398 release_object( process );
1402 /* terminate a process */
1403 DECL_HANDLER(terminate_process)
1405 struct process *process;
1407 if (req->handle)
1409 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1410 if (!process) return;
1412 else process = (struct process *)grab_object( current->process );
1414 reply->self = (current->process == process);
1415 terminate_process( process, current, req->exit_code );
1416 release_object( process );
1419 /* fetch information about a process */
1420 DECL_HANDLER(get_process_info)
1422 struct process *process;
1424 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1426 reply->pid = get_process_id( process );
1427 reply->ppid = process->parent_id;
1428 reply->exit_code = process->exit_code;
1429 reply->priority = process->priority;
1430 reply->affinity = process->affinity;
1431 reply->peb = process->peb;
1432 reply->start_time = process->start_time;
1433 reply->end_time = process->end_time;
1434 reply->cpu = process->cpu;
1435 reply->debugger_present = !!process->debugger;
1436 reply->debug_children = process->debug_children;
1437 release_object( process );
1441 /* retrieve information about a process memory usage */
1442 DECL_HANDLER(get_process_vm_counters)
1444 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1446 if (!process) return;
1447 #ifdef linux
1448 if (process->unix_pid != -1)
1450 FILE *f;
1451 char proc_path[32], line[256];
1452 unsigned long value;
1454 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1455 if ((f = fopen( proc_path, "r" )))
1457 while (fgets( line, sizeof(line), f ))
1459 if (sscanf( line, "VmPeak: %lu", &value ))
1460 reply->peak_virtual_size = (mem_size_t)value * 1024;
1461 else if (sscanf( line, "VmSize: %lu", &value ))
1462 reply->virtual_size = (mem_size_t)value * 1024;
1463 else if (sscanf( line, "VmHWM: %lu", &value ))
1464 reply->peak_working_set_size = (mem_size_t)value * 1024;
1465 else if (sscanf( line, "VmRSS: %lu", &value ))
1466 reply->working_set_size = (mem_size_t)value * 1024;
1467 else if (sscanf( line, "RssAnon: %lu", &value ))
1468 reply->pagefile_usage += (mem_size_t)value * 1024;
1469 else if (sscanf( line, "VmSwap: %lu", &value ))
1470 reply->pagefile_usage += (mem_size_t)value * 1024;
1472 reply->peak_pagefile_usage = reply->pagefile_usage;
1473 fclose( f );
1475 else set_error( STATUS_ACCESS_DENIED );
1477 else set_error( STATUS_ACCESS_DENIED );
1478 #endif
1479 release_object( process );
1482 static void set_process_affinity( struct process *process, affinity_t affinity )
1484 struct thread *thread;
1486 if (!process->running_threads)
1488 set_error( STATUS_PROCESS_IS_TERMINATING );
1489 return;
1492 process->affinity = affinity;
1494 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1496 set_thread_affinity( thread, affinity );
1500 /* set information about a process */
1501 DECL_HANDLER(set_process_info)
1503 struct process *process;
1505 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1507 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1508 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1509 release_object( process );
1513 /* read data from a process address space */
1514 DECL_HANDLER(read_process_memory)
1516 struct process *process;
1517 data_size_t len = get_reply_max_size();
1519 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1521 if (len)
1523 char *buffer = mem_alloc( len );
1524 if (buffer)
1526 if (read_process_memory( process, req->addr, len, buffer ))
1527 set_reply_data_ptr( buffer, len );
1528 else
1529 free( buffer );
1532 release_object( process );
1535 /* write data to a process address space */
1536 DECL_HANDLER(write_process_memory)
1538 struct process *process;
1540 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1542 data_size_t len = get_req_data_size();
1543 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1544 else set_error( STATUS_INVALID_PARAMETER );
1545 release_object( process );
1549 /* notify the server that a dll has been loaded */
1550 DECL_HANDLER(load_dll)
1552 struct process_dll *dll;
1554 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1556 dll->dbg_offset = req->dbg_offset;
1557 dll->dbg_size = req->dbg_size;
1558 dll->name = req->name;
1559 /* only generate event if initialization is done */
1560 if (is_process_init_done( current->process ))
1561 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1565 /* notify the server that a dll is being unloaded */
1566 DECL_HANDLER(unload_dll)
1568 process_unload_dll( current->process, req->base );
1571 /* retrieve information about a module in a process */
1572 DECL_HANDLER(get_dll_info)
1574 struct process *process;
1576 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1578 struct process_dll *dll;
1580 if (req->base_address)
1581 dll = find_process_dll( process, req->base_address );
1582 else /* NULL means main module */
1583 dll = list_head( &process->dlls ) ?
1584 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1586 if (dll)
1588 reply->entry_point = 0; /* FIXME */
1589 reply->filename_len = dll->namelen;
1590 if (dll->filename)
1592 if (dll->namelen <= get_reply_max_size())
1593 set_reply_data( dll->filename, dll->namelen );
1594 else
1595 set_error( STATUS_BUFFER_TOO_SMALL );
1598 else
1599 set_error( STATUS_DLL_NOT_FOUND );
1601 release_object( process );
1605 /* retrieve the process idle event */
1606 DECL_HANDLER(get_process_idle_event)
1608 struct process *process;
1610 reply->event = 0;
1611 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1613 if (process->idle_event && process != current->process)
1614 reply->event = alloc_handle( current->process, process->idle_event,
1615 EVENT_ALL_ACCESS, 0 );
1616 release_object( process );
1620 /* make the current process a system process */
1621 DECL_HANDLER(make_process_system)
1623 struct process *process = current->process;
1625 if (!shutdown_event)
1627 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1628 make_object_static( (struct object *)shutdown_event );
1631 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1632 return;
1634 if (!process->is_system)
1636 process->is_system = 1;
1637 close_process_desktop( process );
1638 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1639 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1643 /* create a new job object */
1644 DECL_HANDLER(create_job)
1646 struct job *job;
1647 struct unicode_str name;
1648 struct object *root;
1649 const struct security_descriptor *sd;
1650 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1652 if (!objattr) return;
1654 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1656 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1657 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1658 else
1659 reply->handle = alloc_handle_no_access_check( current->process, job,
1660 req->access, objattr->attributes );
1661 release_object( job );
1663 if (root) release_object( root );
1666 /* open a job object */
1667 DECL_HANDLER(open_job)
1669 struct unicode_str name = get_req_unicode_str();
1671 reply->handle = open_object( current->process, req->rootdir, req->access,
1672 &job_ops, &name, req->attributes );
1675 /* assign a job object to a process */
1676 DECL_HANDLER(assign_job)
1678 struct process *process;
1679 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1681 if (!job) return;
1683 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1685 if (!process->running_threads)
1686 set_error( STATUS_PROCESS_IS_TERMINATING );
1687 else if (process->job)
1688 set_error( STATUS_ACCESS_DENIED );
1689 else
1690 add_job_process( job, process );
1691 release_object( process );
1693 release_object( job );
1697 /* check if a process is associated with a job */
1698 DECL_HANDLER(process_in_job)
1700 struct process *process;
1701 struct job *job;
1703 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1704 return;
1706 if (!req->job)
1708 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1710 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1712 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1713 release_object( job );
1715 release_object( process );
1718 /* terminate all processes associated with the job */
1719 DECL_HANDLER(terminate_job)
1721 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1723 if (!job) return;
1725 terminate_job( job, req->status );
1726 release_object( job );
1729 /* update limits of the job object */
1730 DECL_HANDLER(set_job_limits)
1732 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1734 if (!job) return;
1736 job->limit_flags = req->limit_flags;
1737 release_object( job );
1740 /* set the jobs completion port */
1741 DECL_HANDLER(set_job_completion_port)
1743 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1745 if (!job) return;
1747 if (!job->completion_port)
1749 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1750 job->completion_key = req->key;
1752 else
1753 set_error( STATUS_INVALID_PARAMETER );
1755 release_object( job );
1758 /* Suspend a process */
1759 DECL_HANDLER(suspend_process)
1761 struct process *process;
1763 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1765 struct list *ptr, *next;
1767 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1769 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1770 suspend_thread( thread );
1773 release_object( process );
1777 /* Resume a process */
1778 DECL_HANDLER(resume_process)
1780 struct process *process;
1782 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1784 struct list *ptr, *next;
1786 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1788 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1789 resume_thread( thread );
1792 release_object( process );