Remove unused include X11/IntrinsicP.h.
[wine/wine64.git] / server / handle.c
blob176249efe76d62936b7d41e6b73f97e7e24a5898
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 "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
41 struct handle_entry
43 struct object *ptr; /* object */
44 unsigned int access; /* access rights */
45 int fd; /* file descriptor (in client process) */
48 struct handle_table
50 struct object obj; /* object header */
51 struct process *process; /* process owning this table */
52 int count; /* number of allocated entries */
53 int last; /* last used entry */
54 int free; /* first entry that may be free */
55 struct handle_entry *entries; /* handle entries */
58 static struct handle_table *global_table;
60 /* reserved handle access rights */
61 #define RESERVED_SHIFT 26
62 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
63 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
64 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
66 #define MIN_HANDLE_ENTRIES 32
69 /* handle to table index conversion */
71 /* handles are a multiple of 4 under NT; handle 0 is not used */
72 inline static obj_handle_t index_to_handle( int index )
74 return (obj_handle_t)((index + 1) << 2);
76 inline static int handle_to_index( obj_handle_t handle )
78 return ((unsigned int)handle >> 2) - 1;
81 /* global handle conversion */
83 #define HANDLE_OBFUSCATOR 0x544a4def
85 inline static int handle_is_global( obj_handle_t handle)
87 return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
89 inline static obj_handle_t handle_local_to_global( obj_handle_t handle )
91 if (!handle) return 0;
92 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
94 inline static obj_handle_t handle_global_to_local( obj_handle_t handle )
96 return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
100 static void handle_table_dump( struct object *obj, int verbose );
101 static void handle_table_destroy( struct object *obj );
103 static const struct object_ops handle_table_ops =
105 sizeof(struct handle_table), /* size */
106 handle_table_dump, /* dump */
107 no_add_queue, /* add_queue */
108 NULL, /* remove_queue */
109 NULL, /* signaled */
110 NULL, /* satisfied */
111 no_signal, /* signal */
112 no_get_fd, /* get_fd */
113 no_lookup_name, /* lookup_name */
114 no_close_handle, /* close_handle */
115 handle_table_destroy /* destroy */
118 /* dump a handle table */
119 static void handle_table_dump( struct object *obj, int verbose )
121 int i;
122 struct handle_table *table = (struct handle_table *)obj;
123 struct handle_entry *entry = table->entries;
125 assert( obj->ops == &handle_table_ops );
127 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
128 table->last, table->count, table->process );
129 if (!verbose) return;
130 entry = table->entries;
131 for (i = 0; i <= table->last; i++, entry++)
133 if (!entry->ptr) continue;
134 fprintf( stderr, " %p: %p %08x ",
135 index_to_handle(i), entry->ptr, entry->access );
136 entry->ptr->ops->dump( entry->ptr, 0 );
140 /* destroy a handle table */
141 static void handle_table_destroy( struct object *obj )
143 int i;
144 struct handle_table *table = (struct handle_table *)obj;
145 struct handle_entry *entry;
147 assert( obj->ops == &handle_table_ops );
149 /* first notify all objects that handles are being closed */
150 if (table->process)
152 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
154 struct object *obj = entry->ptr;
155 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
159 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
161 struct object *obj = entry->ptr;
162 entry->ptr = NULL;
163 if (obj) release_object( obj );
165 free( table->entries );
168 /* allocate a new handle table */
169 struct handle_table *alloc_handle_table( struct process *process, int count )
171 struct handle_table *table;
173 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
174 if (!(table = alloc_object( &handle_table_ops )))
175 return NULL;
176 table->process = process;
177 table->count = count;
178 table->last = -1;
179 table->free = 0;
180 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
181 release_object( table );
182 return NULL;
185 /* grow a handle table */
186 static int grow_handle_table( struct handle_table *table )
188 struct handle_entry *new_entries;
189 int count = table->count;
191 if (count >= INT_MAX / 2) return 0;
192 count *= 2;
193 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
195 set_error( STATUS_NO_MEMORY );
196 return 0;
198 table->entries = new_entries;
199 table->count = count;
200 return 1;
203 /* allocate the first free entry in the handle table */
204 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
206 struct handle_entry *entry = table->entries + table->free;
207 int i;
209 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
210 if (i >= table->count)
212 if (!grow_handle_table( table )) return 0;
213 entry = table->entries + i; /* the entries may have moved */
215 table->last = i;
216 found:
217 table->free = i + 1;
218 entry->ptr = grab_object( obj );
219 entry->access = access;
220 entry->fd = -1;
221 return index_to_handle(i);
224 /* allocate a handle for an object, incrementing its refcount */
225 /* return the handle, or 0 on error */
226 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
228 access &= ~RESERVED_ALL;
229 if (inherit) access |= RESERVED_INHERIT;
230 if (!process->handles)
232 set_error( STATUS_NO_MEMORY );
233 return 0;
235 return alloc_entry( process->handles, obj, access );
238 /* allocate a global handle for an object, incrementing its refcount */
239 /* return the handle, or 0 on error */
240 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
242 if (!global_table)
244 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
245 return 0;
247 return handle_local_to_global( alloc_entry( global_table, obj, access ));
250 /* return a handle entry, or NULL if the handle is invalid */
251 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
253 struct handle_table *table = process->handles;
254 struct handle_entry *entry;
255 int index;
257 if (handle_is_global(handle))
259 handle = handle_global_to_local(handle);
260 table = global_table;
262 if (!table) goto error;
263 index = handle_to_index( handle );
264 if (index < 0) goto error;
265 if (index > table->last) goto error;
266 entry = table->entries + index;
267 if (!entry->ptr) goto error;
268 return entry;
270 error:
271 set_error( STATUS_INVALID_HANDLE );
272 return NULL;
275 /* attempt to shrink a table */
276 static void shrink_handle_table( struct handle_table *table )
278 struct handle_entry *entry = table->entries + table->last;
279 struct handle_entry *new_entries;
280 int count = table->count;
282 while (table->last >= 0)
284 if (entry->ptr) break;
285 table->last--;
286 entry--;
288 if (table->last >= count / 4) return; /* no need to shrink */
289 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
290 count /= 2;
291 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
292 table->count = count;
293 table->entries = new_entries;
296 /* copy the handle table of the parent process */
297 /* return 1 if OK, 0 on error */
298 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
300 struct handle_table *parent_table = parent->handles;
301 struct handle_table *table;
302 int i;
304 assert( parent_table );
305 assert( parent_table->obj.ops == &handle_table_ops );
307 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
308 return NULL;
310 if ((table->last = parent_table->last) >= 0)
312 struct handle_entry *ptr = table->entries;
313 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
314 for (i = 0; i <= table->last; i++, ptr++)
316 if (!ptr->ptr) continue;
317 ptr->fd = -1;
318 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
319 else ptr->ptr = NULL; /* don't inherit this entry */
322 /* attempt to shrink the table */
323 shrink_handle_table( table );
324 return table;
327 /* close a handle and decrement the refcount of the associated object */
328 /* return 1 if OK, 0 on error */
329 int close_handle( struct process *process, obj_handle_t handle, int *fd )
331 struct handle_table *table;
332 struct handle_entry *entry;
333 struct object *obj;
335 if (!(entry = get_handle( process, handle ))) return 0;
336 if (entry->access & RESERVED_CLOSE_PROTECT)
338 set_error( STATUS_HANDLE_NOT_CLOSABLE );
339 return 0;
341 obj = entry->ptr;
342 if (!obj->ops->close_handle( obj, process, handle ))
344 set_error( STATUS_HANDLE_NOT_CLOSABLE );
345 return 0;
347 entry->ptr = NULL;
348 if (fd) *fd = entry->fd;
349 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
350 entry->fd = -1;
351 table = handle_is_global(handle) ? global_table : process->handles;
352 if (entry < table->entries + table->free) table->free = entry - table->entries;
353 if (entry == table->entries + table->last) shrink_handle_table( table );
354 release_object( obj );
355 return 1;
358 /* close all the global handles */
359 void close_global_handles(void)
361 if (global_table)
363 release_object( global_table );
364 global_table = NULL;
368 /* retrieve the object corresponding to one of the magic pseudo-handles */
369 static inline struct object *get_magic_handle( obj_handle_t handle )
371 switch((unsigned long)handle)
373 case 0xfffffffe: /* current thread pseudo-handle */
374 return &current->obj;
375 case 0x7fffffff: /* current process pseudo-handle */
376 case 0xffffffff: /* current process pseudo-handle */
377 return (struct object *)current->process;
378 default:
379 return NULL;
383 /* retrieve the object corresponding to a handle, incrementing its refcount */
384 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
385 unsigned int access, const struct object_ops *ops )
387 struct handle_entry *entry;
388 struct object *obj;
390 if (!(obj = get_magic_handle( handle )))
392 if (!(entry = get_handle( process, handle ))) return NULL;
393 if ((entry->access & access) != access)
395 set_error( STATUS_ACCESS_DENIED );
396 return NULL;
398 obj = entry->ptr;
400 if (ops && (obj->ops != ops))
402 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
403 return NULL;
405 return grab_object( obj );
408 /* retrieve the access rights of a given handle */
409 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
411 struct handle_entry *entry;
413 if (get_magic_handle( handle )) return ~0U; /* magic handles have all access rights */
414 if (!(entry = get_handle( process, handle ))) return 0;
415 return entry->access;
418 /* retrieve the cached fd for a given handle */
419 int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
421 struct handle_entry *entry;
423 if (!(entry = get_handle( process, handle ))) return -1;
424 if ((entry->access & access) != access)
426 set_error( STATUS_ACCESS_DENIED );
427 return -1;
429 return entry->fd;
432 /* set the cached fd for a handle if not set already, and return the current value */
433 int set_handle_unix_fd( struct process *process, obj_handle_t handle, int fd )
435 struct handle_entry *entry;
437 if (handle_is_global( handle )) return -1; /* no fd cache for global handles */
438 if (!(entry = get_handle( process, handle ))) return -1;
439 /* if no current fd set it, otherwise return current fd */
440 if (entry->fd == -1) entry->fd = fd;
441 return entry->fd;
444 /* remove the cached fd and return it */
445 int flush_cached_fd( struct process *process, obj_handle_t handle )
447 struct handle_entry *entry = get_handle( process, handle );
448 int fd = -1;
450 if (entry)
452 fd = entry->fd;
453 entry->fd = -1;
455 return fd;
458 /* find the first inherited handle of the given type */
459 /* this is needed for window stations and desktops (don't ask...) */
460 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
462 struct handle_table *table = process->handles;
463 struct handle_entry *ptr;
464 int i;
466 if (!table) return 0;
468 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
470 if (!ptr->ptr) continue;
471 if (ptr->ptr->ops != ops) continue;
472 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
474 return 0;
477 /* get/set the handle reserved flags */
478 /* return the old flags (or -1 on error) */
479 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
481 struct handle_entry *entry;
482 unsigned int old_access;
484 if (get_magic_handle( handle ))
486 /* we can retrieve but not set info for magic handles */
487 if (mask) set_error( STATUS_ACCESS_DENIED );
488 return 0;
490 if (!(entry = get_handle( process, handle ))) return -1;
491 old_access = entry->access;
492 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
493 flags = (flags << RESERVED_SHIFT) & mask;
494 entry->access = (entry->access & ~mask) | flags;
495 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
498 /* duplicate a handle */
499 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
500 unsigned int access, int inherit, int options )
502 obj_handle_t res;
503 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
505 if (!obj) return 0;
506 if (options & DUP_HANDLE_SAME_ACCESS)
508 struct handle_entry *entry = get_handle( src, src_handle );
509 if (entry)
510 access = entry->access;
511 else /* pseudo-handle, give it full access */
513 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
514 clear_error();
517 access &= ~RESERVED_ALL;
518 if (options & DUP_HANDLE_MAKE_GLOBAL)
519 res = alloc_global_handle( obj, access );
520 else
521 res = alloc_handle( dst, obj, access, inherit );
522 release_object( obj );
523 return res;
526 /* open a new handle to an existing object */
527 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
528 const struct object_ops *ops, unsigned int access, unsigned int attr )
530 obj_handle_t handle = 0;
531 struct object *obj = find_object( namespace, name, attr );
532 if (obj)
534 if (ops && obj->ops != ops)
535 set_error( STATUS_OBJECT_TYPE_MISMATCH );
536 else
537 handle = alloc_handle( current->process, obj, access, attr & OBJ_INHERIT );
538 release_object( obj );
540 else
541 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
542 return handle;
545 /* return the size of the handle table of a given process */
546 unsigned int get_handle_table_count( struct process *process )
548 if (!process->handles) return 0;
549 return process->handles->count;
552 /* close a handle */
553 DECL_HANDLER(close_handle)
555 close_handle( current->process, req->handle, &reply->fd );
558 /* set a handle information */
559 DECL_HANDLER(set_handle_info)
561 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
564 /* duplicate a handle */
565 DECL_HANDLER(dup_handle)
567 struct process *src, *dst;
569 reply->handle = 0;
570 reply->fd = -1;
571 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
573 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
575 reply->handle = duplicate_handle( src, req->src_handle, NULL,
576 req->access, req->inherit, req->options );
578 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
580 reply->handle = duplicate_handle( src, req->src_handle, dst,
581 req->access, req->inherit, req->options );
582 release_object( dst );
584 /* close the handle no matter what happened */
585 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
587 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
588 else close_handle( src, req->src_handle, NULL );
590 release_object( src );