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