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 release_object( obj
);
339 /* close all the global handles */
340 void close_global_handles(void)
344 release_object( global_table
);
349 /* retrieve the object corresponding to one of the magic pseudo-handles */
350 static inline struct object
*get_magic_handle( obj_handle_t handle
)
352 switch((unsigned long)handle
)
354 case 0xfffffffe: /* current thread pseudo-handle */
355 return ¤t
->obj
;
356 case 0x7fffffff: /* current process pseudo-handle */
357 case 0xffffffff: /* current process pseudo-handle */
358 return (struct object
*)current
->process
;
364 /* retrieve the object corresponding to a handle, incrementing its refcount */
365 struct object
*get_handle_obj( struct process
*process
, obj_handle_t handle
,
366 unsigned int access
, const struct object_ops
*ops
)
368 struct handle_entry
*entry
;
371 if (!(obj
= get_magic_handle( handle
)))
373 if (!(entry
= get_handle( process
, handle
))) return NULL
;
374 if ((entry
->access
& access
) != access
)
376 set_error( STATUS_ACCESS_DENIED
);
381 if (ops
&& (obj
->ops
!= ops
))
383 set_error( STATUS_OBJECT_TYPE_MISMATCH
); /* not the right type */
386 return grab_object( obj
);
389 /* retrieve the cached fd for a given handle */
390 int get_handle_fd( struct process
*process
, obj_handle_t handle
, unsigned int access
)
392 struct handle_entry
*entry
;
394 if (!(entry
= get_handle( process
, handle
))) return -1;
395 if ((entry
->access
& access
) != access
)
397 set_error( STATUS_ACCESS_DENIED
);
403 /* find the first inherited handle of the given type */
404 /* this is needed for window stations and desktops (don't ask...) */
405 obj_handle_t
find_inherited_handle( struct process
*process
, const struct object_ops
*ops
)
407 struct handle_table
*table
= process
->handles
;
408 struct handle_entry
*ptr
;
411 if (!table
) return 0;
413 for (i
= 0, ptr
= table
->entries
; i
<= table
->last
; i
++, ptr
++)
415 if (!ptr
->ptr
) continue;
416 if (ptr
->ptr
->ops
!= ops
) continue;
417 if (ptr
->access
& RESERVED_INHERIT
) return index_to_handle(i
);
422 /* get/set the handle reserved flags */
423 /* return the old flags (or -1 on error) */
424 int set_handle_info( struct process
*process
, obj_handle_t handle
, int mask
, int flags
, int *fd
)
426 struct handle_entry
*entry
;
427 unsigned int old_access
;
429 if (get_magic_handle( handle
))
431 /* we can retrieve but not set info for magic handles */
432 if (mask
) set_error( STATUS_ACCESS_DENIED
);
435 if (!(entry
= get_handle( process
, handle
))) return -1;
436 old_access
= entry
->access
;
437 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
438 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
439 entry
->access
= (entry
->access
& ~mask
) | flags
;
440 /* if no current fd set it, otherwise return current fd */
441 if (entry
->fd
== -1) entry
->fd
= *fd
;
443 return (old_access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
446 /* duplicate a handle */
447 obj_handle_t
duplicate_handle( struct process
*src
, obj_handle_t src_handle
, struct process
*dst
,
448 unsigned int access
, int inherit
, int options
)
451 struct object
*obj
= get_handle_obj( src
, src_handle
, 0, NULL
);
454 if (options
& DUP_HANDLE_SAME_ACCESS
)
456 struct handle_entry
*entry
= get_handle( src
, src_handle
);
458 access
= entry
->access
;
459 else /* pseudo-handle, give it full access */
461 access
= STANDARD_RIGHTS_ALL
| SPECIFIC_RIGHTS_ALL
;
465 access
&= ~RESERVED_ALL
;
466 if (options
& DUP_HANDLE_MAKE_GLOBAL
)
467 res
= alloc_global_handle( obj
, access
);
469 res
= alloc_handle( dst
, obj
, access
, inherit
);
470 release_object( obj
);
474 /* open a new handle to an existing object */
475 obj_handle_t
open_object( const struct namespace *namespace, const WCHAR
*name
, size_t len
,
476 const struct object_ops
*ops
, unsigned int access
, int inherit
)
478 obj_handle_t handle
= 0;
479 struct object
*obj
= find_object( namespace, name
, len
);
482 if (ops
&& obj
->ops
!= ops
)
483 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
485 handle
= alloc_handle( current
->process
, obj
, access
, inherit
);
486 release_object( obj
);
489 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
494 DECL_HANDLER(close_handle
)
496 close_handle( current
->process
, req
->handle
, &reply
->fd
);
499 /* set a handle information */
500 DECL_HANDLER(set_handle_info
)
504 if (handle_is_global(req
->handle
)) fd
= -1; /* no fd cache for global handles */
505 reply
->old_flags
= set_handle_info( current
->process
, req
->handle
,
506 req
->mask
, req
->flags
, &fd
);
510 /* duplicate a handle */
511 DECL_HANDLER(dup_handle
)
513 struct process
*src
, *dst
;
517 if ((src
= get_process_from_handle( req
->src_process
, PROCESS_DUP_HANDLE
)))
519 if (req
->options
& DUP_HANDLE_MAKE_GLOBAL
)
521 reply
->handle
= duplicate_handle( src
, req
->src_handle
, NULL
,
522 req
->access
, req
->inherit
, req
->options
);
524 else if ((dst
= get_process_from_handle( req
->dst_process
, PROCESS_DUP_HANDLE
)))
526 reply
->handle
= duplicate_handle( src
, req
->src_handle
, dst
,
527 req
->access
, req
->inherit
, req
->options
);
528 release_object( dst
);
530 /* close the handle no matter what happened */
531 if (req
->options
& DUP_HANDLE_CLOSE_SOURCE
)
533 if (src
== current
->process
) close_handle( src
, req
->src_handle
, &reply
->fd
);
534 else close_handle( src
, req
->src_handle
, NULL
);
536 release_object( src
);