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