wined3d: Get resource info from the texture in wined3d_surface_depth_fill().
[wine.git] / server / process.c
blob11d2987cbf7ea9460778bc63b1877f015dfc16ac
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_link_name, /* link_name */
85 NULL, /* unlink_name */
86 no_open_file, /* open_file */
87 no_close_handle, /* close_handle */
88 process_destroy /* destroy */
91 static const struct fd_ops process_fd_ops =
93 NULL, /* get_poll_events */
94 process_poll_event, /* poll_event */
95 NULL, /* flush */
96 NULL, /* get_fd_type */
97 NULL, /* ioctl */
98 NULL, /* queue_async */
99 NULL, /* reselect_async */
100 NULL /* cancel async */
103 /* process startup info */
105 struct startup_info
107 struct object obj; /* object header */
108 struct file *exe_file; /* file handle for main exe */
109 struct process *process; /* created process */
110 data_size_t info_size; /* size of startup info */
111 data_size_t data_size; /* size of whole startup data */
112 startup_info_t *data; /* data for startup info */
115 static void startup_info_dump( struct object *obj, int verbose );
116 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
117 static void startup_info_destroy( struct object *obj );
119 static const struct object_ops startup_info_ops =
121 sizeof(struct startup_info), /* size */
122 startup_info_dump, /* dump */
123 no_get_type, /* get_type */
124 add_queue, /* add_queue */
125 remove_queue, /* remove_queue */
126 startup_info_signaled, /* signaled */
127 no_satisfied, /* satisfied */
128 no_signal, /* signal */
129 no_get_fd, /* get_fd */
130 no_map_access, /* map_access */
131 default_get_sd, /* get_sd */
132 default_set_sd, /* set_sd */
133 no_lookup_name, /* lookup_name */
134 no_link_name, /* link_name */
135 NULL, /* unlink_name */
136 no_open_file, /* open_file */
137 no_close_handle, /* close_handle */
138 startup_info_destroy /* destroy */
141 /* job object */
143 static void job_dump( struct object *obj, int verbose );
144 static struct object_type *job_get_type( struct object *obj );
145 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
146 static unsigned int job_map_access( struct object *obj, unsigned int access );
147 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
148 static void job_destroy( struct object *obj );
150 struct job
152 struct object obj; /* object header */
153 struct list process_list; /* list of all processes */
154 int num_processes; /* count of running processes */
155 unsigned int limit_flags; /* limit flags */
156 int terminating; /* job is terminating */
157 int signaled; /* job is signaled */
158 struct completion *completion_port; /* associated completion port */
159 apc_param_t completion_key; /* key to send with completion messages */
162 static const struct object_ops job_ops =
164 sizeof(struct job), /* size */
165 job_dump, /* dump */
166 job_get_type, /* get_type */
167 add_queue, /* add_queue */
168 remove_queue, /* remove_queue */
169 job_signaled, /* signaled */
170 no_satisfied, /* satisfied */
171 no_signal, /* signal */
172 no_get_fd, /* get_fd */
173 job_map_access, /* map_access */
174 default_get_sd, /* get_sd */
175 default_set_sd, /* set_sd */
176 no_lookup_name, /* lookup_name */
177 directory_link_name, /* link_name */
178 default_unlink_name, /* unlink_name */
179 no_open_file, /* open_file */
180 job_close_handle, /* close_handle */
181 job_destroy /* destroy */
184 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
185 unsigned int attr, const struct security_descriptor *sd )
187 struct job *job;
189 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
191 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
193 /* initialize it if it didn't already exist */
194 list_init( &job->process_list );
195 job->num_processes = 0;
196 job->limit_flags = 0;
197 job->terminating = 0;
198 job->signaled = 0;
199 job->completion_port = NULL;
200 job->completion_key = 0;
203 return job;
206 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
208 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
211 static struct object_type *job_get_type( struct object *obj )
213 static const WCHAR name[] = {'J','o','b'};
214 static const struct unicode_str str = { name, sizeof(name) };
215 return get_object_type( &str );
218 static unsigned int job_map_access( struct object *obj, unsigned int access )
220 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
221 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
222 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
223 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
224 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
227 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
229 if (job->completion_port)
230 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
233 static void add_job_process( struct job *job, struct process *process )
235 if (!process->running_threads)
237 set_error( STATUS_PROCESS_IS_TERMINATING );
238 return;
240 if (process->job)
242 set_error( STATUS_ACCESS_DENIED );
243 return;
245 process->job = (struct job *)grab_object( job );
246 list_add_tail( &job->process_list, &process->job_entry );
247 job->num_processes++;
249 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
252 /* called when a process has terminated, allow one additional process */
253 static void release_job_process( struct process *process )
255 struct job *job = process->job;
257 if (!job) return;
259 assert( job->num_processes );
260 job->num_processes--;
262 if (!job->terminating)
263 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
265 if (!job->num_processes)
266 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
269 static void terminate_job( struct job *job, int exit_code )
271 /* don't report completion events for terminated processes */
272 job->terminating = 1;
274 for (;;) /* restart from the beginning of the list every time */
276 struct process *process;
278 /* find the first process associated with this job and still running */
279 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
281 if (process->running_threads) break;
283 if (&process->job_entry == &job->process_list) break; /* no process found */
284 assert( process->job == job );
285 terminate_process( process, NULL, exit_code );
288 job->terminating = 0;
289 job->signaled = 1;
290 wake_up( &job->obj, 0 );
293 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
295 struct job *job = (struct job *)obj;
296 assert( obj->ops == &job_ops );
298 if (obj->handle_count == 1) /* last handle */
300 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
301 terminate_job( job, 0 );
303 return 1;
306 static void job_destroy( struct object *obj )
308 struct job *job = (struct job *)obj;
309 assert( obj->ops == &job_ops );
311 assert( !job->num_processes );
312 assert( list_empty(&job->process_list) );
314 if (job->completion_port) release_object( job->completion_port );
317 static void job_dump( struct object *obj, int verbose )
319 struct job *job = (struct job *)obj;
320 assert( obj->ops == &job_ops );
321 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
324 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
326 struct job *job = (struct job *)obj;
327 return job->signaled;
330 struct ptid_entry
332 void *ptr; /* entry ptr */
333 unsigned int next; /* next free entry */
336 static struct ptid_entry *ptid_entries; /* array of ptid entries */
337 static unsigned int used_ptid_entries; /* number of entries in use */
338 static unsigned int alloc_ptid_entries; /* number of allocated entries */
339 static unsigned int next_free_ptid; /* next free entry */
340 static unsigned int last_free_ptid; /* last free entry */
342 static void kill_all_processes(void);
344 #define PTID_OFFSET 8 /* offset for first ptid value */
346 /* allocate a new process or thread id */
347 unsigned int alloc_ptid( void *ptr )
349 struct ptid_entry *entry;
350 unsigned int id;
352 if (used_ptid_entries < alloc_ptid_entries)
354 id = used_ptid_entries + PTID_OFFSET;
355 entry = &ptid_entries[used_ptid_entries++];
357 else if (next_free_ptid)
359 id = next_free_ptid;
360 entry = &ptid_entries[id - PTID_OFFSET];
361 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
363 else /* need to grow the array */
365 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
366 if (!count) count = 64;
367 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
369 set_error( STATUS_NO_MEMORY );
370 return 0;
372 ptid_entries = entry;
373 alloc_ptid_entries = count;
374 id = used_ptid_entries + PTID_OFFSET;
375 entry = &ptid_entries[used_ptid_entries++];
378 entry->ptr = ptr;
379 return id;
382 /* free a process or thread id */
383 void free_ptid( unsigned int id )
385 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
387 entry->ptr = NULL;
388 entry->next = 0;
390 /* append to end of free list so that we don't reuse it too early */
391 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
392 else next_free_ptid = id;
394 last_free_ptid = id;
397 /* retrieve the pointer corresponding to a process or thread id */
398 void *get_ptid_entry( unsigned int id )
400 if (id < PTID_OFFSET) return NULL;
401 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
402 return ptid_entries[id - PTID_OFFSET].ptr;
405 /* return the main thread of the process */
406 struct thread *get_process_first_thread( struct process *process )
408 struct list *ptr = list_head( &process->thread_list );
409 if (!ptr) return NULL;
410 return LIST_ENTRY( ptr, struct thread, proc_entry );
413 /* set the state of the process startup info */
414 static void set_process_startup_state( struct process *process, enum startup_state state )
416 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
417 if (process->startup_info)
419 wake_up( &process->startup_info->obj, 0 );
420 release_object( process->startup_info );
421 process->startup_info = NULL;
425 /* callback for server shutdown */
426 static void server_shutdown_timeout( void *arg )
428 shutdown_timeout = NULL;
429 if (!running_processes)
431 close_master_socket( 0 );
432 return;
434 switch(++shutdown_stage)
436 case 1: /* signal system processes to exit */
437 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
438 if (shutdown_event) set_event( shutdown_event );
439 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
440 close_master_socket( 4 * -TICKS_PER_SEC );
441 break;
442 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
443 kill_all_processes();
444 break;
448 /* forced shutdown, used for wineserver -k */
449 void shutdown_master_socket(void)
451 kill_all_processes();
452 shutdown_stage = 2;
453 if (shutdown_timeout)
455 remove_timeout_user( shutdown_timeout );
456 shutdown_timeout = NULL;
458 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
461 /* final cleanup once we are sure a process is really dead */
462 static void process_died( struct process *process )
464 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
465 if (!process->is_system)
467 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
468 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
470 release_object( process );
471 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
474 /* callback for process sigkill timeout */
475 static void process_sigkill( void *private )
477 struct process *process = private;
479 process->sigkill_timeout = NULL;
480 kill( process->unix_pid, SIGKILL );
481 process_died( process );
484 /* start the sigkill timer for a process upon exit */
485 static void start_sigkill_timer( struct process *process )
487 grab_object( process );
488 if (process->unix_pid != -1 && process->msg_fd)
489 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
490 else
491 process_died( process );
494 /* create a new process and its main thread */
495 /* if the function fails the fd is closed */
496 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
498 struct process *process;
499 struct thread *thread = NULL;
500 int request_pipe[2];
502 if (!(process = alloc_object( &process_ops )))
504 close( fd );
505 goto error;
507 process->parent = NULL;
508 process->debugger = NULL;
509 process->handles = NULL;
510 process->msg_fd = NULL;
511 process->sigkill_timeout = NULL;
512 process->unix_pid = -1;
513 process->exit_code = STILL_ACTIVE;
514 process->running_threads = 0;
515 process->priority = PROCESS_PRIOCLASS_NORMAL;
516 process->suspend = 0;
517 process->is_system = 0;
518 process->debug_children = 1;
519 process->is_terminating = 0;
520 process->job = NULL;
521 process->console = NULL;
522 process->startup_state = STARTUP_IN_PROGRESS;
523 process->startup_info = NULL;
524 process->idle_event = NULL;
525 process->peb = 0;
526 process->ldt_copy = 0;
527 process->winstation = 0;
528 process->desktop = 0;
529 process->token = NULL;
530 process->trace_data = 0;
531 process->rawinput_mouse = NULL;
532 process->rawinput_kbd = NULL;
533 list_init( &process->thread_list );
534 list_init( &process->locks );
535 list_init( &process->classes );
536 list_init( &process->dlls );
537 list_init( &process->rawinput_devices );
539 process->end_time = 0;
540 list_add_tail( &process_list, &process->entry );
542 if (!(process->id = process->group_id = alloc_ptid( process )))
544 close( fd );
545 goto error;
547 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
549 /* create the handle table */
550 if (!parent_thread)
552 process->handles = alloc_handle_table( process, 0 );
553 process->token = token_create_admin();
554 process->affinity = ~0;
556 else
558 struct process *parent = parent_thread->process;
559 process->parent = (struct process *)grab_object( parent );
560 process->handles = inherit_all ? copy_handle_table( process, parent )
561 : alloc_handle_table( process, 0 );
562 /* Note: for security reasons, starting a new process does not attempt
563 * to use the current impersonation token for the new process */
564 process->token = token_duplicate( parent->token, TRUE, 0 );
565 process->affinity = parent->affinity;
567 if (!process->handles || !process->token) goto error;
569 /* create the main thread */
570 if (pipe( request_pipe ) == -1)
572 file_set_error();
573 goto error;
575 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
577 close( request_pipe[0] );
578 close( request_pipe[1] );
579 goto error;
581 close( request_pipe[1] );
582 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
584 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
585 release_object( process );
586 return thread;
588 error:
589 if (process) release_object( process );
590 /* if we failed to start our first process, close everything down */
591 if (!running_processes) close_master_socket( 0 );
592 return NULL;
595 /* initialize the current process and fill in the request */
596 data_size_t init_process( struct thread *thread )
598 struct process *process = thread->process;
599 struct startup_info *info = process->startup_info;
601 init_process_tracing( process );
602 if (!info) return 0;
603 return info->data_size;
606 /* destroy a process when its refcount is 0 */
607 static void process_destroy( struct object *obj )
609 struct process *process = (struct process *)obj;
610 assert( obj->ops == &process_ops );
612 /* we can't have a thread remaining */
613 assert( list_empty( &process->thread_list ));
615 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
617 close_process_handles( process );
618 set_process_startup_state( process, STARTUP_ABORTED );
620 if (process->job)
622 list_remove( &process->job_entry );
623 release_object( process->job );
625 if (process->console) release_object( process->console );
626 if (process->parent) release_object( process->parent );
627 if (process->msg_fd) release_object( process->msg_fd );
628 list_remove( &process->entry );
629 if (process->idle_event) release_object( process->idle_event );
630 if (process->id) free_ptid( process->id );
631 if (process->token) release_object( process->token );
634 /* dump a process on stdout for debugging purposes */
635 static void process_dump( struct object *obj, int verbose )
637 struct process *process = (struct process *)obj;
638 assert( obj->ops == &process_ops );
640 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
643 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
645 struct process *process = (struct process *)obj;
646 return !process->running_threads;
649 static unsigned int process_map_access( struct object *obj, unsigned int access )
651 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
652 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
653 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
654 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
655 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
657 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
658 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
660 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
663 static void process_poll_event( struct fd *fd, int event )
665 struct process *process = get_fd_user( fd );
666 assert( process->obj.ops == &process_ops );
668 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
669 else if (event & POLLIN) receive_fd( process );
672 static void startup_info_destroy( struct object *obj )
674 struct startup_info *info = (struct startup_info *)obj;
675 assert( obj->ops == &startup_info_ops );
676 free( info->data );
677 if (info->exe_file) release_object( info->exe_file );
678 if (info->process) release_object( info->process );
681 static void startup_info_dump( struct object *obj, int verbose )
683 struct startup_info *info = (struct startup_info *)obj;
684 assert( obj->ops == &startup_info_ops );
686 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
687 info->data->hstdin, info->data->hstdout, info->data->hstderr );
690 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
692 struct startup_info *info = (struct startup_info *)obj;
693 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
696 /* get a process from an id (and increment the refcount) */
697 struct process *get_process_from_id( process_id_t id )
699 struct object *obj = get_ptid_entry( id );
701 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
702 set_error( STATUS_INVALID_PARAMETER );
703 return NULL;
706 /* get a process from a handle (and increment the refcount) */
707 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
709 return (struct process *)get_handle_obj( current->process, handle,
710 access, &process_ops );
713 /* find a dll from its base address */
714 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
716 struct process_dll *dll;
718 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
720 if (dll->base == base) return dll;
722 return NULL;
725 /* add a dll to a process list */
726 static struct process_dll *process_load_dll( struct process *process, struct mapping *mapping,
727 mod_handle_t base, const WCHAR *filename,
728 data_size_t name_len )
730 struct process_dll *dll;
732 /* make sure we don't already have one with the same base address */
733 if (find_process_dll( process, base ))
735 set_error( STATUS_INVALID_PARAMETER );
736 return NULL;
739 if ((dll = mem_alloc( sizeof(*dll) )))
741 dll->mapping = NULL;
742 dll->base = base;
743 dll->filename = NULL;
744 dll->namelen = name_len;
745 if (name_len && !(dll->filename = memdup( filename, name_len )))
747 free( dll );
748 return NULL;
750 if (mapping) dll->mapping = grab_mapping_unless_removable( mapping );
751 list_add_tail( &process->dlls, &dll->entry );
753 return dll;
756 /* remove a dll from a process list */
757 static void process_unload_dll( struct process *process, mod_handle_t base )
759 struct process_dll *dll = find_process_dll( process, base );
761 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
763 if (dll->mapping) release_object( dll->mapping );
764 free( dll->filename );
765 list_remove( &dll->entry );
766 free( dll );
767 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
769 else set_error( STATUS_INVALID_PARAMETER );
772 /* terminate a process with the given exit code */
773 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
775 struct thread *thread;
777 grab_object( process ); /* make sure it doesn't get freed when threads die */
778 process->is_terminating = 1;
780 restart:
781 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
783 if (exit_code) thread->exit_code = exit_code;
784 if (thread == skip) continue;
785 if (thread->state == TERMINATED) continue;
786 kill_thread( thread, 1 );
787 goto restart;
789 release_object( process );
792 /* kill all processes */
793 static void kill_all_processes(void)
795 for (;;)
797 struct process *process;
799 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
801 if (process->running_threads) break;
803 if (&process->entry == &process_list) break; /* no process found */
804 terminate_process( process, NULL, 1 );
808 /* kill all processes being attached to a console renderer */
809 void kill_console_processes( struct thread *renderer, int exit_code )
811 for (;;) /* restart from the beginning of the list every time */
813 struct process *process;
815 /* find the first process being attached to 'renderer' and still running */
816 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
818 if (process == renderer->process) continue;
819 if (!process->running_threads) continue;
820 if (process->console && console_get_renderer( process->console ) == renderer) break;
822 if (&process->entry == &process_list) break; /* no process found */
823 terminate_process( process, NULL, exit_code );
827 /* a process has been killed (i.e. its last thread died) */
828 static void process_killed( struct process *process )
830 struct list *ptr;
832 assert( list_empty( &process->thread_list ));
833 process->end_time = current_time;
834 if (!process->is_system) close_process_desktop( process );
835 process->winstation = 0;
836 process->desktop = 0;
837 close_process_handles( process );
838 if (process->idle_event)
840 release_object( process->idle_event );
841 process->idle_event = NULL;
844 /* close the console attached to this process, if any */
845 free_console( process );
847 while ((ptr = list_head( &process->rawinput_devices )))
849 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
850 list_remove( &entry->entry );
851 free( entry );
853 while ((ptr = list_head( &process->dlls )))
855 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
856 if (dll->mapping) release_object( dll->mapping );
857 free( dll->filename );
858 list_remove( &dll->entry );
859 free( dll );
861 destroy_process_classes( process );
862 free_process_user_handles( process );
863 remove_process_locks( process );
864 set_process_startup_state( process, STARTUP_ABORTED );
865 finish_process_tracing( process );
866 release_job_process( process );
867 start_sigkill_timer( process );
868 wake_up( &process->obj, 0 );
871 /* add a thread to a process running threads list */
872 void add_process_thread( struct process *process, struct thread *thread )
874 list_add_tail( &process->thread_list, &thread->proc_entry );
875 if (!process->running_threads++)
877 running_processes++;
878 if (!process->is_system)
880 if (!user_processes++ && shutdown_timeout)
882 remove_timeout_user( shutdown_timeout );
883 shutdown_timeout = NULL;
887 grab_object( thread );
890 /* remove a thread from a process running threads list */
891 void remove_process_thread( struct process *process, struct thread *thread )
893 assert( process->running_threads > 0 );
894 assert( !list_empty( &process->thread_list ));
896 list_remove( &thread->proc_entry );
898 if (!--process->running_threads)
900 /* we have removed the last running thread, exit the process */
901 process->exit_code = thread->exit_code;
902 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
903 process_killed( process );
905 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
906 release_object( thread );
909 /* suspend all the threads of a process */
910 void suspend_process( struct process *process )
912 if (!process->suspend++)
914 struct list *ptr, *next;
916 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
918 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
919 if (!thread->suspend) stop_thread( thread );
924 /* resume all the threads of a process */
925 void resume_process( struct process *process )
927 assert (process->suspend > 0);
928 if (!--process->suspend)
930 struct list *ptr, *next;
932 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
934 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
935 if (!thread->suspend) wake_thread( thread );
940 /* kill a process on the spot */
941 void kill_process( struct process *process, int violent_death )
943 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
945 release_object( process->msg_fd );
946 process->msg_fd = NULL;
949 if (process->sigkill_timeout) /* already waiting for it to die */
951 remove_timeout_user( process->sigkill_timeout );
952 process->sigkill_timeout = NULL;
953 process_died( process );
954 return;
957 if (violent_death) terminate_process( process, NULL, 1 );
958 else
960 struct list *ptr;
962 grab_object( process ); /* make sure it doesn't get freed when threads die */
963 while ((ptr = list_head( &process->thread_list )))
965 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
966 kill_thread( thread, 0 );
968 release_object( process );
972 /* kill all processes being debugged by a given thread */
973 void kill_debugged_processes( struct thread *debugger, int exit_code )
975 for (;;) /* restart from the beginning of the list every time */
977 struct process *process;
979 /* find the first process being debugged by 'debugger' and still running */
980 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
982 if (!process->running_threads) continue;
983 if (process->debugger == debugger) break;
985 if (&process->entry == &process_list) break; /* no process found */
986 process->debugger = NULL;
987 terminate_process( process, NULL, exit_code );
992 /* trigger a breakpoint event in a given process */
993 void break_process( struct process *process )
995 struct thread *thread;
997 suspend_process( process );
999 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1001 if (thread->context) /* inside an exception event already */
1003 break_thread( thread );
1004 goto done;
1007 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1008 else set_error( STATUS_ACCESS_DENIED );
1009 done:
1010 resume_process( process );
1014 /* detach a debugger from all its debuggees */
1015 void detach_debugged_processes( struct thread *debugger )
1017 struct process *process;
1019 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1021 if (process->debugger == debugger && process->running_threads)
1023 debugger_detach( process, debugger );
1029 void enum_processes( int (*cb)(struct process*, void*), void *user )
1031 struct list *ptr, *next;
1033 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1035 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1036 if ((cb)(process, user)) break;
1040 /* set the debugged flag in the process PEB */
1041 int set_process_debug_flag( struct process *process, int flag )
1043 char data = (flag != 0);
1045 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1046 return write_process_memory( process, process->peb + 2, 1, &data );
1049 /* take a snapshot of currently running processes */
1050 struct process_snapshot *process_snap( int *count )
1052 struct process_snapshot *snapshot, *ptr;
1053 struct process *process;
1055 if (!running_processes) return NULL;
1056 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1057 return NULL;
1058 ptr = snapshot;
1059 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1061 if (!process->running_threads) continue;
1062 ptr->process = process;
1063 ptr->threads = process->running_threads;
1064 ptr->count = process->obj.refcount;
1065 ptr->priority = process->priority;
1066 ptr->handles = get_handle_table_count(process);
1067 ptr->unix_pid = process->unix_pid;
1068 grab_object( process );
1069 ptr++;
1072 if (!(*count = ptr - snapshot))
1074 free( snapshot );
1075 snapshot = NULL;
1077 return snapshot;
1080 /* create a new process */
1081 DECL_HANDLER(new_process)
1083 struct startup_info *info;
1084 struct thread *thread;
1085 struct process *process;
1086 struct process *parent = current->process;
1087 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1089 if (socket_fd == -1)
1091 set_error( STATUS_INVALID_PARAMETER );
1092 return;
1094 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1096 set_error( STATUS_INVALID_HANDLE );
1097 close( socket_fd );
1098 return;
1100 if (shutdown_stage)
1102 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1103 close( socket_fd );
1104 return;
1106 if (!is_cpu_supported( req->cpu ))
1108 close( socket_fd );
1109 return;
1112 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1113 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1115 set_error( STATUS_ACCESS_DENIED );
1116 close( socket_fd );
1117 return;
1120 if (!req->info_size) /* create an orphaned process */
1122 create_process( socket_fd, NULL, 0 );
1123 return;
1126 /* build the startup info for a new process */
1127 if (!(info = alloc_object( &startup_info_ops )))
1129 close( socket_fd );
1130 return;
1132 info->exe_file = NULL;
1133 info->process = NULL;
1134 info->data = NULL;
1136 if (req->exe_file &&
1137 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1139 close( socket_fd );
1140 goto done;
1143 info->data_size = get_req_data_size();
1144 info->info_size = min( req->info_size, info->data_size );
1146 if (req->info_size < sizeof(*info->data))
1148 /* make sure we have a full startup_info_t structure */
1149 data_size_t env_size = info->data_size - info->info_size;
1150 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1152 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1154 close( socket_fd );
1155 goto done;
1157 memcpy( info->data, get_req_data(), info_size );
1158 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1159 memcpy( info->data + 1, (const char *)get_req_data() + req->info_size, env_size );
1160 info->info_size = sizeof(startup_info_t);
1161 info->data_size = info->info_size + env_size;
1163 else
1165 data_size_t pos = sizeof(*info->data);
1167 if (!(info->data = memdup( get_req_data(), info->data_size )))
1169 close( socket_fd );
1170 goto done;
1172 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1173 FIXUP_LEN( info->data->curdir_len );
1174 FIXUP_LEN( info->data->dllpath_len );
1175 FIXUP_LEN( info->data->imagepath_len );
1176 FIXUP_LEN( info->data->cmdline_len );
1177 FIXUP_LEN( info->data->title_len );
1178 FIXUP_LEN( info->data->desktop_len );
1179 FIXUP_LEN( info->data->shellinfo_len );
1180 FIXUP_LEN( info->data->runtime_len );
1181 #undef FIXUP_LEN
1184 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
1185 process = thread->process;
1186 process->startup_info = (struct startup_info *)grab_object( info );
1188 if (parent->job
1189 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1190 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1192 add_job_process( parent->job, process );
1195 /* connect to the window station */
1196 connect_process_winstation( process, current );
1198 /* thread will be actually suspended in init_done */
1199 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
1201 /* set the process console */
1202 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1204 /* FIXME: some better error checking should be done...
1205 * like if hConOut and hConIn are console handles, then they should be on the same
1206 * physical console
1208 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1211 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1213 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1214 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1215 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1216 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1217 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1218 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1219 /* some handles above may have been invalid; this is not an error */
1220 if (get_error() == STATUS_INVALID_HANDLE ||
1221 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1224 /* attach to the debugger if requested */
1225 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1227 set_process_debugger( process, current );
1228 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1230 else if (parent->debugger && parent->debug_children)
1232 set_process_debugger( process, parent->debugger );
1233 /* debug_children is set to 1 by default */
1236 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1237 process->group_id = parent->group_id;
1239 info->process = (struct process *)grab_object( process );
1240 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1241 reply->pid = get_process_id( process );
1242 reply->tid = get_thread_id( thread );
1243 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
1244 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
1246 done:
1247 release_object( info );
1250 /* Retrieve information about a newly started process */
1251 DECL_HANDLER(get_new_process_info)
1253 struct startup_info *info;
1255 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1256 0, &startup_info_ops )))
1258 reply->success = is_process_init_done( info->process );
1259 reply->exit_code = info->process->exit_code;
1260 release_object( info );
1264 /* Retrieve the new process startup info */
1265 DECL_HANDLER(get_startup_info)
1267 struct process *process = current->process;
1268 struct startup_info *info = process->startup_info;
1269 data_size_t size;
1271 if (!info) return;
1273 if (info->exe_file &&
1274 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
1276 /* we return the data directly without making a copy so this can only be called once */
1277 reply->info_size = info->info_size;
1278 size = info->data_size;
1279 if (size > get_reply_max_size()) size = get_reply_max_size();
1280 set_reply_data_ptr( info->data, size );
1281 info->data = NULL;
1282 info->data_size = 0;
1285 /* signal the end of the process initialization */
1286 DECL_HANDLER(init_process_done)
1288 struct process_dll *dll;
1289 struct process *process = current->process;
1291 if (is_process_init_done(process))
1293 set_error( STATUS_INVALID_PARAMETER );
1294 return;
1296 if (!(dll = find_process_dll( process, req->module )))
1298 set_error( STATUS_DLL_NOT_FOUND );
1299 return;
1302 /* main exe is the first in the dll list */
1303 list_remove( &dll->entry );
1304 list_add_head( &process->dlls, &dll->entry );
1306 process->ldt_copy = req->ldt_copy;
1307 process->start_time = current_time;
1308 current->entry_point = req->entry;
1310 generate_startup_debug_events( process, req->entry );
1311 set_process_startup_state( process, STARTUP_DONE );
1313 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1314 stop_thread_if_suspended( current );
1315 if (process->debugger) set_process_debug_flag( process, 1 );
1318 /* open a handle to a process */
1319 DECL_HANDLER(open_process)
1321 struct process *process = get_process_from_id( req->pid );
1322 reply->handle = 0;
1323 if (process)
1325 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1326 release_object( process );
1330 /* terminate a process */
1331 DECL_HANDLER(terminate_process)
1333 struct process *process;
1335 if (req->handle)
1337 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1338 if (!process) return;
1340 else process = (struct process *)grab_object( current->process );
1342 reply->self = (current->process == process);
1343 terminate_process( process, current, req->exit_code );
1344 release_object( process );
1347 /* fetch information about a process */
1348 DECL_HANDLER(get_process_info)
1350 struct process *process;
1352 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1354 reply->pid = get_process_id( process );
1355 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1356 reply->exit_code = process->exit_code;
1357 reply->priority = process->priority;
1358 reply->affinity = process->affinity;
1359 reply->peb = process->peb;
1360 reply->start_time = process->start_time;
1361 reply->end_time = process->end_time;
1362 reply->cpu = process->cpu;
1363 reply->debugger_present = !!process->debugger;
1364 reply->debug_children = process->debug_children;
1365 release_object( process );
1369 static void set_process_affinity( struct process *process, affinity_t affinity )
1371 struct thread *thread;
1373 if (!process->running_threads)
1375 set_error( STATUS_PROCESS_IS_TERMINATING );
1376 return;
1379 process->affinity = affinity;
1381 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1383 set_thread_affinity( thread, affinity );
1387 /* set information about a process */
1388 DECL_HANDLER(set_process_info)
1390 struct process *process;
1392 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1394 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1395 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1396 release_object( process );
1400 /* read data from a process address space */
1401 DECL_HANDLER(read_process_memory)
1403 struct process *process;
1404 data_size_t len = get_reply_max_size();
1406 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1408 if (len)
1410 char *buffer = mem_alloc( len );
1411 if (buffer)
1413 if (read_process_memory( process, req->addr, len, buffer ))
1414 set_reply_data_ptr( buffer, len );
1415 else
1416 free( buffer );
1419 release_object( process );
1422 /* write data to a process address space */
1423 DECL_HANDLER(write_process_memory)
1425 struct process *process;
1427 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1429 data_size_t len = get_req_data_size();
1430 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1431 else set_error( STATUS_INVALID_PARAMETER );
1432 release_object( process );
1436 /* notify the server that a dll has been loaded */
1437 DECL_HANDLER(load_dll)
1439 struct process_dll *dll;
1440 struct mapping *mapping = NULL;
1442 if (req->mapping && !(mapping = get_mapping_obj( current->process, req->mapping, SECTION_QUERY )))
1443 return;
1445 if ((dll = process_load_dll( current->process, mapping, req->base,
1446 get_req_data(), get_req_data_size() )))
1448 dll->size = req->size;
1449 dll->dbg_offset = req->dbg_offset;
1450 dll->dbg_size = req->dbg_size;
1451 dll->name = req->name;
1452 /* only generate event if initialization is done */
1453 if (is_process_init_done( current->process ))
1454 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1456 if (mapping) release_object( mapping );
1459 /* notify the server that a dll is being unloaded */
1460 DECL_HANDLER(unload_dll)
1462 process_unload_dll( current->process, req->base );
1465 /* retrieve information about a module in a process */
1466 DECL_HANDLER(get_dll_info)
1468 struct process *process;
1470 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1472 struct process_dll *dll;
1474 if (req->base_address)
1475 dll = find_process_dll( process, req->base_address );
1476 else /* NULL means main module */
1477 dll = list_head( &process->dlls ) ?
1478 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1480 if (dll)
1482 reply->size = dll->size;
1483 reply->entry_point = 0; /* FIXME */
1484 reply->filename_len = dll->namelen;
1485 if (dll->filename)
1487 if (dll->namelen <= get_reply_max_size())
1488 set_reply_data( dll->filename, dll->namelen );
1489 else
1490 set_error( STATUS_BUFFER_TOO_SMALL );
1493 else
1494 set_error( STATUS_DLL_NOT_FOUND );
1496 release_object( process );
1500 /* retrieve the process idle event */
1501 DECL_HANDLER(get_process_idle_event)
1503 struct process *process;
1505 reply->event = 0;
1506 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1508 if (process->idle_event && process != current->process)
1509 reply->event = alloc_handle( current->process, process->idle_event,
1510 EVENT_ALL_ACCESS, 0 );
1511 release_object( process );
1515 /* make the current process a system process */
1516 DECL_HANDLER(make_process_system)
1518 struct process *process = current->process;
1520 if (!shutdown_event)
1522 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1523 make_object_static( (struct object *)shutdown_event );
1526 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1527 return;
1529 if (!process->is_system)
1531 process->is_system = 1;
1532 close_process_desktop( process );
1533 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1534 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1538 /* create a new job object */
1539 DECL_HANDLER(create_job)
1541 struct job *job;
1542 struct unicode_str name;
1543 struct object *root;
1544 const struct security_descriptor *sd;
1545 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1547 if (!objattr) 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 /* open a job object */
1562 DECL_HANDLER(open_job)
1564 struct unicode_str name = get_req_unicode_str();
1566 reply->handle = open_object( current->process, req->rootdir, req->access,
1567 &job_ops, &name, req->attributes );
1570 /* assign a job object to a process */
1571 DECL_HANDLER(assign_job)
1573 struct process *process;
1574 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1576 if (!job) return;
1578 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1580 add_job_process( job, process );
1581 release_object( process );
1583 release_object( job );
1587 /* check if a process is associated with a job */
1588 DECL_HANDLER(process_in_job)
1590 struct process *process;
1591 struct job *job;
1593 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1594 return;
1596 if (!req->job)
1598 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1600 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1602 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1603 release_object( job );
1605 release_object( process );
1608 /* terminate all processes associated with the job */
1609 DECL_HANDLER(terminate_job)
1611 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1613 if (!job) return;
1615 terminate_job( job, req->status );
1616 release_object( job );
1619 /* update limits of the job object */
1620 DECL_HANDLER(set_job_limits)
1622 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1624 if (!job) return;
1626 job->limit_flags = req->limit_flags;
1627 release_object( job );
1630 /* set the jobs completion port */
1631 DECL_HANDLER(set_job_completion_port)
1633 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1635 if (!job) return;
1637 if (!job->completion_port)
1639 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1640 job->completion_key = req->key;
1642 else
1643 set_error( STATUS_INVALID_PARAMETER );
1645 release_object( job );