shlwapi: SHMapHandle should not set error when NULL is passed as hShared.
[wine.git] / server / process.c
blob473d3b1a27a64fd135b669147087e9e9a4d7dedf
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 void process_poll_event( struct fd *fd, int event );
67 static void process_destroy( struct object *obj );
68 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
70 static const struct object_ops process_ops =
72 sizeof(struct process), /* size */
73 process_dump, /* dump */
74 process_get_type, /* get_type */
75 add_queue, /* add_queue */
76 remove_queue, /* remove_queue */
77 process_signaled, /* signaled */
78 no_satisfied, /* satisfied */
79 no_signal, /* signal */
80 no_get_fd, /* get_fd */
81 process_map_access, /* map_access */
82 default_get_sd, /* get_sd */
83 default_set_sd, /* set_sd */
84 no_lookup_name, /* lookup_name */
85 no_link_name, /* link_name */
86 NULL, /* unlink_name */
87 no_open_file, /* open_file */
88 no_kernel_obj_list, /* get_kernel_obj_list */
89 no_close_handle, /* close_handle */
90 process_destroy /* destroy */
93 static const struct fd_ops process_fd_ops =
95 NULL, /* get_poll_events */
96 process_poll_event, /* poll_event */
97 NULL, /* flush */
98 NULL, /* get_fd_type */
99 NULL, /* ioctl */
100 NULL, /* queue_async */
101 NULL, /* reselect_async */
102 NULL /* cancel async */
105 /* process startup info */
107 struct startup_info
109 struct object obj; /* object header */
110 struct process *process; /* created process */
111 data_size_t info_size; /* size of startup info */
112 data_size_t data_size; /* size of whole startup data */
113 startup_info_t *data; /* data for startup info */
116 static void startup_info_dump( struct object *obj, int verbose );
117 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
118 static void startup_info_destroy( struct object *obj );
120 static const struct object_ops startup_info_ops =
122 sizeof(struct startup_info), /* size */
123 startup_info_dump, /* dump */
124 no_get_type, /* get_type */
125 add_queue, /* add_queue */
126 remove_queue, /* remove_queue */
127 startup_info_signaled, /* signaled */
128 no_satisfied, /* satisfied */
129 no_signal, /* signal */
130 no_get_fd, /* get_fd */
131 no_map_access, /* map_access */
132 default_get_sd, /* get_sd */
133 default_set_sd, /* set_sd */
134 no_lookup_name, /* lookup_name */
135 no_link_name, /* link_name */
136 NULL, /* unlink_name */
137 no_open_file, /* open_file */
138 no_kernel_obj_list, /* get_kernel_obj_list */
139 no_close_handle, /* close_handle */
140 startup_info_destroy /* destroy */
143 /* job object */
145 static void job_dump( struct object *obj, int verbose );
146 static struct object_type *job_get_type( struct object *obj );
147 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
148 static unsigned int job_map_access( struct object *obj, unsigned int access );
149 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
150 static void job_destroy( struct object *obj );
152 struct job
154 struct object obj; /* object header */
155 struct list process_list; /* list of all processes */
156 int num_processes; /* count of running processes */
157 unsigned int limit_flags; /* limit flags */
158 int terminating; /* job is terminating */
159 int signaled; /* job is signaled */
160 struct completion *completion_port; /* associated completion port */
161 apc_param_t completion_key; /* key to send with completion messages */
164 static const struct object_ops job_ops =
166 sizeof(struct job), /* size */
167 job_dump, /* dump */
168 job_get_type, /* get_type */
169 add_queue, /* add_queue */
170 remove_queue, /* remove_queue */
171 job_signaled, /* signaled */
172 no_satisfied, /* satisfied */
173 no_signal, /* signal */
174 no_get_fd, /* get_fd */
175 job_map_access, /* map_access */
176 default_get_sd, /* get_sd */
177 default_set_sd, /* set_sd */
178 no_lookup_name, /* lookup_name */
179 directory_link_name, /* link_name */
180 default_unlink_name, /* unlink_name */
181 no_open_file, /* open_file */
182 no_kernel_obj_list, /* get_kernel_obj_list */
183 job_close_handle, /* close_handle */
184 job_destroy /* destroy */
187 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
188 unsigned int attr, const struct security_descriptor *sd )
190 struct job *job;
192 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
194 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
196 /* initialize it if it didn't already exist */
197 list_init( &job->process_list );
198 job->num_processes = 0;
199 job->limit_flags = 0;
200 job->terminating = 0;
201 job->signaled = 0;
202 job->completion_port = NULL;
203 job->completion_key = 0;
206 return job;
209 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
211 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
214 static struct object_type *job_get_type( struct object *obj )
216 static const WCHAR name[] = {'J','o','b'};
217 static const struct unicode_str str = { name, sizeof(name) };
218 return get_object_type( &str );
221 static unsigned int job_map_access( struct object *obj, unsigned int access )
223 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
224 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
225 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
226 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
227 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
230 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
232 if (job->completion_port)
233 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
236 static void add_job_process( struct job *job, struct process *process )
238 process->job = (struct job *)grab_object( job );
239 list_add_tail( &job->process_list, &process->job_entry );
240 job->num_processes++;
242 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
245 /* called when a process has terminated, allow one additional process */
246 static void release_job_process( struct process *process )
248 struct job *job = process->job;
250 if (!job) return;
252 assert( job->num_processes );
253 job->num_processes--;
255 if (!job->terminating)
256 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
258 if (!job->num_processes)
259 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
262 static void terminate_job( struct job *job, int exit_code )
264 /* don't report completion events for terminated processes */
265 job->terminating = 1;
267 for (;;) /* restart from the beginning of the list every time */
269 struct process *process;
271 /* find the first process associated with this job and still running */
272 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
274 if (process->running_threads) break;
276 if (&process->job_entry == &job->process_list) break; /* no process found */
277 assert( process->job == job );
278 terminate_process( process, NULL, exit_code );
281 job->terminating = 0;
282 job->signaled = 1;
283 wake_up( &job->obj, 0 );
286 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
288 struct job *job = (struct job *)obj;
289 assert( obj->ops == &job_ops );
291 if (obj->handle_count == 1) /* last handle */
293 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
294 terminate_job( job, 0 );
296 return 1;
299 static void job_destroy( struct object *obj )
301 struct job *job = (struct job *)obj;
302 assert( obj->ops == &job_ops );
304 assert( !job->num_processes );
305 assert( list_empty(&job->process_list) );
307 if (job->completion_port) release_object( job->completion_port );
310 static void job_dump( struct object *obj, int verbose )
312 struct job *job = (struct job *)obj;
313 assert( obj->ops == &job_ops );
314 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
317 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
319 struct job *job = (struct job *)obj;
320 return job->signaled;
323 struct ptid_entry
325 void *ptr; /* entry ptr */
326 unsigned int next; /* next free entry */
329 static struct ptid_entry *ptid_entries; /* array of ptid entries */
330 static unsigned int used_ptid_entries; /* number of entries in use */
331 static unsigned int alloc_ptid_entries; /* number of allocated entries */
332 static unsigned int next_free_ptid; /* next free entry */
333 static unsigned int last_free_ptid; /* last free entry */
334 static unsigned int num_free_ptids; /* number of free ptids */
336 static void kill_all_processes(void);
338 #define PTID_OFFSET 8 /* offset for first ptid value */
340 /* allocate a new process or thread id */
341 unsigned int alloc_ptid( void *ptr )
343 struct ptid_entry *entry;
344 unsigned int id;
346 if (used_ptid_entries < alloc_ptid_entries)
348 id = used_ptid_entries + PTID_OFFSET;
349 entry = &ptid_entries[used_ptid_entries++];
351 else if (next_free_ptid && num_free_ptids >= 256)
353 id = next_free_ptid;
354 entry = &ptid_entries[id - PTID_OFFSET];
355 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
356 num_free_ptids--;
358 else /* need to grow the array */
360 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
361 if (!count) count = 512;
362 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
364 set_error( STATUS_NO_MEMORY );
365 return 0;
367 ptid_entries = entry;
368 alloc_ptid_entries = count;
369 id = used_ptid_entries + PTID_OFFSET;
370 entry = &ptid_entries[used_ptid_entries++];
373 entry->ptr = ptr;
374 return id;
377 /* free a process or thread id */
378 void free_ptid( unsigned int id )
380 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
382 entry->ptr = NULL;
383 entry->next = 0;
385 /* append to end of free list so that we don't reuse it too early */
386 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
387 else next_free_ptid = id;
388 last_free_ptid = id;
389 num_free_ptids++;
392 /* retrieve the pointer corresponding to a process or thread id */
393 void *get_ptid_entry( unsigned int id )
395 if (id < PTID_OFFSET) return NULL;
396 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
397 return ptid_entries[id - PTID_OFFSET].ptr;
400 /* return the main thread of the process */
401 struct thread *get_process_first_thread( struct process *process )
403 struct list *ptr = list_head( &process->thread_list );
404 if (!ptr) return NULL;
405 return LIST_ENTRY( ptr, struct thread, proc_entry );
408 /* set the state of the process startup info */
409 static void set_process_startup_state( struct process *process, enum startup_state state )
411 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
412 if (process->startup_info)
414 wake_up( &process->startup_info->obj, 0 );
415 release_object( process->startup_info );
416 process->startup_info = NULL;
420 /* callback for server shutdown */
421 static void server_shutdown_timeout( void *arg )
423 shutdown_timeout = NULL;
424 if (!running_processes)
426 close_master_socket( 0 );
427 return;
429 switch(++shutdown_stage)
431 case 1: /* signal system processes to exit */
432 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
433 if (shutdown_event) set_event( shutdown_event );
434 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
435 close_master_socket( 4 * -TICKS_PER_SEC );
436 break;
437 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
438 kill_all_processes();
439 break;
443 /* forced shutdown, used for wineserver -k */
444 void shutdown_master_socket(void)
446 kill_all_processes();
447 shutdown_stage = 2;
448 if (shutdown_timeout)
450 remove_timeout_user( shutdown_timeout );
451 shutdown_timeout = NULL;
453 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
456 /* final cleanup once we are sure a process is really dead */
457 static void process_died( struct process *process )
459 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
460 if (!process->is_system)
462 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
463 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
465 release_object( process );
466 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
469 /* callback for process sigkill timeout */
470 static void process_sigkill( void *private )
472 struct process *process = private;
474 process->sigkill_timeout = NULL;
475 kill( process->unix_pid, SIGKILL );
476 process_died( process );
479 /* start the sigkill timer for a process upon exit */
480 static void start_sigkill_timer( struct process *process )
482 grab_object( process );
483 if (process->unix_pid != -1 && process->msg_fd)
484 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
485 else
486 process_died( process );
489 /* create a new process */
490 /* if the function fails the fd is closed */
491 struct process *create_process( int fd, struct process *parent, int inherit_all,
492 const struct security_descriptor *sd )
494 struct process *process;
496 if (!(process = alloc_object( &process_ops )))
498 close( fd );
499 goto error;
501 process->parent_id = 0;
502 process->debugger = NULL;
503 process->handles = NULL;
504 process->msg_fd = NULL;
505 process->sigkill_timeout = NULL;
506 process->unix_pid = -1;
507 process->exit_code = STILL_ACTIVE;
508 process->running_threads = 0;
509 process->priority = PROCESS_PRIOCLASS_NORMAL;
510 process->suspend = 0;
511 process->is_system = 0;
512 process->debug_children = 1;
513 process->is_terminating = 0;
514 process->job = NULL;
515 process->console = NULL;
516 process->startup_state = STARTUP_IN_PROGRESS;
517 process->startup_info = NULL;
518 process->idle_event = NULL;
519 process->exe_file = NULL;
520 process->peb = 0;
521 process->ldt_copy = 0;
522 process->dir_cache = NULL;
523 process->winstation = 0;
524 process->desktop = 0;
525 process->token = NULL;
526 process->trace_data = 0;
527 process->rawinput_mouse = NULL;
528 process->rawinput_kbd = NULL;
529 list_init( &process->thread_list );
530 list_init( &process->locks );
531 list_init( &process->asyncs );
532 list_init( &process->classes );
533 list_init( &process->views );
534 list_init( &process->dlls );
535 list_init( &process->rawinput_devices );
537 process->end_time = 0;
538 list_add_tail( &process_list, &process->entry );
540 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
541 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
543 close( fd );
544 goto error;
546 if (!(process->id = process->group_id = alloc_ptid( process )))
548 close( fd );
549 goto error;
551 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
553 /* create the handle table */
554 if (!parent)
556 process->handles = alloc_handle_table( process, 0 );
557 process->token = token_create_admin();
558 process->affinity = ~0;
560 else
562 process->parent_id = parent->id;
563 process->handles = inherit_all ? copy_handle_table( process, parent )
564 : alloc_handle_table( process, 0 );
565 /* Note: for security reasons, starting a new process does not attempt
566 * to use the current impersonation token for the new process */
567 process->token = token_duplicate( parent->token, TRUE, 0, NULL );
568 process->affinity = parent->affinity;
570 if (!process->handles || !process->token) goto error;
572 /* Assign a high security label to the token. The default would be medium
573 * but Wine provides admin access to all applications right now so high
574 * makes more sense for the time being. */
575 if (!token_assign_label( process->token, security_high_label_sid ))
576 goto error;
578 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
579 return process;
581 error:
582 if (process) release_object( process );
583 /* if we failed to start our first process, close everything down */
584 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
585 return NULL;
588 /* initialize the current process and fill in the request */
589 data_size_t init_process( struct thread *thread )
591 struct process *process = thread->process;
592 struct startup_info *info = process->startup_info;
594 if (!info) return 0;
595 return info->data_size;
598 /* destroy a process when its refcount is 0 */
599 static void process_destroy( struct object *obj )
601 struct process *process = (struct process *)obj;
602 assert( obj->ops == &process_ops );
604 /* we can't have a thread remaining */
605 assert( list_empty( &process->thread_list ));
606 assert( list_empty( &process->asyncs ));
608 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
610 close_process_handles( process );
611 set_process_startup_state( process, STARTUP_ABORTED );
613 if (process->job)
615 list_remove( &process->job_entry );
616 release_object( process->job );
618 if (process->console) release_object( process->console );
619 if (process->msg_fd) release_object( process->msg_fd );
620 list_remove( &process->entry );
621 if (process->idle_event) release_object( process->idle_event );
622 if (process->exe_file) release_object( process->exe_file );
623 if (process->id) free_ptid( process->id );
624 if (process->token) release_object( process->token );
625 free( process->dir_cache );
628 /* dump a process on stdout for debugging purposes */
629 static void process_dump( struct object *obj, int verbose )
631 struct process *process = (struct process *)obj;
632 assert( obj->ops == &process_ops );
634 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
637 static struct object_type *process_get_type( struct object *obj )
639 static const WCHAR name[] = {'P','r','o','c','e','s','s'};
640 static const struct unicode_str str = { name, sizeof(name) };
641 return get_object_type( &str );
644 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
646 struct process *process = (struct process *)obj;
647 return !process->running_threads;
650 static unsigned int process_map_access( struct object *obj, unsigned int access )
652 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
653 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
654 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
655 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
656 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
658 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
659 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
661 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
664 static void process_poll_event( struct fd *fd, int event )
666 struct process *process = get_fd_user( fd );
667 assert( process->obj.ops == &process_ops );
669 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
670 else if (event & POLLIN) receive_fd( process );
673 static void startup_info_destroy( struct object *obj )
675 struct startup_info *info = (struct startup_info *)obj;
676 assert( obj->ops == &startup_info_ops );
677 free( info->data );
678 if (info->process) release_object( info->process );
681 static void startup_info_dump( struct object *obj, int verbose )
683 struct startup_info *info = (struct startup_info *)obj;
684 assert( obj->ops == &startup_info_ops );
686 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
687 info->data->hstdin, info->data->hstdout, info->data->hstderr );
690 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
692 struct startup_info *info = (struct startup_info *)obj;
693 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
696 /* get a process from an id (and increment the refcount) */
697 struct process *get_process_from_id( process_id_t id )
699 struct object *obj = get_ptid_entry( id );
701 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
702 set_error( STATUS_INVALID_PARAMETER );
703 return NULL;
706 /* get a process from a handle (and increment the refcount) */
707 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
709 return (struct process *)get_handle_obj( current->process, handle,
710 access, &process_ops );
713 /* find a dll from its base address */
714 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
716 struct process_dll *dll;
718 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
720 if (dll->base == base) return dll;
722 return NULL;
725 /* add a dll to a process list */
726 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
727 const WCHAR *filename, data_size_t name_len )
729 struct process_dll *dll;
731 /* make sure we don't already have one with the same base address */
732 if (find_process_dll( process, base ))
734 set_error( STATUS_INVALID_PARAMETER );
735 return NULL;
738 if ((dll = mem_alloc( sizeof(*dll) )))
740 dll->base = base;
741 dll->filename = NULL;
742 dll->namelen = name_len;
743 if (name_len && !(dll->filename = memdup( filename, name_len )))
745 free( dll );
746 return NULL;
748 list_add_tail( &process->dlls, &dll->entry );
750 return dll;
753 /* remove a dll from a process list */
754 static void process_unload_dll( struct process *process, mod_handle_t base )
756 struct process_dll *dll = find_process_dll( process, base );
758 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
760 free( dll->filename );
761 list_remove( &dll->entry );
762 free( dll );
763 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
765 else set_error( STATUS_INVALID_PARAMETER );
768 /* terminate a process with the given exit code */
769 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
771 struct thread *thread;
773 grab_object( process ); /* make sure it doesn't get freed when threads die */
774 process->is_terminating = 1;
776 restart:
777 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
779 if (exit_code) thread->exit_code = exit_code;
780 if (thread == skip) continue;
781 if (thread->state == TERMINATED) continue;
782 kill_thread( thread, 1 );
783 goto restart;
785 release_object( process );
788 /* kill all processes */
789 static void kill_all_processes(void)
791 for (;;)
793 struct process *process;
795 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
797 if (process->running_threads) break;
799 if (&process->entry == &process_list) break; /* no process found */
800 terminate_process( process, NULL, 1 );
804 /* kill all processes being attached to a console renderer */
805 void kill_console_processes( struct thread *renderer, int exit_code )
807 for (;;) /* restart from the beginning of the list every time */
809 struct process *process;
811 /* find the first process being attached to 'renderer' and still running */
812 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
814 if (process == renderer->process) continue;
815 if (!process->running_threads) continue;
816 if (process->console && console_get_renderer( process->console ) == renderer) break;
818 if (&process->entry == &process_list) break; /* no process found */
819 terminate_process( process, NULL, exit_code );
823 /* a process has been killed (i.e. its last thread died) */
824 static void process_killed( struct process *process )
826 struct list *ptr;
828 assert( list_empty( &process->thread_list ));
829 process->end_time = current_time;
830 if (!process->is_system) close_process_desktop( process );
831 process->winstation = 0;
832 process->desktop = 0;
833 close_process_handles( process );
834 cancel_process_asyncs( process );
835 if (process->idle_event) release_object( process->idle_event );
836 if (process->exe_file) release_object( process->exe_file );
837 process->idle_event = NULL;
838 process->exe_file = NULL;
840 /* close the console attached to this process, if any */
841 free_console( process );
843 while ((ptr = list_head( &process->rawinput_devices )))
845 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
846 list_remove( &entry->entry );
847 free( entry );
849 while ((ptr = list_head( &process->dlls )))
851 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
852 free( dll->filename );
853 list_remove( &dll->entry );
854 free( dll );
856 destroy_process_classes( process );
857 free_mapped_views( process );
858 free_process_user_handles( process );
859 remove_process_locks( process );
860 set_process_startup_state( process, STARTUP_ABORTED );
861 finish_process_tracing( process );
862 release_job_process( process );
863 start_sigkill_timer( process );
864 wake_up( &process->obj, 0 );
867 /* add a thread to a process running threads list */
868 void add_process_thread( struct process *process, struct thread *thread )
870 list_add_tail( &process->thread_list, &thread->proc_entry );
871 if (!process->running_threads++)
873 running_processes++;
874 if (!process->is_system)
876 if (!user_processes++ && shutdown_timeout)
878 remove_timeout_user( shutdown_timeout );
879 shutdown_timeout = NULL;
883 grab_object( thread );
886 /* remove a thread from a process running threads list */
887 void remove_process_thread( struct process *process, struct thread *thread )
889 assert( process->running_threads > 0 );
890 assert( !list_empty( &process->thread_list ));
892 list_remove( &thread->proc_entry );
894 if (!--process->running_threads)
896 /* we have removed the last running thread, exit the process */
897 process->exit_code = thread->exit_code;
898 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
899 process_killed( process );
901 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
902 release_object( thread );
905 /* suspend all the threads of a process */
906 void suspend_process( struct process *process )
908 if (!process->suspend++)
910 struct list *ptr, *next;
912 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
914 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
915 if (!thread->suspend) stop_thread( thread );
920 /* resume all the threads of a process */
921 void resume_process( struct process *process )
923 assert (process->suspend > 0);
924 if (!--process->suspend)
926 struct list *ptr, *next;
928 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
930 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
931 if (!thread->suspend) wake_thread( thread );
936 /* kill a process on the spot */
937 void kill_process( struct process *process, int violent_death )
939 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
941 release_object( process->msg_fd );
942 process->msg_fd = NULL;
945 if (process->sigkill_timeout) /* already waiting for it to die */
947 remove_timeout_user( process->sigkill_timeout );
948 process->sigkill_timeout = NULL;
949 process_died( process );
950 return;
953 if (violent_death) terminate_process( process, NULL, 1 );
954 else
956 struct list *ptr;
958 grab_object( process ); /* make sure it doesn't get freed when threads die */
959 while ((ptr = list_head( &process->thread_list )))
961 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
962 kill_thread( thread, 0 );
964 release_object( process );
968 /* kill all processes being debugged by a given thread */
969 void kill_debugged_processes( struct thread *debugger, int exit_code )
971 for (;;) /* restart from the beginning of the list every time */
973 struct process *process;
975 /* find the first process being debugged by 'debugger' and still running */
976 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
978 if (!process->running_threads) continue;
979 if (process->debugger == debugger) break;
981 if (&process->entry == &process_list) break; /* no process found */
982 process->debugger = NULL;
983 terminate_process( process, NULL, exit_code );
988 /* trigger a breakpoint event in a given process */
989 void break_process( struct process *process )
991 struct thread *thread;
993 suspend_process( process );
995 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
997 if (thread->context) /* inside an exception event already */
999 break_thread( thread );
1000 goto done;
1003 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1004 else set_error( STATUS_ACCESS_DENIED );
1005 done:
1006 resume_process( process );
1010 /* detach a debugger from all its debuggees */
1011 void detach_debugged_processes( struct thread *debugger )
1013 struct process *process;
1015 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1017 if (process->debugger == debugger && process->running_threads)
1019 debugger_detach( process, debugger );
1025 void enum_processes( int (*cb)(struct process*, void*), void *user )
1027 struct list *ptr, *next;
1029 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1031 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1032 if ((cb)(process, user)) break;
1036 /* set the debugged flag in the process PEB */
1037 int set_process_debug_flag( struct process *process, int flag )
1039 char data = (flag != 0);
1041 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1042 return write_process_memory( process, process->peb + 2, 1, &data );
1045 /* take a snapshot of currently running processes */
1046 struct process_snapshot *process_snap( int *count )
1048 struct process_snapshot *snapshot, *ptr;
1049 struct process *process;
1051 if (!running_processes) return NULL;
1052 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1053 return NULL;
1054 ptr = snapshot;
1055 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1057 if (!process->running_threads) continue;
1058 ptr->process = process;
1059 ptr->threads = process->running_threads;
1060 ptr->count = process->obj.refcount;
1061 ptr->priority = process->priority;
1062 ptr->handles = get_handle_table_count(process);
1063 grab_object( process );
1064 ptr++;
1067 if (!(*count = ptr - snapshot))
1069 free( snapshot );
1070 snapshot = NULL;
1072 return snapshot;
1075 /* create a new process */
1076 DECL_HANDLER(new_process)
1078 struct startup_info *info;
1079 const void *info_ptr;
1080 struct unicode_str name;
1081 const struct security_descriptor *sd;
1082 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1083 struct process *process = NULL;
1084 struct process *parent = current->process;
1085 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1087 if (socket_fd == -1)
1089 set_error( STATUS_INVALID_PARAMETER );
1090 return;
1092 if (!objattr)
1094 set_error( STATUS_INVALID_PARAMETER );
1095 close( socket_fd );
1096 return;
1098 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1100 set_error( STATUS_INVALID_HANDLE );
1101 close( socket_fd );
1102 return;
1104 if (shutdown_stage)
1106 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1107 close( socket_fd );
1108 return;
1110 if (!is_cpu_supported( req->cpu ))
1112 close( socket_fd );
1113 return;
1116 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1117 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1119 set_error( STATUS_ACCESS_DENIED );
1120 close( socket_fd );
1121 return;
1124 /* build the startup info for a new process */
1125 if (!(info = alloc_object( &startup_info_ops )))
1127 close( socket_fd );
1128 return;
1130 info->process = NULL;
1131 info->data = NULL;
1133 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1134 info->info_size = min( req->info_size, info->data_size );
1136 if (req->info_size < sizeof(*info->data))
1138 /* make sure we have a full startup_info_t structure */
1139 data_size_t env_size = info->data_size - info->info_size;
1140 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1142 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1144 close( socket_fd );
1145 goto done;
1147 memcpy( info->data, info_ptr, info_size );
1148 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1149 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1150 info->info_size = sizeof(startup_info_t);
1151 info->data_size = info->info_size + env_size;
1153 else
1155 data_size_t pos = sizeof(*info->data);
1157 if (!(info->data = memdup( info_ptr, info->data_size )))
1159 close( socket_fd );
1160 goto done;
1162 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1163 FIXUP_LEN( info->data->curdir_len );
1164 FIXUP_LEN( info->data->dllpath_len );
1165 FIXUP_LEN( info->data->imagepath_len );
1166 FIXUP_LEN( info->data->cmdline_len );
1167 FIXUP_LEN( info->data->title_len );
1168 FIXUP_LEN( info->data->desktop_len );
1169 FIXUP_LEN( info->data->shellinfo_len );
1170 FIXUP_LEN( info->data->runtime_len );
1171 #undef FIXUP_LEN
1174 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1176 process->startup_info = (struct startup_info *)grab_object( info );
1178 if (req->exe_file &&
1179 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1180 goto done;
1182 if (parent->job
1183 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1184 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1186 add_job_process( parent->job, process );
1189 /* connect to the window station */
1190 connect_process_winstation( process, current );
1192 /* set the process console */
1193 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1195 /* FIXME: some better error checking should be done...
1196 * like if hConOut and hConIn are console handles, then they should be on the same
1197 * physical console
1199 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1202 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1204 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1205 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1206 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1207 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1208 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1209 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1210 /* some handles above may have been invalid; this is not an error */
1211 if (get_error() == STATUS_INVALID_HANDLE ||
1212 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1215 /* attach to the debugger if requested */
1216 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1218 set_process_debugger( process, current );
1219 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1221 else if (parent->debugger && parent->debug_children)
1223 set_process_debugger( process, parent->debugger );
1224 /* debug_children is set to 1 by default */
1227 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1228 process->group_id = parent->group_id;
1230 info->process = (struct process *)grab_object( process );
1231 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1232 reply->pid = get_process_id( process );
1233 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1235 done:
1236 if (process) release_object( process );
1237 release_object( info );
1240 /* execute a new process, replacing the existing one */
1241 DECL_HANDLER(exec_process)
1243 struct process *process;
1244 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1246 if (socket_fd == -1)
1248 set_error( STATUS_INVALID_PARAMETER );
1249 return;
1251 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1253 set_error( STATUS_INVALID_HANDLE );
1254 close( socket_fd );
1255 return;
1257 if (shutdown_stage)
1259 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1260 close( socket_fd );
1261 return;
1263 if (!is_cpu_supported( req->cpu ))
1265 close( socket_fd );
1266 return;
1268 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1269 create_thread( -1, process, NULL );
1270 release_object( process );
1273 /* Retrieve information about a newly started process */
1274 DECL_HANDLER(get_new_process_info)
1276 struct startup_info *info;
1278 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1279 0, &startup_info_ops )))
1281 reply->success = is_process_init_done( info->process );
1282 reply->exit_code = info->process->exit_code;
1283 release_object( info );
1287 /* Retrieve the new process startup info */
1288 DECL_HANDLER(get_startup_info)
1290 struct process *process = current->process;
1291 struct startup_info *info = process->startup_info;
1292 data_size_t size;
1294 if (!info) return;
1296 /* we return the data directly without making a copy so this can only be called once */
1297 reply->info_size = info->info_size;
1298 size = info->data_size;
1299 if (size > get_reply_max_size()) size = get_reply_max_size();
1300 set_reply_data_ptr( info->data, size );
1301 info->data = NULL;
1302 info->data_size = 0;
1305 /* signal the end of the process initialization */
1306 DECL_HANDLER(init_process_done)
1308 struct process_dll *dll;
1309 struct process *process = current->process;
1311 if (is_process_init_done(process))
1313 set_error( STATUS_INVALID_PARAMETER );
1314 return;
1316 if (!(dll = find_process_dll( process, req->module )))
1318 set_error( STATUS_DLL_NOT_FOUND );
1319 return;
1322 /* main exe is the first in the dll list */
1323 list_remove( &dll->entry );
1324 list_add_head( &process->dlls, &dll->entry );
1326 process->ldt_copy = req->ldt_copy;
1327 process->start_time = current_time;
1328 current->entry_point = req->entry;
1329 if (process->exe_file) release_object( process->exe_file );
1330 process->exe_file = NULL;
1332 init_process_tracing( process );
1333 generate_startup_debug_events( process, req->entry );
1334 set_process_startup_state( process, STARTUP_DONE );
1336 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1337 if (process->debugger) set_process_debug_flag( process, 1 );
1338 reply->suspend = (current->suspend || process->suspend);
1341 /* open a handle to a process */
1342 DECL_HANDLER(open_process)
1344 struct process *process = get_process_from_id( req->pid );
1345 reply->handle = 0;
1346 if (process)
1348 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1349 release_object( process );
1353 /* terminate a process */
1354 DECL_HANDLER(terminate_process)
1356 struct process *process;
1358 if (req->handle)
1360 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1361 if (!process) return;
1363 else process = (struct process *)grab_object( current->process );
1365 reply->self = (current->process == process);
1366 terminate_process( process, current, req->exit_code );
1367 release_object( process );
1370 /* fetch information about a process */
1371 DECL_HANDLER(get_process_info)
1373 struct process *process;
1375 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1377 reply->pid = get_process_id( process );
1378 reply->ppid = process->parent_id;
1379 reply->exit_code = process->exit_code;
1380 reply->priority = process->priority;
1381 reply->affinity = process->affinity;
1382 reply->peb = process->peb;
1383 reply->start_time = process->start_time;
1384 reply->end_time = process->end_time;
1385 reply->cpu = process->cpu;
1386 reply->debugger_present = !!process->debugger;
1387 reply->debug_children = process->debug_children;
1388 release_object( process );
1392 /* retrieve information about a process memory usage */
1393 DECL_HANDLER(get_process_vm_counters)
1395 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1397 if (!process) return;
1398 #ifdef linux
1399 if (process->unix_pid != -1)
1401 FILE *f;
1402 char proc_path[32], line[256];
1403 unsigned long value;
1405 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1406 if ((f = fopen( proc_path, "r" )))
1408 while (fgets( line, sizeof(line), f ))
1410 if (sscanf( line, "VmPeak: %lu", &value ))
1411 reply->peak_virtual_size = (mem_size_t)value * 1024;
1412 else if (sscanf( line, "VmSize: %lu", &value ))
1413 reply->virtual_size = (mem_size_t)value * 1024;
1414 else if (sscanf( line, "VmHWM: %lu", &value ))
1415 reply->peak_working_set_size = (mem_size_t)value * 1024;
1416 else if (sscanf( line, "VmRSS: %lu", &value ))
1417 reply->working_set_size = (mem_size_t)value * 1024;
1418 else if (sscanf( line, "RssAnon: %lu", &value ))
1419 reply->pagefile_usage += (mem_size_t)value * 1024;
1420 else if (sscanf( line, "VmSwap: %lu", &value ))
1421 reply->pagefile_usage += (mem_size_t)value * 1024;
1423 reply->peak_pagefile_usage = reply->pagefile_usage;
1424 fclose( f );
1426 else set_error( STATUS_ACCESS_DENIED );
1428 else set_error( STATUS_ACCESS_DENIED );
1429 #endif
1430 release_object( process );
1433 static void set_process_affinity( struct process *process, affinity_t affinity )
1435 struct thread *thread;
1437 if (!process->running_threads)
1439 set_error( STATUS_PROCESS_IS_TERMINATING );
1440 return;
1443 process->affinity = affinity;
1445 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1447 set_thread_affinity( thread, affinity );
1451 /* set information about a process */
1452 DECL_HANDLER(set_process_info)
1454 struct process *process;
1456 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1458 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1459 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1460 release_object( process );
1464 /* read data from a process address space */
1465 DECL_HANDLER(read_process_memory)
1467 struct process *process;
1468 data_size_t len = get_reply_max_size();
1470 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1472 if (len)
1474 char *buffer = mem_alloc( len );
1475 if (buffer)
1477 if (read_process_memory( process, req->addr, len, buffer ))
1478 set_reply_data_ptr( buffer, len );
1479 else
1480 free( buffer );
1483 release_object( process );
1486 /* write data to a process address space */
1487 DECL_HANDLER(write_process_memory)
1489 struct process *process;
1491 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1493 data_size_t len = get_req_data_size();
1494 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1495 else set_error( STATUS_INVALID_PARAMETER );
1496 release_object( process );
1500 /* notify the server that a dll has been loaded */
1501 DECL_HANDLER(load_dll)
1503 struct process_dll *dll;
1505 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1507 dll->dbg_offset = req->dbg_offset;
1508 dll->dbg_size = req->dbg_size;
1509 dll->name = req->name;
1510 /* only generate event if initialization is done */
1511 if (is_process_init_done( current->process ))
1512 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1516 /* notify the server that a dll is being unloaded */
1517 DECL_HANDLER(unload_dll)
1519 process_unload_dll( current->process, req->base );
1522 /* retrieve information about a module in a process */
1523 DECL_HANDLER(get_dll_info)
1525 struct process *process;
1527 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1529 struct process_dll *dll;
1531 if (req->base_address)
1532 dll = find_process_dll( process, req->base_address );
1533 else /* NULL means main module */
1534 dll = list_head( &process->dlls ) ?
1535 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1537 if (dll)
1539 reply->entry_point = 0; /* FIXME */
1540 reply->filename_len = dll->namelen;
1541 if (dll->filename)
1543 if (dll->namelen <= get_reply_max_size())
1544 set_reply_data( dll->filename, dll->namelen );
1545 else
1546 set_error( STATUS_BUFFER_TOO_SMALL );
1549 else
1550 set_error( STATUS_DLL_NOT_FOUND );
1552 release_object( process );
1556 /* retrieve the process idle event */
1557 DECL_HANDLER(get_process_idle_event)
1559 struct process *process;
1561 reply->event = 0;
1562 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1564 if (process->idle_event && process != current->process)
1565 reply->event = alloc_handle( current->process, process->idle_event,
1566 EVENT_ALL_ACCESS, 0 );
1567 release_object( process );
1571 /* make the current process a system process */
1572 DECL_HANDLER(make_process_system)
1574 struct process *process = current->process;
1576 if (!shutdown_event)
1578 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1579 make_object_static( (struct object *)shutdown_event );
1582 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1583 return;
1585 if (!process->is_system)
1587 process->is_system = 1;
1588 close_process_desktop( process );
1589 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1590 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1594 /* create a new job object */
1595 DECL_HANDLER(create_job)
1597 struct job *job;
1598 struct unicode_str name;
1599 struct object *root;
1600 const struct security_descriptor *sd;
1601 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1603 if (!objattr) return;
1605 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1607 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1608 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1609 else
1610 reply->handle = alloc_handle_no_access_check( current->process, job,
1611 req->access, objattr->attributes );
1612 release_object( job );
1614 if (root) release_object( root );
1617 /* open a job object */
1618 DECL_HANDLER(open_job)
1620 struct unicode_str name = get_req_unicode_str();
1622 reply->handle = open_object( current->process, req->rootdir, req->access,
1623 &job_ops, &name, req->attributes );
1626 /* assign a job object to a process */
1627 DECL_HANDLER(assign_job)
1629 struct process *process;
1630 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1632 if (!job) return;
1634 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1636 if (!process->running_threads)
1637 set_error( STATUS_PROCESS_IS_TERMINATING );
1638 else if (process->job)
1639 set_error( STATUS_ACCESS_DENIED );
1640 else
1641 add_job_process( job, process );
1642 release_object( process );
1644 release_object( job );
1648 /* check if a process is associated with a job */
1649 DECL_HANDLER(process_in_job)
1651 struct process *process;
1652 struct job *job;
1654 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1655 return;
1657 if (!req->job)
1659 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1661 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1663 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1664 release_object( job );
1666 release_object( process );
1669 /* terminate all processes associated with the job */
1670 DECL_HANDLER(terminate_job)
1672 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1674 if (!job) return;
1676 terminate_job( job, req->status );
1677 release_object( job );
1680 /* update limits of the job object */
1681 DECL_HANDLER(set_job_limits)
1683 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1685 if (!job) return;
1687 job->limit_flags = req->limit_flags;
1688 release_object( job );
1691 /* set the jobs completion port */
1692 DECL_HANDLER(set_job_completion_port)
1694 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1696 if (!job) return;
1698 if (!job->completion_port)
1700 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1701 job->completion_key = req->key;
1703 else
1704 set_error( STATUS_INVALID_PARAMETER );
1706 release_object( job );