Release 980822
[wine] / server / process.c
1 /*
2  * Server-side process management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  */
6
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>
14
15 #include "winerror.h"
16 #include "winbase.h"
17 #include "winnt.h"
18
19 #include "server.h"
20 #include "server/thread.h"
21
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)
27
28 struct handle_entry
29 {
30     struct object *ptr;
31     unsigned int   access;
32 };
33
34 /* process structure; not much for now... */
35
36 struct process
37 {
38     struct object        obj;             /* object header */
39     struct process      *next;            /* system-wide process list */
40     struct process      *prev;
41     struct thread       *thread_list;     /* head of the thread list */
42     struct handle_entry *entries;         /* handle entry table */
43     int                  handle_count;    /* nb of allocated handle entries */
44     int                  handle_last;     /* last used handle entry */
45     int                  exit_code;       /* process exit code */
46     int                  running_threads; /* number of threads running in this process */
47     struct timeval       start_time;      /* absolute time at process start */
48     struct timeval       end_time;        /* absolute time at process end */
49 };
50
51 static struct process *first_process;
52
53 #define MIN_HANDLE_ENTRIES  32
54
55 /* process operations */
56
57 static void dump_process( struct object *obj, int verbose );
58 static int process_signaled( struct object *obj, struct thread *thread );
59 static int process_satisfied( struct object *obj, struct thread *thread );
60 static void destroy_process( struct object *obj );
61 static void free_handles( struct process *process );
62 static int copy_handle_table( struct process *process, struct process *parent );
63
64 static const struct object_ops process_ops =
65 {
66     dump_process,
67     process_signaled,
68     process_satisfied,
69     destroy_process
70 };
71
72 /* create a new process */
73 struct process *create_process(void)
74 {
75     struct process *process;
76
77     if (!(process = malloc( sizeof(*process) ))) return NULL;
78
79     if (!copy_handle_table( process, current ? current->process : NULL ))
80     {
81         free( process );
82         return NULL;
83     }
84     init_object( &process->obj, &process_ops, NULL );
85     process->next            = first_process;
86     process->prev            = NULL;
87     process->thread_list     = NULL;
88     process->exit_code       = 0x103;  /* STILL_ACTIVE */
89     process->running_threads = 0;
90
91     if (first_process) first_process->prev = process;
92     first_process = process;
93
94     gettimeofday( &process->start_time, NULL );
95     /* alloc a handle for the process itself */
96     alloc_handle( process, process, PROCESS_ALL_ACCESS, 0 );
97     return process;
98 }
99
100 /* destroy a process when its refcount is 0 */
101 static void destroy_process( struct object *obj )
102 {
103     struct process *process = (struct process *)obj;
104     assert( obj->ops == &process_ops );
105
106     /* we can't have a thread remaining */
107     assert( !process->thread_list );
108     if (process->next) process->next->prev = process->prev;
109     if (process->prev) process->prev->next = process->next;
110     else first_process = process->next;
111     free_handles( process );
112     if (debug_level) memset( process, 0xbb, sizeof(process) );  /* catch errors */
113     free( process );
114 }
115
116 /* dump a process on stdout for debugging purposes */
117 static void dump_process( struct object *obj, int verbose )
118 {
119     struct process *process = (struct process *)obj;
120     assert( obj->ops == &process_ops );
121
122     printf( "Process next=%p prev=%p\n", process->next, process->prev );
123 }
124
125 static int process_signaled( struct object *obj, struct thread *thread )
126 {
127     struct process *process = (struct process *)obj;
128     return (process->running_threads > 0);
129 }
130
131 static int process_satisfied( struct object *obj, struct thread *thread )
132 {
133     return 0;
134 }
135
136 /* get a process from an id (and increment the refcount) */
137 struct process *get_process_from_id( void *id )
138 {
139     struct process *p = first_process;
140     while (p && (p != id)) p = p->next;
141     if (p) grab_object( p );
142     else SET_ERROR( ERROR_INVALID_PARAMETER );
143     return p;
144 }
145
146 /* get a process from a handle (and increment the refcount) */
147 struct process *get_process_from_handle( int handle, unsigned int access )
148 {
149     return (struct process *)get_handle_obj( current->process, handle,
150                                              access, &process_ops );
151 }
152
153 /* a process has been killed (i.e. its last thread died) */
154 static void process_killed( struct process *process, int exit_code )
155 {
156     assert( !process->thread_list );
157     process->exit_code = exit_code;
158     gettimeofday( &process->end_time, NULL );
159     wake_up( &process->obj, 0 );
160     free_handles( process );
161 }
162
163 /* free the process handle entries */
164 static void free_handles( struct process *process )
165 {
166     struct handle_entry *entry;
167     int handle;
168
169     if (!(entry = process->entries)) return;
170     for (handle = 0; handle <= process->handle_last; handle++, entry++)
171     {
172         struct object *obj = entry->ptr;
173         entry->ptr = NULL;
174         if (obj) release_object( obj );
175     }
176     free( process->entries );
177     process->handle_count = 0;
178     process->handle_last  = -1;
179     process->entries = NULL;
180 }
181
182 /* add a thread to a process running threads list */
183 void add_process_thread( struct process *process, struct thread *thread )
184 {
185     thread->proc_next = process->thread_list;
186     thread->proc_prev = NULL;
187     if (thread->proc_next) thread->proc_next->proc_prev = thread;
188     process->thread_list = thread;
189     process->running_threads++;
190     grab_object( thread );
191 }
192
193 /* remove a thread from a process running threads list */
194 void remove_process_thread( struct process *process, struct thread *thread )
195 {
196     assert( process->running_threads > 0 );
197     assert( process->thread_list );
198
199     if (thread->proc_next) thread->proc_next->proc_prev = thread->proc_prev;
200     if (thread->proc_prev) thread->proc_prev->proc_next = thread->proc_next;
201     else process->thread_list = thread->proc_next;
202
203     if (!--process->running_threads)
204     {
205         /* we have removed the last running thread, exit the process */
206         process_killed( process, thread->exit_code );
207     }
208     release_object( thread );
209 }
210
211 /* grow a handle table */
212 /* return 1 if OK, 0 on error */
213 static int grow_handle_table( struct process *process )
214 {
215     struct handle_entry *new_entries;
216     int count = process->handle_count;
217
218     if (count >= INT_MAX / 2) return 0;
219     count *= 2;
220     if (!(new_entries = realloc( process->entries, count * sizeof(struct handle_entry) )))
221     {
222         SET_ERROR( ERROR_NOT_ENOUGH_MEMORY );
223         return 0;
224     }
225     process->handle_count = count;
226     process->entries      = new_entries;
227     return 1;
228 }
229
230 /* allocate a handle for an object, incrementing its refcount */
231 /* return the handle, or -1 on error */
232 int alloc_handle( struct process *process, void *obj, unsigned int access,
233                   int inherit )
234 {
235     struct handle_entry *entry;
236     int handle;
237
238     assert( !(access & RESERVED_ALL) );
239     if (inherit) access |= RESERVED_INHERIT;
240
241     /* find the first free entry */
242
243     if (!(entry = process->entries)) return -1;
244     for (handle = 0; handle <= process->handle_last; handle++, entry++)
245         if (!entry->ptr) goto found;
246
247     if (handle >= process->handle_count)
248     {
249         if (!grow_handle_table( process )) return -1;
250         entry = process->entries + handle;  /* the table may have moved */
251     }
252     process->handle_last = handle;
253
254  found:
255     entry->ptr    = grab_object( obj );
256     entry->access = access;
257     return handle + 1;  /* avoid handle 0 */
258 }
259
260 /* allocate a specific handle for an object, incrementing its refcount */
261 static int alloc_specific_handle( struct process *process, void *obj, int handle,
262                                   unsigned int access, int inherit )
263 {
264     struct handle_entry *entry;
265     struct object *old;
266
267     if (handle == -1) return alloc_handle( process, obj, access, inherit );
268
269     assert( !(access & RESERVED_ALL) );
270     if (inherit) access |= RESERVED_INHERIT;
271
272     handle--;  /* handles start at 1 */
273     if ((handle < 0) || (handle > process->handle_last))
274     {
275         SET_ERROR( ERROR_INVALID_HANDLE );
276         return -1;
277     }
278     entry = process->entries + handle;
279
280     old = entry->ptr;
281     entry->ptr = grab_object( obj );
282     entry->access = access;
283     if (old) release_object( old );
284     return handle + 1;
285 }
286
287 /* return an handle entry, or NULL if the handle is invalid */
288 static struct handle_entry *get_handle( struct process *process, int handle )
289 {
290     struct handle_entry *entry;
291
292     handle--;  /* handles start at 1 */
293     if ((handle < 0) || (handle > process->handle_last)) goto error;
294     entry = process->entries + handle;
295     if (!entry->ptr) goto error;
296     return entry;
297
298  error:
299     SET_ERROR( ERROR_INVALID_HANDLE );
300     return NULL;
301 }
302
303 /* attempt to shrink a table */
304 /* return 1 if OK, 0 on error */
305 static int shrink_handle_table( struct process *process )
306 {
307     struct handle_entry *new_entries;
308     struct handle_entry *entry = process->entries + process->handle_last;
309     int count = process->handle_count;
310
311     while (process->handle_last >= 0)
312     {
313         if (entry->ptr) break;
314         process->handle_last--;
315         entry--;
316     }
317     if (process->handle_last >= count / 4) return 1;  /* no need to shrink */
318     if (count < MIN_HANDLE_ENTRIES * 2) return 1;  /* too small to shrink */
319     count /= 2;
320     if (!(new_entries = realloc( process->entries,
321                                  count * sizeof(struct handle_entry) )))
322         return 0;
323     process->handle_count = count;
324     process->entries      = new_entries;
325     return 1;
326 }
327
328 /* copy the handle table of the parent process */
329 /* return 1 if OK, 0 on error */
330 static int copy_handle_table( struct process *process, struct process *parent )
331 {
332     struct handle_entry *ptr;
333     int i, count, last;
334
335     if (!parent)  /* first process */
336     {
337         count = MIN_HANDLE_ENTRIES;
338         last  = -1;
339     }
340     else
341     {
342         assert( parent->entries );
343         count = parent->handle_count;
344         last  = parent->handle_last;
345     }
346
347     if (!(ptr = malloc( count * sizeof(struct handle_entry)))) return 0;
348     process->entries      = ptr;
349     process->handle_count = count;
350     process->handle_last  = last;
351
352     if (last >= 0)
353     {
354         memcpy( ptr, parent->entries, (last + 1) * sizeof(struct handle_entry) );
355         for (i = 0; i <= last; i++, ptr++)
356         {
357             if (!ptr->ptr) continue;
358             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
359             else ptr->ptr = NULL; /* don't inherit this entry */
360         }
361     }
362     /* attempt to shrink the table */
363     shrink_handle_table( process );
364     return 1;
365 }
366
367 /* close a handle and decrement the refcount of the associated object */
368 /* return 1 if OK, 0 on error */
369 int close_handle( struct process *process, int handle )
370 {
371     struct handle_entry *entry;
372     struct object *obj;
373
374     if (!(entry = get_handle( process, handle ))) return 0;
375     if (entry->access & RESERVED_CLOSE_PROTECT) return 0;  /* FIXME: error code */
376     obj = entry->ptr;
377     entry->ptr = NULL;
378     if (handle-1 == process->handle_last) shrink_handle_table( process );
379     release_object( obj );
380     return 1;
381 }
382
383 /* retrieve the object corresponding to a handle, incrementing its refcount */
384 struct object *get_handle_obj( struct process *process, int handle,
385                                unsigned int access, const struct object_ops *ops )
386 {
387     struct handle_entry *entry;
388     struct object *obj;
389
390     switch( handle )
391     {
392     case 0xfffffffe:  /* current thread pseudo-handle */
393         obj = &current->obj;
394         break;
395     case 0x7fffffff:  /* current process pseudo-handle */
396         obj = (struct object *)current->process;
397         break;
398     default:
399         if (!(entry = get_handle( process, handle ))) return NULL;
400         if ((entry->access & access) != access)
401         {
402             SET_ERROR( ERROR_ACCESS_DENIED );
403             return NULL;
404         }
405         obj = entry->ptr;
406         break;
407     }
408     if (ops && (obj->ops != ops))
409     {
410         SET_ERROR( ERROR_INVALID_HANDLE );  /* not the right type */
411         return NULL;
412     }
413     return grab_object( obj );
414 }
415
416 /* get/set the handle reserved flags */
417 /* return the new flags (or -1 on error) */
418 int set_handle_info( struct process *process, int handle, int mask, int flags )
419 {
420     struct handle_entry *entry;
421
422     if (!(entry = get_handle( process, handle ))) return -1;
423     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
424     flags = (flags << RESERVED_SHIFT) & mask;
425     entry->access = (entry->access & ~mask) | flags;
426     return (entry->access & RESERVED_ALL) >> RESERVED_SHIFT;
427 }
428
429 /* duplicate a handle */
430 int duplicate_handle( struct process *src, int src_handle, struct process *dst,
431                       int dst_handle, unsigned int access, int inherit, int options )
432 {
433     struct handle_entry *entry = get_handle( src, src_handle );
434     if (!entry) return -1;
435
436     if (options & DUPLICATE_SAME_ACCESS) access = entry->access;
437     access &= ~RESERVED_ALL;
438     return alloc_specific_handle( dst, entry->ptr, dst_handle, access, inherit );
439 }
440
441 /* dump a handle table on stdout */
442 void dump_handles( struct process *process )
443 {
444     struct handle_entry *entry;
445     int i;
446
447     if (!process->entries) return;
448     entry = process->entries;
449     for (i = 0; i <= process->handle_last; i++, entry++)
450     {
451         if (!entry->ptr) continue;
452         printf( "%5d: %p %08x ", i + 1, entry->ptr, entry->access );
453         entry->ptr->ops->dump( entry->ptr, 0 );
454     }
455 }
456
457 /* kill a process on the spot */
458 void kill_process( struct process *process, int exit_code )
459 {
460     while (process->thread_list)
461         kill_thread( process->thread_list, exit_code );
462 }
463
464 /* get all information about a process */
465 void get_process_info( struct process *process,
466                        struct get_process_info_reply *reply )
467 {
468     reply->pid       = process;
469     reply->exit_code = process->exit_code;
470 }