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 NULL
, /* queue_async */
96 handle_table_destroy
/* destroy */
99 /* dump a handle table */
100 static void handle_table_dump( struct object
*obj
, int verbose
)
103 struct handle_table
*table
= (struct handle_table
*)obj
;
104 struct handle_entry
*entry
= table
->entries
;
106 assert( obj
->ops
== &handle_table_ops
);
108 fprintf( stderr
, "Handle table last=%d count=%d process=%p\n",
109 table
->last
, table
->count
, table
->process
);
110 if (!verbose
) return;
111 entry
= table
->entries
;
112 for (i
= 0; i
<= table
->last
; i
++, entry
++)
114 if (!entry
->ptr
) continue;
115 fprintf( stderr
, "%9u: %p %08x ",
116 (unsigned int)index_to_handle(i
), entry
->ptr
, entry
->access
);
117 entry
->ptr
->ops
->dump( entry
->ptr
, 0 );
121 /* destroy a handle table */
122 static void handle_table_destroy( struct object
*obj
)
125 struct handle_table
*table
= (struct handle_table
*)obj
;
126 struct handle_entry
*entry
= table
->entries
;
128 assert( obj
->ops
== &handle_table_ops
);
130 for (i
= 0; i
<= table
->last
; i
++, entry
++)
132 struct object
*obj
= entry
->ptr
;
134 if (obj
) release_object( obj
);
136 free( table
->entries
);
139 /* allocate a new handle table */
140 struct object
*alloc_handle_table( struct process
*process
, int count
)
142 struct handle_table
*table
;
144 if (count
< MIN_HANDLE_ENTRIES
) count
= MIN_HANDLE_ENTRIES
;
145 if (!(table
= alloc_object( &handle_table_ops
, -1 )))
147 table
->process
= process
;
148 table
->count
= count
;
151 if ((table
->entries
= mem_alloc( count
* sizeof(*table
->entries
) ))) return &table
->obj
;
152 release_object( table
);
156 /* grow a handle table */
157 static int grow_handle_table( struct handle_table
*table
)
159 struct handle_entry
*new_entries
;
160 int count
= table
->count
;
162 if (count
>= INT_MAX
/ 2) return 0;
164 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(struct handle_entry
) )))
166 set_error( STATUS_NO_MEMORY
);
169 table
->entries
= new_entries
;
170 table
->count
= count
;
174 /* allocate the first free entry in the handle table */
175 static handle_t
alloc_entry( struct handle_table
*table
, void *obj
, unsigned int access
)
177 struct handle_entry
*entry
= table
->entries
+ table
->free
;
180 for (i
= table
->free
; i
<= table
->last
; i
++, entry
++) if (!entry
->ptr
) goto found
;
181 if (i
>= table
->count
)
183 if (!grow_handle_table( table
)) return 0;
184 entry
= table
->entries
+ i
; /* the entries may have moved */
189 entry
->ptr
= grab_object( obj
);
190 entry
->access
= access
;
192 return index_to_handle(i
);
195 /* allocate a handle for an object, incrementing its refcount */
196 /* return the handle, or 0 on error */
197 handle_t
alloc_handle( struct process
*process
, void *obj
, unsigned int access
, int inherit
)
199 struct handle_table
*table
= (struct handle_table
*)process
->handles
;
202 assert( !(access
& RESERVED_ALL
) );
203 if (inherit
) access
|= RESERVED_INHERIT
;
204 return alloc_entry( table
, obj
, access
);
207 /* allocate a global handle for an object, incrementing its refcount */
208 /* return the handle, or 0 on error */
209 static handle_t
alloc_global_handle( void *obj
, unsigned int access
)
213 if (!(global_table
= (struct handle_table
*)alloc_handle_table( NULL
, 0 )))
216 return handle_local_to_global( alloc_entry( global_table
, obj
, access
));
219 /* return a handle entry, or NULL if the handle is invalid */
220 static struct handle_entry
*get_handle( struct process
*process
, handle_t handle
)
222 struct handle_table
*table
= (struct handle_table
*)process
->handles
;
223 struct handle_entry
*entry
;
226 if (handle_is_global(handle
))
228 handle
= handle_global_to_local(handle
);
229 table
= global_table
;
231 if (!table
) goto error
;
232 index
= handle_to_index( handle
);
233 if (index
< 0) goto error
;
234 if (index
> table
->last
) goto error
;
235 entry
= table
->entries
+ index
;
236 if (!entry
->ptr
) goto error
;
240 set_error( STATUS_INVALID_HANDLE
);
244 /* attempt to shrink a table */
245 static void shrink_handle_table( struct handle_table
*table
)
247 struct handle_entry
*entry
= table
->entries
+ table
->last
;
248 struct handle_entry
*new_entries
;
249 int count
= table
->count
;
251 while (table
->last
>= 0)
253 if (entry
->ptr
) break;
257 if (table
->last
>= count
/ 4) return; /* no need to shrink */
258 if (count
< MIN_HANDLE_ENTRIES
* 2) return; /* too small to shrink */
260 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(*new_entries
) ))) return;
261 table
->count
= count
;
262 table
->entries
= new_entries
;
265 /* copy the handle table of the parent process */
266 /* return 1 if OK, 0 on error */
267 struct object
*copy_handle_table( struct process
*process
, struct process
*parent
)
269 struct handle_table
*parent_table
= (struct handle_table
*)parent
->handles
;
270 struct handle_table
*table
;
273 assert( parent_table
);
274 assert( parent_table
->obj
.ops
== &handle_table_ops
);
276 if (!(table
= (struct handle_table
*)alloc_handle_table( process
, parent_table
->count
)))
279 if ((table
->last
= parent_table
->last
) >= 0)
281 struct handle_entry
*ptr
= table
->entries
;
282 memcpy( ptr
, parent_table
->entries
, (table
->last
+ 1) * sizeof(struct handle_entry
) );
283 for (i
= 0; i
<= table
->last
; i
++, ptr
++)
285 if (!ptr
->ptr
) continue;
287 if (ptr
->access
& RESERVED_INHERIT
) grab_object( ptr
->ptr
);
288 else ptr
->ptr
= NULL
; /* don't inherit this entry */
291 /* attempt to shrink the table */
292 shrink_handle_table( table
);
296 /* close a handle and decrement the refcount of the associated object */
297 /* return 1 if OK, 0 on error */
298 int close_handle( struct process
*process
, handle_t handle
, int *fd
)
300 struct handle_table
*table
;
301 struct handle_entry
*entry
;
304 if (!(entry
= get_handle( process
, handle
))) return 0;
305 if (entry
->access
& RESERVED_CLOSE_PROTECT
)
307 set_error( STATUS_INVALID_HANDLE
);
312 if (fd
) *fd
= entry
->fd
;
313 else if (entry
->fd
!= -1) return 1; /* silently ignore close attempt if we cannot close the fd */
315 table
= handle_is_global(handle
) ? global_table
: (struct handle_table
*)process
->handles
;
316 if (entry
< table
->entries
+ table
->free
) table
->free
= entry
- table
->entries
;
317 if (entry
== table
->entries
+ table
->last
) shrink_handle_table( table
);
318 release_object( obj
);
322 /* close all the global handles */
323 void close_global_handles(void)
327 release_object( global_table
);
332 /* retrieve the object corresponding to one of the magic pseudo-handles */
333 static inline struct object
*get_magic_handle( handle_t handle
)
335 switch((unsigned long)handle
)
337 case 0xfffffffe: /* current thread pseudo-handle */
338 return ¤t
->obj
;
339 case 0x7fffffff: /* current process pseudo-handle */
340 case 0xffffffff: /* current process pseudo-handle */
341 return (struct object
*)current
->process
;
347 /* retrieve the object corresponding to a handle, incrementing its refcount */
348 struct object
*get_handle_obj( struct process
*process
, handle_t handle
,
349 unsigned int access
, const struct object_ops
*ops
)
351 struct handle_entry
*entry
;
354 if (!(obj
= get_magic_handle( handle
)))
356 if (!(entry
= get_handle( process
, handle
))) return NULL
;
357 if ((entry
->access
& access
) != access
)
359 set_error( STATUS_ACCESS_DENIED
);
364 if (ops
&& (obj
->ops
!= ops
))
366 set_error( STATUS_OBJECT_TYPE_MISMATCH
); /* not the right type */
369 return grab_object( obj
);
372 /* retrieve the cached fd for a given handle */
373 int get_handle_fd( struct process
*process
, handle_t handle
, unsigned int access
)
375 struct handle_entry
*entry
;
377 if (!(entry
= get_handle( process
, handle
))) return -1;
378 if ((entry
->access
& access
) != access
)
380 set_error( STATUS_ACCESS_DENIED
);
386 /* get/set the handle reserved flags */
387 /* return the old flags (or -1 on error) */
388 static int set_handle_info( struct process
*process
, handle_t handle
,
389 int mask
, int flags
, int *fd
)
391 struct handle_entry
*entry
;
392 unsigned int old_access
;
394 if (get_magic_handle( handle
))
396 /* we can retrieve but not set info for magic handles */
397 if (mask
) set_error( STATUS_ACCESS_DENIED
);
400 if (!(entry
= get_handle( process
, handle
))) return -1;
401 old_access
= entry
->access
;
402 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
403 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
404 entry
->access
= (entry
->access
& ~mask
) | flags
;
405 /* if no current fd set it, otherwise return current fd */
406 if (entry
->fd
== -1) entry
->fd
= *fd
;
408 return (old_access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
411 /* duplicate a handle */
412 handle_t
duplicate_handle( struct process
*src
, handle_t src_handle
, struct process
*dst
,
413 unsigned int access
, int inherit
, int options
)
416 struct object
*obj
= get_handle_obj( src
, src_handle
, 0, NULL
);
419 if (options
& DUP_HANDLE_SAME_ACCESS
)
421 struct handle_entry
*entry
= get_handle( src
, src_handle
);
423 access
= entry
->access
;
424 else /* pseudo-handle, give it full access */
426 access
= STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
;
430 access
&= ~RESERVED_ALL
;
431 if (options
& DUP_HANDLE_MAKE_GLOBAL
)
432 res
= alloc_global_handle( obj
, access
);
434 res
= alloc_handle( dst
, obj
, access
, inherit
);
435 release_object( obj
);
439 /* open a new handle to an existing object */
440 handle_t
open_object( const WCHAR
*name
, size_t len
, const struct object_ops
*ops
,
441 unsigned int access
, int inherit
)
444 struct object
*obj
= find_object( name
, len
);
447 if (ops
&& obj
->ops
!= ops
)
448 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
450 handle
= alloc_handle( current
->process
, obj
, access
, inherit
);
451 release_object( obj
);
454 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
459 DECL_HANDLER(close_handle
)
461 close_handle( current
->process
, req
->handle
, &reply
->fd
);
464 /* set a handle information */
465 DECL_HANDLER(set_handle_info
)
469 if (handle_is_global(req
->handle
)) fd
= -1; /* no fd cache for global handles */
470 reply
->old_flags
= set_handle_info( current
->process
, req
->handle
,
471 req
->mask
, req
->flags
, &fd
);
475 /* duplicate a handle */
476 DECL_HANDLER(dup_handle
)
478 struct process
*src
, *dst
;
482 if ((src
= get_process_from_handle( req
->src_process
, PROCESS_DUP_HANDLE
)))
484 if (req
->options
& DUP_HANDLE_MAKE_GLOBAL
)
486 reply
->handle
= duplicate_handle( src
, req
->src_handle
, NULL
,
487 req
->access
, req
->inherit
, req
->options
);
489 else if ((dst
= get_process_from_handle( req
->dst_process
, PROCESS_DUP_HANDLE
)))
491 reply
->handle
= duplicate_handle( src
, req
->src_handle
, dst
,
492 req
->access
, req
->inherit
, req
->options
);
493 release_object( dst
);
495 /* close the handle no matter what happened */
496 if (req
->options
& DUP_HANDLE_CLOSE_SOURCE
)
498 if (src
== current
->process
) close_handle( src
, req
->src_handle
, &reply
->fd
);
499 else close_handle( src
, req
->src_handle
, NULL
);
501 release_object( src
);