2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
42 #define WIN32_NO_STATUS
55 static struct list process_list
= LIST_INIT(process_list
);
56 static int running_processes
, user_processes
;
57 static struct event
*shutdown_event
; /* signaled when shutdown starts */
58 static struct timeout_user
*shutdown_timeout
; /* timeout for server shutdown */
59 static int shutdown_stage
; /* current stage in the shutdown process */
61 static const WCHAR process_name
[] = {'P','r','o','c','e','s','s'};
63 struct type_descr process_type
=
65 { process_name
, sizeof(process_name
) }, /* name */
66 PROCESS_ALL_ACCESS
, /* valid_access */
68 STANDARD_RIGHTS_READ
| PROCESS_VM_READ
| PROCESS_QUERY_INFORMATION
,
69 STANDARD_RIGHTS_WRITE
| PROCESS_SUSPEND_RESUME
| PROCESS_SET_INFORMATION
| PROCESS_SET_QUOTA
70 | PROCESS_CREATE_PROCESS
| PROCESS_DUP_HANDLE
| PROCESS_VM_WRITE
| PROCESS_VM_OPERATION
71 | PROCESS_CREATE_THREAD
,
72 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
,
77 static void process_dump( struct object
*obj
, int verbose
);
78 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
79 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
80 static struct security_descriptor
*process_get_sd( struct object
*obj
);
81 static void process_poll_event( struct fd
*fd
, int event
);
82 static struct list
*process_get_kernel_obj_list( struct object
*obj
);
83 static void process_destroy( struct object
*obj
);
84 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
);
86 static const struct object_ops process_ops
=
88 sizeof(struct process
), /* size */
89 &process_type
, /* type */
90 process_dump
, /* dump */
91 add_queue
, /* add_queue */
92 remove_queue
, /* remove_queue */
93 process_signaled
, /* signaled */
94 no_satisfied
, /* satisfied */
95 no_signal
, /* signal */
96 no_get_fd
, /* get_fd */
97 process_map_access
, /* map_access */
98 process_get_sd
, /* get_sd */
99 default_set_sd
, /* set_sd */
100 no_get_full_name
, /* get_full_name */
101 no_lookup_name
, /* lookup_name */
102 no_link_name
, /* link_name */
103 NULL
, /* unlink_name */
104 no_open_file
, /* open_file */
105 process_get_kernel_obj_list
, /* get_kernel_obj_list */
106 no_close_handle
, /* close_handle */
107 process_destroy
/* destroy */
110 static const struct fd_ops process_fd_ops
=
112 NULL
, /* get_poll_events */
113 process_poll_event
, /* poll_event */
115 NULL
, /* get_fd_type */
117 NULL
, /* queue_async */
118 NULL
, /* reselect_async */
119 NULL
/* cancel async */
122 /* process startup info */
126 struct object obj
; /* object header */
127 struct process
*process
; /* created process */
128 data_size_t info_size
; /* size of startup info */
129 data_size_t data_size
; /* size of whole startup data */
130 startup_info_t
*data
; /* data for startup info */
133 static void startup_info_dump( struct object
*obj
, int verbose
);
134 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
135 static void startup_info_destroy( struct object
*obj
);
137 static const struct object_ops startup_info_ops
=
139 sizeof(struct startup_info
), /* size */
141 startup_info_dump
, /* dump */
142 add_queue
, /* add_queue */
143 remove_queue
, /* remove_queue */
144 startup_info_signaled
, /* signaled */
145 no_satisfied
, /* satisfied */
146 no_signal
, /* signal */
147 no_get_fd
, /* get_fd */
148 default_map_access
, /* map_access */
149 default_get_sd
, /* get_sd */
150 default_set_sd
, /* set_sd */
151 no_get_full_name
, /* get_full_name */
152 no_lookup_name
, /* lookup_name */
153 no_link_name
, /* link_name */
154 NULL
, /* unlink_name */
155 no_open_file
, /* open_file */
156 no_kernel_obj_list
, /* get_kernel_obj_list */
157 no_close_handle
, /* close_handle */
158 startup_info_destroy
/* destroy */
163 static const WCHAR job_name
[] = {'J','o','b'};
165 struct type_descr job_type
=
167 { job_name
, sizeof(job_name
) }, /* name */
168 JOB_OBJECT_ALL_ACCESS
, /* valid_access */
170 STANDARD_RIGHTS_READ
| JOB_OBJECT_QUERY
,
171 STANDARD_RIGHTS_WRITE
| JOB_OBJECT_TERMINATE
| JOB_OBJECT_SET_ATTRIBUTES
| JOB_OBJECT_ASSIGN_PROCESS
,
172 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
173 JOB_OBJECT_ALL_ACCESS
177 static void job_dump( struct object
*obj
, int verbose
);
178 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
179 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
180 static void job_destroy( struct object
*obj
);
184 struct object obj
; /* object header */
185 struct list process_list
; /* list of processes */
186 int num_processes
; /* count of running processes */
187 int total_processes
; /* count of processes which have been assigned */
188 unsigned int limit_flags
; /* limit flags */
189 int terminating
; /* job is terminating */
190 int signaled
; /* job is signaled */
191 struct completion
*completion_port
; /* associated completion port */
192 apc_param_t completion_key
; /* key to send with completion messages */
194 struct list parent_job_entry
; /* list entry for parent job */
195 struct list child_job_list
; /* list of child jobs */
198 static const struct object_ops job_ops
=
200 sizeof(struct job
), /* size */
201 &job_type
, /* type */
203 add_queue
, /* add_queue */
204 remove_queue
, /* remove_queue */
205 job_signaled
, /* signaled */
206 no_satisfied
, /* satisfied */
207 no_signal
, /* signal */
208 no_get_fd
, /* get_fd */
209 default_map_access
, /* map_access */
210 default_get_sd
, /* get_sd */
211 default_set_sd
, /* set_sd */
212 default_get_full_name
, /* get_full_name */
213 no_lookup_name
, /* lookup_name */
214 directory_link_name
, /* link_name */
215 default_unlink_name
, /* unlink_name */
216 no_open_file
, /* open_file */
217 no_kernel_obj_list
, /* get_kernel_obj_list */
218 job_close_handle
, /* close_handle */
219 job_destroy
/* destroy */
222 static struct job
*create_job_object( struct object
*root
, const struct unicode_str
*name
,
223 unsigned int attr
, const struct security_descriptor
*sd
)
227 if ((job
= create_named_object( root
, &job_ops
, name
, attr
, sd
)))
229 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
231 /* initialize it if it didn't already exist */
232 list_init( &job
->process_list
);
233 list_init( &job
->child_job_list
);
234 job
->num_processes
= 0;
235 job
->total_processes
= 0;
236 job
->limit_flags
= 0;
237 job
->terminating
= 0;
239 job
->completion_port
= NULL
;
240 job
->completion_key
= 0;
247 static struct job
*get_job_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
249 return (struct job
*)get_handle_obj( process
, handle
, access
, &job_ops
);
252 static void add_job_completion( struct job
*job
, apc_param_t msg
, apc_param_t pid
)
254 if (job
->completion_port
)
255 add_completion( job
->completion_port
, job
->completion_key
, pid
, STATUS_SUCCESS
, msg
);
258 static void add_job_completion_existing_processes( struct job
*job
, struct job
*completion_job
)
260 struct process
*process
;
263 assert( completion_job
->obj
.ops
== &job_ops
);
265 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
267 add_job_completion_existing_processes( j
, completion_job
);
270 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
272 if (process
->running_threads
)
273 add_job_completion( completion_job
, JOB_OBJECT_MSG_NEW_PROCESS
, get_process_id( process
));
277 static int process_in_job( struct job
*job
, struct process
*process
)
281 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
283 assert( j
->parent
== job
);
284 if (process_in_job( j
, process
)) return 1;
286 return process
->job
== job
;
289 static process_id_t
*get_job_pids( struct job
*job
, process_id_t
*pids
, process_id_t
*end
)
291 struct process
*process
;
294 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
295 pids
= get_job_pids( j
, pids
, end
);
297 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
299 if (pids
== end
) break;
300 if (process
->end_time
) continue; /* skip processes that ended */
301 *pids
++ = process
->id
;
307 static void add_job_process( struct job
*job
, struct process
*process
)
309 struct job
*j
, *common_parent
;
312 if (job
== process
->job
) return;
314 if ((common_parent
= process
->job
))
318 for (j
= job
->parent
; j
; j
= j
->parent
)
319 if (j
== common_parent
) break;
321 if (j
!= common_parent
)
323 /* Job already has parent and the process is not in the job's chain. */
324 set_error( STATUS_ACCESS_DENIED
);
327 /* process->job is referenced in the job->parent chain. */
328 release_object( process
->job
);
332 if (job
->total_processes
)
334 set_error( STATUS_ACCESS_DENIED
);
337 /* transfer reference. */
338 job
->parent
= process
->job
;
339 list_add_tail( &job
->parent
->child_job_list
, &job
->parent_job_entry
);
341 list_remove( &process
->job_entry
);
343 process
->job
= (struct job
*)grab_object( job
);
344 list_add_tail( &job
->process_list
, &process
->job_entry
);
346 pid
= get_process_id( process
);
347 for (j
= job
; j
!= common_parent
; j
= j
->parent
)
350 j
->total_processes
++;
351 add_job_completion( j
, JOB_OBJECT_MSG_NEW_PROCESS
, pid
);
355 /* called when a process has terminated, allow one additional process */
356 static void release_job_process( struct process
*process
)
358 struct job
*job
= process
->job
;
362 assert( job
->num_processes
);
363 job
->num_processes
--;
365 if (!job
->terminating
)
366 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
368 if (!job
->num_processes
)
369 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
375 static void terminate_job( struct job
*job
, int exit_code
)
377 struct process
*process
, *next_process
;
378 struct job
*j
, *next_job
;
380 LIST_FOR_EACH_ENTRY_SAFE( j
, next_job
, &job
->child_job_list
, struct job
, parent_job_entry
)
382 assert( j
->parent
== job
);
385 terminate_job( j
, exit_code
);
389 job
->terminating
= 1;
390 LIST_FOR_EACH_ENTRY_SAFE( process
, next_process
, &job
->process_list
, struct process
, job_entry
)
392 assert( process
->job
== job
);
393 if (process
->running_threads
) terminate_process( process
, NULL
, exit_code
);
395 job
->terminating
= 0;
397 wake_up( &job
->obj
, 0 );
400 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
402 struct job
*job
= (struct job
*)obj
;
403 assert( obj
->ops
== &job_ops
);
405 if (obj
->handle_count
== 1) /* last handle */
407 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
408 terminate_job( job
, 0 );
413 static void job_destroy( struct object
*obj
)
415 struct job
*job
= (struct job
*)obj
;
416 assert( obj
->ops
== &job_ops
);
418 assert( !job
->num_processes
);
419 assert( list_empty( &job
->process_list
));
420 assert( list_empty( &job
->child_job_list
));
422 if (job
->completion_port
) release_object( job
->completion_port
);
425 list_remove( &job
->parent_job_entry
);
426 release_object( job
->parent
);
430 static void job_dump( struct object
*obj
, int verbose
)
432 struct job
*job
= (struct job
*)obj
;
433 assert( obj
->ops
== &job_ops
);
434 fprintf( stderr
, "Job processes=%d child_jobs=%d parent=%p\n",
435 list_count(&job
->process_list
), list_count(&job
->child_job_list
), job
->parent
);
438 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
440 struct job
*job
= (struct job
*)obj
;
441 return job
->signaled
;
446 void *ptr
; /* entry ptr */
447 unsigned int next
; /* next free entry */
450 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
451 static unsigned int used_ptid_entries
; /* number of entries in use */
452 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
453 static unsigned int next_free_ptid
; /* next free entry */
454 static unsigned int last_free_ptid
; /* last free entry */
455 static unsigned int num_free_ptids
; /* number of free ptids */
457 static void kill_all_processes(void);
459 #define PTID_OFFSET 8 /* offset for first ptid value */
461 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
462 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
464 /* allocate a new process or thread id */
465 unsigned int alloc_ptid( void *ptr
)
467 struct ptid_entry
*entry
;
470 if (used_ptid_entries
< alloc_ptid_entries
)
472 index
= used_ptid_entries
+ PTID_OFFSET
;
473 entry
= &ptid_entries
[used_ptid_entries
++];
475 else if (next_free_ptid
&& num_free_ptids
>= 256)
477 index
= next_free_ptid
;
478 entry
= &ptid_entries
[index
- PTID_OFFSET
];
479 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
482 else /* need to grow the array */
484 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
485 if (!count
) count
= 512;
486 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
488 set_error( STATUS_NO_MEMORY
);
491 ptid_entries
= entry
;
492 alloc_ptid_entries
= count
;
493 index
= used_ptid_entries
+ PTID_OFFSET
;
494 entry
= &ptid_entries
[used_ptid_entries
++];
498 return ptid_from_index( index
);
501 /* free a process or thread id */
502 void free_ptid( unsigned int id
)
504 unsigned int index
= index_from_ptid( id
);
505 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
510 /* append to end of free list so that we don't reuse it too early */
511 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
512 else next_free_ptid
= index
;
513 last_free_ptid
= index
;
517 /* retrieve the pointer corresponding to a process or thread id */
518 void *get_ptid_entry( unsigned int id
)
520 unsigned int index
= index_from_ptid( id
);
521 if (index
< PTID_OFFSET
) return NULL
;
522 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
523 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
526 /* return the main thread of the process */
527 struct thread
*get_process_first_thread( struct process
*process
)
529 struct list
*ptr
= list_head( &process
->thread_list
);
530 if (!ptr
) return NULL
;
531 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
534 /* set the state of the process startup info */
535 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
537 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
538 if (process
->startup_info
)
540 wake_up( &process
->startup_info
->obj
, 0 );
541 release_object( process
->startup_info
);
542 process
->startup_info
= NULL
;
546 /* callback for server shutdown */
547 static void server_shutdown_timeout( void *arg
)
549 shutdown_timeout
= NULL
;
550 if (!running_processes
)
552 close_master_socket( 0 );
555 switch(++shutdown_stage
)
557 case 1: /* signal system processes to exit */
558 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
559 if (shutdown_event
) set_event( shutdown_event
);
560 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
561 close_master_socket( 4 * -TICKS_PER_SEC
);
563 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
564 kill_all_processes();
569 /* forced shutdown, used for wineserver -k */
570 void shutdown_master_socket(void)
572 kill_all_processes();
574 if (shutdown_timeout
)
576 remove_timeout_user( shutdown_timeout
);
577 shutdown_timeout
= NULL
;
579 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
582 /* final cleanup once we are sure a process is really dead */
583 static void process_died( struct process
*process
)
585 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
586 if (!process
->is_system
)
588 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
589 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
591 release_object( process
);
592 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
595 /* callback for process sigkill timeout */
596 static void process_sigkill( void *private )
598 struct process
*process
= private;
600 process
->sigkill_timeout
= NULL
;
601 kill( process
->unix_pid
, SIGKILL
);
602 process_died( process
);
605 /* start the sigkill timer for a process upon exit */
606 static void start_sigkill_timer( struct process
*process
)
608 grab_object( process
);
609 if (process
->unix_pid
!= -1)
610 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
612 process_died( process
);
615 /* create a new process */
616 /* if the function fails the fd is closed */
617 struct process
*create_process( int fd
, struct process
*parent
, unsigned int flags
, const startup_info_t
*info
,
618 const struct security_descriptor
*sd
, const obj_handle_t
*handles
,
619 unsigned int handle_count
, struct token
*token
)
621 struct process
*process
;
623 if (!(process
= alloc_object( &process_ops
)))
628 process
->parent_id
= 0;
629 process
->debug_obj
= NULL
;
630 process
->debug_event
= NULL
;
631 process
->handles
= NULL
;
632 process
->msg_fd
= NULL
;
633 process
->sigkill_timeout
= NULL
;
634 process
->unix_pid
= -1;
635 process
->exit_code
= STILL_ACTIVE
;
636 process
->running_threads
= 0;
637 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
638 process
->suspend
= 0;
639 process
->is_system
= 0;
640 process
->debug_children
= 1;
641 process
->is_terminating
= 0;
642 process
->imagelen
= 0;
643 process
->image
= NULL
;
645 process
->console
= NULL
;
646 process
->startup_state
= STARTUP_IN_PROGRESS
;
647 process
->startup_info
= NULL
;
648 process
->idle_event
= NULL
;
650 process
->ldt_copy
= 0;
651 process
->dir_cache
= NULL
;
652 process
->winstation
= 0;
653 process
->desktop
= 0;
654 process
->token
= NULL
;
655 process
->trace_data
= 0;
656 process
->rawinput_mouse
= NULL
;
657 process
->rawinput_kbd
= NULL
;
658 list_init( &process
->kernel_object
);
659 list_init( &process
->thread_list
);
660 list_init( &process
->locks
);
661 list_init( &process
->asyncs
);
662 list_init( &process
->classes
);
663 list_init( &process
->views
);
664 list_init( &process
->rawinput_devices
);
666 process
->end_time
= 0;
668 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
669 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
674 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
679 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
681 /* create the handle table */
684 process
->handles
= alloc_handle_table( process
, 0 );
685 process
->token
= token_create_admin( TRUE
, -1, TokenElevationTypeFull
, default_session_id
);
686 process
->affinity
= ~0;
690 obj_handle_t std_handles
[3];
692 std_handles
[0] = info
->hstdin
;
693 std_handles
[1] = info
->hstdout
;
694 std_handles
[2] = info
->hstderr
;
696 process
->parent_id
= parent
->id
;
697 if (flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
)
698 process
->handles
= copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
);
700 process
->handles
= alloc_handle_table( process
, 0 );
701 /* Note: for security reasons, starting a new process does not attempt
702 * to use the current impersonation token for the new process */
703 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
704 process
->affinity
= parent
->affinity
;
706 if (!process
->handles
|| !process
->token
) goto error
;
707 process
->session_id
= token_get_session_id( process
->token
);
709 /* Assign a high security label to the token. The default would be medium
710 * but Wine provides admin access to all applications right now so high
711 * makes more sense for the time being. */
712 if (!token_assign_label( process
->token
, security_high_label_sid
))
715 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
719 if (process
) release_object( process
);
720 /* if we failed to start our first process, close everything down */
721 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
725 /* get the process data size */
726 data_size_t
get_process_startup_info_size( struct process
*process
)
728 struct startup_info
*info
= process
->startup_info
;
731 return info
->data_size
;
734 /* destroy a process when its refcount is 0 */
735 static void process_destroy( struct object
*obj
)
737 struct process
*process
= (struct process
*)obj
;
738 assert( obj
->ops
== &process_ops
);
740 /* we can't have a thread remaining */
741 assert( list_empty( &process
->thread_list
));
742 assert( list_empty( &process
->asyncs
));
744 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
746 close_process_handles( process
);
747 set_process_startup_state( process
, STARTUP_ABORTED
);
751 list_remove( &process
->job_entry
);
752 release_object( process
->job
);
754 if (process
->console
) release_object( process
->console
);
755 if (process
->msg_fd
) release_object( process
->msg_fd
);
756 if (process
->idle_event
) release_object( process
->idle_event
);
757 if (process
->id
) free_ptid( process
->id
);
758 if (process
->token
) release_object( process
->token
);
759 free( process
->dir_cache
);
760 free( process
->image
);
763 /* dump a process on stdout for debugging purposes */
764 static void process_dump( struct object
*obj
, int verbose
)
766 struct process
*process
= (struct process
*)obj
;
767 assert( obj
->ops
== &process_ops
);
769 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
772 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
774 struct process
*process
= (struct process
*)obj
;
775 return !process
->running_threads
;
778 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
780 access
= default_map_access( obj
, access
);
781 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
782 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
786 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
788 struct process
*process
= (struct process
*)obj
;
789 return &process
->kernel_object
;
792 static struct security_descriptor
*process_get_sd( struct object
*obj
)
794 static struct security_descriptor
*process_default_sd
;
796 if (obj
->sd
) return obj
->sd
;
798 if (!process_default_sd
)
800 size_t users_sid_len
= security_sid_len( security_domain_users_sid
);
801 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
802 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
803 + users_sid_len
+ admins_sid_len
;
804 ACCESS_ALLOWED_ACE
*aaa
;
807 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
809 process_default_sd
->control
= SE_DACL_PRESENT
;
810 process_default_sd
->owner_len
= admins_sid_len
;
811 process_default_sd
->group_len
= users_sid_len
;
812 process_default_sd
->sacl_len
= 0;
813 process_default_sd
->dacl_len
= dacl_len
;
814 memcpy( process_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
815 memcpy( (char *)(process_default_sd
+ 1) + admins_sid_len
, security_domain_users_sid
, users_sid_len
);
817 dacl
= (ACL
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
818 dacl
->AclRevision
= ACL_REVISION
;
820 dacl
->AclSize
= dacl_len
;
823 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
824 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
825 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
826 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
827 aaa
->Mask
= GENERIC_READ
;
828 memcpy( &aaa
->SidStart
, security_domain_users_sid
, users_sid_len
);
829 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
830 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
831 aaa
->Header
.AceFlags
= 0;
832 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
833 aaa
->Mask
= PROCESS_ALL_ACCESS
;
834 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
836 return process_default_sd
;
839 static void process_poll_event( struct fd
*fd
, int event
)
841 struct process
*process
= get_fd_user( fd
);
842 assert( process
->obj
.ops
== &process_ops
);
844 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
845 else if (event
& POLLIN
) receive_fd( process
);
848 static void startup_info_destroy( struct object
*obj
)
850 struct startup_info
*info
= (struct startup_info
*)obj
;
851 assert( obj
->ops
== &startup_info_ops
);
853 if (info
->process
) release_object( info
->process
);
856 static void startup_info_dump( struct object
*obj
, int verbose
)
858 struct startup_info
*info
= (struct startup_info
*)obj
;
859 assert( obj
->ops
== &startup_info_ops
);
861 fputs( "Startup info", stderr
);
863 fprintf( stderr
, " in=%04x out=%04x err=%04x",
864 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
865 fputc( '\n', stderr
);
868 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
870 struct startup_info
*info
= (struct startup_info
*)obj
;
871 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
874 /* get a process from an id (and increment the refcount) */
875 struct process
*get_process_from_id( process_id_t id
)
877 struct object
*obj
= get_ptid_entry( id
);
879 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
880 set_error( STATUS_INVALID_CID
);
884 /* get a process from a handle (and increment the refcount) */
885 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
887 return (struct process
*)get_handle_obj( current
->process
, handle
,
888 access
, &process_ops
);
891 /* terminate a process with the given exit code */
892 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
894 struct thread
*thread
;
896 grab_object( process
); /* make sure it doesn't get freed when threads die */
897 process
->is_terminating
= 1;
900 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
902 if (exit_code
) thread
->exit_code
= exit_code
;
903 if (thread
== skip
) continue;
904 if (thread
->state
== TERMINATED
) continue;
905 kill_thread( thread
, 1 );
908 release_object( process
);
911 /* kill all processes */
912 static void kill_all_processes(void)
916 while ((ptr
= list_head( &process_list
)))
918 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
919 terminate_process( process
, NULL
, 1 );
923 /* kill all processes being attached to a console renderer */
924 void kill_console_processes( struct thread
*renderer
, int exit_code
)
926 for (;;) /* restart from the beginning of the list every time */
928 struct process
*process
;
930 /* find the first process being attached to 'renderer' and still running */
931 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
933 if (process
== renderer
->process
) continue;
934 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
936 if (&process
->entry
== &process_list
) break; /* no process found */
937 terminate_process( process
, NULL
, exit_code
);
941 /* a process has been killed (i.e. its last thread died) */
942 static void process_killed( struct process
*process
)
946 assert( list_empty( &process
->thread_list
));
947 process
->end_time
= current_time
;
948 close_process_desktop( process
);
949 process
->winstation
= 0;
950 process
->desktop
= 0;
951 cancel_process_asyncs( process
);
952 close_process_handles( process
);
953 if (process
->idle_event
) release_object( process
->idle_event
);
954 process
->idle_event
= NULL
;
955 assert( !process
->console
);
957 while ((ptr
= list_head( &process
->rawinput_devices
)))
959 struct rawinput_device_entry
*entry
= LIST_ENTRY( ptr
, struct rawinput_device_entry
, entry
);
960 list_remove( &entry
->entry
);
963 destroy_process_classes( process
);
964 free_mapped_views( process
);
965 free_process_user_handles( process
);
966 remove_process_locks( process
);
967 set_process_startup_state( process
, STARTUP_ABORTED
);
968 finish_process_tracing( process
);
969 release_job_process( process
);
970 start_sigkill_timer( process
);
971 wake_up( &process
->obj
, 0 );
974 /* add a thread to a process running threads list */
975 void add_process_thread( struct process
*process
, struct thread
*thread
)
977 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
978 if (!process
->running_threads
++)
980 list_add_tail( &process_list
, &process
->entry
);
982 if (!process
->is_system
)
984 if (!user_processes
++ && shutdown_timeout
)
986 remove_timeout_user( shutdown_timeout
);
987 shutdown_timeout
= NULL
;
991 grab_object( thread
);
994 /* remove a thread from a process running threads list */
995 void remove_process_thread( struct process
*process
, struct thread
*thread
)
997 assert( process
->running_threads
> 0 );
998 assert( !list_empty( &process
->thread_list
));
1000 list_remove( &thread
->proc_entry
);
1002 if (!--process
->running_threads
)
1004 /* we have removed the last running thread, exit the process */
1005 process
->exit_code
= thread
->exit_code
;
1006 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
1007 list_remove( &process
->entry
);
1008 process_killed( process
);
1010 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
1011 release_object( thread
);
1014 /* suspend all the threads of a process */
1015 void suspend_process( struct process
*process
)
1017 if (!process
->suspend
++)
1019 struct list
*ptr
, *next
;
1021 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1023 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1024 if (!thread
->suspend
) stop_thread( thread
);
1029 /* resume all the threads of a process */
1030 void resume_process( struct process
*process
)
1032 assert (process
->suspend
> 0);
1033 if (!--process
->suspend
)
1035 struct list
*ptr
, *next
;
1037 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1039 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1040 if (!thread
->suspend
) wake_thread( thread
);
1045 /* kill a process on the spot */
1046 void kill_process( struct process
*process
, int violent_death
)
1048 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1050 release_object( process
->msg_fd
);
1051 process
->msg_fd
= NULL
;
1054 if (process
->sigkill_timeout
) return; /* already waiting for it to die */
1056 if (violent_death
) terminate_process( process
, NULL
, 1 );
1061 grab_object( process
); /* make sure it doesn't get freed when threads die */
1062 while ((ptr
= list_head( &process
->thread_list
)))
1064 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1065 kill_thread( thread
, 0 );
1067 release_object( process
);
1071 /* detach all processes being debugged by a given thread */
1072 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1074 for (;;) /* restart from the beginning of the list every time */
1076 struct process
*process
;
1078 /* find the first process being debugged by 'debugger' and still running */
1079 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1080 if (process
->debug_obj
== debug_obj
) break;
1082 if (&process
->entry
== &process_list
) break; /* no process found */
1085 process
->debug_obj
= NULL
;
1086 terminate_process( process
, NULL
, exit_code
);
1088 else debugger_detach( process
, debug_obj
);
1093 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1095 struct list
*ptr
, *next
;
1097 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1099 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1100 if ((cb
)(process
, user
)) break;
1104 /* set the debugged flag in the process PEB */
1105 int set_process_debug_flag( struct process
*process
, int flag
)
1107 char data
= (flag
!= 0);
1108 client_ptr_t peb32
= 0;
1110 if (!is_machine_64bit( process
->machine
) && is_machine_64bit( native_machine
))
1111 peb32
= process
->peb
+ 0x1000;
1113 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1114 if (peb32
&& !write_process_memory( process
, peb32
+ 2, 1, &data
)) return 0;
1115 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1118 /* create a new process */
1119 DECL_HANDLER(new_process
)
1121 struct startup_info
*info
;
1122 const void *info_ptr
;
1123 struct unicode_str name
;
1124 const struct security_descriptor
*sd
;
1125 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1126 struct process
*process
= NULL
;
1127 struct token
*token
= NULL
;
1128 struct debug_obj
*debug_obj
= NULL
;
1129 struct process
*parent
;
1130 struct thread
*parent_thread
= current
;
1131 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1132 const obj_handle_t
*handles
= NULL
;
1133 const obj_handle_t
*job_handles
= NULL
;
1134 unsigned int i
, job_handle_count
;
1137 if (socket_fd
== -1)
1139 set_error( STATUS_INVALID_PARAMETER
);
1144 set_error( STATUS_INVALID_PARAMETER
);
1148 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1150 set_error( STATUS_INVALID_HANDLE
);
1156 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1161 if (req
->parent_process
)
1163 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1168 parent_thread
= NULL
;
1170 else parent
= (struct process
*)grab_object( current
->process
);
1172 /* If a job further in the job chain does not permit breakaway process creation
1173 * succeeds and the process which is trying to breakaway is assigned to that job. */
1174 if (parent
->job
&& (req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
) &&
1175 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1177 set_error( STATUS_ACCESS_DENIED
);
1179 release_object( parent
);
1183 /* build the startup info for a new process */
1184 if (!(info
= alloc_object( &startup_info_ops
)))
1187 release_object( parent
);
1190 info
->process
= NULL
;
1193 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1195 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1197 set_error( STATUS_INVALID_PARAMETER
);
1201 if (req
->handles_size
)
1204 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1205 info
->data_size
-= req
->handles_size
;
1208 if ((req
->jobs_size
& 3) || req
->jobs_size
> info
->data_size
)
1210 set_error( STATUS_INVALID_PARAMETER
);
1216 job_handles
= info_ptr
;
1217 info_ptr
= (const char *)info_ptr
+ req
->jobs_size
;
1218 info
->data_size
-= req
->jobs_size
;
1221 job_handle_count
= req
->jobs_size
/ sizeof(*handles
);
1222 for (i
= 0; i
< job_handle_count
; ++i
)
1224 if (!(job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
)))
1229 release_object( job
);
1232 info
->info_size
= min( req
->info_size
, info
->data_size
);
1234 if (req
->info_size
< sizeof(*info
->data
))
1236 /* make sure we have a full startup_info_t structure */
1237 data_size_t env_size
= info
->data_size
- info
->info_size
;
1238 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1240 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1245 memcpy( info
->data
, info_ptr
, info_size
);
1246 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1247 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1248 info
->info_size
= sizeof(startup_info_t
);
1249 info
->data_size
= info
->info_size
+ env_size
;
1253 data_size_t pos
= sizeof(*info
->data
);
1255 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1260 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1261 FIXUP_LEN( info
->data
->curdir_len
);
1262 FIXUP_LEN( info
->data
->dllpath_len
);
1263 FIXUP_LEN( info
->data
->imagepath_len
);
1264 FIXUP_LEN( info
->data
->cmdline_len
);
1265 FIXUP_LEN( info
->data
->title_len
);
1266 FIXUP_LEN( info
->data
->desktop_len
);
1267 FIXUP_LEN( info
->data
->shellinfo_len
);
1268 FIXUP_LEN( info
->data
->runtime_len
);
1272 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1277 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1283 if (!(process
= create_process( socket_fd
, parent
, req
->flags
, info
->data
, sd
,
1284 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1287 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1292 if (!(job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)
1293 && !(req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
1294 && job
->limit_flags
& JOB_OBJECT_LIMIT_BREAKAWAY_OK
))
1296 add_job_process( job
, process
);
1297 assert( !get_error() );
1303 for (i
= 0; i
< job_handle_count
; ++i
)
1305 job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
);
1306 add_job_process( job
, process
);
1307 release_object( job
);
1310 release_job_process( process
);
1315 /* connect to the window station */
1316 connect_process_winstation( process
, parent_thread
, parent
);
1318 /* set the process console */
1319 if (info
->data
->console
> 3)
1320 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1321 0, 0, DUPLICATE_SAME_ACCESS
);
1323 if (!(req
->flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
) && info
->data
->console
!= 1)
1325 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1326 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1327 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1328 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1329 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1330 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1331 /* some handles above may have been invalid; this is not an error */
1332 if (get_error() == STATUS_INVALID_HANDLE
||
1333 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1336 /* attach to the debugger */
1339 process
->debug_obj
= debug_obj
;
1340 process
->debug_children
= !(req
->flags
& PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT
);
1342 else if (parent
->debug_children
)
1344 process
->debug_obj
= parent
->debug_obj
;
1345 /* debug_children is set to 1 by default */
1348 if (!info
->data
->console_flags
) process
->group_id
= parent
->group_id
;
1350 info
->process
= (struct process
*)grab_object( process
);
1351 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1352 reply
->pid
= get_process_id( process
);
1353 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1356 if (process
) release_object( process
);
1357 if (debug_obj
) release_object( debug_obj
);
1358 if (token
) release_object( token
);
1359 release_object( parent
);
1360 release_object( info
);
1363 /* Retrieve information about a newly started process */
1364 DECL_HANDLER(get_new_process_info
)
1366 struct startup_info
*info
;
1368 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1369 0, &startup_info_ops
)))
1371 reply
->success
= is_process_init_done( info
->process
);
1372 reply
->exit_code
= info
->process
->exit_code
;
1373 release_object( info
);
1377 /* Retrieve the new process startup info */
1378 DECL_HANDLER(get_startup_info
)
1380 struct process
*process
= current
->process
;
1381 struct startup_info
*info
= process
->startup_info
;
1386 /* we return the data directly without making a copy so this can only be called once */
1387 reply
->info_size
= info
->info_size
;
1388 size
= info
->data_size
;
1389 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1390 set_reply_data_ptr( info
->data
, size
);
1392 info
->data_size
= 0;
1395 /* signal the end of the process initialization */
1396 DECL_HANDLER(init_process_done
)
1398 struct process
*process
= current
->process
;
1399 struct memory_view
*view
;
1401 const pe_image_info_t
*image_info
;
1403 if (is_process_init_done(process
))
1405 set_error( STATUS_INVALID_PARAMETER
);
1408 if (!(view
= get_exe_view( process
)))
1410 set_error( STATUS_DLL_NOT_FOUND
);
1413 if (!(image_info
= get_view_image_info( view
, &base
))) return;
1415 current
->teb
= req
->teb
;
1416 process
->peb
= req
->peb
;
1417 process
->ldt_copy
= req
->ldt_copy
;
1419 process
->start_time
= current_time
;
1420 current
->entry_point
= base
+ image_info
->entry_point
;
1422 init_process_tracing( process
);
1423 generate_startup_debug_events( process
);
1424 set_process_startup_state( process
, STARTUP_DONE
);
1426 if (image_info
->subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1427 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1428 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1429 reply
->entry
= current
->entry_point
;
1430 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1433 /* open a handle to a process */
1434 DECL_HANDLER(open_process
)
1436 struct process
*process
= get_process_from_id( req
->pid
);
1440 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1441 release_object( process
);
1445 /* terminate a process */
1446 DECL_HANDLER(terminate_process
)
1448 struct process
*process
;
1452 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1453 if (!process
) return;
1455 else process
= (struct process
*)grab_object( current
->process
);
1457 reply
->self
= (current
->process
== process
);
1458 terminate_process( process
, current
, req
->exit_code
);
1459 release_object( process
);
1462 /* fetch information about a process */
1463 DECL_HANDLER(get_process_info
)
1465 struct process
*process
;
1467 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1469 reply
->pid
= get_process_id( process
);
1470 reply
->ppid
= process
->parent_id
;
1471 reply
->exit_code
= process
->exit_code
;
1472 reply
->priority
= process
->priority
;
1473 reply
->affinity
= process
->affinity
;
1474 reply
->peb
= process
->peb
;
1475 reply
->start_time
= process
->start_time
;
1476 reply
->end_time
= process
->end_time
;
1477 reply
->session_id
= process
->session_id
;
1478 reply
->machine
= process
->machine
;
1479 if (get_reply_max_size())
1482 const pe_image_info_t
*info
;
1483 struct memory_view
*view
= get_exe_view( process
);
1486 if ((info
= get_view_image_info( view
, &base
)))
1487 set_reply_data( info
, min( sizeof(*info
), get_reply_max_size() ));
1489 else set_error( STATUS_PROCESS_IS_TERMINATING
);
1491 release_object( process
);
1495 /* retrieve debug information about a process */
1496 DECL_HANDLER(get_process_debug_info
)
1498 struct process
*process
;
1500 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1502 reply
->debug_children
= process
->debug_children
;
1503 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1504 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, DEBUG_ALL_ACCESS
, 0 );
1505 release_object( process
);
1508 /* fetch the name of the process image */
1509 DECL_HANDLER(get_process_image_name
)
1511 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1513 if (!process
) return;
1516 struct unicode_str name
= { process
->image
, process
->imagelen
};
1517 /* skip the \??\ prefix */
1518 if (req
->win32
&& name
.len
> 6 * sizeof(WCHAR
) && name
.str
[5] == ':')
1521 name
.len
-= 4 * sizeof(WCHAR
);
1523 /* FIXME: else resolve symlinks in NT path */
1525 reply
->len
= name
.len
;
1526 if (name
.len
<= get_reply_max_size())
1528 WCHAR
*ptr
= set_reply_data( name
.str
, name
.len
);
1529 /* change \??\ to \\?\ */
1530 if (req
->win32
&& name
.len
> sizeof(WCHAR
) && ptr
[1] == '?') ptr
[1] = '\\';
1532 else set_error( STATUS_BUFFER_TOO_SMALL
);
1534 release_object( process
);
1537 /* retrieve information about a process memory usage */
1538 DECL_HANDLER(get_process_vm_counters
)
1540 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1542 if (!process
) return;
1544 if (process
->unix_pid
!= -1)
1547 char proc_path
[32], line
[256];
1548 unsigned long value
;
1550 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1551 if ((f
= fopen( proc_path
, "r" )))
1553 while (fgets( line
, sizeof(line
), f
))
1555 if (sscanf( line
, "VmPeak: %lu", &value
))
1556 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1557 else if (sscanf( line
, "VmSize: %lu", &value
))
1558 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1559 else if (sscanf( line
, "VmHWM: %lu", &value
))
1560 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1561 else if (sscanf( line
, "VmRSS: %lu", &value
))
1562 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1563 else if (sscanf( line
, "RssAnon: %lu", &value
))
1564 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1565 else if (sscanf( line
, "VmSwap: %lu", &value
))
1566 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1568 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1571 else set_error( STATUS_ACCESS_DENIED
);
1573 else set_error( STATUS_ACCESS_DENIED
);
1575 release_object( process
);
1578 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1580 struct thread
*thread
;
1582 if (!process
->running_threads
)
1584 set_error( STATUS_PROCESS_IS_TERMINATING
);
1588 process
->affinity
= affinity
;
1590 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1592 set_thread_affinity( thread
, affinity
);
1596 /* set information about a process */
1597 DECL_HANDLER(set_process_info
)
1599 struct process
*process
;
1601 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1603 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1604 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1605 release_object( process
);
1609 /* read data from a process address space */
1610 DECL_HANDLER(read_process_memory
)
1612 struct process
*process
;
1613 data_size_t len
= get_reply_max_size();
1615 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1619 char *buffer
= mem_alloc( len
);
1622 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1623 set_reply_data_ptr( buffer
, len
);
1628 release_object( process
);
1631 /* write data to a process address space */
1632 DECL_HANDLER(write_process_memory
)
1634 struct process
*process
;
1636 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1638 data_size_t len
= get_req_data_size();
1639 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1640 else set_error( STATUS_INVALID_PARAMETER
);
1641 release_object( process
);
1645 /* retrieve the process idle event */
1646 DECL_HANDLER(get_process_idle_event
)
1648 struct process
*process
;
1651 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1653 if (process
->idle_event
&& process
!= current
->process
)
1654 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1655 EVENT_ALL_ACCESS
, 0 );
1656 release_object( process
);
1660 /* make the current process a system process */
1661 DECL_HANDLER(make_process_system
)
1663 struct process
*process
;
1664 struct thread
*thread
;
1666 if (!shutdown_event
)
1668 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1669 release_object( shutdown_event
);
1672 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
))) return;
1674 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1676 release_object( process
);
1680 if (!process
->is_system
)
1682 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1683 release_thread_desktop( thread
, 0 );
1684 process
->is_system
= 1;
1685 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1686 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1688 release_object( process
);
1691 /* create a new job object */
1692 DECL_HANDLER(create_job
)
1695 struct unicode_str name
;
1696 struct object
*root
;
1697 const struct security_descriptor
*sd
;
1698 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1700 if (!objattr
) return;
1702 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1704 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1705 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1707 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1708 req
->access
, objattr
->attributes
);
1709 release_object( job
);
1711 if (root
) release_object( root
);
1714 /* open a job object */
1715 DECL_HANDLER(open_job
)
1717 struct unicode_str name
= get_req_unicode_str();
1719 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1720 &job_ops
, &name
, req
->attributes
);
1723 /* assign a job object to a process */
1724 DECL_HANDLER(assign_job
)
1726 struct process
*process
;
1727 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1731 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1733 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1734 else add_job_process( job
, process
);
1735 release_object( process
);
1737 release_object( job
);
1741 /* check if a process is associated with a job */
1742 DECL_HANDLER(process_in_job
)
1744 struct process
*process
;
1747 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1752 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1754 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1756 set_error( process_in_job( job
, process
) ? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1757 release_object( job
);
1759 release_object( process
);
1762 /* retrieve information about a job */
1763 DECL_HANDLER(get_job_info
)
1765 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1771 reply
->total_processes
= job
->total_processes
;
1772 reply
->active_processes
= job
->num_processes
;
1774 len
= min( get_reply_max_size(), reply
->active_processes
* sizeof(*pids
) );
1775 if (len
&& ((pids
= set_reply_data_size( len
))))
1776 get_job_pids( job
, pids
, pids
+ len
/ sizeof(*pids
) );
1778 release_object( job
);
1781 /* terminate all processes associated with the job */
1782 DECL_HANDLER(terminate_job
)
1784 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1788 terminate_job( job
, req
->status
);
1789 release_object( job
);
1792 /* update limits of the job object */
1793 DECL_HANDLER(set_job_limits
)
1795 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1799 job
->limit_flags
= req
->limit_flags
;
1800 release_object( job
);
1803 /* set the jobs completion port */
1804 DECL_HANDLER(set_job_completion_port
)
1806 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1810 if (!job
->completion_port
)
1812 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1813 job
->completion_key
= req
->key
;
1815 add_job_completion_existing_processes( job
, job
);
1818 set_error( STATUS_INVALID_PARAMETER
);
1820 release_object( job
);
1823 /* Suspend a process */
1824 DECL_HANDLER(suspend_process
)
1826 struct process
*process
;
1828 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1830 struct list
*ptr
, *next
;
1832 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1834 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1835 suspend_thread( thread
);
1838 release_object( process
);
1842 /* Resume a process */
1843 DECL_HANDLER(resume_process
)
1845 struct process
*process
;
1847 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1849 struct list
*ptr
, *next
;
1851 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1853 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1854 resume_thread( thread
);
1857 release_object( process
);
1861 /* Get a list of processes and threads currently running */
1862 DECL_HANDLER(list_processes
)
1864 struct process
*process
;
1865 struct thread
*thread
;
1866 unsigned int pos
= 0;
1869 reply
->process_count
= 0;
1870 reply
->info_size
= 0;
1872 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1874 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1875 reply
->info_size
+= sizeof(struct process_info
) + process
->imagelen
;
1876 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1877 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1878 reply
->process_count
++;
1881 if (reply
->info_size
> get_reply_max_size())
1883 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1887 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1889 memset( buffer
, 0, reply
->info_size
);
1890 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1892 struct process_info
*process_info
;
1894 pos
= (pos
+ 7) & ~7;
1895 process_info
= (struct process_info
*)(buffer
+ pos
);
1896 process_info
->start_time
= process
->start_time
;
1897 process_info
->name_len
= process
->imagelen
;
1898 process_info
->thread_count
= process
->running_threads
;
1899 process_info
->priority
= process
->priority
;
1900 process_info
->pid
= process
->id
;
1901 process_info
->parent_pid
= process
->parent_id
;
1902 process_info
->session_id
= process
->session_id
;
1903 process_info
->handle_count
= get_handle_table_count(process
);
1904 process_info
->unix_pid
= process
->unix_pid
;
1905 pos
+= sizeof(*process_info
);
1906 memcpy( buffer
+ pos
, process
->image
, process
->imagelen
);
1907 pos
+= process
->imagelen
;
1908 pos
= (pos
+ 7) & ~7;
1909 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1911 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1913 thread_info
->start_time
= thread
->creation_time
;
1914 thread_info
->tid
= thread
->id
;
1915 thread_info
->base_priority
= thread
->priority
;
1916 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1917 thread_info
->unix_tid
= thread
->unix_tid
;
1918 pos
+= sizeof(*thread_info
);