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_lookup_name
, /* lookup_name */
88 no_link_name
, /* link_name */
89 NULL
, /* unlink_name */
90 no_open_file
, /* open_file */
91 process_get_kernel_obj_list
, /* get_kernel_obj_list */
92 no_close_handle
, /* close_handle */
93 process_destroy
/* destroy */
96 static const struct fd_ops process_fd_ops
=
98 NULL
, /* get_poll_events */
99 process_poll_event
, /* poll_event */
101 NULL
, /* get_fd_type */
103 NULL
, /* queue_async */
104 NULL
, /* reselect_async */
105 NULL
/* cancel async */
108 /* process startup info */
112 struct object obj
; /* object header */
113 struct process
*process
; /* created process */
114 data_size_t info_size
; /* size of startup info */
115 data_size_t data_size
; /* size of whole startup data */
116 startup_info_t
*data
; /* data for startup info */
119 static void startup_info_dump( struct object
*obj
, int verbose
);
120 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
121 static void startup_info_destroy( struct object
*obj
);
123 static const struct object_ops startup_info_ops
=
125 sizeof(struct startup_info
), /* size */
126 startup_info_dump
, /* dump */
127 no_get_type
, /* get_type */
128 add_queue
, /* add_queue */
129 remove_queue
, /* remove_queue */
130 startup_info_signaled
, /* signaled */
131 no_satisfied
, /* satisfied */
132 no_signal
, /* signal */
133 no_get_fd
, /* get_fd */
134 no_map_access
, /* map_access */
135 default_get_sd
, /* get_sd */
136 default_set_sd
, /* set_sd */
137 no_lookup_name
, /* lookup_name */
138 no_link_name
, /* link_name */
139 NULL
, /* unlink_name */
140 no_open_file
, /* open_file */
141 no_kernel_obj_list
, /* get_kernel_obj_list */
142 no_close_handle
, /* close_handle */
143 startup_info_destroy
/* destroy */
148 static void job_dump( struct object
*obj
, int verbose
);
149 static struct object_type
*job_get_type( struct object
*obj
);
150 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
151 static unsigned int job_map_access( struct object
*obj
, unsigned int access
);
152 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
153 static void job_destroy( struct object
*obj
);
157 struct object obj
; /* object header */
158 struct list process_list
; /* list of all processes */
159 int num_processes
; /* count of running processes */
160 int total_processes
; /* count of processes which have been assigned */
161 unsigned int limit_flags
; /* limit flags */
162 int terminating
; /* job is terminating */
163 int signaled
; /* job is signaled */
164 struct completion
*completion_port
; /* associated completion port */
165 apc_param_t completion_key
; /* key to send with completion messages */
168 static const struct object_ops job_ops
=
170 sizeof(struct job
), /* size */
172 job_get_type
, /* get_type */
173 add_queue
, /* add_queue */
174 remove_queue
, /* remove_queue */
175 job_signaled
, /* signaled */
176 no_satisfied
, /* satisfied */
177 no_signal
, /* signal */
178 no_get_fd
, /* get_fd */
179 job_map_access
, /* map_access */
180 default_get_sd
, /* get_sd */
181 default_set_sd
, /* set_sd */
182 no_lookup_name
, /* lookup_name */
183 directory_link_name
, /* link_name */
184 default_unlink_name
, /* unlink_name */
185 no_open_file
, /* open_file */
186 no_kernel_obj_list
, /* get_kernel_obj_list */
187 job_close_handle
, /* close_handle */
188 job_destroy
/* destroy */
191 static struct job
*create_job_object( struct object
*root
, const struct unicode_str
*name
,
192 unsigned int attr
, const struct security_descriptor
*sd
)
196 if ((job
= create_named_object( root
, &job_ops
, name
, attr
, sd
)))
198 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
200 /* initialize it if it didn't already exist */
201 list_init( &job
->process_list
);
202 job
->num_processes
= 0;
203 job
->total_processes
= 0;
204 job
->limit_flags
= 0;
205 job
->terminating
= 0;
207 job
->completion_port
= NULL
;
208 job
->completion_key
= 0;
214 static struct job
*get_job_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
216 return (struct job
*)get_handle_obj( process
, handle
, access
, &job_ops
);
219 static struct object_type
*job_get_type( struct object
*obj
)
221 static const WCHAR name
[] = {'J','o','b'};
222 static const struct unicode_str str
= { name
, sizeof(name
) };
223 return get_object_type( &str
);
226 static unsigned int job_map_access( struct object
*obj
, unsigned int access
)
228 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
229 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
;
230 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
231 if (access
& GENERIC_ALL
) access
|= JOB_OBJECT_ALL_ACCESS
;
232 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
235 static void add_job_completion( struct job
*job
, apc_param_t msg
, apc_param_t pid
)
237 if (job
->completion_port
)
238 add_completion( job
->completion_port
, job
->completion_key
, pid
, STATUS_SUCCESS
, msg
);
241 static void add_job_process( struct job
*job
, struct process
*process
)
243 process
->job
= (struct job
*)grab_object( job
);
244 list_add_tail( &job
->process_list
, &process
->job_entry
);
245 job
->num_processes
++;
246 job
->total_processes
++;
248 add_job_completion( job
, JOB_OBJECT_MSG_NEW_PROCESS
, get_process_id(process
) );
251 /* called when a process has terminated, allow one additional process */
252 static void release_job_process( struct process
*process
)
254 struct job
*job
= process
->job
;
258 assert( job
->num_processes
);
259 job
->num_processes
--;
261 if (!job
->terminating
)
262 add_job_completion( job
, JOB_OBJECT_MSG_EXIT_PROCESS
, get_process_id(process
) );
264 if (!job
->num_processes
)
265 add_job_completion( job
, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO
, 0 );
268 static void terminate_job( struct job
*job
, int exit_code
)
270 /* don't report completion events for terminated processes */
271 job
->terminating
= 1;
273 for (;;) /* restart from the beginning of the list every time */
275 struct process
*process
;
277 /* find the first process associated with this job and still running */
278 LIST_FOR_EACH_ENTRY( process
, &job
->process_list
, struct process
, job_entry
)
280 if (process
->running_threads
) break;
282 if (&process
->job_entry
== &job
->process_list
) break; /* no process found */
283 assert( process
->job
== job
);
284 terminate_process( process
, NULL
, exit_code
);
287 job
->terminating
= 0;
289 wake_up( &job
->obj
, 0 );
292 static int job_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
294 struct job
*job
= (struct job
*)obj
;
295 assert( obj
->ops
== &job_ops
);
297 if (obj
->handle_count
== 1) /* last handle */
299 if (job
->limit_flags
& JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
)
300 terminate_job( job
, 0 );
305 static void job_destroy( struct object
*obj
)
307 struct job
*job
= (struct job
*)obj
;
308 assert( obj
->ops
== &job_ops
);
310 assert( !job
->num_processes
);
311 assert( list_empty(&job
->process_list
) );
313 if (job
->completion_port
) release_object( job
->completion_port
);
316 static void job_dump( struct object
*obj
, int verbose
)
318 struct job
*job
= (struct job
*)obj
;
319 assert( obj
->ops
== &job_ops
);
320 fprintf( stderr
, "Job processes=%d\n", list_count(&job
->process_list
) );
323 static int job_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
325 struct job
*job
= (struct job
*)obj
;
326 return job
->signaled
;
331 void *ptr
; /* entry ptr */
332 unsigned int next
; /* next free entry */
335 static struct ptid_entry
*ptid_entries
; /* array of ptid entries */
336 static unsigned int used_ptid_entries
; /* number of entries in use */
337 static unsigned int alloc_ptid_entries
; /* number of allocated entries */
338 static unsigned int next_free_ptid
; /* next free entry */
339 static unsigned int last_free_ptid
; /* last free entry */
340 static unsigned int num_free_ptids
; /* number of free ptids */
342 static void kill_all_processes(void);
344 #define PTID_OFFSET 8 /* offset for first ptid value */
346 static unsigned int index_from_ptid(unsigned int id
) { return id
/ 4; }
347 static unsigned int ptid_from_index(unsigned int index
) { return index
* 4; }
349 /* allocate a new process or thread id */
350 unsigned int alloc_ptid( void *ptr
)
352 struct ptid_entry
*entry
;
355 if (used_ptid_entries
< alloc_ptid_entries
)
357 index
= used_ptid_entries
+ PTID_OFFSET
;
358 entry
= &ptid_entries
[used_ptid_entries
++];
360 else if (next_free_ptid
&& num_free_ptids
>= 256)
362 index
= next_free_ptid
;
363 entry
= &ptid_entries
[index
- PTID_OFFSET
];
364 if (!(next_free_ptid
= entry
->next
)) last_free_ptid
= 0;
367 else /* need to grow the array */
369 unsigned int count
= alloc_ptid_entries
+ (alloc_ptid_entries
/ 2);
370 if (!count
) count
= 512;
371 if (!(entry
= realloc( ptid_entries
, count
* sizeof(*entry
) )))
373 set_error( STATUS_NO_MEMORY
);
376 ptid_entries
= entry
;
377 alloc_ptid_entries
= count
;
378 index
= used_ptid_entries
+ PTID_OFFSET
;
379 entry
= &ptid_entries
[used_ptid_entries
++];
383 return ptid_from_index( index
);
386 /* free a process or thread id */
387 void free_ptid( unsigned int id
)
389 unsigned int index
= index_from_ptid( id
);
390 struct ptid_entry
*entry
= &ptid_entries
[index
- PTID_OFFSET
];
395 /* append to end of free list so that we don't reuse it too early */
396 if (last_free_ptid
) ptid_entries
[last_free_ptid
- PTID_OFFSET
].next
= index
;
397 else next_free_ptid
= index
;
398 last_free_ptid
= index
;
402 /* retrieve the pointer corresponding to a process or thread id */
403 void *get_ptid_entry( unsigned int id
)
405 unsigned int index
= index_from_ptid( id
);
406 if (index
< PTID_OFFSET
) return NULL
;
407 if (index
- PTID_OFFSET
>= used_ptid_entries
) return NULL
;
408 return ptid_entries
[index
- PTID_OFFSET
].ptr
;
411 /* return the main thread of the process */
412 struct thread
*get_process_first_thread( struct process
*process
)
414 struct list
*ptr
= list_head( &process
->thread_list
);
415 if (!ptr
) return NULL
;
416 return LIST_ENTRY( ptr
, struct thread
, proc_entry
);
419 /* set the state of the process startup info */
420 static void set_process_startup_state( struct process
*process
, enum startup_state state
)
422 if (process
->startup_state
== STARTUP_IN_PROGRESS
) process
->startup_state
= state
;
423 if (process
->startup_info
)
425 wake_up( &process
->startup_info
->obj
, 0 );
426 release_object( process
->startup_info
);
427 process
->startup_info
= NULL
;
431 /* callback for server shutdown */
432 static void server_shutdown_timeout( void *arg
)
434 shutdown_timeout
= NULL
;
435 if (!running_processes
)
437 close_master_socket( 0 );
440 switch(++shutdown_stage
)
442 case 1: /* signal system processes to exit */
443 if (debug_level
) fprintf( stderr
, "wineserver: shutting down\n" );
444 if (shutdown_event
) set_event( shutdown_event
);
445 shutdown_timeout
= add_timeout_user( 2 * -TICKS_PER_SEC
, server_shutdown_timeout
, NULL
);
446 close_master_socket( 4 * -TICKS_PER_SEC
);
448 case 2: /* now forcibly kill all processes (but still wait for SIGKILL timeouts) */
449 kill_all_processes();
454 /* forced shutdown, used for wineserver -k */
455 void shutdown_master_socket(void)
457 kill_all_processes();
459 if (shutdown_timeout
)
461 remove_timeout_user( shutdown_timeout
);
462 shutdown_timeout
= NULL
;
464 close_master_socket( 2 * -TICKS_PER_SEC
); /* for SIGKILL timeouts */
467 /* final cleanup once we are sure a process is really dead */
468 static void process_died( struct process
*process
)
470 if (debug_level
) fprintf( stderr
, "%04x: *process killed*\n", process
->id
);
471 if (!process
->is_system
)
473 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
474 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
476 release_object( process
);
477 if (!--running_processes
&& shutdown_stage
) close_master_socket( 0 );
480 /* callback for process sigkill timeout */
481 static void process_sigkill( void *private )
483 struct process
*process
= private;
485 process
->sigkill_timeout
= NULL
;
486 kill( process
->unix_pid
, SIGKILL
);
487 process_died( process
);
490 /* start the sigkill timer for a process upon exit */
491 static void start_sigkill_timer( struct process
*process
)
493 grab_object( process
);
494 if (process
->unix_pid
!= -1 && process
->msg_fd
)
495 process
->sigkill_timeout
= add_timeout_user( -TICKS_PER_SEC
, process_sigkill
, process
);
497 process_died( process
);
500 /* create a new process */
501 /* if the function fails the fd is closed */
502 struct process
*create_process( int fd
, struct process
*parent
, int inherit_all
,
503 const struct security_descriptor
*sd
)
505 struct process
*process
;
507 if (!(process
= alloc_object( &process_ops
)))
512 process
->parent_id
= 0;
513 process
->debugger
= NULL
;
514 process
->debug_event
= NULL
;
515 process
->handles
= NULL
;
516 process
->msg_fd
= NULL
;
517 process
->sigkill_timeout
= NULL
;
518 process
->unix_pid
= -1;
519 process
->exit_code
= STILL_ACTIVE
;
520 process
->running_threads
= 0;
521 process
->priority
= PROCESS_PRIOCLASS_NORMAL
;
522 process
->suspend
= 0;
523 process
->is_system
= 0;
524 process
->debug_children
= 1;
525 process
->is_terminating
= 0;
527 process
->console
= NULL
;
528 process
->startup_state
= STARTUP_IN_PROGRESS
;
529 process
->startup_info
= NULL
;
530 process
->idle_event
= NULL
;
531 process
->exe_file
= NULL
;
533 process
->ldt_copy
= 0;
534 process
->dir_cache
= NULL
;
535 process
->winstation
= 0;
536 process
->desktop
= 0;
537 process
->token
= NULL
;
538 process
->trace_data
= 0;
539 process
->rawinput_mouse
= NULL
;
540 process
->rawinput_kbd
= NULL
;
541 list_init( &process
->kernel_object
);
542 list_init( &process
->thread_list
);
543 list_init( &process
->locks
);
544 list_init( &process
->asyncs
);
545 list_init( &process
->classes
);
546 list_init( &process
->views
);
547 list_init( &process
->dlls
);
548 list_init( &process
->rawinput_devices
);
550 process
->end_time
= 0;
551 list_add_tail( &process_list
, &process
->entry
);
553 if (sd
&& !default_set_sd( &process
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
554 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
559 if (!(process
->id
= process
->group_id
= alloc_ptid( process
)))
564 if (!(process
->msg_fd
= create_anonymous_fd( &process_fd_ops
, fd
, &process
->obj
, 0 ))) goto error
;
566 /* create the handle table */
569 process
->handles
= alloc_handle_table( process
, 0 );
570 process
->token
= token_create_admin();
571 process
->affinity
= ~0;
575 process
->parent_id
= parent
->id
;
576 process
->handles
= inherit_all
? copy_handle_table( process
, parent
)
577 : alloc_handle_table( process
, 0 );
578 /* Note: for security reasons, starting a new process does not attempt
579 * to use the current impersonation token for the new process */
580 process
->token
= token_duplicate( parent
->token
, TRUE
, 0, NULL
);
581 process
->affinity
= parent
->affinity
;
583 if (!process
->handles
|| !process
->token
) goto error
;
585 /* Assign a high security label to the token. The default would be medium
586 * but Wine provides admin access to all applications right now so high
587 * makes more sense for the time being. */
588 if (!token_assign_label( process
->token
, security_high_label_sid
))
591 set_fd_events( process
->msg_fd
, POLLIN
); /* start listening to events */
595 if (process
) release_object( process
);
596 /* if we failed to start our first process, close everything down */
597 if (!running_processes
&& master_socket_timeout
!= TIMEOUT_INFINITE
) close_master_socket( 0 );
601 /* initialize the current process and fill in the request */
602 data_size_t
init_process( struct thread
*thread
)
604 struct process
*process
= thread
->process
;
605 struct startup_info
*info
= process
->startup_info
;
608 return info
->data_size
;
611 /* destroy a process when its refcount is 0 */
612 static void process_destroy( struct object
*obj
)
614 struct process
*process
= (struct process
*)obj
;
615 assert( obj
->ops
== &process_ops
);
617 /* we can't have a thread remaining */
618 assert( list_empty( &process
->thread_list
));
619 assert( list_empty( &process
->asyncs
));
621 assert( !process
->sigkill_timeout
); /* timeout should hold a reference to the process */
623 close_process_handles( process
);
624 set_process_startup_state( process
, STARTUP_ABORTED
);
628 list_remove( &process
->job_entry
);
629 release_object( process
->job
);
631 if (process
->console
) release_object( process
->console
);
632 if (process
->msg_fd
) release_object( process
->msg_fd
);
633 list_remove( &process
->entry
);
634 if (process
->idle_event
) release_object( process
->idle_event
);
635 if (process
->exe_file
) release_object( process
->exe_file
);
636 if (process
->id
) free_ptid( process
->id
);
637 if (process
->token
) release_object( process
->token
);
638 free( process
->dir_cache
);
641 /* dump a process on stdout for debugging purposes */
642 static void process_dump( struct object
*obj
, int verbose
)
644 struct process
*process
= (struct process
*)obj
;
645 assert( obj
->ops
== &process_ops
);
647 fprintf( stderr
, "Process id=%04x handles=%p\n", process
->id
, process
->handles
);
650 static struct object_type
*process_get_type( struct object
*obj
)
652 static const WCHAR name
[] = {'P','r','o','c','e','s','s'};
653 static const struct unicode_str str
= { name
, sizeof(name
) };
654 return get_object_type( &str
);
657 static int process_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
659 struct process
*process
= (struct process
*)obj
;
660 return !process
->running_threads
;
663 static unsigned int process_map_access( struct object
*obj
, unsigned int access
)
665 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
;
666 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| PROCESS_SET_QUOTA
| PROCESS_SET_INFORMATION
| PROCESS_SUSPEND_RESUME
|
667 PROCESS_VM_WRITE
| PROCESS_DUP_HANDLE
| PROCESS_CREATE_PROCESS
| PROCESS_CREATE_THREAD
| PROCESS_VM_OPERATION
;
668 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SYNCHRONIZE
| PROCESS_QUERY_LIMITED_INFORMATION
| PROCESS_TERMINATE
;
669 if (access
& GENERIC_ALL
) access
|= PROCESS_ALL_ACCESS
;
671 if (access
& PROCESS_QUERY_INFORMATION
) access
|= PROCESS_QUERY_LIMITED_INFORMATION
;
672 if (access
& PROCESS_SET_INFORMATION
) access
|= PROCESS_SET_LIMITED_INFORMATION
;
674 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
677 static struct list
*process_get_kernel_obj_list( struct object
*obj
)
679 struct process
*process
= (struct process
*)obj
;
680 return &process
->kernel_object
;
683 static struct security_descriptor
*process_get_sd( struct object
*obj
)
685 static struct security_descriptor
*process_default_sd
;
687 if (obj
->sd
) return obj
->sd
;
689 if (!process_default_sd
)
691 size_t users_sid_len
= security_sid_len( security_domain_users_sid
);
692 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
693 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
694 + users_sid_len
+ admins_sid_len
;
695 ACCESS_ALLOWED_ACE
*aaa
;
698 process_default_sd
= mem_alloc( sizeof(*process_default_sd
) + admins_sid_len
+ users_sid_len
700 process_default_sd
->control
= SE_DACL_PRESENT
;
701 process_default_sd
->owner_len
= admins_sid_len
;
702 process_default_sd
->group_len
= users_sid_len
;
703 process_default_sd
->sacl_len
= 0;
704 process_default_sd
->dacl_len
= dacl_len
;
705 memcpy( process_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
706 memcpy( (char *)(process_default_sd
+ 1) + admins_sid_len
, security_domain_users_sid
, users_sid_len
);
708 dacl
= (ACL
*)((char *)(process_default_sd
+ 1) + admins_sid_len
+ users_sid_len
);
709 dacl
->AclRevision
= ACL_REVISION
;
711 dacl
->AclSize
= dacl_len
;
714 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
715 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
716 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
717 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
718 aaa
->Mask
= GENERIC_READ
;
719 memcpy( &aaa
->SidStart
, security_domain_users_sid
, users_sid_len
);
720 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
721 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
722 aaa
->Header
.AceFlags
= 0;
723 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
724 aaa
->Mask
= PROCESS_ALL_ACCESS
;
725 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
727 return process_default_sd
;
730 static void process_poll_event( struct fd
*fd
, int event
)
732 struct process
*process
= get_fd_user( fd
);
733 assert( process
->obj
.ops
== &process_ops
);
735 if (event
& (POLLERR
| POLLHUP
)) kill_process( process
, 0 );
736 else if (event
& POLLIN
) receive_fd( process
);
739 static void startup_info_destroy( struct object
*obj
)
741 struct startup_info
*info
= (struct startup_info
*)obj
;
742 assert( obj
->ops
== &startup_info_ops
);
744 if (info
->process
) release_object( info
->process
);
747 static void startup_info_dump( struct object
*obj
, int verbose
)
749 struct startup_info
*info
= (struct startup_info
*)obj
;
750 assert( obj
->ops
== &startup_info_ops
);
752 fprintf( stderr
, "Startup info in=%04x out=%04x err=%04x\n",
753 info
->data
->hstdin
, info
->data
->hstdout
, info
->data
->hstderr
);
756 static int startup_info_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
758 struct startup_info
*info
= (struct startup_info
*)obj
;
759 return info
->process
&& info
->process
->startup_state
!= STARTUP_IN_PROGRESS
;
762 /* get a process from an id (and increment the refcount) */
763 struct process
*get_process_from_id( process_id_t id
)
765 struct object
*obj
= get_ptid_entry( id
);
767 if (obj
&& obj
->ops
== &process_ops
) return (struct process
*)grab_object( obj
);
768 set_error( STATUS_INVALID_PARAMETER
);
772 /* get a process from a handle (and increment the refcount) */
773 struct process
*get_process_from_handle( obj_handle_t handle
, unsigned int access
)
775 return (struct process
*)get_handle_obj( current
->process
, handle
,
776 access
, &process_ops
);
779 /* find a dll from its base address */
780 static inline struct process_dll
*find_process_dll( struct process
*process
, mod_handle_t base
)
782 struct process_dll
*dll
;
784 LIST_FOR_EACH_ENTRY( dll
, &process
->dlls
, struct process_dll
, entry
)
786 if (dll
->base
== base
) return dll
;
791 /* add a dll to a process list */
792 static struct process_dll
*process_load_dll( struct process
*process
, mod_handle_t base
,
793 const WCHAR
*filename
, data_size_t name_len
)
795 struct process_dll
*dll
;
797 /* make sure we don't already have one with the same base address */
798 if (find_process_dll( process
, base
))
800 set_error( STATUS_INVALID_PARAMETER
);
804 if ((dll
= mem_alloc( sizeof(*dll
) )))
807 dll
->filename
= NULL
;
808 dll
->namelen
= name_len
;
809 if (name_len
&& !(dll
->filename
= memdup( filename
, name_len
)))
814 list_add_tail( &process
->dlls
, &dll
->entry
);
819 /* remove a dll from a process list */
820 static void process_unload_dll( struct process
*process
, mod_handle_t base
)
822 struct process_dll
*dll
= find_process_dll( process
, base
);
824 if (dll
&& (&dll
->entry
!= list_head( &process
->dlls
))) /* main exe can't be unloaded */
826 free( dll
->filename
);
827 list_remove( &dll
->entry
);
829 generate_debug_event( current
, UNLOAD_DLL_DEBUG_EVENT
, &base
);
831 else set_error( STATUS_INVALID_PARAMETER
);
834 /* terminate a process with the given exit code */
835 static void terminate_process( struct process
*process
, struct thread
*skip
, int exit_code
)
837 struct thread
*thread
;
839 grab_object( process
); /* make sure it doesn't get freed when threads die */
840 process
->is_terminating
= 1;
843 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
845 if (exit_code
) thread
->exit_code
= exit_code
;
846 if (thread
== skip
) continue;
847 if (thread
->state
== TERMINATED
) continue;
848 kill_thread( thread
, 1 );
851 release_object( process
);
854 /* kill all processes */
855 static void kill_all_processes(void)
859 struct process
*process
;
861 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
863 if (process
->running_threads
) break;
865 if (&process
->entry
== &process_list
) break; /* no process found */
866 terminate_process( process
, NULL
, 1 );
870 /* kill all processes being attached to a console renderer */
871 void kill_console_processes( struct thread
*renderer
, int exit_code
)
873 for (;;) /* restart from the beginning of the list every time */
875 struct process
*process
;
877 /* find the first process being attached to 'renderer' and still running */
878 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
880 if (process
== renderer
->process
) continue;
881 if (!process
->running_threads
) continue;
882 if (process
->console
&& console_get_renderer( process
->console
) == renderer
) break;
884 if (&process
->entry
== &process_list
) break; /* no process found */
885 terminate_process( process
, NULL
, exit_code
);
889 /* a process has been killed (i.e. its last thread died) */
890 static void process_killed( struct process
*process
)
894 assert( list_empty( &process
->thread_list
));
895 process
->end_time
= current_time
;
896 if (!process
->is_system
) close_process_desktop( process
);
897 process
->winstation
= 0;
898 process
->desktop
= 0;
899 close_process_handles( process
);
900 cancel_process_asyncs( process
);
901 if (process
->idle_event
) release_object( process
->idle_event
);
902 if (process
->exe_file
) release_object( process
->exe_file
);
903 process
->idle_event
= NULL
;
904 process
->exe_file
= NULL
;
906 /* close the console attached to this process, if any */
907 free_console( process
);
909 while ((ptr
= list_head( &process
->rawinput_devices
)))
911 struct rawinput_device_entry
*entry
= LIST_ENTRY( ptr
, struct rawinput_device_entry
, entry
);
912 list_remove( &entry
->entry
);
915 while ((ptr
= list_head( &process
->dlls
)))
917 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
918 free( dll
->filename
);
919 list_remove( &dll
->entry
);
922 destroy_process_classes( process
);
923 free_mapped_views( process
);
924 free_process_user_handles( process
);
925 remove_process_locks( process
);
926 set_process_startup_state( process
, STARTUP_ABORTED
);
927 finish_process_tracing( process
);
928 release_job_process( process
);
929 start_sigkill_timer( process
);
930 wake_up( &process
->obj
, 0 );
933 /* add a thread to a process running threads list */
934 void add_process_thread( struct process
*process
, struct thread
*thread
)
936 list_add_tail( &process
->thread_list
, &thread
->proc_entry
);
937 if (!process
->running_threads
++)
940 if (!process
->is_system
)
942 if (!user_processes
++ && shutdown_timeout
)
944 remove_timeout_user( shutdown_timeout
);
945 shutdown_timeout
= NULL
;
949 grab_object( thread
);
952 /* remove a thread from a process running threads list */
953 void remove_process_thread( struct process
*process
, struct thread
*thread
)
955 assert( process
->running_threads
> 0 );
956 assert( !list_empty( &process
->thread_list
));
958 list_remove( &thread
->proc_entry
);
960 if (!--process
->running_threads
)
962 /* we have removed the last running thread, exit the process */
963 process
->exit_code
= thread
->exit_code
;
964 generate_debug_event( thread
, EXIT_PROCESS_DEBUG_EVENT
, process
);
965 process_killed( process
);
967 else generate_debug_event( thread
, EXIT_THREAD_DEBUG_EVENT
, thread
);
968 release_object( thread
);
971 /* suspend all the threads of a process */
972 void suspend_process( struct process
*process
)
974 if (!process
->suspend
++)
976 struct list
*ptr
, *next
;
978 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
980 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
981 if (!thread
->suspend
) stop_thread( thread
);
986 /* resume all the threads of a process */
987 void resume_process( struct process
*process
)
989 assert (process
->suspend
> 0);
990 if (!--process
->suspend
)
992 struct list
*ptr
, *next
;
994 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
996 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
997 if (!thread
->suspend
) wake_thread( thread
);
1002 /* kill a process on the spot */
1003 void kill_process( struct process
*process
, int violent_death
)
1005 if (!violent_death
&& process
->msg_fd
) /* normal termination on pipe close */
1007 release_object( process
->msg_fd
);
1008 process
->msg_fd
= NULL
;
1011 if (process
->sigkill_timeout
) /* already waiting for it to die */
1013 remove_timeout_user( process
->sigkill_timeout
);
1014 process
->sigkill_timeout
= NULL
;
1015 process_died( process
);
1019 if (violent_death
) terminate_process( process
, NULL
, 1 );
1024 grab_object( process
); /* make sure it doesn't get freed when threads die */
1025 while ((ptr
= list_head( &process
->thread_list
)))
1027 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1028 kill_thread( thread
, 0 );
1030 release_object( process
);
1034 /* kill all processes being debugged by a given thread */
1035 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
1037 for (;;) /* restart from the beginning of the list every time */
1039 struct process
*process
;
1041 /* find the first process being debugged by 'debugger' and still running */
1042 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1044 if (!process
->running_threads
) continue;
1045 if (process
->debugger
== debugger
) break;
1047 if (&process
->entry
== &process_list
) break; /* no process found */
1048 process
->debugger
= NULL
;
1049 terminate_process( process
, NULL
, exit_code
);
1054 /* detach a debugger from all its debuggees */
1055 void detach_debugged_processes( struct thread
*debugger
)
1057 struct process
*process
;
1059 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1061 if (process
->debugger
== debugger
&& process
->running_threads
)
1063 debugger_detach( process
, debugger
);
1069 void enum_processes( int (*cb
)(struct process
*, void*), void *user
)
1071 struct list
*ptr
, *next
;
1073 LIST_FOR_EACH_SAFE( ptr
, next
, &process_list
)
1075 struct process
*process
= LIST_ENTRY( ptr
, struct process
, entry
);
1076 if ((cb
)(process
, user
)) break;
1080 /* set the debugged flag in the process PEB */
1081 int set_process_debug_flag( struct process
*process
, int flag
)
1083 char data
= (flag
!= 0);
1085 /* BeingDebugged flag is the byte at offset 2 in the PEB */
1086 return write_process_memory( process
, process
->peb
+ 2, 1, &data
);
1089 /* create a new process */
1090 DECL_HANDLER(new_process
)
1092 struct startup_info
*info
;
1093 const void *info_ptr
;
1094 struct unicode_str name
;
1095 const struct security_descriptor
*sd
;
1096 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
1097 struct process
*process
= NULL
;
1098 struct process
*parent
;
1099 struct thread
*parent_thread
= current
;
1100 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1102 if (socket_fd
== -1)
1104 set_error( STATUS_INVALID_PARAMETER
);
1109 set_error( STATUS_INVALID_PARAMETER
);
1113 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1115 set_error( STATUS_INVALID_HANDLE
);
1121 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1125 if (!is_cpu_supported( req
->cpu
))
1131 if (req
->parent_process
)
1133 if (!(parent
= get_process_from_handle( req
->parent_process
, PROCESS_CREATE_PROCESS
)))
1138 parent_thread
= NULL
;
1140 else parent
= (struct process
*)grab_object( current
->process
);
1142 if (parent
->job
&& (req
->create_flags
& CREATE_BREAKAWAY_FROM_JOB
) &&
1143 !(parent
->job
->limit_flags
& (JOB_OBJECT_LIMIT_BREAKAWAY_OK
| JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
)))
1145 set_error( STATUS_ACCESS_DENIED
);
1147 release_object( parent
);
1151 /* build the startup info for a new process */
1152 if (!(info
= alloc_object( &startup_info_ops
)))
1155 release_object( parent
);
1158 info
->process
= NULL
;
1161 info_ptr
= get_req_data_after_objattr( objattr
, &info
->data_size
);
1162 info
->info_size
= min( req
->info_size
, info
->data_size
);
1164 if (req
->info_size
< sizeof(*info
->data
))
1166 /* make sure we have a full startup_info_t structure */
1167 data_size_t env_size
= info
->data_size
- info
->info_size
;
1168 data_size_t info_size
= min( req
->info_size
, FIELD_OFFSET( startup_info_t
, curdir_len
));
1170 if (!(info
->data
= mem_alloc( sizeof(*info
->data
) + env_size
)))
1175 memcpy( info
->data
, info_ptr
, info_size
);
1176 memset( (char *)info
->data
+ info_size
, 0, sizeof(*info
->data
) - info_size
);
1177 memcpy( info
->data
+ 1, (const char *)info_ptr
+ req
->info_size
, env_size
);
1178 info
->info_size
= sizeof(startup_info_t
);
1179 info
->data_size
= info
->info_size
+ env_size
;
1183 data_size_t pos
= sizeof(*info
->data
);
1185 if (!(info
->data
= memdup( info_ptr
, info
->data_size
)))
1190 #define FIXUP_LEN(len) do { (len) = min( (len), info->info_size - pos ); pos += (len); } while(0)
1191 FIXUP_LEN( info
->data
->curdir_len
);
1192 FIXUP_LEN( info
->data
->dllpath_len
);
1193 FIXUP_LEN( info
->data
->imagepath_len
);
1194 FIXUP_LEN( info
->data
->cmdline_len
);
1195 FIXUP_LEN( info
->data
->title_len
);
1196 FIXUP_LEN( info
->data
->desktop_len
);
1197 FIXUP_LEN( info
->data
->shellinfo_len
);
1198 FIXUP_LEN( info
->data
->runtime_len
);
1202 if (!(process
= create_process( socket_fd
, parent
, req
->inherit_all
, sd
))) goto done
;
1204 process
->startup_info
= (struct startup_info
*)grab_object( info
);
1206 if (req
->exe_file
&&
1207 !(process
->exe_file
= get_file_obj( current
->process
, req
->exe_file
, FILE_READ_DATA
)))
1211 && !(req
->create_flags
& CREATE_BREAKAWAY_FROM_JOB
)
1212 && !(parent
->job
->limit_flags
& JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
))
1214 add_job_process( parent
->job
, process
);
1217 /* connect to the window station */
1218 connect_process_winstation( process
, parent_thread
, parent
);
1220 /* set the process console */
1221 if (!(req
->create_flags
& (DETACHED_PROCESS
| CREATE_NEW_CONSOLE
)))
1223 /* FIXME: some better error checking should be done...
1224 * like if hConOut and hConIn are console handles, then they should be on the same
1227 info
->data
->console
= inherit_console( parent_thread
, parent
,
1228 process
, req
->inherit_all
? info
->data
->hstdin
: 0 );
1231 if (!req
->inherit_all
&& !(req
->create_flags
& CREATE_NEW_CONSOLE
))
1233 info
->data
->hstdin
= duplicate_handle( parent
, info
->data
->hstdin
, process
,
1234 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1235 info
->data
->hstdout
= duplicate_handle( parent
, info
->data
->hstdout
, process
,
1236 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1237 info
->data
->hstderr
= duplicate_handle( parent
, info
->data
->hstderr
, process
,
1238 0, OBJ_INHERIT
, DUPLICATE_SAME_ACCESS
);
1239 /* some handles above may have been invalid; this is not an error */
1240 if (get_error() == STATUS_INVALID_HANDLE
||
1241 get_error() == STATUS_OBJECT_TYPE_MISMATCH
) clear_error();
1243 /* attach to the debugger if requested */
1244 if (req
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
1246 set_process_debugger( process
, current
);
1247 process
->debug_children
= !(req
->create_flags
& DEBUG_ONLY_THIS_PROCESS
);
1249 else if (current
->process
->debugger
&& current
->process
->debug_children
)
1251 set_process_debugger( process
, current
->process
->debugger
);
1252 /* debug_children is set to 1 by default */
1255 if (!(req
->create_flags
& CREATE_NEW_PROCESS_GROUP
))
1256 process
->group_id
= parent
->group_id
;
1258 info
->process
= (struct process
*)grab_object( process
);
1259 reply
->info
= alloc_handle( current
->process
, info
, SYNCHRONIZE
, 0 );
1260 reply
->pid
= get_process_id( process
);
1261 reply
->handle
= alloc_handle_no_access_check( current
->process
, process
, req
->access
, objattr
->attributes
);
1264 if (process
) release_object( process
);
1265 release_object( parent
);
1266 release_object( info
);
1269 /* execute a new process, replacing the existing one */
1270 DECL_HANDLER(exec_process
)
1272 struct process
*process
;
1273 int socket_fd
= thread_get_inflight_fd( current
, req
->socket_fd
);
1275 if (socket_fd
== -1)
1277 set_error( STATUS_INVALID_PARAMETER
);
1280 if (fcntl( socket_fd
, F_SETFL
, O_NONBLOCK
) == -1)
1282 set_error( STATUS_INVALID_HANDLE
);
1288 set_error( STATUS_SHUTDOWN_IN_PROGRESS
);
1292 if (!is_cpu_supported( req
->cpu
))
1297 if (!(process
= create_process( socket_fd
, NULL
, 0, NULL
))) return;
1298 create_thread( -1, process
, NULL
);
1299 release_object( process
);
1302 /* Retrieve information about a newly started process */
1303 DECL_HANDLER(get_new_process_info
)
1305 struct startup_info
*info
;
1307 if ((info
= (struct startup_info
*)get_handle_obj( current
->process
, req
->info
,
1308 0, &startup_info_ops
)))
1310 reply
->success
= is_process_init_done( info
->process
);
1311 reply
->exit_code
= info
->process
->exit_code
;
1312 release_object( info
);
1316 /* Retrieve the new process startup info */
1317 DECL_HANDLER(get_startup_info
)
1319 struct process
*process
= current
->process
;
1320 struct startup_info
*info
= process
->startup_info
;
1325 /* we return the data directly without making a copy so this can only be called once */
1326 reply
->info_size
= info
->info_size
;
1327 size
= info
->data_size
;
1328 if (size
> get_reply_max_size()) size
= get_reply_max_size();
1329 set_reply_data_ptr( info
->data
, size
);
1331 info
->data_size
= 0;
1334 /* signal the end of the process initialization */
1335 DECL_HANDLER(init_process_done
)
1337 struct process_dll
*dll
;
1338 struct process
*process
= current
->process
;
1340 if (is_process_init_done(process
))
1342 set_error( STATUS_INVALID_PARAMETER
);
1345 if (!(dll
= find_process_dll( process
, req
->module
)))
1347 set_error( STATUS_DLL_NOT_FOUND
);
1351 /* main exe is the first in the dll list */
1352 list_remove( &dll
->entry
);
1353 list_add_head( &process
->dlls
, &dll
->entry
);
1355 process
->ldt_copy
= req
->ldt_copy
;
1356 process
->start_time
= current_time
;
1357 current
->entry_point
= req
->entry
;
1358 if (process
->exe_file
) release_object( process
->exe_file
);
1359 process
->exe_file
= NULL
;
1361 init_process_tracing( process
);
1362 generate_startup_debug_events( process
, req
->entry
);
1363 set_process_startup_state( process
, STARTUP_DONE
);
1365 if (req
->gui
) process
->idle_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
1366 if (process
->debugger
) set_process_debug_flag( process
, 1 );
1367 reply
->suspend
= (current
->suspend
|| process
->suspend
);
1370 /* open a handle to a process */
1371 DECL_HANDLER(open_process
)
1373 struct process
*process
= get_process_from_id( req
->pid
);
1377 reply
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->attributes
);
1378 release_object( process
);
1382 /* terminate a process */
1383 DECL_HANDLER(terminate_process
)
1385 struct process
*process
;
1389 process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
);
1390 if (!process
) return;
1392 else process
= (struct process
*)grab_object( current
->process
);
1394 reply
->self
= (current
->process
== process
);
1395 terminate_process( process
, current
, req
->exit_code
);
1396 release_object( process
);
1399 /* fetch information about a process */
1400 DECL_HANDLER(get_process_info
)
1402 struct process
*process
;
1404 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1406 reply
->pid
= get_process_id( process
);
1407 reply
->ppid
= process
->parent_id
;
1408 reply
->exit_code
= process
->exit_code
;
1409 reply
->priority
= process
->priority
;
1410 reply
->affinity
= process
->affinity
;
1411 reply
->peb
= process
->peb
;
1412 reply
->start_time
= process
->start_time
;
1413 reply
->end_time
= process
->end_time
;
1414 reply
->cpu
= process
->cpu
;
1415 reply
->debugger_present
= !!process
->debugger
;
1416 reply
->debug_children
= process
->debug_children
;
1417 if (get_reply_max_size())
1419 const pe_image_info_t
*info
;
1420 struct process_dll
*exe
= get_process_exe_module( process
);
1421 if (exe
&& (info
= get_mapping_image_info( process
, exe
->base
)))
1422 set_reply_data( info
, min( sizeof(*info
), get_reply_max_size() ));
1424 release_object( process
);
1428 /* retrieve information about a process memory usage */
1429 DECL_HANDLER(get_process_vm_counters
)
1431 struct process
*process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
);
1433 if (!process
) return;
1435 if (process
->unix_pid
!= -1)
1438 char proc_path
[32], line
[256];
1439 unsigned long value
;
1441 sprintf( proc_path
, "/proc/%u/status", process
->unix_pid
);
1442 if ((f
= fopen( proc_path
, "r" )))
1444 while (fgets( line
, sizeof(line
), f
))
1446 if (sscanf( line
, "VmPeak: %lu", &value
))
1447 reply
->peak_virtual_size
= (mem_size_t
)value
* 1024;
1448 else if (sscanf( line
, "VmSize: %lu", &value
))
1449 reply
->virtual_size
= (mem_size_t
)value
* 1024;
1450 else if (sscanf( line
, "VmHWM: %lu", &value
))
1451 reply
->peak_working_set_size
= (mem_size_t
)value
* 1024;
1452 else if (sscanf( line
, "VmRSS: %lu", &value
))
1453 reply
->working_set_size
= (mem_size_t
)value
* 1024;
1454 else if (sscanf( line
, "RssAnon: %lu", &value
))
1455 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1456 else if (sscanf( line
, "VmSwap: %lu", &value
))
1457 reply
->pagefile_usage
+= (mem_size_t
)value
* 1024;
1459 reply
->peak_pagefile_usage
= reply
->pagefile_usage
;
1462 else set_error( STATUS_ACCESS_DENIED
);
1464 else set_error( STATUS_ACCESS_DENIED
);
1466 release_object( process
);
1469 static void set_process_affinity( struct process
*process
, affinity_t affinity
)
1471 struct thread
*thread
;
1473 if (!process
->running_threads
)
1475 set_error( STATUS_PROCESS_IS_TERMINATING
);
1479 process
->affinity
= affinity
;
1481 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1483 set_thread_affinity( thread
, affinity
);
1487 /* set information about a process */
1488 DECL_HANDLER(set_process_info
)
1490 struct process
*process
;
1492 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
1494 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
) process
->priority
= req
->priority
;
1495 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
) set_process_affinity( process
, req
->affinity
);
1496 release_object( process
);
1500 /* read data from a process address space */
1501 DECL_HANDLER(read_process_memory
)
1503 struct process
*process
;
1504 data_size_t len
= get_reply_max_size();
1506 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
))) return;
1510 char *buffer
= mem_alloc( len
);
1513 if (read_process_memory( process
, req
->addr
, len
, buffer
))
1514 set_reply_data_ptr( buffer
, len
);
1519 release_object( process
);
1522 /* write data to a process address space */
1523 DECL_HANDLER(write_process_memory
)
1525 struct process
*process
;
1527 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
1529 data_size_t len
= get_req_data_size();
1530 if (len
) write_process_memory( process
, req
->addr
, len
, get_req_data() );
1531 else set_error( STATUS_INVALID_PARAMETER
);
1532 release_object( process
);
1536 /* notify the server that a dll has been loaded */
1537 DECL_HANDLER(load_dll
)
1539 struct process_dll
*dll
;
1541 if ((dll
= process_load_dll( current
->process
, req
->base
, get_req_data(), get_req_data_size() )))
1543 dll
->dbg_offset
= req
->dbg_offset
;
1544 dll
->dbg_size
= req
->dbg_size
;
1545 dll
->name
= req
->name
;
1546 /* only generate event if initialization is done */
1547 if (is_process_init_done( current
->process
))
1548 generate_debug_event( current
, LOAD_DLL_DEBUG_EVENT
, dll
);
1552 /* notify the server that a dll is being unloaded */
1553 DECL_HANDLER(unload_dll
)
1555 process_unload_dll( current
->process
, req
->base
);
1558 /* retrieve information about a module in a process */
1559 DECL_HANDLER(get_dll_info
)
1561 struct process
*process
;
1563 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_LIMITED_INFORMATION
)))
1565 struct process_dll
*dll
;
1567 if (req
->base_address
)
1568 dll
= find_process_dll( process
, req
->base_address
);
1569 else /* NULL means main module */
1570 dll
= list_head( &process
->dlls
) ?
1571 LIST_ENTRY(list_head( &process
->dlls
), struct process_dll
, entry
) : NULL
;
1575 reply
->entry_point
= 0; /* FIXME */
1576 reply
->filename_len
= dll
->namelen
;
1579 if (dll
->namelen
<= get_reply_max_size())
1580 set_reply_data( dll
->filename
, dll
->namelen
);
1582 set_error( STATUS_BUFFER_TOO_SMALL
);
1586 set_error( STATUS_DLL_NOT_FOUND
);
1588 release_object( process
);
1592 /* retrieve the process idle event */
1593 DECL_HANDLER(get_process_idle_event
)
1595 struct process
*process
;
1598 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
1600 if (process
->idle_event
&& process
!= current
->process
)
1601 reply
->event
= alloc_handle( current
->process
, process
->idle_event
,
1602 EVENT_ALL_ACCESS
, 0 );
1603 release_object( process
);
1607 /* make the current process a system process */
1608 DECL_HANDLER(make_process_system
)
1610 struct process
*process
= current
->process
;
1612 if (!shutdown_event
)
1614 if (!(shutdown_event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
))) return;
1615 make_object_static( (struct object
*)shutdown_event
);
1618 if (!(reply
->event
= alloc_handle( current
->process
, shutdown_event
, SYNCHRONIZE
, 0 )))
1621 if (!process
->is_system
)
1623 process
->is_system
= 1;
1624 close_process_desktop( process
);
1625 if (!--user_processes
&& !shutdown_stage
&& master_socket_timeout
!= TIMEOUT_INFINITE
)
1626 shutdown_timeout
= add_timeout_user( master_socket_timeout
, server_shutdown_timeout
, NULL
);
1630 /* create a new job object */
1631 DECL_HANDLER(create_job
)
1634 struct unicode_str name
;
1635 struct object
*root
;
1636 const struct security_descriptor
*sd
;
1637 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1639 if (!objattr
) return;
1641 if ((job
= create_job_object( root
, &name
, objattr
->attributes
, sd
)))
1643 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
1644 reply
->handle
= alloc_handle( current
->process
, job
, req
->access
, objattr
->attributes
);
1646 reply
->handle
= alloc_handle_no_access_check( current
->process
, job
,
1647 req
->access
, objattr
->attributes
);
1648 release_object( job
);
1650 if (root
) release_object( root
);
1653 /* open a job object */
1654 DECL_HANDLER(open_job
)
1656 struct unicode_str name
= get_req_unicode_str();
1658 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
1659 &job_ops
, &name
, req
->attributes
);
1662 /* assign a job object to a process */
1663 DECL_HANDLER(assign_job
)
1665 struct process
*process
;
1666 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_ASSIGN_PROCESS
);
1670 if ((process
= get_process_from_handle( req
->process
, PROCESS_SET_QUOTA
| PROCESS_TERMINATE
)))
1672 if (!process
->running_threads
)
1673 set_error( STATUS_PROCESS_IS_TERMINATING
);
1674 else if (process
->job
)
1675 set_error( STATUS_ACCESS_DENIED
);
1677 add_job_process( job
, process
);
1678 release_object( process
);
1680 release_object( job
);
1684 /* check if a process is associated with a job */
1685 DECL_HANDLER(process_in_job
)
1687 struct process
*process
;
1690 if (!(process
= get_process_from_handle( req
->process
, PROCESS_QUERY_INFORMATION
)))
1695 set_error( process
->job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1697 else if ((job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_QUERY
)))
1699 set_error( process
->job
== job
? STATUS_PROCESS_IN_JOB
: STATUS_PROCESS_NOT_IN_JOB
);
1700 release_object( job
);
1702 release_object( process
);
1705 /* retrieve information about a job */
1706 DECL_HANDLER(get_job_info
)
1708 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_QUERY
);
1712 reply
->total_processes
= job
->total_processes
;
1713 reply
->active_processes
= job
->num_processes
;
1714 release_object( job
);
1717 /* terminate all processes associated with the job */
1718 DECL_HANDLER(terminate_job
)
1720 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_TERMINATE
);
1724 terminate_job( job
, req
->status
);
1725 release_object( job
);
1728 /* update limits of the job object */
1729 DECL_HANDLER(set_job_limits
)
1731 struct job
*job
= get_job_obj( current
->process
, req
->handle
, JOB_OBJECT_SET_ATTRIBUTES
);
1735 job
->limit_flags
= req
->limit_flags
;
1736 release_object( job
);
1739 /* set the jobs completion port */
1740 DECL_HANDLER(set_job_completion_port
)
1742 struct job
*job
= get_job_obj( current
->process
, req
->job
, JOB_OBJECT_SET_ATTRIBUTES
);
1746 if (!job
->completion_port
)
1748 job
->completion_port
= get_completion_obj( current
->process
, req
->port
, IO_COMPLETION_MODIFY_STATE
);
1749 job
->completion_key
= req
->key
;
1752 set_error( STATUS_INVALID_PARAMETER
);
1754 release_object( job
);
1757 /* Suspend a process */
1758 DECL_HANDLER(suspend_process
)
1760 struct process
*process
;
1762 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SUSPEND_RESUME
)))
1764 struct list
*ptr
, *next
;
1766 LIST_FOR_EACH_SAFE( ptr
, next
, &process
->thread_list
)
1768 struct thread
*thread
= LIST_ENTRY( ptr
, struct thread
, proc_entry
);
1769 suspend_thread( thread
);
1772 release_object( process
);
1776 /* Resume a process */
1777 DECL_HANDLER(resume_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 resume_thread( thread
);
1791 release_object( process
);
1795 /* Get a list of processes and threads currently running */
1796 DECL_HANDLER(list_processes
)
1798 struct process
*process
;
1799 struct thread
*thread
;
1800 unsigned int pos
= 0;
1803 reply
->process_count
= 0;
1804 reply
->info_size
= 0;
1806 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1808 struct process_dll
*exe
= get_process_exe_module( process
);
1809 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1810 reply
->info_size
+= sizeof(struct process_info
);
1811 if (exe
) reply
->info_size
+= exe
->namelen
;
1812 reply
->info_size
= (reply
->info_size
+ 7) & ~7;
1813 reply
->info_size
+= process
->running_threads
* sizeof(struct thread_info
);
1814 reply
->process_count
++;
1817 if (reply
->info_size
> get_reply_max_size())
1819 set_error( STATUS_INFO_LENGTH_MISMATCH
);
1823 if (!(buffer
= set_reply_data_size( reply
->info_size
))) return;
1825 memset( buffer
, 0, reply
->info_size
);
1826 LIST_FOR_EACH_ENTRY( process
, &process_list
, struct process
, entry
)
1828 struct process_info
*process_info
;
1829 struct process_dll
*exe
= get_process_exe_module( process
);
1831 pos
= (pos
+ 7) & ~7;
1832 process_info
= (struct process_info
*)(buffer
+ pos
);
1833 process_info
->start_time
= process
->start_time
;
1834 process_info
->name_len
= exe
? exe
->namelen
: 0;
1835 process_info
->thread_count
= process
->running_threads
;
1836 process_info
->priority
= process
->priority
;
1837 process_info
->pid
= process
->id
;
1838 process_info
->parent_pid
= process
->parent_id
;
1839 process_info
->handle_count
= get_handle_table_count(process
);
1840 process_info
->unix_pid
= process
->unix_pid
;
1841 pos
+= sizeof(*process_info
);
1845 memcpy( buffer
+ pos
, exe
->filename
, exe
->namelen
);
1846 pos
+= exe
->namelen
;
1849 pos
= (pos
+ 7) & ~7;
1850 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
1852 struct thread_info
*thread_info
= (struct thread_info
*)(buffer
+ pos
);
1854 thread_info
->start_time
= thread
->creation_time
;
1855 thread_info
->tid
= thread
->id
;
1856 thread_info
->base_priority
= thread
->priority
;
1857 thread_info
->current_priority
= thread
->priority
; /* FIXME */
1858 thread_info
->unix_tid
= thread
->unix_tid
;
1859 pos
+= sizeof(*thread_info
);