Built a complete translation table for RtlNtStatusToDosError.
[wine.git] / server / handle.c
blobbe8151104b0c5ce97978a08187022d7e07dee785
1 /*
2 * Server-side handle management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <limits.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
13 #include "winerror.h"
14 #include "winbase.h"
16 #include "handle.h"
17 #include "process.h"
18 #include "thread.h"
19 #include "request.h"
21 struct handle_entry
23 struct object *ptr;
24 unsigned int access;
27 struct handle_table
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 /* global handle macros */
46 #define HANDLE_OBFUSCATOR 0x544a4def
47 #define HANDLE_IS_GLOBAL(h) (((h) ^ HANDLE_OBFUSCATOR) < 0x10000)
48 #define HANDLE_LOCAL_TO_GLOBAL(h) ((h) ^ HANDLE_OBFUSCATOR)
49 #define HANDLE_GLOBAL_TO_LOCAL(h) ((h) ^ HANDLE_OBFUSCATOR)
51 #define MIN_HANDLE_ENTRIES 32
54 /* handle to table index conversion */
56 /* handles are a multiple of 4 under NT; handle 0 is not used */
57 static int inline index_to_handle( int index )
59 return (index + 1) << 2;
61 static int inline handle_to_index( int handle )
63 return (handle >> 2) - 1;
67 static void handle_table_dump( struct object *obj, int verbose );
68 static void handle_table_destroy( struct object *obj );
70 static const struct object_ops handle_table_ops =
72 sizeof(struct handle_table), /* size */
73 handle_table_dump, /* dump */
74 no_add_queue, /* add_queue */
75 NULL, /* remove_queue */
76 NULL, /* signaled */
77 NULL, /* satisfied */
78 NULL, /* get_poll_events */
79 NULL, /* poll_event */
80 no_read_fd, /* get_read_fd */
81 no_write_fd, /* get_write_fd */
82 no_flush, /* flush */
83 no_get_file_info, /* get_file_info */
84 handle_table_destroy /* destroy */
87 /* dump a handle table */
88 static void handle_table_dump( struct object *obj, int verbose )
90 int i;
91 struct handle_table *table = (struct handle_table *)obj;
92 struct handle_entry *entry = table->entries;
94 assert( obj->ops == &handle_table_ops );
96 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
97 table->last, table->count, table->process );
98 if (!verbose) return;
99 entry = table->entries;
100 for (i = 0; i <= table->last; i++, entry++)
102 if (!entry->ptr) continue;
103 fprintf( stderr, "%9d: %p %08x ", index_to_handle(i), entry->ptr, entry->access );
104 entry->ptr->ops->dump( entry->ptr, 0 );
108 /* destroy a handle table */
109 static void handle_table_destroy( struct object *obj )
111 int i;
112 struct handle_table *table = (struct handle_table *)obj;
113 struct handle_entry *entry = table->entries;
115 assert( obj->ops == &handle_table_ops );
117 for (i = 0; i <= table->last; i++, entry++)
119 struct object *obj = entry->ptr;
120 entry->ptr = NULL;
121 if (obj) release_object( obj );
123 free( table->entries );
126 /* allocate a new handle table */
127 struct object *alloc_handle_table( struct process *process, int count )
129 struct handle_table *table;
131 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
132 if (!(table = alloc_object( &handle_table_ops, -1 )))
133 return NULL;
134 table->process = process;
135 table->count = count;
136 table->last = -1;
137 table->free = 0;
138 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
139 release_object( table );
140 return NULL;
143 /* grow a handle table */
144 static int grow_handle_table( struct handle_table *table )
146 struct handle_entry *new_entries;
147 int count = table->count;
149 if (count >= INT_MAX / 2) return 0;
150 count *= 2;
151 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
153 set_error( ERROR_OUTOFMEMORY );
154 return 0;
156 table->entries = new_entries;
157 table->count = count;
158 return 1;
161 /* allocate the first free entry in the handle table */
162 static int alloc_entry( struct handle_table *table, void *obj, unsigned int access )
164 struct handle_entry *entry = table->entries + table->free;
165 int i;
167 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
168 if (i >= table->count)
170 if (!grow_handle_table( table )) return -1;
171 entry = table->entries + i; /* the entries may have moved */
173 table->last = i;
174 found:
175 table->free = i + 1;
176 entry->ptr = grab_object( obj );
177 entry->access = access;
178 return index_to_handle(i);
181 /* allocate a handle for an object, incrementing its refcount */
182 /* return the handle, or -1 on error */
183 int alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
185 struct handle_table *table = (struct handle_table *)process->handles;
187 assert( table );
188 assert( !(access & RESERVED_ALL) );
189 if (inherit) access |= RESERVED_INHERIT;
190 return alloc_entry( table, obj, access );
193 /* allocate a global handle for an object, incrementing its refcount */
194 /* return the handle, or -1 on error */
195 static int alloc_global_handle( void *obj, unsigned int access )
197 int handle;
199 if (!global_table)
201 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 ))) return -1;
203 if ((handle = alloc_entry( global_table, obj, access )) != -1)
204 handle = HANDLE_LOCAL_TO_GLOBAL(handle);
205 return handle;
208 /* return a handle entry, or NULL if the handle is invalid */
209 static struct handle_entry *get_handle( struct process *process, int handle )
211 struct handle_table *table = (struct handle_table *)process->handles;
212 struct handle_entry *entry;
214 if (HANDLE_IS_GLOBAL(handle))
216 handle = HANDLE_GLOBAL_TO_LOCAL(handle);
217 table = global_table;
219 if (!table) goto error;
220 handle = handle_to_index( handle );
221 if (handle < 0) goto error;
222 if (handle > table->last) goto error;
223 entry = table->entries + handle;
224 if (!entry->ptr) goto error;
225 return entry;
227 error:
228 set_error( ERROR_INVALID_HANDLE );
229 return NULL;
232 /* attempt to shrink a table */
233 static void shrink_handle_table( struct handle_table *table )
235 struct handle_entry *entry = table->entries + table->last;
236 struct handle_entry *new_entries;
237 int count = table->count;
239 while (table->last >= 0)
241 if (entry->ptr) break;
242 table->last--;
243 entry--;
245 if (table->last >= count / 4) return; /* no need to shrink */
246 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
247 count /= 2;
248 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
249 table->count = count;
250 table->entries = new_entries;
253 /* copy the handle table of the parent process */
254 /* return 1 if OK, 0 on error */
255 struct object *copy_handle_table( struct process *process, struct process *parent )
257 struct handle_table *parent_table = (struct handle_table *)parent->handles;
258 struct handle_table *table;
259 int i;
261 assert( parent_table );
262 assert( parent_table->obj.ops == &handle_table_ops );
264 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
265 return NULL;
267 if ((table->last = parent_table->last) >= 0)
269 struct handle_entry *ptr = table->entries;
270 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
271 for (i = 0; i <= table->last; i++, ptr++)
273 if (!ptr->ptr) continue;
274 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
275 else ptr->ptr = NULL; /* don't inherit this entry */
278 /* attempt to shrink the table */
279 shrink_handle_table( table );
280 return &table->obj;
283 /* close a handle and decrement the refcount of the associated object */
284 /* return 1 if OK, 0 on error */
285 int close_handle( struct process *process, int handle )
287 struct handle_table *table;
288 struct handle_entry *entry;
289 struct object *obj;
291 if (!(entry = get_handle( process, handle ))) return 0;
292 if (entry->access & RESERVED_CLOSE_PROTECT)
294 set_error( ERROR_INVALID_HANDLE );
295 return 0;
297 obj = entry->ptr;
298 entry->ptr = NULL;
299 table = HANDLE_IS_GLOBAL(handle) ? global_table : (struct handle_table *)process->handles;
300 if (entry < table->entries + table->free) table->free = entry - table->entries;
301 if (entry == table->entries + table->last) shrink_handle_table( table );
302 release_object( obj );
303 return 1;
306 /* close all the global handles */
307 void close_global_handles(void)
309 if (global_table)
311 release_object( global_table );
312 global_table = NULL;
316 /* retrieve the object corresponding to one of the magic pseudo-handles */
317 static inline struct object *get_magic_handle( int handle )
319 switch(handle)
321 case 0xfffffffe: /* current thread pseudo-handle */
322 return &current->obj;
323 case 0x7fffffff: /* current process pseudo-handle */
324 case 0xffffffff: /* current process pseudo-handle */
325 return (struct object *)current->process;
326 default:
327 return NULL;
331 /* retrieve the object corresponding to a handle, incrementing its refcount */
332 struct object *get_handle_obj( struct process *process, int handle,
333 unsigned int access, const struct object_ops *ops )
335 struct handle_entry *entry;
336 struct object *obj;
338 if (!(obj = get_magic_handle( handle )))
340 if (!(entry = get_handle( process, handle ))) return NULL;
341 if ((entry->access & access) != access)
343 set_error( ERROR_ACCESS_DENIED );
344 return NULL;
346 obj = entry->ptr;
348 if (ops && (obj->ops != ops))
350 set_error( ERROR_INVALID_HANDLE ); /* not the right type */
351 return NULL;
353 return grab_object( obj );
356 /* get/set the handle reserved flags */
357 /* return the new flags (or -1 on error) */
358 static int set_handle_info( struct process *process, int handle, int mask, int flags )
360 struct handle_entry *entry;
362 if (get_magic_handle( handle ))
364 /* we can retrieve but not set info for magic handles */
365 if (mask) set_error( ERROR_ACCESS_DENIED );
366 return 0;
368 if (!(entry = get_handle( process, handle ))) return -1;
369 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
370 flags = (flags << RESERVED_SHIFT) & mask;
371 entry->access = (entry->access & ~mask) | flags;
372 return (entry->access & RESERVED_ALL) >> RESERVED_SHIFT;
375 /* duplicate a handle */
376 int duplicate_handle( struct process *src, int src_handle, struct process *dst,
377 unsigned int access, int inherit, int options )
379 int res;
380 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
382 if (!obj) return -1;
383 if (options & DUP_HANDLE_SAME_ACCESS)
385 struct handle_entry *entry = get_handle( src, src_handle );
386 if (entry)
387 access = entry->access;
388 else /* pseudo-handle, give it full access */
390 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
391 clear_error();
394 access &= ~RESERVED_ALL;
395 if (options & DUP_HANDLE_MAKE_GLOBAL)
396 res = alloc_global_handle( obj, access );
397 else
398 res = alloc_handle( dst, obj, access, inherit );
399 release_object( obj );
400 return res;
403 /* open a new handle to an existing object */
404 int open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
405 unsigned int access, int inherit )
407 int handle = -1;
408 struct object *obj = find_object( name, len );
409 if (obj)
411 if (ops && obj->ops != ops)
412 set_error( ERROR_INVALID_HANDLE );
413 else
414 handle = alloc_handle( current->process, obj, access, inherit );
415 release_object( obj );
417 else
418 set_error( ERROR_FILE_NOT_FOUND );
419 return handle;
422 /* close a handle */
423 DECL_HANDLER(close_handle)
425 close_handle( current->process, req->handle );
428 /* get information about a handle */
429 DECL_HANDLER(get_handle_info)
431 req->flags = set_handle_info( current->process, req->handle, 0, 0 );
434 /* set a handle information */
435 DECL_HANDLER(set_handle_info)
437 set_handle_info( current->process, req->handle, req->mask, req->flags );
440 /* duplicate a handle */
441 DECL_HANDLER(dup_handle)
443 struct process *src, *dst;
445 req->handle = -1;
446 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
448 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
450 req->handle = duplicate_handle( src, req->src_handle, NULL,
451 req->access, req->inherit, req->options );
453 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
455 req->handle = duplicate_handle( src, req->src_handle, dst,
456 req->access, req->inherit, req->options );
457 release_object( dst );
459 /* close the handle no matter what happened */
460 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
461 close_handle( src, req->src_handle );
462 release_object( src );