d3d11/tests: Add basic test for quad tessellation.
[wine.git] / server / process.c
blob5eabbbef8ef7095bd68085173c0ba7d4f85d057c
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #include <unistd.h>
36 #ifdef HAVE_POLL_H
37 #include <poll.h>
38 #endif
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "winternl.h"
44 #include "file.h"
45 #include "handle.h"
46 #include "process.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "user.h"
50 #include "security.h"
52 /* process structure */
54 static struct list process_list = LIST_INIT(process_list);
55 static int running_processes, user_processes;
56 static struct event *shutdown_event; /* signaled when shutdown starts */
57 static struct timeout_user *shutdown_timeout; /* timeout for server shutdown */
58 static int shutdown_stage; /* current stage in the shutdown process */
60 /* process operations */
62 static void process_dump( struct object *obj, int verbose );
63 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
64 static unsigned int process_map_access( struct object *obj, unsigned int access );
65 static void process_poll_event( struct fd *fd, int event );
66 static void process_destroy( struct object *obj );
67 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
69 static const struct object_ops process_ops =
71 sizeof(struct process), /* size */
72 process_dump, /* dump */
73 no_get_type, /* get_type */
74 add_queue, /* add_queue */
75 remove_queue, /* remove_queue */
76 process_signaled, /* signaled */
77 no_satisfied, /* satisfied */
78 no_signal, /* signal */
79 no_get_fd, /* get_fd */
80 process_map_access, /* map_access */
81 default_get_sd, /* get_sd */
82 default_set_sd, /* set_sd */
83 no_lookup_name, /* lookup_name */
84 no_link_name, /* link_name */
85 NULL, /* unlink_name */
86 no_open_file, /* open_file */
87 no_close_handle, /* close_handle */
88 process_destroy /* destroy */
91 static const struct fd_ops process_fd_ops =
93 NULL, /* get_poll_events */
94 process_poll_event, /* poll_event */
95 NULL, /* flush */
96 NULL, /* get_fd_type */
97 NULL, /* ioctl */
98 NULL, /* queue_async */
99 NULL, /* reselect_async */
100 NULL /* cancel async */
103 /* process startup info */
105 struct startup_info
107 struct object obj; /* object header */
108 struct file *exe_file; /* file handle for main exe */
109 struct process *process; /* created process */
110 data_size_t info_size; /* size of startup info */
111 data_size_t data_size; /* size of whole startup data */
112 startup_info_t *data; /* data for startup info */
115 static void startup_info_dump( struct object *obj, int verbose );
116 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
117 static void startup_info_destroy( struct object *obj );
119 static const struct object_ops startup_info_ops =
121 sizeof(struct startup_info), /* size */
122 startup_info_dump, /* dump */
123 no_get_type, /* get_type */
124 add_queue, /* add_queue */
125 remove_queue, /* remove_queue */
126 startup_info_signaled, /* signaled */
127 no_satisfied, /* satisfied */
128 no_signal, /* signal */
129 no_get_fd, /* get_fd */
130 no_map_access, /* map_access */
131 default_get_sd, /* get_sd */
132 default_set_sd, /* set_sd */
133 no_lookup_name, /* lookup_name */
134 no_link_name, /* link_name */
135 NULL, /* unlink_name */
136 no_open_file, /* open_file */
137 no_close_handle, /* close_handle */
138 startup_info_destroy /* destroy */
141 /* job object */
143 static void job_dump( struct object *obj, int verbose );
144 static struct object_type *job_get_type( struct object *obj );
145 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
146 static unsigned int job_map_access( struct object *obj, unsigned int access );
147 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
148 static void job_destroy( struct object *obj );
150 struct job
152 struct object obj; /* object header */
153 struct list process_list; /* list of all processes */
154 int num_processes; /* count of running processes */
155 unsigned int limit_flags; /* limit flags */
156 int terminating; /* job is terminating */
157 int signaled; /* job is signaled */
158 struct completion *completion_port; /* associated completion port */
159 apc_param_t completion_key; /* key to send with completion messages */
162 static const struct object_ops job_ops =
164 sizeof(struct job), /* size */
165 job_dump, /* dump */
166 job_get_type, /* get_type */
167 add_queue, /* add_queue */
168 remove_queue, /* remove_queue */
169 job_signaled, /* signaled */
170 no_satisfied, /* satisfied */
171 no_signal, /* signal */
172 no_get_fd, /* get_fd */
173 job_map_access, /* map_access */
174 default_get_sd, /* get_sd */
175 default_set_sd, /* set_sd */
176 no_lookup_name, /* lookup_name */
177 directory_link_name, /* link_name */
178 default_unlink_name, /* unlink_name */
179 no_open_file, /* open_file */
180 job_close_handle, /* close_handle */
181 job_destroy /* destroy */
184 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
185 unsigned int attr, const struct security_descriptor *sd )
187 struct job *job;
189 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
191 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
193 /* initialize it if it didn't already exist */
194 list_init( &job->process_list );
195 job->num_processes = 0;
196 job->limit_flags = 0;
197 job->terminating = 0;
198 job->signaled = 0;
199 job->completion_port = NULL;
200 job->completion_key = 0;
203 return job;
206 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
208 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
211 static struct object_type *job_get_type( struct object *obj )
213 static const WCHAR name[] = {'J','o','b'};
214 static const struct unicode_str str = { name, sizeof(name) };
215 return get_object_type( &str );
218 static unsigned int job_map_access( struct object *obj, unsigned int access )
220 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
221 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
222 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
223 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
224 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
227 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
229 if (job->completion_port)
230 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
233 static void add_job_process( struct job *job, struct process *process )
235 if (!process->running_threads)
237 set_error( STATUS_PROCESS_IS_TERMINATING );
238 return;
240 if (process->job)
242 set_error( STATUS_ACCESS_DENIED );
243 return;
245 process->job = (struct job *)grab_object( job );
246 list_add_tail( &job->process_list, &process->job_entry );
247 job->num_processes++;
249 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
252 /* called when a process has terminated, allow one additional process */
253 static void release_job_process( struct process *process )
255 struct job *job = process->job;
257 if (!job) return;
259 assert( job->num_processes );
260 job->num_processes--;
262 if (!job->terminating)
263 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
265 if (!job->num_processes)
266 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
269 static void terminate_job( struct job *job, int exit_code )
271 /* don't report completion events for terminated processes */
272 job->terminating = 1;
274 for (;;) /* restart from the beginning of the list every time */
276 struct process *process;
278 /* find the first process associated with this job and still running */
279 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
281 if (process->running_threads) break;
283 if (&process->job_entry == &job->process_list) break; /* no process found */
284 assert( process->job == job );
285 terminate_process( process, NULL, exit_code );
288 job->terminating = 0;
289 job->signaled = 1;
290 wake_up( &job->obj, 0 );
293 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
295 struct job *job = (struct job *)obj;
296 assert( obj->ops == &job_ops );
298 if (obj->handle_count == 1) /* last handle */
300 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
301 terminate_job( job, 0 );
303 return 1;
306 static void job_destroy( struct object *obj )
308 struct job *job = (struct job *)obj;
309 assert( obj->ops == &job_ops );
311 assert( !job->num_processes );
312 assert( list_empty(&job->process_list) );
314 if (job->completion_port) release_object( job->completion_port );
317 static void job_dump( struct object *obj, int verbose )
319 struct job *job = (struct job *)obj;
320 assert( obj->ops == &job_ops );
321 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
324 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
326 struct job *job = (struct job *)obj;
327 return job->signaled;
330 struct ptid_entry
332 void *ptr; /* entry ptr */
333 unsigned int next; /* next free entry */
336 static struct ptid_entry *ptid_entries; /* array of ptid entries */
337 static unsigned int used_ptid_entries; /* number of entries in use */
338 static unsigned int alloc_ptid_entries; /* number of allocated entries */
339 static unsigned int next_free_ptid; /* next free entry */
340 static unsigned int last_free_ptid; /* last free entry */
341 static unsigned int num_free_ptids; /* number of free ptids */
343 static void kill_all_processes(void);
345 #define PTID_OFFSET 8 /* offset for first ptid value */
347 /* allocate a new process or thread id */
348 unsigned int alloc_ptid( void *ptr )
350 struct ptid_entry *entry;
351 unsigned int id;
353 if (used_ptid_entries < alloc_ptid_entries)
355 id = used_ptid_entries + PTID_OFFSET;
356 entry = &ptid_entries[used_ptid_entries++];
358 else if (next_free_ptid && num_free_ptids >= 256)
360 id = next_free_ptid;
361 entry = &ptid_entries[id - PTID_OFFSET];
362 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
363 num_free_ptids--;
365 else /* need to grow the array */
367 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
368 if (!count) count = 512;
369 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
371 set_error( STATUS_NO_MEMORY );
372 return 0;
374 ptid_entries = entry;
375 alloc_ptid_entries = count;
376 id = used_ptid_entries + PTID_OFFSET;
377 entry = &ptid_entries[used_ptid_entries++];
380 entry->ptr = ptr;
381 return id;
384 /* free a process or thread id */
385 void free_ptid( unsigned int id )
387 struct ptid_entry *entry = &ptid_entries[id - PTID_OFFSET];
389 entry->ptr = NULL;
390 entry->next = 0;
392 /* append to end of free list so that we don't reuse it too early */
393 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = id;
394 else next_free_ptid = id;
395 last_free_ptid = id;
396 num_free_ptids++;
399 /* retrieve the pointer corresponding to a process or thread id */
400 void *get_ptid_entry( unsigned int id )
402 if (id < PTID_OFFSET) return NULL;
403 if (id - PTID_OFFSET >= used_ptid_entries) return NULL;
404 return ptid_entries[id - PTID_OFFSET].ptr;
407 /* return the main thread of the process */
408 struct thread *get_process_first_thread( struct process *process )
410 struct list *ptr = list_head( &process->thread_list );
411 if (!ptr) return NULL;
412 return LIST_ENTRY( ptr, struct thread, proc_entry );
415 /* set the state of the process startup info */
416 static void set_process_startup_state( struct process *process, enum startup_state state )
418 if (process->startup_state == STARTUP_IN_PROGRESS) process->startup_state = state;
419 if (process->startup_info)
421 wake_up( &process->startup_info->obj, 0 );
422 release_object( process->startup_info );
423 process->startup_info = NULL;
427 /* callback for server shutdown */
428 static void server_shutdown_timeout( void *arg )
430 shutdown_timeout = NULL;
431 if (!running_processes)
433 close_master_socket( 0 );
434 return;
436 switch(++shutdown_stage)
438 case 1: /* signal system processes to exit */
439 if (debug_level) fprintf( stderr, "wineserver: shutting down\n" );
440 if (shutdown_event) set_event( shutdown_event );
441 shutdown_timeout = add_timeout_user( 2 * -TICKS_PER_SEC, server_shutdown_timeout, NULL );
442 close_master_socket( 4 * -TICKS_PER_SEC );
443 break;
444 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
445 kill_all_processes();
446 break;
450 /* forced shutdown, used for wineserver -k */
451 void shutdown_master_socket(void)
453 kill_all_processes();
454 shutdown_stage = 2;
455 if (shutdown_timeout)
457 remove_timeout_user( shutdown_timeout );
458 shutdown_timeout = NULL;
460 close_master_socket( 2 * -TICKS_PER_SEC ); /* for SIGKILL timeouts */
463 /* final cleanup once we are sure a process is really dead */
464 static void process_died( struct process *process )
466 if (debug_level) fprintf( stderr, "%04x: *process killed*\n", process->id );
467 if (!process->is_system)
469 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
470 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
472 release_object( process );
473 if (!--running_processes && shutdown_stage) close_master_socket( 0 );
476 /* callback for process sigkill timeout */
477 static void process_sigkill( void *private )
479 struct process *process = private;
481 process->sigkill_timeout = NULL;
482 kill( process->unix_pid, SIGKILL );
483 process_died( process );
486 /* start the sigkill timer for a process upon exit */
487 static void start_sigkill_timer( struct process *process )
489 grab_object( process );
490 if (process->unix_pid != -1 && process->msg_fd)
491 process->sigkill_timeout = add_timeout_user( -TICKS_PER_SEC, process_sigkill, process );
492 else
493 process_died( process );
496 /* create a new process and its main thread */
497 /* if the function fails the fd is closed */
498 struct thread *create_process( int fd, struct thread *parent_thread, int inherit_all )
500 struct process *process;
501 struct thread *thread = NULL;
502 int request_pipe[2];
504 if (!(process = alloc_object( &process_ops )))
506 close( fd );
507 goto error;
509 process->parent_id = 0;
510 process->debugger = NULL;
511 process->handles = NULL;
512 process->msg_fd = NULL;
513 process->sigkill_timeout = NULL;
514 process->unix_pid = -1;
515 process->exit_code = STILL_ACTIVE;
516 process->running_threads = 0;
517 process->priority = PROCESS_PRIOCLASS_NORMAL;
518 process->suspend = 0;
519 process->is_system = 0;
520 process->debug_children = 1;
521 process->is_terminating = 0;
522 process->job = NULL;
523 process->console = NULL;
524 process->startup_state = STARTUP_IN_PROGRESS;
525 process->startup_info = NULL;
526 process->idle_event = NULL;
527 process->peb = 0;
528 process->ldt_copy = 0;
529 process->dir_cache = NULL;
530 process->winstation = 0;
531 process->desktop = 0;
532 process->token = NULL;
533 process->trace_data = 0;
534 process->rawinput_mouse = NULL;
535 process->rawinput_kbd = NULL;
536 list_init( &process->thread_list );
537 list_init( &process->locks );
538 list_init( &process->asyncs );
539 list_init( &process->classes );
540 list_init( &process->dlls );
541 list_init( &process->rawinput_devices );
543 process->end_time = 0;
544 list_add_tail( &process_list, &process->entry );
546 if (!(process->id = process->group_id = alloc_ptid( process )))
548 close( fd );
549 goto error;
551 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
553 /* create the handle table */
554 if (!parent_thread)
556 process->handles = alloc_handle_table( process, 0 );
557 process->token = token_create_admin();
558 process->affinity = ~0;
560 else
562 struct process *parent = parent_thread->process;
563 process->parent_id = parent->id;
564 process->handles = inherit_all ? copy_handle_table( process, parent )
565 : alloc_handle_table( process, 0 );
566 /* Note: for security reasons, starting a new process does not attempt
567 * to use the current impersonation token for the new process */
568 process->token = token_duplicate( parent->token, TRUE, 0 );
569 process->affinity = parent->affinity;
571 if (!process->handles || !process->token) goto error;
573 /* create the main thread */
574 if (pipe( request_pipe ) == -1)
576 file_set_error();
577 goto error;
579 if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
581 close( request_pipe[0] );
582 close( request_pipe[1] );
583 goto error;
585 close( request_pipe[1] );
586 if (!(thread = create_thread( request_pipe[0], process ))) goto error;
588 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
589 release_object( process );
590 return thread;
592 error:
593 if (process) release_object( process );
594 /* if we failed to start our first process, close everything down */
595 if (!running_processes) close_master_socket( 0 );
596 return NULL;
599 /* initialize the current process and fill in the request */
600 data_size_t init_process( struct thread *thread )
602 struct process *process = thread->process;
603 struct startup_info *info = process->startup_info;
605 init_process_tracing( process );
606 if (!info) return 0;
607 return info->data_size;
610 /* destroy a process when its refcount is 0 */
611 static void process_destroy( struct object *obj )
613 struct process *process = (struct process *)obj;
614 assert( obj->ops == &process_ops );
616 /* we can't have a thread remaining */
617 assert( list_empty( &process->thread_list ));
618 assert( list_empty( &process->asyncs ));
620 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
622 close_process_handles( process );
623 set_process_startup_state( process, STARTUP_ABORTED );
625 if (process->job)
627 list_remove( &process->job_entry );
628 release_object( process->job );
630 if (process->console) release_object( process->console );
631 if (process->msg_fd) release_object( process->msg_fd );
632 list_remove( &process->entry );
633 if (process->idle_event) release_object( process->idle_event );
634 if (process->id) free_ptid( process->id );
635 if (process->token) release_object( process->token );
636 free( process->dir_cache );
639 /* dump a process on stdout for debugging purposes */
640 static void process_dump( struct object *obj, int verbose )
642 struct process *process = (struct process *)obj;
643 assert( obj->ops == &process_ops );
645 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
648 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
650 struct process *process = (struct process *)obj;
651 return !process->running_threads;
654 static unsigned int process_map_access( struct object *obj, unsigned int access )
656 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
657 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
658 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
659 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
660 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
662 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
663 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
665 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
668 static void process_poll_event( struct fd *fd, int event )
670 struct process *process = get_fd_user( fd );
671 assert( process->obj.ops == &process_ops );
673 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
674 else if (event & POLLIN) receive_fd( process );
677 static void startup_info_destroy( struct object *obj )
679 struct startup_info *info = (struct startup_info *)obj;
680 assert( obj->ops == &startup_info_ops );
681 free( info->data );
682 if (info->exe_file) release_object( info->exe_file );
683 if (info->process) release_object( info->process );
686 static void startup_info_dump( struct object *obj, int verbose )
688 struct startup_info *info = (struct startup_info *)obj;
689 assert( obj->ops == &startup_info_ops );
691 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
692 info->data->hstdin, info->data->hstdout, info->data->hstderr );
695 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
697 struct startup_info *info = (struct startup_info *)obj;
698 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
701 /* get a process from an id (and increment the refcount) */
702 struct process *get_process_from_id( process_id_t id )
704 struct object *obj = get_ptid_entry( id );
706 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
707 set_error( STATUS_INVALID_PARAMETER );
708 return NULL;
711 /* get a process from a handle (and increment the refcount) */
712 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
714 return (struct process *)get_handle_obj( current->process, handle,
715 access, &process_ops );
718 /* find a dll from its base address */
719 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
721 struct process_dll *dll;
723 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
725 if (dll->base == base) return dll;
727 return NULL;
730 /* add a dll to a process list */
731 static struct process_dll *process_load_dll( struct process *process, struct mapping *mapping,
732 mod_handle_t base, const WCHAR *filename,
733 data_size_t name_len )
735 struct process_dll *dll;
737 /* make sure we don't already have one with the same base address */
738 if (find_process_dll( process, base ))
740 set_error( STATUS_INVALID_PARAMETER );
741 return NULL;
744 if ((dll = mem_alloc( sizeof(*dll) )))
746 dll->mapping = NULL;
747 dll->base = base;
748 dll->filename = NULL;
749 dll->namelen = name_len;
750 if (name_len && !(dll->filename = memdup( filename, name_len )))
752 free( dll );
753 return NULL;
755 if (mapping) dll->mapping = grab_mapping_unless_removable( mapping );
756 list_add_tail( &process->dlls, &dll->entry );
758 return dll;
761 /* remove a dll from a process list */
762 static void process_unload_dll( struct process *process, mod_handle_t base )
764 struct process_dll *dll = find_process_dll( process, base );
766 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
768 if (dll->mapping) release_object( dll->mapping );
769 free( dll->filename );
770 list_remove( &dll->entry );
771 free( dll );
772 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
774 else set_error( STATUS_INVALID_PARAMETER );
777 /* terminate a process with the given exit code */
778 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
780 struct thread *thread;
782 grab_object( process ); /* make sure it doesn't get freed when threads die */
783 process->is_terminating = 1;
785 restart:
786 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
788 if (exit_code) thread->exit_code = exit_code;
789 if (thread == skip) continue;
790 if (thread->state == TERMINATED) continue;
791 kill_thread( thread, 1 );
792 goto restart;
794 release_object( process );
797 /* kill all processes */
798 static void kill_all_processes(void)
800 for (;;)
802 struct process *process;
804 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
806 if (process->running_threads) break;
808 if (&process->entry == &process_list) break; /* no process found */
809 terminate_process( process, NULL, 1 );
813 /* kill all processes being attached to a console renderer */
814 void kill_console_processes( struct thread *renderer, int exit_code )
816 for (;;) /* restart from the beginning of the list every time */
818 struct process *process;
820 /* find the first process being attached to 'renderer' and still running */
821 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
823 if (process == renderer->process) continue;
824 if (!process->running_threads) continue;
825 if (process->console && console_get_renderer( process->console ) == renderer) break;
827 if (&process->entry == &process_list) break; /* no process found */
828 terminate_process( process, NULL, exit_code );
832 /* a process has been killed (i.e. its last thread died) */
833 static void process_killed( struct process *process )
835 struct list *ptr;
837 assert( list_empty( &process->thread_list ));
838 process->end_time = current_time;
839 if (!process->is_system) close_process_desktop( process );
840 process->winstation = 0;
841 process->desktop = 0;
842 close_process_handles( process );
843 cancel_process_asyncs( process );
844 if (process->idle_event)
846 release_object( process->idle_event );
847 process->idle_event = NULL;
850 /* close the console attached to this process, if any */
851 free_console( process );
853 while ((ptr = list_head( &process->rawinput_devices )))
855 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
856 list_remove( &entry->entry );
857 free( entry );
859 while ((ptr = list_head( &process->dlls )))
861 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
862 if (dll->mapping) release_object( dll->mapping );
863 free( dll->filename );
864 list_remove( &dll->entry );
865 free( dll );
867 destroy_process_classes( process );
868 free_process_user_handles( process );
869 remove_process_locks( process );
870 set_process_startup_state( process, STARTUP_ABORTED );
871 finish_process_tracing( process );
872 release_job_process( process );
873 start_sigkill_timer( process );
874 wake_up( &process->obj, 0 );
877 /* add a thread to a process running threads list */
878 void add_process_thread( struct process *process, struct thread *thread )
880 list_add_tail( &process->thread_list, &thread->proc_entry );
881 if (!process->running_threads++)
883 running_processes++;
884 if (!process->is_system)
886 if (!user_processes++ && shutdown_timeout)
888 remove_timeout_user( shutdown_timeout );
889 shutdown_timeout = NULL;
893 grab_object( thread );
896 /* remove a thread from a process running threads list */
897 void remove_process_thread( struct process *process, struct thread *thread )
899 assert( process->running_threads > 0 );
900 assert( !list_empty( &process->thread_list ));
902 list_remove( &thread->proc_entry );
904 if (!--process->running_threads)
906 /* we have removed the last running thread, exit the process */
907 process->exit_code = thread->exit_code;
908 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
909 process_killed( process );
911 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
912 release_object( thread );
915 /* suspend all the threads of a process */
916 void suspend_process( struct process *process )
918 if (!process->suspend++)
920 struct list *ptr, *next;
922 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
924 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
925 if (!thread->suspend) stop_thread( thread );
930 /* resume all the threads of a process */
931 void resume_process( struct process *process )
933 assert (process->suspend > 0);
934 if (!--process->suspend)
936 struct list *ptr, *next;
938 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
940 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
941 if (!thread->suspend) wake_thread( thread );
946 /* kill a process on the spot */
947 void kill_process( struct process *process, int violent_death )
949 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
951 release_object( process->msg_fd );
952 process->msg_fd = NULL;
955 if (process->sigkill_timeout) /* already waiting for it to die */
957 remove_timeout_user( process->sigkill_timeout );
958 process->sigkill_timeout = NULL;
959 process_died( process );
960 return;
963 if (violent_death) terminate_process( process, NULL, 1 );
964 else
966 struct list *ptr;
968 grab_object( process ); /* make sure it doesn't get freed when threads die */
969 while ((ptr = list_head( &process->thread_list )))
971 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
972 kill_thread( thread, 0 );
974 release_object( process );
978 /* kill all processes being debugged by a given thread */
979 void kill_debugged_processes( struct thread *debugger, int exit_code )
981 for (;;) /* restart from the beginning of the list every time */
983 struct process *process;
985 /* find the first process being debugged by 'debugger' and still running */
986 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
988 if (!process->running_threads) continue;
989 if (process->debugger == debugger) break;
991 if (&process->entry == &process_list) break; /* no process found */
992 process->debugger = NULL;
993 terminate_process( process, NULL, exit_code );
998 /* trigger a breakpoint event in a given process */
999 void break_process( struct process *process )
1001 struct thread *thread;
1003 suspend_process( process );
1005 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1007 if (thread->context) /* inside an exception event already */
1009 break_thread( thread );
1010 goto done;
1013 if ((thread = get_process_first_thread( process ))) thread->debug_break = 1;
1014 else set_error( STATUS_ACCESS_DENIED );
1015 done:
1016 resume_process( process );
1020 /* detach a debugger from all its debuggees */
1021 void detach_debugged_processes( struct thread *debugger )
1023 struct process *process;
1025 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1027 if (process->debugger == debugger && process->running_threads)
1029 debugger_detach( process, debugger );
1035 void enum_processes( int (*cb)(struct process*, void*), void *user )
1037 struct list *ptr, *next;
1039 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1041 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1042 if ((cb)(process, user)) break;
1046 /* set the debugged flag in the process PEB */
1047 int set_process_debug_flag( struct process *process, int flag )
1049 char data = (flag != 0);
1051 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1052 return write_process_memory( process, process->peb + 2, 1, &data );
1055 /* take a snapshot of currently running processes */
1056 struct process_snapshot *process_snap( int *count )
1058 struct process_snapshot *snapshot, *ptr;
1059 struct process *process;
1061 if (!running_processes) return NULL;
1062 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1063 return NULL;
1064 ptr = snapshot;
1065 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1067 if (!process->running_threads) continue;
1068 ptr->process = process;
1069 ptr->threads = process->running_threads;
1070 ptr->count = process->obj.refcount;
1071 ptr->priority = process->priority;
1072 ptr->handles = get_handle_table_count(process);
1073 grab_object( process );
1074 ptr++;
1077 if (!(*count = ptr - snapshot))
1079 free( snapshot );
1080 snapshot = NULL;
1082 return snapshot;
1085 /* create a new process */
1086 DECL_HANDLER(new_process)
1088 struct startup_info *info;
1089 struct thread *thread;
1090 struct process *process;
1091 struct process *parent = current->process;
1092 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1094 if (socket_fd == -1)
1096 set_error( STATUS_INVALID_PARAMETER );
1097 return;
1099 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1101 set_error( STATUS_INVALID_HANDLE );
1102 close( socket_fd );
1103 return;
1105 if (shutdown_stage)
1107 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1108 close( socket_fd );
1109 return;
1111 if (!is_cpu_supported( req->cpu ))
1113 close( socket_fd );
1114 return;
1117 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1118 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1120 set_error( STATUS_ACCESS_DENIED );
1121 close( socket_fd );
1122 return;
1125 if (!req->info_size) /* create an orphaned process */
1127 create_process( socket_fd, NULL, 0 );
1128 return;
1131 /* build the startup info for a new process */
1132 if (!(info = alloc_object( &startup_info_ops )))
1134 close( socket_fd );
1135 return;
1137 info->exe_file = NULL;
1138 info->process = NULL;
1139 info->data = NULL;
1141 if (req->exe_file &&
1142 !(info->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1144 close( socket_fd );
1145 goto done;
1148 info->data_size = get_req_data_size();
1149 info->info_size = min( req->info_size, info->data_size );
1151 if (req->info_size < sizeof(*info->data))
1153 /* make sure we have a full startup_info_t structure */
1154 data_size_t env_size = info->data_size - info->info_size;
1155 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1157 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1159 close( socket_fd );
1160 goto done;
1162 memcpy( info->data, get_req_data(), info_size );
1163 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1164 memcpy( info->data + 1, (const char *)get_req_data() + req->info_size, env_size );
1165 info->info_size = sizeof(startup_info_t);
1166 info->data_size = info->info_size + env_size;
1168 else
1170 data_size_t pos = sizeof(*info->data);
1172 if (!(info->data = memdup( get_req_data(), info->data_size )))
1174 close( socket_fd );
1175 goto done;
1177 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1178 FIXUP_LEN( info->data->curdir_len );
1179 FIXUP_LEN( info->data->dllpath_len );
1180 FIXUP_LEN( info->data->imagepath_len );
1181 FIXUP_LEN( info->data->cmdline_len );
1182 FIXUP_LEN( info->data->title_len );
1183 FIXUP_LEN( info->data->desktop_len );
1184 FIXUP_LEN( info->data->shellinfo_len );
1185 FIXUP_LEN( info->data->runtime_len );
1186 #undef FIXUP_LEN
1189 if (!(thread = create_process( socket_fd, current, req->inherit_all ))) goto done;
1190 process = thread->process;
1191 process->startup_info = (struct startup_info *)grab_object( info );
1193 if (parent->job
1194 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1195 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1197 add_job_process( parent->job, process );
1200 /* connect to the window station */
1201 connect_process_winstation( process, current );
1203 /* thread will be actually suspended in init_done */
1204 if (req->create_flags & CREATE_SUSPENDED) thread->suspend++;
1206 /* set the process console */
1207 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1209 /* FIXME: some better error checking should be done...
1210 * like if hConOut and hConIn are console handles, then they should be on the same
1211 * physical console
1213 inherit_console( current, process, req->inherit_all ? info->data->hstdin : 0 );
1216 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1218 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1219 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1220 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1221 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1222 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1223 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1224 /* some handles above may have been invalid; this is not an error */
1225 if (get_error() == STATUS_INVALID_HANDLE ||
1226 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1229 /* attach to the debugger if requested */
1230 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1232 set_process_debugger( process, current );
1233 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1235 else if (parent->debugger && parent->debug_children)
1237 set_process_debugger( process, parent->debugger );
1238 /* debug_children is set to 1 by default */
1241 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1242 process->group_id = parent->group_id;
1244 info->process = (struct process *)grab_object( process );
1245 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1246 reply->pid = get_process_id( process );
1247 reply->tid = get_thread_id( thread );
1248 reply->phandle = alloc_handle( parent, process, req->process_access, req->process_attr );
1249 reply->thandle = alloc_handle( parent, thread, req->thread_access, req->thread_attr );
1251 done:
1252 release_object( info );
1255 /* Retrieve information about a newly started process */
1256 DECL_HANDLER(get_new_process_info)
1258 struct startup_info *info;
1260 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1261 0, &startup_info_ops )))
1263 reply->success = is_process_init_done( info->process );
1264 reply->exit_code = info->process->exit_code;
1265 release_object( info );
1269 /* Retrieve the new process startup info */
1270 DECL_HANDLER(get_startup_info)
1272 struct process *process = current->process;
1273 struct startup_info *info = process->startup_info;
1274 data_size_t size;
1276 if (!info) return;
1278 if (info->exe_file &&
1279 !(reply->exe_file = alloc_handle( process, info->exe_file, GENERIC_READ, 0 ))) return;
1281 /* we return the data directly without making a copy so this can only be called once */
1282 reply->info_size = info->info_size;
1283 size = info->data_size;
1284 if (size > get_reply_max_size()) size = get_reply_max_size();
1285 set_reply_data_ptr( info->data, size );
1286 info->data = NULL;
1287 info->data_size = 0;
1290 /* signal the end of the process initialization */
1291 DECL_HANDLER(init_process_done)
1293 struct process_dll *dll;
1294 struct process *process = current->process;
1296 if (is_process_init_done(process))
1298 set_error( STATUS_INVALID_PARAMETER );
1299 return;
1301 if (!(dll = find_process_dll( process, req->module )))
1303 set_error( STATUS_DLL_NOT_FOUND );
1304 return;
1307 /* main exe is the first in the dll list */
1308 list_remove( &dll->entry );
1309 list_add_head( &process->dlls, &dll->entry );
1311 process->ldt_copy = req->ldt_copy;
1312 process->start_time = current_time;
1313 current->entry_point = req->entry;
1315 generate_startup_debug_events( process, req->entry );
1316 set_process_startup_state( process, STARTUP_DONE );
1318 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1319 stop_thread_if_suspended( current );
1320 if (process->debugger) set_process_debug_flag( process, 1 );
1323 /* open a handle to a process */
1324 DECL_HANDLER(open_process)
1326 struct process *process = get_process_from_id( req->pid );
1327 reply->handle = 0;
1328 if (process)
1330 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1331 release_object( process );
1335 /* terminate a process */
1336 DECL_HANDLER(terminate_process)
1338 struct process *process;
1340 if (req->handle)
1342 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1343 if (!process) return;
1345 else process = (struct process *)grab_object( current->process );
1347 reply->self = (current->process == process);
1348 terminate_process( process, current, req->exit_code );
1349 release_object( process );
1352 /* fetch information about a process */
1353 DECL_HANDLER(get_process_info)
1355 struct process *process;
1357 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1359 reply->pid = get_process_id( process );
1360 reply->ppid = process->parent_id;
1361 reply->exit_code = process->exit_code;
1362 reply->priority = process->priority;
1363 reply->affinity = process->affinity;
1364 reply->peb = process->peb;
1365 reply->start_time = process->start_time;
1366 reply->end_time = process->end_time;
1367 reply->cpu = process->cpu;
1368 reply->debugger_present = !!process->debugger;
1369 reply->debug_children = process->debug_children;
1370 release_object( process );
1374 /* retrieve information about a process memory usage */
1375 DECL_HANDLER(get_process_vm_counters)
1377 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1379 if (!process) return;
1380 #ifdef linux
1381 if (process->unix_pid != -1)
1383 FILE *f;
1384 char proc_path[32], line[256];
1385 unsigned long value;
1387 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1388 if ((f = fopen( proc_path, "r" )))
1390 while (fgets( line, sizeof(line), f ))
1392 if (sscanf( line, "VmPeak: %lu", &value ))
1393 reply->peak_virtual_size = (mem_size_t)value * 1024;
1394 else if (sscanf( line, "VmSize: %lu", &value ))
1395 reply->virtual_size = (mem_size_t)value * 1024;
1396 else if (sscanf( line, "VmHWM: %lu", &value ))
1397 reply->peak_working_set_size = (mem_size_t)value * 1024;
1398 else if (sscanf( line, "VmRSS: %lu", &value ))
1399 reply->working_set_size = (mem_size_t)value * 1024;
1400 else if (sscanf( line, "RssAnon: %lu", &value ))
1401 reply->pagefile_usage += (mem_size_t)value * 1024;
1402 else if (sscanf( line, "VmSwap: %lu", &value ))
1403 reply->pagefile_usage += (mem_size_t)value * 1024;
1405 reply->peak_pagefile_usage = reply->pagefile_usage;
1406 fclose( f );
1408 else set_error( STATUS_ACCESS_DENIED );
1410 else set_error( STATUS_ACCESS_DENIED );
1411 #endif
1412 release_object( process );
1415 static void set_process_affinity( struct process *process, affinity_t affinity )
1417 struct thread *thread;
1419 if (!process->running_threads)
1421 set_error( STATUS_PROCESS_IS_TERMINATING );
1422 return;
1425 process->affinity = affinity;
1427 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1429 set_thread_affinity( thread, affinity );
1433 /* set information about a process */
1434 DECL_HANDLER(set_process_info)
1436 struct process *process;
1438 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1440 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1441 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1442 release_object( process );
1446 /* read data from a process address space */
1447 DECL_HANDLER(read_process_memory)
1449 struct process *process;
1450 data_size_t len = get_reply_max_size();
1452 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1454 if (len)
1456 char *buffer = mem_alloc( len );
1457 if (buffer)
1459 if (read_process_memory( process, req->addr, len, buffer ))
1460 set_reply_data_ptr( buffer, len );
1461 else
1462 free( buffer );
1465 release_object( process );
1468 /* write data to a process address space */
1469 DECL_HANDLER(write_process_memory)
1471 struct process *process;
1473 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1475 data_size_t len = get_req_data_size();
1476 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1477 else set_error( STATUS_INVALID_PARAMETER );
1478 release_object( process );
1482 /* notify the server that a dll has been loaded */
1483 DECL_HANDLER(load_dll)
1485 struct process_dll *dll;
1486 struct mapping *mapping = NULL;
1488 if (req->mapping && !(mapping = get_mapping_obj( current->process, req->mapping, SECTION_QUERY )))
1489 return;
1491 if ((dll = process_load_dll( current->process, mapping, req->base,
1492 get_req_data(), get_req_data_size() )))
1494 dll->size = req->size;
1495 dll->dbg_offset = req->dbg_offset;
1496 dll->dbg_size = req->dbg_size;
1497 dll->name = req->name;
1498 /* only generate event if initialization is done */
1499 if (is_process_init_done( current->process ))
1500 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1502 if (mapping) release_object( mapping );
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->size = dll->size;
1529 reply->entry_point = 0; /* FIXME */
1530 reply->filename_len = dll->namelen;
1531 if (dll->filename)
1533 if (dll->namelen <= get_reply_max_size())
1534 set_reply_data( dll->filename, dll->namelen );
1535 else
1536 set_error( STATUS_BUFFER_TOO_SMALL );
1539 else
1540 set_error( STATUS_DLL_NOT_FOUND );
1542 release_object( process );
1546 /* retrieve the process idle event */
1547 DECL_HANDLER(get_process_idle_event)
1549 struct process *process;
1551 reply->event = 0;
1552 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1554 if (process->idle_event && process != current->process)
1555 reply->event = alloc_handle( current->process, process->idle_event,
1556 EVENT_ALL_ACCESS, 0 );
1557 release_object( process );
1561 /* make the current process a system process */
1562 DECL_HANDLER(make_process_system)
1564 struct process *process = current->process;
1566 if (!shutdown_event)
1568 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1569 make_object_static( (struct object *)shutdown_event );
1572 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1573 return;
1575 if (!process->is_system)
1577 process->is_system = 1;
1578 close_process_desktop( process );
1579 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1580 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1584 /* create a new job object */
1585 DECL_HANDLER(create_job)
1587 struct job *job;
1588 struct unicode_str name;
1589 struct object *root;
1590 const struct security_descriptor *sd;
1591 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1593 if (!objattr) return;
1595 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1597 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1598 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1599 else
1600 reply->handle = alloc_handle_no_access_check( current->process, job,
1601 req->access, objattr->attributes );
1602 release_object( job );
1604 if (root) release_object( root );
1607 /* open a job object */
1608 DECL_HANDLER(open_job)
1610 struct unicode_str name = get_req_unicode_str();
1612 reply->handle = open_object( current->process, req->rootdir, req->access,
1613 &job_ops, &name, req->attributes );
1616 /* assign a job object to a process */
1617 DECL_HANDLER(assign_job)
1619 struct process *process;
1620 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1622 if (!job) return;
1624 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1626 add_job_process( job, process );
1627 release_object( process );
1629 release_object( job );
1633 /* check if a process is associated with a job */
1634 DECL_HANDLER(process_in_job)
1636 struct process *process;
1637 struct job *job;
1639 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1640 return;
1642 if (!req->job)
1644 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1646 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1648 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1649 release_object( job );
1651 release_object( process );
1654 /* terminate all processes associated with the job */
1655 DECL_HANDLER(terminate_job)
1657 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1659 if (!job) return;
1661 terminate_job( job, req->status );
1662 release_object( job );
1665 /* update limits of the job object */
1666 DECL_HANDLER(set_job_limits)
1668 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1670 if (!job) return;
1672 job->limit_flags = req->limit_flags;
1673 release_object( job );
1676 /* set the jobs completion port */
1677 DECL_HANDLER(set_job_completion_port)
1679 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1681 if (!job) return;
1683 if (!job->completion_port)
1685 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1686 job->completion_key = req->key;
1688 else
1689 set_error( STATUS_INVALID_PARAMETER );
1691 release_object( job );