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 void add_job_process( struct job
*job
, struct process
*process
)
291 struct job
*j
, *common_parent
;
294 if (job
== process
->job
) return;
296 if ((common_parent
= process
->job
))
300 for (j
= job
->parent
; j
; j
= j
->parent
)
301 if (j
== common_parent
) break;
303 if (j
!= common_parent
)
305 /* Job already has parent and the process is not in the job's chain. */
306 set_error( STATUS_ACCESS_DENIED
);
309 /* process->job is referenced in the job->parent chain. */
310 release_object( process
->job
);
314 if (job
->total_processes
)
316 set_error( STATUS_ACCESS_DENIED
);
319 /* transfer reference. */
320 job
->parent
= process
->job
;
321 list_add_tail( &job
->parent
->child_job_list
, &job
->parent_job_entry
);
323 list_remove( &process
->job_entry
);
325 process
->job
= (struct job
*)grab_object( job
);
326 list_add_tail( &job
->process_list
, &process
->job_entry
);
328 pid
= get_process_id( process
);
329 for (j
= job
; j
!= common_parent
; j
= j
->parent
)
332 j
->total_processes
++;
333 add_job_completion( j
, JOB_OBJECT_MSG_NEW_PROCESS
, pid
);
337 /* called when a process has terminated, allow one additional process */
338 static void release_job_process( struct process
*process
)
340 struct job
*job
= process
->job
;
344 assert( job
->num_processes
);
345 job
->num_processes
--;
347 if (!job
->terminating
)
348 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
350 if (!job
->num_processes
)
351 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
357 static void terminate_job( struct job
*job
, int exit_code
)
359 struct process
*process
, *next_process
;
360 struct job
*j
, *next_job
;
362 LIST_FOR_EACH_ENTRY_SAFE( j
, next_job
, &job
->child_job_list
, struct job
, parent_job_entry
)
364 assert( j
->parent
== job
);
367 terminate_job( j
, exit_code
);
371 job
->terminating
= 1;
372 LIST_FOR_EACH_ENTRY_SAFE( process
, next_process
, &job
->process_list
, struct process
, job_entry
)
374 assert( process
->job
== job
);
375 if (process
->running_threads
) terminate_process( process
, NULL
, exit_code
);
377 job
->terminating
= 0;
379 wake_up( &job
->obj
, 0 );
382 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
384 struct job
*job
= (struct job
*)obj
;
385 assert( obj
->ops
== &job_ops
);
387 if (obj
->handle_count
== 1) /* last handle */
389 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
390 terminate_job( job
, 0 );
395 static void job_destroy( struct object
*obj
)
397 struct job
*job
= (struct job
*)obj
;
398 assert( obj
->ops
== &job_ops
);
400 assert( !job
->num_processes
);
401 assert( list_empty( &job
->process_list
));
402 assert( list_empty( &job
->child_job_list
));
404 if (job
->completion_port
) release_object( job
->completion_port
);
407 list_remove( &job
->parent_job_entry
);
408 release_object( job
->parent
);
412 static void job_dump( struct object
*obj
, int verbose
)
414 struct job
*job
= (struct job
*)obj
;
415 assert( obj
->ops
== &job_ops
);
416 fprintf( stderr
, "Job processes=%d child_jobs=%d parent=%p\n",
417 list_count(&job
->process_list
), list_count(&job
->child_job_list
), job
->parent
);
420 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
422 struct job
*job
= (struct job
*)obj
;
423 return job
->signaled
;
428 void *ptr
; /* entry ptr */
429 unsigned int next
; /* next free entry */
432 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
433 static unsigned int used_ptid_entries
; /* number of entries in use */
434 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
435 static unsigned int next_free_ptid
; /* next free entry */
436 static unsigned int last_free_ptid
; /* last free entry */
437 static unsigned int num_free_ptids
; /* number of free ptids */
439 static void kill_all_processes(void);
441 #define PTID_OFFSET 8 /* offset for first ptid value */
443 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
444 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
446 /* allocate a new process or thread id */
447 unsigned int alloc_ptid( void *ptr
)
449 struct ptid_entry
*entry
;
452 if (used_ptid_entries
< alloc_ptid_entries
)
454 index
= used_ptid_entries
+ PTID_OFFSET
;
455 entry
= &ptid_entries
[used_ptid_entries
++];
457 else if (next_free_ptid
&& num_free_ptids
>= 256)
459 index
= next_free_ptid
;
460 entry
= &ptid_entries
[index
- PTID_OFFSET
];
461 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
464 else /* need to grow the array */
466 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
467 if (!count
) count
= 512;
468 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
470 set_error( STATUS_NO_MEMORY
);
473 ptid_entries
= entry
;
474 alloc_ptid_entries
= count
;
475 index
= used_ptid_entries
+ PTID_OFFSET
;
476 entry
= &ptid_entries
[used_ptid_entries
++];
480 return ptid_from_index( index
);
483 /* free a process or thread id */
484 void free_ptid( unsigned int id
)
486 unsigned int index
= index_from_ptid( id
);
487 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
492 /* append to end of free list so that we don't reuse it too early */
493 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
494 else next_free_ptid
= index
;
495 last_free_ptid
= index
;
499 /* retrieve the pointer corresponding to a process or thread id */
500 void *get_ptid_entry( unsigned int id
)
502 unsigned int index
= index_from_ptid( id
);
503 if (index
< PTID_OFFSET
) return NULL
;
504 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
505 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
508 /* return the main thread of the process */
509 struct thread
*get_process_first_thread( struct process
*process
)
511 struct list
*ptr
= list_head( &process
->thread_list
);
512 if (!ptr
) return NULL
;
513 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
516 /* set the state of the process startup info */
517 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
519 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
520 if (process
->startup_info
)
522 wake_up( &process
->startup_info
->obj
, 0 );
523 release_object( process
->startup_info
);
524 process
->startup_info
= NULL
;
528 /* callback for server shutdown */
529 static void server_shutdown_timeout( void *arg
)
531 shutdown_timeout
= NULL
;
532 if (!running_processes
)
534 close_master_socket( 0 );
537 switch(++shutdown_stage
)
539 case 1: /* signal system processes to exit */
540 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
541 if (shutdown_event
) set_event( shutdown_event
);
542 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
543 close_master_socket( 4 * -TICKS_PER_SEC
);
545 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
546 kill_all_processes();
551 /* forced shutdown, used for wineserver -k */
552 void shutdown_master_socket(void)
554 kill_all_processes();
556 if (shutdown_timeout
)
558 remove_timeout_user( shutdown_timeout
);
559 shutdown_timeout
= NULL
;
561 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
564 /* final cleanup once we are sure a process is really dead */
565 static void process_died( struct process
*process
)
567 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
568 if (!process
->is_system
)
570 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
571 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
573 release_object( process
);
574 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
577 /* callback for process sigkill timeout */
578 static void process_sigkill( void *private )
580 struct process
*process
= private;
582 process
->sigkill_timeout
= NULL
;
583 kill( process
->unix_pid
, SIGKILL
);
584 process_died( process
);
587 /* start the sigkill timer for a process upon exit */
588 static void start_sigkill_timer( struct process
*process
)
590 grab_object( process
);
591 if (process
->unix_pid
!= -1)
592 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
594 process_died( process
);
597 /* create a new process */
598 /* if the function fails the fd is closed */
599 struct process
*create_process( int fd
, struct process
*parent
, unsigned int flags
, const startup_info_t
*info
,
600 const struct security_descriptor
*sd
, const obj_handle_t
*handles
,
601 unsigned int handle_count
, struct token
*token
)
603 struct process
*process
;
605 if (!(process
= alloc_object( &process_ops
)))
610 process
->parent_id
= 0;
611 process
->debug_obj
= NULL
;
612 process
->debug_event
= NULL
;
613 process
->handles
= NULL
;
614 process
->msg_fd
= NULL
;
615 process
->sigkill_timeout
= NULL
;
616 process
->unix_pid
= -1;
617 process
->exit_code
= STILL_ACTIVE
;
618 process
->running_threads
= 0;
619 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
620 process
->suspend
= 0;
621 process
->is_system
= 0;
622 process
->debug_children
= 1;
623 process
->is_terminating
= 0;
624 process
->imagelen
= 0;
625 process
->image
= NULL
;
627 process
->console
= NULL
;
628 process
->startup_state
= STARTUP_IN_PROGRESS
;
629 process
->startup_info
= NULL
;
630 process
->idle_event
= NULL
;
632 process
->ldt_copy
= 0;
633 process
->dir_cache
= NULL
;
634 process
->winstation
= 0;
635 process
->desktop
= 0;
636 process
->token
= NULL
;
637 process
->trace_data
= 0;
638 process
->rawinput_mouse
= NULL
;
639 process
->rawinput_kbd
= NULL
;
640 list_init( &process
->kernel_object
);
641 list_init( &process
->thread_list
);
642 list_init( &process
->locks
);
643 list_init( &process
->asyncs
);
644 list_init( &process
->classes
);
645 list_init( &process
->views
);
646 list_init( &process
->rawinput_devices
);
648 process
->end_time
= 0;
650 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
651 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
656 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
661 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
663 /* create the handle table */
666 process
->handles
= alloc_handle_table( process
, 0 );
667 process
->token
= token_create_admin( TokenElevationTypeFull
);
668 process
->affinity
= ~0;
672 obj_handle_t std_handles
[3];
674 std_handles
[0] = info
->hstdin
;
675 std_handles
[1] = info
->hstdout
;
676 std_handles
[2] = info
->hstderr
;
678 process
->parent_id
= parent
->id
;
679 if (flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
)
680 process
->handles
= copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
);
682 process
->handles
= alloc_handle_table( process
, 0 );
683 /* Note: for security reasons, starting a new process does not attempt
684 * to use the current impersonation token for the new process */
685 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
686 process
->affinity
= parent
->affinity
;
688 if (!process
->handles
|| !process
->token
) goto error
;
690 /* Assign a high security label to the token. The default would be medium
691 * but Wine provides admin access to all applications right now so high
692 * makes more sense for the time being. */
693 if (!token_assign_label( process
->token
, security_high_label_sid
))
696 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
700 if (process
) release_object( process
);
701 /* if we failed to start our first process, close everything down */
702 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
706 /* get the process data size */
707 data_size_t
get_process_startup_info_size( struct process
*process
)
709 struct startup_info
*info
= process
->startup_info
;
712 return info
->data_size
;
715 /* destroy a process when its refcount is 0 */
716 static void process_destroy( struct object
*obj
)
718 struct process
*process
= (struct process
*)obj
;
719 assert( obj
->ops
== &process_ops
);
721 /* we can't have a thread remaining */
722 assert( list_empty( &process
->thread_list
));
723 assert( list_empty( &process
->asyncs
));
725 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
727 close_process_handles( process
);
728 set_process_startup_state( process
, STARTUP_ABORTED
);
732 list_remove( &process
->job_entry
);
733 release_object( process
->job
);
735 if (process
->console
) release_object( process
->console
);
736 if (process
->msg_fd
) release_object( process
->msg_fd
);
737 if (process
->idle_event
) release_object( process
->idle_event
);
738 if (process
->id
) free_ptid( process
->id
);
739 if (process
->token
) release_object( process
->token
);
740 free( process
->dir_cache
);
741 free( process
->image
);
744 /* dump a process on stdout for debugging purposes */
745 static void process_dump( struct object
*obj
, int verbose
)
747 struct process
*process
= (struct process
*)obj
;
748 assert( obj
->ops
== &process_ops
);
750 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
753 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
755 struct process
*process
= (struct process
*)obj
;
756 return !process
->running_threads
;
759 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
761 access
= default_map_access( obj
, access
);
762 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
763 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
767 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
769 struct process
*process
= (struct process
*)obj
;
770 return &process
->kernel_object
;
773 static struct security_descriptor
*process_get_sd( struct object
*obj
)
775 static struct security_descriptor
*process_default_sd
;
777 if (obj
->sd
) return obj
->sd
;
779 if (!process_default_sd
)
781 size_t users_sid_len
= security_sid_len( security_domain_users_sid
);
782 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
783 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
784 + users_sid_len
+ admins_sid_len
;
785 ACCESS_ALLOWED_ACE
*aaa
;
788 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
790 process_default_sd
->control
= SE_DACL_PRESENT
;
791 process_default_sd
->owner_len
= admins_sid_len
;
792 process_default_sd
->group_len
= users_sid_len
;
793 process_default_sd
->sacl_len
= 0;
794 process_default_sd
->dacl_len
= dacl_len
;
795 memcpy( process_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
796 memcpy( (char *)(process_default_sd
+ 1) + admins_sid_len
, security_domain_users_sid
, users_sid_len
);
798 dacl
= (ACL
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
799 dacl
->AclRevision
= ACL_REVISION
;
801 dacl
->AclSize
= dacl_len
;
804 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
805 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
806 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
807 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
808 aaa
->Mask
= GENERIC_READ
;
809 memcpy( &aaa
->SidStart
, security_domain_users_sid
, users_sid_len
);
810 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
811 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
812 aaa
->Header
.AceFlags
= 0;
813 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
814 aaa
->Mask
= PROCESS_ALL_ACCESS
;
815 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
817 return process_default_sd
;
820 static void process_poll_event( struct fd
*fd
, int event
)
822 struct process
*process
= get_fd_user( fd
);
823 assert( process
->obj
.ops
== &process_ops
);
825 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
826 else if (event
& POLLIN
) receive_fd( process
);
829 static void startup_info_destroy( struct object
*obj
)
831 struct startup_info
*info
= (struct startup_info
*)obj
;
832 assert( obj
->ops
== &startup_info_ops
);
834 if (info
->process
) release_object( info
->process
);
837 static void startup_info_dump( struct object
*obj
, int verbose
)
839 struct startup_info
*info
= (struct startup_info
*)obj
;
840 assert( obj
->ops
== &startup_info_ops
);
842 fputs( "Startup info", stderr
);
844 fprintf( stderr
, " in=%04x out=%04x err=%04x",
845 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
846 fputc( '\n', stderr
);
849 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
851 struct startup_info
*info
= (struct startup_info
*)obj
;
852 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
855 /* get a process from an id (and increment the refcount) */
856 struct process
*get_process_from_id( process_id_t id
)
858 struct object
*obj
= get_ptid_entry( id
);
860 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
861 set_error( STATUS_INVALID_CID
);
865 /* get a process from a handle (and increment the refcount) */
866 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
868 return (struct process
*)get_handle_obj( current
->process
, handle
,
869 access
, &process_ops
);
872 /* terminate a process with the given exit code */
873 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
875 struct thread
*thread
;
877 grab_object( process
); /* make sure it doesn't get freed when threads die */
878 process
->is_terminating
= 1;
881 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
883 if (exit_code
) thread
->exit_code
= exit_code
;
884 if (thread
== skip
) continue;
885 if (thread
->state
== TERMINATED
) continue;
886 kill_thread( thread
, 1 );
889 release_object( process
);
892 /* kill all processes */
893 static void kill_all_processes(void)
897 while ((ptr
= list_head( &process_list
)))
899 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
900 terminate_process( process
, NULL
, 1 );
904 /* kill all processes being attached to a console renderer */
905 void kill_console_processes( struct thread
*renderer
, int exit_code
)
907 for (;;) /* restart from the beginning of the list every time */
909 struct process
*process
;
911 /* find the first process being attached to 'renderer' and still running */
912 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
914 if (process
== renderer
->process
) continue;
915 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
917 if (&process
->entry
== &process_list
) break; /* no process found */
918 terminate_process( process
, NULL
, exit_code
);
922 /* a process has been killed (i.e. its last thread died) */
923 static void process_killed( struct process
*process
)
927 assert( list_empty( &process
->thread_list
));
928 process
->end_time
= current_time
;
929 close_process_desktop( process
);
930 process
->winstation
= 0;
931 process
->desktop
= 0;
932 cancel_process_asyncs( process
);
933 close_process_handles( process
);
934 if (process
->idle_event
) release_object( process
->idle_event
);
935 process
->idle_event
= NULL
;
936 assert( !process
->console
);
938 while ((ptr
= list_head( &process
->rawinput_devices
)))
940 struct rawinput_device_entry
*entry
= LIST_ENTRY( ptr
, struct rawinput_device_entry
, entry
);
941 list_remove( &entry
->entry
);
944 destroy_process_classes( process
);
945 free_mapped_views( process
);
946 free_process_user_handles( process
);
947 remove_process_locks( process
);
948 set_process_startup_state( process
, STARTUP_ABORTED
);
949 finish_process_tracing( process
);
950 release_job_process( process
);
951 start_sigkill_timer( process
);
952 wake_up( &process
->obj
, 0 );
955 /* add a thread to a process running threads list */
956 void add_process_thread( struct process
*process
, struct thread
*thread
)
958 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
959 if (!process
->running_threads
++)
961 list_add_tail( &process_list
, &process
->entry
);
963 if (!process
->is_system
)
965 if (!user_processes
++ && shutdown_timeout
)
967 remove_timeout_user( shutdown_timeout
);
968 shutdown_timeout
= NULL
;
972 grab_object( thread
);
975 /* remove a thread from a process running threads list */
976 void remove_process_thread( struct process
*process
, struct thread
*thread
)
978 assert( process
->running_threads
> 0 );
979 assert( !list_empty( &process
->thread_list
));
981 list_remove( &thread
->proc_entry
);
983 if (!--process
->running_threads
)
985 /* we have removed the last running thread, exit the process */
986 process
->exit_code
= thread
->exit_code
;
987 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
988 list_remove( &process
->entry
);
989 process_killed( process
);
991 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
992 release_object( thread
);
995 /* suspend all the threads of a process */
996 void suspend_process( struct process
*process
)
998 if (!process
->suspend
++)
1000 struct list
*ptr
, *next
;
1002 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1004 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1005 if (!thread
->suspend
) stop_thread( thread
);
1010 /* resume all the threads of a process */
1011 void resume_process( struct process
*process
)
1013 assert (process
->suspend
> 0);
1014 if (!--process
->suspend
)
1016 struct list
*ptr
, *next
;
1018 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1020 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1021 if (!thread
->suspend
) wake_thread( thread
);
1026 /* kill a process on the spot */
1027 void kill_process( struct process
*process
, int violent_death
)
1029 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1031 release_object( process
->msg_fd
);
1032 process
->msg_fd
= NULL
;
1035 if (process
->sigkill_timeout
) return; /* already waiting for it to die */
1037 if (violent_death
) terminate_process( process
, NULL
, 1 );
1042 grab_object( process
); /* make sure it doesn't get freed when threads die */
1043 while ((ptr
= list_head( &process
->thread_list
)))
1045 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1046 kill_thread( thread
, 0 );
1048 release_object( process
);
1052 /* detach all processes being debugged by a given thread */
1053 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1055 for (;;) /* restart from the beginning of the list every time */
1057 struct process
*process
;
1059 /* find the first process being debugged by 'debugger' and still running */
1060 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1061 if (process
->debug_obj
== debug_obj
) break;
1063 if (&process
->entry
== &process_list
) break; /* no process found */
1066 process
->debug_obj
= NULL
;
1067 terminate_process( process
, NULL
, exit_code
);
1069 else debugger_detach( process
, debug_obj
);
1074 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1076 struct list
*ptr
, *next
;
1078 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1080 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1081 if ((cb
)(process
, user
)) break;
1085 /* set the debugged flag in the process PEB */
1086 int set_process_debug_flag( struct process
*process
, int flag
)
1088 char data
= (flag
!= 0);
1089 client_ptr_t peb32
= 0;
1091 if (!is_machine_64bit( process
->machine
) && is_machine_64bit( native_machine
))
1092 peb32
= process
->peb
+ 0x1000;
1094 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1095 if (peb32
&& !write_process_memory( process
, peb32
+ 2, 1, &data
)) return 0;
1096 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1099 /* create a new process */
1100 DECL_HANDLER(new_process
)
1102 struct startup_info
*info
;
1103 const void *info_ptr
;
1104 struct unicode_str name
;
1105 const struct security_descriptor
*sd
;
1106 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1107 struct process
*process
= NULL
;
1108 struct token
*token
= NULL
;
1109 struct debug_obj
*debug_obj
= NULL
;
1110 struct process
*parent
;
1111 struct thread
*parent_thread
= current
;
1112 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1113 const obj_handle_t
*handles
= NULL
;
1114 const obj_handle_t
*job_handles
= NULL
;
1115 unsigned int i
, job_handle_count
;
1118 if (socket_fd
== -1)
1120 set_error( STATUS_INVALID_PARAMETER
);
1125 set_error( STATUS_INVALID_PARAMETER
);
1129 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1131 set_error( STATUS_INVALID_HANDLE
);
1137 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1142 if (req
->parent_process
)
1144 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1149 parent_thread
= NULL
;
1151 else parent
= (struct process
*)grab_object( current
->process
);
1153 /* If a job further in the job chain does not permit breakaway process creation
1154 * succeeds and the process which is trying to breakaway is assigned to that job. */
1155 if (parent
->job
&& (req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
) &&
1156 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1158 set_error( STATUS_ACCESS_DENIED
);
1160 release_object( parent
);
1164 /* build the startup info for a new process */
1165 if (!(info
= alloc_object( &startup_info_ops
)))
1168 release_object( parent
);
1171 info
->process
= NULL
;
1174 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1176 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1178 set_error( STATUS_INVALID_PARAMETER
);
1182 if (req
->handles_size
)
1185 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1186 info
->data_size
-= req
->handles_size
;
1189 if ((req
->jobs_size
& 3) || req
->jobs_size
> info
->data_size
)
1191 set_error( STATUS_INVALID_PARAMETER
);
1197 job_handles
= info_ptr
;
1198 info_ptr
= (const char *)info_ptr
+ req
->jobs_size
;
1199 info
->data_size
-= req
->jobs_size
;
1202 job_handle_count
= req
->jobs_size
/ sizeof(*handles
);
1203 for (i
= 0; i
< job_handle_count
; ++i
)
1205 if (!(job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
)))
1210 release_object( job
);
1213 info
->info_size
= min( req
->info_size
, info
->data_size
);
1215 if (req
->info_size
< sizeof(*info
->data
))
1217 /* make sure we have a full startup_info_t structure */
1218 data_size_t env_size
= info
->data_size
- info
->info_size
;
1219 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1221 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1226 memcpy( info
->data
, info_ptr
, info_size
);
1227 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1228 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1229 info
->info_size
= sizeof(startup_info_t
);
1230 info
->data_size
= info
->info_size
+ env_size
;
1234 data_size_t pos
= sizeof(*info
->data
);
1236 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1241 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1242 FIXUP_LEN( info
->data
->curdir_len
);
1243 FIXUP_LEN( info
->data
->dllpath_len
);
1244 FIXUP_LEN( info
->data
->imagepath_len
);
1245 FIXUP_LEN( info
->data
->cmdline_len
);
1246 FIXUP_LEN( info
->data
->title_len
);
1247 FIXUP_LEN( info
->data
->desktop_len
);
1248 FIXUP_LEN( info
->data
->shellinfo_len
);
1249 FIXUP_LEN( info
->data
->runtime_len
);
1253 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1258 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1264 if (!(process
= create_process( socket_fd
, parent
, req
->flags
, info
->data
, sd
,
1265 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1268 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1273 if (!(job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)
1274 && !(req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
1275 && job
->limit_flags
& JOB_OBJECT_LIMIT_BREAKAWAY_OK
))
1277 add_job_process( job
, process
);
1278 assert( !get_error() );
1284 for (i
= 0; i
< job_handle_count
; ++i
)
1286 job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
);
1287 add_job_process( job
, process
);
1288 release_object( job
);
1291 release_job_process( process
);
1296 /* connect to the window station */
1297 connect_process_winstation( process
, parent_thread
, parent
);
1299 /* set the process console */
1300 if (info
->data
->console
> 3)
1301 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1302 0, 0, DUPLICATE_SAME_ACCESS
);
1304 if (!(req
->flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
) && info
->data
->console
!= 1)
1306 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1307 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1308 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1309 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1310 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1311 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1312 /* some handles above may have been invalid; this is not an error */
1313 if (get_error() == STATUS_INVALID_HANDLE
||
1314 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1317 /* attach to the debugger */
1320 process
->debug_obj
= debug_obj
;
1321 process
->debug_children
= !(req
->flags
& PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT
);
1323 else if (parent
->debug_children
)
1325 process
->debug_obj
= parent
->debug_obj
;
1326 /* debug_children is set to 1 by default */
1329 if (!info
->data
->console_flags
) process
->group_id
= parent
->group_id
;
1331 info
->process
= (struct process
*)grab_object( process
);
1332 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1333 reply
->pid
= get_process_id( process
);
1334 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1337 if (process
) release_object( process
);
1338 if (debug_obj
) release_object( debug_obj
);
1339 if (token
) release_object( token
);
1340 release_object( parent
);
1341 release_object( info
);
1344 /* Retrieve information about a newly started process */
1345 DECL_HANDLER(get_new_process_info
)
1347 struct startup_info
*info
;
1349 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1350 0, &startup_info_ops
)))
1352 reply
->success
= is_process_init_done( info
->process
);
1353 reply
->exit_code
= info
->process
->exit_code
;
1354 release_object( info
);
1358 /* Retrieve the new process startup info */
1359 DECL_HANDLER(get_startup_info
)
1361 struct process
*process
= current
->process
;
1362 struct startup_info
*info
= process
->startup_info
;
1367 /* we return the data directly without making a copy so this can only be called once */
1368 reply
->info_size
= info
->info_size
;
1369 size
= info
->data_size
;
1370 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1371 set_reply_data_ptr( info
->data
, size
);
1373 info
->data_size
= 0;
1376 /* signal the end of the process initialization */
1377 DECL_HANDLER(init_process_done
)
1379 struct process
*process
= current
->process
;
1380 struct memory_view
*view
;
1382 const pe_image_info_t
*image_info
;
1384 if (is_process_init_done(process
))
1386 set_error( STATUS_INVALID_PARAMETER
);
1389 if (!(view
= get_exe_view( process
)))
1391 set_error( STATUS_DLL_NOT_FOUND
);
1394 if (!(image_info
= get_view_image_info( view
, &base
))) return;
1396 current
->teb
= req
->teb
;
1397 process
->peb
= req
->peb
;
1398 process
->ldt_copy
= req
->ldt_copy
;
1400 process
->start_time
= current_time
;
1401 current
->entry_point
= image_info
->entry_point
;
1403 init_process_tracing( process
);
1404 generate_startup_debug_events( process
);
1405 set_process_startup_state( process
, STARTUP_DONE
);
1407 if (image_info
->subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1408 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1409 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1410 reply
->entry
= image_info
->entry_point
;
1411 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1414 /* open a handle to a process */
1415 DECL_HANDLER(open_process
)
1417 struct process
*process
= get_process_from_id( req
->pid
);
1421 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1422 release_object( process
);
1426 /* terminate a process */
1427 DECL_HANDLER(terminate_process
)
1429 struct process
*process
;
1433 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1434 if (!process
) return;
1436 else process
= (struct process
*)grab_object( current
->process
);
1438 reply
->self
= (current
->process
== process
);
1439 terminate_process( process
, current
, req
->exit_code
);
1440 release_object( process
);
1443 /* fetch information about a process */
1444 DECL_HANDLER(get_process_info
)
1446 struct process
*process
;
1448 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1450 reply
->pid
= get_process_id( process
);
1451 reply
->ppid
= process
->parent_id
;
1452 reply
->exit_code
= process
->exit_code
;
1453 reply
->priority
= process
->priority
;
1454 reply
->affinity
= process
->affinity
;
1455 reply
->peb
= process
->peb
;
1456 reply
->start_time
= process
->start_time
;
1457 reply
->end_time
= process
->end_time
;
1458 reply
->machine
= process
->machine
;
1459 if (get_reply_max_size())
1462 const pe_image_info_t
*info
;
1463 struct memory_view
*view
= get_exe_view( process
);
1466 if ((info
= get_view_image_info( view
, &base
)))
1467 set_reply_data( info
, min( sizeof(*info
), get_reply_max_size() ));
1469 else set_error( STATUS_PROCESS_IS_TERMINATING
);
1471 release_object( process
);
1475 /* retrieve debug information about a process */
1476 DECL_HANDLER(get_process_debug_info
)
1478 struct process
*process
;
1480 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1482 reply
->debug_children
= process
->debug_children
;
1483 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1484 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, DEBUG_ALL_ACCESS
, 0 );
1485 release_object( process
);
1488 /* fetch the name of the process image */
1489 DECL_HANDLER(get_process_image_name
)
1491 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1493 if (!process
) return;
1496 struct unicode_str name
= { process
->image
, process
->imagelen
};
1497 /* skip the \??\ prefix */
1498 if (req
->win32
&& name
.len
> 6 * sizeof(WCHAR
) && name
.str
[5] == ':')
1501 name
.len
-= 4 * sizeof(WCHAR
);
1503 /* FIXME: else resolve symlinks in NT path */
1505 reply
->len
= name
.len
;
1506 if (name
.len
<= get_reply_max_size())
1508 WCHAR
*ptr
= set_reply_data( name
.str
, name
.len
);
1509 /* change \??\ to \\?\ */
1510 if (req
->win32
&& name
.len
> sizeof(WCHAR
) && ptr
[1] == '?') ptr
[1] = '\\';
1512 else set_error( STATUS_BUFFER_TOO_SMALL
);
1514 release_object( process
);
1517 /* retrieve information about a process memory usage */
1518 DECL_HANDLER(get_process_vm_counters
)
1520 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1522 if (!process
) return;
1524 if (process
->unix_pid
!= -1)
1527 char proc_path
[32], line
[256];
1528 unsigned long value
;
1530 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1531 if ((f
= fopen( proc_path
, "r" )))
1533 while (fgets( line
, sizeof(line
), f
))
1535 if (sscanf( line
, "VmPeak: %lu", &value
))
1536 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1537 else if (sscanf( line
, "VmSize: %lu", &value
))
1538 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1539 else if (sscanf( line
, "VmHWM: %lu", &value
))
1540 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1541 else if (sscanf( line
, "VmRSS: %lu", &value
))
1542 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1543 else if (sscanf( line
, "RssAnon: %lu", &value
))
1544 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1545 else if (sscanf( line
, "VmSwap: %lu", &value
))
1546 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1548 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1551 else set_error( STATUS_ACCESS_DENIED
);
1553 else set_error( STATUS_ACCESS_DENIED
);
1555 release_object( process
);
1558 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1560 struct thread
*thread
;
1562 if (!process
->running_threads
)
1564 set_error( STATUS_PROCESS_IS_TERMINATING
);
1568 process
->affinity
= affinity
;
1570 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1572 set_thread_affinity( thread
, affinity
);
1576 /* set information about a process */
1577 DECL_HANDLER(set_process_info
)
1579 struct process
*process
;
1581 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1583 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1584 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1585 release_object( process
);
1589 /* read data from a process address space */
1590 DECL_HANDLER(read_process_memory
)
1592 struct process
*process
;
1593 data_size_t len
= get_reply_max_size();
1595 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1599 char *buffer
= mem_alloc( len
);
1602 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1603 set_reply_data_ptr( buffer
, len
);
1608 release_object( process
);
1611 /* write data to a process address space */
1612 DECL_HANDLER(write_process_memory
)
1614 struct process
*process
;
1616 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1618 data_size_t len
= get_req_data_size();
1619 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1620 else set_error( STATUS_INVALID_PARAMETER
);
1621 release_object( process
);
1625 /* retrieve the process idle event */
1626 DECL_HANDLER(get_process_idle_event
)
1628 struct process
*process
;
1631 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1633 if (process
->idle_event
&& process
!= current
->process
)
1634 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1635 EVENT_ALL_ACCESS
, 0 );
1636 release_object( process
);
1640 /* make the current process a system process */
1641 DECL_HANDLER(make_process_system
)
1643 struct process
*process
;
1644 struct thread
*thread
;
1646 if (!shutdown_event
)
1648 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1649 release_object( shutdown_event
);
1652 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
))) return;
1654 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1656 release_object( process
);
1660 if (!process
->is_system
)
1662 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1663 release_thread_desktop( thread
, 0 );
1664 process
->is_system
= 1;
1665 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1666 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1668 release_object( process
);
1671 /* create a new job object */
1672 DECL_HANDLER(create_job
)
1675 struct unicode_str name
;
1676 struct object
*root
;
1677 const struct security_descriptor
*sd
;
1678 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1680 if (!objattr
) return;
1682 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1684 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1685 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1687 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1688 req
->access
, objattr
->attributes
);
1689 release_object( job
);
1691 if (root
) release_object( root
);
1694 /* open a job object */
1695 DECL_HANDLER(open_job
)
1697 struct unicode_str name
= get_req_unicode_str();
1699 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1700 &job_ops
, &name
, req
->attributes
);
1703 /* assign a job object to a process */
1704 DECL_HANDLER(assign_job
)
1706 struct process
*process
;
1707 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1711 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1713 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1714 else add_job_process( job
, process
);
1715 release_object( process
);
1717 release_object( job
);
1721 /* check if a process is associated with a job */
1722 DECL_HANDLER(process_in_job
)
1724 struct process
*process
;
1727 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1732 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1734 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1736 set_error( process_in_job( job
, process
) ? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1737 release_object( job
);
1739 release_object( process
);
1742 /* retrieve information about a job */
1743 DECL_HANDLER(get_job_info
)
1745 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1749 reply
->total_processes
= job
->total_processes
;
1750 reply
->active_processes
= job
->num_processes
;
1751 release_object( job
);
1754 /* terminate all processes associated with the job */
1755 DECL_HANDLER(terminate_job
)
1757 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1761 terminate_job( job
, req
->status
);
1762 release_object( job
);
1765 /* update limits of the job object */
1766 DECL_HANDLER(set_job_limits
)
1768 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1772 job
->limit_flags
= req
->limit_flags
;
1773 release_object( job
);
1776 /* set the jobs completion port */
1777 DECL_HANDLER(set_job_completion_port
)
1779 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1783 if (!job
->completion_port
)
1785 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1786 job
->completion_key
= req
->key
;
1788 add_job_completion_existing_processes( job
, job
);
1791 set_error( STATUS_INVALID_PARAMETER
);
1793 release_object( job
);
1796 /* Suspend a process */
1797 DECL_HANDLER(suspend_process
)
1799 struct process
*process
;
1801 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1803 struct list
*ptr
, *next
;
1805 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1807 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1808 suspend_thread( thread
);
1811 release_object( process
);
1815 /* Resume a process */
1816 DECL_HANDLER(resume_process
)
1818 struct process
*process
;
1820 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1822 struct list
*ptr
, *next
;
1824 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1826 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1827 resume_thread( thread
);
1830 release_object( process
);
1834 /* Get a list of processes and threads currently running */
1835 DECL_HANDLER(list_processes
)
1837 struct process
*process
;
1838 struct thread
*thread
;
1839 unsigned int pos
= 0;
1842 reply
->process_count
= 0;
1843 reply
->info_size
= 0;
1845 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1847 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1848 reply
->info_size
+= sizeof(struct process_info
) + process
->imagelen
;
1849 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1850 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1851 reply
->process_count
++;
1854 if (reply
->info_size
> get_reply_max_size())
1856 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1860 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1862 memset( buffer
, 0, reply
->info_size
);
1863 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1865 struct process_info
*process_info
;
1867 pos
= (pos
+ 7) & ~7;
1868 process_info
= (struct process_info
*)(buffer
+ pos
);
1869 process_info
->start_time
= process
->start_time
;
1870 process_info
->name_len
= process
->imagelen
;
1871 process_info
->thread_count
= process
->running_threads
;
1872 process_info
->priority
= process
->priority
;
1873 process_info
->pid
= process
->id
;
1874 process_info
->parent_pid
= process
->parent_id
;
1875 process_info
->handle_count
= get_handle_table_count(process
);
1876 process_info
->unix_pid
= process
->unix_pid
;
1877 pos
+= sizeof(*process_info
);
1878 memcpy( buffer
+ pos
, process
->image
, process
->imagelen
);
1879 pos
+= process
->imagelen
;
1880 pos
= (pos
+ 7) & ~7;
1881 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1883 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1885 thread_info
->start_time
= thread
->creation_time
;
1886 thread_info
->tid
= thread
->id
;
1887 thread_info
->base_priority
= thread
->priority
;
1888 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1889 thread_info
->unix_tid
= thread
->unix_tid
;
1890 pos
+= sizeof(*thread_info
);