Fixed process_signaled (thanks to Uwe Bonnes).
[wine/multimedia.git] / server / process.c
blobe06d72dd2e2230e2f3f3484630a5315ff8623581
1 /*
2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <limits.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/time.h>
13 #include <unistd.h>
15 #include "winerror.h"
16 #include "winbase.h"
17 #include "winnt.h"
19 #include "server.h"
20 #include "server/process.h"
21 #include "server/thread.h"
23 /* reserved handle access rights */
24 #define RESERVED_SHIFT 25
25 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
26 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
27 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
29 /* global handle macros */
30 #define HANDLE_OBFUSCATOR 0x544a4def
31 #define HANDLE_IS_GLOBAL(h) (((h) ^ HANDLE_OBFUSCATOR) < 0x10000)
32 #define HANDLE_LOCAL_TO_GLOBAL(h) ((h) ^ HANDLE_OBFUSCATOR)
33 #define HANDLE_GLOBAL_TO_LOCAL(h) ((h) ^ HANDLE_OBFUSCATOR)
35 struct handle_entry
37 struct object *ptr;
38 unsigned int access;
41 /* process structure; not much for now... */
43 struct process
45 struct object obj; /* object header */
46 struct process *next; /* system-wide process list */
47 struct process *prev;
48 struct thread *thread_list; /* head of the thread list */
49 struct handle_entry *entries; /* handle entry table */
50 int handle_count; /* nb of allocated handle entries */
51 int handle_last; /* last used handle entry */
52 int exit_code; /* process exit code */
53 int running_threads; /* number of threads running in this process */
54 struct timeval start_time; /* absolute time at process start */
55 struct timeval end_time; /* absolute time at process end */
56 int priority; /* priority class */
57 int affinity; /* process affinity mask */
58 struct object *console_in; /* console input */
59 struct object *console_out; /* console output */
62 static struct process *first_process;
63 static struct process *initial_process;
64 static int running_processes;
66 #define MIN_HANDLE_ENTRIES 32
68 /* process operations */
70 static void process_dump( struct object *obj, int verbose );
71 static int process_signaled( struct object *obj, struct thread *thread );
72 static void process_destroy( struct object *obj );
73 static void free_handles( struct process *process );
74 static int copy_handle_table( struct process *process, struct process *parent );
76 static const struct object_ops process_ops =
78 process_dump,
79 add_queue,
80 remove_queue,
81 process_signaled,
82 no_satisfied,
83 no_read_fd,
84 no_write_fd,
85 no_flush,
86 no_get_file_info,
87 process_destroy
90 /* create a new process */
91 struct process *create_process(void)
93 struct process *process, *parent;
95 if (!(process = mem_alloc( sizeof(*process) ))) return NULL;
97 parent = current ? current->process : NULL;
98 if (!copy_handle_table( process, parent ))
100 free( process );
101 return NULL;
103 init_object( &process->obj, &process_ops, NULL );
104 process->next = first_process;
105 process->prev = NULL;
106 process->thread_list = NULL;
107 process->exit_code = 0x103; /* STILL_ACTIVE */
108 process->running_threads = 0;
109 process->priority = NORMAL_PRIORITY_CLASS;
110 process->affinity = 1;
111 process->console_in = NULL;
112 process->console_out = NULL;
113 if (parent)
115 if (parent->console_in) process->console_in = grab_object( parent->console_in );
116 if (parent->console_out) process->console_out = grab_object( parent->console_out );
119 if (first_process) first_process->prev = process;
120 first_process = process;
121 if (!initial_process)
123 initial_process = process;
124 grab_object( initial_process ); /* so that we never free it */
127 gettimeofday( &process->start_time, NULL );
128 /* alloc a handle for the process itself */
129 alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
130 return process;
133 /* destroy a process when its refcount is 0 */
134 static void process_destroy( struct object *obj )
136 struct process *process = (struct process *)obj;
137 assert( obj->ops == &process_ops );
138 assert( process != initial_process );
140 /* we can't have a thread remaining */
141 assert( !process->thread_list );
142 if (process->next) process->next->prev = process->prev;
143 if (process->prev) process->prev->next = process->next;
144 else first_process = process->next;
145 free_console( process );
146 free_handles( process );
147 if (debug_level) memset( process, 0xbb, sizeof(process) ); /* catch errors */
148 free( process );
151 /* dump a process on stdout for debugging purposes */
152 static void process_dump( struct object *obj, int verbose )
154 struct process *process = (struct process *)obj;
155 assert( obj->ops == &process_ops );
157 printf( "Process next=%p prev=%p\n", process->next, process->prev );
160 static int process_signaled( struct object *obj, struct thread *thread )
162 struct process *process = (struct process *)obj;
163 return !process->running_threads;
166 /* get a process from an id (and increment the refcount) */
167 struct process *get_process_from_id( void *id )
169 struct process *p = first_process;
170 while (p && (p != id)) p = p->next;
171 if (p) grab_object( p );
172 else SET_ERROR( ERROR_INVALID_PARAMETER );
173 return p;
176 /* get a process from a handle (and increment the refcount) */
177 struct process *get_process_from_handle( int handle, unsigned int access )
179 return (struct process *)get_handle_obj( current->process, handle,
180 access, &process_ops );
183 /* a process has been killed (i.e. its last thread died) */
184 static void process_killed( struct process *process, int exit_code )
186 assert( !process->thread_list );
187 process->exit_code = exit_code;
188 gettimeofday( &process->end_time, NULL );
189 wake_up( &process->obj, 0 );
190 free_handles( process );
193 /* free the process handle entries */
194 static void free_handles( struct process *process )
196 struct handle_entry *entry;
197 int handle;
199 if (!(entry = process->entries)) return;
200 for (handle = 0; handle <= process->handle_last; handle++, entry++)
202 struct object *obj = entry->ptr;
203 entry->ptr = NULL;
204 if (obj) release_object( obj );
206 free( process->entries );
207 process->handle_count = 0;
208 process->handle_last = -1;
209 process->entries = NULL;
212 /* add a thread to a process running threads list */
213 void add_process_thread( struct process *process, struct thread *thread )
215 thread->proc_next = process->thread_list;
216 thread->proc_prev = NULL;
217 if (thread->proc_next) thread->proc_next->proc_prev = thread;
218 process->thread_list = thread;
219 if (!process->running_threads++) running_processes++;
220 grab_object( thread );
223 /* remove a thread from a process running threads list */
224 void remove_process_thread( struct process *process, struct thread *thread )
226 assert( process->running_threads > 0 );
227 assert( process->thread_list );
229 if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
230 if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
231 else process->thread_list = thread->proc_next;
233 if (!--process->running_threads)
235 /* we have removed the last running thread, exit the process */
236 running_processes--;
237 process_killed( process, thread->exit_code );
239 release_object( thread );
242 /* grow a handle table */
243 /* return 1 if OK, 0 on error */
244 static int grow_handle_table( struct process *process )
246 struct handle_entry *new_entries;
247 int count = process->handle_count;
249 if (count >= INT_MAX / 2) return 0;
250 count *= 2;
251 if (!(new_entries = realloc( process->entries, count * sizeof(struct handle_entry) )))
253 SET_ERROR( ERROR_OUTOFMEMORY );
254 return 0;
256 process->handle_count = count;
257 process->entries = new_entries;
258 return 1;
261 /* allocate a handle for an object, incrementing its refcount */
262 /* return the handle, or -1 on error */
263 int alloc_handle( struct process *process, void *obj, unsigned int access,
264 int inherit )
266 struct handle_entry *entry;
267 int handle;
269 assert( !(access & RESERVED_ALL) );
270 if (inherit) access |= RESERVED_INHERIT;
272 /* find the first free entry */
274 if (!(entry = process->entries)) return -1;
275 for (handle = 0; handle <= process->handle_last; handle++, entry++)
276 if (!entry->ptr) goto found;
278 if (handle >= process->handle_count)
280 if (!grow_handle_table( process )) return -1;
281 entry = process->entries + handle; /* the table may have moved */
283 process->handle_last = handle;
285 found:
286 entry->ptr = grab_object( obj );
287 entry->access = access;
288 return handle + 1; /* avoid handle 0 */
291 /* return an handle entry, or NULL if the handle is invalid */
292 static struct handle_entry *get_handle( struct process *process, int handle )
294 struct handle_entry *entry;
296 if (HANDLE_IS_GLOBAL(handle))
298 handle = HANDLE_GLOBAL_TO_LOCAL(handle);
299 process = initial_process;
301 handle--; /* handles start at 1 */
302 if ((handle < 0) || (handle > process->handle_last)) goto error;
303 entry = process->entries + handle;
304 if (!entry->ptr) goto error;
305 return entry;
307 error:
308 SET_ERROR( ERROR_INVALID_HANDLE );
309 return NULL;
312 /* attempt to shrink a table */
313 /* return 1 if OK, 0 on error */
314 static int shrink_handle_table( struct process *process )
316 struct handle_entry *new_entries;
317 struct handle_entry *entry = process->entries + process->handle_last;
318 int count = process->handle_count;
320 while (process->handle_last >= 0)
322 if (entry->ptr) break;
323 process->handle_last--;
324 entry--;
326 if (process->handle_last >= count / 4) return 1; /* no need to shrink */
327 if (count < MIN_HANDLE_ENTRIES * 2) return 1; /* too small to shrink */
328 count /= 2;
329 if (!(new_entries = realloc( process->entries,
330 count * sizeof(struct handle_entry) )))
331 return 0;
332 process->handle_count = count;
333 process->entries = new_entries;
334 return 1;
337 /* copy the handle table of the parent process */
338 /* return 1 if OK, 0 on error */
339 static int copy_handle_table( struct process *process, struct process *parent )
341 struct handle_entry *ptr;
342 int i, count, last;
344 if (!parent) /* first process */
346 count = MIN_HANDLE_ENTRIES;
347 last = -1;
349 else
351 assert( parent->entries );
352 count = parent->handle_count;
353 last = parent->handle_last;
356 if (!(ptr = mem_alloc( count * sizeof(struct handle_entry)))) return 0;
357 process->entries = ptr;
358 process->handle_count = count;
359 process->handle_last = last;
361 if (last >= 0)
363 memcpy( ptr, parent->entries, (last + 1) * sizeof(struct handle_entry) );
364 for (i = 0; i <= last; i++, ptr++)
366 if (!ptr->ptr) continue;
367 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
368 else ptr->ptr = NULL; /* don't inherit this entry */
371 /* attempt to shrink the table */
372 shrink_handle_table( process );
373 return 1;
376 /* close a handle and decrement the refcount of the associated object */
377 /* return 1 if OK, 0 on error */
378 int close_handle( struct process *process, int handle )
380 struct handle_entry *entry;
381 struct object *obj;
383 if (HANDLE_IS_GLOBAL(handle))
385 handle = HANDLE_GLOBAL_TO_LOCAL(handle);
386 process = initial_process;
388 if (!(entry = get_handle( process, handle ))) return 0;
389 if (entry->access & RESERVED_CLOSE_PROTECT) return 0; /* FIXME: error code */
390 obj = entry->ptr;
391 entry->ptr = NULL;
392 if (handle-1 == process->handle_last) shrink_handle_table( process );
393 release_object( obj );
394 return 1;
397 /* retrieve the object corresponding to a handle, incrementing its refcount */
398 struct object *get_handle_obj( struct process *process, int handle,
399 unsigned int access, const struct object_ops *ops )
401 struct handle_entry *entry;
402 struct object *obj;
404 switch( handle )
406 case 0xfffffffe: /* current thread pseudo-handle */
407 obj = &current->obj;
408 break;
409 case 0x7fffffff: /* current process pseudo-handle */
410 obj = (struct object *)current->process;
411 break;
412 default:
413 if (!(entry = get_handle( process, handle ))) return NULL;
414 if ((entry->access & access) != access)
416 SET_ERROR( ERROR_ACCESS_DENIED );
417 return NULL;
419 obj = entry->ptr;
420 break;
422 if (ops && (obj->ops != ops))
424 SET_ERROR( ERROR_INVALID_HANDLE ); /* not the right type */
425 return NULL;
427 return grab_object( obj );
430 /* get/set the handle reserved flags */
431 /* return the new flags (or -1 on error) */
432 int set_handle_info( struct process *process, int handle, int mask, int flags )
434 struct handle_entry *entry;
436 if (!(entry = get_handle( process, handle ))) return -1;
437 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
438 flags = (flags << RESERVED_SHIFT) & mask;
439 entry->access = (entry->access & ~mask) | flags;
440 return (entry->access & RESERVED_ALL) >> RESERVED_SHIFT;
443 /* duplicate a handle */
444 int duplicate_handle( struct process *src, int src_handle, struct process *dst,
445 unsigned int access, int inherit, int options )
447 int res;
448 struct handle_entry *entry = get_handle( src, src_handle );
449 if (!entry) return -1;
451 if (options & DUP_HANDLE_SAME_ACCESS) access = entry->access;
452 if (options & DUP_HANDLE_MAKE_GLOBAL) dst = initial_process;
453 access &= ~RESERVED_ALL;
454 res = alloc_handle( dst, entry->ptr, access, inherit );
455 if (options & DUP_HANDLE_MAKE_GLOBAL) res = HANDLE_LOCAL_TO_GLOBAL(res);
456 return res;
459 /* open a new handle to an existing object */
460 int open_object( const char *name, const struct object_ops *ops,
461 unsigned int access, int inherit )
463 struct object *obj = find_object( name );
464 if (!obj)
466 SET_ERROR( ERROR_FILE_NOT_FOUND );
467 return -1;
469 if (ops && obj->ops != ops)
471 release_object( obj );
472 SET_ERROR( ERROR_INVALID_HANDLE ); /* FIXME: not the right type */
473 return -1;
475 return alloc_handle( current->process, obj, access, inherit );
478 /* dump a handle table on stdout */
479 void dump_handles( struct process *process )
481 struct handle_entry *entry;
482 int i;
484 if (!process->entries) return;
485 entry = process->entries;
486 for (i = 0; i <= process->handle_last; i++, entry++)
488 if (!entry->ptr) continue;
489 printf( "%5d: %p %08x ", i + 1, entry->ptr, entry->access );
490 entry->ptr->ops->dump( entry->ptr, 0 );
494 /* kill a process on the spot */
495 void kill_process( struct process *process, int exit_code )
497 while (process->thread_list)
498 kill_thread( process->thread_list, exit_code );
501 /* get all information about a process */
502 void get_process_info( struct process *process,
503 struct get_process_info_reply *reply )
505 reply->pid = process;
506 reply->exit_code = process->exit_code;
507 reply->priority = process->priority;
508 reply->process_affinity = process->affinity;
509 reply->system_affinity = 1;
512 /* set all information about a process */
513 void set_process_info( struct process *process,
514 struct set_process_info_request *req )
516 if (req->mask & SET_PROCESS_INFO_PRIORITY)
517 process->priority = req->priority;
518 if (req->mask & SET_PROCESS_INFO_AFFINITY)
520 if (req->affinity != 1) SET_ERROR( ERROR_INVALID_PARAMETER );
521 else process->affinity = req->affinity;
525 /* allocate a console for this process */
526 int alloc_console( struct process *process )
528 struct object *obj[2];
529 if (process->console_in || process->console_out)
531 SET_ERROR( ERROR_ACCESS_DENIED );
532 return 0;
534 if (!create_console( -1, obj )) return 0;
535 process->console_in = obj[0];
536 process->console_out = obj[1];
537 return 1;
540 /* free the console for this process */
541 int free_console( struct process *process )
543 if (process->console_in) release_object( process->console_in );
544 if (process->console_out) release_object( process->console_out );
545 process->console_in = process->console_out = NULL;
546 return 1;
549 /* get the process console */
550 struct object *get_console( struct process *process, int output )
552 struct object *obj;
553 if (!(obj = output ? process->console_out : process->console_in))
554 return NULL;
555 return grab_object( obj );
558 /* take a snapshot of currently running processes */
559 struct process_snapshot *process_snap( int *count )
561 struct process_snapshot *snapshot, *ptr;
562 struct process *process;
563 if (!running_processes) return NULL;
564 if (!(snapshot = mem_alloc( sizeof(*snapshot) * running_processes )))
565 return NULL;
566 ptr = snapshot;
567 for (process = first_process; process; process = process->next)
569 if (!process->running_threads) continue;
570 ptr->process = process;
571 ptr->threads = process->running_threads;
572 ptr->priority = process->priority;
573 grab_object( process );
574 ptr++;
576 *count = running_processes;
577 return snapshot;