msvcrt/tests: Don't pass path containing spaces to _spawnvp.
[wine/multimedia.git] / server / handle.c
blob0293ed6f99b66c6e5e2571bdd1b636b3c87e3cbe
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;
101 static void handle_table_dump( struct object *obj, int verbose );
102 static void handle_table_destroy( struct object *obj );
104 static const struct object_ops handle_table_ops =
106 sizeof(struct handle_table), /* size */
107 handle_table_dump, /* dump */
108 no_get_type, /* get_type */
109 no_add_queue, /* add_queue */
110 NULL, /* remove_queue */
111 NULL, /* signaled */
112 NULL, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 no_map_access, /* map_access */
116 default_get_sd, /* get_sd */
117 default_set_sd, /* set_sd */
118 no_lookup_name, /* lookup_name */
119 no_open_file, /* open_file */
120 no_close_handle, /* close_handle */
121 handle_table_destroy /* destroy */
124 /* dump a handle table */
125 static void handle_table_dump( struct object *obj, int verbose )
127 int i;
128 struct handle_table *table = (struct handle_table *)obj;
129 struct handle_entry *entry;
131 assert( obj->ops == &handle_table_ops );
133 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
134 table->last, table->count, table->process );
135 if (!verbose) return;
136 entry = table->entries;
137 for (i = 0; i <= table->last; i++, entry++)
139 if (!entry->ptr) continue;
140 fprintf( stderr, " %04x: %p %08x ",
141 index_to_handle(i), entry->ptr, entry->access );
142 entry->ptr->ops->dump( entry->ptr, 0 );
146 /* destroy a handle table */
147 static void handle_table_destroy( struct object *obj )
149 int i;
150 struct handle_table *table = (struct handle_table *)obj;
151 struct handle_entry *entry;
153 assert( obj->ops == &handle_table_ops );
155 /* first notify all objects that handles are being closed */
156 if (table->process)
158 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
160 struct object *obj = entry->ptr;
161 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
165 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
167 struct object *obj = entry->ptr;
168 entry->ptr = NULL;
169 if (obj) release_object( obj );
171 free( table->entries );
174 /* close all the process handles and free the handle table */
175 void close_process_handles( struct process *process )
177 struct handle_table *table = process->handles;
179 process->handles = NULL;
180 if (table) release_object( table );
183 /* allocate a new handle table */
184 struct handle_table *alloc_handle_table( struct process *process, int count )
186 struct handle_table *table;
188 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
189 if (!(table = alloc_object( &handle_table_ops )))
190 return NULL;
191 table->process = process;
192 table->count = count;
193 table->last = -1;
194 table->free = 0;
195 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
196 release_object( table );
197 return NULL;
200 /* grow a handle table */
201 static int grow_handle_table( struct handle_table *table )
203 struct handle_entry *new_entries;
204 int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
206 if (count == table->count ||
207 !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
209 set_error( STATUS_INSUFFICIENT_RESOURCES );
210 return 0;
212 table->entries = new_entries;
213 table->count = count;
214 return 1;
217 /* allocate the first free entry in the handle table */
218 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
220 struct handle_entry *entry = table->entries + table->free;
221 int i;
223 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
224 if (i >= table->count)
226 if (!grow_handle_table( table )) return 0;
227 entry = table->entries + i; /* the entries may have moved */
229 table->last = i;
230 found:
231 table->free = i + 1;
232 entry->ptr = grab_object( obj );
233 entry->access = access;
234 return index_to_handle(i);
237 /* allocate a handle for an object, incrementing its refcount */
238 static obj_handle_t alloc_handle_entry( struct process *process, void *ptr,
239 unsigned int access, unsigned int attr )
241 struct object *obj = ptr;
243 assert( !(access & RESERVED_ALL) );
244 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
245 if (!process->handles)
247 set_error( STATUS_PROCESS_IS_TERMINATING );
248 return 0;
250 return alloc_entry( process->handles, obj, access );
253 /* allocate a handle for an object, incrementing its refcount */
254 /* return the handle, or 0 on error */
255 obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
257 struct object *obj = ptr;
258 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
259 return alloc_handle_entry( process, ptr, access, attr );
262 /* allocate a handle for an object, checking the dacl allows the process to */
263 /* access it and incrementing its refcount */
264 /* return the handle, or 0 on error */
265 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
267 struct object *obj = ptr;
268 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
269 if (access && !check_object_access( obj, &access )) return 0;
270 return alloc_handle_entry( process, ptr, access, attr );
273 /* allocate a global handle for an object, incrementing its refcount */
274 /* return the handle, or 0 on error */
275 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
277 if (!global_table)
279 if (!(global_table = alloc_handle_table( NULL, 0 )))
280 return 0;
281 make_object_static( &global_table->obj );
283 return handle_local_to_global( alloc_entry( global_table, obj, access ));
286 /* allocate a global handle for an object, checking the dacl allows the */
287 /* process to access it and incrementing its refcount and incrementing its refcount */
288 /* return the handle, or 0 on error */
289 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
291 if (access && !check_object_access( obj, &access )) return 0;
292 return alloc_global_handle_no_access_check( obj, access );
295 /* return a handle entry, or NULL if the handle is invalid */
296 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
298 struct handle_table *table = process->handles;
299 struct handle_entry *entry;
300 int index;
302 if (handle_is_global(handle))
304 handle = handle_global_to_local(handle);
305 table = global_table;
307 if (!table) return NULL;
308 index = handle_to_index( handle );
309 if (index < 0) return NULL;
310 if (index > table->last) return NULL;
311 entry = table->entries + index;
312 if (!entry->ptr) return NULL;
313 return entry;
316 /* attempt to shrink a table */
317 static void shrink_handle_table( struct handle_table *table )
319 struct handle_entry *entry = table->entries + table->last;
320 struct handle_entry *new_entries;
321 int count = table->count;
323 while (table->last >= 0)
325 if (entry->ptr) break;
326 table->last--;
327 entry--;
329 if (table->last >= count / 4) return; /* no need to shrink */
330 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
331 count /= 2;
332 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
333 table->count = count;
334 table->entries = new_entries;
337 /* copy the handle table of the parent process */
338 /* return 1 if OK, 0 on error */
339 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
341 struct handle_table *parent_table = parent->handles;
342 struct handle_table *table;
343 int i;
345 assert( parent_table );
346 assert( parent_table->obj.ops == &handle_table_ops );
348 if (!(table = alloc_handle_table( process, parent_table->count )))
349 return NULL;
351 if ((table->last = parent_table->last) >= 0)
353 struct handle_entry *ptr = table->entries;
354 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
355 for (i = 0; i <= table->last; i++, ptr++)
357 if (!ptr->ptr) continue;
358 if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
359 else ptr->ptr = NULL; /* don't inherit this entry */
362 /* attempt to shrink the table */
363 shrink_handle_table( table );
364 return table;
367 /* close a handle and decrement the refcount of the associated object */
368 unsigned int close_handle( struct process *process, obj_handle_t handle )
370 struct handle_table *table;
371 struct handle_entry *entry;
372 struct object *obj;
374 if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
375 if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
376 obj = entry->ptr;
377 if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
378 entry->ptr = NULL;
379 table = handle_is_global(handle) ? global_table : process->handles;
380 if (entry < table->entries + table->free) table->free = entry - table->entries;
381 if (entry == table->entries + table->last) shrink_handle_table( table );
382 release_object( obj );
383 return STATUS_SUCCESS;
386 /* retrieve the object corresponding to one of the magic pseudo-handles */
387 static inline struct object *get_magic_handle( obj_handle_t handle )
389 switch(handle)
391 case 0xfffffffe: /* current thread pseudo-handle */
392 return &current->obj;
393 case 0x7fffffff: /* current process pseudo-handle */
394 case 0xffffffff: /* current process pseudo-handle */
395 return (struct object *)current->process;
396 default:
397 return NULL;
401 /* retrieve the object corresponding to a handle, incrementing its refcount */
402 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
403 unsigned int access, const struct object_ops *ops )
405 struct handle_entry *entry;
406 struct object *obj;
408 if (!(obj = get_magic_handle( handle )))
410 if (!(entry = get_handle( process, handle )))
412 set_error( STATUS_INVALID_HANDLE );
413 return NULL;
415 obj = entry->ptr;
416 if (ops && (obj->ops != ops))
418 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
419 return NULL;
421 if ((entry->access & access) != access)
423 set_error( STATUS_ACCESS_DENIED );
424 return NULL;
427 else if (ops && (obj->ops != ops))
429 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
430 return NULL;
432 return grab_object( obj );
435 /* retrieve the access rights of a given handle */
436 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
438 struct handle_entry *entry;
440 if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
441 if (!(entry = get_handle( process, handle ))) return 0;
442 return entry->access & ~RESERVED_ALL;
445 /* find the first inherited handle of the given type */
446 /* this is needed for window stations and desktops (don't ask...) */
447 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
449 struct handle_table *table = process->handles;
450 struct handle_entry *ptr;
451 int i;
453 if (!table) return 0;
455 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
457 if (!ptr->ptr) continue;
458 if (ptr->ptr->ops != ops) continue;
459 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
461 return 0;
464 /* enumerate handles of a given type */
465 /* this is needed for window stations and desktops */
466 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
467 unsigned int *index )
469 struct handle_table *table = process->handles;
470 unsigned int i;
471 struct handle_entry *entry;
473 if (!table) return 0;
475 for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
477 if (!entry->ptr) continue;
478 if (entry->ptr->ops != ops) continue;
479 *index = i + 1;
480 return index_to_handle(i);
482 return 0;
485 /* get/set the handle reserved flags */
486 /* return the old flags (or -1 on error) */
487 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
489 struct handle_entry *entry;
490 unsigned int old_access;
492 if (get_magic_handle( handle ))
494 /* we can retrieve but not set info for magic handles */
495 if (mask) set_error( STATUS_ACCESS_DENIED );
496 return 0;
498 if (!(entry = get_handle( process, handle )))
500 set_error( STATUS_INVALID_HANDLE );
501 return -1;
503 old_access = entry->access;
504 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
505 flags = (flags << RESERVED_SHIFT) & mask;
506 entry->access = (entry->access & ~mask) | flags;
507 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
510 /* duplicate a handle */
511 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
512 unsigned int access, unsigned int attr, unsigned int options )
514 obj_handle_t res;
515 struct handle_entry *entry;
516 unsigned int src_access;
517 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
519 if (!obj) return 0;
520 if ((entry = get_handle( src, src_handle )))
521 src_access = entry->access;
522 else /* pseudo-handle, give it full access */
523 src_access = obj->ops->map_access( obj, GENERIC_ALL );
524 src_access &= ~RESERVED_ALL;
526 if (options & DUP_HANDLE_SAME_ACCESS)
527 access = src_access;
528 else
529 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
531 /* asking for the more access rights than src_access? */
532 if (access & ~src_access)
534 if (options & DUP_HANDLE_MAKE_GLOBAL)
535 res = alloc_global_handle( obj, access );
536 else
537 res = alloc_handle( dst, obj, access, attr );
539 else
541 if (options & DUP_HANDLE_MAKE_GLOBAL)
542 res = alloc_global_handle_no_access_check( obj, access );
543 else if ((options & DUP_HANDLE_CLOSE_SOURCE) && src == dst &&
544 entry && !(entry->access & RESERVED_CLOSE_PROTECT))
546 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
547 entry->access = access;
548 res = src_handle;
550 else
551 res = alloc_handle_entry( dst, obj, access, attr );
554 release_object( obj );
555 return res;
558 /* open a new handle to an existing object */
559 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
560 const struct object_ops *ops, unsigned int access, unsigned int attr )
562 obj_handle_t handle = 0;
563 struct object *obj = find_object( namespace, name, attr );
564 if (obj)
566 if (ops && obj->ops != ops)
567 set_error( STATUS_OBJECT_TYPE_MISMATCH );
568 else
569 handle = alloc_handle( current->process, obj, access, attr );
570 release_object( obj );
572 else
573 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
574 return handle;
577 /* return the size of the handle table of a given process */
578 unsigned int get_handle_table_count( struct process *process )
580 if (!process->handles) return 0;
581 return process->handles->count;
584 /* close a handle */
585 DECL_HANDLER(close_handle)
587 unsigned int err = close_handle( current->process, req->handle );
588 set_error( err );
591 /* set a handle information */
592 DECL_HANDLER(set_handle_info)
594 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
597 /* duplicate a handle */
598 DECL_HANDLER(dup_handle)
600 struct process *src, *dst = NULL;
602 reply->handle = 0;
603 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
605 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
607 reply->handle = duplicate_handle( src, req->src_handle, NULL,
608 req->access, req->attributes, req->options );
610 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
612 reply->handle = duplicate_handle( src, req->src_handle, dst,
613 req->access, req->attributes, req->options );
614 release_object( dst );
616 /* close the handle no matter what happened */
617 if ((req->options & DUP_HANDLE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
618 reply->closed = !close_handle( src, req->src_handle );
619 reply->self = (src == current->process);
620 release_object( src );
624 DECL_HANDLER(get_object_info)
626 struct object *obj;
627 WCHAR *name;
629 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
631 reply->access = get_handle_access( current->process, req->handle );
632 reply->ref_count = obj->refcount;
633 if ((name = get_object_full_name( obj, &reply->total )))
634 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
635 release_object( obj );
638 DECL_HANDLER(set_security_object)
640 data_size_t sd_size = get_req_data_size();
641 const struct security_descriptor *sd = get_req_data();
642 struct object *obj;
643 unsigned int access = 0;
645 if (!sd_is_valid( sd, sd_size ))
647 set_error( STATUS_ACCESS_VIOLATION );
648 return;
651 if (req->security_info & OWNER_SECURITY_INFORMATION ||
652 req->security_info & GROUP_SECURITY_INFORMATION)
653 access |= WRITE_OWNER;
654 if (req->security_info & SACL_SECURITY_INFORMATION)
655 access |= ACCESS_SYSTEM_SECURITY;
656 if (req->security_info & DACL_SECURITY_INFORMATION)
657 access |= WRITE_DAC;
659 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
661 obj->ops->set_sd( obj, sd, req->security_info );
662 release_object( obj );
665 DECL_HANDLER(get_security_object)
667 const struct security_descriptor *sd;
668 struct object *obj;
669 unsigned int access = READ_CONTROL;
670 struct security_descriptor req_sd;
671 int present;
672 const SID *owner, *group;
673 const ACL *sacl, *dacl;
675 if (req->security_info & SACL_SECURITY_INFORMATION)
676 access |= ACCESS_SYSTEM_SECURITY;
678 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
680 sd = obj->ops->get_sd( obj );
681 if (sd)
683 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
685 owner = sd_get_owner( sd );
686 if (req->security_info & OWNER_SECURITY_INFORMATION)
687 req_sd.owner_len = sd->owner_len;
688 else
689 req_sd.owner_len = 0;
691 group = sd_get_group( sd );
692 if (req->security_info & GROUP_SECURITY_INFORMATION)
693 req_sd.group_len = sd->group_len;
694 else
695 req_sd.group_len = 0;
697 req_sd.control |= SE_SACL_PRESENT;
698 sacl = sd_get_sacl( sd, &present );
699 if (req->security_info & SACL_SECURITY_INFORMATION && present)
700 req_sd.sacl_len = sd->sacl_len;
701 else
702 req_sd.sacl_len = 0;
704 req_sd.control |= SE_DACL_PRESENT;
705 dacl = sd_get_dacl( sd, &present );
706 if (req->security_info & DACL_SECURITY_INFORMATION && present)
707 req_sd.dacl_len = sd->dacl_len;
708 else
709 req_sd.dacl_len = 0;
711 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
712 req_sd.sacl_len + req_sd.dacl_len;
713 if (reply->sd_len <= get_reply_max_size())
715 char *ptr = set_reply_data_size(reply->sd_len);
717 memcpy( ptr, &req_sd, sizeof(req_sd) );
718 ptr += sizeof(req_sd);
719 memcpy( ptr, owner, req_sd.owner_len );
720 ptr += req_sd.owner_len;
721 memcpy( ptr, group, req_sd.group_len );
722 ptr += req_sd.group_len;
723 memcpy( ptr, sacl, req_sd.sacl_len );
724 ptr += req_sd.sacl_len;
725 memcpy( ptr, dacl, req_sd.dacl_len );
727 else
728 set_error(STATUS_BUFFER_TOO_SMALL);
731 release_object( obj );