Added one safety check to AFM parsing.
[wine/multimedia.git] / server / handle.c
blobc3b8a05071a729d8421483486abd19bb73e37462
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 "winbase.h"
15 #include "handle.h"
16 #include "process.h"
17 #include "thread.h"
18 #include "request.h"
20 struct handle_entry
22 struct object *ptr; /* object */
23 unsigned int access; /* access rights */
24 int fd; /* file descriptor (in client process) */
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 inline static int index_to_handle( int index )
59 return (index + 1) << 2;
61 inline static int 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), /* size */
73 handle_table_dump, /* dump */
74 no_add_queue, /* add_queue */
75 NULL, /* remove_queue */
76 NULL, /* signaled */
77 NULL, /* satisfied */
78 NULL, /* get_poll_events */
79 NULL, /* poll_event */
80 no_get_fd, /* get_fd */
81 no_flush, /* flush */
82 no_get_file_info, /* get_file_info */
83 handle_table_destroy /* destroy */
86 /* dump a handle table */
87 static void handle_table_dump( struct object *obj, int verbose )
89 int i;
90 struct handle_table *table = (struct handle_table *)obj;
91 struct handle_entry *entry = table->entries;
93 assert( obj->ops == &handle_table_ops );
95 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
96 table->last, table->count, table->process );
97 if (!verbose) return;
98 entry = table->entries;
99 for (i = 0; i <= table->last; i++, entry++)
101 if (!entry->ptr) continue;
102 fprintf( stderr, "%9d: %p %08x ", index_to_handle(i), entry->ptr, entry->access );
103 entry->ptr->ops->dump( entry->ptr, 0 );
107 /* destroy a handle table */
108 static void handle_table_destroy( struct object *obj )
110 int i;
111 struct handle_table *table = (struct handle_table *)obj;
112 struct handle_entry *entry = table->entries;
114 assert( obj->ops == &handle_table_ops );
116 for (i = 0; i <= table->last; i++, entry++)
118 struct object *obj = entry->ptr;
119 entry->ptr = NULL;
120 if (obj) release_object( obj );
122 free( table->entries );
125 /* allocate a new handle table */
126 struct object *alloc_handle_table( struct process *process, int count )
128 struct handle_table *table;
130 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
131 if (!(table = alloc_object( &handle_table_ops, -1 )))
132 return NULL;
133 table->process = process;
134 table->count = count;
135 table->last = -1;
136 table->free = 0;
137 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
138 release_object( table );
139 return NULL;
142 /* grow a handle table */
143 static int grow_handle_table( struct handle_table *table )
145 struct handle_entry *new_entries;
146 int count = table->count;
148 if (count >= INT_MAX / 2) return 0;
149 count *= 2;
150 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
152 set_error( STATUS_NO_MEMORY );
153 return 0;
155 table->entries = new_entries;
156 table->count = count;
157 return 1;
160 /* allocate the first free entry in the handle table */
161 static int alloc_entry( struct handle_table *table, void *obj, unsigned int access )
163 struct handle_entry *entry = table->entries + table->free;
164 int i;
166 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
167 if (i >= table->count)
169 if (!grow_handle_table( table )) return -1;
170 entry = table->entries + i; /* the entries may have moved */
172 table->last = i;
173 found:
174 table->free = i + 1;
175 entry->ptr = grab_object( obj );
176 entry->access = access;
177 entry->fd = -1;
178 return index_to_handle(i);
181 /* allocate a handle for an object, incrementing its refcount */
182 /* return the handle, or -1 on error */
183 int alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
185 struct handle_table *table = (struct handle_table *)process->handles;
187 assert( table );
188 assert( !(access & RESERVED_ALL) );
189 if (inherit) access |= RESERVED_INHERIT;
190 return alloc_entry( table, obj, access );
193 /* allocate a global handle for an object, incrementing its refcount */
194 /* return the handle, or -1 on error */
195 static int alloc_global_handle( void *obj, unsigned int access )
197 int handle;
199 if (!global_table)
201 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 ))) return -1;
203 if ((handle = alloc_entry( global_table, obj, access )) != -1)
204 handle = HANDLE_LOCAL_TO_GLOBAL(handle);
205 return handle;
208 /* return a handle entry, or NULL if the handle is invalid */
209 static struct handle_entry *get_handle( struct process *process, int handle )
211 struct handle_table *table = (struct handle_table *)process->handles;
212 struct handle_entry *entry;
214 if (HANDLE_IS_GLOBAL(handle))
216 handle = HANDLE_GLOBAL_TO_LOCAL(handle);
217 table = global_table;
219 if (!table) goto error;
220 handle = handle_to_index( handle );
221 if (handle < 0) goto error;
222 if (handle > table->last) goto error;
223 entry = table->entries + handle;
224 if (!entry->ptr) goto error;
225 return entry;
227 error:
228 set_error( STATUS_INVALID_HANDLE );
229 return NULL;
232 /* attempt to shrink a table */
233 static void shrink_handle_table( struct handle_table *table )
235 struct handle_entry *entry = table->entries + table->last;
236 struct handle_entry *new_entries;
237 int count = table->count;
239 while (table->last >= 0)
241 if (entry->ptr) break;
242 table->last--;
243 entry--;
245 if (table->last >= count / 4) return; /* no need to shrink */
246 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
247 count /= 2;
248 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
249 table->count = count;
250 table->entries = new_entries;
253 /* copy the handle table of the parent process */
254 /* return 1 if OK, 0 on error */
255 struct object *copy_handle_table( struct process *process, struct process *parent )
257 struct handle_table *parent_table = (struct handle_table *)parent->handles;
258 struct handle_table *table;
259 int i;
261 assert( parent_table );
262 assert( parent_table->obj.ops == &handle_table_ops );
264 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
265 return NULL;
267 if ((table->last = parent_table->last) >= 0)
269 struct handle_entry *ptr = table->entries;
270 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
271 for (i = 0; i <= table->last; i++, ptr++)
273 if (!ptr->ptr) continue;
274 ptr->fd = -1;
275 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
276 else ptr->ptr = NULL; /* don't inherit this entry */
279 /* attempt to shrink the table */
280 shrink_handle_table( table );
281 return &table->obj;
284 /* close a handle and decrement the refcount of the associated object */
285 /* return 1 if OK, 0 on error */
286 int close_handle( struct process *process, int handle, int *fd )
288 struct handle_table *table;
289 struct handle_entry *entry;
290 struct object *obj;
292 if (!(entry = get_handle( process, handle ))) return 0;
293 if (entry->access & RESERVED_CLOSE_PROTECT)
295 set_error( STATUS_INVALID_HANDLE );
296 return 0;
298 obj = entry->ptr;
299 entry->ptr = NULL;
300 if (fd) *fd = entry->fd;
301 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
302 entry->fd = -1;
303 table = HANDLE_IS_GLOBAL(handle) ? global_table : (struct handle_table *)process->handles;
304 if (entry < table->entries + table->free) table->free = entry - table->entries;
305 if (entry == table->entries + table->last) shrink_handle_table( table );
306 release_object( obj );
307 return 1;
310 /* close all the global handles */
311 void close_global_handles(void)
313 if (global_table)
315 release_object( global_table );
316 global_table = NULL;
320 /* retrieve the object corresponding to one of the magic pseudo-handles */
321 static inline struct object *get_magic_handle( int handle )
323 switch(handle)
325 case 0xfffffffe: /* current thread pseudo-handle */
326 return &current->obj;
327 case 0x7fffffff: /* current process pseudo-handle */
328 case 0xffffffff: /* current process pseudo-handle */
329 return (struct object *)current->process;
330 default:
331 return NULL;
335 /* retrieve the object corresponding to a handle, incrementing its refcount */
336 struct object *get_handle_obj( struct process *process, int handle,
337 unsigned int access, const struct object_ops *ops )
339 struct handle_entry *entry;
340 struct object *obj;
342 if (!(obj = get_magic_handle( handle )))
344 if (!(entry = get_handle( process, handle ))) return NULL;
345 if ((entry->access & access) != access)
347 set_error( STATUS_ACCESS_DENIED );
348 return NULL;
350 obj = entry->ptr;
352 if (ops && (obj->ops != ops))
354 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
355 return NULL;
357 return grab_object( obj );
360 /* retrieve the cached fd for a given handle */
361 int get_handle_fd( struct process *process, int handle, unsigned int access )
363 struct handle_entry *entry;
365 if (!(entry = get_handle( process, handle ))) return -1;
366 if ((entry->access & access) != access)
368 set_error( STATUS_ACCESS_DENIED );
369 return -1;
371 return entry->fd;
374 /* get/set the handle reserved flags */
375 /* return the old flags (or -1 on error) */
376 static int set_handle_info( struct process *process, int handle, int mask, int flags, int *fd )
378 struct handle_entry *entry;
379 unsigned int old_access;
381 if (get_magic_handle( handle ))
383 /* we can retrieve but not set info for magic handles */
384 if (mask) set_error( STATUS_ACCESS_DENIED );
385 return 0;
387 if (!(entry = get_handle( process, handle ))) return -1;
388 old_access = entry->access;
389 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
390 flags = (flags << RESERVED_SHIFT) & mask;
391 entry->access = (entry->access & ~mask) | flags;
392 /* if no current fd set it, otherwise return current fd */
393 if (entry->fd == -1) entry->fd = *fd;
394 *fd = entry->fd;
395 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
398 /* duplicate a handle */
399 int duplicate_handle( struct process *src, int src_handle, struct process *dst,
400 unsigned int access, int inherit, int options )
402 int res;
403 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
405 if (!obj) return -1;
406 if (options & DUP_HANDLE_SAME_ACCESS)
408 struct handle_entry *entry = get_handle( src, src_handle );
409 if (entry)
410 access = entry->access;
411 else /* pseudo-handle, give it full access */
413 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
414 clear_error();
417 access &= ~RESERVED_ALL;
418 if (options & DUP_HANDLE_MAKE_GLOBAL)
419 res = alloc_global_handle( obj, access );
420 else
421 res = alloc_handle( dst, obj, access, inherit );
422 release_object( obj );
423 return res;
426 /* open a new handle to an existing object */
427 int open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
428 unsigned int access, int inherit )
430 int handle = -1;
431 struct object *obj = find_object( name, len );
432 if (obj)
434 if (ops && obj->ops != ops)
435 set_error( STATUS_OBJECT_TYPE_MISMATCH );
436 else
437 handle = alloc_handle( current->process, obj, access, inherit );
438 release_object( obj );
440 else
441 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
442 return handle;
445 /* close a handle */
446 DECL_HANDLER(close_handle)
448 close_handle( current->process, req->handle, &req->fd );
451 /* set a handle information */
452 DECL_HANDLER(set_handle_info)
454 int fd = req->fd;
456 if (HANDLE_IS_GLOBAL(req->handle)) fd = -1; /* no fd cache for global handles */
457 req->old_flags = set_handle_info( current->process, req->handle, req->mask, req->flags, &fd );
458 req->cur_fd = fd;
461 /* duplicate a handle */
462 DECL_HANDLER(dup_handle)
464 struct process *src, *dst;
466 req->handle = -1;
467 req->fd = -1;
468 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
470 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
472 req->handle = duplicate_handle( src, req->src_handle, NULL,
473 req->access, req->inherit, req->options );
475 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
477 req->handle = duplicate_handle( src, req->src_handle, dst,
478 req->access, req->inherit, req->options );
479 release_object( dst );
481 /* close the handle no matter what happened */
482 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
484 if (src == current->process) close_handle( src, req->src_handle, &req->fd );
485 else close_handle( src, req->src_handle, NULL );
487 release_object( src );