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