2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
43 #define WIN32_NO_STATUS
56 static struct list process_list
= LIST_INIT(process_list
);
57 static int running_processes
, user_processes
;
58 static struct event
*shutdown_event
; /* signaled when shutdown starts */
59 static struct timeout_user
*shutdown_timeout
; /* timeout for server shutdown */
60 static int shutdown_stage
; /* current stage in the shutdown process */
62 static const WCHAR process_name
[] = {'P','r','o','c','e','s','s'};
64 struct type_descr process_type
=
66 { process_name
, sizeof(process_name
) }, /* name */
67 PROCESS_ALL_ACCESS
, /* valid_access */
69 STANDARD_RIGHTS_READ
| PROCESS_VM_READ
| PROCESS_QUERY_INFORMATION
,
70 STANDARD_RIGHTS_WRITE
| PROCESS_SUSPEND_RESUME
| PROCESS_SET_INFORMATION
| PROCESS_SET_QUOTA
71 | PROCESS_CREATE_PROCESS
| PROCESS_DUP_HANDLE
| PROCESS_VM_WRITE
| PROCESS_VM_OPERATION
72 | PROCESS_CREATE_THREAD
,
73 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
,
78 static void process_dump( struct object
*obj
, int verbose
);
79 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
80 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
81 static struct security_descriptor
*process_get_sd( struct object
*obj
);
82 static void process_poll_event( struct fd
*fd
, int event
);
83 static struct list
*process_get_kernel_obj_list( struct object
*obj
);
84 static void process_destroy( struct object
*obj
);
85 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
);
87 static const struct object_ops process_ops
=
89 sizeof(struct process
), /* size */
90 &process_type
, /* type */
91 process_dump
, /* dump */
92 add_queue
, /* add_queue */
93 remove_queue
, /* remove_queue */
94 process_signaled
, /* signaled */
95 no_satisfied
, /* satisfied */
96 no_signal
, /* signal */
97 no_get_fd
, /* get_fd */
98 process_map_access
, /* map_access */
99 process_get_sd
, /* get_sd */
100 default_set_sd
, /* set_sd */
101 no_get_full_name
, /* get_full_name */
102 no_lookup_name
, /* lookup_name */
103 no_link_name
, /* link_name */
104 NULL
, /* unlink_name */
105 no_open_file
, /* open_file */
106 process_get_kernel_obj_list
, /* get_kernel_obj_list */
107 no_close_handle
, /* close_handle */
108 process_destroy
/* destroy */
111 static const struct fd_ops process_fd_ops
=
113 NULL
, /* get_poll_events */
114 process_poll_event
, /* poll_event */
116 NULL
, /* get_fd_type */
118 NULL
, /* queue_async */
119 NULL
, /* reselect_async */
120 NULL
/* cancel async */
123 /* process startup info */
127 struct object obj
; /* object header */
128 struct process
*process
; /* created process */
129 data_size_t info_size
; /* size of startup info */
130 data_size_t data_size
; /* size of whole startup data */
131 startup_info_t
*data
; /* data for startup info */
134 static void startup_info_dump( struct object
*obj
, int verbose
);
135 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
136 static void startup_info_destroy( struct object
*obj
);
138 static const struct object_ops startup_info_ops
=
140 sizeof(struct startup_info
), /* size */
142 startup_info_dump
, /* dump */
143 add_queue
, /* add_queue */
144 remove_queue
, /* remove_queue */
145 startup_info_signaled
, /* signaled */
146 no_satisfied
, /* satisfied */
147 no_signal
, /* signal */
148 no_get_fd
, /* get_fd */
149 default_map_access
, /* map_access */
150 default_get_sd
, /* get_sd */
151 default_set_sd
, /* set_sd */
152 no_get_full_name
, /* get_full_name */
153 no_lookup_name
, /* lookup_name */
154 no_link_name
, /* link_name */
155 NULL
, /* unlink_name */
156 no_open_file
, /* open_file */
157 no_kernel_obj_list
, /* get_kernel_obj_list */
158 no_close_handle
, /* close_handle */
159 startup_info_destroy
/* destroy */
164 static const WCHAR job_name
[] = {'J','o','b'};
166 struct type_descr job_type
=
168 { job_name
, sizeof(job_name
) }, /* name */
169 JOB_OBJECT_ALL_ACCESS
, /* valid_access */
171 STANDARD_RIGHTS_READ
| JOB_OBJECT_QUERY
,
172 STANDARD_RIGHTS_WRITE
| JOB_OBJECT_TERMINATE
| JOB_OBJECT_SET_ATTRIBUTES
| JOB_OBJECT_ASSIGN_PROCESS
,
173 STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
,
174 JOB_OBJECT_ALL_ACCESS
178 static void job_dump( struct object
*obj
, int verbose
);
179 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
180 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
181 static void job_destroy( struct object
*obj
);
185 struct object obj
; /* object header */
186 struct list process_list
; /* list of processes */
187 int num_processes
; /* count of running processes */
188 int total_processes
; /* count of processes which have been assigned */
189 unsigned int limit_flags
; /* limit flags */
190 int terminating
; /* job is terminating */
191 int signaled
; /* job is signaled */
192 struct completion
*completion_port
; /* associated completion port */
193 apc_param_t completion_key
; /* key to send with completion messages */
195 struct list parent_job_entry
; /* list entry for parent job */
196 struct list child_job_list
; /* list of child jobs */
199 static const struct object_ops job_ops
=
201 sizeof(struct job
), /* size */
202 &job_type
, /* type */
204 add_queue
, /* add_queue */
205 remove_queue
, /* remove_queue */
206 job_signaled
, /* signaled */
207 no_satisfied
, /* satisfied */
208 no_signal
, /* signal */
209 no_get_fd
, /* get_fd */
210 default_map_access
, /* map_access */
211 default_get_sd
, /* get_sd */
212 default_set_sd
, /* set_sd */
213 default_get_full_name
, /* get_full_name */
214 no_lookup_name
, /* lookup_name */
215 directory_link_name
, /* link_name */
216 default_unlink_name
, /* unlink_name */
217 no_open_file
, /* open_file */
218 no_kernel_obj_list
, /* get_kernel_obj_list */
219 job_close_handle
, /* close_handle */
220 job_destroy
/* destroy */
223 static struct job
*create_job_object( struct object
*root
, const struct unicode_str
*name
,
224 unsigned int attr
, const struct security_descriptor
*sd
)
228 if ((job
= create_named_object( root
, &job_ops
, name
, attr
, sd
)))
230 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
232 /* initialize it if it didn't already exist */
233 list_init( &job
->process_list
);
234 list_init( &job
->child_job_list
);
235 job
->num_processes
= 0;
236 job
->total_processes
= 0;
237 job
->limit_flags
= 0;
238 job
->terminating
= 0;
240 job
->completion_port
= NULL
;
241 job
->completion_key
= 0;
248 static struct job
*get_job_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
250 return (struct job
*)get_handle_obj( process
, handle
, access
, &job_ops
);
253 static void add_job_completion( struct job
*job
, apc_param_t msg
, apc_param_t pid
)
255 if (job
->completion_port
)
256 add_completion( job
->completion_port
, job
->completion_key
, pid
, STATUS_SUCCESS
, msg
);
259 static void add_job_completion_existing_processes( struct job
*job
, struct job
*completion_job
)
261 struct process
*process
;
264 assert( completion_job
->obj
.ops
== &job_ops
);
266 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
268 add_job_completion_existing_processes( j
, completion_job
);
271 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
273 if (process
->running_threads
)
274 add_job_completion( completion_job
, JOB_OBJECT_MSG_NEW_PROCESS
, get_process_id( process
));
278 static int process_in_job( struct job
*job
, struct process
*process
)
282 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
284 assert( j
->parent
== job
);
285 if (process_in_job( j
, process
)) return 1;
287 return process
->job
== job
;
290 static process_id_t
*get_job_pids( struct job
*job
, process_id_t
*pids
, process_id_t
*end
)
292 struct process
*process
;
295 LIST_FOR_EACH_ENTRY( j
, &job
->child_job_list
, struct job
, parent_job_entry
)
296 pids
= get_job_pids( j
, pids
, end
);
298 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
300 if (pids
== end
) break;
301 if (process
->end_time
) continue; /* skip processes that ended */
302 *pids
++ = process
->id
;
308 static void add_job_process( struct job
*job
, struct process
*process
)
310 struct job
*j
, *common_parent
;
313 if (job
== process
->job
) return;
315 if ((common_parent
= process
->job
))
319 for (j
= job
->parent
; j
; j
= j
->parent
)
320 if (j
== common_parent
) break;
322 if (j
!= common_parent
)
324 /* Job already has parent and the process is not in the job's chain. */
325 set_error( STATUS_ACCESS_DENIED
);
328 /* process->job is referenced in the job->parent chain. */
329 release_object( process
->job
);
333 if (job
->total_processes
)
335 set_error( STATUS_ACCESS_DENIED
);
338 /* transfer reference. */
339 job
->parent
= process
->job
;
340 list_add_tail( &job
->parent
->child_job_list
, &job
->parent_job_entry
);
342 list_remove( &process
->job_entry
);
344 process
->job
= (struct job
*)grab_object( job
);
345 list_add_tail( &job
->process_list
, &process
->job_entry
);
347 pid
= get_process_id( process
);
348 for (j
= job
; j
!= common_parent
; j
= j
->parent
)
351 j
->total_processes
++;
352 add_job_completion( j
, JOB_OBJECT_MSG_NEW_PROCESS
, pid
);
356 /* called when a process has terminated, allow one additional process */
357 static void release_job_process( struct process
*process
)
359 struct job
*job
= process
->job
;
363 assert( job
->num_processes
);
364 job
->num_processes
--;
366 if (!job
->terminating
)
367 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
369 if (!job
->num_processes
)
370 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
376 static void terminate_job( struct job
*job
, int exit_code
)
378 struct process
*process
, *next_process
;
379 struct job
*j
, *next_job
;
381 LIST_FOR_EACH_ENTRY_SAFE( j
, next_job
, &job
->child_job_list
, struct job
, parent_job_entry
)
383 assert( j
->parent
== job
);
386 terminate_job( j
, exit_code
);
390 job
->terminating
= 1;
391 LIST_FOR_EACH_ENTRY_SAFE( process
, next_process
, &job
->process_list
, struct process
, job_entry
)
393 assert( process
->job
== job
);
394 if (process
->running_threads
) terminate_process( process
, NULL
, exit_code
);
396 job
->terminating
= 0;
398 wake_up( &job
->obj
, 0 );
401 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
403 struct job
*job
= (struct job
*)obj
;
404 assert( obj
->ops
== &job_ops
);
406 if (obj
->handle_count
== 1) /* last handle */
408 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
409 terminate_job( job
, 0 );
414 static void job_destroy( struct object
*obj
)
416 struct job
*job
= (struct job
*)obj
;
417 assert( obj
->ops
== &job_ops
);
419 assert( !job
->num_processes
);
420 assert( list_empty( &job
->process_list
));
421 assert( list_empty( &job
->child_job_list
));
423 if (job
->completion_port
) release_object( job
->completion_port
);
426 list_remove( &job
->parent_job_entry
);
427 release_object( job
->parent
);
431 static void job_dump( struct object
*obj
, int verbose
)
433 struct job
*job
= (struct job
*)obj
;
434 assert( obj
->ops
== &job_ops
);
435 fprintf( stderr
, "Job processes=%d child_jobs=%d parent=%p\n",
436 list_count(&job
->process_list
), list_count(&job
->child_job_list
), job
->parent
);
439 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
441 struct job
*job
= (struct job
*)obj
;
442 return job
->signaled
;
447 void *ptr
; /* entry ptr */
448 unsigned int next
; /* next free entry */
451 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
452 static unsigned int used_ptid_entries
; /* number of entries in use */
453 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
454 static unsigned int next_free_ptid
; /* next free entry */
455 static unsigned int last_free_ptid
; /* last free entry */
456 static unsigned int num_free_ptids
; /* number of free ptids */
458 static void kill_all_processes(void);
460 #define PTID_OFFSET 8 /* offset for first ptid value */
462 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
463 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
465 /* allocate a new process or thread id */
466 unsigned int alloc_ptid( void *ptr
)
468 struct ptid_entry
*entry
;
471 if (used_ptid_entries
< alloc_ptid_entries
)
473 index
= used_ptid_entries
+ PTID_OFFSET
;
474 entry
= &ptid_entries
[used_ptid_entries
++];
476 else if (next_free_ptid
&& num_free_ptids
>= 256)
478 index
= next_free_ptid
;
479 entry
= &ptid_entries
[index
- PTID_OFFSET
];
480 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
483 else /* need to grow the array */
485 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
486 if (!count
) count
= 512;
487 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
489 set_error( STATUS_NO_MEMORY
);
492 ptid_entries
= entry
;
493 alloc_ptid_entries
= count
;
494 index
= used_ptid_entries
+ PTID_OFFSET
;
495 entry
= &ptid_entries
[used_ptid_entries
++];
499 return ptid_from_index( index
);
502 /* free a process or thread id */
503 void free_ptid( unsigned int id
)
505 unsigned int index
= index_from_ptid( id
);
506 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
511 /* append to end of free list so that we don't reuse it too early */
512 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
513 else next_free_ptid
= index
;
514 last_free_ptid
= index
;
518 /* retrieve the pointer corresponding to a process or thread id */
519 void *get_ptid_entry( unsigned int id
)
521 unsigned int index
= index_from_ptid( id
);
522 if (index
< PTID_OFFSET
) return NULL
;
523 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
524 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
527 /* return the main thread of the process */
528 struct thread
*get_process_first_thread( struct process
*process
)
530 struct list
*ptr
= list_head( &process
->thread_list
);
531 if (!ptr
) return NULL
;
532 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
535 /* set the state of the process startup info */
536 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
538 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
539 if (process
->startup_info
)
541 wake_up( &process
->startup_info
->obj
, 0 );
542 release_object( process
->startup_info
);
543 process
->startup_info
= NULL
;
547 /* callback for server shutdown */
548 static void server_shutdown_timeout( void *arg
)
550 shutdown_timeout
= NULL
;
551 if (!running_processes
)
553 close_master_socket( 0 );
556 switch(++shutdown_stage
)
558 case 1: /* signal system processes to exit */
559 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
560 if (shutdown_event
) set_event( shutdown_event
);
561 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
562 close_master_socket( 4 * -TICKS_PER_SEC
);
564 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
565 kill_all_processes();
570 /* forced shutdown, used for wineserver -k */
571 void shutdown_master_socket(void)
573 kill_all_processes();
575 if (shutdown_timeout
)
577 remove_timeout_user( shutdown_timeout
);
578 shutdown_timeout
= NULL
;
580 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
583 /* final cleanup once we are sure a process is really dead */
584 static void process_died( struct process
*process
)
586 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
587 if (!process
->is_system
)
589 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
590 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
592 release_object( process
);
593 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
596 /* callback for process sigkill timeout */
597 static void process_sigkill( void *private )
599 struct process
*process
= private;
601 process
->sigkill_timeout
= NULL
;
602 kill( process
->unix_pid
, SIGKILL
);
603 process_died( process
);
606 /* start the sigkill timer for a process upon exit */
607 static void start_sigkill_timer( struct process
*process
)
609 grab_object( process
);
610 if (process
->unix_pid
!= -1)
611 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
613 process_died( process
);
616 /* create a new process */
617 /* if the function fails the fd is closed */
618 struct process
*create_process( int fd
, struct process
*parent
, unsigned int flags
, const startup_info_t
*info
,
619 const struct security_descriptor
*sd
, const obj_handle_t
*handles
,
620 unsigned int handle_count
, struct token
*token
)
622 struct process
*process
;
624 if (!(process
= alloc_object( &process_ops
)))
629 process
->parent_id
= 0;
630 process
->debug_obj
= NULL
;
631 process
->debug_event
= NULL
;
632 process
->handles
= NULL
;
633 process
->msg_fd
= NULL
;
634 process
->sigkill_timeout
= NULL
;
635 process
->unix_pid
= -1;
636 process
->exit_code
= STILL_ACTIVE
;
637 process
->running_threads
= 0;
638 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
639 process
->suspend
= 0;
640 process
->is_system
= 0;
641 process
->debug_children
= 1;
642 process
->is_terminating
= 0;
643 process
->imagelen
= 0;
644 process
->image
= NULL
;
646 process
->console
= NULL
;
647 process
->startup_state
= STARTUP_IN_PROGRESS
;
648 process
->startup_info
= NULL
;
649 process
->idle_event
= NULL
;
651 process
->ldt_copy
= 0;
652 process
->dir_cache
= NULL
;
653 process
->winstation
= 0;
654 process
->desktop
= 0;
655 process
->token
= NULL
;
656 process
->trace_data
= 0;
657 process
->rawinput_mouse
= NULL
;
658 process
->rawinput_kbd
= NULL
;
659 list_init( &process
->kernel_object
);
660 list_init( &process
->thread_list
);
661 list_init( &process
->locks
);
662 list_init( &process
->asyncs
);
663 list_init( &process
->classes
);
664 list_init( &process
->views
);
665 list_init( &process
->rawinput_devices
);
667 process
->end_time
= 0;
669 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
670 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
675 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
680 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
682 /* create the handle table */
685 process
->handles
= alloc_handle_table( process
, 0 );
686 process
->token
= token_create_admin( TRUE
, -1, TokenElevationTypeFull
, default_session_id
);
687 process
->affinity
= ~0;
691 obj_handle_t std_handles
[3];
693 std_handles
[0] = info
->hstdin
;
694 std_handles
[1] = info
->hstdout
;
695 std_handles
[2] = info
->hstderr
;
697 process
->parent_id
= parent
->id
;
698 if (flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
)
699 process
->handles
= copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
);
701 process
->handles
= alloc_handle_table( process
, 0 );
702 /* Note: for security reasons, starting a new process does not attempt
703 * to use the current impersonation token for the new process */
704 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
705 process
->affinity
= parent
->affinity
;
707 if (!process
->handles
|| !process
->token
) goto error
;
708 process
->session_id
= token_get_session_id( process
->token
);
710 /* Assign a high security label to the token. The default would be medium
711 * but Wine provides admin access to all applications right now so high
712 * makes more sense for the time being. */
713 if (!token_assign_label( process
->token
, security_high_label_sid
))
716 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
720 if (process
) release_object( process
);
721 /* if we failed to start our first process, close everything down */
722 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
726 /* get the process data size */
727 data_size_t
get_process_startup_info_size( struct process
*process
)
729 struct startup_info
*info
= process
->startup_info
;
732 return info
->data_size
;
735 /* destroy a process when its refcount is 0 */
736 static void process_destroy( struct object
*obj
)
738 struct process
*process
= (struct process
*)obj
;
739 assert( obj
->ops
== &process_ops
);
741 /* we can't have a thread remaining */
742 assert( list_empty( &process
->thread_list
));
743 assert( list_empty( &process
->asyncs
));
745 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
747 close_process_handles( process
);
748 set_process_startup_state( process
, STARTUP_ABORTED
);
752 list_remove( &process
->job_entry
);
753 release_object( process
->job
);
755 if (process
->console
) release_object( process
->console
);
756 if (process
->msg_fd
) release_object( process
->msg_fd
);
757 if (process
->idle_event
) release_object( process
->idle_event
);
758 if (process
->id
) free_ptid( process
->id
);
759 if (process
->token
) release_object( process
->token
);
760 free( process
->dir_cache
);
761 free( process
->image
);
764 /* dump a process on stdout for debugging purposes */
765 static void process_dump( struct object
*obj
, int verbose
)
767 struct process
*process
= (struct process
*)obj
;
768 assert( obj
->ops
== &process_ops
);
770 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
773 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
775 struct process
*process
= (struct process
*)obj
;
776 return !process
->running_threads
;
779 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
781 access
= default_map_access( obj
, access
);
782 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
783 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
787 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
789 struct process
*process
= (struct process
*)obj
;
790 return &process
->kernel_object
;
793 static struct security_descriptor
*process_get_sd( struct object
*obj
)
795 static struct security_descriptor
*process_default_sd
;
797 if (obj
->sd
) return obj
->sd
;
799 if (!process_default_sd
)
801 size_t users_sid_len
= security_sid_len( security_domain_users_sid
);
802 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
803 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
804 + users_sid_len
+ admins_sid_len
;
805 ACCESS_ALLOWED_ACE
*aaa
;
808 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
810 process_default_sd
->control
= SE_DACL_PRESENT
;
811 process_default_sd
->owner_len
= admins_sid_len
;
812 process_default_sd
->group_len
= users_sid_len
;
813 process_default_sd
->sacl_len
= 0;
814 process_default_sd
->dacl_len
= dacl_len
;
815 memcpy( process_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
816 memcpy( (char *)(process_default_sd
+ 1) + admins_sid_len
, security_domain_users_sid
, users_sid_len
);
818 dacl
= (ACL
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
819 dacl
->AclRevision
= ACL_REVISION
;
821 dacl
->AclSize
= dacl_len
;
824 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
825 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
826 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
827 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
828 aaa
->Mask
= GENERIC_READ
;
829 memcpy( &aaa
->SidStart
, security_domain_users_sid
, users_sid_len
);
830 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
831 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
832 aaa
->Header
.AceFlags
= 0;
833 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
834 aaa
->Mask
= PROCESS_ALL_ACCESS
;
835 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
837 return process_default_sd
;
840 static void process_poll_event( struct fd
*fd
, int event
)
842 struct process
*process
= get_fd_user( fd
);
843 assert( process
->obj
.ops
== &process_ops
);
845 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
846 else if (event
& POLLIN
) receive_fd( process
);
849 static void startup_info_destroy( struct object
*obj
)
851 struct startup_info
*info
= (struct startup_info
*)obj
;
852 assert( obj
->ops
== &startup_info_ops
);
854 if (info
->process
) release_object( info
->process
);
857 static void startup_info_dump( struct object
*obj
, int verbose
)
859 struct startup_info
*info
= (struct startup_info
*)obj
;
860 assert( obj
->ops
== &startup_info_ops
);
862 fputs( "Startup info", stderr
);
864 fprintf( stderr
, " in=%04x out=%04x err=%04x",
865 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
866 fputc( '\n', stderr
);
869 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
871 struct startup_info
*info
= (struct startup_info
*)obj
;
872 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
875 /* get a process from an id (and increment the refcount) */
876 struct process
*get_process_from_id( process_id_t id
)
878 struct object
*obj
= get_ptid_entry( id
);
880 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
881 set_error( STATUS_INVALID_CID
);
885 /* get a process from a handle (and increment the refcount) */
886 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
888 return (struct process
*)get_handle_obj( current
->process
, handle
,
889 access
, &process_ops
);
892 /* terminate a process with the given exit code */
893 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
895 struct thread
*thread
;
897 grab_object( process
); /* make sure it doesn't get freed when threads die */
898 process
->is_terminating
= 1;
901 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
903 if (exit_code
) thread
->exit_code
= exit_code
;
904 if (thread
== skip
) continue;
905 if (thread
->state
== TERMINATED
) continue;
906 kill_thread( thread
, 1 );
909 release_object( process
);
912 /* kill all processes */
913 static void kill_all_processes(void)
917 while ((ptr
= list_head( &process_list
)))
919 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
920 terminate_process( process
, NULL
, 1 );
924 /* kill all processes being attached to a console renderer */
925 void kill_console_processes( struct thread
*renderer
, int exit_code
)
927 for (;;) /* restart from the beginning of the list every time */
929 struct process
*process
;
931 /* find the first process being attached to 'renderer' and still running */
932 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
934 if (process
== renderer
->process
) continue;
935 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
937 if (&process
->entry
== &process_list
) break; /* no process found */
938 terminate_process( process
, NULL
, exit_code
);
942 /* a process has been killed (i.e. its last thread died) */
943 static void process_killed( struct process
*process
)
947 assert( list_empty( &process
->thread_list
));
948 process
->end_time
= current_time
;
949 close_process_desktop( process
);
950 process
->winstation
= 0;
951 process
->desktop
= 0;
952 cancel_process_asyncs( process
);
953 close_process_handles( process
);
954 if (process
->idle_event
) release_object( process
->idle_event
);
955 process
->idle_event
= NULL
;
956 assert( !process
->console
);
958 while ((ptr
= list_head( &process
->rawinput_devices
)))
960 struct rawinput_device_entry
*entry
= LIST_ENTRY( ptr
, struct rawinput_device_entry
, entry
);
961 list_remove( &entry
->entry
);
964 destroy_process_classes( process
);
965 free_mapped_views( process
);
966 free_process_user_handles( process
);
967 remove_process_locks( process
);
968 set_process_startup_state( process
, STARTUP_ABORTED
);
969 finish_process_tracing( process
);
970 release_job_process( process
);
971 start_sigkill_timer( process
);
972 wake_up( &process
->obj
, 0 );
975 /* add a thread to a process running threads list */
976 void add_process_thread( struct process
*process
, struct thread
*thread
)
978 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
979 if (!process
->running_threads
++)
981 list_add_tail( &process_list
, &process
->entry
);
983 if (!process
->is_system
)
985 if (!user_processes
++ && shutdown_timeout
)
987 remove_timeout_user( shutdown_timeout
);
988 shutdown_timeout
= NULL
;
992 grab_object( thread
);
995 /* remove a thread from a process running threads list */
996 void remove_process_thread( struct process
*process
, struct thread
*thread
)
998 assert( process
->running_threads
> 0 );
999 assert( !list_empty( &process
->thread_list
));
1001 list_remove( &thread
->proc_entry
);
1003 if (!--process
->running_threads
)
1005 /* we have removed the last running thread, exit the process */
1006 process
->exit_code
= thread
->exit_code
;
1007 generate_debug_event( thread
, DbgExitProcessStateChange
, process
);
1008 list_remove( &process
->entry
);
1009 process_killed( process
);
1011 else generate_debug_event( thread
, DbgExitThreadStateChange
, thread
);
1012 release_object( thread
);
1015 /* suspend all the threads of a process */
1016 void suspend_process( struct process
*process
)
1018 if (!process
->suspend
++)
1020 struct list
*ptr
, *next
;
1022 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1024 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1025 if (!thread
->suspend
) stop_thread( thread
);
1030 /* resume all the threads of a process */
1031 void resume_process( struct process
*process
)
1033 assert (process
->suspend
> 0);
1034 if (!--process
->suspend
)
1036 struct list
*ptr
, *next
;
1038 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1040 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1041 if (!thread
->suspend
) wake_thread( thread
);
1046 /* kill a process on the spot */
1047 void kill_process( struct process
*process
, int violent_death
)
1049 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1051 release_object( process
->msg_fd
);
1052 process
->msg_fd
= NULL
;
1055 if (process
->sigkill_timeout
) return; /* already waiting for it to die */
1057 if (violent_death
) terminate_process( process
, NULL
, 1 );
1062 grab_object( process
); /* make sure it doesn't get freed when threads die */
1063 while ((ptr
= list_head( &process
->thread_list
)))
1065 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1066 kill_thread( thread
, 0 );
1068 release_object( process
);
1072 /* detach all processes being debugged by a given thread */
1073 void detach_debugged_processes( struct debug_obj
*debug_obj
, int exit_code
)
1075 for (;;) /* restart from the beginning of the list every time */
1077 struct process
*process
;
1079 /* find the first process being debugged by 'debugger' and still running */
1080 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1081 if (process
->debug_obj
== debug_obj
) break;
1083 if (&process
->entry
== &process_list
) break; /* no process found */
1086 process
->debug_obj
= NULL
;
1087 terminate_process( process
, NULL
, exit_code
);
1089 else debugger_detach( process
, debug_obj
);
1094 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1096 struct list
*ptr
, *next
;
1098 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1100 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1101 if ((cb
)(process
, user
)) break;
1105 /* set the debugged flag in the process PEB */
1106 int set_process_debug_flag( struct process
*process
, int flag
)
1108 char data
= (flag
!= 0);
1109 client_ptr_t peb32
= 0;
1111 if (!is_machine_64bit( process
->machine
) && is_machine_64bit( native_machine
))
1112 peb32
= process
->peb
+ 0x1000;
1114 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1115 if (peb32
&& !write_process_memory( process
, peb32
+ 2, 1, &data
)) return 0;
1116 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1119 /* create a new process */
1120 DECL_HANDLER(new_process
)
1122 struct startup_info
*info
;
1123 const void *info_ptr
;
1124 struct unicode_str name
;
1125 const struct security_descriptor
*sd
;
1126 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1127 struct process
*process
= NULL
;
1128 struct token
*token
= NULL
;
1129 struct debug_obj
*debug_obj
= NULL
;
1130 struct process
*parent
;
1131 struct thread
*parent_thread
= current
;
1132 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1133 const obj_handle_t
*handles
= NULL
;
1134 const obj_handle_t
*job_handles
= NULL
;
1135 unsigned int i
, job_handle_count
;
1138 if (socket_fd
== -1)
1140 set_error( STATUS_INVALID_PARAMETER
);
1145 set_error( STATUS_INVALID_PARAMETER
);
1149 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1151 set_error( STATUS_INVALID_HANDLE
);
1157 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1162 if (req
->parent_process
)
1164 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1169 parent_thread
= NULL
;
1171 else parent
= (struct process
*)grab_object( current
->process
);
1173 /* If a job further in the job chain does not permit breakaway process creation
1174 * succeeds and the process which is trying to breakaway is assigned to that job. */
1175 if (parent
->job
&& (req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
) &&
1176 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1178 set_error( STATUS_ACCESS_DENIED
);
1180 release_object( parent
);
1184 /* build the startup info for a new process */
1185 if (!(info
= alloc_object( &startup_info_ops
)))
1188 release_object( parent
);
1191 info
->process
= NULL
;
1194 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1196 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1198 set_error( STATUS_INVALID_PARAMETER
);
1202 if (req
->handles_size
)
1205 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1206 info
->data_size
-= req
->handles_size
;
1209 if ((req
->jobs_size
& 3) || req
->jobs_size
> info
->data_size
)
1211 set_error( STATUS_INVALID_PARAMETER
);
1217 job_handles
= info_ptr
;
1218 info_ptr
= (const char *)info_ptr
+ req
->jobs_size
;
1219 info
->data_size
-= req
->jobs_size
;
1222 job_handle_count
= req
->jobs_size
/ sizeof(*handles
);
1223 for (i
= 0; i
< job_handle_count
; ++i
)
1225 if (!(job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
)))
1230 release_object( job
);
1233 info
->info_size
= min( req
->info_size
, info
->data_size
);
1235 if (req
->info_size
< sizeof(*info
->data
))
1237 /* make sure we have a full startup_info_t structure */
1238 data_size_t env_size
= info
->data_size
- info
->info_size
;
1239 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1241 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1246 memcpy( info
->data
, info_ptr
, info_size
);
1247 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1248 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1249 info
->info_size
= sizeof(startup_info_t
);
1250 info
->data_size
= info
->info_size
+ env_size
;
1254 data_size_t pos
= sizeof(*info
->data
);
1256 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1261 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1262 FIXUP_LEN( info
->data
->curdir_len
);
1263 FIXUP_LEN( info
->data
->dllpath_len
);
1264 FIXUP_LEN( info
->data
->imagepath_len
);
1265 FIXUP_LEN( info
->data
->cmdline_len
);
1266 FIXUP_LEN( info
->data
->title_len
);
1267 FIXUP_LEN( info
->data
->desktop_len
);
1268 FIXUP_LEN( info
->data
->shellinfo_len
);
1269 FIXUP_LEN( info
->data
->runtime_len
);
1273 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1278 if (req
->debug
&& !(debug_obj
= get_debug_obj( current
->process
, req
->debug
, DEBUG_PROCESS_ASSIGN
)))
1284 if (!(process
= create_process( socket_fd
, parent
, req
->flags
, info
->data
, sd
,
1285 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1288 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1293 if (!(job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)
1294 && !(req
->flags
& PROCESS_CREATE_FLAGS_BREAKAWAY
1295 && job
->limit_flags
& JOB_OBJECT_LIMIT_BREAKAWAY_OK
))
1297 add_job_process( job
, process
);
1298 assert( !get_error() );
1304 for (i
= 0; i
< job_handle_count
; ++i
)
1306 job
= get_job_obj( current
->process
, job_handles
[i
], JOB_OBJECT_ASSIGN_PROCESS
);
1307 add_job_process( job
, process
);
1308 release_object( job
);
1311 release_job_process( process
);
1316 /* connect to the window station */
1317 connect_process_winstation( process
, parent_thread
, parent
);
1319 /* set the process console */
1320 if (info
->data
->console
> 3)
1321 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1322 0, 0, DUPLICATE_SAME_ACCESS
);
1324 if (!(req
->flags
& PROCESS_CREATE_FLAGS_INHERIT_HANDLES
) && info
->data
->console
!= 1)
1326 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1327 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1328 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1329 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1330 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1331 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1332 /* some handles above may have been invalid; this is not an error */
1333 if (get_error() == STATUS_INVALID_HANDLE
||
1334 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1337 /* attach to the debugger */
1340 process
->debug_obj
= debug_obj
;
1341 process
->debug_children
= !(req
->flags
& PROCESS_CREATE_FLAGS_NO_DEBUG_INHERIT
);
1343 else if (parent
->debug_children
)
1345 process
->debug_obj
= parent
->debug_obj
;
1346 /* debug_children is set to 1 by default */
1349 if (!info
->data
->console_flags
) process
->group_id
= parent
->group_id
;
1351 info
->process
= (struct process
*)grab_object( process
);
1352 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1353 reply
->pid
= get_process_id( process
);
1354 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1357 if (process
) release_object( process
);
1358 if (debug_obj
) release_object( debug_obj
);
1359 if (token
) release_object( token
);
1360 release_object( parent
);
1361 release_object( info
);
1364 /* Retrieve information about a newly started process */
1365 DECL_HANDLER(get_new_process_info
)
1367 struct startup_info
*info
;
1369 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1370 0, &startup_info_ops
)))
1372 reply
->success
= is_process_init_done( info
->process
);
1373 reply
->exit_code
= info
->process
->exit_code
;
1374 release_object( info
);
1378 /* Retrieve the new process startup info */
1379 DECL_HANDLER(get_startup_info
)
1381 struct process
*process
= current
->process
;
1382 struct startup_info
*info
= process
->startup_info
;
1387 /* we return the data directly without making a copy so this can only be called once */
1388 reply
->info_size
= info
->info_size
;
1389 size
= info
->data_size
;
1390 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1391 set_reply_data_ptr( info
->data
, size
);
1393 info
->data_size
= 0;
1396 /* signal the end of the process initialization */
1397 DECL_HANDLER(init_process_done
)
1399 struct process
*process
= current
->process
;
1400 struct memory_view
*view
;
1402 const pe_image_info_t
*image_info
;
1404 if (is_process_init_done(process
))
1406 set_error( STATUS_INVALID_PARAMETER
);
1409 if (!(view
= get_exe_view( process
)))
1411 set_error( STATUS_DLL_NOT_FOUND
);
1414 if (!(image_info
= get_view_image_info( view
, &base
))) return;
1416 current
->teb
= req
->teb
;
1417 process
->peb
= req
->peb
;
1418 process
->ldt_copy
= req
->ldt_copy
;
1420 process
->start_time
= current_time
;
1421 current
->entry_point
= base
+ image_info
->entry_point
;
1423 init_process_tracing( process
);
1424 generate_startup_debug_events( process
);
1425 set_process_startup_state( process
, STARTUP_DONE
);
1427 if (image_info
->subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1428 process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1429 if (process
->debug_obj
) set_process_debug_flag( process
, 1 );
1430 reply
->entry
= current
->entry_point
;
1431 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1434 /* open a handle to a process */
1435 DECL_HANDLER(open_process
)
1437 struct process
*process
= get_process_from_id( req
->pid
);
1441 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1442 release_object( process
);
1446 /* terminate a process */
1447 DECL_HANDLER(terminate_process
)
1449 struct process
*process
;
1453 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1454 if (!process
) return;
1456 else process
= (struct process
*)grab_object( current
->process
);
1458 reply
->self
= (current
->process
== process
);
1459 terminate_process( process
, current
, req
->exit_code
);
1460 release_object( process
);
1463 /* fetch information about a process */
1464 DECL_HANDLER(get_process_info
)
1466 struct process
*process
;
1468 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1470 reply
->pid
= get_process_id( process
);
1471 reply
->ppid
= process
->parent_id
;
1472 reply
->exit_code
= process
->exit_code
;
1473 reply
->priority
= process
->priority
;
1474 reply
->affinity
= process
->affinity
;
1475 reply
->peb
= process
->peb
;
1476 reply
->start_time
= process
->start_time
;
1477 reply
->end_time
= process
->end_time
;
1478 reply
->session_id
= process
->session_id
;
1479 reply
->machine
= process
->machine
;
1480 if (get_reply_max_size())
1483 const pe_image_info_t
*info
;
1484 struct memory_view
*view
= get_exe_view( process
);
1487 if ((info
= get_view_image_info( view
, &base
)))
1488 set_reply_data( info
, min( sizeof(*info
), get_reply_max_size() ));
1490 else set_error( STATUS_PROCESS_IS_TERMINATING
);
1492 release_object( process
);
1496 /* retrieve debug information about a process */
1497 DECL_HANDLER(get_process_debug_info
)
1499 struct process
*process
;
1501 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
))) return;
1503 reply
->debug_children
= process
->debug_children
;
1504 if (!process
->debug_obj
) set_error( STATUS_PORT_NOT_SET
);
1505 else reply
->debug
= alloc_handle( current
->process
, process
->debug_obj
, DEBUG_ALL_ACCESS
, 0 );
1506 release_object( process
);
1509 /* fetch the name of the process image */
1510 DECL_HANDLER(get_process_image_name
)
1512 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1514 if (!process
) return;
1517 struct unicode_str name
= { process
->image
, process
->imagelen
};
1518 /* skip the \??\ prefix */
1519 if (req
->win32
&& name
.len
> 6 * sizeof(WCHAR
) && name
.str
[5] == ':')
1522 name
.len
-= 4 * sizeof(WCHAR
);
1524 /* FIXME: else resolve symlinks in NT path */
1526 reply
->len
= name
.len
;
1527 if (name
.len
<= get_reply_max_size())
1529 WCHAR
*ptr
= set_reply_data( name
.str
, name
.len
);
1530 /* change \??\ to \\?\ */
1531 if (req
->win32
&& name
.len
> sizeof(WCHAR
) && ptr
[1] == '?') ptr
[1] = '\\';
1533 else set_error( STATUS_BUFFER_TOO_SMALL
);
1535 release_object( process
);
1538 /* retrieve information about a process memory usage */
1539 DECL_HANDLER(get_process_vm_counters
)
1541 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1543 if (!process
) return;
1545 if (process
->unix_pid
!= -1)
1548 char proc_path
[32], line
[256];
1549 unsigned long value
;
1551 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1552 if ((f
= fopen( proc_path
, "r" )))
1554 while (fgets( line
, sizeof(line
), f
))
1556 if (sscanf( line
, "VmPeak: %lu", &value
))
1557 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1558 else if (sscanf( line
, "VmSize: %lu", &value
))
1559 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1560 else if (sscanf( line
, "VmHWM: %lu", &value
))
1561 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1562 else if (sscanf( line
, "VmRSS: %lu", &value
))
1563 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1564 else if (sscanf( line
, "RssAnon: %lu", &value
))
1565 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1566 else if (sscanf( line
, "VmSwap: %lu", &value
))
1567 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1569 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1572 else set_error( STATUS_ACCESS_DENIED
);
1574 else set_error( STATUS_ACCESS_DENIED
);
1576 release_object( process
);
1579 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1581 struct thread
*thread
;
1583 if (!process
->running_threads
)
1585 set_error( STATUS_PROCESS_IS_TERMINATING
);
1589 process
->affinity
= affinity
;
1591 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1593 set_thread_affinity( thread
, affinity
);
1597 /* set information about a process */
1598 DECL_HANDLER(set_process_info
)
1600 struct process
*process
;
1602 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1604 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1605 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1606 release_object( process
);
1610 /* read data from a process address space */
1611 DECL_HANDLER(read_process_memory
)
1613 struct process
*process
;
1614 data_size_t len
= get_reply_max_size();
1616 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1620 char *buffer
= mem_alloc( len
);
1623 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1624 set_reply_data_ptr( buffer
, len
);
1629 release_object( process
);
1632 /* write data to a process address space */
1633 DECL_HANDLER(write_process_memory
)
1635 struct process
*process
;
1637 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1639 data_size_t len
= get_req_data_size();
1640 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1641 else set_error( STATUS_INVALID_PARAMETER
);
1642 release_object( process
);
1646 /* retrieve the process idle event */
1647 DECL_HANDLER(get_process_idle_event
)
1649 struct process
*process
;
1652 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1654 if (process
->idle_event
&& process
!= current
->process
)
1655 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1656 EVENT_ALL_ACCESS
, 0 );
1657 release_object( process
);
1661 /* make the current process a system process */
1662 DECL_HANDLER(make_process_system
)
1664 struct process
*process
;
1665 struct thread
*thread
;
1667 if (!shutdown_event
)
1669 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1670 release_object( shutdown_event
);
1673 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
))) return;
1675 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1677 release_object( process
);
1681 if (!process
->is_system
)
1683 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1684 release_thread_desktop( thread
, 0 );
1685 process
->is_system
= 1;
1686 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1687 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1689 release_object( process
);
1692 /* create a new job object */
1693 DECL_HANDLER(create_job
)
1696 struct unicode_str name
;
1697 struct object
*root
;
1698 const struct security_descriptor
*sd
;
1699 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1701 if (!objattr
) return;
1703 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1705 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1706 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1708 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1709 req
->access
, objattr
->attributes
);
1710 release_object( job
);
1712 if (root
) release_object( root
);
1715 /* open a job object */
1716 DECL_HANDLER(open_job
)
1718 struct unicode_str name
= get_req_unicode_str();
1720 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1721 &job_ops
, &name
, req
->attributes
);
1724 /* assign a job object to a process */
1725 DECL_HANDLER(assign_job
)
1727 struct process
*process
;
1728 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1732 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1734 if (!process
->running_threads
) set_error( STATUS_PROCESS_IS_TERMINATING
);
1735 else add_job_process( job
, process
);
1736 release_object( process
);
1738 release_object( job
);
1742 /* check if a process is associated with a job */
1743 DECL_HANDLER(process_in_job
)
1745 struct process
*process
;
1748 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1753 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1755 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1757 set_error( process_in_job( job
, process
) ? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1758 release_object( job
);
1760 release_object( process
);
1763 /* retrieve information about a job */
1764 DECL_HANDLER(get_job_info
)
1766 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1772 reply
->total_processes
= job
->total_processes
;
1773 reply
->active_processes
= job
->num_processes
;
1775 len
= min( get_reply_max_size(), reply
->active_processes
* sizeof(*pids
) );
1776 if (len
&& ((pids
= set_reply_data_size( len
))))
1777 get_job_pids( job
, pids
, pids
+ len
/ sizeof(*pids
) );
1779 release_object( job
);
1782 /* terminate all processes associated with the job */
1783 DECL_HANDLER(terminate_job
)
1785 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1789 terminate_job( job
, req
->status
);
1790 release_object( job
);
1793 /* update limits of the job object */
1794 DECL_HANDLER(set_job_limits
)
1796 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1800 job
->limit_flags
= req
->limit_flags
;
1801 release_object( job
);
1804 /* set the jobs completion port */
1805 DECL_HANDLER(set_job_completion_port
)
1807 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1811 if (!job
->completion_port
)
1813 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1814 job
->completion_key
= req
->key
;
1816 add_job_completion_existing_processes( job
, job
);
1819 set_error( STATUS_INVALID_PARAMETER
);
1821 release_object( job
);
1824 /* Suspend a process */
1825 DECL_HANDLER(suspend_process
)
1827 struct process
*process
;
1829 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1831 struct list
*ptr
, *next
;
1833 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1835 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1836 suspend_thread( thread
);
1839 release_object( process
);
1843 /* Resume a process */
1844 DECL_HANDLER(resume_process
)
1846 struct process
*process
;
1848 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1850 struct list
*ptr
, *next
;
1852 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1854 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1855 resume_thread( thread
);
1858 release_object( process
);
1862 /* Get a list of processes and threads currently running */
1863 DECL_HANDLER(list_processes
)
1865 struct process
*process
;
1866 struct thread
*thread
;
1867 unsigned int pos
= 0;
1870 reply
->process_count
= 0;
1871 reply
->info_size
= 0;
1873 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1875 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1876 reply
->info_size
+= sizeof(struct process_info
) + process
->imagelen
;
1877 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1878 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1879 reply
->process_count
++;
1882 if (reply
->info_size
> get_reply_max_size())
1884 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1888 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1890 memset( buffer
, 0, reply
->info_size
);
1891 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1893 struct process_info
*process_info
;
1895 pos
= (pos
+ 7) & ~7;
1896 process_info
= (struct process_info
*)(buffer
+ pos
);
1897 process_info
->start_time
= process
->start_time
;
1898 process_info
->name_len
= process
->imagelen
;
1899 process_info
->thread_count
= process
->running_threads
;
1900 process_info
->priority
= process
->priority
;
1901 process_info
->pid
= process
->id
;
1902 process_info
->parent_pid
= process
->parent_id
;
1903 process_info
->session_id
= process
->session_id
;
1904 process_info
->handle_count
= get_handle_table_count(process
);
1905 process_info
->unix_pid
= process
->unix_pid
;
1906 pos
+= sizeof(*process_info
);
1907 memcpy( buffer
+ pos
, process
->image
, process
->imagelen
);
1908 pos
+= process
->imagelen
;
1909 pos
= (pos
+ 7) & ~7;
1910 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1912 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1914 thread_info
->start_time
= thread
->creation_time
;
1915 thread_info
->tid
= thread
->id
;
1916 thread_info
->base_priority
= thread
->priority
;
1917 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1918 thread_info
->unix_tid
= thread
->unix_tid
;
1919 pos
+= sizeof(*thread_info
);