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