include: Use force_align_arg_pointer on MacOS to fix the stack on entry to Wine.
[wine/wine64.git] / server / handle.c
blobe1e82e5b4f2976568270fd630b4b8d929ca63d33
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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_map_access, /* map_access */
114 no_lookup_name, /* lookup_name */
115 no_close_handle, /* close_handle */
116 handle_table_destroy /* destroy */
119 /* dump a handle table */
120 static void handle_table_dump( struct object *obj, int verbose )
122 int i;
123 struct handle_table *table = (struct handle_table *)obj;
124 struct handle_entry *entry = table->entries;
126 assert( obj->ops == &handle_table_ops );
128 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
129 table->last, table->count, table->process );
130 if (!verbose) return;
131 entry = table->entries;
132 for (i = 0; i <= table->last; i++, entry++)
134 if (!entry->ptr) continue;
135 fprintf( stderr, " %p: %p %08x ",
136 index_to_handle(i), entry->ptr, entry->access );
137 entry->ptr->ops->dump( entry->ptr, 0 );
141 /* destroy a handle table */
142 static void handle_table_destroy( struct object *obj )
144 int i;
145 struct handle_table *table = (struct handle_table *)obj;
146 struct handle_entry *entry;
148 assert( obj->ops == &handle_table_ops );
150 /* first notify all objects that handles are being closed */
151 if (table->process)
153 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
155 struct object *obj = entry->ptr;
156 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
160 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
162 struct object *obj = entry->ptr;
163 entry->ptr = NULL;
164 if (obj) release_object( obj );
166 free( table->entries );
169 /* allocate a new handle table */
170 struct handle_table *alloc_handle_table( struct process *process, int count )
172 struct handle_table *table;
174 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
175 if (!(table = alloc_object( &handle_table_ops )))
176 return NULL;
177 table->process = process;
178 table->count = count;
179 table->last = -1;
180 table->free = 0;
181 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
182 release_object( table );
183 return NULL;
186 /* grow a handle table */
187 static int grow_handle_table( struct handle_table *table )
189 struct handle_entry *new_entries;
190 int count = table->count;
192 if (count >= INT_MAX / 2) return 0;
193 count *= 2;
194 if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
196 set_error( STATUS_NO_MEMORY );
197 return 0;
199 table->entries = new_entries;
200 table->count = count;
201 return 1;
204 /* allocate the first free entry in the handle table */
205 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
207 struct handle_entry *entry = table->entries + table->free;
208 int i;
210 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
211 if (i >= table->count)
213 if (!grow_handle_table( table )) return 0;
214 entry = table->entries + i; /* the entries may have moved */
216 table->last = i;
217 found:
218 table->free = i + 1;
219 entry->ptr = grab_object( obj );
220 entry->access = access;
221 entry->fd = -1;
222 return index_to_handle(i);
225 /* allocate a handle for an object, incrementing its refcount */
226 /* return the handle, or 0 on error */
227 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
229 struct object *obj = ptr;
231 access = obj->ops->map_access( obj, access );
232 access &= ~RESERVED_ALL;
233 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
234 if (!process->handles)
236 set_error( STATUS_NO_MEMORY );
237 return 0;
239 return alloc_entry( process->handles, obj, access );
242 /* allocate a global handle for an object, incrementing its refcount */
243 /* return the handle, or 0 on error */
244 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
246 if (!global_table)
248 if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
249 return 0;
250 make_object_static( &global_table->obj );
252 return handle_local_to_global( alloc_entry( global_table, obj, access ));
255 /* return a handle entry, or NULL if the handle is invalid */
256 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
258 struct handle_table *table = process->handles;
259 struct handle_entry *entry;
260 int index;
262 if (handle_is_global(handle))
264 handle = handle_global_to_local(handle);
265 table = global_table;
267 if (!table) goto error;
268 index = handle_to_index( handle );
269 if (index < 0) goto error;
270 if (index > table->last) goto error;
271 entry = table->entries + index;
272 if (!entry->ptr) goto error;
273 return entry;
275 error:
276 set_error( STATUS_INVALID_HANDLE );
277 return NULL;
280 /* attempt to shrink a table */
281 static void shrink_handle_table( struct handle_table *table )
283 struct handle_entry *entry = table->entries + table->last;
284 struct handle_entry *new_entries;
285 int count = table->count;
287 while (table->last >= 0)
289 if (entry->ptr) break;
290 table->last--;
291 entry--;
293 if (table->last >= count / 4) return; /* no need to shrink */
294 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
295 count /= 2;
296 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
297 table->count = count;
298 table->entries = new_entries;
301 /* copy the handle table of the parent process */
302 /* return 1 if OK, 0 on error */
303 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
305 struct handle_table *parent_table = parent->handles;
306 struct handle_table *table;
307 int i;
309 assert( parent_table );
310 assert( parent_table->obj.ops == &handle_table_ops );
312 if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
313 return NULL;
315 if ((table->last = parent_table->last) >= 0)
317 struct handle_entry *ptr = table->entries;
318 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
319 for (i = 0; i <= table->last; i++, ptr++)
321 if (!ptr->ptr) continue;
322 ptr->fd = -1;
323 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
324 else ptr->ptr = NULL; /* don't inherit this entry */
327 /* attempt to shrink the table */
328 shrink_handle_table( table );
329 return table;
332 /* close a handle and decrement the refcount of the associated object */
333 /* return 1 if OK, 0 on error */
334 int close_handle( struct process *process, obj_handle_t handle, int *fd )
336 struct handle_table *table;
337 struct handle_entry *entry;
338 struct object *obj;
340 if (!(entry = get_handle( process, handle ))) return 0;
341 if (entry->access & RESERVED_CLOSE_PROTECT)
343 set_error( STATUS_HANDLE_NOT_CLOSABLE );
344 return 0;
346 obj = entry->ptr;
347 if (!obj->ops->close_handle( obj, process, handle ))
349 set_error( STATUS_HANDLE_NOT_CLOSABLE );
350 return 0;
352 entry->ptr = NULL;
353 if (fd) *fd = entry->fd;
354 else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */
355 entry->fd = -1;
356 table = handle_is_global(handle) ? global_table : process->handles;
357 if (entry < table->entries + table->free) table->free = entry - table->entries;
358 if (entry == table->entries + table->last) shrink_handle_table( table );
359 release_object( obj );
360 return 1;
363 /* retrieve the object corresponding to one of the magic pseudo-handles */
364 static inline struct object *get_magic_handle( obj_handle_t handle )
366 switch((unsigned long)handle)
368 case 0xfffffffe: /* current thread pseudo-handle */
369 return &current->obj;
370 case 0x7fffffff: /* current process pseudo-handle */
371 case 0xffffffff: /* current process pseudo-handle */
372 return (struct object *)current->process;
373 default:
374 return NULL;
378 /* retrieve the object corresponding to a handle, incrementing its refcount */
379 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
380 unsigned int access, const struct object_ops *ops )
382 struct handle_entry *entry;
383 struct object *obj;
385 if (!(obj = get_magic_handle( handle )))
387 if (!(entry = get_handle( process, handle ))) return NULL;
388 if ((entry->access & access) != access)
390 set_error( STATUS_ACCESS_DENIED );
391 return NULL;
393 obj = entry->ptr;
395 if (ops && (obj->ops != ops))
397 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
398 return NULL;
400 return grab_object( obj );
403 /* retrieve the access rights of a given handle */
404 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
406 struct handle_entry *entry;
408 if (get_magic_handle( handle )) return ~0U; /* magic handles have all access rights */
409 if (!(entry = get_handle( process, handle ))) return 0;
410 return entry->access;
413 /* retrieve the cached fd for a given handle */
414 int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
416 struct handle_entry *entry;
418 if (!(entry = get_handle( process, handle ))) return -1;
419 if ((entry->access & access) != access)
421 set_error( STATUS_ACCESS_DENIED );
422 return -1;
424 return entry->fd;
427 /* set the cached fd for a handle if not set already, and return the current value */
428 int set_handle_unix_fd( struct process *process, obj_handle_t handle, int fd )
430 struct handle_entry *entry;
432 if (handle_is_global( handle )) return -1; /* no fd cache for global handles */
433 if (!(entry = get_handle( process, handle ))) return -1;
434 /* if no current fd set it, otherwise return current fd */
435 if (entry->fd == -1) entry->fd = fd;
436 return entry->fd;
439 /* remove the cached fd and return it */
440 int flush_cached_fd( struct process *process, obj_handle_t handle )
442 struct handle_entry *entry = get_handle( process, handle );
443 int fd = -1;
445 if (entry)
447 fd = entry->fd;
448 entry->fd = -1;
450 return fd;
453 /* find the first inherited handle of the given type */
454 /* this is needed for window stations and desktops (don't ask...) */
455 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
457 struct handle_table *table = process->handles;
458 struct handle_entry *ptr;
459 int i;
461 if (!table) return 0;
463 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
465 if (!ptr->ptr) continue;
466 if (ptr->ptr->ops != ops) continue;
467 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
469 return 0;
472 /* get/set the handle reserved flags */
473 /* return the old flags (or -1 on error) */
474 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
476 struct handle_entry *entry;
477 unsigned int old_access;
479 if (get_magic_handle( handle ))
481 /* we can retrieve but not set info for magic handles */
482 if (mask) set_error( STATUS_ACCESS_DENIED );
483 return 0;
485 if (!(entry = get_handle( process, handle ))) return -1;
486 old_access = entry->access;
487 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
488 flags = (flags << RESERVED_SHIFT) & mask;
489 entry->access = (entry->access & ~mask) | flags;
490 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
493 /* duplicate a handle */
494 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
495 unsigned int access, unsigned int attr, unsigned int options )
497 obj_handle_t res;
498 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
500 if (!obj) return 0;
501 if (options & DUP_HANDLE_SAME_ACCESS)
503 struct handle_entry *entry = get_handle( src, src_handle );
504 if (entry)
505 access = entry->access;
506 else /* pseudo-handle, give it full access */
508 access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
509 clear_error();
512 access &= ~RESERVED_ALL;
513 if (options & DUP_HANDLE_MAKE_GLOBAL)
514 res = alloc_global_handle( obj, access );
515 else
516 res = alloc_handle( dst, obj, access, attr );
517 release_object( obj );
518 return res;
521 /* open a new handle to an existing object */
522 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
523 const struct object_ops *ops, unsigned int access, unsigned int attr )
525 obj_handle_t handle = 0;
526 struct object *obj = find_object( namespace, name, attr );
527 if (obj)
529 if (ops && obj->ops != ops)
530 set_error( STATUS_OBJECT_TYPE_MISMATCH );
531 else
532 handle = alloc_handle( current->process, obj, access, attr );
533 release_object( obj );
535 else
536 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
537 return handle;
540 /* return the size of the handle table of a given process */
541 unsigned int get_handle_table_count( struct process *process )
543 if (!process->handles) return 0;
544 return process->handles->count;
547 /* close a handle */
548 DECL_HANDLER(close_handle)
550 close_handle( current->process, req->handle, &reply->fd );
553 /* set a handle information */
554 DECL_HANDLER(set_handle_info)
556 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
559 /* duplicate a handle */
560 DECL_HANDLER(dup_handle)
562 struct process *src, *dst;
564 reply->handle = 0;
565 reply->fd = -1;
566 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
568 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
570 reply->handle = duplicate_handle( src, req->src_handle, NULL,
571 req->access, req->attributes, req->options );
573 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
575 reply->handle = duplicate_handle( src, req->src_handle, dst,
576 req->access, req->attributes, req->options );
577 release_object( dst );
579 /* close the handle no matter what happened */
580 if (req->options & DUP_HANDLE_CLOSE_SOURCE)
582 if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
583 else close_handle( src, req->src_handle, NULL );
585 release_object( src );