Release 980927
[wine.git] / scheduler / handle.c
blob3dfd03ab9058d863c22beefbae9599322d647b35
1 /*
2 * Win32 process handles
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include "debug.h"
10 #include "winbase.h"
11 #include "winerror.h"
12 #include "heap.h"
13 #include "process.h"
14 #include "server.h"
15 #include "thread.h"
17 #define HTABLE_SIZE 0x30 /* Handle table initial size */
18 #define HTABLE_INC 0x10 /* Handle table increment */
20 /* Reserved access rights */
21 #define RESERVED_ALL (0x0007 << RESERVED_SHIFT)
22 #define RESERVED_SHIFT 25
23 #define RESERVED_INHERIT (HANDLE_FLAG_INHERIT<<RESERVED_SHIFT)
24 #define RESERVED_CLOSE_PROTECT (HANDLE_FLAG_PROTECT_FROM_CLOSE<<RESERVED_SHIFT)
27 /***********************************************************************
28 * HANDLE_GrowTable
30 static BOOL32 HANDLE_GrowTable( PDB32 *process, INT32 incr )
32 HANDLE_TABLE *table;
34 SYSTEM_LOCK();
35 table = process->handle_table;
36 table = HeapReAlloc( process->system_heap,
37 HEAP_ZERO_MEMORY | HEAP_NO_SERIALIZE, table,
38 sizeof(HANDLE_TABLE) +
39 (table->count + incr - 1) * sizeof(HANDLE_ENTRY) );
40 if (table)
42 table->count += incr;
43 process->handle_table = table;
45 SYSTEM_UNLOCK();
46 return (table != NULL);
50 /***********************************************************************
51 * HANDLE_CreateTable
53 * Create a process handle table, optionally inheriting the parent's handles.
55 BOOL32 HANDLE_CreateTable( PDB32 *pdb, BOOL32 inherit )
57 DWORD size;
59 /* Process must not already have a handle table */
60 assert( !pdb->handle_table );
62 /* If this is the first process, simply allocate a table */
63 if (!pdb->parent) inherit = FALSE;
65 SYSTEM_LOCK();
66 size = inherit ? pdb->parent->handle_table->count : HTABLE_SIZE;
67 if ((pdb->handle_table = HeapAlloc( pdb->system_heap,
68 HEAP_ZERO_MEMORY | HEAP_NO_SERIALIZE,
69 sizeof(HANDLE_TABLE) +
70 (size-1) * sizeof(HANDLE_ENTRY) )))
72 pdb->handle_table->count = size;
73 if (inherit)
75 HANDLE_ENTRY *src = pdb->parent->handle_table->entries;
76 HANDLE_ENTRY *dst = pdb->handle_table->entries;
77 HANDLE32 h;
79 for (h = 0; h < size; h++, src++, dst++)
81 /* Check if handle is valid and inheritable */
82 if (src->ptr && (src->access & RESERVED_INHERIT))
84 dst->access = src->access;
85 dst->ptr = src->ptr;
86 dst->server = src->server;
87 K32OBJ_IncCount( dst->ptr );
91 /* Handle 1 is the process itself (unless the parent decided otherwise) */
92 if (!pdb->handle_table->entries[1].ptr)
94 pdb->handle_table->entries[1].ptr = &pdb->header;
95 pdb->handle_table->entries[1].access = PROCESS_ALL_ACCESS;
96 pdb->handle_table->entries[1].server = -1; /* FIXME */
97 K32OBJ_IncCount( &pdb->header );
100 SYSTEM_UNLOCK();
101 return (pdb->handle_table != NULL);
105 /***********************************************************************
106 * HANDLE_Alloc
108 * Allocate a handle for a kernel object and increment its refcount.
110 HANDLE32 HANDLE_Alloc( PDB32 *pdb, K32OBJ *ptr, DWORD access,
111 BOOL32 inherit, int server_handle )
113 HANDLE32 h;
114 HANDLE_ENTRY *entry;
116 assert( ptr );
118 /* Set the inherit reserved flag */
119 access &= ~RESERVED_ALL;
120 if (inherit) access |= RESERVED_INHERIT;
122 SYSTEM_LOCK();
123 K32OBJ_IncCount( ptr );
124 /* Don't try to allocate handle 0 */
125 entry = pdb->handle_table->entries + 1;
126 for (h = 1; h < pdb->handle_table->count; h++, entry++)
127 if (!entry->ptr) break;
128 if ((h < pdb->handle_table->count) || HANDLE_GrowTable( pdb, HTABLE_INC ))
130 entry = &pdb->handle_table->entries[h];
131 entry->access = access;
132 entry->ptr = ptr;
133 entry->server = server_handle;
134 SYSTEM_UNLOCK();
135 return h;
137 K32OBJ_DecCount( ptr );
138 SYSTEM_UNLOCK();
139 if (server_handle != -1) CLIENT_CloseHandle( server_handle );
140 SetLastError( ERROR_OUTOFMEMORY );
141 return INVALID_HANDLE_VALUE32;
145 /***********************************************************************
146 * HANDLE_GetObjPtr
148 * Retrieve a pointer to a kernel object and increments its reference count.
149 * The refcount must be decremented when the pointer is no longer used.
151 K32OBJ *HANDLE_GetObjPtr( PDB32 *pdb, HANDLE32 handle,
152 K32OBJ_TYPE type, DWORD access,
153 int *server_handle )
155 K32OBJ *ptr = NULL;
157 SYSTEM_LOCK();
158 if ((handle > 0) && (handle <= pdb->handle_table->count))
160 HANDLE_ENTRY *entry = &pdb->handle_table->entries[handle];
161 if ((entry->access & access) != access)
162 WARN(win32, "Handle %08x bad access (acc=%08lx req=%08lx)\n",
163 handle, entry->access, access );
164 ptr = entry->ptr;
165 if (server_handle) *server_handle = entry->server;
167 else if (handle == CURRENT_THREAD_PSEUDOHANDLE)
169 ptr = (K32OBJ *)THREAD_Current();
170 if (server_handle) *server_handle = CURRENT_THREAD_PSEUDOHANDLE;
172 else if (handle == CURRENT_PROCESS_PSEUDOHANDLE)
174 ptr = (K32OBJ *)PROCESS_Current();
175 if (server_handle) *server_handle = CURRENT_PROCESS_PSEUDOHANDLE;
178 if (ptr && ((type == K32OBJ_UNKNOWN) || (ptr->type == type)))
179 K32OBJ_IncCount( ptr );
180 else
181 ptr = NULL;
183 SYSTEM_UNLOCK();
184 if (!ptr) SetLastError( ERROR_INVALID_HANDLE );
185 return ptr;
189 /***********************************************************************
190 * HANDLE_SetObjPtr
192 * Change the object pointer of a handle, and increment the refcount.
193 * Use with caution!
195 BOOL32 HANDLE_SetObjPtr( PDB32 *pdb, HANDLE32 handle, K32OBJ *ptr,
196 DWORD access )
198 BOOL32 ret = FALSE;
200 SYSTEM_LOCK();
201 if ((handle > 0) && (handle <= pdb->handle_table->count))
203 HANDLE_ENTRY *entry = &pdb->handle_table->entries[handle];
204 K32OBJ *old_ptr = entry->ptr;
205 K32OBJ_IncCount( ptr );
206 entry->access = access;
207 entry->ptr = ptr;
208 if (old_ptr) K32OBJ_DecCount( old_ptr );
209 ret = TRUE;
211 SYSTEM_UNLOCK();
212 if (!ret) SetLastError( ERROR_INVALID_HANDLE );
213 return ret;
217 /*********************************************************************
218 * HANDLE_GetAccess
220 static BOOL32 HANDLE_GetAccess( PDB32 *pdb, HANDLE32 handle, LPDWORD access )
222 BOOL32 ret = FALSE;
224 SYSTEM_LOCK();
225 if ((handle > 0) && (handle <= pdb->handle_table->count))
227 HANDLE_ENTRY *entry = &pdb->handle_table->entries[handle];
228 if (entry->ptr)
230 *access = entry->access & ~RESERVED_ALL;
231 ret = TRUE;
234 SYSTEM_UNLOCK();
235 if (!ret) SetLastError( ERROR_INVALID_HANDLE );
236 return ret;
240 /*********************************************************************
241 * HANDLE_Close
243 static BOOL32 HANDLE_Close( PDB32 *pdb, HANDLE32 handle )
245 BOOL32 ret = FALSE;
246 K32OBJ *ptr;
248 SYSTEM_LOCK();
249 if ((handle > 0) && (handle <= pdb->handle_table->count))
251 HANDLE_ENTRY *entry = &pdb->handle_table->entries[handle];
252 if ((ptr = entry->ptr))
254 if (!(entry->access & RESERVED_CLOSE_PROTECT))
256 entry->access = 0;
257 entry->ptr = NULL;
258 if (entry->server != -1)
259 CLIENT_CloseHandle( entry->server );
260 K32OBJ_DecCount( ptr );
261 ret = TRUE;
263 /* FIXME: else SetLastError */
266 SYSTEM_UNLOCK();
267 if (!ret) SetLastError( ERROR_INVALID_HANDLE );
268 return ret;
272 /*********************************************************************
273 * HANDLE_CloseAll
275 * Close all handles pointing to a given object (or all handles of the
276 * process if the object is NULL)
278 void HANDLE_CloseAll( PDB32 *pdb, K32OBJ *obj )
280 HANDLE_ENTRY *entry;
281 K32OBJ *ptr;
282 HANDLE32 handle;
284 SYSTEM_LOCK();
285 entry = pdb->handle_table->entries;
286 for (handle = 0; handle < pdb->handle_table->count; handle++, entry++)
288 if (!(ptr = entry->ptr)) continue; /* empty slot */
289 if (obj && (ptr != obj)) continue; /* not the right object */
290 entry->access = 0;
291 entry->ptr = NULL;
292 if (entry->server != -1) CLIENT_CloseHandle( entry->server );
293 K32OBJ_DecCount( ptr );
295 SYSTEM_UNLOCK();
299 /*********************************************************************
300 * CloseHandle (KERNEL32.23)
302 BOOL32 WINAPI CloseHandle( HANDLE32 handle )
304 return HANDLE_Close( PROCESS_Current(), handle );
308 /*********************************************************************
309 * GetHandleInformation (KERNEL32.336)
311 BOOL32 WINAPI GetHandleInformation( HANDLE32 handle, LPDWORD flags )
313 BOOL32 ret = FALSE;
314 PDB32 *pdb = PROCESS_Current();
316 SYSTEM_LOCK();
317 if ((handle > 0) && (handle <= pdb->handle_table->count))
319 HANDLE_ENTRY *entry = &pdb->handle_table->entries[handle];
320 if (entry->ptr)
322 if (flags)
323 *flags = (entry->access & RESERVED_ALL) >> RESERVED_SHIFT;
324 ret = TRUE;
327 SYSTEM_UNLOCK();
328 if (!ret) SetLastError( ERROR_INVALID_HANDLE );
329 return ret;
333 /*********************************************************************
334 * SetHandleInformation (KERNEL32.653)
336 BOOL32 WINAPI SetHandleInformation( HANDLE32 handle, DWORD mask, DWORD flags )
338 BOOL32 ret = FALSE;
339 PDB32 *pdb = PROCESS_Current();
341 mask = (mask << RESERVED_SHIFT) & RESERVED_ALL;
342 flags = (flags << RESERVED_SHIFT) & RESERVED_ALL;
343 SYSTEM_LOCK();
344 if ((handle > 0) && (handle <= pdb->handle_table->count))
346 HANDLE_ENTRY *entry = &pdb->handle_table->entries[handle];
347 if (entry->ptr)
349 entry->access = (entry->access & ~mask) | flags;
350 ret = TRUE;
353 SYSTEM_UNLOCK();
354 if (!ret) SetLastError( ERROR_INVALID_HANDLE );
355 return ret;
359 /*********************************************************************
360 * DuplicateHandle (KERNEL32.192)
362 BOOL32 WINAPI DuplicateHandle( HANDLE32 source_process, HANDLE32 source,
363 HANDLE32 dest_process, HANDLE32 *dest,
364 DWORD access, BOOL32 inherit, DWORD options )
366 PDB32 *src_pdb = NULL, *dst_pdb = NULL;
367 K32OBJ *obj = NULL;
368 BOOL32 ret = FALSE;
369 HANDLE32 handle;
370 int src_process, src_handle, dst_process, dst_handle;
372 SYSTEM_LOCK();
374 if (!(src_pdb = PROCESS_GetPtr( source_process, PROCESS_DUP_HANDLE, &src_process )))
375 goto done;
376 if (!(obj = HANDLE_GetObjPtr( src_pdb, source, K32OBJ_UNKNOWN, 0, &src_handle )))
377 goto done;
379 /* Now that we are sure the source is valid, handle the options */
381 if (options & DUPLICATE_SAME_ACCESS)
382 HANDLE_GetAccess( src_pdb, source, &access );
383 if (options & DUPLICATE_CLOSE_SOURCE)
384 HANDLE_Close( src_pdb, source );
386 /* And duplicate the handle in the dest process */
388 if (!(dst_pdb = PROCESS_GetPtr( dest_process, PROCESS_DUP_HANDLE, &dst_process )))
389 goto done;
391 if ((src_process != -1) && (src_handle != -1) && (dst_process != -1))
392 dst_handle = CLIENT_DuplicateHandle( src_process, src_handle, dst_process, -1,
393 access, inherit, options );
394 else
395 dst_handle = -1;
397 if ((handle = HANDLE_Alloc( dst_pdb, obj, access, inherit,
398 dst_handle )) != INVALID_HANDLE_VALUE32)
400 if (dest) *dest = handle;
401 ret = TRUE;
404 done:
405 if (dst_pdb) K32OBJ_DecCount( &dst_pdb->header );
406 if (obj) K32OBJ_DecCount( obj );
407 if (src_pdb) K32OBJ_DecCount( &src_pdb->header );
408 SYSTEM_UNLOCK();
409 return ret;