ReactOS 0.4.15-dev-8413-gc1c91f2
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:73

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}
struct NameRec_ * Name
Definition: cdprocs.h:460
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define NonPagedPool
Definition: env_spec_w32.h:307
Status
Definition: gdiplustypes.h:25
#define OBJ_OPENLINK
Definition: winternl.h:230
VOID NTAPI ExpDeleteMutant(PVOID ObjectBody)
Definition: mutant.c:38
GENERIC_MAPPING ExpMutantMapping
Definition: mutant.c:20
POBJECT_TYPE ExMutantObjectType
Definition: mutant.c:18
#define MUTANT_ALL_ACCESS
Definition: extypes.h:110
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define L(x)
Definition: ntvdm.h:50
NTSTATUS NTAPI ObCreateObjectType(IN PUNICODE_STRING TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, IN PVOID Reserved, OUT POBJECT_TYPE *ObjectType)
Definition: oblife.c:1136
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
OB_DELETE_METHOD DeleteProcedure
Definition: obtypes.h:369
ULONG DefaultNonPagedPoolCharge
Definition: obtypes.h:365
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
struct _KMUTANT KMUTANT

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 */
94 {
95 /* Enter SEH Block */
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*/
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 */
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}
#define PAGED_CODE()
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define ExGetPreviousMode
Definition: ex.h:140
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:349
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define KernelMode
Definition: asm.h:34
VOID NTAPI KeInitializeMutant(IN PKMUTANT Mutant, IN BOOLEAN InitialOwner)
Definition: mutex.c:22
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:2935
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:1039
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658
_In_ BOOLEAN InitialOwner
Definition: kefuncs.h:565
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103

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 */
175 {
176 /* Enter SEH Block */
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 */
194 NULL,
196 NULL,
197 &hMutant);
198
199 /* Check for success */
200 if(NT_SUCCESS(Status))
201 {
202 /* Enter SEH for return */
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:2532

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]),
243 MutantInformation,
244 MutantInformationLength,
246 NULL,
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,
259 (PVOID*)&Mutant,
260 NULL);
261 /* Check for Status */
262 if (NT_SUCCESS(Status))
263 {
264 /* Enter SEH Block for return */
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 KeGetCurrentThread
Definition: hal.h:55
#define ICIF_PROBE_READ_WRITE
Definition: icif.h:24
static const INFORMATION_CLASS_INFO ExMutantInfoClass[]
Definition: mutant.c:28
struct _MUTANT_BASIC_INFORMATION * PMUTANT_BASIC_INFORMATION
struct _MUTANT_BASIC_INFORMATION MUTANT_BASIC_INFORMATION
#define MUTANT_QUERY_STATE
Definition: extypes.h:109
static __inline NTSTATUS DefaultQueryInfoBufferCheck(_In_ ULONG Class, _In_ const INFORMATION_CLASS_INFO *ClassList, _In_ ULONG ClassListEntries, _In_ ULONG Flags, _In_opt_ PVOID Buffer, _In_ ULONG BufferLength, _In_opt_ PULONG ReturnLength, _In_opt_ PULONG_PTR ReturnLengthPtr, _In_ KPROCESSOR_MODE PreviousMode)
Probe helper that validates the provided parameters whenever a NtQuery*** system call is invoked from...
Definition: probe.h:219
LONG NTAPI KeReadStateMutant(IN PKMUTANT Mutant)
Definition: mutex.c:87
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
BOOLEAN Abandoned
Definition: ketypes.h:844
struct _KTHREAD *RESTRICTED_POINTER OwnerThread
Definition: ketypes.h:843
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG _Out_ PULONG ResultLength
Definition: wdfdevice.h:3776
#define ObDereferenceObject
Definition: obfuncs.h:203

◆ 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 */
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 */
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 */
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}
long LONG
Definition: pedump.c:60
#define ProbeForWriteLong(Ptr)
Definition: probe.h:37

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:18
#define IQS_SAME(Type, Alignment, Flags)
Definition: icif.h:37
uint32_t ULONG
Definition: typedefs.h:59

Definition at line 28 of file mutant.c.

Referenced by NtQueryMutant().

◆ ExMutantObjectType

POBJECT_TYPE ExMutantObjectType = NULL

◆ ExpMutantMapping

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

Definition at line 20 of file mutant.c.

Referenced by ExpInitializeMutantImplementation().