Fix the MHz detection code.
[wine/multimedia.git] / server / handle.c
blobff9e54605c87e21bb9031090589a9a76a65d8bb0
1 /*
2 * Server-side handle management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "winbase.h"
32 #include "handle.h"
33 #include "process.h"
34 #include "thread.h"
35 #include "request.h"
37 struct handle_entry
39 struct object *ptr; /* object */
40 unsigned int access; /* access rights */
41 int fd; /* file descriptor (in client process) */
44 struct handle_table
46 struct object obj; /* object header */
47 struct process *process; /* process owning this table */
48 int count; /* number of allocated entries */
49 int last; /* last used entry */
50 int free; /* first entry that may be free */
51 struct handle_entry *entries; /* handle entries */
54 static struct handle_table *global_table;
56 /* reserved handle access rights */
57 #define RESERVED_SHIFT 25
58 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
59 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
60 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
62 #define MIN_HANDLE_ENTRIES 32
65 /* handle to table index conversion */
67 /* handles are a multiple of 4 under NT; handle 0 is not used */
68 inline static obj_handle_t index_to_handle( int index )
70 return (obj_handle_t)((index + 1) << 2);
72 inline static int handle_to_index( obj_handle_t handle )
74 return ((unsigned int)handle >> 2) - 1;
77 /* global handle conversion */
79 #define HANDLE_OBFUSCATOR 0x544a4def
81 inline static int handle_is_global( obj_handle_t handle)
83 return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
85 inline static obj_handle_t handle_local_to_global( obj_handle_t handle )
87 if (!handle) return 0;
88 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
90 inline static obj_handle_t handle_global_to_local( obj_handle_t handle )
92 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
96 static void handle_table_dump( struct object *obj, int verbose );
97 static void handle_table_destroy( struct object *obj );
99 static const struct object_ops handle_table_ops =
101 sizeof(struct handle_table), /* size */
102 handle_table_dump, /* dump */
103 no_add_queue, /* add_queue */
104 NULL, /* remove_queue */
105 NULL, /* signaled */
106 NULL, /* satisfied */
107 no_get_fd, /* get_fd */
108 handle_table_destroy /* destroy */
111 /* dump a handle table */
112 static void handle_table_dump( struct object *obj, int verbose )
114 int i;
115 struct handle_table *table = (struct handle_table *)obj;
116 struct handle_entry *entry = table->entries;
118 assert( obj->ops == &handle_table_ops );
120 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
121 table->last, table->count, table->process );
122 if (!verbose) return;
123 entry = table->entries;
124 for (i = 0; i <= table->last; i++, entry++)
126 if (!entry->ptr) continue;
127 fprintf( stderr, "%9u: %p %08x ",
128 (unsigned int)index_to_handle(i), entry->ptr, entry->access );
129 entry->ptr->ops->dump( entry->ptr, 0 );
133 /* destroy a handle table */
134 static void handle_table_destroy( struct object *obj )
136 int i;
137 struct handle_table *table = (struct handle_table *)obj;
138 struct handle_entry *entry = table->entries;
140 assert( obj->ops == &handle_table_ops );
142 for (i = 0; i <= table->last; i++, entry++)
144 struct object *obj = entry->ptr;
145 entry->ptr = NULL;
146 if (obj) release_object( obj );
148 free( table->entries );
151 /* allocate a new handle table */
152 struct handle_table *alloc_handle_table( struct process *process, int count )
154 struct handle_table *table;
156 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
157 if (!(table = alloc_object( &handle_table_ops )))
158 return NULL;
159 table->process = process;
160 table->count = count;
161 table->last = -1;
162 table->free = 0;
163 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
164 release_object( table );
165 return NULL;
168 /* grow a handle table */
169 static int grow_handle_table( struct handle_table *table )
171 struct handle_entry *new_entries;
172 int count = table->count;
174 if (count >= INT_MAX / 2) return 0;
175 count *= 2;
176 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
178 set_error( STATUS_NO_MEMORY );
179 return 0;
181 table->entries = new_entries;
182 table->count = count;
183 return 1;
186 /* allocate the first free entry in the handle table */
187 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
189 struct handle_entry *entry = table->entries + table->free;
190 int i;
192 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
193 if (i >= table->count)
195 if (!grow_handle_table( table )) return 0;
196 entry = table->entries + i; /* the entries may have moved */
198 table->last = i;
199 found:
200 table->free = i + 1;
201 entry->ptr = grab_object( obj );
202 entry->access = access;
203 entry->fd = -1;
204 return index_to_handle(i);
207 /* allocate a handle for an object, incrementing its refcount */
208 /* return the handle, or 0 on error */
209 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
211 struct handle_table *table = process->handles;
213 assert( table );
214 assert( !(access & RESERVED_ALL) );
215 if (inherit) access |= RESERVED_INHERIT;
216 return alloc_entry( table, obj, access );
219 /* allocate a global handle for an object, incrementing its refcount */
220 /* return the handle, or 0 on error */
221 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
223 if (!global_table)
225 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
226 return 0;
228 return handle_local_to_global( alloc_entry( global_table, obj, access ));
231 /* return a handle entry, or NULL if the handle is invalid */
232 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
234 struct handle_table *table = process->handles;
235 struct handle_entry *entry;
236 int index;
238 if (handle_is_global(handle))
240 handle = handle_global_to_local(handle);
241 table = global_table;
243 if (!table) goto error;
244 index = handle_to_index( handle );
245 if (index < 0) goto error;
246 if (index > table->last) goto error;
247 entry = table->entries + index;
248 if (!entry->ptr) goto error;
249 return entry;
251 error:
252 set_error( STATUS_INVALID_HANDLE );
253 return NULL;
256 /* attempt to shrink a table */
257 static void shrink_handle_table( struct handle_table *table )
259 struct handle_entry *entry = table->entries + table->last;
260 struct handle_entry *new_entries;
261 int count = table->count;
263 while (table->last >= 0)
265 if (entry->ptr) break;
266 table->last--;
267 entry--;
269 if (table->last >= count / 4) return; /* no need to shrink */
270 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
271 count /= 2;
272 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
273 table->count = count;
274 table->entries = new_entries;
277 /* copy the handle table of the parent process */
278 /* return 1 if OK, 0 on error */
279 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
281 struct handle_table *parent_table = parent->handles;
282 struct handle_table *table;
283 int i;
285 assert( parent_table );
286 assert( parent_table->obj.ops == &handle_table_ops );
288 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
289 return NULL;
291 if ((table->last = parent_table->last) >= 0)
293 struct handle_entry *ptr = table->entries;
294 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
295 for (i = 0; i <= table->last; i++, ptr++)
297 if (!ptr->ptr) continue;
298 ptr->fd = -1;
299 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
300 else ptr->ptr = NULL; /* don't inherit this entry */
303 /* attempt to shrink the table */
304 shrink_handle_table( table );
305 return table;
308 /* close a handle and decrement the refcount of the associated object */
309 /* return 1 if OK, 0 on error */
310 int close_handle( struct process *process, obj_handle_t handle, int *fd )
312 struct handle_table *table;
313 struct handle_entry *entry;
314 struct object *obj;
316 if (!(entry = get_handle( process, handle ))) return 0;
317 if (entry->access & RESERVED_CLOSE_PROTECT)
319 set_error( STATUS_INVALID_HANDLE );
320 return 0;
322 obj = entry->ptr;
323 entry->ptr = NULL;
324 if (fd) *fd = entry->fd;
325 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
326 entry->fd = -1;
327 table = handle_is_global(handle) ? global_table : process->handles;
328 if (entry < table->entries + table->free) table->free = entry - table->entries;
329 if (entry == table->entries + table->last) shrink_handle_table( table );
330 /* hack: windows seems to treat registry handles differently */
331 registry_close_handle( obj, handle );
332 release_object( obj );
333 return 1;
336 /* close all the global handles */
337 void close_global_handles(void)
339 if (global_table)
341 release_object( global_table );
342 global_table = NULL;
346 /* retrieve the object corresponding to one of the magic pseudo-handles */
347 static inline struct object *get_magic_handle( obj_handle_t handle )
349 switch((unsigned long)handle)
351 case 0xfffffffe: /* current thread pseudo-handle */
352 return &current->obj;
353 case 0x7fffffff: /* current process pseudo-handle */
354 case 0xffffffff: /* current process pseudo-handle */
355 return (struct object *)current->process;
356 default:
357 return NULL;
361 /* retrieve the object corresponding to a handle, incrementing its refcount */
362 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
363 unsigned int access, const struct object_ops *ops )
365 struct handle_entry *entry;
366 struct object *obj;
368 if (!(obj = get_magic_handle( handle )))
370 if (!(entry = get_handle( process, handle ))) return NULL;
371 if ((entry->access & access) != access)
373 set_error( STATUS_ACCESS_DENIED );
374 return NULL;
376 obj = entry->ptr;
378 if (ops && (obj->ops != ops))
380 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
381 return NULL;
383 return grab_object( obj );
386 /* retrieve the cached fd for a given handle */
387 int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
389 struct handle_entry *entry;
391 if (!(entry = get_handle( process, handle ))) return -1;
392 if ((entry->access & access) != access)
394 set_error( STATUS_ACCESS_DENIED );
395 return -1;
397 return entry->fd;
400 /* remove the cached fd and return it */
401 int flush_cached_fd( struct process *process, obj_handle_t handle )
403 struct handle_entry *entry = get_handle( process, handle );
404 int fd = -1;
406 if (entry)
408 fd = entry->fd;
409 entry->fd = -1;
411 return fd;
414 /* find the first inherited handle of the given type */
415 /* this is needed for window stations and desktops (don't ask...) */
416 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
418 struct handle_table *table = process->handles;
419 struct handle_entry *ptr;
420 int i;
422 if (!table) return 0;
424 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
426 if (!ptr->ptr) continue;
427 if (ptr->ptr->ops != ops) continue;
428 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
430 return 0;
433 /* get/set the handle reserved flags */
434 /* return the old flags (or -1 on error) */
435 int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
437 struct handle_entry *entry;
438 unsigned int old_access;
440 if (get_magic_handle( handle ))
442 /* we can retrieve but not set info for magic handles */
443 if (mask) set_error( STATUS_ACCESS_DENIED );
444 return 0;
446 if (!(entry = get_handle( process, handle ))) return -1;
447 old_access = entry->access;
448 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
449 flags = (flags << RESERVED_SHIFT) & mask;
450 entry->access = (entry->access & ~mask) | flags;
451 /* if no current fd set it, otherwise return current fd */
452 if (entry->fd == -1) entry->fd = *fd;
453 *fd = entry->fd;
454 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
457 /* duplicate a handle */
458 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
459 unsigned int access, int inherit, int options )
461 obj_handle_t res;
462 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
464 if (!obj) return 0;
465 if (options & DUP_HANDLE_SAME_ACCESS)
467 struct handle_entry *entry = get_handle( src, src_handle );
468 if (entry)
469 access = entry->access;
470 else /* pseudo-handle, give it full access */
472 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
473 clear_error();
476 access &= ~RESERVED_ALL;
477 if (options & DUP_HANDLE_MAKE_GLOBAL)
478 res = alloc_global_handle( obj, access );
479 else
480 res = alloc_handle( dst, obj, access, inherit );
481 release_object( obj );
482 return res;
485 /* open a new handle to an existing object */
486 obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
487 const struct object_ops *ops, unsigned int access, int inherit )
489 obj_handle_t handle = 0;
490 struct object *obj = find_object( namespace, name, len );
491 if (obj)
493 if (ops && obj->ops != ops)
494 set_error( STATUS_OBJECT_TYPE_MISMATCH );
495 else
496 handle = alloc_handle( current->process, obj, access, inherit );
497 release_object( obj );
499 else
500 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
501 return handle;
504 /* close a handle */
505 DECL_HANDLER(close_handle)
507 close_handle( current->process, req->handle, &reply->fd );
510 /* set a handle information */
511 DECL_HANDLER(set_handle_info)
513 int fd = req->fd;
515 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
516 reply->old_flags = set_handle_info( current->process, req->handle,
517 req->mask, req->flags, &fd );
518 reply->cur_fd = fd;
521 /* duplicate a handle */
522 DECL_HANDLER(dup_handle)
524 struct process *src, *dst;
526 reply->handle = 0;
527 reply->fd = -1;
528 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
530 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
532 reply->handle = duplicate_handle( src, req->src_handle, NULL,
533 req->access, req->inherit, req->options );
535 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
537 reply->handle = duplicate_handle( src, req->src_handle, dst,
538 req->access, req->inherit, req->options );
539 release_object( dst );
541 /* close the handle no matter what happened */
542 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
544 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
545 else close_handle( src, req->src_handle, NULL );
547 release_object( src );