83eea3ba00bcbb7a294da0198a0cdb8899c3f661
[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 CID_FLAG_PROCESS 0x1
22 #define CID_FLAG_THREAD 0x2
23 #define CID_FLAGS_MASK (CID_FLAG_PROCESS | CID_FLAG_THREAD)
24
25 /* FUNCTIONS *****************************************************************/
26
27 VOID INIT_FUNCTION
28 PsInitClientIDManagment(VOID)
29 {
30 PspCidTable = ExCreateHandleTable(NULL);
31 ASSERT(PspCidTable);
32 }
33
34 NTSTATUS
35 PsCreateCidHandle(PVOID Object, POBJECT_TYPE ObjectType, PHANDLE Handle)
36 {
37 HANDLE_TABLE_ENTRY NewEntry;
38 LONG ExHandle;
39
40 PAGED_CODE();
41
42 NewEntry.u1.Object = Object;
43 if(ObjectType == PsThreadType)
44 NewEntry.u2.GrantedAccess = CID_FLAG_THREAD;
45 else if(ObjectType == PsProcessType)
46 NewEntry.u2.GrantedAccess = CID_FLAG_PROCESS;
47 else
48 {
49 DPRINT1("Can't create CID handles for %wZ objects\n", &ObjectType->Name);
50 KEBUGCHECK(0);
51 }
52
53 ExHandle = ExCreateHandle(PspCidTable,
54 &NewEntry);
55 if(ExHandle != EX_INVALID_HANDLE)
56 {
57 *Handle = EX_HANDLE_TO_HANDLE(ExHandle);
58 return STATUS_SUCCESS;
59 }
60
61 return STATUS_UNSUCCESSFUL;
62 }
63
64 NTSTATUS
65 PsDeleteCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType)
66 {
67 PHANDLE_TABLE_ENTRY Entry;
68 LONG ExHandle = HANDLE_TO_EX_HANDLE(CidHandle);
69
70 PAGED_CODE();
71
72 KeEnterCriticalRegion();
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 KeLeaveCriticalRegion();
85 return STATUS_SUCCESS;
86 }
87 else
88 {
89 ExUnlockHandleTableEntry(PspCidTable,
90 Entry);
91 KeLeaveCriticalRegion();
92 return STATUS_OBJECT_TYPE_MISMATCH;
93 }
94 }
95 KeLeaveCriticalRegion();
96 return STATUS_INVALID_HANDLE;
97 }
98
99 PHANDLE_TABLE_ENTRY
100 PsLookupCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType, PVOID *Object)
101 {
102 PHANDLE_TABLE_ENTRY Entry;
103
104 PAGED_CODE();
105
106 KeEnterCriticalRegion();
107
108 Entry = ExMapHandleToPointer(PspCidTable,
109 HANDLE_TO_EX_HANDLE(CidHandle));
110 if(Entry != NULL)
111 {
112 if((ObjectType == PsProcessType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_PROCESS)) ||
113 (ObjectType == PsThreadType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_THREAD)))
114 {
115 *Object = Entry->u1.Object;
116 return Entry;
117 }
118 else
119 {
120 DPRINT1("CID Obj type mismatch handle 0x%x %wZ vs 0x%x\n", CidHandle,
121 &ObjectType->Name, Entry->u2.GrantedAccess);
122 ExUnlockHandleTableEntry(PspCidTable,
123 Entry);
124 }
125 }
126
127 KeLeaveCriticalRegion();
128
129 return NULL;
130 }
131
132 /*
133 * @implemented
134 */
135 NTSTATUS STDCALL
136 PsLookupProcessThreadByCid(IN PCLIENT_ID Cid,
137 OUT PEPROCESS *Process OPTIONAL,
138 OUT PETHREAD *Thread)
139 {
140 PHANDLE_TABLE_ENTRY CidEntry;
141 PETHREAD FoundThread;
142
143 PAGED_CODE();
144
145 ASSERT(Thread);
146 ASSERT(Cid);
147
148 CidEntry = PsLookupCidHandle(Cid->UniqueThread, PsThreadType, (PVOID*)&FoundThread);
149 if(CidEntry != NULL)
150 {
151 ObReferenceObject(FoundThread);
152
153 PsUnlockCidHandle(CidEntry);
154
155 if(Process != NULL)
156 {
157 *Process = FoundThread->ThreadsProcess;
158 }
159 *Thread = FoundThread;
160 return STATUS_SUCCESS;
161 }
162
163 return STATUS_INVALID_PARAMETER;
164 }
165
166
167 /*
168 * @implemented
169 */
170 NTSTATUS STDCALL
171 PsLookupThreadByThreadId(IN HANDLE ThreadId,
172 OUT PETHREAD *Thread)
173 {
174 PHANDLE_TABLE_ENTRY CidEntry;
175 PETHREAD FoundThread;
176
177 PAGED_CODE();
178
179 ASSERT(Thread);
180
181 CidEntry = PsLookupCidHandle(ThreadId, PsThreadType, (PVOID*)&FoundThread);
182 if(CidEntry != NULL)
183 {
184 ObReferenceObject(FoundThread);
185
186 PsUnlockCidHandle(CidEntry);
187
188 *Thread = FoundThread;
189 return STATUS_SUCCESS;
190 }
191
192 return STATUS_INVALID_PARAMETER;
193 }
194
195 VOID
196 PsUnlockCidHandle(PHANDLE_TABLE_ENTRY CidEntry)
197 {
198 PAGED_CODE();
199
200 ExUnlockHandleTableEntry(PspCidTable,
201 CidEntry);
202 KeLeaveCriticalRegion();
203 }
204
205 /* EOF */