ws2_32: Reimplement inet_pton on top of ntdll functions.
[wine.git] / server / process.c
blob6722addb09151095c41f07baae6392e2c0a91fd1
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 struct object_type *process_get_type( struct object *obj );
64 static int process_signaled( struct object *obj, struct wait_queue_entry *entry );
65 static unsigned int process_map_access( struct object *obj, unsigned int access );
66 static struct security_descriptor *process_get_sd( struct object *obj );
67 static void process_poll_event( struct fd *fd, int event );
68 static struct list *process_get_kernel_obj_list( struct object *obj );
69 static void process_destroy( struct object *obj );
70 static void terminate_process( struct process *process, struct thread *skip, int exit_code );
72 static const struct object_ops process_ops =
74 sizeof(struct process), /* size */
75 process_dump, /* dump */
76 process_get_type, /* get_type */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 process_signaled, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 no_get_fd, /* get_fd */
83 process_map_access, /* map_access */
84 process_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_name */
89 no_open_file, /* open_file */
90 process_get_kernel_obj_list, /* get_kernel_obj_list */
91 no_close_handle, /* close_handle */
92 process_destroy /* destroy */
95 static const struct fd_ops process_fd_ops =
97 NULL, /* get_poll_events */
98 process_poll_event, /* poll_event */
99 NULL, /* flush */
100 NULL, /* get_fd_type */
101 NULL, /* ioctl */
102 NULL, /* queue_async */
103 NULL, /* reselect_async */
104 NULL /* cancel async */
107 /* process startup info */
109 struct startup_info
111 struct object obj; /* object header */
112 struct process *process; /* created process */
113 data_size_t info_size; /* size of startup info */
114 data_size_t data_size; /* size of whole startup data */
115 startup_info_t *data; /* data for startup info */
118 static void startup_info_dump( struct object *obj, int verbose );
119 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry );
120 static void startup_info_destroy( struct object *obj );
122 static const struct object_ops startup_info_ops =
124 sizeof(struct startup_info), /* size */
125 startup_info_dump, /* dump */
126 no_get_type, /* get_type */
127 add_queue, /* add_queue */
128 remove_queue, /* remove_queue */
129 startup_info_signaled, /* signaled */
130 no_satisfied, /* satisfied */
131 no_signal, /* signal */
132 no_get_fd, /* get_fd */
133 no_map_access, /* map_access */
134 default_get_sd, /* get_sd */
135 default_set_sd, /* set_sd */
136 no_lookup_name, /* lookup_name */
137 no_link_name, /* link_name */
138 NULL, /* unlink_name */
139 no_open_file, /* open_file */
140 no_kernel_obj_list, /* get_kernel_obj_list */
141 no_close_handle, /* close_handle */
142 startup_info_destroy /* destroy */
145 /* job object */
147 static void job_dump( struct object *obj, int verbose );
148 static struct object_type *job_get_type( struct object *obj );
149 static int job_signaled( struct object *obj, struct wait_queue_entry *entry );
150 static unsigned int job_map_access( struct object *obj, unsigned int access );
151 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
152 static void job_destroy( struct object *obj );
154 struct job
156 struct object obj; /* object header */
157 struct list process_list; /* list of all processes */
158 int num_processes; /* count of running processes */
159 unsigned int limit_flags; /* limit flags */
160 int terminating; /* job is terminating */
161 int signaled; /* job is signaled */
162 struct completion *completion_port; /* associated completion port */
163 apc_param_t completion_key; /* key to send with completion messages */
166 static const struct object_ops job_ops =
168 sizeof(struct job), /* size */
169 job_dump, /* dump */
170 job_get_type, /* get_type */
171 add_queue, /* add_queue */
172 remove_queue, /* remove_queue */
173 job_signaled, /* signaled */
174 no_satisfied, /* satisfied */
175 no_signal, /* signal */
176 no_get_fd, /* get_fd */
177 job_map_access, /* map_access */
178 default_get_sd, /* get_sd */
179 default_set_sd, /* set_sd */
180 no_lookup_name, /* lookup_name */
181 directory_link_name, /* link_name */
182 default_unlink_name, /* unlink_name */
183 no_open_file, /* open_file */
184 no_kernel_obj_list, /* get_kernel_obj_list */
185 job_close_handle, /* close_handle */
186 job_destroy /* destroy */
189 static struct job *create_job_object( struct object *root, const struct unicode_str *name,
190 unsigned int attr, const struct security_descriptor *sd )
192 struct job *job;
194 if ((job = create_named_object( root, &job_ops, name, attr, sd )))
196 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
198 /* initialize it if it didn't already exist */
199 list_init( &job->process_list );
200 job->num_processes = 0;
201 job->limit_flags = 0;
202 job->terminating = 0;
203 job->signaled = 0;
204 job->completion_port = NULL;
205 job->completion_key = 0;
208 return job;
211 static struct job *get_job_obj( struct process *process, obj_handle_t handle, unsigned int access )
213 return (struct job *)get_handle_obj( process, handle, access, &job_ops );
216 static struct object_type *job_get_type( struct object *obj )
218 static const WCHAR name[] = {'J','o','b'};
219 static const struct unicode_str str = { name, sizeof(name) };
220 return get_object_type( &str );
223 static unsigned int job_map_access( struct object *obj, unsigned int access )
225 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
226 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
227 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
228 if (access & GENERIC_ALL) access |= JOB_OBJECT_ALL_ACCESS;
229 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
232 static void add_job_completion( struct job *job, apc_param_t msg, apc_param_t pid )
234 if (job->completion_port)
235 add_completion( job->completion_port, job->completion_key, pid, STATUS_SUCCESS, msg );
238 static void add_job_process( struct job *job, struct process *process )
240 process->job = (struct job *)grab_object( job );
241 list_add_tail( &job->process_list, &process->job_entry );
242 job->num_processes++;
244 add_job_completion( job, JOB_OBJECT_MSG_NEW_PROCESS, get_process_id(process) );
247 /* called when a process has terminated, allow one additional process */
248 static void release_job_process( struct process *process )
250 struct job *job = process->job;
252 if (!job) return;
254 assert( job->num_processes );
255 job->num_processes--;
257 if (!job->terminating)
258 add_job_completion( job, JOB_OBJECT_MSG_EXIT_PROCESS, get_process_id(process) );
260 if (!job->num_processes)
261 add_job_completion( job, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, 0 );
264 static void terminate_job( struct job *job, int exit_code )
266 /* don't report completion events for terminated processes */
267 job->terminating = 1;
269 for (;;) /* restart from the beginning of the list every time */
271 struct process *process;
273 /* find the first process associated with this job and still running */
274 LIST_FOR_EACH_ENTRY( process, &job->process_list, struct process, job_entry )
276 if (process->running_threads) break;
278 if (&process->job_entry == &job->process_list) break; /* no process found */
279 assert( process->job == job );
280 terminate_process( process, NULL, exit_code );
283 job->terminating = 0;
284 job->signaled = 1;
285 wake_up( &job->obj, 0 );
288 static int job_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
290 struct job *job = (struct job *)obj;
291 assert( obj->ops == &job_ops );
293 if (obj->handle_count == 1) /* last handle */
295 if (job->limit_flags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
296 terminate_job( job, 0 );
298 return 1;
301 static void job_destroy( struct object *obj )
303 struct job *job = (struct job *)obj;
304 assert( obj->ops == &job_ops );
306 assert( !job->num_processes );
307 assert( list_empty(&job->process_list) );
309 if (job->completion_port) release_object( job->completion_port );
312 static void job_dump( struct object *obj, int verbose )
314 struct job *job = (struct job *)obj;
315 assert( obj->ops == &job_ops );
316 fprintf( stderr, "Job processes=%d\n", list_count(&job->process_list) );
319 static int job_signaled( struct object *obj, struct wait_queue_entry *entry )
321 struct job *job = (struct job *)obj;
322 return job->signaled;
325 struct ptid_entry
327 void *ptr; /* entry ptr */
328 unsigned int next; /* next free entry */
331 static struct ptid_entry *ptid_entries; /* array of ptid entries */
332 static unsigned int used_ptid_entries; /* number of entries in use */
333 static unsigned int alloc_ptid_entries; /* number of allocated entries */
334 static unsigned int next_free_ptid; /* next free entry */
335 static unsigned int last_free_ptid; /* last free entry */
336 static unsigned int num_free_ptids; /* number of free ptids */
338 static void kill_all_processes(void);
340 #define PTID_OFFSET 8 /* offset for first ptid value */
342 static unsigned int index_from_ptid(unsigned int id) { return id / 4; }
343 static unsigned int ptid_from_index(unsigned int index) { return index * 4; }
345 /* allocate a new process or thread id */
346 unsigned int alloc_ptid( void *ptr )
348 struct ptid_entry *entry;
349 unsigned int index;
351 if (used_ptid_entries < alloc_ptid_entries)
353 index = used_ptid_entries + PTID_OFFSET;
354 entry = &ptid_entries[used_ptid_entries++];
356 else if (next_free_ptid && num_free_ptids >= 256)
358 index = next_free_ptid;
359 entry = &ptid_entries[index - PTID_OFFSET];
360 if (!(next_free_ptid = entry->next)) last_free_ptid = 0;
361 num_free_ptids--;
363 else /* need to grow the array */
365 unsigned int count = alloc_ptid_entries + (alloc_ptid_entries / 2);
366 if (!count) count = 512;
367 if (!(entry = realloc( ptid_entries, count * sizeof(*entry) )))
369 set_error( STATUS_NO_MEMORY );
370 return 0;
372 ptid_entries = entry;
373 alloc_ptid_entries = count;
374 index = used_ptid_entries + PTID_OFFSET;
375 entry = &ptid_entries[used_ptid_entries++];
378 entry->ptr = ptr;
379 return ptid_from_index( index );
382 /* free a process or thread id */
383 void free_ptid( unsigned int id )
385 unsigned int index = index_from_ptid( id );
386 struct ptid_entry *entry = &ptid_entries[index - PTID_OFFSET];
388 entry->ptr = NULL;
389 entry->next = 0;
391 /* append to end of free list so that we don't reuse it too early */
392 if (last_free_ptid) ptid_entries[last_free_ptid - PTID_OFFSET].next = index;
393 else next_free_ptid = index;
394 last_free_ptid = index;
395 num_free_ptids++;
398 /* retrieve the pointer corresponding to a process or thread id */
399 void *get_ptid_entry( unsigned int id )
401 unsigned int index = index_from_ptid( id );
402 if (index < PTID_OFFSET) return NULL;
403 if (index - PTID_OFFSET >= used_ptid_entries) return NULL;
404 return ptid_entries[index - 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 */
497 /* if the function fails the fd is closed */
498 struct process *create_process( int fd, struct process *parent, int inherit_all,
499 const struct security_descriptor *sd )
501 struct process *process;
503 if (!(process = alloc_object( &process_ops )))
505 close( fd );
506 goto error;
508 process->parent_id = 0;
509 process->debugger = NULL;
510 process->debug_event = 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->exe_file = NULL;
528 process->peb = 0;
529 process->ldt_copy = 0;
530 process->dir_cache = NULL;
531 process->winstation = 0;
532 process->desktop = 0;
533 process->token = NULL;
534 process->trace_data = 0;
535 process->rawinput_mouse = NULL;
536 process->rawinput_kbd = NULL;
537 list_init( &process->kernel_object );
538 list_init( &process->thread_list );
539 list_init( &process->locks );
540 list_init( &process->asyncs );
541 list_init( &process->classes );
542 list_init( &process->views );
543 list_init( &process->dlls );
544 list_init( &process->rawinput_devices );
546 process->end_time = 0;
547 list_add_tail( &process_list, &process->entry );
549 if (sd && !default_set_sd( &process->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
550 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ))
552 close( fd );
553 goto error;
555 if (!(process->id = process->group_id = alloc_ptid( process )))
557 close( fd );
558 goto error;
560 if (!(process->msg_fd = create_anonymous_fd( &process_fd_ops, fd, &process->obj, 0 ))) goto error;
562 /* create the handle table */
563 if (!parent)
565 process->handles = alloc_handle_table( process, 0 );
566 process->token = token_create_admin();
567 process->affinity = ~0;
569 else
571 process->parent_id = parent->id;
572 process->handles = inherit_all ? copy_handle_table( process, parent )
573 : alloc_handle_table( process, 0 );
574 /* Note: for security reasons, starting a new process does not attempt
575 * to use the current impersonation token for the new process */
576 process->token = token_duplicate( parent->token, TRUE, 0, NULL );
577 process->affinity = parent->affinity;
579 if (!process->handles || !process->token) goto error;
581 /* Assign a high security label to the token. The default would be medium
582 * but Wine provides admin access to all applications right now so high
583 * makes more sense for the time being. */
584 if (!token_assign_label( process->token, security_high_label_sid ))
585 goto error;
587 set_fd_events( process->msg_fd, POLLIN ); /* start listening to events */
588 return process;
590 error:
591 if (process) release_object( process );
592 /* if we failed to start our first process, close everything down */
593 if (!running_processes && master_socket_timeout != TIMEOUT_INFINITE) close_master_socket( 0 );
594 return NULL;
597 /* initialize the current process and fill in the request */
598 data_size_t init_process( struct thread *thread )
600 struct process *process = thread->process;
601 struct startup_info *info = process->startup_info;
603 if (!info) return 0;
604 return info->data_size;
607 /* destroy a process when its refcount is 0 */
608 static void process_destroy( struct object *obj )
610 struct process *process = (struct process *)obj;
611 assert( obj->ops == &process_ops );
613 /* we can't have a thread remaining */
614 assert( list_empty( &process->thread_list ));
615 assert( list_empty( &process->asyncs ));
617 assert( !process->sigkill_timeout ); /* timeout should hold a reference to the process */
619 close_process_handles( process );
620 set_process_startup_state( process, STARTUP_ABORTED );
622 if (process->job)
624 list_remove( &process->job_entry );
625 release_object( process->job );
627 if (process->console) release_object( process->console );
628 if (process->msg_fd) release_object( process->msg_fd );
629 list_remove( &process->entry );
630 if (process->idle_event) release_object( process->idle_event );
631 if (process->exe_file) release_object( process->exe_file );
632 if (process->id) free_ptid( process->id );
633 if (process->token) release_object( process->token );
634 free( process->dir_cache );
637 /* dump a process on stdout for debugging purposes */
638 static void process_dump( struct object *obj, int verbose )
640 struct process *process = (struct process *)obj;
641 assert( obj->ops == &process_ops );
643 fprintf( stderr, "Process id=%04x handles=%p\n", process->id, process->handles );
646 static struct object_type *process_get_type( struct object *obj )
648 static const WCHAR name[] = {'P','r','o','c','e','s','s'};
649 static const struct unicode_str str = { name, sizeof(name) };
650 return get_object_type( &str );
653 static int process_signaled( struct object *obj, struct wait_queue_entry *entry )
655 struct process *process = (struct process *)obj;
656 return !process->running_threads;
659 static unsigned int process_map_access( struct object *obj, unsigned int access )
661 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
662 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_SUSPEND_RESUME |
663 PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION;
664 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE;
665 if (access & GENERIC_ALL) access |= PROCESS_ALL_ACCESS;
667 if (access & PROCESS_QUERY_INFORMATION) access |= PROCESS_QUERY_LIMITED_INFORMATION;
668 if (access & PROCESS_SET_INFORMATION) access |= PROCESS_SET_LIMITED_INFORMATION;
670 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
673 static struct list *process_get_kernel_obj_list( struct object *obj )
675 struct process *process = (struct process *)obj;
676 return &process->kernel_object;
679 static struct security_descriptor *process_get_sd( struct object *obj )
681 static struct security_descriptor *process_default_sd;
683 if (obj->sd) return obj->sd;
685 if (!process_default_sd)
687 size_t users_sid_len = security_sid_len( security_domain_users_sid );
688 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
689 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
690 + users_sid_len + admins_sid_len;
691 ACCESS_ALLOWED_ACE *aaa;
692 ACL *dacl;
694 process_default_sd = mem_alloc( sizeof(*process_default_sd) + admins_sid_len + users_sid_len
695 + dacl_len );
696 process_default_sd->control = SE_DACL_PRESENT;
697 process_default_sd->owner_len = admins_sid_len;
698 process_default_sd->group_len = users_sid_len;
699 process_default_sd->sacl_len = 0;
700 process_default_sd->dacl_len = dacl_len;
701 memcpy( process_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
702 memcpy( (char *)(process_default_sd + 1) + admins_sid_len, security_domain_users_sid, users_sid_len );
704 dacl = (ACL *)((char *)(process_default_sd + 1) + admins_sid_len + users_sid_len);
705 dacl->AclRevision = ACL_REVISION;
706 dacl->Sbz1 = 0;
707 dacl->AclSize = dacl_len;
708 dacl->AceCount = 2;
709 dacl->Sbz2 = 0;
710 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
711 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
712 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
713 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
714 aaa->Mask = GENERIC_READ;
715 memcpy( &aaa->SidStart, security_domain_users_sid, users_sid_len );
716 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
717 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
718 aaa->Header.AceFlags = 0;
719 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
720 aaa->Mask = PROCESS_ALL_ACCESS;
721 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
723 return process_default_sd;
726 static void process_poll_event( struct fd *fd, int event )
728 struct process *process = get_fd_user( fd );
729 assert( process->obj.ops == &process_ops );
731 if (event & (POLLERR | POLLHUP)) kill_process( process, 0 );
732 else if (event & POLLIN) receive_fd( process );
735 static void startup_info_destroy( struct object *obj )
737 struct startup_info *info = (struct startup_info *)obj;
738 assert( obj->ops == &startup_info_ops );
739 free( info->data );
740 if (info->process) release_object( info->process );
743 static void startup_info_dump( struct object *obj, int verbose )
745 struct startup_info *info = (struct startup_info *)obj;
746 assert( obj->ops == &startup_info_ops );
748 fprintf( stderr, "Startup info in=%04x out=%04x err=%04x\n",
749 info->data->hstdin, info->data->hstdout, info->data->hstderr );
752 static int startup_info_signaled( struct object *obj, struct wait_queue_entry *entry )
754 struct startup_info *info = (struct startup_info *)obj;
755 return info->process && info->process->startup_state != STARTUP_IN_PROGRESS;
758 /* get a process from an id (and increment the refcount) */
759 struct process *get_process_from_id( process_id_t id )
761 struct object *obj = get_ptid_entry( id );
763 if (obj && obj->ops == &process_ops) return (struct process *)grab_object( obj );
764 set_error( STATUS_INVALID_PARAMETER );
765 return NULL;
768 /* get a process from a handle (and increment the refcount) */
769 struct process *get_process_from_handle( obj_handle_t handle, unsigned int access )
771 return (struct process *)get_handle_obj( current->process, handle,
772 access, &process_ops );
775 /* find a dll from its base address */
776 static inline struct process_dll *find_process_dll( struct process *process, mod_handle_t base )
778 struct process_dll *dll;
780 LIST_FOR_EACH_ENTRY( dll, &process->dlls, struct process_dll, entry )
782 if (dll->base == base) return dll;
784 return NULL;
787 /* add a dll to a process list */
788 static struct process_dll *process_load_dll( struct process *process, mod_handle_t base,
789 const WCHAR *filename, data_size_t name_len )
791 struct process_dll *dll;
793 /* make sure we don't already have one with the same base address */
794 if (find_process_dll( process, base ))
796 set_error( STATUS_INVALID_PARAMETER );
797 return NULL;
800 if ((dll = mem_alloc( sizeof(*dll) )))
802 dll->base = base;
803 dll->filename = NULL;
804 dll->namelen = name_len;
805 if (name_len && !(dll->filename = memdup( filename, name_len )))
807 free( dll );
808 return NULL;
810 list_add_tail( &process->dlls, &dll->entry );
812 return dll;
815 /* remove a dll from a process list */
816 static void process_unload_dll( struct process *process, mod_handle_t base )
818 struct process_dll *dll = find_process_dll( process, base );
820 if (dll && (&dll->entry != list_head( &process->dlls ))) /* main exe can't be unloaded */
822 free( dll->filename );
823 list_remove( &dll->entry );
824 free( dll );
825 generate_debug_event( current, UNLOAD_DLL_DEBUG_EVENT, &base );
827 else set_error( STATUS_INVALID_PARAMETER );
830 /* terminate a process with the given exit code */
831 static void terminate_process( struct process *process, struct thread *skip, int exit_code )
833 struct thread *thread;
835 grab_object( process ); /* make sure it doesn't get freed when threads die */
836 process->is_terminating = 1;
838 restart:
839 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
841 if (exit_code) thread->exit_code = exit_code;
842 if (thread == skip) continue;
843 if (thread->state == TERMINATED) continue;
844 kill_thread( thread, 1 );
845 goto restart;
847 release_object( process );
850 /* kill all processes */
851 static void kill_all_processes(void)
853 for (;;)
855 struct process *process;
857 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
859 if (process->running_threads) break;
861 if (&process->entry == &process_list) break; /* no process found */
862 terminate_process( process, NULL, 1 );
866 /* kill all processes being attached to a console renderer */
867 void kill_console_processes( struct thread *renderer, int exit_code )
869 for (;;) /* restart from the beginning of the list every time */
871 struct process *process;
873 /* find the first process being attached to 'renderer' and still running */
874 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
876 if (process == renderer->process) continue;
877 if (!process->running_threads) continue;
878 if (process->console && console_get_renderer( process->console ) == renderer) break;
880 if (&process->entry == &process_list) break; /* no process found */
881 terminate_process( process, NULL, exit_code );
885 /* a process has been killed (i.e. its last thread died) */
886 static void process_killed( struct process *process )
888 struct list *ptr;
890 assert( list_empty( &process->thread_list ));
891 process->end_time = current_time;
892 if (!process->is_system) close_process_desktop( process );
893 process->winstation = 0;
894 process->desktop = 0;
895 close_process_handles( process );
896 cancel_process_asyncs( process );
897 if (process->idle_event) release_object( process->idle_event );
898 if (process->exe_file) release_object( process->exe_file );
899 process->idle_event = NULL;
900 process->exe_file = NULL;
902 /* close the console attached to this process, if any */
903 free_console( process );
905 while ((ptr = list_head( &process->rawinput_devices )))
907 struct rawinput_device_entry *entry = LIST_ENTRY( ptr, struct rawinput_device_entry, entry );
908 list_remove( &entry->entry );
909 free( entry );
911 while ((ptr = list_head( &process->dlls )))
913 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
914 free( dll->filename );
915 list_remove( &dll->entry );
916 free( dll );
918 destroy_process_classes( process );
919 free_mapped_views( process );
920 free_process_user_handles( process );
921 remove_process_locks( process );
922 set_process_startup_state( process, STARTUP_ABORTED );
923 finish_process_tracing( process );
924 release_job_process( process );
925 start_sigkill_timer( process );
926 wake_up( &process->obj, 0 );
929 /* add a thread to a process running threads list */
930 void add_process_thread( struct process *process, struct thread *thread )
932 list_add_tail( &process->thread_list, &thread->proc_entry );
933 if (!process->running_threads++)
935 running_processes++;
936 if (!process->is_system)
938 if (!user_processes++ && shutdown_timeout)
940 remove_timeout_user( shutdown_timeout );
941 shutdown_timeout = NULL;
945 grab_object( thread );
948 /* remove a thread from a process running threads list */
949 void remove_process_thread( struct process *process, struct thread *thread )
951 assert( process->running_threads > 0 );
952 assert( !list_empty( &process->thread_list ));
954 list_remove( &thread->proc_entry );
956 if (!--process->running_threads)
958 /* we have removed the last running thread, exit the process */
959 process->exit_code = thread->exit_code;
960 generate_debug_event( thread, EXIT_PROCESS_DEBUG_EVENT, process );
961 process_killed( process );
963 else generate_debug_event( thread, EXIT_THREAD_DEBUG_EVENT, thread );
964 release_object( thread );
967 /* suspend all the threads of a process */
968 void suspend_process( struct process *process )
970 if (!process->suspend++)
972 struct list *ptr, *next;
974 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
976 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
977 if (!thread->suspend) stop_thread( thread );
982 /* resume all the threads of a process */
983 void resume_process( struct process *process )
985 assert (process->suspend > 0);
986 if (!--process->suspend)
988 struct list *ptr, *next;
990 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
992 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
993 if (!thread->suspend) wake_thread( thread );
998 /* kill a process on the spot */
999 void kill_process( struct process *process, int violent_death )
1001 if (!violent_death && process->msg_fd) /* normal termination on pipe close */
1003 release_object( process->msg_fd );
1004 process->msg_fd = NULL;
1007 if (process->sigkill_timeout) /* already waiting for it to die */
1009 remove_timeout_user( process->sigkill_timeout );
1010 process->sigkill_timeout = NULL;
1011 process_died( process );
1012 return;
1015 if (violent_death) terminate_process( process, NULL, 1 );
1016 else
1018 struct list *ptr;
1020 grab_object( process ); /* make sure it doesn't get freed when threads die */
1021 while ((ptr = list_head( &process->thread_list )))
1023 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1024 kill_thread( thread, 0 );
1026 release_object( process );
1030 /* kill all processes being debugged by a given thread */
1031 void kill_debugged_processes( struct thread *debugger, int exit_code )
1033 for (;;) /* restart from the beginning of the list every time */
1035 struct process *process;
1037 /* find the first process being debugged by 'debugger' and still running */
1038 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1040 if (!process->running_threads) continue;
1041 if (process->debugger == debugger) break;
1043 if (&process->entry == &process_list) break; /* no process found */
1044 process->debugger = NULL;
1045 terminate_process( process, NULL, exit_code );
1050 /* detach a debugger from all its debuggees */
1051 void detach_debugged_processes( struct thread *debugger )
1053 struct process *process;
1055 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1057 if (process->debugger == debugger && process->running_threads)
1059 debugger_detach( process, debugger );
1065 void enum_processes( int (*cb)(struct process*, void*), void *user )
1067 struct list *ptr, *next;
1069 LIST_FOR_EACH_SAFE( ptr, next, &process_list )
1071 struct process *process = LIST_ENTRY( ptr, struct process, entry );
1072 if ((cb)(process, user)) break;
1076 /* set the debugged flag in the process PEB */
1077 int set_process_debug_flag( struct process *process, int flag )
1079 char data = (flag != 0);
1081 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1082 return write_process_memory( process, process->peb + 2, 1, &data );
1085 /* take a snapshot of currently running processes */
1086 struct process_snapshot *process_snap( int *count )
1088 struct process_snapshot *snapshot, *ptr;
1089 struct process *process;
1091 if (!running_processes) return NULL;
1092 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
1093 return NULL;
1094 ptr = snapshot;
1095 LIST_FOR_EACH_ENTRY( process, &process_list, struct process, entry )
1097 if (!process->running_threads) continue;
1098 ptr->process = process;
1099 ptr->threads = process->running_threads;
1100 ptr->count = process->obj.refcount;
1101 ptr->priority = process->priority;
1102 ptr->handles = get_handle_table_count(process);
1103 grab_object( process );
1104 ptr++;
1107 if (!(*count = ptr - snapshot))
1109 free( snapshot );
1110 snapshot = NULL;
1112 return snapshot;
1115 /* create a new process */
1116 DECL_HANDLER(new_process)
1118 struct startup_info *info;
1119 const void *info_ptr;
1120 struct unicode_str name;
1121 const struct security_descriptor *sd;
1122 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
1123 struct process *process = NULL;
1124 struct process *parent;
1125 struct thread *parent_thread = current;
1126 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1128 if (socket_fd == -1)
1130 set_error( STATUS_INVALID_PARAMETER );
1131 return;
1133 if (!objattr)
1135 set_error( STATUS_INVALID_PARAMETER );
1136 close( socket_fd );
1137 return;
1139 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1141 set_error( STATUS_INVALID_HANDLE );
1142 close( socket_fd );
1143 return;
1145 if (shutdown_stage)
1147 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1148 close( socket_fd );
1149 return;
1151 if (!is_cpu_supported( req->cpu ))
1153 close( socket_fd );
1154 return;
1157 if (req->parent_process)
1159 if (!(parent = get_process_from_handle( req->parent_process, PROCESS_CREATE_PROCESS)))
1161 close( socket_fd );
1162 return;
1164 parent_thread = NULL;
1166 else parent = (struct process *)grab_object( current->process );
1168 if (parent->job && (req->create_flags & CREATE_BREAKAWAY_FROM_JOB) &&
1169 !(parent->job->limit_flags & (JOB_OBJECT_LIMIT_BREAKAWAY_OK | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)))
1171 set_error( STATUS_ACCESS_DENIED );
1172 close( socket_fd );
1173 release_object( parent );
1174 return;
1177 /* build the startup info for a new process */
1178 if (!(info = alloc_object( &startup_info_ops )))
1180 close( socket_fd );
1181 release_object( parent );
1182 return;
1184 info->process = NULL;
1185 info->data = NULL;
1187 info_ptr = get_req_data_after_objattr( objattr, &info->data_size );
1188 info->info_size = min( req->info_size, info->data_size );
1190 if (req->info_size < sizeof(*info->data))
1192 /* make sure we have a full startup_info_t structure */
1193 data_size_t env_size = info->data_size - info->info_size;
1194 data_size_t info_size = min( req->info_size, FIELD_OFFSET( startup_info_t, curdir_len ));
1196 if (!(info->data = mem_alloc( sizeof(*info->data) + env_size )))
1198 close( socket_fd );
1199 goto done;
1201 memcpy( info->data, info_ptr, info_size );
1202 memset( (char *)info->data + info_size, 0, sizeof(*info->data) - info_size );
1203 memcpy( info->data + 1, (const char *)info_ptr + req->info_size, env_size );
1204 info->info_size = sizeof(startup_info_t);
1205 info->data_size = info->info_size + env_size;
1207 else
1209 data_size_t pos = sizeof(*info->data);
1211 if (!(info->data = memdup( info_ptr, info->data_size )))
1213 close( socket_fd );
1214 goto done;
1216 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1217 FIXUP_LEN( info->data->curdir_len );
1218 FIXUP_LEN( info->data->dllpath_len );
1219 FIXUP_LEN( info->data->imagepath_len );
1220 FIXUP_LEN( info->data->cmdline_len );
1221 FIXUP_LEN( info->data->title_len );
1222 FIXUP_LEN( info->data->desktop_len );
1223 FIXUP_LEN( info->data->shellinfo_len );
1224 FIXUP_LEN( info->data->runtime_len );
1225 #undef FIXUP_LEN
1228 if (!(process = create_process( socket_fd, parent, req->inherit_all, sd ))) goto done;
1230 process->startup_info = (struct startup_info *)grab_object( info );
1232 if (req->exe_file &&
1233 !(process->exe_file = get_file_obj( current->process, req->exe_file, FILE_READ_DATA )))
1234 goto done;
1236 if (parent->job
1237 && !(req->create_flags & CREATE_BREAKAWAY_FROM_JOB)
1238 && !(parent->job->limit_flags & JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK))
1240 add_job_process( parent->job, process );
1243 /* connect to the window station */
1244 connect_process_winstation( process, parent_thread, parent );
1246 /* set the process console */
1247 if (!(req->create_flags & (DETACHED_PROCESS | CREATE_NEW_CONSOLE)))
1249 /* FIXME: some better error checking should be done...
1250 * like if hConOut and hConIn are console handles, then they should be on the same
1251 * physical console
1253 inherit_console( parent_thread, parent, process, req->inherit_all ? info->data->hstdin : 0 );
1256 if (!req->inherit_all && !(req->create_flags & CREATE_NEW_CONSOLE))
1258 info->data->hstdin = duplicate_handle( parent, info->data->hstdin, process,
1259 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1260 info->data->hstdout = duplicate_handle( parent, info->data->hstdout, process,
1261 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1262 info->data->hstderr = duplicate_handle( parent, info->data->hstderr, process,
1263 0, OBJ_INHERIT, DUPLICATE_SAME_ACCESS );
1264 /* some handles above may have been invalid; this is not an error */
1265 if (get_error() == STATUS_INVALID_HANDLE ||
1266 get_error() == STATUS_OBJECT_TYPE_MISMATCH) clear_error();
1268 /* attach to the debugger if requested */
1269 if (req->create_flags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS))
1271 set_process_debugger( process, current );
1272 process->debug_children = !(req->create_flags & DEBUG_ONLY_THIS_PROCESS);
1274 else if (current->process->debugger && current->process->debug_children)
1276 set_process_debugger( process, current->process->debugger );
1277 /* debug_children is set to 1 by default */
1280 if (!(req->create_flags & CREATE_NEW_PROCESS_GROUP))
1281 process->group_id = parent->group_id;
1283 info->process = (struct process *)grab_object( process );
1284 reply->info = alloc_handle( current->process, info, SYNCHRONIZE, 0 );
1285 reply->pid = get_process_id( process );
1286 reply->handle = alloc_handle_no_access_check( current->process, process, req->access, objattr->attributes );
1288 done:
1289 if (process) release_object( process );
1290 release_object( parent );
1291 release_object( info );
1294 /* execute a new process, replacing the existing one */
1295 DECL_HANDLER(exec_process)
1297 struct process *process;
1298 int socket_fd = thread_get_inflight_fd( current, req->socket_fd );
1300 if (socket_fd == -1)
1302 set_error( STATUS_INVALID_PARAMETER );
1303 return;
1305 if (fcntl( socket_fd, F_SETFL, O_NONBLOCK ) == -1)
1307 set_error( STATUS_INVALID_HANDLE );
1308 close( socket_fd );
1309 return;
1311 if (shutdown_stage)
1313 set_error( STATUS_SHUTDOWN_IN_PROGRESS );
1314 close( socket_fd );
1315 return;
1317 if (!is_cpu_supported( req->cpu ))
1319 close( socket_fd );
1320 return;
1322 if (!(process = create_process( socket_fd, NULL, 0, NULL ))) return;
1323 create_thread( -1, process, NULL );
1324 release_object( process );
1327 /* Retrieve information about a newly started process */
1328 DECL_HANDLER(get_new_process_info)
1330 struct startup_info *info;
1332 if ((info = (struct startup_info *)get_handle_obj( current->process, req->info,
1333 0, &startup_info_ops )))
1335 reply->success = is_process_init_done( info->process );
1336 reply->exit_code = info->process->exit_code;
1337 release_object( info );
1341 /* Retrieve the new process startup info */
1342 DECL_HANDLER(get_startup_info)
1344 struct process *process = current->process;
1345 struct startup_info *info = process->startup_info;
1346 data_size_t size;
1348 if (!info) return;
1350 /* we return the data directly without making a copy so this can only be called once */
1351 reply->info_size = info->info_size;
1352 size = info->data_size;
1353 if (size > get_reply_max_size()) size = get_reply_max_size();
1354 set_reply_data_ptr( info->data, size );
1355 info->data = NULL;
1356 info->data_size = 0;
1359 /* signal the end of the process initialization */
1360 DECL_HANDLER(init_process_done)
1362 struct process_dll *dll;
1363 struct process *process = current->process;
1364 struct mapping *mapping;
1366 if (is_process_init_done(process))
1368 set_error( STATUS_INVALID_PARAMETER );
1369 return;
1371 if (!(dll = find_process_dll( process, req->module )))
1373 set_error( STATUS_DLL_NOT_FOUND );
1374 return;
1376 if (!(mapping = get_mapping_obj( current->process, req->usd_handle, SECTION_MAP_WRITE )))
1378 set_error( STATUS_INVALID_PARAMETER );
1379 return;
1381 init_kusd_mapping( mapping );
1382 release_object( mapping );
1384 /* main exe is the first in the dll list */
1385 list_remove( &dll->entry );
1386 list_add_head( &process->dlls, &dll->entry );
1388 process->ldt_copy = req->ldt_copy;
1389 process->start_time = current_time;
1390 current->entry_point = req->entry;
1391 if (process->exe_file) release_object( process->exe_file );
1392 process->exe_file = NULL;
1394 init_process_tracing( process );
1395 generate_startup_debug_events( process, req->entry );
1396 set_process_startup_state( process, STARTUP_DONE );
1398 if (req->gui) process->idle_event = create_event( NULL, NULL, 0, 1, 0, NULL );
1399 if (process->debugger) set_process_debug_flag( process, 1 );
1400 reply->suspend = (current->suspend || process->suspend);
1403 /* open a handle to a process */
1404 DECL_HANDLER(open_process)
1406 struct process *process = get_process_from_id( req->pid );
1407 reply->handle = 0;
1408 if (process)
1410 reply->handle = alloc_handle( current->process, process, req->access, req->attributes );
1411 release_object( process );
1415 /* terminate a process */
1416 DECL_HANDLER(terminate_process)
1418 struct process *process;
1420 if (req->handle)
1422 process = get_process_from_handle( req->handle, PROCESS_TERMINATE );
1423 if (!process) return;
1425 else process = (struct process *)grab_object( current->process );
1427 reply->self = (current->process == process);
1428 terminate_process( process, current, req->exit_code );
1429 release_object( process );
1432 /* fetch information about a process */
1433 DECL_HANDLER(get_process_info)
1435 struct process *process;
1437 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1439 reply->pid = get_process_id( process );
1440 reply->ppid = process->parent_id;
1441 reply->exit_code = process->exit_code;
1442 reply->priority = process->priority;
1443 reply->affinity = process->affinity;
1444 reply->peb = process->peb;
1445 reply->start_time = process->start_time;
1446 reply->end_time = process->end_time;
1447 reply->cpu = process->cpu;
1448 reply->debugger_present = !!process->debugger;
1449 reply->debug_children = process->debug_children;
1450 release_object( process );
1454 /* retrieve information about a process memory usage */
1455 DECL_HANDLER(get_process_vm_counters)
1457 struct process *process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION );
1459 if (!process) return;
1460 #ifdef linux
1461 if (process->unix_pid != -1)
1463 FILE *f;
1464 char proc_path[32], line[256];
1465 unsigned long value;
1467 sprintf( proc_path, "/proc/%u/status", process->unix_pid );
1468 if ((f = fopen( proc_path, "r" )))
1470 while (fgets( line, sizeof(line), f ))
1472 if (sscanf( line, "VmPeak: %lu", &value ))
1473 reply->peak_virtual_size = (mem_size_t)value * 1024;
1474 else if (sscanf( line, "VmSize: %lu", &value ))
1475 reply->virtual_size = (mem_size_t)value * 1024;
1476 else if (sscanf( line, "VmHWM: %lu", &value ))
1477 reply->peak_working_set_size = (mem_size_t)value * 1024;
1478 else if (sscanf( line, "VmRSS: %lu", &value ))
1479 reply->working_set_size = (mem_size_t)value * 1024;
1480 else if (sscanf( line, "RssAnon: %lu", &value ))
1481 reply->pagefile_usage += (mem_size_t)value * 1024;
1482 else if (sscanf( line, "VmSwap: %lu", &value ))
1483 reply->pagefile_usage += (mem_size_t)value * 1024;
1485 reply->peak_pagefile_usage = reply->pagefile_usage;
1486 fclose( f );
1488 else set_error( STATUS_ACCESS_DENIED );
1490 else set_error( STATUS_ACCESS_DENIED );
1491 #endif
1492 release_object( process );
1495 static void set_process_affinity( struct process *process, affinity_t affinity )
1497 struct thread *thread;
1499 if (!process->running_threads)
1501 set_error( STATUS_PROCESS_IS_TERMINATING );
1502 return;
1505 process->affinity = affinity;
1507 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1509 set_thread_affinity( thread, affinity );
1513 /* set information about a process */
1514 DECL_HANDLER(set_process_info)
1516 struct process *process;
1518 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
1520 if (req->mask & SET_PROCESS_INFO_PRIORITY) process->priority = req->priority;
1521 if (req->mask & SET_PROCESS_INFO_AFFINITY) set_process_affinity( process, req->affinity );
1522 release_object( process );
1526 /* read data from a process address space */
1527 DECL_HANDLER(read_process_memory)
1529 struct process *process;
1530 data_size_t len = get_reply_max_size();
1532 if (!(process = get_process_from_handle( req->handle, PROCESS_VM_READ ))) return;
1534 if (len)
1536 char *buffer = mem_alloc( len );
1537 if (buffer)
1539 if (read_process_memory( process, req->addr, len, buffer ))
1540 set_reply_data_ptr( buffer, len );
1541 else
1542 free( buffer );
1545 release_object( process );
1548 /* write data to a process address space */
1549 DECL_HANDLER(write_process_memory)
1551 struct process *process;
1553 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
1555 data_size_t len = get_req_data_size();
1556 if (len) write_process_memory( process, req->addr, len, get_req_data() );
1557 else set_error( STATUS_INVALID_PARAMETER );
1558 release_object( process );
1562 /* notify the server that a dll has been loaded */
1563 DECL_HANDLER(load_dll)
1565 struct process_dll *dll;
1567 if ((dll = process_load_dll( current->process, req->base, get_req_data(), get_req_data_size() )))
1569 dll->dbg_offset = req->dbg_offset;
1570 dll->dbg_size = req->dbg_size;
1571 dll->name = req->name;
1572 /* only generate event if initialization is done */
1573 if (is_process_init_done( current->process ))
1574 generate_debug_event( current, LOAD_DLL_DEBUG_EVENT, dll );
1578 /* notify the server that a dll is being unloaded */
1579 DECL_HANDLER(unload_dll)
1581 process_unload_dll( current->process, req->base );
1584 /* retrieve information about a module in a process */
1585 DECL_HANDLER(get_dll_info)
1587 struct process *process;
1589 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_LIMITED_INFORMATION )))
1591 struct process_dll *dll;
1593 if (req->base_address)
1594 dll = find_process_dll( process, req->base_address );
1595 else /* NULL means main module */
1596 dll = list_head( &process->dlls ) ?
1597 LIST_ENTRY(list_head( &process->dlls ), struct process_dll, entry) : NULL;
1599 if (dll)
1601 reply->entry_point = 0; /* FIXME */
1602 reply->filename_len = dll->namelen;
1603 if (dll->filename)
1605 if (dll->namelen <= get_reply_max_size())
1606 set_reply_data( dll->filename, dll->namelen );
1607 else
1608 set_error( STATUS_BUFFER_TOO_SMALL );
1611 else
1612 set_error( STATUS_DLL_NOT_FOUND );
1614 release_object( process );
1618 /* retrieve the process idle event */
1619 DECL_HANDLER(get_process_idle_event)
1621 struct process *process;
1623 reply->event = 0;
1624 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
1626 if (process->idle_event && process != current->process)
1627 reply->event = alloc_handle( current->process, process->idle_event,
1628 EVENT_ALL_ACCESS, 0 );
1629 release_object( process );
1633 /* make the current process a system process */
1634 DECL_HANDLER(make_process_system)
1636 struct process *process = current->process;
1638 if (!shutdown_event)
1640 if (!(shutdown_event = create_event( NULL, NULL, 0, 1, 0, NULL ))) return;
1641 make_object_static( (struct object *)shutdown_event );
1644 if (!(reply->event = alloc_handle( current->process, shutdown_event, SYNCHRONIZE, 0 )))
1645 return;
1647 if (!process->is_system)
1649 process->is_system = 1;
1650 close_process_desktop( process );
1651 if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE)
1652 shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL );
1656 /* create a new job object */
1657 DECL_HANDLER(create_job)
1659 struct job *job;
1660 struct unicode_str name;
1661 struct object *root;
1662 const struct security_descriptor *sd;
1663 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1665 if (!objattr) return;
1667 if ((job = create_job_object( root, &name, objattr->attributes, sd )))
1669 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1670 reply->handle = alloc_handle( current->process, job, req->access, objattr->attributes );
1671 else
1672 reply->handle = alloc_handle_no_access_check( current->process, job,
1673 req->access, objattr->attributes );
1674 release_object( job );
1676 if (root) release_object( root );
1679 /* open a job object */
1680 DECL_HANDLER(open_job)
1682 struct unicode_str name = get_req_unicode_str();
1684 reply->handle = open_object( current->process, req->rootdir, req->access,
1685 &job_ops, &name, req->attributes );
1688 /* assign a job object to a process */
1689 DECL_HANDLER(assign_job)
1691 struct process *process;
1692 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_ASSIGN_PROCESS );
1694 if (!job) return;
1696 if ((process = get_process_from_handle( req->process, PROCESS_SET_QUOTA | PROCESS_TERMINATE )))
1698 if (!process->running_threads)
1699 set_error( STATUS_PROCESS_IS_TERMINATING );
1700 else if (process->job)
1701 set_error( STATUS_ACCESS_DENIED );
1702 else
1703 add_job_process( job, process );
1704 release_object( process );
1706 release_object( job );
1710 /* check if a process is associated with a job */
1711 DECL_HANDLER(process_in_job)
1713 struct process *process;
1714 struct job *job;
1716 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION )))
1717 return;
1719 if (!req->job)
1721 set_error( process->job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1723 else if ((job = get_job_obj( current->process, req->job, JOB_OBJECT_QUERY )))
1725 set_error( process->job == job ? STATUS_PROCESS_IN_JOB : STATUS_PROCESS_NOT_IN_JOB );
1726 release_object( job );
1728 release_object( process );
1731 /* terminate all processes associated with the job */
1732 DECL_HANDLER(terminate_job)
1734 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_TERMINATE );
1736 if (!job) return;
1738 terminate_job( job, req->status );
1739 release_object( job );
1742 /* update limits of the job object */
1743 DECL_HANDLER(set_job_limits)
1745 struct job *job = get_job_obj( current->process, req->handle, JOB_OBJECT_SET_ATTRIBUTES );
1747 if (!job) return;
1749 job->limit_flags = req->limit_flags;
1750 release_object( job );
1753 /* set the jobs completion port */
1754 DECL_HANDLER(set_job_completion_port)
1756 struct job *job = get_job_obj( current->process, req->job, JOB_OBJECT_SET_ATTRIBUTES );
1758 if (!job) return;
1760 if (!job->completion_port)
1762 job->completion_port = get_completion_obj( current->process, req->port, IO_COMPLETION_MODIFY_STATE );
1763 job->completion_key = req->key;
1765 else
1766 set_error( STATUS_INVALID_PARAMETER );
1768 release_object( job );
1771 /* Suspend a process */
1772 DECL_HANDLER(suspend_process)
1774 struct process *process;
1776 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1778 struct list *ptr, *next;
1780 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1782 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1783 suspend_thread( thread );
1786 release_object( process );
1790 /* Resume a process */
1791 DECL_HANDLER(resume_process)
1793 struct process *process;
1795 if ((process = get_process_from_handle( req->handle, PROCESS_SUSPEND_RESUME )))
1797 struct list *ptr, *next;
1799 LIST_FOR_EACH_SAFE( ptr, next, &process->thread_list )
1801 struct thread *thread = LIST_ENTRY( ptr, struct thread, proc_entry );
1802 resume_thread( thread );
1805 release_object( process );