xaudio2: Use an HRESULT return code.
[wine.git] / server / process.c
blobce26947fc20e75cfdb6328fafac1fc406aca3ead
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 int process_signaled( struct object *obj, struct wait_queue_entry *entry );
64 static unsigned int process_map_access( struct object *obj, unsigned int access );
65 static void process_poll_event( struct fd *fd, int event );
66 static void process_destroy( struct object *obj );
67 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
69 static const struct object_ops process_ops =
71 sizeof(struct process), /* size */
72 process_dump, /* dump */
73 no_get_type, /* get_type */
74 add_queue, /* add_queue */
75 remove_queue, /* remove_queue */
76 process_signaled, /* signaled */
77 no_satisfied, /* satisfied */
78 no_signal, /* signal */
79 no_get_fd, /* get_fd */
80 process_map_access, /* map_access */
81 default_get_sd, /* get_sd */
82 default_set_sd, /* set_sd */
83 no_lookup_name, /* lookup_name */
84 no_open_file, /* open_file */
85 no_close_handle, /* close_handle */
86 process_destroy /* destroy */
89 static const struct fd_ops process_fd_ops =
91 NULL, /* get_poll_events */
92 process_poll_event, /* poll_event */
93 NULL, /* flush */
94 NULL, /* get_fd_type */
95 NULL, /* ioctl */
96 NULL, /* queue_async */
97 NULL, /* reselect_async */
98 NULL /* cancel async */
101 /* process startup info */
103 struct startup_info
105 struct object obj; /* object header */
106 struct file *exe_file; /* file handle for main exe */
107 struct process *process; /* created process */
108 data_size_t info_size; /* size of startup info */
109 data_size_t data_size; /* size of whole startup data */
110 startup_info_t *data; /* data for startup info */
113 static void startup_info_dump( struct object *obj, int verbose );
114 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
115 static void startup_info_destroy( struct object *obj );
117 static const struct object_ops startup_info_ops =
119 sizeof(struct startup_info), /* size */
120 startup_info_dump, /* dump */
121 no_get_type, /* get_type */
122 add_queue, /* add_queue */
123 remove_queue, /* remove_queue */
124 startup_info_signaled, /* signaled */
125 no_satisfied, /* satisfied */
126 no_signal, /* signal */
127 no_get_fd, /* get_fd */
128 no_map_access, /* map_access */
129 default_get_sd, /* get_sd */
130 default_set_sd, /* set_sd */
131 no_lookup_name, /* lookup_name */
132 no_open_file, /* open_file */
133 no_close_handle, /* close_handle */
134 startup_info_destroy /* destroy */
137 /* job object */
139 static void job_dump( struct object *obj, int verbose );
140 static struct object_type *job_get_type( struct object *obj );
141 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
142 static unsigned int job_map_access( struct object *obj, unsigned int access );
143 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
144 static void job_destroy( struct object *obj );
146 struct job
148 struct object obj; /* object header */
149 struct list process_list; /* list of all processes */
150 int num_processes; /* count of running processes */
151 unsigned int limit_flags; /* limit flags */
152 int terminating; /* job is terminating */
153 int signaled; /* job is signaled */
154 struct completion *completion_port; /* associated completion port */
155 apc_param_t completion_key; /* key to send with completion messages */
158 static const struct object_ops job_ops =
160 sizeof(struct job), /* size */
161 job_dump, /* dump */
162 job_get_type, /* get_type */
163 add_queue, /* add_queue */
164 remove_queue, /* remove_queue */
165 job_signaled, /* signaled */
166 no_satisfied, /* satisfied */
167 no_signal, /* signal */
168 no_get_fd, /* get_fd */
169 job_map_access, /* map_access */
170 default_get_sd, /* get_sd */
171 default_set_sd, /* set_sd */
172 no_lookup_name, /* lookup_name */
173 no_open_file, /* open_file */
174 job_close_handle, /* close_handle */
175 job_destroy /* destroy */
178 static struct job *create_job_object( struct directory *root, const struct unicode_str *name,
179 unsigned int attr, const struct security_descriptor *sd )
181 struct job *job;
183 if ((job = create_named_object_dir( root, name, attr, &job_ops )))
185 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
187 /* initialize it if it didn't already exist */
188 if (sd) default_set_sd( &job->obj, sd, OWNER_SECURITY_INFORMATION |
189 GROUP_SECURITY_INFORMATION |
190 DACL_SECURITY_INFORMATION |
191 SACL_SECURITY_INFORMATION );
192 list_init( &job->process_list );
193 job->num_processes = 0;
194 job->limit_flags = 0;
195 job->terminating = 0;
196 job->signaled = 0;
197 job->completion_port = NULL;
198 job->completion_key = 0;
201 return job;
204 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
206 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
209 static struct object_type *job_get_type( struct object *obj )
211 static const WCHAR name[] = {'J','o','b'};
212 static const struct unicode_str str = { name, sizeof(name) };
213 return get_object_type( &str );
216 static unsigned int job_map_access( struct object *obj, unsigned int access )
218 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
219 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
220 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
221 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
222 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
225 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
227 if (job->completion_port)
228 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
231 static void add_job_process( struct job *job, struct process *process )
233 if (!process->running_threads)
235 set_error( STATUS_PROCESS_IS_TERMINATING );
236 return;
238 if (process->job)
240 set_error( STATUS_ACCESS_DENIED );
241 return;
243 process->job = (struct job *)grab_object( job );
244 list_add_tail( &job->process_list, &process->job_entry );
245 job->num_processes++;
247 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
250 /* called when a process has terminated, allow one additional process */
251 static void release_job_process( struct process *process )
253 struct job *job = process->job;
255 if (!job) return;
257 assert( job->num_processes );
258 job->num_processes--;
260 if (!job->terminating)
261 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
263 if (!job->num_processes)
264 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
267 static void terminate_job( struct job *job, int exit_code )
269 /* don't report completion events for terminated processes */
270 job->terminating = 1;
272 for (;;) /* restart from the beginning of the list every time */
274 struct process *process;
276 /* find the first process associated with this job and still running */
277 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
279 if (process->running_threads) break;
281 if (&process->job_entry == &job->process_list) break; /* no process found */
282 assert( process->job == job );
283 terminate_process( process, NULL, exit_code );
286 job->terminating = 0;
287 job->signaled = 1;
288 wake_up( &job->obj, 0 );
291 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
293 struct job *job = (struct job *)obj;
294 assert( obj->ops == &job_ops );
296 if (obj->handle_count == 1) /* last handle */
298 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
299 terminate_job( job, 0 );
301 return 1;
304 static void job_destroy( struct object *obj )
306 struct job *job = (struct job *)obj;
307 assert( obj->ops == &job_ops );
309 assert( !job->num_processes );
310 assert( list_empty(&job->process_list) );
312 if (job->completion_port) release_object( job->completion_port );
315 static void job_dump( struct object *obj, int verbose )
317 struct job *job = (struct job *)obj;
318 assert( obj->ops == &job_ops );
319 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
322 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
324 struct job *job = (struct job *)obj;
325 return job->signaled;
328 struct ptid_entry
330 void *ptr; /* entry ptr */
331 unsigned int next; /* next free entry */
334 static struct ptid_entry *ptid_entries; /* array of ptid entries */
335 static unsigned int used_ptid_entries; /* number of entries in use */
336 static unsigned int alloc_ptid_entries; /* number of allocated entries */
337 static unsigned int next_free_ptid; /* next free entry */
338 static unsigned int last_free_ptid; /* last free entry */
340 static void kill_all_processes(void);
342 #define PTID_OFFSET 8 /* offset for first ptid value */
344 /* allocate a new process or thread id */
345 unsigned int alloc_ptid( void *ptr )
347 struct ptid_entry *entry;
348 unsigned int id;
350 if (used_ptid_entries < alloc_ptid_entries)
352 id = used_ptid_entries + PTID_OFFSET;
353 entry = &ptid_entries[used_ptid_entries++];
355 else if (next_free_ptid)
357 id = next_free_ptid;
358 entry = &ptid_entries[id - PTID_OFFSET];
359 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
361 else /* need to grow the array */
363 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
364 if (!count) count = 64;
365 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
367 set_error( STATUS_NO_MEMORY );
368 return 0;
370 ptid_entries = entry;
371 alloc_ptid_entries = count;
372 id = used_ptid_entries + PTID_OFFSET;
373 entry = &ptid_entries[used_ptid_entries++];
376 entry->ptr = ptr;
377 return id;
380 /* free a process or thread id */
381 void free_ptid( unsigned int id )
383 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
385 entry->ptr = NULL;
386 entry->next = 0;
388 /* append to end of free list so that we don't reuse it too early */
389 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
390 else next_free_ptid = id;
392 last_free_ptid = id;
395 /* retrieve the pointer corresponding to a process or thread id */
396 void *get_ptid_entry( unsigned int id )
398 if (id < PTID_OFFSET) return NULL;
399 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
400 return ptid_entries[id - PTID_OFFSET].ptr;
403 /* return the main thread of the process */
404 struct thread *get_process_first_thread( struct process *process )
406 struct list *ptr = list_head( &process->thread_list );
407 if (!ptr) return NULL;
408 return LIST_ENTRY( ptr, struct thread, proc_entry );
411 /* set the state of the process startup info */
412 static void set_process_startup_state( struct process *process, enum startup_state state )
414 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
415 if (process->startup_info)
417 wake_up( &process->startup_info->obj, 0 );
418 release_object( process->startup_info );
419 process->startup_info = NULL;
423 /* callback for server shutdown */
424 static void server_shutdown_timeout( void *arg )
426 shutdown_timeout = NULL;
427 if (!running_processes)
429 close_master_socket( 0 );
430 return;
432 switch(++shutdown_stage)
434 case 1: /* signal system processes to exit */
435 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
436 if (shutdown_event) set_event( shutdown_event );
437 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
438 close_master_socket( 4 * -TICKS_PER_SEC );
439 break;
440 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
441 kill_all_processes();
442 break;
446 /* forced shutdown, used for wineserver -k */
447 void shutdown_master_socket(void)
449 kill_all_processes();
450 shutdown_stage = 2;
451 if (shutdown_timeout)
453 remove_timeout_user( shutdown_timeout );
454 shutdown_timeout = NULL;
456 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
459 /* final cleanup once we are sure a process is really dead */
460 static void process_died( struct process *process )
462 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
463 if (!process->is_system)
465 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
466 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
468 release_object( process );
469 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
472 /* callback for process sigkill timeout */
473 static void process_sigkill( void *private )
475 struct process *process = private;
477 process->sigkill_timeout = NULL;
478 kill( process->unix_pid, SIGKILL );
479 process_died( process );
482 /* start the sigkill timer for a process upon exit */
483 static void start_sigkill_timer( struct process *process )
485 grab_object( process );
486 if (process->unix_pid != -1 && process->msg_fd)
487 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
488 else
489 process_died( process );
492 /* create a new process and its main thread */
493 /* if the function fails the fd is closed */
494 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
496 struct process *process;
497 struct thread *thread = NULL;
498 int request_pipe[2];
500 if (!(process = alloc_object( &process_ops )))
502 close( fd );
503 goto error;
505 process->parent = NULL;
506 process->debugger = NULL;
507 process->handles = NULL;
508 process->msg_fd = NULL;
509 process->sigkill_timeout = NULL;
510 process->unix_pid = -1;
511 process->exit_code = STILL_ACTIVE;
512 process->running_threads = 0;
513 process->priority = PROCESS_PRIOCLASS_NORMAL;
514 process->suspend = 0;
515 process->is_system = 0;
516 process->debug_children = 1;
517 process->is_terminating = 0;
518 process->job = NULL;
519 process->console = NULL;
520 process->startup_state = STARTUP_IN_PROGRESS;
521 process->startup_info = NULL;
522 process->idle_event = NULL;
523 process->peb = 0;
524 process->ldt_copy = 0;
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->thread_list );
532 list_init( &process->locks );
533 list_init( &process->classes );
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 (!(process->id = process->group_id = alloc_ptid( process )))
542 close( fd );
543 goto error;
545 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
547 /* create the handle table */
548 if (!parent_thread)
550 process->handles = alloc_handle_table( process, 0 );
551 process->token = token_create_admin();
552 process->affinity = ~0;
554 else
556 struct process *parent = parent_thread->process;
557 process->parent = (struct process *)grab_object( parent );
558 process->handles = inherit_all ? copy_handle_table( process, parent )
559 : alloc_handle_table( process, 0 );
560 /* Note: for security reasons, starting a new process does not attempt
561 * to use the current impersonation token for the new process */
562 process->token = token_duplicate( parent->token, TRUE, 0 );
563 process->affinity = parent->affinity;
565 if (!process->handles || !process->token) goto error;
567 /* create the main thread */
568 if (pipe( request_pipe ) == -1)
570 file_set_error();
571 goto error;
573 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
575 close( request_pipe[0] );
576 close( request_pipe[1] );
577 goto error;
579 close( request_pipe[1] );
580 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
582 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
583 release_object( process );
584 return thread;
586 error:
587 if (process) release_object( process );
588 /* if we failed to start our first process, close everything down */
589 if (!running_processes) close_master_socket( 0 );
590 return NULL;
593 /* initialize the current process and fill in the request */
594 data_size_t init_process( struct thread *thread )
596 struct process *process = thread->process;
597 struct startup_info *info = process->startup_info;
599 init_process_tracing( process );
600 if (!info) return 0;
601 return info->data_size;
604 /* destroy a process when its refcount is 0 */
605 static void process_destroy( struct object *obj )
607 struct process *process = (struct process *)obj;
608 assert( obj->ops == &process_ops );
610 /* we can't have a thread remaining */
611 assert( list_empty( &process->thread_list ));
613 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
615 close_process_handles( process );
616 set_process_startup_state( process, STARTUP_ABORTED );
618 if (process->job)
620 list_remove( &process->job_entry );
621 release_object( process->job );
623 if (process->console) release_object( process->console );
624 if (process->parent) release_object( process->parent );
625 if (process->msg_fd) release_object( process->msg_fd );
626 list_remove( &process->entry );
627 if (process->idle_event) release_object( process->idle_event );
628 if (process->id) free_ptid( process->id );
629 if (process->token) release_object( process->token );
632 /* dump a process on stdout for debugging purposes */
633 static void process_dump( struct object *obj, int verbose )
635 struct process *process = (struct process *)obj;
636 assert( obj->ops == &process_ops );
638 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
641 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
643 struct process *process = (struct process *)obj;
644 return !process->running_threads;
647 static unsigned int process_map_access( struct object *obj, unsigned int access )
649 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
650 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
651 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
652 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
653 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
655 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
656 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
658 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
661 static void process_poll_event( struct fd *fd, int event )
663 struct process *process = get_fd_user( fd );
664 assert( process->obj.ops == &process_ops );
666 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
667 else if (event & POLLIN) receive_fd( process );
670 static void startup_info_destroy( struct object *obj )
672 struct startup_info *info = (struct startup_info *)obj;
673 assert( obj->ops == &startup_info_ops );
674 free( info->data );
675 if (info->exe_file) release_object( info->exe_file );
676 if (info->process) release_object( info->process );
679 static void startup_info_dump( struct object *obj, int verbose )
681 struct startup_info *info = (struct startup_info *)obj;
682 assert( obj->ops == &startup_info_ops );
684 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
685 info->data->hstdin, info->data->hstdout, info->data->hstderr );
688 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
690 struct startup_info *info = (struct startup_info *)obj;
691 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
694 /* get a process from an id (and increment the refcount) */
695 struct process *get_process_from_id( process_id_t id )
697 struct object *obj = get_ptid_entry( id );
699 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
700 set_error( STATUS_INVALID_PARAMETER );
701 return NULL;
704 /* get a process from a handle (and increment the refcount) */
705 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
707 return (struct process *)get_handle_obj( current->process, handle,
708 access, &process_ops );
711 /* find a dll from its base address */
712 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
714 struct process_dll *dll;
716 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
718 if (dll->base == base) return dll;
720 return NULL;
723 /* add a dll to a process list */
724 static struct process_dll *process_load_dll( struct process *process, struct mapping *mapping,
725 mod_handle_t base, const WCHAR *filename,
726 data_size_t name_len )
728 struct process_dll *dll;
730 /* make sure we don't already have one with the same base address */
731 if (find_process_dll( process, base ))
733 set_error( STATUS_INVALID_PARAMETER );
734 return NULL;
737 if ((dll = mem_alloc( sizeof(*dll) )))
739 dll->mapping = NULL;
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 if (mapping) dll->mapping = grab_mapping_unless_removable( mapping );
749 list_add_tail( &process->dlls, &dll->entry );
751 return dll;
754 /* remove a dll from a process list */
755 static void process_unload_dll( struct process *process, mod_handle_t base )
757 struct process_dll *dll = find_process_dll( process, base );
759 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
761 if (dll->mapping) release_object( dll->mapping );
762 free( dll->filename );
763 list_remove( &dll->entry );
764 free( dll );
765 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
767 else set_error( STATUS_INVALID_PARAMETER );
770 /* terminate a process with the given exit code */
771 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
773 struct thread *thread;
775 grab_object( process ); /* make sure it doesn't get freed when threads die */
776 process->is_terminating = 1;
778 restart:
779 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
781 if (exit_code) thread->exit_code = exit_code;
782 if (thread == skip) continue;
783 if (thread->state == TERMINATED) continue;
784 kill_thread( thread, 1 );
785 goto restart;
787 release_object( process );
790 /* kill all processes */
791 static void kill_all_processes(void)
793 for (;;)
795 struct process *process;
797 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
799 if (process->running_threads) break;
801 if (&process->entry == &process_list) break; /* no process found */
802 terminate_process( process, NULL, 1 );
806 /* kill all processes being attached to a console renderer */
807 void kill_console_processes( struct thread *renderer, int exit_code )
809 for (;;) /* restart from the beginning of the list every time */
811 struct process *process;
813 /* find the first process being attached to 'renderer' and still running */
814 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
816 if (process == renderer->process) continue;
817 if (!process->running_threads) continue;
818 if (process->console && console_get_renderer( process->console ) == renderer) break;
820 if (&process->entry == &process_list) break; /* no process found */
821 terminate_process( process, NULL, exit_code );
825 /* a process has been killed (i.e. its last thread died) */
826 static void process_killed( struct process *process )
828 struct list *ptr;
830 assert( list_empty( &process->thread_list ));
831 process->end_time = current_time;
832 if (!process->is_system) close_process_desktop( process );
833 process->winstation = 0;
834 process->desktop = 0;
835 close_process_handles( process );
836 if (process->idle_event)
838 release_object( process->idle_event );
839 process->idle_event = NULL;
842 /* close the console attached to this process, if any */
843 free_console( process );
845 while ((ptr = list_head( &process->rawinput_devices )))
847 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
848 list_remove( &entry->entry );
849 free( entry );
851 while ((ptr = list_head( &process->dlls )))
853 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
854 if (dll->mapping) release_object( dll->mapping );
855 free( dll->filename );
856 list_remove( &dll->entry );
857 free( dll );
859 destroy_process_classes( process );
860 free_process_user_handles( process );
861 remove_process_locks( process );
862 set_process_startup_state( process, STARTUP_ABORTED );
863 finish_process_tracing( process );
864 release_job_process( process );
865 start_sigkill_timer( process );
866 wake_up( &process->obj, 0 );
869 /* add a thread to a process running threads list */
870 void add_process_thread( struct process *process, struct thread *thread )
872 list_add_tail( &process->thread_list, &thread->proc_entry );
873 if (!process->running_threads++)
875 running_processes++;
876 if (!process->is_system)
878 if (!user_processes++ && shutdown_timeout)
880 remove_timeout_user( shutdown_timeout );
881 shutdown_timeout = NULL;
885 grab_object( thread );
888 /* remove a thread from a process running threads list */
889 void remove_process_thread( struct process *process, struct thread *thread )
891 assert( process->running_threads > 0 );
892 assert( !list_empty( &process->thread_list ));
894 list_remove( &thread->proc_entry );
896 if (!--process->running_threads)
898 /* we have removed the last running thread, exit the process */
899 process->exit_code = thread->exit_code;
900 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
901 process_killed( process );
903 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
904 release_object( thread );
907 /* suspend all the threads of a process */
908 void suspend_process( struct process *process )
910 if (!process->suspend++)
912 struct list *ptr, *next;
914 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
916 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
917 if (!thread->suspend) stop_thread( thread );
922 /* resume all the threads of a process */
923 void resume_process( struct process *process )
925 assert (process->suspend > 0);
926 if (!--process->suspend)
928 struct list *ptr, *next;
930 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
932 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
933 if (!thread->suspend) wake_thread( thread );
938 /* kill a process on the spot */
939 void kill_process( struct process *process, int violent_death )
941 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
943 release_object( process->msg_fd );
944 process->msg_fd = NULL;
947 if (process->sigkill_timeout) /* already waiting for it to die */
949 remove_timeout_user( process->sigkill_timeout );
950 process->sigkill_timeout = NULL;
951 process_died( process );
952 return;
955 if (violent_death) terminate_process( process, NULL, 1 );
956 else
958 struct list *ptr;
960 grab_object( process ); /* make sure it doesn't get freed when threads die */
961 while ((ptr = list_head( &process->thread_list )))
963 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
964 kill_thread( thread, 0 );
966 release_object( process );
970 /* kill all processes being debugged by a given thread */
971 void kill_debugged_processes( struct thread *debugger, int exit_code )
973 for (;;) /* restart from the beginning of the list every time */
975 struct process *process;
977 /* find the first process being debugged by 'debugger' and still running */
978 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
980 if (!process->running_threads) continue;
981 if (process->debugger == debugger) break;
983 if (&process->entry == &process_list) break; /* no process found */
984 process->debugger = NULL;
985 terminate_process( process, NULL, exit_code );
990 /* trigger a breakpoint event in a given process */
991 void break_process( struct process *process )
993 struct thread *thread;
995 suspend_process( process );
997 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
999 if (thread->context) /* inside an exception event already */
1001 break_thread( thread );
1002 goto done;
1005 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1006 else set_error( STATUS_ACCESS_DENIED );
1007 done:
1008 resume_process( process );
1012 /* detach a debugger from all its debuggees */
1013 void detach_debugged_processes( struct thread *debugger )
1015 struct process *process;
1017 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1019 if (process->debugger == debugger && process->running_threads)
1021 debugger_detach( process, debugger );
1027 void enum_processes( int (*cb)(struct process*, void*), void *user )
1029 struct list *ptr, *next;
1031 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1033 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1034 if ((cb)(process, user)) break;
1038 /* set the debugged flag in the process PEB */
1039 int set_process_debug_flag( struct process *process, int flag )
1041 char data = (flag != 0);
1043 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1044 return write_process_memory( process, process->peb + 2, 1, &data );
1047 /* take a snapshot of currently running processes */
1048 struct process_snapshot *process_snap( int *count )
1050 struct process_snapshot *snapshot, *ptr;
1051 struct process *process;
1053 if (!running_processes) return NULL;
1054 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1055 return NULL;
1056 ptr = snapshot;
1057 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1059 if (!process->running_threads) continue;
1060 ptr->process = process;
1061 ptr->threads = process->running_threads;
1062 ptr->count = process->obj.refcount;
1063 ptr->priority = process->priority;
1064 ptr->handles = get_handle_table_count(process);
1065 ptr->unix_pid = process->unix_pid;
1066 grab_object( process );
1067 ptr++;
1070 if (!(*count = ptr - snapshot))
1072 free( snapshot );
1073 snapshot = NULL;
1075 return snapshot;
1078 /* create a new process */
1079 DECL_HANDLER(new_process)
1081 struct startup_info *info;
1082 struct thread *thread;
1083 struct process *process;
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 (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1094 set_error( STATUS_INVALID_HANDLE );
1095 close( socket_fd );
1096 return;
1098 if (shutdown_stage)
1100 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1101 close( socket_fd );
1102 return;
1104 if (!is_cpu_supported( req->cpu ))
1106 close( socket_fd );
1107 return;
1110 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1111 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1113 set_error( STATUS_ACCESS_DENIED );
1114 close( socket_fd );
1115 return;
1118 if (!req->info_size) /* create an orphaned process */
1120 create_process( socket_fd, NULL, 0 );
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->exe_file = NULL;
1131 info->process = NULL;
1132 info->data = NULL;
1134 if (req->exe_file &&
1135 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1137 close( socket_fd );
1138 goto done;
1141 info->data_size = get_req_data_size();
1142 info->info_size = min( req->info_size, info->data_size );
1144 if (req->info_size < sizeof(*info->data))
1146 /* make sure we have a full startup_info_t structure */
1147 data_size_t env_size = info->data_size - info->info_size;
1148 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1150 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1152 close( socket_fd );
1153 goto done;
1155 memcpy( info->data, get_req_data(), info_size );
1156 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1157 memcpy( info->data + 1, (const char *)get_req_data() + req->info_size, env_size );
1158 info->info_size = sizeof(startup_info_t);
1159 info->data_size = info->info_size + env_size;
1161 else
1163 data_size_t pos = sizeof(*info->data);
1165 if (!(info->data = memdup( get_req_data(), info->data_size )))
1167 close( socket_fd );
1168 goto done;
1170 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1171 FIXUP_LEN( info->data->curdir_len );
1172 FIXUP_LEN( info->data->dllpath_len );
1173 FIXUP_LEN( info->data->imagepath_len );
1174 FIXUP_LEN( info->data->cmdline_len );
1175 FIXUP_LEN( info->data->title_len );
1176 FIXUP_LEN( info->data->desktop_len );
1177 FIXUP_LEN( info->data->shellinfo_len );
1178 FIXUP_LEN( info->data->runtime_len );
1179 #undef FIXUP_LEN
1182 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
1183 process = thread->process;
1184 process->startup_info = (struct startup_info *)grab_object( info );
1186 if (parent->job
1187 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1188 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1190 add_job_process( parent->job, process );
1193 /* connect to the window station */
1194 connect_process_winstation( process, current );
1196 /* thread will be actually suspended in init_done */
1197 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
1199 /* set the process console */
1200 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1202 /* FIXME: some better error checking should be done...
1203 * like if hConOut and hConIn are console handles, then they should be on the same
1204 * physical console
1206 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1209 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1211 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1212 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1213 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1214 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1215 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1216 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1217 /* some handles above may have been invalid; this is not an error */
1218 if (get_error() == STATUS_INVALID_HANDLE ||
1219 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1222 /* attach to the debugger if requested */
1223 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1225 set_process_debugger( process, current );
1226 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1228 else if (parent->debugger && parent->debug_children)
1230 set_process_debugger( process, parent->debugger );
1231 /* debug_children is set to 1 by default */
1234 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1235 process->group_id = parent->group_id;
1237 info->process = (struct process *)grab_object( process );
1238 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1239 reply->pid = get_process_id( process );
1240 reply->tid = get_thread_id( thread );
1241 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
1242 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
1244 done:
1245 release_object( info );
1248 /* Retrieve information about a newly started process */
1249 DECL_HANDLER(get_new_process_info)
1251 struct startup_info *info;
1253 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1254 0, &startup_info_ops )))
1256 reply->success = is_process_init_done( info->process );
1257 reply->exit_code = info->process->exit_code;
1258 release_object( info );
1262 /* Retrieve the new process startup info */
1263 DECL_HANDLER(get_startup_info)
1265 struct process *process = current->process;
1266 struct startup_info *info = process->startup_info;
1267 data_size_t size;
1269 if (!info) return;
1271 if (info->exe_file &&
1272 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
1274 /* we return the data directly without making a copy so this can only be called once */
1275 reply->info_size = info->info_size;
1276 size = info->data_size;
1277 if (size > get_reply_max_size()) size = get_reply_max_size();
1278 set_reply_data_ptr( info->data, size );
1279 info->data = NULL;
1280 info->data_size = 0;
1283 /* signal the end of the process initialization */
1284 DECL_HANDLER(init_process_done)
1286 struct process_dll *dll;
1287 struct process *process = current->process;
1289 if (is_process_init_done(process))
1291 set_error( STATUS_INVALID_PARAMETER );
1292 return;
1294 if (!(dll = find_process_dll( process, req->module )))
1296 set_error( STATUS_DLL_NOT_FOUND );
1297 return;
1300 /* main exe is the first in the dll list */
1301 list_remove( &dll->entry );
1302 list_add_head( &process->dlls, &dll->entry );
1304 process->ldt_copy = req->ldt_copy;
1305 process->start_time = current_time;
1306 current->entry_point = req->entry;
1308 generate_startup_debug_events( process, req->entry );
1309 set_process_startup_state( process, STARTUP_DONE );
1311 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1312 stop_thread_if_suspended( current );
1313 if (process->debugger) set_process_debug_flag( process, 1 );
1316 /* open a handle to a process */
1317 DECL_HANDLER(open_process)
1319 struct process *process = get_process_from_id( req->pid );
1320 reply->handle = 0;
1321 if (process)
1323 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1324 release_object( process );
1328 /* terminate a process */
1329 DECL_HANDLER(terminate_process)
1331 struct process *process;
1333 if (req->handle)
1335 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1336 if (!process) return;
1338 else process = (struct process *)grab_object( current->process );
1340 reply->self = (current->process == process);
1341 terminate_process( process, current, req->exit_code );
1342 release_object( process );
1345 /* fetch information about a process */
1346 DECL_HANDLER(get_process_info)
1348 struct process *process;
1350 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1352 reply->pid = get_process_id( process );
1353 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1354 reply->exit_code = process->exit_code;
1355 reply->priority = process->priority;
1356 reply->affinity = process->affinity;
1357 reply->peb = process->peb;
1358 reply->start_time = process->start_time;
1359 reply->end_time = process->end_time;
1360 reply->cpu = process->cpu;
1361 reply->debugger_present = !!process->debugger;
1362 reply->debug_children = process->debug_children;
1363 release_object( process );
1367 static void set_process_affinity( struct process *process, affinity_t affinity )
1369 struct thread *thread;
1371 if (!process->running_threads)
1373 set_error( STATUS_PROCESS_IS_TERMINATING );
1374 return;
1377 process->affinity = affinity;
1379 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1381 set_thread_affinity( thread, affinity );
1385 /* set information about a process */
1386 DECL_HANDLER(set_process_info)
1388 struct process *process;
1390 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1392 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1393 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1394 release_object( process );
1398 /* read data from a process address space */
1399 DECL_HANDLER(read_process_memory)
1401 struct process *process;
1402 data_size_t len = get_reply_max_size();
1404 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1406 if (len)
1408 char *buffer = mem_alloc( len );
1409 if (buffer)
1411 if (read_process_memory( process, req->addr, len, buffer ))
1412 set_reply_data_ptr( buffer, len );
1413 else
1414 free( buffer );
1417 release_object( process );
1420 /* write data to a process address space */
1421 DECL_HANDLER(write_process_memory)
1423 struct process *process;
1425 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1427 data_size_t len = get_req_data_size();
1428 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1429 else set_error( STATUS_INVALID_PARAMETER );
1430 release_object( process );
1434 /* notify the server that a dll has been loaded */
1435 DECL_HANDLER(load_dll)
1437 struct process_dll *dll;
1438 struct mapping *mapping = NULL;
1440 if (req->mapping && !(mapping = get_mapping_obj( current->process, req->mapping, SECTION_QUERY )))
1441 return;
1443 if ((dll = process_load_dll( current->process, mapping, req->base,
1444 get_req_data(), get_req_data_size() )))
1446 dll->size = req->size;
1447 dll->dbg_offset = req->dbg_offset;
1448 dll->dbg_size = req->dbg_size;
1449 dll->name = req->name;
1450 /* only generate event if initialization is done */
1451 if (is_process_init_done( current->process ))
1452 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1454 if (mapping) release_object( mapping );
1457 /* notify the server that a dll is being unloaded */
1458 DECL_HANDLER(unload_dll)
1460 process_unload_dll( current->process, req->base );
1463 /* retrieve information about a module in a process */
1464 DECL_HANDLER(get_dll_info)
1466 struct process *process;
1468 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1470 struct process_dll *dll;
1472 if (req->base_address)
1473 dll = find_process_dll( process, req->base_address );
1474 else /* NULL means main module */
1475 dll = list_head( &process->dlls ) ?
1476 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1478 if (dll)
1480 reply->size = dll->size;
1481 reply->entry_point = 0; /* FIXME */
1482 reply->filename_len = dll->namelen;
1483 if (dll->filename)
1485 if (dll->namelen <= get_reply_max_size())
1486 set_reply_data( dll->filename, dll->namelen );
1487 else
1488 set_error( STATUS_BUFFER_TOO_SMALL );
1491 else
1492 set_error( STATUS_DLL_NOT_FOUND );
1494 release_object( process );
1498 /* retrieve the process idle event */
1499 DECL_HANDLER(get_process_idle_event)
1501 struct process *process;
1503 reply->event = 0;
1504 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1506 if (process->idle_event && process != current->process)
1507 reply->event = alloc_handle( current->process, process->idle_event,
1508 EVENT_ALL_ACCESS, 0 );
1509 release_object( process );
1513 /* make the current process a system process */
1514 DECL_HANDLER(make_process_system)
1516 struct process *process = current->process;
1518 if (!shutdown_event)
1520 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1521 make_object_static( (struct object *)shutdown_event );
1524 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1525 return;
1527 if (!process->is_system)
1529 process->is_system = 1;
1530 close_process_desktop( process );
1531 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1532 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1536 /* create a new job object */
1537 DECL_HANDLER(create_job)
1539 struct job *job;
1540 struct unicode_str name;
1541 struct directory *root = NULL;
1542 const struct security_descriptor *sd;
1543 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name );
1545 if (!objattr) return;
1547 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) return;
1549 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1551 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1552 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1553 else
1554 reply->handle = alloc_handle_no_access_check( current->process, job,
1555 req->access, objattr->attributes );
1556 release_object( job );
1558 if (root) release_object( root );
1561 /* assign a job object to a process */
1562 DECL_HANDLER(assign_job)
1564 struct process *process;
1565 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1567 if (!job) return;
1569 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1571 add_job_process( job, process );
1572 release_object( process );
1574 release_object( job );
1578 /* check if a process is associated with a job */
1579 DECL_HANDLER(process_in_job)
1581 struct process *process;
1582 struct job *job;
1584 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1585 return;
1587 if (!req->job)
1589 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1591 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1593 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1594 release_object( job );
1596 release_object( process );
1599 /* terminate all processes associated with the job */
1600 DECL_HANDLER(terminate_job)
1602 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1604 if (!job) return;
1606 terminate_job( job, req->status );
1607 release_object( job );
1610 /* update limits of the job object */
1611 DECL_HANDLER(set_job_limits)
1613 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1615 if (!job) return;
1617 job->limit_flags = req->limit_flags;
1618 release_object( job );
1621 /* set the jobs completion port */
1622 DECL_HANDLER(set_job_completion_port)
1624 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1626 if (!job) return;
1628 if (!job->completion_port)
1630 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1631 job->completion_key = req->key;
1633 else
1634 set_error( STATUS_INVALID_PARAMETER );
1636 release_object( job );