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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #define WIN32_NO_STATUS
43 struct object
*ptr
; /* object */
44 unsigned int access
; /* access rights */
49 struct object obj
; /* object header */
50 struct process
*process
; /* process owning this table */
51 int count
; /* number of allocated entries */
52 int last
; /* last used entry */
53 int free
; /* first entry that may be free */
54 struct handle_entry
*entries
; /* handle entries */
57 static struct handle_table
*global_table
;
59 /* reserved handle access rights */
60 #define RESERVED_SHIFT 26
61 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
62 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
63 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
65 #define MIN_HANDLE_ENTRIES 32
68 /* handle to table index conversion */
70 /* handles are a multiple of 4 under NT; handle 0 is not used */
71 inline static obj_handle_t
index_to_handle( int index
)
73 return (obj_handle_t
)((unsigned long)(index
+ 1) << 2);
75 inline static int handle_to_index( obj_handle_t handle
)
77 return ((unsigned long)handle
>> 2) - 1;
80 /* global handle conversion */
82 #define HANDLE_OBFUSCATOR 0x544a4def
84 inline static int handle_is_global( obj_handle_t handle
)
86 return ((unsigned long)handle
^ HANDLE_OBFUSCATOR
) < 0x10000;
88 inline static obj_handle_t
handle_local_to_global( obj_handle_t handle
)
90 if (!handle
) return 0;
91 return (obj_handle_t
)((unsigned long)handle
^ HANDLE_OBFUSCATOR
);
93 inline static obj_handle_t
handle_global_to_local( obj_handle_t handle
)
95 return (obj_handle_t
)((unsigned long)handle
^ HANDLE_OBFUSCATOR
);
99 static void handle_table_dump( struct object
*obj
, int verbose
);
100 static void handle_table_destroy( struct object
*obj
);
102 static const struct object_ops handle_table_ops
=
104 sizeof(struct handle_table
), /* size */
105 handle_table_dump
, /* dump */
106 no_add_queue
, /* add_queue */
107 NULL
, /* remove_queue */
109 NULL
, /* satisfied */
110 no_signal
, /* signal */
111 no_get_fd
, /* get_fd */
112 no_map_access
, /* map_access */
113 no_lookup_name
, /* lookup_name */
114 no_close_handle
, /* close_handle */
115 handle_table_destroy
/* destroy */
118 /* dump a handle table */
119 static void handle_table_dump( struct object
*obj
, int verbose
)
122 struct handle_table
*table
= (struct handle_table
*)obj
;
123 struct handle_entry
*entry
= table
->entries
;
125 assert( obj
->ops
== &handle_table_ops
);
127 fprintf( stderr
, "Handle table last=%d count=%d process=%p\n",
128 table
->last
, table
->count
, table
->process
);
129 if (!verbose
) return;
130 entry
= table
->entries
;
131 for (i
= 0; i
<= table
->last
; i
++, entry
++)
133 if (!entry
->ptr
) continue;
134 fprintf( stderr
, " %p: %p %08x ",
135 index_to_handle(i
), entry
->ptr
, entry
->access
);
136 entry
->ptr
->ops
->dump( entry
->ptr
, 0 );
140 /* destroy a handle table */
141 static void handle_table_destroy( struct object
*obj
)
144 struct handle_table
*table
= (struct handle_table
*)obj
;
145 struct handle_entry
*entry
;
147 assert( obj
->ops
== &handle_table_ops
);
149 /* first notify all objects that handles are being closed */
152 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
154 struct object
*obj
= entry
->ptr
;
155 if (obj
) obj
->ops
->close_handle( obj
, table
->process
, index_to_handle(i
) );
159 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
161 struct object
*obj
= entry
->ptr
;
163 if (obj
) release_object( obj
);
165 free( table
->entries
);
168 /* allocate a new handle table */
169 struct handle_table
*alloc_handle_table( struct process
*process
, int count
)
171 struct handle_table
*table
;
173 if (count
< MIN_HANDLE_ENTRIES
) count
= MIN_HANDLE_ENTRIES
;
174 if (!(table
= alloc_object( &handle_table_ops
)))
176 table
->process
= process
;
177 table
->count
= count
;
180 if ((table
->entries
= mem_alloc( count
* sizeof(*table
->entries
) ))) return table
;
181 release_object( table
);
185 /* grow a handle table */
186 static int grow_handle_table( struct handle_table
*table
)
188 struct handle_entry
*new_entries
;
189 int count
= table
->count
;
191 if (count
>= INT_MAX
/ 2) return 0;
193 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(struct handle_entry
) )))
195 set_error( STATUS_NO_MEMORY
);
198 table
->entries
= new_entries
;
199 table
->count
= count
;
203 /* allocate the first free entry in the handle table */
204 static obj_handle_t
alloc_entry( struct handle_table
*table
, void *obj
, unsigned int access
)
206 struct handle_entry
*entry
= table
->entries
+ table
->free
;
209 for (i
= table
->free
; i
<= table
->last
; i
++, entry
++) if (!entry
->ptr
) goto found
;
210 if (i
>= table
->count
)
212 if (!grow_handle_table( table
)) return 0;
213 entry
= table
->entries
+ i
; /* the entries may have moved */
218 entry
->ptr
= grab_object( obj
);
219 entry
->access
= access
;
220 return index_to_handle(i
);
223 /* allocate a handle for an object, incrementing its refcount */
224 /* return the handle, or 0 on error */
225 obj_handle_t
alloc_handle( struct process
*process
, void *ptr
, unsigned int access
, unsigned int attr
)
227 struct object
*obj
= ptr
;
229 access
= obj
->ops
->map_access( obj
, access
);
230 access
&= ~RESERVED_ALL
;
231 if (attr
& OBJ_INHERIT
) access
|= RESERVED_INHERIT
;
232 if (!process
->handles
)
234 set_error( STATUS_NO_MEMORY
);
237 return alloc_entry( process
->handles
, obj
, access
);
240 /* allocate a global handle for an object, incrementing its refcount */
241 /* return the handle, or 0 on error */
242 static obj_handle_t
alloc_global_handle( void *obj
, unsigned int access
)
246 if (!(global_table
= (struct handle_table
*)alloc_handle_table( NULL
, 0 )))
248 make_object_static( &global_table
->obj
);
250 return handle_local_to_global( alloc_entry( global_table
, obj
, access
));
253 /* return a handle entry, or NULL if the handle is invalid */
254 static struct handle_entry
*get_handle( struct process
*process
, obj_handle_t handle
)
256 struct handle_table
*table
= process
->handles
;
257 struct handle_entry
*entry
;
260 if (handle_is_global(handle
))
262 handle
= handle_global_to_local(handle
);
263 table
= global_table
;
265 if (!table
) goto error
;
266 index
= handle_to_index( handle
);
267 if (index
< 0) goto error
;
268 if (index
> table
->last
) goto error
;
269 entry
= table
->entries
+ index
;
270 if (!entry
->ptr
) goto error
;
274 set_error( STATUS_INVALID_HANDLE
);
278 /* attempt to shrink a table */
279 static void shrink_handle_table( struct handle_table
*table
)
281 struct handle_entry
*entry
= table
->entries
+ table
->last
;
282 struct handle_entry
*new_entries
;
283 int count
= table
->count
;
285 while (table
->last
>= 0)
287 if (entry
->ptr
) break;
291 if (table
->last
>= count
/ 4) return; /* no need to shrink */
292 if (count
< MIN_HANDLE_ENTRIES
* 2) return; /* too small to shrink */
294 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(*new_entries
) ))) return;
295 table
->count
= count
;
296 table
->entries
= new_entries
;
299 /* copy the handle table of the parent process */
300 /* return 1 if OK, 0 on error */
301 struct handle_table
*copy_handle_table( struct process
*process
, struct process
*parent
)
303 struct handle_table
*parent_table
= parent
->handles
;
304 struct handle_table
*table
;
307 assert( parent_table
);
308 assert( parent_table
->obj
.ops
== &handle_table_ops
);
310 if (!(table
= (struct handle_table
*)alloc_handle_table( process
, parent_table
->count
)))
313 if ((table
->last
= parent_table
->last
) >= 0)
315 struct handle_entry
*ptr
= table
->entries
;
316 memcpy( ptr
, parent_table
->entries
, (table
->last
+ 1) * sizeof(struct handle_entry
) );
317 for (i
= 0; i
<= table
->last
; i
++, ptr
++)
319 if (!ptr
->ptr
) continue;
320 if (ptr
->access
& RESERVED_INHERIT
) grab_object( ptr
->ptr
);
321 else ptr
->ptr
= NULL
; /* don't inherit this entry */
324 /* attempt to shrink the table */
325 shrink_handle_table( table
);
329 /* close a handle and decrement the refcount of the associated object */
330 /* return 1 if OK, 0 on error */
331 int close_handle( struct process
*process
, obj_handle_t handle
)
333 struct handle_table
*table
;
334 struct handle_entry
*entry
;
337 if (!(entry
= get_handle( process
, handle
))) return 0;
338 if (entry
->access
& RESERVED_CLOSE_PROTECT
)
340 set_error( STATUS_HANDLE_NOT_CLOSABLE
);
344 if (!obj
->ops
->close_handle( obj
, process
, handle
))
346 set_error( STATUS_HANDLE_NOT_CLOSABLE
);
350 table
= handle_is_global(handle
) ? global_table
: process
->handles
;
351 if (entry
< table
->entries
+ table
->free
) table
->free
= entry
- table
->entries
;
352 if (entry
== table
->entries
+ table
->last
) shrink_handle_table( table
);
353 release_object( obj
);
357 /* retrieve the object corresponding to one of the magic pseudo-handles */
358 static inline struct object
*get_magic_handle( obj_handle_t handle
)
360 switch((unsigned long)handle
)
362 case 0xfffffffe: /* current thread pseudo-handle */
363 return ¤t
->obj
;
364 case 0x7fffffff: /* current process pseudo-handle */
365 case 0xffffffff: /* current process pseudo-handle */
366 return (struct object
*)current
->process
;
372 /* retrieve the object corresponding to a handle, incrementing its refcount */
373 struct object
*get_handle_obj( struct process
*process
, obj_handle_t handle
,
374 unsigned int access
, const struct object_ops
*ops
)
376 struct handle_entry
*entry
;
379 if (!(obj
= get_magic_handle( handle
)))
381 if (!(entry
= get_handle( process
, handle
))) return NULL
;
382 if ((entry
->access
& access
) != access
)
384 set_error( STATUS_ACCESS_DENIED
);
389 if (ops
&& (obj
->ops
!= ops
))
391 set_error( STATUS_OBJECT_TYPE_MISMATCH
); /* not the right type */
394 return grab_object( obj
);
397 /* retrieve the access rights of a given handle */
398 unsigned int get_handle_access( struct process
*process
, obj_handle_t handle
)
400 struct handle_entry
*entry
;
402 if (get_magic_handle( handle
)) return ~RESERVED_ALL
; /* magic handles have all access rights */
403 if (!(entry
= get_handle( process
, handle
))) return 0;
404 return entry
->access
& ~RESERVED_ALL
;
407 /* find the first inherited handle of the given type */
408 /* this is needed for window stations and desktops (don't ask...) */
409 obj_handle_t
find_inherited_handle( struct process
*process
, const struct object_ops
*ops
)
411 struct handle_table
*table
= process
->handles
;
412 struct handle_entry
*ptr
;
415 if (!table
) return 0;
417 for (i
= 0, ptr
= table
->entries
; i
<= table
->last
; i
++, ptr
++)
419 if (!ptr
->ptr
) continue;
420 if (ptr
->ptr
->ops
!= ops
) continue;
421 if (ptr
->access
& RESERVED_INHERIT
) return index_to_handle(i
);
426 /* get/set the handle reserved flags */
427 /* return the old flags (or -1 on error) */
428 static int set_handle_flags( struct process
*process
, obj_handle_t handle
, int mask
, int flags
)
430 struct handle_entry
*entry
;
431 unsigned int old_access
;
433 if (get_magic_handle( handle
))
435 /* we can retrieve but not set info for magic handles */
436 if (mask
) set_error( STATUS_ACCESS_DENIED
);
439 if (!(entry
= get_handle( process
, handle
))) return -1;
440 old_access
= entry
->access
;
441 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
442 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
443 entry
->access
= (entry
->access
& ~mask
) | flags
;
444 return (old_access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
447 /* duplicate a handle */
448 obj_handle_t
duplicate_handle( struct process
*src
, obj_handle_t src_handle
, struct process
*dst
,
449 unsigned int access
, unsigned int attr
, unsigned int options
)
452 struct object
*obj
= get_handle_obj( src
, src_handle
, 0, NULL
);
455 if (options
& DUP_HANDLE_SAME_ACCESS
)
457 struct handle_entry
*entry
= get_handle( src
, src_handle
);
459 access
= entry
->access
;
460 else /* pseudo-handle, give it full access */
462 access
= STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
;
466 access
&= ~RESERVED_ALL
;
467 if (options
& DUP_HANDLE_MAKE_GLOBAL
)
468 res
= alloc_global_handle( obj
, access
);
470 res
= alloc_handle( dst
, obj
, access
, attr
);
471 release_object( obj
);
475 /* open a new handle to an existing object */
476 obj_handle_t
open_object( const struct namespace *namespace, const struct unicode_str
*name
,
477 const struct object_ops
*ops
, unsigned int access
, unsigned int attr
)
479 obj_handle_t handle
= 0;
480 struct object
*obj
= find_object( namespace, name
, attr
);
483 if (ops
&& obj
->ops
!= ops
)
484 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
486 handle
= alloc_handle( current
->process
, obj
, access
, attr
);
487 release_object( obj
);
490 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
494 /* return the size of the handle table of a given process */
495 unsigned int get_handle_table_count( struct process
*process
)
497 if (!process
->handles
) return 0;
498 return process
->handles
->count
;
502 DECL_HANDLER(close_handle
)
504 close_handle( current
->process
, req
->handle
);
507 /* set a handle information */
508 DECL_HANDLER(set_handle_info
)
510 reply
->old_flags
= set_handle_flags( current
->process
, req
->handle
, req
->mask
, req
->flags
);
513 /* duplicate a handle */
514 DECL_HANDLER(dup_handle
)
516 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
->attributes
, 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
->attributes
, req
->options
);
530 release_object( dst
);
532 /* close the handle no matter what happened */
533 if (req
->options
& DUP_HANDLE_CLOSE_SOURCE
)
535 unsigned int err
= get_error(); /* don't overwrite error from the above calls */
536 reply
->closed
= close_handle( src
, req
->src_handle
);
539 reply
->self
= (src
== current
->process
);
540 release_object( src
);
544 DECL_HANDLER(get_object_info
)
548 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
550 reply
->access
= get_handle_access( current
->process
, req
->handle
);
551 reply
->ref_count
= obj
->refcount
;
552 release_object( obj
);