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"
41 struct object
*ptr
; /* object */
42 unsigned int access
; /* access rights */
43 int fd
; /* file descriptor (in client process) */
48 struct object obj
; /* object header */
49 struct process
*process
; /* process owning this table */
50 int count
; /* number of allocated entries */
51 int last
; /* last used entry */
52 int free
; /* first entry that may be free */
53 struct handle_entry
*entries
; /* handle entries */
56 static struct handle_table
*global_table
;
58 /* reserved handle access rights */
59 #define RESERVED_SHIFT 26
60 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
61 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
62 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
64 #define MIN_HANDLE_ENTRIES 32
67 /* handle to table index conversion */
69 /* handles are a multiple of 4 under NT; handle 0 is not used */
70 inline static obj_handle_t
index_to_handle( int index
)
72 return (obj_handle_t
)((index
+ 1) << 2);
74 inline static int handle_to_index( obj_handle_t handle
)
76 return ((unsigned int)handle
>> 2) - 1;
79 /* global handle conversion */
81 #define HANDLE_OBFUSCATOR 0x544a4def
83 inline static int handle_is_global( obj_handle_t handle
)
85 return ((unsigned long)handle
^ HANDLE_OBFUSCATOR
) < 0x10000;
87 inline static obj_handle_t
handle_local_to_global( obj_handle_t handle
)
89 if (!handle
) return 0;
90 return (obj_handle_t
)((unsigned long)handle
^ HANDLE_OBFUSCATOR
);
92 inline static obj_handle_t
handle_global_to_local( obj_handle_t handle
)
94 return (obj_handle_t
)((unsigned long)handle
^ HANDLE_OBFUSCATOR
);
98 static void handle_table_dump( struct object
*obj
, int verbose
);
99 static void handle_table_destroy( struct object
*obj
);
101 static const struct object_ops handle_table_ops
=
103 sizeof(struct handle_table
), /* size */
104 handle_table_dump
, /* dump */
105 no_add_queue
, /* add_queue */
106 NULL
, /* remove_queue */
108 NULL
, /* satisfied */
109 no_signal
, /* signal */
110 no_get_fd
, /* get_fd */
111 no_close_handle
, /* close_handle */
112 handle_table_destroy
/* destroy */
115 /* dump a handle table */
116 static void handle_table_dump( struct object
*obj
, int verbose
)
119 struct handle_table
*table
= (struct handle_table
*)obj
;
120 struct handle_entry
*entry
= table
->entries
;
122 assert( obj
->ops
== &handle_table_ops
);
124 fprintf( stderr
, "Handle table last=%d count=%d process=%p\n",
125 table
->last
, table
->count
, table
->process
);
126 if (!verbose
) return;
127 entry
= table
->entries
;
128 for (i
= 0; i
<= table
->last
; i
++, entry
++)
130 if (!entry
->ptr
) continue;
131 fprintf( stderr
, "%9u: %p %08x ",
132 (unsigned int)index_to_handle(i
), entry
->ptr
, entry
->access
);
133 entry
->ptr
->ops
->dump( entry
->ptr
, 0 );
137 /* destroy a handle table */
138 static void handle_table_destroy( struct object
*obj
)
141 struct handle_table
*table
= (struct handle_table
*)obj
;
142 struct handle_entry
*entry
;
144 assert( obj
->ops
== &handle_table_ops
);
146 /* first notify all objects that handles are being closed */
149 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
151 struct object
*obj
= entry
->ptr
;
152 if (obj
) obj
->ops
->close_handle( obj
, table
->process
, index_to_handle(i
) );
156 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
158 struct object
*obj
= entry
->ptr
;
160 if (obj
) release_object( obj
);
162 free( table
->entries
);
165 /* allocate a new handle table */
166 struct handle_table
*alloc_handle_table( struct process
*process
, int count
)
168 struct handle_table
*table
;
170 if (count
< MIN_HANDLE_ENTRIES
) count
= MIN_HANDLE_ENTRIES
;
171 if (!(table
= alloc_object( &handle_table_ops
)))
173 table
->process
= process
;
174 table
->count
= count
;
177 if ((table
->entries
= mem_alloc( count
* sizeof(*table
->entries
) ))) return table
;
178 release_object( table
);
182 /* grow a handle table */
183 static int grow_handle_table( struct handle_table
*table
)
185 struct handle_entry
*new_entries
;
186 int count
= table
->count
;
188 if (count
>= INT_MAX
/ 2) return 0;
190 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(struct handle_entry
) )))
192 set_error( STATUS_NO_MEMORY
);
195 table
->entries
= new_entries
;
196 table
->count
= count
;
200 /* allocate the first free entry in the handle table */
201 static obj_handle_t
alloc_entry( struct handle_table
*table
, void *obj
, unsigned int access
)
203 struct handle_entry
*entry
= table
->entries
+ table
->free
;
206 for (i
= table
->free
; i
<= table
->last
; i
++, entry
++) if (!entry
->ptr
) goto found
;
207 if (i
>= table
->count
)
209 if (!grow_handle_table( table
)) return 0;
210 entry
= table
->entries
+ i
; /* the entries may have moved */
215 entry
->ptr
= grab_object( obj
);
216 entry
->access
= access
;
218 return index_to_handle(i
);
221 /* allocate a handle for an object, incrementing its refcount */
222 /* return the handle, or 0 on error */
223 obj_handle_t
alloc_handle( struct process
*process
, void *obj
, unsigned int access
, int inherit
)
225 struct handle_table
*table
= process
->handles
;
228 assert( !(access
& RESERVED_ALL
) );
229 if (inherit
) access
|= RESERVED_INHERIT
;
230 return alloc_entry( table
, obj
, access
);
233 /* allocate a global handle for an object, incrementing its refcount */
234 /* return the handle, or 0 on error */
235 static obj_handle_t
alloc_global_handle( void *obj
, unsigned int access
)
239 if (!(global_table
= (struct handle_table
*)alloc_handle_table( NULL
, 0 )))
242 return handle_local_to_global( alloc_entry( global_table
, obj
, access
));
245 /* return a handle entry, or NULL if the handle is invalid */
246 static struct handle_entry
*get_handle( struct process
*process
, obj_handle_t handle
)
248 struct handle_table
*table
= process
->handles
;
249 struct handle_entry
*entry
;
252 if (handle_is_global(handle
))
254 handle
= handle_global_to_local(handle
);
255 table
= global_table
;
257 if (!table
) goto error
;
258 index
= handle_to_index( handle
);
259 if (index
< 0) goto error
;
260 if (index
> table
->last
) goto error
;
261 entry
= table
->entries
+ index
;
262 if (!entry
->ptr
) goto error
;
266 set_error( STATUS_INVALID_HANDLE
);
270 /* attempt to shrink a table */
271 static void shrink_handle_table( struct handle_table
*table
)
273 struct handle_entry
*entry
= table
->entries
+ table
->last
;
274 struct handle_entry
*new_entries
;
275 int count
= table
->count
;
277 while (table
->last
>= 0)
279 if (entry
->ptr
) break;
283 if (table
->last
>= count
/ 4) return; /* no need to shrink */
284 if (count
< MIN_HANDLE_ENTRIES
* 2) return; /* too small to shrink */
286 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(*new_entries
) ))) return;
287 table
->count
= count
;
288 table
->entries
= new_entries
;
291 /* copy the handle table of the parent process */
292 /* return 1 if OK, 0 on error */
293 struct handle_table
*copy_handle_table( struct process
*process
, struct process
*parent
)
295 struct handle_table
*parent_table
= parent
->handles
;
296 struct handle_table
*table
;
299 assert( parent_table
);
300 assert( parent_table
->obj
.ops
== &handle_table_ops
);
302 if (!(table
= (struct handle_table
*)alloc_handle_table( process
, parent_table
->count
)))
305 if ((table
->last
= parent_table
->last
) >= 0)
307 struct handle_entry
*ptr
= table
->entries
;
308 memcpy( ptr
, parent_table
->entries
, (table
->last
+ 1) * sizeof(struct handle_entry
) );
309 for (i
= 0; i
<= table
->last
; i
++, ptr
++)
311 if (!ptr
->ptr
) continue;
313 if (ptr
->access
& RESERVED_INHERIT
) grab_object( ptr
->ptr
);
314 else ptr
->ptr
= NULL
; /* don't inherit this entry */
317 /* attempt to shrink the table */
318 shrink_handle_table( table
);
322 /* close a handle and decrement the refcount of the associated object */
323 /* return 1 if OK, 0 on error */
324 int close_handle( struct process
*process
, obj_handle_t handle
, int *fd
)
326 struct handle_table
*table
;
327 struct handle_entry
*entry
;
330 if (!(entry
= get_handle( process
, handle
))) return 0;
331 if (entry
->access
& RESERVED_CLOSE_PROTECT
)
333 set_error( STATUS_INVALID_HANDLE
);
337 if (!obj
->ops
->close_handle( obj
, process
, handle
))
339 set_error( STATUS_INVALID_HANDLE
);
343 if (fd
) *fd
= entry
->fd
;
344 else if (entry
->fd
!= -1) return 1; /* silently ignore close attempt if we cannot close the fd */
346 table
= handle_is_global(handle
) ? global_table
: process
->handles
;
347 if (entry
< table
->entries
+ table
->free
) table
->free
= entry
- table
->entries
;
348 if (entry
== table
->entries
+ table
->last
) shrink_handle_table( table
);
349 release_object( obj
);
353 /* close all the global handles */
354 void close_global_handles(void)
358 release_object( global_table
);
363 /* retrieve the object corresponding to one of the magic pseudo-handles */
364 static inline struct object
*get_magic_handle( obj_handle_t handle
)
366 switch((unsigned long)handle
)
368 case 0xfffffffe: /* current thread pseudo-handle */
369 return ¤t
->obj
;
370 case 0x7fffffff: /* current process pseudo-handle */
371 case 0xffffffff: /* current process pseudo-handle */
372 return (struct object
*)current
->process
;
378 /* retrieve the object corresponding to a handle, incrementing its refcount */
379 struct object
*get_handle_obj( struct process
*process
, obj_handle_t handle
,
380 unsigned int access
, const struct object_ops
*ops
)
382 struct handle_entry
*entry
;
385 if (!(obj
= get_magic_handle( handle
)))
387 if (!(entry
= get_handle( process
, handle
))) return NULL
;
388 if ((entry
->access
& access
) != access
)
390 set_error( STATUS_ACCESS_DENIED
);
395 if (ops
&& (obj
->ops
!= ops
))
397 set_error( STATUS_OBJECT_TYPE_MISMATCH
); /* not the right type */
400 return grab_object( obj
);
403 /* retrieve the access rights of a given handle */
404 unsigned int get_handle_access( struct process
*process
, obj_handle_t handle
)
406 struct handle_entry
*entry
;
408 if (get_magic_handle( handle
)) return ~0U; /* magic handles have all access rights */
409 if (!(entry
= get_handle( process
, handle
))) return 0;
410 return entry
->access
;
413 /* retrieve the cached fd for a given handle */
414 int get_handle_unix_fd( struct process
*process
, obj_handle_t handle
, unsigned int access
)
416 struct handle_entry
*entry
;
418 if (!(entry
= get_handle( process
, handle
))) return -1;
419 if ((entry
->access
& access
) != access
)
421 set_error( STATUS_ACCESS_DENIED
);
427 /* remove the cached fd and return it */
428 int flush_cached_fd( struct process
*process
, obj_handle_t handle
)
430 struct handle_entry
*entry
= get_handle( process
, handle
);
441 /* find the first inherited handle of the given type */
442 /* this is needed for window stations and desktops (don't ask...) */
443 obj_handle_t
find_inherited_handle( struct process
*process
, const struct object_ops
*ops
)
445 struct handle_table
*table
= process
->handles
;
446 struct handle_entry
*ptr
;
449 if (!table
) return 0;
451 for (i
= 0, ptr
= table
->entries
; i
<= table
->last
; i
++, ptr
++)
453 if (!ptr
->ptr
) continue;
454 if (ptr
->ptr
->ops
!= ops
) continue;
455 if (ptr
->access
& RESERVED_INHERIT
) return index_to_handle(i
);
460 /* get/set the handle reserved flags */
461 /* return the old flags (or -1 on error) */
462 int set_handle_info( struct process
*process
, obj_handle_t handle
, int mask
, int flags
, int *fd
)
464 struct handle_entry
*entry
;
465 unsigned int old_access
;
467 if (get_magic_handle( handle
))
469 /* we can retrieve but not set info for magic handles */
470 if (mask
) set_error( STATUS_ACCESS_DENIED
);
473 if (!(entry
= get_handle( process
, handle
))) return -1;
474 old_access
= entry
->access
;
475 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
476 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
477 entry
->access
= (entry
->access
& ~mask
) | flags
;
478 /* if no current fd set it, otherwise return current fd */
479 if (entry
->fd
== -1) entry
->fd
= *fd
;
481 return (old_access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
484 /* duplicate a handle */
485 obj_handle_t
duplicate_handle( struct process
*src
, obj_handle_t src_handle
, struct process
*dst
,
486 unsigned int access
, int inherit
, int options
)
489 struct object
*obj
= get_handle_obj( src
, src_handle
, 0, NULL
);
492 if (options
& DUP_HANDLE_SAME_ACCESS
)
494 struct handle_entry
*entry
= get_handle( src
, src_handle
);
496 access
= entry
->access
;
497 else /* pseudo-handle, give it full access */
499 access
= STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
;
503 access
&= ~RESERVED_ALL
;
504 if (options
& DUP_HANDLE_MAKE_GLOBAL
)
505 res
= alloc_global_handle( obj
, access
);
507 res
= alloc_handle( dst
, obj
, access
, inherit
);
508 release_object( obj
);
512 /* open a new handle to an existing object */
513 obj_handle_t
open_object( const struct namespace *namespace, const WCHAR
*name
, size_t len
,
514 const struct object_ops
*ops
, unsigned int access
, int inherit
)
516 obj_handle_t handle
= 0;
517 struct object
*obj
= find_object( namespace, name
, len
);
520 if (ops
&& obj
->ops
!= ops
)
521 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
523 handle
= alloc_handle( current
->process
, obj
, access
, inherit
);
524 release_object( obj
);
527 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
531 /* return the size of the handle table of a given process */
532 unsigned int get_handle_table_count( struct process
*process
)
534 return process
->handles
->count
;
538 DECL_HANDLER(close_handle
)
540 close_handle( current
->process
, req
->handle
, &reply
->fd
);
543 /* set a handle information */
544 DECL_HANDLER(set_handle_info
)
548 if (handle_is_global(req
->handle
)) fd
= -1; /* no fd cache for global handles */
549 reply
->old_flags
= set_handle_info( current
->process
, req
->handle
,
550 req
->mask
, req
->flags
, &fd
);
554 /* duplicate a handle */
555 DECL_HANDLER(dup_handle
)
557 struct process
*src
, *dst
;
561 if ((src
= get_process_from_handle( req
->src_process
, PROCESS_DUP_HANDLE
)))
563 if (req
->options
& DUP_HANDLE_MAKE_GLOBAL
)
565 reply
->handle
= duplicate_handle( src
, req
->src_handle
, NULL
,
566 req
->access
, req
->inherit
, req
->options
);
568 else if ((dst
= get_process_from_handle( req
->dst_process
, PROCESS_DUP_HANDLE
)))
570 reply
->handle
= duplicate_handle( src
, req
->src_handle
, dst
,
571 req
->access
, req
->inherit
, req
->options
);
572 release_object( dst
);
574 /* close the handle no matter what happened */
575 if (req
->options
& DUP_HANDLE_CLOSE_SOURCE
)
577 if (src
== current
->process
) close_handle( src
, req
->src_handle
, &reply
->fd
);
578 else close_handle( src
, req
->src_handle
, NULL
);
580 release_object( src
);