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
22 #include "wine/port.h"
32 #define WIN32_NO_STATUS
44 struct object
*ptr
; /* object */
45 unsigned int access
; /* access rights */
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
)
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
);
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 */
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_close_handle
, /* close_handle */
137 handle_table_destroy
/* destroy */
140 /* dump a handle table */
141 static void handle_table_dump( struct object
*obj
, int verbose
)
144 struct handle_table
*table
= (struct handle_table
*)obj
;
145 struct handle_entry
*entry
;
147 assert( obj
->ops
== &handle_table_ops
);
149 fprintf( stderr
, "Handle table last=%d count=%d process=%p\n",
150 table
->last
, table
->count
, table
->process
);
151 if (!verbose
) return;
152 entry
= table
->entries
;
153 for (i
= 0; i
<= table
->last
; i
++, entry
++)
155 if (!entry
->ptr
) continue;
156 fprintf( stderr
, " %04x: %p %08x ",
157 index_to_handle(i
), entry
->ptr
, entry
->access
);
158 dump_object_name( entry
->ptr
);
159 entry
->ptr
->ops
->dump( entry
->ptr
, 0 );
163 /* destroy a handle table */
164 static void handle_table_destroy( struct object
*obj
)
167 struct handle_table
*table
= (struct handle_table
*)obj
;
168 struct handle_entry
*entry
;
170 assert( obj
->ops
== &handle_table_ops
);
172 /* first notify all objects that handles are being closed */
175 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
177 struct object
*obj
= entry
->ptr
;
178 if (obj
) obj
->ops
->close_handle( obj
, table
->process
, index_to_handle(i
) );
182 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
184 struct object
*obj
= entry
->ptr
;
186 if (obj
) 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
)))
208 table
->process
= process
;
209 table
->count
= count
;
212 if ((table
->entries
= mem_alloc( count
* sizeof(*table
->entries
) ))) return table
;
213 release_object( table
);
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
);
229 table
->entries
= new_entries
;
230 table
->count
= count
;
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
;
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 */
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
);
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 access
= obj
->ops
->map_access( obj
, access
) & ~RESERVED_ALL
;
276 return alloc_handle_entry( process
, ptr
, access
, attr
);
279 /* allocate a handle for an object, checking the dacl allows the process to */
280 /* access it and incrementing its refcount */
281 /* return the handle, or 0 on error */
282 obj_handle_t
alloc_handle( struct process
*process
, void *ptr
, unsigned int access
, unsigned int attr
)
284 struct object
*obj
= ptr
;
285 access
= obj
->ops
->map_access( obj
, access
) & ~RESERVED_ALL
;
286 if (access
&& !check_object_access( obj
, &access
)) return 0;
287 return alloc_handle_entry( process
, ptr
, access
, attr
);
290 /* allocate a global handle for an object, incrementing its refcount */
291 /* return the handle, or 0 on error */
292 static obj_handle_t
alloc_global_handle_no_access_check( void *obj
, unsigned int access
)
296 if (!(global_table
= alloc_handle_table( NULL
, 0 )))
298 make_object_static( &global_table
->obj
);
300 return handle_local_to_global( alloc_entry( global_table
, obj
, access
));
303 /* allocate a global handle for an object, checking the dacl allows the */
304 /* process to access it and incrementing its refcount and incrementing its refcount */
305 /* return the handle, or 0 on error */
306 static obj_handle_t
alloc_global_handle( void *obj
, unsigned int access
)
308 if (access
&& !check_object_access( obj
, &access
)) return 0;
309 return alloc_global_handle_no_access_check( obj
, access
);
312 /* return a handle entry, or NULL if the handle is invalid */
313 static struct handle_entry
*get_handle( struct process
*process
, obj_handle_t handle
)
315 struct handle_table
*table
= process
->handles
;
316 struct handle_entry
*entry
;
319 if (handle_is_global(handle
))
321 handle
= handle_global_to_local(handle
);
322 table
= global_table
;
324 if (!table
) return NULL
;
325 index
= handle_to_index( handle
);
326 if (index
< 0) return NULL
;
327 if (index
> table
->last
) return NULL
;
328 entry
= table
->entries
+ index
;
329 if (!entry
->ptr
) return NULL
;
333 /* attempt to shrink a table */
334 static void shrink_handle_table( struct handle_table
*table
)
336 struct handle_entry
*entry
= table
->entries
+ table
->last
;
337 struct handle_entry
*new_entries
;
338 int count
= table
->count
;
340 while (table
->last
>= 0)
342 if (entry
->ptr
) break;
346 if (table
->last
>= count
/ 4) return; /* no need to shrink */
347 if (count
< MIN_HANDLE_ENTRIES
* 2) return; /* too small to shrink */
349 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(*new_entries
) ))) return;
350 table
->count
= count
;
351 table
->entries
= new_entries
;
354 /* copy the handle table of the parent process */
355 /* return 1 if OK, 0 on error */
356 struct handle_table
*copy_handle_table( struct process
*process
, struct process
*parent
)
358 struct handle_table
*parent_table
= parent
->handles
;
359 struct handle_table
*table
;
362 assert( parent_table
);
363 assert( parent_table
->obj
.ops
== &handle_table_ops
);
365 if (!(table
= alloc_handle_table( process
, parent_table
->count
)))
368 if ((table
->last
= parent_table
->last
) >= 0)
370 struct handle_entry
*ptr
= table
->entries
;
371 memcpy( ptr
, parent_table
->entries
, (table
->last
+ 1) * sizeof(struct handle_entry
) );
372 for (i
= 0; i
<= table
->last
; i
++, ptr
++)
374 if (!ptr
->ptr
) continue;
375 if (ptr
->access
& RESERVED_INHERIT
) grab_object_for_handle( ptr
->ptr
);
376 else ptr
->ptr
= NULL
; /* don't inherit this entry */
379 /* attempt to shrink the table */
380 shrink_handle_table( table
);
384 /* close a handle and decrement the refcount of the associated object */
385 unsigned int close_handle( struct process
*process
, obj_handle_t handle
)
387 struct handle_table
*table
;
388 struct handle_entry
*entry
;
391 if (!(entry
= get_handle( process
, handle
))) return STATUS_INVALID_HANDLE
;
392 if (entry
->access
& RESERVED_CLOSE_PROTECT
) return STATUS_HANDLE_NOT_CLOSABLE
;
394 if (!obj
->ops
->close_handle( obj
, process
, handle
)) return STATUS_HANDLE_NOT_CLOSABLE
;
396 table
= handle_is_global(handle
) ? global_table
: process
->handles
;
397 if (entry
< table
->entries
+ table
->free
) table
->free
= entry
- table
->entries
;
398 if (entry
== table
->entries
+ table
->last
) shrink_handle_table( table
);
399 release_object_from_handle( obj
);
400 return STATUS_SUCCESS
;
403 /* retrieve the object corresponding to one of the magic pseudo-handles */
404 static inline struct object
*get_magic_handle( obj_handle_t handle
)
408 case 0xfffffffe: /* current thread pseudo-handle */
409 return ¤t
->obj
;
410 case 0x7fffffff: /* current process pseudo-handle */
411 case 0xffffffff: /* current process pseudo-handle */
412 return (struct object
*)current
->process
;
418 /* retrieve the object corresponding to a handle, incrementing its refcount */
419 struct object
*get_handle_obj( struct process
*process
, obj_handle_t handle
,
420 unsigned int access
, const struct object_ops
*ops
)
422 struct handle_entry
*entry
;
425 if (!(obj
= get_magic_handle( handle
)))
427 if (!(entry
= get_handle( process
, handle
)))
429 set_error( STATUS_INVALID_HANDLE
);
433 if (ops
&& (obj
->ops
!= ops
))
435 set_error( STATUS_OBJECT_TYPE_MISMATCH
); /* not the right type */
438 if ((entry
->access
& access
) != access
)
440 set_error( STATUS_ACCESS_DENIED
);
444 else if (ops
&& (obj
->ops
!= ops
))
446 set_error( STATUS_OBJECT_TYPE_MISMATCH
); /* not the right type */
449 return grab_object( obj
);
452 /* retrieve the access rights of a given handle */
453 unsigned int get_handle_access( struct process
*process
, obj_handle_t handle
)
455 struct handle_entry
*entry
;
457 if (get_magic_handle( handle
)) return ~RESERVED_ALL
; /* magic handles have all access rights */
458 if (!(entry
= get_handle( process
, handle
))) return 0;
459 return entry
->access
& ~RESERVED_ALL
;
462 /* find the first inherited handle of the given type */
463 /* this is needed for window stations and desktops (don't ask...) */
464 obj_handle_t
find_inherited_handle( struct process
*process
, const struct object_ops
*ops
)
466 struct handle_table
*table
= process
->handles
;
467 struct handle_entry
*ptr
;
470 if (!table
) return 0;
472 for (i
= 0, ptr
= table
->entries
; i
<= table
->last
; i
++, ptr
++)
474 if (!ptr
->ptr
) continue;
475 if (ptr
->ptr
->ops
!= ops
) continue;
476 if (ptr
->access
& RESERVED_INHERIT
) return index_to_handle(i
);
481 /* enumerate handles of a given type */
482 /* this is needed for window stations and desktops */
483 obj_handle_t
enumerate_handles( struct process
*process
, const struct object_ops
*ops
,
484 unsigned int *index
)
486 struct handle_table
*table
= process
->handles
;
488 struct handle_entry
*entry
;
490 if (!table
) return 0;
492 for (i
= *index
, entry
= &table
->entries
[i
]; i
<= table
->last
; i
++, entry
++)
494 if (!entry
->ptr
) continue;
495 if (entry
->ptr
->ops
!= ops
) continue;
497 return index_to_handle(i
);
502 /* get/set the handle reserved flags */
503 /* return the old flags (or -1 on error) */
504 static int set_handle_flags( struct process
*process
, obj_handle_t handle
, int mask
, int flags
)
506 struct handle_entry
*entry
;
507 unsigned int old_access
;
509 if (get_magic_handle( handle
))
511 /* we can retrieve but not set info for magic handles */
512 if (mask
) set_error( STATUS_ACCESS_DENIED
);
515 if (!(entry
= get_handle( process
, handle
)))
517 set_error( STATUS_INVALID_HANDLE
);
520 old_access
= entry
->access
;
521 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
522 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
523 entry
->access
= (entry
->access
& ~mask
) | flags
;
524 return (old_access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
527 /* duplicate a handle */
528 obj_handle_t
duplicate_handle( struct process
*src
, obj_handle_t src_handle
, struct process
*dst
,
529 unsigned int access
, unsigned int attr
, unsigned int options
)
532 struct handle_entry
*entry
;
533 unsigned int src_access
;
534 struct object
*obj
= get_handle_obj( src
, src_handle
, 0, NULL
);
537 if ((entry
= get_handle( src
, src_handle
)))
538 src_access
= entry
->access
;
539 else /* pseudo-handle, give it full access */
540 src_access
= obj
->ops
->map_access( obj
, GENERIC_ALL
);
541 src_access
&= ~RESERVED_ALL
;
543 if (options
& DUP_HANDLE_SAME_ACCESS
)
546 access
= obj
->ops
->map_access( obj
, access
) & ~RESERVED_ALL
;
548 /* asking for the more access rights than src_access? */
549 if (access
& ~src_access
)
551 if (options
& DUP_HANDLE_MAKE_GLOBAL
)
552 res
= alloc_global_handle( obj
, access
);
554 res
= alloc_handle( dst
, obj
, access
, attr
);
558 if (options
& DUP_HANDLE_MAKE_GLOBAL
)
559 res
= alloc_global_handle_no_access_check( obj
, access
);
560 else if ((options
& DUP_HANDLE_CLOSE_SOURCE
) && src
== dst
&&
561 entry
&& !(entry
->access
& RESERVED_CLOSE_PROTECT
))
563 if (attr
& OBJ_INHERIT
) access
|= RESERVED_INHERIT
;
564 entry
->access
= access
;
568 res
= alloc_handle_entry( dst
, obj
, access
, attr
);
571 release_object( obj
);
575 /* open a new handle to an existing object */
576 obj_handle_t
open_object( struct process
*process
, obj_handle_t parent
, unsigned int access
,
577 const struct object_ops
*ops
, const struct unicode_str
*name
,
578 unsigned int attributes
)
580 obj_handle_t handle
= 0;
581 struct directory
*root
= NULL
;
584 if (name
->len
>= 65534)
586 set_error( STATUS_OBJECT_NAME_INVALID
);
590 if (parent
&& !(root
= get_directory_obj( process
, parent
, 0 ))) return 0;
592 if ((obj
= open_object_dir( root
, name
, attributes
, ops
)))
594 handle
= alloc_handle( process
, obj
, access
, attributes
);
595 release_object( obj
);
597 if (root
) release_object( root
);
601 /* return the size of the handle table of a given process */
602 unsigned int get_handle_table_count( struct process
*process
)
604 if (!process
->handles
) return 0;
605 return process
->handles
->count
;
609 DECL_HANDLER(close_handle
)
611 unsigned int err
= close_handle( current
->process
, req
->handle
);
615 /* set a handle information */
616 DECL_HANDLER(set_handle_info
)
618 reply
->old_flags
= set_handle_flags( current
->process
, req
->handle
, req
->mask
, req
->flags
);
621 /* duplicate a handle */
622 DECL_HANDLER(dup_handle
)
624 struct process
*src
, *dst
= NULL
;
627 if ((src
= get_process_from_handle( req
->src_process
, PROCESS_DUP_HANDLE
)))
629 if (req
->options
& DUP_HANDLE_MAKE_GLOBAL
)
631 reply
->handle
= duplicate_handle( src
, req
->src_handle
, NULL
,
632 req
->access
, req
->attributes
, req
->options
);
634 else if ((dst
= get_process_from_handle( req
->dst_process
, PROCESS_DUP_HANDLE
)))
636 reply
->handle
= duplicate_handle( src
, req
->src_handle
, dst
,
637 req
->access
, req
->attributes
, req
->options
);
638 release_object( dst
);
640 /* close the handle no matter what happened */
641 if ((req
->options
& DUP_HANDLE_CLOSE_SOURCE
) && (src
!= dst
|| req
->src_handle
!= reply
->handle
))
642 reply
->closed
= !close_handle( src
, req
->src_handle
);
643 reply
->self
= (src
== current
->process
);
644 release_object( src
);
648 DECL_HANDLER(get_object_info
)
653 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
655 reply
->access
= get_handle_access( current
->process
, req
->handle
);
656 reply
->ref_count
= obj
->refcount
;
657 reply
->handle_count
= obj
->handle_count
;
658 if ((name
= get_object_full_name( obj
, &reply
->total
)))
659 set_reply_data_ptr( name
, min( reply
->total
, get_reply_max_size() ));
660 release_object( obj
);
663 DECL_HANDLER(set_security_object
)
665 data_size_t sd_size
= get_req_data_size();
666 const struct security_descriptor
*sd
= get_req_data();
668 unsigned int access
= 0;
670 if (!sd_is_valid( sd
, sd_size
))
672 set_error( STATUS_ACCESS_VIOLATION
);
676 if (req
->security_info
& OWNER_SECURITY_INFORMATION
||
677 req
->security_info
& GROUP_SECURITY_INFORMATION
)
678 access
|= WRITE_OWNER
;
679 if (req
->security_info
& SACL_SECURITY_INFORMATION
)
680 access
|= ACCESS_SYSTEM_SECURITY
;
681 if (req
->security_info
& DACL_SECURITY_INFORMATION
)
684 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, access
, NULL
))) return;
686 obj
->ops
->set_sd( obj
, sd
, req
->security_info
);
687 release_object( obj
);
690 DECL_HANDLER(get_security_object
)
692 const struct security_descriptor
*sd
;
694 unsigned int access
= READ_CONTROL
;
695 struct security_descriptor req_sd
;
697 const SID
*owner
, *group
;
698 const ACL
*sacl
, *dacl
;
700 if (req
->security_info
& SACL_SECURITY_INFORMATION
)
701 access
|= ACCESS_SYSTEM_SECURITY
;
703 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, access
, NULL
))) return;
705 sd
= obj
->ops
->get_sd( obj
);
708 req_sd
.control
= sd
->control
& ~SE_SELF_RELATIVE
;
710 owner
= sd_get_owner( sd
);
711 if (req
->security_info
& OWNER_SECURITY_INFORMATION
)
712 req_sd
.owner_len
= sd
->owner_len
;
714 req_sd
.owner_len
= 0;
716 group
= sd_get_group( sd
);
717 if (req
->security_info
& GROUP_SECURITY_INFORMATION
)
718 req_sd
.group_len
= sd
->group_len
;
720 req_sd
.group_len
= 0;
722 req_sd
.control
|= SE_SACL_PRESENT
;
723 sacl
= sd_get_sacl( sd
, &present
);
724 if (req
->security_info
& SACL_SECURITY_INFORMATION
&& present
)
725 req_sd
.sacl_len
= sd
->sacl_len
;
729 req_sd
.control
|= SE_DACL_PRESENT
;
730 dacl
= sd_get_dacl( sd
, &present
);
731 if (req
->security_info
& DACL_SECURITY_INFORMATION
&& present
)
732 req_sd
.dacl_len
= sd
->dacl_len
;
736 reply
->sd_len
= sizeof(req_sd
) + req_sd
.owner_len
+ req_sd
.group_len
+
737 req_sd
.sacl_len
+ req_sd
.dacl_len
;
738 if (reply
->sd_len
<= get_reply_max_size())
740 char *ptr
= set_reply_data_size(reply
->sd_len
);
742 memcpy( ptr
, &req_sd
, sizeof(req_sd
) );
743 ptr
+= sizeof(req_sd
);
744 memcpy( ptr
, owner
, req_sd
.owner_len
);
745 ptr
+= req_sd
.owner_len
;
746 memcpy( ptr
, group
, req_sd
.group_len
);
747 ptr
+= req_sd
.group_len
;
748 memcpy( ptr
, sacl
, req_sd
.sacl_len
);
749 ptr
+= req_sd
.sacl_len
;
750 memcpy( ptr
, dacl
, req_sd
.dacl_len
);
753 set_error(STATUS_BUFFER_TOO_SMALL
);
756 release_object( obj
);
759 struct enum_handle_info
762 struct handle_info
*handle
;
765 static int enum_handles( struct process
*process
, void *user
)
767 struct enum_handle_info
*info
= user
;
768 struct handle_table
*table
= process
->handles
;
769 struct handle_entry
*entry
;
770 struct handle_info
*handle
;
776 for (i
= 0, entry
= table
->entries
; i
<= table
->last
; i
++, entry
++)
778 if (!entry
->ptr
) continue;
784 assert( info
->count
);
785 handle
= info
->handle
++;
786 handle
->owner
= process
->id
;
787 handle
->handle
= index_to_handle(i
);
788 handle
->access
= entry
->access
& ~RESERVED_ALL
;
795 DECL_HANDLER(get_system_handles
)
797 struct enum_handle_info info
;
798 struct handle_info
*handle
;
799 data_size_t max_handles
= get_reply_max_size() / sizeof(*handle
);
803 enum_processes( enum_handles
, &info
);
804 reply
->count
= info
.count
;
806 if (max_handles
< info
.count
)
807 set_error( STATUS_BUFFER_TOO_SMALL
);
808 else if ((handle
= set_reply_data_size( info
.count
* sizeof(*handle
) )))
810 info
.handle
= handle
;
811 enum_processes( enum_handles
, &info
);