blob: 6a3e1f613c35a314ff77aa30a21019b560dc540c [file] [log] [blame]
Alexandre Julliard43c190e1999-05-15 10:48:19 +00001/*
2 * Server-side handle management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
5 */
6
7#include <assert.h>
8#include <limits.h>
9#include <string.h>
10#include <stdio.h>
11#include <stdlib.h>
12
Alexandre Julliard43c190e1999-05-15 10:48:19 +000013#include "winbase.h"
14
15#include "handle.h"
16#include "process.h"
17#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000018#include "request.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000019
20struct handle_entry
21{
Alexandre Julliardd549f692000-12-22 02:04:15 +000022 struct object *ptr; /* object */
23 unsigned int access; /* access rights */
24 int fd; /* file descriptor (in client process) */
Alexandre Julliard43c190e1999-05-15 10:48:19 +000025};
26
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000027struct handle_table
28{
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 */
35};
36
37static struct handle_table *global_table;
Alexandre Julliard43c190e1999-05-15 10:48:19 +000038
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)
44
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)
50
51#define MIN_HANDLE_ENTRIES 32
52
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000053
Alexandre Julliardceeb6931999-11-04 02:33:45 +000054/* handle to table index conversion */
55
56/* handles are a multiple of 4 under NT; handle 0 is not used */
Joerg Mayer650ba4d2000-10-29 18:06:04 +000057inline static int index_to_handle( int index )
Alexandre Julliardceeb6931999-11-04 02:33:45 +000058{
59 return (index + 1) << 2;
60}
Joerg Mayer650ba4d2000-10-29 18:06:04 +000061inline static int handle_to_index( int handle )
Alexandre Julliardceeb6931999-11-04 02:33:45 +000062{
63 return (handle >> 2) - 1;
64}
65
66
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000067static void handle_table_dump( struct object *obj, int verbose );
68static void handle_table_destroy( struct object *obj );
69
70static const struct object_ops handle_table_ops =
71{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000072 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 */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000080 no_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000081 no_flush, /* flush */
82 no_get_file_info, /* get_file_info */
83 handle_table_destroy /* destroy */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +000084};
85
86/* dump a handle table */
87static void handle_table_dump( struct object *obj, int verbose )
88{
89 int i;
90 struct handle_table *table = (struct handle_table *)obj;
91 struct handle_entry *entry = table->entries;
92
93 assert( obj->ops == &handle_table_ops );
94
95 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
96 table->last, table->count, table->process );
97 if (!verbose) return;
98 entry = table->entries;
99 for (i = 0; i <= table->last; i++, entry++)
100 {
101 if (!entry->ptr) continue;
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000102 fprintf( stderr, "%9d: %p %08x ", index_to_handle(i), entry->ptr, entry->access );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000103 entry->ptr->ops->dump( entry->ptr, 0 );
104 }
105}
106
107/* destroy a handle table */
108static void handle_table_destroy( struct object *obj )
109{
110 int i;
111 struct handle_table *table = (struct handle_table *)obj;
112 struct handle_entry *entry = table->entries;
113
114 assert( obj->ops == &handle_table_ops );
115
116 for (i = 0; i <= table->last; i++, entry++)
117 {
118 struct object *obj = entry->ptr;
119 entry->ptr = NULL;
120 if (obj) release_object( obj );
121 }
122 free( table->entries );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000123}
124
125/* allocate a new handle table */
126struct object *alloc_handle_table( struct process *process, int count )
127{
128 struct handle_table *table;
129
130 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000131 if (!(table = alloc_object( &handle_table_ops, -1 )))
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000132 return NULL;
133 table->process = process;
134 table->count = count;
135 table->last = -1;
136 table->free = 0;
137 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
138 release_object( table );
139 return NULL;
140}
141
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000142/* grow a handle table */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000143static int grow_handle_table( struct handle_table *table )
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000144{
145 struct handle_entry *new_entries;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000146 int count = table->count;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000147
148 if (count >= INT_MAX / 2) return 0;
149 count *= 2;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000150 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000151 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000152 set_error( STATUS_NO_MEMORY );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000153 return 0;
154 }
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000155 table->entries = new_entries;
156 table->count = count;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000157 return 1;
158}
159
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000160/* allocate the first free entry in the handle table */
161static int alloc_entry( struct handle_table *table, void *obj, unsigned int access )
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000162{
163 struct handle_entry *entry = table->entries + table->free;
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000164 int i;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000165
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000166 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
167 if (i >= table->count)
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000168 {
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000169 if (!grow_handle_table( table )) return -1;
170 entry = table->entries + i; /* the entries may have moved */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000171 }
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000172 table->last = i;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000173 found:
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000174 table->free = i + 1;
175 entry->ptr = grab_object( obj );
176 entry->access = access;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000177 entry->fd = -1;
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000178 return index_to_handle(i);
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000179}
180
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000181/* allocate a handle for an object, incrementing its refcount */
182/* return the handle, or -1 on error */
Alexandre Julliard3da5f841999-05-16 16:54:54 +0000183int alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000184{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000185 struct handle_table *table = (struct handle_table *)process->handles;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000186
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000187 assert( table );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000188 assert( !(access & RESERVED_ALL) );
189 if (inherit) access |= RESERVED_INHERIT;
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000190 return alloc_entry( table, obj, access );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000191}
192
193/* allocate a global handle for an object, incrementing its refcount */
194/* return the handle, or -1 on error */
195static int alloc_global_handle( void *obj, unsigned int access )
196{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000197 int handle;
198
199 if (!global_table)
200 {
201 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 ))) return -1;
202 }
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000203 if ((handle = alloc_entry( global_table, obj, access )) != -1)
204 handle = HANDLE_LOCAL_TO_GLOBAL(handle);
205 return handle;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000206}
207
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000208/* return a handle entry, or NULL if the handle is invalid */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000209static struct handle_entry *get_handle( struct process *process, int handle )
210{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000211 struct handle_table *table = (struct handle_table *)process->handles;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000212 struct handle_entry *entry;
213
214 if (HANDLE_IS_GLOBAL(handle))
215 {
216 handle = HANDLE_GLOBAL_TO_LOCAL(handle);
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000217 table = global_table;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000218 }
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000219 if (!table) goto error;
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000220 handle = handle_to_index( handle );
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000221 if (handle < 0) goto error;
222 if (handle > table->last) goto error;
223 entry = table->entries + handle;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000224 if (!entry->ptr) goto error;
225 return entry;
226
227 error:
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000228 set_error( STATUS_INVALID_HANDLE );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000229 return NULL;
230}
231
232/* attempt to shrink a table */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000233static void shrink_handle_table( struct handle_table *table )
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000234{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000235 struct handle_entry *entry = table->entries + table->last;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000236 struct handle_entry *new_entries;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000237 int count = table->count;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000238
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000239 while (table->last >= 0)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000240 {
241 if (entry->ptr) break;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000242 table->last--;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000243 entry--;
244 }
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000245 if (table->last >= count / 4) return; /* no need to shrink */
246 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000247 count /= 2;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000248 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
249 table->count = count;
250 table->entries = new_entries;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000251}
252
253/* copy the handle table of the parent process */
254/* return 1 if OK, 0 on error */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000255struct object *copy_handle_table( struct process *process, struct process *parent )
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000256{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000257 struct handle_table *parent_table = (struct handle_table *)parent->handles;
258 struct handle_table *table;
259 int i;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000260
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000261 assert( parent_table );
262 assert( parent_table->obj.ops == &handle_table_ops );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000263
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000264 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
265 return NULL;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000266
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000267 if ((table->last = parent_table->last) >= 0)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000268 {
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000269 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++)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000272 {
273 if (!ptr->ptr) continue;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000274 ptr->fd = -1;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000275 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
276 else ptr->ptr = NULL; /* don't inherit this entry */
277 }
278 }
279 /* attempt to shrink the table */
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000280 shrink_handle_table( table );
281 return &table->obj;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000282}
283
284/* close a handle and decrement the refcount of the associated object */
285/* return 1 if OK, 0 on error */
Alexandre Julliardd549f692000-12-22 02:04:15 +0000286int close_handle( struct process *process, int handle, int *fd )
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000287{
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000288 struct handle_table *table;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000289 struct handle_entry *entry;
290 struct object *obj;
291
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000292 if (!(entry = get_handle( process, handle ))) return 0;
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000293 if (entry->access & RESERVED_CLOSE_PROTECT)
294 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000295 set_error( STATUS_INVALID_HANDLE );
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000296 return 0;
297 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000298 obj = entry->ptr;
299 entry->ptr = NULL;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000300 if (fd) *fd = entry->fd;
301 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
302 entry->fd = -1;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000303 table = HANDLE_IS_GLOBAL(handle) ? global_table : (struct handle_table *)process->handles;
304 if (entry < table->entries + table->free) table->free = entry - table->entries;
305 if (entry == table->entries + table->last) shrink_handle_table( table );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000306 release_object( obj );
307 return 1;
308}
309
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000310/* close all the global handles */
311void close_global_handles(void)
312{
313 if (global_table)
314 {
315 release_object( global_table );
316 global_table = NULL;
317 }
318}
319
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000320/* retrieve the object corresponding to one of the magic pseudo-handles */
321static inline struct object *get_magic_handle( int handle )
322{
323 switch(handle)
324 {
325 case 0xfffffffe: /* current thread pseudo-handle */
326 return &current->obj;
327 case 0x7fffffff: /* current process pseudo-handle */
328 case 0xffffffff: /* current process pseudo-handle */
329 return (struct object *)current->process;
330 default:
331 return NULL;
332 }
333}
334
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000335/* retrieve the object corresponding to a handle, incrementing its refcount */
336struct object *get_handle_obj( struct process *process, int handle,
337 unsigned int access, const struct object_ops *ops )
338{
339 struct handle_entry *entry;
340 struct object *obj;
341
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000342 if (!(obj = get_magic_handle( handle )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000343 {
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000344 if (!(entry = get_handle( process, handle ))) return NULL;
345 if ((entry->access & access) != access)
346 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000347 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000348 return NULL;
349 }
350 obj = entry->ptr;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000351 }
352 if (ops && (obj->ops != ops))
353 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000354 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000355 return NULL;
356 }
357 return grab_object( obj );
358}
359
Alexandre Julliardd549f692000-12-22 02:04:15 +0000360/* retrieve the cached fd for a given handle */
361int get_handle_fd( struct process *process, int handle, unsigned int access )
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000362{
363 struct handle_entry *entry;
364
Alexandre Julliardd549f692000-12-22 02:04:15 +0000365 if (HANDLE_IS_GLOBAL(handle)) return -1; /* no fd cache for global handles */
366 if (!(entry = get_handle( process, handle ))) return -1;
367 if ((entry->access & access) != access)
368 {
369 set_error( STATUS_ACCESS_DENIED );
370 return -1;
371 }
372 return entry->fd;
373}
374
375/* get/set the handle reserved flags */
376/* return the old flags (or -1 on error) */
377static int set_handle_info( struct process *process, int handle, int mask, int flags, int *fd )
378{
379 struct handle_entry *entry;
380 unsigned int old_access;
381
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000382 if (get_magic_handle( handle ))
383 {
384 /* we can retrieve but not set info for magic handles */
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000385 if (mask) set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardceeb6931999-11-04 02:33:45 +0000386 return 0;
387 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000388 if (!(entry = get_handle( process, handle ))) return -1;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000389 old_access = entry->access;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000390 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
391 flags = (flags << RESERVED_SHIFT) & mask;
392 entry->access = (entry->access & ~mask) | flags;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000393 /* if no current fd set it, otherwise return current fd */
394 if (entry->fd == -1) entry->fd = *fd;
395 *fd = entry->fd;
396 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000397}
398
399/* duplicate a handle */
400int duplicate_handle( struct process *src, int src_handle, struct process *dst,
401 unsigned int access, int inherit, int options )
402{
403 int res;
Alexandre Julliard0042cb31999-05-29 11:17:25 +0000404 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000405
Alexandre Julliard0042cb31999-05-29 11:17:25 +0000406 if (!obj) return -1;
407 if (options & DUP_HANDLE_SAME_ACCESS)
408 {
409 struct handle_entry *entry = get_handle( src, src_handle );
410 if (entry)
411 access = entry->access;
412 else /* pseudo-handle, give it full access */
413 {
414 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000415 clear_error();
Alexandre Julliard0042cb31999-05-29 11:17:25 +0000416 }
417 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000418 access &= ~RESERVED_ALL;
Alexandre Julliardeb2e77f1999-06-04 19:49:54 +0000419 if (options & DUP_HANDLE_MAKE_GLOBAL)
420 res = alloc_global_handle( obj, access );
421 else
422 res = alloc_handle( dst, obj, access, inherit );
Alexandre Julliard0042cb31999-05-29 11:17:25 +0000423 release_object( obj );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000424 return res;
425}
426
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000427/* open a new handle to an existing object */
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000428int open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000429 unsigned int access, int inherit )
430{
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000431 int handle = -1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000432 struct object *obj = find_object( name, len );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000433 if (obj)
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000434 {
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000435 if (ops && obj->ops != ops)
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000436 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000437 else
438 handle = alloc_handle( current->process, obj, access, inherit );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000439 release_object( obj );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000440 }
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000441 else
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000442 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000443 return handle;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000444}
445
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000446/* close a handle */
447DECL_HANDLER(close_handle)
448{
Alexandre Julliardd549f692000-12-22 02:04:15 +0000449 close_handle( current->process, req->handle, &req->fd );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000450}
451
452/* set a handle information */
453DECL_HANDLER(set_handle_info)
454{
Alexandre Julliardd549f692000-12-22 02:04:15 +0000455 int fd = req->fd;
456
457 if (HANDLE_IS_GLOBAL(req->handle)) fd = -1; /* no fd cache for global handles */
458 req->old_flags = set_handle_info( current->process, req->handle, req->mask, req->flags, &fd );
459 req->cur_fd = fd;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000460}
461
462/* duplicate a handle */
463DECL_HANDLER(dup_handle)
464{
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000465 struct process *src, *dst;
466
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000467 req->handle = -1;
Alexandre Julliardd549f692000-12-22 02:04:15 +0000468 req->fd = -1;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000469 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
470 {
471 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
472 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000473 req->handle = duplicate_handle( src, req->src_handle, NULL,
474 req->access, req->inherit, req->options );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000475 }
476 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
477 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000478 req->handle = duplicate_handle( src, req->src_handle, dst,
479 req->access, req->inherit, req->options );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000480 release_object( dst );
481 }
482 /* close the handle no matter what happened */
483 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
Alexandre Julliardd549f692000-12-22 02:04:15 +0000484 {
485 if (src == current->process) close_handle( src, req->src_handle, &req->fd );
486 else close_handle( src, req->src_handle, NULL );
487 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000488 release_object( src );
489 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000490}