ReactOS  0.4.14-dev-297-g23e575c
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 INIT_FUNCTION 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 42 of file mutant.c.

43 {
44  DPRINT("ExpDeleteMutant(ObjectBody 0x%p)\n", ObjectBody);
45 
46  /* Make sure to release the Mutant */
47  KeReleaseMutant((PKMUTANT)ObjectBody,
49  TRUE,
50  FALSE);
51 }
#define TRUE
Definition: types.h:120
void DPRINT(...)
Definition: polytest.cpp:61
#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

Referenced by ExpInitializeMutantImplementation().

◆ ExpInitializeMutantImplementation()

BOOLEAN INIT_FUNCTION NTAPI ExpInitializeMutantImplementation ( VOID  )

Definition at line 56 of file mutant.c.

57 {
58  OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
61  DPRINT("Creating Mutant Object Type\n");
62 
63  /* Create the Event Pair Object Type */
64  RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
65  RtlInitUnicodeString(&Name, L"Mutant");
66  ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
67  ObjectTypeInitializer.DefaultNonPagedPoolCharge = sizeof(KMUTANT);
68  ObjectTypeInitializer.GenericMapping = ExpMutantMapping;
69  ObjectTypeInitializer.PoolType = NonPagedPool;
70  ObjectTypeInitializer.DeleteProcedure = ExpDeleteMutant;
71  ObjectTypeInitializer.ValidAccessMask = MUTANT_ALL_ACCESS;
72  ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
73  Status = ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &ExMutantObjectType);
74  if (!NT_SUCCESS(Status)) return FALSE;
75  return TRUE;
76 }
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 OBJ_OPENLINK
Definition: winternl.h:230
#define MUTANT_ALL_ACCESS
Definition: extypes.h:110
VOID NTAPI ExpDeleteMutant(PVOID ObjectBody)
Definition: mutant.c:42
struct NameRec_ * Name
Definition: cdprocs.h:464
smooth NULL
Definition: ftsmooth.c:416
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:22
void DPRINT(...)
Definition: polytest.cpp:61
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
GENERIC_MAPPING ExpMutantMapping
Definition: mutant.c:24
static const WCHAR L[]
Definition: oid.c:1250
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
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 83 of file mutant.c.

87 {
89  HANDLE hMutant;
90  PKMUTANT Mutant;
92  PAGED_CODE();
93  DPRINT("NtCreateMutant(0x%p, 0x%x, 0x%p)\n",
94  MutantHandle, DesiredAccess, ObjectAttributes);
95 
96  /* Check if we were called from user-mode */
97  if (PreviousMode != KernelMode)
98  {
99  /* Enter SEH Block */
100  _SEH2_TRY
101  {
102  /* Check handle pointer */
103  ProbeForWriteHandle(MutantHandle);
104  }
106  {
107  /* Return the exception code */
109  }
110  _SEH2_END;
111  }
112 
113  /* Create the Mutant Object*/
117  PreviousMode,
118  NULL,
119  sizeof(KMUTANT),
120  0,
121  0,
122  (PVOID*)&Mutant);
123 
124  /* Check for success */
125  if(NT_SUCCESS(Status))
126  {
127  /* Initialize the Kernel Mutant */
128  DPRINT("Initializing the Mutant\n");
130 
131  /* Insert the Object */
132  Status = ObInsertObject((PVOID)Mutant,
133  NULL,
135  0,
136  NULL,
137  &hMutant);
138 
139  /* Check for success */
140  if (NT_SUCCESS(Status))
141  {
142  /* Enter SEH for return */
143  _SEH2_TRY
144  {
145  /* Return the handle to the caller */
146  *MutantHandle = hMutant;
147  }
149  {
150  /* Get the exception code */
152  }
153  _SEH2_END;
154  }
155  }
156 
157  /* Return Status */
158  return Status;
159 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
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: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
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:22
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
#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
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
_In_ BOOLEAN InitialOwner
Definition: kefuncs.h:590

Referenced by GRAPHICS_BUFFER_Initialize(), and Test_Release().

◆ NtOpenMutant()

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

Definition at line 166 of file mutant.c.

169 {
170  HANDLE hMutant;
173  PAGED_CODE();
174  DPRINT("NtOpenMutant(0x%p, 0x%x, 0x%p)\n",
175  MutantHandle, DesiredAccess, ObjectAttributes);
176 
177  /* Check if we were called from user-mode */
178  if (PreviousMode != KernelMode)
179  {
180  /* Enter SEH Block */
181  _SEH2_TRY
182  {
183  /* Check handle pointer */
184  ProbeForWriteHandle(MutantHandle);
185  }
187  {
188  /* Return the exception code */
190  }
191  _SEH2_END;
192  }
193 
194  /* Open the Object */
197  PreviousMode,
198  NULL,
200  NULL,
201  &hMutant);
202 
203  /* Check for success */
204  if(NT_SUCCESS(Status))
205  {
206  /* Enter SEH for return */
207  _SEH2_TRY
208  {
209  /* Return the handle to the caller */
210  *MutantHandle = hMutant;
211  }
213  {
215  }
216  _SEH2_END;
217  }
218 
219  /* Return Status */
220  return Status;
221 }
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
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:22
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
#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

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 228 of file mutant.c.

233 {
234  PKMUTANT Mutant;
237  PMUTANT_BASIC_INFORMATION BasicInfo =
238  (PMUTANT_BASIC_INFORMATION)MutantInformation;
239  PAGED_CODE();
240 
241  /* Check buffers and parameters */
242  Status = DefaultQueryInfoBufferCheck(MutantInformationClass,
244  sizeof(ExMutantInfoClass) /
245  sizeof(ExMutantInfoClass[0]),
246  MutantInformation,
247  MutantInformationLength,
248  ResultLength,
249  NULL,
250  PreviousMode);
251  if(!NT_SUCCESS(Status))
252  {
253  DPRINT("NtQueryMutant() failed, Status: 0x%x\n", Status);
254  return Status;
255  }
256 
257  /* Open the Object */
258  Status = ObReferenceObjectByHandle(MutantHandle,
261  PreviousMode,
262  (PVOID*)&Mutant,
263  NULL);
264  /* Check for Status */
265  if (NT_SUCCESS(Status))
266  {
267  /* Enter SEH Block for return */
268  _SEH2_TRY
269  {
270  /* Fill out the Basic Information Requested */
271  DPRINT("Returning Mutant Information\n");
272  BasicInfo->CurrentCount = KeReadStateMutant(Mutant);
273  BasicInfo->OwnedByCaller = (Mutant->OwnerThread ==
275  BasicInfo->AbandonedState = Mutant->Abandoned;
276 
277  /* Return the Result Length if requested */
279  }
281  {
283  }
284  _SEH2_END;
285 
286  /* Release the Object */
287  ObDereferenceObject(Mutant);
288  }
289 
290  /* Return Status */
291  return Status;
292 }
IN CINT OUT PVOID IN ULONG OUT PULONG ResultLength
Definition: conport.c:47
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 PAGED_CODE()
Definition: video.h:57
struct _MUTANT_BASIC_INFORMATION MUTANT_BASIC_INFORMATION
_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
smooth NULL
Definition: ftsmooth.c:416
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:22
void DPRINT(...)
Definition: polytest.cpp:61
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
static const INFORMATION_CLASS_INFO ExMutantInfoClass[]
Definition: mutant.c:32
struct _MUTANT_BASIC_INFORMATION * PMUTANT_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)
Definition: probe.h:59
Status
Definition: gdiplustypes.h:24
LONG NTAPI KeReadStateMutant(IN PKMUTANT Mutant)
Definition: mutex.c:87
_SEH2_END
Definition: create.c:4424
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
BOOLEAN Abandoned
Definition: ketypes.h:832
struct _KTHREAD *RESTRICTED_POINTER OwnerThread
Definition: ketypes.h:831
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
#define KeGetCurrentThread
Definition: hal.h:44
#define MUTANT_QUERY_STATE
Definition: extypes.h:109

◆ NtReleaseMutant()

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

Definition at line 299 of file mutant.c.

301 {
302  PKMUTANT Mutant;
305  PAGED_CODE();
306  DPRINT("NtReleaseMutant(MutantHandle 0x%p PreviousCount 0x%p)\n",
307  MutantHandle,
308  PreviousCount);
309 
310  /* Check if we were called from user-mode */
311  if ((PreviousCount) && (PreviousMode != KernelMode))
312  {
313  /* Entry SEH Block */
314  _SEH2_TRY
315  {
316  /* Make sure the state pointer is valid */
317  ProbeForWriteLong(PreviousCount);
318  }
320  {
321  /* Return the exception code */
323  }
324  _SEH2_END;
325  }
326 
327  /* Open the Object */
328  Status = ObReferenceObjectByHandle(MutantHandle,
329  0, /* No access rights required */
331  PreviousMode,
332  (PVOID*)&Mutant,
333  NULL);
334 
335  /* Check for Success and release if such */
336  if (NT_SUCCESS(Status))
337  {
338  /*
339  * Release the mutant. doing so might raise an exception which we're
340  * required to catch!
341  */
342  _SEH2_TRY
343  {
344  /* Release the mutant */
345  LONG Prev = KeReleaseMutant(Mutant,
347  FALSE,
348  FALSE);
349 
350  /* Return the previous count if requested */
351  if (PreviousCount) *PreviousCount = Prev;
352  }
354  {
355  /* Get the exception code */
357  }
358  _SEH2_END;
359 
360  /* Dereference it */
361  ObDereferenceObject(Mutant);
362  }
363 
364  /* Return Status */
365  return Status;
366 }
#define ProbeForWriteLong(Ptr)
Definition: probe.h:37
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 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
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:22
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
#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
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12

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

Variable Documentation

◆ ExMutantInfoClass

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

Definition at line 32 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 24 of file mutant.c.

Referenced by ExpInitializeMutantImplementation().