Porting fixes.
[wine.git] / server / handle.c
blob471d5a7edee23edd211359bf3577fcb33d84de35
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 <stdio.h>
28 #include <stdlib.h>
30 #include "winbase.h"
32 #include "handle.h"
33 #include "process.h"
34 #include "thread.h"
35 #include "request.h"
37 struct handle_entry
39 struct object *ptr; /* object */
40 unsigned int access; /* access rights */
41 int fd; /* file descriptor (in client process) */
44 struct handle_table
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 */
105 NULL, /* signaled */
106 NULL, /* satisfied */
107 no_get_fd, /* get_fd */
108 no_get_file_info, /* get_file_info */
109 handle_table_destroy /* destroy */
112 /* dump a handle table */
113 static void handle_table_dump( struct object *obj, int verbose )
115 int i;
116 struct handle_table *table = (struct handle_table *)obj;
117 struct handle_entry *entry = table->entries;
119 assert( obj->ops == &handle_table_ops );
121 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
122 table->last, table->count, table->process );
123 if (!verbose) return;
124 entry = table->entries;
125 for (i = 0; i <= table->last; i++, entry++)
127 if (!entry->ptr) continue;
128 fprintf( stderr, "%9u: %p %08x ",
129 (unsigned int)index_to_handle(i), entry->ptr, entry->access );
130 entry->ptr->ops->dump( entry->ptr, 0 );
134 /* destroy a handle table */
135 static void handle_table_destroy( struct object *obj )
137 int i;
138 struct handle_table *table = (struct handle_table *)obj;
139 struct handle_entry *entry = table->entries;
141 assert( obj->ops == &handle_table_ops );
143 for (i = 0; i <= table->last; i++, entry++)
145 struct object *obj = entry->ptr;
146 entry->ptr = NULL;
147 if (obj) release_object( obj );
149 free( table->entries );
152 /* allocate a new handle table */
153 struct handle_table *alloc_handle_table( struct process *process, int count )
155 struct handle_table *table;
157 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
158 if (!(table = alloc_object( &handle_table_ops, -1 )))
159 return NULL;
160 table->process = process;
161 table->count = count;
162 table->last = -1;
163 table->free = 0;
164 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
165 release_object( table );
166 return NULL;
169 /* grow a handle table */
170 static int grow_handle_table( struct handle_table *table )
172 struct handle_entry *new_entries;
173 int count = table->count;
175 if (count >= INT_MAX / 2) return 0;
176 count *= 2;
177 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
179 set_error( STATUS_NO_MEMORY );
180 return 0;
182 table->entries = new_entries;
183 table->count = count;
184 return 1;
187 /* allocate the first free entry in the handle table */
188 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
190 struct handle_entry *entry = table->entries + table->free;
191 int i;
193 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
194 if (i >= table->count)
196 if (!grow_handle_table( table )) return 0;
197 entry = table->entries + i; /* the entries may have moved */
199 table->last = i;
200 found:
201 table->free = i + 1;
202 entry->ptr = grab_object( obj );
203 entry->access = access;
204 entry->fd = -1;
205 return index_to_handle(i);
208 /* allocate a handle for an object, incrementing its refcount */
209 /* return the handle, or 0 on error */
210 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
212 struct handle_table *table = process->handles;
214 assert( table );
215 assert( !(access & RESERVED_ALL) );
216 if (inherit) access |= RESERVED_INHERIT;
217 return alloc_entry( table, obj, access );
220 /* allocate a global handle for an object, incrementing its refcount */
221 /* return the handle, or 0 on error */
222 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
224 if (!global_table)
226 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
227 return 0;
229 return handle_local_to_global( alloc_entry( global_table, obj, access ));
232 /* return a handle entry, or NULL if the handle is invalid */
233 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
235 struct handle_table *table = process->handles;
236 struct handle_entry *entry;
237 int index;
239 if (handle_is_global(handle))
241 handle = handle_global_to_local(handle);
242 table = global_table;
244 if (!table) goto error;
245 index = handle_to_index( handle );
246 if (index < 0) goto error;
247 if (index > table->last) goto error;
248 entry = table->entries + index;
249 if (!entry->ptr) goto error;
250 return entry;
252 error:
253 set_error( STATUS_INVALID_HANDLE );
254 return NULL;
257 /* attempt to shrink a table */
258 static void shrink_handle_table( struct handle_table *table )
260 struct handle_entry *entry = table->entries + table->last;
261 struct handle_entry *new_entries;
262 int count = table->count;
264 while (table->last >= 0)
266 if (entry->ptr) break;
267 table->last--;
268 entry--;
270 if (table->last >= count / 4) return; /* no need to shrink */
271 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
272 count /= 2;
273 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
274 table->count = count;
275 table->entries = new_entries;
278 /* copy the handle table of the parent process */
279 /* return 1 if OK, 0 on error */
280 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
282 struct handle_table *parent_table = parent->handles;
283 struct handle_table *table;
284 int i;
286 assert( parent_table );
287 assert( parent_table->obj.ops == &handle_table_ops );
289 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
290 return NULL;
292 if ((table->last = parent_table->last) >= 0)
294 struct handle_entry *ptr = table->entries;
295 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
296 for (i = 0; i <= table->last; i++, ptr++)
298 if (!ptr->ptr) continue;
299 ptr->fd = -1;
300 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
301 else ptr->ptr = NULL; /* don't inherit this entry */
304 /* attempt to shrink the table */
305 shrink_handle_table( table );
306 return table;
309 /* close a handle and decrement the refcount of the associated object */
310 /* return 1 if OK, 0 on error */
311 int close_handle( struct process *process, obj_handle_t handle, int *fd )
313 struct handle_table *table;
314 struct handle_entry *entry;
315 struct object *obj;
317 if (!(entry = get_handle( process, handle ))) return 0;
318 if (entry->access & RESERVED_CLOSE_PROTECT)
320 set_error( STATUS_INVALID_HANDLE );
321 return 0;
323 obj = entry->ptr;
324 entry->ptr = NULL;
325 if (fd) *fd = entry->fd;
326 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
327 entry->fd = -1;
328 table = handle_is_global(handle) ? global_table : process->handles;
329 if (entry < table->entries + table->free) table->free = entry - table->entries;
330 if (entry == table->entries + table->last) shrink_handle_table( table );
331 /* hack: windows seems to treat registry handles differently */
332 registry_close_handle( obj, handle );
333 release_object( obj );
334 return 1;
337 /* close all the global handles */
338 void close_global_handles(void)
340 if (global_table)
342 release_object( global_table );
343 global_table = NULL;
347 /* retrieve the object corresponding to one of the magic pseudo-handles */
348 static inline struct object *get_magic_handle( obj_handle_t handle )
350 switch((unsigned long)handle)
352 case 0xfffffffe: /* current thread pseudo-handle */
353 return &current->obj;
354 case 0x7fffffff: /* current process pseudo-handle */
355 case 0xffffffff: /* current process pseudo-handle */
356 return (struct object *)current->process;
357 default:
358 return NULL;
362 /* retrieve the object corresponding to a handle, incrementing its refcount */
363 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
364 unsigned int access, const struct object_ops *ops )
366 struct handle_entry *entry;
367 struct object *obj;
369 if (!(obj = get_magic_handle( handle )))
371 if (!(entry = get_handle( process, handle ))) return NULL;
372 if ((entry->access & access) != access)
374 set_error( STATUS_ACCESS_DENIED );
375 return NULL;
377 obj = entry->ptr;
379 if (ops && (obj->ops != ops))
381 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
382 return NULL;
384 return grab_object( obj );
387 /* retrieve the cached fd for a given handle */
388 int get_handle_fd( struct process *process, obj_handle_t handle, unsigned int access )
390 struct handle_entry *entry;
392 if (!(entry = get_handle( process, handle ))) return -1;
393 if ((entry->access & access) != access)
395 set_error( STATUS_ACCESS_DENIED );
396 return -1;
398 return entry->fd;
401 /* find the first inherited handle of the given type */
402 /* this is needed for window stations and desktops (don't ask...) */
403 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
405 struct handle_table *table = process->handles;
406 struct handle_entry *ptr;
407 int i;
409 if (!table) return 0;
411 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
413 if (!ptr->ptr) continue;
414 if (ptr->ptr->ops != ops) continue;
415 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
417 return 0;
420 /* get/set the handle reserved flags */
421 /* return the old flags (or -1 on error) */
422 int set_handle_info( struct process *process, obj_handle_t handle, int mask, int flags, int *fd )
424 struct handle_entry *entry;
425 unsigned int old_access;
427 if (get_magic_handle( handle ))
429 /* we can retrieve but not set info for magic handles */
430 if (mask) set_error( STATUS_ACCESS_DENIED );
431 return 0;
433 if (!(entry = get_handle( process, handle ))) return -1;
434 old_access = entry->access;
435 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
436 flags = (flags << RESERVED_SHIFT) & mask;
437 entry->access = (entry->access & ~mask) | flags;
438 /* if no current fd set it, otherwise return current fd */
439 if (entry->fd == -1) entry->fd = *fd;
440 *fd = entry->fd;
441 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
444 /* duplicate a handle */
445 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
446 unsigned int access, int inherit, int options )
448 obj_handle_t res;
449 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
451 if (!obj) return 0;
452 if (options & DUP_HANDLE_SAME_ACCESS)
454 struct handle_entry *entry = get_handle( src, src_handle );
455 if (entry)
456 access = entry->access;
457 else /* pseudo-handle, give it full access */
459 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
460 clear_error();
463 access &= ~RESERVED_ALL;
464 if (options & DUP_HANDLE_MAKE_GLOBAL)
465 res = alloc_global_handle( obj, access );
466 else
467 res = alloc_handle( dst, obj, access, inherit );
468 release_object( obj );
469 return res;
472 /* open a new handle to an existing object */
473 obj_handle_t open_object( const struct namespace *namespace, const WCHAR *name, size_t len,
474 const struct object_ops *ops, unsigned int access, int inherit )
476 obj_handle_t handle = 0;
477 struct object *obj = find_object( namespace, name, len );
478 if (obj)
480 if (ops && obj->ops != ops)
481 set_error( STATUS_OBJECT_TYPE_MISMATCH );
482 else
483 handle = alloc_handle( current->process, obj, access, inherit );
484 release_object( obj );
486 else
487 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
488 return handle;
491 /* close a handle */
492 DECL_HANDLER(close_handle)
494 close_handle( current->process, req->handle, &reply->fd );
497 /* set a handle information */
498 DECL_HANDLER(set_handle_info)
500 int fd = req->fd;
502 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
503 reply->old_flags = set_handle_info( current->process, req->handle,
504 req->mask, req->flags, &fd );
505 reply->cur_fd = fd;
508 /* duplicate a handle */
509 DECL_HANDLER(dup_handle)
511 struct process *src, *dst;
513 reply->handle = 0;
514 reply->fd = -1;
515 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
517 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
519 reply->handle = duplicate_handle( src, req->src_handle, NULL,
520 req->access, req->inherit, req->options );
522 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
524 reply->handle = duplicate_handle( src, req->src_handle, dst,
525 req->access, req->inherit, req->options );
526 release_object( dst );
528 /* close the handle no matter what happened */
529 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
531 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
532 else close_handle( src, req->src_handle, NULL );
534 release_object( src );