wininet: Fix memory leak by not calling get_cookie_header twice.
[wine.git] / server / process.c
blob569a8fea0ad79cf2962cd580ebfe3d961c6fcc02
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #include <unistd.h>
36 #ifdef HAVE_POLL_H
37 #include <poll.h>
38 #endif
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "user.h"
50 #include "security.h"
52 /* process structure */
54 static struct list process_list = LIST_INIT(process_list);
55 static int running_processes, user_processes;
56 static struct event *shutdown_event; /* signaled when shutdown starts */
57 static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
58 static int shutdown_stage; /* current stage in the shutdown process */
60 /* process operations */
62 static void process_dump( struct object *obj, int verbose );
63 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
64 static unsigned int process_map_access( struct object *obj, unsigned int access );
65 static void process_poll_event( struct fd *fd, int event );
66 static void process_destroy( struct object *obj );
67 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
69 static const struct object_ops process_ops =
71 sizeof(struct process), /* size */
72 process_dump, /* dump */
73 no_get_type, /* get_type */
74 add_queue, /* add_queue */
75 remove_queue, /* remove_queue */
76 process_signaled, /* signaled */
77 no_satisfied, /* satisfied */
78 no_signal, /* signal */
79 no_get_fd, /* get_fd */
80 process_map_access, /* map_access */
81 default_get_sd, /* get_sd */
82 default_set_sd, /* set_sd */
83 no_lookup_name, /* lookup_name */
84 no_open_file, /* open_file */
85 no_close_handle, /* close_handle */
86 process_destroy /* destroy */
89 static const struct fd_ops process_fd_ops =
91 NULL, /* get_poll_events */
92 process_poll_event, /* poll_event */
93 NULL, /* flush */
94 NULL, /* get_fd_type */
95 NULL, /* ioctl */
96 NULL, /* queue_async */
97 NULL, /* reselect_async */
98 NULL /* cancel async */
101 /* process startup info */
103 struct startup_info
105 struct object obj; /* object header */
106 struct file *exe_file; /* file handle for main exe */
107 struct process *process; /* created process */
108 data_size_t info_size; /* size of startup info */
109 data_size_t data_size; /* size of whole startup data */
110 startup_info_t *data; /* data for startup info */
113 static void startup_info_dump( struct object *obj, int verbose );
114 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
115 static void startup_info_destroy( struct object *obj );
117 static const struct object_ops startup_info_ops =
119 sizeof(struct startup_info), /* size */
120 startup_info_dump, /* dump */
121 no_get_type, /* get_type */
122 add_queue, /* add_queue */
123 remove_queue, /* remove_queue */
124 startup_info_signaled, /* signaled */
125 no_satisfied, /* satisfied */
126 no_signal, /* signal */
127 no_get_fd, /* get_fd */
128 no_map_access, /* map_access */
129 default_get_sd, /* get_sd */
130 default_set_sd, /* set_sd */
131 no_lookup_name, /* lookup_name */
132 no_open_file, /* open_file */
133 no_close_handle, /* close_handle */
134 startup_info_destroy /* destroy */
137 /* job object */
139 static void job_dump( struct object *obj, int verbose );
140 static struct object_type *job_get_type( struct object *obj );
141 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
142 static unsigned int job_map_access( struct object *obj, unsigned int access );
143 static void job_destroy( struct object *obj );
145 struct job
147 struct object obj; /* object header */
148 struct list process_list; /* list of all processes */
149 int num_processes; /* count of running processes */
150 unsigned int limit_flags; /* limit flags */
151 int terminating; /* job is terminating */
152 int signaled; /* job is signaled */
153 struct completion *completion_port; /* associated completion port */
154 apc_param_t completion_key; /* key to send with completion messages */
157 static const struct object_ops job_ops =
159 sizeof(struct job), /* size */
160 job_dump, /* dump */
161 job_get_type, /* get_type */
162 add_queue, /* add_queue */
163 remove_queue, /* remove_queue */
164 job_signaled, /* signaled */
165 no_satisfied, /* satisfied */
166 no_signal, /* signal */
167 no_get_fd, /* get_fd */
168 job_map_access, /* map_access */
169 default_get_sd, /* get_sd */
170 default_set_sd, /* set_sd */
171 no_lookup_name, /* lookup_name */
172 no_open_file, /* open_file */
173 no_close_handle, /* close_handle */
174 job_destroy /* destroy */
177 static struct job *create_job_object( struct directory *root, const struct unicode_str *name,
178 unsigned int attr, const struct security_descriptor *sd )
180 struct job *job;
182 if ((job = create_named_object_dir( root, name, attr, &job_ops )))
184 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
186 /* initialize it if it didn't already exist */
187 if (sd) default_set_sd( &job->obj, sd, OWNER_SECURITY_INFORMATION |
188 GROUP_SECURITY_INFORMATION |
189 DACL_SECURITY_INFORMATION |
190 SACL_SECURITY_INFORMATION );
191 list_init( &job->process_list );
192 job->num_processes = 0;
193 job->limit_flags = 0;
194 job->terminating = 0;
195 job->signaled = 0;
196 job->completion_port = NULL;
197 job->completion_key = 0;
200 return job;
203 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
205 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
208 static struct object_type *job_get_type( struct object *obj )
210 static const WCHAR name[] = {'J','o','b'};
211 static const struct unicode_str str = { name, sizeof(name) };
212 return get_object_type( &str );
215 static unsigned int job_map_access( struct object *obj, unsigned int access )
217 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
218 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
219 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
220 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
221 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
224 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
226 if (job->completion_port)
227 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
230 static void add_job_process( struct job *job, struct process *process )
232 if (!process->running_threads)
234 set_error( STATUS_PROCESS_IS_TERMINATING );
235 return;
237 if (process->job)
239 set_error( STATUS_ACCESS_DENIED );
240 return;
242 process->job = (struct job *)grab_object( job );
243 list_add_tail( &job->process_list, &process->job_entry );
244 job->num_processes++;
246 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
249 /* called when a process has terminated, allow one additional process */
250 static void release_job_process( struct process *process )
252 struct job *job = process->job;
254 if (!job) return;
256 assert( job->num_processes );
257 job->num_processes--;
259 if (!job->terminating)
260 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
262 if (!job->num_processes)
263 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
266 static void terminate_job( struct job *job, int exit_code )
268 /* don't report completion events for terminated processes */
269 job->terminating = 1;
271 for (;;) /* restart from the beginning of the list every time */
273 struct process *process;
275 /* find the first process associated with this job and still running */
276 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
278 if (process->running_threads) break;
280 if (&process->job_entry == &job->process_list) break; /* no process found */
281 assert( process->job == job );
282 terminate_process( process, NULL, exit_code );
285 job->terminating = 0;
286 job->signaled = 1;
287 wake_up( &job->obj, 0 );
290 static void job_destroy( struct object *obj )
292 struct job *job = (struct job *)obj;
293 assert( obj->ops == &job_ops );
295 assert( !job->num_processes );
296 assert( list_empty(&job->process_list) );
298 if (job->completion_port) release_object( job->completion_port );
301 static void job_dump( struct object *obj, int verbose )
303 struct job *job = (struct job *)obj;
304 assert( obj->ops == &job_ops );
305 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
308 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
310 struct job *job = (struct job *)obj;
311 return job->signaled;
314 struct ptid_entry
316 void *ptr; /* entry ptr */
317 unsigned int next; /* next free entry */
320 static struct ptid_entry *ptid_entries; /* array of ptid entries */
321 static unsigned int used_ptid_entries; /* number of entries in use */
322 static unsigned int alloc_ptid_entries; /* number of allocated entries */
323 static unsigned int next_free_ptid; /* next free entry */
324 static unsigned int last_free_ptid; /* last free entry */
326 static void kill_all_processes(void);
328 #define PTID_OFFSET 8 /* offset for first ptid value */
330 /* allocate a new process or thread id */
331 unsigned int alloc_ptid( void *ptr )
333 struct ptid_entry *entry;
334 unsigned int id;
336 if (used_ptid_entries < alloc_ptid_entries)
338 id = used_ptid_entries + PTID_OFFSET;
339 entry = &ptid_entries[used_ptid_entries++];
341 else if (next_free_ptid)
343 id = next_free_ptid;
344 entry = &ptid_entries[id - PTID_OFFSET];
345 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
347 else /* need to grow the array */
349 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
350 if (!count) count = 64;
351 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
353 set_error( STATUS_NO_MEMORY );
354 return 0;
356 ptid_entries = entry;
357 alloc_ptid_entries = count;
358 id = used_ptid_entries + PTID_OFFSET;
359 entry = &ptid_entries[used_ptid_entries++];
362 entry->ptr = ptr;
363 return id;
366 /* free a process or thread id */
367 void free_ptid( unsigned int id )
369 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
371 entry->ptr = NULL;
372 entry->next = 0;
374 /* append to end of free list so that we don't reuse it too early */
375 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
376 else next_free_ptid = id;
378 last_free_ptid = id;
381 /* retrieve the pointer corresponding to a process or thread id */
382 void *get_ptid_entry( unsigned int id )
384 if (id < PTID_OFFSET) return NULL;
385 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
386 return ptid_entries[id - PTID_OFFSET].ptr;
389 /* return the main thread of the process */
390 struct thread *get_process_first_thread( struct process *process )
392 struct list *ptr = list_head( &process->thread_list );
393 if (!ptr) return NULL;
394 return LIST_ENTRY( ptr, struct thread, proc_entry );
397 /* set the state of the process startup info */
398 static void set_process_startup_state( struct process *process, enum startup_state state )
400 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
401 if (process->startup_info)
403 wake_up( &process->startup_info->obj, 0 );
404 release_object( process->startup_info );
405 process->startup_info = NULL;
409 /* callback for server shutdown */
410 static void server_shutdown_timeout( void *arg )
412 shutdown_timeout = NULL;
413 if (!running_processes)
415 close_master_socket( 0 );
416 return;
418 switch(++shutdown_stage)
420 case 1: /* signal system processes to exit */
421 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
422 if (shutdown_event) set_event( shutdown_event );
423 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
424 close_master_socket( 4 * -TICKS_PER_SEC );
425 break;
426 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
427 kill_all_processes();
428 break;
432 /* forced shutdown, used for wineserver -k */
433 void shutdown_master_socket(void)
435 kill_all_processes();
436 shutdown_stage = 2;
437 if (shutdown_timeout)
439 remove_timeout_user( shutdown_timeout );
440 shutdown_timeout = NULL;
442 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
445 /* final cleanup once we are sure a process is really dead */
446 static void process_died( struct process *process )
448 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
449 if (!process->is_system)
451 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
452 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
454 release_object( process );
455 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
458 /* callback for process sigkill timeout */
459 static void process_sigkill( void *private )
461 struct process *process = private;
463 process->sigkill_timeout = NULL;
464 kill( process->unix_pid, SIGKILL );
465 process_died( process );
468 /* start the sigkill timer for a process upon exit */
469 static void start_sigkill_timer( struct process *process )
471 grab_object( process );
472 if (process->unix_pid != -1 && process->msg_fd)
473 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
474 else
475 process_died( process );
478 /* create a new process and its main thread */
479 /* if the function fails the fd is closed */
480 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
482 struct process *process;
483 struct thread *thread = NULL;
484 int request_pipe[2];
486 if (!(process = alloc_object( &process_ops )))
488 close( fd );
489 goto error;
491 process->parent = NULL;
492 process->debugger = NULL;
493 process->handles = NULL;
494 process->msg_fd = NULL;
495 process->sigkill_timeout = NULL;
496 process->unix_pid = -1;
497 process->exit_code = STILL_ACTIVE;
498 process->running_threads = 0;
499 process->priority = PROCESS_PRIOCLASS_NORMAL;
500 process->suspend = 0;
501 process->is_system = 0;
502 process->debug_children = 0;
503 process->is_terminating = 0;
504 process->job = NULL;
505 process->console = NULL;
506 process->startup_state = STARTUP_IN_PROGRESS;
507 process->startup_info = NULL;
508 process->idle_event = NULL;
509 process->peb = 0;
510 process->ldt_copy = 0;
511 process->winstation = 0;
512 process->desktop = 0;
513 process->token = NULL;
514 process->trace_data = 0;
515 process->rawinput_mouse = NULL;
516 process->rawinput_kbd = NULL;
517 list_init( &process->thread_list );
518 list_init( &process->locks );
519 list_init( &process->classes );
520 list_init( &process->dlls );
521 list_init( &process->rawinput_devices );
523 process->end_time = 0;
524 list_add_tail( &process_list, &process->entry );
526 if (!(process->id = process->group_id = alloc_ptid( process )))
528 close( fd );
529 goto error;
531 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
533 /* create the handle table */
534 if (!parent_thread)
536 process->handles = alloc_handle_table( process, 0 );
537 process->token = token_create_admin();
538 process->affinity = ~0;
540 else
542 struct process *parent = parent_thread->process;
543 process->parent = (struct process *)grab_object( parent );
544 process->handles = inherit_all ? copy_handle_table( process, parent )
545 : alloc_handle_table( process, 0 );
546 /* Note: for security reasons, starting a new process does not attempt
547 * to use the current impersonation token for the new process */
548 process->token = token_duplicate( parent->token, TRUE, 0 );
549 process->affinity = parent->affinity;
551 if (!process->handles || !process->token) goto error;
553 /* create the main thread */
554 if (pipe( request_pipe ) == -1)
556 file_set_error();
557 goto error;
559 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
561 close( request_pipe[0] );
562 close( request_pipe[1] );
563 goto error;
565 close( request_pipe[1] );
566 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
568 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
569 release_object( process );
570 return thread;
572 error:
573 if (process) release_object( process );
574 /* if we failed to start our first process, close everything down */
575 if (!running_processes) close_master_socket( 0 );
576 return NULL;
579 /* initialize the current process and fill in the request */
580 data_size_t init_process( struct thread *thread )
582 struct process *process = thread->process;
583 struct startup_info *info = process->startup_info;
585 init_process_tracing( process );
586 if (!info) return 0;
587 return info->data_size;
590 /* destroy a process when its refcount is 0 */
591 static void process_destroy( struct object *obj )
593 struct process *process = (struct process *)obj;
594 assert( obj->ops == &process_ops );
596 /* we can't have a thread remaining */
597 assert( list_empty( &process->thread_list ));
599 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
601 close_process_handles( process );
602 set_process_startup_state( process, STARTUP_ABORTED );
604 if (process->job)
606 list_remove( &process->job_entry );
607 release_object( process->job );
609 if (process->console) release_object( process->console );
610 if (process->parent) release_object( process->parent );
611 if (process->msg_fd) release_object( process->msg_fd );
612 list_remove( &process->entry );
613 if (process->idle_event) release_object( process->idle_event );
614 if (process->id) free_ptid( process->id );
615 if (process->token) release_object( process->token );
618 /* dump a process on stdout for debugging purposes */
619 static void process_dump( struct object *obj, int verbose )
621 struct process *process = (struct process *)obj;
622 assert( obj->ops == &process_ops );
624 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
627 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
629 struct process *process = (struct process *)obj;
630 return !process->running_threads;
633 static unsigned int process_map_access( struct object *obj, unsigned int access )
635 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
636 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
637 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
638 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
639 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
640 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
643 static void process_poll_event( struct fd *fd, int event )
645 struct process *process = get_fd_user( fd );
646 assert( process->obj.ops == &process_ops );
648 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
649 else if (event & POLLIN) receive_fd( process );
652 static void startup_info_destroy( struct object *obj )
654 struct startup_info *info = (struct startup_info *)obj;
655 assert( obj->ops == &startup_info_ops );
656 free( info->data );
657 if (info->exe_file) release_object( info->exe_file );
658 if (info->process) release_object( info->process );
661 static void startup_info_dump( struct object *obj, int verbose )
663 struct startup_info *info = (struct startup_info *)obj;
664 assert( obj->ops == &startup_info_ops );
666 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
667 info->data->hstdin, info->data->hstdout, info->data->hstderr );
670 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
672 struct startup_info *info = (struct startup_info *)obj;
673 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
676 /* get a process from an id (and increment the refcount) */
677 struct process *get_process_from_id( process_id_t id )
679 struct object *obj = get_ptid_entry( id );
681 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
682 set_error( STATUS_INVALID_PARAMETER );
683 return NULL;
686 /* get a process from a handle (and increment the refcount) */
687 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
689 return (struct process *)get_handle_obj( current->process, handle,
690 access, &process_ops );
693 /* find a dll from its base address */
694 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
696 struct process_dll *dll;
698 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
700 if (dll->base == base) return dll;
702 return NULL;
705 /* add a dll to a process list */
706 static struct process_dll *process_load_dll( struct process *process, struct mapping *mapping,
707 mod_handle_t base, const WCHAR *filename,
708 data_size_t name_len )
710 struct process_dll *dll;
712 /* make sure we don't already have one with the same base address */
713 if (find_process_dll( process, base ))
715 set_error( STATUS_INVALID_PARAMETER );
716 return NULL;
719 if ((dll = mem_alloc( sizeof(*dll) )))
721 dll->mapping = NULL;
722 dll->base = base;
723 dll->filename = NULL;
724 dll->namelen = name_len;
725 if (name_len && !(dll->filename = memdup( filename, name_len )))
727 free( dll );
728 return NULL;
730 if (mapping) dll->mapping = grab_mapping_unless_removable( mapping );
731 list_add_tail( &process->dlls, &dll->entry );
733 return dll;
736 /* remove a dll from a process list */
737 static void process_unload_dll( struct process *process, mod_handle_t base )
739 struct process_dll *dll = find_process_dll( process, base );
741 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
743 if (dll->mapping) release_object( dll->mapping );
744 free( dll->filename );
745 list_remove( &dll->entry );
746 free( dll );
747 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
749 else set_error( STATUS_INVALID_PARAMETER );
752 /* terminate a process with the given exit code */
753 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
755 struct thread *thread;
757 grab_object( process ); /* make sure it doesn't get freed when threads die */
758 process->is_terminating = 1;
760 restart:
761 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
763 if (exit_code) thread->exit_code = exit_code;
764 if (thread == skip) continue;
765 if (thread->state == TERMINATED) continue;
766 kill_thread( thread, 1 );
767 goto restart;
769 release_object( process );
772 /* kill all processes */
773 static void kill_all_processes(void)
775 for (;;)
777 struct process *process;
779 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
781 if (process->running_threads) break;
783 if (&process->entry == &process_list) break; /* no process found */
784 terminate_process( process, NULL, 1 );
788 /* kill all processes being attached to a console renderer */
789 void kill_console_processes( struct thread *renderer, int exit_code )
791 for (;;) /* restart from the beginning of the list every time */
793 struct process *process;
795 /* find the first process being attached to 'renderer' and still running */
796 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
798 if (process == renderer->process) continue;
799 if (!process->running_threads) continue;
800 if (process->console && console_get_renderer( process->console ) == renderer) break;
802 if (&process->entry == &process_list) break; /* no process found */
803 terminate_process( process, NULL, exit_code );
807 /* a process has been killed (i.e. its last thread died) */
808 static void process_killed( struct process *process )
810 struct list *ptr;
812 assert( list_empty( &process->thread_list ));
813 process->end_time = current_time;
814 if (!process->is_system) close_process_desktop( process );
815 process->winstation = 0;
816 process->desktop = 0;
817 close_process_handles( process );
818 if (process->idle_event)
820 release_object( process->idle_event );
821 process->idle_event = NULL;
824 /* close the console attached to this process, if any */
825 free_console( process );
827 while ((ptr = list_head( &process->rawinput_devices )))
829 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
830 list_remove( &entry->entry );
831 free( entry );
833 while ((ptr = list_head( &process->dlls )))
835 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
836 if (dll->mapping) release_object( dll->mapping );
837 free( dll->filename );
838 list_remove( &dll->entry );
839 free( dll );
841 destroy_process_classes( process );
842 free_process_user_handles( process );
843 remove_process_locks( process );
844 set_process_startup_state( process, STARTUP_ABORTED );
845 finish_process_tracing( process );
846 release_job_process( process );
847 start_sigkill_timer( process );
848 wake_up( &process->obj, 0 );
851 /* add a thread to a process running threads list */
852 void add_process_thread( struct process *process, struct thread *thread )
854 list_add_tail( &process->thread_list, &thread->proc_entry );
855 if (!process->running_threads++)
857 running_processes++;
858 if (!process->is_system)
860 if (!user_processes++ && shutdown_timeout)
862 remove_timeout_user( shutdown_timeout );
863 shutdown_timeout = NULL;
867 grab_object( thread );
870 /* remove a thread from a process running threads list */
871 void remove_process_thread( struct process *process, struct thread *thread )
873 assert( process->running_threads > 0 );
874 assert( !list_empty( &process->thread_list ));
876 list_remove( &thread->proc_entry );
878 if (!--process->running_threads)
880 /* we have removed the last running thread, exit the process */
881 process->exit_code = thread->exit_code;
882 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
883 process_killed( process );
885 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
886 release_object( thread );
889 /* suspend all the threads of a process */
890 void suspend_process( struct process *process )
892 if (!process->suspend++)
894 struct list *ptr, *next;
896 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
898 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
899 if (!thread->suspend) stop_thread( thread );
904 /* resume all the threads of a process */
905 void resume_process( struct process *process )
907 assert (process->suspend > 0);
908 if (!--process->suspend)
910 struct list *ptr, *next;
912 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
914 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
915 if (!thread->suspend) wake_thread( thread );
920 /* kill a process on the spot */
921 void kill_process( struct process *process, int violent_death )
923 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
925 release_object( process->msg_fd );
926 process->msg_fd = NULL;
929 if (process->sigkill_timeout) /* already waiting for it to die */
931 remove_timeout_user( process->sigkill_timeout );
932 process->sigkill_timeout = NULL;
933 process_died( process );
934 return;
937 if (violent_death) terminate_process( process, NULL, 1 );
938 else
940 struct list *ptr;
942 grab_object( process ); /* make sure it doesn't get freed when threads die */
943 while ((ptr = list_head( &process->thread_list )))
945 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
946 kill_thread( thread, 0 );
948 release_object( process );
952 /* kill all processes being debugged by a given thread */
953 void kill_debugged_processes( struct thread *debugger, int exit_code )
955 for (;;) /* restart from the beginning of the list every time */
957 struct process *process;
959 /* find the first process being debugged by 'debugger' and still running */
960 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
962 if (!process->running_threads) continue;
963 if (process->debugger == debugger) break;
965 if (&process->entry == &process_list) break; /* no process found */
966 process->debugger = NULL;
967 terminate_process( process, NULL, exit_code );
972 /* trigger a breakpoint event in a given process */
973 void break_process( struct process *process )
975 struct thread *thread;
977 suspend_process( process );
979 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
981 if (thread->context) /* inside an exception event already */
983 break_thread( thread );
984 goto done;
987 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
988 else set_error( STATUS_ACCESS_DENIED );
989 done:
990 resume_process( process );
994 /* detach a debugger from all its debuggees */
995 void detach_debugged_processes( struct thread *debugger )
997 struct process *process;
999 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1001 if (process->debugger == debugger && process->running_threads)
1003 debugger_detach( process, debugger );
1009 void enum_processes( int (*cb)(struct process*, void*), void *user )
1011 struct list *ptr, *next;
1013 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1015 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1016 if ((cb)(process, user)) break;
1020 /* set the debugged flag in the process PEB */
1021 int set_process_debug_flag( struct process *process, int flag )
1023 char data = (flag != 0);
1025 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1026 return write_process_memory( process, process->peb + 2, 1, &data );
1029 /* take a snapshot of currently running processes */
1030 struct process_snapshot *process_snap( int *count )
1032 struct process_snapshot *snapshot, *ptr;
1033 struct process *process;
1035 if (!running_processes) return NULL;
1036 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1037 return NULL;
1038 ptr = snapshot;
1039 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1041 if (!process->running_threads) continue;
1042 ptr->process = process;
1043 ptr->threads = process->running_threads;
1044 ptr->count = process->obj.refcount;
1045 ptr->priority = process->priority;
1046 ptr->handles = get_handle_table_count(process);
1047 ptr->unix_pid = process->unix_pid;
1048 grab_object( process );
1049 ptr++;
1052 if (!(*count = ptr - snapshot))
1054 free( snapshot );
1055 snapshot = NULL;
1057 return snapshot;
1060 /* create a new process */
1061 DECL_HANDLER(new_process)
1063 struct startup_info *info;
1064 struct thread *thread;
1065 struct process *process;
1066 struct process *parent = current->process;
1067 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1069 if (socket_fd == -1)
1071 set_error( STATUS_INVALID_PARAMETER );
1072 return;
1074 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1076 set_error( STATUS_INVALID_HANDLE );
1077 close( socket_fd );
1078 return;
1080 if (shutdown_stage)
1082 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1083 close( socket_fd );
1084 return;
1086 if (!is_cpu_supported( req->cpu ))
1088 close( socket_fd );
1089 return;
1092 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1093 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1095 set_error( STATUS_ACCESS_DENIED );
1096 close( socket_fd );
1097 return;
1100 if (!req->info_size) /* create an orphaned process */
1102 create_process( socket_fd, NULL, 0 );
1103 return;
1106 /* build the startup info for a new process */
1107 if (!(info = alloc_object( &startup_info_ops )))
1109 close( socket_fd );
1110 return;
1112 info->exe_file = NULL;
1113 info->process = NULL;
1114 info->data = NULL;
1116 if (req->exe_file &&
1117 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1119 close( socket_fd );
1120 goto done;
1123 info->data_size = get_req_data_size();
1124 info->info_size = min( req->info_size, info->data_size );
1126 if (req->info_size < sizeof(*info->data))
1128 /* make sure we have a full startup_info_t structure */
1129 data_size_t env_size = info->data_size - info->info_size;
1130 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1132 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1134 close( socket_fd );
1135 goto done;
1137 memcpy( info->data, get_req_data(), info_size );
1138 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1139 memcpy( info->data + 1, (const char *)get_req_data() + req->info_size, env_size );
1140 info->info_size = sizeof(startup_info_t);
1141 info->data_size = info->info_size + env_size;
1143 else
1145 data_size_t pos = sizeof(*info->data);
1147 if (!(info->data = memdup( get_req_data(), info->data_size )))
1149 close( socket_fd );
1150 goto done;
1152 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1153 FIXUP_LEN( info->data->curdir_len );
1154 FIXUP_LEN( info->data->dllpath_len );
1155 FIXUP_LEN( info->data->imagepath_len );
1156 FIXUP_LEN( info->data->cmdline_len );
1157 FIXUP_LEN( info->data->title_len );
1158 FIXUP_LEN( info->data->desktop_len );
1159 FIXUP_LEN( info->data->shellinfo_len );
1160 FIXUP_LEN( info->data->runtime_len );
1161 #undef FIXUP_LEN
1164 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
1165 process = thread->process;
1166 process->debug_children = (req->create_flags & DEBUG_PROCESS)
1167 && !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1168 process->startup_info = (struct startup_info *)grab_object( info );
1170 if (parent->job
1171 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1172 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1174 add_job_process( parent->job, process );
1177 /* connect to the window station */
1178 connect_process_winstation( process, current );
1180 /* thread will be actually suspended in init_done */
1181 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
1183 /* set the process console */
1184 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1186 /* FIXME: some better error checking should be done...
1187 * like if hConOut and hConIn are console handles, then they should be on the same
1188 * physical console
1190 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1193 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1195 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1196 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1197 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1198 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1199 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1200 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1201 /* some handles above may have been invalid; this is not an error */
1202 if (get_error() == STATUS_INVALID_HANDLE ||
1203 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1206 /* attach to the debugger if requested */
1207 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1208 set_process_debugger( process, current );
1209 else if (parent->debugger && parent->debug_children)
1210 set_process_debugger( process, parent->debugger );
1212 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1213 process->group_id = parent->group_id;
1215 info->process = (struct process *)grab_object( process );
1216 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1217 reply->pid = get_process_id( process );
1218 reply->tid = get_thread_id( thread );
1219 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
1220 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
1222 done:
1223 release_object( info );
1226 /* Retrieve information about a newly started process */
1227 DECL_HANDLER(get_new_process_info)
1229 struct startup_info *info;
1231 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1232 0, &startup_info_ops )))
1234 reply->success = is_process_init_done( info->process );
1235 reply->exit_code = info->process->exit_code;
1236 release_object( info );
1240 /* Retrieve the new process startup info */
1241 DECL_HANDLER(get_startup_info)
1243 struct process *process = current->process;
1244 struct startup_info *info = process->startup_info;
1245 data_size_t size;
1247 if (!info) return;
1249 if (info->exe_file &&
1250 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
1252 /* we return the data directly without making a copy so this can only be called once */
1253 reply->info_size = info->info_size;
1254 size = info->data_size;
1255 if (size > get_reply_max_size()) size = get_reply_max_size();
1256 set_reply_data_ptr( info->data, size );
1257 info->data = NULL;
1258 info->data_size = 0;
1261 /* signal the end of the process initialization */
1262 DECL_HANDLER(init_process_done)
1264 struct process_dll *dll;
1265 struct process *process = current->process;
1267 if (is_process_init_done(process))
1269 set_error( STATUS_INVALID_PARAMETER );
1270 return;
1272 if (!(dll = find_process_dll( process, req->module )))
1274 set_error( STATUS_DLL_NOT_FOUND );
1275 return;
1278 /* main exe is the first in the dll list */
1279 list_remove( &dll->entry );
1280 list_add_head( &process->dlls, &dll->entry );
1282 process->ldt_copy = req->ldt_copy;
1283 process->start_time = current_time;
1285 generate_startup_debug_events( process, req->entry );
1286 set_process_startup_state( process, STARTUP_DONE );
1288 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1289 stop_thread_if_suspended( current );
1290 if (process->debugger) set_process_debug_flag( process, 1 );
1293 /* open a handle to a process */
1294 DECL_HANDLER(open_process)
1296 struct process *process = get_process_from_id( req->pid );
1297 reply->handle = 0;
1298 if (process)
1300 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1301 release_object( process );
1305 /* terminate a process */
1306 DECL_HANDLER(terminate_process)
1308 struct process *process;
1310 if (req->handle)
1312 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1313 if (!process) return;
1315 else process = (struct process *)grab_object( current->process );
1317 reply->self = (current->process == process);
1318 terminate_process( process, current, req->exit_code );
1319 release_object( process );
1322 /* fetch information about a process */
1323 DECL_HANDLER(get_process_info)
1325 struct process *process;
1327 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1329 reply->pid = get_process_id( process );
1330 reply->ppid = process->parent ? get_process_id( process->parent ) : 0;
1331 reply->exit_code = process->exit_code;
1332 reply->priority = process->priority;
1333 reply->affinity = process->affinity;
1334 reply->peb = process->peb;
1335 reply->start_time = process->start_time;
1336 reply->end_time = process->end_time;
1337 reply->cpu = process->cpu;
1338 reply->debugger_present = !!process->debugger;
1339 release_object( process );
1343 static void set_process_affinity( struct process *process, affinity_t affinity )
1345 struct thread *thread;
1347 if (!process->running_threads)
1349 set_error( STATUS_PROCESS_IS_TERMINATING );
1350 return;
1353 process->affinity = affinity;
1355 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1357 set_thread_affinity( thread, affinity );
1361 /* set information about a process */
1362 DECL_HANDLER(set_process_info)
1364 struct process *process;
1366 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1368 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1369 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1370 release_object( process );
1374 /* read data from a process address space */
1375 DECL_HANDLER(read_process_memory)
1377 struct process *process;
1378 data_size_t len = get_reply_max_size();
1380 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1382 if (len)
1384 char *buffer = mem_alloc( len );
1385 if (buffer)
1387 if (read_process_memory( process, req->addr, len, buffer ))
1388 set_reply_data_ptr( buffer, len );
1389 else
1390 free( buffer );
1393 release_object( process );
1396 /* write data to a process address space */
1397 DECL_HANDLER(write_process_memory)
1399 struct process *process;
1401 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1403 data_size_t len = get_req_data_size();
1404 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1405 else set_error( STATUS_INVALID_PARAMETER );
1406 release_object( process );
1410 /* notify the server that a dll has been loaded */
1411 DECL_HANDLER(load_dll)
1413 struct process_dll *dll;
1414 struct mapping *mapping = NULL;
1416 if (req->mapping && !(mapping = get_mapping_obj( current->process, req->mapping, SECTION_QUERY )))
1417 return;
1419 if ((dll = process_load_dll( current->process, mapping, req->base,
1420 get_req_data(), get_req_data_size() )))
1422 dll->size = req->size;
1423 dll->dbg_offset = req->dbg_offset;
1424 dll->dbg_size = req->dbg_size;
1425 dll->name = req->name;
1426 /* only generate event if initialization is done */
1427 if (is_process_init_done( current->process ))
1428 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1430 if (mapping) release_object( mapping );
1433 /* notify the server that a dll is being unloaded */
1434 DECL_HANDLER(unload_dll)
1436 process_unload_dll( current->process, req->base );
1439 /* retrieve information about a module in a process */
1440 DECL_HANDLER(get_dll_info)
1442 struct process *process;
1444 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1446 struct process_dll *dll;
1448 if (req->base_address)
1449 dll = find_process_dll( process, req->base_address );
1450 else /* NULL means main module */
1451 dll = list_head( &process->dlls ) ?
1452 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1454 if (dll)
1456 reply->size = dll->size;
1457 reply->entry_point = 0; /* FIXME */
1458 reply->filename_len = dll->namelen;
1459 if (dll->filename)
1461 if (dll->namelen <= get_reply_max_size())
1462 set_reply_data( dll->filename, dll->namelen );
1463 else
1464 set_error( STATUS_BUFFER_TOO_SMALL );
1467 else
1468 set_error( STATUS_DLL_NOT_FOUND );
1470 release_object( process );
1474 /* retrieve the process idle event */
1475 DECL_HANDLER(get_process_idle_event)
1477 struct process *process;
1479 reply->event = 0;
1480 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1482 if (process->idle_event && process != current->process)
1483 reply->event = alloc_handle( current->process, process->idle_event,
1484 EVENT_ALL_ACCESS, 0 );
1485 release_object( process );
1489 /* make the current process a system process */
1490 DECL_HANDLER(make_process_system)
1492 struct process *process = current->process;
1494 if (!shutdown_event)
1496 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1497 make_object_static( (struct object *)shutdown_event );
1500 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1501 return;
1503 if (!process->is_system)
1505 process->is_system = 1;
1506 close_process_desktop( process );
1507 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1508 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1512 /* create a new job object */
1513 DECL_HANDLER(create_job)
1515 struct job *job;
1516 struct unicode_str name;
1517 struct directory *root = NULL;
1518 const struct object_attributes *objattr = get_req_data();
1519 const struct security_descriptor *sd;
1521 if (!objattr_is_valid( objattr, get_req_data_size() )) return;
1523 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
1524 objattr_get_name( objattr, &name );
1526 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) return;
1528 if ((job = create_job_object( root, &name, req->attributes, sd )))
1530 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1531 reply->handle = alloc_handle( current->process, job, req->access, req->attributes );
1532 else
1533 reply->handle = alloc_handle_no_access_check( current->process, job, req->access, req->attributes );
1534 release_object( job );
1536 if (root) release_object( root );
1539 /* assign a job object to a process */
1540 DECL_HANDLER(assign_job)
1542 struct process *process;
1543 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1545 if (!job) return;
1547 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1549 add_job_process( job, process );
1550 release_object( process );
1552 release_object( job );
1556 /* check if a process is associated with a job */
1557 DECL_HANDLER(process_in_job)
1559 struct process *process;
1560 struct job *job;
1562 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1563 return;
1565 if (!req->job)
1567 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1569 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1571 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1572 release_object( job );
1574 release_object( process );
1577 /* terminate all processes associated with the job */
1578 DECL_HANDLER(terminate_job)
1580 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1582 if (!job) return;
1584 terminate_job( job, req->status );
1585 release_object( job );
1588 /* update limits of the job object */
1589 DECL_HANDLER(set_job_limits)
1591 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1593 if (!job) return;
1595 job->limit_flags = req->limit_flags;
1596 release_object( job );
1599 /* set the jobs completion port */
1600 DECL_HANDLER(set_job_completion_port)
1602 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1604 if (!job) return;
1606 if (!job->completion_port)
1608 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1609 job->completion_key = req->key;
1611 else
1612 set_error( STATUS_INVALID_PARAMETER );
1614 release_object( job );