wined3d: Acquire a context for the front buffer in swapchain_gl_present().
[wine.git] / server / handle.c
bloba2a8bb5479cf1eb6c977451a46bd243b8081aec1
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_link_name, /* link_name */
134 NULL, /* unlink_name */
135 no_open_file, /* open_file */
136 no_kernel_obj_list, /* get_kernel_obj_list */
137 no_close_handle, /* close_handle */
138 handle_table_destroy /* destroy */
141 /* dump a handle table */
142 static void handle_table_dump( struct object *obj, int verbose )
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 fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
151 table->last, table->count, table->process );
152 if (!verbose) return;
153 entry = table->entries;
154 for (i = 0; i <= table->last; i++, entry++)
156 if (!entry->ptr) continue;
157 fprintf( stderr, " %04x: %p %08x ",
158 index_to_handle(i), entry->ptr, entry->access );
159 dump_object_name( entry->ptr );
160 entry->ptr->ops->dump( entry->ptr, 0 );
164 /* destroy a handle table */
165 static void handle_table_destroy( struct object *obj )
167 int i;
168 struct handle_table *table = (struct handle_table *)obj;
169 struct handle_entry *entry;
171 assert( obj->ops == &handle_table_ops );
173 /* first notify all objects that handles are being closed */
174 if (table->process)
176 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
178 struct object *obj = entry->ptr;
179 if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
183 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
185 struct object *obj = entry->ptr;
186 entry->ptr = NULL;
187 if (obj) release_object_from_handle( obj );
189 free( table->entries );
192 /* close all the process handles and free the handle table */
193 void close_process_handles( struct process *process )
195 struct handle_table *table = process->handles;
197 process->handles = NULL;
198 if (table) release_object( table );
201 /* allocate a new handle table */
202 struct handle_table *alloc_handle_table( struct process *process, int count )
204 struct handle_table *table;
206 if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
207 if (!(table = alloc_object( &handle_table_ops )))
208 return NULL;
209 table->process = process;
210 table->count = count;
211 table->last = -1;
212 table->free = 0;
213 if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
214 release_object( table );
215 return NULL;
218 /* grow a handle table */
219 static int grow_handle_table( struct handle_table *table )
221 struct handle_entry *new_entries;
222 int count = min( table->count * 2, MAX_HANDLE_ENTRIES );
224 if (count == table->count ||
225 !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
227 set_error( STATUS_INSUFFICIENT_RESOURCES );
228 return 0;
230 table->entries = new_entries;
231 table->count = count;
232 return 1;
235 /* allocate the first free entry in the handle table */
236 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
238 struct handle_entry *entry = table->entries + table->free;
239 int i;
241 for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
242 if (i >= table->count)
244 if (!grow_handle_table( table )) return 0;
245 entry = table->entries + i; /* the entries may have moved */
247 table->last = i;
248 found:
249 table->free = i + 1;
250 entry->ptr = grab_object_for_handle( obj );
251 entry->access = access;
252 return index_to_handle(i);
255 /* allocate a handle for an object, incrementing its refcount */
256 static obj_handle_t alloc_handle_entry( struct process *process, void *ptr,
257 unsigned int access, unsigned int attr )
259 struct object *obj = ptr;
261 assert( !(access & RESERVED_ALL) );
262 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
263 if (!process->handles)
265 set_error( STATUS_PROCESS_IS_TERMINATING );
266 return 0;
268 return alloc_entry( process->handles, obj, access );
271 /* allocate a handle for an object, incrementing its refcount */
272 /* return the handle, or 0 on error */
273 obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
275 struct object *obj = ptr;
276 if (access & MAXIMUM_ALLOWED) access = GENERIC_ALL;
277 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
278 return alloc_handle_entry( process, ptr, access, attr );
281 /* allocate a handle for an object, checking the dacl allows the process to */
282 /* access it and incrementing its refcount */
283 /* return the handle, or 0 on error */
284 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
286 struct object *obj = ptr;
287 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
288 if (access && !check_object_access( obj, &access )) return 0;
289 return alloc_handle_entry( process, ptr, access, attr );
292 /* allocate a global handle for an object, incrementing its refcount */
293 /* return the handle, or 0 on error */
294 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
296 if (!global_table)
298 if (!(global_table = alloc_handle_table( NULL, 0 )))
299 return 0;
300 make_object_static( &global_table->obj );
302 return handle_local_to_global( alloc_entry( global_table, obj, access ));
305 /* allocate a global handle for an object, checking the dacl allows the */
306 /* process to access it and incrementing its refcount and incrementing its refcount */
307 /* return the handle, or 0 on error */
308 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
310 if (access && !check_object_access( obj, &access )) return 0;
311 return alloc_global_handle_no_access_check( obj, access );
314 /* return a handle entry, or NULL if the handle is invalid */
315 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
317 struct handle_table *table = process->handles;
318 struct handle_entry *entry;
319 int index;
321 if (handle_is_global(handle))
323 handle = handle_global_to_local(handle);
324 table = global_table;
326 if (!table) return NULL;
327 index = handle_to_index( handle );
328 if (index < 0) return NULL;
329 if (index > table->last) return NULL;
330 entry = table->entries + index;
331 if (!entry->ptr) return NULL;
332 return entry;
335 /* attempt to shrink a table */
336 static void shrink_handle_table( struct handle_table *table )
338 struct handle_entry *entry = table->entries + table->last;
339 struct handle_entry *new_entries;
340 int count = table->count;
342 while (table->last >= 0)
344 if (entry->ptr) break;
345 table->last--;
346 entry--;
348 if (table->last >= count / 4) return; /* no need to shrink */
349 if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */
350 count /= 2;
351 if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
352 table->count = count;
353 table->entries = new_entries;
356 /* copy the handle table of the parent process */
357 /* return 1 if OK, 0 on error */
358 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
360 struct handle_table *parent_table = parent->handles;
361 struct handle_table *table;
362 int i;
364 assert( parent_table );
365 assert( parent_table->obj.ops == &handle_table_ops );
367 if (!(table = alloc_handle_table( process, parent_table->count )))
368 return NULL;
370 if ((table->last = parent_table->last) >= 0)
372 struct handle_entry *ptr = table->entries;
373 memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
374 for (i = 0; i <= table->last; i++, ptr++)
376 if (!ptr->ptr) continue;
377 if (ptr->access & RESERVED_INHERIT) grab_object_for_handle( ptr->ptr );
378 else ptr->ptr = NULL; /* don't inherit this entry */
381 /* attempt to shrink the table */
382 shrink_handle_table( table );
383 return table;
386 /* close a handle and decrement the refcount of the associated object */
387 unsigned int close_handle( struct process *process, obj_handle_t handle )
389 struct handle_table *table;
390 struct handle_entry *entry;
391 struct object *obj;
393 if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE;
394 if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE;
395 obj = entry->ptr;
396 if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE;
397 entry->ptr = NULL;
398 table = handle_is_global(handle) ? global_table : process->handles;
399 if (entry < table->entries + table->free) table->free = entry - table->entries;
400 if (entry == table->entries + table->last) shrink_handle_table( table );
401 release_object_from_handle( obj );
402 return STATUS_SUCCESS;
405 /* retrieve the object corresponding to one of the magic pseudo-handles */
406 static inline struct object *get_magic_handle( obj_handle_t handle )
408 switch(handle)
410 case 0xfffffffa: /* current thread impersonation token pseudo-handle */
411 return (struct object *)thread_get_impersonation_token( current );
412 case 0xfffffffb: /* current thread token pseudo-handle */
413 return (struct object *)current->token;
414 case 0xfffffffc: /* current process token pseudo-handle */
415 return (struct object *)current->process->token;
416 case 0xfffffffe: /* current thread pseudo-handle */
417 return &current->obj;
418 case 0x7fffffff: /* current process pseudo-handle */
419 case 0xffffffff: /* current process pseudo-handle */
420 return (struct object *)current->process;
421 default:
422 return NULL;
426 /* retrieve the object corresponding to a handle, incrementing its refcount */
427 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
428 unsigned int access, const struct object_ops *ops )
430 struct handle_entry *entry;
431 struct object *obj;
433 if (!(obj = get_magic_handle( handle )))
435 if (!(entry = get_handle( process, handle )))
437 set_error( STATUS_INVALID_HANDLE );
438 return NULL;
440 obj = entry->ptr;
441 if (ops && (obj->ops != ops))
443 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
444 return NULL;
446 if ((entry->access & access) != access)
448 set_error( STATUS_ACCESS_DENIED );
449 return NULL;
452 else if (ops && (obj->ops != ops))
454 set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */
455 return NULL;
457 return grab_object( obj );
460 /* retrieve the access rights of a given handle */
461 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
463 struct handle_entry *entry;
465 if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */
466 if (!(entry = get_handle( process, handle ))) return 0;
467 return entry->access & ~RESERVED_ALL;
470 /* find the first inherited handle of the given type */
471 /* this is needed for window stations and desktops (don't ask...) */
472 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
474 struct handle_table *table = process->handles;
475 struct handle_entry *ptr;
476 int i;
478 if (!table) return 0;
480 for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
482 if (!ptr->ptr) continue;
483 if (ptr->ptr->ops != ops) continue;
484 if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
486 return 0;
489 /* enumerate handles of a given type */
490 /* this is needed for window stations and desktops */
491 obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops,
492 unsigned int *index )
494 struct handle_table *table = process->handles;
495 unsigned int i;
496 struct handle_entry *entry;
498 if (!table) return 0;
500 for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++)
502 if (!entry->ptr) continue;
503 if (entry->ptr->ops != ops) continue;
504 *index = i + 1;
505 return index_to_handle(i);
507 return 0;
510 /* get/set the handle reserved flags */
511 /* return the old flags (or -1 on error) */
512 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
514 struct handle_entry *entry;
515 unsigned int old_access;
517 if (get_magic_handle( handle ))
519 /* we can retrieve but not set info for magic handles */
520 if (mask) set_error( STATUS_ACCESS_DENIED );
521 return 0;
523 if (!(entry = get_handle( process, handle )))
525 set_error( STATUS_INVALID_HANDLE );
526 return -1;
528 old_access = entry->access;
529 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
530 flags = (flags << RESERVED_SHIFT) & mask;
531 entry->access = (entry->access & ~mask) | flags;
532 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
535 /* duplicate a handle */
536 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
537 unsigned int access, unsigned int attr, unsigned int options )
539 obj_handle_t res;
540 struct handle_entry *entry;
541 unsigned int src_access;
542 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
544 if (!obj) return 0;
545 if ((entry = get_handle( src, src_handle )))
546 src_access = entry->access;
547 else /* pseudo-handle, give it full access */
548 src_access = obj->ops->map_access( obj, GENERIC_ALL );
549 src_access &= ~RESERVED_ALL;
551 if (options & DUP_HANDLE_SAME_ACCESS)
552 access = src_access;
553 else
554 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
556 /* asking for the more access rights than src_access? */
557 if (access & ~src_access)
559 if (options & DUP_HANDLE_MAKE_GLOBAL)
560 res = alloc_global_handle( obj, access );
561 else
562 res = alloc_handle( dst, obj, access, attr );
564 else
566 if (options & DUP_HANDLE_MAKE_GLOBAL)
567 res = alloc_global_handle_no_access_check( obj, access );
568 else if ((options & DUP_HANDLE_CLOSE_SOURCE) && src == dst &&
569 entry && !(entry->access & RESERVED_CLOSE_PROTECT))
571 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
572 entry->access = access;
573 res = src_handle;
575 else
576 res = alloc_handle_entry( dst, obj, access, attr );
579 release_object( obj );
580 return res;
583 /* open a new handle to an existing object */
584 obj_handle_t open_object( struct process *process, obj_handle_t parent, unsigned int access,
585 const struct object_ops *ops, const struct unicode_str *name,
586 unsigned int attributes )
588 obj_handle_t handle = 0;
589 struct object *obj, *root = NULL;
591 if (name->len >= 65534)
593 set_error( STATUS_OBJECT_NAME_INVALID );
594 return 0;
597 if (parent)
599 if (name->len)
600 root = get_directory_obj( process, parent );
601 else /* opening the object itself can work for non-directories too */
602 root = get_handle_obj( process, parent, 0, NULL );
603 if (!root) return 0;
606 if ((obj = open_named_object( root, ops, name, attributes )))
608 handle = alloc_handle( process, obj, access, attributes );
609 release_object( obj );
611 if (root) release_object( root );
612 return handle;
615 /* return the size of the handle table of a given process */
616 unsigned int get_handle_table_count( struct process *process )
618 if (!process->handles) return 0;
619 return process->handles->count;
622 /* close a handle */
623 DECL_HANDLER(close_handle)
625 unsigned int err = close_handle( current->process, req->handle );
626 set_error( err );
629 /* set a handle information */
630 DECL_HANDLER(set_handle_info)
632 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
635 /* duplicate a handle */
636 DECL_HANDLER(dup_handle)
638 struct process *src, *dst = NULL;
640 reply->handle = 0;
641 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
643 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
645 reply->handle = duplicate_handle( src, req->src_handle, NULL,
646 req->access, req->attributes, req->options );
648 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
650 reply->handle = duplicate_handle( src, req->src_handle, dst,
651 req->access, req->attributes, req->options );
652 release_object( dst );
654 /* close the handle no matter what happened */
655 if ((req->options & DUP_HANDLE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
656 reply->closed = !close_handle( src, req->src_handle );
657 reply->self = (src == current->process);
658 release_object( src );
662 DECL_HANDLER(get_object_info)
664 struct object *obj;
665 WCHAR *name;
667 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
669 reply->access = get_handle_access( current->process, req->handle );
670 reply->ref_count = obj->refcount;
671 reply->handle_count = obj->handle_count;
672 if ((name = get_object_full_name( obj, &reply->total )))
673 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
674 release_object( obj );
677 DECL_HANDLER(set_security_object)
679 data_size_t sd_size = get_req_data_size();
680 const struct security_descriptor *sd = get_req_data();
681 struct object *obj;
682 unsigned int access = 0;
684 if (!sd_is_valid( sd, sd_size ))
686 set_error( STATUS_ACCESS_VIOLATION );
687 return;
690 if (req->security_info & OWNER_SECURITY_INFORMATION ||
691 req->security_info & GROUP_SECURITY_INFORMATION ||
692 req->security_info & LABEL_SECURITY_INFORMATION)
693 access |= WRITE_OWNER;
694 if (req->security_info & SACL_SECURITY_INFORMATION)
695 access |= ACCESS_SYSTEM_SECURITY;
696 if (req->security_info & DACL_SECURITY_INFORMATION)
697 access |= WRITE_DAC;
699 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
701 obj->ops->set_sd( obj, sd, req->security_info );
702 release_object( obj );
705 DECL_HANDLER(get_security_object)
707 const struct security_descriptor *sd;
708 struct object *obj;
709 unsigned int access = READ_CONTROL;
710 struct security_descriptor req_sd;
711 int present;
712 const SID *owner, *group;
713 const ACL *sacl, *dacl;
714 ACL *label_acl = NULL;
716 if (req->security_info & SACL_SECURITY_INFORMATION)
717 access |= ACCESS_SYSTEM_SECURITY;
719 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
721 sd = obj->ops->get_sd( obj );
722 if (sd)
724 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
726 owner = sd_get_owner( sd );
727 if (req->security_info & OWNER_SECURITY_INFORMATION)
728 req_sd.owner_len = sd->owner_len;
729 else
730 req_sd.owner_len = 0;
732 group = sd_get_group( sd );
733 if (req->security_info & GROUP_SECURITY_INFORMATION)
734 req_sd.group_len = sd->group_len;
735 else
736 req_sd.group_len = 0;
738 sacl = sd_get_sacl( sd, &present );
739 if (req->security_info & SACL_SECURITY_INFORMATION && present)
740 req_sd.sacl_len = sd->sacl_len;
741 else if (req->security_info & LABEL_SECURITY_INFORMATION && present && sacl)
743 if (!(label_acl = extract_security_labels( sacl ))) goto done;
744 req_sd.sacl_len = label_acl->AclSize;
745 sacl = label_acl;
747 else
748 req_sd.sacl_len = 0;
750 dacl = sd_get_dacl( sd, &present );
751 if (req->security_info & DACL_SECURITY_INFORMATION && present)
752 req_sd.dacl_len = sd->dacl_len;
753 else
754 req_sd.dacl_len = 0;
756 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
757 req_sd.sacl_len + req_sd.dacl_len;
758 if (reply->sd_len <= get_reply_max_size())
760 char *ptr = set_reply_data_size(reply->sd_len);
762 memcpy( ptr, &req_sd, sizeof(req_sd) );
763 ptr += sizeof(req_sd);
764 memcpy( ptr, owner, req_sd.owner_len );
765 ptr += req_sd.owner_len;
766 memcpy( ptr, group, req_sd.group_len );
767 ptr += req_sd.group_len;
768 memcpy( ptr, sacl, req_sd.sacl_len );
769 ptr += req_sd.sacl_len;
770 memcpy( ptr, dacl, req_sd.dacl_len );
772 else
773 set_error(STATUS_BUFFER_TOO_SMALL);
776 done:
777 release_object( obj );
778 free( label_acl );
781 struct enum_handle_info
783 unsigned int count;
784 struct handle_info *handle;
787 static int enum_handles( struct process *process, void *user )
789 struct enum_handle_info *info = user;
790 struct handle_table *table = process->handles;
791 struct handle_entry *entry;
792 struct handle_info *handle;
793 unsigned int i;
795 if (!table)
796 return 0;
798 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
800 if (!entry->ptr) continue;
801 if (!info->handle)
803 info->count++;
804 continue;
806 assert( info->count );
807 handle = info->handle++;
808 handle->owner = process->id;
809 handle->handle = index_to_handle(i);
810 handle->access = entry->access & ~RESERVED_ALL;
811 info->count--;
814 return 0;
817 DECL_HANDLER(get_system_handles)
819 struct enum_handle_info info;
820 struct handle_info *handle;
821 data_size_t max_handles = get_reply_max_size() / sizeof(*handle);
823 info.handle = NULL;
824 info.count = 0;
825 enum_processes( enum_handles, &info );
826 reply->count = info.count;
828 if (max_handles < info.count)
829 set_error( STATUS_BUFFER_TOO_SMALL );
830 else if ((handle = set_reply_data_size( info.count * sizeof(*handle) )))
832 info.handle = handle;
833 enum_processes( enum_handles, &info );