server: Add link_name and unlink_name object operations.
[wine.git] / server / process.c
blobd51c8847b943c2884c524dd0a7a9a2c709073bfa
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 no_link_name, /* link_name */
178 NULL, /* 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 directory *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_dir( root, name, attr, &job_ops )))
191 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
193 /* initialize it if it didn't already exist */
194 if (sd) default_set_sd( &job->obj, sd, OWNER_SECURITY_INFORMATION |
195 GROUP_SECURITY_INFORMATION |
196 DACL_SECURITY_INFORMATION |
197 SACL_SECURITY_INFORMATION );
198 list_init( &job->process_list );
199 job->num_processes = 0;
200 job->limit_flags = 0;
201 job->terminating = 0;
202 job->signaled = 0;
203 job->completion_port = NULL;
204 job->completion_key = 0;
207 return job;
210 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
212 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
215 static struct object_type *job_get_type( struct object *obj )
217 static const WCHAR name[] = {'J','o','b'};
218 static const struct unicode_str str = { name, sizeof(name) };
219 return get_object_type( &str );
222 static unsigned int job_map_access( struct object *obj, unsigned int access )
224 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
225 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
226 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
227 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
228 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
231 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
233 if (job->completion_port)
234 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
237 static void add_job_process( struct job *job, struct process *process )
239 if (!process->running_threads)
241 set_error( STATUS_PROCESS_IS_TERMINATING );
242 return;
244 if (process->job)
246 set_error( STATUS_ACCESS_DENIED );
247 return;
249 process->job = (struct job *)grab_object( job );
250 list_add_tail( &job->process_list, &process->job_entry );
251 job->num_processes++;
253 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
256 /* called when a process has terminated, allow one additional process */
257 static void release_job_process( struct process *process )
259 struct job *job = process->job;
261 if (!job) return;
263 assert( job->num_processes );
264 job->num_processes--;
266 if (!job->terminating)
267 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
269 if (!job->num_processes)
270 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
273 static void terminate_job( struct job *job, int exit_code )
275 /* don't report completion events for terminated processes */
276 job->terminating = 1;
278 for (;;) /* restart from the beginning of the list every time */
280 struct process *process;
282 /* find the first process associated with this job and still running */
283 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
285 if (process->running_threads) break;
287 if (&process->job_entry == &job->process_list) break; /* no process found */
288 assert( process->job == job );
289 terminate_process( process, NULL, exit_code );
292 job->terminating = 0;
293 job->signaled = 1;
294 wake_up( &job->obj, 0 );
297 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
299 struct job *job = (struct job *)obj;
300 assert( obj->ops == &job_ops );
302 if (obj->handle_count == 1) /* last handle */
304 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
305 terminate_job( job, 0 );
307 return 1;
310 static void job_destroy( struct object *obj )
312 struct job *job = (struct job *)obj;
313 assert( obj->ops == &job_ops );
315 assert( !job->num_processes );
316 assert( list_empty(&job->process_list) );
318 if (job->completion_port) release_object( job->completion_port );
321 static void job_dump( struct object *obj, int verbose )
323 struct job *job = (struct job *)obj;
324 assert( obj->ops == &job_ops );
325 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
328 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
330 struct job *job = (struct job *)obj;
331 return job->signaled;
334 struct ptid_entry
336 void *ptr; /* entry ptr */
337 unsigned int next; /* next free entry */
340 static struct ptid_entry *ptid_entries; /* array of ptid entries */
341 static unsigned int used_ptid_entries; /* number of entries in use */
342 static unsigned int alloc_ptid_entries; /* number of allocated entries */
343 static unsigned int next_free_ptid; /* next free entry */
344 static unsigned int last_free_ptid; /* last free entry */
346 static void kill_all_processes(void);
348 #define PTID_OFFSET 8 /* offset for first ptid value */
350 /* allocate a new process or thread id */
351 unsigned int alloc_ptid( void *ptr )
353 struct ptid_entry *entry;
354 unsigned int id;
356 if (used_ptid_entries < alloc_ptid_entries)
358 id = used_ptid_entries + PTID_OFFSET;
359 entry = &ptid_entries[used_ptid_entries++];
361 else if (next_free_ptid)
363 id = next_free_ptid;
364 entry = &ptid_entries[id - PTID_OFFSET];
365 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
367 else /* need to grow the array */
369 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
370 if (!count) count = 64;
371 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
373 set_error( STATUS_NO_MEMORY );
374 return 0;
376 ptid_entries = entry;
377 alloc_ptid_entries = count;
378 id = used_ptid_entries + PTID_OFFSET;
379 entry = &ptid_entries[used_ptid_entries++];
382 entry->ptr = ptr;
383 return id;
386 /* free a process or thread id */
387 void free_ptid( unsigned int id )
389 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
391 entry->ptr = NULL;
392 entry->next = 0;
394 /* append to end of free list so that we don't reuse it too early */
395 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
396 else next_free_ptid = id;
398 last_free_ptid = id;
401 /* retrieve the pointer corresponding to a process or thread id */
402 void *get_ptid_entry( unsigned int id )
404 if (id < PTID_OFFSET) return NULL;
405 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
406 return ptid_entries[id - PTID_OFFSET].ptr;
409 /* return the main thread of the process */
410 struct thread *get_process_first_thread( struct process *process )
412 struct list *ptr = list_head( &process->thread_list );
413 if (!ptr) return NULL;
414 return LIST_ENTRY( ptr, struct thread, proc_entry );
417 /* set the state of the process startup info */
418 static void set_process_startup_state( struct process *process, enum startup_state state )
420 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
421 if (process->startup_info)
423 wake_up( &process->startup_info->obj, 0 );
424 release_object( process->startup_info );
425 process->startup_info = NULL;
429 /* callback for server shutdown */
430 static void server_shutdown_timeout( void *arg )
432 shutdown_timeout = NULL;
433 if (!running_processes)
435 close_master_socket( 0 );
436 return;
438 switch(++shutdown_stage)
440 case 1: /* signal system processes to exit */
441 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
442 if (shutdown_event) set_event( shutdown_event );
443 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
444 close_master_socket( 4 * -TICKS_PER_SEC );
445 break;
446 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
447 kill_all_processes();
448 break;
452 /* forced shutdown, used for wineserver -k */
453 void shutdown_master_socket(void)
455 kill_all_processes();
456 shutdown_stage = 2;
457 if (shutdown_timeout)
459 remove_timeout_user( shutdown_timeout );
460 shutdown_timeout = NULL;
462 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
465 /* final cleanup once we are sure a process is really dead */
466 static void process_died( struct process *process )
468 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
469 if (!process->is_system)
471 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
472 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
474 release_object( process );
475 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
478 /* callback for process sigkill timeout */
479 static void process_sigkill( void *private )
481 struct process *process = private;
483 process->sigkill_timeout = NULL;
484 kill( process->unix_pid, SIGKILL );
485 process_died( process );
488 /* start the sigkill timer for a process upon exit */
489 static void start_sigkill_timer( struct process *process )
491 grab_object( process );
492 if (process->unix_pid != -1 && process->msg_fd)
493 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
494 else
495 process_died( process );
498 /* create a new process and its main thread */
499 /* if the function fails the fd is closed */
500 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
502 struct process *process;
503 struct thread *thread = NULL;
504 int request_pipe[2];
506 if (!(process = alloc_object( &process_ops )))
508 close( fd );
509 goto error;
511 process->parent = NULL;
512 process->debugger = NULL;
513 process->handles = NULL;
514 process->msg_fd = NULL;
515 process->sigkill_timeout = NULL;
516 process->unix_pid = -1;
517 process->exit_code = STILL_ACTIVE;
518 process->running_threads = 0;
519 process->priority = PROCESS_PRIOCLASS_NORMAL;
520 process->suspend = 0;
521 process->is_system = 0;
522 process->debug_children = 1;
523 process->is_terminating = 0;
524 process->job = NULL;
525 process->console = NULL;
526 process->startup_state = STARTUP_IN_PROGRESS;
527 process->startup_info = NULL;
528 process->idle_event = NULL;
529 process->peb = 0;
530 process->ldt_copy = 0;
531 process->winstation = 0;
532 process->desktop = 0;
533 process->token = NULL;
534 process->trace_data = 0;
535 process->rawinput_mouse = NULL;
536 process->rawinput_kbd = NULL;
537 list_init( &process->thread_list );
538 list_init( &process->locks );
539 list_init( &process->classes );
540 list_init( &process->dlls );
541 list_init( &process->rawinput_devices );
543 process->end_time = 0;
544 list_add_tail( &process_list, &process->entry );
546 if (!(process->id = process->group_id = alloc_ptid( process )))
548 close( fd );
549 goto error;
551 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
553 /* create the handle table */
554 if (!parent_thread)
556 process->handles = alloc_handle_table( process, 0 );
557 process->token = token_create_admin();
558 process->affinity = ~0;
560 else
562 struct process *parent = parent_thread->process;
563 process->parent = (struct process *)grab_object( parent );
564 process->handles = inherit_all ? copy_handle_table( process, parent )
565 : alloc_handle_table( process, 0 );
566 /* Note: for security reasons, starting a new process does not attempt
567 * to use the current impersonation token for the new process */
568 process->token = token_duplicate( parent->token, TRUE, 0 );
569 process->affinity = parent->affinity;
571 if (!process->handles || !process->token) goto error;
573 /* create the main thread */
574 if (pipe( request_pipe ) == -1)
576 file_set_error();
577 goto error;
579 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
581 close( request_pipe[0] );
582 close( request_pipe[1] );
583 goto error;
585 close( request_pipe[1] );
586 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
588 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
589 release_object( process );
590 return thread;
592 error:
593 if (process) release_object( process );
594 /* if we failed to start our first process, close everything down */
595 if (!running_processes) close_master_socket( 0 );
596 return NULL;
599 /* initialize the current process and fill in the request */
600 data_size_t init_process( struct thread *thread )
602 struct process *process = thread->process;
603 struct startup_info *info = process->startup_info;
605 init_process_tracing( process );
606 if (!info) return 0;
607 return info->data_size;
610 /* destroy a process when its refcount is 0 */
611 static void process_destroy( struct object *obj )
613 struct process *process = (struct process *)obj;
614 assert( obj->ops == &process_ops );
616 /* we can't have a thread remaining */
617 assert( list_empty( &process->thread_list ));
619 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
621 close_process_handles( process );
622 set_process_startup_state( process, STARTUP_ABORTED );
624 if (process->job)
626 list_remove( &process->job_entry );
627 release_object( process->job );
629 if (process->console) release_object( process->console );
630 if (process->parent) release_object( process->parent );
631 if (process->msg_fd) release_object( process->msg_fd );
632 list_remove( &process->entry );
633 if (process->idle_event) release_object( process->idle_event );
634 if (process->id) free_ptid( process->id );
635 if (process->token) release_object( process->token );
638 /* dump a process on stdout for debugging purposes */
639 static void process_dump( struct object *obj, int verbose )
641 struct process *process = (struct process *)obj;
642 assert( obj->ops == &process_ops );
644 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
647 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
649 struct process *process = (struct process *)obj;
650 return !process->running_threads;
653 static unsigned int process_map_access( struct object *obj, unsigned int access )
655 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
656 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
657 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
658 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
659 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
661 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
662 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
664 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
667 static void process_poll_event( struct fd *fd, int event )
669 struct process *process = get_fd_user( fd );
670 assert( process->obj.ops == &process_ops );
672 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
673 else if (event & POLLIN) receive_fd( process );
676 static void startup_info_destroy( struct object *obj )
678 struct startup_info *info = (struct startup_info *)obj;
679 assert( obj->ops == &startup_info_ops );
680 free( info->data );
681 if (info->exe_file) release_object( info->exe_file );
682 if (info->process) release_object( info->process );
685 static void startup_info_dump( struct object *obj, int verbose )
687 struct startup_info *info = (struct startup_info *)obj;
688 assert( obj->ops == &startup_info_ops );
690 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
691 info->data->hstdin, info->data->hstdout, info->data->hstderr );
694 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
696 struct startup_info *info = (struct startup_info *)obj;
697 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
700 /* get a process from an id (and increment the refcount) */
701 struct process *get_process_from_id( process_id_t id )
703 struct object *obj = get_ptid_entry( id );
705 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
706 set_error( STATUS_INVALID_PARAMETER );
707 return NULL;
710 /* get a process from a handle (and increment the refcount) */
711 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
713 return (struct process *)get_handle_obj( current->process, handle,
714 access, &process_ops );
717 /* find a dll from its base address */
718 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
720 struct process_dll *dll;
722 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
724 if (dll->base == base) return dll;
726 return NULL;
729 /* add a dll to a process list */
730 static struct process_dll *process_load_dll( struct process *process, struct mapping *mapping,
731 mod_handle_t base, const WCHAR *filename,
732 data_size_t name_len )
734 struct process_dll *dll;
736 /* make sure we don't already have one with the same base address */
737 if (find_process_dll( process, base ))
739 set_error( STATUS_INVALID_PARAMETER );
740 return NULL;
743 if ((dll = mem_alloc( sizeof(*dll) )))
745 dll->mapping = NULL;
746 dll->base = base;
747 dll->filename = NULL;
748 dll->namelen = name_len;
749 if (name_len && !(dll->filename = memdup( filename, name_len )))
751 free( dll );
752 return NULL;
754 if (mapping) dll->mapping = grab_mapping_unless_removable( mapping );
755 list_add_tail( &process->dlls, &dll->entry );
757 return dll;
760 /* remove a dll from a process list */
761 static void process_unload_dll( struct process *process, mod_handle_t base )
763 struct process_dll *dll = find_process_dll( process, base );
765 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
767 if (dll->mapping) release_object( dll->mapping );
768 free( dll->filename );
769 list_remove( &dll->entry );
770 free( dll );
771 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
773 else set_error( STATUS_INVALID_PARAMETER );
776 /* terminate a process with the given exit code */
777 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
779 struct thread *thread;
781 grab_object( process ); /* make sure it doesn't get freed when threads die */
782 process->is_terminating = 1;
784 restart:
785 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
787 if (exit_code) thread->exit_code = exit_code;
788 if (thread == skip) continue;
789 if (thread->state == TERMINATED) continue;
790 kill_thread( thread, 1 );
791 goto restart;
793 release_object( process );
796 /* kill all processes */
797 static void kill_all_processes(void)
799 for (;;)
801 struct process *process;
803 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
805 if (process->running_threads) break;
807 if (&process->entry == &process_list) break; /* no process found */
808 terminate_process( process, NULL, 1 );
812 /* kill all processes being attached to a console renderer */
813 void kill_console_processes( struct thread *renderer, int exit_code )
815 for (;;) /* restart from the beginning of the list every time */
817 struct process *process;
819 /* find the first process being attached to 'renderer' and still running */
820 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
822 if (process == renderer->process) continue;
823 if (!process->running_threads) continue;
824 if (process->console && console_get_renderer( process->console ) == renderer) break;
826 if (&process->entry == &process_list) break; /* no process found */
827 terminate_process( process, NULL, exit_code );
831 /* a process has been killed (i.e. its last thread died) */
832 static void process_killed( struct process *process )
834 struct list *ptr;
836 assert( list_empty( &process->thread_list ));
837 process->end_time = current_time;
838 if (!process->is_system) close_process_desktop( process );
839 process->winstation = 0;
840 process->desktop = 0;
841 close_process_handles( process );
842 if (process->idle_event)
844 release_object( process->idle_event );
845 process->idle_event = NULL;
848 /* close the console attached to this process, if any */
849 free_console( process );
851 while ((ptr = list_head( &process->rawinput_devices )))
853 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
854 list_remove( &entry->entry );
855 free( entry );
857 while ((ptr = list_head( &process->dlls )))
859 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
860 if (dll->mapping) release_object( dll->mapping );
861 free( dll->filename );
862 list_remove( &dll->entry );
863 free( dll );
865 destroy_process_classes( process );
866 free_process_user_handles( process );
867 remove_process_locks( process );
868 set_process_startup_state( process, STARTUP_ABORTED );
869 finish_process_tracing( process );
870 release_job_process( process );
871 start_sigkill_timer( process );
872 wake_up( &process->obj, 0 );
875 /* add a thread to a process running threads list */
876 void add_process_thread( struct process *process, struct thread *thread )
878 list_add_tail( &process->thread_list, &thread->proc_entry );
879 if (!process->running_threads++)
881 running_processes++;
882 if (!process->is_system)
884 if (!user_processes++ && shutdown_timeout)
886 remove_timeout_user( shutdown_timeout );
887 shutdown_timeout = NULL;
891 grab_object( thread );
894 /* remove a thread from a process running threads list */
895 void remove_process_thread( struct process *process, struct thread *thread )
897 assert( process->running_threads > 0 );
898 assert( !list_empty( &process->thread_list ));
900 list_remove( &thread->proc_entry );
902 if (!--process->running_threads)
904 /* we have removed the last running thread, exit the process */
905 process->exit_code = thread->exit_code;
906 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
907 process_killed( process );
909 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
910 release_object( thread );
913 /* suspend all the threads of a process */
914 void suspend_process( struct process *process )
916 if (!process->suspend++)
918 struct list *ptr, *next;
920 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
922 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
923 if (!thread->suspend) stop_thread( thread );
928 /* resume all the threads of a process */
929 void resume_process( struct process *process )
931 assert (process->suspend > 0);
932 if (!--process->suspend)
934 struct list *ptr, *next;
936 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
938 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
939 if (!thread->suspend) wake_thread( thread );
944 /* kill a process on the spot */
945 void kill_process( struct process *process, int violent_death )
947 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
949 release_object( process->msg_fd );
950 process->msg_fd = NULL;
953 if (process->sigkill_timeout) /* already waiting for it to die */
955 remove_timeout_user( process->sigkill_timeout );
956 process->sigkill_timeout = NULL;
957 process_died( process );
958 return;
961 if (violent_death) terminate_process( process, NULL, 1 );
962 else
964 struct list *ptr;
966 grab_object( process ); /* make sure it doesn't get freed when threads die */
967 while ((ptr = list_head( &process->thread_list )))
969 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
970 kill_thread( thread, 0 );
972 release_object( process );
976 /* kill all processes being debugged by a given thread */
977 void kill_debugged_processes( struct thread *debugger, int exit_code )
979 for (;;) /* restart from the beginning of the list every time */
981 struct process *process;
983 /* find the first process being debugged by 'debugger' and still running */
984 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
986 if (!process->running_threads) continue;
987 if (process->debugger == debugger) break;
989 if (&process->entry == &process_list) break; /* no process found */
990 process->debugger = NULL;
991 terminate_process( process, NULL, exit_code );
996 /* trigger a breakpoint event in a given process */
997 void break_process( struct process *process )
999 struct thread *thread;
1001 suspend_process( process );
1003 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1005 if (thread->context) /* inside an exception event already */
1007 break_thread( thread );
1008 goto done;
1011 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1012 else set_error( STATUS_ACCESS_DENIED );
1013 done:
1014 resume_process( process );
1018 /* detach a debugger from all its debuggees */
1019 void detach_debugged_processes( struct thread *debugger )
1021 struct process *process;
1023 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1025 if (process->debugger == debugger && process->running_threads)
1027 debugger_detach( process, debugger );
1033 void enum_processes( int (*cb)(struct process*, void*), void *user )
1035 struct list *ptr, *next;
1037 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1039 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1040 if ((cb)(process, user)) break;
1044 /* set the debugged flag in the process PEB */
1045 int set_process_debug_flag( struct process *process, int flag )
1047 char data = (flag != 0);
1049 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1050 return write_process_memory( process, process->peb + 2, 1, &data );
1053 /* take a snapshot of currently running processes */
1054 struct process_snapshot *process_snap( int *count )
1056 struct process_snapshot *snapshot, *ptr;
1057 struct process *process;
1059 if (!running_processes) return NULL;
1060 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1061 return NULL;
1062 ptr = snapshot;
1063 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1065 if (!process->running_threads) continue;
1066 ptr->process = process;
1067 ptr->threads = process->running_threads;
1068 ptr->count = process->obj.refcount;
1069 ptr->priority = process->priority;
1070 ptr->handles = get_handle_table_count(process);
1071 ptr->unix_pid = process->unix_pid;
1072 grab_object( process );
1073 ptr++;
1076 if (!(*count = ptr - snapshot))
1078 free( snapshot );
1079 snapshot = NULL;
1081 return snapshot;
1084 /* create a new process */
1085 DECL_HANDLER(new_process)
1087 struct startup_info *info;
1088 struct thread *thread;
1089 struct process *process;
1090 struct process *parent = current->process;
1091 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1093 if (socket_fd == -1)
1095 set_error( STATUS_INVALID_PARAMETER );
1096 return;
1098 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1100 set_error( STATUS_INVALID_HANDLE );
1101 close( socket_fd );
1102 return;
1104 if (shutdown_stage)
1106 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1107 close( socket_fd );
1108 return;
1110 if (!is_cpu_supported( req->cpu ))
1112 close( socket_fd );
1113 return;
1116 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1117 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1119 set_error( STATUS_ACCESS_DENIED );
1120 close( socket_fd );
1121 return;
1124 if (!req->info_size) /* create an orphaned process */
1126 create_process( socket_fd, NULL, 0 );
1127 return;
1130 /* build the startup info for a new process */
1131 if (!(info = alloc_object( &startup_info_ops )))
1133 close( socket_fd );
1134 return;
1136 info->exe_file = NULL;
1137 info->process = NULL;
1138 info->data = NULL;
1140 if (req->exe_file &&
1141 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1143 close( socket_fd );
1144 goto done;
1147 info->data_size = get_req_data_size();
1148 info->info_size = min( req->info_size, info->data_size );
1150 if (req->info_size < sizeof(*info->data))
1152 /* make sure we have a full startup_info_t structure */
1153 data_size_t env_size = info->data_size - info->info_size;
1154 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1156 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1158 close( socket_fd );
1159 goto done;
1161 memcpy( info->data, get_req_data(), info_size );
1162 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1163 memcpy( info->data + 1, (const char *)get_req_data() + req->info_size, env_size );
1164 info->info_size = sizeof(startup_info_t);
1165 info->data_size = info->info_size + env_size;
1167 else
1169 data_size_t pos = sizeof(*info->data);
1171 if (!(info->data = memdup( get_req_data(), info->data_size )))
1173 close( socket_fd );
1174 goto done;
1176 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1177 FIXUP_LEN( info->data->curdir_len );
1178 FIXUP_LEN( info->data->dllpath_len );
1179 FIXUP_LEN( info->data->imagepath_len );
1180 FIXUP_LEN( info->data->cmdline_len );
1181 FIXUP_LEN( info->data->title_len );
1182 FIXUP_LEN( info->data->desktop_len );
1183 FIXUP_LEN( info->data->shellinfo_len );
1184 FIXUP_LEN( info->data->runtime_len );
1185 #undef FIXUP_LEN
1188 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
1189 process = thread->process;
1190 process->startup_info = (struct startup_info *)grab_object( info );
1192 if (parent->job
1193 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1194 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1196 add_job_process( parent->job, process );
1199 /* connect to the window station */
1200 connect_process_winstation( process, current );
1202 /* thread will be actually suspended in init_done */
1203 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
1205 /* set the process console */
1206 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1208 /* FIXME: some better error checking should be done...
1209 * like if hConOut and hConIn are console handles, then they should be on the same
1210 * physical console
1212 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1215 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1217 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1218 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1219 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1220 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1221 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1222 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1223 /* some handles above may have been invalid; this is not an error */
1224 if (get_error() == STATUS_INVALID_HANDLE ||
1225 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1228 /* attach to the debugger if requested */
1229 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1231 set_process_debugger( process, current );
1232 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1234 else if (parent->debugger && parent->debug_children)
1236 set_process_debugger( process, parent->debugger );
1237 /* debug_children is set to 1 by default */
1240 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1241 process->group_id = parent->group_id;
1243 info->process = (struct process *)grab_object( process );
1244 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1245 reply->pid = get_process_id( process );
1246 reply->tid = get_thread_id( thread );
1247 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
1248 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
1250 done:
1251 release_object( info );
1254 /* Retrieve information about a newly started process */
1255 DECL_HANDLER(get_new_process_info)
1257 struct startup_info *info;
1259 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1260 0, &startup_info_ops )))
1262 reply->success = is_process_init_done( info->process );
1263 reply->exit_code = info->process->exit_code;
1264 release_object( info );
1268 /* Retrieve the new process startup info */
1269 DECL_HANDLER(get_startup_info)
1271 struct process *process = current->process;
1272 struct startup_info *info = process->startup_info;
1273 data_size_t size;
1275 if (!info) return;
1277 if (info->exe_file &&
1278 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
1280 /* we return the data directly without making a copy so this can only be called once */
1281 reply->info_size = info->info_size;
1282 size = info->data_size;
1283 if (size > get_reply_max_size()) size = get_reply_max_size();
1284 set_reply_data_ptr( info->data, size );
1285 info->data = NULL;
1286 info->data_size = 0;
1289 /* signal the end of the process initialization */
1290 DECL_HANDLER(init_process_done)
1292 struct process_dll *dll;
1293 struct process *process = current->process;
1295 if (is_process_init_done(process))
1297 set_error( STATUS_INVALID_PARAMETER );
1298 return;
1300 if (!(dll = find_process_dll( process, req->module )))
1302 set_error( STATUS_DLL_NOT_FOUND );
1303 return;
1306 /* main exe is the first in the dll list */
1307 list_remove( &dll->entry );
1308 list_add_head( &process->dlls, &dll->entry );
1310 process->ldt_copy = req->ldt_copy;
1311 process->start_time = current_time;
1312 current->entry_point = req->entry;
1314 generate_startup_debug_events( process, req->entry );
1315 set_process_startup_state( process, STARTUP_DONE );
1317 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1318 stop_thread_if_suspended( current );
1319 if (process->debugger) set_process_debug_flag( process, 1 );
1322 /* open a handle to a process */
1323 DECL_HANDLER(open_process)
1325 struct process *process = get_process_from_id( req->pid );
1326 reply->handle = 0;
1327 if (process)
1329 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1330 release_object( process );
1334 /* terminate a process */
1335 DECL_HANDLER(terminate_process)
1337 struct process *process;
1339 if (req->handle)
1341 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1342 if (!process) return;
1344 else process = (struct process *)grab_object( current->process );
1346 reply->self = (current->process == process);
1347 terminate_process( process, current, req->exit_code );
1348 release_object( process );
1351 /* fetch information about a process */
1352 DECL_HANDLER(get_process_info)
1354 struct process *process;
1356 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1358 reply->pid = get_process_id( process );
1359 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1360 reply->exit_code = process->exit_code;
1361 reply->priority = process->priority;
1362 reply->affinity = process->affinity;
1363 reply->peb = process->peb;
1364 reply->start_time = process->start_time;
1365 reply->end_time = process->end_time;
1366 reply->cpu = process->cpu;
1367 reply->debugger_present = !!process->debugger;
1368 reply->debug_children = process->debug_children;
1369 release_object( process );
1373 static void set_process_affinity( struct process *process, affinity_t affinity )
1375 struct thread *thread;
1377 if (!process->running_threads)
1379 set_error( STATUS_PROCESS_IS_TERMINATING );
1380 return;
1383 process->affinity = affinity;
1385 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1387 set_thread_affinity( thread, affinity );
1391 /* set information about a process */
1392 DECL_HANDLER(set_process_info)
1394 struct process *process;
1396 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1398 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1399 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1400 release_object( process );
1404 /* read data from a process address space */
1405 DECL_HANDLER(read_process_memory)
1407 struct process *process;
1408 data_size_t len = get_reply_max_size();
1410 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1412 if (len)
1414 char *buffer = mem_alloc( len );
1415 if (buffer)
1417 if (read_process_memory( process, req->addr, len, buffer ))
1418 set_reply_data_ptr( buffer, len );
1419 else
1420 free( buffer );
1423 release_object( process );
1426 /* write data to a process address space */
1427 DECL_HANDLER(write_process_memory)
1429 struct process *process;
1431 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1433 data_size_t len = get_req_data_size();
1434 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1435 else set_error( STATUS_INVALID_PARAMETER );
1436 release_object( process );
1440 /* notify the server that a dll has been loaded */
1441 DECL_HANDLER(load_dll)
1443 struct process_dll *dll;
1444 struct mapping *mapping = NULL;
1446 if (req->mapping && !(mapping = get_mapping_obj( current->process, req->mapping, SECTION_QUERY )))
1447 return;
1449 if ((dll = process_load_dll( current->process, mapping, req->base,
1450 get_req_data(), get_req_data_size() )))
1452 dll->size = req->size;
1453 dll->dbg_offset = req->dbg_offset;
1454 dll->dbg_size = req->dbg_size;
1455 dll->name = req->name;
1456 /* only generate event if initialization is done */
1457 if (is_process_init_done( current->process ))
1458 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1460 if (mapping) release_object( mapping );
1463 /* notify the server that a dll is being unloaded */
1464 DECL_HANDLER(unload_dll)
1466 process_unload_dll( current->process, req->base );
1469 /* retrieve information about a module in a process */
1470 DECL_HANDLER(get_dll_info)
1472 struct process *process;
1474 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1476 struct process_dll *dll;
1478 if (req->base_address)
1479 dll = find_process_dll( process, req->base_address );
1480 else /* NULL means main module */
1481 dll = list_head( &process->dlls ) ?
1482 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1484 if (dll)
1486 reply->size = dll->size;
1487 reply->entry_point = 0; /* FIXME */
1488 reply->filename_len = dll->namelen;
1489 if (dll->filename)
1491 if (dll->namelen <= get_reply_max_size())
1492 set_reply_data( dll->filename, dll->namelen );
1493 else
1494 set_error( STATUS_BUFFER_TOO_SMALL );
1497 else
1498 set_error( STATUS_DLL_NOT_FOUND );
1500 release_object( process );
1504 /* retrieve the process idle event */
1505 DECL_HANDLER(get_process_idle_event)
1507 struct process *process;
1509 reply->event = 0;
1510 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1512 if (process->idle_event && process != current->process)
1513 reply->event = alloc_handle( current->process, process->idle_event,
1514 EVENT_ALL_ACCESS, 0 );
1515 release_object( process );
1519 /* make the current process a system process */
1520 DECL_HANDLER(make_process_system)
1522 struct process *process = current->process;
1524 if (!shutdown_event)
1526 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1527 make_object_static( (struct object *)shutdown_event );
1530 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1531 return;
1533 if (!process->is_system)
1535 process->is_system = 1;
1536 close_process_desktop( process );
1537 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1538 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1542 /* create a new job object */
1543 DECL_HANDLER(create_job)
1545 struct job *job;
1546 struct unicode_str name;
1547 struct directory *root;
1548 const struct security_descriptor *sd;
1549 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1551 if (!objattr) return;
1553 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1555 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1556 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1557 else
1558 reply->handle = alloc_handle_no_access_check( current->process, job,
1559 req->access, objattr->attributes );
1560 release_object( job );
1562 if (root) release_object( root );
1565 /* open a job object */
1566 DECL_HANDLER(open_job)
1568 struct unicode_str name = get_req_unicode_str();
1570 reply->handle = open_object( current->process, req->rootdir, req->access,
1571 &job_ops, &name, req->attributes );
1574 /* assign a job object to a process */
1575 DECL_HANDLER(assign_job)
1577 struct process *process;
1578 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1580 if (!job) return;
1582 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1584 add_job_process( job, process );
1585 release_object( process );
1587 release_object( job );
1591 /* check if a process is associated with a job */
1592 DECL_HANDLER(process_in_job)
1594 struct process *process;
1595 struct job *job;
1597 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1598 return;
1600 if (!req->job)
1602 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1604 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1606 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1607 release_object( job );
1609 release_object( process );
1612 /* terminate all processes associated with the job */
1613 DECL_HANDLER(terminate_job)
1615 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1617 if (!job) return;
1619 terminate_job( job, req->status );
1620 release_object( job );
1623 /* update limits of the job object */
1624 DECL_HANDLER(set_job_limits)
1626 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1628 if (!job) return;
1630 job->limit_flags = req->limit_flags;
1631 release_object( job );
1634 /* set the jobs completion port */
1635 DECL_HANDLER(set_job_completion_port)
1637 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1639 if (!job) return;
1641 if (!job->completion_port)
1643 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1644 job->completion_key = req->key;
1646 else
1647 set_error( STATUS_INVALID_PARAMETER );
1649 release_object( job );