server: Cancel socket asyncs when the last handle in process is closed.
[wine.git] / server / handle.c
blob0595fdb403b5d2bca67e515ceaada691d18a3213
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"
23 #include <assert.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.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 obj->ops->type->handle_count++;
105 obj->ops->type->handle_max = max( obj->ops->type->handle_max, obj->ops->type->handle_count );
106 return grab_object( obj );
109 /* release an object and decrement its handle count */
110 static void release_object_from_handle( struct object *obj )
112 assert( obj->handle_count );
113 obj->ops->type->handle_count--;
114 obj->handle_count--;
115 release_object( obj );
118 static void handle_table_dump( struct object *obj, int verbose );
119 static void handle_table_destroy( struct object *obj );
121 static const struct object_ops handle_table_ops =
123 sizeof(struct handle_table), /* size */
124 &no_type, /* type */
125 handle_table_dump, /* dump */
126 no_add_queue, /* add_queue */
127 NULL, /* remove_queue */
128 NULL, /* signaled */
129 NULL, /* satisfied */
130 no_signal, /* signal */
131 no_get_fd, /* get_fd */
132 default_map_access, /* map_access */
133 default_get_sd, /* get_sd */
134 default_set_sd, /* set_sd */
135 no_get_full_name, /* get_full_name */
136 no_lookup_name, /* lookup_name */
137 no_link_name, /* link_name */
138 NULL, /* unlink_name */
139 no_open_file, /* open_file */
140 no_kernel_obj_list, /* get_kernel_obj_list */
141 no_close_handle, /* close_handle */
142 handle_table_destroy /* destroy */
145 /* dump a handle table */
146 static void handle_table_dump( struct object *obj, int verbose )
148 int i;
149 struct handle_table *table = (struct handle_table *)obj;
150 struct handle_entry *entry;
152 assert( obj->ops == &handle_table_ops );
154 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
155 table->last, table->count, table->process );
156 if (!verbose) return;
157 entry = table->entries;
158 for (i = 0; i <= table->last; i++, entry++)
160 if (!entry->ptr) continue;
161 fprintf( stderr, " %04x: %p %08x ",
162 index_to_handle(i), entry->ptr, entry->access );
163 dump_object_name( entry->ptr );
164 entry->ptr->ops->dump( entry->ptr, 0 );
168 /* destroy a handle table */
169 static void handle_table_destroy( struct object *obj )
171 int i;
172 struct handle_table *table = (struct handle_table *)obj;
173 struct handle_entry *entry;
175 assert( obj->ops == &handle_table_ops );
177 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
179 struct object *obj = entry->ptr;
180 entry->ptr = NULL;
181 if (obj)
183 if (table->process)
184 obj->ops->close_handle( obj, table->process, index_to_handle(i) );
185 release_object_from_handle( obj );
188 free( table->entries );
191 /* close all the process handles and free the handle table */
192 void close_process_handles( struct process *process )
194 struct handle_table *table = process->handles;
196 process->handles = NULL;
197 if (table) release_object( table );
200 /* allocate a new handle table */
201 struct handle_table *alloc_handle_table( struct process *process, int count )
203 struct handle_table *table;
205 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
206 if (!(table = alloc_object( &handle_table_ops )))
207 return NULL;
208 table->process = process;
209 table->count = count;
210 table->last = -1;
211 table->free = 0;
212 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
213 release_object( table );
214 return NULL;
217 /* grow a handle table */
218 static int grow_handle_table( struct handle_table *table )
220 struct handle_entry *new_entries;
221 int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
223 if (count == table->count ||
224 !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
226 set_error( STATUS_INSUFFICIENT_RESOURCES );
227 return 0;
229 table->entries = new_entries;
230 table->count = count;
231 return 1;
234 /* allocate the first free entry in the handle table */
235 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
237 struct handle_entry *entry = table->entries + table->free;
238 int i;
240 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
241 if (i >= table->count)
243 if (!grow_handle_table( table )) return 0;
244 entry = table->entries + i; /* the entries may have moved */
246 table->last = i;
247 found:
248 table->free = i + 1;
249 entry->ptr = grab_object_for_handle( obj );
250 entry->access = access;
251 return index_to_handle(i);
254 /* allocate a handle for an object, incrementing its refcount */
255 static obj_handle_t alloc_handle_entry( struct process *process, void *ptr,
256 unsigned int access, unsigned int attr )
258 struct object *obj = ptr;
260 assert( !(access & RESERVED_ALL) );
261 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
262 if (!process->handles)
264 set_error( STATUS_PROCESS_IS_TERMINATING );
265 return 0;
267 return alloc_entry( process->handles, obj, access );
270 /* allocate a handle for an object, incrementing its refcount */
271 /* return the handle, or 0 on error */
272 obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
274 struct object *obj = ptr;
275 if (access & MAXIMUM_ALLOWED) access = GENERIC_ALL;
276 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
277 return alloc_handle_entry( process, ptr, access, attr );
280 /* allocate a handle for an object, checking the dacl allows the process to */
281 /* access it and incrementing its refcount */
282 /* return the handle, or 0 on error */
283 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
285 struct object *obj = ptr;
286 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
287 if (access && !check_object_access( NULL, obj, &access )) return 0;
288 return alloc_handle_entry( process, ptr, access, attr );
291 /* allocate a global handle for an object, incrementing its refcount */
292 /* return the handle, or 0 on error */
293 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
295 if (!global_table)
297 if (!(global_table = alloc_handle_table( NULL, 0 )))
298 return 0;
299 make_object_permanent( &global_table->obj );
301 return handle_local_to_global( alloc_entry( global_table, obj, access ));
304 /* allocate a global handle for an object, checking the dacl allows the */
305 /* process to access it and incrementing its refcount and incrementing its refcount */
306 /* return the handle, or 0 on error */
307 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
309 if (access && !check_object_access( NULL, obj, &access )) return 0;
310 return alloc_global_handle_no_access_check( obj, access );
313 /* return a handle entry, or NULL if the handle is invalid */
314 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
316 struct handle_table *table = process->handles;
317 struct handle_entry *entry;
318 int index;
320 if (handle_is_global(handle))
322 handle = handle_global_to_local(handle);
323 table = global_table;
325 if (!table) return NULL;
326 index = handle_to_index( handle );
327 if (index < 0) return NULL;
328 if (index > table->last) return NULL;
329 entry = table->entries + index;
330 if (!entry->ptr) return NULL;
331 return entry;
334 /* attempt to shrink a table */
335 static void shrink_handle_table( struct handle_table *table )
337 struct handle_entry *entry = table->entries + table->last;
338 struct handle_entry *new_entries;
339 int count = table->count;
341 while (table->last >= 0)
343 if (entry->ptr) break;
344 table->last--;
345 entry--;
347 if (table->last >= count / 4) return; /* no need to shrink */
348 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
349 count /= 2;
350 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
351 table->count = count;
352 table->entries = new_entries;
355 static void inherit_handle( struct process *parent, const obj_handle_t handle, struct handle_table *table )
357 struct handle_entry *dst, *src;
358 int index;
360 dst = table->entries;
362 src = get_handle( parent, handle );
363 if (!src || !(src->access & RESERVED_INHERIT)) return;
364 index = handle_to_index( handle );
365 if (dst[index].ptr) return;
366 grab_object_for_handle( src->ptr );
367 dst[index] = *src;
368 table->last = max( table->last, index );
371 /* copy the handle table of the parent process */
372 /* return 1 if OK, 0 on error */
373 struct handle_table *copy_handle_table( struct process *process, struct process *parent,
374 const obj_handle_t *handles, unsigned int handle_count,
375 const obj_handle_t *std_handles )
377 struct handle_table *parent_table = parent->handles;
378 struct handle_table *table;
379 int i;
381 assert( parent_table );
382 assert( parent_table->obj.ops == &handle_table_ops );
384 if (!(table = alloc_handle_table( process, parent_table->count )))
385 return NULL;
387 if (handles)
389 memset( table->entries, 0, parent_table->count * sizeof(*table->entries) );
391 for (i = 0; i < handle_count; i++)
393 inherit_handle( parent, handles[i], table );
396 for (i = 0; i < 3; i++)
398 inherit_handle( parent, std_handles[i], table );
401 else
403 if ((table->last = parent_table->last) >= 0)
405 struct handle_entry *ptr = table->entries;
406 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
407 for (i = 0; i <= table->last; i++, ptr++)
409 if (!ptr->ptr) continue;
410 if (ptr->access & RESERVED_INHERIT) grab_object_for_handle( ptr->ptr );
411 else ptr->ptr = NULL; /* don't inherit this entry */
415 /* attempt to shrink the table */
416 shrink_handle_table( table );
417 return table;
420 /* close a handle and decrement the refcount of the associated object */
421 unsigned int close_handle( struct process *process, obj_handle_t handle )
423 struct handle_table *table;
424 struct handle_entry *entry;
425 struct object *obj;
427 if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
428 if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
429 obj = entry->ptr;
430 if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
431 entry->ptr = NULL;
432 table = handle_is_global(handle) ? global_table : process->handles;
433 if (entry < table->entries + table->free) table->free = entry - table->entries;
434 if (entry == table->entries + table->last) shrink_handle_table( table );
435 release_object_from_handle( obj );
436 return STATUS_SUCCESS;
439 /* retrieve the object corresponding to one of the magic pseudo-handles */
440 static inline struct object *get_magic_handle( obj_handle_t handle )
442 switch(handle)
444 case 0xfffffffa: /* current thread impersonation token pseudo-handle */
445 return (struct object *)thread_get_impersonation_token( current );
446 case 0xfffffffb: /* current thread token pseudo-handle */
447 return (struct object *)current->token;
448 case 0xfffffffc: /* current process token pseudo-handle */
449 return (struct object *)current->process->token;
450 case 0xfffffffe: /* current thread pseudo-handle */
451 return &current->obj;
452 case 0x7fffffff: /* current process pseudo-handle */
453 case 0xffffffff: /* current process pseudo-handle */
454 return (struct object *)current->process;
455 default:
456 return NULL;
460 /* retrieve the object corresponding to a handle, incrementing its refcount */
461 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
462 unsigned int access, const struct object_ops *ops )
464 struct handle_entry *entry;
465 struct object *obj;
467 if (!(obj = get_magic_handle( handle )))
469 if (!(entry = get_handle( process, handle )))
471 set_error( STATUS_INVALID_HANDLE );
472 return NULL;
474 obj = entry->ptr;
475 if (ops && (obj->ops != ops))
477 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
478 return NULL;
480 if ((entry->access & access) != access)
482 set_error( STATUS_ACCESS_DENIED );
483 return NULL;
486 else if (ops && (obj->ops != ops))
488 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
489 return NULL;
491 return grab_object( obj );
494 /* retrieve the access rights of a given handle */
495 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
497 struct handle_entry *entry;
499 if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
500 if (!(entry = get_handle( process, handle ))) return 0;
501 return entry->access & ~RESERVED_ALL;
504 /* find the first inherited handle of the given type */
505 /* this is needed for window stations and desktops (don't ask...) */
506 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
508 struct handle_table *table = process->handles;
509 struct handle_entry *ptr;
510 int i;
512 if (!table) return 0;
514 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
516 if (!ptr->ptr) continue;
517 if (ptr->ptr->ops != ops) continue;
518 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
520 return 0;
523 /* return number of open handles to the object in the process */
524 unsigned int get_obj_handle_count( struct process *process, const struct object *obj )
526 struct handle_table *table = process->handles;
527 struct handle_entry *ptr;
528 unsigned int count = 0;
529 int i;
531 if (!table) return 0;
533 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
534 if (ptr->ptr == obj) ++count;
535 return count;
538 /* get/set the handle reserved flags */
539 /* return the old flags (or -1 on error) */
540 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
542 struct handle_entry *entry;
543 unsigned int old_access;
545 if (get_magic_handle( handle ))
547 /* we can retrieve but not set info for magic handles */
548 if (mask) set_error( STATUS_ACCESS_DENIED );
549 return 0;
551 if (!(entry = get_handle( process, handle )))
553 set_error( STATUS_INVALID_HANDLE );
554 return -1;
556 old_access = entry->access;
557 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
558 flags = (flags << RESERVED_SHIFT) & mask;
559 entry->access = (entry->access & ~mask) | flags;
560 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
563 /* duplicate a handle */
564 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
565 unsigned int access, unsigned int attr, unsigned int options )
567 obj_handle_t res;
568 struct handle_entry *entry;
569 unsigned int src_access;
570 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
572 if (!obj) return 0;
573 if ((entry = get_handle( src, src_handle )))
574 src_access = entry->access;
575 else /* pseudo-handle, give it full access */
576 src_access = obj->ops->map_access( obj, GENERIC_ALL );
577 src_access &= ~RESERVED_ALL;
579 if (options & DUPLICATE_SAME_ACCESS)
580 access = src_access;
581 else
582 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
584 /* asking for the more access rights than src_access? */
585 if (access & ~src_access)
587 if ((current->token && !check_object_access( current->token, obj, &access )) ||
588 !check_object_access( dst->token, obj, &access ))
590 release_object( obj );
591 return 0;
594 if (options & DUPLICATE_MAKE_GLOBAL)
595 res = alloc_global_handle( obj, access );
596 else
597 res = alloc_handle_no_access_check( dst, obj, access, attr );
599 else
601 if (options & DUPLICATE_MAKE_GLOBAL)
602 res = alloc_global_handle_no_access_check( obj, access );
603 else if ((options & DUPLICATE_CLOSE_SOURCE) && src == dst &&
604 entry && !(entry->access & RESERVED_CLOSE_PROTECT))
606 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
607 entry->access = access;
608 res = src_handle;
610 else
611 res = alloc_handle_entry( dst, obj, access, attr );
614 release_object( obj );
615 return res;
618 /* open a new handle to an existing object */
619 obj_handle_t open_object( struct process *process, obj_handle_t parent, unsigned int access,
620 const struct object_ops *ops, const struct unicode_str *name,
621 unsigned int attributes )
623 obj_handle_t handle = 0;
624 struct object *obj, *root = NULL;
626 if (name->len >= 65534)
628 set_error( STATUS_OBJECT_NAME_INVALID );
629 return 0;
632 if (parent)
634 if (name->len)
635 root = get_directory_obj( process, parent );
636 else /* opening the object itself can work for non-directories too */
637 root = get_handle_obj( process, parent, 0, NULL );
638 if (!root) return 0;
641 if ((obj = open_named_object( root, ops, name, attributes )))
643 handle = alloc_handle( process, obj, access, attributes );
644 release_object( obj );
646 if (root) release_object( root );
647 return handle;
650 /* return the size of the handle table of a given process */
651 unsigned int get_handle_table_count( struct process *process )
653 if (!process->handles) return 0;
654 return process->handles->count;
657 /* close a handle */
658 DECL_HANDLER(close_handle)
660 unsigned int err = close_handle( current->process, req->handle );
661 set_error( err );
664 /* set a handle information */
665 DECL_HANDLER(set_handle_info)
667 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
670 /* duplicate a handle */
671 DECL_HANDLER(dup_handle)
673 struct process *src, *dst = NULL;
675 reply->handle = 0;
676 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
678 if (req->options & DUPLICATE_MAKE_GLOBAL)
680 reply->handle = duplicate_handle( src, req->src_handle, NULL,
681 req->access, req->attributes, req->options );
683 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
685 reply->handle = duplicate_handle( src, req->src_handle, dst,
686 req->access, req->attributes, req->options );
687 release_object( dst );
689 /* close the handle no matter what happened */
690 if ((req->options & DUPLICATE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
691 close_handle( src, req->src_handle );
692 release_object( src );
696 DECL_HANDLER(get_object_info)
698 struct object *obj;
700 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
702 reply->access = get_handle_access( current->process, req->handle );
703 reply->ref_count = obj->refcount;
704 reply->handle_count = obj->handle_count;
705 release_object( obj );
708 DECL_HANDLER(get_object_name)
710 struct object *obj;
711 WCHAR *name;
713 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
715 if ((name = obj->ops->get_full_name( obj, &reply->total )))
716 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
717 release_object( obj );
720 DECL_HANDLER(set_security_object)
722 data_size_t sd_size = get_req_data_size();
723 const struct security_descriptor *sd = get_req_data();
724 struct object *obj;
725 unsigned int access = 0;
727 if (!sd_is_valid( sd, sd_size ))
729 set_error( STATUS_ACCESS_VIOLATION );
730 return;
733 if (req->security_info & OWNER_SECURITY_INFORMATION ||
734 req->security_info & GROUP_SECURITY_INFORMATION ||
735 req->security_info & LABEL_SECURITY_INFORMATION)
736 access |= WRITE_OWNER;
737 if (req->security_info & SACL_SECURITY_INFORMATION)
738 access |= ACCESS_SYSTEM_SECURITY;
739 if (req->security_info & DACL_SECURITY_INFORMATION)
740 access |= WRITE_DAC;
742 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
744 obj->ops->set_sd( obj, sd, req->security_info );
745 release_object( obj );
748 DECL_HANDLER(get_security_object)
750 const struct security_descriptor *sd;
751 struct object *obj;
752 unsigned int access = READ_CONTROL;
753 struct security_descriptor req_sd;
754 int present;
755 const struct sid *owner, *group;
756 const struct acl *sacl, *dacl;
757 struct acl *label_acl = NULL;
759 if (req->security_info & SACL_SECURITY_INFORMATION)
760 access |= ACCESS_SYSTEM_SECURITY;
762 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
764 sd = obj->ops->get_sd( obj );
765 if (sd)
767 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
769 owner = sd_get_owner( sd );
770 if (req->security_info & OWNER_SECURITY_INFORMATION)
771 req_sd.owner_len = sd->owner_len;
772 else
773 req_sd.owner_len = 0;
775 group = sd_get_group( sd );
776 if (req->security_info & GROUP_SECURITY_INFORMATION)
777 req_sd.group_len = sd->group_len;
778 else
779 req_sd.group_len = 0;
781 sacl = sd_get_sacl( sd, &present );
782 if (req->security_info & SACL_SECURITY_INFORMATION && present)
783 req_sd.sacl_len = sd->sacl_len;
784 else if (req->security_info & LABEL_SECURITY_INFORMATION && present && sacl)
786 if (!(label_acl = extract_security_labels( sacl ))) goto done;
787 req_sd.sacl_len = label_acl->size;
788 sacl = label_acl;
790 else
791 req_sd.sacl_len = 0;
793 dacl = sd_get_dacl( sd, &present );
794 if (req->security_info & DACL_SECURITY_INFORMATION && present)
795 req_sd.dacl_len = sd->dacl_len;
796 else
797 req_sd.dacl_len = 0;
799 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
800 req_sd.sacl_len + req_sd.dacl_len;
801 if (reply->sd_len <= get_reply_max_size())
803 char *ptr = set_reply_data_size(reply->sd_len);
805 memcpy( ptr, &req_sd, sizeof(req_sd) );
806 ptr += sizeof(req_sd);
807 memcpy( ptr, owner, req_sd.owner_len );
808 ptr += req_sd.owner_len;
809 memcpy( ptr, group, req_sd.group_len );
810 ptr += req_sd.group_len;
811 memcpy( ptr, sacl, req_sd.sacl_len );
812 ptr += req_sd.sacl_len;
813 memcpy( ptr, dacl, req_sd.dacl_len );
815 else
816 set_error(STATUS_BUFFER_TOO_SMALL);
819 done:
820 release_object( obj );
821 free( label_acl );
824 struct enum_handle_info
826 unsigned int count;
827 struct handle_info *handle;
830 static int enum_handles( struct process *process, void *user )
832 struct enum_handle_info *info = user;
833 struct handle_table *table = process->handles;
834 struct handle_entry *entry;
835 struct handle_info *handle;
836 unsigned int i;
838 if (!table)
839 return 0;
841 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
843 if (!entry->ptr) continue;
844 if (!info->handle)
846 info->count++;
847 continue;
849 assert( info->count );
850 handle = info->handle++;
851 handle->owner = process->id;
852 handle->handle = index_to_handle(i);
853 handle->access = entry->access & ~RESERVED_ALL;
854 handle->type = entry->ptr->ops->type->index;
855 handle->attributes = 0;
856 if (entry->access & RESERVED_INHERIT) handle->attributes |= OBJ_INHERIT;
857 if (entry->access & RESERVED_CLOSE_PROTECT) handle->attributes |= OBJ_PROTECT_CLOSE;
858 info->count--;
861 return 0;
864 DECL_HANDLER(get_system_handles)
866 struct enum_handle_info info;
867 struct handle_info *handle;
868 data_size_t max_handles = get_reply_max_size() / sizeof(*handle);
870 info.handle = NULL;
871 info.count = 0;
872 enum_processes( enum_handles, &info );
873 reply->count = info.count;
875 if (max_handles < info.count)
876 set_error( STATUS_BUFFER_TOO_SMALL );
877 else if ((handle = set_reply_data_size( info.count * sizeof(*handle) )))
879 info.handle = handle;
880 enum_processes( enum_handles, &info );
884 DECL_HANDLER(make_temporary)
886 struct object *obj;
888 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
890 if (obj->is_permanent)
892 make_object_temporary( obj );
893 release_object( obj );
895 release_object( obj );
898 DECL_HANDLER(compare_objects)
900 struct object *obj1, *obj2;
902 if (!(obj1 = get_handle_obj( current->process, req->first, 0, NULL ))) return;
903 if (!(obj2 = get_handle_obj( current->process, req->second, 0, NULL )))
905 release_object( obj1 );
906 return;
909 if (obj1 != obj2) set_error( STATUS_NOT_SAME_OBJECT );
911 release_object( obj2 );
912 release_object( obj1 );