948264677f1a630f1fd2a003484382d24255ef3e
[reactos.git] / reactos / ntoskrnl / se / luid.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/se/luid.c
6 * PURPOSE: Security manager
7 *
8 * PROGRAMMERS: No programmer listed.
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <internal/debug.h>
15
16 /* GLOBALS *******************************************************************/
17
18 static LARGE_INTEGER LuidIncrement;
19 static LARGE_INTEGER LuidValue;
20
21 /* FUNCTIONS *****************************************************************/
22
23 VOID
24 INIT_FUNCTION
25 NTAPI
26 SepInitLuid(VOID)
27 {
28 LUID DummyLuidValue = SYSTEM_LUID;
29
30 LuidValue.u.HighPart = DummyLuidValue.HighPart;
31 LuidValue.u.LowPart = DummyLuidValue.LowPart;
32 LuidIncrement.QuadPart = 1;
33 }
34
35
36 NTSTATUS
37 NTAPI
38 ExpAllocateLocallyUniqueId(OUT LUID *LocallyUniqueId)
39 {
40 LARGE_INTEGER NewLuid, PrevLuid;
41
42 /* atomically increment the luid */
43 do
44 {
45 PrevLuid = LuidValue;
46 NewLuid = RtlLargeIntegerAdd(PrevLuid,
47 LuidIncrement);
48 } while(ExfInterlockedCompareExchange64(&LuidValue.QuadPart,
49 &NewLuid.QuadPart,
50 &PrevLuid.QuadPart) != PrevLuid.QuadPart);
51
52 LocallyUniqueId->LowPart = NewLuid.u.LowPart;
53 LocallyUniqueId->HighPart = NewLuid.u.HighPart;
54
55 return STATUS_SUCCESS;
56 }
57
58
59 /*
60 * @implemented
61 */
62 NTSTATUS STDCALL
63 NtAllocateLocallyUniqueId(OUT LUID *LocallyUniqueId)
64 {
65 LUID NewLuid;
66 KPROCESSOR_MODE PreviousMode;
67 NTSTATUS Status = STATUS_SUCCESS;
68
69 PAGED_CODE();
70
71 PreviousMode = ExGetPreviousMode();
72
73 if(PreviousMode != KernelMode)
74 {
75 _SEH_TRY
76 {
77 ProbeForWrite(LocallyUniqueId,
78 sizeof(LUID),
79 sizeof(ULONG));
80 }
81 _SEH_HANDLE
82 {
83 Status = _SEH_GetExceptionCode();
84 }
85 _SEH_END;
86
87 if(!NT_SUCCESS(Status))
88 {
89 return Status;
90 }
91 }
92
93 Status = ExpAllocateLocallyUniqueId(&NewLuid);
94
95 _SEH_TRY
96 {
97 *LocallyUniqueId = NewLuid;
98 }
99 _SEH_HANDLE
100 {
101 Status = _SEH_GetExceptionCode();
102 }
103 _SEH_END;
104
105 return Status;
106 }
107
108 /* EOF */