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 |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <limits.h> |
| 26 | #include <string.h> |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 31 | #include "ntstatus.h" |
| 32 | #define WIN32_NO_STATUS |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 33 | #include "windef.h" |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 34 | #include "winternl.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 35 | |
| 36 | #include "handle.h" |
| 37 | #include "process.h" |
| 38 | #include "thread.h" |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 39 | #include "security.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 40 | #include "request.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 41 | |
| 42 | struct handle_entry |
| 43 | { |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 44 | struct object *ptr; /* object */ |
| 45 | unsigned int access; /* access rights */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 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; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 59 | |
| 60 | /* reserved handle access rights */ |
Alexandre Julliard | 78a3e63 | 2005-06-09 12:07:12 +0000 | [diff] [blame] | 61 | #define RESERVED_SHIFT 26 |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 66 | #define MIN_HANDLE_ENTRIES 32 |
Alexandre Julliard | 9434e19 | 2008-12-04 16:12:04 +0100 | [diff] [blame] | 67 | #define MAX_HANDLE_ENTRIES 0x00ffffff |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 68 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 69 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 70 | /* handle to table index conversion */ |
| 71 | |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 72 | /* handles are a multiple of 4 under NT; handle 0 is not used */ |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 73 | static inline obj_handle_t index_to_handle( int index ) |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 74 | { |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 75 | return (obj_handle_t)((index + 1) << 2); |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 76 | } |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 77 | static inline int handle_to_index( obj_handle_t handle ) |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 78 | { |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 79 | return (handle >> 2) - 1; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* global handle conversion */ |
| 83 | |
| 84 | #define HANDLE_OBFUSCATOR 0x544a4def |
| 85 | |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 86 | static inline int handle_is_global( obj_handle_t handle) |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 87 | { |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 88 | return (handle ^ HANDLE_OBFUSCATOR) <= (MAX_HANDLE_ENTRIES << 2); |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 89 | } |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 90 | static inline obj_handle_t handle_local_to_global( obj_handle_t handle ) |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 91 | { |
| 92 | if (!handle) return 0; |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 93 | return handle ^ HANDLE_OBFUSCATOR; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 94 | } |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 95 | static inline obj_handle_t handle_global_to_local( obj_handle_t handle ) |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 96 | { |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 97 | return handle ^ HANDLE_OBFUSCATOR; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 101 | static void handle_table_dump( struct object *obj, int verbose ); |
| 102 | static void handle_table_destroy( struct object *obj ); |
| 103 | |
| 104 | static const struct object_ops handle_table_ops = |
| 105 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 106 | sizeof(struct handle_table), /* size */ |
| 107 | handle_table_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 108 | no_get_type, /* get_type */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 109 | no_add_queue, /* add_queue */ |
| 110 | NULL, /* remove_queue */ |
| 111 | NULL, /* signaled */ |
| 112 | NULL, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 113 | no_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 114 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 115 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 116 | default_get_sd, /* get_sd */ |
| 117 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 118 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 119 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 120 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 121 | handle_table_destroy /* destroy */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | /* dump a handle table */ |
| 125 | static void handle_table_dump( struct object *obj, int verbose ) |
| 126 | { |
| 127 | int i; |
| 128 | struct handle_table *table = (struct handle_table *)obj; |
André Hentschel | c7becc3 | 2011-07-05 21:38:01 +0200 | [diff] [blame] | 129 | struct handle_entry *entry; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 130 | |
| 131 | assert( obj->ops == &handle_table_ops ); |
| 132 | |
| 133 | fprintf( stderr, "Handle table last=%d count=%d process=%p\n", |
| 134 | table->last, table->count, table->process ); |
| 135 | if (!verbose) return; |
| 136 | entry = table->entries; |
| 137 | for (i = 0; i <= table->last; i++, entry++) |
| 138 | { |
| 139 | if (!entry->ptr) continue; |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 140 | fprintf( stderr, " %04x: %p %08x ", |
Alexandre Julliard | 0ec183c | 2005-09-27 09:36:54 +0000 | [diff] [blame] | 141 | index_to_handle(i), entry->ptr, entry->access ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 142 | entry->ptr->ops->dump( entry->ptr, 0 ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* destroy a handle table */ |
| 147 | static void handle_table_destroy( struct object *obj ) |
| 148 | { |
| 149 | int i; |
| 150 | struct handle_table *table = (struct handle_table *)obj; |
Alexandre Julliard | 0901b23 | 2005-06-14 19:16:56 +0000 | [diff] [blame] | 151 | struct handle_entry *entry; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 152 | |
| 153 | assert( obj->ops == &handle_table_ops ); |
| 154 | |
Alexandre Julliard | 0901b23 | 2005-06-14 19:16:56 +0000 | [diff] [blame] | 155 | /* first notify all objects that handles are being closed */ |
| 156 | if (table->process) |
| 157 | { |
| 158 | for (i = 0, entry = table->entries; i <= table->last; i++, entry++) |
| 159 | { |
| 160 | struct object *obj = entry->ptr; |
| 161 | if (obj) obj->ops->close_handle( obj, table->process, index_to_handle(i) ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | for (i = 0, entry = table->entries; i <= table->last; i++, entry++) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 166 | { |
| 167 | struct object *obj = entry->ptr; |
| 168 | entry->ptr = NULL; |
| 169 | if (obj) release_object( obj ); |
| 170 | } |
| 171 | free( table->entries ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alexandre Julliard | 350c0ca | 2010-08-24 12:10:51 +0200 | [diff] [blame] | 174 | /* close all the process handles and free the handle table */ |
| 175 | void close_process_handles( struct process *process ) |
| 176 | { |
| 177 | struct handle_table *table = process->handles; |
| 178 | |
| 179 | process->handles = NULL; |
| 180 | if (table) release_object( table ); |
| 181 | } |
| 182 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 183 | /* allocate a new handle table */ |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 184 | struct handle_table *alloc_handle_table( struct process *process, int count ) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 185 | { |
| 186 | struct handle_table *table; |
| 187 | |
| 188 | if (count < MIN_HANDLE_ENTRIES) count = MIN_HANDLE_ENTRIES; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 189 | if (!(table = alloc_object( &handle_table_ops ))) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 190 | return NULL; |
| 191 | table->process = process; |
| 192 | table->count = count; |
| 193 | table->last = -1; |
| 194 | table->free = 0; |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 195 | if ((table->entries = mem_alloc( count * sizeof(*table->entries) ))) return table; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 196 | release_object( table ); |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 200 | /* grow a handle table */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 201 | static int grow_handle_table( struct handle_table *table ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 202 | { |
| 203 | struct handle_entry *new_entries; |
Alexandre Julliard | 9434e19 | 2008-12-04 16:12:04 +0100 | [diff] [blame] | 204 | int count = min( table->count * 2, MAX_HANDLE_ENTRIES ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 205 | |
Alexandre Julliard | 9434e19 | 2008-12-04 16:12:04 +0100 | [diff] [blame] | 206 | if (count == table->count || |
| 207 | !(new_entries = realloc( table->entries, count * sizeof(struct handle_entry) ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 208 | { |
Alexandre Julliard | 9434e19 | 2008-12-04 16:12:04 +0100 | [diff] [blame] | 209 | set_error( STATUS_INSUFFICIENT_RESOURCES ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 212 | table->entries = new_entries; |
| 213 | table->count = count; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 214 | return 1; |
| 215 | } |
| 216 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 217 | /* allocate the first free entry in the handle table */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 218 | static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned int access ) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 219 | { |
| 220 | struct handle_entry *entry = table->entries + table->free; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 221 | int i; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 222 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 223 | for (i = table->free; i <= table->last; i++, entry++) if (!entry->ptr) goto found; |
| 224 | if (i >= table->count) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 225 | { |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 226 | if (!grow_handle_table( table )) return 0; |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 227 | entry = table->entries + i; /* the entries may have moved */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 228 | } |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 229 | table->last = i; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 230 | found: |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 231 | table->free = i + 1; |
| 232 | entry->ptr = grab_object( obj ); |
| 233 | entry->access = access; |
| 234 | return index_to_handle(i); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 237 | /* allocate a handle for an object, incrementing its refcount */ |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 238 | /* return the handle, or 0 on error */ |
Rob Shearman | 92db6d2 | 2007-11-05 14:23:36 +0000 | [diff] [blame] | 239 | obj_handle_t alloc_handle_no_access_check( struct process *process, void *ptr, unsigned int access, unsigned int attr ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 240 | { |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 241 | struct object *obj = ptr; |
| 242 | |
Alexandre Julliard | c976db1 | 2005-07-13 12:09:49 +0000 | [diff] [blame] | 243 | access &= ~RESERVED_ALL; |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 244 | if (attr & OBJ_INHERIT) access |= RESERVED_INHERIT; |
Alexandre Julliard | c976db1 | 2005-07-13 12:09:49 +0000 | [diff] [blame] | 245 | if (!process->handles) |
| 246 | { |
| 247 | set_error( STATUS_NO_MEMORY ); |
| 248 | return 0; |
| 249 | } |
| 250 | return alloc_entry( process->handles, obj, access ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 253 | /* allocate a handle for an object, checking the dacl allows the process to */ |
| 254 | /* access it and incrementing its refcount */ |
| 255 | /* return the handle, or 0 on error */ |
| 256 | obj_handle_t alloc_handle( struct process *process, void *ptr, unsigned int access, unsigned int attr ) |
| 257 | { |
| 258 | struct object *obj = ptr; |
| 259 | access = obj->ops->map_access( obj, access ); |
| 260 | if (access && !check_object_access( obj, &access )) return 0; |
| 261 | return alloc_handle_no_access_check( process, ptr, access, attr ); |
| 262 | } |
| 263 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 264 | /* allocate a global handle for an object, incrementing its refcount */ |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 265 | /* return the handle, or 0 on error */ |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 266 | static obj_handle_t alloc_global_handle_no_access_check( void *obj, unsigned int access ) |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 267 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 268 | if (!global_table) |
| 269 | { |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 270 | if (!(global_table = (struct handle_table *)alloc_handle_table( NULL, 0 ))) |
| 271 | return 0; |
Alexandre Julliard | b00fb17 | 2006-03-22 20:32:04 +0100 | [diff] [blame] | 272 | make_object_static( &global_table->obj ); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 273 | } |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 274 | return handle_local_to_global( alloc_entry( global_table, obj, access )); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 277 | /* allocate a global handle for an object, checking the dacl allows the */ |
| 278 | /* process to access it and incrementing its refcount and incrementing its refcount */ |
| 279 | /* return the handle, or 0 on error */ |
| 280 | static obj_handle_t alloc_global_handle( void *obj, unsigned int access ) |
| 281 | { |
| 282 | if (access && !check_object_access( obj, &access )) return 0; |
| 283 | return alloc_global_handle_no_access_check( obj, access ); |
| 284 | } |
| 285 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 286 | /* return a handle entry, or NULL if the handle is invalid */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 287 | static struct handle_entry *get_handle( struct process *process, obj_handle_t handle ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 288 | { |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 289 | struct handle_table *table = process->handles; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 290 | struct handle_entry *entry; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 291 | int index; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 292 | |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 293 | if (handle_is_global(handle)) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 294 | { |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 295 | handle = handle_global_to_local(handle); |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 296 | table = global_table; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 297 | } |
Alexandre Julliard | 1a6c472 | 2009-12-01 13:49:43 +0100 | [diff] [blame] | 298 | if (!table) return NULL; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 299 | index = handle_to_index( handle ); |
Alexandre Julliard | 1a6c472 | 2009-12-01 13:49:43 +0100 | [diff] [blame] | 300 | if (index < 0) return NULL; |
| 301 | if (index > table->last) return NULL; |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 302 | entry = table->entries + index; |
Alexandre Julliard | 1a6c472 | 2009-12-01 13:49:43 +0100 | [diff] [blame] | 303 | if (!entry->ptr) return NULL; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 304 | return entry; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* attempt to shrink a table */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 308 | static void shrink_handle_table( struct handle_table *table ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 309 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 310 | struct handle_entry *entry = table->entries + table->last; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 311 | struct handle_entry *new_entries; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 312 | int count = table->count; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 313 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 314 | while (table->last >= 0) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 315 | { |
| 316 | if (entry->ptr) break; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 317 | table->last--; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 318 | entry--; |
| 319 | } |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 320 | if (table->last >= count / 4) return; /* no need to shrink */ |
| 321 | if (count < MIN_HANDLE_ENTRIES * 2) return; /* too small to shrink */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 322 | count /= 2; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 323 | if (!(new_entries = realloc( table->entries, count * sizeof(*new_entries) ))) return; |
| 324 | table->count = count; |
| 325 | table->entries = new_entries; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* copy the handle table of the parent process */ |
| 329 | /* return 1 if OK, 0 on error */ |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 330 | struct handle_table *copy_handle_table( struct process *process, struct process *parent ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 331 | { |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 332 | struct handle_table *parent_table = parent->handles; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 333 | struct handle_table *table; |
| 334 | int i; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 335 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 336 | assert( parent_table ); |
| 337 | assert( parent_table->obj.ops == &handle_table_ops ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 338 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 339 | if (!(table = (struct handle_table *)alloc_handle_table( process, parent_table->count ))) |
| 340 | return NULL; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 341 | |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 342 | if ((table->last = parent_table->last) >= 0) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 343 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 344 | struct handle_entry *ptr = table->entries; |
| 345 | memcpy( ptr, parent_table->entries, (table->last + 1) * sizeof(struct handle_entry) ); |
| 346 | for (i = 0; i <= table->last; i++, ptr++) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 347 | { |
| 348 | if (!ptr->ptr) continue; |
| 349 | if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr ); |
| 350 | else ptr->ptr = NULL; /* don't inherit this entry */ |
| 351 | } |
| 352 | } |
| 353 | /* attempt to shrink the table */ |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 354 | shrink_handle_table( table ); |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 355 | return table; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* close a handle and decrement the refcount of the associated object */ |
Alexandre Julliard | 60efdd5 | 2009-12-01 13:59:41 +0100 | [diff] [blame] | 359 | unsigned int close_handle( struct process *process, obj_handle_t handle ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 360 | { |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 361 | struct handle_table *table; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 362 | struct handle_entry *entry; |
| 363 | struct object *obj; |
| 364 | |
Alexandre Julliard | 60efdd5 | 2009-12-01 13:59:41 +0100 | [diff] [blame] | 365 | if (!(entry = get_handle( process, handle ))) return STATUS_INVALID_HANDLE; |
| 366 | if (entry->access & RESERVED_CLOSE_PROTECT) return STATUS_HANDLE_NOT_CLOSABLE; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 367 | obj = entry->ptr; |
Alexandre Julliard | 60efdd5 | 2009-12-01 13:59:41 +0100 | [diff] [blame] | 368 | if (!obj->ops->close_handle( obj, process, handle )) return STATUS_HANDLE_NOT_CLOSABLE; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 369 | entry->ptr = NULL; |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 370 | table = handle_is_global(handle) ? global_table : process->handles; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 371 | if (entry < table->entries + table->free) table->free = entry - table->entries; |
| 372 | if (entry == table->entries + table->last) shrink_handle_table( table ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 373 | release_object( obj ); |
Alexandre Julliard | 60efdd5 | 2009-12-01 13:59:41 +0100 | [diff] [blame] | 374 | return STATUS_SUCCESS; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 377 | /* retrieve the object corresponding to one of the magic pseudo-handles */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 378 | static inline struct object *get_magic_handle( obj_handle_t handle ) |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 379 | { |
Alexandre Julliard | 0d3d456 | 2008-12-08 16:04:20 +0100 | [diff] [blame] | 380 | switch(handle) |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 381 | { |
| 382 | case 0xfffffffe: /* current thread pseudo-handle */ |
| 383 | return ¤t->obj; |
| 384 | case 0x7fffffff: /* current process pseudo-handle */ |
| 385 | case 0xffffffff: /* current process pseudo-handle */ |
| 386 | return (struct object *)current->process; |
| 387 | default: |
| 388 | return NULL; |
| 389 | } |
| 390 | } |
| 391 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 392 | /* retrieve the object corresponding to a handle, incrementing its refcount */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 393 | struct object *get_handle_obj( struct process *process, obj_handle_t handle, |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 394 | unsigned int access, const struct object_ops *ops ) |
| 395 | { |
| 396 | struct handle_entry *entry; |
| 397 | struct object *obj; |
| 398 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 399 | if (!(obj = get_magic_handle( handle ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 400 | { |
Alexandre Julliard | 1a6c472 | 2009-12-01 13:49:43 +0100 | [diff] [blame] | 401 | if (!(entry = get_handle( process, handle ))) |
| 402 | { |
| 403 | set_error( STATUS_INVALID_HANDLE ); |
| 404 | return NULL; |
| 405 | } |
Bernhard Loos | bf3c2a0 | 2011-07-19 13:20:39 +0200 | [diff] [blame] | 406 | obj = entry->ptr; |
| 407 | if (ops && (obj->ops != ops)) |
| 408 | { |
| 409 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */ |
| 410 | return NULL; |
| 411 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 412 | if ((entry->access & access) != access) |
| 413 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 414 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 415 | return NULL; |
| 416 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 417 | } |
Bernhard Loos | bf3c2a0 | 2011-07-19 13:20:39 +0200 | [diff] [blame] | 418 | else if (ops && (obj->ops != ops)) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 419 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 420 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); /* not the right type */ |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
| 423 | return grab_object( obj ); |
| 424 | } |
| 425 | |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 426 | /* retrieve the access rights of a given handle */ |
| 427 | unsigned int get_handle_access( struct process *process, obj_handle_t handle ) |
| 428 | { |
| 429 | struct handle_entry *entry; |
| 430 | |
Vitaliy Margolen | bae7502 | 2007-01-24 23:43:04 -0700 | [diff] [blame] | 431 | if (get_magic_handle( handle )) return ~RESERVED_ALL; /* magic handles have all access rights */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 432 | if (!(entry = get_handle( process, handle ))) return 0; |
Vitaliy Margolen | bae7502 | 2007-01-24 23:43:04 -0700 | [diff] [blame] | 433 | return entry->access & ~RESERVED_ALL; |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Alexandre Julliard | 526a28d | 2002-10-02 23:49:30 +0000 | [diff] [blame] | 436 | /* find the first inherited handle of the given type */ |
| 437 | /* this is needed for window stations and desktops (don't ask...) */ |
| 438 | obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops ) |
| 439 | { |
| 440 | struct handle_table *table = process->handles; |
| 441 | struct handle_entry *ptr; |
| 442 | int i; |
| 443 | |
| 444 | if (!table) return 0; |
| 445 | |
| 446 | for (i = 0, ptr = table->entries; i <= table->last; i++, ptr++) |
| 447 | { |
| 448 | if (!ptr->ptr) continue; |
| 449 | if (ptr->ptr->ops != ops) continue; |
| 450 | if (ptr->access & RESERVED_INHERIT) return index_to_handle(i); |
| 451 | } |
| 452 | return 0; |
| 453 | } |
| 454 | |
Alexandre Julliard | d30b574 | 2007-12-05 16:45:32 +0100 | [diff] [blame] | 455 | /* enumerate handles of a given type */ |
| 456 | /* this is needed for window stations and desktops */ |
| 457 | obj_handle_t enumerate_handles( struct process *process, const struct object_ops *ops, |
| 458 | unsigned int *index ) |
| 459 | { |
| 460 | struct handle_table *table = process->handles; |
| 461 | unsigned int i; |
| 462 | struct handle_entry *entry; |
| 463 | |
| 464 | if (!table) return 0; |
| 465 | |
| 466 | for (i = *index, entry = &table->entries[i]; i <= table->last; i++, entry++) |
| 467 | { |
| 468 | if (!entry->ptr) continue; |
| 469 | if (entry->ptr->ops != ops) continue; |
| 470 | *index = i + 1; |
| 471 | return index_to_handle(i); |
| 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 476 | /* get/set the handle reserved flags */ |
| 477 | /* return the old flags (or -1 on error) */ |
Alexandre Julliard | 38502f7 | 2005-08-23 18:43:50 +0000 | [diff] [blame] | 478 | static int set_handle_flags( struct process *process, obj_handle_t handle, int mask, int flags ) |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 479 | { |
| 480 | struct handle_entry *entry; |
| 481 | unsigned int old_access; |
| 482 | |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 483 | if (get_magic_handle( handle )) |
| 484 | { |
| 485 | /* we can retrieve but not set info for magic handles */ |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 486 | if (mask) set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | ceeb693 | 1999-11-04 02:33:45 +0000 | [diff] [blame] | 487 | return 0; |
| 488 | } |
Alexandre Julliard | 1a6c472 | 2009-12-01 13:49:43 +0100 | [diff] [blame] | 489 | if (!(entry = get_handle( process, handle ))) |
| 490 | { |
| 491 | set_error( STATUS_INVALID_HANDLE ); |
| 492 | return -1; |
| 493 | } |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 494 | old_access = entry->access; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 495 | mask = (mask << RESERVED_SHIFT) & RESERVED_ALL; |
| 496 | flags = (flags << RESERVED_SHIFT) & mask; |
| 497 | entry->access = (entry->access & ~mask) | flags; |
Alexandre Julliard | d549f69 | 2000-12-22 02:04:15 +0000 | [diff] [blame] | 498 | return (old_access & RESERVED_ALL) >> RESERVED_SHIFT; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* duplicate a handle */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 502 | obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst, |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 503 | unsigned int access, unsigned int attr, unsigned int options ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 504 | { |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 505 | obj_handle_t res; |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 506 | struct handle_entry *entry; |
| 507 | unsigned int src_access; |
Alexandre Julliard | 0042cb3 | 1999-05-29 11:17:25 +0000 | [diff] [blame] | 508 | struct object *obj = get_handle_obj( src, src_handle, 0, NULL ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 509 | |
Alexandre Julliard | 8081e5a | 2001-01-05 04:08:07 +0000 | [diff] [blame] | 510 | if (!obj) return 0; |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 511 | if ((entry = get_handle( src, src_handle ))) |
| 512 | src_access = entry->access; |
| 513 | else /* pseudo-handle, give it full access */ |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 514 | src_access = obj->ops->map_access( obj, GENERIC_ALL ); |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 515 | src_access &= ~RESERVED_ALL; |
| 516 | |
| 517 | if (options & DUP_HANDLE_SAME_ACCESS) |
| 518 | access = src_access; |
Alexandre Julliard | eb2e77f | 1999-06-04 19:49:54 +0000 | [diff] [blame] | 519 | else |
Rob Shearman | 6a76a0a | 2007-02-21 13:59:59 +0000 | [diff] [blame] | 520 | access = obj->ops->map_access( obj, access ) & ~RESERVED_ALL; |
| 521 | |
| 522 | /* asking for the more access rights than src_access? */ |
| 523 | if (access & ~src_access) |
| 524 | { |
| 525 | if (options & DUP_HANDLE_MAKE_GLOBAL) |
| 526 | res = alloc_global_handle( obj, access ); |
| 527 | else |
| 528 | res = alloc_handle( dst, obj, access, attr ); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | if (options & DUP_HANDLE_MAKE_GLOBAL) |
| 533 | res = alloc_global_handle_no_access_check( obj, access ); |
| 534 | else |
| 535 | res = alloc_handle_no_access_check( dst, obj, access, attr ); |
| 536 | } |
| 537 | |
Alexandre Julliard | 0042cb3 | 1999-05-29 11:17:25 +0000 | [diff] [blame] | 538 | release_object( obj ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 539 | return res; |
| 540 | } |
| 541 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 542 | /* open a new handle to an existing object */ |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 543 | obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name, |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 544 | const struct object_ops *ops, unsigned int access, unsigned int attr ) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 545 | { |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 546 | obj_handle_t handle = 0; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 547 | struct object *obj = find_object( namespace, name, attr ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 548 | if (obj) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 549 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 550 | if (ops && obj->ops != ops) |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 551 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 552 | else |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 553 | handle = alloc_handle( current->process, obj, access, attr ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 554 | release_object( obj ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 555 | } |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 556 | else |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 557 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 558 | return handle; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Eric Pouech | 9fd54b2 | 2003-09-16 01:07:21 +0000 | [diff] [blame] | 561 | /* return the size of the handle table of a given process */ |
| 562 | unsigned int get_handle_table_count( struct process *process ) |
| 563 | { |
Alexandre Julliard | c976db1 | 2005-07-13 12:09:49 +0000 | [diff] [blame] | 564 | if (!process->handles) return 0; |
Eric Pouech | 9fd54b2 | 2003-09-16 01:07:21 +0000 | [diff] [blame] | 565 | return process->handles->count; |
| 566 | } |
| 567 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 568 | /* close a handle */ |
| 569 | DECL_HANDLER(close_handle) |
| 570 | { |
Alexandre Julliard | 60efdd5 | 2009-12-01 13:59:41 +0100 | [diff] [blame] | 571 | unsigned int err = close_handle( current->process, req->handle ); |
| 572 | set_error( err ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* set a handle information */ |
| 576 | DECL_HANDLER(set_handle_info) |
| 577 | { |
Alexandre Julliard | 38502f7 | 2005-08-23 18:43:50 +0000 | [diff] [blame] | 578 | reply->old_flags = set_handle_flags( current->process, req->handle, req->mask, req->flags ); |
| 579 | } |
| 580 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 581 | /* duplicate a handle */ |
| 582 | DECL_HANDLER(dup_handle) |
| 583 | { |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 584 | struct process *src, *dst; |
| 585 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 586 | reply->handle = 0; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 587 | if ((src = get_process_from_handle( req->src_process, PROCESS_DUP_HANDLE ))) |
| 588 | { |
| 589 | if (req->options & DUP_HANDLE_MAKE_GLOBAL) |
| 590 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 591 | reply->handle = duplicate_handle( src, req->src_handle, NULL, |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 592 | req->access, req->attributes, req->options ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 593 | } |
| 594 | else if ((dst = get_process_from_handle( req->dst_process, PROCESS_DUP_HANDLE ))) |
| 595 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 596 | reply->handle = duplicate_handle( src, req->src_handle, dst, |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 597 | req->access, req->attributes, req->options ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 598 | release_object( dst ); |
| 599 | } |
| 600 | /* close the handle no matter what happened */ |
Alexandre Julliard | 60efdd5 | 2009-12-01 13:59:41 +0100 | [diff] [blame] | 601 | if (req->options & DUP_HANDLE_CLOSE_SOURCE) reply->closed = !close_handle( src, req->src_handle ); |
Alexandre Julliard | 3410354 | 2007-01-18 12:18:51 +0100 | [diff] [blame] | 602 | reply->self = (src == current->process); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 603 | release_object( src ); |
| 604 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 605 | } |
Vitaliy Margolen | bae7502 | 2007-01-24 23:43:04 -0700 | [diff] [blame] | 606 | |
| 607 | DECL_HANDLER(get_object_info) |
| 608 | { |
| 609 | struct object *obj; |
Alexandre Julliard | 658dae9 | 2010-01-08 13:01:50 +0100 | [diff] [blame] | 610 | WCHAR *name; |
Vitaliy Margolen | bae7502 | 2007-01-24 23:43:04 -0700 | [diff] [blame] | 611 | |
| 612 | if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return; |
| 613 | |
| 614 | reply->access = get_handle_access( current->process, req->handle ); |
| 615 | reply->ref_count = obj->refcount; |
Alexandre Julliard | 658dae9 | 2010-01-08 13:01:50 +0100 | [diff] [blame] | 616 | if ((name = get_object_full_name( obj, &reply->total ))) |
| 617 | set_reply_data_ptr( name, min( reply->total, get_reply_max_size() )); |
Vitaliy Margolen | bae7502 | 2007-01-24 23:43:04 -0700 | [diff] [blame] | 618 | release_object( obj ); |
| 619 | } |
Rob Shearman | 5af809a | 2007-10-02 15:54:51 +0100 | [diff] [blame] | 620 | |
| 621 | DECL_HANDLER(set_security_object) |
| 622 | { |
| 623 | data_size_t sd_size = get_req_data_size(); |
| 624 | const struct security_descriptor *sd = get_req_data(); |
| 625 | struct object *obj; |
| 626 | unsigned int access = 0; |
| 627 | |
| 628 | if (!sd_is_valid( sd, sd_size )) |
| 629 | { |
| 630 | set_error( STATUS_ACCESS_VIOLATION ); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (req->security_info & OWNER_SECURITY_INFORMATION || |
| 635 | req->security_info & GROUP_SECURITY_INFORMATION) |
| 636 | access |= WRITE_OWNER; |
| 637 | if (req->security_info & SACL_SECURITY_INFORMATION) |
| 638 | access |= ACCESS_SYSTEM_SECURITY; |
| 639 | if (req->security_info & DACL_SECURITY_INFORMATION) |
| 640 | access |= WRITE_DAC; |
| 641 | |
| 642 | if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return; |
| 643 | |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 644 | obj->ops->set_sd( obj, sd, req->security_info ); |
Rob Shearman | 5af809a | 2007-10-02 15:54:51 +0100 | [diff] [blame] | 645 | release_object( obj ); |
| 646 | } |
Rob Shearman | 5356bfd | 2007-10-02 15:55:13 +0100 | [diff] [blame] | 647 | |
| 648 | DECL_HANDLER(get_security_object) |
| 649 | { |
| 650 | const struct security_descriptor *sd; |
| 651 | struct object *obj; |
| 652 | unsigned int access = READ_CONTROL; |
| 653 | struct security_descriptor req_sd; |
| 654 | int present; |
| 655 | const SID *owner, *group; |
| 656 | const ACL *sacl, *dacl; |
| 657 | |
| 658 | if (req->security_info & SACL_SECURITY_INFORMATION) |
| 659 | access |= ACCESS_SYSTEM_SECURITY; |
| 660 | |
| 661 | if (!(obj = get_handle_obj( current->process, req->handle, access, NULL ))) return; |
| 662 | |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 663 | sd = obj->ops->get_sd( obj ); |
Rob Shearman | 5356bfd | 2007-10-02 15:55:13 +0100 | [diff] [blame] | 664 | if (sd) |
| 665 | { |
| 666 | req_sd.control = sd->control & ~SE_SELF_RELATIVE; |
| 667 | |
| 668 | owner = sd_get_owner( sd ); |
| 669 | if (req->security_info & OWNER_SECURITY_INFORMATION) |
| 670 | req_sd.owner_len = sd->owner_len; |
Rob Shearman | 9980f47 | 2007-10-31 16:53:37 +0000 | [diff] [blame] | 671 | else |
| 672 | req_sd.owner_len = 0; |
Rob Shearman | 5356bfd | 2007-10-02 15:55:13 +0100 | [diff] [blame] | 673 | |
| 674 | group = sd_get_group( sd ); |
| 675 | if (req->security_info & GROUP_SECURITY_INFORMATION) |
| 676 | req_sd.group_len = sd->group_len; |
Rob Shearman | 9980f47 | 2007-10-31 16:53:37 +0000 | [diff] [blame] | 677 | else |
| 678 | req_sd.group_len = 0; |
Rob Shearman | 5356bfd | 2007-10-02 15:55:13 +0100 | [diff] [blame] | 679 | |
| 680 | req_sd.control |= SE_SACL_PRESENT; |
| 681 | sacl = sd_get_sacl( sd, &present ); |
| 682 | if (req->security_info & SACL_SECURITY_INFORMATION && present) |
| 683 | req_sd.sacl_len = sd->sacl_len; |
| 684 | else |
| 685 | req_sd.sacl_len = 0; |
| 686 | |
| 687 | req_sd.control |= SE_DACL_PRESENT; |
| 688 | dacl = sd_get_dacl( sd, &present ); |
| 689 | if (req->security_info & DACL_SECURITY_INFORMATION && present) |
| 690 | req_sd.dacl_len = sd->dacl_len; |
| 691 | else |
| 692 | req_sd.dacl_len = 0; |
| 693 | |
| 694 | reply->sd_len = sizeof(req_sd) + req_sd.owner_len + req_sd.group_len + |
| 695 | req_sd.sacl_len + req_sd.dacl_len; |
| 696 | if (reply->sd_len <= get_reply_max_size()) |
| 697 | { |
| 698 | char *ptr = set_reply_data_size(reply->sd_len); |
| 699 | |
| 700 | memcpy( ptr, &req_sd, sizeof(req_sd) ); |
| 701 | ptr += sizeof(req_sd); |
| 702 | memcpy( ptr, owner, req_sd.owner_len ); |
| 703 | ptr += req_sd.owner_len; |
| 704 | memcpy( ptr, group, req_sd.group_len ); |
| 705 | ptr += req_sd.group_len; |
| 706 | memcpy( ptr, sacl, req_sd.sacl_len ); |
| 707 | ptr += req_sd.sacl_len; |
| 708 | memcpy( ptr, dacl, req_sd.dacl_len ); |
| 709 | } |
| 710 | else |
| 711 | set_error(STATUS_BUFFER_TOO_SMALL); |
| 712 | } |
| 713 | |
| 714 | release_object( obj ); |
| 715 | } |