2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
26 /* process structure */
28 static struct process
*first_process
;
29 static int running_processes
;
31 /* process operations */
33 static void process_dump( struct object
*obj
, int verbose
);
34 static int process_signaled( struct object
*obj
, struct thread
*thread
);
35 static void process_destroy( struct object
*obj
);
37 static const struct object_ops process_ops
=
39 sizeof(struct process
),
53 /* create a new process */
54 static struct process
*create_process( struct process
*parent
, struct new_process_request
*req
,
55 const char *cmd_line
, size_t len
)
57 struct process
*process
;
59 if (!(process
= alloc_object( &process_ops
))) return NULL
;
62 process
->thread_list
= NULL
;
63 process
->debugger
= NULL
;
64 process
->handles
= NULL
;
65 process
->exit_code
= 0x103; /* STILL_ACTIVE */
66 process
->running_threads
= 0;
67 process
->priority
= NORMAL_PRIORITY_CLASS
;
68 process
->affinity
= 1;
70 process
->create_flags
= 0;
71 process
->console_in
= NULL
;
72 process
->console_out
= NULL
;
73 process
->init_event
= NULL
;
75 gettimeofday( &process
->start_time
, NULL
);
77 /* copy the request structure */
78 if (!(process
->info
= mem_alloc( sizeof(*process
->info
) + len
))) goto error
;
79 memcpy( process
->info
, req
, sizeof(*req
) );
80 memcpy( process
->info
->cmdline
, cmd_line
, len
);
81 process
->info
->cmdline
[len
] = 0;
82 req
= process
->info
; /* use the copy now */
83 process
->create_flags
= req
->create_flags
;
86 process
->handles
= copy_handle_table( process
, parent
);
88 process
->handles
= alloc_handle_table( process
, 0 );
89 if (!process
->handles
) goto error
;
91 /* alloc a handle for the process itself */
92 alloc_handle( process
, process
, PROCESS_ALL_ACCESS
, 0 );
94 /* get the init done event */
97 if (!(process
->init_event
= get_event_obj( parent
, req
->event
, EVENT_MODIFY_STATE
)))
101 /* set the process console */
102 if (process
->create_flags
& CREATE_NEW_CONSOLE
)
104 if (!alloc_console( process
)) goto error
;
106 else if (!(process
->create_flags
& DETACHED_PROCESS
))
108 if (parent
->console_in
) process
->console_in
= grab_object( parent
->console_in
);
109 if (parent
->console_out
) process
->console_out
= grab_object( parent
->console_out
);
112 if (!req
->inherit_all
&& !(req
->start_flags
& STARTF_USESTDHANDLES
))
114 process
->info
->hstdin
= duplicate_handle( parent
, req
->hstdin
, process
,
115 0, TRUE
, DUPLICATE_SAME_ACCESS
);
116 process
->info
->hstdout
= duplicate_handle( parent
, req
->hstdout
, process
,
117 0, TRUE
, DUPLICATE_SAME_ACCESS
);
118 process
->info
->hstderr
= duplicate_handle( parent
, req
->hstderr
, process
,
119 0, TRUE
, DUPLICATE_SAME_ACCESS
);
122 /* attach to the debugger if requested */
123 if (process
->create_flags
& (DEBUG_PROCESS
| DEBUG_ONLY_THIS_PROCESS
))
124 debugger_attach( process
, current
);
125 else if (parent
&& parent
->debugger
&& !(parent
->create_flags
& DEBUG_ONLY_THIS_PROCESS
))
126 debugger_attach( process
, parent
->debugger
);
128 if ((process
->next
= first_process
) != NULL
) process
->next
->prev
= process
;
129 first_process
= process
;
133 free_console( process
);
134 if (process
->handles
) release_object( process
->handles
);
135 release_object( process
);
139 /* create the initial process */
140 struct process
*create_initial_process(void)
142 struct process
*process
;
143 struct new_process_request req
;
147 req
.create_flags
= CREATE_NEW_CONSOLE
;
148 req
.start_flags
= STARTF_USESTDHANDLES
;
155 if ((process
= create_process( NULL
, &req
, "", 1 )))
157 process
->info
->hstdin
= alloc_handle( process
, process
->console_in
,
158 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
159 process
->info
->hstdout
= alloc_handle( process
, process
->console_out
,
160 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
161 process
->info
->hstderr
= alloc_handle( process
, process
->console_out
,
162 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, 1 );
167 /* destroy a process when its refcount is 0 */
168 static void process_destroy( struct object
*obj
)
170 struct process
*process
= (struct process
*)obj
;
171 assert( obj
->ops
== &process_ops
);
173 /* we can't have a thread remaining */
174 assert( !process
->thread_list
);
175 if (process
->next
) process
->next
->prev
= process
->prev
;
176 if (process
->prev
) process
->prev
->next
= process
->next
;
177 else first_process
= process
->next
;
178 if (process
->info
) free( process
->info
);
179 if (process
->init_event
) release_object( process
->init_event
);
182 /* dump a process on stdout for debugging purposes */
183 static void process_dump( struct object
*obj
, int verbose
)
185 struct process
*process
= (struct process
*)obj
;
186 assert( obj
->ops
== &process_ops
);
188 fprintf( stderr
, "Process next=%p prev=%p console=%p/%p handles=%p\n",
189 process
->next
, process
->prev
, process
->console_in
, process
->console_out
,
193 static int process_signaled( struct object
*obj
, struct thread
*thread
)
195 struct process
*process
= (struct process
*)obj
;
196 return !process
->running_threads
;
200 /* get a process from an id (and increment the refcount) */
201 struct process
*get_process_from_id( void *id
)
203 struct process
*p
= first_process
;
204 while (p
&& (p
!= id
)) p
= p
->next
;
205 if (p
) grab_object( p
);
206 else set_error( ERROR_INVALID_PARAMETER
);
210 /* get a process from a handle (and increment the refcount) */
211 struct process
*get_process_from_handle( int handle
, unsigned int access
)
213 return (struct process
*)get_handle_obj( current
->process
, handle
,
214 access
, &process_ops
);
217 /* a process has been killed (i.e. its last thread died) */
218 static void process_killed( struct process
*process
, int exit_code
)
220 assert( !process
->thread_list
);
221 process
->exit_code
= exit_code
;
222 gettimeofday( &process
->end_time
, NULL
);
223 release_object( process
->handles
);
224 process
->handles
= NULL
;
225 free_console( process
);
226 wake_up( &process
->obj
, 0 );
227 if (!--running_processes
)
229 /* last process died, close global handles */
230 close_global_handles();
234 /* add a thread to a process running threads list */
235 void add_process_thread( struct process
*process
, struct thread
*thread
)
237 thread
->proc_next
= process
->thread_list
;
238 thread
->proc_prev
= NULL
;
239 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
240 process
->thread_list
= thread
;
241 if (!process
->running_threads
++) running_processes
++;
242 grab_object( thread
);
245 /* remove a thread from a process running threads list */
246 void remove_process_thread( struct process
*process
, struct thread
*thread
)
248 assert( process
->running_threads
> 0 );
249 assert( process
->thread_list
);
251 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
252 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
253 else process
->thread_list
= thread
->proc_next
;
255 if (!--process
->running_threads
)
257 /* we have removed the last running thread, exit the process */
258 process_killed( process
, thread
->exit_code
);
260 release_object( thread
);
263 /* suspend all the threads of a process */
264 void suspend_process( struct process
*process
)
266 if (!process
->suspend
++)
268 struct thread
*thread
= process
->thread_list
;
269 for (; thread
; thread
= thread
->proc_next
)
271 if (!thread
->suspend
) stop_thread( thread
);
276 /* resume all the threads of a process */
277 void resume_process( struct process
*process
)
279 assert (process
->suspend
> 0);
280 if (!--process
->suspend
)
282 struct thread
*thread
= process
->thread_list
;
283 for (; thread
; thread
= thread
->proc_next
)
285 if (!thread
->suspend
) continue_thread( thread
);
290 /* kill a process on the spot */
291 void kill_process( struct process
*process
, int exit_code
)
293 while (process
->thread_list
)
294 kill_thread( process
->thread_list
, exit_code
);
297 /* kill all processes being debugged by a given thread */
298 void kill_debugged_processes( struct thread
*debugger
, int exit_code
)
300 for (;;) /* restart from the beginning of the list every time */
302 struct process
*process
= first_process
;
303 /* find the first process being debugged by 'debugger' and still running */
304 while (process
&& (process
->debugger
!= debugger
|| !process
->running_threads
))
305 process
= process
->next
;
306 if (!process
) return;
307 process
->debugger
= NULL
;
308 kill_process( process
, exit_code
);
312 /* get all information about a process */
313 static void get_process_info( struct process
*process
, struct get_process_info_request
*req
)
316 req
->exit_code
= process
->exit_code
;
317 req
->priority
= process
->priority
;
318 req
->process_affinity
= process
->affinity
;
319 req
->system_affinity
= 1;
322 /* set all information about a process */
323 static void set_process_info( struct process
*process
,
324 struct set_process_info_request
*req
)
326 if (req
->mask
& SET_PROCESS_INFO_PRIORITY
)
327 process
->priority
= req
->priority
;
328 if (req
->mask
& SET_PROCESS_INFO_AFFINITY
)
330 if (req
->affinity
!= 1) set_error( ERROR_INVALID_PARAMETER
);
331 else process
->affinity
= req
->affinity
;
335 /* read data from a process memory space */
336 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
337 /* we read the total size in all cases to check for permissions */
338 static void read_process_memory( struct process
*process
, const int *addr
,
339 size_t len
, size_t max
, int *dest
)
341 struct thread
*thread
= process
->thread_list
;
343 if ((unsigned int)addr
% sizeof(int)) /* address must be aligned */
345 set_error( ERROR_INVALID_PARAMETER
);
348 suspend_thread( thread
, 0 );
349 if (thread
->attached
)
351 while (len
> 0 && max
)
353 if (read_thread_int( thread
, addr
++, dest
++ ) == -1) goto done
;
357 /* check the rest for read permission */
360 int dummy
, page
= get_page_size() / sizeof(int);
365 if (read_thread_int( thread
, addr
- 1, &dummy
) == -1) goto done
;
367 if (len
&& (read_thread_int( thread
, addr
+ len
- 1, &dummy
) == -1)) goto done
;
370 else set_error( ERROR_ACCESS_DENIED
);
372 resume_thread( thread
);
375 /* write data to a process memory space */
376 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
377 /* we check the total size for write permissions */
378 static void write_process_memory( struct process
*process
, int *addr
, size_t len
,
379 size_t max
, unsigned int first_mask
,
380 unsigned int last_mask
, const int *src
)
382 struct thread
*thread
= process
->thread_list
;
384 if (!len
|| ((unsigned int)addr
% sizeof(int))) /* address must be aligned */
386 set_error( ERROR_INVALID_PARAMETER
);
389 suspend_thread( thread
, 0 );
390 if (thread
->attached
)
392 /* first word is special */
395 if (write_thread_int( thread
, addr
++, *src
++, first_mask
) == -1) goto done
;
399 else last_mask
&= first_mask
;
401 while (len
> 1 && max
)
403 if (write_thread_int( thread
, addr
++, *src
++, ~0 ) == -1) goto done
;
410 /* last word is special too */
411 if (write_thread_int( thread
, addr
, *src
, last_mask
) == -1) goto done
;
415 /* check the rest for write permission */
416 int page
= get_page_size() / sizeof(int);
421 if (write_thread_int( thread
, addr
- 1, 0, 0 ) == -1) goto done
;
423 if (len
&& (write_thread_int( thread
, addr
+ len
- 1, 0, 0 ) == -1)) goto done
;
426 else set_error( ERROR_ACCESS_DENIED
);
428 resume_thread( thread
);
431 /* take a snapshot of currently running processes */
432 struct process_snapshot
*process_snap( int *count
)
434 struct process_snapshot
*snapshot
, *ptr
;
435 struct process
*process
;
436 if (!running_processes
) return NULL
;
437 if (!(snapshot
= mem_alloc( sizeof(*snapshot
) * running_processes
)))
440 for (process
= first_process
; process
; process
= process
->next
)
442 if (!process
->running_threads
) continue;
443 ptr
->process
= process
;
444 ptr
->threads
= process
->running_threads
;
445 ptr
->priority
= process
->priority
;
446 grab_object( process
);
449 *count
= running_processes
;
453 /* create a new process */
454 DECL_HANDLER(new_process
)
456 size_t len
= get_req_strlen( req
->cmdline
);
457 struct process
*process
;
461 if ((process
= create_process( current
->process
, req
, req
->cmdline
, len
)))
463 req
->handle
= alloc_handle( current
->process
, process
, PROCESS_ALL_ACCESS
, req
->inherit
);
465 release_object( process
);
469 /* initialize a new process */
470 DECL_HANDLER(init_process
)
472 struct new_process_request
*info
;
474 if (!current
->unix_pid
)
476 fatal_protocol_error( current
, "init_process: init_thread not called yet\n" );
479 if (!(info
= current
->process
->info
))
481 fatal_protocol_error( current
, "init_process: called twice\n" );
484 current
->process
->info
= NULL
;
485 req
->start_flags
= info
->start_flags
;
486 req
->hstdin
= info
->hstdin
;
487 req
->hstdout
= info
->hstdout
;
488 req
->hstderr
= info
->hstderr
;
489 req
->cmd_show
= info
->cmd_show
;
490 req
->env_ptr
= info
->env_ptr
;
491 strcpy( req
->cmdline
, info
->cmdline
);
495 /* signal the end of the process initialization */
496 DECL_HANDLER(init_process_done
)
498 struct process
*process
= current
->process
;
499 if (!process
->init_event
)
501 fatal_protocol_error( current
, "init_process_done: no event\n" );
504 set_event( process
->init_event
);
505 release_object( process
->init_event
);
506 process
->init_event
= NULL
;
507 if (current
->suspend
+ current
->process
->suspend
> 0) stop_thread( current
);
510 /* open a handle to a process */
511 DECL_HANDLER(open_process
)
513 struct process
*process
= get_process_from_id( req
->pid
);
517 req
->handle
= alloc_handle( current
->process
, process
, req
->access
, req
->inherit
);
518 release_object( process
);
522 /* terminate a process */
523 DECL_HANDLER(terminate_process
)
525 struct process
*process
;
527 if ((process
= get_process_from_handle( req
->handle
, PROCESS_TERMINATE
)))
529 kill_process( process
, req
->exit_code
);
530 release_object( process
);
534 /* fetch information about a process */
535 DECL_HANDLER(get_process_info
)
537 struct process
*process
;
539 if ((process
= get_process_from_handle( req
->handle
, PROCESS_QUERY_INFORMATION
)))
541 get_process_info( process
, req
);
542 release_object( process
);
546 /* set information about a process */
547 DECL_HANDLER(set_process_info
)
549 struct process
*process
;
551 if ((process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
)))
553 set_process_info( process
, req
);
554 release_object( process
);
558 /* read data from a process address space */
559 DECL_HANDLER(read_process_memory
)
561 struct process
*process
;
563 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_READ
)))
565 read_process_memory( process
, req
->addr
, req
->len
,
566 get_req_size( req
->data
, sizeof(int) ), req
->data
);
567 release_object( process
);
571 /* write data to a process address space */
572 DECL_HANDLER(write_process_memory
)
574 struct process
*process
;
576 if ((process
= get_process_from_handle( req
->handle
, PROCESS_VM_WRITE
)))
578 write_process_memory( process
, req
->addr
, req
->len
, get_req_size( req
->data
, sizeof(int) ),
579 req
->first_mask
, req
->last_mask
, req
->data
);
580 release_object( process
);