setupapi: SetupDiCreateDeviceInfoListEx returns INVALID_HANDLE_VALUE on error.
[wine/multimedia.git] / server / process.c
blob0bf71942c7c0c1ac105d3c5b35b183f59e7d73b9
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 = 0;
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;
654 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
657 static void process_poll_event( struct fd *fd, int event )
659 struct process *process = get_fd_user( fd );
660 assert( process->obj.ops == &process_ops );
662 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
663 else if (event & POLLIN) receive_fd( process );
666 static void startup_info_destroy( struct object *obj )
668 struct startup_info *info = (struct startup_info *)obj;
669 assert( obj->ops == &startup_info_ops );
670 free( info->data );
671 if (info->exe_file) release_object( info->exe_file );
672 if (info->process) release_object( info->process );
675 static void startup_info_dump( struct object *obj, int verbose )
677 struct startup_info *info = (struct startup_info *)obj;
678 assert( obj->ops == &startup_info_ops );
680 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
681 info->data->hstdin, info->data->hstdout, info->data->hstderr );
684 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
686 struct startup_info *info = (struct startup_info *)obj;
687 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
690 /* get a process from an id (and increment the refcount) */
691 struct process *get_process_from_id( process_id_t id )
693 struct object *obj = get_ptid_entry( id );
695 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
696 set_error( STATUS_INVALID_PARAMETER );
697 return NULL;
700 /* get a process from a handle (and increment the refcount) */
701 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
703 return (struct process *)get_handle_obj( current->process, handle,
704 access, &process_ops );
707 /* find a dll from its base address */
708 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
710 struct process_dll *dll;
712 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
714 if (dll->base == base) return dll;
716 return NULL;
719 /* add a dll to a process list */
720 static struct process_dll *process_load_dll( struct process *process, struct mapping *mapping,
721 mod_handle_t base, const WCHAR *filename,
722 data_size_t name_len )
724 struct process_dll *dll;
726 /* make sure we don't already have one with the same base address */
727 if (find_process_dll( process, base ))
729 set_error( STATUS_INVALID_PARAMETER );
730 return NULL;
733 if ((dll = mem_alloc( sizeof(*dll) )))
735 dll->mapping = NULL;
736 dll->base = base;
737 dll->filename = NULL;
738 dll->namelen = name_len;
739 if (name_len && !(dll->filename = memdup( filename, name_len )))
741 free( dll );
742 return NULL;
744 if (mapping) dll->mapping = grab_mapping_unless_removable( mapping );
745 list_add_tail( &process->dlls, &dll->entry );
747 return dll;
750 /* remove a dll from a process list */
751 static void process_unload_dll( struct process *process, mod_handle_t base )
753 struct process_dll *dll = find_process_dll( process, base );
755 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
757 if (dll->mapping) release_object( dll->mapping );
758 free( dll->filename );
759 list_remove( &dll->entry );
760 free( dll );
761 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
763 else set_error( STATUS_INVALID_PARAMETER );
766 /* terminate a process with the given exit code */
767 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
769 struct thread *thread;
771 grab_object( process ); /* make sure it doesn't get freed when threads die */
772 process->is_terminating = 1;
774 restart:
775 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
777 if (exit_code) thread->exit_code = exit_code;
778 if (thread == skip) continue;
779 if (thread->state == TERMINATED) continue;
780 kill_thread( thread, 1 );
781 goto restart;
783 release_object( process );
786 /* kill all processes */
787 static void kill_all_processes(void)
789 for (;;)
791 struct process *process;
793 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
795 if (process->running_threads) break;
797 if (&process->entry == &process_list) break; /* no process found */
798 terminate_process( process, NULL, 1 );
802 /* kill all processes being attached to a console renderer */
803 void kill_console_processes( struct thread *renderer, int exit_code )
805 for (;;) /* restart from the beginning of the list every time */
807 struct process *process;
809 /* find the first process being attached to 'renderer' and still running */
810 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
812 if (process == renderer->process) continue;
813 if (!process->running_threads) continue;
814 if (process->console && console_get_renderer( process->console ) == renderer) break;
816 if (&process->entry == &process_list) break; /* no process found */
817 terminate_process( process, NULL, exit_code );
821 /* a process has been killed (i.e. its last thread died) */
822 static void process_killed( struct process *process )
824 struct list *ptr;
826 assert( list_empty( &process->thread_list ));
827 process->end_time = current_time;
828 if (!process->is_system) close_process_desktop( process );
829 process->winstation = 0;
830 process->desktop = 0;
831 close_process_handles( process );
832 if (process->idle_event)
834 release_object( process->idle_event );
835 process->idle_event = NULL;
838 /* close the console attached to this process, if any */
839 free_console( process );
841 while ((ptr = list_head( &process->rawinput_devices )))
843 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
844 list_remove( &entry->entry );
845 free( entry );
847 while ((ptr = list_head( &process->dlls )))
849 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
850 if (dll->mapping) release_object( dll->mapping );
851 free( dll->filename );
852 list_remove( &dll->entry );
853 free( dll );
855 destroy_process_classes( process );
856 free_process_user_handles( process );
857 remove_process_locks( process );
858 set_process_startup_state( process, STARTUP_ABORTED );
859 finish_process_tracing( process );
860 release_job_process( process );
861 start_sigkill_timer( process );
862 wake_up( &process->obj, 0 );
865 /* add a thread to a process running threads list */
866 void add_process_thread( struct process *process, struct thread *thread )
868 list_add_tail( &process->thread_list, &thread->proc_entry );
869 if (!process->running_threads++)
871 running_processes++;
872 if (!process->is_system)
874 if (!user_processes++ && shutdown_timeout)
876 remove_timeout_user( shutdown_timeout );
877 shutdown_timeout = NULL;
881 grab_object( thread );
884 /* remove a thread from a process running threads list */
885 void remove_process_thread( struct process *process, struct thread *thread )
887 assert( process->running_threads > 0 );
888 assert( !list_empty( &process->thread_list ));
890 list_remove( &thread->proc_entry );
892 if (!--process->running_threads)
894 /* we have removed the last running thread, exit the process */
895 process->exit_code = thread->exit_code;
896 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
897 process_killed( process );
899 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
900 release_object( thread );
903 /* suspend all the threads of a process */
904 void suspend_process( struct process *process )
906 if (!process->suspend++)
908 struct list *ptr, *next;
910 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
912 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
913 if (!thread->suspend) stop_thread( thread );
918 /* resume all the threads of a process */
919 void resume_process( struct process *process )
921 assert (process->suspend > 0);
922 if (!--process->suspend)
924 struct list *ptr, *next;
926 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
928 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
929 if (!thread->suspend) wake_thread( thread );
934 /* kill a process on the spot */
935 void kill_process( struct process *process, int violent_death )
937 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
939 release_object( process->msg_fd );
940 process->msg_fd = NULL;
943 if (process->sigkill_timeout) /* already waiting for it to die */
945 remove_timeout_user( process->sigkill_timeout );
946 process->sigkill_timeout = NULL;
947 process_died( process );
948 return;
951 if (violent_death) terminate_process( process, NULL, 1 );
952 else
954 struct list *ptr;
956 grab_object( process ); /* make sure it doesn't get freed when threads die */
957 while ((ptr = list_head( &process->thread_list )))
959 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
960 kill_thread( thread, 0 );
962 release_object( process );
966 /* kill all processes being debugged by a given thread */
967 void kill_debugged_processes( struct thread *debugger, int exit_code )
969 for (;;) /* restart from the beginning of the list every time */
971 struct process *process;
973 /* find the first process being debugged by 'debugger' and still running */
974 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
976 if (!process->running_threads) continue;
977 if (process->debugger == debugger) break;
979 if (&process->entry == &process_list) break; /* no process found */
980 process->debugger = NULL;
981 terminate_process( process, NULL, exit_code );
986 /* trigger a breakpoint event in a given process */
987 void break_process( struct process *process )
989 struct thread *thread;
991 suspend_process( process );
993 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
995 if (thread->context) /* inside an exception event already */
997 break_thread( thread );
998 goto done;
1001 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1002 else set_error( STATUS_ACCESS_DENIED );
1003 done:
1004 resume_process( process );
1008 /* detach a debugger from all its debuggees */
1009 void detach_debugged_processes( struct thread *debugger )
1011 struct process *process;
1013 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1015 if (process->debugger == debugger && process->running_threads)
1017 debugger_detach( process, debugger );
1023 void enum_processes( int (*cb)(struct process*, void*), void *user )
1025 struct list *ptr, *next;
1027 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1029 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1030 if ((cb)(process, user)) break;
1034 /* set the debugged flag in the process PEB */
1035 int set_process_debug_flag( struct process *process, int flag )
1037 char data = (flag != 0);
1039 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1040 return write_process_memory( process, process->peb + 2, 1, &data );
1043 /* take a snapshot of currently running processes */
1044 struct process_snapshot *process_snap( int *count )
1046 struct process_snapshot *snapshot, *ptr;
1047 struct process *process;
1049 if (!running_processes) return NULL;
1050 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1051 return NULL;
1052 ptr = snapshot;
1053 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1055 if (!process->running_threads) continue;
1056 ptr->process = process;
1057 ptr->threads = process->running_threads;
1058 ptr->count = process->obj.refcount;
1059 ptr->priority = process->priority;
1060 ptr->handles = get_handle_table_count(process);
1061 ptr->unix_pid = process->unix_pid;
1062 grab_object( process );
1063 ptr++;
1066 if (!(*count = ptr - snapshot))
1068 free( snapshot );
1069 snapshot = NULL;
1071 return snapshot;
1074 /* create a new process */
1075 DECL_HANDLER(new_process)
1077 struct startup_info *info;
1078 struct thread *thread;
1079 struct process *process;
1080 struct process *parent = current->process;
1081 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1083 if (socket_fd == -1)
1085 set_error( STATUS_INVALID_PARAMETER );
1086 return;
1088 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1090 set_error( STATUS_INVALID_HANDLE );
1091 close( socket_fd );
1092 return;
1094 if (shutdown_stage)
1096 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1097 close( socket_fd );
1098 return;
1100 if (!is_cpu_supported( req->cpu ))
1102 close( socket_fd );
1103 return;
1106 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1107 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1109 set_error( STATUS_ACCESS_DENIED );
1110 close( socket_fd );
1111 return;
1114 if (!req->info_size) /* create an orphaned process */
1116 create_process( socket_fd, NULL, 0 );
1117 return;
1120 /* build the startup info for a new process */
1121 if (!(info = alloc_object( &startup_info_ops )))
1123 close( socket_fd );
1124 return;
1126 info->exe_file = NULL;
1127 info->process = NULL;
1128 info->data = NULL;
1130 if (req->exe_file &&
1131 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1133 close( socket_fd );
1134 goto done;
1137 info->data_size = get_req_data_size();
1138 info->info_size = min( req->info_size, info->data_size );
1140 if (req->info_size < sizeof(*info->data))
1142 /* make sure we have a full startup_info_t structure */
1143 data_size_t env_size = info->data_size - info->info_size;
1144 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1146 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1148 close( socket_fd );
1149 goto done;
1151 memcpy( info->data, get_req_data(), info_size );
1152 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1153 memcpy( info->data + 1, (const char *)get_req_data() + req->info_size, env_size );
1154 info->info_size = sizeof(startup_info_t);
1155 info->data_size = info->info_size + env_size;
1157 else
1159 data_size_t pos = sizeof(*info->data);
1161 if (!(info->data = memdup( get_req_data(), info->data_size )))
1163 close( socket_fd );
1164 goto done;
1166 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1167 FIXUP_LEN( info->data->curdir_len );
1168 FIXUP_LEN( info->data->dllpath_len );
1169 FIXUP_LEN( info->data->imagepath_len );
1170 FIXUP_LEN( info->data->cmdline_len );
1171 FIXUP_LEN( info->data->title_len );
1172 FIXUP_LEN( info->data->desktop_len );
1173 FIXUP_LEN( info->data->shellinfo_len );
1174 FIXUP_LEN( info->data->runtime_len );
1175 #undef FIXUP_LEN
1178 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
1179 process = thread->process;
1180 process->debug_children = (req->create_flags & DEBUG_PROCESS)
1181 && !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1182 process->startup_info = (struct startup_info *)grab_object( info );
1184 if (parent->job
1185 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1186 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1188 add_job_process( parent->job, process );
1191 /* connect to the window station */
1192 connect_process_winstation( process, current );
1194 /* thread will be actually suspended in init_done */
1195 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
1197 /* set the process console */
1198 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1200 /* FIXME: some better error checking should be done...
1201 * like if hConOut and hConIn are console handles, then they should be on the same
1202 * physical console
1204 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1207 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1209 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1210 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1211 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1212 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1213 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1214 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1215 /* some handles above may have been invalid; this is not an error */
1216 if (get_error() == STATUS_INVALID_HANDLE ||
1217 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1220 /* attach to the debugger if requested */
1221 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1222 set_process_debugger( process, current );
1223 else if (parent->debugger && parent->debug_children)
1224 set_process_debugger( process, parent->debugger );
1226 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1227 process->group_id = parent->group_id;
1229 info->process = (struct process *)grab_object( process );
1230 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1231 reply->pid = get_process_id( process );
1232 reply->tid = get_thread_id( thread );
1233 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
1234 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
1236 done:
1237 release_object( info );
1240 /* Retrieve information about a newly started process */
1241 DECL_HANDLER(get_new_process_info)
1243 struct startup_info *info;
1245 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1246 0, &startup_info_ops )))
1248 reply->success = is_process_init_done( info->process );
1249 reply->exit_code = info->process->exit_code;
1250 release_object( info );
1254 /* Retrieve the new process startup info */
1255 DECL_HANDLER(get_startup_info)
1257 struct process *process = current->process;
1258 struct startup_info *info = process->startup_info;
1259 data_size_t size;
1261 if (!info) return;
1263 if (info->exe_file &&
1264 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
1266 /* we return the data directly without making a copy so this can only be called once */
1267 reply->info_size = info->info_size;
1268 size = info->data_size;
1269 if (size > get_reply_max_size()) size = get_reply_max_size();
1270 set_reply_data_ptr( info->data, size );
1271 info->data = NULL;
1272 info->data_size = 0;
1275 /* signal the end of the process initialization */
1276 DECL_HANDLER(init_process_done)
1278 struct process_dll *dll;
1279 struct process *process = current->process;
1281 if (is_process_init_done(process))
1283 set_error( STATUS_INVALID_PARAMETER );
1284 return;
1286 if (!(dll = find_process_dll( process, req->module )))
1288 set_error( STATUS_DLL_NOT_FOUND );
1289 return;
1292 /* main exe is the first in the dll list */
1293 list_remove( &dll->entry );
1294 list_add_head( &process->dlls, &dll->entry );
1296 process->ldt_copy = req->ldt_copy;
1297 process->start_time = current_time;
1299 generate_startup_debug_events( process, req->entry );
1300 set_process_startup_state( process, STARTUP_DONE );
1302 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1303 stop_thread_if_suspended( current );
1304 if (process->debugger) set_process_debug_flag( process, 1 );
1307 /* open a handle to a process */
1308 DECL_HANDLER(open_process)
1310 struct process *process = get_process_from_id( req->pid );
1311 reply->handle = 0;
1312 if (process)
1314 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1315 release_object( process );
1319 /* terminate a process */
1320 DECL_HANDLER(terminate_process)
1322 struct process *process;
1324 if (req->handle)
1326 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1327 if (!process) return;
1329 else process = (struct process *)grab_object( current->process );
1331 reply->self = (current->process == process);
1332 terminate_process( process, current, req->exit_code );
1333 release_object( process );
1336 /* fetch information about a process */
1337 DECL_HANDLER(get_process_info)
1339 struct process *process;
1341 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1343 reply->pid = get_process_id( process );
1344 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1345 reply->exit_code = process->exit_code;
1346 reply->priority = process->priority;
1347 reply->affinity = process->affinity;
1348 reply->peb = process->peb;
1349 reply->start_time = process->start_time;
1350 reply->end_time = process->end_time;
1351 reply->cpu = process->cpu;
1352 reply->debugger_present = !!process->debugger;
1353 release_object( process );
1357 static void set_process_affinity( struct process *process, affinity_t affinity )
1359 struct thread *thread;
1361 if (!process->running_threads)
1363 set_error( STATUS_PROCESS_IS_TERMINATING );
1364 return;
1367 process->affinity = affinity;
1369 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1371 set_thread_affinity( thread, affinity );
1375 /* set information about a process */
1376 DECL_HANDLER(set_process_info)
1378 struct process *process;
1380 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1382 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1383 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1384 release_object( process );
1388 /* read data from a process address space */
1389 DECL_HANDLER(read_process_memory)
1391 struct process *process;
1392 data_size_t len = get_reply_max_size();
1394 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1396 if (len)
1398 char *buffer = mem_alloc( len );
1399 if (buffer)
1401 if (read_process_memory( process, req->addr, len, buffer ))
1402 set_reply_data_ptr( buffer, len );
1403 else
1404 free( buffer );
1407 release_object( process );
1410 /* write data to a process address space */
1411 DECL_HANDLER(write_process_memory)
1413 struct process *process;
1415 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1417 data_size_t len = get_req_data_size();
1418 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1419 else set_error( STATUS_INVALID_PARAMETER );
1420 release_object( process );
1424 /* notify the server that a dll has been loaded */
1425 DECL_HANDLER(load_dll)
1427 struct process_dll *dll;
1428 struct mapping *mapping = NULL;
1430 if (req->mapping && !(mapping = get_mapping_obj( current->process, req->mapping, SECTION_QUERY )))
1431 return;
1433 if ((dll = process_load_dll( current->process, mapping, req->base,
1434 get_req_data(), get_req_data_size() )))
1436 dll->size = req->size;
1437 dll->dbg_offset = req->dbg_offset;
1438 dll->dbg_size = req->dbg_size;
1439 dll->name = req->name;
1440 /* only generate event if initialization is done */
1441 if (is_process_init_done( current->process ))
1442 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1444 if (mapping) release_object( mapping );
1447 /* notify the server that a dll is being unloaded */
1448 DECL_HANDLER(unload_dll)
1450 process_unload_dll( current->process, req->base );
1453 /* retrieve information about a module in a process */
1454 DECL_HANDLER(get_dll_info)
1456 struct process *process;
1458 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1460 struct process_dll *dll;
1462 if (req->base_address)
1463 dll = find_process_dll( process, req->base_address );
1464 else /* NULL means main module */
1465 dll = list_head( &process->dlls ) ?
1466 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1468 if (dll)
1470 reply->size = dll->size;
1471 reply->entry_point = 0; /* FIXME */
1472 reply->filename_len = dll->namelen;
1473 if (dll->filename)
1475 if (dll->namelen <= get_reply_max_size())
1476 set_reply_data( dll->filename, dll->namelen );
1477 else
1478 set_error( STATUS_BUFFER_TOO_SMALL );
1481 else
1482 set_error( STATUS_DLL_NOT_FOUND );
1484 release_object( process );
1488 /* retrieve the process idle event */
1489 DECL_HANDLER(get_process_idle_event)
1491 struct process *process;
1493 reply->event = 0;
1494 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1496 if (process->idle_event && process != current->process)
1497 reply->event = alloc_handle( current->process, process->idle_event,
1498 EVENT_ALL_ACCESS, 0 );
1499 release_object( process );
1503 /* make the current process a system process */
1504 DECL_HANDLER(make_process_system)
1506 struct process *process = current->process;
1508 if (!shutdown_event)
1510 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1511 make_object_static( (struct object *)shutdown_event );
1514 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1515 return;
1517 if (!process->is_system)
1519 process->is_system = 1;
1520 close_process_desktop( process );
1521 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1522 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1526 /* create a new job object */
1527 DECL_HANDLER(create_job)
1529 struct job *job;
1530 struct unicode_str name;
1531 struct directory *root = NULL;
1532 const struct object_attributes *objattr = get_req_data();
1533 const struct security_descriptor *sd;
1535 if (!objattr_is_valid( objattr, get_req_data_size() )) return;
1537 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
1538 objattr_get_name( objattr, &name );
1540 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) return;
1542 if ((job = create_job_object( root, &name, req->attributes, sd )))
1544 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1545 reply->handle = alloc_handle( current->process, job, req->access, req->attributes );
1546 else
1547 reply->handle = alloc_handle_no_access_check( current->process, job, req->access, req->attributes );
1548 release_object( job );
1550 if (root) release_object( root );
1553 /* assign a job object to a process */
1554 DECL_HANDLER(assign_job)
1556 struct process *process;
1557 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1559 if (!job) return;
1561 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1563 add_job_process( job, process );
1564 release_object( process );
1566 release_object( job );
1570 /* check if a process is associated with a job */
1571 DECL_HANDLER(process_in_job)
1573 struct process *process;
1574 struct job *job;
1576 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1577 return;
1579 if (!req->job)
1581 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1583 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1585 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1586 release_object( job );
1588 release_object( process );
1591 /* terminate all processes associated with the job */
1592 DECL_HANDLER(terminate_job)
1594 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1596 if (!job) return;
1598 terminate_job( job, req->status );
1599 release_object( job );
1602 /* update limits of the job object */
1603 DECL_HANDLER(set_job_limits)
1605 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1607 if (!job) return;
1609 job->limit_flags = req->limit_flags;
1610 release_object( job );
1613 /* set the jobs completion port */
1614 DECL_HANDLER(set_job_completion_port)
1616 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1618 if (!job) return;
1620 if (!job->completion_port)
1622 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1623 job->completion_key = req->key;
1625 else
1626 set_error( STATUS_INVALID_PARAMETER );
1628 release_object( job );