msvcr120: Add [_]strtoimax[_l] and [_]strtoumax[_l].
[wine.git] / server / handle.c
blob10e6a52e57114c887277bfbb838f3c5daeb2215a
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 /* get/set the handle reserved flags */
490 /* return the old flags (or -1 on error) */
491 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
493 struct handle_entry *entry;
494 unsigned int old_access;
496 if (get_magic_handle( handle ))
498 /* we can retrieve but not set info for magic handles */
499 if (mask) set_error( STATUS_ACCESS_DENIED );
500 return 0;
502 if (!(entry = get_handle( process, handle )))
504 set_error( STATUS_INVALID_HANDLE );
505 return -1;
507 old_access = entry->access;
508 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
509 flags = (flags << RESERVED_SHIFT) & mask;
510 entry->access = (entry->access & ~mask) | flags;
511 return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
514 /* duplicate a handle */
515 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
516 unsigned int access, unsigned int attr, unsigned int options )
518 obj_handle_t res;
519 struct handle_entry *entry;
520 unsigned int src_access;
521 struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
523 if (!obj) return 0;
524 if ((entry = get_handle( src, src_handle )))
525 src_access = entry->access;
526 else /* pseudo-handle, give it full access */
527 src_access = obj->ops->map_access( obj, GENERIC_ALL );
528 src_access &= ~RESERVED_ALL;
530 if (options & DUP_HANDLE_SAME_ACCESS)
531 access = src_access;
532 else
533 access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
535 /* asking for the more access rights than src_access? */
536 if (access & ~src_access)
538 if (options & DUP_HANDLE_MAKE_GLOBAL)
539 res = alloc_global_handle( obj, access );
540 else
541 res = alloc_handle( dst, obj, access, attr );
543 else
545 if (options & DUP_HANDLE_MAKE_GLOBAL)
546 res = alloc_global_handle_no_access_check( obj, access );
547 else if ((options & DUP_HANDLE_CLOSE_SOURCE) && src == dst &&
548 entry && !(entry->access & RESERVED_CLOSE_PROTECT))
550 if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
551 entry->access = access;
552 res = src_handle;
554 else
555 res = alloc_handle_entry( dst, obj, access, attr );
558 release_object( obj );
559 return res;
562 /* open a new handle to an existing object */
563 obj_handle_t open_object( struct process *process, obj_handle_t parent, unsigned int access,
564 const struct object_ops *ops, const struct unicode_str *name,
565 unsigned int attributes )
567 obj_handle_t handle = 0;
568 struct object *obj, *root = NULL;
570 if (name->len >= 65534)
572 set_error( STATUS_OBJECT_NAME_INVALID );
573 return 0;
576 if (parent)
578 if (name->len)
579 root = get_directory_obj( process, parent );
580 else /* opening the object itself can work for non-directories too */
581 root = get_handle_obj( process, parent, 0, NULL );
582 if (!root) return 0;
585 if ((obj = open_named_object( root, ops, name, attributes )))
587 handle = alloc_handle( process, obj, access, attributes );
588 release_object( obj );
590 if (root) release_object( root );
591 return handle;
594 /* return the size of the handle table of a given process */
595 unsigned int get_handle_table_count( struct process *process )
597 if (!process->handles) return 0;
598 return process->handles->count;
601 /* close a handle */
602 DECL_HANDLER(close_handle)
604 unsigned int err = close_handle( current->process, req->handle );
605 set_error( err );
608 /* set a handle information */
609 DECL_HANDLER(set_handle_info)
611 reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
614 /* duplicate a handle */
615 DECL_HANDLER(dup_handle)
617 struct process *src, *dst = NULL;
619 reply->handle = 0;
620 if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
622 if (req->options & DUP_HANDLE_MAKE_GLOBAL)
624 reply->handle = duplicate_handle( src, req->src_handle, NULL,
625 req->access, req->attributes, req->options );
627 else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
629 reply->handle = duplicate_handle( src, req->src_handle, dst,
630 req->access, req->attributes, req->options );
631 release_object( dst );
633 /* close the handle no matter what happened */
634 if ((req->options & DUP_HANDLE_CLOSE_SOURCE) && (src != dst || req->src_handle != reply->handle))
635 reply->closed = !close_handle( src, req->src_handle );
636 reply->self = (src == current->process);
637 release_object( src );
641 DECL_HANDLER(get_object_info)
643 struct object *obj;
644 WCHAR *name;
646 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
648 reply->access = get_handle_access( current->process, req->handle );
649 reply->ref_count = obj->refcount;
650 reply->handle_count = obj->handle_count;
651 if ((name = get_object_full_name( obj, &reply->total )))
652 set_reply_data_ptr( name, min( reply->total, get_reply_max_size() ));
653 release_object( obj );
656 DECL_HANDLER(set_security_object)
658 data_size_t sd_size = get_req_data_size();
659 const struct security_descriptor *sd = get_req_data();
660 struct object *obj;
661 unsigned int access = 0;
663 if (!sd_is_valid( sd, sd_size ))
665 set_error( STATUS_ACCESS_VIOLATION );
666 return;
669 if (req->security_info & OWNER_SECURITY_INFORMATION ||
670 req->security_info & GROUP_SECURITY_INFORMATION ||
671 req->security_info & LABEL_SECURITY_INFORMATION)
672 access |= WRITE_OWNER;
673 if (req->security_info & SACL_SECURITY_INFORMATION)
674 access |= ACCESS_SYSTEM_SECURITY;
675 if (req->security_info & DACL_SECURITY_INFORMATION)
676 access |= WRITE_DAC;
678 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
680 obj->ops->set_sd( obj, sd, req->security_info );
681 release_object( obj );
684 DECL_HANDLER(get_security_object)
686 const struct security_descriptor *sd;
687 struct object *obj;
688 unsigned int access = READ_CONTROL;
689 struct security_descriptor req_sd;
690 int present;
691 const SID *owner, *group;
692 const ACL *sacl, *dacl;
693 ACL *label_acl = NULL;
695 if (req->security_info & SACL_SECURITY_INFORMATION)
696 access |= ACCESS_SYSTEM_SECURITY;
698 if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return;
700 sd = obj->ops->get_sd( obj );
701 if (sd)
703 req_sd.control = sd->control & ~SE_SELF_RELATIVE;
705 owner = sd_get_owner( sd );
706 if (req->security_info & OWNER_SECURITY_INFORMATION)
707 req_sd.owner_len = sd->owner_len;
708 else
709 req_sd.owner_len = 0;
711 group = sd_get_group( sd );
712 if (req->security_info & GROUP_SECURITY_INFORMATION)
713 req_sd.group_len = sd->group_len;
714 else
715 req_sd.group_len = 0;
717 sacl = sd_get_sacl( sd, &present );
718 if (req->security_info & SACL_SECURITY_INFORMATION && present)
719 req_sd.sacl_len = sd->sacl_len;
720 else if (req->security_info & LABEL_SECURITY_INFORMATION && present && sacl)
722 if (!(label_acl = extract_security_labels( sacl ))) goto done;
723 req_sd.sacl_len = label_acl->AclSize;
724 sacl = label_acl;
726 else
727 req_sd.sacl_len = 0;
729 dacl = sd_get_dacl( sd, &present );
730 if (req->security_info & DACL_SECURITY_INFORMATION && present)
731 req_sd.dacl_len = sd->dacl_len;
732 else
733 req_sd.dacl_len = 0;
735 reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len +
736 req_sd.sacl_len + req_sd.dacl_len;
737 if (reply->sd_len <= get_reply_max_size())
739 char *ptr = set_reply_data_size(reply->sd_len);
741 memcpy( ptr, &req_sd, sizeof(req_sd) );
742 ptr += sizeof(req_sd);
743 memcpy( ptr, owner, req_sd.owner_len );
744 ptr += req_sd.owner_len;
745 memcpy( ptr, group, req_sd.group_len );
746 ptr += req_sd.group_len;
747 memcpy( ptr, sacl, req_sd.sacl_len );
748 ptr += req_sd.sacl_len;
749 memcpy( ptr, dacl, req_sd.dacl_len );
751 else
752 set_error(STATUS_BUFFER_TOO_SMALL);
755 done:
756 release_object( obj );
757 free( label_acl );
760 struct enum_handle_info
762 unsigned int count;
763 struct handle_info *handle;
766 static int enum_handles( struct process *process, void *user )
768 struct enum_handle_info *info = user;
769 struct handle_table *table = process->handles;
770 struct handle_entry *entry;
771 struct handle_info *handle;
772 unsigned int i;
774 if (!table)
775 return 0;
777 for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
779 if (!entry->ptr) continue;
780 if (!info->handle)
782 info->count++;
783 continue;
785 assert( info->count );
786 handle = info->handle++;
787 handle->owner = process->id;
788 handle->handle = index_to_handle(i);
789 handle->access = entry->access & ~RESERVED_ALL;
790 info->count--;
793 return 0;
796 DECL_HANDLER(get_system_handles)
798 struct enum_handle_info info;
799 struct handle_info *handle;
800 data_size_t max_handles = get_reply_max_size() / sizeof(*handle);
802 info.handle = NULL;
803 info.count = 0;
804 enum_processes( enum_handles, &info );
805 reply->count = info.count;
807 if (max_handles < info.count)
808 set_error( STATUS_BUFFER_TOO_SMALL );
809 else if ((handle = set_reply_data_size( info.count * sizeof(*handle) )))
811 info.handle = handle;
812 enum_processes( enum_handles, &info );
816 DECL_HANDLER(make_temporary)
818 struct object *obj;
820 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
822 if (obj->is_permanent)
824 make_object_temporary( obj );
825 release_object( obj );
827 release_object( obj );