2 * Server-side process management
4 * Copyright (C) 1998 Alexandre Julliard
20 #include "server/thread.h"
22 /* reserved handle access rights */
23 #define RESERVED_SHIFT 25
24 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
25 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
26 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
28 /* global handle macros */
29 #define HANDLE_OBFUSCATOR 0x544a4def
30 #define HANDLE_IS_GLOBAL(h) (((h) ^ HANDLE_OBFUSCATOR) < 0x10000)
31 #define HANDLE_LOCAL_TO_GLOBAL(h) ((h) ^ HANDLE_OBFUSCATOR)
32 #define HANDLE_GLOBAL_TO_LOCAL(h) ((h) ^ HANDLE_OBFUSCATOR)
40 /* process structure; not much for now... */
44 struct object obj
; /* object header */
45 struct process
*next
; /* system-wide process list */
47 struct thread
*thread_list
; /* head of the thread list */
48 struct handle_entry
*entries
; /* handle entry table */
49 int handle_count
; /* nb of allocated handle entries */
50 int handle_last
; /* last used handle entry */
51 int exit_code
; /* process exit code */
52 int running_threads
; /* number of threads running in this process */
53 struct timeval start_time
; /* absolute time at process start */
54 struct timeval end_time
; /* absolute time at process end */
57 static struct process
*first_process
;
58 static struct process
*initial_process
;
60 #define MIN_HANDLE_ENTRIES 32
62 /* process operations */
64 static void process_dump( struct object
*obj
, int verbose
);
65 static int process_signaled( struct object
*obj
, struct thread
*thread
);
66 static void process_destroy( struct object
*obj
);
67 static void free_handles( struct process
*process
);
68 static int copy_handle_table( struct process
*process
, struct process
*parent
);
70 static const struct object_ops process_ops
=
84 /* create a new process */
85 struct process
*create_process(void)
87 struct process
*process
;
89 if (!(process
= mem_alloc( sizeof(*process
) ))) return NULL
;
91 if (!copy_handle_table( process
, current
? current
->process
: NULL
))
96 init_object( &process
->obj
, &process_ops
, NULL
);
97 process
->next
= first_process
;
99 process
->thread_list
= NULL
;
100 process
->exit_code
= 0x103; /* STILL_ACTIVE */
101 process
->running_threads
= 0;
103 if (first_process
) first_process
->prev
= process
;
104 first_process
= process
;
105 if (!initial_process
)
107 initial_process
= process
;
108 grab_object( initial_process
); /* so that we never free it */
111 gettimeofday( &process
->start_time
, NULL
);
112 /* alloc a handle for the process itself */
113 alloc_handle( process
, process
, PROCESS_ALL_ACCESS
, 0 );
117 /* destroy a process when its refcount is 0 */
118 static void process_destroy( struct object
*obj
)
120 struct process
*process
= (struct process
*)obj
;
121 assert( obj
->ops
== &process_ops
);
122 assert( process
!= initial_process
);
124 /* we can't have a thread remaining */
125 assert( !process
->thread_list
);
126 if (process
->next
) process
->next
->prev
= process
->prev
;
127 if (process
->prev
) process
->prev
->next
= process
->next
;
128 else first_process
= process
->next
;
129 free_handles( process
);
130 if (debug_level
) memset( process
, 0xbb, sizeof(process
) ); /* catch errors */
134 /* dump a process on stdout for debugging purposes */
135 static void process_dump( struct object
*obj
, int verbose
)
137 struct process
*process
= (struct process
*)obj
;
138 assert( obj
->ops
== &process_ops
);
140 printf( "Process next=%p prev=%p\n", process
->next
, process
->prev
);
143 static int process_signaled( struct object
*obj
, struct thread
*thread
)
145 struct process
*process
= (struct process
*)obj
;
146 return (process
->running_threads
> 0);
149 /* get a process from an id (and increment the refcount) */
150 struct process
*get_process_from_id( void *id
)
152 struct process
*p
= first_process
;
153 while (p
&& (p
!= id
)) p
= p
->next
;
154 if (p
) grab_object( p
);
155 else SET_ERROR( ERROR_INVALID_PARAMETER
);
159 /* get a process from a handle (and increment the refcount) */
160 struct process
*get_process_from_handle( int handle
, unsigned int access
)
162 return (struct process
*)get_handle_obj( current
->process
, handle
,
163 access
, &process_ops
);
166 /* a process has been killed (i.e. its last thread died) */
167 static void process_killed( struct process
*process
, int exit_code
)
169 assert( !process
->thread_list
);
170 process
->exit_code
= exit_code
;
171 gettimeofday( &process
->end_time
, NULL
);
172 wake_up( &process
->obj
, 0 );
173 free_handles( process
);
176 /* free the process handle entries */
177 static void free_handles( struct process
*process
)
179 struct handle_entry
*entry
;
182 if (!(entry
= process
->entries
)) return;
183 for (handle
= 0; handle
<= process
->handle_last
; handle
++, entry
++)
185 struct object
*obj
= entry
->ptr
;
187 if (obj
) release_object( obj
);
189 free( process
->entries
);
190 process
->handle_count
= 0;
191 process
->handle_last
= -1;
192 process
->entries
= NULL
;
195 /* add a thread to a process running threads list */
196 void add_process_thread( struct process
*process
, struct thread
*thread
)
198 thread
->proc_next
= process
->thread_list
;
199 thread
->proc_prev
= NULL
;
200 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
;
201 process
->thread_list
= thread
;
202 process
->running_threads
++;
203 grab_object( thread
);
206 /* remove a thread from a process running threads list */
207 void remove_process_thread( struct process
*process
, struct thread
*thread
)
209 assert( process
->running_threads
> 0 );
210 assert( process
->thread_list
);
212 if (thread
->proc_next
) thread
->proc_next
->proc_prev
= thread
->proc_prev
;
213 if (thread
->proc_prev
) thread
->proc_prev
->proc_next
= thread
->proc_next
;
214 else process
->thread_list
= thread
->proc_next
;
216 if (!--process
->running_threads
)
218 /* we have removed the last running thread, exit the process */
219 process_killed( process
, thread
->exit_code
);
221 release_object( thread
);
224 /* grow a handle table */
225 /* return 1 if OK, 0 on error */
226 static int grow_handle_table( struct process
*process
)
228 struct handle_entry
*new_entries
;
229 int count
= process
->handle_count
;
231 if (count
>= INT_MAX
/ 2) return 0;
233 if (!(new_entries
= realloc( process
->entries
, count
* sizeof(struct handle_entry
) )))
235 SET_ERROR( ERROR_OUTOFMEMORY
);
238 process
->handle_count
= count
;
239 process
->entries
= new_entries
;
243 /* allocate a handle for an object, incrementing its refcount */
244 /* return the handle, or -1 on error */
245 int alloc_handle( struct process
*process
, void *obj
, unsigned int access
,
248 struct handle_entry
*entry
;
251 assert( !(access
& RESERVED_ALL
) );
252 if (inherit
) access
|= RESERVED_INHERIT
;
254 /* find the first free entry */
256 if (!(entry
= process
->entries
)) return -1;
257 for (handle
= 0; handle
<= process
->handle_last
; handle
++, entry
++)
258 if (!entry
->ptr
) goto found
;
260 if (handle
>= process
->handle_count
)
262 if (!grow_handle_table( process
)) return -1;
263 entry
= process
->entries
+ handle
; /* the table may have moved */
265 process
->handle_last
= handle
;
268 entry
->ptr
= grab_object( obj
);
269 entry
->access
= access
;
270 return handle
+ 1; /* avoid handle 0 */
273 /* allocate a specific handle for an object, incrementing its refcount */
274 static int alloc_specific_handle( struct process
*process
, void *obj
, int handle
,
275 unsigned int access
, int inherit
)
277 struct handle_entry
*entry
;
280 if (handle
== -1) return alloc_handle( process
, obj
, access
, inherit
);
282 assert( !(access
& RESERVED_ALL
) );
283 if (inherit
) access
|= RESERVED_INHERIT
;
285 handle
--; /* handles start at 1 */
286 if ((handle
< 0) || (handle
> process
->handle_last
))
288 SET_ERROR( ERROR_INVALID_HANDLE
);
291 entry
= process
->entries
+ handle
;
294 entry
->ptr
= grab_object( obj
);
295 entry
->access
= access
;
296 if (old
) release_object( old
);
300 /* return an handle entry, or NULL if the handle is invalid */
301 static struct handle_entry
*get_handle( struct process
*process
, int handle
)
303 struct handle_entry
*entry
;
305 if (HANDLE_IS_GLOBAL(handle
))
307 handle
= HANDLE_GLOBAL_TO_LOCAL(handle
);
308 process
= initial_process
;
310 handle
--; /* handles start at 1 */
311 if ((handle
< 0) || (handle
> process
->handle_last
)) goto error
;
312 entry
= process
->entries
+ handle
;
313 if (!entry
->ptr
) goto error
;
317 SET_ERROR( ERROR_INVALID_HANDLE
);
321 /* attempt to shrink a table */
322 /* return 1 if OK, 0 on error */
323 static int shrink_handle_table( struct process
*process
)
325 struct handle_entry
*new_entries
;
326 struct handle_entry
*entry
= process
->entries
+ process
->handle_last
;
327 int count
= process
->handle_count
;
329 while (process
->handle_last
>= 0)
331 if (entry
->ptr
) break;
332 process
->handle_last
--;
335 if (process
->handle_last
>= count
/ 4) return 1; /* no need to shrink */
336 if (count
< MIN_HANDLE_ENTRIES
* 2) return 1; /* too small to shrink */
338 if (!(new_entries
= realloc( process
->entries
,
339 count
* sizeof(struct handle_entry
) )))
341 process
->handle_count
= count
;
342 process
->entries
= new_entries
;
346 /* copy the handle table of the parent process */
347 /* return 1 if OK, 0 on error */
348 static int copy_handle_table( struct process
*process
, struct process
*parent
)
350 struct handle_entry
*ptr
;
353 if (!parent
) /* first process */
355 count
= MIN_HANDLE_ENTRIES
;
360 assert( parent
->entries
);
361 count
= parent
->handle_count
;
362 last
= parent
->handle_last
;
365 if (!(ptr
= mem_alloc( count
* sizeof(struct handle_entry
)))) return 0;
366 process
->entries
= ptr
;
367 process
->handle_count
= count
;
368 process
->handle_last
= last
;
372 memcpy( ptr
, parent
->entries
, (last
+ 1) * sizeof(struct handle_entry
) );
373 for (i
= 0; i
<= last
; i
++, ptr
++)
375 if (!ptr
->ptr
) continue;
376 if (ptr
->access
& RESERVED_INHERIT
) grab_object( ptr
->ptr
);
377 else ptr
->ptr
= NULL
; /* don't inherit this entry */
380 /* attempt to shrink the table */
381 shrink_handle_table( process
);
385 /* close a handle and decrement the refcount of the associated object */
386 /* return 1 if OK, 0 on error */
387 int close_handle( struct process
*process
, int handle
)
389 struct handle_entry
*entry
;
392 if (HANDLE_IS_GLOBAL(handle
))
394 handle
= HANDLE_GLOBAL_TO_LOCAL(handle
);
395 process
= initial_process
;
397 if (!(entry
= get_handle( process
, handle
))) return 0;
398 if (entry
->access
& RESERVED_CLOSE_PROTECT
) return 0; /* FIXME: error code */
401 if (handle
-1 == process
->handle_last
) shrink_handle_table( process
);
402 release_object( obj
);
406 /* retrieve the object corresponding to a handle, incrementing its refcount */
407 struct object
*get_handle_obj( struct process
*process
, int handle
,
408 unsigned int access
, const struct object_ops
*ops
)
410 struct handle_entry
*entry
;
415 case 0xfffffffe: /* current thread pseudo-handle */
418 case 0x7fffffff: /* current process pseudo-handle */
419 obj
= (struct object
*)current
->process
;
422 if (!(entry
= get_handle( process
, handle
))) return NULL
;
423 if ((entry
->access
& access
) != access
)
425 SET_ERROR( ERROR_ACCESS_DENIED
);
431 if (ops
&& (obj
->ops
!= ops
))
433 SET_ERROR( ERROR_INVALID_HANDLE
); /* not the right type */
436 return grab_object( obj
);
439 /* get/set the handle reserved flags */
440 /* return the new flags (or -1 on error) */
441 int set_handle_info( struct process
*process
, int handle
, int mask
, int flags
)
443 struct handle_entry
*entry
;
445 if (!(entry
= get_handle( process
, handle
))) return -1;
446 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
447 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
448 entry
->access
= (entry
->access
& ~mask
) | flags
;
449 return (entry
->access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
452 /* duplicate a handle */
453 int duplicate_handle( struct process
*src
, int src_handle
, struct process
*dst
,
454 int dst_handle
, unsigned int access
, int inherit
, int options
)
457 struct handle_entry
*entry
= get_handle( src
, src_handle
);
458 if (!entry
) return -1;
460 if (options
& DUP_HANDLE_SAME_ACCESS
) access
= entry
->access
;
461 if (options
& DUP_HANDLE_MAKE_GLOBAL
) dst
= initial_process
;
462 access
&= ~RESERVED_ALL
;
463 res
= alloc_specific_handle( dst
, entry
->ptr
, dst_handle
, access
, inherit
);
464 if (options
& DUP_HANDLE_MAKE_GLOBAL
) res
= HANDLE_LOCAL_TO_GLOBAL(res
);
468 /* open a new handle to an existing object */
469 int open_object( const char *name
, const struct object_ops
*ops
,
470 unsigned int access
, int inherit
)
472 struct object
*obj
= find_object( name
);
473 if (!obj
) return -1; /* FIXME: set error code */
474 if (ops
&& obj
->ops
!= ops
)
476 release_object( obj
);
477 return -1; /* FIXME: set error code */
479 return alloc_handle( current
->process
, obj
, access
, inherit
);
482 /* dump a handle table on stdout */
483 void dump_handles( struct process
*process
)
485 struct handle_entry
*entry
;
488 if (!process
->entries
) return;
489 entry
= process
->entries
;
490 for (i
= 0; i
<= process
->handle_last
; i
++, entry
++)
492 if (!entry
->ptr
) continue;
493 printf( "%5d: %p %08x ", i
+ 1, entry
->ptr
, entry
->access
);
494 entry
->ptr
->ops
->dump( entry
->ptr
, 0 );
498 /* kill a process on the spot */
499 void kill_process( struct process
*process
, int exit_code
)
501 while (process
->thread_list
)
502 kill_thread( process
->thread_list
, exit_code
);
505 /* get all information about a process */
506 void get_process_info( struct process
*process
,
507 struct get_process_info_reply
*reply
)
509 reply
->pid
= process
;
510 reply
->exit_code
= process
->exit_code
;