wined3d: Acquire a context for the front buffer in swapchain_gl_present().
[wine.git] / server / process.c
blobb67bd88da47536c79902d032ff442c365a5389ce
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 /* detach a debugger from all its debuggees */
1045 void detach_debugged_processes( struct thread *debugger )
1047 struct process *process;
1049 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1051 if (process->debugger == debugger && process->running_threads)
1053 debugger_detach( process, debugger );
1059 void enum_processes( int (*cb)(struct process*, void*), void *user )
1061 struct list *ptr, *next;
1063 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1065 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1066 if ((cb)(process, user)) break;
1070 /* set the debugged flag in the process PEB */
1071 int set_process_debug_flag( struct process *process, int flag )
1073 char data = (flag != 0);
1075 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1076 return write_process_memory( process, process->peb + 2, 1, &data );
1079 /* take a snapshot of currently running processes */
1080 struct process_snapshot *process_snap( int *count )
1082 struct process_snapshot *snapshot, *ptr;
1083 struct process *process;
1085 if (!running_processes) return NULL;
1086 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1087 return NULL;
1088 ptr = snapshot;
1089 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1091 if (!process->running_threads) continue;
1092 ptr->process = process;
1093 ptr->threads = process->running_threads;
1094 ptr->count = process->obj.refcount;
1095 ptr->priority = process->priority;
1096 ptr->handles = get_handle_table_count(process);
1097 grab_object( process );
1098 ptr++;
1101 if (!(*count = ptr - snapshot))
1103 free( snapshot );
1104 snapshot = NULL;
1106 return snapshot;
1109 /* create a new process */
1110 DECL_HANDLER(new_process)
1112 struct startup_info *info;
1113 const void *info_ptr;
1114 struct unicode_str name;
1115 const struct security_descriptor *sd;
1116 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1117 struct process *process = NULL;
1118 struct process *parent = current->process;
1119 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1121 if (socket_fd == -1)
1123 set_error( STATUS_INVALID_PARAMETER );
1124 return;
1126 if (!objattr)
1128 set_error( STATUS_INVALID_PARAMETER );
1129 close( socket_fd );
1130 return;
1132 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1134 set_error( STATUS_INVALID_HANDLE );
1135 close( socket_fd );
1136 return;
1138 if (shutdown_stage)
1140 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1141 close( socket_fd );
1142 return;
1144 if (!is_cpu_supported( req->cpu ))
1146 close( socket_fd );
1147 return;
1150 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1151 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1153 set_error( STATUS_ACCESS_DENIED );
1154 close( socket_fd );
1155 return;
1158 /* build the startup info for a new process */
1159 if (!(info = alloc_object( &startup_info_ops )))
1161 close( socket_fd );
1162 return;
1164 info->process = NULL;
1165 info->data = NULL;
1167 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1168 info->info_size = min( req->info_size, info->data_size );
1170 if (req->info_size < sizeof(*info->data))
1172 /* make sure we have a full startup_info_t structure */
1173 data_size_t env_size = info->data_size - info->info_size;
1174 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1176 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1178 close( socket_fd );
1179 goto done;
1181 memcpy( info->data, info_ptr, info_size );
1182 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1183 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1184 info->info_size = sizeof(startup_info_t);
1185 info->data_size = info->info_size + env_size;
1187 else
1189 data_size_t pos = sizeof(*info->data);
1191 if (!(info->data = memdup( info_ptr, info->data_size )))
1193 close( socket_fd );
1194 goto done;
1196 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1197 FIXUP_LEN( info->data->curdir_len );
1198 FIXUP_LEN( info->data->dllpath_len );
1199 FIXUP_LEN( info->data->imagepath_len );
1200 FIXUP_LEN( info->data->cmdline_len );
1201 FIXUP_LEN( info->data->title_len );
1202 FIXUP_LEN( info->data->desktop_len );
1203 FIXUP_LEN( info->data->shellinfo_len );
1204 FIXUP_LEN( info->data->runtime_len );
1205 #undef FIXUP_LEN
1208 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1210 process->startup_info = (struct startup_info *)grab_object( info );
1212 if (req->exe_file &&
1213 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1214 goto done;
1216 if (parent->job
1217 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1218 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1220 add_job_process( parent->job, process );
1223 /* connect to the window station */
1224 connect_process_winstation( process, current );
1226 /* set the process console */
1227 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1229 /* FIXME: some better error checking should be done...
1230 * like if hConOut and hConIn are console handles, then they should be on the same
1231 * physical console
1233 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1236 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1238 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1239 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1240 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1241 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1242 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1243 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1244 /* some handles above may have been invalid; this is not an error */
1245 if (get_error() == STATUS_INVALID_HANDLE ||
1246 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1249 /* attach to the debugger if requested */
1250 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1252 set_process_debugger( process, current );
1253 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1255 else if (parent->debugger && parent->debug_children)
1257 set_process_debugger( process, parent->debugger );
1258 /* debug_children is set to 1 by default */
1261 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1262 process->group_id = parent->group_id;
1264 info->process = (struct process *)grab_object( process );
1265 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1266 reply->pid = get_process_id( process );
1267 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1269 done:
1270 if (process) release_object( process );
1271 release_object( info );
1274 /* execute a new process, replacing the existing one */
1275 DECL_HANDLER(exec_process)
1277 struct process *process;
1278 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1280 if (socket_fd == -1)
1282 set_error( STATUS_INVALID_PARAMETER );
1283 return;
1285 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1287 set_error( STATUS_INVALID_HANDLE );
1288 close( socket_fd );
1289 return;
1291 if (shutdown_stage)
1293 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1294 close( socket_fd );
1295 return;
1297 if (!is_cpu_supported( req->cpu ))
1299 close( socket_fd );
1300 return;
1302 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1303 create_thread( -1, process, NULL );
1304 release_object( process );
1307 /* Retrieve information about a newly started process */
1308 DECL_HANDLER(get_new_process_info)
1310 struct startup_info *info;
1312 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1313 0, &startup_info_ops )))
1315 reply->success = is_process_init_done( info->process );
1316 reply->exit_code = info->process->exit_code;
1317 release_object( info );
1321 /* Retrieve the new process startup info */
1322 DECL_HANDLER(get_startup_info)
1324 struct process *process = current->process;
1325 struct startup_info *info = process->startup_info;
1326 data_size_t size;
1328 if (!info) return;
1330 /* we return the data directly without making a copy so this can only be called once */
1331 reply->info_size = info->info_size;
1332 size = info->data_size;
1333 if (size > get_reply_max_size()) size = get_reply_max_size();
1334 set_reply_data_ptr( info->data, size );
1335 info->data = NULL;
1336 info->data_size = 0;
1339 /* signal the end of the process initialization */
1340 DECL_HANDLER(init_process_done)
1342 struct process_dll *dll;
1343 struct process *process = current->process;
1345 if (is_process_init_done(process))
1347 set_error( STATUS_INVALID_PARAMETER );
1348 return;
1350 if (!(dll = find_process_dll( process, req->module )))
1352 set_error( STATUS_DLL_NOT_FOUND );
1353 return;
1356 /* main exe is the first in the dll list */
1357 list_remove( &dll->entry );
1358 list_add_head( &process->dlls, &dll->entry );
1360 process->ldt_copy = req->ldt_copy;
1361 process->start_time = current_time;
1362 current->entry_point = req->entry;
1363 if (process->exe_file) release_object( process->exe_file );
1364 process->exe_file = NULL;
1366 init_process_tracing( process );
1367 generate_startup_debug_events( process, req->entry );
1368 set_process_startup_state( process, STARTUP_DONE );
1370 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1371 if (process->debugger) set_process_debug_flag( process, 1 );
1372 reply->suspend = (current->suspend || process->suspend);
1375 /* open a handle to a process */
1376 DECL_HANDLER(open_process)
1378 struct process *process = get_process_from_id( req->pid );
1379 reply->handle = 0;
1380 if (process)
1382 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1383 release_object( process );
1387 /* terminate a process */
1388 DECL_HANDLER(terminate_process)
1390 struct process *process;
1392 if (req->handle)
1394 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1395 if (!process) return;
1397 else process = (struct process *)grab_object( current->process );
1399 reply->self = (current->process == process);
1400 terminate_process( process, current, req->exit_code );
1401 release_object( process );
1404 /* fetch information about a process */
1405 DECL_HANDLER(get_process_info)
1407 struct process *process;
1409 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1411 reply->pid = get_process_id( process );
1412 reply->ppid = process->parent_id;
1413 reply->exit_code = process->exit_code;
1414 reply->priority = process->priority;
1415 reply->affinity = process->affinity;
1416 reply->peb = process->peb;
1417 reply->start_time = process->start_time;
1418 reply->end_time = process->end_time;
1419 reply->cpu = process->cpu;
1420 reply->debugger_present = !!process->debugger;
1421 reply->debug_children = process->debug_children;
1422 release_object( process );
1426 /* retrieve information about a process memory usage */
1427 DECL_HANDLER(get_process_vm_counters)
1429 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1431 if (!process) return;
1432 #ifdef linux
1433 if (process->unix_pid != -1)
1435 FILE *f;
1436 char proc_path[32], line[256];
1437 unsigned long value;
1439 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1440 if ((f = fopen( proc_path, "r" )))
1442 while (fgets( line, sizeof(line), f ))
1444 if (sscanf( line, "VmPeak: %lu", &value ))
1445 reply->peak_virtual_size = (mem_size_t)value * 1024;
1446 else if (sscanf( line, "VmSize: %lu", &value ))
1447 reply->virtual_size = (mem_size_t)value * 1024;
1448 else if (sscanf( line, "VmHWM: %lu", &value ))
1449 reply->peak_working_set_size = (mem_size_t)value * 1024;
1450 else if (sscanf( line, "VmRSS: %lu", &value ))
1451 reply->working_set_size = (mem_size_t)value * 1024;
1452 else if (sscanf( line, "RssAnon: %lu", &value ))
1453 reply->pagefile_usage += (mem_size_t)value * 1024;
1454 else if (sscanf( line, "VmSwap: %lu", &value ))
1455 reply->pagefile_usage += (mem_size_t)value * 1024;
1457 reply->peak_pagefile_usage = reply->pagefile_usage;
1458 fclose( f );
1460 else set_error( STATUS_ACCESS_DENIED );
1462 else set_error( STATUS_ACCESS_DENIED );
1463 #endif
1464 release_object( process );
1467 static void set_process_affinity( struct process *process, affinity_t affinity )
1469 struct thread *thread;
1471 if (!process->running_threads)
1473 set_error( STATUS_PROCESS_IS_TERMINATING );
1474 return;
1477 process->affinity = affinity;
1479 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1481 set_thread_affinity( thread, affinity );
1485 /* set information about a process */
1486 DECL_HANDLER(set_process_info)
1488 struct process *process;
1490 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1492 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1493 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1494 release_object( process );
1498 /* read data from a process address space */
1499 DECL_HANDLER(read_process_memory)
1501 struct process *process;
1502 data_size_t len = get_reply_max_size();
1504 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1506 if (len)
1508 char *buffer = mem_alloc( len );
1509 if (buffer)
1511 if (read_process_memory( process, req->addr, len, buffer ))
1512 set_reply_data_ptr( buffer, len );
1513 else
1514 free( buffer );
1517 release_object( process );
1520 /* write data to a process address space */
1521 DECL_HANDLER(write_process_memory)
1523 struct process *process;
1525 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1527 data_size_t len = get_req_data_size();
1528 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1529 else set_error( STATUS_INVALID_PARAMETER );
1530 release_object( process );
1534 /* notify the server that a dll has been loaded */
1535 DECL_HANDLER(load_dll)
1537 struct process_dll *dll;
1539 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1541 dll->dbg_offset = req->dbg_offset;
1542 dll->dbg_size = req->dbg_size;
1543 dll->name = req->name;
1544 /* only generate event if initialization is done */
1545 if (is_process_init_done( current->process ))
1546 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1550 /* notify the server that a dll is being unloaded */
1551 DECL_HANDLER(unload_dll)
1553 process_unload_dll( current->process, req->base );
1556 /* retrieve information about a module in a process */
1557 DECL_HANDLER(get_dll_info)
1559 struct process *process;
1561 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1563 struct process_dll *dll;
1565 if (req->base_address)
1566 dll = find_process_dll( process, req->base_address );
1567 else /* NULL means main module */
1568 dll = list_head( &process->dlls ) ?
1569 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1571 if (dll)
1573 reply->entry_point = 0; /* FIXME */
1574 reply->filename_len = dll->namelen;
1575 if (dll->filename)
1577 if (dll->namelen <= get_reply_max_size())
1578 set_reply_data( dll->filename, dll->namelen );
1579 else
1580 set_error( STATUS_BUFFER_TOO_SMALL );
1583 else
1584 set_error( STATUS_DLL_NOT_FOUND );
1586 release_object( process );
1590 /* retrieve the process idle event */
1591 DECL_HANDLER(get_process_idle_event)
1593 struct process *process;
1595 reply->event = 0;
1596 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1598 if (process->idle_event && process != current->process)
1599 reply->event = alloc_handle( current->process, process->idle_event,
1600 EVENT_ALL_ACCESS, 0 );
1601 release_object( process );
1605 /* make the current process a system process */
1606 DECL_HANDLER(make_process_system)
1608 struct process *process = current->process;
1610 if (!shutdown_event)
1612 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1613 make_object_static( (struct object *)shutdown_event );
1616 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1617 return;
1619 if (!process->is_system)
1621 process->is_system = 1;
1622 close_process_desktop( process );
1623 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1624 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1628 /* create a new job object */
1629 DECL_HANDLER(create_job)
1631 struct job *job;
1632 struct unicode_str name;
1633 struct object *root;
1634 const struct security_descriptor *sd;
1635 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1637 if (!objattr) return;
1639 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1641 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1642 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1643 else
1644 reply->handle = alloc_handle_no_access_check( current->process, job,
1645 req->access, objattr->attributes );
1646 release_object( job );
1648 if (root) release_object( root );
1651 /* open a job object */
1652 DECL_HANDLER(open_job)
1654 struct unicode_str name = get_req_unicode_str();
1656 reply->handle = open_object( current->process, req->rootdir, req->access,
1657 &job_ops, &name, req->attributes );
1660 /* assign a job object to a process */
1661 DECL_HANDLER(assign_job)
1663 struct process *process;
1664 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1666 if (!job) return;
1668 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1670 if (!process->running_threads)
1671 set_error( STATUS_PROCESS_IS_TERMINATING );
1672 else if (process->job)
1673 set_error( STATUS_ACCESS_DENIED );
1674 else
1675 add_job_process( job, process );
1676 release_object( process );
1678 release_object( job );
1682 /* check if a process is associated with a job */
1683 DECL_HANDLER(process_in_job)
1685 struct process *process;
1686 struct job *job;
1688 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1689 return;
1691 if (!req->job)
1693 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1695 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1697 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1698 release_object( job );
1700 release_object( process );
1703 /* terminate all processes associated with the job */
1704 DECL_HANDLER(terminate_job)
1706 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1708 if (!job) return;
1710 terminate_job( job, req->status );
1711 release_object( job );
1714 /* update limits of the job object */
1715 DECL_HANDLER(set_job_limits)
1717 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1719 if (!job) return;
1721 job->limit_flags = req->limit_flags;
1722 release_object( job );
1725 /* set the jobs completion port */
1726 DECL_HANDLER(set_job_completion_port)
1728 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1730 if (!job) return;
1732 if (!job->completion_port)
1734 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1735 job->completion_key = req->key;
1737 else
1738 set_error( STATUS_INVALID_PARAMETER );
1740 release_object( job );
1743 /* Suspend a process */
1744 DECL_HANDLER(suspend_process)
1746 struct process *process;
1748 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1750 struct list *ptr, *next;
1752 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1754 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1755 suspend_thread( thread );
1758 release_object( process );
1762 /* Resume a process */
1763 DECL_HANDLER(resume_process)
1765 struct process *process;
1767 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1769 struct list *ptr, *next;
1771 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1773 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1774 resume_thread( thread );
1777 release_object( process );