Fixed handling of debug events on thread/process exit.
[wine/multimedia.git] / server / process.c
blobb105cc965adb02ea1dcc1fa3501309ede7ce945e
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/time.h>
15 #include <unistd.h>
17 #include "winerror.h"
18 #include "winbase.h"
19 #include "winnt.h"
21 #include "handle.h"
22 #include "process.h"
23 #include "thread.h"
24 #include "request.h"
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),
40 process_dump,
41 add_queue,
42 remove_queue,
43 process_signaled,
44 no_satisfied,
45 no_read_fd,
46 no_write_fd,
47 no_flush,
48 no_get_file_info,
49 process_destroy
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;
60 process->next = NULL;
61 process->prev = 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;
69 process->suspend = 0;
70 process->console_in = NULL;
71 process->console_out = NULL;
72 process->init_event = NULL;
73 process->info = NULL;
74 gettimeofday( &process->start_time, NULL );
76 if (req->inherit_all)
77 process->handles = copy_handle_table( process, parent );
78 else
79 process->handles = alloc_handle_table( process, 0 );
80 if (!process->handles) goto error;
82 /* alloc a handle for the process itself */
83 alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
85 if (!(process->info = mem_alloc( sizeof(*process->info) + len ))) goto error;
86 memcpy( process->info, req, sizeof(*req) );
87 memcpy( process->info->cmdline, cmd_line, len );
88 process->info->cmdline[len] = 0;
90 /* get the init done event */
91 if (req->event != -1)
93 if (!(process->init_event = get_event_obj( parent, req->event, EVENT_MODIFY_STATE )))
94 goto error;
97 /* set the process console */
98 if (req->create_flags & CREATE_NEW_CONSOLE)
100 if (!alloc_console( process )) goto error;
102 else if (!(req->create_flags & DETACHED_PROCESS))
104 if (parent->console_in) process->console_in = grab_object( parent->console_in );
105 if (parent->console_out) process->console_out = grab_object( parent->console_out );
108 if (!req->inherit_all && !(req->start_flags & STARTF_USESTDHANDLES))
110 process->info->hstdin = duplicate_handle( parent, req->hstdin, process,
111 0, TRUE, DUPLICATE_SAME_ACCESS );
112 process->info->hstdout = duplicate_handle( parent, req->hstdout, process,
113 0, TRUE, DUPLICATE_SAME_ACCESS );
114 process->info->hstderr = duplicate_handle( parent, req->hstderr, process,
115 0, TRUE, DUPLICATE_SAME_ACCESS );
118 /* attach to the debugger if requested */
119 if (req->create_flags & DEBUG_PROCESS)
120 debugger_attach( process, current );
121 else if (parent && parent->debugger && !(req->create_flags & DEBUG_ONLY_THIS_PROCESS))
122 debugger_attach( process, parent->debugger );
124 if ((process->next = first_process) != NULL) process->next->prev = process;
125 first_process = process;
126 return process;
128 error:
129 free_console( process );
130 if (process->handles) release_object( process->handles );
131 release_object( process );
132 return NULL;
135 /* create the initial process */
136 struct process *create_initial_process(void)
138 struct process *process;
139 struct new_process_request req;
141 req.inherit = 0;
142 req.inherit_all = 0;
143 req.create_flags = CREATE_NEW_CONSOLE;
144 req.start_flags = STARTF_USESTDHANDLES;
145 req.hstdin = -1;
146 req.hstdout = -1;
147 req.hstderr = -1;
148 req.event = -1;
149 req.cmd_show = 0;
150 req.env_ptr = NULL;
151 if ((process = create_process( NULL, &req, "", 1 )))
153 process->info->hstdin = alloc_handle( process, process->console_in,
154 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
155 process->info->hstdout = alloc_handle( process, process->console_out,
156 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
157 process->info->hstderr = alloc_handle( process, process->console_out,
158 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, 1 );
160 return process;
163 /* destroy a process when its refcount is 0 */
164 static void process_destroy( struct object *obj )
166 struct process *process = (struct process *)obj;
167 assert( obj->ops == &process_ops );
169 /* we can't have a thread remaining */
170 assert( !process->thread_list );
171 if (process->next) process->next->prev = process->prev;
172 if (process->prev) process->prev->next = process->next;
173 else first_process = process->next;
174 if (process->info) free( process->info );
175 if (process->init_event) release_object( process->init_event );
178 /* dump a process on stdout for debugging purposes */
179 static void process_dump( struct object *obj, int verbose )
181 struct process *process = (struct process *)obj;
182 assert( obj->ops == &process_ops );
184 fprintf( stderr, "Process next=%p prev=%p console=%p/%p handles=%p\n",
185 process->next, process->prev, process->console_in, process->console_out,
186 process->handles );
189 static int process_signaled( struct object *obj, struct thread *thread )
191 struct process *process = (struct process *)obj;
192 return !process->running_threads;
196 /* get a process from an id (and increment the refcount) */
197 struct process *get_process_from_id( void *id )
199 struct process *p = first_process;
200 while (p && (p != id)) p = p->next;
201 if (p) grab_object( p );
202 else set_error( ERROR_INVALID_PARAMETER );
203 return p;
206 /* get a process from a handle (and increment the refcount) */
207 struct process *get_process_from_handle( int handle, unsigned int access )
209 return (struct process *)get_handle_obj( current->process, handle,
210 access, &process_ops );
213 /* a process has been killed (i.e. its last thread died) */
214 static void process_killed( struct process *process, int exit_code )
216 assert( !process->thread_list );
217 process->exit_code = exit_code;
218 gettimeofday( &process->end_time, NULL );
219 release_object( process->handles );
220 process->handles = NULL;
221 free_console( process );
222 wake_up( &process->obj, 0 );
223 if (!--running_processes)
225 /* last process died, close global handles */
226 close_global_handles();
230 /* add a thread to a process running threads list */
231 void add_process_thread( struct process *process, struct thread *thread )
233 thread->proc_next = process->thread_list;
234 thread->proc_prev = NULL;
235 if (thread->proc_next) thread->proc_next->proc_prev = thread;
236 process->thread_list = thread;
237 if (!process->running_threads++) running_processes++;
238 grab_object( thread );
241 /* remove a thread from a process running threads list */
242 void remove_process_thread( struct process *process, struct thread *thread )
244 assert( process->running_threads > 0 );
245 assert( process->thread_list );
247 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
248 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
249 else process->thread_list = thread->proc_next;
251 if (!--process->running_threads)
253 /* we have removed the last running thread, exit the process */
254 process_killed( process, thread->exit_code );
256 release_object( thread );
259 /* suspend all the threads of a process */
260 void suspend_process( struct process *process )
262 if (!process->suspend++)
264 struct thread *thread = process->thread_list;
265 for (; thread; thread = thread->proc_next)
267 if (!thread->suspend) stop_thread( thread );
272 /* resume all the threads of a process */
273 void resume_process( struct process *process )
275 assert (process->suspend > 0);
276 if (!--process->suspend)
278 struct thread *thread = process->thread_list;
279 for (; thread; thread = thread->proc_next)
281 if (!thread->suspend) continue_thread( thread );
286 /* kill a process on the spot */
287 void kill_process( struct process *process, int exit_code )
289 while (process->thread_list)
290 kill_thread( process->thread_list, exit_code );
293 /* kill all processes being debugged by a given thread */
294 void kill_debugged_processes( struct thread *debugger, int exit_code )
296 for (;;) /* restart from the beginning of the list every time */
298 struct process *process = first_process;
299 /* find the first process being debugged by 'debugger' and still running */
300 while (process && (process->debugger != debugger || !process->running_threads))
301 process = process->next;
302 if (!process) return;
303 process->debugger = NULL;
304 kill_process( process, exit_code );
308 /* get all information about a process */
309 static void get_process_info( struct process *process, struct get_process_info_request *req )
311 req->pid = process;
312 req->exit_code = process->exit_code;
313 req->priority = process->priority;
314 req->process_affinity = process->affinity;
315 req->system_affinity = 1;
318 /* set all information about a process */
319 static void set_process_info( struct process *process,
320 struct set_process_info_request *req )
322 if (req->mask & SET_PROCESS_INFO_PRIORITY)
323 process->priority = req->priority;
324 if (req->mask & SET_PROCESS_INFO_AFFINITY)
326 if (req->affinity != 1) set_error( ERROR_INVALID_PARAMETER );
327 else process->affinity = req->affinity;
331 /* read data from a process memory space */
332 /* len is the total size (in ints), max is the size we can actually store in the output buffer */
333 /* we read the total size in all cases to check for permissions */
334 static void read_process_memory( struct process *process, const int *addr,
335 size_t len, size_t max, int *dest )
337 struct thread *thread = process->thread_list;
339 if ((unsigned int)addr % sizeof(int)) /* address must be aligned */
341 set_error( ERROR_INVALID_PARAMETER );
342 return;
344 suspend_thread( thread, 0 );
345 if (thread->attached)
347 while (len > 0 && max)
349 if (read_thread_int( thread, addr++, dest++ ) == -1) goto done;
350 max--;
351 len--;
353 /* check the rest for read permission */
354 if (len > 0)
356 int dummy, page = get_page_size() / sizeof(int);
357 while (len >= page)
359 addr += page;
360 len -= page;
361 if (read_thread_int( thread, addr - 1, &dummy ) == -1) goto done;
363 if (len && (read_thread_int( thread, addr + len - 1, &dummy ) == -1)) goto done;
366 else set_error( ERROR_ACCESS_DENIED );
367 done:
368 resume_thread( thread );
371 /* write data to a process memory space */
372 /* len is the total size (in ints), max is the size we can actually read from the input buffer */
373 /* we check the total size for write permissions */
374 static void write_process_memory( struct process *process, int *addr, size_t len,
375 size_t max, unsigned int first_mask,
376 unsigned int last_mask, const int *src )
378 struct thread *thread = process->thread_list;
380 if (!len || ((unsigned int)addr % sizeof(int))) /* address must be aligned */
382 set_error( ERROR_INVALID_PARAMETER );
383 return;
385 suspend_thread( thread, 0 );
386 if (thread->attached)
388 /* first word is special */
389 if (len > 1)
391 if (write_thread_int( thread, addr++, *src++, first_mask ) == -1) goto done;
392 len--;
393 max--;
395 else last_mask &= first_mask;
397 while (len > 1 && max)
399 if (write_thread_int( thread, addr++, *src++, ~0 ) == -1) goto done;
400 max--;
401 len--;
404 if (max)
406 /* last word is special too */
407 if (write_thread_int( thread, addr, *src, last_mask ) == -1) goto done;
409 else
411 /* check the rest for write permission */
412 int page = get_page_size() / sizeof(int);
413 while (len >= page)
415 addr += page;
416 len -= page;
417 if (write_thread_int( thread, addr - 1, 0, 0 ) == -1) goto done;
419 if (len && (write_thread_int( thread, addr + len - 1, 0, 0 ) == -1)) goto done;
422 else set_error( ERROR_ACCESS_DENIED );
423 done:
424 resume_thread( thread );
427 /* take a snapshot of currently running processes */
428 struct process_snapshot *process_snap( int *count )
430 struct process_snapshot *snapshot, *ptr;
431 struct process *process;
432 if (!running_processes) return NULL;
433 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
434 return NULL;
435 ptr = snapshot;
436 for (process = first_process; process; process = process->next)
438 if (!process->running_threads) continue;
439 ptr->process = process;
440 ptr->threads = process->running_threads;
441 ptr->priority = process->priority;
442 grab_object( process );
443 ptr++;
445 *count = running_processes;
446 return snapshot;
449 /* create a new process */
450 DECL_HANDLER(new_process)
452 size_t len = get_req_strlen( req->cmdline );
453 struct process *process;
455 req->handle = -1;
456 req->pid = NULL;
457 if ((process = create_process( current->process, req, req->cmdline, len )))
459 req->handle = alloc_handle( current->process, process, PROCESS_ALL_ACCESS, req->inherit );
460 req->pid = process;
461 release_object( process );
465 /* initialize a new process */
466 DECL_HANDLER(init_process)
468 struct new_process_request *info;
470 if (!current->unix_pid)
472 fatal_protocol_error( current, "init_process: init_thread not called yet\n" );
473 return;
475 if (!(info = current->process->info))
477 fatal_protocol_error( current, "init_process: called twice\n" );
478 return;
480 current->process->info = NULL;
481 req->start_flags = info->start_flags;
482 req->hstdin = info->hstdin;
483 req->hstdout = info->hstdout;
484 req->hstderr = info->hstderr;
485 req->cmd_show = info->cmd_show;
486 req->env_ptr = info->env_ptr;
487 strcpy( req->cmdline, info->cmdline );
488 free( info );
491 /* signal the end of the process initialization */
492 DECL_HANDLER(init_process_done)
494 struct process *process = current->process;
495 if (!process->init_event)
497 fatal_protocol_error( current, "init_process_done: no event\n" );
498 return;
500 set_event( process->init_event );
501 release_object( process->init_event );
502 process->init_event = NULL;
503 if (current->suspend + current->process->suspend > 0) stop_thread( current );
506 /* open a handle to a process */
507 DECL_HANDLER(open_process)
509 struct process *process = get_process_from_id( req->pid );
510 req->handle = -1;
511 if (process)
513 req->handle = alloc_handle( current->process, process, req->access, req->inherit );
514 release_object( process );
518 /* terminate a process */
519 DECL_HANDLER(terminate_process)
521 struct process *process;
523 if ((process = get_process_from_handle( req->handle, PROCESS_TERMINATE )))
525 kill_process( process, req->exit_code );
526 release_object( process );
530 /* fetch information about a process */
531 DECL_HANDLER(get_process_info)
533 struct process *process;
535 if ((process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION )))
537 get_process_info( process, req );
538 release_object( process );
542 /* set information about a process */
543 DECL_HANDLER(set_process_info)
545 struct process *process;
547 if ((process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION )))
549 set_process_info( process, req );
550 release_object( process );
554 /* read data from a process address space */
555 DECL_HANDLER(read_process_memory)
557 struct process *process;
559 if ((process = get_process_from_handle( req->handle, PROCESS_VM_READ )))
561 read_process_memory( process, req->addr, req->len,
562 get_req_size( req->data, sizeof(int) ), req->data );
563 release_object( process );
567 /* write data to a process address space */
568 DECL_HANDLER(write_process_memory)
570 struct process *process;
572 if ((process = get_process_from_handle( req->handle, PROCESS_VM_WRITE )))
574 write_process_memory( process, req->addr, req->len, get_req_size( req->data, sizeof(int) ),
575 req->first_mask, req->last_mask, req->data );
576 release_object( process );