3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ps/kill.c
6 * PURPOSE: Terminating a thread
7 * PROGRAMMER: David Welch (welch@cwcom.net)
12 /* INCLUDES *****************************************************************/
16 #include <internal/debug.h>
19 /* TYPES ********************************************************************/
21 typedef struct _SD_CACHE_ENTRY
27 } SD_CACHE_ENTRY
, *PSD_CACHE_ENTRY
;
30 /* GLOBALS ******************************************************************/
32 PLIST_ENTRY ObpSdCache
;
33 KSPIN_LOCK ObpSdCacheSpinLock
;
37 #define SD_CACHE_ENTRIES 0x100
39 /* FUNCTIONS ****************************************************************/
46 ObpSdCache
= ExAllocatePool(NonPagedPool
,
47 SD_CACHE_ENTRIES
* sizeof(LIST_ENTRY
));
48 if (ObpSdCache
== NULL
)
50 return STATUS_INSUFFICIENT_RESOURCES
;
53 for (i
= 0; i
< SD_CACHE_ENTRIES
; i
++)
55 InitializeListHead(&ObpSdCache
[i
]);
58 KeInitializeSpinLock(&ObpSdCacheSpinLock
);
60 return STATUS_SUCCESS
;
67 KeAcquireSpinLock(&ObpSdCacheSpinLock
,
73 ObpSdCacheUnlock(VOID
)
75 KeReleaseSpinLock(&ObpSdCacheSpinLock
,
90 for (i
= 0; i
< Length
; i
++)
101 ObpHashSecurityDescriptor(IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
112 RtlGetOwnerSecurityDescriptor(SecurityDescriptor
,
116 RtlGetGroupSecurityDescriptor(SecurityDescriptor
,
120 RtlGetDaclSecurityDescriptor(SecurityDescriptor
,
125 RtlGetSaclSecurityDescriptor(SecurityDescriptor
,
133 Value
+= ObpHash(Owner
, RtlLengthSid(Owner
));
138 Value
+= ObpHash(Group
, RtlLengthSid(Group
));
141 if (DaclPresent
== TRUE
&& Dacl
!= NULL
)
143 Value
+= ObpHash(Dacl
, Dacl
->AclSize
);
146 if (SaclPresent
== TRUE
&& Sacl
!= NULL
)
148 Value
+= ObpHash(Sacl
, Sacl
->AclSize
);
155 static PSD_CACHE_ENTRY
156 ObpCreateCacheEntry(IN PSECURITY_DESCRIPTOR SecurityDescriptor
,
159 OUT PSECURITY_DESCRIPTOR
*NewSD
)
161 PSECURITY_DESCRIPTOR Sd
;
162 PSD_CACHE_ENTRY CacheEntry
;
165 DPRINT("ObpCreateCacheEntry() called\n");
167 Length
= RtlLengthSecurityDescriptor(SecurityDescriptor
);
169 CacheEntry
= ExAllocatePool(NonPagedPool
,
170 sizeof(SD_CACHE_ENTRY
) + Length
);
171 if (CacheEntry
== NULL
)
173 DPRINT1("ExAllocatePool() failed\n");
177 CacheEntry
->HashValue
= HashValue
;
178 CacheEntry
->Index
= Index
;
179 CacheEntry
->RefCount
= 1;
181 Sd
= (PSECURITY_DESCRIPTOR
)(CacheEntry
+ 1);
188 DPRINT("ObpCreateCacheEntry() done\n");
195 ObpCompareSecurityDescriptors(IN PSECURITY_DESCRIPTOR Sd1
,
196 IN PSECURITY_DESCRIPTOR Sd2
)
201 Length1
= RtlLengthSecurityDescriptor(Sd1
);
202 Length2
= RtlLengthSecurityDescriptor(Sd2
);
203 if (Length1
!= Length2
)
206 if (RtlCompareMemory(Sd1
, Sd2
, Length1
) != Length1
)
214 ObpAddSecurityDescriptor(IN PSECURITY_DESCRIPTOR SourceSD
,
215 OUT PSECURITY_DESCRIPTOR
*DestinationSD
)
217 PSECURITY_DESCRIPTOR Sd
;
218 PLIST_ENTRY CurrentEntry
;
219 PSD_CACHE_ENTRY CacheEntry
;
224 DPRINT("ObpAddSecurityDescriptor() called\n");
226 HashValue
= ObpHashSecurityDescriptor(SourceSD
);
227 Index
= HashValue
& 0xFF;
231 if (!IsListEmpty(&ObpSdCache
[Index
]))
233 CurrentEntry
= ObpSdCache
[Index
].Flink
;
234 while (CurrentEntry
!= &ObpSdCache
[Index
])
236 CacheEntry
= CONTAINING_RECORD(CurrentEntry
,
239 Sd
= (PSECURITY_DESCRIPTOR
)(CacheEntry
+ 1);
241 if (CacheEntry
->HashValue
== HashValue
&&
242 ObpCompareSecurityDescriptors(SourceSD
, Sd
))
244 CacheEntry
->RefCount
++;
245 DPRINT("RefCount %lu\n", CacheEntry
->RefCount
);
250 DPRINT("ObpAddSecurityDescriptor() done\n");
252 return STATUS_SUCCESS
;
255 CurrentEntry
= CurrentEntry
->Flink
;
259 CacheEntry
= ObpCreateCacheEntry(SourceSD
,
263 if (CacheEntry
== NULL
)
265 DPRINT1("ObpCreateCacheEntry() failed\n");
266 Status
= STATUS_INSUFFICIENT_RESOURCES
;
270 DPRINT("RefCount 1\n");
271 InsertTailList(&ObpSdCache
[Index
], &CacheEntry
->ListEntry
);
272 Status
= STATUS_SUCCESS
;
277 DPRINT("ObpAddSecurityDescriptor() done\n");
284 ObpRemoveSecurityDescriptor(IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
286 PSD_CACHE_ENTRY CacheEntry
;
288 DPRINT("ObpRemoveSecurityDescriptor() called\n");
292 CacheEntry
= (PSD_CACHE_ENTRY
)((ULONG_PTR
)SecurityDescriptor
- sizeof(SD_CACHE_ENTRY
));
294 CacheEntry
->RefCount
--;
295 DPRINT("RefCount %lu\n", CacheEntry
->RefCount
);
296 if (CacheEntry
->RefCount
== 0)
298 DPRINT("Remove cache entry\n");
299 RemoveEntryList(&CacheEntry
->ListEntry
);
300 ExFreePool(CacheEntry
);
305 DPRINT("ObpRemoveSecurityDescriptor() done\n");
307 return STATUS_SUCCESS
;
312 ObpReferenceCachedSecurityDescriptor(IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
314 PSD_CACHE_ENTRY CacheEntry
;
316 DPRINT("ObpReferenceCachedSecurityDescriptor() called\n");
320 CacheEntry
= (PSD_CACHE_ENTRY
)((ULONG_PTR
)SecurityDescriptor
- sizeof(SD_CACHE_ENTRY
));
322 CacheEntry
->RefCount
++;
323 DPRINT("RefCount %lu\n", CacheEntry
->RefCount
);
327 DPRINT("ObpReferenceCachedSecurityDescriptor() done\n");
332 ObpDereferenceCachedSecurityDescriptor(IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
334 DPRINT("ObpDereferenceCachedSecurityDescriptor() called\n");
336 ObpRemoveSecurityDescriptor(SecurityDescriptor
);
338 DPRINT("ObpDereferenceCachedSecurityDescriptor() done\n");