server: Replace inline static with static inline.
[wine] / server / handle.c
1 /*
2  * Server-side handle management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "security.h"
40 #include "request.h"
41
42 struct handle_entry
43 {
44     struct object *ptr;       /* object */
45     unsigned int   access;    /* access rights */
46 };
47
48 struct handle_table
49 {
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 */
56 };
57
58 static struct handle_table *global_table;
59
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)
65
66 #define MIN_HANDLE_ENTRIES  32
67
68
69 /* handle to table index conversion */
70
71 /* handles are a multiple of 4 under NT; handle 0 is not used */
72 static inline obj_handle_t index_to_handle( int index )
73 {
74     return (obj_handle_t)((unsigned long)(index + 1) << 2);
75 }
76 static inline int handle_to_index( obj_handle_t handle )
77 {
78     return ((unsigned long)handle >> 2) - 1;
79 }
80
81 /* global handle conversion */
82
83 #define HANDLE_OBFUSCATOR 0x544a4def
84
85 static inline int handle_is_global( obj_handle_t handle)
86 {
87     return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
88 }
89 static inline obj_handle_t handle_local_to_global( obj_handle_t handle )
90 {
91     if (!handle) return 0;
92     return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
93 }
94 static inline obj_handle_t handle_global_to_local( obj_handle_t handle )
95 {
96     return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
97 }
98
99
100 static void handle_table_dump( struct object *obj, int verbose );
101 static void handle_table_destroy( struct object *obj );
102
103 static const struct object_ops handle_table_ops =
104 {
105     sizeof(struct handle_table),     /* size */
106     handle_table_dump,               /* dump */
107     no_add_queue,                    /* add_queue */
108     NULL,                            /* remove_queue */
109     NULL,                            /* signaled */
110     NULL,                            /* satisfied */
111     no_signal,                       /* signal */
112     no_get_fd,                       /* get_fd */
113     no_map_access,                   /* map_access */
114     no_lookup_name,                  /* lookup_name */
115     no_close_handle,                 /* close_handle */
116     handle_table_destroy             /* destroy */
117 };
118
119 /* dump a handle table */
120 static void handle_table_dump( struct object *obj, int verbose )
121 {
122     int i;
123     struct handle_table *table = (struct handle_table *)obj;
124     struct handle_entry *entry = table->entries;
125
126     assert( obj->ops == &handle_table_ops );
127
128     fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
129              table->last, table->count, table->process );
130     if (!verbose) return;
131     entry = table->entries;
132     for (i = 0; i <= table->last; i++, entry++)
133     {
134         if (!entry->ptr) continue;
135         fprintf( stderr, "    %p: %p %08x ",
136                  index_to_handle(i), entry->ptr, entry->access );
137         entry->ptr->ops->dump( entry->ptr, 0 );
138     }
139 }
140
141 /* destroy a handle table */
142 static void handle_table_destroy( struct object *obj )
143 {
144     int i;
145     struct handle_table *table = (struct handle_table *)obj;
146     struct handle_entry *entry;
147
148     assert( obj->ops == &handle_table_ops );
149
150     /* first notify all objects that handles are being closed */
151     if (table->process)
152     {
153         for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
154         {
155             struct object *obj = entry->ptr;
156             if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
157         }
158     }
159
160     for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
161     {
162         struct object *obj = entry->ptr;
163         entry->ptr = NULL;
164         if (obj) release_object( obj );
165     }
166     free( table->entries );
167 }
168
169 /* allocate a new handle table */
170 struct handle_table *alloc_handle_table( struct process *process, int count )
171 {
172     struct handle_table *table;
173
174     if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
175     if (!(table = alloc_object( &handle_table_ops )))
176         return NULL;
177     table->process = process;
178     table->count   = count;
179     table->last    = -1;
180     table->free    = 0;
181     if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
182     release_object( table );
183     return NULL;
184 }
185
186 /* grow a handle table */
187 static int grow_handle_table( struct handle_table *table )
188 {
189     struct handle_entry *new_entries;
190     int count = table->count;
191
192     if (count >= INT_MAX / 2) return 0;
193     count *= 2;
194     if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
195     {
196         set_error( STATUS_NO_MEMORY );
197         return 0;
198     }
199     table->entries = new_entries;
200     table->count   = count;
201     return 1;
202 }
203
204 /* allocate the first free entry in the handle table */
205 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
206 {
207     struct handle_entry *entry = table->entries + table->free;
208     int i;
209
210     for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
211     if (i >= table->count)
212     {
213         if (!grow_handle_table( table )) return 0;
214         entry = table->entries + i;  /* the entries may have moved */
215     }
216     table->last = i;
217  found:
218     table->free = i + 1;
219     entry->ptr    = grab_object( obj );
220     entry->access = access;
221     return index_to_handle(i);
222 }
223
224 /* allocate a handle for an object, incrementing its refcount */
225 /* return the handle, or 0 on error */
226 static obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr )
227 {
228     struct object *obj = ptr;
229
230     access &= ~RESERVED_ALL;
231     if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
232     if (!process->handles)
233     {
234         set_error( STATUS_NO_MEMORY );
235         return 0;
236     }
237     return alloc_entry( process->handles, obj, access );
238 }
239
240 /* allocate a handle for an object, checking the dacl allows the process to */
241 /* access it and incrementing its refcount */
242 /* return the handle, or 0 on error */
243 obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
244 {
245     struct object *obj = ptr;
246     access = obj->ops->map_access( obj, access );
247     if (access && !check_object_access( obj, &access )) return 0;
248     return alloc_handle_no_access_check( process, ptr, access, attr );
249 }
250
251 /* allocate a global handle for an object, incrementing its refcount */
252 /* return the handle, or 0 on error */
253 static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access )
254 {
255     if (!global_table)
256     {
257         if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
258             return 0;
259         make_object_static( &global_table->obj );
260     }
261     return handle_local_to_global( alloc_entry( global_table, obj, access ));
262 }
263
264 /* allocate a global handle for an object, checking the dacl allows the */
265 /* process to access it and incrementing its refcount and incrementing its refcount */
266 /* return the handle, or 0 on error */
267 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
268 {
269     if (access && !check_object_access( obj, &access )) return 0;
270     return alloc_global_handle_no_access_check( obj, access );
271 }
272
273 /* return a handle entry, or NULL if the handle is invalid */
274 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
275 {
276     struct handle_table *table = process->handles;
277     struct handle_entry *entry;
278     int index;
279
280     if (handle_is_global(handle))
281     {
282         handle = handle_global_to_local(handle);
283         table = global_table;
284     }
285     if (!table) goto error;
286     index = handle_to_index( handle );
287     if (index < 0) goto error;
288     if (index > table->last) goto error;
289     entry = table->entries + index;
290     if (!entry->ptr) goto error;
291     return entry;
292
293  error:
294     set_error( STATUS_INVALID_HANDLE );
295     return NULL;
296 }
297
298 /* attempt to shrink a table */
299 static void shrink_handle_table( struct handle_table *table )
300 {
301     struct handle_entry *entry = table->entries + table->last;
302     struct handle_entry *new_entries;
303     int count = table->count;
304
305     while (table->last >= 0)
306     {
307         if (entry->ptr) break;
308         table->last--;
309         entry--;
310     }
311     if (table->last >= count / 4) return;  /* no need to shrink */
312     if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
313     count /= 2;
314     if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
315     table->count   = count;
316     table->entries = new_entries;
317 }
318
319 /* copy the handle table of the parent process */
320 /* return 1 if OK, 0 on error */
321 struct handle_table *copy_handle_table( struct process *process, struct process *parent )
322 {
323     struct handle_table *parent_table = parent->handles;
324     struct handle_table *table;
325     int i;
326
327     assert( parent_table );
328     assert( parent_table->obj.ops == &handle_table_ops );
329
330     if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
331         return NULL;
332
333     if ((table->last = parent_table->last) >= 0)
334     {
335         struct handle_entry *ptr = table->entries;
336         memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
337         for (i = 0; i <= table->last; i++, ptr++)
338         {
339             if (!ptr->ptr) continue;
340             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
341             else ptr->ptr = NULL; /* don't inherit this entry */
342         }
343     }
344     /* attempt to shrink the table */
345     shrink_handle_table( table );
346     return table;
347 }
348
349 /* close a handle and decrement the refcount of the associated object */
350 /* return 1 if OK, 0 on error */
351 int close_handle( struct process *process, obj_handle_t handle )
352 {
353     struct handle_table *table;
354     struct handle_entry *entry;
355     struct object *obj;
356
357     if (!(entry = get_handle( process, handle ))) return 0;
358     if (entry->access & RESERVED_CLOSE_PROTECT)
359     {
360         set_error( STATUS_HANDLE_NOT_CLOSABLE );
361         return 0;
362     }
363     obj = entry->ptr;
364     if (!obj->ops->close_handle( obj, process, handle ))
365     {
366         set_error( STATUS_HANDLE_NOT_CLOSABLE );
367         return 0;
368     }
369     entry->ptr = NULL;
370     table = handle_is_global(handle) ? global_table : process->handles;
371     if (entry < table->entries + table->free) table->free = entry - table->entries;
372     if (entry == table->entries + table->last) shrink_handle_table( table );
373     release_object( obj );
374     return 1;
375 }
376
377 /* retrieve the object corresponding to one of the magic pseudo-handles */
378 static inline struct object *get_magic_handle( obj_handle_t handle )
379 {
380     switch((unsigned long)handle)
381     {
382         case 0xfffffffe:  /* current thread pseudo-handle */
383             return &current->obj;
384         case 0x7fffffff:  /* current process pseudo-handle */
385         case 0xffffffff:  /* current process pseudo-handle */
386             return (struct object *)current->process;
387         default:
388             return NULL;
389     }
390 }
391
392 /* retrieve the object corresponding to a handle, incrementing its refcount */
393 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
394                                unsigned int access, const struct object_ops *ops )
395 {
396     struct handle_entry *entry;
397     struct object *obj;
398
399     if (!(obj = get_magic_handle( handle )))
400     {
401         if (!(entry = get_handle( process, handle ))) return NULL;
402         if ((entry->access & access) != access)
403         {
404             set_error( STATUS_ACCESS_DENIED );
405             return NULL;
406         }
407         obj = entry->ptr;
408     }
409     if (ops && (obj->ops != ops))
410     {
411         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
412         return NULL;
413     }
414     return grab_object( obj );
415 }
416
417 /* retrieve the access rights of a given handle */
418 unsigned int get_handle_access( struct process *process, obj_handle_t handle )
419 {
420     struct handle_entry *entry;
421
422     if (get_magic_handle( handle )) return ~RESERVED_ALL;  /* magic handles have all access rights */
423     if (!(entry = get_handle( process, handle ))) return 0;
424     return entry->access & ~RESERVED_ALL;
425 }
426
427 /* find the first inherited handle of the given type */
428 /* this is needed for window stations and desktops (don't ask...) */
429 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
430 {
431     struct handle_table *table = process->handles;
432     struct handle_entry *ptr;
433     int i;
434
435     if (!table) return 0;
436
437     for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
438     {
439         if (!ptr->ptr) continue;
440         if (ptr->ptr->ops != ops) continue;
441         if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
442     }
443     return 0;
444 }
445
446 /* get/set the handle reserved flags */
447 /* return the old flags (or -1 on error) */
448 static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
449 {
450     struct handle_entry *entry;
451     unsigned int old_access;
452
453     if (get_magic_handle( handle ))
454     {
455         /* we can retrieve but not set info for magic handles */
456         if (mask) set_error( STATUS_ACCESS_DENIED );
457         return 0;
458     }
459     if (!(entry = get_handle( process, handle ))) return -1;
460     old_access = entry->access;
461     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
462     flags = (flags << RESERVED_SHIFT) & mask;
463     entry->access = (entry->access & ~mask) | flags;
464     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
465 }
466
467 /* duplicate a handle */
468 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
469                                unsigned int access, unsigned int attr, unsigned int options )
470 {
471     obj_handle_t res;
472     struct handle_entry *entry;
473     unsigned int src_access;
474     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
475
476     if (!obj) return 0;
477     if ((entry = get_handle( src, src_handle )))
478         src_access = entry->access;
479     else  /* pseudo-handle, give it full access */
480     {
481         src_access = obj->ops->map_access( obj, GENERIC_ALL );
482         clear_error();
483     }
484     src_access &= ~RESERVED_ALL;
485
486     if (options & DUP_HANDLE_SAME_ACCESS)
487         access = src_access;
488     else
489         access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL;
490
491     /* asking for the more access rights than src_access? */
492     if (access & ~src_access)
493     {
494         if (options & DUP_HANDLE_MAKE_GLOBAL)
495             res = alloc_global_handle( obj, access );
496         else
497             res = alloc_handle( dst, obj, access, attr );
498     }
499     else
500     {
501         if (options & DUP_HANDLE_MAKE_GLOBAL)
502             res = alloc_global_handle_no_access_check( obj, access );
503         else
504             res = alloc_handle_no_access_check( dst, obj, access, attr );
505     }
506
507     release_object( obj );
508     return res;
509 }
510
511 /* open a new handle to an existing object */
512 obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
513                           const struct object_ops *ops, unsigned int access, unsigned int attr )
514 {
515     obj_handle_t handle = 0;
516     struct object *obj = find_object( namespace, name, attr );
517     if (obj)
518     {
519         if (ops && obj->ops != ops)
520             set_error( STATUS_OBJECT_TYPE_MISMATCH );
521         else
522             handle = alloc_handle( current->process, obj, access, attr );
523         release_object( obj );
524     }
525     else
526         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
527     return handle;
528 }
529
530 /* return the size of the handle table of a given process */
531 unsigned int get_handle_table_count( struct process *process )
532 {
533     if (!process->handles) return 0;
534     return process->handles->count;
535 }
536
537 /* close a handle */
538 DECL_HANDLER(close_handle)
539 {
540     close_handle( current->process, req->handle );
541 }
542
543 /* set a handle information */
544 DECL_HANDLER(set_handle_info)
545 {
546     reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
547 }
548
549 /* duplicate a handle */
550 DECL_HANDLER(dup_handle)
551 {
552     struct process *src, *dst;
553
554     reply->handle = 0;
555     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
556     {
557         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
558         {
559             reply->handle = duplicate_handle( src, req->src_handle, NULL,
560                                               req->access, req->attributes, req->options );
561         }
562         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
563         {
564             reply->handle = duplicate_handle( src, req->src_handle, dst,
565                                               req->access, req->attributes, req->options );
566             release_object( dst );
567         }
568         /* close the handle no matter what happened */
569         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
570         {
571             unsigned int err = get_error();  /* don't overwrite error from the above calls */
572             reply->closed = close_handle( src, req->src_handle );
573             set_error( err );
574         }
575         reply->self = (src == current->process);
576         release_object( src );
577     }
578 }
579
580 DECL_HANDLER(get_object_info)
581 {
582     struct object *obj;
583
584     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
585
586     reply->access = get_handle_access( current->process, req->handle );
587     reply->ref_count = obj->refcount;
588     release_object( obj );
589 }