Check POLLHUP semantics at remote shutdown when wineserver is started.
[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     NULL,                            /* get_poll_events */
108     NULL,                            /* poll_event */
109     no_get_fd,                       /* get_fd */
110     no_flush,                        /* flush */
111     no_get_file_info,                /* get_file_info */
112     NULL,                            /* queue_async */
113     handle_table_destroy             /* destroy */
114 };
115
116 /* dump a handle table */
117 static void handle_table_dump( struct object *obj, int verbose )
118 {
119     int i;
120     struct handle_table *table = (struct handle_table *)obj;
121     struct handle_entry *entry = table->entries;
122
123     assert( obj->ops == &handle_table_ops );
124
125     fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
126              table->last, table->count, table->process );
127     if (!verbose) return;
128     entry = table->entries;
129     for (i = 0; i <= table->last; i++, entry++)
130     {
131         if (!entry->ptr) continue;
132         fprintf( stderr, "%9u: %p %08x ",
133                  (unsigned int)index_to_handle(i), entry->ptr, entry->access );
134         entry->ptr->ops->dump( entry->ptr, 0 );
135     }
136 }
137
138 /* destroy a handle table */
139 static void handle_table_destroy( struct object *obj )
140 {
141     int i;
142     struct handle_table *table = (struct handle_table *)obj;
143     struct handle_entry *entry = table->entries;
144
145     assert( obj->ops == &handle_table_ops );
146
147     for (i = 0; i <= table->last; i++, entry++)
148     {
149         struct object *obj = entry->ptr;
150         entry->ptr = NULL;
151         if (obj) release_object( obj );
152     }
153     free( table->entries );
154 }
155
156 /* allocate a new handle table */
157 struct object *alloc_handle_table( struct process *process, int count )
158 {
159     struct handle_table *table;
160
161     if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
162     if (!(table = alloc_object( &handle_table_ops, -1 )))
163         return NULL;
164     table->process = process;
165     table->count   = count;
166     table->last    = -1;
167     table->free    = 0;
168     if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj;
169     release_object( table );
170     return NULL;
171 }
172
173 /* grow a handle table */
174 static int grow_handle_table( struct handle_table *table )
175 {
176     struct handle_entry *new_entries;
177     int count = table->count;
178
179     if (count >= INT_MAX / 2) return 0;
180     count *= 2;
181     if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
182     {
183         set_error( STATUS_NO_MEMORY );
184         return 0;
185     }
186     table->entries = new_entries;
187     table->count   = count;
188     return 1;
189 }
190
191 /* allocate the first free entry in the handle table */
192 static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
193 {
194     struct handle_entry *entry = table->entries + table->free;
195     int i;
196
197     for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
198     if (i >= table->count)
199     {
200         if (!grow_handle_table( table )) return 0;
201         entry = table->entries + i;  /* the entries may have moved */
202     }
203     table->last = i;
204  found:
205     table->free = i + 1;
206     entry->ptr    = grab_object( obj );
207     entry->access = access;
208     entry->fd     = -1;
209     return index_to_handle(i);
210 }
211
212 /* allocate a handle for an object, incrementing its refcount */
213 /* return the handle, or 0 on error */
214 obj_handle_t alloc_handle( struct process *process, void *obj, unsigned int access, int inherit )
215 {
216     struct handle_table *table = (struct handle_table *)process->handles;
217
218     assert( table );
219     assert( !(access & RESERVED_ALL) );
220     if (inherit) access |= RESERVED_INHERIT;
221     return alloc_entry( table, obj, access );
222 }
223
224 /* allocate a global handle for an object, incrementing its refcount */
225 /* return the handle, or 0 on error */
226 static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
227 {
228     if (!global_table)
229     {
230         if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
231             return 0;
232     }
233     return handle_local_to_global( alloc_entry( global_table, obj, access ));
234 }
235
236 /* return a handle entry, or NULL if the handle is invalid */
237 static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
238 {
239     struct handle_table *table = (struct handle_table *)process->handles;
240     struct handle_entry *entry;
241     int index;
242
243     if (handle_is_global(handle))
244     {
245         handle = handle_global_to_local(handle);
246         table = global_table;
247     }
248     if (!table) goto error;
249     index = handle_to_index( handle );
250     if (index < 0) goto error;
251     if (index > table->last) goto error;
252     entry = table->entries + index;
253     if (!entry->ptr) goto error;
254     return entry;
255
256  error:
257     set_error( STATUS_INVALID_HANDLE );
258     return NULL;
259 }
260
261 /* attempt to shrink a table */
262 static void shrink_handle_table( struct handle_table *table )
263 {
264     struct handle_entry *entry = table->entries + table->last;
265     struct handle_entry *new_entries;
266     int count = table->count;
267
268     while (table->last >= 0)
269     {
270         if (entry->ptr) break;
271         table->last--;
272         entry--;
273     }
274     if (table->last >= count / 4) return;  /* no need to shrink */
275     if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
276     count /= 2;
277     if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
278     table->count   = count;
279     table->entries = new_entries;
280 }
281
282 /* copy the handle table of the parent process */
283 /* return 1 if OK, 0 on error */
284 struct object *copy_handle_table( struct process *process, struct process *parent )
285 {
286     struct handle_table *parent_table = (struct handle_table *)parent->handles;
287     struct handle_table *table;
288     int i;
289
290     assert( parent_table );
291     assert( parent_table->obj.ops == &handle_table_ops );
292
293     if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
294         return NULL;
295
296     if ((table->last = parent_table->last) >= 0)
297     {
298         struct handle_entry *ptr = table->entries;
299         memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
300         for (i = 0; i <= table->last; i++, ptr++)
301         {
302             if (!ptr->ptr) continue;
303             ptr->fd = -1;
304             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
305             else ptr->ptr = NULL; /* don't inherit this entry */
306         }
307     }
308     /* attempt to shrink the table */
309     shrink_handle_table( table );
310     return &table->obj;
311 }
312
313 /* close a handle and decrement the refcount of the associated object */
314 /* return 1 if OK, 0 on error */
315 int close_handle( struct process *process, obj_handle_t handle, int *fd )
316 {
317     struct handle_table *table;
318     struct handle_entry *entry;
319     struct object *obj;
320
321     if (!(entry = get_handle( process, handle ))) return 0;
322     if (entry->access & RESERVED_CLOSE_PROTECT)
323     {
324         set_error( STATUS_INVALID_HANDLE );
325         return 0;
326     }
327     obj = entry->ptr;
328     entry->ptr = NULL;
329     if (fd) *fd = entry->fd;
330     else if (entry->fd != -1) return 1;  /* silently ignore close attempt if we cannot close the fd */
331     entry->fd = -1;
332     table = handle_is_global(handle) ? global_table : (struct handle_table *)process->handles;
333     if (entry < table->entries + table->free) table->free = entry - table->entries;
334     if (entry == table->entries + table->last) shrink_handle_table( table );
335     release_object( obj );
336     return 1;
337 }
338
339 /* close all the global handles */
340 void close_global_handles(void)
341 {
342     if (global_table)
343     {
344         release_object( global_table );
345         global_table = NULL;
346     }
347 }
348
349 /* retrieve the object corresponding to one of the magic pseudo-handles */
350 static inline struct object *get_magic_handle( obj_handle_t handle )
351 {
352     switch((unsigned long)handle)
353     {
354         case 0xfffffffe:  /* current thread pseudo-handle */
355             return &current->obj;
356         case 0x7fffffff:  /* current process pseudo-handle */
357         case 0xffffffff:  /* current process pseudo-handle */
358             return (struct object *)current->process;
359         default:
360             return NULL;
361     }
362 }
363
364 /* retrieve the object corresponding to a handle, incrementing its refcount */
365 struct object *get_handle_obj( struct process *process, obj_handle_t handle,
366                                unsigned int access, const struct object_ops *ops )
367 {
368     struct handle_entry *entry;
369     struct object *obj;
370
371     if (!(obj = get_magic_handle( handle )))
372     {
373         if (!(entry = get_handle( process, handle ))) return NULL;
374         if ((entry->access & access) != access)
375         {
376             set_error( STATUS_ACCESS_DENIED );
377             return NULL;
378         }
379         obj = entry->ptr;
380     }
381     if (ops && (obj->ops != ops))
382     {
383         set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
384         return NULL;
385     }
386     return grab_object( obj );
387 }
388
389 /* retrieve the cached fd for a given handle */
390 int get_handle_fd( struct process *process, obj_handle_t handle, unsigned int access )
391 {
392     struct handle_entry *entry;
393
394     if (!(entry = get_handle( process, handle ))) return -1;
395     if ((entry->access & access) != access)
396     {
397         set_error( STATUS_ACCESS_DENIED );
398         return -1;
399     }
400     return entry->fd;
401 }
402
403 /* get/set the handle reserved flags */
404 /* return the old flags (or -1 on error) */
405 static int set_handle_info( struct process *process, obj_handle_t handle,
406                             int mask, int flags, int *fd )
407 {
408     struct handle_entry *entry;
409     unsigned int old_access;
410
411     if (get_magic_handle( handle ))
412     {
413         /* we can retrieve but not set info for magic handles */
414         if (mask) set_error( STATUS_ACCESS_DENIED );
415         return 0;
416     }
417     if (!(entry = get_handle( process, handle ))) return -1;
418     old_access = entry->access;
419     mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
420     flags = (flags << RESERVED_SHIFT) & mask;
421     entry->access = (entry->access & ~mask) | flags;
422     /* if no current fd set it, otherwise return current fd */
423     if (entry->fd == -1) entry->fd = *fd;
424     *fd = entry->fd;
425     return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
426 }
427
428 /* duplicate a handle */
429 obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
430                            unsigned int access, int inherit, int options )
431 {
432     obj_handle_t res;
433     struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
434
435     if (!obj) return 0;
436     if (options & DUP_HANDLE_SAME_ACCESS)
437     {
438         struct handle_entry *entry = get_handle( src, src_handle );
439         if (entry)
440             access = entry->access;
441         else  /* pseudo-handle, give it full access */
442         {
443             access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
444             clear_error();
445         }
446     }
447     access &= ~RESERVED_ALL;
448     if (options & DUP_HANDLE_MAKE_GLOBAL)
449         res = alloc_global_handle( obj, access );
450     else
451         res = alloc_handle( dst, obj, access, inherit );
452     release_object( obj );
453     return res;
454 }
455
456 /* open a new handle to an existing object */
457 obj_handle_t open_object( const WCHAR *name, size_t len, const struct object_ops *ops,
458                       unsigned int access, int inherit )
459 {
460     obj_handle_t handle = 0;
461     struct object *obj = find_object( name, len );
462     if (obj)
463     {
464         if (ops && obj->ops != ops)
465             set_error( STATUS_OBJECT_TYPE_MISMATCH );
466         else
467             handle = alloc_handle( current->process, obj, access, inherit );
468         release_object( obj );
469     }
470     else
471         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
472     return handle;
473 }
474
475 /* close a handle */
476 DECL_HANDLER(close_handle)
477 {
478     close_handle( current->process, req->handle, &reply->fd );
479 }
480
481 /* set a handle information */
482 DECL_HANDLER(set_handle_info)
483 {
484     int fd = req->fd;
485
486     if (handle_is_global(req->handle)) fd = -1;  /* no fd cache for global handles */
487     reply->old_flags = set_handle_info( current->process, req->handle,
488                                         req->mask, req->flags, &fd );
489     reply->cur_fd = fd;
490 }
491
492 /* duplicate a handle */
493 DECL_HANDLER(dup_handle)
494 {
495     struct process *src, *dst;
496
497     reply->handle = 0;
498     reply->fd = -1;
499     if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
500     {
501         if (req->options & DUP_HANDLE_MAKE_GLOBAL)
502         {
503             reply->handle = duplicate_handle( src, req->src_handle, NULL,
504                                               req->access, req->inherit, req->options );
505         }
506         else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
507         {
508             reply->handle = duplicate_handle( src, req->src_handle, dst,
509                                               req->access, req->inherit, req->options );
510             release_object( dst );
511         }
512         /* close the handle no matter what happened */
513         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
514         {
515             if (src == current->process) close_handle( src, req->src_handle, &reply->fd );
516             else close_handle( src, req->src_handle, NULL );
517         }
518         release_object( src );
519     }
520 }