ntdll: Add more traces to raise_exception on ARM64.
[wine.git] / server / process.c
blob3bd545ac493701e83f193706effe0cfe6c30110a
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 process *parent, 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)
552 process->handles = alloc_handle_table( process, 0 );
553 process->token = token_create_admin();
554 process->affinity = ~0;
556 else
558 process->parent_id = parent->id;
559 process->handles = inherit_all ? copy_handle_table( process, parent )
560 : alloc_handle_table( process, 0 );
561 /* Note: for security reasons, starting a new process does not attempt
562 * to use the current impersonation token for the new process */
563 process->token = token_duplicate( parent->token, TRUE, 0, NULL );
564 process->affinity = parent->affinity;
566 if (!process->handles || !process->token) goto error;
568 /* Assign a high security label to the token. The default would be medium
569 * but Wine provides admin access to all applications right now so high
570 * makes more sense for the time being. */
571 if (!token_assign_label( process->token, security_high_label_sid ))
572 goto error;
574 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
575 return process;
577 error:
578 if (process) release_object( process );
579 /* if we failed to start our first process, close everything down */
580 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
581 return NULL;
584 /* initialize the current process and fill in the request */
585 data_size_t init_process( struct thread *thread )
587 struct process *process = thread->process;
588 struct startup_info *info = process->startup_info;
590 init_process_tracing( process );
591 if (!info) return 0;
592 return info->data_size;
595 /* destroy a process when its refcount is 0 */
596 static void process_destroy( struct object *obj )
598 struct process *process = (struct process *)obj;
599 assert( obj->ops == &process_ops );
601 /* we can't have a thread remaining */
602 assert( list_empty( &process->thread_list ));
603 assert( list_empty( &process->asyncs ));
605 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
607 close_process_handles( process );
608 set_process_startup_state( process, STARTUP_ABORTED );
610 if (process->job)
612 list_remove( &process->job_entry );
613 release_object( process->job );
615 if (process->console) release_object( process->console );
616 if (process->msg_fd) release_object( process->msg_fd );
617 list_remove( &process->entry );
618 if (process->idle_event) release_object( process->idle_event );
619 if (process->exe_file) release_object( process->exe_file );
620 if (process->id) free_ptid( process->id );
621 if (process->token) release_object( process->token );
622 free( process->dir_cache );
625 /* dump a process on stdout for debugging purposes */
626 static void process_dump( struct object *obj, int verbose )
628 struct process *process = (struct process *)obj;
629 assert( obj->ops == &process_ops );
631 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
634 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
636 struct process *process = (struct process *)obj;
637 return !process->running_threads;
640 static unsigned int process_map_access( struct object *obj, unsigned int access )
642 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
643 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
644 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
645 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
646 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
648 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
649 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
651 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
654 static void process_poll_event( struct fd *fd, int event )
656 struct process *process = get_fd_user( fd );
657 assert( process->obj.ops == &process_ops );
659 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
660 else if (event & POLLIN) receive_fd( process );
663 static void startup_info_destroy( struct object *obj )
665 struct startup_info *info = (struct startup_info *)obj;
666 assert( obj->ops == &startup_info_ops );
667 free( info->data );
668 if (info->process) release_object( info->process );
671 static void startup_info_dump( struct object *obj, int verbose )
673 struct startup_info *info = (struct startup_info *)obj;
674 assert( obj->ops == &startup_info_ops );
676 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
677 info->data->hstdin, info->data->hstdout, info->data->hstderr );
680 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
682 struct startup_info *info = (struct startup_info *)obj;
683 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
686 /* get a process from an id (and increment the refcount) */
687 struct process *get_process_from_id( process_id_t id )
689 struct object *obj = get_ptid_entry( id );
691 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
692 set_error( STATUS_INVALID_PARAMETER );
693 return NULL;
696 /* get a process from a handle (and increment the refcount) */
697 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
699 return (struct process *)get_handle_obj( current->process, handle,
700 access, &process_ops );
703 /* find a dll from its base address */
704 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
706 struct process_dll *dll;
708 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
710 if (dll->base == base) return dll;
712 return NULL;
715 /* add a dll to a process list */
716 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
717 const WCHAR *filename, data_size_t name_len )
719 struct process_dll *dll;
721 /* make sure we don't already have one with the same base address */
722 if (find_process_dll( process, base ))
724 set_error( STATUS_INVALID_PARAMETER );
725 return NULL;
728 if ((dll = mem_alloc( sizeof(*dll) )))
730 dll->base = base;
731 dll->filename = NULL;
732 dll->namelen = name_len;
733 if (name_len && !(dll->filename = memdup( filename, name_len )))
735 free( dll );
736 return NULL;
738 list_add_tail( &process->dlls, &dll->entry );
740 return dll;
743 /* remove a dll from a process list */
744 static void process_unload_dll( struct process *process, mod_handle_t base )
746 struct process_dll *dll = find_process_dll( process, base );
748 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
750 free( dll->filename );
751 list_remove( &dll->entry );
752 free( dll );
753 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
755 else set_error( STATUS_INVALID_PARAMETER );
758 /* terminate a process with the given exit code */
759 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
761 struct thread *thread;
763 grab_object( process ); /* make sure it doesn't get freed when threads die */
764 process->is_terminating = 1;
766 restart:
767 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
769 if (exit_code) thread->exit_code = exit_code;
770 if (thread == skip) continue;
771 if (thread->state == TERMINATED) continue;
772 kill_thread( thread, 1 );
773 goto restart;
775 release_object( process );
778 /* kill all processes */
779 static void kill_all_processes(void)
781 for (;;)
783 struct process *process;
785 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
787 if (process->running_threads) break;
789 if (&process->entry == &process_list) break; /* no process found */
790 terminate_process( process, NULL, 1 );
794 /* kill all processes being attached to a console renderer */
795 void kill_console_processes( struct thread *renderer, int exit_code )
797 for (;;) /* restart from the beginning of the list every time */
799 struct process *process;
801 /* find the first process being attached to 'renderer' and still running */
802 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
804 if (process == renderer->process) continue;
805 if (!process->running_threads) continue;
806 if (process->console && console_get_renderer( process->console ) == renderer) break;
808 if (&process->entry == &process_list) break; /* no process found */
809 terminate_process( process, NULL, exit_code );
813 /* a process has been killed (i.e. its last thread died) */
814 static void process_killed( struct process *process )
816 struct list *ptr;
818 assert( list_empty( &process->thread_list ));
819 process->end_time = current_time;
820 if (!process->is_system) close_process_desktop( process );
821 process->winstation = 0;
822 process->desktop = 0;
823 close_process_handles( process );
824 cancel_process_asyncs( process );
825 if (process->idle_event) release_object( process->idle_event );
826 if (process->exe_file) release_object( process->exe_file );
827 process->idle_event = NULL;
828 process->exe_file = NULL;
830 /* close the console attached to this process, if any */
831 free_console( process );
833 while ((ptr = list_head( &process->rawinput_devices )))
835 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
836 list_remove( &entry->entry );
837 free( entry );
839 while ((ptr = list_head( &process->dlls )))
841 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
842 free( dll->filename );
843 list_remove( &dll->entry );
844 free( dll );
846 destroy_process_classes( process );
847 free_mapped_views( process );
848 free_process_user_handles( process );
849 remove_process_locks( process );
850 set_process_startup_state( process, STARTUP_ABORTED );
851 finish_process_tracing( process );
852 release_job_process( process );
853 start_sigkill_timer( process );
854 wake_up( &process->obj, 0 );
857 /* add a thread to a process running threads list */
858 void add_process_thread( struct process *process, struct thread *thread )
860 list_add_tail( &process->thread_list, &thread->proc_entry );
861 if (!process->running_threads++)
863 running_processes++;
864 if (!process->is_system)
866 if (!user_processes++ && shutdown_timeout)
868 remove_timeout_user( shutdown_timeout );
869 shutdown_timeout = NULL;
873 grab_object( thread );
876 /* remove a thread from a process running threads list */
877 void remove_process_thread( struct process *process, struct thread *thread )
879 assert( process->running_threads > 0 );
880 assert( !list_empty( &process->thread_list ));
882 list_remove( &thread->proc_entry );
884 if (!--process->running_threads)
886 /* we have removed the last running thread, exit the process */
887 process->exit_code = thread->exit_code;
888 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
889 process_killed( process );
891 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
892 release_object( thread );
895 /* suspend all the threads of a process */
896 void suspend_process( struct process *process )
898 if (!process->suspend++)
900 struct list *ptr, *next;
902 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
904 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
905 if (!thread->suspend) stop_thread( thread );
910 /* resume all the threads of a process */
911 void resume_process( struct process *process )
913 assert (process->suspend > 0);
914 if (!--process->suspend)
916 struct list *ptr, *next;
918 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
920 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
921 if (!thread->suspend) wake_thread( thread );
926 /* kill a process on the spot */
927 void kill_process( struct process *process, int violent_death )
929 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
931 release_object( process->msg_fd );
932 process->msg_fd = NULL;
935 if (process->sigkill_timeout) /* already waiting for it to die */
937 remove_timeout_user( process->sigkill_timeout );
938 process->sigkill_timeout = NULL;
939 process_died( process );
940 return;
943 if (violent_death) terminate_process( process, NULL, 1 );
944 else
946 struct list *ptr;
948 grab_object( process ); /* make sure it doesn't get freed when threads die */
949 while ((ptr = list_head( &process->thread_list )))
951 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
952 kill_thread( thread, 0 );
954 release_object( process );
958 /* kill all processes being debugged by a given thread */
959 void kill_debugged_processes( struct thread *debugger, int exit_code )
961 for (;;) /* restart from the beginning of the list every time */
963 struct process *process;
965 /* find the first process being debugged by 'debugger' and still running */
966 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
968 if (!process->running_threads) continue;
969 if (process->debugger == debugger) break;
971 if (&process->entry == &process_list) break; /* no process found */
972 process->debugger = NULL;
973 terminate_process( process, NULL, exit_code );
978 /* trigger a breakpoint event in a given process */
979 void break_process( struct process *process )
981 struct thread *thread;
983 suspend_process( process );
985 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
987 if (thread->context) /* inside an exception event already */
989 break_thread( thread );
990 goto done;
993 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
994 else set_error( STATUS_ACCESS_DENIED );
995 done:
996 resume_process( process );
1000 /* detach a debugger from all its debuggees */
1001 void detach_debugged_processes( struct thread *debugger )
1003 struct process *process;
1005 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1007 if (process->debugger == debugger && process->running_threads)
1009 debugger_detach( process, debugger );
1015 void enum_processes( int (*cb)(struct process*, void*), void *user )
1017 struct list *ptr, *next;
1019 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1021 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1022 if ((cb)(process, user)) break;
1026 /* set the debugged flag in the process PEB */
1027 int set_process_debug_flag( struct process *process, int flag )
1029 char data = (flag != 0);
1031 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1032 return write_process_memory( process, process->peb + 2, 1, &data );
1035 /* take a snapshot of currently running processes */
1036 struct process_snapshot *process_snap( int *count )
1038 struct process_snapshot *snapshot, *ptr;
1039 struct process *process;
1041 if (!running_processes) return NULL;
1042 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1043 return NULL;
1044 ptr = snapshot;
1045 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1047 if (!process->running_threads) continue;
1048 ptr->process = process;
1049 ptr->threads = process->running_threads;
1050 ptr->count = process->obj.refcount;
1051 ptr->priority = process->priority;
1052 ptr->handles = get_handle_table_count(process);
1053 grab_object( process );
1054 ptr++;
1057 if (!(*count = ptr - snapshot))
1059 free( snapshot );
1060 snapshot = NULL;
1062 return snapshot;
1065 /* create a new process */
1066 DECL_HANDLER(new_process)
1068 struct startup_info *info;
1069 const void *info_ptr;
1070 struct unicode_str name;
1071 const struct security_descriptor *sd;
1072 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1073 struct process *process = NULL;
1074 struct process *parent = current->process;
1075 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1077 if (socket_fd == -1)
1079 set_error( STATUS_INVALID_PARAMETER );
1080 return;
1082 if (!objattr)
1084 set_error( STATUS_INVALID_PARAMETER );
1085 close( socket_fd );
1086 return;
1088 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1090 set_error( STATUS_INVALID_HANDLE );
1091 close( socket_fd );
1092 return;
1094 if (shutdown_stage)
1096 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1097 close( socket_fd );
1098 return;
1100 if (!is_cpu_supported( req->cpu ))
1102 close( socket_fd );
1103 return;
1106 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1107 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1109 set_error( STATUS_ACCESS_DENIED );
1110 close( socket_fd );
1111 return;
1114 /* build the startup info for a new process */
1115 if (!(info = alloc_object( &startup_info_ops )))
1117 close( socket_fd );
1118 return;
1120 info->process = NULL;
1121 info->data = NULL;
1123 info_ptr = get_req_data_after_objattr( objattr, &info->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, info_ptr, info_size );
1138 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1139 memcpy( info->data + 1, (const char *)info_ptr + 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( info_ptr, 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 (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1166 process->startup_info = (struct startup_info *)grab_object( info );
1168 if (req->exe_file &&
1169 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1170 goto done;
1172 if (parent->job
1173 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1174 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1176 add_job_process( parent->job, process );
1179 /* connect to the window station */
1180 connect_process_winstation( process, current );
1182 /* set the process console */
1183 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1185 /* FIXME: some better error checking should be done...
1186 * like if hConOut and hConIn are console handles, then they should be on the same
1187 * physical console
1189 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1192 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1194 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1195 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1196 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1197 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1198 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1199 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1200 /* some handles above may have been invalid; this is not an error */
1201 if (get_error() == STATUS_INVALID_HANDLE ||
1202 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1205 /* attach to the debugger if requested */
1206 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1208 set_process_debugger( process, current );
1209 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1211 else if (parent->debugger && parent->debug_children)
1213 set_process_debugger( process, parent->debugger );
1214 /* debug_children is set to 1 by default */
1217 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1218 process->group_id = parent->group_id;
1220 info->process = (struct process *)grab_object( process );
1221 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1222 reply->pid = get_process_id( process );
1223 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1225 done:
1226 if (process) release_object( process );
1227 release_object( info );
1230 /* execute a new process, replacing the existing one */
1231 DECL_HANDLER(exec_process)
1233 struct process *process;
1234 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1236 if (socket_fd == -1)
1238 set_error( STATUS_INVALID_PARAMETER );
1239 return;
1241 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1243 set_error( STATUS_INVALID_HANDLE );
1244 close( socket_fd );
1245 return;
1247 if (shutdown_stage)
1249 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1250 close( socket_fd );
1251 return;
1253 if (!is_cpu_supported( req->cpu ))
1255 close( socket_fd );
1256 return;
1258 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1259 create_thread( -1, process, NULL );
1260 release_object( process );
1263 /* Retrieve information about a newly started process */
1264 DECL_HANDLER(get_new_process_info)
1266 struct startup_info *info;
1268 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1269 0, &startup_info_ops )))
1271 reply->success = is_process_init_done( info->process );
1272 reply->exit_code = info->process->exit_code;
1273 release_object( info );
1277 /* Retrieve the new process startup info */
1278 DECL_HANDLER(get_startup_info)
1280 struct process *process = current->process;
1281 struct startup_info *info = process->startup_info;
1282 data_size_t size;
1284 if (!info) return;
1286 /* we return the data directly without making a copy so this can only be called once */
1287 reply->info_size = info->info_size;
1288 size = info->data_size;
1289 if (size > get_reply_max_size()) size = get_reply_max_size();
1290 set_reply_data_ptr( info->data, size );
1291 info->data = NULL;
1292 info->data_size = 0;
1295 /* signal the end of the process initialization */
1296 DECL_HANDLER(init_process_done)
1298 struct process_dll *dll;
1299 struct process *process = current->process;
1301 if (is_process_init_done(process))
1303 set_error( STATUS_INVALID_PARAMETER );
1304 return;
1306 if (!(dll = find_process_dll( process, req->module )))
1308 set_error( STATUS_DLL_NOT_FOUND );
1309 return;
1312 /* main exe is the first in the dll list */
1313 list_remove( &dll->entry );
1314 list_add_head( &process->dlls, &dll->entry );
1316 process->ldt_copy = req->ldt_copy;
1317 process->start_time = current_time;
1318 current->entry_point = req->entry;
1319 if (process->exe_file) release_object( process->exe_file );
1320 process->exe_file = NULL;
1322 generate_startup_debug_events( process, req->entry );
1323 set_process_startup_state( process, STARTUP_DONE );
1325 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1326 if (process->debugger) set_process_debug_flag( process, 1 );
1327 reply->suspend = (current->suspend || process->suspend);
1330 /* open a handle to a process */
1331 DECL_HANDLER(open_process)
1333 struct process *process = get_process_from_id( req->pid );
1334 reply->handle = 0;
1335 if (process)
1337 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1338 release_object( process );
1342 /* terminate a process */
1343 DECL_HANDLER(terminate_process)
1345 struct process *process;
1347 if (req->handle)
1349 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1350 if (!process) return;
1352 else process = (struct process *)grab_object( current->process );
1354 reply->self = (current->process == process);
1355 terminate_process( process, current, req->exit_code );
1356 release_object( process );
1359 /* fetch information about a process */
1360 DECL_HANDLER(get_process_info)
1362 struct process *process;
1364 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1366 reply->pid = get_process_id( process );
1367 reply->ppid = process->parent_id;
1368 reply->exit_code = process->exit_code;
1369 reply->priority = process->priority;
1370 reply->affinity = process->affinity;
1371 reply->peb = process->peb;
1372 reply->start_time = process->start_time;
1373 reply->end_time = process->end_time;
1374 reply->cpu = process->cpu;
1375 reply->debugger_present = !!process->debugger;
1376 reply->debug_children = process->debug_children;
1377 release_object( process );
1381 /* retrieve information about a process memory usage */
1382 DECL_HANDLER(get_process_vm_counters)
1384 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1386 if (!process) return;
1387 #ifdef linux
1388 if (process->unix_pid != -1)
1390 FILE *f;
1391 char proc_path[32], line[256];
1392 unsigned long value;
1394 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1395 if ((f = fopen( proc_path, "r" )))
1397 while (fgets( line, sizeof(line), f ))
1399 if (sscanf( line, "VmPeak: %lu", &value ))
1400 reply->peak_virtual_size = (mem_size_t)value * 1024;
1401 else if (sscanf( line, "VmSize: %lu", &value ))
1402 reply->virtual_size = (mem_size_t)value * 1024;
1403 else if (sscanf( line, "VmHWM: %lu", &value ))
1404 reply->peak_working_set_size = (mem_size_t)value * 1024;
1405 else if (sscanf( line, "VmRSS: %lu", &value ))
1406 reply->working_set_size = (mem_size_t)value * 1024;
1407 else if (sscanf( line, "RssAnon: %lu", &value ))
1408 reply->pagefile_usage += (mem_size_t)value * 1024;
1409 else if (sscanf( line, "VmSwap: %lu", &value ))
1410 reply->pagefile_usage += (mem_size_t)value * 1024;
1412 reply->peak_pagefile_usage = reply->pagefile_usage;
1413 fclose( f );
1415 else set_error( STATUS_ACCESS_DENIED );
1417 else set_error( STATUS_ACCESS_DENIED );
1418 #endif
1419 release_object( process );
1422 static void set_process_affinity( struct process *process, affinity_t affinity )
1424 struct thread *thread;
1426 if (!process->running_threads)
1428 set_error( STATUS_PROCESS_IS_TERMINATING );
1429 return;
1432 process->affinity = affinity;
1434 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1436 set_thread_affinity( thread, affinity );
1440 /* set information about a process */
1441 DECL_HANDLER(set_process_info)
1443 struct process *process;
1445 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1447 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1448 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1449 release_object( process );
1453 /* read data from a process address space */
1454 DECL_HANDLER(read_process_memory)
1456 struct process *process;
1457 data_size_t len = get_reply_max_size();
1459 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1461 if (len)
1463 char *buffer = mem_alloc( len );
1464 if (buffer)
1466 if (read_process_memory( process, req->addr, len, buffer ))
1467 set_reply_data_ptr( buffer, len );
1468 else
1469 free( buffer );
1472 release_object( process );
1475 /* write data to a process address space */
1476 DECL_HANDLER(write_process_memory)
1478 struct process *process;
1480 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1482 data_size_t len = get_req_data_size();
1483 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1484 else set_error( STATUS_INVALID_PARAMETER );
1485 release_object( process );
1489 /* notify the server that a dll has been loaded */
1490 DECL_HANDLER(load_dll)
1492 struct process_dll *dll;
1494 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1496 dll->dbg_offset = req->dbg_offset;
1497 dll->dbg_size = req->dbg_size;
1498 dll->name = req->name;
1499 /* only generate event if initialization is done */
1500 if (is_process_init_done( current->process ))
1501 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1505 /* notify the server that a dll is being unloaded */
1506 DECL_HANDLER(unload_dll)
1508 process_unload_dll( current->process, req->base );
1511 /* retrieve information about a module in a process */
1512 DECL_HANDLER(get_dll_info)
1514 struct process *process;
1516 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1518 struct process_dll *dll;
1520 if (req->base_address)
1521 dll = find_process_dll( process, req->base_address );
1522 else /* NULL means main module */
1523 dll = list_head( &process->dlls ) ?
1524 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1526 if (dll)
1528 reply->entry_point = 0; /* FIXME */
1529 reply->filename_len = dll->namelen;
1530 if (dll->filename)
1532 if (dll->namelen <= get_reply_max_size())
1533 set_reply_data( dll->filename, dll->namelen );
1534 else
1535 set_error( STATUS_BUFFER_TOO_SMALL );
1538 else
1539 set_error( STATUS_DLL_NOT_FOUND );
1541 release_object( process );
1545 /* retrieve the process idle event */
1546 DECL_HANDLER(get_process_idle_event)
1548 struct process *process;
1550 reply->event = 0;
1551 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1553 if (process->idle_event && process != current->process)
1554 reply->event = alloc_handle( current->process, process->idle_event,
1555 EVENT_ALL_ACCESS, 0 );
1556 release_object( process );
1560 /* make the current process a system process */
1561 DECL_HANDLER(make_process_system)
1563 struct process *process = current->process;
1565 if (!shutdown_event)
1567 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1568 make_object_static( (struct object *)shutdown_event );
1571 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1572 return;
1574 if (!process->is_system)
1576 process->is_system = 1;
1577 close_process_desktop( process );
1578 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1579 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1583 /* create a new job object */
1584 DECL_HANDLER(create_job)
1586 struct job *job;
1587 struct unicode_str name;
1588 struct object *root;
1589 const struct security_descriptor *sd;
1590 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1592 if (!objattr) return;
1594 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1596 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1597 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1598 else
1599 reply->handle = alloc_handle_no_access_check( current->process, job,
1600 req->access, objattr->attributes );
1601 release_object( job );
1603 if (root) release_object( root );
1606 /* open a job object */
1607 DECL_HANDLER(open_job)
1609 struct unicode_str name = get_req_unicode_str();
1611 reply->handle = open_object( current->process, req->rootdir, req->access,
1612 &job_ops, &name, req->attributes );
1615 /* assign a job object to a process */
1616 DECL_HANDLER(assign_job)
1618 struct process *process;
1619 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1621 if (!job) return;
1623 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1625 if (!process->running_threads)
1626 set_error( STATUS_PROCESS_IS_TERMINATING );
1627 else if (process->job)
1628 set_error( STATUS_ACCESS_DENIED );
1629 else
1630 add_job_process( job, process );
1631 release_object( process );
1633 release_object( job );
1637 /* check if a process is associated with a job */
1638 DECL_HANDLER(process_in_job)
1640 struct process *process;
1641 struct job *job;
1643 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1644 return;
1646 if (!req->job)
1648 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1650 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1652 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1653 release_object( job );
1655 release_object( process );
1658 /* terminate all processes associated with the job */
1659 DECL_HANDLER(terminate_job)
1661 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1663 if (!job) return;
1665 terminate_job( job, req->status );
1666 release_object( job );
1669 /* update limits of the job object */
1670 DECL_HANDLER(set_job_limits)
1672 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1674 if (!job) return;
1676 job->limit_flags = req->limit_flags;
1677 release_object( job );
1680 /* set the jobs completion port */
1681 DECL_HANDLER(set_job_completion_port)
1683 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1685 if (!job) return;
1687 if (!job->completion_port)
1689 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1690 job->completion_key = req->key;
1692 else
1693 set_error( STATUS_INVALID_PARAMETER );
1695 release_object( job );