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 /* Assign a high security label to the token. The default would be medium
739 * but Wine provides admin access to all applications right now so high
740 * makes more sense for the time being. */
741 if (!token_assign_label( process
->token
, &high_label_sid
))
744 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
748 if (process
) release_object( process
);
749 /* if we failed to start our first process, close everything down */
750 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
754 /* get the process data size */
755 data_size_t
get_process_startup_info_size( struct process
*process
)
757 struct startup_info
*info
= process
->startup_info
;
760 return info
->data_size
;
763 /* destroy a process when its refcount is 0 */
764 static void process_destroy( struct object
*obj
)
766 struct process
*process
= (struct process
*)obj
;
767 assert( obj
->ops
== &process_ops
);
769 /* we can't have a thread remaining */
770 assert( list_empty( &process
->thread_list
));
771 assert( list_empty( &process
->asyncs
));
773 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
775 close_process_handles( process
);
776 set_process_startup_state( process
, STARTUP_ABORTED
);
780 list_remove( &process
->job_entry
);
781 release_object( process
->job
);
783 if (process
->console
) release_object( process
->console
);
784 if (process
->msg_fd
) release_object( process
->msg_fd
);
785 if (process
->idle_event
) release_object( process
->idle_event
);
786 if (process
->id
) free_ptid( process
->id
);
787 if (process
->token
) release_object( process
->token
);
788 list_remove( &process
->rawinput_entry
);
789 free( process
->rawinput_devices
);
790 free( process
->dir_cache
);
791 free( process
->image
);
794 /* dump a process on stdout for debugging purposes */
795 static void process_dump( struct object
*obj
, int verbose
)
797 struct process
*process
= (struct process
*)obj
;
798 assert( obj
->ops
== &process_ops
);
800 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
803 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
805 struct process
*process
= (struct process
*)obj
;
806 return !process
->running_threads
;
809 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
811 access
= default_map_access( obj
, access
);
812 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
813 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
817 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
819 struct process
*process
= (struct process
*)obj
;
820 return &process
->kernel_object
;
823 static struct security_descriptor
*process_get_sd( struct object
*obj
)
825 static struct security_descriptor
*process_default_sd
;
827 if (obj
->sd
) return obj
->sd
;
829 if (!process_default_sd
)
834 size_t users_sid_len
= sid_len( &domain_users_sid
);
835 size_t admins_sid_len
= sid_len( &builtin_admins_sid
);
836 size_t dacl_len
= sizeof(*dacl
) + 2 * sizeof(*ace
) + users_sid_len
+ admins_sid_len
;
838 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
840 process_default_sd
->control
= SE_DACL_PRESENT
;
841 process_default_sd
->owner_len
= admins_sid_len
;
842 process_default_sd
->group_len
= users_sid_len
;
843 process_default_sd
->sacl_len
= 0;
844 process_default_sd
->dacl_len
= dacl_len
;
845 sid
= (struct sid
*)(process_default_sd
+ 1);
846 sid
= copy_sid( sid
, &builtin_admins_sid
);
847 sid
= copy_sid( sid
, &domain_users_sid
);
849 dacl
= (struct acl
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
850 dacl
->revision
= ACL_REVISION
;
852 dacl
->size
= dacl_len
;
855 ace
= set_ace( ace_first( dacl
), &domain_users_sid
, ACCESS_ALLOWED_ACE_TYPE
,
856 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
, GENERIC_READ
);
857 set_ace( ace_next( ace
), &builtin_admins_sid
, ACCESS_ALLOWED_ACE_TYPE
, 0, PROCESS_ALL_ACCESS
);
859 return process_default_sd
;
862 static void process_poll_event( struct fd
*fd
, int event
)
864 struct process
*process
= get_fd_user( fd
);
865 assert( process
->obj
.ops
== &process_ops
);
867 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
868 else if (event
& POLLIN
) receive_fd( process
);
871 static void startup_info_destroy( struct object
*obj
)
873 struct startup_info
*info
= (struct startup_info
*)obj
;
874 assert( obj
->ops
== &startup_info_ops
);
876 if (info
->process
) release_object( info
->process
);
879 static void startup_info_dump( struct object
*obj
, int verbose
)
881 struct startup_info
*info
= (struct startup_info
*)obj
;
882 assert( obj
->ops
== &startup_info_ops
);
884 fputs( "Startup info", stderr
);
886 fprintf( stderr
, " in=%04x out=%04x err=%04x",
887 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
888 fputc( '\n', stderr
);
891 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
893 struct startup_info
*info
= (struct startup_info
*)obj
;
894 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
897 /* get a process from an id (and increment the refcount) */
898 struct process
*get_process_from_id( process_id_t id
)
900 struct object
*obj
= get_ptid_entry( id
);
902 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
903 set_error( STATUS_INVALID_CID
);
907 /* get a process from a handle (and increment the refcount) */
908 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
910 return (struct process
*)get_handle_obj( current
->process
, handle
,
911 access
, &process_ops
);
914 /* terminate a process with the given exit code */
915 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
917 struct thread
*thread
;
919 grab_object( process
); /* make sure it doesn't get freed when threads die */
920 process
->is_terminating
= 1;
923 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
925 if (exit_code
) thread
->exit_code
= exit_code
;
926 if (thread
== skip
) continue;
927 if (thread
->state
== TERMINATED
) continue;
928 kill_thread( thread
, 1 );
931 release_object( process
);
934 /* kill all processes */
935 static void kill_all_processes(void)
939 while ((ptr
= list_head( &process_list
)))
941 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
942 terminate_process( process
, NULL
, 1 );
946 /* kill all processes being attached to a console renderer */
947 void kill_console_processes( struct thread
*renderer
, int exit_code
)
949 for (;;) /* restart from the beginning of the list every time */
951 struct process
*process
;
953 /* find the first process being attached to 'renderer' and still running */
954 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
956 if (process
== renderer
->process
) continue;
957 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
959 if (&process
->entry
== &process_list
) break; /* no process found */
960 terminate_process( process
, NULL
, exit_code
);
964 /* a process has been killed (i.e. its last thread died) */
965 static void process_killed( struct process
*process
)
967 assert( list_empty( &process
->thread_list
));
968 process
->end_time
= current_time
;
969 close_process_desktop( process
);
970 process
->winstation
= 0;
971 process
->desktop
= 0;
972 cancel_process_asyncs( process
);
973 close_process_handles( process
);
974 if (process
->idle_event
) release_object( process
->idle_event
);
975 process
->idle_event
= NULL
;
976 assert( !process
->console
);
978 destroy_process_classes( process
);
979 free_mapped_views( process
);
980 free_process_user_handles( process
);
981 remove_process_locks( process
);
982 set_process_startup_state( process
, STARTUP_ABORTED
);
983 finish_process_tracing( process
);
984 release_job_process( process
);
985 start_sigkill_timer( process
);
986 wake_up( &process
->obj
, 0 );
989 /* add a thread to a process running threads list */
990 void add_process_thread( struct process
*process
, struct thread
*thread
)
992 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
993 if (!process
->running_threads
++)
995 list_add_tail( &process_list
, &process
->entry
);
997 if (!process
->is_system
)
999 if (!user_processes
++ && shutdown_timeout
)
1001 remove_timeout_user( shutdown_timeout
);
1002 shutdown_timeout
= NULL
;
1006 grab_object( thread
);
1009 /* remove a thread from a process running threads list */
1010 void remove_process_thread( struct process
*process
, struct thread
*thread
)
1012 assert( process
->running_threads
> 0 );
1013 assert( !list_empty( &process
->thread_list
));
1015 list_remove( &thread
->proc_entry
);
1017 if (!--process
->running_threads
)
1019 /* we have removed the last running thread, exit the process */
1020 process
->exit_code
= thread
->exit_code
;
1021 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
1022 list_remove( &process
->entry
);
1023 process_killed( process
);
1025 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
1026 release_object( thread
);
1029 /* suspend all the threads of a process */
1030 void suspend_process( struct process
*process
)
1032 if (!process
->suspend
++)
1034 struct list
*ptr
, *next
;
1036 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1038 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1039 if (!thread
->suspend
) stop_thread( thread
);
1044 /* resume all the threads of a process */
1045 void resume_process( struct process
*process
)
1047 assert (process
->suspend
> 0);
1048 if (!--process
->suspend
)
1050 struct list
*ptr
, *next
;
1052 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1054 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1055 if (!thread
->suspend
) wake_thread( thread
);
1060 /* kill a process on the spot */
1061 void kill_process( struct process
*process
, int violent_death
)
1063 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1065 release_object( process
->msg_fd
);
1066 process
->msg_fd
= NULL
;
1069 if (process
->sigkill_timeout
) return; /* already waiting for it to die */
1071 if (violent_death
) terminate_process( process
, NULL
, 1 );
1076 grab_object( process
); /* make sure it doesn't get freed when threads die */
1077 while ((ptr
= list_head( &process
->thread_list
)))
1079 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1080 kill_thread( thread
, 0 );
1082 release_object( process
);
1086 /* detach all processes being debugged by a given thread */
1087 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1089 for (;;) /* restart from the beginning of the list every time */
1091 struct process
*process
;
1093 /* find the first process being debugged by 'debugger' and still running */
1094 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1095 if (process
->debug_obj
== debug_obj
) break;
1097 if (&process
->entry
== &process_list
) break; /* no process found */
1100 process
->debug_obj
= NULL
;
1101 terminate_process( process
, NULL
, exit_code
);
1103 else debugger_detach( process
, debug_obj
);
1108 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1110 struct list
*ptr
, *next
;
1112 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1114 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1115 if ((cb
)(process
, user
)) break;
1119 /* set the debugged flag in the process PEB */
1120 int set_process_debug_flag( struct process
*process
, int flag
)
1122 char data
= (flag
!= 0);
1123 client_ptr_t peb32
= 0;
1125 if (!is_machine_64bit( process
->machine
) && is_machine_64bit( native_machine
))
1126 peb32
= process
->peb
+ 0x1000;
1128 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1129 if (peb32
&& !write_process_memory( process
, peb32
+ 2, 1, &data
)) return 0;
1130 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1133 /* create a new process */
1134 DECL_HANDLER(new_process
)
1136 struct startup_info
*info
;
1137 const void *info_ptr
;
1138 struct unicode_str name
, desktop_path
= {0};
1139 const struct security_descriptor
*sd
;
1140 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1141 struct process
*process
= NULL
;
1142 struct token
*token
= NULL
;
1143 struct debug_obj
*debug_obj
= NULL
;
1144 struct process
*parent
;
1145 struct thread
*parent_thread
= current
;
1146 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1147 const obj_handle_t
*handles
= NULL
;
1148 const obj_handle_t
*job_handles
= NULL
;
1149 unsigned int i
, job_handle_count
;
1152 if (socket_fd
== -1)
1154 set_error( STATUS_INVALID_PARAMETER
);
1159 set_error( STATUS_INVALID_PARAMETER
);
1163 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1165 set_error( STATUS_INVALID_HANDLE
);
1171 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1176 if (req
->parent_process
)
1178 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1183 parent_thread
= NULL
;
1185 else parent
= (struct process
*)grab_object( current
->process
);
1187 /* If a job further in the job chain does not permit breakaway process creation
1188 * succeeds and the process which is trying to breakaway is assigned to that job. */
1189 if (parent
->job
&& (req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
) &&
1190 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1192 set_error( STATUS_ACCESS_DENIED
);
1194 release_object( parent
);
1198 /* build the startup info for a new process */
1199 if (!(info
= alloc_object( &startup_info_ops
)))
1202 release_object( parent
);
1205 info
->process
= NULL
;
1208 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1210 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1212 set_error( STATUS_INVALID_PARAMETER
);
1216 if (req
->handles_size
)
1219 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1220 info
->data_size
-= req
->handles_size
;
1223 if ((req
->jobs_size
& 3) || req
->jobs_size
> info
->data_size
)
1225 set_error( STATUS_INVALID_PARAMETER
);
1231 job_handles
= info_ptr
;
1232 info_ptr
= (const char *)info_ptr
+ req
->jobs_size
;
1233 info
->data_size
-= req
->jobs_size
;
1236 job_handle_count
= req
->jobs_size
/ sizeof(*handles
);
1237 for (i
= 0; i
< job_handle_count
; ++i
)
1239 if (!(job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
)))
1244 release_object( job
);
1247 info
->info_size
= min( req
->info_size
, info
->data_size
);
1249 if (req
->info_size
< sizeof(*info
->data
))
1251 /* make sure we have a full startup_info_t structure */
1252 data_size_t env_size
= info
->data_size
- info
->info_size
;
1253 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1255 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1260 memcpy( info
->data
, info_ptr
, info_size
);
1261 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1262 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1263 info
->info_size
= sizeof(startup_info_t
);
1264 info
->data_size
= info
->info_size
+ env_size
;
1268 data_size_t pos
= sizeof(*info
->data
);
1270 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1275 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1276 FIXUP_LEN( info
->data
->curdir_len
);
1277 FIXUP_LEN( info
->data
->dllpath_len
);
1278 FIXUP_LEN( info
->data
->imagepath_len
);
1279 FIXUP_LEN( info
->data
->cmdline_len
);
1280 FIXUP_LEN( info
->data
->title_len
);
1281 desktop_path
.str
= (WCHAR
*)((char *)info
->data
+ pos
);
1282 FIXUP_LEN( info
->data
->desktop_len
);
1283 desktop_path
.len
= info
->data
->desktop_len
;
1284 FIXUP_LEN( info
->data
->shellinfo_len
);
1285 FIXUP_LEN( info
->data
->runtime_len
);
1289 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1294 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1300 if (!(process
= create_process( socket_fd
, parent
, req
->flags
, info
->data
, sd
,
1301 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1304 process
->machine
= req
->machine
;
1305 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1310 if (!(job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)
1311 && !(req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
1312 && job
->limit_flags
& JOB_OBJECT_LIMIT_BREAKAWAY_OK
))
1314 add_job_process( job
, process
);
1315 assert( !get_error() );
1321 for (i
= 0; i
< job_handle_count
; ++i
)
1323 job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
);
1324 add_job_process( job
, process
);
1325 release_object( job
);
1328 release_job_process( process
);
1333 /* connect to the window station */
1334 connect_process_winstation( process
, &desktop_path
, parent_thread
, parent
);
1336 /* inherit the process console, but keep pseudo handles (< 0), and 0 (= not attached to a console) as is */
1337 if ((int)info
->data
->console
> 0)
1338 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1339 0, 0, DUPLICATE_SAME_ACCESS
);
1341 if (!(req
->flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
) && info
->data
->console
!= 1)
1343 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1344 0, 0, DUPLICATE_SAME_ACCESS
| DUPLICATE_SAME_ATTRIBUTES
);
1345 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1346 0, 0, DUPLICATE_SAME_ACCESS
| DUPLICATE_SAME_ATTRIBUTES
);
1347 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1348 0, 0, DUPLICATE_SAME_ACCESS
| DUPLICATE_SAME_ATTRIBUTES
);
1349 /* some handles above may have been invalid; this is not an error */
1350 if (get_error() == STATUS_INVALID_HANDLE
||
1351 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1354 /* attach to the debugger */
1357 process
->debug_obj
= debug_obj
;
1358 process
->debug_children
= !(req
->flags
& PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT
);
1360 else if (parent
->debug_children
)
1362 process
->debug_obj
= parent
->debug_obj
;
1363 /* debug_children is set to 1 by default */
1366 if (info
->data
->process_group_id
== parent
->group_id
)
1367 process
->group_id
= parent
->group_id
;
1369 info
->data
->process_group_id
= process
->group_id
;
1371 info
->process
= (struct process
*)grab_object( process
);
1372 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1373 reply
->pid
= get_process_id( process
);
1374 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1377 if (process
) release_object( process
);
1378 if (debug_obj
) release_object( debug_obj
);
1379 if (token
) release_object( token
);
1380 release_object( parent
);
1381 release_object( info
);
1384 /* Retrieve information about a newly started process */
1385 DECL_HANDLER(get_new_process_info
)
1387 struct startup_info
*info
;
1389 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1390 0, &startup_info_ops
)))
1392 reply
->success
= is_process_init_done( info
->process
);
1393 reply
->exit_code
= info
->process
->exit_code
;
1394 release_object( info
);
1398 /* Retrieve the new process startup info */
1399 DECL_HANDLER(get_startup_info
)
1401 struct process
*process
= current
->process
;
1402 struct startup_info
*info
= process
->startup_info
;
1407 /* we return the data directly without making a copy so this can only be called once */
1408 reply
->machine
= process
->machine
;
1409 reply
->info_size
= info
->info_size
;
1410 size
= info
->data_size
;
1411 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1412 set_reply_data_ptr( info
->data
, size
);
1414 info
->data_size
= 0;
1417 /* signal the end of the process initialization */
1418 DECL_HANDLER(init_process_done
)
1420 struct process
*process
= current
->process
;
1422 if (is_process_init_done(process
))
1424 set_error( STATUS_INVALID_PARAMETER
);
1428 current
->teb
= req
->teb
;
1429 process
->peb
= req
->peb
;
1430 process
->ldt_copy
= req
->ldt_copy
;
1432 process
->start_time
= current_time
;
1434 init_process_tracing( process
);
1435 generate_startup_debug_events( process
);
1436 set_process_startup_state( process
, STARTUP_DONE
);
1438 if (process
->image_info
.subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1439 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1440 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1441 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1444 /* open a handle to a process */
1445 DECL_HANDLER(open_process
)
1447 struct process
*process
= get_process_from_id( req
->pid
);
1451 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1452 release_object( process
);
1456 /* terminate a process */
1457 DECL_HANDLER(terminate_process
)
1459 struct process
*process
;
1463 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1464 if (!process
) return;
1466 else process
= (struct process
*)grab_object( current
->process
);
1468 reply
->self
= (current
->process
== process
);
1469 terminate_process( process
, current
, req
->exit_code
);
1470 release_object( process
);
1473 /* fetch information about a process */
1474 DECL_HANDLER(get_process_info
)
1476 struct process
*process
;
1478 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1480 reply
->pid
= get_process_id( process
);
1481 reply
->ppid
= process
->parent_id
;
1482 reply
->exit_code
= process
->exit_code
;
1483 reply
->priority
= process
->priority
;
1484 reply
->affinity
= process
->affinity
;
1485 reply
->peb
= process
->peb
;
1486 reply
->start_time
= process
->start_time
;
1487 reply
->end_time
= process
->end_time
;
1488 reply
->session_id
= process
->session_id
;
1489 reply
->machine
= process
->machine
;
1490 if (get_reply_max_size())
1492 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1493 else set_reply_data( &process
->image_info
, min( sizeof(process
->image_info
), get_reply_max_size() ));
1495 release_object( process
);
1499 /* retrieve debug information about a process */
1500 DECL_HANDLER(get_process_debug_info
)
1502 struct process
*process
;
1504 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1506 reply
->debug_children
= process
->debug_children
;
1507 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1508 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, MAXIMUM_ALLOWED
, 0 );
1509 release_object( process
);
1512 /* fetch the name of the process image */
1513 DECL_HANDLER(get_process_image_name
)
1515 struct process
*process
;
1517 if (req
->pid
) process
= get_process_from_id( req
->pid
);
1518 else 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 snprintf( proc_path
, sizeof(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 if (req
->mask
& SET_PROCESS_INFO_TOKEN
)
1634 struct token
*token
;
1636 if ((token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1638 release_object( process
->token
);
1639 process
->token
= token
;
1642 release_object( process
);
1646 /* read data from a process address space */
1647 DECL_HANDLER(read_process_memory
)
1649 struct process
*process
;
1650 data_size_t len
= get_reply_max_size();
1652 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1656 char *buffer
= mem_alloc( len
);
1659 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1660 set_reply_data_ptr( buffer
, len
);
1665 release_object( process
);
1668 /* write data to a process address space */
1669 DECL_HANDLER(write_process_memory
)
1671 struct process
*process
;
1673 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1675 data_size_t len
= get_req_data_size();
1676 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1677 release_object( process
);
1681 /* retrieve the process idle event */
1682 DECL_HANDLER(get_process_idle_event
)
1684 struct process
*process
;
1687 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1689 if (process
->idle_event
&& process
!= current
->process
)
1690 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1691 EVENT_ALL_ACCESS
, 0 );
1692 release_object( process
);
1696 /* make the current process a system process */
1697 DECL_HANDLER(make_process_system
)
1699 struct process
*process
;
1700 struct thread
*thread
;
1702 if (!shutdown_event
)
1704 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1705 release_object( shutdown_event
);
1708 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
))) return;
1710 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1712 release_object( process
);
1716 if (!process
->is_system
)
1718 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1719 release_thread_desktop( thread
, 0 );
1720 process
->is_system
= 1;
1721 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1722 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1724 release_object( process
);
1727 /* create a new job object */
1728 DECL_HANDLER(create_job
)
1731 struct unicode_str name
;
1732 struct object
*root
;
1733 const struct security_descriptor
*sd
;
1734 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1736 if (!objattr
) return;
1738 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1740 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1741 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1743 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1744 req
->access
, objattr
->attributes
);
1745 release_object( job
);
1747 if (root
) release_object( root
);
1750 /* open a job object */
1751 DECL_HANDLER(open_job
)
1753 struct unicode_str name
= get_req_unicode_str();
1755 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1756 &job_ops
, &name
, req
->attributes
);
1759 /* assign a job object to a process */
1760 DECL_HANDLER(assign_job
)
1762 struct process
*process
;
1763 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1767 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1769 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1770 else add_job_process( job
, process
);
1771 release_object( process
);
1773 release_object( job
);
1777 /* check if a process is associated with a job */
1778 DECL_HANDLER(process_in_job
)
1780 struct process
*process
;
1783 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1788 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1790 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1792 set_error( process_in_job( job
, process
) ? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1793 release_object( job
);
1795 release_object( process
);
1798 /* retrieve information about a job */
1799 DECL_HANDLER(get_job_info
)
1801 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1807 reply
->total_processes
= job
->total_processes
;
1808 reply
->active_processes
= job
->num_processes
;
1810 len
= min( get_reply_max_size(), reply
->active_processes
* sizeof(*pids
) );
1811 if (len
&& ((pids
= set_reply_data_size( len
))))
1812 get_job_pids( job
, pids
, pids
+ len
/ sizeof(*pids
) );
1814 release_object( job
);
1817 /* terminate all processes associated with the job */
1818 DECL_HANDLER(terminate_job
)
1820 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1824 terminate_job( job
, req
->status
);
1825 release_object( job
);
1828 /* update limits of the job object */
1829 DECL_HANDLER(set_job_limits
)
1831 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1835 job
->limit_flags
= req
->limit_flags
;
1836 release_object( job
);
1839 /* set the jobs completion port */
1840 DECL_HANDLER(set_job_completion_port
)
1842 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1846 if (!job
->completion_port
)
1848 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1849 job
->completion_key
= req
->key
;
1851 add_job_completion_existing_processes( job
, job
);
1854 set_error( STATUS_INVALID_PARAMETER
);
1856 release_object( job
);
1859 /* Suspend a process */
1860 DECL_HANDLER(suspend_process
)
1862 struct process
*process
;
1864 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1866 struct list
*ptr
, *next
;
1868 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1870 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1871 suspend_thread( thread
);
1874 release_object( process
);
1878 /* Resume a process */
1879 DECL_HANDLER(resume_process
)
1881 struct process
*process
;
1883 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1885 struct list
*ptr
, *next
;
1887 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1889 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1890 resume_thread( thread
);
1893 release_object( process
);
1897 /* Get a list of processes and threads currently running */
1898 DECL_HANDLER(list_processes
)
1900 struct process
*process
;
1901 struct thread
*thread
;
1902 unsigned int pos
= 0;
1905 reply
->process_count
= 0;
1906 reply
->total_thread_count
= 0;
1907 reply
->total_name_len
= 0;
1908 reply
->info_size
= 0;
1910 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1912 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1913 reply
->info_size
+= sizeof(struct process_info
) + process
->imagelen
;
1914 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1915 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1916 reply
->process_count
++;
1917 reply
->total_thread_count
+= process
->running_threads
;
1918 reply
->total_name_len
+= process
->imagelen
;
1921 if (reply
->info_size
> get_reply_max_size())
1923 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1927 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1929 memset( buffer
, 0, reply
->info_size
);
1930 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1932 struct process_info
*process_info
;
1934 pos
= (pos
+ 7) & ~7;
1935 process_info
= (struct process_info
*)(buffer
+ pos
);
1936 process_info
->start_time
= process
->start_time
;
1937 process_info
->name_len
= process
->imagelen
;
1938 process_info
->thread_count
= process
->running_threads
;
1939 process_info
->priority
= process
->priority
;
1940 process_info
->pid
= process
->id
;
1941 process_info
->parent_pid
= process
->parent_id
;
1942 process_info
->session_id
= process
->session_id
;
1943 process_info
->handle_count
= get_handle_table_count(process
);
1944 process_info
->unix_pid
= process
->unix_pid
;
1945 pos
+= sizeof(*process_info
);
1946 memcpy( buffer
+ pos
, process
->image
, process
->imagelen
);
1947 pos
+= process
->imagelen
;
1948 pos
= (pos
+ 7) & ~7;
1949 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1951 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1953 thread_info
->start_time
= thread
->creation_time
;
1954 thread_info
->tid
= thread
->id
;
1955 thread_info
->base_priority
= thread
->priority
;
1956 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1957 thread_info
->unix_tid
= thread
->unix_tid
;
1958 thread_info
->entry_point
= thread
->entry_point
;
1959 thread_info
->teb
= thread
->teb
;
1960 pos
+= sizeof(*thread_info
);