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
22 #include "wine/port.h"
39 struct object *ptr; /* object */
40 unsigned int access; /* access rights */
41 int fd; /* file descriptor (in client process) */
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 */
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 )
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 )
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;
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 )))
159 table->process = process;
160 table->count = count;
163 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
164 release_object( table );
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;
176 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
178 set_error( STATUS_NO_MEMORY );
181 table->entries = new_entries;
182 table->count = count;
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;
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 */
201 entry->ptr = grab_object( obj );
202 entry->access = access;
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;
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 )
225 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 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;
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;
252 set_error( STATUS_INVALID_HANDLE );
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;
269 if (table->last >= count / 4) return; /* no need to shrink */
270 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
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;
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 )))
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;
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 );
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;
316 if (!(entry = get_handle( process, handle ))) return 0;
317 if (entry->access & RESERVED_CLOSE_PROTECT)
319 set_error( STATUS_INVALID_HANDLE );
324 if (fd) *fd = entry->fd;
325 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
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 );
336 /* close all the global handles */
337 void close_global_handles(void)
341 release_object( global_table );
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 ¤t->obj;
353 case 0x7fffffff: /* current process pseudo-handle */
354 case 0xffffffff: /* current process pseudo-handle */
355 return (struct object *)current->process;
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;
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 );
378 if (ops && (obj->ops != ops))
380 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
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 );
400 /* find the first inherited handle of the given type */
401 /* this is needed for window stations and desktops (don't ask...) */
402 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
404 struct handle_table *table = process->handles;
405 struct handle_entry *ptr;
408 if (!table) return 0;
410 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
412 if (!ptr->ptr) continue;
413 if (ptr->ptr->ops != ops) continue;
414 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
419 /* get/set the handle reserved flags */
420 /* return the old flags (or -1 on error) */
421 int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
423 struct handle_entry *entry;
424 unsigned int old_access;
426 if (get_magic_handle( handle ))
428 /* we can retrieve but not set info for magic handles */
429 if (mask) set_error( STATUS_ACCESS_DENIED );
432 if (!(entry = get_handle( process, handle ))) return -1;
433 old_access = entry->access;
434 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
435 flags = (flags << RESERVED_SHIFT) & mask;
436 entry->access = (entry->access & ~mask) | flags;
437 /* if no current fd set it, otherwise return current fd */
438 if (entry->fd == -1) entry->fd = *fd;
440 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
443 /* duplicate a handle */
444 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
445 unsigned int access, int inherit, int options )
448 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
451 if (options & DUP_HANDLE_SAME_ACCESS)
453 struct handle_entry *entry = get_handle( src, src_handle );
455 access = entry->access;
456 else /* pseudo-handle, give it full access */
458 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
462 access &= ~RESERVED_ALL;
463 if (options & DUP_HANDLE_MAKE_GLOBAL)
464 res = alloc_global_handle( obj, access );
466 res = alloc_handle( dst, obj, access, inherit );
467 release_object( obj );
471 /* open a new handle to an existing object */
472 obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
473 const struct object_ops *ops, unsigned int access, int inherit )
475 obj_handle_t handle = 0;
476 struct object *obj = find_object( namespace, name, len );
479 if (ops && obj->ops != ops)
480 set_error( STATUS_OBJECT_TYPE_MISMATCH );
482 handle = alloc_handle( current->process, obj, access, inherit );
483 release_object( obj );
486 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
491 DECL_HANDLER(close_handle)
493 close_handle( current->process, req->handle, &reply->fd );
496 /* set a handle information */
497 DECL_HANDLER(set_handle_info)
501 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
502 reply->old_flags = set_handle_info( current->process, req->handle,
503 req->mask, req->flags, &fd );
507 /* duplicate a handle */
508 DECL_HANDLER(dup_handle)
510 struct process *src, *dst;
514 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
516 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
518 reply->handle = duplicate_handle( src, req->src_handle, NULL,
519 req->access, req->inherit, req->options );
521 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
523 reply->handle = duplicate_handle( src, req->src_handle, dst,
524 req->access, req->inherit, req->options );
525 release_object( dst );
527 /* close the handle no matter what happened */
528 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
530 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
531 else close_handle( src, req->src_handle, NULL );
533 release_object( src );