2 * Server-side handle management
4 * Copyright (C) 1998 Alexandre Julliard
26 static struct process
*initial_process
;
28 /* reserved handle access rights */
29 #define RESERVED_SHIFT 25
30 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
31 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
32 #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)
34 /* global handle macros */
35 #define HANDLE_OBFUSCATOR 0x544a4def
36 #define HANDLE_IS_GLOBAL(h) (((h) ^ HANDLE_OBFUSCATOR) < 0x10000)
37 #define HANDLE_LOCAL_TO_GLOBAL(h) ((h) ^ HANDLE_OBFUSCATOR)
38 #define HANDLE_GLOBAL_TO_LOCAL(h) ((h) ^ HANDLE_OBFUSCATOR)
40 #define MIN_HANDLE_ENTRIES 32
42 /* grow a handle table */
43 /* return 1 if OK, 0 on error */
44 static int grow_handle_table( struct handle_table
*table
)
46 struct handle_entry
*new_entries
;
47 int count
= table
->count
;
49 if (count
>= INT_MAX
/ 2) return 0;
51 if (!(new_entries
= realloc( table
->entries
, count
* sizeof(struct handle_entry
) )))
53 SET_ERROR( ERROR_OUTOFMEMORY
);
57 table
->entries
= new_entries
;
61 /* allocate a handle for an object, incrementing its refcount */
62 /* return the handle, or -1 on error */
63 int alloc_handle( struct process
*process
, void *obj
, unsigned int access
,
66 struct handle_table
*table
= get_process_handles( process
);
67 struct handle_entry
*entry
;
70 assert( !(access
& RESERVED_ALL
) );
71 if (inherit
) access
|= RESERVED_INHERIT
;
73 /* find the first free entry */
75 if (!(entry
= table
->entries
)) return -1;
76 for (handle
= 0; handle
<= table
->last
; handle
++, entry
++)
77 if (!entry
->ptr
) goto found
;
79 if (handle
>= table
->count
)
81 if (!grow_handle_table( table
)) return -1;
82 entry
= table
->entries
+ handle
; /* the table may have moved */
87 entry
->ptr
= grab_object( obj
);
88 entry
->access
= access
;
89 return handle
+ 1; /* avoid handle 0 */
92 /* return an handle entry, or NULL if the handle is invalid */
93 static struct handle_entry
*get_handle( struct process
*process
, int handle
)
95 struct handle_table
*table
;
96 struct handle_entry
*entry
;
98 if (HANDLE_IS_GLOBAL(handle
))
100 handle
= HANDLE_GLOBAL_TO_LOCAL(handle
);
101 process
= initial_process
;
103 table
= get_process_handles( process
);
104 handle
--; /* handles start at 1 */
105 if ((handle
< 0) || (handle
> table
->last
)) goto error
;
106 entry
= table
->entries
+ handle
;
107 if (!entry
->ptr
) goto error
;
111 SET_ERROR( ERROR_INVALID_HANDLE
);
115 /* attempt to shrink a table */
116 /* return 1 if OK, 0 on error */
117 static int shrink_handle_table( struct handle_table
*table
)
119 struct handle_entry
*new_entries
;
120 struct handle_entry
*entry
= table
->entries
+ table
->last
;
121 int count
= table
->count
;
123 while (table
->last
>= 0)
125 if (entry
->ptr
) break;
129 if (table
->last
>= count
/ 4) return 1; /* no need to shrink */
130 if (count
< MIN_HANDLE_ENTRIES
* 2) return 1; /* too small to shrink */
132 if (!(new_entries
= realloc( table
->entries
,
133 count
* sizeof(struct handle_entry
) )))
135 table
->count
= count
;
136 table
->entries
= new_entries
;
140 /* copy the handle table of the parent process */
141 /* return 1 if OK, 0 on error */
142 int copy_handle_table( struct process
*process
, struct process
*parent
)
144 struct handle_table
*parent_table
;
145 struct handle_table
*table
= get_process_handles( process
);
146 struct handle_entry
*ptr
;
149 if (!parent
) /* first process */
151 if (!initial_process
) initial_process
= process
;
153 count
= MIN_HANDLE_ENTRIES
;
158 parent_table
= get_process_handles( parent
);
159 assert( parent_table
->entries
);
160 count
= parent_table
->count
;
161 last
= parent_table
->last
;
164 if (!(ptr
= mem_alloc( count
* sizeof(struct handle_entry
)))) return 0;
165 table
->entries
= ptr
;
166 table
->count
= count
;
171 memcpy( ptr
, parent_table
->entries
, (last
+ 1) * sizeof(struct handle_entry
) );
172 for (i
= 0; i
<= last
; i
++, ptr
++)
174 if (!ptr
->ptr
) continue;
175 if (ptr
->access
& RESERVED_INHERIT
) grab_object( ptr
->ptr
);
176 else ptr
->ptr
= NULL
; /* don't inherit this entry */
179 /* attempt to shrink the table */
180 shrink_handle_table( table
);
184 /* close a handle and decrement the refcount of the associated object */
185 /* return 1 if OK, 0 on error */
186 int close_handle( struct process
*process
, int handle
)
188 struct handle_table
*table
;
189 struct handle_entry
*entry
;
192 if (HANDLE_IS_GLOBAL(handle
))
194 handle
= HANDLE_GLOBAL_TO_LOCAL(handle
);
195 process
= initial_process
;
197 table
= get_process_handles( process
);
198 if (!(entry
= get_handle( process
, handle
))) return 0;
199 if (entry
->access
& RESERVED_CLOSE_PROTECT
) return 0; /* FIXME: error code */
202 if (handle
-1 == table
->last
) shrink_handle_table( table
);
203 release_object( obj
);
207 /* retrieve the object corresponding to a handle, incrementing its refcount */
208 struct object
*get_handle_obj( struct process
*process
, int handle
,
209 unsigned int access
, const struct object_ops
*ops
)
211 struct handle_entry
*entry
;
216 case 0xfffffffe: /* current thread pseudo-handle */
219 case 0x7fffffff: /* current process pseudo-handle */
220 obj
= (struct object
*)current
->process
;
223 if (!(entry
= get_handle( process
, handle
))) return NULL
;
224 if ((entry
->access
& access
) != access
)
226 SET_ERROR( ERROR_ACCESS_DENIED
);
232 if (ops
&& (obj
->ops
!= ops
))
234 SET_ERROR( ERROR_INVALID_HANDLE
); /* not the right type */
237 return grab_object( obj
);
240 /* get/set the handle reserved flags */
241 /* return the new flags (or -1 on error) */
242 static int set_handle_info( struct process
*process
, int handle
, int mask
, int flags
)
244 struct handle_entry
*entry
;
246 if (!(entry
= get_handle( process
, handle
))) return -1;
247 mask
= (mask
<< RESERVED_SHIFT
) & RESERVED_ALL
;
248 flags
= (flags
<< RESERVED_SHIFT
) & mask
;
249 entry
->access
= (entry
->access
& ~mask
) | flags
;
250 return (entry
->access
& RESERVED_ALL
) >> RESERVED_SHIFT
;
253 /* duplicate a handle */
254 int duplicate_handle( struct process
*src
, int src_handle
, struct process
*dst
,
255 unsigned int access
, int inherit
, int options
)
258 struct handle_entry
*entry
= get_handle( src
, src_handle
);
259 if (!entry
) return -1;
261 if (options
& DUP_HANDLE_SAME_ACCESS
) access
= entry
->access
;
262 if (options
& DUP_HANDLE_MAKE_GLOBAL
) dst
= initial_process
;
263 access
&= ~RESERVED_ALL
;
264 res
= alloc_handle( dst
, entry
->ptr
, access
, inherit
);
265 if (options
& DUP_HANDLE_MAKE_GLOBAL
) res
= HANDLE_LOCAL_TO_GLOBAL(res
);
269 /* free the process handle entries */
270 void free_handles( struct process
*process
)
272 struct handle_table
*table
= get_process_handles( process
);
273 struct handle_entry
*entry
;
276 if (!(entry
= table
->entries
)) return;
277 for (handle
= 0; handle
<= table
->last
; handle
++, entry
++)
279 struct object
*obj
= entry
->ptr
;
281 if (obj
) release_object( obj
);
283 free( table
->entries
);
286 table
->entries
= NULL
;
289 /* open a new handle to an existing object */
290 int open_object( const char *name
, const struct object_ops
*ops
,
291 unsigned int access
, int inherit
)
293 struct object
*obj
= find_object( name
);
296 SET_ERROR( ERROR_FILE_NOT_FOUND
);
299 if (ops
&& obj
->ops
!= ops
)
301 release_object( obj
);
302 SET_ERROR( ERROR_INVALID_HANDLE
); /* FIXME: not the right type */
305 return alloc_handle( current
->process
, obj
, access
, inherit
);
308 /* dump a handle table on stdout */
309 void dump_handles( struct process
*process
)
311 struct handle_table
*table
= get_process_handles( process
);
312 struct handle_entry
*entry
;
315 if (!table
->entries
) return;
316 entry
= table
->entries
;
317 for (i
= 0; i
<= table
->last
; i
++, entry
++)
319 if (!entry
->ptr
) continue;
320 printf( "%5d: %p %08x ", i
+ 1, entry
->ptr
, entry
->access
);
321 entry
->ptr
->ops
->dump( entry
->ptr
, 0 );
326 DECL_HANDLER(close_handle
)
328 close_handle( current
->process
, req
->handle
);
329 send_reply( current
, -1, 0 );
332 /* get information about a handle */
333 DECL_HANDLER(get_handle_info
)
335 struct get_handle_info_reply reply
;
336 reply
.flags
= set_handle_info( current
->process
, req
->handle
, 0, 0 );
337 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );
340 /* set a handle information */
341 DECL_HANDLER(set_handle_info
)
343 set_handle_info( current
->process
, req
->handle
, req
->mask
, req
->flags
);
344 send_reply( current
, -1, 0 );
347 /* duplicate a handle */
348 DECL_HANDLER(dup_handle
)
350 struct dup_handle_reply reply
= { -1 };
351 struct process
*src
, *dst
;
353 if ((src
= get_process_from_handle( req
->src_process
, PROCESS_DUP_HANDLE
)))
355 if (req
->options
& DUP_HANDLE_MAKE_GLOBAL
)
357 reply
.handle
= duplicate_handle( src
, req
->src_handle
, NULL
,
358 req
->access
, req
->inherit
, req
->options
);
360 else if ((dst
= get_process_from_handle( req
->dst_process
, PROCESS_DUP_HANDLE
)))
362 reply
.handle
= duplicate_handle( src
, req
->src_handle
, dst
,
363 req
->access
, req
->inherit
, req
->options
);
364 release_object( dst
);
366 /* close the handle no matter what happened */
367 if (req
->options
& DUP_HANDLE_CLOSE_SOURCE
)
368 close_handle( src
, req
->src_handle
);
369 release_object( src
);
371 send_reply( current
, -1, 1, &reply
, sizeof(reply
) );