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
53 /* process structure */
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 /* process operations */
63 static void process_dump( struct object
*obj
, int verbose
);
64 static struct object_type
*process_get_type( struct object
*obj
);
65 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
66 static unsigned int process_map_access( struct object
*obj
, unsigned int access
);
67 static struct security_descriptor
*process_get_sd( struct object
*obj
);
68 static void process_poll_event( struct fd
*fd
, int event
);
69 static struct list
*process_get_kernel_obj_list( struct object
*obj
);
70 static void process_destroy( struct object
*obj
);
71 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
);
73 static const struct object_ops process_ops
=
75 sizeof(struct process
), /* size */
76 process_dump
, /* dump */
77 process_get_type
, /* get_type */
78 add_queue
, /* add_queue */
79 remove_queue
, /* remove_queue */
80 process_signaled
, /* signaled */
81 no_satisfied
, /* satisfied */
82 no_signal
, /* signal */
83 no_get_fd
, /* get_fd */
84 process_map_access
, /* map_access */
85 process_get_sd
, /* get_sd */
86 default_set_sd
, /* set_sd */
87 no_get_full_name
, /* get_full_name */
88 no_lookup_name
, /* lookup_name */
89 no_link_name
, /* link_name */
90 NULL
, /* unlink_name */
91 no_open_file
, /* open_file */
92 process_get_kernel_obj_list
, /* get_kernel_obj_list */
93 no_close_handle
, /* close_handle */
94 process_destroy
/* destroy */
97 static const struct fd_ops process_fd_ops
=
99 NULL
, /* get_poll_events */
100 process_poll_event
, /* poll_event */
102 NULL
, /* get_fd_type */
104 NULL
, /* queue_async */
105 NULL
, /* reselect_async */
106 NULL
/* cancel async */
109 /* process startup info */
113 struct object obj
; /* object header */
114 struct process
*process
; /* created process */
115 data_size_t info_size
; /* size of startup info */
116 data_size_t data_size
; /* size of whole startup data */
117 startup_info_t
*data
; /* data for startup info */
120 static void startup_info_dump( struct object
*obj
, int verbose
);
121 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
122 static void startup_info_destroy( struct object
*obj
);
124 static const struct object_ops startup_info_ops
=
126 sizeof(struct startup_info
), /* size */
127 startup_info_dump
, /* dump */
128 no_get_type
, /* get_type */
129 add_queue
, /* add_queue */
130 remove_queue
, /* remove_queue */
131 startup_info_signaled
, /* signaled */
132 no_satisfied
, /* satisfied */
133 no_signal
, /* signal */
134 no_get_fd
, /* get_fd */
135 no_map_access
, /* map_access */
136 default_get_sd
, /* get_sd */
137 default_set_sd
, /* set_sd */
138 no_get_full_name
, /* get_full_name */
139 no_lookup_name
, /* lookup_name */
140 no_link_name
, /* link_name */
141 NULL
, /* unlink_name */
142 no_open_file
, /* open_file */
143 no_kernel_obj_list
, /* get_kernel_obj_list */
144 no_close_handle
, /* close_handle */
145 startup_info_destroy
/* destroy */
150 static void job_dump( struct object
*obj
, int verbose
);
151 static struct object_type
*job_get_type( struct object
*obj
);
152 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
153 static unsigned int job_map_access( struct object
*obj
, unsigned int access
);
154 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
155 static void job_destroy( struct object
*obj
);
159 struct object obj
; /* object header */
160 struct list process_list
; /* list of all processes */
161 int num_processes
; /* count of running processes */
162 int total_processes
; /* count of processes which have been assigned */
163 unsigned int limit_flags
; /* limit flags */
164 int terminating
; /* job is terminating */
165 int signaled
; /* job is signaled */
166 struct completion
*completion_port
; /* associated completion port */
167 apc_param_t completion_key
; /* key to send with completion messages */
170 static const struct object_ops job_ops
=
172 sizeof(struct job
), /* size */
174 job_get_type
, /* get_type */
175 add_queue
, /* add_queue */
176 remove_queue
, /* remove_queue */
177 job_signaled
, /* signaled */
178 no_satisfied
, /* satisfied */
179 no_signal
, /* signal */
180 no_get_fd
, /* get_fd */
181 job_map_access
, /* map_access */
182 default_get_sd
, /* get_sd */
183 default_set_sd
, /* set_sd */
184 default_get_full_name
, /* get_full_name */
185 no_lookup_name
, /* lookup_name */
186 directory_link_name
, /* link_name */
187 default_unlink_name
, /* unlink_name */
188 no_open_file
, /* open_file */
189 no_kernel_obj_list
, /* get_kernel_obj_list */
190 job_close_handle
, /* close_handle */
191 job_destroy
/* destroy */
194 static struct job
*create_job_object( struct object
*root
, const struct unicode_str
*name
,
195 unsigned int attr
, const struct security_descriptor
*sd
)
199 if ((job
= create_named_object( root
, &job_ops
, name
, attr
, sd
)))
201 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
203 /* initialize it if it didn't already exist */
204 list_init( &job
->process_list
);
205 job
->num_processes
= 0;
206 job
->total_processes
= 0;
207 job
->limit_flags
= 0;
208 job
->terminating
= 0;
210 job
->completion_port
= NULL
;
211 job
->completion_key
= 0;
217 static struct job
*get_job_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
219 return (struct job
*)get_handle_obj( process
, handle
, access
, &job_ops
);
222 static struct object_type
*job_get_type( struct object
*obj
)
224 static const WCHAR name
[] = {'J','o','b'};
225 static const struct unicode_str str
= { name
, sizeof(name
) };
226 return get_object_type( &str
);
229 static unsigned int job_map_access( struct object
*obj
, unsigned int access
)
231 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
232 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
;
233 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
234 if (access
& GENERIC_ALL
) access
|= JOB_OBJECT_ALL_ACCESS
;
235 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
238 static void add_job_completion( struct job
*job
, apc_param_t msg
, apc_param_t pid
)
240 if (job
->completion_port
)
241 add_completion( job
->completion_port
, job
->completion_key
, pid
, STATUS_SUCCESS
, msg
);
244 static void add_job_process( struct job
*job
, struct process
*process
)
246 process
->job
= (struct job
*)grab_object( job
);
247 list_add_tail( &job
->process_list
, &process
->job_entry
);
248 job
->num_processes
++;
249 job
->total_processes
++;
251 add_job_completion( job
, JOB_OBJECT_MSG_NEW_PROCESS
, get_process_id(process
) );
254 /* called when a process has terminated, allow one additional process */
255 static void release_job_process( struct process
*process
)
257 struct job
*job
= process
->job
;
261 assert( job
->num_processes
);
262 job
->num_processes
--;
264 if (!job
->terminating
)
265 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
267 if (!job
->num_processes
)
268 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
271 static void terminate_job( struct job
*job
, int exit_code
)
273 /* don't report completion events for terminated processes */
274 job
->terminating
= 1;
276 for (;;) /* restart from the beginning of the list every time */
278 struct process
*process
;
280 /* find the first process associated with this job and still running */
281 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
283 if (process
->running_threads
) break;
285 if (&process
->job_entry
== &job
->process_list
) break; /* no process found */
286 assert( process
->job
== job
);
287 terminate_process( process
, NULL
, exit_code
);
290 job
->terminating
= 0;
292 wake_up( &job
->obj
, 0 );
295 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
297 struct job
*job
= (struct job
*)obj
;
298 assert( obj
->ops
== &job_ops
);
300 if (obj
->handle_count
== 1) /* last handle */
302 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
303 terminate_job( job
, 0 );
308 static void job_destroy( struct object
*obj
)
310 struct job
*job
= (struct job
*)obj
;
311 assert( obj
->ops
== &job_ops
);
313 assert( !job
->num_processes
);
314 assert( list_empty(&job
->process_list
) );
316 if (job
->completion_port
) release_object( job
->completion_port
);
319 static void job_dump( struct object
*obj
, int verbose
)
321 struct job
*job
= (struct job
*)obj
;
322 assert( obj
->ops
== &job_ops
);
323 fprintf( stderr
, "Job processes=%d\n", list_count(&job
->process_list
) );
326 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
328 struct job
*job
= (struct job
*)obj
;
329 return job
->signaled
;
334 void *ptr
; /* entry ptr */
335 unsigned int next
; /* next free entry */
338 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
339 static unsigned int used_ptid_entries
; /* number of entries in use */
340 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
341 static unsigned int next_free_ptid
; /* next free entry */
342 static unsigned int last_free_ptid
; /* last free entry */
343 static unsigned int num_free_ptids
; /* number of free ptids */
345 static void kill_all_processes(void);
347 #define PTID_OFFSET 8 /* offset for first ptid value */
349 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
350 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
352 /* allocate a new process or thread id */
353 unsigned int alloc_ptid( void *ptr
)
355 struct ptid_entry
*entry
;
358 if (used_ptid_entries
< alloc_ptid_entries
)
360 index
= used_ptid_entries
+ PTID_OFFSET
;
361 entry
= &ptid_entries
[used_ptid_entries
++];
363 else if (next_free_ptid
&& num_free_ptids
>= 256)
365 index
= next_free_ptid
;
366 entry
= &ptid_entries
[index
- PTID_OFFSET
];
367 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
370 else /* need to grow the array */
372 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
373 if (!count
) count
= 512;
374 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
376 set_error( STATUS_NO_MEMORY
);
379 ptid_entries
= entry
;
380 alloc_ptid_entries
= count
;
381 index
= used_ptid_entries
+ PTID_OFFSET
;
382 entry
= &ptid_entries
[used_ptid_entries
++];
386 return ptid_from_index( index
);
389 /* free a process or thread id */
390 void free_ptid( unsigned int id
)
392 unsigned int index
= index_from_ptid( id
);
393 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
398 /* append to end of free list so that we don't reuse it too early */
399 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
400 else next_free_ptid
= index
;
401 last_free_ptid
= index
;
405 /* retrieve the pointer corresponding to a process or thread id */
406 void *get_ptid_entry( unsigned int id
)
408 unsigned int index
= index_from_ptid( id
);
409 if (index
< PTID_OFFSET
) return NULL
;
410 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
411 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
414 /* return the main thread of the process */
415 struct thread
*get_process_first_thread( struct process
*process
)
417 struct list
*ptr
= list_head( &process
->thread_list
);
418 if (!ptr
) return NULL
;
419 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
422 /* set the state of the process startup info */
423 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
425 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
426 if (process
->startup_info
)
428 wake_up( &process
->startup_info
->obj
, 0 );
429 release_object( process
->startup_info
);
430 process
->startup_info
= NULL
;
434 /* callback for server shutdown */
435 static void server_shutdown_timeout( void *arg
)
437 shutdown_timeout
= NULL
;
438 if (!running_processes
)
440 close_master_socket( 0 );
443 switch(++shutdown_stage
)
445 case 1: /* signal system processes to exit */
446 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
447 if (shutdown_event
) set_event( shutdown_event
);
448 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
449 close_master_socket( 4 * -TICKS_PER_SEC
);
451 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
452 kill_all_processes();
457 /* forced shutdown, used for wineserver -k */
458 void shutdown_master_socket(void)
460 kill_all_processes();
462 if (shutdown_timeout
)
464 remove_timeout_user( shutdown_timeout
);
465 shutdown_timeout
= NULL
;
467 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
470 /* final cleanup once we are sure a process is really dead */
471 static void process_died( struct process
*process
)
473 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
474 if (!process
->is_system
)
476 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
477 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
479 release_object( process
);
480 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
483 /* callback for process sigkill timeout */
484 static void process_sigkill( void *private )
486 struct process
*process
= private;
488 process
->sigkill_timeout
= NULL
;
489 kill( process
->unix_pid
, SIGKILL
);
490 process_died( process
);
493 /* start the sigkill timer for a process upon exit */
494 static void start_sigkill_timer( struct process
*process
)
496 grab_object( process
);
497 if (process
->unix_pid
!= -1 && process
->msg_fd
)
498 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
500 process_died( process
);
503 /* create a new process */
504 /* if the function fails the fd is closed */
505 struct process
*create_process( int fd
, struct process
*parent
, int inherit_all
, const startup_info_t
*info
,
506 const struct security_descriptor
*sd
, const obj_handle_t
*handles
,
507 unsigned int handle_count
, struct token
*token
)
509 struct process
*process
;
511 if (!(process
= alloc_object( &process_ops
)))
516 process
->parent_id
= 0;
517 process
->debugger
= NULL
;
518 process
->debug_event
= NULL
;
519 process
->handles
= NULL
;
520 process
->msg_fd
= NULL
;
521 process
->sigkill_timeout
= NULL
;
522 process
->unix_pid
= -1;
523 process
->exit_code
= STILL_ACTIVE
;
524 process
->running_threads
= 0;
525 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
526 process
->suspend
= 0;
527 process
->is_system
= 0;
528 process
->debug_children
= 1;
529 process
->is_terminating
= 0;
531 process
->console
= NULL
;
532 process
->startup_state
= STARTUP_IN_PROGRESS
;
533 process
->startup_info
= NULL
;
534 process
->idle_event
= NULL
;
536 process
->ldt_copy
= 0;
537 process
->dir_cache
= NULL
;
538 process
->winstation
= 0;
539 process
->desktop
= 0;
540 process
->token
= NULL
;
541 process
->trace_data
= 0;
542 process
->rawinput_mouse
= NULL
;
543 process
->rawinput_kbd
= NULL
;
544 list_init( &process
->kernel_object
);
545 list_init( &process
->thread_list
);
546 list_init( &process
->locks
);
547 list_init( &process
->asyncs
);
548 list_init( &process
->classes
);
549 list_init( &process
->views
);
550 list_init( &process
->dlls
);
551 list_init( &process
->rawinput_devices
);
553 process
->end_time
= 0;
554 list_add_tail( &process_list
, &process
->entry
);
556 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
557 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
562 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
567 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
569 /* create the handle table */
572 process
->handles
= alloc_handle_table( process
, 0 );
573 process
->token
= token_create_admin();
574 process
->affinity
= ~0;
578 obj_handle_t std_handles
[3];
580 std_handles
[0] = info
->hstdin
;
581 std_handles
[1] = info
->hstdout
;
582 std_handles
[2] = info
->hstderr
;
584 process
->parent_id
= parent
->id
;
585 process
->handles
= inherit_all
? copy_handle_table( process
, parent
, handles
, handle_count
, std_handles
)
586 : alloc_handle_table( process
, 0 );
587 /* Note: for security reasons, starting a new process does not attempt
588 * to use the current impersonation token for the new process */
589 process
->token
= token_duplicate( token
? token
: parent
->token
, TRUE
, 0, NULL
, NULL
, 0, NULL
, 0 );
590 process
->affinity
= parent
->affinity
;
592 if (!process
->handles
|| !process
->token
) goto error
;
594 /* Assign a high security label to the token. The default would be medium
595 * but Wine provides admin access to all applications right now so high
596 * makes more sense for the time being. */
597 if (!token_assign_label( process
->token
, security_high_label_sid
))
600 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
604 if (process
) release_object( process
);
605 /* if we failed to start our first process, close everything down */
606 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
610 /* initialize the current process and fill in the request */
611 data_size_t
init_process( struct thread
*thread
)
613 struct process
*process
= thread
->process
;
614 struct startup_info
*info
= process
->startup_info
;
617 return info
->data_size
;
620 /* destroy a process when its refcount is 0 */
621 static void process_destroy( struct object
*obj
)
623 struct process
*process
= (struct process
*)obj
;
624 assert( obj
->ops
== &process_ops
);
626 /* we can't have a thread remaining */
627 assert( list_empty( &process
->thread_list
));
628 assert( list_empty( &process
->asyncs
));
630 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
632 close_process_handles( process
);
633 set_process_startup_state( process
, STARTUP_ABORTED
);
637 list_remove( &process
->job_entry
);
638 release_object( process
->job
);
640 if (process
->console
) release_object( process
->console
);
641 if (process
->msg_fd
) release_object( process
->msg_fd
);
642 list_remove( &process
->entry
);
643 if (process
->idle_event
) release_object( process
->idle_event
);
644 if (process
->id
) free_ptid( process
->id
);
645 if (process
->token
) release_object( process
->token
);
646 free( process
->dir_cache
);
649 /* dump a process on stdout for debugging purposes */
650 static void process_dump( struct object
*obj
, int verbose
)
652 struct process
*process
= (struct process
*)obj
;
653 assert( obj
->ops
== &process_ops
);
655 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
658 static struct object_type
*process_get_type( struct object
*obj
)
660 static const WCHAR name
[] = {'P','r','o','c','e','s','s'};
661 static const struct unicode_str str
= { name
, sizeof(name
) };
662 return get_object_type( &str
);
665 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
667 struct process
*process
= (struct process
*)obj
;
668 return !process
->running_threads
;
671 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
673 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
;
674 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
675 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
;
676 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
;
677 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
679 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
680 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
682 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
685 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
687 struct process
*process
= (struct process
*)obj
;
688 return &process
->kernel_object
;
691 static struct security_descriptor
*process_get_sd( struct object
*obj
)
693 static struct security_descriptor
*process_default_sd
;
695 if (obj
->sd
) return obj
->sd
;
697 if (!process_default_sd
)
699 size_t users_sid_len
= security_sid_len( security_domain_users_sid
);
700 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
701 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
702 + users_sid_len
+ admins_sid_len
;
703 ACCESS_ALLOWED_ACE
*aaa
;
706 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
708 process_default_sd
->control
= SE_DACL_PRESENT
;
709 process_default_sd
->owner_len
= admins_sid_len
;
710 process_default_sd
->group_len
= users_sid_len
;
711 process_default_sd
->sacl_len
= 0;
712 process_default_sd
->dacl_len
= dacl_len
;
713 memcpy( process_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
714 memcpy( (char *)(process_default_sd
+ 1) + admins_sid_len
, security_domain_users_sid
, users_sid_len
);
716 dacl
= (ACL
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
717 dacl
->AclRevision
= ACL_REVISION
;
719 dacl
->AclSize
= dacl_len
;
722 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
723 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
724 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
725 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
726 aaa
->Mask
= GENERIC_READ
;
727 memcpy( &aaa
->SidStart
, security_domain_users_sid
, users_sid_len
);
728 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
729 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
730 aaa
->Header
.AceFlags
= 0;
731 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
732 aaa
->Mask
= PROCESS_ALL_ACCESS
;
733 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
735 return process_default_sd
;
738 static void process_poll_event( struct fd
*fd
, int event
)
740 struct process
*process
= get_fd_user( fd
);
741 assert( process
->obj
.ops
== &process_ops
);
743 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
744 else if (event
& POLLIN
) receive_fd( process
);
747 static void startup_info_destroy( struct object
*obj
)
749 struct startup_info
*info
= (struct startup_info
*)obj
;
750 assert( obj
->ops
== &startup_info_ops
);
752 if (info
->process
) release_object( info
->process
);
755 static void startup_info_dump( struct object
*obj
, int verbose
)
757 struct startup_info
*info
= (struct startup_info
*)obj
;
758 assert( obj
->ops
== &startup_info_ops
);
760 fputs( "Startup info", stderr
);
762 fprintf( stderr
, " in=%04x out=%04x err=%04x",
763 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
764 fputc( '\n', stderr
);
767 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
769 struct startup_info
*info
= (struct startup_info
*)obj
;
770 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
773 /* get a process from an id (and increment the refcount) */
774 struct process
*get_process_from_id( process_id_t id
)
776 struct object
*obj
= get_ptid_entry( id
);
778 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
779 set_error( STATUS_INVALID_CID
);
783 /* get a process from a handle (and increment the refcount) */
784 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
786 return (struct process
*)get_handle_obj( current
->process
, handle
,
787 access
, &process_ops
);
790 /* find a dll from its base address */
791 static inline struct process_dll
*find_process_dll( struct process
*process
, mod_handle_t base
)
793 struct process_dll
*dll
;
795 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
797 if (dll
->base
== base
) return dll
;
802 /* add a dll to a process list */
803 static struct process_dll
*process_load_dll( struct process
*process
, mod_handle_t base
,
804 const WCHAR
*filename
, data_size_t name_len
)
806 struct process_dll
*dll
;
808 /* make sure we don't already have one with the same base address */
809 if (find_process_dll( process
, base
))
811 set_error( STATUS_INVALID_PARAMETER
);
815 if ((dll
= mem_alloc( sizeof(*dll
) )))
818 dll
->filename
= NULL
;
819 dll
->namelen
= name_len
;
820 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
825 list_add_tail( &process
->dlls
, &dll
->entry
);
830 /* remove a dll from a process list */
831 static void process_unload_dll( struct process
*process
, mod_handle_t base
)
833 struct process_dll
*dll
= find_process_dll( process
, base
);
835 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
837 free( dll
->filename
);
838 list_remove( &dll
->entry
);
840 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, &base
);
842 else set_error( STATUS_INVALID_PARAMETER
);
845 /* terminate a process with the given exit code */
846 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
848 struct thread
*thread
;
850 grab_object( process
); /* make sure it doesn't get freed when threads die */
851 process
->is_terminating
= 1;
854 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
856 if (exit_code
) thread
->exit_code
= exit_code
;
857 if (thread
== skip
) continue;
858 if (thread
->state
== TERMINATED
) continue;
859 kill_thread( thread
, 1 );
862 release_object( process
);
865 /* kill all processes */
866 static void kill_all_processes(void)
870 struct process
*process
;
872 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
874 if (process
->running_threads
) break;
876 if (&process
->entry
== &process_list
) break; /* no process found */
877 terminate_process( process
, NULL
, 1 );
881 /* kill all processes being attached to a console renderer */
882 void kill_console_processes( struct thread
*renderer
, int exit_code
)
884 for (;;) /* restart from the beginning of the list every time */
886 struct process
*process
;
888 /* find the first process being attached to 'renderer' and still running */
889 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
891 if (process
== renderer
->process
) continue;
892 if (!process
->running_threads
) continue;
893 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
895 if (&process
->entry
== &process_list
) break; /* no process found */
896 terminate_process( process
, NULL
, exit_code
);
900 /* a process has been killed (i.e. its last thread died) */
901 static void process_killed( struct process
*process
)
905 assert( list_empty( &process
->thread_list
));
906 process
->end_time
= current_time
;
907 if (!process
->is_system
) close_process_desktop( process
);
908 process
->winstation
= 0;
909 process
->desktop
= 0;
910 close_process_handles( process
);
911 cancel_process_asyncs( process
);
912 if (process
->idle_event
) release_object( process
->idle_event
);
913 process
->idle_event
= NULL
;
914 assert( !process
->console
);
916 while ((ptr
= list_head( &process
->rawinput_devices
)))
918 struct rawinput_device_entry
*entry
= LIST_ENTRY( ptr
, struct rawinput_device_entry
, entry
);
919 list_remove( &entry
->entry
);
922 while ((ptr
= list_head( &process
->dlls
)))
924 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
925 free( dll
->filename
);
926 list_remove( &dll
->entry
);
929 destroy_process_classes( process
);
930 free_mapped_views( process
);
931 free_process_user_handles( process
);
932 remove_process_locks( process
);
933 set_process_startup_state( process
, STARTUP_ABORTED
);
934 finish_process_tracing( process
);
935 release_job_process( process
);
936 start_sigkill_timer( process
);
937 wake_up( &process
->obj
, 0 );
940 /* add a thread to a process running threads list */
941 void add_process_thread( struct process
*process
, struct thread
*thread
)
943 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
944 if (!process
->running_threads
++)
947 if (!process
->is_system
)
949 if (!user_processes
++ && shutdown_timeout
)
951 remove_timeout_user( shutdown_timeout
);
952 shutdown_timeout
= NULL
;
956 grab_object( thread
);
959 /* remove a thread from a process running threads list */
960 void remove_process_thread( struct process
*process
, struct thread
*thread
)
962 assert( process
->running_threads
> 0 );
963 assert( !list_empty( &process
->thread_list
));
965 list_remove( &thread
->proc_entry
);
967 if (!--process
->running_threads
)
969 /* we have removed the last running thread, exit the process */
970 process
->exit_code
= thread
->exit_code
;
971 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
972 process_killed( process
);
974 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
975 release_object( thread
);
978 /* suspend all the threads of a process */
979 void suspend_process( struct process
*process
)
981 if (!process
->suspend
++)
983 struct list
*ptr
, *next
;
985 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
987 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
988 if (!thread
->suspend
) stop_thread( thread
);
993 /* resume all the threads of a process */
994 void resume_process( struct process
*process
)
996 assert (process
->suspend
> 0);
997 if (!--process
->suspend
)
999 struct list
*ptr
, *next
;
1001 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1003 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1004 if (!thread
->suspend
) wake_thread( thread
);
1009 /* kill a process on the spot */
1010 void kill_process( struct process
*process
, int violent_death
)
1012 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1014 release_object( process
->msg_fd
);
1015 process
->msg_fd
= NULL
;
1018 if (process
->sigkill_timeout
) /* already waiting for it to die */
1020 remove_timeout_user( process
->sigkill_timeout
);
1021 process
->sigkill_timeout
= NULL
;
1022 process_died( process
);
1026 if (violent_death
) terminate_process( process
, NULL
, 1 );
1031 grab_object( process
); /* make sure it doesn't get freed when threads die */
1032 while ((ptr
= list_head( &process
->thread_list
)))
1034 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1035 kill_thread( thread
, 0 );
1037 release_object( process
);
1041 /* kill all processes being debugged by a given thread */
1042 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
1044 for (;;) /* restart from the beginning of the list every time */
1046 struct process
*process
;
1048 /* find the first process being debugged by 'debugger' and still running */
1049 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1051 if (!process
->running_threads
) continue;
1052 if (process
->debugger
== debugger
) break;
1054 if (&process
->entry
== &process_list
) break; /* no process found */
1055 process
->debugger
= NULL
;
1056 terminate_process( process
, NULL
, exit_code
);
1061 /* detach a debugger from all its debuggees */
1062 void detach_debugged_processes( struct thread
*debugger
)
1064 struct process
*process
;
1066 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1068 if (process
->debugger
== debugger
&& process
->running_threads
)
1070 debugger_detach( process
, debugger
);
1076 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1078 struct list
*ptr
, *next
;
1080 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1082 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1083 if ((cb
)(process
, user
)) break;
1087 /* set the debugged flag in the process PEB */
1088 int set_process_debug_flag( struct process
*process
, int flag
)
1090 char data
= (flag
!= 0);
1092 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1093 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1096 /* create a new process */
1097 DECL_HANDLER(new_process
)
1099 struct startup_info
*info
;
1100 const void *info_ptr
;
1101 struct unicode_str name
;
1102 const struct security_descriptor
*sd
;
1103 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1104 struct process
*process
= NULL
;
1105 struct token
*token
= NULL
;
1106 struct process
*parent
;
1107 struct thread
*parent_thread
= current
;
1108 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1109 const obj_handle_t
*handles
= NULL
;
1111 if (socket_fd
== -1)
1113 set_error( STATUS_INVALID_PARAMETER
);
1118 set_error( STATUS_INVALID_PARAMETER
);
1122 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1124 set_error( STATUS_INVALID_HANDLE
);
1130 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1134 if (!is_cpu_supported( req
->cpu
))
1140 if (req
->parent_process
)
1142 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1147 parent_thread
= NULL
;
1149 else parent
= (struct process
*)grab_object( current
->process
);
1151 if (parent
->job
&& (req
->create_flags
& CREATE_BREAKAWAY_FROM_JOB
) &&
1152 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1154 set_error( STATUS_ACCESS_DENIED
);
1156 release_object( parent
);
1160 /* build the startup info for a new process */
1161 if (!(info
= alloc_object( &startup_info_ops
)))
1164 release_object( parent
);
1167 info
->process
= NULL
;
1170 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1172 if ((req
->handles_size
& 3) || req
->handles_size
> info
->data_size
)
1174 set_error( STATUS_INVALID_PARAMETER
);
1178 if (req
->handles_size
)
1181 info_ptr
= (const char *)info_ptr
+ req
->handles_size
;
1182 info
->data_size
-= req
->handles_size
;
1184 info
->info_size
= min( req
->info_size
, info
->data_size
);
1186 if (req
->info_size
< sizeof(*info
->data
))
1188 /* make sure we have a full startup_info_t structure */
1189 data_size_t env_size
= info
->data_size
- info
->info_size
;
1190 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1192 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1197 memcpy( info
->data
, info_ptr
, info_size
);
1198 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1199 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1200 info
->info_size
= sizeof(startup_info_t
);
1201 info
->data_size
= info
->info_size
+ env_size
;
1205 data_size_t pos
= sizeof(*info
->data
);
1207 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1212 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1213 FIXUP_LEN( info
->data
->curdir_len
);
1214 FIXUP_LEN( info
->data
->dllpath_len
);
1215 FIXUP_LEN( info
->data
->imagepath_len
);
1216 FIXUP_LEN( info
->data
->cmdline_len
);
1217 FIXUP_LEN( info
->data
->title_len
);
1218 FIXUP_LEN( info
->data
->desktop_len
);
1219 FIXUP_LEN( info
->data
->shellinfo_len
);
1220 FIXUP_LEN( info
->data
->runtime_len
);
1224 if (req
->token
&& !(token
= get_token_obj( current
->process
, req
->token
, TOKEN_QUERY
| TOKEN_ASSIGN_PRIMARY
)))
1230 if (!(process
= create_process( socket_fd
, parent
, req
->inherit_all
, info
->data
, sd
,
1231 handles
, req
->handles_size
/ sizeof(*handles
), token
)))
1234 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1237 && !(req
->create_flags
& CREATE_BREAKAWAY_FROM_JOB
)
1238 && !(parent
->job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
))
1240 add_job_process( parent
->job
, process
);
1243 /* connect to the window station */
1244 connect_process_winstation( process
, parent_thread
, parent
);
1246 /* set the process console */
1247 if (info
->data
->console
> 3)
1248 info
->data
->console
= duplicate_handle( parent
, info
->data
->console
, process
,
1249 0, 0, DUPLICATE_SAME_ACCESS
);
1251 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
1253 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1254 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1255 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1256 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1257 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1258 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1259 /* some handles above may have been invalid; this is not an error */
1260 if (get_error() == STATUS_INVALID_HANDLE
||
1261 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1263 /* attach to the debugger if requested */
1264 if (req
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
1266 set_process_debugger( process
, current
);
1267 process
->debug_children
= !(req
->create_flags
& DEBUG_ONLY_THIS_PROCESS
);
1269 else if (current
->process
->debugger
&& current
->process
->debug_children
)
1271 set_process_debugger( process
, current
->process
->debugger
);
1272 /* debug_children is set to 1 by default */
1275 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
1276 process
->group_id
= parent
->group_id
;
1278 info
->process
= (struct process
*)grab_object( process
);
1279 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1280 reply
->pid
= get_process_id( process
);
1281 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1284 if (process
) release_object( process
);
1285 if (token
) release_object( token
);
1286 release_object( parent
);
1287 release_object( info
);
1290 /* execute a new process, replacing the existing one */
1291 DECL_HANDLER(exec_process
)
1293 struct process
*process
;
1294 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1296 if (socket_fd
== -1)
1298 set_error( STATUS_INVALID_PARAMETER
);
1301 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1303 set_error( STATUS_INVALID_HANDLE
);
1309 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1313 if (!is_cpu_supported( req
->cpu
))
1318 if (!(process
= create_process( socket_fd
, NULL
, 0, NULL
, NULL
, NULL
, 0, NULL
))) return;
1319 create_thread( -1, process
, NULL
);
1320 release_object( process
);
1323 /* Retrieve information about a newly started process */
1324 DECL_HANDLER(get_new_process_info
)
1326 struct startup_info
*info
;
1328 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1329 0, &startup_info_ops
)))
1331 reply
->success
= is_process_init_done( info
->process
);
1332 reply
->exit_code
= info
->process
->exit_code
;
1333 release_object( info
);
1337 /* Retrieve the new process startup info */
1338 DECL_HANDLER(get_startup_info
)
1340 struct process
*process
= current
->process
;
1341 struct startup_info
*info
= process
->startup_info
;
1346 /* we return the data directly without making a copy so this can only be called once */
1347 reply
->info_size
= info
->info_size
;
1348 size
= info
->data_size
;
1349 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1350 set_reply_data_ptr( info
->data
, size
);
1352 info
->data_size
= 0;
1355 /* signal the end of the process initialization */
1356 DECL_HANDLER(init_process_done
)
1358 struct process_dll
*dll
;
1359 struct process
*process
= current
->process
;
1361 if (is_process_init_done(process
))
1363 set_error( STATUS_INVALID_PARAMETER
);
1366 if (!(dll
= find_process_dll( process
, req
->module
)))
1368 set_error( STATUS_DLL_NOT_FOUND
);
1372 /* main exe is the first in the dll list */
1373 list_remove( &dll
->entry
);
1374 list_add_head( &process
->dlls
, &dll
->entry
);
1376 process
->ldt_copy
= req
->ldt_copy
;
1377 process
->start_time
= current_time
;
1378 current
->entry_point
= req
->entry
;
1380 init_process_tracing( process
);
1381 generate_startup_debug_events( process
, req
->entry
);
1382 set_process_startup_state( process
, STARTUP_DONE
);
1384 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1385 if (process
->debugger
) set_process_debug_flag( process
, 1 );
1386 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1389 /* open a handle to a process */
1390 DECL_HANDLER(open_process
)
1392 struct process
*process
= get_process_from_id( req
->pid
);
1396 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1397 release_object( process
);
1401 /* terminate a process */
1402 DECL_HANDLER(terminate_process
)
1404 struct process
*process
;
1408 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1409 if (!process
) return;
1411 else process
= (struct process
*)grab_object( current
->process
);
1413 reply
->self
= (current
->process
== process
);
1414 terminate_process( process
, current
, req
->exit_code
);
1415 release_object( process
);
1418 /* fetch information about a process */
1419 DECL_HANDLER(get_process_info
)
1421 struct process
*process
;
1423 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1425 reply
->pid
= get_process_id( process
);
1426 reply
->ppid
= process
->parent_id
;
1427 reply
->exit_code
= process
->exit_code
;
1428 reply
->priority
= process
->priority
;
1429 reply
->affinity
= process
->affinity
;
1430 reply
->peb
= process
->peb
;
1431 reply
->start_time
= process
->start_time
;
1432 reply
->end_time
= process
->end_time
;
1433 reply
->cpu
= process
->cpu
;
1434 reply
->debugger_present
= !!process
->debugger
;
1435 reply
->debug_children
= process
->debug_children
;
1436 if (get_reply_max_size())
1438 const pe_image_info_t
*info
;
1439 struct process_dll
*exe
= get_process_exe_module( process
);
1440 if (exe
&& (info
= get_mapping_image_info( process
, exe
->base
)))
1441 set_reply_data( info
, min( sizeof(*info
), get_reply_max_size() ));
1443 release_object( process
);
1447 /* retrieve information about a process memory usage */
1448 DECL_HANDLER(get_process_vm_counters
)
1450 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1452 if (!process
) return;
1454 if (process
->unix_pid
!= -1)
1457 char proc_path
[32], line
[256];
1458 unsigned long value
;
1460 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1461 if ((f
= fopen( proc_path
, "r" )))
1463 while (fgets( line
, sizeof(line
), f
))
1465 if (sscanf( line
, "VmPeak: %lu", &value
))
1466 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1467 else if (sscanf( line
, "VmSize: %lu", &value
))
1468 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1469 else if (sscanf( line
, "VmHWM: %lu", &value
))
1470 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1471 else if (sscanf( line
, "VmRSS: %lu", &value
))
1472 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1473 else if (sscanf( line
, "RssAnon: %lu", &value
))
1474 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1475 else if (sscanf( line
, "VmSwap: %lu", &value
))
1476 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1478 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1481 else set_error( STATUS_ACCESS_DENIED
);
1483 else set_error( STATUS_ACCESS_DENIED
);
1485 release_object( process
);
1488 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1490 struct thread
*thread
;
1492 if (!process
->running_threads
)
1494 set_error( STATUS_PROCESS_IS_TERMINATING
);
1498 process
->affinity
= affinity
;
1500 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1502 set_thread_affinity( thread
, affinity
);
1506 /* set information about a process */
1507 DECL_HANDLER(set_process_info
)
1509 struct process
*process
;
1511 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1513 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1514 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1515 release_object( process
);
1519 /* read data from a process address space */
1520 DECL_HANDLER(read_process_memory
)
1522 struct process
*process
;
1523 data_size_t len
= get_reply_max_size();
1525 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1529 char *buffer
= mem_alloc( len
);
1532 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1533 set_reply_data_ptr( buffer
, len
);
1538 release_object( process
);
1541 /* write data to a process address space */
1542 DECL_HANDLER(write_process_memory
)
1544 struct process
*process
;
1546 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1548 data_size_t len
= get_req_data_size();
1549 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1550 else set_error( STATUS_INVALID_PARAMETER
);
1551 release_object( process
);
1555 /* notify the server that a dll has been loaded */
1556 DECL_HANDLER(load_dll
)
1558 struct process_dll
*dll
;
1560 if ((dll
= process_load_dll( current
->process
, req
->base
, get_req_data(), get_req_data_size() )))
1562 dll
->dbg_offset
= req
->dbg_offset
;
1563 dll
->dbg_size
= req
->dbg_size
;
1564 dll
->name
= req
->name
;
1565 /* only generate event if initialization is done */
1566 if (is_process_init_done( current
->process
))
1567 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1571 /* notify the server that a dll is being unloaded */
1572 DECL_HANDLER(unload_dll
)
1574 process_unload_dll( current
->process
, req
->base
);
1577 /* retrieve information about a module in a process */
1578 DECL_HANDLER(get_dll_info
)
1580 struct process
*process
;
1582 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1584 struct process_dll
*dll
;
1586 if (req
->base_address
)
1587 dll
= find_process_dll( process
, req
->base_address
);
1588 else /* NULL means main module */
1589 dll
= list_head( &process
->dlls
) ?
1590 LIST_ENTRY(list_head( &process
->dlls
), struct process_dll
, entry
) : NULL
;
1594 reply
->entry_point
= 0; /* FIXME */
1595 reply
->filename_len
= dll
->namelen
;
1598 if (dll
->namelen
<= get_reply_max_size())
1599 set_reply_data( dll
->filename
, dll
->namelen
);
1601 set_error( STATUS_BUFFER_TOO_SMALL
);
1605 set_error( STATUS_DLL_NOT_FOUND
);
1607 release_object( process
);
1611 /* retrieve the process idle event */
1612 DECL_HANDLER(get_process_idle_event
)
1614 struct process
*process
;
1617 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1619 if (process
->idle_event
&& process
!= current
->process
)
1620 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1621 EVENT_ALL_ACCESS
, 0 );
1622 release_object( process
);
1626 /* make the current process a system process */
1627 DECL_HANDLER(make_process_system
)
1629 struct process
*process
= current
->process
;
1631 if (!shutdown_event
)
1633 if (!(shutdown_event
= create_event( NULL
, NULL
, OBJ_PERMANENT
, 1, 0, NULL
))) return;
1634 release_object( shutdown_event
);
1637 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1640 if (!process
->is_system
)
1642 process
->is_system
= 1;
1643 close_process_desktop( process
);
1644 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1645 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1649 /* create a new job object */
1650 DECL_HANDLER(create_job
)
1653 struct unicode_str name
;
1654 struct object
*root
;
1655 const struct security_descriptor
*sd
;
1656 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1658 if (!objattr
) return;
1660 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1662 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1663 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1665 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1666 req
->access
, objattr
->attributes
);
1667 release_object( job
);
1669 if (root
) release_object( root
);
1672 /* open a job object */
1673 DECL_HANDLER(open_job
)
1675 struct unicode_str name
= get_req_unicode_str();
1677 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1678 &job_ops
, &name
, req
->attributes
);
1681 /* assign a job object to a process */
1682 DECL_HANDLER(assign_job
)
1684 struct process
*process
;
1685 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1689 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1691 if (!process
->running_threads
)
1692 set_error( STATUS_PROCESS_IS_TERMINATING
);
1693 else if (process
->job
)
1694 set_error( STATUS_ACCESS_DENIED
);
1696 add_job_process( job
, process
);
1697 release_object( process
);
1699 release_object( job
);
1703 /* check if a process is associated with a job */
1704 DECL_HANDLER(process_in_job
)
1706 struct process
*process
;
1709 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1714 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1716 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1718 set_error( process
->job
== job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1719 release_object( job
);
1721 release_object( process
);
1724 /* retrieve information about a job */
1725 DECL_HANDLER(get_job_info
)
1727 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1731 reply
->total_processes
= job
->total_processes
;
1732 reply
->active_processes
= job
->num_processes
;
1733 release_object( job
);
1736 /* terminate all processes associated with the job */
1737 DECL_HANDLER(terminate_job
)
1739 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1743 terminate_job( job
, req
->status
);
1744 release_object( job
);
1747 /* update limits of the job object */
1748 DECL_HANDLER(set_job_limits
)
1750 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1754 job
->limit_flags
= req
->limit_flags
;
1755 release_object( job
);
1758 /* set the jobs completion port */
1759 DECL_HANDLER(set_job_completion_port
)
1761 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1765 if (!job
->completion_port
)
1767 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1768 job
->completion_key
= req
->key
;
1771 set_error( STATUS_INVALID_PARAMETER
);
1773 release_object( job
);
1776 /* Suspend a process */
1777 DECL_HANDLER(suspend_process
)
1779 struct process
*process
;
1781 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1783 struct list
*ptr
, *next
;
1785 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1787 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1788 suspend_thread( thread
);
1791 release_object( process
);
1795 /* Resume a process */
1796 DECL_HANDLER(resume_process
)
1798 struct process
*process
;
1800 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1802 struct list
*ptr
, *next
;
1804 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1806 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1807 resume_thread( thread
);
1810 release_object( process
);
1814 /* Get a list of processes and threads currently running */
1815 DECL_HANDLER(list_processes
)
1817 struct process
*process
;
1818 struct thread
*thread
;
1819 unsigned int pos
= 0;
1822 reply
->process_count
= 0;
1823 reply
->info_size
= 0;
1825 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1827 struct process_dll
*exe
= get_process_exe_module( process
);
1828 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1829 reply
->info_size
+= sizeof(struct process_info
);
1830 if (exe
) reply
->info_size
+= exe
->namelen
;
1831 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1832 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1833 reply
->process_count
++;
1836 if (reply
->info_size
> get_reply_max_size())
1838 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1842 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1844 memset( buffer
, 0, reply
->info_size
);
1845 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1847 struct process_info
*process_info
;
1848 struct process_dll
*exe
= get_process_exe_module( process
);
1850 pos
= (pos
+ 7) & ~7;
1851 process_info
= (struct process_info
*)(buffer
+ pos
);
1852 process_info
->start_time
= process
->start_time
;
1853 process_info
->name_len
= exe
? exe
->namelen
: 0;
1854 process_info
->thread_count
= process
->running_threads
;
1855 process_info
->priority
= process
->priority
;
1856 process_info
->pid
= process
->id
;
1857 process_info
->parent_pid
= process
->parent_id
;
1858 process_info
->handle_count
= get_handle_table_count(process
);
1859 process_info
->unix_pid
= process
->unix_pid
;
1860 pos
+= sizeof(*process_info
);
1864 memcpy( buffer
+ pos
, exe
->filename
, exe
->namelen
);
1865 pos
+= exe
->namelen
;
1868 pos
= (pos
+ 7) & ~7;
1869 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1871 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1873 thread_info
->start_time
= thread
->creation_time
;
1874 thread_info
->tid
= thread
->id
;
1875 thread_info
->base_priority
= thread
->priority
;
1876 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1877 thread_info
->unix_tid
= thread
->unix_tid
;
1878 pos
+= sizeof(*thread_info
);