Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / server / handle.c
blob89c92d08dfd85e37f9c045ef1de1ec9e7f9e27b6
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 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 )
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, "%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 )
141 int i;
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;
150 entry->ptr = NULL;
151 if (obj) release_object( obj );
153 free( table->entries );
156 /* allocate a new handle table */
157 struct object *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 )))
163 return NULL;
164 table->process = process;
165 table->count = count;
166 table->last = -1;
167 table->free = 0;
168 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
169 release_object( table );
170 return NULL;
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;
180 count *= 2;
181 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
183 set_error( STATUS_NO_MEMORY );
184 return 0;
186 table->entries = new_entries;
187 table->count = count;
188 return 1;
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;
195 int i;
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 */
203 table->last = i;
204 found:
205 table->free = i + 1;
206 entry->ptr = grab_object( obj );
207 entry->access = access;
208 entry->fd = -1;
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 = (struct handle_table *)process->handles;
218 assert( table );
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 )
228 if (!global_table)
230 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
231 return 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 = (struct handle_table *)process->handles;
240 struct handle_entry *entry;
241 int index;
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;
254 return entry;
256 error:
257 set_error( STATUS_INVALID_HANDLE );
258 return NULL;
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;
271 table->last--;
272 entry--;
274 if (table->last >= count / 4) return; /* no need to shrink */
275 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
276 count /= 2;
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 object *copy_handle_table( struct process *process, struct process *parent )
286 struct handle_table *parent_table = (struct handle_table *)parent->handles;
287 struct handle_table *table;
288 int i;
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 )))
294 return NULL;
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;
303 ptr->fd = -1;
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 );
310 return &table->obj;
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;
319 struct object *obj;
321 if (!(entry = get_handle( process, handle ))) return 0;
322 if (entry->access & RESERVED_CLOSE_PROTECT)
324 set_error( STATUS_INVALID_HANDLE );
325 return 0;
327 obj = entry->ptr;
328 entry->ptr = NULL;
329 if (fd) *fd = entry->fd;
330 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
331 entry->fd = -1;
332 table = handle_is_global(handle) ? global_table : (struct handle_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 );
336 return 1;
339 /* close all the global handles */
340 void close_global_handles(void)
342 if (global_table)
344 release_object( global_table );
345 global_table = NULL;
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 &current->obj;
356 case 0x7fffffff: /* current process pseudo-handle */
357 case 0xffffffff: /* current process pseudo-handle */
358 return (struct object *)current->process;
359 default:
360 return NULL;
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;
369 struct object *obj;
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 );
377 return NULL;
379 obj = entry->ptr;
381 if (ops && (obj->ops != ops))
383 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
384 return NULL;
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 );
398 return -1;
400 return entry->fd;
403 /* get/set the handle reserved flags */
404 /* return the old flags (or -1 on error) */
405 static int set_handle_info( struct process *process, obj_handle_t handle,
406 int mask, int flags, int *fd )
408 struct handle_entry *entry;
409 unsigned int old_access;
411 if (get_magic_handle( handle ))
413 /* we can retrieve but not set info for magic handles */
414 if (mask) set_error( STATUS_ACCESS_DENIED );
415 return 0;
417 if (!(entry = get_handle( process, handle ))) return -1;
418 old_access = entry->access;
419 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
420 flags = (flags << RESERVED_SHIFT) & mask;
421 entry->access = (entry->access & ~mask) | flags;
422 /* if no current fd set it, otherwise return current fd */
423 if (entry->fd == -1) entry->fd = *fd;
424 *fd = entry->fd;
425 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
428 /* duplicate a handle */
429 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
430 unsigned int access, int inherit, int options )
432 obj_handle_t res;
433 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
435 if (!obj) return 0;
436 if (options & DUP_HANDLE_SAME_ACCESS)
438 struct handle_entry *entry = get_handle( src, src_handle );
439 if (entry)
440 access = entry->access;
441 else /* pseudo-handle, give it full access */
443 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
444 clear_error();
447 access &= ~RESERVED_ALL;
448 if (options & DUP_HANDLE_MAKE_GLOBAL)
449 res = alloc_global_handle( obj, access );
450 else
451 res = alloc_handle( dst, obj, access, inherit );
452 release_object( obj );
453 return res;
456 /* open a new handle to an existing object */
457 obj_handle_t open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
458 unsigned int access, int inherit )
460 obj_handle_t handle = 0;
461 struct object *obj = find_object( name, len );
462 if (obj)
464 if (ops && obj->ops != ops)
465 set_error( STATUS_OBJECT_TYPE_MISMATCH );
466 else
467 handle = alloc_handle( current->process, obj, access, inherit );
468 release_object( obj );
470 else
471 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
472 return handle;
475 /* close a handle */
476 DECL_HANDLER(close_handle)
478 close_handle( current->process, req->handle, &reply->fd );
481 /* set a handle information */
482 DECL_HANDLER(set_handle_info)
484 int fd = req->fd;
486 if (handle_is_global(req->handle)) fd = -1; /* no fd cache for global handles */
487 reply->old_flags = set_handle_info( current->process, req->handle,
488 req->mask, req->flags, &fd );
489 reply->cur_fd = fd;
492 /* duplicate a handle */
493 DECL_HANDLER(dup_handle)
495 struct process *src, *dst;
497 reply->handle = 0;
498 reply->fd = -1;
499 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
501 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
503 reply->handle = duplicate_handle( src, req->src_handle, NULL,
504 req->access, req->inherit, req->options );
506 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
508 reply->handle = duplicate_handle( src, req->src_handle, dst,
509 req->access, req->inherit, req->options );
510 release_object( dst );
512 /* close the handle no matter what happened */
513 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
515 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
516 else close_handle( src, req->src_handle, NULL );
518 release_object( src );