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
->unix_pid
= -1;
660 process
->exit_code
= STILL_ACTIVE
;
661 process
->running_threads
= 0;
662 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
663 process
->suspend
= 0;
664 process
->is_system
= 0;
665 process
->debug_children
= 1;
666 process
->is_terminating
= 0;
667 process
->imagelen
= 0;
668 process
->image
= NULL
;
670 process
->console
= NULL
;
671 process
->startup_state
= STARTUP_IN_PROGRESS
;
672 process
->startup_info
= NULL
;
673 process
->idle_event
= NULL
;
675 process
->ldt_copy
= 0;
676 process
->dir_cache
= NULL
;
677 process
->winstation
= 0;
678 process
->desktop
= 0;
679 process
->token
= NULL
;
680 process
->trace_data
= 0;
681 process
->rawinput_devices
= NULL
;
682 process
->rawinput_device_count
= 0;
683 process
->rawinput_mouse
= NULL
;
684 process
->rawinput_kbd
= NULL
;
685 memset( &process
->image_info
, 0, sizeof(process
->image_info
) );
686 list_init( &process
->kernel_object
);
687 list_init( &process
->thread_list
);
688 list_init( &process
->locks
);
689 list_init( &process
->asyncs
);
690 list_init( &process
->classes
);
691 list_init( &process
->views
);
693 process
->end_time
= 0;
695 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
696 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
701 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
706 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
708 /* create the handle table */
711 process
->handles
= alloc_handle_table( process
, 0 );
712 process
->token
= token_create_admin( TRUE
, -1, TokenElevationTypeFull
, default_session_id
);
713 process
->affinity
= ~0;
717 obj_handle_t std_handles
[3];
719 std_handles
[0] = info
->hstdin
;
720 std_handles
[1] = info
->hstdout
;
721 std_handles
[2] = info
->hstderr
;
723 process
->parent_id
= parent
->id
;
724 if (flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
)
725 process
->handles
= copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
);
727 process
->handles
= alloc_handle_table( process
, 0 );
728 /* Note: for security reasons, starting a new process does not attempt
729 * to use the current impersonation token for the new process */
730 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
731 process
->affinity
= parent
->affinity
;
733 if (!process
->handles
|| !process
->token
) goto error
;
734 process
->session_id
= token_get_session_id( process
->token
);
736 /* Assign a high security label to the token. The default would be medium
737 * but Wine provides admin access to all applications right now so high
738 * makes more sense for the time being. */
739 if (!token_assign_label( process
->token
, &high_label_sid
))
742 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
746 if (process
) release_object( process
);
747 /* if we failed to start our first process, close everything down */
748 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
752 /* get the process data size */
753 data_size_t
get_process_startup_info_size( struct process
*process
)
755 struct startup_info
*info
= process
->startup_info
;
758 return info
->data_size
;
761 /* destroy a process when its refcount is 0 */
762 static void process_destroy( struct object
*obj
)
764 struct process
*process
= (struct process
*)obj
;
765 assert( obj
->ops
== &process_ops
);
767 /* we can't have a thread remaining */
768 assert( list_empty( &process
->thread_list
));
769 assert( list_empty( &process
->asyncs
));
771 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
773 close_process_handles( process
);
774 set_process_startup_state( process
, STARTUP_ABORTED
);
778 list_remove( &process
->job_entry
);
779 release_object( process
->job
);
781 if (process
->console
) release_object( process
->console
);
782 if (process
->msg_fd
) release_object( process
->msg_fd
);
783 if (process
->idle_event
) release_object( process
->idle_event
);
784 if (process
->id
) free_ptid( process
->id
);
785 if (process
->token
) release_object( process
->token
);
786 free( process
->rawinput_devices
);
787 free( process
->dir_cache
);
788 free( process
->image
);
791 /* dump a process on stdout for debugging purposes */
792 static void process_dump( struct object
*obj
, int verbose
)
794 struct process
*process
= (struct process
*)obj
;
795 assert( obj
->ops
== &process_ops
);
797 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
800 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
802 struct process
*process
= (struct process
*)obj
;
803 return !process
->running_threads
;
806 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
808 access
= default_map_access( obj
, access
);
809 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
810 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
814 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
816 struct process
*process
= (struct process
*)obj
;
817 return &process
->kernel_object
;
820 static struct security_descriptor
*process_get_sd( struct object
*obj
)
822 static struct security_descriptor
*process_default_sd
;
824 if (obj
->sd
) return obj
->sd
;
826 if (!process_default_sd
)
831 size_t users_sid_len
= sid_len( &domain_users_sid
);
832 size_t admins_sid_len
= sid_len( &builtin_admins_sid
);
833 size_t dacl_len
= sizeof(*dacl
) + 2 * sizeof(*ace
) + users_sid_len
+ admins_sid_len
;
835 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
837 process_default_sd
->control
= SE_DACL_PRESENT
;
838 process_default_sd
->owner_len
= admins_sid_len
;
839 process_default_sd
->group_len
= users_sid_len
;
840 process_default_sd
->sacl_len
= 0;
841 process_default_sd
->dacl_len
= dacl_len
;
842 sid
= (struct sid
*)(process_default_sd
+ 1);
843 sid
= copy_sid( sid
, &builtin_admins_sid
);
844 sid
= copy_sid( sid
, &domain_users_sid
);
846 dacl
= (struct acl
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
847 dacl
->revision
= ACL_REVISION
;
849 dacl
->size
= dacl_len
;
852 ace
= set_ace( ace_first( dacl
), &domain_users_sid
, ACCESS_ALLOWED_ACE_TYPE
,
853 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
, GENERIC_READ
);
854 set_ace( ace_next( ace
), &builtin_admins_sid
, ACCESS_ALLOWED_ACE_TYPE
, 0, PROCESS_ALL_ACCESS
);
856 return process_default_sd
;
859 static void process_poll_event( struct fd
*fd
, int event
)
861 struct process
*process
= get_fd_user( fd
);
862 assert( process
->obj
.ops
== &process_ops
);
864 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
865 else if (event
& POLLIN
) receive_fd( process
);
868 static void startup_info_destroy( struct object
*obj
)
870 struct startup_info
*info
= (struct startup_info
*)obj
;
871 assert( obj
->ops
== &startup_info_ops
);
873 if (info
->process
) release_object( info
->process
);
876 static void startup_info_dump( struct object
*obj
, int verbose
)
878 struct startup_info
*info
= (struct startup_info
*)obj
;
879 assert( obj
->ops
== &startup_info_ops
);
881 fputs( "Startup info", stderr
);
883 fprintf( stderr
, " in=%04x out=%04x err=%04x",
884 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
885 fputc( '\n', stderr
);
888 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
890 struct startup_info
*info
= (struct startup_info
*)obj
;
891 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
894 /* get a process from an id (and increment the refcount) */
895 struct process
*get_process_from_id( process_id_t id
)
897 struct object
*obj
= get_ptid_entry( id
);
899 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
900 set_error( STATUS_INVALID_CID
);
904 /* get a process from a handle (and increment the refcount) */
905 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
907 return (struct process
*)get_handle_obj( current
->process
, handle
,
908 access
, &process_ops
);
911 /* terminate a process with the given exit code */
912 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
914 struct thread
*thread
;
916 grab_object( process
); /* make sure it doesn't get freed when threads die */
917 process
->is_terminating
= 1;
920 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
922 if (exit_code
) thread
->exit_code
= exit_code
;
923 if (thread
== skip
) continue;
924 if (thread
->state
== TERMINATED
) continue;
925 kill_thread( thread
, 1 );
928 release_object( process
);
931 /* kill all processes */
932 static void kill_all_processes(void)
936 while ((ptr
= list_head( &process_list
)))
938 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
939 terminate_process( process
, NULL
, 1 );
943 /* kill all processes being attached to a console renderer */
944 void kill_console_processes( struct thread
*renderer
, int exit_code
)
946 for (;;) /* restart from the beginning of the list every time */
948 struct process
*process
;
950 /* find the first process being attached to 'renderer' and still running */
951 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
953 if (process
== renderer
->process
) continue;
954 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
956 if (&process
->entry
== &process_list
) break; /* no process found */
957 terminate_process( process
, NULL
, exit_code
);
961 /* a process has been killed (i.e. its last thread died) */
962 static void process_killed( struct process
*process
)
964 assert( list_empty( &process
->thread_list
));
965 process
->end_time
= current_time
;
966 close_process_desktop( process
);
967 process
->winstation
= 0;
968 process
->desktop
= 0;
969 cancel_process_asyncs( process
);
970 close_process_handles( process
);
971 if (process
->idle_event
) release_object( process
->idle_event
);
972 process
->idle_event
= NULL
;
973 assert( !process
->console
);
975 destroy_process_classes( process
);
976 free_mapped_views( process
);
977 free_process_user_handles( process
);
978 remove_process_locks( process
);
979 set_process_startup_state( process
, STARTUP_ABORTED
);
980 finish_process_tracing( process
);
981 release_job_process( process
);
982 start_sigkill_timer( process
);
983 wake_up( &process
->obj
, 0 );
986 /* add a thread to a process running threads list */
987 void add_process_thread( struct process
*process
, struct thread
*thread
)
989 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
990 if (!process
->running_threads
++)
992 list_add_tail( &process_list
, &process
->entry
);
994 if (!process
->is_system
)
996 if (!user_processes
++ && shutdown_timeout
)
998 remove_timeout_user( shutdown_timeout
);
999 shutdown_timeout
= NULL
;
1003 grab_object( thread
);
1006 /* remove a thread from a process running threads list */
1007 void remove_process_thread( struct process
*process
, struct thread
*thread
)
1009 assert( process
->running_threads
> 0 );
1010 assert( !list_empty( &process
->thread_list
));
1012 list_remove( &thread
->proc_entry
);
1014 if (!--process
->running_threads
)
1016 /* we have removed the last running thread, exit the process */
1017 process
->exit_code
= thread
->exit_code
;
1018 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
1019 list_remove( &process
->entry
);
1020 process_killed( process
);
1022 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
1023 release_object( thread
);
1026 /* suspend all the threads of a process */
1027 void suspend_process( struct process
*process
)
1029 if (!process
->suspend
++)
1031 struct list
*ptr
, *next
;
1033 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1035 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1036 if (!thread
->suspend
) stop_thread( thread
);
1041 /* resume all the threads of a process */
1042 void resume_process( struct process
*process
)
1044 assert (process
->suspend
> 0);
1045 if (!--process
->suspend
)
1047 struct list
*ptr
, *next
;
1049 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1051 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1052 if (!thread
->suspend
) wake_thread( thread
);
1057 /* kill a process on the spot */
1058 void kill_process( struct process
*process
, int violent_death
)
1060 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1062 release_object( process
->msg_fd
);
1063 process
->msg_fd
= NULL
;
1066 if (process
->sigkill_timeout
) return; /* already waiting for it to die */
1068 if (violent_death
) terminate_process( process
, NULL
, 1 );
1073 grab_object( process
); /* make sure it doesn't get freed when threads die */
1074 while ((ptr
= list_head( &process
->thread_list
)))
1076 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1077 kill_thread( thread
, 0 );
1079 release_object( process
);
1083 /* detach all processes being debugged by a given thread */
1084 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1086 for (;;) /* restart from the beginning of the list every time */
1088 struct process
*process
;
1090 /* find the first process being debugged by 'debugger' and still running */
1091 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1092 if (process
->debug_obj
== debug_obj
) break;
1094 if (&process
->entry
== &process_list
) break; /* no process found */
1097 process
->debug_obj
= NULL
;
1098 terminate_process( process
, NULL
, exit_code
);
1100 else debugger_detach( process
, debug_obj
);
1105 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1107 struct list
*ptr
, *next
;
1109 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1111 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1112 if ((cb
)(process
, user
)) break;
1116 /* set the debugged flag in the process PEB */
1117 int set_process_debug_flag( struct process
*process
, int flag
)
1119 char data
= (flag
!= 0);
1120 client_ptr_t peb32
= 0;
1122 if (!is_machine_64bit( process
->machine
) && is_machine_64bit( native_machine
))
1123 peb32
= process
->peb
+ 0x1000;
1125 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1126 if (peb32
&& !write_process_memory( process
, peb32
+ 2, 1, &data
)) return 0;
1127 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1130 /* create a new process */
1131 DECL_HANDLER(new_process
)
1133 struct startup_info
*info
;
1134 const void *info_ptr
;
1135 struct unicode_str name
;
1136 const struct security_descriptor
*sd
;
1137 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1138 struct process
*process
= NULL
;
1139 struct token
*token
= NULL
;
1140 struct debug_obj
*debug_obj
= NULL
;
1141 struct process
*parent
;
1142 struct thread
*parent_thread
= current
;
1143 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1144 const obj_handle_t
*handles
= NULL
;
1145 const obj_handle_t
*job_handles
= NULL
;
1146 unsigned int i
, job_handle_count
;
1149 if (socket_fd
== -1)
1151 set_error( STATUS_INVALID_PARAMETER
);
1156 set_error( STATUS_INVALID_PARAMETER
);
1160 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1162 set_error( STATUS_INVALID_HANDLE
);
1168 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1173 if (req
->parent_process
)
1175 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1180 parent_thread
= NULL
;
1182 else parent
= (struct process
*)grab_object( current
->process
);
1184 /* If a job further in the job chain does not permit breakaway process creation
1185 * succeeds and the process which is trying to breakaway is assigned to that job. */
1186 if (parent
->job
&& (req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
) &&
1187 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1189 set_error( STATUS_ACCESS_DENIED
);
1191 release_object( parent
);
1195 /* build the startup info for a new process */
1196 if (!(info
= alloc_object( &startup_info_ops
)))
1199 release_object( parent
);
1202 info
->process
= NULL
;
1205 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1207 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1209 set_error( STATUS_INVALID_PARAMETER
);
1213 if (req
->handles_size
)
1216 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1217 info
->data_size
-= req
->handles_size
;
1220 if ((req
->jobs_size
& 3) || req
->jobs_size
> info
->data_size
)
1222 set_error( STATUS_INVALID_PARAMETER
);
1228 job_handles
= info_ptr
;
1229 info_ptr
= (const char *)info_ptr
+ req
->jobs_size
;
1230 info
->data_size
-= req
->jobs_size
;
1233 job_handle_count
= req
->jobs_size
/ sizeof(*handles
);
1234 for (i
= 0; i
< job_handle_count
; ++i
)
1236 if (!(job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
)))
1241 release_object( job
);
1244 info
->info_size
= min( req
->info_size
, info
->data_size
);
1246 if (req
->info_size
< sizeof(*info
->data
))
1248 /* make sure we have a full startup_info_t structure */
1249 data_size_t env_size
= info
->data_size
- info
->info_size
;
1250 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1252 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1257 memcpy( info
->data
, info_ptr
, info_size
);
1258 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1259 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1260 info
->info_size
= sizeof(startup_info_t
);
1261 info
->data_size
= info
->info_size
+ env_size
;
1265 data_size_t pos
= sizeof(*info
->data
);
1267 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1272 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1273 FIXUP_LEN( info
->data
->curdir_len
);
1274 FIXUP_LEN( info
->data
->dllpath_len
);
1275 FIXUP_LEN( info
->data
->imagepath_len
);
1276 FIXUP_LEN( info
->data
->cmdline_len
);
1277 FIXUP_LEN( info
->data
->title_len
);
1278 FIXUP_LEN( info
->data
->desktop_len
);
1279 FIXUP_LEN( info
->data
->shellinfo_len
);
1280 FIXUP_LEN( info
->data
->runtime_len
);
1284 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1289 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1295 if (!(process
= create_process( socket_fd
, parent
, req
->flags
, info
->data
, sd
,
1296 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1299 process
->machine
= req
->machine
;
1300 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1305 if (!(job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)
1306 && !(req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
1307 && job
->limit_flags
& JOB_OBJECT_LIMIT_BREAKAWAY_OK
))
1309 add_job_process( job
, process
);
1310 assert( !get_error() );
1316 for (i
= 0; i
< job_handle_count
; ++i
)
1318 job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
);
1319 add_job_process( job
, process
);
1320 release_object( job
);
1323 release_job_process( process
);
1328 /* connect to the window station */
1329 connect_process_winstation( process
, parent_thread
, parent
);
1331 /* inherit the process console, but keep pseudo handles (< 0), and 0 (= not attached to a console) as is */
1332 if ((int)info
->data
->console
> 0)
1333 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1334 0, 0, DUPLICATE_SAME_ACCESS
);
1336 if (!(req
->flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
) && info
->data
->console
!= 1)
1338 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1339 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1340 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1341 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1342 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1343 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1344 /* some handles above may have been invalid; this is not an error */
1345 if (get_error() == STATUS_INVALID_HANDLE
||
1346 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1349 /* attach to the debugger */
1352 process
->debug_obj
= debug_obj
;
1353 process
->debug_children
= !(req
->flags
& PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT
);
1355 else if (parent
->debug_children
)
1357 process
->debug_obj
= parent
->debug_obj
;
1358 /* debug_children is set to 1 by default */
1361 if (!info
->data
->console_flags
) process
->group_id
= parent
->group_id
;
1363 info
->process
= (struct process
*)grab_object( process
);
1364 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1365 reply
->pid
= get_process_id( process
);
1366 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1369 if (process
) release_object( process
);
1370 if (debug_obj
) release_object( debug_obj
);
1371 if (token
) release_object( token
);
1372 release_object( parent
);
1373 release_object( info
);
1376 /* Retrieve information about a newly started process */
1377 DECL_HANDLER(get_new_process_info
)
1379 struct startup_info
*info
;
1381 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1382 0, &startup_info_ops
)))
1384 reply
->success
= is_process_init_done( info
->process
);
1385 reply
->exit_code
= info
->process
->exit_code
;
1386 release_object( info
);
1390 /* Retrieve the new process startup info */
1391 DECL_HANDLER(get_startup_info
)
1393 struct process
*process
= current
->process
;
1394 struct startup_info
*info
= process
->startup_info
;
1399 /* we return the data directly without making a copy so this can only be called once */
1400 reply
->machine
= process
->machine
;
1401 reply
->info_size
= info
->info_size
;
1402 size
= info
->data_size
;
1403 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1404 set_reply_data_ptr( info
->data
, size
);
1406 info
->data_size
= 0;
1409 /* signal the end of the process initialization */
1410 DECL_HANDLER(init_process_done
)
1412 struct process
*process
= current
->process
;
1413 struct memory_view
*view
;
1415 const pe_image_info_t
*image_info
;
1417 if (is_process_init_done(process
))
1419 set_error( STATUS_INVALID_PARAMETER
);
1422 if (!(view
= get_exe_view( process
)))
1424 set_error( STATUS_DLL_NOT_FOUND
);
1427 if (!(image_info
= get_view_image_info( view
, &base
))) return;
1429 current
->teb
= req
->teb
;
1430 process
->peb
= req
->peb
;
1431 process
->ldt_copy
= req
->ldt_copy
;
1433 process
->start_time
= current_time
;
1434 current
->entry_point
= base
+ image_info
->entry_point
;
1436 init_process_tracing( process
);
1437 generate_startup_debug_events( process
);
1438 set_process_startup_state( process
, STARTUP_DONE
);
1440 if (image_info
->subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1441 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1442 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1443 reply
->entry
= current
->entry_point
;
1444 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1447 /* open a handle to a process */
1448 DECL_HANDLER(open_process
)
1450 struct process
*process
= get_process_from_id( req
->pid
);
1454 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1455 release_object( process
);
1459 /* terminate a process */
1460 DECL_HANDLER(terminate_process
)
1462 struct process
*process
;
1466 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1467 if (!process
) return;
1469 else process
= (struct process
*)grab_object( current
->process
);
1471 reply
->self
= (current
->process
== process
);
1472 terminate_process( process
, current
, req
->exit_code
);
1473 release_object( process
);
1476 /* fetch information about a process */
1477 DECL_HANDLER(get_process_info
)
1479 struct process
*process
;
1481 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1483 reply
->pid
= get_process_id( process
);
1484 reply
->ppid
= process
->parent_id
;
1485 reply
->exit_code
= process
->exit_code
;
1486 reply
->priority
= process
->priority
;
1487 reply
->affinity
= process
->affinity
;
1488 reply
->peb
= process
->peb
;
1489 reply
->start_time
= process
->start_time
;
1490 reply
->end_time
= process
->end_time
;
1491 reply
->session_id
= process
->session_id
;
1492 reply
->machine
= process
->machine
;
1493 if (get_reply_max_size())
1495 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1496 else set_reply_data( &process
->image_info
, min( sizeof(process
->image_info
), get_reply_max_size() ));
1498 release_object( process
);
1502 /* retrieve debug information about a process */
1503 DECL_HANDLER(get_process_debug_info
)
1505 struct process
*process
;
1507 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1509 reply
->debug_children
= process
->debug_children
;
1510 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1511 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, MAXIMUM_ALLOWED
, 0 );
1512 release_object( process
);
1515 /* fetch the name of the process image */
1516 DECL_HANDLER(get_process_image_name
)
1518 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1520 if (!process
) return;
1523 struct unicode_str name
= { process
->image
, process
->imagelen
};
1524 /* skip the \??\ prefix */
1525 if (req
->win32
&& name
.len
> 6 * sizeof(WCHAR
) && name
.str
[5] == ':')
1528 name
.len
-= 4 * sizeof(WCHAR
);
1530 /* FIXME: else resolve symlinks in NT path */
1532 reply
->len
= name
.len
;
1533 if (name
.len
<= get_reply_max_size())
1535 WCHAR
*ptr
= set_reply_data( name
.str
, name
.len
);
1536 /* change \??\ to \\?\ */
1537 if (req
->win32
&& name
.len
> sizeof(WCHAR
) && ptr
[1] == '?') ptr
[1] = '\\';
1539 else set_error( STATUS_BUFFER_TOO_SMALL
);
1541 else set_error( STATUS_INVALID_HANDLE
);
1542 release_object( process
);
1545 /* retrieve information about a process memory usage */
1546 DECL_HANDLER(get_process_vm_counters
)
1548 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1550 if (!process
) return;
1551 if (process
->unix_pid
!= -1)
1555 char proc_path
[32], line
[256];
1556 unsigned long value
;
1558 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1559 if ((f
= fopen( proc_path
, "r" )))
1561 while (fgets( line
, sizeof(line
), f
))
1563 if (sscanf( line
, "VmPeak: %lu", &value
))
1564 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1565 else if (sscanf( line
, "VmSize: %lu", &value
))
1566 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1567 else if (sscanf( line
, "VmHWM: %lu", &value
))
1568 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1569 else if (sscanf( line
, "VmRSS: %lu", &value
))
1570 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1571 else if (sscanf( line
, "RssAnon: %lu", &value
))
1572 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1573 else if (sscanf( line
, "VmSwap: %lu", &value
))
1574 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1576 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1579 else set_error( STATUS_ACCESS_DENIED
);
1580 #elif defined(HAVE_LIBPROCSTAT)
1581 struct procstat
*procstat
;
1584 if ((procstat
= procstat_open_sysctl()))
1586 struct kinfo_proc
*kp
= procstat_getprocs( procstat
, KERN_PROC_PID
, process
->unix_pid
, &count
);
1589 reply
->virtual_size
= kp
->ki_size
;
1590 reply
->peak_virtual_size
= reply
->virtual_size
;
1591 reply
->working_set_size
= kp
->ki_rssize
<< PAGE_SHIFT
;
1592 reply
->peak_working_set_size
= kp
->ki_rusage
.ru_maxrss
* 1024;
1593 procstat_freeprocs( procstat
, kp
);
1595 else set_error( STATUS_ACCESS_DENIED
);
1596 procstat_close( procstat
);
1598 else set_error( STATUS_ACCESS_DENIED
);
1601 else set_error( STATUS_ACCESS_DENIED
);
1602 release_object( process
);
1605 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1607 struct thread
*thread
;
1609 if (!process
->running_threads
)
1611 set_error( STATUS_PROCESS_IS_TERMINATING
);
1615 process
->affinity
= affinity
;
1617 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1619 set_thread_affinity( thread
, affinity
);
1623 /* set information about a process */
1624 DECL_HANDLER(set_process_info
)
1626 struct process
*process
;
1628 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1630 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1631 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1632 release_object( process
);
1636 /* read data from a process address space */
1637 DECL_HANDLER(read_process_memory
)
1639 struct process
*process
;
1640 data_size_t len
= get_reply_max_size();
1642 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1646 char *buffer
= mem_alloc( len
);
1649 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1650 set_reply_data_ptr( buffer
, len
);
1655 release_object( process
);
1658 /* write data to a process address space */
1659 DECL_HANDLER(write_process_memory
)
1661 struct process
*process
;
1663 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1665 data_size_t len
= get_req_data_size();
1666 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1667 else set_error( STATUS_INVALID_PARAMETER
);
1668 release_object( process
);
1672 /* retrieve the process idle event */
1673 DECL_HANDLER(get_process_idle_event
)
1675 struct process
*process
;
1678 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1680 if (process
->idle_event
&& process
!= current
->process
)
1681 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1682 EVENT_ALL_ACCESS
, 0 );
1683 release_object( process
);
1687 /* make the current process a system process */
1688 DECL_HANDLER(make_process_system
)
1690 struct process
*process
;
1691 struct thread
*thread
;
1693 if (!shutdown_event
)
1695 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1696 release_object( shutdown_event
);
1699 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
))) return;
1701 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1703 release_object( process
);
1707 if (!process
->is_system
)
1709 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1710 release_thread_desktop( thread
, 0 );
1711 process
->is_system
= 1;
1712 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1713 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1715 release_object( process
);
1718 /* create a new job object */
1719 DECL_HANDLER(create_job
)
1722 struct unicode_str name
;
1723 struct object
*root
;
1724 const struct security_descriptor
*sd
;
1725 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1727 if (!objattr
) return;
1729 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1731 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1732 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1734 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1735 req
->access
, objattr
->attributes
);
1736 release_object( job
);
1738 if (root
) release_object( root
);
1741 /* open a job object */
1742 DECL_HANDLER(open_job
)
1744 struct unicode_str name
= get_req_unicode_str();
1746 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1747 &job_ops
, &name
, req
->attributes
);
1750 /* assign a job object to a process */
1751 DECL_HANDLER(assign_job
)
1753 struct process
*process
;
1754 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1758 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1760 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1761 else add_job_process( job
, process
);
1762 release_object( process
);
1764 release_object( job
);
1768 /* check if a process is associated with a job */
1769 DECL_HANDLER(process_in_job
)
1771 struct process
*process
;
1774 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1779 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1781 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1783 set_error( process_in_job( job
, process
) ? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1784 release_object( job
);
1786 release_object( process
);
1789 /* retrieve information about a job */
1790 DECL_HANDLER(get_job_info
)
1792 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1798 reply
->total_processes
= job
->total_processes
;
1799 reply
->active_processes
= job
->num_processes
;
1801 len
= min( get_reply_max_size(), reply
->active_processes
* sizeof(*pids
) );
1802 if (len
&& ((pids
= set_reply_data_size( len
))))
1803 get_job_pids( job
, pids
, pids
+ len
/ sizeof(*pids
) );
1805 release_object( job
);
1808 /* terminate all processes associated with the job */
1809 DECL_HANDLER(terminate_job
)
1811 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1815 terminate_job( job
, req
->status
);
1816 release_object( job
);
1819 /* update limits of the job object */
1820 DECL_HANDLER(set_job_limits
)
1822 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1826 job
->limit_flags
= req
->limit_flags
;
1827 release_object( job
);
1830 /* set the jobs completion port */
1831 DECL_HANDLER(set_job_completion_port
)
1833 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1837 if (!job
->completion_port
)
1839 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1840 job
->completion_key
= req
->key
;
1842 add_job_completion_existing_processes( job
, job
);
1845 set_error( STATUS_INVALID_PARAMETER
);
1847 release_object( job
);
1850 /* Suspend a process */
1851 DECL_HANDLER(suspend_process
)
1853 struct process
*process
;
1855 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1857 struct list
*ptr
, *next
;
1859 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1861 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1862 suspend_thread( thread
);
1865 release_object( process
);
1869 /* Resume a process */
1870 DECL_HANDLER(resume_process
)
1872 struct process
*process
;
1874 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1876 struct list
*ptr
, *next
;
1878 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1880 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1881 resume_thread( thread
);
1884 release_object( process
);
1888 /* Get a list of processes and threads currently running */
1889 DECL_HANDLER(list_processes
)
1891 struct process
*process
;
1892 struct thread
*thread
;
1893 unsigned int pos
= 0;
1896 reply
->process_count
= 0;
1897 reply
->total_thread_count
= 0;
1898 reply
->total_name_len
= 0;
1899 reply
->info_size
= 0;
1901 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1903 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1904 reply
->info_size
+= sizeof(struct process_info
) + process
->imagelen
;
1905 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1906 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1907 reply
->process_count
++;
1908 reply
->total_thread_count
+= process
->running_threads
;
1909 reply
->total_name_len
+= process
->imagelen
;
1912 if (reply
->info_size
> get_reply_max_size())
1914 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1918 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1920 memset( buffer
, 0, reply
->info_size
);
1921 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1923 struct process_info
*process_info
;
1925 pos
= (pos
+ 7) & ~7;
1926 process_info
= (struct process_info
*)(buffer
+ pos
);
1927 process_info
->start_time
= process
->start_time
;
1928 process_info
->name_len
= process
->imagelen
;
1929 process_info
->thread_count
= process
->running_threads
;
1930 process_info
->priority
= process
->priority
;
1931 process_info
->pid
= process
->id
;
1932 process_info
->parent_pid
= process
->parent_id
;
1933 process_info
->session_id
= process
->session_id
;
1934 process_info
->handle_count
= get_handle_table_count(process
);
1935 process_info
->unix_pid
= process
->unix_pid
;
1936 pos
+= sizeof(*process_info
);
1937 memcpy( buffer
+ pos
, process
->image
, process
->imagelen
);
1938 pos
+= process
->imagelen
;
1939 pos
= (pos
+ 7) & ~7;
1940 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1942 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1944 thread_info
->start_time
= thread
->creation_time
;
1945 thread_info
->tid
= thread
->id
;
1946 thread_info
->base_priority
= thread
->priority
;
1947 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1948 thread_info
->unix_tid
= thread
->unix_tid
;
1949 thread_info
->entry_point
= thread
->entry_point
;
1950 thread_info
->teb
= thread
->teb
;
1951 pos
+= sizeof(*thread_info
);