Save the registry before exiting on a SIGTERM.
[wine] / server / handle.c
1 /*
2  * Server-side handle 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
13 #include "winbase.h"
14
15 #include "handle.h"
16 #include "process.h"
17 #include "thread.h"
18 #include "request.h"
19
20 struct handle_entry
21 {
22     struct object *ptr;       /* object */
23     unsigned int   access;    /* access rights */
24     int            fd;        /* file descriptor (in client process) */
25 };
26
27 struct handle_table
28 {
29     struct object        obj;         /* object header */
30     struct process      *process;     /* process owning this table */
31     int                  count;       /* number of allocated entries */
32     int                  last;        /* last used entry */
33     int                  free;        /* first entry that may be free */
34     struct handle_entry *entries;     /* handle entries */
35 };
36
37 static struct handle_table *global_table;
38
39 /* reserved handle access rights */
40 #define RESERVED_SHIFT         25
41 #define RESERVED_INHERIT       (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
42 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
43 #define RESERVED_ALL           (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
44
45 #define MIN_HANDLE_ENTRIES  32
46
47
48 /* handle to table index conversion */
49
50 /* handles are a multiple of 4 under NT; handle 0 is not used */
51 inline static handle_t index_to_handle( int index )
52 {
53     return (handle_t)((index + 1) << 2);
54 }
55 inline static int handle_to_index( handle_t handle )
56 {
57     return ((unsigned int)handle >> 2) - 1;
58 }
59
60 /* global handle conversion */
61
62 #define HANDLE_OBFUSCATOR 0x544a4def
63
64 inline static int handle_is_global( handle_t handle)
65 {
66     return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
67 }
68 inline static handle_t handle_local_to_global( handle_t handle )
69 {
70     if (!handle) return 0;
71     return (handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
72 }
73 inline static handle_t handle_global_to_local( handle_t handle )
74 {
75     return (handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
76 }
77
78
79 static void handle_table_dump( struct object *obj, int verbose );
80 static void handle_table_destroy( struct object *obj );
81
82 static const struct object_ops handle_table_ops =
83 {
84     sizeof(struct handle_table),     /* size */
85     handle_table_dump,               /* dump */
86     no_add_queue,                    /* add_queue */
87     NULL,                            /* remove_queue */
88     NULL,                            /* signaled */
89     NULL,                            /* satisfied */
90     NULL,                            /* get_poll_events */
91     NULL,                            /* poll_event */
92     no_get_fd,                       /* get_fd */
93     no_flush,                        /* flush */
94     no_get_file_info,                /* get_file_info */
95     NULL,                            /* queue_async */
96     handle_table_destroy             /* destroy */
97 };
98
99 /* dump a handle table */
100 static void handle_table_dump( struct object *obj, int verbose )
101 {
102     int i;
103     struct handle_table *table = (struct handle_table *)obj;
104     struct handle_entry *entry = table->entries;
105
106     assert( obj->ops == &handle_table_ops );
107
108     fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
109              table->last, table->count, table->process );
110     if (!verbose) return;
111     entry = table->entries;
112     for (i = 0; i <= table->last; i++, entry++)
113     {
114         if (!entry->ptr) continue;
115         fprintf( stderr, "%9u: %p %08x ",
116                  (unsigned int)index_to_handle(i), entry->ptr, entry->access );
117         entry->ptr->ops->dump( entry->ptr, 0 );
118     }
119 }
120
121 /* destroy a handle table */
122 static void handle_table_destroy( struct object *obj )
123 {
124     int i;
125     struct handle_table *table = (struct handle_table *)obj;
126     struct handle_entry *entry = table->entries;
127
128     assert( obj->ops == &handle_table_ops );
129
130     for (i = 0; i <= table->last; i++, entry++)
131     {
132         struct object *obj = entry->ptr;
133         entry->ptr = NULL;
134         if (obj) release_object( obj );
135     }
136     free( table->entries );
137 }
138
139 /* allocate a new handle table */
140 struct object *alloc_handle_table( struct process *process, int count )
141 {
142     struct handle_table *table;
143
144     if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
145     if (!(table = alloc_object( &handle_table_ops, -1 )))
146         return NULL;
147     table->process = process;
148     table->count   = count;
149     table->last    = -1;
150     table->free    = 0;
151     if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
152     release_object( table );
153     return NULL;
154 }
155
156 /* grow a handle table */
157 static int grow_handle_table( struct handle_table *table )
158 {
159     struct handle_entry *new_entries;
160     int count = table->count;
161
162     if (count >= INT_MAX / 2) return 0;
163     count *= 2;
164     if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
165     {
166         set_error( STATUS_NO_MEMORY );
167         return 0;
168     }
169     table->entries = new_entries;
170     table->count   = count;
171     return 1;
172 }
173
174 /* allocate the first free entry in the handle table */
175 static handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
176 {
177     struct handle_entry *entry = table->entries + table->free;
178     int i;
179
180     for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
181     if (i >= table->count)
182     {
183         if (!grow_handle_table( table )) return 0;
184         entry = table->entries + i;  /* the entries may have moved */
185     }
186     table->last = i;
187  found:
188     table->free = i + 1;
189     entry->ptr    = grab_object( obj );
190     entry->access = access;
191     entry->fd     = -1;
192     return index_to_handle(i);
193 }
194
195 /* allocate a handle for an object, incrementing its refcount */
196 /* return the handle, or 0 on error */
197 handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
198 {
199     struct handle_table *table = (struct handle_table *)process->handles;
200
201     assert( table );
202     assert( !(access & RESERVED_ALL) );
203     if (inherit) access |= RESERVED_INHERIT;
204     return alloc_entry( table, obj, access );
205 }
206
207 /* allocate a global handle for an object, incrementing its refcount */
208 /* return the handle, or 0 on error */
209 static handle_t alloc_global_handle( void *obj, unsigned int access )
210 {
211     if (!global_table)
212     {
213         if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
214             return 0;
215     }
216     return handle_local_to_global( alloc_entry( global_table, obj, access ));
217 }
218
219 /* return a handle entry, or NULL if the handle is invalid */
220 static struct handle_entry *get_handle( struct process *process, handle_t handle )
221 {
222     struct handle_table *table = (struct handle_table *)process->handles;
223     struct handle_entry *entry;
224     int index;
225
226     if (handle_is_global(handle))
227     {
228         handle = handle_global_to_local(handle);
229         table = global_table;
230     }
231     if (!table) goto error;
232     index = handle_to_index( handle );
233     if (index < 0) goto error;
234     if (index > table->last) goto error;
235     entry = table->entries + index;
236     if (!entry->ptr) goto error;
237     return entry;
238
239  error:
240     set_error( STATUS_INVALID_HANDLE );
241     return NULL;
242 }
243
244 /* attempt to shrink a table */
245 static void shrink_handle_table( struct handle_table *table )
246 {
247     struct handle_entry *entry = table->entries + table->last;
248     struct handle_entry *new_entries;
249     int count = table->count;
250
251     while (table->last >= 0)
252     {
253         if (entry->ptr) break;
254         table->last--;
255         entry--;
256     }
257     if (table->last >= count / 4) return;  /* no need to shrink */
258     if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
259     count /= 2;
260     if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
261     table->count   = count;
262     table->entries = new_entries;
263 }
264
265 /* copy the handle table of the parent process */
266 /* return 1 if OK, 0 on error */
267 struct object *copy_handle_table( struct process *process, struct process *parent )
268 {
269     struct handle_table *parent_table = (struct handle_table *)parent->handles;
270     struct handle_table *table;
271     int i;
272
273     assert( parent_table );
274     assert( parent_table->obj.ops == &handle_table_ops );
275
276     if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
277         return NULL;
278
279     if ((table->last = parent_table->last) >= 0)
280     {
281         struct handle_entry *ptr = table->entries;
282         memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
283         for (i = 0; i <= table->last; i++, ptr++)
284         {
285             if (!ptr->ptr) continue;
286             ptr->fd = -1;
287             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
288             else ptr->ptr = NULL; /* don't inherit this entry */
289         }
290     }
291     /* attempt to shrink the table */
292     shrink_handle_table( table );
293     return &table->obj;
294 }
295
296 /* close a handle and decrement the refcount of the associated object */
297 /* return 1 if OK, 0 on error */
298 int close_handle( struct process *process, handle_t handle, int *fd )
299 {
300     struct handle_table *table;
301     struct handle_entry *entry;
302     struct object *obj;
303
304     if (!(entry = get_handle( process, handle ))) return 0;
305     if (entry->access & RESERVED_CLOSE_PROTECT)
306     {
307         set_error( STATUS_INVALID_HANDLE );
308         return 0;
309     }
310     obj = entry->ptr;
311     entry->ptr = NULL;
312     if (fd) *fd = entry->fd;
313     else if (entry->fd != -1) return 1;  /* silently ignore close attempt if we cannot close the fd */
314     entry->fd = -1;
315     table = handle_is_global(handle) ? global_table : (struct handle_table *)process->handles;
316     if (entry < table->entries + table->free) table->free = entry - table->entries;
317     if (entry == table->entries + table->last) shrink_handle_table( table );
318     release_object( obj );
319     return 1;
320 }
321
322 /* close all the global handles */
323 void close_global_handles(void)
324 {
325     if (global_table)
326     {
327         release_object( global_table );
328         global_table = NULL;
329     }
330 }
331
332 /* retrieve the object corresponding to one of the magic pseudo-handles */
333 static inline struct object *get_magic_handle( handle_t handle )
334 {
335     switch((unsigned long)handle)
336     {
337         case 0xfffffffe:  /* current thread pseudo-handle */
338             return &current->obj;
339         case 0x7fffffff:  /* current process pseudo-handle */
340         case 0xffffffff:  /* current process pseudo-handle */
341             return (struct object *)current->process;
342         default:
343             return NULL;
344     }
345 }
346
347 /* retrieve the object corresponding to a handle, incrementing its refcount */
348 struct object *get_handle_obj( struct process *process, handle_t handle,
349                                unsigned int access, const struct object_ops *ops )
350 {
351     struct handle_entry *entry;
352     struct object *obj;
353
354     if (!(obj = get_magic_handle( handle )))
355     {
356         if (!(entry = get_handle( process, handle ))) return NULL;
357         if ((entry->access & access) != access)
358         {
359             set_error( STATUS_ACCESS_DENIED );
360             return NULL;
361         }
362         obj = entry->ptr;
363     }
364     if (ops && (obj->ops != ops))
365     {
366         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
367         return NULL;
368     }
369     return grab_object( obj );
370 }
371
372 /* retrieve the cached fd for a given handle */
373 int get_handle_fd( struct process *process, handle_t handle, unsigned int access )
374 {
375     struct handle_entry *entry;
376
377     if (!(entry = get_handle( process, handle ))) return -1;
378     if ((entry->access & access) != access)
379     {
380         set_error( STATUS_ACCESS_DENIED );
381         return -1;
382     }
383     return entry->fd;
384 }
385
386 /* get/set the handle reserved flags */
387 /* return the old flags (or -1 on error) */
388 static int set_handle_info( struct process *process, handle_t handle,
389                             int mask, int flags, int *fd )
390 {
391     struct handle_entry *entry;
392     unsigned int old_access;
393
394     if (get_magic_handle( handle ))
395     {
396         /* we can retrieve but not set info for magic handles */
397         if (mask) set_error( STATUS_ACCESS_DENIED );
398         return 0;
399     }
400     if (!(entry = get_handle( process, handle ))) return -1;
401     old_access = entry->access;
402     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
403     flags = (flags << RESERVED_SHIFT) & mask;
404     entry->access = (entry->access & ~mask) | flags;
405     /* if no current fd set it, otherwise return current fd */
406     if (entry->fd == -1) entry->fd = *fd;
407     *fd = entry->fd;
408     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
409 }
410
411 /* duplicate a handle */
412 handle_t duplicate_handle( struct process *src, handle_t src_handle, struct process *dst,
413                            unsigned int access, int inherit, int options )
414 {
415     handle_t res;
416     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
417
418     if (!obj) return 0;
419     if (options & DUP_HANDLE_SAME_ACCESS)
420     {
421         struct handle_entry *entry = get_handle( src, src_handle );
422         if (entry)
423             access = entry->access;
424         else  /* pseudo-handle, give it full access */
425         {
426             access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
427             clear_error();
428         }
429     }
430     access &= ~RESERVED_ALL;
431     if (options & DUP_HANDLE_MAKE_GLOBAL)
432         res = alloc_global_handle( obj, access );
433     else
434         res = alloc_handle( dst, obj, access, inherit );
435     release_object( obj );
436     return res;
437 }
438
439 /* open a new handle to an existing object */
440 handle_t open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
441                       unsigned int access, int inherit )
442 {
443     handle_t handle = 0;
444     struct object *obj = find_object( name, len );
445     if (obj)
446     {
447         if (ops && obj->ops != ops)
448             set_error( STATUS_OBJECT_TYPE_MISMATCH );
449         else
450             handle = alloc_handle( current->process, obj, access, inherit );
451         release_object( obj );
452     }
453     else
454         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
455     return handle;
456 }
457
458 /* close a handle */
459 DECL_HANDLER(close_handle)
460 {
461     close_handle( current->process, req->handle, &reply->fd );
462 }
463
464 /* set a handle information */
465 DECL_HANDLER(set_handle_info)
466 {
467     int fd = req->fd;
468
469     if (handle_is_global(req->handle)) fd = -1;  /* no fd cache for global handles */
470     reply->old_flags = set_handle_info( current->process, req->handle,
471                                         req->mask, req->flags, &fd );
472     reply->cur_fd = fd;
473 }
474
475 /* duplicate a handle */
476 DECL_HANDLER(dup_handle)
477 {
478     struct process *src, *dst;
479
480     reply->handle = 0;
481     reply->fd = -1;
482     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
483     {
484         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
485         {
486             reply->handle = duplicate_handle( src, req->src_handle, NULL,
487                                               req->access, req->inherit, req->options );
488         }
489         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
490         {
491             reply->handle = duplicate_handle( src, req->src_handle, dst,
492                                               req->access, req->inherit, req->options );
493             release_object( dst );
494         }
495         /* close the handle no matter what happened */
496         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
497         {
498             if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
499             else close_handle( src, req->src_handle, NULL );
500         }
501         release_object( src );
502     }
503 }