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"
40 struct object *ptr; /* object */
41 unsigned int access; /* access rights */
42 int fd; /* file descriptor (in client process) */
47 struct object obj; /* object header */
48 struct process *process; /* process owning this table */
49 int count; /* number of allocated entries */
50 int last; /* last used entry */
51 int free; /* first entry that may be free */
52 struct handle_entry *entries; /* handle entries */
55 static struct handle_table *global_table;
57 /* reserved handle access rights */
58 #define RESERVED_SHIFT 26
59 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
60 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
61 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
63 #define MIN_HANDLE_ENTRIES 32
66 /* handle to table index conversion */
68 /* handles are a multiple of 4 under NT; handle 0 is not used */
69 inline static obj_handle_t index_to_handle( int index )
71 return (obj_handle_t)((index + 1) << 2);
73 inline static int handle_to_index( obj_handle_t handle )
75 return ((unsigned int)handle >> 2) - 1;
78 /* global handle conversion */
80 #define HANDLE_OBFUSCATOR 0x544a4def
82 inline static int handle_is_global( obj_handle_t handle)
84 return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
86 inline static obj_handle_t handle_local_to_global( obj_handle_t handle )
88 if (!handle) return 0;
89 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
91 inline static obj_handle_t handle_global_to_local( obj_handle_t handle )
93 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
97 static void handle_table_dump( struct object *obj, int verbose );
98 static void handle_table_destroy( struct object *obj );
100 static const struct object_ops handle_table_ops =
102 sizeof(struct handle_table), /* size */
103 handle_table_dump, /* dump */
104 no_add_queue, /* add_queue */
105 NULL, /* remove_queue */
107 NULL, /* satisfied */
108 no_signal, /* signal */
109 no_get_fd, /* get_fd */
110 no_close_handle, /* close_handle */
111 handle_table_destroy /* destroy */
114 /* dump a handle table */
115 static void handle_table_dump( struct object *obj, int verbose )
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 )
140 struct handle_table *table = (struct handle_table *)obj;
141 struct handle_entry *entry;
143 assert( obj->ops == &handle_table_ops );
145 /* first notify all objects that handles are being closed */
148 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
150 struct object *obj = entry->ptr;
151 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
155 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
157 struct object *obj = entry->ptr;
159 if (obj) release_object( obj );
161 free( table->entries );
164 /* allocate a new handle table */
165 struct handle_table *alloc_handle_table( struct process *process, int count )
167 struct handle_table *table;
169 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
170 if (!(table = alloc_object( &handle_table_ops )))
172 table->process = process;
173 table->count = count;
176 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
177 release_object( table );
181 /* grow a handle table */
182 static int grow_handle_table( struct handle_table *table )
184 struct handle_entry *new_entries;
185 int count = table->count;
187 if (count >= INT_MAX / 2) return 0;
189 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
191 set_error( STATUS_NO_MEMORY );
194 table->entries = new_entries;
195 table->count = count;
199 /* allocate the first free entry in the handle table */
200 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
202 struct handle_entry *entry = table->entries + table->free;
205 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
206 if (i >= table->count)
208 if (!grow_handle_table( table )) return 0;
209 entry = table->entries + i; /* the entries may have moved */
214 entry->ptr = grab_object( obj );
215 entry->access = access;
217 return index_to_handle(i);
220 /* allocate a handle for an object, incrementing its refcount */
221 /* return the handle, or 0 on error */
222 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
224 struct handle_table *table = process->handles;
227 assert( !(access & RESERVED_ALL) );
228 if (inherit) access |= RESERVED_INHERIT;
229 return alloc_entry( table, obj, access );
232 /* allocate a global handle for an object, incrementing its refcount */
233 /* return the handle, or 0 on error */
234 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
238 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
241 return handle_local_to_global( alloc_entry( global_table, obj, access ));
244 /* return a handle entry, or NULL if the handle is invalid */
245 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
247 struct handle_table *table = process->handles;
248 struct handle_entry *entry;
251 if (handle_is_global(handle))
253 handle = handle_global_to_local(handle);
254 table = global_table;
256 if (!table) goto error;
257 index = handle_to_index( handle );
258 if (index < 0) goto error;
259 if (index > table->last) goto error;
260 entry = table->entries + index;
261 if (!entry->ptr) goto error;
265 set_error( STATUS_INVALID_HANDLE );
269 /* attempt to shrink a table */
270 static void shrink_handle_table( struct handle_table *table )
272 struct handle_entry *entry = table->entries + table->last;
273 struct handle_entry *new_entries;
274 int count = table->count;
276 while (table->last >= 0)
278 if (entry->ptr) break;
282 if (table->last >= count / 4) return; /* no need to shrink */
283 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
285 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
286 table->count = count;
287 table->entries = new_entries;
290 /* copy the handle table of the parent process */
291 /* return 1 if OK, 0 on error */
292 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
294 struct handle_table *parent_table = parent->handles;
295 struct handle_table *table;
298 assert( parent_table );
299 assert( parent_table->obj.ops == &handle_table_ops );
301 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
304 if ((table->last = parent_table->last) >= 0)
306 struct handle_entry *ptr = table->entries;
307 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
308 for (i = 0; i <= table->last; i++, ptr++)
310 if (!ptr->ptr) continue;
312 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
313 else ptr->ptr = NULL; /* don't inherit this entry */
316 /* attempt to shrink the table */
317 shrink_handle_table( table );
321 /* close a handle and decrement the refcount of the associated object */
322 /* return 1 if OK, 0 on error */
323 int close_handle( struct process *process, obj_handle_t handle, int *fd )
325 struct handle_table *table;
326 struct handle_entry *entry;
329 if (!(entry = get_handle( process, handle ))) return 0;
330 if (entry->access & RESERVED_CLOSE_PROTECT)
332 set_error( STATUS_INVALID_HANDLE );
336 if (!obj->ops->close_handle( obj, process, handle ))
338 set_error( STATUS_INVALID_HANDLE );
342 if (fd) *fd = entry->fd;
343 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
345 table = handle_is_global(handle) ? global_table : process->handles;
346 if (entry < table->entries + table->free) table->free = entry - table->entries;
347 if (entry == table->entries + table->last) shrink_handle_table( table );
348 release_object( obj );
352 /* close all the global handles */
353 void close_global_handles(void)
357 release_object( global_table );
362 /* retrieve the object corresponding to one of the magic pseudo-handles */
363 static inline struct object *get_magic_handle( obj_handle_t handle )
365 switch((unsigned long)handle)
367 case 0xfffffffe: /* current thread pseudo-handle */
368 return ¤t->obj;
369 case 0x7fffffff: /* current process pseudo-handle */
370 case 0xffffffff: /* current process pseudo-handle */
371 return (struct object *)current->process;
377 /* retrieve the object corresponding to a handle, incrementing its refcount */
378 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
379 unsigned int access, const struct object_ops *ops )
381 struct handle_entry *entry;
384 if (!(obj = get_magic_handle( handle )))
386 if (!(entry = get_handle( process, handle ))) return NULL;
387 if ((entry->access & access) != access)
389 set_error( STATUS_ACCESS_DENIED );
394 if (ops && (obj->ops != ops))
396 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
399 return grab_object( obj );
402 /* retrieve the access rights of a given handle */
403 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
405 struct handle_entry *entry;
407 if (get_magic_handle( handle )) return ~0U; /* magic handles have all access rights */
408 if (!(entry = get_handle( process, handle ))) return 0;
409 return entry->access;
412 /* retrieve the cached fd for a given handle */
413 int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
415 struct handle_entry *entry;
417 if (!(entry = get_handle( process, handle ))) return -1;
418 if ((entry->access & access) != access)
420 set_error( STATUS_ACCESS_DENIED );
426 /* remove the cached fd and return it */
427 int flush_cached_fd( struct process *process, obj_handle_t handle )
429 struct handle_entry *entry = get_handle( process, handle );
440 /* find the first inherited handle of the given type */
441 /* this is needed for window stations and desktops (don't ask...) */
442 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
444 struct handle_table *table = process->handles;
445 struct handle_entry *ptr;
448 if (!table) return 0;
450 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
452 if (!ptr->ptr) continue;
453 if (ptr->ptr->ops != ops) continue;
454 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
459 /* get/set the handle reserved flags */
460 /* return the old flags (or -1 on error) */
461 int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
463 struct handle_entry *entry;
464 unsigned int old_access;
466 if (get_magic_handle( handle ))
468 /* we can retrieve but not set info for magic handles */
469 if (mask) set_error( STATUS_ACCESS_DENIED );
472 if (!(entry = get_handle( process, handle ))) return -1;
473 old_access = entry->access;
474 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
475 flags = (flags << RESERVED_SHIFT) & mask;
476 entry->access = (entry->access & ~mask) | flags;
477 /* if no current fd set it, otherwise return current fd */
478 if (entry->fd == -1) entry->fd = *fd;
480 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
483 /* duplicate a handle */
484 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
485 unsigned int access, int inherit, int options )
488 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
491 if (options & DUP_HANDLE_SAME_ACCESS)
493 struct handle_entry *entry = get_handle( src, src_handle );
495 access = entry->access;
496 else /* pseudo-handle, give it full access */
498 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
502 access &= ~RESERVED_ALL;
503 if (options & DUP_HANDLE_MAKE_GLOBAL)
504 res = alloc_global_handle( obj, access );
506 res = alloc_handle( dst, obj, access, inherit );
507 release_object( obj );
511 /* open a new handle to an existing object */
512 obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
513 const struct object_ops *ops, unsigned int access, int inherit )
515 obj_handle_t handle = 0;
516 struct object *obj = find_object( namespace, name, len );
519 if (ops && obj->ops != ops)
520 set_error( STATUS_OBJECT_TYPE_MISMATCH );
522 handle = alloc_handle( current->process, obj, access, inherit );
523 release_object( obj );
526 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
530 /* return the size of the handle table of a given process */
531 unsigned int get_handle_table_count( struct process *process )
533 return process->handles->count;
537 DECL_HANDLER(close_handle)
539 close_handle( current->process, req->handle, &reply->fd );
542 /* set a handle information */
543 DECL_HANDLER(set_handle_info)
547 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
548 reply->old_flags = set_handle_info( current->process, req->handle,
549 req->mask, req->flags, &fd );
553 /* duplicate a handle */
554 DECL_HANDLER(dup_handle)
556 struct process *src, *dst;
560 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
562 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
564 reply->handle = duplicate_handle( src, req->src_handle, NULL,
565 req->access, req->inherit, req->options );
567 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
569 reply->handle = duplicate_handle( src, req->src_handle, dst,
570 req->access, req->inherit, req->options );
571 release_object( dst );
573 /* close the handle no matter what happened */
574 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
576 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
577 else close_handle( src, req->src_handle, NULL );
579 release_object( src );