2 * Server-side handle management
4 * Copyright (C) 1998 Alexandre Julliard
22 struct object *ptr; /* object */
23 unsigned int access; /* access rights */
24 int fd; /* file descriptor (in client process) */
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 #define MIN_HANDLE_ENTRIES 32
48 /* handle to table index conversion */
50 /* handles are a multiple of 4 under NT; handle 0 is not used */
51 inline static handle_t index_to_handle( int index )
53 return (handle_t)((index + 1) << 2);
55 inline static int handle_to_index( handle_t handle )
57 return ((unsigned int)handle >> 2) - 1;
60 /* global handle conversion */
62 #define HANDLE_OBFUSCATOR 0x544a4def
64 inline static int handle_is_global( handle_t handle)
66 return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
68 inline static handle_t handle_local_to_global( handle_t handle )
70 if (!handle) return 0;
71 return (handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
73 inline static handle_t handle_global_to_local( handle_t handle )
75 return (handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
79 static void handle_table_dump( struct object *obj, int verbose );
80 static void handle_table_destroy( struct object *obj );
82 static const struct object_ops handle_table_ops =
84 sizeof(struct handle_table), /* size */
85 handle_table_dump, /* dump */
86 no_add_queue, /* add_queue */
87 NULL, /* remove_queue */
90 NULL, /* get_poll_events */
91 NULL, /* poll_event */
92 no_get_fd, /* get_fd */
94 no_get_file_info, /* get_file_info */
95 handle_table_destroy /* destroy */
98 /* dump a handle table */
99 static void handle_table_dump( struct object *obj, int verbose )
102 struct handle_table *table = (struct handle_table *)obj;
103 struct handle_entry *entry = table->entries;
105 assert( obj->ops == &handle_table_ops );
107 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
108 table->last, table->count, table->process );
109 if (!verbose) return;
110 entry = table->entries;
111 for (i = 0; i <= table->last; i++, entry++)
113 if (!entry->ptr) continue;
114 fprintf( stderr, "%9u: %p %08x ",
115 (unsigned int)index_to_handle(i), entry->ptr, entry->access );
116 entry->ptr->ops->dump( entry->ptr, 0 );
120 /* destroy a handle table */
121 static void handle_table_destroy( struct object *obj )
124 struct handle_table *table = (struct handle_table *)obj;
125 struct handle_entry *entry = table->entries;
127 assert( obj->ops == &handle_table_ops );
129 for (i = 0; i <= table->last; i++, entry++)
131 struct object *obj = entry->ptr;
133 if (obj) release_object( obj );
135 free( table->entries );
138 /* allocate a new handle table */
139 struct object *alloc_handle_table( struct process *process, int count )
141 struct handle_table *table;
143 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
144 if (!(table = alloc_object( &handle_table_ops, -1 )))
146 table->process = process;
147 table->count = count;
150 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
151 release_object( table );
155 /* grow a handle table */
156 static int grow_handle_table( struct handle_table *table )
158 struct handle_entry *new_entries;
159 int count = table->count;
161 if (count >= INT_MAX / 2) return 0;
163 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
165 set_error( STATUS_NO_MEMORY );
168 table->entries = new_entries;
169 table->count = count;
173 /* allocate the first free entry in the handle table */
174 static handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
176 struct handle_entry *entry = table->entries + table->free;
179 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
180 if (i >= table->count)
182 if (!grow_handle_table( table )) return 0;
183 entry = table->entries + i; /* the entries may have moved */
188 entry->ptr = grab_object( obj );
189 entry->access = access;
191 return index_to_handle(i);
194 /* allocate a handle for an object, incrementing its refcount */
195 /* return the handle, or 0 on error */
196 handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
198 struct handle_table *table = (struct handle_table *)process->handles;
201 assert( !(access & RESERVED_ALL) );
202 if (inherit) access |= RESERVED_INHERIT;
203 return alloc_entry( table, obj, access );
206 /* allocate a global handle for an object, incrementing its refcount */
207 /* return the handle, or 0 on error */
208 static handle_t alloc_global_handle( void *obj, unsigned int access )
212 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
215 return handle_local_to_global( alloc_entry( global_table, obj, access ));
218 /* return a handle entry, or NULL if the handle is invalid */
219 static struct handle_entry *get_handle( struct process *process, handle_t handle )
221 struct handle_table *table = (struct handle_table *)process->handles;
222 struct handle_entry *entry;
225 if (handle_is_global(handle))
227 handle = handle_global_to_local(handle);
228 table = global_table;
230 if (!table) goto error;
231 index = handle_to_index( handle );
232 if (index < 0) goto error;
233 if (index > table->last) goto error;
234 entry = table->entries + index;
235 if (!entry->ptr) goto error;
239 set_error( STATUS_INVALID_HANDLE );
243 /* attempt to shrink a table */
244 static void shrink_handle_table( struct handle_table *table )
246 struct handle_entry *entry = table->entries + table->last;
247 struct handle_entry *new_entries;
248 int count = table->count;
250 while (table->last >= 0)
252 if (entry->ptr) break;
256 if (table->last >= count / 4) return; /* no need to shrink */
257 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
259 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
260 table->count = count;
261 table->entries = new_entries;
264 /* copy the handle table of the parent process */
265 /* return 1 if OK, 0 on error */
266 struct object *copy_handle_table( struct process *process, struct process *parent )
268 struct handle_table *parent_table = (struct handle_table *)parent->handles;
269 struct handle_table *table;
272 assert( parent_table );
273 assert( parent_table->obj.ops == &handle_table_ops );
275 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
278 if ((table->last = parent_table->last) >= 0)
280 struct handle_entry *ptr = table->entries;
281 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
282 for (i = 0; i <= table->last; i++, ptr++)
284 if (!ptr->ptr) continue;
286 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
287 else ptr->ptr = NULL; /* don't inherit this entry */
290 /* attempt to shrink the table */
291 shrink_handle_table( table );
295 /* close a handle and decrement the refcount of the associated object */
296 /* return 1 if OK, 0 on error */
297 int close_handle( struct process *process, handle_t handle, int *fd )
299 struct handle_table *table;
300 struct handle_entry *entry;
303 if (!(entry = get_handle( process, handle ))) return 0;
304 if (entry->access & RESERVED_CLOSE_PROTECT)
306 set_error( STATUS_INVALID_HANDLE );
311 if (fd) *fd = entry->fd;
312 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
314 table = handle_is_global(handle) ? global_table : (struct handle_table *)process->handles;
315 if (entry < table->entries + table->free) table->free = entry - table->entries;
316 if (entry == table->entries + table->last) shrink_handle_table( table );
317 release_object( obj );
321 /* close all the global handles */
322 void close_global_handles(void)
326 release_object( global_table );
331 /* retrieve the object corresponding to one of the magic pseudo-handles */
332 static inline struct object *get_magic_handle( handle_t handle )
334 switch((unsigned long)handle)
336 case 0xfffffffe: /* current thread pseudo-handle */
337 return ¤t->obj;
338 case 0x7fffffff: /* current process pseudo-handle */
339 case 0xffffffff: /* current process pseudo-handle */
340 return (struct object *)current->process;
346 /* retrieve the object corresponding to a handle, incrementing its refcount */
347 struct object *get_handle_obj( struct process *process, handle_t handle,
348 unsigned int access, const struct object_ops *ops )
350 struct handle_entry *entry;
353 if (!(obj = get_magic_handle( handle )))
355 if (!(entry = get_handle( process, handle ))) return NULL;
356 if ((entry->access & access) != access)
358 set_error( STATUS_ACCESS_DENIED );
363 if (ops && (obj->ops != ops))
365 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
368 return grab_object( obj );
371 /* retrieve the cached fd for a given handle */
372 int get_handle_fd( struct process *process, handle_t handle, unsigned int access )
374 struct handle_entry *entry;
376 if (!(entry = get_handle( process, handle ))) return -1;
377 if ((entry->access & access) != access)
379 set_error( STATUS_ACCESS_DENIED );
385 /* get/set the handle reserved flags */
386 /* return the old flags (or -1 on error) */
387 static int set_handle_info( struct process *process, handle_t handle,
388 int mask, int flags, int *fd )
390 struct handle_entry *entry;
391 unsigned int old_access;
393 if (get_magic_handle( handle ))
395 /* we can retrieve but not set info for magic handles */
396 if (mask) set_error( STATUS_ACCESS_DENIED );
399 if (!(entry = get_handle( process, handle ))) return -1;
400 old_access = entry->access;
401 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
402 flags = (flags << RESERVED_SHIFT) & mask;
403 entry->access = (entry->access & ~mask) | flags;
404 /* if no current fd set it, otherwise return current fd */
405 if (entry->fd == -1) entry->fd = *fd;
407 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
410 /* duplicate a handle */
411 handle_t duplicate_handle( struct process *src, handle_t src_handle, struct process *dst,
412 unsigned int access, int inherit, int options )
415 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
418 if (options & DUP_HANDLE_SAME_ACCESS)
420 struct handle_entry *entry = get_handle( src, src_handle );
422 access = entry->access;
423 else /* pseudo-handle, give it full access */
425 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
429 access &= ~RESERVED_ALL;
430 if (options & DUP_HANDLE_MAKE_GLOBAL)
431 res = alloc_global_handle( obj, access );
433 res = alloc_handle( dst, obj, access, inherit );
434 release_object( obj );
438 /* open a new handle to an existing object */
439 handle_t open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
440 unsigned int access, int inherit )
443 struct object *obj = find_object( name, len );
446 if (ops && obj->ops != ops)
447 set_error( STATUS_OBJECT_TYPE_MISMATCH );
449 handle = alloc_handle( current->process, obj, access, inherit );
450 release_object( obj );
453 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
458 DECL_HANDLER(close_handle)
460 close_handle( current->process, req->handle, &req->fd );
463 /* set a handle information */
464 DECL_HANDLER(set_handle_info)
468 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
469 req->old_flags = set_handle_info( current->process, req->handle, req->mask, req->flags, &fd );
473 /* duplicate a handle */
474 DECL_HANDLER(dup_handle)
476 struct process *src, *dst;
480 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
482 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
484 req->handle = duplicate_handle( src, req->src_handle, NULL,
485 req->access, req->inherit, req->options );
487 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
489 req->handle = duplicate_handle( src, req->src_handle, dst,
490 req->access, req->inherit, req->options );
491 release_object( dst );
493 /* close the handle no matter what happened */
494 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
496 if (src == current->process) close_handle( src, req->src_handle, &req->fd );
497 else close_handle( src, req->src_handle, NULL );
499 release_object( src );