ReactOS  0.4.14-dev-342-gdc047f9
sem.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for sem.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

BOOLEAN INIT_FUNCTION NTAPI ExpInitializeSemaphoreImplementation (VOID)
 
NTSTATUS NTAPI NtCreateSemaphore (OUT PHANDLE SemaphoreHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN LONG InitialCount, IN LONG MaximumCount)
 
NTSTATUS NTAPI NtOpenSemaphore (OUT PHANDLE SemaphoreHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
 
NTSTATUS NTAPI NtQuerySemaphore (IN HANDLE SemaphoreHandle, IN SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass, OUT PVOID SemaphoreInformation, IN ULONG SemaphoreInformationLength, OUT PULONG ReturnLength OPTIONAL)
 
NTSTATUS NTAPI NtReleaseSemaphore (IN HANDLE SemaphoreHandle, IN LONG ReleaseCount, OUT PLONG PreviousCount OPTIONAL)
 

Variables

POBJECT_TYPE ExSemaphoreObjectType
 
GENERIC_MAPPING ExSemaphoreMapping
 
static const INFORMATION_CLASS_INFO ExSemaphoreInfoClass []
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file sem.c.

Function Documentation

◆ ExpInitializeSemaphoreImplementation()

BOOLEAN INIT_FUNCTION NTAPI ExpInitializeSemaphoreImplementation ( VOID  )

Definition at line 43 of file sem.c.

44 {
45  OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
48  DPRINT("Creating Semaphore Object Type\n");
49 
50  /* Create the Event Pair Object Type */
51  RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
52  RtlInitUnicodeString(&Name, L"Semaphore");
53  ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
54  ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(KSEMAPHORE);
55  ObjectTypeInitializer.GenericMapping = ExSemaphoreMapping;
56  ObjectTypeInitializer.PoolType = NonPagedPool;
57  ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
58  ObjectTypeInitializer.ValidAccessMask = SEMAPHORE_ALL_ACCESS;
59  Status = ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &ExSemaphoreObjectType);
60  if (!NT_SUCCESS(Status)) return FALSE;
61  return TRUE;
62 }
NTSTATUS NTAPI ObCreateObjectType(IN PUNICODE_STRING TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, IN PVOID Reserved, OUT POBJECT_TYPE *ObjectType)
Definition: oblife.c:1048
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define OBJ_OPENLINK
Definition: winternl.h:230
struct NameRec_ * Name
Definition: cdprocs.h:464
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
GENERIC_MAPPING ExSemaphoreMapping
Definition: sem.c:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const WCHAR L[]
Definition: oid.c:1250
struct _KSEMAPHORE KSEMAPHORE
Status
Definition: gdiplustypes.h:24
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define SEMAPHORE_ALL_ACCESS
Definition: winbase.h:160
ULONG DefaultNonPagedPoolCharge
Definition: obtypes.h:365
POBJECT_TYPE ExSemaphoreObjectType
Definition: sem.c:22

Referenced by ExpInitSystemPhase1().

◆ NtCreateSemaphore()

NTSTATUS NTAPI NtCreateSemaphore ( OUT PHANDLE  SemaphoreHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
IN LONG  InitialCount,
IN LONG  MaximumCount 
)

Definition at line 69 of file sem.c.

74 {
75  PKSEMAPHORE Semaphore;
76  HANDLE hSemaphore;
79  PAGED_CODE();
80 
81  /* Check if we were called from user-mode */
82  if (PreviousMode != KernelMode)
83  {
84  /* Enter SEH Block */
85  _SEH2_TRY
86  {
87  /* Check handle pointer */
88  ProbeForWriteHandle(SemaphoreHandle);
89  }
91  {
92  /* Return the exception code */
94  }
95  _SEH2_END;
96  }
97 
98  /* Make sure the counts make sense */
99  if ((MaximumCount <= 0) ||
100  (InitialCount < 0) ||
101  (InitialCount > MaximumCount))
102  {
103  DPRINT("Invalid Count Data!\n");
105  }
106 
107  /* Create the Semaphore Object */
111  PreviousMode,
112  NULL,
113  sizeof(KSEMAPHORE),
114  0,
115  0,
116  (PVOID*)&Semaphore);
117 
118  /* Check for Success */
119  if (NT_SUCCESS(Status))
120  {
121  /* Initialize it */
122  KeInitializeSemaphore(Semaphore,
123  InitialCount,
124  MaximumCount);
125 
126  /* Insert it into the Object Tree */
127  Status = ObInsertObject((PVOID)Semaphore,
128  NULL,
130  0,
131  NULL,
132  &hSemaphore);
133 
134  /* Check for success */
135  if (NT_SUCCESS(Status))
136  {
137  /* Enter SEH Block for return */
138  _SEH2_TRY
139  {
140  /* Return the handle */
141  *SemaphoreHandle = hSemaphore;
142  }
144  {
145  /* Get the exception code */
147  }
148  _SEH2_END;
149  }
150  }
151 
152  /* Return Status */
153  return Status;
154 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3066
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
NTSTATUS NTAPI ObCreateObject(IN KPROCESSOR_MODE ProbeMode OPTIONAL, IN POBJECT_TYPE Type, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext OPTIONAL, IN ULONG ObjectSize, IN ULONG PagedPoolCharge OPTIONAL, IN ULONG NonPagedPoolCharge OPTIONAL, OUT PVOID *Object)
Definition: oblife.c:952
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
VOID NTAPI KeInitializeSemaphore(IN PKSEMAPHORE Semaphore, IN LONG Count, IN LONG Limit)
Definition: semphobj.c:22
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
NTSTATUS NTAPI ObInsertObject(IN PVOID Object, IN PACCESS_STATE AccessState OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG ObjectPointerBias, OUT PVOID *NewObject OPTIONAL, OUT PHANDLE Handle)
Definition: obhandle.c:2932
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK DesiredAccess
Definition: create.c:4157
*Count PHARDWARE_COUNTER _In_ ULONG MaximumCount
Definition: kefuncs.h:1460
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
POBJECT_TYPE ExSemaphoreObjectType
Definition: sem.c:22

Referenced by RtlInitializeResource().

◆ NtOpenSemaphore()

NTSTATUS NTAPI NtOpenSemaphore ( OUT PHANDLE  SemaphoreHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES  ObjectAttributes 
)

Definition at line 161 of file sem.c.

164 {
165  HANDLE hSemaphore;
168  PAGED_CODE();
169 
170  /* Check if we were called from user-mode */
171  if (PreviousMode != KernelMode)
172  {
173  /* Enter SEH Block */
174  _SEH2_TRY
175  {
176  /* Check handle pointer */
177  ProbeForWriteHandle(SemaphoreHandle);
178  }
180  {
181  /* Return the exception code */
183  }
184  _SEH2_END;
185  }
186 
187  /* Open the Object */
190  PreviousMode,
191  NULL,
193  NULL,
194  &hSemaphore);
195 
196  /* Check for success */
197  if (NT_SUCCESS(Status))
198  {
199  /* Enter SEH Block for return */
200  _SEH2_TRY
201  {
202  /* Return the handle */
203  *SemaphoreHandle = hSemaphore;
204  }
206  {
207  /* Get the exception code */
209  }
210  _SEH2_END;
211  }
212 
213  /* Return Status */
214  return Status;
215 }
NTSTATUS NTAPI ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN OUT PVOID ParseContext, OUT PHANDLE Handle)
Definition: obhandle.c:2529
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3066
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK DesiredAccess
Definition: create.c:4157
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
POBJECT_TYPE ExSemaphoreObjectType
Definition: sem.c:22

Referenced by NtOpenObject().

◆ NtQuerySemaphore()

NTSTATUS NTAPI NtQuerySemaphore ( IN HANDLE  SemaphoreHandle,
IN SEMAPHORE_INFORMATION_CLASS  SemaphoreInformationClass,
OUT PVOID  SemaphoreInformation,
IN ULONG  SemaphoreInformationLength,
OUT PULONG ReturnLength  OPTIONAL 
)

Definition at line 222 of file sem.c.

227 {
228  PKSEMAPHORE Semaphore;
231  PAGED_CODE();
232 
233  /* Check buffers and class validity */
234  Status = DefaultQueryInfoBufferCheck(SemaphoreInformationClass,
236  sizeof(ExSemaphoreInfoClass) /
237  sizeof(ExSemaphoreInfoClass[0]),
238  SemaphoreInformation,
239  SemaphoreInformationLength,
240  ReturnLength,
241  NULL,
242  PreviousMode);
243  if (!NT_SUCCESS(Status))
244  {
245  /* Invalid buffers */
246  DPRINT("NtQuerySemaphore() failed, Status: 0x%x\n", Status);
247  return Status;
248  }
249 
250  /* Get the Object */
251  Status = ObReferenceObjectByHandle(SemaphoreHandle,
254  PreviousMode,
255  (PVOID*)&Semaphore,
256  NULL);
257 
258  /* Check for success */
259  if (NT_SUCCESS(Status))
260  {
261  /* Entry SEH Block */
262  _SEH2_TRY
263  {
264  PSEMAPHORE_BASIC_INFORMATION BasicInfo =
265  (PSEMAPHORE_BASIC_INFORMATION)SemaphoreInformation;
266 
267  /* Return the basic information */
268  BasicInfo->CurrentCount = KeReadStateSemaphore(Semaphore);
269  BasicInfo->MaximumCount = Semaphore->Limit;
270 
271  /* Return the length */
272  if (ReturnLength) *ReturnLength = sizeof(*BasicInfo);
273  }
275  {
276  /* Get the exception code */
278  }
279  _SEH2_END;
280 
281  /* Dereference the Object */
282  ObDereferenceObject(Semaphore);
283  }
284 
285  /* Return status */
286  return Status;
287 }
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3066
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define SEMAPHORE_QUERY_STATE
Definition: security.c:57
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:496
LONG Limit
Definition: ketypes.h:805
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
static const INFORMATION_CLASS_INFO ExSemaphoreInfoClass[]
Definition: sem.c:32
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
static __inline NTSTATUS DefaultQueryInfoBufferCheck(ULONG Class, const INFORMATION_CLASS_INFO *ClassList, ULONG ClassListEntries, PVOID Buffer, ULONG BufferLength, PULONG ReturnLength, PULONG_PTR ReturnLengthPtr, KPROCESSOR_MODE PreviousMode)
Definition: probe.h:59
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
LONG NTAPI KeReadStateSemaphore(IN PKSEMAPHORE Semaphore)
Definition: semphobj.c:41
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
struct _SEMAPHORE_BASIC_INFORMATION * PSEMAPHORE_BASIC_INFORMATION
POBJECT_TYPE ExSemaphoreObjectType
Definition: sem.c:22

◆ NtReleaseSemaphore()

NTSTATUS NTAPI NtReleaseSemaphore ( IN HANDLE  SemaphoreHandle,
IN LONG  ReleaseCount,
OUT PLONG PreviousCount  OPTIONAL 
)

Definition at line 294 of file sem.c.

297 {
299  PKSEMAPHORE Semaphore;
301  PAGED_CODE();
302 
303  /* Check if we were called from user-mode */
304  if ((PreviousCount) && (PreviousMode != KernelMode))
305  {
306  /* Entry SEH Block */
307  _SEH2_TRY
308  {
309  /* Make sure the state pointer is valid */
310  ProbeForWriteLong(PreviousCount);
311  }
313  {
314  /* Return the exception code */
316  }
317  _SEH2_END;
318  }
319 
320  /* Make sure count makes sense */
321  if (ReleaseCount <= 0)
322  {
323  DPRINT("Invalid Release Count\n");
325  }
326 
327  /* Get the Object */
328  Status = ObReferenceObjectByHandle(SemaphoreHandle,
331  PreviousMode,
332  (PVOID*)&Semaphore,
333  NULL);
334 
335  /* Check for success */
336  if (NT_SUCCESS(Status))
337  {
338  /* Enter SEH Block */
339  _SEH2_TRY
340  {
341  /* Release the semaphore */
342  LONG PrevCount = KeReleaseSemaphore(Semaphore,
344  ReleaseCount,
345  FALSE);
346 
347  /* Return the old count if requested */
348  if (PreviousCount) *PreviousCount = PrevCount;
349  }
351  {
352  /* Get the exception code */
354  }
355  _SEH2_END;
356 
357  /* Dereference the Semaphore */
358  ObDereferenceObject(Semaphore);
359  }
360 
361  /* Return Status */
362  return Status;
363 }
#define ProbeForWriteLong(Ptr)
Definition: probe.h:37
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
#define SEMAPHORE_MODIFY_STATE
Definition: winbase.h:161
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3066
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:496
long LONG
Definition: pedump.c:60
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
LONG NTAPI KeReleaseSemaphore(IN PKSEMAPHORE Semaphore, IN KPRIORITY Increment, IN LONG Adjustment, IN BOOLEAN Wait)
Definition: semphobj.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:566
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
POBJECT_TYPE ExSemaphoreObjectType
Definition: sem.c:22

Referenced by ReleaseSemaphore(), RtlConvertExclusiveToShared(), and RtlReleaseResource().

Variable Documentation

◆ ExSemaphoreInfoClass

const INFORMATION_CLASS_INFO ExSemaphoreInfoClass[]
static
Initial value:
=
{
ICI_SQ_SAME( sizeof(SEMAPHORE_BASIC_INFORMATION), sizeof(ULONG), ICIF_QUERY),
}
unsigned int ULONG
Definition: retypes.h:1

Definition at line 32 of file sem.c.

Referenced by NtQuerySemaphore().

◆ ExSemaphoreMapping

GENERIC_MAPPING ExSemaphoreMapping
Initial value:
=
{
}
#define STANDARD_RIGHTS_WRITE
Definition: nt_native.h:66
#define SEMAPHORE_MODIFY_STATE
Definition: winbase.h:161
#define SEMAPHORE_QUERY_STATE
Definition: security.c:57
#define STANDARD_RIGHTS_EXECUTE
Definition: nt_native.h:67
#define STANDARD_RIGHTS_READ
Definition: nt_native.h:65
#define SYNCHRONIZE
Definition: nt_native.h:61
#define SEMAPHORE_ALL_ACCESS
Definition: winbase.h:160

Definition at line 24 of file sem.c.

Referenced by ExpInitializeSemaphoreImplementation().

◆ ExSemaphoreObjectType