msvcp110: Add tr2_sys__Read_dir implementation.
[wine.git] / server / handle.c
blob5043ff70b7117ead702e3653d0e92834d1e8e86f
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 "security.h"
40 #include "request.h"
42 struct handle_entry
44 struct object *ptr; /* object */
45 unsigned int access; /* access rights */
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
67 #define MAX_HANDLE_ENTRIES 0x00ffffff
70 /* handle to table index conversion */
72 /* handles are a multiple of 4 under NT; handle 0 is not used */
73 static inline obj_handle_t index_to_handle( int index )
75 return (obj_handle_t)((index + 1) << 2);
77 static inline int handle_to_index( obj_handle_t handle )
79 return (handle >> 2) - 1;
82 /* global handle conversion */
84 #define HANDLE_OBFUSCATOR 0x544a4def
86 static inline int handle_is_global( obj_handle_t handle)
88 return (handle ^ HANDLE_OBFUSCATOR) <= (MAX_HANDLE_ENTRIES << 2);
90 static inline obj_handle_t handle_local_to_global( obj_handle_t handle )
92 if (!handle) return 0;
93 return handle ^ HANDLE_OBFUSCATOR;
95 static inline obj_handle_t handle_global_to_local( obj_handle_t handle )
97 return handle ^ HANDLE_OBFUSCATOR;
100 /* grab an object and increment its handle count */
101 static struct object *grab_object_for_handle( struct object *obj )
103 obj->handle_count++;
104 return grab_object( obj );
107 /* release an object and decrement its handle count */
108 static void release_object_from_handle( struct object *obj )
110 assert( obj->handle_count );
111 obj->handle_count--;
112 release_object( obj );
115 static void handle_table_dump( struct object *obj, int verbose );
116 static void handle_table_destroy( struct object *obj );
118 static const struct object_ops handle_table_ops =
120 sizeof(struct handle_table), /* size */
121 handle_table_dump, /* dump */
122 no_get_type, /* get_type */
123 no_add_queue, /* add_queue */
124 NULL, /* remove_queue */
125 NULL, /* signaled */
126 NULL, /* satisfied */
127 no_signal, /* signal */
128 no_get_fd, /* get_fd */
129 no_map_access, /* map_access */
130 default_get_sd, /* get_sd */
131 default_set_sd, /* set_sd */
132 no_lookup_name, /* lookup_name */
133 no_open_file, /* open_file */
134 no_close_handle, /* close_handle */
135 handle_table_destroy /* destroy */
138 /* dump a handle table */
139 static void handle_table_dump( struct object *obj, int verbose )
141 int i;
142 struct handle_table *table = (struct handle_table *)obj;
143 struct handle_entry *entry;
145 assert( obj->ops == &handle_table_ops );
147 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
148 table->last, table->count, table->process );
149 if (!verbose) return;
150 entry = table->entries;
151 for (i = 0; i <= table->last; i++, entry++)
153 if (!entry->ptr) continue;
154 fprintf( stderr, " %04x: %p %08x ",
155 index_to_handle(i), entry->ptr, entry->access );
156 entry->ptr->ops->dump( entry->ptr, 0 );
160 /* destroy a handle table */
161 static void handle_table_destroy( struct object *obj )
163 int i;
164 struct handle_table *table = (struct handle_table *)obj;
165 struct handle_entry *entry;
167 assert( obj->ops == &handle_table_ops );
169 /* first notify all objects that handles are being closed */
170 if (table->process)
172 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
174 struct object *obj = entry->ptr;
175 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
179 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
181 struct object *obj = entry->ptr;
182 entry->ptr = NULL;
183 if (obj) release_object_from_handle( obj );
185 free( table->entries );
188 /* close all the process handles and free the handle table */
189 void close_process_handles( struct process *process )
191 struct handle_table *table = process->handles;
193 process->handles = NULL;
194 if (table) release_object( table );
197 /* allocate a new handle table */
198 struct handle_table *alloc_handle_table( struct process *process, int count )
200 struct handle_table *table;
202 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
203 if (!(table = alloc_object( &handle_table_ops )))
204 return NULL;
205 table->process = process;
206 table->count = count;
207 table->last = -1;
208 table->free = 0;
209 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
210 release_object( table );
211 return NULL;
214 /* grow a handle table */
215 static int grow_handle_table( struct handle_table *table )
217 struct handle_entry *new_entries;
218 int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
220 if (count == table->count ||
221 !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
223 set_error( STATUS_INSUFFICIENT_RESOURCES );
224 return 0;
226 table->entries = new_entries;
227 table->count = count;
228 return 1;
231 /* allocate the first free entry in the handle table */
232 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
234 struct handle_entry *entry = table->entries + table->free;
235 int i;
237 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
238 if (i >= table->count)
240 if (!grow_handle_table( table )) return 0;
241 entry = table->entries + i; /* the entries may have moved */
243 table->last = i;
244 found:
245 table->free = i + 1;
246 entry->ptr = grab_object_for_handle( obj );
247 entry->access = access;
248 return index_to_handle(i);
251 /* allocate a handle for an object, incrementing its refcount */
252 static obj_handle_t alloc_handle_entry( struct process *process, void *ptr,
253 unsigned int access, unsigned int attr )
255 struct object *obj = ptr;
257 assert( !(access & RESERVED_ALL) );
258 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
259 if (!process->handles)
261 set_error( STATUS_PROCESS_IS_TERMINATING );
262 return 0;
264 return alloc_entry( process->handles, obj, access );
267 /* allocate a handle for an object, incrementing its refcount */
268 /* return the handle, or 0 on error */
269 obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
271 struct object *obj = ptr;
272 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
273 return alloc_handle_entry( process, ptr, access, attr );
276 /* allocate a handle for an object, checking the dacl allows the process to */
277 /* access it and incrementing its refcount */
278 /* return the handle, or 0 on error */
279 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
281 struct object *obj = ptr;
282 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
283 if (access && !check_object_access( obj, &access )) return 0;
284 return alloc_handle_entry( process, ptr, access, attr );
287 /* allocate a global handle for an object, incrementing its refcount */
288 /* return the handle, or 0 on error */
289 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
291 if (!global_table)
293 if (!(global_table = alloc_handle_table( NULL, 0 )))
294 return 0;
295 make_object_static( &global_table->obj );
297 return handle_local_to_global( alloc_entry( global_table, obj, access ));
300 /* allocate a global handle for an object, checking the dacl allows the */
301 /* process to access it and incrementing its refcount and incrementing its refcount */
302 /* return the handle, or 0 on error */
303 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
305 if (access && !check_object_access( obj, &access )) return 0;
306 return alloc_global_handle_no_access_check( obj, access );
309 /* return a handle entry, or NULL if the handle is invalid */
310 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
312 struct handle_table *table = process->handles;
313 struct handle_entry *entry;
314 int index;
316 if (handle_is_global(handle))
318 handle = handle_global_to_local(handle);
319 table = global_table;
321 if (!table) return NULL;
322 index = handle_to_index( handle );
323 if (index < 0) return NULL;
324 if (index > table->last) return NULL;
325 entry = table->entries + index;
326 if (!entry->ptr) return NULL;
327 return entry;
330 /* attempt to shrink a table */
331 static void shrink_handle_table( struct handle_table *table )
333 struct handle_entry *entry = table->entries + table->last;
334 struct handle_entry *new_entries;
335 int count = table->count;
337 while (table->last >= 0)
339 if (entry->ptr) break;
340 table->last--;
341 entry--;
343 if (table->last >= count / 4) return; /* no need to shrink */
344 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
345 count /= 2;
346 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
347 table->count = count;
348 table->entries = new_entries;
351 /* copy the handle table of the parent process */
352 /* return 1 if OK, 0 on error */
353 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
355 struct handle_table *parent_table = parent->handles;
356 struct handle_table *table;
357 int i;
359 assert( parent_table );
360 assert( parent_table->obj.ops == &handle_table_ops );
362 if (!(table = alloc_handle_table( process, parent_table->count )))
363 return NULL;
365 if ((table->last = parent_table->last) >= 0)
367 struct handle_entry *ptr = table->entries;
368 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
369 for (i = 0; i <= table->last; i++, ptr++)
371 if (!ptr->ptr) continue;
372 if (ptr->access & RESERVED_INHERIT) grab_object_for_handle( ptr->ptr );
373 else ptr->ptr = NULL; /* don't inherit this entry */
376 /* attempt to shrink the table */
377 shrink_handle_table( table );
378 return table;
381 /* close a handle and decrement the refcount of the associated object */
382 unsigned int close_handle( struct process *process, obj_handle_t handle )
384 struct handle_table *table;
385 struct handle_entry *entry;
386 struct object *obj;
388 if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
389 if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
390 obj = entry->ptr;
391 if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
392 entry->ptr = NULL;
393 table = handle_is_global(handle) ? global_table : process->handles;
394 if (entry < table->entries + table->free) table->free = entry - table->entries;
395 if (entry == table->entries + table->last) shrink_handle_table( table );
396 release_object_from_handle( obj );
397 return STATUS_SUCCESS;
400 /* retrieve the object corresponding to one of the magic pseudo-handles */
401 static inline struct object *get_magic_handle( obj_handle_t handle )
403 switch(handle)
405 case 0xfffffffe: /* current thread pseudo-handle */
406 return &current->obj;
407 case 0x7fffffff: /* current process pseudo-handle */
408 case 0xffffffff: /* current process pseudo-handle */
409 return (struct object *)current->process;
410 default:
411 return NULL;
415 /* retrieve the object corresponding to a handle, incrementing its refcount */
416 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
417 unsigned int access, const struct object_ops *ops )
419 struct handle_entry *entry;
420 struct object *obj;
422 if (!(obj = get_magic_handle( handle )))
424 if (!(entry = get_handle( process, handle )))
426 set_error( STATUS_INVALID_HANDLE );
427 return NULL;
429 obj = entry->ptr;
430 if (ops && (obj->ops != ops))
432 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
433 return NULL;
435 if ((entry->access & access) != access)
437 set_error( STATUS_ACCESS_DENIED );
438 return NULL;
441 else if (ops && (obj->ops != ops))
443 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
444 return NULL;
446 return grab_object( obj );
449 /* retrieve the access rights of a given handle */
450 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
452 struct handle_entry *entry;
454 if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
455 if (!(entry = get_handle( process, handle ))) return 0;
456 return entry->access & ~RESERVED_ALL;
459 /* find the first inherited handle of the given type */
460 /* this is needed for window stations and desktops (don't ask...) */
461 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
463 struct handle_table *table = process->handles;
464 struct handle_entry *ptr;
465 int i;
467 if (!table) return 0;
469 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
471 if (!ptr->ptr) continue;
472 if (ptr->ptr->ops != ops) continue;
473 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
475 return 0;
478 /* enumerate handles of a given type */
479 /* this is needed for window stations and desktops */
480 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
481 unsigned int *index )
483 struct handle_table *table = process->handles;
484 unsigned int i;
485 struct handle_entry *entry;
487 if (!table) return 0;
489 for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
491 if (!entry->ptr) continue;
492 if (entry->ptr->ops != ops) continue;
493 *index = i + 1;
494 return index_to_handle(i);
496 return 0;
499 /* get/set the handle reserved flags */
500 /* return the old flags (or -1 on error) */
501 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
503 struct handle_entry *entry;
504 unsigned int old_access;
506 if (get_magic_handle( handle ))
508 /* we can retrieve but not set info for magic handles */
509 if (mask) set_error( STATUS_ACCESS_DENIED );
510 return 0;
512 if (!(entry = get_handle( process, handle )))
514 set_error( STATUS_INVALID_HANDLE );
515 return -1;
517 old_access = entry->access;
518 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
519 flags = (flags << RESERVED_SHIFT) & mask;
520 entry->access = (entry->access & ~mask) | flags;
521 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
524 /* duplicate a handle */
525 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
526 unsigned int access, unsigned int attr, unsigned int options )
528 obj_handle_t res;
529 struct handle_entry *entry;
530 unsigned int src_access;
531 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
533 if (!obj) return 0;
534 if ((entry = get_handle( src, src_handle )))
535 src_access = entry->access;
536 else /* pseudo-handle, give it full access */
537 src_access = obj->ops->map_access( obj, GENERIC_ALL );
538 src_access &= ~RESERVED_ALL;
540 if (options & DUP_HANDLE_SAME_ACCESS)
541 access = src_access;
542 else
543 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
545 /* asking for the more access rights than src_access? */
546 if (access & ~src_access)
548 if (options & DUP_HANDLE_MAKE_GLOBAL)
549 res = alloc_global_handle( obj, access );
550 else
551 res = alloc_handle( dst, obj, access, attr );
553 else
555 if (options & DUP_HANDLE_MAKE_GLOBAL)
556 res = alloc_global_handle_no_access_check( obj, access );
557 else if ((options & DUP_HANDLE_CLOSE_SOURCE) && src == dst &&
558 entry && !(entry->access & RESERVED_CLOSE_PROTECT))
560 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
561 entry->access = access;
562 res = src_handle;
564 else
565 res = alloc_handle_entry( dst, obj, access, attr );
568 release_object( obj );
569 return res;
572 /* open a new handle to an existing object */
573 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
574 const struct object_ops *ops, unsigned int access, unsigned int attr )
576 obj_handle_t handle = 0;
577 struct object *obj = find_object( namespace, name, attr );
578 if (obj)
580 if (ops && obj->ops != ops)
581 set_error( STATUS_OBJECT_TYPE_MISMATCH );
582 else
583 handle = alloc_handle( current->process, obj, access, attr );
584 release_object( obj );
586 else
587 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
588 return handle;
591 /* return the size of the handle table of a given process */
592 unsigned int get_handle_table_count( struct process *process )
594 if (!process->handles) return 0;
595 return process->handles->count;
598 /* close a handle */
599 DECL_HANDLER(close_handle)
601 unsigned int err = close_handle( current->process, req->handle );
602 set_error( err );
605 /* set a handle information */
606 DECL_HANDLER(set_handle_info)
608 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
611 /* duplicate a handle */
612 DECL_HANDLER(dup_handle)
614 struct process *src, *dst = NULL;
616 reply->handle = 0;
617 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
619 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
621 reply->handle = duplicate_handle( src, req->src_handle, NULL,
622 req->access, req->attributes, req->options );
624 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
626 reply->handle = duplicate_handle( src, req->src_handle, dst,
627 req->access, req->attributes, req->options );
628 release_object( dst );
630 /* close the handle no matter what happened */
631 if ((req->options & DUP_HANDLE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
632 reply->closed = !close_handle( src, req->src_handle );
633 reply->self = (src == current->process);
634 release_object( src );
638 DECL_HANDLER(get_object_info)
640 struct object *obj;
641 WCHAR *name;
643 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
645 reply->access = get_handle_access( current->process, req->handle );
646 reply->ref_count = obj->refcount;
647 reply->handle_count = obj->handle_count;
648 if ((name = get_object_full_name( obj, &reply->total )))
649 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
650 release_object( obj );
653 DECL_HANDLER(set_security_object)
655 data_size_t sd_size = get_req_data_size();
656 const struct security_descriptor *sd = get_req_data();
657 struct object *obj;
658 unsigned int access = 0;
660 if (!sd_is_valid( sd, sd_size ))
662 set_error( STATUS_ACCESS_VIOLATION );
663 return;
666 if (req->security_info & OWNER_SECURITY_INFORMATION ||
667 req->security_info & GROUP_SECURITY_INFORMATION)
668 access |= WRITE_OWNER;
669 if (req->security_info & SACL_SECURITY_INFORMATION)
670 access |= ACCESS_SYSTEM_SECURITY;
671 if (req->security_info & DACL_SECURITY_INFORMATION)
672 access |= WRITE_DAC;
674 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
676 obj->ops->set_sd( obj, sd, req->security_info );
677 release_object( obj );
680 DECL_HANDLER(get_security_object)
682 const struct security_descriptor *sd;
683 struct object *obj;
684 unsigned int access = READ_CONTROL;
685 struct security_descriptor req_sd;
686 int present;
687 const SID *owner, *group;
688 const ACL *sacl, *dacl;
690 if (req->security_info & SACL_SECURITY_INFORMATION)
691 access |= ACCESS_SYSTEM_SECURITY;
693 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
695 sd = obj->ops->get_sd( obj );
696 if (sd)
698 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
700 owner = sd_get_owner( sd );
701 if (req->security_info & OWNER_SECURITY_INFORMATION)
702 req_sd.owner_len = sd->owner_len;
703 else
704 req_sd.owner_len = 0;
706 group = sd_get_group( sd );
707 if (req->security_info & GROUP_SECURITY_INFORMATION)
708 req_sd.group_len = sd->group_len;
709 else
710 req_sd.group_len = 0;
712 req_sd.control |= SE_SACL_PRESENT;
713 sacl = sd_get_sacl( sd, &present );
714 if (req->security_info & SACL_SECURITY_INFORMATION && present)
715 req_sd.sacl_len = sd->sacl_len;
716 else
717 req_sd.sacl_len = 0;
719 req_sd.control |= SE_DACL_PRESENT;
720 dacl = sd_get_dacl( sd, &present );
721 if (req->security_info & DACL_SECURITY_INFORMATION && present)
722 req_sd.dacl_len = sd->dacl_len;
723 else
724 req_sd.dacl_len = 0;
726 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
727 req_sd.sacl_len + req_sd.dacl_len;
728 if (reply->sd_len <= get_reply_max_size())
730 char *ptr = set_reply_data_size(reply->sd_len);
732 memcpy( ptr, &req_sd, sizeof(req_sd) );
733 ptr += sizeof(req_sd);
734 memcpy( ptr, owner, req_sd.owner_len );
735 ptr += req_sd.owner_len;
736 memcpy( ptr, group, req_sd.group_len );
737 ptr += req_sd.group_len;
738 memcpy( ptr, sacl, req_sd.sacl_len );
739 ptr += req_sd.sacl_len;
740 memcpy( ptr, dacl, req_sd.dacl_len );
742 else
743 set_error(STATUS_BUFFER_TOO_SMALL);
746 release_object( obj );