Add some tests for _fcvt.
[wine/multimedia.git] / server / handle.c
blobb9312cea7448aa6ff379617389553bda968ee3a2
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "process.h"
36 #include "thread.h"
37 #include "request.h"
39 struct handle_entry
41 struct object *ptr; /* object */
42 unsigned int access; /* access rights */
43 int fd; /* file descriptor (in client process) */
46 struct handle_table
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 */
107 NULL, /* signaled */
108 NULL, /* satisfied */
109 no_signal, /* signal */
110 no_get_fd, /* get_fd */
111 no_lookup_name, /* lookup_name */
112 no_close_handle, /* close_handle */
113 handle_table_destroy /* destroy */
116 /* dump a handle table */
117 static void handle_table_dump( struct object *obj, int verbose )
119 int i;
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, " %p: %p %08x ",
133 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 )
141 int i;
142 struct handle_table *table = (struct handle_table *)obj;
143 struct handle_entry *entry;
145 assert( obj->ops == &handle_table_ops );
147 /* first notify all objects that handles are being closed */
148 if (table->process)
150 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
152 struct object *obj = entry->ptr;
153 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
157 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
159 struct object *obj = entry->ptr;
160 entry->ptr = NULL;
161 if (obj) release_object( obj );
163 free( table->entries );
166 /* allocate a new handle table */
167 struct handle_table *alloc_handle_table( struct process *process, int count )
169 struct handle_table *table;
171 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
172 if (!(table = alloc_object( &handle_table_ops )))
173 return NULL;
174 table->process = process;
175 table->count = count;
176 table->last = -1;
177 table->free = 0;
178 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
179 release_object( table );
180 return NULL;
183 /* grow a handle table */
184 static int grow_handle_table( struct handle_table *table )
186 struct handle_entry *new_entries;
187 int count = table->count;
189 if (count >= INT_MAX / 2) return 0;
190 count *= 2;
191 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
193 set_error( STATUS_NO_MEMORY );
194 return 0;
196 table->entries = new_entries;
197 table->count = count;
198 return 1;
201 /* allocate the first free entry in the handle table */
202 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
204 struct handle_entry *entry = table->entries + table->free;
205 int i;
207 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
208 if (i >= table->count)
210 if (!grow_handle_table( table )) return 0;
211 entry = table->entries + i; /* the entries may have moved */
213 table->last = i;
214 found:
215 table->free = i + 1;
216 entry->ptr = grab_object( obj );
217 entry->access = access;
218 entry->fd = -1;
219 return index_to_handle(i);
222 /* allocate a handle for an object, incrementing its refcount */
223 /* return the handle, or 0 on error */
224 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
226 access &= ~RESERVED_ALL;
227 if (inherit) access |= RESERVED_INHERIT;
228 if (!process->handles)
230 set_error( STATUS_NO_MEMORY );
231 return 0;
233 return alloc_entry( process->handles, obj, access );
236 /* allocate a global handle for an object, incrementing its refcount */
237 /* return the handle, or 0 on error */
238 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
240 if (!global_table)
242 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
243 return 0;
245 return handle_local_to_global( alloc_entry( global_table, obj, access ));
248 /* return a handle entry, or NULL if the handle is invalid */
249 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
251 struct handle_table *table = process->handles;
252 struct handle_entry *entry;
253 int index;
255 if (handle_is_global(handle))
257 handle = handle_global_to_local(handle);
258 table = global_table;
260 if (!table) goto error;
261 index = handle_to_index( handle );
262 if (index < 0) goto error;
263 if (index > table->last) goto error;
264 entry = table->entries + index;
265 if (!entry->ptr) goto error;
266 return entry;
268 error:
269 set_error( STATUS_INVALID_HANDLE );
270 return NULL;
273 /* attempt to shrink a table */
274 static void shrink_handle_table( struct handle_table *table )
276 struct handle_entry *entry = table->entries + table->last;
277 struct handle_entry *new_entries;
278 int count = table->count;
280 while (table->last >= 0)
282 if (entry->ptr) break;
283 table->last--;
284 entry--;
286 if (table->last >= count / 4) return; /* no need to shrink */
287 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
288 count /= 2;
289 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
290 table->count = count;
291 table->entries = new_entries;
294 /* copy the handle table of the parent process */
295 /* return 1 if OK, 0 on error */
296 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
298 struct handle_table *parent_table = parent->handles;
299 struct handle_table *table;
300 int i;
302 assert( parent_table );
303 assert( parent_table->obj.ops == &handle_table_ops );
305 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
306 return NULL;
308 if ((table->last = parent_table->last) >= 0)
310 struct handle_entry *ptr = table->entries;
311 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
312 for (i = 0; i <= table->last; i++, ptr++)
314 if (!ptr->ptr) continue;
315 ptr->fd = -1;
316 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
317 else ptr->ptr = NULL; /* don't inherit this entry */
320 /* attempt to shrink the table */
321 shrink_handle_table( table );
322 return table;
325 /* close a handle and decrement the refcount of the associated object */
326 /* return 1 if OK, 0 on error */
327 int close_handle( struct process *process, obj_handle_t handle, int *fd )
329 struct handle_table *table;
330 struct handle_entry *entry;
331 struct object *obj;
333 if (!(entry = get_handle( process, handle ))) return 0;
334 if (entry->access & RESERVED_CLOSE_PROTECT)
336 set_error( STATUS_HANDLE_NOT_CLOSABLE );
337 return 0;
339 obj = entry->ptr;
340 if (!obj->ops->close_handle( obj, process, handle ))
342 set_error( STATUS_HANDLE_NOT_CLOSABLE );
343 return 0;
345 entry->ptr = NULL;
346 if (fd) *fd = entry->fd;
347 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
348 entry->fd = -1;
349 table = handle_is_global(handle) ? global_table : process->handles;
350 if (entry < table->entries + table->free) table->free = entry - table->entries;
351 if (entry == table->entries + table->last) shrink_handle_table( table );
352 release_object( obj );
353 return 1;
356 /* close all the global handles */
357 void close_global_handles(void)
359 if (global_table)
361 release_object( global_table );
362 global_table = NULL;
366 /* retrieve the object corresponding to one of the magic pseudo-handles */
367 static inline struct object *get_magic_handle( obj_handle_t handle )
369 switch((unsigned long)handle)
371 case 0xfffffffe: /* current thread pseudo-handle */
372 return &current->obj;
373 case 0x7fffffff: /* current process pseudo-handle */
374 case 0xffffffff: /* current process pseudo-handle */
375 return (struct object *)current->process;
376 default:
377 return NULL;
381 /* retrieve the object corresponding to a handle, incrementing its refcount */
382 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
383 unsigned int access, const struct object_ops *ops )
385 struct handle_entry *entry;
386 struct object *obj;
388 if (!(obj = get_magic_handle( handle )))
390 if (!(entry = get_handle( process, handle ))) return NULL;
391 if ((entry->access & access) != access)
393 set_error( STATUS_ACCESS_DENIED );
394 return NULL;
396 obj = entry->ptr;
398 if (ops && (obj->ops != ops))
400 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
401 return NULL;
403 return grab_object( obj );
406 /* retrieve the access rights of a given handle */
407 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
409 struct handle_entry *entry;
411 if (get_magic_handle( handle )) return ~0U; /* magic handles have all access rights */
412 if (!(entry = get_handle( process, handle ))) return 0;
413 return entry->access;
416 /* retrieve the cached fd for a given handle */
417 int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
419 struct handle_entry *entry;
421 if (!(entry = get_handle( process, handle ))) return -1;
422 if ((entry->access & access) != access)
424 set_error( STATUS_ACCESS_DENIED );
425 return -1;
427 return entry->fd;
430 /* set the cached fd for a handle if not set already, and return the current value */
431 int set_handle_unix_fd( struct process *process, obj_handle_t handle, int fd )
433 struct handle_entry *entry;
435 if (handle_is_global( handle )) return -1; /* no fd cache for global handles */
436 if (!(entry = get_handle( process, handle ))) return -1;
437 /* if no current fd set it, otherwise return current fd */
438 if (entry->fd == -1) entry->fd = fd;
439 return entry->fd;
442 /* remove the cached fd and return it */
443 int flush_cached_fd( struct process *process, obj_handle_t handle )
445 struct handle_entry *entry = get_handle( process, handle );
446 int fd = -1;
448 if (entry)
450 fd = entry->fd;
451 entry->fd = -1;
453 return fd;
456 /* find the first inherited handle of the given type */
457 /* this is needed for window stations and desktops (don't ask...) */
458 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
460 struct handle_table *table = process->handles;
461 struct handle_entry *ptr;
462 int i;
464 if (!table) return 0;
466 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
468 if (!ptr->ptr) continue;
469 if (ptr->ptr->ops != ops) continue;
470 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
472 return 0;
475 /* get/set the handle reserved flags */
476 /* return the old flags (or -1 on error) */
477 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
479 struct handle_entry *entry;
480 unsigned int old_access;
482 if (get_magic_handle( handle ))
484 /* we can retrieve but not set info for magic handles */
485 if (mask) set_error( STATUS_ACCESS_DENIED );
486 return 0;
488 if (!(entry = get_handle( process, handle ))) return -1;
489 old_access = entry->access;
490 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
491 flags = (flags << RESERVED_SHIFT) & mask;
492 entry->access = (entry->access & ~mask) | flags;
493 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
496 /* duplicate a handle */
497 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
498 unsigned int access, int inherit, int options )
500 obj_handle_t res;
501 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
503 if (!obj) return 0;
504 if (options & DUP_HANDLE_SAME_ACCESS)
506 struct handle_entry *entry = get_handle( src, src_handle );
507 if (entry)
508 access = entry->access;
509 else /* pseudo-handle, give it full access */
511 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
512 clear_error();
515 access &= ~RESERVED_ALL;
516 if (options & DUP_HANDLE_MAKE_GLOBAL)
517 res = alloc_global_handle( obj, access );
518 else
519 res = alloc_handle( dst, obj, access, inherit );
520 release_object( obj );
521 return res;
524 /* open a new handle to an existing object */
525 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
526 const struct object_ops *ops, unsigned int access, unsigned int attr )
528 obj_handle_t handle = 0;
529 struct object *obj = find_object( namespace, name, attr );
530 if (obj)
532 if (ops && obj->ops != ops)
533 set_error( STATUS_OBJECT_TYPE_MISMATCH );
534 else
535 handle = alloc_handle( current->process, obj, access, attr & OBJ_INHERIT );
536 release_object( obj );
538 else
539 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
540 return handle;
543 /* return the size of the handle table of a given process */
544 unsigned int get_handle_table_count( struct process *process )
546 if (!process->handles) return 0;
547 return process->handles->count;
550 /* close a handle */
551 DECL_HANDLER(close_handle)
553 close_handle( current->process, req->handle, &reply->fd );
556 /* set a handle information */
557 DECL_HANDLER(set_handle_info)
559 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
562 /* duplicate a handle */
563 DECL_HANDLER(dup_handle)
565 struct process *src, *dst;
567 reply->handle = 0;
568 reply->fd = -1;
569 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
571 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
573 reply->handle = duplicate_handle( src, req->src_handle, NULL,
574 req->access, req->inherit, req->options );
576 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
578 reply->handle = duplicate_handle( src, req->src_handle, dst,
579 req->access, req->inherit, req->options );
580 release_object( dst );
582 /* close the handle no matter what happened */
583 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
585 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
586 else close_handle( src, req->src_handle, NULL );
588 release_object( src );