msi/tests: Introduce a variadic check_record() helper.
[wine.git] / server / process.c
blobe55c8a861e03d50fbf463cf8a8943997ee37932b
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 if (!info) return 0;
591 return info->data_size;
594 /* destroy a process when its refcount is 0 */
595 static void process_destroy( struct object *obj )
597 struct process *process = (struct process *)obj;
598 assert( obj->ops == &process_ops );
600 /* we can't have a thread remaining */
601 assert( list_empty( &process->thread_list ));
602 assert( list_empty( &process->asyncs ));
604 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
606 close_process_handles( process );
607 set_process_startup_state( process, STARTUP_ABORTED );
609 if (process->job)
611 list_remove( &process->job_entry );
612 release_object( process->job );
614 if (process->console) release_object( process->console );
615 if (process->msg_fd) release_object( process->msg_fd );
616 list_remove( &process->entry );
617 if (process->idle_event) release_object( process->idle_event );
618 if (process->exe_file) release_object( process->exe_file );
619 if (process->id) free_ptid( process->id );
620 if (process->token) release_object( process->token );
621 free( process->dir_cache );
624 /* dump a process on stdout for debugging purposes */
625 static void process_dump( struct object *obj, int verbose )
627 struct process *process = (struct process *)obj;
628 assert( obj->ops == &process_ops );
630 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
633 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
635 struct process *process = (struct process *)obj;
636 return !process->running_threads;
639 static unsigned int process_map_access( struct object *obj, unsigned int access )
641 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
642 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
643 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
644 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
645 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
647 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
648 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
650 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
653 static void process_poll_event( struct fd *fd, int event )
655 struct process *process = get_fd_user( fd );
656 assert( process->obj.ops == &process_ops );
658 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
659 else if (event & POLLIN) receive_fd( process );
662 static void startup_info_destroy( struct object *obj )
664 struct startup_info *info = (struct startup_info *)obj;
665 assert( obj->ops == &startup_info_ops );
666 free( info->data );
667 if (info->process) release_object( info->process );
670 static void startup_info_dump( struct object *obj, int verbose )
672 struct startup_info *info = (struct startup_info *)obj;
673 assert( obj->ops == &startup_info_ops );
675 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
676 info->data->hstdin, info->data->hstdout, info->data->hstderr );
679 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
681 struct startup_info *info = (struct startup_info *)obj;
682 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
685 /* get a process from an id (and increment the refcount) */
686 struct process *get_process_from_id( process_id_t id )
688 struct object *obj = get_ptid_entry( id );
690 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
691 set_error( STATUS_INVALID_PARAMETER );
692 return NULL;
695 /* get a process from a handle (and increment the refcount) */
696 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
698 return (struct process *)get_handle_obj( current->process, handle,
699 access, &process_ops );
702 /* find a dll from its base address */
703 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
705 struct process_dll *dll;
707 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
709 if (dll->base == base) return dll;
711 return NULL;
714 /* add a dll to a process list */
715 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
716 const WCHAR *filename, data_size_t name_len )
718 struct process_dll *dll;
720 /* make sure we don't already have one with the same base address */
721 if (find_process_dll( process, base ))
723 set_error( STATUS_INVALID_PARAMETER );
724 return NULL;
727 if ((dll = mem_alloc( sizeof(*dll) )))
729 dll->base = base;
730 dll->filename = NULL;
731 dll->namelen = name_len;
732 if (name_len && !(dll->filename = memdup( filename, name_len )))
734 free( dll );
735 return NULL;
737 list_add_tail( &process->dlls, &dll->entry );
739 return dll;
742 /* remove a dll from a process list */
743 static void process_unload_dll( struct process *process, mod_handle_t base )
745 struct process_dll *dll = find_process_dll( process, base );
747 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
749 free( dll->filename );
750 list_remove( &dll->entry );
751 free( dll );
752 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
754 else set_error( STATUS_INVALID_PARAMETER );
757 /* terminate a process with the given exit code */
758 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
760 struct thread *thread;
762 grab_object( process ); /* make sure it doesn't get freed when threads die */
763 process->is_terminating = 1;
765 restart:
766 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
768 if (exit_code) thread->exit_code = exit_code;
769 if (thread == skip) continue;
770 if (thread->state == TERMINATED) continue;
771 kill_thread( thread, 1 );
772 goto restart;
774 release_object( process );
777 /* kill all processes */
778 static void kill_all_processes(void)
780 for (;;)
782 struct process *process;
784 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
786 if (process->running_threads) break;
788 if (&process->entry == &process_list) break; /* no process found */
789 terminate_process( process, NULL, 1 );
793 /* kill all processes being attached to a console renderer */
794 void kill_console_processes( struct thread *renderer, int exit_code )
796 for (;;) /* restart from the beginning of the list every time */
798 struct process *process;
800 /* find the first process being attached to 'renderer' and still running */
801 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
803 if (process == renderer->process) continue;
804 if (!process->running_threads) continue;
805 if (process->console && console_get_renderer( process->console ) == renderer) break;
807 if (&process->entry == &process_list) break; /* no process found */
808 terminate_process( process, NULL, exit_code );
812 /* a process has been killed (i.e. its last thread died) */
813 static void process_killed( struct process *process )
815 struct list *ptr;
817 assert( list_empty( &process->thread_list ));
818 process->end_time = current_time;
819 if (!process->is_system) close_process_desktop( process );
820 process->winstation = 0;
821 process->desktop = 0;
822 close_process_handles( process );
823 cancel_process_asyncs( process );
824 if (process->idle_event) release_object( process->idle_event );
825 if (process->exe_file) release_object( process->exe_file );
826 process->idle_event = NULL;
827 process->exe_file = NULL;
829 /* close the console attached to this process, if any */
830 free_console( process );
832 while ((ptr = list_head( &process->rawinput_devices )))
834 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
835 list_remove( &entry->entry );
836 free( entry );
838 while ((ptr = list_head( &process->dlls )))
840 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
841 free( dll->filename );
842 list_remove( &dll->entry );
843 free( dll );
845 destroy_process_classes( process );
846 free_mapped_views( process );
847 free_process_user_handles( process );
848 remove_process_locks( process );
849 set_process_startup_state( process, STARTUP_ABORTED );
850 finish_process_tracing( process );
851 release_job_process( process );
852 start_sigkill_timer( process );
853 wake_up( &process->obj, 0 );
856 /* add a thread to a process running threads list */
857 void add_process_thread( struct process *process, struct thread *thread )
859 list_add_tail( &process->thread_list, &thread->proc_entry );
860 if (!process->running_threads++)
862 running_processes++;
863 if (!process->is_system)
865 if (!user_processes++ && shutdown_timeout)
867 remove_timeout_user( shutdown_timeout );
868 shutdown_timeout = NULL;
872 grab_object( thread );
875 /* remove a thread from a process running threads list */
876 void remove_process_thread( struct process *process, struct thread *thread )
878 assert( process->running_threads > 0 );
879 assert( !list_empty( &process->thread_list ));
881 list_remove( &thread->proc_entry );
883 if (!--process->running_threads)
885 /* we have removed the last running thread, exit the process */
886 process->exit_code = thread->exit_code;
887 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
888 process_killed( process );
890 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
891 release_object( thread );
894 /* suspend all the threads of a process */
895 void suspend_process( struct process *process )
897 if (!process->suspend++)
899 struct list *ptr, *next;
901 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
903 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
904 if (!thread->suspend) stop_thread( thread );
909 /* resume all the threads of a process */
910 void resume_process( struct process *process )
912 assert (process->suspend > 0);
913 if (!--process->suspend)
915 struct list *ptr, *next;
917 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
919 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
920 if (!thread->suspend) wake_thread( thread );
925 /* kill a process on the spot */
926 void kill_process( struct process *process, int violent_death )
928 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
930 release_object( process->msg_fd );
931 process->msg_fd = NULL;
934 if (process->sigkill_timeout) /* already waiting for it to die */
936 remove_timeout_user( process->sigkill_timeout );
937 process->sigkill_timeout = NULL;
938 process_died( process );
939 return;
942 if (violent_death) terminate_process( process, NULL, 1 );
943 else
945 struct list *ptr;
947 grab_object( process ); /* make sure it doesn't get freed when threads die */
948 while ((ptr = list_head( &process->thread_list )))
950 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
951 kill_thread( thread, 0 );
953 release_object( process );
957 /* kill all processes being debugged by a given thread */
958 void kill_debugged_processes( struct thread *debugger, int exit_code )
960 for (;;) /* restart from the beginning of the list every time */
962 struct process *process;
964 /* find the first process being debugged by 'debugger' and still running */
965 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
967 if (!process->running_threads) continue;
968 if (process->debugger == debugger) break;
970 if (&process->entry == &process_list) break; /* no process found */
971 process->debugger = NULL;
972 terminate_process( process, NULL, exit_code );
977 /* trigger a breakpoint event in a given process */
978 void break_process( struct process *process )
980 struct thread *thread;
982 suspend_process( process );
984 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
986 if (thread->context) /* inside an exception event already */
988 break_thread( thread );
989 goto done;
992 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
993 else set_error( STATUS_ACCESS_DENIED );
994 done:
995 resume_process( process );
999 /* detach a debugger from all its debuggees */
1000 void detach_debugged_processes( struct thread *debugger )
1002 struct process *process;
1004 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1006 if (process->debugger == debugger && process->running_threads)
1008 debugger_detach( process, debugger );
1014 void enum_processes( int (*cb)(struct process*, void*), void *user )
1016 struct list *ptr, *next;
1018 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1020 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1021 if ((cb)(process, user)) break;
1025 /* set the debugged flag in the process PEB */
1026 int set_process_debug_flag( struct process *process, int flag )
1028 char data = (flag != 0);
1030 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1031 return write_process_memory( process, process->peb + 2, 1, &data );
1034 /* take a snapshot of currently running processes */
1035 struct process_snapshot *process_snap( int *count )
1037 struct process_snapshot *snapshot, *ptr;
1038 struct process *process;
1040 if (!running_processes) return NULL;
1041 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1042 return NULL;
1043 ptr = snapshot;
1044 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1046 if (!process->running_threads) continue;
1047 ptr->process = process;
1048 ptr->threads = process->running_threads;
1049 ptr->count = process->obj.refcount;
1050 ptr->priority = process->priority;
1051 ptr->handles = get_handle_table_count(process);
1052 grab_object( process );
1053 ptr++;
1056 if (!(*count = ptr - snapshot))
1058 free( snapshot );
1059 snapshot = NULL;
1061 return snapshot;
1064 /* create a new process */
1065 DECL_HANDLER(new_process)
1067 struct startup_info *info;
1068 const void *info_ptr;
1069 struct unicode_str name;
1070 const struct security_descriptor *sd;
1071 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1072 struct process *process = NULL;
1073 struct process *parent = current->process;
1074 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1076 if (socket_fd == -1)
1078 set_error( STATUS_INVALID_PARAMETER );
1079 return;
1081 if (!objattr)
1083 set_error( STATUS_INVALID_PARAMETER );
1084 close( socket_fd );
1085 return;
1087 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1089 set_error( STATUS_INVALID_HANDLE );
1090 close( socket_fd );
1091 return;
1093 if (shutdown_stage)
1095 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1096 close( socket_fd );
1097 return;
1099 if (!is_cpu_supported( req->cpu ))
1101 close( socket_fd );
1102 return;
1105 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1106 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1108 set_error( STATUS_ACCESS_DENIED );
1109 close( socket_fd );
1110 return;
1113 /* build the startup info for a new process */
1114 if (!(info = alloc_object( &startup_info_ops )))
1116 close( socket_fd );
1117 return;
1119 info->process = NULL;
1120 info->data = NULL;
1122 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1123 info->info_size = min( req->info_size, info->data_size );
1125 if (req->info_size < sizeof(*info->data))
1127 /* make sure we have a full startup_info_t structure */
1128 data_size_t env_size = info->data_size - info->info_size;
1129 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1131 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1133 close( socket_fd );
1134 goto done;
1136 memcpy( info->data, info_ptr, info_size );
1137 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1138 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1139 info->info_size = sizeof(startup_info_t);
1140 info->data_size = info->info_size + env_size;
1142 else
1144 data_size_t pos = sizeof(*info->data);
1146 if (!(info->data = memdup( info_ptr, info->data_size )))
1148 close( socket_fd );
1149 goto done;
1151 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1152 FIXUP_LEN( info->data->curdir_len );
1153 FIXUP_LEN( info->data->dllpath_len );
1154 FIXUP_LEN( info->data->imagepath_len );
1155 FIXUP_LEN( info->data->cmdline_len );
1156 FIXUP_LEN( info->data->title_len );
1157 FIXUP_LEN( info->data->desktop_len );
1158 FIXUP_LEN( info->data->shellinfo_len );
1159 FIXUP_LEN( info->data->runtime_len );
1160 #undef FIXUP_LEN
1163 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1165 process->startup_info = (struct startup_info *)grab_object( info );
1167 if (req->exe_file &&
1168 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1169 goto done;
1171 if (parent->job
1172 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1173 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1175 add_job_process( parent->job, process );
1178 /* connect to the window station */
1179 connect_process_winstation( process, current );
1181 /* set the process console */
1182 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1184 /* FIXME: some better error checking should be done...
1185 * like if hConOut and hConIn are console handles, then they should be on the same
1186 * physical console
1188 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1191 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1193 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1194 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1195 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1196 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1197 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1198 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1199 /* some handles above may have been invalid; this is not an error */
1200 if (get_error() == STATUS_INVALID_HANDLE ||
1201 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1204 /* attach to the debugger if requested */
1205 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1207 set_process_debugger( process, current );
1208 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1210 else if (parent->debugger && parent->debug_children)
1212 set_process_debugger( process, parent->debugger );
1213 /* debug_children is set to 1 by default */
1216 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1217 process->group_id = parent->group_id;
1219 info->process = (struct process *)grab_object( process );
1220 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1221 reply->pid = get_process_id( process );
1222 reply->handle = alloc_handle_no_access_check( parent, process, req->access, objattr->attributes );
1224 done:
1225 if (process) release_object( process );
1226 release_object( info );
1229 /* execute a new process, replacing the existing one */
1230 DECL_HANDLER(exec_process)
1232 struct process *process;
1233 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1235 if (socket_fd == -1)
1237 set_error( STATUS_INVALID_PARAMETER );
1238 return;
1240 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1242 set_error( STATUS_INVALID_HANDLE );
1243 close( socket_fd );
1244 return;
1246 if (shutdown_stage)
1248 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1249 close( socket_fd );
1250 return;
1252 if (!is_cpu_supported( req->cpu ))
1254 close( socket_fd );
1255 return;
1257 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1258 create_thread( -1, process, NULL );
1259 release_object( process );
1262 /* Retrieve information about a newly started process */
1263 DECL_HANDLER(get_new_process_info)
1265 struct startup_info *info;
1267 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1268 0, &startup_info_ops )))
1270 reply->success = is_process_init_done( info->process );
1271 reply->exit_code = info->process->exit_code;
1272 release_object( info );
1276 /* Retrieve the new process startup info */
1277 DECL_HANDLER(get_startup_info)
1279 struct process *process = current->process;
1280 struct startup_info *info = process->startup_info;
1281 data_size_t size;
1283 if (!info) return;
1285 /* we return the data directly without making a copy so this can only be called once */
1286 reply->info_size = info->info_size;
1287 size = info->data_size;
1288 if (size > get_reply_max_size()) size = get_reply_max_size();
1289 set_reply_data_ptr( info->data, size );
1290 info->data = NULL;
1291 info->data_size = 0;
1294 /* signal the end of the process initialization */
1295 DECL_HANDLER(init_process_done)
1297 struct process_dll *dll;
1298 struct process *process = current->process;
1300 if (is_process_init_done(process))
1302 set_error( STATUS_INVALID_PARAMETER );
1303 return;
1305 if (!(dll = find_process_dll( process, req->module )))
1307 set_error( STATUS_DLL_NOT_FOUND );
1308 return;
1311 /* main exe is the first in the dll list */
1312 list_remove( &dll->entry );
1313 list_add_head( &process->dlls, &dll->entry );
1315 process->ldt_copy = req->ldt_copy;
1316 process->start_time = current_time;
1317 current->entry_point = req->entry;
1318 if (process->exe_file) release_object( process->exe_file );
1319 process->exe_file = NULL;
1321 init_process_tracing( process );
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 );