Pierre Mageau
[wine/multimedia.git] / server / handle.c
blobea6d6c89f1e15ad1c3788dd45b42ba6a7902c58a
1 /*
2 * Server-side handle management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <limits.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
13 #include "winerror.h"
14 #include "winbase.h"
16 #include "handle.h"
17 #include "process.h"
18 #include "thread.h"
19 #include "request.h"
21 struct handle_entry
23 struct object *ptr;
24 unsigned int access;
27 struct handle_table
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 */
37 static struct handle_table *global_table;
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)
45 /* global handle macros */
46 #define HANDLE_OBFUSCATOR 0x544a4def
47 #define HANDLE_IS_GLOBAL(h) (((h) ^ HANDLE_OBFUSCATOR) < 0x10000)
48 #define HANDLE_LOCAL_TO_GLOBAL(h) ((h) ^ HANDLE_OBFUSCATOR)
49 #define HANDLE_GLOBAL_TO_LOCAL(h) ((h) ^ HANDLE_OBFUSCATOR)
51 #define MIN_HANDLE_ENTRIES 32
54 /* handle to table index conversion */
56 /* handles are a multiple of 4 under NT; handle 0 is not used */
57 static int inline index_to_handle( int index )
59 return (index + 1) << 2;
61 static int inline handle_to_index( int handle )
63 return (handle >> 2) - 1;
67 static void handle_table_dump( struct object *obj, int verbose );
68 static void handle_table_destroy( struct object *obj );
70 static const struct object_ops handle_table_ops =
72 sizeof(struct handle_table),
73 handle_table_dump,
74 no_add_queue,
75 NULL, /* should never get called */
76 NULL, /* should never get called */
77 NULL, /* should never get called */
78 no_read_fd,
79 no_write_fd,
80 no_flush,
81 no_get_file_info,
82 handle_table_destroy
85 /* dump a handle table */
86 static void handle_table_dump( struct object *obj, int verbose )
88 int i;
89 struct handle_table *table = (struct handle_table *)obj;
90 struct handle_entry *entry = table->entries;
92 assert( obj->ops == &handle_table_ops );
94 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
95 table->last, table->count, table->process );
96 if (!verbose) return;
97 entry = table->entries;
98 for (i = 0; i <= table->last; i++, entry++)
100 if (!entry->ptr) continue;
101 fprintf( stderr, "%9d: %p %08x ", index_to_handle(i), entry->ptr, entry->access );
102 entry->ptr->ops->dump( entry->ptr, 0 );
106 /* destroy a handle table */
107 static void handle_table_destroy( struct object *obj )
109 int i;
110 struct handle_table *table = (struct handle_table *)obj;
111 struct handle_entry *entry = table->entries;
113 assert( obj->ops == &handle_table_ops );
115 for (i = 0; i <= table->last; i++, entry++)
117 struct object *obj = entry->ptr;
118 entry->ptr = NULL;
119 if (obj) release_object( obj );
121 free( table->entries );
124 /* allocate a new handle table */
125 struct object *alloc_handle_table( struct process *process, int count )
127 struct handle_table *table;
129 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
130 if (!(table = alloc_object( &handle_table_ops )))
131 return NULL;
132 table->process = process;
133 table->count = count;
134 table->last = -1;
135 table->free = 0;
136 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
137 release_object( table );
138 return NULL;
141 /* grow a handle table */
142 static int grow_handle_table( struct handle_table *table )
144 struct handle_entry *new_entries;
145 int count = table->count;
147 if (count >= INT_MAX / 2) return 0;
148 count *= 2;
149 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
151 set_error( ERROR_OUTOFMEMORY );
152 return 0;
154 table->entries = new_entries;
155 table->count = count;
156 return 1;
159 /* allocate the first free entry in the handle table */
160 static int alloc_entry( struct handle_table *table, void *obj, unsigned int access )
162 struct handle_entry *entry = table->entries + table->free;
163 int i;
165 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
166 if (i >= table->count)
168 if (!grow_handle_table( table )) return -1;
169 entry = table->entries + i; /* the entries may have moved */
171 table->last = i;
172 found:
173 table->free = i + 1;
174 entry->ptr = grab_object( obj );
175 entry->access = access;
176 return index_to_handle(i);
179 /* allocate a handle for an object, incrementing its refcount */
180 /* return the handle, or -1 on error */
181 int alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
183 struct handle_table *table = (struct handle_table *)process->handles;
185 assert( table );
186 assert( !(access & RESERVED_ALL) );
187 if (inherit) access |= RESERVED_INHERIT;
188 return alloc_entry( table, obj, access );
191 /* allocate a global handle for an object, incrementing its refcount */
192 /* return the handle, or -1 on error */
193 static int alloc_global_handle( void *obj, unsigned int access )
195 int handle;
197 if (!global_table)
199 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 ))) return -1;
201 if ((handle = alloc_entry( global_table, obj, access )) != -1)
202 handle = HANDLE_LOCAL_TO_GLOBAL(handle);
203 return handle;
206 /* return a handle entry, or NULL if the handle is invalid */
207 static struct handle_entry *get_handle( struct process *process, int handle )
209 struct handle_table *table = (struct handle_table *)process->handles;
210 struct handle_entry *entry;
212 if (HANDLE_IS_GLOBAL(handle))
214 handle = HANDLE_GLOBAL_TO_LOCAL(handle);
215 table = global_table;
217 if (!table) goto error;
218 handle = handle_to_index( handle );
219 if (handle < 0) goto error;
220 if (handle > table->last) goto error;
221 entry = table->entries + handle;
222 if (!entry->ptr) goto error;
223 return entry;
225 error:
226 set_error( ERROR_INVALID_HANDLE );
227 return NULL;
230 /* attempt to shrink a table */
231 static void shrink_handle_table( struct handle_table *table )
233 struct handle_entry *entry = table->entries + table->last;
234 struct handle_entry *new_entries;
235 int count = table->count;
237 while (table->last >= 0)
239 if (entry->ptr) break;
240 table->last--;
241 entry--;
243 if (table->last >= count / 4) return; /* no need to shrink */
244 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
245 count /= 2;
246 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
247 table->count = count;
248 table->entries = new_entries;
251 /* copy the handle table of the parent process */
252 /* return 1 if OK, 0 on error */
253 struct object *copy_handle_table( struct process *process, struct process *parent )
255 struct handle_table *parent_table = (struct handle_table *)parent->handles;
256 struct handle_table *table;
257 int i;
259 assert( parent_table );
260 assert( parent_table->obj.ops == &handle_table_ops );
262 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
263 return NULL;
265 if ((table->last = parent_table->last) >= 0)
267 struct handle_entry *ptr = table->entries;
268 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
269 for (i = 0; i <= table->last; i++, ptr++)
271 if (!ptr->ptr) continue;
272 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
273 else ptr->ptr = NULL; /* don't inherit this entry */
276 /* attempt to shrink the table */
277 shrink_handle_table( table );
278 return &table->obj;
281 /* close a handle and decrement the refcount of the associated object */
282 /* return 1 if OK, 0 on error */
283 int close_handle( struct process *process, int handle )
285 struct handle_table *table;
286 struct handle_entry *entry;
287 struct object *obj;
289 if (!(entry = get_handle( process, handle ))) return 0;
290 if (entry->access & RESERVED_CLOSE_PROTECT)
292 set_error( ERROR_INVALID_HANDLE );
293 return 0;
295 obj = entry->ptr;
296 entry->ptr = NULL;
297 table = HANDLE_IS_GLOBAL(handle) ? global_table : (struct handle_table *)process->handles;
298 if (entry < table->entries + table->free) table->free = entry - table->entries;
299 if (entry == table->entries + table->last) shrink_handle_table( table );
300 release_object( obj );
301 return 1;
304 /* close all the global handles */
305 void close_global_handles(void)
307 if (global_table)
309 release_object( global_table );
310 global_table = NULL;
314 /* retrieve the object corresponding to one of the magic pseudo-handles */
315 static inline struct object *get_magic_handle( int handle )
317 switch(handle)
319 case 0xfffffffe: /* current thread pseudo-handle */
320 return &current->obj;
321 case 0x7fffffff: /* current process pseudo-handle */
322 case 0xffffffff: /* current process pseudo-handle */
323 return (struct object *)current->process;
324 default:
325 return NULL;
329 /* retrieve the object corresponding to a handle, incrementing its refcount */
330 struct object *get_handle_obj( struct process *process, int handle,
331 unsigned int access, const struct object_ops *ops )
333 struct handle_entry *entry;
334 struct object *obj;
336 if (!(obj = get_magic_handle( handle )))
338 if (!(entry = get_handle( process, handle ))) return NULL;
339 if ((entry->access & access) != access)
341 set_error( ERROR_ACCESS_DENIED );
342 return NULL;
344 obj = entry->ptr;
346 if (ops && (obj->ops != ops))
348 set_error( ERROR_INVALID_HANDLE ); /* not the right type */
349 return NULL;
351 return grab_object( obj );
354 /* get/set the handle reserved flags */
355 /* return the new flags (or -1 on error) */
356 static int set_handle_info( struct process *process, int handle, int mask, int flags )
358 struct handle_entry *entry;
360 if (get_magic_handle( handle ))
362 /* we can retrieve but not set info for magic handles */
363 if (mask) set_error( ERROR_ACCESS_DENIED );
364 return 0;
366 if (!(entry = get_handle( process, handle ))) return -1;
367 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
368 flags = (flags << RESERVED_SHIFT) & mask;
369 entry->access = (entry->access & ~mask) | flags;
370 return (entry->access & RESERVED_ALL) >> RESERVED_SHIFT;
373 /* duplicate a handle */
374 int duplicate_handle( struct process *src, int src_handle, struct process *dst,
375 unsigned int access, int inherit, int options )
377 int res;
378 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
380 if (!obj) return -1;
381 if (options & DUP_HANDLE_SAME_ACCESS)
383 struct handle_entry *entry = get_handle( src, src_handle );
384 if (entry)
385 access = entry->access;
386 else /* pseudo-handle, give it full access */
388 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
389 clear_error();
392 access &= ~RESERVED_ALL;
393 if (options & DUP_HANDLE_MAKE_GLOBAL)
394 res = alloc_global_handle( obj, access );
395 else
396 res = alloc_handle( dst, obj, access, inherit );
397 release_object( obj );
398 return res;
401 /* open a new handle to an existing object */
402 int open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
403 unsigned int access, int inherit )
405 int handle = -1;
406 struct object *obj = find_object( name, len );
407 if (obj)
409 if (ops && obj->ops != ops)
410 set_error( ERROR_INVALID_HANDLE );
411 else
412 handle = alloc_handle( current->process, obj, access, inherit );
413 release_object( obj );
415 else
416 set_error( ERROR_FILE_NOT_FOUND );
417 return handle;
420 /* close a handle */
421 DECL_HANDLER(close_handle)
423 close_handle( current->process, req->handle );
426 /* get information about a handle */
427 DECL_HANDLER(get_handle_info)
429 req->flags = set_handle_info( current->process, req->handle, 0, 0 );
432 /* set a handle information */
433 DECL_HANDLER(set_handle_info)
435 set_handle_info( current->process, req->handle, req->mask, req->flags );
438 /* duplicate a handle */
439 DECL_HANDLER(dup_handle)
441 struct process *src, *dst;
443 req->handle = -1;
444 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
446 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
448 req->handle = duplicate_handle( src, req->src_handle, NULL,
449 req->access, req->inherit, req->options );
451 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
453 req->handle = duplicate_handle( src, req->src_handle, dst,
454 req->access, req->inherit, req->options );
455 release_object( dst );
457 /* close the handle no matter what happened */
458 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
459 close_handle( src, req->src_handle );
460 release_object( src );