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
32 #include <sys/types.h>
34 #include <sys/socket.h>
37 #ifdef HAVE_SYS_PARAM_H
38 # include <sys/param.h>
40 #ifdef HAVE_SYS_QUEUE_H
41 # include <sys/queue.h>
43 #ifdef HAVE_SYS_SYSCTL_H
44 # include <sys/sysctl.h>
46 #ifdef HAVE_SYS_USER_H
47 # define thread __unix_thread
48 # include <sys/user.h>
51 #ifdef HAVE_LIBPROCSTAT
52 # include <libprocstat.h>
56 #define WIN32_NO_STATUS
69 static struct list process_list
= LIST_INIT(process_list
);
70 static int running_processes
, user_processes
;
71 static struct event
*shutdown_event
; /* signaled when shutdown starts */
72 static struct timeout_user
*shutdown_timeout
; /* timeout for server shutdown */
73 static int shutdown_stage
; /* current stage in the shutdown process */
75 static const WCHAR process_name
[] = {'P','r','o','c','e','s','s'};
77 struct type_descr process_type
=
79 { process_name
, sizeof(process_name
) }, /* name */
80 PROCESS_ALL_ACCESS
, /* valid_access */
82 STANDARD_RIGHTS_READ
| PROCESS_VM_READ
| PROCESS_QUERY_INFORMATION
,
83 STANDARD_RIGHTS_WRITE
| PROCESS_SUSPEND_RESUME
| PROCESS_SET_INFORMATION
| PROCESS_SET_QUOTA
84 | PROCESS_CREATE_PROCESS
| PROCESS_DUP_HANDLE
| PROCESS_VM_WRITE
| PROCESS_VM_OPERATION
85 | PROCESS_CREATE_THREAD
,
86 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
,
91 static void process_dump( struct object
*obj
, int verbose
);
92 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
93 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
94 static struct security_descriptor
*process_get_sd( struct object
*obj
);
95 static void process_poll_event( struct fd
*fd
, int event
);
96 static struct list
*process_get_kernel_obj_list( struct object
*obj
);
97 static void process_destroy( struct object
*obj
);
98 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
);
100 static const struct object_ops process_ops
=
102 sizeof(struct process
), /* size */
103 &process_type
, /* type */
104 process_dump
, /* dump */
105 add_queue
, /* add_queue */
106 remove_queue
, /* remove_queue */
107 process_signaled
, /* signaled */
108 no_satisfied
, /* satisfied */
109 no_signal
, /* signal */
110 no_get_fd
, /* get_fd */
111 process_map_access
, /* map_access */
112 process_get_sd
, /* get_sd */
113 default_set_sd
, /* set_sd */
114 no_get_full_name
, /* get_full_name */
115 no_lookup_name
, /* lookup_name */
116 no_link_name
, /* link_name */
117 NULL
, /* unlink_name */
118 no_open_file
, /* open_file */
119 process_get_kernel_obj_list
, /* get_kernel_obj_list */
120 no_close_handle
, /* close_handle */
121 process_destroy
/* destroy */
124 static const struct fd_ops process_fd_ops
=
126 NULL
, /* get_poll_events */
127 process_poll_event
, /* poll_event */
129 NULL
, /* get_fd_type */
131 NULL
, /* queue_async */
132 NULL
, /* reselect_async */
133 NULL
/* cancel async */
136 /* process startup info */
140 struct object obj
; /* object header */
141 struct process
*process
; /* created process */
142 data_size_t info_size
; /* size of startup info */
143 data_size_t data_size
; /* size of whole startup data */
144 startup_info_t
*data
; /* data for startup info */
147 static void startup_info_dump( struct object
*obj
, int verbose
);
148 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
149 static void startup_info_destroy( struct object
*obj
);
151 static const struct object_ops startup_info_ops
=
153 sizeof(struct startup_info
), /* size */
155 startup_info_dump
, /* dump */
156 add_queue
, /* add_queue */
157 remove_queue
, /* remove_queue */
158 startup_info_signaled
, /* signaled */
159 no_satisfied
, /* satisfied */
160 no_signal
, /* signal */
161 no_get_fd
, /* get_fd */
162 default_map_access
, /* map_access */
163 default_get_sd
, /* get_sd */
164 default_set_sd
, /* set_sd */
165 no_get_full_name
, /* get_full_name */
166 no_lookup_name
, /* lookup_name */
167 no_link_name
, /* link_name */
168 NULL
, /* unlink_name */
169 no_open_file
, /* open_file */
170 no_kernel_obj_list
, /* get_kernel_obj_list */
171 no_close_handle
, /* close_handle */
172 startup_info_destroy
/* destroy */
177 static const WCHAR job_name
[] = {'J','o','b'};
179 struct type_descr job_type
=
181 { job_name
, sizeof(job_name
) }, /* name */
182 JOB_OBJECT_ALL_ACCESS
, /* valid_access */
184 STANDARD_RIGHTS_READ
| JOB_OBJECT_QUERY
,
185 STANDARD_RIGHTS_WRITE
| JOB_OBJECT_TERMINATE
| JOB_OBJECT_SET_ATTRIBUTES
| JOB_OBJECT_ASSIGN_PROCESS
,
186 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
187 JOB_OBJECT_ALL_ACCESS
191 static void job_dump( struct object
*obj
, int verbose
);
192 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
193 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
194 static void job_destroy( struct object
*obj
);
198 struct object obj
; /* object header */
199 struct list process_list
; /* list of processes */
200 int num_processes
; /* count of running processes */
201 int total_processes
; /* count of processes which have been assigned */
202 unsigned int limit_flags
; /* limit flags */
203 int terminating
; /* job is terminating */
204 int signaled
; /* job is signaled */
205 struct completion
*completion_port
; /* associated completion port */
206 apc_param_t completion_key
; /* key to send with completion messages */
208 struct list parent_job_entry
; /* list entry for parent job */
209 struct list child_job_list
; /* list of child jobs */
212 static const struct object_ops job_ops
=
214 sizeof(struct job
), /* size */
215 &job_type
, /* type */
217 add_queue
, /* add_queue */
218 remove_queue
, /* remove_queue */
219 job_signaled
, /* signaled */
220 no_satisfied
, /* satisfied */
221 no_signal
, /* signal */
222 no_get_fd
, /* get_fd */
223 default_map_access
, /* map_access */
224 default_get_sd
, /* get_sd */
225 default_set_sd
, /* set_sd */
226 default_get_full_name
, /* get_full_name */
227 no_lookup_name
, /* lookup_name */
228 directory_link_name
, /* link_name */
229 default_unlink_name
, /* unlink_name */
230 no_open_file
, /* open_file */
231 no_kernel_obj_list
, /* get_kernel_obj_list */
232 job_close_handle
, /* close_handle */
233 job_destroy
/* destroy */
236 static struct job
*create_job_object( struct object
*root
, const struct unicode_str
*name
,
237 unsigned int attr
, const struct security_descriptor
*sd
)
241 if ((job
= create_named_object( root
, &job_ops
, name
, attr
, sd
)))
243 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
245 /* initialize it if it didn't already exist */
246 list_init( &job
->process_list
);
247 list_init( &job
->child_job_list
);
248 job
->num_processes
= 0;
249 job
->total_processes
= 0;
250 job
->limit_flags
= 0;
251 job
->terminating
= 0;
253 job
->completion_port
= NULL
;
254 job
->completion_key
= 0;
261 static struct job
*get_job_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
263 return (struct job
*)get_handle_obj( process
, handle
, access
, &job_ops
);
266 static void add_job_completion( struct job
*job
, apc_param_t msg
, apc_param_t pid
)
268 if (job
->completion_port
)
269 add_completion( job
->completion_port
, job
->completion_key
, pid
, STATUS_SUCCESS
, msg
);
272 static void add_job_completion_existing_processes( struct job
*job
, struct job
*completion_job
)
274 struct process
*process
;
277 assert( completion_job
->obj
.ops
== &job_ops
);
279 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
281 add_job_completion_existing_processes( j
, completion_job
);
284 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
286 if (process
->running_threads
)
287 add_job_completion( completion_job
, JOB_OBJECT_MSG_NEW_PROCESS
, get_process_id( process
));
291 static int process_in_job( struct job
*job
, struct process
*process
)
295 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
297 assert( j
->parent
== job
);
298 if (process_in_job( j
, process
)) return 1;
300 return process
->job
== job
;
303 static process_id_t
*get_job_pids( struct job
*job
, process_id_t
*pids
, process_id_t
*end
)
305 struct process
*process
;
308 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
309 pids
= get_job_pids( j
, pids
, end
);
311 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
313 if (pids
== end
) break;
314 if (process
->end_time
) continue; /* skip processes that ended */
315 *pids
++ = process
->id
;
321 static void add_job_process( struct job
*job
, struct process
*process
)
323 struct job
*j
, *common_parent
;
326 if (job
== process
->job
) return;
328 if ((common_parent
= process
->job
))
332 for (j
= job
->parent
; j
; j
= j
->parent
)
333 if (j
== common_parent
) break;
335 if (j
!= common_parent
)
337 /* Job already has parent and the process is not in the job's chain. */
338 set_error( STATUS_ACCESS_DENIED
);
341 /* process->job is referenced in the job->parent chain. */
342 release_object( process
->job
);
346 if (job
->total_processes
)
348 set_error( STATUS_ACCESS_DENIED
);
351 /* transfer reference. */
352 job
->parent
= process
->job
;
353 list_add_tail( &job
->parent
->child_job_list
, &job
->parent_job_entry
);
355 list_remove( &process
->job_entry
);
357 process
->job
= (struct job
*)grab_object( job
);
358 list_add_tail( &job
->process_list
, &process
->job_entry
);
360 pid
= get_process_id( process
);
361 for (j
= job
; j
!= common_parent
; j
= j
->parent
)
364 j
->total_processes
++;
365 add_job_completion( j
, JOB_OBJECT_MSG_NEW_PROCESS
, pid
);
369 /* called when a process has terminated, allow one additional process */
370 static void release_job_process( struct process
*process
)
372 struct job
*job
= process
->job
;
376 assert( job
->num_processes
);
377 job
->num_processes
--;
379 if (!job
->terminating
)
380 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
382 if (!job
->num_processes
)
383 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
389 static void terminate_job( struct job
*job
, int exit_code
)
391 struct process
*process
, *next_process
;
392 struct job
*j
, *next_job
;
394 LIST_FOR_EACH_ENTRY_SAFE( j
, next_job
, &job
->child_job_list
, struct job
, parent_job_entry
)
396 assert( j
->parent
== job
);
399 terminate_job( j
, exit_code
);
403 job
->terminating
= 1;
404 LIST_FOR_EACH_ENTRY_SAFE( process
, next_process
, &job
->process_list
, struct process
, job_entry
)
406 assert( process
->job
== job
);
407 if (process
->running_threads
) terminate_process( process
, NULL
, exit_code
);
409 job
->terminating
= 0;
411 wake_up( &job
->obj
, 0 );
414 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
416 struct job
*job
= (struct job
*)obj
;
417 assert( obj
->ops
== &job_ops
);
419 if (obj
->handle_count
== 1) /* last handle */
421 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
422 terminate_job( job
, 0 );
427 static void job_destroy( struct object
*obj
)
429 struct job
*job
= (struct job
*)obj
;
430 assert( obj
->ops
== &job_ops
);
432 assert( !job
->num_processes
);
433 assert( list_empty( &job
->process_list
));
434 assert( list_empty( &job
->child_job_list
));
436 if (job
->completion_port
) release_object( job
->completion_port
);
439 list_remove( &job
->parent_job_entry
);
440 release_object( job
->parent
);
444 static void job_dump( struct object
*obj
, int verbose
)
446 struct job
*job
= (struct job
*)obj
;
447 assert( obj
->ops
== &job_ops
);
448 fprintf( stderr
, "Job processes=%d child_jobs=%d parent=%p\n",
449 list_count(&job
->process_list
), list_count(&job
->child_job_list
), job
->parent
);
452 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
454 struct job
*job
= (struct job
*)obj
;
455 return job
->signaled
;
460 void *ptr
; /* entry ptr */
461 unsigned int next
; /* next free entry */
464 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
465 static unsigned int used_ptid_entries
; /* number of entries in use */
466 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
467 static unsigned int next_free_ptid
; /* next free entry */
468 static unsigned int last_free_ptid
; /* last free entry */
469 static unsigned int num_free_ptids
; /* number of free ptids */
471 static void kill_all_processes(void);
473 #define PTID_OFFSET 8 /* offset for first ptid value */
475 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
476 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
478 /* allocate a new process or thread id */
479 unsigned int alloc_ptid( void *ptr
)
481 struct ptid_entry
*entry
;
484 if (used_ptid_entries
< alloc_ptid_entries
)
486 index
= used_ptid_entries
+ PTID_OFFSET
;
487 entry
= &ptid_entries
[used_ptid_entries
++];
489 else if (next_free_ptid
&& num_free_ptids
>= 256)
491 index
= next_free_ptid
;
492 entry
= &ptid_entries
[index
- PTID_OFFSET
];
493 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
496 else /* need to grow the array */
498 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
499 if (!count
) count
= 512;
500 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
502 set_error( STATUS_NO_MEMORY
);
505 ptid_entries
= entry
;
506 alloc_ptid_entries
= count
;
507 index
= used_ptid_entries
+ PTID_OFFSET
;
508 entry
= &ptid_entries
[used_ptid_entries
++];
512 return ptid_from_index( index
);
515 /* free a process or thread id */
516 void free_ptid( unsigned int id
)
518 unsigned int index
= index_from_ptid( id
);
519 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
524 /* append to end of free list so that we don't reuse it too early */
525 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
526 else next_free_ptid
= index
;
527 last_free_ptid
= index
;
531 /* retrieve the pointer corresponding to a process or thread id */
532 void *get_ptid_entry( unsigned int id
)
534 unsigned int index
= index_from_ptid( id
);
535 if (index
< PTID_OFFSET
) return NULL
;
536 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
537 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
540 /* return the main thread of the process */
541 struct thread
*get_process_first_thread( struct process
*process
)
543 struct list
*ptr
= list_head( &process
->thread_list
);
544 if (!ptr
) return NULL
;
545 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
548 /* set the state of the process startup info */
549 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
551 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
552 if (process
->startup_info
)
554 wake_up( &process
->startup_info
->obj
, 0 );
555 release_object( process
->startup_info
);
556 process
->startup_info
= NULL
;
560 /* callback for server shutdown */
561 static void server_shutdown_timeout( void *arg
)
563 shutdown_timeout
= NULL
;
564 if (!running_processes
)
566 close_master_socket( 0 );
569 switch(++shutdown_stage
)
571 case 1: /* signal system processes to exit */
572 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
573 if (shutdown_event
) set_event( shutdown_event
);
574 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
575 close_master_socket( 4 * -TICKS_PER_SEC
);
577 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
578 kill_all_processes();
583 /* forced shutdown, used for wineserver -k */
584 void shutdown_master_socket(void)
586 kill_all_processes();
588 if (shutdown_timeout
)
590 remove_timeout_user( shutdown_timeout
);
591 shutdown_timeout
= NULL
;
593 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
596 /* final cleanup once we are sure a process is really dead */
597 static void process_died( struct process
*process
)
599 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
600 if (!process
->is_system
)
602 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
603 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
605 release_object( process
);
606 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
609 /* callback for process sigkill timeout */
610 static void process_sigkill( void *private )
612 struct process
*process
= private;
615 process
->sigkill_delay
*= 2;
616 if (process
->sigkill_delay
>= TICKS_PER_SEC
/ 2)
619 if (!kill( process
->unix_pid
, signal
) && !signal
)
620 process
->sigkill_timeout
= add_timeout_user( -process
->sigkill_delay
, process_sigkill
, process
);
623 process
->sigkill_delay
= TICKS_PER_SEC
/ 64;
624 process
->sigkill_timeout
= NULL
;
625 process_died( process
);
629 /* start the sigkill timer for a process upon exit */
630 static void start_sigkill_timer( struct process
*process
)
632 grab_object( process
);
633 if (process
->unix_pid
!= -1)
634 process
->sigkill_timeout
= add_timeout_user( -process
->sigkill_delay
, process_sigkill
, process
);
636 process_died( process
);
639 /* create a new process */
640 /* if the function fails the fd is closed */
641 struct process
*create_process( int fd
, struct process
*parent
, unsigned int flags
, const startup_info_t
*info
,
642 const struct security_descriptor
*sd
, const obj_handle_t
*handles
,
643 unsigned int handle_count
, struct token
*token
)
645 struct process
*process
;
647 if (!(process
= alloc_object( &process_ops
)))
652 process
->parent_id
= 0;
653 process
->debug_obj
= NULL
;
654 process
->debug_event
= NULL
;
655 process
->handles
= NULL
;
656 process
->msg_fd
= NULL
;
657 process
->sigkill_timeout
= NULL
;
658 process
->sigkill_delay
= TICKS_PER_SEC
/ 64;
659 process
->machine
= native_machine
;
660 process
->unix_pid
= -1;
661 process
->exit_code
= STILL_ACTIVE
;
662 process
->running_threads
= 0;
663 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
664 process
->suspend
= 0;
665 process
->is_system
= 0;
666 process
->debug_children
= 1;
667 process
->is_terminating
= 0;
668 process
->imagelen
= 0;
669 process
->image
= NULL
;
671 process
->console
= NULL
;
672 process
->startup_state
= STARTUP_IN_PROGRESS
;
673 process
->startup_info
= NULL
;
674 process
->idle_event
= NULL
;
676 process
->ldt_copy
= 0;
677 process
->dir_cache
= NULL
;
678 process
->winstation
= 0;
679 process
->desktop
= 0;
680 process
->token
= NULL
;
681 process
->trace_data
= 0;
682 process
->rawinput_devices
= NULL
;
683 process
->rawinput_device_count
= 0;
684 process
->rawinput_mouse
= NULL
;
685 process
->rawinput_kbd
= NULL
;
686 memset( &process
->image_info
, 0, sizeof(process
->image_info
) );
687 list_init( &process
->rawinput_entry
);
688 list_init( &process
->kernel_object
);
689 list_init( &process
->thread_list
);
690 list_init( &process
->locks
);
691 list_init( &process
->asyncs
);
692 list_init( &process
->classes
);
693 list_init( &process
->views
);
695 process
->end_time
= 0;
697 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
698 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
703 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
708 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
710 /* create the handle table */
713 process
->handles
= alloc_handle_table( process
, 0 );
714 process
->token
= token_create_admin( TRUE
, -1, TokenElevationTypeLimited
, default_session_id
);
715 process
->affinity
= ~0;
719 obj_handle_t std_handles
[3];
721 std_handles
[0] = info
->hstdin
;
722 std_handles
[1] = info
->hstdout
;
723 std_handles
[2] = info
->hstderr
;
725 process
->parent_id
= parent
->id
;
726 if (flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
)
727 process
->handles
= copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
);
729 process
->handles
= alloc_handle_table( process
, 0 );
730 /* Note: for security reasons, starting a new process does not attempt
731 * to use the current impersonation token for the new process */
732 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
733 process
->affinity
= parent
->affinity
;
735 if (!process
->handles
|| !process
->token
) goto error
;
736 process
->session_id
= token_get_session_id( process
->token
);
738 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
742 if (process
) release_object( process
);
743 /* if we failed to start our first process, close everything down */
744 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
748 /* get the process data size */
749 data_size_t
get_process_startup_info_size( struct process
*process
)
751 struct startup_info
*info
= process
->startup_info
;
754 return info
->data_size
;
757 /* destroy a process when its refcount is 0 */
758 static void process_destroy( struct object
*obj
)
760 struct process
*process
= (struct process
*)obj
;
761 assert( obj
->ops
== &process_ops
);
763 /* we can't have a thread remaining */
764 assert( list_empty( &process
->thread_list
));
765 assert( list_empty( &process
->asyncs
));
767 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
769 close_process_handles( process
);
770 set_process_startup_state( process
, STARTUP_ABORTED
);
774 list_remove( &process
->job_entry
);
775 release_object( process
->job
);
777 if (process
->console
) release_object( process
->console
);
778 if (process
->msg_fd
) release_object( process
->msg_fd
);
779 if (process
->idle_event
) release_object( process
->idle_event
);
780 if (process
->id
) free_ptid( process
->id
);
781 if (process
->token
) release_object( process
->token
);
782 list_remove( &process
->rawinput_entry
);
783 free( process
->rawinput_devices
);
784 free( process
->dir_cache
);
785 free( process
->image
);
788 /* dump a process on stdout for debugging purposes */
789 static void process_dump( struct object
*obj
, int verbose
)
791 struct process
*process
= (struct process
*)obj
;
792 assert( obj
->ops
== &process_ops
);
794 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
797 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
799 struct process
*process
= (struct process
*)obj
;
800 return !process
->running_threads
;
803 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
805 access
= default_map_access( obj
, access
);
806 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
807 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
811 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
813 struct process
*process
= (struct process
*)obj
;
814 return &process
->kernel_object
;
817 static struct security_descriptor
*process_get_sd( struct object
*obj
)
819 static struct security_descriptor
*process_default_sd
;
821 if (obj
->sd
) return obj
->sd
;
823 if (!process_default_sd
)
828 size_t users_sid_len
= sid_len( &domain_users_sid
);
829 size_t admins_sid_len
= sid_len( &builtin_admins_sid
);
830 size_t dacl_len
= sizeof(*dacl
) + 2 * sizeof(*ace
) + users_sid_len
+ admins_sid_len
;
832 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
834 process_default_sd
->control
= SE_DACL_PRESENT
;
835 process_default_sd
->owner_len
= admins_sid_len
;
836 process_default_sd
->group_len
= users_sid_len
;
837 process_default_sd
->sacl_len
= 0;
838 process_default_sd
->dacl_len
= dacl_len
;
839 sid
= (struct sid
*)(process_default_sd
+ 1);
840 sid
= copy_sid( sid
, &builtin_admins_sid
);
841 sid
= copy_sid( sid
, &domain_users_sid
);
843 dacl
= (struct acl
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
844 dacl
->revision
= ACL_REVISION
;
846 dacl
->size
= dacl_len
;
849 ace
= set_ace( ace_first( dacl
), &domain_users_sid
, ACCESS_ALLOWED_ACE_TYPE
,
850 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
, GENERIC_READ
);
851 set_ace( ace_next( ace
), &builtin_admins_sid
, ACCESS_ALLOWED_ACE_TYPE
, 0, PROCESS_ALL_ACCESS
);
853 return process_default_sd
;
856 static void process_poll_event( struct fd
*fd
, int event
)
858 struct process
*process
= get_fd_user( fd
);
859 assert( process
->obj
.ops
== &process_ops
);
861 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, !process
->is_terminating
);
862 else if (event
& POLLIN
) receive_fd( process
);
865 static void startup_info_destroy( struct object
*obj
)
867 struct startup_info
*info
= (struct startup_info
*)obj
;
868 assert( obj
->ops
== &startup_info_ops
);
870 if (info
->process
) release_object( info
->process
);
873 static void startup_info_dump( struct object
*obj
, int verbose
)
875 struct startup_info
*info
= (struct startup_info
*)obj
;
876 assert( obj
->ops
== &startup_info_ops
);
878 fputs( "Startup info", stderr
);
880 fprintf( stderr
, " in=%04x out=%04x err=%04x",
881 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
882 fputc( '\n', stderr
);
885 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
887 struct startup_info
*info
= (struct startup_info
*)obj
;
888 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
891 /* get a process from an id (and increment the refcount) */
892 struct process
*get_process_from_id( process_id_t id
)
894 struct object
*obj
= get_ptid_entry( id
);
896 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
897 set_error( STATUS_INVALID_CID
);
901 /* get a process from a handle (and increment the refcount) */
902 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
904 return (struct process
*)get_handle_obj( current
->process
, handle
,
905 access
, &process_ops
);
908 /* terminate a process with the given exit code */
909 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
911 struct thread
*thread
;
913 grab_object( process
); /* make sure it doesn't get freed when threads die */
914 process
->is_terminating
= 1;
917 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
919 if (exit_code
) thread
->exit_code
= exit_code
;
920 if (thread
== skip
) continue;
921 if (thread
->state
== TERMINATED
) continue;
922 kill_thread( thread
, 1 );
925 release_object( process
);
928 /* kill all processes */
929 static void kill_all_processes(void)
933 while ((ptr
= list_head( &process_list
)))
935 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
936 terminate_process( process
, NULL
, 1 );
940 /* kill all processes being attached to a console renderer */
941 void kill_console_processes( struct thread
*renderer
, int exit_code
)
943 for (;;) /* restart from the beginning of the list every time */
945 struct process
*process
;
947 /* find the first process being attached to 'renderer' and still running */
948 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
950 if (process
== renderer
->process
) continue;
951 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
953 if (&process
->entry
== &process_list
) break; /* no process found */
954 terminate_process( process
, NULL
, exit_code
);
958 /* a process has been killed (i.e. its last thread died) */
959 static void process_killed( struct process
*process
)
961 assert( list_empty( &process
->thread_list
));
962 process
->end_time
= current_time
;
963 close_process_desktop( process
);
964 process
->winstation
= 0;
965 process
->desktop
= 0;
966 cancel_process_asyncs( process
);
967 close_process_handles( process
);
968 if (process
->idle_event
) release_object( process
->idle_event
);
969 process
->idle_event
= NULL
;
970 assert( !process
->console
);
972 destroy_process_classes( process
);
973 free_mapped_views( process
);
974 free_process_user_handles( process
);
975 remove_process_locks( process
);
976 set_process_startup_state( process
, STARTUP_ABORTED
);
977 finish_process_tracing( process
);
978 release_job_process( process
);
979 start_sigkill_timer( process
);
980 wake_up( &process
->obj
, 0 );
983 /* add a thread to a process running threads list */
984 void add_process_thread( struct process
*process
, struct thread
*thread
)
986 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
987 if (!process
->running_threads
++)
989 list_add_tail( &process_list
, &process
->entry
);
991 if (!process
->is_system
)
993 if (!user_processes
++ && shutdown_timeout
)
995 remove_timeout_user( shutdown_timeout
);
996 shutdown_timeout
= NULL
;
1000 grab_object( thread
);
1003 /* remove a thread from a process running threads list */
1004 void remove_process_thread( struct process
*process
, struct thread
*thread
)
1006 assert( process
->running_threads
> 0 );
1007 assert( !list_empty( &process
->thread_list
));
1009 list_remove( &thread
->proc_entry
);
1011 if (!--process
->running_threads
)
1013 /* we have removed the last running thread, exit the process */
1014 process
->exit_code
= thread
->exit_code
;
1015 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
1016 list_remove( &process
->entry
);
1017 process_killed( process
);
1019 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
1020 release_object( thread
);
1023 /* suspend all the threads of a process */
1024 void suspend_process( struct process
*process
)
1026 if (!process
->suspend
++)
1028 struct list
*ptr
, *next
;
1030 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1032 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1033 if (!thread
->suspend
) stop_thread( thread
);
1038 /* resume all the threads of a process */
1039 void resume_process( struct process
*process
)
1041 assert (process
->suspend
> 0);
1042 if (!--process
->suspend
)
1044 struct list
*ptr
, *next
;
1046 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1048 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1049 if (!thread
->suspend
) wake_thread( thread
);
1054 /* kill a process on the spot */
1055 void kill_process( struct process
*process
, int violent_death
)
1057 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1059 release_object( process
->msg_fd
);
1060 process
->msg_fd
= NULL
;
1063 if (process
->sigkill_timeout
) return; /* already waiting for it to die */
1065 if (violent_death
) terminate_process( process
, NULL
, 1 );
1070 grab_object( process
); /* make sure it doesn't get freed when threads die */
1071 while ((ptr
= list_head( &process
->thread_list
)))
1073 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1074 kill_thread( thread
, 0 );
1076 release_object( process
);
1080 /* detach all processes being debugged by a given thread */
1081 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1083 for (;;) /* restart from the beginning of the list every time */
1085 struct process
*process
;
1087 /* find the first process being debugged by 'debugger' and still running */
1088 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1089 if (process
->debug_obj
== debug_obj
) break;
1091 if (&process
->entry
== &process_list
) break; /* no process found */
1094 process
->debug_obj
= NULL
;
1095 terminate_process( process
, NULL
, exit_code
);
1097 else debugger_detach( process
, debug_obj
);
1102 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1104 struct list
*ptr
, *next
;
1106 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1108 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1109 if ((cb
)(process
, user
)) break;
1113 /* set the debugged flag in the process PEB */
1114 int set_process_debug_flag( struct process
*process
, int flag
)
1116 char data
= (flag
!= 0);
1117 client_ptr_t peb32
= 0;
1119 if (!is_machine_64bit( process
->machine
) && is_machine_64bit( native_machine
))
1120 peb32
= process
->peb
+ 0x1000;
1122 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1123 if (peb32
&& !write_process_memory( process
, peb32
+ 2, 1, &data
)) return 0;
1124 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1127 /* create a new process */
1128 DECL_HANDLER(new_process
)
1130 struct startup_info
*info
;
1131 const void *info_ptr
;
1132 struct unicode_str name
, desktop_path
= {0};
1133 const struct security_descriptor
*sd
;
1134 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1135 struct process
*process
= NULL
;
1136 struct token
*token
= NULL
;
1137 struct debug_obj
*debug_obj
= NULL
;
1138 struct process
*parent
;
1139 struct thread
*parent_thread
= current
;
1140 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1141 const obj_handle_t
*handles
= NULL
;
1142 const obj_handle_t
*job_handles
= NULL
;
1143 unsigned int i
, job_handle_count
;
1146 if (socket_fd
== -1)
1148 set_error( STATUS_INVALID_PARAMETER
);
1153 set_error( STATUS_INVALID_PARAMETER
);
1157 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1159 set_error( STATUS_INVALID_HANDLE
);
1165 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1170 if (req
->parent_process
)
1172 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1177 parent_thread
= NULL
;
1179 else parent
= (struct process
*)grab_object( current
->process
);
1181 /* If a job further in the job chain does not permit breakaway process creation
1182 * succeeds and the process which is trying to breakaway is assigned to that job. */
1183 if (parent
->job
&& (req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
) &&
1184 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1186 set_error( STATUS_ACCESS_DENIED
);
1188 release_object( parent
);
1192 /* build the startup info for a new process */
1193 if (!(info
= alloc_object( &startup_info_ops
)))
1196 release_object( parent
);
1199 info
->process
= NULL
;
1202 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1204 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1206 set_error( STATUS_INVALID_PARAMETER
);
1210 if (req
->handles_size
)
1213 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1214 info
->data_size
-= req
->handles_size
;
1217 if ((req
->jobs_size
& 3) || req
->jobs_size
> info
->data_size
)
1219 set_error( STATUS_INVALID_PARAMETER
);
1225 job_handles
= info_ptr
;
1226 info_ptr
= (const char *)info_ptr
+ req
->jobs_size
;
1227 info
->data_size
-= req
->jobs_size
;
1230 job_handle_count
= req
->jobs_size
/ sizeof(*handles
);
1231 for (i
= 0; i
< job_handle_count
; ++i
)
1233 if (!(job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
)))
1238 release_object( job
);
1241 info
->info_size
= min( req
->info_size
, info
->data_size
);
1243 if (req
->info_size
< sizeof(*info
->data
))
1245 /* make sure we have a full startup_info_t structure */
1246 data_size_t env_size
= info
->data_size
- info
->info_size
;
1247 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1249 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1254 memcpy( info
->data
, info_ptr
, info_size
);
1255 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1256 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1257 info
->info_size
= sizeof(startup_info_t
);
1258 info
->data_size
= info
->info_size
+ env_size
;
1262 data_size_t pos
= sizeof(*info
->data
);
1264 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1269 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1270 FIXUP_LEN( info
->data
->curdir_len
);
1271 FIXUP_LEN( info
->data
->dllpath_len
);
1272 FIXUP_LEN( info
->data
->imagepath_len
);
1273 FIXUP_LEN( info
->data
->cmdline_len
);
1274 FIXUP_LEN( info
->data
->title_len
);
1275 desktop_path
.str
= (WCHAR
*)((char *)info
->data
+ pos
);
1276 FIXUP_LEN( info
->data
->desktop_len
);
1277 desktop_path
.len
= info
->data
->desktop_len
;
1278 FIXUP_LEN( info
->data
->shellinfo_len
);
1279 FIXUP_LEN( info
->data
->runtime_len
);
1283 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1288 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1294 if (!(process
= create_process( socket_fd
, parent
, req
->flags
, info
->data
, sd
,
1295 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1298 process
->machine
= req
->machine
;
1299 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1304 if (!(job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)
1305 && !(req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
1306 && job
->limit_flags
& JOB_OBJECT_LIMIT_BREAKAWAY_OK
))
1308 add_job_process( job
, process
);
1309 assert( !get_error() );
1315 for (i
= 0; i
< job_handle_count
; ++i
)
1317 job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
);
1318 add_job_process( job
, process
);
1319 release_object( job
);
1322 release_job_process( process
);
1327 /* connect to the window station */
1328 connect_process_winstation( process
, &desktop_path
, parent_thread
, parent
);
1330 /* inherit the process console, but keep pseudo handles (< 0), and 0 (= not attached to a console) as is */
1331 if ((int)info
->data
->console
> 0)
1332 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1333 0, 0, DUPLICATE_SAME_ACCESS
);
1335 if (!(req
->flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
) && info
->data
->console
!= 1)
1337 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1338 0, 0, DUPLICATE_SAME_ACCESS
| DUPLICATE_SAME_ATTRIBUTES
);
1339 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1340 0, 0, DUPLICATE_SAME_ACCESS
| DUPLICATE_SAME_ATTRIBUTES
);
1341 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1342 0, 0, DUPLICATE_SAME_ACCESS
| DUPLICATE_SAME_ATTRIBUTES
);
1343 /* some handles above may have been invalid; this is not an error */
1344 if (get_error() == STATUS_INVALID_HANDLE
||
1345 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1348 /* attach to the debugger */
1351 process
->debug_obj
= debug_obj
;
1352 process
->debug_children
= !(req
->flags
& PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT
);
1354 else if (parent
->debug_children
)
1356 process
->debug_obj
= parent
->debug_obj
;
1357 /* debug_children is set to 1 by default */
1360 if (info
->data
->process_group_id
== parent
->group_id
)
1361 process
->group_id
= parent
->group_id
;
1363 info
->data
->process_group_id
= process
->group_id
;
1365 info
->process
= (struct process
*)grab_object( process
);
1366 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1367 reply
->pid
= get_process_id( process
);
1368 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1371 if (process
) release_object( process
);
1372 if (debug_obj
) release_object( debug_obj
);
1373 if (token
) release_object( token
);
1374 release_object( parent
);
1375 release_object( info
);
1378 /* Retrieve information about a newly started process */
1379 DECL_HANDLER(get_new_process_info
)
1381 struct startup_info
*info
;
1383 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1384 0, &startup_info_ops
)))
1386 reply
->success
= is_process_init_done( info
->process
);
1387 reply
->exit_code
= info
->process
->exit_code
;
1388 release_object( info
);
1392 /* Retrieve the new process startup info */
1393 DECL_HANDLER(get_startup_info
)
1395 struct process
*process
= current
->process
;
1396 struct startup_info
*info
= process
->startup_info
;
1401 /* we return the data directly without making a copy so this can only be called once */
1402 reply
->machine
= process
->machine
;
1403 reply
->info_size
= info
->info_size
;
1404 size
= info
->data_size
;
1405 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1406 set_reply_data_ptr( info
->data
, size
);
1408 info
->data_size
= 0;
1411 /* signal the end of the process initialization */
1412 DECL_HANDLER(init_process_done
)
1414 struct process
*process
= current
->process
;
1416 if (is_process_init_done(process
))
1418 set_error( STATUS_INVALID_PARAMETER
);
1422 current
->teb
= req
->teb
;
1423 process
->peb
= req
->peb
;
1424 process
->ldt_copy
= req
->ldt_copy
;
1426 process
->start_time
= current_time
;
1428 init_process_tracing( process
);
1429 generate_startup_debug_events( process
);
1430 set_process_startup_state( process
, STARTUP_DONE
);
1432 if (process
->image_info
.subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1433 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1434 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1435 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1438 /* open a handle to a process */
1439 DECL_HANDLER(open_process
)
1441 struct process
*process
= get_process_from_id( req
->pid
);
1445 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1446 release_object( process
);
1450 /* terminate a process */
1451 DECL_HANDLER(terminate_process
)
1453 struct process
*process
;
1457 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1458 if (!process
) return;
1460 else process
= (struct process
*)grab_object( current
->process
);
1462 reply
->self
= (current
->process
== process
);
1463 terminate_process( process
, current
, req
->exit_code
);
1464 release_object( process
);
1467 /* fetch information about a process */
1468 DECL_HANDLER(get_process_info
)
1470 struct process
*process
;
1472 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1474 reply
->pid
= get_process_id( process
);
1475 reply
->ppid
= process
->parent_id
;
1476 reply
->exit_code
= process
->exit_code
;
1477 reply
->priority
= process
->priority
;
1478 reply
->affinity
= process
->affinity
;
1479 reply
->peb
= process
->peb
;
1480 reply
->start_time
= process
->start_time
;
1481 reply
->end_time
= process
->end_time
;
1482 reply
->session_id
= process
->session_id
;
1483 reply
->machine
= process
->machine
;
1484 if (get_reply_max_size())
1486 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1487 else set_reply_data( &process
->image_info
, min( sizeof(process
->image_info
), get_reply_max_size() ));
1489 release_object( process
);
1493 /* retrieve debug information about a process */
1494 DECL_HANDLER(get_process_debug_info
)
1496 struct process
*process
;
1498 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1500 reply
->debug_children
= process
->debug_children
;
1501 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1502 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, MAXIMUM_ALLOWED
, 0 );
1503 release_object( process
);
1506 /* fetch the name of the process image */
1507 DECL_HANDLER(get_process_image_name
)
1509 struct process
*process
;
1511 if (req
->pid
) process
= get_process_from_id( req
->pid
);
1512 else process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1514 if (!process
) return;
1517 struct unicode_str name
= { process
->image
, process
->imagelen
};
1518 /* skip the \??\ prefix */
1519 if (req
->win32
&& name
.len
> 6 * sizeof(WCHAR
) && name
.str
[5] == ':')
1522 name
.len
-= 4 * sizeof(WCHAR
);
1524 /* FIXME: else resolve symlinks in NT path */
1526 reply
->len
= name
.len
;
1527 if (name
.len
<= get_reply_max_size())
1529 WCHAR
*ptr
= set_reply_data( name
.str
, name
.len
);
1530 /* change \??\ to \\?\ */
1531 if (req
->win32
&& name
.len
> sizeof(WCHAR
) && ptr
[1] == '?') ptr
[1] = '\\';
1533 else set_error( STATUS_BUFFER_TOO_SMALL
);
1535 else set_error( STATUS_INVALID_HANDLE
);
1536 release_object( process
);
1539 /* retrieve information about a process memory usage */
1540 DECL_HANDLER(get_process_vm_counters
)
1542 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1544 if (!process
) return;
1545 if (process
->unix_pid
!= -1)
1549 char proc_path
[32], line
[256];
1550 unsigned long value
;
1552 snprintf( proc_path
, sizeof(proc_path
), "/proc/%u/status", process
->unix_pid
);
1553 if ((f
= fopen( proc_path
, "r" )))
1555 while (fgets( line
, sizeof(line
), f
))
1557 if (sscanf( line
, "VmPeak: %lu", &value
))
1558 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1559 else if (sscanf( line
, "VmSize: %lu", &value
))
1560 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1561 else if (sscanf( line
, "VmHWM: %lu", &value
))
1562 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1563 else if (sscanf( line
, "VmRSS: %lu", &value
))
1564 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1565 else if (sscanf( line
, "RssAnon: %lu", &value
))
1566 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1567 else if (sscanf( line
, "VmSwap: %lu", &value
))
1568 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1570 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1573 else set_error( STATUS_ACCESS_DENIED
);
1574 #elif defined(HAVE_LIBPROCSTAT)
1575 struct procstat
*procstat
;
1578 if ((procstat
= procstat_open_sysctl()))
1580 struct kinfo_proc
*kp
= procstat_getprocs( procstat
, KERN_PROC_PID
, process
->unix_pid
, &count
);
1583 reply
->virtual_size
= kp
->ki_size
;
1584 reply
->peak_virtual_size
= reply
->virtual_size
;
1585 reply
->working_set_size
= kp
->ki_rssize
<< PAGE_SHIFT
;
1586 reply
->peak_working_set_size
= kp
->ki_rusage
.ru_maxrss
* 1024;
1587 procstat_freeprocs( procstat
, kp
);
1589 else set_error( STATUS_ACCESS_DENIED
);
1590 procstat_close( procstat
);
1592 else set_error( STATUS_ACCESS_DENIED
);
1595 else set_error( STATUS_ACCESS_DENIED
);
1596 release_object( process
);
1599 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1601 struct thread
*thread
;
1603 if (!process
->running_threads
)
1605 set_error( STATUS_PROCESS_IS_TERMINATING
);
1609 process
->affinity
= affinity
;
1611 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1613 set_thread_affinity( thread
, affinity
);
1617 /* set information about a process */
1618 DECL_HANDLER(set_process_info
)
1620 struct process
*process
;
1622 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1624 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1625 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1626 if (req
->mask
& SET_PROCESS_INFO_TOKEN
)
1628 struct token
*token
;
1630 if ((token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1632 release_object( process
->token
);
1633 process
->token
= token
;
1636 release_object( process
);
1640 /* read data from a process address space */
1641 DECL_HANDLER(read_process_memory
)
1643 struct process
*process
;
1644 data_size_t len
= get_reply_max_size();
1646 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1650 char *buffer
= mem_alloc( len
);
1653 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1654 set_reply_data_ptr( buffer
, len
);
1659 release_object( process
);
1662 /* write data to a process address space */
1663 DECL_HANDLER(write_process_memory
)
1665 struct process
*process
;
1667 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1669 data_size_t len
= get_req_data_size();
1670 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1671 release_object( process
);
1675 /* retrieve the process idle event */
1676 DECL_HANDLER(get_process_idle_event
)
1678 struct process
*process
;
1681 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1683 if (process
->idle_event
)
1684 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1685 EVENT_ALL_ACCESS
, 0 );
1686 release_object( process
);
1690 /* make the current process a system process */
1691 DECL_HANDLER(make_process_system
)
1693 struct process
*process
;
1694 struct thread
*thread
;
1696 if (!shutdown_event
)
1698 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1699 release_object( shutdown_event
);
1702 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
))) return;
1704 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1706 release_object( process
);
1710 if (!process
->is_system
)
1712 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1713 release_thread_desktop( thread
, 0 );
1714 process
->is_system
= 1;
1715 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1716 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1718 release_object( process
);
1721 /* create a new job object */
1722 DECL_HANDLER(create_job
)
1725 struct unicode_str name
;
1726 struct object
*root
;
1727 const struct security_descriptor
*sd
;
1728 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1730 if (!objattr
) return;
1732 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1734 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1735 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1737 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1738 req
->access
, objattr
->attributes
);
1739 release_object( job
);
1741 if (root
) release_object( root
);
1744 /* open a job object */
1745 DECL_HANDLER(open_job
)
1747 struct unicode_str name
= get_req_unicode_str();
1749 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1750 &job_ops
, &name
, req
->attributes
);
1753 /* assign a job object to a process */
1754 DECL_HANDLER(assign_job
)
1756 struct process
*process
;
1757 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1761 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1763 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1764 else add_job_process( job
, process
);
1765 release_object( process
);
1767 release_object( job
);
1771 /* check if a process is associated with a job */
1772 DECL_HANDLER(process_in_job
)
1774 struct process
*process
;
1777 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1782 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1784 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1786 set_error( process_in_job( job
, process
) ? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1787 release_object( job
);
1789 release_object( process
);
1792 /* retrieve information about a job */
1793 DECL_HANDLER(get_job_info
)
1795 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1801 reply
->total_processes
= job
->total_processes
;
1802 reply
->active_processes
= job
->num_processes
;
1804 len
= min( get_reply_max_size(), reply
->active_processes
* sizeof(*pids
) );
1805 if (len
&& ((pids
= set_reply_data_size( len
))))
1806 get_job_pids( job
, pids
, pids
+ len
/ sizeof(*pids
) );
1808 release_object( job
);
1811 /* terminate all processes associated with the job */
1812 DECL_HANDLER(terminate_job
)
1814 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1818 terminate_job( job
, req
->status
);
1819 release_object( job
);
1822 /* update limits of the job object */
1823 DECL_HANDLER(set_job_limits
)
1825 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1829 job
->limit_flags
= req
->limit_flags
;
1830 release_object( job
);
1833 /* set the jobs completion port */
1834 DECL_HANDLER(set_job_completion_port
)
1836 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1840 if (!job
->completion_port
)
1842 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1843 job
->completion_key
= req
->key
;
1845 add_job_completion_existing_processes( job
, job
);
1848 set_error( STATUS_INVALID_PARAMETER
);
1850 release_object( job
);
1853 /* Suspend a process */
1854 DECL_HANDLER(suspend_process
)
1856 struct process
*process
;
1858 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1860 struct list
*ptr
, *next
;
1862 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1864 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1865 suspend_thread( thread
);
1868 release_object( process
);
1872 /* Resume a process */
1873 DECL_HANDLER(resume_process
)
1875 struct process
*process
;
1877 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1879 struct list
*ptr
, *next
;
1881 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1883 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1884 resume_thread( thread
);
1887 release_object( process
);
1891 /* Get a list of processes and threads currently running */
1892 DECL_HANDLER(list_processes
)
1894 struct process
*process
;
1895 struct thread
*thread
;
1896 unsigned int pos
= 0;
1899 reply
->process_count
= 0;
1900 reply
->total_thread_count
= 0;
1901 reply
->total_name_len
= 0;
1902 reply
->info_size
= 0;
1904 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1906 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1907 reply
->info_size
+= sizeof(struct process_info
) + process
->imagelen
;
1908 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1909 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1910 reply
->process_count
++;
1911 reply
->total_thread_count
+= process
->running_threads
;
1912 reply
->total_name_len
+= process
->imagelen
;
1915 if (reply
->info_size
> get_reply_max_size())
1917 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1921 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1923 memset( buffer
, 0, reply
->info_size
);
1924 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1926 struct process_info
*process_info
;
1928 pos
= (pos
+ 7) & ~7;
1929 process_info
= (struct process_info
*)(buffer
+ pos
);
1930 process_info
->start_time
= process
->start_time
;
1931 process_info
->name_len
= process
->imagelen
;
1932 process_info
->thread_count
= process
->running_threads
;
1933 process_info
->priority
= process
->priority
;
1934 process_info
->pid
= process
->id
;
1935 process_info
->parent_pid
= process
->parent_id
;
1936 process_info
->session_id
= process
->session_id
;
1937 process_info
->handle_count
= get_handle_table_count(process
);
1938 process_info
->unix_pid
= process
->unix_pid
;
1939 pos
+= sizeof(*process_info
);
1940 memcpy( buffer
+ pos
, process
->image
, process
->imagelen
);
1941 pos
+= process
->imagelen
;
1942 pos
= (pos
+ 7) & ~7;
1943 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1945 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1947 thread_info
->start_time
= thread
->creation_time
;
1948 thread_info
->tid
= thread
->id
;
1949 thread_info
->base_priority
= thread
->priority
;
1950 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1951 thread_info
->unix_tid
= thread
->unix_tid
;
1952 thread_info
->entry_point
= thread
->entry_point
;
1953 thread_info
->teb
= thread
->teb
;
1954 pos
+= sizeof(*thread_info
);