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