f311cd30c873b7d4cba53aef70d7a6f6a35aaf74
[reactos.git] / reactos / ntoskrnl / ps / cid.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ps/cid.c
6 * PURPOSE: Client ID (CID) management
7 *
8 * PROGRAMMERS: Thomas Weidenmueller <w3seek@reactos.com>
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* GLOBALS ******************************************************************/
18
19 PHANDLE_TABLE PspCidTable = NULL;
20
21 #define TAG_CIDOBJECT TAG('C', 'I', 'D', 'O')
22
23 #define CID_FLAG_PROCESS 0x1
24 #define CID_FLAG_THREAD 0x2
25 #define CID_FLAGS_MASK (CID_FLAG_PROCESS | CID_FLAG_THREAD)
26
27 /* FUNCTIONS *****************************************************************/
28
29 VOID INIT_FUNCTION
30 PsInitClientIDManagment(VOID)
31 {
32 PspCidTable = ExCreateHandleTable(NULL);
33 ASSERT(PspCidTable);
34 }
35
36 NTSTATUS
37 PsCreateCidHandle(PVOID Object, POBJECT_TYPE ObjectType, PHANDLE Handle)
38 {
39 HANDLE_TABLE_ENTRY NewEntry;
40 LONG ExHandle;
41
42 PAGED_CODE();
43
44 NewEntry.u1.Object = Object;
45 if(ObjectType == PsThreadType)
46 NewEntry.u2.GrantedAccess = CID_FLAG_THREAD;
47 else if(ObjectType == PsProcessType)
48 NewEntry.u2.GrantedAccess = CID_FLAG_PROCESS;
49 else
50 {
51 DPRINT1("Can't create CID handles for %wZ objects\n", &ObjectType->TypeName);
52 KEBUGCHECK(0);
53 }
54
55 ExHandle = ExCreateHandle(PspCidTable,
56 &NewEntry);
57 if(ExHandle != EX_INVALID_HANDLE)
58 {
59 *Handle = EX_HANDLE_TO_HANDLE(ExHandle);
60 return STATUS_SUCCESS;
61 }
62
63 return STATUS_UNSUCCESSFUL;
64 }
65
66 NTSTATUS
67 PsDeleteCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType)
68 {
69 PHANDLE_TABLE_ENTRY Entry;
70 LONG ExHandle = HANDLE_TO_EX_HANDLE(CidHandle);
71
72 PAGED_CODE();
73
74 Entry = ExMapHandleToPointer(PspCidTable,
75 ExHandle);
76 if(Entry != NULL)
77 {
78 if((ObjectType == PsThreadType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_THREAD)) ||
79 (ObjectType == PsProcessType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_PROCESS)))
80 {
81 ExDestroyHandleByEntry(PspCidTable,
82 Entry,
83 ExHandle);
84 return STATUS_SUCCESS;
85 }
86 else
87 {
88 ExUnlockHandleTableEntry(PspCidTable,
89 Entry);
90 return STATUS_OBJECT_TYPE_MISMATCH;
91 }
92 }
93
94 return STATUS_INVALID_HANDLE;
95 }
96
97 PHANDLE_TABLE_ENTRY
98 PsLookupCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType, PVOID *Object)
99 {
100 PHANDLE_TABLE_ENTRY Entry;
101
102 PAGED_CODE();
103
104 KeEnterCriticalRegion();
105
106 Entry = ExMapHandleToPointer(PspCidTable,
107 HANDLE_TO_EX_HANDLE(CidHandle));
108 if(Entry != NULL)
109 {
110 if((ObjectType == PsProcessType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_PROCESS)) ||
111 (ObjectType == PsThreadType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_THREAD)))
112 {
113 *Object = Entry->u1.Object;
114 return Entry;
115 }
116 else
117 {
118 DPRINT1("CID Obj type mismatch handle 0x%x %wZ vs 0x%x\n", CidHandle,
119 &ObjectType->TypeName, Entry->u2.GrantedAccess);
120 ExUnlockHandleTableEntry(PspCidTable,
121 Entry);
122 KEBUGCHECK(0);
123 }
124 }
125
126 KeLeaveCriticalRegion();
127
128 return NULL;
129 }
130
131 VOID
132 PsUnlockCidHandle(PHANDLE_TABLE_ENTRY CidEntry)
133 {
134 PAGED_CODE();
135
136 ExUnlockHandleTableEntry(PspCidTable,
137 CidEntry);
138 KeLeaveCriticalRegion();
139 }
140
141 /* EOF */