2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
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)
41 /* process structure; not much for now... */
45 struct object obj
; /* object header */
46 struct process
*next
; /* system-wide process list */
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
=
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
))
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
;
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 );
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 */
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
);
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
;
199 if (!(entry
= process
->entries
)) return;
200 for (handle
= 0; handle
<= process
->handle_last
; handle
++, entry
++)
202 struct object
*obj
= entry
->ptr
;
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 */
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;
251 if (!(new_entries
= realloc( process
->entries
, count
* sizeof(struct handle_entry
) )))
253 SET_ERROR( ERROR_OUTOFMEMORY
);
256 process
->handle_count
= count
;
257 process
->entries
= new_entries
;
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
,
266 struct handle_entry
*entry
;
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
;
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
;
308 SET_ERROR( ERROR_INVALID_HANDLE
);
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
--;
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 */
329 if (!(new_entries
= realloc( process
->entries
,
330 count
* sizeof(struct handle_entry
) )))
332 process
->handle_count
= count
;
333 process
->entries
= new_entries
;
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
;
344 if (!parent
) /* first process */
346 count
= MIN_HANDLE_ENTRIES
;
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
;
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
);
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
;
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 */
392 if (handle
-1 == process
->handle_last
) shrink_handle_table( process
);
393 release_object( obj
);
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
;
406 case 0xfffffffe: /* current thread pseudo-handle */
409 case 0x7fffffff: /* current process pseudo-handle */
410 obj
= (struct object
*)current
->process
;
413 if (!(entry
= get_handle( process
, handle
))) return NULL
;
414 if ((entry
->access
& access
) != access
)
416 SET_ERROR( ERROR_ACCESS_DENIED
);
422 if (ops
&& (obj
->ops
!= ops
))
424 SET_ERROR( ERROR_INVALID_HANDLE
); /* not the right type */
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
)
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
);
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
);
466 SET_ERROR( ERROR_FILE_NOT_FOUND
);
469 if (ops
&& obj
->ops
!= ops
)
471 release_object( obj
);
472 SET_ERROR( ERROR_INVALID_HANDLE
); /* FIXME: not the right type */
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
;
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
);
534 if (!create_console( -1, obj
)) return 0;
535 process
->console_in
= obj
[0];
536 process
->console_out
= obj
[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
;
549 /* get the process console */
550 struct object
*get_console( struct process
*process
, int output
)
553 if (!(obj
= output
? process
->console_out
: process
->console_in
))
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
)))
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
);
576 *count
= running_processes
;