ntdll: Implement RtlSetUnhandledExceptionFilter().
[wine.git] / server / process.c
blobb512141048a6d6337339d2d8bd23f913d73beb5e
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 process *process; /* created process */
109 data_size_t info_size; /* size of startup info */
110 data_size_t data_size; /* size of whole startup data */
111 startup_info_t *data; /* data for startup info */
114 static void startup_info_dump( struct object *obj, int verbose );
115 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
116 static void startup_info_destroy( struct object *obj );
118 static const struct object_ops startup_info_ops =
120 sizeof(struct startup_info), /* size */
121 startup_info_dump, /* dump */
122 no_get_type, /* get_type */
123 add_queue, /* add_queue */
124 remove_queue, /* remove_queue */
125 startup_info_signaled, /* signaled */
126 no_satisfied, /* satisfied */
127 no_signal, /* signal */
128 no_get_fd, /* get_fd */
129 no_map_access, /* map_access */
130 default_get_sd, /* get_sd */
131 default_set_sd, /* set_sd */
132 no_lookup_name, /* lookup_name */
133 no_link_name, /* link_name */
134 NULL, /* unlink_name */
135 no_open_file, /* open_file */
136 no_close_handle, /* close_handle */
137 startup_info_destroy /* destroy */
140 /* job object */
142 static void job_dump( struct object *obj, int verbose );
143 static struct object_type *job_get_type( struct object *obj );
144 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
145 static unsigned int job_map_access( struct object *obj, unsigned int access );
146 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
147 static void job_destroy( struct object *obj );
149 struct job
151 struct object obj; /* object header */
152 struct list process_list; /* list of all processes */
153 int num_processes; /* count of running processes */
154 unsigned int limit_flags; /* limit flags */
155 int terminating; /* job is terminating */
156 int signaled; /* job is signaled */
157 struct completion *completion_port; /* associated completion port */
158 apc_param_t completion_key; /* key to send with completion messages */
161 static const struct object_ops job_ops =
163 sizeof(struct job), /* size */
164 job_dump, /* dump */
165 job_get_type, /* get_type */
166 add_queue, /* add_queue */
167 remove_queue, /* remove_queue */
168 job_signaled, /* signaled */
169 no_satisfied, /* satisfied */
170 no_signal, /* signal */
171 no_get_fd, /* get_fd */
172 job_map_access, /* map_access */
173 default_get_sd, /* get_sd */
174 default_set_sd, /* set_sd */
175 no_lookup_name, /* lookup_name */
176 directory_link_name, /* link_name */
177 default_unlink_name, /* unlink_name */
178 no_open_file, /* open_file */
179 job_close_handle, /* close_handle */
180 job_destroy /* destroy */
183 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
184 unsigned int attr, const struct security_descriptor *sd )
186 struct job *job;
188 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
190 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
192 /* initialize it if it didn't already exist */
193 list_init( &job->process_list );
194 job->num_processes = 0;
195 job->limit_flags = 0;
196 job->terminating = 0;
197 job->signaled = 0;
198 job->completion_port = NULL;
199 job->completion_key = 0;
202 return job;
205 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
207 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
210 static struct object_type *job_get_type( struct object *obj )
212 static const WCHAR name[] = {'J','o','b'};
213 static const struct unicode_str str = { name, sizeof(name) };
214 return get_object_type( &str );
217 static unsigned int job_map_access( struct object *obj, unsigned int access )
219 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
220 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
221 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
222 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
223 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
226 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
228 if (job->completion_port)
229 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
232 static void add_job_process( struct job *job, struct process *process )
234 process->job = (struct job *)grab_object( job );
235 list_add_tail( &job->process_list, &process->job_entry );
236 job->num_processes++;
238 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
241 /* called when a process has terminated, allow one additional process */
242 static void release_job_process( struct process *process )
244 struct job *job = process->job;
246 if (!job) return;
248 assert( job->num_processes );
249 job->num_processes--;
251 if (!job->terminating)
252 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
254 if (!job->num_processes)
255 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
258 static void terminate_job( struct job *job, int exit_code )
260 /* don't report completion events for terminated processes */
261 job->terminating = 1;
263 for (;;) /* restart from the beginning of the list every time */
265 struct process *process;
267 /* find the first process associated with this job and still running */
268 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
270 if (process->running_threads) break;
272 if (&process->job_entry == &job->process_list) break; /* no process found */
273 assert( process->job == job );
274 terminate_process( process, NULL, exit_code );
277 job->terminating = 0;
278 job->signaled = 1;
279 wake_up( &job->obj, 0 );
282 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
284 struct job *job = (struct job *)obj;
285 assert( obj->ops == &job_ops );
287 if (obj->handle_count == 1) /* last handle */
289 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
290 terminate_job( job, 0 );
292 return 1;
295 static void job_destroy( struct object *obj )
297 struct job *job = (struct job *)obj;
298 assert( obj->ops == &job_ops );
300 assert( !job->num_processes );
301 assert( list_empty(&job->process_list) );
303 if (job->completion_port) release_object( job->completion_port );
306 static void job_dump( struct object *obj, int verbose )
308 struct job *job = (struct job *)obj;
309 assert( obj->ops == &job_ops );
310 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
313 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
315 struct job *job = (struct job *)obj;
316 return job->signaled;
319 struct ptid_entry
321 void *ptr; /* entry ptr */
322 unsigned int next; /* next free entry */
325 static struct ptid_entry *ptid_entries; /* array of ptid entries */
326 static unsigned int used_ptid_entries; /* number of entries in use */
327 static unsigned int alloc_ptid_entries; /* number of allocated entries */
328 static unsigned int next_free_ptid; /* next free entry */
329 static unsigned int last_free_ptid; /* last free entry */
330 static unsigned int num_free_ptids; /* number of free ptids */
332 static void kill_all_processes(void);
334 #define PTID_OFFSET 8 /* offset for first ptid value */
336 /* allocate a new process or thread id */
337 unsigned int alloc_ptid( void *ptr )
339 struct ptid_entry *entry;
340 unsigned int id;
342 if (used_ptid_entries < alloc_ptid_entries)
344 id = used_ptid_entries + PTID_OFFSET;
345 entry = &ptid_entries[used_ptid_entries++];
347 else if (next_free_ptid && num_free_ptids >= 256)
349 id = next_free_ptid;
350 entry = &ptid_entries[id - PTID_OFFSET];
351 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
352 num_free_ptids--;
354 else /* need to grow the array */
356 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
357 if (!count) count = 512;
358 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
360 set_error( STATUS_NO_MEMORY );
361 return 0;
363 ptid_entries = entry;
364 alloc_ptid_entries = count;
365 id = used_ptid_entries + PTID_OFFSET;
366 entry = &ptid_entries[used_ptid_entries++];
369 entry->ptr = ptr;
370 return id;
373 /* free a process or thread id */
374 void free_ptid( unsigned int id )
376 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
378 entry->ptr = NULL;
379 entry->next = 0;
381 /* append to end of free list so that we don't reuse it too early */
382 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
383 else next_free_ptid = id;
384 last_free_ptid = id;
385 num_free_ptids++;
388 /* retrieve the pointer corresponding to a process or thread id */
389 void *get_ptid_entry( unsigned int id )
391 if (id < PTID_OFFSET) return NULL;
392 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
393 return ptid_entries[id - PTID_OFFSET].ptr;
396 /* return the main thread of the process */
397 struct thread *get_process_first_thread( struct process *process )
399 struct list *ptr = list_head( &process->thread_list );
400 if (!ptr) return NULL;
401 return LIST_ENTRY( ptr, struct thread, proc_entry );
404 /* set the state of the process startup info */
405 static void set_process_startup_state( struct process *process, enum startup_state state )
407 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
408 if (process->startup_info)
410 wake_up( &process->startup_info->obj, 0 );
411 release_object( process->startup_info );
412 process->startup_info = NULL;
416 /* callback for server shutdown */
417 static void server_shutdown_timeout( void *arg )
419 shutdown_timeout = NULL;
420 if (!running_processes)
422 close_master_socket( 0 );
423 return;
425 switch(++shutdown_stage)
427 case 1: /* signal system processes to exit */
428 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
429 if (shutdown_event) set_event( shutdown_event );
430 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
431 close_master_socket( 4 * -TICKS_PER_SEC );
432 break;
433 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
434 kill_all_processes();
435 break;
439 /* forced shutdown, used for wineserver -k */
440 void shutdown_master_socket(void)
442 kill_all_processes();
443 shutdown_stage = 2;
444 if (shutdown_timeout)
446 remove_timeout_user( shutdown_timeout );
447 shutdown_timeout = NULL;
449 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
452 /* final cleanup once we are sure a process is really dead */
453 static void process_died( struct process *process )
455 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
456 if (!process->is_system)
458 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
459 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
461 release_object( process );
462 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
465 /* callback for process sigkill timeout */
466 static void process_sigkill( void *private )
468 struct process *process = private;
470 process->sigkill_timeout = NULL;
471 kill( process->unix_pid, SIGKILL );
472 process_died( process );
475 /* start the sigkill timer for a process upon exit */
476 static void start_sigkill_timer( struct process *process )
478 grab_object( process );
479 if (process->unix_pid != -1 && process->msg_fd)
480 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
481 else
482 process_died( process );
485 /* create a new process */
486 /* if the function fails the fd is closed */
487 struct process *create_process( int fd, struct thread *parent_thread, int inherit_all,
488 const struct security_descriptor *sd )
490 struct process *process;
492 if (!(process = alloc_object( &process_ops )))
494 close( fd );
495 goto error;
497 process->parent_id = 0;
498 process->debugger = NULL;
499 process->handles = NULL;
500 process->msg_fd = NULL;
501 process->sigkill_timeout = NULL;
502 process->unix_pid = -1;
503 process->exit_code = STILL_ACTIVE;
504 process->running_threads = 0;
505 process->priority = PROCESS_PRIOCLASS_NORMAL;
506 process->suspend = 0;
507 process->is_system = 0;
508 process->debug_children = 1;
509 process->is_terminating = 0;
510 process->job = NULL;
511 process->console = NULL;
512 process->startup_state = STARTUP_IN_PROGRESS;
513 process->startup_info = NULL;
514 process->idle_event = NULL;
515 process->exe_file = NULL;
516 process->peb = 0;
517 process->ldt_copy = 0;
518 process->dir_cache = NULL;
519 process->winstation = 0;
520 process->desktop = 0;
521 process->token = NULL;
522 process->trace_data = 0;
523 process->rawinput_mouse = NULL;
524 process->rawinput_kbd = NULL;
525 list_init( &process->thread_list );
526 list_init( &process->locks );
527 list_init( &process->asyncs );
528 list_init( &process->classes );
529 list_init( &process->views );
530 list_init( &process->dlls );
531 list_init( &process->rawinput_devices );
533 process->end_time = 0;
534 list_add_tail( &process_list, &process->entry );
536 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
537 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
539 close( fd );
540 goto error;
542 if (!(process->id = process->group_id = alloc_ptid( process )))
544 close( fd );
545 goto error;
547 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
549 /* create the handle table */
550 if (!parent_thread)
552 process->handles = alloc_handle_table( process, 0 );
553 process->token = token_create_admin();
554 process->affinity = ~0;
556 else
558 struct process *parent = parent_thread->process;
559 process->parent_id = parent->id;
560 process->handles = inherit_all ? copy_handle_table( process, parent )
561 : alloc_handle_table( process, 0 );
562 /* Note: for security reasons, starting a new process does not attempt
563 * to use the current impersonation token for the new process */
564 process->token = token_duplicate( parent->token, TRUE, 0, NULL );
565 process->affinity = parent->affinity;
567 if (!process->handles || !process->token) goto error;
569 /* Assign a high security label to the token. The default would be medium
570 * but Wine provides admin access to all applications right now so high
571 * makes more sense for the time being. */
572 if (!token_assign_label( process->token, security_high_label_sid ))
573 goto error;
575 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
576 return process;
578 error:
579 if (process) release_object( process );
580 /* if we failed to start our first process, close everything down */
581 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
582 return NULL;
585 /* initialize the current process and fill in the request */
586 data_size_t init_process( struct thread *thread )
588 struct process *process = thread->process;
589 struct startup_info *info = process->startup_info;
591 init_process_tracing( process );
592 if (!info) return 0;
593 return info->data_size;
596 /* destroy a process when its refcount is 0 */
597 static void process_destroy( struct object *obj )
599 struct process *process = (struct process *)obj;
600 assert( obj->ops == &process_ops );
602 /* we can't have a thread remaining */
603 assert( list_empty( &process->thread_list ));
604 assert( list_empty( &process->asyncs ));
606 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
608 close_process_handles( process );
609 set_process_startup_state( process, STARTUP_ABORTED );
611 if (process->job)
613 list_remove( &process->job_entry );
614 release_object( process->job );
616 if (process->console) release_object( process->console );
617 if (process->msg_fd) release_object( process->msg_fd );
618 list_remove( &process->entry );
619 if (process->idle_event) release_object( process->idle_event );
620 if (process->exe_file) release_object( process->exe_file );
621 if (process->id) free_ptid( process->id );
622 if (process->token) release_object( process->token );
623 free( process->dir_cache );
626 /* dump a process on stdout for debugging purposes */
627 static void process_dump( struct object *obj, int verbose )
629 struct process *process = (struct process *)obj;
630 assert( obj->ops == &process_ops );
632 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
635 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
637 struct process *process = (struct process *)obj;
638 return !process->running_threads;
641 static unsigned int process_map_access( struct object *obj, unsigned int access )
643 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
644 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
645 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
646 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
647 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
649 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
650 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
652 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
655 static void process_poll_event( struct fd *fd, int event )
657 struct process *process = get_fd_user( fd );
658 assert( process->obj.ops == &process_ops );
660 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
661 else if (event & POLLIN) receive_fd( process );
664 static void startup_info_destroy( struct object *obj )
666 struct startup_info *info = (struct startup_info *)obj;
667 assert( obj->ops == &startup_info_ops );
668 free( info->data );
669 if (info->process) release_object( info->process );
672 static void startup_info_dump( struct object *obj, int verbose )
674 struct startup_info *info = (struct startup_info *)obj;
675 assert( obj->ops == &startup_info_ops );
677 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
678 info->data->hstdin, info->data->hstdout, info->data->hstderr );
681 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
683 struct startup_info *info = (struct startup_info *)obj;
684 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
687 /* get a process from an id (and increment the refcount) */
688 struct process *get_process_from_id( process_id_t id )
690 struct object *obj = get_ptid_entry( id );
692 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
693 set_error( STATUS_INVALID_PARAMETER );
694 return NULL;
697 /* get a process from a handle (and increment the refcount) */
698 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
700 return (struct process *)get_handle_obj( current->process, handle,
701 access, &process_ops );
704 /* find a dll from its base address */
705 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
707 struct process_dll *dll;
709 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
711 if (dll->base == base) return dll;
713 return NULL;
716 /* add a dll to a process list */
717 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
718 const WCHAR *filename, data_size_t name_len )
720 struct process_dll *dll;
722 /* make sure we don't already have one with the same base address */
723 if (find_process_dll( process, base ))
725 set_error( STATUS_INVALID_PARAMETER );
726 return NULL;
729 if ((dll = mem_alloc( sizeof(*dll) )))
731 dll->base = base;
732 dll->filename = NULL;
733 dll->namelen = name_len;
734 if (name_len && !(dll->filename = memdup( filename, name_len )))
736 free( dll );
737 return NULL;
739 list_add_tail( &process->dlls, &dll->entry );
741 return dll;
744 /* remove a dll from a process list */
745 static void process_unload_dll( struct process *process, mod_handle_t base )
747 struct process_dll *dll = find_process_dll( process, base );
749 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
751 free( dll->filename );
752 list_remove( &dll->entry );
753 free( dll );
754 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
756 else set_error( STATUS_INVALID_PARAMETER );
759 /* terminate a process with the given exit code */
760 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
762 struct thread *thread;
764 grab_object( process ); /* make sure it doesn't get freed when threads die */
765 process->is_terminating = 1;
767 restart:
768 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
770 if (exit_code) thread->exit_code = exit_code;
771 if (thread == skip) continue;
772 if (thread->state == TERMINATED) continue;
773 kill_thread( thread, 1 );
774 goto restart;
776 release_object( process );
779 /* kill all processes */
780 static void kill_all_processes(void)
782 for (;;)
784 struct process *process;
786 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
788 if (process->running_threads) break;
790 if (&process->entry == &process_list) break; /* no process found */
791 terminate_process( process, NULL, 1 );
795 /* kill all processes being attached to a console renderer */
796 void kill_console_processes( struct thread *renderer, int exit_code )
798 for (;;) /* restart from the beginning of the list every time */
800 struct process *process;
802 /* find the first process being attached to 'renderer' and still running */
803 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
805 if (process == renderer->process) continue;
806 if (!process->running_threads) continue;
807 if (process->console && console_get_renderer( process->console ) == renderer) break;
809 if (&process->entry == &process_list) break; /* no process found */
810 terminate_process( process, NULL, exit_code );
814 /* a process has been killed (i.e. its last thread died) */
815 static void process_killed( struct process *process )
817 struct list *ptr;
819 assert( list_empty( &process->thread_list ));
820 process->end_time = current_time;
821 if (!process->is_system) close_process_desktop( process );
822 process->winstation = 0;
823 process->desktop = 0;
824 close_process_handles( process );
825 cancel_process_asyncs( process );
826 if (process->idle_event) release_object( process->idle_event );
827 if (process->exe_file) release_object( process->exe_file );
828 process->idle_event = NULL;
829 process->exe_file = NULL;
831 /* close the console attached to this process, if any */
832 free_console( process );
834 while ((ptr = list_head( &process->rawinput_devices )))
836 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
837 list_remove( &entry->entry );
838 free( entry );
840 while ((ptr = list_head( &process->dlls )))
842 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
843 free( dll->filename );
844 list_remove( &dll->entry );
845 free( dll );
847 destroy_process_classes( process );
848 free_mapped_views( process );
849 free_process_user_handles( process );
850 remove_process_locks( process );
851 set_process_startup_state( process, STARTUP_ABORTED );
852 finish_process_tracing( process );
853 release_job_process( process );
854 start_sigkill_timer( process );
855 wake_up( &process->obj, 0 );
858 /* add a thread to a process running threads list */
859 void add_process_thread( struct process *process, struct thread *thread )
861 list_add_tail( &process->thread_list, &thread->proc_entry );
862 if (!process->running_threads++)
864 running_processes++;
865 if (!process->is_system)
867 if (!user_processes++ && shutdown_timeout)
869 remove_timeout_user( shutdown_timeout );
870 shutdown_timeout = NULL;
874 grab_object( thread );
877 /* remove a thread from a process running threads list */
878 void remove_process_thread( struct process *process, struct thread *thread )
880 assert( process->running_threads > 0 );
881 assert( !list_empty( &process->thread_list ));
883 list_remove( &thread->proc_entry );
885 if (!--process->running_threads)
887 /* we have removed the last running thread, exit the process */
888 process->exit_code = thread->exit_code;
889 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
890 process_killed( process );
892 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
893 release_object( thread );
896 /* suspend all the threads of a process */
897 void suspend_process( struct process *process )
899 if (!process->suspend++)
901 struct list *ptr, *next;
903 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
905 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
906 if (!thread->suspend) stop_thread( thread );
911 /* resume all the threads of a process */
912 void resume_process( struct process *process )
914 assert (process->suspend > 0);
915 if (!--process->suspend)
917 struct list *ptr, *next;
919 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
921 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
922 if (!thread->suspend) wake_thread( thread );
927 /* kill a process on the spot */
928 void kill_process( struct process *process, int violent_death )
930 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
932 release_object( process->msg_fd );
933 process->msg_fd = NULL;
936 if (process->sigkill_timeout) /* already waiting for it to die */
938 remove_timeout_user( process->sigkill_timeout );
939 process->sigkill_timeout = NULL;
940 process_died( process );
941 return;
944 if (violent_death) terminate_process( process, NULL, 1 );
945 else
947 struct list *ptr;
949 grab_object( process ); /* make sure it doesn't get freed when threads die */
950 while ((ptr = list_head( &process->thread_list )))
952 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
953 kill_thread( thread, 0 );
955 release_object( process );
959 /* kill all processes being debugged by a given thread */
960 void kill_debugged_processes( struct thread *debugger, int exit_code )
962 for (;;) /* restart from the beginning of the list every time */
964 struct process *process;
966 /* find the first process being debugged by 'debugger' and still running */
967 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
969 if (!process->running_threads) continue;
970 if (process->debugger == debugger) break;
972 if (&process->entry == &process_list) break; /* no process found */
973 process->debugger = NULL;
974 terminate_process( process, NULL, exit_code );
979 /* trigger a breakpoint event in a given process */
980 void break_process( struct process *process )
982 struct thread *thread;
984 suspend_process( process );
986 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
988 if (thread->context) /* inside an exception event already */
990 break_thread( thread );
991 goto done;
994 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
995 else set_error( STATUS_ACCESS_DENIED );
996 done:
997 resume_process( process );
1001 /* detach a debugger from all its debuggees */
1002 void detach_debugged_processes( struct thread *debugger )
1004 struct process *process;
1006 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1008 if (process->debugger == debugger && process->running_threads)
1010 debugger_detach( process, debugger );
1016 void enum_processes( int (*cb)(struct process*, void*), void *user )
1018 struct list *ptr, *next;
1020 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1022 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1023 if ((cb)(process, user)) break;
1027 /* set the debugged flag in the process PEB */
1028 int set_process_debug_flag( struct process *process, int flag )
1030 char data = (flag != 0);
1032 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1033 return write_process_memory( process, process->peb + 2, 1, &data );
1036 /* take a snapshot of currently running processes */
1037 struct process_snapshot *process_snap( int *count )
1039 struct process_snapshot *snapshot, *ptr;
1040 struct process *process;
1042 if (!running_processes) return NULL;
1043 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1044 return NULL;
1045 ptr = snapshot;
1046 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1048 if (!process->running_threads) continue;
1049 ptr->process = process;
1050 ptr->threads = process->running_threads;
1051 ptr->count = process->obj.refcount;
1052 ptr->priority = process->priority;
1053 ptr->handles = get_handle_table_count(process);
1054 grab_object( process );
1055 ptr++;
1058 if (!(*count = ptr - snapshot))
1060 free( snapshot );
1061 snapshot = NULL;
1063 return snapshot;
1066 /* create a new process */
1067 DECL_HANDLER(new_process)
1069 struct startup_info *info;
1070 const void *info_ptr;
1071 struct unicode_str name;
1072 const struct security_descriptor *sd;
1073 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1074 struct process *process = NULL;
1075 struct process *parent = current->process;
1076 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1078 if (socket_fd == -1)
1080 set_error( STATUS_INVALID_PARAMETER );
1081 return;
1083 if (!objattr)
1085 set_error( STATUS_INVALID_PARAMETER );
1086 close( socket_fd );
1087 return;
1089 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1091 set_error( STATUS_INVALID_HANDLE );
1092 close( socket_fd );
1093 return;
1095 if (shutdown_stage)
1097 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1098 close( socket_fd );
1099 return;
1101 if (!is_cpu_supported( req->cpu ))
1103 close( socket_fd );
1104 return;
1107 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1108 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1110 set_error( STATUS_ACCESS_DENIED );
1111 close( socket_fd );
1112 return;
1115 if (!req->info_size) /* create an orphaned process */
1117 if ((process = create_process( socket_fd, NULL, 0, sd )))
1119 create_thread( -1, process, NULL );
1120 release_object( process );
1122 return;
1125 /* build the startup info for a new process */
1126 if (!(info = alloc_object( &startup_info_ops )))
1128 close( socket_fd );
1129 return;
1131 info->process = NULL;
1132 info->data = NULL;
1134 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1135 info->info_size = min( req->info_size, info->data_size );
1137 if (req->info_size < sizeof(*info->data))
1139 /* make sure we have a full startup_info_t structure */
1140 data_size_t env_size = info->data_size - info->info_size;
1141 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1143 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1145 close( socket_fd );
1146 goto done;
1148 memcpy( info->data, info_ptr, info_size );
1149 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1150 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1151 info->info_size = sizeof(startup_info_t);
1152 info->data_size = info->info_size + env_size;
1154 else
1156 data_size_t pos = sizeof(*info->data);
1158 if (!(info->data = memdup( info_ptr, info->data_size )))
1160 close( socket_fd );
1161 goto done;
1163 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1164 FIXUP_LEN( info->data->curdir_len );
1165 FIXUP_LEN( info->data->dllpath_len );
1166 FIXUP_LEN( info->data->imagepath_len );
1167 FIXUP_LEN( info->data->cmdline_len );
1168 FIXUP_LEN( info->data->title_len );
1169 FIXUP_LEN( info->data->desktop_len );
1170 FIXUP_LEN( info->data->shellinfo_len );
1171 FIXUP_LEN( info->data->runtime_len );
1172 #undef FIXUP_LEN
1175 if (!(process = create_process( socket_fd, current, req->inherit_all, sd ))) goto done;
1177 process->startup_info = (struct startup_info *)grab_object( info );
1179 if (req->exe_file &&
1180 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1181 goto done;
1183 if (parent->job
1184 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1185 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1187 add_job_process( parent->job, process );
1190 /* connect to the window station */
1191 connect_process_winstation( process, current );
1193 /* set the process console */
1194 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1196 /* FIXME: some better error checking should be done...
1197 * like if hConOut and hConIn are console handles, then they should be on the same
1198 * physical console
1200 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1203 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1205 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1206 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1207 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1208 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1209 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1210 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1211 /* some handles above may have been invalid; this is not an error */
1212 if (get_error() == STATUS_INVALID_HANDLE ||
1213 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1216 /* attach to the debugger if requested */
1217 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1219 set_process_debugger( process, current );
1220 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1222 else if (parent->debugger && parent->debug_children)
1224 set_process_debugger( process, parent->debugger );
1225 /* debug_children is set to 1 by default */
1228 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1229 process->group_id = parent->group_id;
1231 info->process = (struct process *)grab_object( process );
1232 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1233 reply->pid = get_process_id( process );
1234 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1236 done:
1237 if (process) release_object( process );
1238 release_object( info );
1241 /* Retrieve information about a newly started process */
1242 DECL_HANDLER(get_new_process_info)
1244 struct startup_info *info;
1246 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1247 0, &startup_info_ops )))
1249 reply->success = is_process_init_done( info->process );
1250 reply->exit_code = info->process->exit_code;
1251 release_object( info );
1255 /* Retrieve the new process startup info */
1256 DECL_HANDLER(get_startup_info)
1258 struct process *process = current->process;
1259 struct startup_info *info = process->startup_info;
1260 data_size_t size;
1262 if (!info) return;
1264 /* we return the data directly without making a copy so this can only be called once */
1265 reply->info_size = info->info_size;
1266 size = info->data_size;
1267 if (size > get_reply_max_size()) size = get_reply_max_size();
1268 set_reply_data_ptr( info->data, size );
1269 info->data = NULL;
1270 info->data_size = 0;
1273 /* signal the end of the process initialization */
1274 DECL_HANDLER(init_process_done)
1276 struct process_dll *dll;
1277 struct process *process = current->process;
1279 if (is_process_init_done(process))
1281 set_error( STATUS_INVALID_PARAMETER );
1282 return;
1284 if (!(dll = find_process_dll( process, req->module )))
1286 set_error( STATUS_DLL_NOT_FOUND );
1287 return;
1290 /* main exe is the first in the dll list */
1291 list_remove( &dll->entry );
1292 list_add_head( &process->dlls, &dll->entry );
1294 process->ldt_copy = req->ldt_copy;
1295 process->start_time = current_time;
1296 current->entry_point = req->entry;
1297 if (process->exe_file) release_object( process->exe_file );
1298 process->exe_file = NULL;
1300 generate_startup_debug_events( process, req->entry );
1301 set_process_startup_state( process, STARTUP_DONE );
1303 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1304 if (process->debugger) set_process_debug_flag( process, 1 );
1305 reply->suspend = (current->suspend || process->suspend);
1308 /* open a handle to a process */
1309 DECL_HANDLER(open_process)
1311 struct process *process = get_process_from_id( req->pid );
1312 reply->handle = 0;
1313 if (process)
1315 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1316 release_object( process );
1320 /* terminate a process */
1321 DECL_HANDLER(terminate_process)
1323 struct process *process;
1325 if (req->handle)
1327 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1328 if (!process) return;
1330 else process = (struct process *)grab_object( current->process );
1332 reply->self = (current->process == process);
1333 terminate_process( process, current, req->exit_code );
1334 release_object( process );
1337 /* fetch information about a process */
1338 DECL_HANDLER(get_process_info)
1340 struct process *process;
1342 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1344 reply->pid = get_process_id( process );
1345 reply->ppid = process->parent_id;
1346 reply->exit_code = process->exit_code;
1347 reply->priority = process->priority;
1348 reply->affinity = process->affinity;
1349 reply->peb = process->peb;
1350 reply->start_time = process->start_time;
1351 reply->end_time = process->end_time;
1352 reply->cpu = process->cpu;
1353 reply->debugger_present = !!process->debugger;
1354 reply->debug_children = process->debug_children;
1355 release_object( process );
1359 /* retrieve information about a process memory usage */
1360 DECL_HANDLER(get_process_vm_counters)
1362 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1364 if (!process) return;
1365 #ifdef linux
1366 if (process->unix_pid != -1)
1368 FILE *f;
1369 char proc_path[32], line[256];
1370 unsigned long value;
1372 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1373 if ((f = fopen( proc_path, "r" )))
1375 while (fgets( line, sizeof(line), f ))
1377 if (sscanf( line, "VmPeak: %lu", &value ))
1378 reply->peak_virtual_size = (mem_size_t)value * 1024;
1379 else if (sscanf( line, "VmSize: %lu", &value ))
1380 reply->virtual_size = (mem_size_t)value * 1024;
1381 else if (sscanf( line, "VmHWM: %lu", &value ))
1382 reply->peak_working_set_size = (mem_size_t)value * 1024;
1383 else if (sscanf( line, "VmRSS: %lu", &value ))
1384 reply->working_set_size = (mem_size_t)value * 1024;
1385 else if (sscanf( line, "RssAnon: %lu", &value ))
1386 reply->pagefile_usage += (mem_size_t)value * 1024;
1387 else if (sscanf( line, "VmSwap: %lu", &value ))
1388 reply->pagefile_usage += (mem_size_t)value * 1024;
1390 reply->peak_pagefile_usage = reply->pagefile_usage;
1391 fclose( f );
1393 else set_error( STATUS_ACCESS_DENIED );
1395 else set_error( STATUS_ACCESS_DENIED );
1396 #endif
1397 release_object( process );
1400 static void set_process_affinity( struct process *process, affinity_t affinity )
1402 struct thread *thread;
1404 if (!process->running_threads)
1406 set_error( STATUS_PROCESS_IS_TERMINATING );
1407 return;
1410 process->affinity = affinity;
1412 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1414 set_thread_affinity( thread, affinity );
1418 /* set information about a process */
1419 DECL_HANDLER(set_process_info)
1421 struct process *process;
1423 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1425 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1426 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1427 release_object( process );
1431 /* read data from a process address space */
1432 DECL_HANDLER(read_process_memory)
1434 struct process *process;
1435 data_size_t len = get_reply_max_size();
1437 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1439 if (len)
1441 char *buffer = mem_alloc( len );
1442 if (buffer)
1444 if (read_process_memory( process, req->addr, len, buffer ))
1445 set_reply_data_ptr( buffer, len );
1446 else
1447 free( buffer );
1450 release_object( process );
1453 /* write data to a process address space */
1454 DECL_HANDLER(write_process_memory)
1456 struct process *process;
1458 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1460 data_size_t len = get_req_data_size();
1461 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1462 else set_error( STATUS_INVALID_PARAMETER );
1463 release_object( process );
1467 /* notify the server that a dll has been loaded */
1468 DECL_HANDLER(load_dll)
1470 struct process_dll *dll;
1472 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1474 dll->dbg_offset = req->dbg_offset;
1475 dll->dbg_size = req->dbg_size;
1476 dll->name = req->name;
1477 /* only generate event if initialization is done */
1478 if (is_process_init_done( current->process ))
1479 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1483 /* notify the server that a dll is being unloaded */
1484 DECL_HANDLER(unload_dll)
1486 process_unload_dll( current->process, req->base );
1489 /* retrieve information about a module in a process */
1490 DECL_HANDLER(get_dll_info)
1492 struct process *process;
1494 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1496 struct process_dll *dll;
1498 if (req->base_address)
1499 dll = find_process_dll( process, req->base_address );
1500 else /* NULL means main module */
1501 dll = list_head( &process->dlls ) ?
1502 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1504 if (dll)
1506 reply->entry_point = 0; /* FIXME */
1507 reply->filename_len = dll->namelen;
1508 if (dll->filename)
1510 if (dll->namelen <= get_reply_max_size())
1511 set_reply_data( dll->filename, dll->namelen );
1512 else
1513 set_error( STATUS_BUFFER_TOO_SMALL );
1516 else
1517 set_error( STATUS_DLL_NOT_FOUND );
1519 release_object( process );
1523 /* retrieve the process idle event */
1524 DECL_HANDLER(get_process_idle_event)
1526 struct process *process;
1528 reply->event = 0;
1529 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1531 if (process->idle_event && process != current->process)
1532 reply->event = alloc_handle( current->process, process->idle_event,
1533 EVENT_ALL_ACCESS, 0 );
1534 release_object( process );
1538 /* make the current process a system process */
1539 DECL_HANDLER(make_process_system)
1541 struct process *process = current->process;
1543 if (!shutdown_event)
1545 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1546 make_object_static( (struct object *)shutdown_event );
1549 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1550 return;
1552 if (!process->is_system)
1554 process->is_system = 1;
1555 close_process_desktop( process );
1556 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1557 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1561 /* create a new job object */
1562 DECL_HANDLER(create_job)
1564 struct job *job;
1565 struct unicode_str name;
1566 struct object *root;
1567 const struct security_descriptor *sd;
1568 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1570 if (!objattr) return;
1572 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1574 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1575 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1576 else
1577 reply->handle = alloc_handle_no_access_check( current->process, job,
1578 req->access, objattr->attributes );
1579 release_object( job );
1581 if (root) release_object( root );
1584 /* open a job object */
1585 DECL_HANDLER(open_job)
1587 struct unicode_str name = get_req_unicode_str();
1589 reply->handle = open_object( current->process, req->rootdir, req->access,
1590 &job_ops, &name, req->attributes );
1593 /* assign a job object to a process */
1594 DECL_HANDLER(assign_job)
1596 struct process *process;
1597 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1599 if (!job) return;
1601 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1603 if (!process->running_threads)
1604 set_error( STATUS_PROCESS_IS_TERMINATING );
1605 else if (process->job)
1606 set_error( STATUS_ACCESS_DENIED );
1607 else
1608 add_job_process( job, process );
1609 release_object( process );
1611 release_object( job );
1615 /* check if a process is associated with a job */
1616 DECL_HANDLER(process_in_job)
1618 struct process *process;
1619 struct job *job;
1621 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1622 return;
1624 if (!req->job)
1626 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1628 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1630 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1631 release_object( job );
1633 release_object( process );
1636 /* terminate all processes associated with the job */
1637 DECL_HANDLER(terminate_job)
1639 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1641 if (!job) return;
1643 terminate_job( job, req->status );
1644 release_object( job );
1647 /* update limits of the job object */
1648 DECL_HANDLER(set_job_limits)
1650 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1652 if (!job) return;
1654 job->limit_flags = req->limit_flags;
1655 release_object( job );
1658 /* set the jobs completion port */
1659 DECL_HANDLER(set_job_completion_port)
1661 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1663 if (!job) return;
1665 if (!job->completion_port)
1667 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1668 job->completion_key = req->key;
1670 else
1671 set_error( STATUS_INVALID_PARAMETER );
1673 release_object( job );