handle.c 16.8 KB
Newer Older
1 2 3 4
/*
 * Server-side handle management
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

24 25 26
#include <assert.h>
#include <limits.h>
#include <string.h>
27
#include <stdarg.h>
28 29 30
#include <stdio.h>
#include <stdlib.h>

31 32
#include "ntstatus.h"
#define WIN32_NO_STATUS
33
#include "windef.h"
34
#include "winternl.h"
35 36 37 38

#include "handle.h"
#include "process.h"
#include "thread.h"
39
#include "request.h"
40 41 42

struct handle_entry
{
43 44
    struct object *ptr;       /* object */
    unsigned int   access;    /* access rights */
45 46
};

47 48 49 50 51 52 53 54 55 56 57
struct handle_table
{
    struct object        obj;         /* object header */
    struct process      *process;     /* process owning this table */
    int                  count;       /* number of allocated entries */
    int                  last;        /* last used entry */
    int                  free;        /* first entry that may be free */
    struct handle_entry *entries;     /* handle entries */
};

static struct handle_table *global_table;
58 59

/* reserved handle access rights */
60
#define RESERVED_SHIFT         26
61 62 63 64 65 66
#define RESERVED_INHERIT       (HANDLE_FLAG_INHERIT << RESERVED_SHIFT)
#define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT)
#define RESERVED_ALL           (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT)

#define MIN_HANDLE_ENTRIES  32

67

68 69
/* handle to table index conversion */

70
/* handles are a multiple of 4 under NT; handle 0 is not used */
71
inline static obj_handle_t index_to_handle( int index )
72
{
73
    return (obj_handle_t)((unsigned long)(index + 1) << 2);
74
}
75
inline static int handle_to_index( obj_handle_t handle )
76
{
77
    return ((unsigned long)handle >> 2) - 1;
78 79 80 81 82 83
}

/* global handle conversion */

#define HANDLE_OBFUSCATOR 0x544a4def

84
inline static int handle_is_global( obj_handle_t handle)
85 86 87
{
    return ((unsigned long)handle ^ HANDLE_OBFUSCATOR) < 0x10000;
}
88
inline static obj_handle_t handle_local_to_global( obj_handle_t handle )
89
{
90
    if (!handle) return 0;
91
    return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
92
}
93
inline static obj_handle_t handle_global_to_local( obj_handle_t handle )
94
{
95
    return (obj_handle_t)((unsigned long)handle ^ HANDLE_OBFUSCATOR);
96 97 98
}


99 100 101 102 103
static void handle_table_dump( struct object *obj, int verbose );
static void handle_table_destroy( struct object *obj );

static const struct object_ops handle_table_ops =
{
104 105 106 107 108 109
    sizeof(struct handle_table),     /* size */
    handle_table_dump,               /* dump */
    no_add_queue,                    /* add_queue */
    NULL,                            /* remove_queue */
    NULL,                            /* signaled */
    NULL,                            /* satisfied */
110
    no_signal,                       /* signal */
111
    no_get_fd,                       /* get_fd */
112
    no_map_access,                   /* map_access */
113
    no_lookup_name,                  /* lookup_name */
114
    no_close_handle,                 /* close_handle */
115
    handle_table_destroy             /* destroy */
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
};

/* dump a handle table */
static void handle_table_dump( struct object *obj, int verbose )
{
    int i;
    struct handle_table *table = (struct handle_table *)obj;
    struct handle_entry *entry = table->entries;

    assert( obj->ops == &handle_table_ops );

    fprintf( stderr, "Handle table last=%d count=%d process=%p\n",
             table->last, table->count, table->process );
    if (!verbose) return;
    entry = table->entries;
    for (i = 0; i <= table->last; i++, entry++)
    {
        if (!entry->ptr) continue;
134 135
        fprintf( stderr, "    %p: %p %08x ",
                 index_to_handle(i), entry->ptr, entry->access );
136 137 138 139 140 141 142 143 144
        entry->ptr->ops->dump( entry->ptr, 0 );
    }
}

/* destroy a handle table */
static void handle_table_destroy( struct object *obj )
{
    int i;
    struct handle_table *table = (struct handle_table *)obj;
145
    struct handle_entry *entry;
146 147 148

    assert( obj->ops == &handle_table_ops );

149 150 151 152 153 154 155 156 157 158 159
    /* first notify all objects that handles are being closed */
    if (table->process)
    {
        for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
        {
            struct object *obj = entry->ptr;
            if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) );
        }
    }

    for (i = 0, entry = table->entries; i <= table->last; i++, entry++)
160 161 162 163 164 165 166 167 168
    {
        struct object *obj = entry->ptr;
        entry->ptr = NULL;
        if (obj) release_object( obj );
    }
    free( table->entries );
}

/* allocate a new handle table */
169
struct handle_table *alloc_handle_table( struct process *process, int count )
170 171 172 173
{
    struct handle_table *table;

    if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES;
174
    if (!(table = alloc_object( &handle_table_ops )))
175 176 177 178 179
        return NULL;
    table->process = process;
    table->count   = count;
    table->last    = -1;
    table->free    = 0;
180
    if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table;
181 182 183 184
    release_object( table );
    return NULL;
}

185
/* grow a handle table */
186
static int grow_handle_table( struct handle_table *table )
187 188
{
    struct handle_entry *new_entries;
189
    int count = table->count;
190 191 192

    if (count >= INT_MAX / 2) return 0;
    count *= 2;
193
    if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) )))
194
    {
195
        set_error( STATUS_NO_MEMORY );
196 197
        return 0;
    }
198 199
    table->entries = new_entries;
    table->count   = count;
200 201 202
    return 1;
}

203
/* allocate the first free entry in the handle table */
204
static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access )
205 206
{
    struct handle_entry *entry = table->entries + table->free;
207
    int i;
208

209 210
    for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found;
    if (i >= table->count)
211
    {
212
        if (!grow_handle_table( table )) return 0;
213
        entry = table->entries + i;  /* the entries may have moved */
214
    }
215
    table->last = i;
216
 found:
217 218 219 220
    table->free = i + 1;
    entry->ptr    = grab_object( obj );
    entry->access = access;
    return index_to_handle(i);
221 222
}

223
/* allocate a handle for an object, incrementing its refcount */
224
/* return the handle, or 0 on error */
225
obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr )
226
{
227 228 229
    struct object *obj = ptr;

    access = obj->ops->map_access( obj, access );
230
    access &= ~RESERVED_ALL;
231
    if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT;
232 233 234 235 236 237
    if (!process->handles)
    {
        set_error( STATUS_NO_MEMORY );
        return 0;
    }
    return alloc_entry( process->handles, obj, access );
238
}
239

240
/* allocate a global handle for an object, incrementing its refcount */
241
/* return the handle, or 0 on error */
242
static obj_handle_t alloc_global_handle( void *obj, unsigned int access )
243 244
{
    if (!global_table)
245
    {
246 247
        if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 )))
            return 0;
248
        make_object_static( &global_table->obj );
249
    }
250
    return handle_local_to_global( alloc_entry( global_table, obj, access ));
251 252
}

253
/* return a handle entry, or NULL if the handle is invalid */
254
static struct handle_entry *get_handle( struct process *process, obj_handle_t handle )
255
{
256
    struct handle_table *table = process->handles;
257
    struct handle_entry *entry;
258
    int index;
259

260
    if (handle_is_global(handle))
261
    {
262
        handle = handle_global_to_local(handle);
263
        table = global_table;
264
    }
265
    if (!table) goto error;
266 267 268 269
    index = handle_to_index( handle );
    if (index < 0) goto error;
    if (index > table->last) goto error;
    entry = table->entries + index;
270 271 272 273
    if (!entry->ptr) goto error;
    return entry;

 error:
274
    set_error( STATUS_INVALID_HANDLE );
275 276 277 278
    return NULL;
}

/* attempt to shrink a table */
279
static void shrink_handle_table( struct handle_table *table )
280
{
281
    struct handle_entry *entry = table->entries + table->last;
282
    struct handle_entry *new_entries;
283
    int count = table->count;
284

285
    while (table->last >= 0)
286 287
    {
        if (entry->ptr) break;
288
        table->last--;
289 290
        entry--;
    }
291 292
    if (table->last >= count / 4) return;  /* no need to shrink */
    if (count < MIN_HANDLE_ENTRIES * 2) return;  /* too small to shrink */
293
    count /= 2;
294 295 296
    if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return;
    table->count   = count;
    table->entries = new_entries;
297 298 299 300
}

/* copy the handle table of the parent process */
/* return 1 if OK, 0 on error */
301
struct handle_table *copy_handle_table( struct process *process, struct process *parent )
302
{
303
    struct handle_table *parent_table = parent->handles;
304 305
    struct handle_table *table;
    int i;
306

307 308
    assert( parent_table );
    assert( parent_table->obj.ops == &handle_table_ops );
309

310 311
    if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count )))
        return NULL;
312

313
    if ((table->last = parent_table->last) >= 0)
314
    {
315 316 317
        struct handle_entry *ptr = table->entries;
        memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) );
        for (i = 0; i <= table->last; i++, ptr++)
318 319 320 321 322 323 324
        {
            if (!ptr->ptr) continue;
            if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
            else ptr->ptr = NULL; /* don't inherit this entry */
        }
    }
    /* attempt to shrink the table */
325
    shrink_handle_table( table );
326
    return table;
327 328 329 330
}

/* close a handle and decrement the refcount of the associated object */
/* return 1 if OK, 0 on error */
331
int close_handle( struct process *process, obj_handle_t handle )
332
{
333
    struct handle_table *table;
334 335 336 337
    struct handle_entry *entry;
    struct object *obj;

    if (!(entry = get_handle( process, handle ))) return 0;
338 339
    if (entry->access & RESERVED_CLOSE_PROTECT)
    {
340
        set_error( STATUS_HANDLE_NOT_CLOSABLE );
341 342
        return 0;
    }
343
    obj = entry->ptr;
344 345
    if (!obj->ops->close_handle( obj, process, handle ))
    {
346
        set_error( STATUS_HANDLE_NOT_CLOSABLE );
347 348
        return 0;
    }
349
    entry->ptr = NULL;
350
    table = handle_is_global(handle) ? global_table : process->handles;
351 352
    if (entry < table->entries + table->free) table->free = entry - table->entries;
    if (entry == table->entries + table->last) shrink_handle_table( table );
353 354 355 356
    release_object( obj );
    return 1;
}

357
/* retrieve the object corresponding to one of the magic pseudo-handles */
358
static inline struct object *get_magic_handle( obj_handle_t handle )
359
{
360
    switch((unsigned long)handle)
361 362 363 364 365 366 367 368 369 370 371
    {
        case 0xfffffffe:  /* current thread pseudo-handle */
            return &current->obj;
        case 0x7fffffff:  /* current process pseudo-handle */
        case 0xffffffff:  /* current process pseudo-handle */
            return (struct object *)current->process;
        default:
            return NULL;
    }
}

372
/* retrieve the object corresponding to a handle, incrementing its refcount */
373
struct object *get_handle_obj( struct process *process, obj_handle_t handle,
374 375 376 377 378
                               unsigned int access, const struct object_ops *ops )
{
    struct handle_entry *entry;
    struct object *obj;

379
    if (!(obj = get_magic_handle( handle )))
380 381 382 383
    {
        if (!(entry = get_handle( process, handle ))) return NULL;
        if ((entry->access & access) != access)
        {
384
            set_error( STATUS_ACCESS_DENIED );
385 386 387 388 389 390
            return NULL;
        }
        obj = entry->ptr;
    }
    if (ops && (obj->ops != ops))
    {
391
        set_error( STATUS_OBJECT_TYPE_MISMATCH );  /* not the right type */
392 393 394 395 396
        return NULL;
    }
    return grab_object( obj );
}

397 398 399 400 401 402 403 404 405 406
/* retrieve the access rights of a given handle */
unsigned int get_handle_access( struct process *process, obj_handle_t handle )
{
    struct handle_entry *entry;

    if (get_magic_handle( handle )) return ~0U;  /* magic handles have all access rights */
    if (!(entry = get_handle( process, handle ))) return 0;
    return entry->access;
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/* find the first inherited handle of the given type */
/* this is needed for window stations and desktops (don't ask...) */
obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
{
    struct handle_table *table = process->handles;
    struct handle_entry *ptr;
    int i;

    if (!table) return 0;

    for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++)
    {
        if (!ptr->ptr) continue;
        if (ptr->ptr->ops != ops) continue;
        if (ptr->access & RESERVED_INHERIT) return index_to_handle(i);
    }
    return 0;
}

426
/* get/set the handle reserved flags */
427
/* return the old flags (or -1 on error) */
428
static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags )
429 430
{
    struct handle_entry *entry;
431
    unsigned int old_access;
432

433 434 435
    if (get_magic_handle( handle ))
    {
        /* we can retrieve but not set info for magic handles */
436
        if (mask) set_error( STATUS_ACCESS_DENIED );
437 438
        return 0;
    }
439
    if (!(entry = get_handle( process, handle ))) return -1;
440
    old_access = entry->access;
441 442 443
    mask  = (mask << RESERVED_SHIFT) & RESERVED_ALL;
    flags = (flags << RESERVED_SHIFT) & mask;
    entry->access = (entry->access & ~mask) | flags;
444
    return (old_access & RESERVED_ALL) >> RESERVED_SHIFT;
445 446 447
}

/* duplicate a handle */
448
obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
449
                               unsigned int access, unsigned int attr, unsigned int options )
450
{
451
    obj_handle_t res;
452
    struct object *obj = get_handle_obj( src, src_handle, 0, NULL );
453

454
    if (!obj) return 0;
455 456 457 458 459 460 461 462
    if (options & DUP_HANDLE_SAME_ACCESS)
    {
        struct handle_entry *entry = get_handle( src, src_handle );
        if (entry)
            access = entry->access;
        else  /* pseudo-handle, give it full access */
        {
            access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
463
            clear_error();
464 465
        }
    }
466
    access &= ~RESERVED_ALL;
467 468 469
    if (options & DUP_HANDLE_MAKE_GLOBAL)
        res = alloc_global_handle( obj, access );
    else
470
        res = alloc_handle( dst, obj, access, attr );
471
    release_object( obj );
472 473 474 475
    return res;
}

/* open a new handle to an existing object */
476
obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
477
                          const struct object_ops *ops, unsigned int access, unsigned int attr )
478
{
479
    obj_handle_t handle = 0;
480
    struct object *obj = find_object( namespace, name, attr );
481
    if (obj)
482
    {
483
        if (ops && obj->ops != ops)
484
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
485
        else
486
            handle = alloc_handle( current->process, obj, access, attr );
487 488
        release_object( obj );
    }
489
    else
490
        set_error( STATUS_OBJECT_NAME_NOT_FOUND );
491
    return handle;
492 493
}

494 495 496
/* return the size of the handle table of a given process */
unsigned int get_handle_table_count( struct process *process )
{
497
    if (!process->handles) return 0;
498 499 500
    return process->handles->count;
}

501 502 503
/* close a handle */
DECL_HANDLER(close_handle)
{
504
    close_handle( current->process, req->handle );
505 506 507 508
}

/* set a handle information */
DECL_HANDLER(set_handle_info)
509 510 511 512
{
    reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags );
}

513 514 515 516 517
/* duplicate a handle */
DECL_HANDLER(dup_handle)
{
    struct process *src, *dst;

518
    reply->handle = 0;
519 520 521 522
    if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE )))
    {
        if (req->options & DUP_HANDLE_MAKE_GLOBAL)
        {
523
            reply->handle = duplicate_handle( src, req->src_handle, NULL,
524
                                              req->access, req->attributes, req->options );
525 526 527
        }
        else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE )))
        {
528
            reply->handle = duplicate_handle( src, req->src_handle, dst,
529
                                              req->access, req->attributes, req->options );
530 531 532 533
            release_object( dst );
        }
        /* close the handle no matter what happened */
        if (req->options & DUP_HANDLE_CLOSE_SOURCE)
534
        {
535
            unsigned int err = get_error();  /* don't overwrite error from the above calls */
536
            reply->closed = close_handle( src, req->src_handle );
537
            set_error( err );
538
        }
539
        reply->self = (src == current->process);
540 541 542
        release_object( src );
    }
}