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