ReactOS  0.4.15-dev-3182-g7b62228
mutant.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for mutant.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

VOID NTAPI ExpDeleteMutant (PVOID ObjectBody)
 
BOOLEAN NTAPI ExpInitializeMutantImplementation (VOID)
 
NTSTATUS NTAPI NtCreateMutant (OUT PHANDLE MutantHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN BOOLEAN InitialOwner)
 
NTSTATUS NTAPI NtOpenMutant (OUT PHANDLE MutantHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
 
NTSTATUS NTAPI NtQueryMutant (IN HANDLE MutantHandle, IN MUTANT_INFORMATION_CLASS MutantInformationClass, OUT PVOID MutantInformation, IN ULONG MutantInformationLength, OUT PULONG ResultLength OPTIONAL)
 
NTSTATUS NTAPI NtReleaseMutant (IN HANDLE MutantHandle, IN PLONG PreviousCount OPTIONAL)
 

Variables

POBJECT_TYPE ExMutantObjectType = NULL
 
GENERIC_MAPPING ExpMutantMapping
 
static const INFORMATION_CLASS_INFO ExMutantInfoClass []
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file mutant.c.

Function Documentation

◆ ExpDeleteMutant()

VOID NTAPI ExpDeleteMutant ( PVOID  ObjectBody)

Definition at line 38 of file mutant.c.

39 {
40  DPRINT("ExpDeleteMutant(ObjectBody 0x%p)\n", ObjectBody);
41 
42  /* Make sure to release the Mutant */
43  KeReleaseMutant((PKMUTANT)ObjectBody,
45  TRUE,
46  FALSE);
47 }
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define MUTANT_INCREMENT
Definition: extypes.h:84
LONG NTAPI KeReleaseMutant(IN PKMUTANT Mutant, IN KPRIORITY Increment, IN BOOLEAN Abandon, IN BOOLEAN Wait)
Definition: mutex.c:98
#define DPRINT
Definition: sndvol32.h:71

Referenced by ExpInitializeMutantImplementation().

◆ ExpInitializeMutantImplementation()

BOOLEAN NTAPI ExpInitializeMutantImplementation ( VOID  )

Definition at line 52 of file mutant.c.

53 {
54  OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
57  DPRINT("Creating Mutant Object Type\n");
58 
59  /* Create the Event Pair Object Type */
60  RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
61  RtlInitUnicodeString(&Name, L"Mutant");
62  ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
63  ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(KMUTANT);
64  ObjectTypeInitializer.GenericMapping = ExpMutantMapping;
65  ObjectTypeInitializer.PoolType = NonPagedPool;
66  ObjectTypeInitializer.DeleteProcedure = ExpDeleteMutant;
67  ObjectTypeInitializer.ValidAccessMask = MUTANT_ALL_ACCESS;
68  ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
69  Status = ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &ExMutantObjectType);
70  if (!NT_SUCCESS(Status)) return FALSE;
71  return TRUE;
72 }
#define OBJ_OPENLINK
Definition: winternl.h:230
struct _KMUTANT KMUTANT
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 FALSE
Definition: types.h:117
#define MUTANT_ALL_ACCESS
Definition: extypes.h:110
VOID NTAPI ExpDeleteMutant(PVOID ObjectBody)
Definition: mutant.c:38
struct NameRec_ * Name
Definition: cdprocs.h:459
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:18
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
GENERIC_MAPPING ExpMutantMapping
Definition: mutant.c:20
static const WCHAR L[]
Definition: oid.c:1250
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
#define NULL
Definition: types.h:112
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define DPRINT
Definition: sndvol32.h:71
OB_DELETE_METHOD DeleteProcedure
Definition: obtypes.h:369
ULONG DefaultNonPagedPoolCharge
Definition: obtypes.h:365

Referenced by ExpInitSystemPhase1().

◆ NtCreateMutant()

NTSTATUS NTAPI NtCreateMutant ( OUT PHANDLE  MutantHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
IN BOOLEAN  InitialOwner 
)

Definition at line 79 of file mutant.c.

83 {
85  HANDLE hMutant;
86  PKMUTANT Mutant;
88  PAGED_CODE();
89  DPRINT("NtCreateMutant(0x%p, 0x%x, 0x%p)\n",
90  MutantHandle, DesiredAccess, ObjectAttributes);
91 
92  /* Check if we were called from user-mode */
93  if (PreviousMode != KernelMode)
94  {
95  /* Enter SEH Block */
96  _SEH2_TRY
97  {
98  /* Check handle pointer */
99  ProbeForWriteHandle(MutantHandle);
100  }
102  {
103  /* Return the exception code */
105  }
106  _SEH2_END;
107  }
108 
109  /* Create the Mutant Object*/
113  PreviousMode,
114  NULL,
115  sizeof(KMUTANT),
116  0,
117  0,
118  (PVOID*)&Mutant);
119 
120  /* Check for success */
121  if(NT_SUCCESS(Status))
122  {
123  /* Initialize the Kernel Mutant */
124  DPRINT("Initializing the Mutant\n");
126 
127  /* Insert the Object */
128  Status = ObInsertObject((PVOID)Mutant,
129  NULL,
131  0,
132  NULL,
133  &hMutant);
134 
135  /* Check for success */
136  if (NT_SUCCESS(Status))
137  {
138  /* Enter SEH for return */
139  _SEH2_TRY
140  {
141  /* Return the handle to the caller */
142  *MutantHandle = hMutant;
143  }
145  {
146  /* Get the exception code */
148  }
149  _SEH2_END;
150  }
151  }
152 
153  /* Return Status */
154  return Status;
155 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2654
VOID NTAPI KeInitializeMutant(IN PKMUTANT Mutant, IN BOOLEAN InitialOwner)
Definition: mutex.c:22
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
_SEH2_TRY
Definition: create.c:4226
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:18
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
Status
Definition: gdiplustypes.h:24
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
_SEH2_END
Definition: create.c:4400
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:2931
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
#define NULL
Definition: types.h:112
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define DPRINT
Definition: sndvol32.h:71
_In_ BOOLEAN InitialOwner
Definition: kefuncs.h:577
#define PAGED_CODE()

Referenced by GRAPHICS_BUFFER_Initialize(), InitializeUserModePnpManager(), and Test_Release().

◆ NtOpenMutant()

NTSTATUS NTAPI NtOpenMutant ( OUT PHANDLE  MutantHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES  ObjectAttributes 
)

Definition at line 162 of file mutant.c.

165 {
166  HANDLE hMutant;
169  PAGED_CODE();
170  DPRINT("NtOpenMutant(0x%p, 0x%x, 0x%p)\n",
171  MutantHandle, DesiredAccess, ObjectAttributes);
172 
173  /* Check if we were called from user-mode */
174  if (PreviousMode != KernelMode)
175  {
176  /* Enter SEH Block */
177  _SEH2_TRY
178  {
179  /* Check handle pointer */
180  ProbeForWriteHandle(MutantHandle);
181  }
183  {
184  /* Return the exception code */
186  }
187  _SEH2_END;
188  }
189 
190  /* Open the Object */
193  PreviousMode,
194  NULL,
196  NULL,
197  &hMutant);
198 
199  /* Check for success */
200  if(NT_SUCCESS(Status))
201  {
202  /* Enter SEH for return */
203  _SEH2_TRY
204  {
205  /* Return the handle to the caller */
206  *MutantHandle = hMutant;
207  }
209  {
211  }
212  _SEH2_END;
213  }
214 
215  /* Return Status */
216  return Status;
217 }
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:2528
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2654
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
_SEH2_TRY
Definition: create.c:4226
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:18
Status
Definition: gdiplustypes.h:24
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
_SEH2_END
Definition: create.c:4400
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
#define NULL
Definition: types.h:112
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define DPRINT
Definition: sndvol32.h:71
#define PAGED_CODE()

Referenced by NtOpenObject().

◆ NtQueryMutant()

NTSTATUS NTAPI NtQueryMutant ( IN HANDLE  MutantHandle,
IN MUTANT_INFORMATION_CLASS  MutantInformationClass,
OUT PVOID  MutantInformation,
IN ULONG  MutantInformationLength,
OUT PULONG ResultLength  OPTIONAL 
)

Definition at line 224 of file mutant.c.

229 {
230  PKMUTANT Mutant;
233  PMUTANT_BASIC_INFORMATION BasicInfo =
234  (PMUTANT_BASIC_INFORMATION)MutantInformation;
235  PAGED_CODE();
236 
237  /* Check buffers and parameters */
238  Status = DefaultQueryInfoBufferCheck(MutantInformationClass,
240  sizeof(ExMutantInfoClass) /
241  sizeof(ExMutantInfoClass[0]),
242  MutantInformation,
243  MutantInformationLength,
244  ResultLength,
245  NULL,
246  PreviousMode,
247  TRUE);
248  if(!NT_SUCCESS(Status))
249  {
250  DPRINT("NtQueryMutant() failed, Status: 0x%x\n", Status);
251  return Status;
252  }
253 
254  /* Open the Object */
255  Status = ObReferenceObjectByHandle(MutantHandle,
258  PreviousMode,
259  (PVOID*)&Mutant,
260  NULL);
261  /* Check for Status */
262  if (NT_SUCCESS(Status))
263  {
264  /* Enter SEH Block for return */
265  _SEH2_TRY
266  {
267  /* Fill out the Basic Information Requested */
268  DPRINT("Returning Mutant Information\n");
269  BasicInfo->CurrentCount = KeReadStateMutant(Mutant);
270  BasicInfo->OwnedByCaller = (Mutant->OwnerThread ==
272  BasicInfo->AbandonedState = Mutant->Abandoned;
273 
274  /* Return the Result Length if requested */
276  }
278  {
280  }
281  _SEH2_END;
282 
283  /* Release the Object */
284  ObDereferenceObject(Mutant);
285  }
286 
287  /* Return Status */
288  return Status;
289 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
struct _MUTANT_BASIC_INFORMATION MUTANT_BASIC_INFORMATION
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, BOOLEAN CompleteProbing)
Definition: probe.h:59
_SEH2_TRY
Definition: create.c:4226
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:494
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:18
Status
Definition: gdiplustypes.h:24
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ObDereferenceObject
Definition: obfuncs.h:203
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
static const INFORMATION_CLASS_INFO ExMutantInfoClass[]
Definition: mutant.c:28
struct _MUTANT_BASIC_INFORMATION * PMUTANT_BASIC_INFORMATION
LONG NTAPI KeReadStateMutant(IN PKMUTANT Mutant)
Definition: mutex.c:87
_SEH2_END
Definition: create.c:4400
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
#define NULL
Definition: types.h:112
BOOLEAN Abandoned
Definition: ketypes.h:832
struct _KTHREAD *RESTRICTED_POINTER OwnerThread
Definition: ketypes.h:831
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG _Out_ PULONG ResultLength
Definition: wdfdevice.h:3776
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define DPRINT
Definition: sndvol32.h:71
#define KeGetCurrentThread
Definition: hal.h:55
#define MUTANT_QUERY_STATE
Definition: extypes.h:109
#define PAGED_CODE()

◆ NtReleaseMutant()

NTSTATUS NTAPI NtReleaseMutant ( IN HANDLE  MutantHandle,
IN PLONG PreviousCount  OPTIONAL 
)

Definition at line 296 of file mutant.c.

298 {
299  PKMUTANT Mutant;
302  PAGED_CODE();
303  DPRINT("NtReleaseMutant(MutantHandle 0x%p PreviousCount 0x%p)\n",
304  MutantHandle,
305  PreviousCount);
306 
307  /* Check if we were called from user-mode */
308  if ((PreviousCount) && (PreviousMode != KernelMode))
309  {
310  /* Entry SEH Block */
311  _SEH2_TRY
312  {
313  /* Make sure the state pointer is valid */
314  ProbeForWriteLong(PreviousCount);
315  }
317  {
318  /* Return the exception code */
320  }
321  _SEH2_END;
322  }
323 
324  /* Open the Object */
325  Status = ObReferenceObjectByHandle(MutantHandle,
326  0, /* No access rights required */
328  PreviousMode,
329  (PVOID*)&Mutant,
330  NULL);
331 
332  /* Check for Success and release if such */
333  if (NT_SUCCESS(Status))
334  {
335  /*
336  * Release the mutant. doing so might raise an exception which we're
337  * required to catch!
338  */
339  _SEH2_TRY
340  {
341  /* Release the mutant */
342  LONG Prev = KeReleaseMutant(Mutant,
344  FALSE,
345  FALSE);
346 
347  /* Return the previous count if requested */
348  if (PreviousCount) *PreviousCount = Prev;
349  }
351  {
352  /* Get the exception code */
354  }
355  _SEH2_END;
356 
357  /* Dereference it */
358  ObDereferenceObject(Mutant);
359  }
360 
361  /* Return Status */
362  return Status;
363 }
#define ProbeForWriteLong(Ptr)
Definition: probe.h:37
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
_SEH2_TRY
Definition: create.c:4226
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:494
#define FALSE
Definition: types.h:117
long LONG
Definition: pedump.c:60
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:18
Status
Definition: gdiplustypes.h:24
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define ObDereferenceObject
Definition: obfuncs.h:203
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
#define MUTANT_INCREMENT
Definition: extypes.h:84
LONG NTAPI KeReleaseMutant(IN PKMUTANT Mutant, IN KPRIORITY Increment, IN BOOLEAN Abandon, IN BOOLEAN Wait)
Definition: mutex.c:98
_SEH2_END
Definition: create.c:4400
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
#define NULL
Definition: types.h:112
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define DPRINT
Definition: sndvol32.h:71
#define PAGED_CODE()

Referenced by DeviceInstallThread(), GuiCopyFromGraphicsBuffer(), GuiPaintGraphicsBuffer(), PnpEventThread(), ReleaseMutex(), and Test_Release().

Variable Documentation

◆ ExMutantInfoClass

const INFORMATION_CLASS_INFO ExMutantInfoClass[]
static
Initial value:
=
{
}
#define ICIF_QUERY
Definition: icif.h:19
unsigned int ULONG
Definition: retypes.h:1
#define IQS_SAME(Type, Alignment, Flags)
Definition: icif.h:34

Definition at line 28 of file mutant.c.

Referenced by NtQueryMutant().

◆ ExMutantObjectType

POBJECT_TYPE ExMutantObjectType = NULL

◆ ExpMutantMapping

GENERIC_MAPPING ExpMutantMapping
Initial value:
=
{
}
#define STANDARD_RIGHTS_WRITE
Definition: nt_native.h:66
#define STANDARD_RIGHTS_EXECUTE
Definition: nt_native.h:67
#define MUTANT_ALL_ACCESS
Definition: extypes.h:110
#define STANDARD_RIGHTS_READ
Definition: nt_native.h:65
#define SYNCHRONIZE
Definition: nt_native.h:61
#define MUTANT_QUERY_STATE
Definition: extypes.h:109

Definition at line 20 of file mutant.c.

Referenced by ExpInitializeMutantImplementation().