3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: ntoskrnl/ob/handle.c
24 * PURPOSE: Managing handles
25 * PROGRAMMER: David Welch (welch@cwcom.net)
30 /* INCLUDES ****************************************************************/
34 #include <internal/debug.h>
36 /* TYPES *******************************************************************/
39 * PURPOSE: Defines a handle
41 typedef struct _HANDLE_ENTRY
44 ACCESS_MASK GrantedAccess
;
45 } HANDLE_ENTRY
, *PHANDLE_ENTRY
;
47 #define HANDLE_BLOCK_ENTRIES \
49 (sizeof(LIST_ENTRY) + sizeof(ULONG))) / sizeof(HANDLE_ENTRY))
51 #define OB_HANDLE_FLAG_MASK 0x00000007
52 #define OB_HANDLE_FLAG_AUDIT 0x00000004
53 #define OB_HANDLE_FLAG_PROTECT 0x00000002
54 #define OB_HANDLE_FLAG_INHERIT 0x00000001
56 #define OB_POINTER_TO_ENTRY(Pointer) \
57 (PVOID)((ULONG_PTR)(Pointer) & ~OB_HANDLE_FLAG_MASK)
59 #define OB_ENTRY_TO_POINTER(Entry) \
60 (PVOID)((ULONG_PTR)(Entry) & ~OB_HANDLE_FLAG_MASK)
63 * PURPOSE: Defines a 4 page's worth of handles
68 ULONG allocation_hint
;
69 ULONG allocation_count
;
70 HANDLE_ENTRY handles
[HANDLE_BLOCK_ENTRIES
];
71 } HANDLE_BLOCK
, *PHANDLE_BLOCK
;
74 /* GLOBALS *******************************************************************/
76 #define TAG_HANDLE_TABLE TAG('H', 'T', 'B', 'L')
78 /* FUNCTIONS ***************************************************************/
82 * FUNCTION: Get the data structure for a handle
84 * Process = Process to get the handle for
86 * ARGUMENTS: A pointer to the information about the handle on success,
90 ObpGetObjectByHandle(PHANDLE_TABLE HandleTable
,
95 unsigned int handle
= (((unsigned int)h
) >> 2) - 1;
96 unsigned int count
= handle
/ HANDLE_BLOCK_ENTRIES
;
97 HANDLE_BLOCK
* blk
= NULL
;
100 DPRINT("ObpGetObjectByHandle(HandleTable %x, h %x)\n",HandleTable
,h
);
102 current
= HandleTable
->ListHead
.Flink
;
103 DPRINT("current %x\n",current
);
105 for (i
= 0; i
< count
; i
++)
107 current
= current
->Flink
;
108 if (current
== (&(HandleTable
->ListHead
)))
114 blk
= CONTAINING_RECORD(current
,HANDLE_BLOCK
,entry
);
117 DPRINT("object: %p\n",&(blk
->handles
[handle
%HANDLE_BLOCK_ENTRIES
]));
118 return(&(blk
->handles
[handle
%HANDLE_BLOCK_ENTRIES
]));
123 ObpQueryHandleAttributes(HANDLE Handle
,
124 POBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo
)
128 PHANDLE_ENTRY HandleEntry
;
130 DPRINT("ObpQueryHandleAttributes(Handle %x)\n", Handle
);
132 Process
= PsGetCurrentProcess();
134 KeAcquireSpinLock(&Process
->HandleTable
.ListLock
, &oldIrql
);
135 HandleEntry
= ObpGetObjectByHandle(&Process
->HandleTable
,
138 if (HandleEntry
== NULL
)
140 KeReleaseSpinLock(&Process
->HandleTable
.ListLock
, oldIrql
);
141 return STATUS_INVALID_HANDLE
;
144 HandleInfo
->Inherit
=
145 ((ULONG_PTR
)HandleEntry
->ObjectBody
& OB_HANDLE_FLAG_INHERIT
);
146 HandleInfo
->ProtectFromClose
=
147 ((ULONG_PTR
)HandleEntry
->ObjectBody
& OB_HANDLE_FLAG_PROTECT
);
149 KeReleaseSpinLock(&Process
->HandleTable
.ListLock
, oldIrql
);
151 return STATUS_SUCCESS
;
156 ObpSetHandleAttributes(HANDLE Handle
,
157 POBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo
)
159 PHANDLE_ENTRY HandleEntry
;
163 DPRINT("ObpQueryHandleAttributes(Handle %x)\n", Handle
);
165 Process
= PsGetCurrentProcess();
167 KeAcquireSpinLock(&Process
->HandleTable
.ListLock
, &oldIrql
);
168 HandleEntry
= ObpGetObjectByHandle(&Process
->HandleTable
,
171 if (HandleEntry
== NULL
)
173 KeReleaseSpinLock(&Process
->HandleTable
.ListLock
, oldIrql
);
174 return STATUS_INVALID_HANDLE
;
177 if (HandleInfo
->Inherit
)
178 HandleEntry
->ObjectBody
= (PVOID
)((ULONG_PTR
)HandleEntry
->ObjectBody
| OB_HANDLE_FLAG_INHERIT
);
180 HandleEntry
->ObjectBody
= (PVOID
)((ULONG_PTR
)HandleEntry
->ObjectBody
& ~OB_HANDLE_FLAG_INHERIT
);
182 if (HandleInfo
->ProtectFromClose
)
183 HandleEntry
->ObjectBody
= (PVOID
)((ULONG_PTR
)HandleEntry
->ObjectBody
| OB_HANDLE_FLAG_PROTECT
);
185 HandleEntry
->ObjectBody
= (PVOID
)((ULONG_PTR
)HandleEntry
->ObjectBody
& ~OB_HANDLE_FLAG_PROTECT
);
187 /* FIXME: Do we need to set anything in the object header??? */
189 KeReleaseSpinLock(&Process
->HandleTable
.ListLock
, oldIrql
);
191 return STATUS_SUCCESS
;
196 ObDuplicateObject(PEPROCESS SourceProcess
,
197 PEPROCESS TargetProcess
,
199 PHANDLE TargetHandle
,
200 ACCESS_MASK DesiredAccess
,
201 BOOLEAN InheritHandle
,
205 PHANDLE_ENTRY SourceHandleEntry
;
208 KeAcquireSpinLock(&SourceProcess
->HandleTable
.ListLock
, &oldIrql
);
209 SourceHandleEntry
= ObpGetObjectByHandle(&SourceProcess
->HandleTable
,
212 if (SourceHandleEntry
== NULL
)
214 KeReleaseSpinLock(&SourceProcess
->HandleTable
.ListLock
, oldIrql
);
215 return STATUS_INVALID_HANDLE
;
218 ObjectBody
= OB_ENTRY_TO_POINTER(SourceHandleEntry
->ObjectBody
);
219 ObReferenceObjectByPointer(ObjectBody
,
224 if (Options
& DUPLICATE_SAME_ACCESS
)
226 DesiredAccess
= SourceHandleEntry
->GrantedAccess
;
229 KeReleaseSpinLock(&SourceProcess
->HandleTable
.ListLock
, oldIrql
);
230 ObCreateHandle(TargetProcess
,
236 if (Options
& DUPLICATE_CLOSE_SOURCE
)
238 ZwClose(SourceHandle
);
241 ObDereferenceObject(ObjectBody
);
243 return STATUS_SUCCESS
;
250 NtDuplicateObject (IN HANDLE SourceProcessHandle
,
251 IN HANDLE SourceHandle
,
252 IN HANDLE TargetProcessHandle
,
253 OUT PHANDLE UnsafeTargetHandle
,
254 IN ACCESS_MASK DesiredAccess
,
255 IN BOOLEAN InheritHandle
,
258 * FUNCTION: Copies a handle from one process space to another
260 * SourceProcessHandle = The source process owning the handle. The
261 * source process should have opened
262 * the SourceHandle with PROCESS_DUP_HANDLE
264 * SourceHandle = The handle to the object.
265 * TargetProcessHandle = The destination process owning the handle
266 * TargetHandle (OUT) = Caller should supply storage for the
268 * DesiredAccess = The desired access to the handle.
269 * InheritHandle = Indicates wheter the new handle will be inheritable
271 * Options = Specifies special actions upon duplicating the handle.
272 * Can be one of the values DUPLICATE_CLOSE_SOURCE |
273 * DUPLICATE_SAME_ACCESS. DUPLICATE_CLOSE_SOURCE specifies
274 * that the source handle should be closed after duplicating.
275 * DUPLICATE_SAME_ACCESS specifies to ignore the
276 * DesiredAccess paramter and just grant the same access to
279 * REMARKS: This function maps to the win32 DuplicateHandle.
282 PEPROCESS SourceProcess
;
283 PEPROCESS TargetProcess
;
284 PHANDLE_ENTRY SourceHandleEntry
;
290 ASSERT_IRQL(PASSIVE_LEVEL
);
292 Status
= ObReferenceObjectByHandle(SourceProcessHandle
,
296 (PVOID
*)&SourceProcess
,
298 if (!NT_SUCCESS(Status
))
303 Status
= ObReferenceObjectByHandle(TargetProcessHandle
,
307 (PVOID
*)&TargetProcess
,
309 if (!NT_SUCCESS(Status
))
311 ObDereferenceObject(SourceProcess
);
315 /* Check for magic handle first */
316 if (SourceHandle
== NtCurrentThread())
318 ObReferenceObjectByHandle(SourceHandle
,
325 ObCreateHandle(TargetProcess
,
333 KeAcquireSpinLock(&SourceProcess
->HandleTable
.ListLock
, &oldIrql
);
334 SourceHandleEntry
= ObpGetObjectByHandle(&SourceProcess
->HandleTable
,
337 if (SourceHandleEntry
== NULL
)
339 KeReleaseSpinLock(&SourceProcess
->HandleTable
.ListLock
, oldIrql
);
340 ObDereferenceObject(SourceProcess
);
341 ObDereferenceObject(TargetProcess
);
342 return(STATUS_INVALID_HANDLE
);
344 ObjectBody
= OB_ENTRY_TO_POINTER(SourceHandleEntry
->ObjectBody
);
345 ObReferenceObjectByPointer(ObjectBody
,
350 if (Options
& DUPLICATE_SAME_ACCESS
)
352 DesiredAccess
= SourceHandleEntry
->GrantedAccess
;
355 KeReleaseSpinLock(&SourceProcess
->HandleTable
.ListLock
, oldIrql
);
356 if (!((ULONG_PTR
)SourceHandleEntry
->ObjectBody
& OB_HANDLE_FLAG_INHERIT
))
358 ObDereferenceObject(TargetProcess
);
359 ObDereferenceObject(SourceProcess
);
360 ObDereferenceObject(ObjectBody
);
361 return STATUS_INVALID_HANDLE
;
363 ObCreateHandle(TargetProcess
,
370 if (Options
& DUPLICATE_CLOSE_SOURCE
)
372 ZwClose(SourceHandle
);
375 ObDereferenceObject(TargetProcess
);
376 ObDereferenceObject(SourceProcess
);
377 ObDereferenceObject(ObjectBody
);
379 Status
= MmCopyToCaller(UnsafeTargetHandle
, &TargetHandle
, sizeof(HANDLE
));
380 if (!NT_SUCCESS(Status
))
385 return(STATUS_SUCCESS
);
388 VOID
ObCloseAllHandles(PEPROCESS Process
)
391 PHANDLE_TABLE HandleTable
;
392 PLIST_ENTRY current_entry
;
393 PHANDLE_BLOCK current
;
397 DPRINT("ObCloseAllHandles(Process %x)\n", Process
);
399 HandleTable
= &Process
->HandleTable
;
401 KeAcquireSpinLock(&HandleTable
->ListLock
, &oldIrql
);
403 current_entry
= HandleTable
->ListHead
.Flink
;
405 while (current_entry
!= &HandleTable
->ListHead
)
407 current
= CONTAINING_RECORD(current_entry
, HANDLE_BLOCK
, entry
);
409 for (i
= 0; i
< HANDLE_BLOCK_ENTRIES
; i
++)
411 ObjectBody
= OB_ENTRY_TO_POINTER(current
->handles
[i
].ObjectBody
);
413 if (ObjectBody
!= NULL
)
415 POBJECT_HEADER Header
= BODY_TO_HEADER(ObjectBody
);
418 if (Header
->ObjectType
== PsProcessType
||
419 Header
->ObjectType
== PsThreadType
)
421 DPRINT("Deleting handle to %x\n", ObjectBody
);
425 ObReferenceObjectByPointer(ObjectBody
,
429 InterlockedDecrement(&Header
->HandleCount
);
430 current
->handles
[i
].ObjectBody
= NULL
;
432 KeReleaseSpinLock(&HandleTable
->ListLock
, oldIrql
);
433 if ((Header
->ObjectType
!= NULL
) &&
434 (Header
->ObjectType
->Close
!= NULL
))
436 Header
->ObjectType
->Close(ObjectBody
,
437 Header
->HandleCount
);
440 ObDereferenceObject(ObjectBody
);
441 KeAcquireSpinLock(&HandleTable
->ListLock
, &oldIrql
);
442 current_entry
= &HandleTable
->ListHead
;
447 current_entry
= current_entry
->Flink
;
449 KeReleaseSpinLock(&HandleTable
->ListLock
, oldIrql
);
450 DPRINT("ObCloseAllHandles() finished\n");
451 DPRINT("Type %x\n", BODY_TO_HEADER(Process
)->ObjectType
);
454 VOID
ObDeleteHandleTable(PEPROCESS Process
)
456 * FUNCTION: Deletes the handle table associated with a process
459 PLIST_ENTRY current
= NULL
;
460 PHANDLE_TABLE HandleTable
= NULL
;
462 ObCloseAllHandles(Process
);
464 HandleTable
= &Process
->HandleTable
;
465 current
= RemoveHeadList(&HandleTable
->ListHead
);
467 while (current
!= &HandleTable
->ListHead
)
469 HANDLE_BLOCK
* HandleBlock
= CONTAINING_RECORD(current
,
472 DPRINT("Freeing %x\n", HandleBlock
);
473 ExFreePool(HandleBlock
);
475 current
= RemoveHeadList(&HandleTable
->ListHead
);
480 VOID
ObCreateHandleTable(PEPROCESS Parent
,
484 * FUNCTION: Creates a handle table for a process
486 * Parent = Parent process (or NULL if this is the first process)
487 * Inherit = True if the process should inherit its parent's handles
488 * Process = Process whose handle table is to be created
491 PHANDLE_TABLE ParentHandleTable
, HandleTable
;
493 PLIST_ENTRY parent_current
;
495 PHANDLE_BLOCK current_block
, new_block
;
497 DPRINT("ObCreateHandleTable(Parent %x, Inherit %d, Process %x)\n",
498 Parent
,Inherit
,Process
);
500 InitializeListHead(&(Process
->HandleTable
.ListHead
));
501 KeInitializeSpinLock(&(Process
->HandleTable
.ListLock
));
505 ParentHandleTable
= &Parent
->HandleTable
;
506 HandleTable
= &Process
->HandleTable
;
508 KeAcquireSpinLock(&Parent
->HandleTable
.ListLock
, &oldIrql
);
509 KeAcquireSpinLockAtDpcLevel(&Process
->HandleTable
.ListLock
);
511 parent_current
= ParentHandleTable
->ListHead
.Flink
;
513 while (parent_current
!= &ParentHandleTable
->ListHead
)
515 current_block
= CONTAINING_RECORD(parent_current
,
518 new_block
= ExAllocatePoolWithTag(NonPagedPool
,
519 sizeof(HANDLE_BLOCK
),
521 if (new_block
== NULL
)
525 RtlZeroMemory(new_block
, sizeof(HANDLE_BLOCK
));
527 for (i
=0; i
<HANDLE_BLOCK_ENTRIES
; i
++)
529 if (current_block
->handles
[i
].ObjectBody
)
531 if ((ULONG_PTR
)current_block
->handles
[i
].ObjectBody
& OB_HANDLE_FLAG_INHERIT
)
533 new_block
->handles
[i
].ObjectBody
=
534 current_block
->handles
[i
].ObjectBody
;
535 new_block
->handles
[i
].GrantedAccess
=
536 current_block
->handles
[i
].GrantedAccess
;
537 InterlockedIncrement(&(BODY_TO_HEADER(OB_ENTRY_TO_POINTER(current_block
->handles
[i
].ObjectBody
))->HandleCount
));
541 InsertTailList(&Process
->HandleTable
.ListHead
, &new_block
->entry
);
542 parent_current
= parent_current
->Flink
;
544 KeReleaseSpinLockFromDpcLevel(&Process
->HandleTable
.ListLock
);
545 KeReleaseSpinLock(&Parent
->HandleTable
.ListLock
, oldIrql
);
551 ObDeleteHandle(PEPROCESS Process
,
555 PHANDLE_ENTRY HandleEntry
;
558 PHANDLE_TABLE HandleTable
;
559 POBJECT_HEADER Header
;
562 DPRINT("ObDeleteHandle(Handle %x)\n",Handle
);
564 HandleTable
= &Process
->HandleTable
;
566 KeAcquireSpinLock(&HandleTable
->ListLock
, &oldIrql
);
568 HandleEntry
= ObpGetObjectByHandle(HandleTable
, Handle
, &Block
);
569 if (HandleEntry
== NULL
)
571 KeReleaseSpinLock(&HandleTable
->ListLock
, oldIrql
);
573 return STATUS_INVALID_HANDLE
;
576 if ((ULONG_PTR
)HandleEntry
->ObjectBody
& OB_HANDLE_FLAG_PROTECT
)
578 KeReleaseSpinLock(&HandleTable
->ListLock
, oldIrql
);
580 return STATUS_HANDLE_NOT_CLOSABLE
;
583 Body
= OB_ENTRY_TO_POINTER(HandleEntry
->ObjectBody
);
584 DPRINT("ObjectBody %x\n", Body
);
587 KeReleaseSpinLock(&HandleTable
->ListLock
, oldIrql
);
589 return STATUS_UNSUCCESSFUL
;
592 Header
= BODY_TO_HEADER(Body
);
593 ObReferenceObjectByPointer(Body
,
597 InterlockedDecrement(&Header
->HandleCount
);
598 HandleEntry
->ObjectBody
= NULL
;
600 Block
->allocation_count
--;
601 Block
->allocation_hint
= (ULONG_PTR
)Handle
% HANDLE_BLOCK_ENTRIES
;
603 KeReleaseSpinLock(&HandleTable
->ListLock
, oldIrql
);
605 if ((Header
->ObjectType
!= NULL
) &&
606 (Header
->ObjectType
->Close
!= NULL
))
608 Header
->ObjectType
->Close(Body
, Header
->HandleCount
);
613 DPRINT("Finished ObDeleteHandle()\n");
615 return STATUS_SUCCESS
;
620 ObCreateHandle(PEPROCESS Process
,
622 ACCESS_MASK GrantedAccess
,
624 PHANDLE HandleReturn
)
626 * FUNCTION: Add a handle referencing an object
628 * obj = Object body that the handle should refer to
629 * RETURNS: The created handle
630 * NOTE: The handle is valid only in the context of the current process
634 unsigned int handle
=1;
635 unsigned int Loop
, Index
, MaxIndex
;
636 HANDLE_BLOCK
* new_blk
= NULL
;
637 PHANDLE_TABLE HandleTable
;
640 DPRINT("ObCreateHandle(Process %x, obj %x)\n",Process
,ObjectBody
);
644 if (ObjectBody
!= NULL
)
646 InterlockedIncrement(&(BODY_TO_HEADER(ObjectBody
)->HandleCount
));
648 HandleTable
= &Process
->HandleTable
;
649 KeAcquireSpinLock(&HandleTable
->ListLock
, &oldlvl
);
650 current
= HandleTable
->ListHead
.Flink
;
652 * Scan through the currently allocated handle blocks looking for a free
655 while (current
!= (&HandleTable
->ListHead
))
657 HANDLE_BLOCK
* blk
= CONTAINING_RECORD(current
,HANDLE_BLOCK
,entry
);
659 DPRINT("Current %x\n",current
);
661 if (blk
->allocation_count
== HANDLE_BLOCK_ENTRIES
)
663 handle
= handle
+ HANDLE_BLOCK_ENTRIES
;
664 current
= current
->Flink
;
668 Index
= blk
->allocation_hint
;
669 MaxIndex
= HANDLE_BLOCK_ENTRIES
;
670 for (Loop
= 0; Loop
< 2; Loop
++)
672 for (Index
= 0; Index
< MaxIndex
; Index
++)
674 DPRINT("Considering slot %d containing %x\n", Index
, blk
->handles
[Index
]);
675 if (blk
->handles
[Index
].ObjectBody
== NULL
)
677 blk
->handles
[Index
].ObjectBody
= OB_POINTER_TO_ENTRY(ObjectBody
);
679 blk
->handles
[Index
].ObjectBody
= (PVOID
)((ULONG_PTR
)blk
->handles
[Index
].ObjectBody
| OB_HANDLE_FLAG_INHERIT
);
680 blk
->handles
[Index
].GrantedAccess
= GrantedAccess
;
681 blk
->allocation_hint
= Index
+ 1;
682 blk
->allocation_count
++;
683 KeReleaseSpinLock(&HandleTable
->ListLock
, oldlvl
);
684 *HandleReturn
= (HANDLE
)((handle
+ Index
) << 2);
685 return(STATUS_SUCCESS
);
689 MaxIndex
= blk
->allocation_hint
;
692 handle
= handle
+ HANDLE_BLOCK_ENTRIES
;
693 current
= current
->Flink
;
697 * Add a new handle block to the end of the list
700 (HANDLE_BLOCK
*)ExAllocatePoolWithTag(NonPagedPool
,sizeof(HANDLE_BLOCK
),
704 *HandleReturn
= (PHANDLE
)NULL
;
705 return(STATUS_INSUFFICIENT_RESOURCES
);
707 RtlZeroMemory(new_blk
,sizeof(HANDLE_BLOCK
));
708 InsertTailList(&(Process
->HandleTable
.ListHead
),
710 new_blk
->handles
[0].ObjectBody
= OB_POINTER_TO_ENTRY(ObjectBody
);
712 new_blk
->handles
[0].ObjectBody
= (PVOID
)((ULONG_PTR
)new_blk
->handles
[0].ObjectBody
| OB_HANDLE_FLAG_INHERIT
);
713 new_blk
->handles
[0].GrantedAccess
= GrantedAccess
;
714 new_blk
->allocation_hint
= 1;
715 new_blk
->allocation_count
++;
716 KeReleaseSpinLock(&HandleTable
->ListLock
, oldlvl
);
717 *HandleReturn
= (HANDLE
)(handle
<< 2);
718 return(STATUS_SUCCESS
);
726 ObQueryObjectAuditingByHandle(IN HANDLE Handle
,
727 OUT PBOOLEAN GenerateOnClose
)
731 PHANDLE_ENTRY HandleEntry
;
733 DPRINT("ObQueryObjectAuditingByHandle(Handle %x)\n", Handle
);
735 Process
= PsGetCurrentProcess();
737 KeAcquireSpinLock(&Process
->HandleTable
.ListLock
, &oldIrql
);
738 HandleEntry
= ObpGetObjectByHandle(&Process
->HandleTable
,
741 if (HandleEntry
== NULL
)
743 KeReleaseSpinLock(&Process
->HandleTable
.ListLock
, oldIrql
);
744 return STATUS_INVALID_HANDLE
;
747 *GenerateOnClose
= (BOOLEAN
)((ULONG_PTR
)HandleEntry
->ObjectBody
| OB_HANDLE_FLAG_AUDIT
);
749 KeReleaseSpinLock(&Process
->HandleTable
.ListLock
, oldIrql
);
751 return STATUS_SUCCESS
;
756 * FUNCTION: Increments the reference count for an object and returns a
757 * pointer to its body
759 * Handle = Handle for the object
760 * DesiredAccess = Desired access to the object
763 * Object (OUT) = Points to the object body on return
764 * HandleInformation (OUT) = Contains information about the handle
771 ObReferenceObjectByHandle(HANDLE Handle
,
772 ACCESS_MASK DesiredAccess
,
773 POBJECT_TYPE ObjectType
,
774 KPROCESSOR_MODE AccessMode
,
776 POBJECT_HANDLE_INFORMATION HandleInformation
)
778 PHANDLE_ENTRY HandleEntry
;
779 POBJECT_HEADER ObjectHeader
;
782 ACCESS_MASK GrantedAccess
;
786 ASSERT_IRQL(PASSIVE_LEVEL
);
788 DPRINT("ObReferenceObjectByHandle(Handle %x, DesiredAccess %x, "
789 "ObjectType %x, AccessMode %d, Object %x)\n",Handle
,DesiredAccess
,
790 ObjectType
,AccessMode
,Object
);
793 * Handle special handle names
795 if (Handle
== NtCurrentProcess() &&
796 (ObjectType
== PsProcessType
|| ObjectType
== NULL
))
798 DPRINT("Reference from %x\n", ((PULONG
)&Handle
)[-1]);
800 Status
= ObReferenceObjectByPointer(PsGetCurrentProcess(),
804 if (! NT_SUCCESS(Status
))
809 if (HandleInformation
!= NULL
)
811 HandleInformation
->HandleAttributes
= 0; /* FIXME? */
812 HandleInformation
->GrantedAccess
= PROCESS_ALL_ACCESS
;
815 *Object
= PsGetCurrentProcess();
816 DPRINT("Referencing current process %x\n", PsGetCurrentProcess());
817 return STATUS_SUCCESS
;
819 else if (Handle
== NtCurrentProcess())
822 return(STATUS_OBJECT_TYPE_MISMATCH
);
825 if (Handle
== NtCurrentThread() &&
826 (ObjectType
== PsThreadType
|| ObjectType
== NULL
))
828 Status
= ObReferenceObjectByPointer(PsGetCurrentThread(),
832 if (! NT_SUCCESS(Status
))
837 if (HandleInformation
!= NULL
)
839 HandleInformation
->HandleAttributes
= 0; /* FIXME? */
840 HandleInformation
->GrantedAccess
= THREAD_ALL_ACCESS
;
843 *Object
= PsGetCurrentThread();
845 return STATUS_SUCCESS
;
847 else if (Handle
== NtCurrentThread())
850 return(STATUS_OBJECT_TYPE_MISMATCH
);
853 KeAcquireSpinLock(&PsGetCurrentProcess()->HandleTable
.ListLock
,
855 HandleEntry
= ObpGetObjectByHandle(&PsGetCurrentProcess()->HandleTable
,
858 if (HandleEntry
== NULL
|| HandleEntry
->ObjectBody
== 0)
860 KeReleaseSpinLock(&PsGetCurrentProcess()->HandleTable
.ListLock
,
862 return(STATUS_INVALID_HANDLE
);
864 ObjectBody
= OB_ENTRY_TO_POINTER(HandleEntry
->ObjectBody
);
865 DPRINT("ObjectBody %p\n",ObjectBody
);
866 ObjectHeader
= BODY_TO_HEADER(ObjectBody
);
867 DPRINT("ObjectHeader->RefCount %lu\n",ObjectHeader
->RefCount
);
868 ObReferenceObjectByPointer(ObjectBody
,
872 Attributes
= (ULONG_PTR
)HandleEntry
->ObjectBody
& OB_HANDLE_FLAG_MASK
;
873 GrantedAccess
= HandleEntry
->GrantedAccess
;
874 KeReleaseSpinLock(&PsGetCurrentProcess()->HandleTable
.ListLock
,
877 ObjectHeader
= BODY_TO_HEADER(ObjectBody
);
878 DPRINT("ObjectHeader->RefCount %lu\n",ObjectHeader
->RefCount
);
880 if (ObjectType
!= NULL
&& ObjectType
!= ObjectHeader
->ObjectType
)
883 return(STATUS_OBJECT_TYPE_MISMATCH
);
886 if (ObjectHeader
->ObjectType
== PsProcessType
)
888 DPRINT("Reference from %x\n", ((PULONG
)&Handle
)[-1]);
891 if (DesiredAccess
&& AccessMode
== UserMode
)
893 RtlMapGenericMask(&DesiredAccess
, ObjectHeader
->ObjectType
->Mapping
);
895 if (!(GrantedAccess
& DesiredAccess
) &&
896 !((~GrantedAccess
) & DesiredAccess
))
899 return(STATUS_ACCESS_DENIED
);
903 if (HandleInformation
!= NULL
)
905 HandleInformation
->HandleAttributes
= Attributes
;
906 HandleInformation
->GrantedAccess
= GrantedAccess
;
909 *Object
= ObjectBody
;
912 return(STATUS_SUCCESS
);
916 /**********************************************************************
921 * Closes a handle reference to an object.
933 NtClose(IN HANDLE Handle
)
936 POBJECT_HEADER Header
;
939 ASSERT_IRQL(PASSIVE_LEVEL
);
941 DPRINT("NtClose(Handle %x)\n",Handle
);
943 Status
= ObDeleteHandle(PsGetCurrentProcess(),
946 if (!NT_SUCCESS(Status
))
948 if(((PEPROCESS
)(KeGetCurrentThread()->ApcState
.Process
))->ExceptionPort
)
949 KeRaiseUserException(Status
);
953 Header
= BODY_TO_HEADER(ObjectBody
);
955 DPRINT("Dereferencing %x\n", ObjectBody
);
956 ObDereferenceObject(ObjectBody
);
958 return(STATUS_SUCCESS
);
966 ObInsertObject(IN PVOID Object
,
967 IN PACCESS_STATE PassedAccessState OPTIONAL
,
968 IN ACCESS_MASK DesiredAccess
,
969 IN ULONG AdditionalReferences
,
970 OUT PVOID
* ReferencedObject OPTIONAL
,
973 POBJECT_HEADER ObjectHeader
;
976 Access
= DesiredAccess
;
977 ObjectHeader
= BODY_TO_HEADER(Object
);
979 RtlMapGenericMask(&Access
,
980 ObjectHeader
->ObjectType
->Mapping
);
982 return(ObCreateHandle(PsGetCurrentProcess(),
985 ObjectHeader
->Inherit
,
991 ObpGetHandleCountByHandleTable(PHANDLE_TABLE HandleTable
)
994 POBJECT_HEADER Header
;
1001 KeAcquireSpinLock(&HandleTable
->ListLock
, &OldIrql
);
1003 current
= HandleTable
->ListHead
.Flink
;
1004 while (current
!= &HandleTable
->ListHead
)
1006 blk
= CONTAINING_RECORD(current
, HANDLE_BLOCK
, entry
);
1008 for (i
= 0; i
< HANDLE_BLOCK_ENTRIES
; i
++)
1010 ObjectBody
= OB_ENTRY_TO_POINTER(blk
->handles
[i
].ObjectBody
);
1011 if (ObjectBody
!= NULL
)
1013 Header
= BODY_TO_HEADER(ObjectBody
);
1015 /* Make sure this is real. */
1016 if (Header
->ObjectType
!= NULL
)
1021 current
= current
->Flink
;
1024 KeReleaseSpinLock(&HandleTable
->ListLock
,
1031 * FUNCTION: Searches the handle table of a specified process whether it contains a
1032 * valid handle to the Object we're looking for. If not, it'll create one.
1035 * The parameters of this function is basically a mixture of some of the parameters
1036 * of ObReferenceObjectByHandle() and ObReferenceObjectByPointer(). A little thinking
1037 * about what this function does (by it's name) makes clear what parameters it requires.
1038 * For example the AccessMode parameter of ObReferenceObjectByHandle/Pointer() is not
1039 * required at all as it only has influence on the object security. This function doesn't
1040 * want to get access to an object, it just looks for a valid handle and if it can't find
1041 * one, it'll just create one. It wouldn't make sense to check for security again as the
1042 * caller already has a pointer to the object.
1044 * A test on an XP machine shows that this prototype appears to be correct.
1047 * Process = This parameter simply describes in which handle table we're looking
1048 * for a handle to the object.
1049 * Object = The object pointer that we're looking for
1050 * ObjectType = Just a sanity check as ObReferenceObjectByHandle() and
1051 * ObReferenceObjectByPointer() provides.
1052 * HandleInformation = This one has to be the opposite meaning of the usage in
1053 * ObReferenceObjectByHandle(). If we actually found a valid
1054 * handle in the table, we need to check against the information
1055 * provided so we make sure this handle has all access rights
1056 * (and attributes?!) we need. If they don't match, we can't
1057 * use this handle and keep looking because the caller is likely
1058 * to depend on these access rights.
1059 * HandleReturn = The last parameter is the same as in ObCreateHandle(). If we could
1060 * find a suitable handle in the handle table, return this handle, if
1061 * not, we'll just create one using ObCreateHandle() with all access
1062 * rights the caller needs.
1069 ObFindHandleForObject(IN PEPROCESS Process
,
1071 IN POBJECT_TYPE ObjectType
,
1072 IN POBJECT_HANDLE_INFORMATION HandleInformation
,
1073 OUT PHANDLE HandleReturn
)
1076 return STATUS_UNSUCCESSFUL
;