Finish DirectSoundCapture/DirectSoundCapture8 split.
[wine/dcerpc.git] / server / handle.c
blob347960dd06e969806648f77cea198cd34ca54132
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 <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "windef.h"
32 #include "winbase.h"
34 #include "handle.h"
35 #include "process.h"
36 #include "thread.h"
37 #include "request.h"
39 struct handle_entry
41 struct object *ptr; /* object */
42 unsigned int access; /* access rights */
43 int fd; /* file descriptor (in client process) */
46 struct handle_table
48 struct object obj; /* object header */
49 struct process *process; /* process owning this table */
50 int count; /* number of allocated entries */
51 int last; /* last used entry */
52 int free; /* first entry that may be free */
53 struct handle_entry *entries; /* handle entries */
56 static struct handle_table *global_table;
58 /* reserved handle access rights */
59 #define RESERVED_SHIFT 25
60 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
61 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
62 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
64 #define MIN_HANDLE_ENTRIES 32
67 /* handle to table index conversion */
69 /* handles are a multiple of 4 under NT; handle 0 is not used */
70 inline static obj_handle_t index_to_handle( int index )
72 return (obj_handle_t)((index + 1) << 2);
74 inline static int handle_to_index( obj_handle_t handle )
76 return ((unsigned int)handle >> 2) - 1;
79 /* global handle conversion */
81 #define HANDLE_OBFUSCATOR 0x544a4def
83 inline static int handle_is_global( obj_handle_t handle)
85 return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
87 inline static obj_handle_t handle_local_to_global( obj_handle_t handle )
89 if (!handle) return 0;
90 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
92 inline static obj_handle_t handle_global_to_local( obj_handle_t handle )
94 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
98 static void handle_table_dump( struct object *obj, int verbose );
99 static void handle_table_destroy( struct object *obj );
101 static const struct object_ops handle_table_ops =
103 sizeof(struct handle_table), /* size */
104 handle_table_dump, /* dump */
105 no_add_queue, /* add_queue */
106 NULL, /* remove_queue */
107 NULL, /* signaled */
108 NULL, /* satisfied */
109 no_signal, /* signal */
110 no_get_fd, /* get_fd */
111 handle_table_destroy /* destroy */
114 /* dump a handle table */
115 static void handle_table_dump( struct object *obj, int verbose )
117 int i;
118 struct handle_table *table = (struct handle_table *)obj;
119 struct handle_entry *entry = table->entries;
121 assert( obj->ops == &handle_table_ops );
123 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
124 table->last, table->count, table->process );
125 if (!verbose) return;
126 entry = table->entries;
127 for (i = 0; i <= table->last; i++, entry++)
129 if (!entry->ptr) continue;
130 fprintf( stderr, "%9u: %p %08x ",
131 (unsigned int)index_to_handle(i), entry->ptr, entry->access );
132 entry->ptr->ops->dump( entry->ptr, 0 );
136 /* destroy a handle table */
137 static void handle_table_destroy( struct object *obj )
139 int i;
140 struct handle_table *table = (struct handle_table *)obj;
141 struct handle_entry *entry = table->entries;
143 assert( obj->ops == &handle_table_ops );
145 for (i = 0; i <= table->last; i++, entry++)
147 struct object *obj = entry->ptr;
148 entry->ptr = NULL;
149 if (obj) release_object( obj );
151 free( table->entries );
154 /* allocate a new handle table */
155 struct handle_table *alloc_handle_table( struct process *process, int count )
157 struct handle_table *table;
159 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
160 if (!(table = alloc_object( &handle_table_ops )))
161 return NULL;
162 table->process = process;
163 table->count = count;
164 table->last = -1;
165 table->free = 0;
166 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
167 release_object( table );
168 return NULL;
171 /* grow a handle table */
172 static int grow_handle_table( struct handle_table *table )
174 struct handle_entry *new_entries;
175 int count = table->count;
177 if (count >= INT_MAX / 2) return 0;
178 count *= 2;
179 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
181 set_error( STATUS_NO_MEMORY );
182 return 0;
184 table->entries = new_entries;
185 table->count = count;
186 return 1;
189 /* allocate the first free entry in the handle table */
190 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
192 struct handle_entry *entry = table->entries + table->free;
193 int i;
195 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
196 if (i >= table->count)
198 if (!grow_handle_table( table )) return 0;
199 entry = table->entries + i; /* the entries may have moved */
201 table->last = i;
202 found:
203 table->free = i + 1;
204 entry->ptr = grab_object( obj );
205 entry->access = access;
206 entry->fd = -1;
207 return index_to_handle(i);
210 /* allocate a handle for an object, incrementing its refcount */
211 /* return the handle, or 0 on error */
212 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
214 struct handle_table *table = process->handles;
216 assert( table );
217 assert( !(access & RESERVED_ALL) );
218 if (inherit) access |= RESERVED_INHERIT;
219 return alloc_entry( table, obj, access );
222 /* allocate a global handle for an object, incrementing its refcount */
223 /* return the handle, or 0 on error */
224 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
226 if (!global_table)
228 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
229 return 0;
231 return handle_local_to_global( alloc_entry( global_table, obj, access ));
234 /* return a handle entry, or NULL if the handle is invalid */
235 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
237 struct handle_table *table = process->handles;
238 struct handle_entry *entry;
239 int index;
241 if (handle_is_global(handle))
243 handle = handle_global_to_local(handle);
244 table = global_table;
246 if (!table) goto error;
247 index = handle_to_index( handle );
248 if (index < 0) goto error;
249 if (index > table->last) goto error;
250 entry = table->entries + index;
251 if (!entry->ptr) goto error;
252 return entry;
254 error:
255 set_error( STATUS_INVALID_HANDLE );
256 return NULL;
259 /* attempt to shrink a table */
260 static void shrink_handle_table( struct handle_table *table )
262 struct handle_entry *entry = table->entries + table->last;
263 struct handle_entry *new_entries;
264 int count = table->count;
266 while (table->last >= 0)
268 if (entry->ptr) break;
269 table->last--;
270 entry--;
272 if (table->last >= count / 4) return; /* no need to shrink */
273 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
274 count /= 2;
275 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
276 table->count = count;
277 table->entries = new_entries;
280 /* copy the handle table of the parent process */
281 /* return 1 if OK, 0 on error */
282 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
284 struct handle_table *parent_table = parent->handles;
285 struct handle_table *table;
286 int i;
288 assert( parent_table );
289 assert( parent_table->obj.ops == &handle_table_ops );
291 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
292 return NULL;
294 if ((table->last = parent_table->last) >= 0)
296 struct handle_entry *ptr = table->entries;
297 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
298 for (i = 0; i <= table->last; i++, ptr++)
300 if (!ptr->ptr) continue;
301 ptr->fd = -1;
302 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
303 else ptr->ptr = NULL; /* don't inherit this entry */
306 /* attempt to shrink the table */
307 shrink_handle_table( table );
308 return table;
311 /* close a handle and decrement the refcount of the associated object */
312 /* return 1 if OK, 0 on error */
313 int close_handle( struct process *process, obj_handle_t handle, int *fd )
315 struct handle_table *table;
316 struct handle_entry *entry;
317 struct object *obj;
319 if (!(entry = get_handle( process, handle ))) return 0;
320 if (entry->access & RESERVED_CLOSE_PROTECT)
322 set_error( STATUS_INVALID_HANDLE );
323 return 0;
325 obj = entry->ptr;
326 entry->ptr = NULL;
327 if (fd) *fd = entry->fd;
328 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
329 entry->fd = -1;
330 table = handle_is_global(handle) ? global_table : process->handles;
331 if (entry < table->entries + table->free) table->free = entry - table->entries;
332 if (entry == table->entries + table->last) shrink_handle_table( table );
333 /* hack: windows seems to treat registry handles differently */
334 registry_close_handle( obj, handle );
335 release_object( obj );
336 return 1;
339 /* close all the global handles */
340 void close_global_handles(void)
342 if (global_table)
344 release_object( global_table );
345 global_table = NULL;
349 /* retrieve the object corresponding to one of the magic pseudo-handles */
350 static inline struct object *get_magic_handle( obj_handle_t handle )
352 switch((unsigned long)handle)
354 case 0xfffffffe: /* current thread pseudo-handle */
355 return &current->obj;
356 case 0x7fffffff: /* current process pseudo-handle */
357 case 0xffffffff: /* current process pseudo-handle */
358 return (struct object *)current->process;
359 default:
360 return NULL;
364 /* retrieve the object corresponding to a handle, incrementing its refcount */
365 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
366 unsigned int access, const struct object_ops *ops )
368 struct handle_entry *entry;
369 struct object *obj;
371 if (!(obj = get_magic_handle( handle )))
373 if (!(entry = get_handle( process, handle ))) return NULL;
374 if ((entry->access & access) != access)
376 set_error( STATUS_ACCESS_DENIED );
377 return NULL;
379 obj = entry->ptr;
381 if (ops && (obj->ops != ops))
383 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
384 return NULL;
386 return grab_object( obj );
389 /* retrieve the access rights of a given handle */
390 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
392 struct handle_entry *entry;
394 if (get_magic_handle( handle )) return ~0U; /* magic handles have all access rights */
395 if (!(entry = get_handle( process, handle ))) return 0;
396 return entry->access;
399 /* retrieve the cached fd for a given handle */
400 int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
402 struct handle_entry *entry;
404 if (!(entry = get_handle( process, handle ))) return -1;
405 if ((entry->access & access) != access)
407 set_error( STATUS_ACCESS_DENIED );
408 return -1;
410 return entry->fd;
413 /* remove the cached fd and return it */
414 int flush_cached_fd( struct process *process, obj_handle_t handle )
416 struct handle_entry *entry = get_handle( process, handle );
417 int fd = -1;
419 if (entry)
421 fd = entry->fd;
422 entry->fd = -1;
424 return fd;
427 /* find the first inherited handle of the given type */
428 /* this is needed for window stations and desktops (don't ask...) */
429 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
431 struct handle_table *table = process->handles;
432 struct handle_entry *ptr;
433 int i;
435 if (!table) return 0;
437 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
439 if (!ptr->ptr) continue;
440 if (ptr->ptr->ops != ops) continue;
441 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
443 return 0;
446 /* get/set the handle reserved flags */
447 /* return the old flags (or -1 on error) */
448 int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
450 struct handle_entry *entry;
451 unsigned int old_access;
453 if (get_magic_handle( handle ))
455 /* we can retrieve but not set info for magic handles */
456 if (mask) set_error( STATUS_ACCESS_DENIED );
457 return 0;
459 if (!(entry = get_handle( process, handle ))) return -1;
460 old_access = entry->access;
461 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
462 flags = (flags << RESERVED_SHIFT) & mask;
463 entry->access = (entry->access & ~mask) | flags;
464 /* if no current fd set it, otherwise return current fd */
465 if (entry->fd == -1) entry->fd = *fd;
466 *fd = entry->fd;
467 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
470 /* duplicate a handle */
471 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
472 unsigned int access, int inherit, int options )
474 obj_handle_t res;
475 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
477 if (!obj) return 0;
478 if (options & DUP_HANDLE_SAME_ACCESS)
480 struct handle_entry *entry = get_handle( src, src_handle );
481 if (entry)
482 access = entry->access;
483 else /* pseudo-handle, give it full access */
485 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
486 clear_error();
489 access &= ~RESERVED_ALL;
490 if (options & DUP_HANDLE_MAKE_GLOBAL)
491 res = alloc_global_handle( obj, access );
492 else
493 res = alloc_handle( dst, obj, access, inherit );
494 release_object( obj );
495 return res;
498 /* open a new handle to an existing object */
499 obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
500 const struct object_ops *ops, unsigned int access, int inherit )
502 obj_handle_t handle = 0;
503 struct object *obj = find_object( namespace, name, len );
504 if (obj)
506 if (ops && obj->ops != ops)
507 set_error( STATUS_OBJECT_TYPE_MISMATCH );
508 else
509 handle = alloc_handle( current->process, obj, access, inherit );
510 release_object( obj );
512 else
513 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
514 return handle;
517 /* return the size of the handle table of a given process */
518 unsigned int get_handle_table_count( struct process *process )
520 return process->handles->count;
523 /* close a handle */
524 DECL_HANDLER(close_handle)
526 close_handle( current->process, req->handle, &reply->fd );
529 /* set a handle information */
530 DECL_HANDLER(set_handle_info)
532 int fd = req->fd;
534 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
535 reply->old_flags = set_handle_info( current->process, req->handle,
536 req->mask, req->flags, &fd );
537 reply->cur_fd = fd;
540 /* duplicate a handle */
541 DECL_HANDLER(dup_handle)
543 struct process *src, *dst;
545 reply->handle = 0;
546 reply->fd = -1;
547 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
549 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
551 reply->handle = duplicate_handle( src, req->src_handle, NULL,
552 req->access, req->inherit, req->options );
554 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
556 reply->handle = duplicate_handle( src, req->src_handle, dst,
557 req->access, req->inherit, req->options );
558 release_object( dst );
560 /* close the handle no matter what happened */
561 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
563 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
564 else close_handle( src, req->src_handle, NULL );
566 release_object( src );