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
22 #include "wine/port.h"
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
42 #define WIN32_NO_STATUS
55 static struct list process_list
= LIST_INIT(process_list
);
56 static int running_processes
, user_processes
;
57 static struct event
*shutdown_event
; /* signaled when shutdown starts */
58 static struct timeout_user
*shutdown_timeout
; /* timeout for server shutdown */
59 static int shutdown_stage
; /* current stage in the shutdown process */
61 static const WCHAR process_name
[] = {'P','r','o','c','e','s','s'};
63 struct type_descr process_type
=
65 { process_name
, sizeof(process_name
) }, /* name */
66 PROCESS_ALL_ACCESS
, /* valid_access */
68 STANDARD_RIGHTS_READ
| PROCESS_VM_READ
| PROCESS_QUERY_INFORMATION
,
69 STANDARD_RIGHTS_WRITE
| PROCESS_SUSPEND_RESUME
| PROCESS_SET_INFORMATION
| PROCESS_SET_QUOTA
70 | PROCESS_CREATE_PROCESS
| PROCESS_DUP_HANDLE
| PROCESS_VM_WRITE
| PROCESS_VM_OPERATION
71 | PROCESS_CREATE_THREAD
,
72 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
,
77 static void process_dump( struct object
*obj
, int verbose
);
78 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
79 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
80 static struct security_descriptor
*process_get_sd( struct object
*obj
);
81 static void process_poll_event( struct fd
*fd
, int event
);
82 static struct list
*process_get_kernel_obj_list( struct object
*obj
);
83 static void process_destroy( struct object
*obj
);
84 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
);
86 static const struct object_ops process_ops
=
88 sizeof(struct process
), /* size */
89 &process_type
, /* type */
90 process_dump
, /* dump */
91 add_queue
, /* add_queue */
92 remove_queue
, /* remove_queue */
93 process_signaled
, /* signaled */
94 no_satisfied
, /* satisfied */
95 no_signal
, /* signal */
96 no_get_fd
, /* get_fd */
97 process_map_access
, /* map_access */
98 process_get_sd
, /* get_sd */
99 default_set_sd
, /* set_sd */
100 no_get_full_name
, /* get_full_name */
101 no_lookup_name
, /* lookup_name */
102 no_link_name
, /* link_name */
103 NULL
, /* unlink_name */
104 no_open_file
, /* open_file */
105 process_get_kernel_obj_list
, /* get_kernel_obj_list */
106 no_close_handle
, /* close_handle */
107 process_destroy
/* destroy */
110 static const struct fd_ops process_fd_ops
=
112 NULL
, /* get_poll_events */
113 process_poll_event
, /* poll_event */
115 NULL
, /* get_fd_type */
117 NULL
, /* queue_async */
118 NULL
, /* reselect_async */
119 NULL
/* cancel async */
122 /* process startup info */
126 struct object obj
; /* object header */
127 struct process
*process
; /* created process */
128 data_size_t info_size
; /* size of startup info */
129 data_size_t data_size
; /* size of whole startup data */
130 startup_info_t
*data
; /* data for startup info */
133 static void startup_info_dump( struct object
*obj
, int verbose
);
134 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
135 static void startup_info_destroy( struct object
*obj
);
137 static const struct object_ops startup_info_ops
=
139 sizeof(struct startup_info
), /* size */
141 startup_info_dump
, /* dump */
142 add_queue
, /* add_queue */
143 remove_queue
, /* remove_queue */
144 startup_info_signaled
, /* signaled */
145 no_satisfied
, /* satisfied */
146 no_signal
, /* signal */
147 no_get_fd
, /* get_fd */
148 default_map_access
, /* map_access */
149 default_get_sd
, /* get_sd */
150 default_set_sd
, /* set_sd */
151 no_get_full_name
, /* get_full_name */
152 no_lookup_name
, /* lookup_name */
153 no_link_name
, /* link_name */
154 NULL
, /* unlink_name */
155 no_open_file
, /* open_file */
156 no_kernel_obj_list
, /* get_kernel_obj_list */
157 no_close_handle
, /* close_handle */
158 startup_info_destroy
/* destroy */
163 static const WCHAR job_name
[] = {'J','o','b'};
165 struct type_descr job_type
=
167 { job_name
, sizeof(job_name
) }, /* name */
168 JOB_OBJECT_ALL_ACCESS
, /* valid_access */
170 STANDARD_RIGHTS_READ
| JOB_OBJECT_QUERY
,
171 STANDARD_RIGHTS_WRITE
| JOB_OBJECT_TERMINATE
| JOB_OBJECT_SET_ATTRIBUTES
| JOB_OBJECT_ASSIGN_PROCESS
,
172 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
173 JOB_OBJECT_ALL_ACCESS
177 static void job_dump( struct object
*obj
, int verbose
);
178 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
179 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
180 static void job_destroy( struct object
*obj
);
184 struct object obj
; /* object header */
185 struct list process_list
; /* list of all processes */
186 int num_processes
; /* count of running processes */
187 int total_processes
; /* count of processes which have been assigned */
188 unsigned int limit_flags
; /* limit flags */
189 int terminating
; /* job is terminating */
190 int signaled
; /* job is signaled */
191 struct completion
*completion_port
; /* associated completion port */
192 apc_param_t completion_key
; /* key to send with completion messages */
195 static const struct object_ops job_ops
=
197 sizeof(struct job
), /* size */
198 &job_type
, /* type */
200 add_queue
, /* add_queue */
201 remove_queue
, /* remove_queue */
202 job_signaled
, /* signaled */
203 no_satisfied
, /* satisfied */
204 no_signal
, /* signal */
205 no_get_fd
, /* get_fd */
206 default_map_access
, /* map_access */
207 default_get_sd
, /* get_sd */
208 default_set_sd
, /* set_sd */
209 default_get_full_name
, /* get_full_name */
210 no_lookup_name
, /* lookup_name */
211 directory_link_name
, /* link_name */
212 default_unlink_name
, /* unlink_name */
213 no_open_file
, /* open_file */
214 no_kernel_obj_list
, /* get_kernel_obj_list */
215 job_close_handle
, /* close_handle */
216 job_destroy
/* destroy */
219 static struct job
*create_job_object( struct object
*root
, const struct unicode_str
*name
,
220 unsigned int attr
, const struct security_descriptor
*sd
)
224 if ((job
= create_named_object( root
, &job_ops
, name
, attr
, sd
)))
226 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
228 /* initialize it if it didn't already exist */
229 list_init( &job
->process_list
);
230 job
->num_processes
= 0;
231 job
->total_processes
= 0;
232 job
->limit_flags
= 0;
233 job
->terminating
= 0;
235 job
->completion_port
= NULL
;
236 job
->completion_key
= 0;
242 static struct job
*get_job_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
244 return (struct job
*)get_handle_obj( process
, handle
, access
, &job_ops
);
247 static void add_job_completion( struct job
*job
, apc_param_t msg
, apc_param_t pid
)
249 if (job
->completion_port
)
250 add_completion( job
->completion_port
, job
->completion_key
, pid
, STATUS_SUCCESS
, msg
);
253 static void add_job_process( struct job
*job
, struct process
*process
)
255 process
->job
= (struct job
*)grab_object( job
);
256 list_add_tail( &job
->process_list
, &process
->job_entry
);
257 job
->num_processes
++;
258 job
->total_processes
++;
260 add_job_completion( job
, JOB_OBJECT_MSG_NEW_PROCESS
, get_process_id(process
) );
263 /* called when a process has terminated, allow one additional process */
264 static void release_job_process( struct process
*process
)
266 struct job
*job
= process
->job
;
270 assert( job
->num_processes
);
271 job
->num_processes
--;
273 if (!job
->terminating
)
274 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
276 if (!job
->num_processes
)
277 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
280 static void terminate_job( struct job
*job
, int exit_code
)
282 /* don't report completion events for terminated processes */
283 job
->terminating
= 1;
285 for (;;) /* restart from the beginning of the list every time */
287 struct process
*process
;
289 /* find the first process associated with this job and still running */
290 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
292 if (process
->running_threads
) break;
294 if (&process
->job_entry
== &job
->process_list
) break; /* no process found */
295 assert( process
->job
== job
);
296 terminate_process( process
, NULL
, exit_code
);
299 job
->terminating
= 0;
301 wake_up( &job
->obj
, 0 );
304 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
306 struct job
*job
= (struct job
*)obj
;
307 assert( obj
->ops
== &job_ops
);
309 if (obj
->handle_count
== 1) /* last handle */
311 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
312 terminate_job( job
, 0 );
317 static void job_destroy( struct object
*obj
)
319 struct job
*job
= (struct job
*)obj
;
320 assert( obj
->ops
== &job_ops
);
322 assert( !job
->num_processes
);
323 assert( list_empty(&job
->process_list
) );
325 if (job
->completion_port
) release_object( job
->completion_port
);
328 static void job_dump( struct object
*obj
, int verbose
)
330 struct job
*job
= (struct job
*)obj
;
331 assert( obj
->ops
== &job_ops
);
332 fprintf( stderr
, "Job processes=%d\n", list_count(&job
->process_list
) );
335 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
337 struct job
*job
= (struct job
*)obj
;
338 return job
->signaled
;
343 void *ptr
; /* entry ptr */
344 unsigned int next
; /* next free entry */
347 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
348 static unsigned int used_ptid_entries
; /* number of entries in use */
349 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
350 static unsigned int next_free_ptid
; /* next free entry */
351 static unsigned int last_free_ptid
; /* last free entry */
352 static unsigned int num_free_ptids
; /* number of free ptids */
354 static void kill_all_processes(void);
356 #define PTID_OFFSET 8 /* offset for first ptid value */
358 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
359 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
361 /* allocate a new process or thread id */
362 unsigned int alloc_ptid( void *ptr
)
364 struct ptid_entry
*entry
;
367 if (used_ptid_entries
< alloc_ptid_entries
)
369 index
= used_ptid_entries
+ PTID_OFFSET
;
370 entry
= &ptid_entries
[used_ptid_entries
++];
372 else if (next_free_ptid
&& num_free_ptids
>= 256)
374 index
= next_free_ptid
;
375 entry
= &ptid_entries
[index
- PTID_OFFSET
];
376 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
379 else /* need to grow the array */
381 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
382 if (!count
) count
= 512;
383 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
385 set_error( STATUS_NO_MEMORY
);
388 ptid_entries
= entry
;
389 alloc_ptid_entries
= count
;
390 index
= used_ptid_entries
+ PTID_OFFSET
;
391 entry
= &ptid_entries
[used_ptid_entries
++];
395 return ptid_from_index( index
);
398 /* free a process or thread id */
399 void free_ptid( unsigned int id
)
401 unsigned int index
= index_from_ptid( id
);
402 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
407 /* append to end of free list so that we don't reuse it too early */
408 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
409 else next_free_ptid
= index
;
410 last_free_ptid
= index
;
414 /* retrieve the pointer corresponding to a process or thread id */
415 void *get_ptid_entry( unsigned int id
)
417 unsigned int index
= index_from_ptid( id
);
418 if (index
< PTID_OFFSET
) return NULL
;
419 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
420 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
423 /* return the main thread of the process */
424 struct thread
*get_process_first_thread( struct process
*process
)
426 struct list
*ptr
= list_head( &process
->thread_list
);
427 if (!ptr
) return NULL
;
428 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
431 /* set the state of the process startup info */
432 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
434 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
435 if (process
->startup_info
)
437 wake_up( &process
->startup_info
->obj
, 0 );
438 release_object( process
->startup_info
);
439 process
->startup_info
= NULL
;
443 /* callback for server shutdown */
444 static void server_shutdown_timeout( void *arg
)
446 shutdown_timeout
= NULL
;
447 if (!running_processes
)
449 close_master_socket( 0 );
452 switch(++shutdown_stage
)
454 case 1: /* signal system processes to exit */
455 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
456 if (shutdown_event
) set_event( shutdown_event
);
457 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
458 close_master_socket( 4 * -TICKS_PER_SEC
);
460 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
461 kill_all_processes();
466 /* forced shutdown, used for wineserver -k */
467 void shutdown_master_socket(void)
469 kill_all_processes();
471 if (shutdown_timeout
)
473 remove_timeout_user( shutdown_timeout
);
474 shutdown_timeout
= NULL
;
476 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
479 /* final cleanup once we are sure a process is really dead */
480 static void process_died( struct process
*process
)
482 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
483 if (!process
->is_system
)
485 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
486 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
488 release_object( process
);
489 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
492 /* callback for process sigkill timeout */
493 static void process_sigkill( void *private )
495 struct process
*process
= private;
497 process
->sigkill_timeout
= NULL
;
498 kill( process
->unix_pid
, SIGKILL
);
499 process_died( process
);
502 /* start the sigkill timer for a process upon exit */
503 static void start_sigkill_timer( struct process
*process
)
505 grab_object( process
);
506 if (process
->unix_pid
!= -1 && process
->msg_fd
)
507 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
509 process_died( process
);
512 /* create a new process */
513 /* if the function fails the fd is closed */
514 struct process
*create_process( int fd
, struct process
*parent
, int inherit_all
, const startup_info_t
*info
,
515 const struct security_descriptor
*sd
, const obj_handle_t
*handles
,
516 unsigned int handle_count
, struct token
*token
)
518 struct process
*process
;
520 if (!(process
= alloc_object( &process_ops
)))
525 process
->parent_id
= 0;
526 process
->debug_obj
= NULL
;
527 process
->debug_event
= NULL
;
528 process
->handles
= NULL
;
529 process
->msg_fd
= NULL
;
530 process
->sigkill_timeout
= NULL
;
531 process
->unix_pid
= -1;
532 process
->exit_code
= STILL_ACTIVE
;
533 process
->running_threads
= 0;
534 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
535 process
->suspend
= 0;
536 process
->is_system
= 0;
537 process
->debug_children
= 1;
538 process
->is_terminating
= 0;
540 process
->console
= NULL
;
541 process
->startup_state
= STARTUP_IN_PROGRESS
;
542 process
->startup_info
= NULL
;
543 process
->idle_event
= NULL
;
545 process
->ldt_copy
= 0;
546 process
->dir_cache
= NULL
;
547 process
->winstation
= 0;
548 process
->desktop
= 0;
549 process
->token
= NULL
;
550 process
->trace_data
= 0;
551 process
->rawinput_mouse
= NULL
;
552 process
->rawinput_kbd
= NULL
;
553 list_init( &process
->kernel_object
);
554 list_init( &process
->thread_list
);
555 list_init( &process
->locks
);
556 list_init( &process
->asyncs
);
557 list_init( &process
->classes
);
558 list_init( &process
->views
);
559 list_init( &process
->dlls
);
560 list_init( &process
->rawinput_devices
);
562 process
->end_time
= 0;
563 list_add_tail( &process_list
, &process
->entry
);
565 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
566 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
571 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
576 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
578 /* create the handle table */
581 process
->handles
= alloc_handle_table( process
, 0 );
582 process
->token
= token_create_admin();
583 process
->affinity
= ~0;
587 obj_handle_t std_handles
[3];
589 std_handles
[0] = info
->hstdin
;
590 std_handles
[1] = info
->hstdout
;
591 std_handles
[2] = info
->hstderr
;
593 process
->parent_id
= parent
->id
;
594 process
->handles
= inherit_all
? copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
)
595 : alloc_handle_table( process
, 0 );
596 /* Note: for security reasons, starting a new process does not attempt
597 * to use the current impersonation token for the new process */
598 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
599 process
->affinity
= parent
->affinity
;
601 if (!process
->handles
|| !process
->token
) goto error
;
603 /* Assign a high security label to the token. The default would be medium
604 * but Wine provides admin access to all applications right now so high
605 * makes more sense for the time being. */
606 if (!token_assign_label( process
->token
, security_high_label_sid
))
609 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
613 if (process
) release_object( process
);
614 /* if we failed to start our first process, close everything down */
615 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
619 /* get the process data size */
620 data_size_t
get_process_startup_info_size( struct process
*process
)
622 struct startup_info
*info
= process
->startup_info
;
625 return info
->data_size
;
628 /* destroy a process when its refcount is 0 */
629 static void process_destroy( struct object
*obj
)
631 struct process
*process
= (struct process
*)obj
;
632 assert( obj
->ops
== &process_ops
);
634 /* we can't have a thread remaining */
635 assert( list_empty( &process
->thread_list
));
636 assert( list_empty( &process
->asyncs
));
638 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
640 close_process_handles( process
);
641 set_process_startup_state( process
, STARTUP_ABORTED
);
645 list_remove( &process
->job_entry
);
646 release_object( process
->job
);
648 if (process
->console
) release_object( process
->console
);
649 if (process
->msg_fd
) release_object( process
->msg_fd
);
650 list_remove( &process
->entry
);
651 if (process
->idle_event
) release_object( process
->idle_event
);
652 if (process
->id
) free_ptid( process
->id
);
653 if (process
->token
) release_object( process
->token
);
654 free( process
->dir_cache
);
657 /* dump a process on stdout for debugging purposes */
658 static void process_dump( struct object
*obj
, int verbose
)
660 struct process
*process
= (struct process
*)obj
;
661 assert( obj
->ops
== &process_ops
);
663 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
666 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
668 struct process
*process
= (struct process
*)obj
;
669 return !process
->running_threads
;
672 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
674 access
= default_map_access( obj
, access
);
675 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
676 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
680 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
682 struct process
*process
= (struct process
*)obj
;
683 return &process
->kernel_object
;
686 static struct security_descriptor
*process_get_sd( struct object
*obj
)
688 static struct security_descriptor
*process_default_sd
;
690 if (obj
->sd
) return obj
->sd
;
692 if (!process_default_sd
)
694 size_t users_sid_len
= security_sid_len( security_domain_users_sid
);
695 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
696 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
697 + users_sid_len
+ admins_sid_len
;
698 ACCESS_ALLOWED_ACE
*aaa
;
701 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
703 process_default_sd
->control
= SE_DACL_PRESENT
;
704 process_default_sd
->owner_len
= admins_sid_len
;
705 process_default_sd
->group_len
= users_sid_len
;
706 process_default_sd
->sacl_len
= 0;
707 process_default_sd
->dacl_len
= dacl_len
;
708 memcpy( process_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
709 memcpy( (char *)(process_default_sd
+ 1) + admins_sid_len
, security_domain_users_sid
, users_sid_len
);
711 dacl
= (ACL
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
712 dacl
->AclRevision
= ACL_REVISION
;
714 dacl
->AclSize
= dacl_len
;
717 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
718 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
719 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
720 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
721 aaa
->Mask
= GENERIC_READ
;
722 memcpy( &aaa
->SidStart
, security_domain_users_sid
, users_sid_len
);
723 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
724 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
725 aaa
->Header
.AceFlags
= 0;
726 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
727 aaa
->Mask
= PROCESS_ALL_ACCESS
;
728 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
730 return process_default_sd
;
733 static void process_poll_event( struct fd
*fd
, int event
)
735 struct process
*process
= get_fd_user( fd
);
736 assert( process
->obj
.ops
== &process_ops
);
738 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
739 else if (event
& POLLIN
) receive_fd( process
);
742 static void startup_info_destroy( struct object
*obj
)
744 struct startup_info
*info
= (struct startup_info
*)obj
;
745 assert( obj
->ops
== &startup_info_ops
);
747 if (info
->process
) release_object( info
->process
);
750 static void startup_info_dump( struct object
*obj
, int verbose
)
752 struct startup_info
*info
= (struct startup_info
*)obj
;
753 assert( obj
->ops
== &startup_info_ops
);
755 fputs( "Startup info", stderr
);
757 fprintf( stderr
, " in=%04x out=%04x err=%04x",
758 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
759 fputc( '\n', stderr
);
762 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
764 struct startup_info
*info
= (struct startup_info
*)obj
;
765 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
768 /* get a process from an id (and increment the refcount) */
769 struct process
*get_process_from_id( process_id_t id
)
771 struct object
*obj
= get_ptid_entry( id
);
773 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
774 set_error( STATUS_INVALID_CID
);
778 /* get a process from a handle (and increment the refcount) */
779 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
781 return (struct process
*)get_handle_obj( current
->process
, handle
,
782 access
, &process_ops
);
785 /* find a dll from its base address */
786 static inline struct process_dll
*find_process_dll( struct process
*process
, mod_handle_t base
)
788 struct process_dll
*dll
;
790 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
792 if (dll
->base
== base
) return dll
;
797 /* add a dll to a process list */
798 static struct process_dll
*process_load_dll( struct process
*process
, mod_handle_t base
,
799 const WCHAR
*filename
, data_size_t name_len
)
801 struct process_dll
*dll
;
803 /* make sure we don't already have one with the same base address */
804 if (find_process_dll( process
, base
))
806 set_error( STATUS_INVALID_PARAMETER
);
810 if ((dll
= mem_alloc( sizeof(*dll
) )))
813 dll
->filename
= NULL
;
814 dll
->namelen
= name_len
;
815 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
820 list_add_tail( &process
->dlls
, &dll
->entry
);
825 /* remove a dll from a process list */
826 static void process_unload_dll( struct process
*process
, mod_handle_t base
)
828 struct process_dll
*dll
= find_process_dll( process
, base
);
830 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
832 free( dll
->filename
);
833 list_remove( &dll
->entry
);
835 generate_debug_event( current
, DbgUnloadDllStateChange
, &base
);
837 else set_error( STATUS_INVALID_PARAMETER
);
840 /* terminate a process with the given exit code */
841 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
843 struct thread
*thread
;
845 grab_object( process
); /* make sure it doesn't get freed when threads die */
846 process
->is_terminating
= 1;
849 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
851 if (exit_code
) thread
->exit_code
= exit_code
;
852 if (thread
== skip
) continue;
853 if (thread
->state
== TERMINATED
) continue;
854 kill_thread( thread
, 1 );
857 release_object( process
);
860 /* kill all processes */
861 static void kill_all_processes(void)
865 struct process
*process
;
867 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
869 if (process
->running_threads
) break;
871 if (&process
->entry
== &process_list
) break; /* no process found */
872 terminate_process( process
, NULL
, 1 );
876 /* kill all processes being attached to a console renderer */
877 void kill_console_processes( struct thread
*renderer
, int exit_code
)
879 for (;;) /* restart from the beginning of the list every time */
881 struct process
*process
;
883 /* find the first process being attached to 'renderer' and still running */
884 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
886 if (process
== renderer
->process
) continue;
887 if (!process
->running_threads
) continue;
888 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
890 if (&process
->entry
== &process_list
) break; /* no process found */
891 terminate_process( process
, NULL
, exit_code
);
895 /* a process has been killed (i.e. its last thread died) */
896 static void process_killed( struct process
*process
)
900 assert( list_empty( &process
->thread_list
));
901 process
->end_time
= current_time
;
902 if (!process
->is_system
) close_process_desktop( process
);
903 process
->winstation
= 0;
904 process
->desktop
= 0;
905 close_process_handles( process
);
906 cancel_process_asyncs( process
);
907 if (process
->idle_event
) release_object( process
->idle_event
);
908 process
->idle_event
= NULL
;
909 assert( !process
->console
);
911 while ((ptr
= list_head( &process
->rawinput_devices
)))
913 struct rawinput_device_entry
*entry
= LIST_ENTRY( ptr
, struct rawinput_device_entry
, entry
);
914 list_remove( &entry
->entry
);
917 while ((ptr
= list_head( &process
->dlls
)))
919 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
920 free( dll
->filename
);
921 list_remove( &dll
->entry
);
924 destroy_process_classes( process
);
925 free_mapped_views( process
);
926 free_process_user_handles( process
);
927 remove_process_locks( process
);
928 set_process_startup_state( process
, STARTUP_ABORTED
);
929 finish_process_tracing( process
);
930 release_job_process( process
);
931 start_sigkill_timer( process
);
932 wake_up( &process
->obj
, 0 );
935 /* add a thread to a process running threads list */
936 void add_process_thread( struct process
*process
, struct thread
*thread
)
938 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
939 if (!process
->running_threads
++)
942 if (!process
->is_system
)
944 if (!user_processes
++ && shutdown_timeout
)
946 remove_timeout_user( shutdown_timeout
);
947 shutdown_timeout
= NULL
;
951 grab_object( thread
);
954 /* remove a thread from a process running threads list */
955 void remove_process_thread( struct process
*process
, struct thread
*thread
)
957 assert( process
->running_threads
> 0 );
958 assert( !list_empty( &process
->thread_list
));
960 list_remove( &thread
->proc_entry
);
962 if (!--process
->running_threads
)
964 /* we have removed the last running thread, exit the process */
965 process
->exit_code
= thread
->exit_code
;
966 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
967 process_killed( process
);
969 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
970 release_object( thread
);
973 /* suspend all the threads of a process */
974 void suspend_process( struct process
*process
)
976 if (!process
->suspend
++)
978 struct list
*ptr
, *next
;
980 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
982 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
983 if (!thread
->suspend
) stop_thread( thread
);
988 /* resume all the threads of a process */
989 void resume_process( struct process
*process
)
991 assert (process
->suspend
> 0);
992 if (!--process
->suspend
)
994 struct list
*ptr
, *next
;
996 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
998 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
999 if (!thread
->suspend
) wake_thread( thread
);
1004 /* kill a process on the spot */
1005 void kill_process( struct process
*process
, int violent_death
)
1007 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1009 release_object( process
->msg_fd
);
1010 process
->msg_fd
= NULL
;
1013 if (process
->sigkill_timeout
) /* already waiting for it to die */
1015 remove_timeout_user( process
->sigkill_timeout
);
1016 process
->sigkill_timeout
= NULL
;
1017 process_died( process
);
1021 if (violent_death
) terminate_process( process
, NULL
, 1 );
1026 grab_object( process
); /* make sure it doesn't get freed when threads die */
1027 while ((ptr
= list_head( &process
->thread_list
)))
1029 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1030 kill_thread( thread
, 0 );
1032 release_object( process
);
1036 /* detach all processes being debugged by a given thread */
1037 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1039 for (;;) /* restart from the beginning of the list every time */
1041 struct process
*process
;
1043 /* find the first process being debugged by 'debugger' and still running */
1044 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1046 if (!process
->running_threads
) continue;
1047 if (process
->debug_obj
== debug_obj
) break;
1049 if (&process
->entry
== &process_list
) break; /* no process found */
1052 process
->debug_obj
= NULL
;
1053 terminate_process( process
, NULL
, exit_code
);
1055 else debugger_detach( process
, debug_obj
);
1060 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1062 struct list
*ptr
, *next
;
1064 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1066 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1067 if ((cb
)(process
, user
)) break;
1071 /* set the debugged flag in the process PEB */
1072 int set_process_debug_flag( struct process
*process
, int flag
)
1074 char data
= (flag
!= 0);
1076 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1077 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1080 /* create a new process */
1081 DECL_HANDLER(new_process
)
1083 struct startup_info
*info
;
1084 const void *info_ptr
;
1085 struct unicode_str name
;
1086 const struct security_descriptor
*sd
;
1087 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1088 struct process
*process
= NULL
;
1089 struct token
*token
= NULL
;
1090 struct debug_obj
*debug_obj
= NULL
;
1091 struct process
*parent
;
1092 struct thread
*parent_thread
= current
;
1093 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1094 const obj_handle_t
*handles
= NULL
;
1096 if (socket_fd
== -1)
1098 set_error( STATUS_INVALID_PARAMETER
);
1103 set_error( STATUS_INVALID_PARAMETER
);
1107 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1109 set_error( STATUS_INVALID_HANDLE
);
1115 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1119 if (!is_cpu_supported( req
->cpu
))
1125 if (req
->parent_process
)
1127 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1132 parent_thread
= NULL
;
1134 else parent
= (struct process
*)grab_object( current
->process
);
1136 if (parent
->job
&& (req
->create_flags
& CREATE_BREAKAWAY_FROM_JOB
) &&
1137 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1139 set_error( STATUS_ACCESS_DENIED
);
1141 release_object( parent
);
1145 /* build the startup info for a new process */
1146 if (!(info
= alloc_object( &startup_info_ops
)))
1149 release_object( parent
);
1152 info
->process
= NULL
;
1155 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1157 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1159 set_error( STATUS_INVALID_PARAMETER
);
1163 if (req
->handles_size
)
1166 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1167 info
->data_size
-= req
->handles_size
;
1169 info
->info_size
= min( req
->info_size
, info
->data_size
);
1171 if (req
->info_size
< sizeof(*info
->data
))
1173 /* make sure we have a full startup_info_t structure */
1174 data_size_t env_size
= info
->data_size
- info
->info_size
;
1175 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1177 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1182 memcpy( info
->data
, info_ptr
, info_size
);
1183 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1184 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1185 info
->info_size
= sizeof(startup_info_t
);
1186 info
->data_size
= info
->info_size
+ env_size
;
1190 data_size_t pos
= sizeof(*info
->data
);
1192 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1197 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1198 FIXUP_LEN( info
->data
->curdir_len
);
1199 FIXUP_LEN( info
->data
->dllpath_len
);
1200 FIXUP_LEN( info
->data
->imagepath_len
);
1201 FIXUP_LEN( info
->data
->cmdline_len
);
1202 FIXUP_LEN( info
->data
->title_len
);
1203 FIXUP_LEN( info
->data
->desktop_len
);
1204 FIXUP_LEN( info
->data
->shellinfo_len
);
1205 FIXUP_LEN( info
->data
->runtime_len
);
1209 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1214 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1220 if (!(process
= create_process( socket_fd
, parent
, req
->inherit_all
, info
->data
, sd
,
1221 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1224 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1227 && !(req
->create_flags
& CREATE_BREAKAWAY_FROM_JOB
)
1228 && !(parent
->job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
))
1230 add_job_process( parent
->job
, process
);
1233 /* connect to the window station */
1234 connect_process_winstation( process
, parent_thread
, parent
);
1236 /* set the process console */
1237 if (info
->data
->console
> 3)
1238 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1239 0, 0, DUPLICATE_SAME_ACCESS
);
1241 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
1243 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1244 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1245 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1246 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1247 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1248 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1249 /* some handles above may have been invalid; this is not an error */
1250 if (get_error() == STATUS_INVALID_HANDLE
||
1251 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1254 /* attach to the debugger */
1257 process
->debug_obj
= debug_obj
;
1258 process
->debug_children
= !(req
->create_flags
& DEBUG_ONLY_THIS_PROCESS
);
1260 else if (parent
->debug_children
)
1262 process
->debug_obj
= parent
->debug_obj
;
1263 /* debug_children is set to 1 by default */
1266 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
1267 process
->group_id
= parent
->group_id
;
1269 info
->process
= (struct process
*)grab_object( process
);
1270 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1271 reply
->pid
= get_process_id( process
);
1272 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1275 if (process
) release_object( process
);
1276 if (debug_obj
) release_object( debug_obj
);
1277 if (token
) release_object( token
);
1278 release_object( parent
);
1279 release_object( info
);
1282 /* execute a new process, replacing the existing one */
1283 DECL_HANDLER(exec_process
)
1285 struct process
*process
;
1286 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1288 if (socket_fd
== -1)
1290 set_error( STATUS_INVALID_PARAMETER
);
1293 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1295 set_error( STATUS_INVALID_HANDLE
);
1301 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1305 if (!is_cpu_supported( req
->cpu
))
1310 if (!(process
= create_process( socket_fd
, NULL
, 0, NULL
, NULL
, NULL
, 0, NULL
))) return;
1311 create_thread( -1, process
, NULL
);
1312 release_object( process
);
1315 /* Retrieve information about a newly started process */
1316 DECL_HANDLER(get_new_process_info
)
1318 struct startup_info
*info
;
1320 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1321 0, &startup_info_ops
)))
1323 reply
->success
= is_process_init_done( info
->process
);
1324 reply
->exit_code
= info
->process
->exit_code
;
1325 release_object( info
);
1329 /* Retrieve the new process startup info */
1330 DECL_HANDLER(get_startup_info
)
1332 struct process
*process
= current
->process
;
1333 struct startup_info
*info
= process
->startup_info
;
1338 /* we return the data directly without making a copy so this can only be called once */
1339 reply
->info_size
= info
->info_size
;
1340 size
= info
->data_size
;
1341 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1342 set_reply_data_ptr( info
->data
, size
);
1344 info
->data_size
= 0;
1347 /* signal the end of the process initialization */
1348 DECL_HANDLER(init_process_done
)
1350 struct process_dll
*dll
;
1351 struct process
*process
= current
->process
;
1352 struct memory_view
*view
;
1354 const pe_image_info_t
*image_info
;
1356 if (is_process_init_done(process
))
1358 set_error( STATUS_INVALID_PARAMETER
);
1361 if (!(view
= get_exe_view( process
)))
1363 set_error( STATUS_DLL_NOT_FOUND
);
1366 if (!(image_info
= get_view_image_info( view
, &base
))) return;
1368 if ((dll
= find_process_dll( process
, base
)))
1370 /* main exe is the first in the dll list */
1371 list_remove( &dll
->entry
);
1372 list_add_head( &process
->dlls
, &dll
->entry
);
1375 process
->start_time
= current_time
;
1376 current
->entry_point
= image_info
->entry_point
;
1378 init_process_tracing( process
);
1379 generate_startup_debug_events( process
);
1380 set_process_startup_state( process
, STARTUP_DONE
);
1382 if (image_info
->subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1383 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1384 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1385 reply
->entry
= image_info
->entry_point
;
1386 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1389 /* open a handle to a process */
1390 DECL_HANDLER(open_process
)
1392 struct process
*process
= get_process_from_id( req
->pid
);
1396 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1397 release_object( process
);
1401 /* terminate a process */
1402 DECL_HANDLER(terminate_process
)
1404 struct process
*process
;
1408 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1409 if (!process
) return;
1411 else process
= (struct process
*)grab_object( current
->process
);
1413 reply
->self
= (current
->process
== process
);
1414 terminate_process( process
, current
, req
->exit_code
);
1415 release_object( process
);
1418 /* fetch information about a process */
1419 DECL_HANDLER(get_process_info
)
1421 struct process
*process
;
1423 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1425 reply
->pid
= get_process_id( process
);
1426 reply
->ppid
= process
->parent_id
;
1427 reply
->exit_code
= process
->exit_code
;
1428 reply
->priority
= process
->priority
;
1429 reply
->affinity
= process
->affinity
;
1430 reply
->peb
= process
->peb
;
1431 reply
->start_time
= process
->start_time
;
1432 reply
->end_time
= process
->end_time
;
1433 reply
->cpu
= process
->cpu
;
1434 if (get_reply_max_size())
1437 const pe_image_info_t
*info
;
1438 struct memory_view
*view
= get_exe_view( process
);
1439 if (view
&& (info
= get_view_image_info( view
, &base
)))
1440 set_reply_data( info
, min( sizeof(*info
), get_reply_max_size() ));
1442 release_object( process
);
1446 /* retrieve debug information about a process */
1447 DECL_HANDLER(get_process_debug_info
)
1449 struct process
*process
;
1451 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1453 reply
->debug_children
= process
->debug_children
;
1454 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1455 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, DEBUG_ALL_ACCESS
, 0 );
1456 release_object( process
);
1459 /* retrieve information about a process memory usage */
1460 DECL_HANDLER(get_process_vm_counters
)
1462 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1464 if (!process
) return;
1466 if (process
->unix_pid
!= -1)
1469 char proc_path
[32], line
[256];
1470 unsigned long value
;
1472 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1473 if ((f
= fopen( proc_path
, "r" )))
1475 while (fgets( line
, sizeof(line
), f
))
1477 if (sscanf( line
, "VmPeak: %lu", &value
))
1478 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1479 else if (sscanf( line
, "VmSize: %lu", &value
))
1480 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1481 else if (sscanf( line
, "VmHWM: %lu", &value
))
1482 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1483 else if (sscanf( line
, "VmRSS: %lu", &value
))
1484 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1485 else if (sscanf( line
, "RssAnon: %lu", &value
))
1486 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1487 else if (sscanf( line
, "VmSwap: %lu", &value
))
1488 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1490 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1493 else set_error( STATUS_ACCESS_DENIED
);
1495 else set_error( STATUS_ACCESS_DENIED
);
1497 release_object( process
);
1500 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1502 struct thread
*thread
;
1504 if (!process
->running_threads
)
1506 set_error( STATUS_PROCESS_IS_TERMINATING
);
1510 process
->affinity
= affinity
;
1512 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1514 set_thread_affinity( thread
, affinity
);
1518 /* set information about a process */
1519 DECL_HANDLER(set_process_info
)
1521 struct process
*process
;
1523 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1525 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1526 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1527 release_object( process
);
1531 /* read data from a process address space */
1532 DECL_HANDLER(read_process_memory
)
1534 struct process
*process
;
1535 data_size_t len
= get_reply_max_size();
1537 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1541 char *buffer
= mem_alloc( len
);
1544 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1545 set_reply_data_ptr( buffer
, len
);
1550 release_object( process
);
1553 /* write data to a process address space */
1554 DECL_HANDLER(write_process_memory
)
1556 struct process
*process
;
1558 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1560 data_size_t len
= get_req_data_size();
1561 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1562 else set_error( STATUS_INVALID_PARAMETER
);
1563 release_object( process
);
1567 /* notify the server that a dll has been loaded */
1568 DECL_HANDLER(load_dll
)
1570 struct process_dll
*dll
;
1572 if ((dll
= process_load_dll( current
->process
, req
->base
, get_req_data(), get_req_data_size() )))
1574 dll
->name
= req
->name
;
1575 /* only generate event if initialization is done */
1576 if (is_process_init_done( current
->process
))
1577 generate_debug_event( current
, DbgLoadDllStateChange
, dll
);
1581 /* notify the server that a dll is being unloaded */
1582 DECL_HANDLER(unload_dll
)
1584 process_unload_dll( current
->process
, req
->base
);
1587 /* retrieve information about a module in a process */
1588 DECL_HANDLER(get_dll_info
)
1590 struct process
*process
;
1592 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1594 struct process_dll
*dll
;
1596 if (req
->base_address
)
1597 dll
= find_process_dll( process
, req
->base_address
);
1598 else /* NULL means main module */
1599 dll
= list_head( &process
->dlls
) ?
1600 LIST_ENTRY(list_head( &process
->dlls
), struct process_dll
, entry
) : NULL
;
1604 reply
->entry_point
= 0; /* FIXME */
1605 reply
->filename_len
= dll
->namelen
;
1608 if (dll
->namelen
<= get_reply_max_size())
1609 set_reply_data( dll
->filename
, dll
->namelen
);
1611 set_error( STATUS_BUFFER_TOO_SMALL
);
1615 set_error( STATUS_DLL_NOT_FOUND
);
1617 release_object( process
);
1621 /* retrieve the process idle event */
1622 DECL_HANDLER(get_process_idle_event
)
1624 struct process
*process
;
1627 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1629 if (process
->idle_event
&& process
!= current
->process
)
1630 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1631 EVENT_ALL_ACCESS
, 0 );
1632 release_object( process
);
1636 /* make the current process a system process */
1637 DECL_HANDLER(make_process_system
)
1639 struct process
*process
= current
->process
;
1641 if (!shutdown_event
)
1643 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1644 release_object( shutdown_event
);
1647 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1650 if (!process
->is_system
)
1652 process
->is_system
= 1;
1653 close_process_desktop( process
);
1654 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1655 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1659 /* create a new job object */
1660 DECL_HANDLER(create_job
)
1663 struct unicode_str name
;
1664 struct object
*root
;
1665 const struct security_descriptor
*sd
;
1666 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1668 if (!objattr
) return;
1670 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1672 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1673 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1675 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1676 req
->access
, objattr
->attributes
);
1677 release_object( job
);
1679 if (root
) release_object( root
);
1682 /* open a job object */
1683 DECL_HANDLER(open_job
)
1685 struct unicode_str name
= get_req_unicode_str();
1687 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1688 &job_ops
, &name
, req
->attributes
);
1691 /* assign a job object to a process */
1692 DECL_HANDLER(assign_job
)
1694 struct process
*process
;
1695 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1699 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1701 if (!process
->running_threads
)
1702 set_error( STATUS_PROCESS_IS_TERMINATING
);
1703 else if (process
->job
)
1704 set_error( STATUS_ACCESS_DENIED
);
1706 add_job_process( job
, process
);
1707 release_object( process
);
1709 release_object( job
);
1713 /* check if a process is associated with a job */
1714 DECL_HANDLER(process_in_job
)
1716 struct process
*process
;
1719 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1724 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1726 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1728 set_error( process
->job
== job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1729 release_object( job
);
1731 release_object( process
);
1734 /* retrieve information about a job */
1735 DECL_HANDLER(get_job_info
)
1737 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1741 reply
->total_processes
= job
->total_processes
;
1742 reply
->active_processes
= job
->num_processes
;
1743 release_object( job
);
1746 /* terminate all processes associated with the job */
1747 DECL_HANDLER(terminate_job
)
1749 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1753 terminate_job( job
, req
->status
);
1754 release_object( job
);
1757 /* update limits of the job object */
1758 DECL_HANDLER(set_job_limits
)
1760 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1764 job
->limit_flags
= req
->limit_flags
;
1765 release_object( job
);
1768 /* set the jobs completion port */
1769 DECL_HANDLER(set_job_completion_port
)
1771 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1775 if (!job
->completion_port
)
1777 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1778 job
->completion_key
= req
->key
;
1781 set_error( STATUS_INVALID_PARAMETER
);
1783 release_object( job
);
1786 /* Suspend a process */
1787 DECL_HANDLER(suspend_process
)
1789 struct process
*process
;
1791 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1793 struct list
*ptr
, *next
;
1795 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1797 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1798 suspend_thread( thread
);
1801 release_object( process
);
1805 /* Resume a process */
1806 DECL_HANDLER(resume_process
)
1808 struct process
*process
;
1810 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1812 struct list
*ptr
, *next
;
1814 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1816 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1817 resume_thread( thread
);
1820 release_object( process
);
1824 /* Get a list of processes and threads currently running */
1825 DECL_HANDLER(list_processes
)
1827 struct process
*process
;
1828 struct thread
*thread
;
1829 unsigned int pos
= 0;
1832 reply
->process_count
= 0;
1833 reply
->info_size
= 0;
1835 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1837 struct process_dll
*exe
= get_process_exe_module( process
);
1838 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1839 reply
->info_size
+= sizeof(struct process_info
);
1840 if (exe
) reply
->info_size
+= exe
->namelen
;
1841 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1842 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1843 reply
->process_count
++;
1846 if (reply
->info_size
> get_reply_max_size())
1848 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1852 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1854 memset( buffer
, 0, reply
->info_size
);
1855 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1857 struct process_info
*process_info
;
1858 struct process_dll
*exe
= get_process_exe_module( process
);
1860 pos
= (pos
+ 7) & ~7;
1861 process_info
= (struct process_info
*)(buffer
+ pos
);
1862 process_info
->start_time
= process
->start_time
;
1863 process_info
->name_len
= exe
? exe
->namelen
: 0;
1864 process_info
->thread_count
= process
->running_threads
;
1865 process_info
->priority
= process
->priority
;
1866 process_info
->pid
= process
->id
;
1867 process_info
->parent_pid
= process
->parent_id
;
1868 process_info
->handle_count
= get_handle_table_count(process
);
1869 process_info
->unix_pid
= process
->unix_pid
;
1870 pos
+= sizeof(*process_info
);
1874 memcpy( buffer
+ pos
, exe
->filename
, exe
->namelen
);
1875 pos
+= exe
->namelen
;
1878 pos
= (pos
+ 7) & ~7;
1879 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1881 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1883 thread_info
->start_time
= thread
->creation_time
;
1884 thread_info
->tid
= thread
->id
;
1885 thread_info
->base_priority
= thread
->priority
;
1886 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1887 thread_info
->unix_tid
= thread
->unix_tid
;
1888 pos
+= sizeof(*thread_info
);