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 NULL, /* get_poll_events */
108 NULL, /* poll_event */
109 no_get_fd, /* get_fd */
110 no_flush, /* flush */
111 no_get_file_info, /* get_file_info */
112 NULL, /* queue_async */
113 handle_table_destroy /* destroy */
116 /* dump a handle table */
117 static void handle_table_dump( struct object *obj, int verbose )
120 struct handle_table *table = (struct handle_table *)obj;
121 struct handle_entry *entry = table->entries;
123 assert( obj->ops == &handle_table_ops );
125 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
126 table->last, table->count, table->process );
127 if (!verbose) return;
128 entry = table->entries;
129 for (i = 0; i <= table->last; i++, entry++)
131 if (!entry->ptr) continue;
132 fprintf( stderr, "%9u: %p %08x ",
133 (unsigned int)index_to_handle(i), entry->ptr, entry->access );
134 entry->ptr->ops->dump( entry->ptr, 0 );
138 /* destroy a handle table */
139 static void handle_table_destroy( struct object *obj )
142 struct handle_table *table = (struct handle_table *)obj;
143 struct handle_entry *entry = table->entries;
145 assert( obj->ops == &handle_table_ops );
147 for (i = 0; i <= table->last; i++, entry++)
149 struct object *obj = entry->ptr;
151 if (obj) release_object( obj );
153 free( table->entries );
156 /* allocate a new handle table */
157 struct handle_table *alloc_handle_table( struct process *process, int count )
159 struct handle_table *table;
161 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
162 if (!(table = alloc_object( &handle_table_ops, -1 )))
164 table->process = process;
165 table->count = count;
168 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
169 release_object( table );
173 /* grow a handle table */
174 static int grow_handle_table( struct handle_table *table )
176 struct handle_entry *new_entries;
177 int count = table->count;
179 if (count >= INT_MAX / 2) return 0;
181 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
183 set_error( STATUS_NO_MEMORY );
186 table->entries = new_entries;
187 table->count = count;
191 /* allocate the first free entry in the handle table */
192 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
194 struct handle_entry *entry = table->entries + table->free;
197 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
198 if (i >= table->count)
200 if (!grow_handle_table( table )) return 0;
201 entry = table->entries + i; /* the entries may have moved */
206 entry->ptr = grab_object( obj );
207 entry->access = access;
209 return index_to_handle(i);
212 /* allocate a handle for an object, incrementing its refcount */
213 /* return the handle, or 0 on error */
214 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
216 struct handle_table *table = process->handles;
219 assert( !(access & RESERVED_ALL) );
220 if (inherit) access |= RESERVED_INHERIT;
221 return alloc_entry( table, obj, access );
224 /* allocate a global handle for an object, incrementing its refcount */
225 /* return the handle, or 0 on error */
226 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
230 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
233 return handle_local_to_global( alloc_entry( global_table, obj, access ));
236 /* return a handle entry, or NULL if the handle is invalid */
237 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
239 struct handle_table *table = process->handles;
240 struct handle_entry *entry;
243 if (handle_is_global(handle))
245 handle = handle_global_to_local(handle);
246 table = global_table;
248 if (!table) goto error;
249 index = handle_to_index( handle );
250 if (index < 0) goto error;
251 if (index > table->last) goto error;
252 entry = table->entries + index;
253 if (!entry->ptr) goto error;
257 set_error( STATUS_INVALID_HANDLE );
261 /* attempt to shrink a table */
262 static void shrink_handle_table( struct handle_table *table )
264 struct handle_entry *entry = table->entries + table->last;
265 struct handle_entry *new_entries;
266 int count = table->count;
268 while (table->last >= 0)
270 if (entry->ptr) break;
274 if (table->last >= count / 4) return; /* no need to shrink */
275 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
277 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
278 table->count = count;
279 table->entries = new_entries;
282 /* copy the handle table of the parent process */
283 /* return 1 if OK, 0 on error */
284 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
286 struct handle_table *parent_table = parent->handles;
287 struct handle_table *table;
290 assert( parent_table );
291 assert( parent_table->obj.ops == &handle_table_ops );
293 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
296 if ((table->last = parent_table->last) >= 0)
298 struct handle_entry *ptr = table->entries;
299 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
300 for (i = 0; i <= table->last; i++, ptr++)
302 if (!ptr->ptr) continue;
304 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
305 else ptr->ptr = NULL; /* don't inherit this entry */
308 /* attempt to shrink the table */
309 shrink_handle_table( table );
313 /* close a handle and decrement the refcount of the associated object */
314 /* return 1 if OK, 0 on error */
315 int close_handle( struct process *process, obj_handle_t handle, int *fd )
317 struct handle_table *table;
318 struct handle_entry *entry;
321 if (!(entry = get_handle( process, handle ))) return 0;
322 if (entry->access & RESERVED_CLOSE_PROTECT)
324 set_error( STATUS_INVALID_HANDLE );
329 if (fd) *fd = entry->fd;
330 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
332 table = handle_is_global(handle) ? global_table : process->handles;
333 if (entry < table->entries + table->free) table->free = entry - table->entries;
334 if (entry == table->entries + table->last) shrink_handle_table( table );
335 /* hack: windows seems to treat registry handles differently */
336 registry_close_handle( obj, handle );
337 release_object( obj );
341 /* close all the global handles */
342 void close_global_handles(void)
346 release_object( global_table );
351 /* retrieve the object corresponding to one of the magic pseudo-handles */
352 static inline struct object *get_magic_handle( obj_handle_t handle )
354 switch((unsigned long)handle)
356 case 0xfffffffe: /* current thread pseudo-handle */
357 return ¤t->obj;
358 case 0x7fffffff: /* current process pseudo-handle */
359 case 0xffffffff: /* current process pseudo-handle */
360 return (struct object *)current->process;
366 /* retrieve the object corresponding to a handle, incrementing its refcount */
367 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
368 unsigned int access, const struct object_ops *ops )
370 struct handle_entry *entry;
373 if (!(obj = get_magic_handle( handle )))
375 if (!(entry = get_handle( process, handle ))) return NULL;
376 if ((entry->access & access) != access)
378 set_error( STATUS_ACCESS_DENIED );
383 if (ops && (obj->ops != ops))
385 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
388 return grab_object( obj );
391 /* retrieve the cached fd for a given handle */
392 int get_handle_fd( struct process *process, obj_handle_t handle, unsigned int access )
394 struct handle_entry *entry;
396 if (!(entry = get_handle( process, handle ))) return -1;
397 if ((entry->access & access) != access)
399 set_error( STATUS_ACCESS_DENIED );
405 /* find the first inherited handle of the given type */
406 /* this is needed for window stations and desktops (don't ask...) */
407 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
409 struct handle_table *table = process->handles;
410 struct handle_entry *ptr;
413 if (!table) return 0;
415 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
417 if (!ptr->ptr) continue;
418 if (ptr->ptr->ops != ops) continue;
419 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
424 /* get/set the handle reserved flags */
425 /* return the old flags (or -1 on error) */
426 int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
428 struct handle_entry *entry;
429 unsigned int old_access;
431 if (get_magic_handle( handle ))
433 /* we can retrieve but not set info for magic handles */
434 if (mask) set_error( STATUS_ACCESS_DENIED );
437 if (!(entry = get_handle( process, handle ))) return -1;
438 old_access = entry->access;
439 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
440 flags = (flags << RESERVED_SHIFT) & mask;
441 entry->access = (entry->access & ~mask) | flags;
442 /* if no current fd set it, otherwise return current fd */
443 if (entry->fd == -1) entry->fd = *fd;
445 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
448 /* duplicate a handle */
449 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
450 unsigned int access, int inherit, int options )
453 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
456 if (options & DUP_HANDLE_SAME_ACCESS)
458 struct handle_entry *entry = get_handle( src, src_handle );
460 access = entry->access;
461 else /* pseudo-handle, give it full access */
463 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
467 access &= ~RESERVED_ALL;
468 if (options & DUP_HANDLE_MAKE_GLOBAL)
469 res = alloc_global_handle( obj, access );
471 res = alloc_handle( dst, obj, access, inherit );
472 release_object( obj );
476 /* open a new handle to an existing object */
477 obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
478 const struct object_ops *ops, unsigned int access, int inherit )
480 obj_handle_t handle = 0;
481 struct object *obj = find_object( namespace, name, len );
484 if (ops && obj->ops != ops)
485 set_error( STATUS_OBJECT_TYPE_MISMATCH );
487 handle = alloc_handle( current->process, obj, access, inherit );
488 release_object( obj );
491 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
496 DECL_HANDLER(close_handle)
498 close_handle( current->process, req->handle, &reply->fd );
501 /* set a handle information */
502 DECL_HANDLER(set_handle_info)
506 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
507 reply->old_flags = set_handle_info( current->process, req->handle,
508 req->mask, req->flags, &fd );
512 /* duplicate a handle */
513 DECL_HANDLER(dup_handle)
515 struct process *src, *dst;
519 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
521 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
523 reply->handle = duplicate_handle( src, req->src_handle, NULL,
524 req->access, req->inherit, req->options );
526 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
528 reply->handle = duplicate_handle( src, req->src_handle, dst,
529 req->access, req->inherit, req->options );
530 release_object( dst );
532 /* close the handle no matter what happened */
533 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
535 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
536 else close_handle( src, req->src_handle, NULL );
538 release_object( src );