Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side handle management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <limits.h> |
| 9 | #include <string.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 13 | #include "winbase.h" |
| 14 | |
| 15 | #include "handle.h" |
| 16 | #include "process.h" |
| 17 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 18 | #include "request.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 19 | |
| 20 | struct handle_entry |
| 21 | { |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 22 | struct object *ptr; /* object */ |
| 23 | unsigned int access; /* access rights */ |
| 24 | int fd; /* file descriptor (in client process) */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 27 | struct handle_table |
| 28 | { |
| 29 | struct object obj; /* object header */ |
| 30 | struct process *process; /* process owning this table */ |
| 31 | int count; /* number of allocated entries */ |
| 32 | int last; /* last used entry */ |
| 33 | int free; /* first entry that may be free */ |
| 34 | struct handle_entry *entries; /* handle entries */ |
| 35 | }; |
| 36 | |
| 37 | static struct handle_table *global_table; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 38 | |
| 39 | /* reserved handle access rights */ |
| 40 | #define RESERVED_SHIFT 25 |
| 41 | #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT << RESERVED_SHIFT) |
| 42 | #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE << RESERVED_SHIFT) |
| 43 | #define RESERVED_ALL (RESERVED_INHERIT | RESERVED_CLOSE_PROTECT) |
| 44 | |
| 45 | /* global handle macros */ |
| 46 | #define HANDLE_OBFUSCATOR 0x544a4def |
| 47 | #define HANDLE_IS_GLOBAL(h) (((h) ^ HANDLE_OBFUSCATOR) < 0x10000) |
| 48 | #define HANDLE_LOCAL_TO_GLOBAL(h) ((h) ^ HANDLE_OBFUSCATOR) |
| 49 | #define HANDLE_GLOBAL_TO_LOCAL(h) ((h) ^ HANDLE_OBFUSCATOR) |
| 50 | |
| 51 | #define MIN_HANDLE_ENTRIES 32 |
| 52 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 53 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 54 | /* handle to table index conversion */ |
| 55 | |
| 56 | /* handles are a multiple of 4 under NT; handle 0 is not used */ |
Joerg Mayer | 650ba4d | 2000-10-29 18:06:04 +0000 | [diff] [blame] | 57 | inline static int index_to_handle( int index ) |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 58 | { |
| 59 | return (index + 1) << 2; |
| 60 | } |
Joerg Mayer | 650ba4d | 2000-10-29 18:06:04 +0000 | [diff] [blame] | 61 | inline static int handle_to_index( int handle ) |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 62 | { |
| 63 | return (handle >> 2) - 1; |
| 64 | } |
| 65 | |
| 66 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 67 | static void handle_table_dump( struct object *obj, int verbose ); |
| 68 | static void handle_table_destroy( struct object *obj ); |
| 69 | |
| 70 | static const struct object_ops handle_table_ops = |
| 71 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 72 | sizeof(struct handle_table), /* size */ |
| 73 | handle_table_dump, /* dump */ |
| 74 | no_add_queue, /* add_queue */ |
| 75 | NULL, /* remove_queue */ |
| 76 | NULL, /* signaled */ |
| 77 | NULL, /* satisfied */ |
| 78 | NULL, /* get_poll_events */ |
| 79 | NULL, /* poll_event */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 80 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 81 | no_flush, /* flush */ |
| 82 | no_get_file_info, /* get_file_info */ |
| 83 | handle_table_destroy /* destroy */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* dump a handle table */ |
| 87 | static void handle_table_dump( struct object *obj, int verbose ) |
| 88 | { |
| 89 | int i; |
| 90 | struct handle_table *table = (struct handle_table *)obj; |
| 91 | struct handle_entry *entry = table->entries; |
| 92 | |
| 93 | assert( obj->ops == &handle_table_ops ); |
| 94 | |
| 95 | fprintf( stderr, "Handle table last=%d count=%d process=%p\n", |
| 96 | table->last, table->count, table->process ); |
| 97 | if (!verbose) return; |
| 98 | entry = table->entries; |
| 99 | for (i = 0; i <= table->last; i++, entry++) |
| 100 | { |
| 101 | if (!entry->ptr) continue; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 102 | fprintf( stderr, "%9d: %p %08x ", index_to_handle(i), entry->ptr, entry->access ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 103 | entry->ptr->ops->dump( entry->ptr, 0 ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* destroy a handle table */ |
| 108 | static void handle_table_destroy( struct object *obj ) |
| 109 | { |
| 110 | int i; |
| 111 | struct handle_table *table = (struct handle_table *)obj; |
| 112 | struct handle_entry *entry = table->entries; |
| 113 | |
| 114 | assert( obj->ops == &handle_table_ops ); |
| 115 | |
| 116 | for (i = 0; i <= table->last; i++, entry++) |
| 117 | { |
| 118 | struct object *obj = entry->ptr; |
| 119 | entry->ptr = NULL; |
| 120 | if (obj) release_object( obj ); |
| 121 | } |
| 122 | free( table->entries ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* allocate a new handle table */ |
| 126 | struct object *alloc_handle_table( struct process *process, int count ) |
| 127 | { |
| 128 | struct handle_table *table; |
| 129 | |
| 130 | if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES; |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 131 | if (!(table = alloc_object( &handle_table_ops, -1 ))) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | table->process = process; |
| 134 | table->count = count; |
| 135 | table->last = -1; |
| 136 | table->free = 0; |
| 137 | if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return &table->obj; |
| 138 | release_object( table ); |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 142 | /* grow a handle table */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 143 | static int grow_handle_table( struct handle_table *table ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 144 | { |
| 145 | struct handle_entry *new_entries; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 146 | int count = table->count; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 147 | |
| 148 | if (count >= INT_MAX / 2) return 0; |
| 149 | count *= 2; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 150 | if (!(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 151 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 152 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 155 | table->entries = new_entries; |
| 156 | table->count = count; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 160 | /* allocate the first free entry in the handle table */ |
| 161 | static int alloc_entry( struct handle_table *table, void *obj, unsigned int access ) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 162 | { |
| 163 | struct handle_entry *entry = table->entries + table->free; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 164 | int i; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 165 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 166 | for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found; |
| 167 | if (i >= table->count) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 168 | { |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 169 | if (!grow_handle_table( table )) return -1; |
| 170 | entry = table->entries + i; /* the entries may have moved */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 171 | } |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 172 | table->last = i; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 173 | found: |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 174 | table->free = i + 1; |
| 175 | entry->ptr = grab_object( obj ); |
| 176 | entry->access = access; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 177 | entry->fd = -1; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 178 | return index_to_handle(i); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 181 | /* allocate a handle for an object, incrementing its refcount */ |
| 182 | /* return the handle, or -1 on error */ |
Alexandre Julliard | 3da5f84 | 1999-05-16 16:54:54 +0000 | [diff] [blame] | 183 | int alloc_handle( struct process *process, void *obj, unsigned int access, int inherit ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 184 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 185 | struct handle_table *table = (struct handle_table *)process->handles; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 186 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 187 | assert( table ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 188 | assert( !(access & RESERVED_ALL) ); |
| 189 | if (inherit) access |= RESERVED_INHERIT; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 190 | return alloc_entry( table, obj, access ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* allocate a global handle for an object, incrementing its refcount */ |
| 194 | /* return the handle, or -1 on error */ |
| 195 | static int alloc_global_handle( void *obj, unsigned int access ) |
| 196 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 197 | int handle; |
| 198 | |
| 199 | if (!global_table) |
| 200 | { |
| 201 | if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 ))) return -1; |
| 202 | } |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 203 | if ((handle = alloc_entry( global_table, obj, access )) != -1) |
| 204 | handle = HANDLE_LOCAL_TO_GLOBAL(handle); |
| 205 | return handle; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 208 | /* return a handle entry, or NULL if the handle is invalid */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 209 | static struct handle_entry *get_handle( struct process *process, int handle ) |
| 210 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 211 | struct handle_table *table = (struct handle_table *)process->handles; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 212 | struct handle_entry *entry; |
| 213 | |
| 214 | if (HANDLE_IS_GLOBAL(handle)) |
| 215 | { |
| 216 | handle = HANDLE_GLOBAL_TO_LOCAL(handle); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 217 | table = global_table; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 218 | } |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 219 | if (!table) goto error; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 220 | handle = handle_to_index( handle ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 221 | if (handle < 0) goto error; |
| 222 | if (handle > table->last) goto error; |
| 223 | entry = table->entries + handle; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 224 | if (!entry->ptr) goto error; |
| 225 | return entry; |
| 226 | |
| 227 | error: |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 228 | set_error( STATUS_INVALID_HANDLE ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | /* attempt to shrink a table */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 233 | static void shrink_handle_table( struct handle_table *table ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 234 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 235 | struct handle_entry *entry = table->entries + table->last; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 236 | struct handle_entry *new_entries; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 237 | int count = table->count; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 238 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 239 | while (table->last >= 0) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 240 | { |
| 241 | if (entry->ptr) break; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 242 | table->last--; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 243 | entry--; |
| 244 | } |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 245 | if (table->last >= count / 4) return; /* no need to shrink */ |
| 246 | if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 247 | count /= 2; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 248 | if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return; |
| 249 | table->count = count; |
| 250 | table->entries = new_entries; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* copy the handle table of the parent process */ |
| 254 | /* return 1 if OK, 0 on error */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 255 | struct object *copy_handle_table( struct process *process, struct process *parent ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 256 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 257 | struct handle_table *parent_table = (struct handle_table *)parent->handles; |
| 258 | struct handle_table *table; |
| 259 | int i; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 260 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 261 | assert( parent_table ); |
| 262 | assert( parent_table->obj.ops == &handle_table_ops ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 263 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 264 | if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count ))) |
| 265 | return NULL; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 266 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 267 | if ((table->last = parent_table->last) >= 0) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 268 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 269 | struct handle_entry *ptr = table->entries; |
| 270 | memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) ); |
| 271 | for (i = 0; i <= table->last; i++, ptr++) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 272 | { |
| 273 | if (!ptr->ptr) continue; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 274 | ptr->fd = -1; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 275 | if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr ); |
| 276 | else ptr->ptr = NULL; /* don't inherit this entry */ |
| 277 | } |
| 278 | } |
| 279 | /* attempt to shrink the table */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 280 | shrink_handle_table( table ); |
| 281 | return &table->obj; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* close a handle and decrement the refcount of the associated object */ |
| 285 | /* return 1 if OK, 0 on error */ |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 286 | int close_handle( struct process *process, int handle, int *fd ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 287 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 288 | struct handle_table *table; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 289 | struct handle_entry *entry; |
| 290 | struct object *obj; |
| 291 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 292 | if (!(entry = get_handle( process, handle ))) return 0; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 293 | if (entry->access & RESERVED_CLOSE_PROTECT) |
| 294 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 295 | set_error( STATUS_INVALID_HANDLE ); |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 296 | return 0; |
| 297 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 298 | obj = entry->ptr; |
| 299 | entry->ptr = NULL; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 300 | if (fd) *fd = entry->fd; |
| 301 | else if (entry->fd != -1) return 1; /* silently ignore close attempt if we cannot close the fd */ |
| 302 | entry->fd = -1; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 303 | table = HANDLE_IS_GLOBAL(handle) ? global_table : (struct handle_table *)process->handles; |
| 304 | if (entry < table->entries + table->free) table->free = entry - table->entries; |
| 305 | if (entry == table->entries + table->last) shrink_handle_table( table ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 306 | release_object( obj ); |
| 307 | return 1; |
| 308 | } |
| 309 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 310 | /* close all the global handles */ |
| 311 | void close_global_handles(void) |
| 312 | { |
| 313 | if (global_table) |
| 314 | { |
| 315 | release_object( global_table ); |
| 316 | global_table = NULL; |
| 317 | } |
| 318 | } |
| 319 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 320 | /* retrieve the object corresponding to one of the magic pseudo-handles */ |
| 321 | static inline struct object *get_magic_handle( int handle ) |
| 322 | { |
| 323 | switch(handle) |
| 324 | { |
| 325 | case 0xfffffffe: /* current thread pseudo-handle */ |
| 326 | return ¤t->obj; |
| 327 | case 0x7fffffff: /* current process pseudo-handle */ |
| 328 | case 0xffffffff: /* current process pseudo-handle */ |
| 329 | return (struct object *)current->process; |
| 330 | default: |
| 331 | return NULL; |
| 332 | } |
| 333 | } |
| 334 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 335 | /* retrieve the object corresponding to a handle, incrementing its refcount */ |
| 336 | struct object *get_handle_obj( struct process *process, int handle, |
| 337 | unsigned int access, const struct object_ops *ops ) |
| 338 | { |
| 339 | struct handle_entry *entry; |
| 340 | struct object *obj; |
| 341 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 342 | if (!(obj = get_magic_handle( handle ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 343 | { |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 344 | if (!(entry = get_handle( process, handle ))) return NULL; |
| 345 | if ((entry->access & access) != access) |
| 346 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 347 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
| 350 | obj = entry->ptr; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 351 | } |
| 352 | if (ops && (obj->ops != ops)) |
| 353 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 354 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | return grab_object( obj ); |
| 358 | } |
| 359 | |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 360 | /* retrieve the cached fd for a given handle */ |
| 361 | int get_handle_fd( struct process *process, int handle, unsigned int access ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 362 | { |
| 363 | struct handle_entry *entry; |
| 364 | |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 365 | if (HANDLE_IS_GLOBAL(handle)) return -1; /* no fd cache for global handles */ |
| 366 | if (!(entry = get_handle( process, handle ))) return -1; |
| 367 | if ((entry->access & access) != access) |
| 368 | { |
| 369 | set_error( STATUS_ACCESS_DENIED ); |
| 370 | return -1; |
| 371 | } |
| 372 | return entry->fd; |
| 373 | } |
| 374 | |
| 375 | /* get/set the handle reserved flags */ |
| 376 | /* return the old flags (or -1 on error) */ |
| 377 | static int set_handle_info( struct process *process, int handle, int mask, int flags, int *fd ) |
| 378 | { |
| 379 | struct handle_entry *entry; |
| 380 | unsigned int old_access; |
| 381 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 382 | if (get_magic_handle( handle )) |
| 383 | { |
| 384 | /* we can retrieve but not set info for magic handles */ |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 385 | if (mask) set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 386 | return 0; |
| 387 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 388 | if (!(entry = get_handle( process, handle ))) return -1; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 389 | old_access = entry->access; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 390 | mask = (mask << RESERVED_SHIFT) & RESERVED_ALL; |
| 391 | flags = (flags << RESERVED_SHIFT) & mask; |
| 392 | entry->access = (entry->access & ~mask) | flags; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 393 | /* if no current fd set it, otherwise return current fd */ |
| 394 | if (entry->fd == -1) entry->fd = *fd; |
| 395 | *fd = entry->fd; |
| 396 | return (old_access & RESERVED_ALL) >> RESERVED_SHIFT; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* duplicate a handle */ |
| 400 | int duplicate_handle( struct process *src, int src_handle, struct process *dst, |
| 401 | unsigned int access, int inherit, int options ) |
| 402 | { |
| 403 | int res; |
Alexandre Julliard | 0042cb3 | 1999-05-29 11:17:25 +0000 | [diff] [blame] | 404 | struct object *obj = get_handle_obj( src, src_handle, 0, NULL ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 405 | |
Alexandre Julliard | 0042cb3 | 1999-05-29 11:17:25 +0000 | [diff] [blame] | 406 | if (!obj) return -1; |
| 407 | if (options & DUP_HANDLE_SAME_ACCESS) |
| 408 | { |
| 409 | struct handle_entry *entry = get_handle( src, src_handle ); |
| 410 | if (entry) |
| 411 | access = entry->access; |
| 412 | else /* pseudo-handle, give it full access */ |
| 413 | { |
| 414 | access = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 415 | clear_error(); |
Alexandre Julliard | 0042cb3 | 1999-05-29 11:17:25 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 418 | access &= ~RESERVED_ALL; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 419 | if (options & DUP_HANDLE_MAKE_GLOBAL) |
| 420 | res = alloc_global_handle( obj, access ); |
| 421 | else |
| 422 | res = alloc_handle( dst, obj, access, inherit ); |
Alexandre Julliard | 0042cb3 | 1999-05-29 11:17:25 +0000 | [diff] [blame] | 423 | release_object( obj ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 424 | return res; |
| 425 | } |
| 426 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 427 | /* open a new handle to an existing object */ |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 428 | int open_object( const WCHAR *name, size_t len, const struct object_ops *ops, |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 429 | unsigned int access, int inherit ) |
| 430 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 431 | int handle = -1; |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 432 | struct object *obj = find_object( name, len ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 433 | if (obj) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 434 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 435 | if (ops && obj->ops != ops) |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 436 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 437 | else |
| 438 | handle = alloc_handle( current->process, obj, access, inherit ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 439 | release_object( obj ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 440 | } |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 441 | else |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 442 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 443 | return handle; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 446 | /* close a handle */ |
| 447 | DECL_HANDLER(close_handle) |
| 448 | { |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 449 | close_handle( current->process, req->handle, &req->fd ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* set a handle information */ |
| 453 | DECL_HANDLER(set_handle_info) |
| 454 | { |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 455 | int fd = req->fd; |
| 456 | |
| 457 | if (HANDLE_IS_GLOBAL(req->handle)) fd = -1; /* no fd cache for global handles */ |
| 458 | req->old_flags = set_handle_info( current->process, req->handle, req->mask, req->flags, &fd ); |
| 459 | req->cur_fd = fd; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* duplicate a handle */ |
| 463 | DECL_HANDLER(dup_handle) |
| 464 | { |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 465 | struct process *src, *dst; |
| 466 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 467 | req->handle = -1; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 468 | req->fd = -1; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 469 | if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE ))) |
| 470 | { |
| 471 | if (req->options & DUP_HANDLE_MAKE_GLOBAL) |
| 472 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 473 | req->handle = duplicate_handle( src, req->src_handle, NULL, |
| 474 | req->access, req->inherit, req->options ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 475 | } |
| 476 | else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE ))) |
| 477 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 478 | req->handle = duplicate_handle( src, req->src_handle, dst, |
| 479 | req->access, req->inherit, req->options ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 480 | release_object( dst ); |
| 481 | } |
| 482 | /* close the handle no matter what happened */ |
| 483 | if (req->options & DUP_HANDLE_CLOSE_SOURCE) |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame^] | 484 | { |
| 485 | if (src == current->process) close_handle( src, req->src_handle, &req->fd ); |
| 486 | else close_handle( src, req->src_handle, NULL ); |
| 487 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 488 | release_object( src ); |
| 489 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 490 | } |