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