2005-06-25 17:01:17 +00:00
|
|
|
/*
|
2006-06-28 13:53:30 +00:00
|
|
|
* PROJECT: ReactOS Kernel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
1998-08-28 23:24:25 +00:00
|
|
|
* FILE: ntoskrnl/ex/callback.c
|
2000-07-04 01:30:18 +00:00
|
|
|
* PURPOSE: Executive callbacks
|
2007-01-16 04:16:31 +00:00
|
|
|
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
|
2000-07-04 01:30:18 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
1998-08-28 23:24:25 +00:00
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2005-06-25 17:01:17 +00:00
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
1998-08-28 23:24:25 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* TYPES *********************************************************************/
|
2005-06-25 17:01:17 +00:00
|
|
|
|
|
|
|
/* Mapping for Callback Object */
|
|
|
|
GENERIC_MAPPING ExpCallbackMapping =
|
|
|
|
{
|
2012-09-02 22:06:42 +00:00
|
|
|
STANDARD_RIGHTS_READ,
|
|
|
|
STANDARD_RIGHTS_WRITE | CALLBACK_MODIFY_STATE,
|
|
|
|
STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
|
2007-01-16 04:16:31 +00:00
|
|
|
CALLBACK_ALL_ACCESS
|
2005-06-25 17:01:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Kernel Default Callbacks */
|
|
|
|
PCALLBACK_OBJECT SetSystemTimeCallback;
|
|
|
|
PCALLBACK_OBJECT SetSystemStateCallback;
|
|
|
|
PCALLBACK_OBJECT PowerStateCallback;
|
|
|
|
SYSTEM_CALLBACKS ExpInitializeCallback[] =
|
|
|
|
{
|
|
|
|
{&SetSystemTimeCallback, L"\\Callback\\SetSystemTime"},
|
|
|
|
{&SetSystemStateCallback, L"\\Callback\\SetSystemState"},
|
|
|
|
{&PowerStateCallback, L"\\Callback\\PowerState"},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
POBJECT_TYPE ExCallbackObjectType;
|
|
|
|
KEVENT ExpCallbackEvent;
|
2007-01-16 14:20:19 +00:00
|
|
|
EX_PUSH_LOCK ExpCallBackFlush;
|
2005-06-25 17:01:17 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
2007-01-16 05:31:05 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2007-01-16 07:20:16 +00:00
|
|
|
ExInitializeCallBack(IN OUT PEX_CALLBACK Callback)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Initialize the fast reference */
|
|
|
|
ExInitializeFastReference(&Callback->RoutineBlock, NULL);
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PEX_CALLBACK_ROUTINE_BLOCK
|
|
|
|
NTAPI
|
|
|
|
ExAllocateCallBack(IN PEX_CALLBACK_FUNCTION Function,
|
|
|
|
IN PVOID Context)
|
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock;
|
2007-01-16 05:31:05 +00:00
|
|
|
|
|
|
|
/* Allocate a callback */
|
2009-03-03 20:24:10 +00:00
|
|
|
CallbackBlock = ExAllocatePoolWithTag(PagedPool,
|
|
|
|
sizeof(EX_CALLBACK_ROUTINE_BLOCK),
|
2012-06-14 22:06:24 +00:00
|
|
|
TAG_CALLBACK_ROUTINE_BLOCK);
|
2009-03-03 20:24:10 +00:00
|
|
|
if (CallbackBlock)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
|
|
|
/* Initialize it */
|
2009-03-03 20:24:10 +00:00
|
|
|
CallbackBlock->Function = Function;
|
|
|
|
CallbackBlock->Context = Context;
|
|
|
|
ExInitializeRundownProtection(&CallbackBlock->RundownProtect);
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return it */
|
2009-03-03 20:24:10 +00:00
|
|
|
return CallbackBlock;
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-03-03 20:24:10 +00:00
|
|
|
ExFreeCallBack(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
|
|
|
/* Just free it from memory */
|
2012-06-14 22:06:24 +00:00
|
|
|
ExFreePoolWithTag(CallbackBlock, TAG_CALLBACK_ROUTINE_BLOCK);
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-03-03 20:24:10 +00:00
|
|
|
ExWaitForCallBacks(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
|
|
|
/* Wait on the rundown */
|
2009-03-03 20:24:10 +00:00
|
|
|
ExWaitForRundownProtectionRelease(&CallbackBlock->RundownProtect);
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PEX_CALLBACK_FUNCTION
|
|
|
|
NTAPI
|
2009-03-03 20:24:10 +00:00
|
|
|
ExGetCallBackBlockRoutine(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
|
|
|
/* Return the function */
|
2009-03-03 20:24:10 +00:00
|
|
|
return CallbackBlock->Function;
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PVOID
|
|
|
|
NTAPI
|
2009-03-03 20:24:10 +00:00
|
|
|
ExGetCallBackBlockContext(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
|
|
|
/* Return the context */
|
2009-03-03 20:24:10 +00:00
|
|
|
return CallbackBlock->Context;
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExDereferenceCallBackBlock(IN OUT PEX_CALLBACK CallBack,
|
2009-03-03 20:24:10 +00:00
|
|
|
IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
|
2007-01-16 05:31:05 +00:00
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Release a fast reference */
|
|
|
|
if (!ExReleaseFastReference(&CallBack->RoutineBlock, CallbackBlock))
|
2007-01-16 07:20:16 +00:00
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Take slow path */
|
|
|
|
ExReleaseRundownProtection(&CallbackBlock->RundownProtect);
|
2007-01-16 07:20:16 +00:00
|
|
|
}
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PEX_CALLBACK_ROUTINE_BLOCK
|
|
|
|
NTAPI
|
|
|
|
ExReferenceCallBackBlock(IN OUT PEX_CALLBACK CallBack)
|
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
EX_FAST_REF OldValue;
|
|
|
|
ULONG_PTR Count;
|
|
|
|
PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock;
|
2012-06-14 22:06:24 +00:00
|
|
|
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Acquire a reference */
|
|
|
|
OldValue = ExAcquireFastReference(&CallBack->RoutineBlock);
|
|
|
|
Count = ExGetCountFastReference(OldValue);
|
2007-01-16 07:20:16 +00:00
|
|
|
|
|
|
|
/* Fail if there isn't any object */
|
2009-03-03 20:24:10 +00:00
|
|
|
if (!ExGetObjectFastReference(OldValue)) return NULL;
|
2007-01-16 07:20:16 +00:00
|
|
|
|
|
|
|
/* Check if we don't have a reference */
|
2009-03-03 20:24:10 +00:00
|
|
|
if (!Count)
|
2007-01-16 07:20:16 +00:00
|
|
|
{
|
|
|
|
/* FIXME: Race */
|
|
|
|
DPRINT1("Unhandled callback race condition\n");
|
2008-08-24 15:48:05 +00:00
|
|
|
ASSERT(FALSE);
|
2009-03-03 20:24:10 +00:00
|
|
|
return NULL;
|
2007-01-16 07:20:16 +00:00
|
|
|
}
|
2012-06-14 22:06:24 +00:00
|
|
|
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Get the callback block */
|
|
|
|
CallbackBlock = ExGetObjectFastReference(OldValue);
|
2012-06-14 22:06:24 +00:00
|
|
|
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Check if this is the last reference */
|
|
|
|
if (Count == 1)
|
2007-01-16 07:20:16 +00:00
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Acquire rundown protection */
|
|
|
|
if (ExfAcquireRundownProtectionEx(&CallbackBlock->RundownProtect,
|
|
|
|
MAX_FAST_REFS))
|
2007-01-16 07:20:16 +00:00
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Insert references */
|
|
|
|
if (!ExInsertFastReference(&CallBack->RoutineBlock, CallbackBlock))
|
2007-01-16 07:20:16 +00:00
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Backdown the rundown acquire */
|
|
|
|
ExfReleaseRundownProtectionEx(&CallbackBlock->RundownProtect,
|
|
|
|
MAX_FAST_REFS);
|
2007-01-16 07:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the callback block */
|
2009-03-03 20:24:10 +00:00
|
|
|
return CallbackBlock;
|
2007-01-16 05:31:05 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 14:20:19 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
ExCompareExchangeCallBack(IN OUT PEX_CALLBACK CallBack,
|
|
|
|
IN PEX_CALLBACK_ROUTINE_BLOCK NewBlock,
|
|
|
|
IN PEX_CALLBACK_ROUTINE_BLOCK OldBlock)
|
|
|
|
{
|
2009-03-03 20:24:10 +00:00
|
|
|
EX_FAST_REF OldValue;
|
|
|
|
PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock;
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
ULONG Count;
|
2007-01-16 14:20:19 +00:00
|
|
|
|
|
|
|
/* Check that we have a new block */
|
|
|
|
if (NewBlock)
|
|
|
|
{
|
|
|
|
/* Acquire rundown */
|
|
|
|
if (!ExfAcquireRundownProtectionEx(&NewBlock->RundownProtect,
|
|
|
|
MAX_FAST_REFS + 1))
|
|
|
|
{
|
|
|
|
/* This should never happen */
|
2018-07-04 01:42:04 +00:00
|
|
|
ASSERTMSG("Callback block is already undergoing rundown\n", FALSE);
|
2007-01-16 14:20:19 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
[HAL/NDK]
- Make Vector parameter in HalEnableSystemInterrupt, HalDisableSystemInterrupt and HalBeginSystemInterrupt an ULONG, not an UCHAR
[NDK]
- 64bit fixes for HANDLE_TABLE, KPROCESS, SECTION_IMAGE_INFORMATION, MMADDRESS_LIST, MMVAD_FLAGS, MMVAD, MMVAD_LONG, MMVAD_SHORT, MEMORY_DESCRIPTOR, MEMORY_ALLOCATION_DESCRIPTOR, LdrVerifyMappedImageMatchesChecksum
- KDPC_DATA::DpcQueueDepth is signed on amd64, unsigned on x86
[NTOSKRNL]
- Fix hundreds of MSVC and amd64 warnings
- add a pragma message to FstubFixupEfiPartition, since it looks broken
- Move portable Ke constants from <arch>/cpu.c to krnlinit.c
- Fixed a bug in amd64 KiGeneralProtectionFaultHandler
svn path=/trunk/; revision=53734
2011-09-18 13:11:45 +00:00
|
|
|
|
2009-03-03 20:24:10 +00:00
|
|
|
/* Do the swap */
|
|
|
|
OldValue = ExCompareSwapFastReference(&CallBack->RoutineBlock,
|
|
|
|
NewBlock,
|
|
|
|
OldBlock);
|
2007-01-16 14:20:19 +00:00
|
|
|
|
|
|
|
/* Get the routine block */
|
2009-03-03 20:24:10 +00:00
|
|
|
CallbackBlock = ExGetObjectFastReference(OldValue);
|
|
|
|
Count = ExGetCountFastReference(OldValue);
|
2007-01-16 14:20:19 +00:00
|
|
|
|
|
|
|
/* Make sure the swap worked */
|
2009-03-03 20:24:10 +00:00
|
|
|
if (CallbackBlock == OldBlock)
|
2007-01-16 14:20:19 +00:00
|
|
|
{
|
|
|
|
/* Make sure we replaced a valid pointer */
|
2009-03-03 20:24:10 +00:00
|
|
|
if (CallbackBlock)
|
2007-01-16 14:20:19 +00:00
|
|
|
{
|
|
|
|
/* Acquire the flush lock and immediately release it */
|
|
|
|
KeEnterCriticalRegion();
|
|
|
|
ExWaitOnPushLock(&ExpCallBackFlush);
|
|
|
|
|
|
|
|
/* Release rundown protection */
|
|
|
|
KeLeaveCriticalRegion();
|
2009-03-03 20:24:10 +00:00
|
|
|
ExfReleaseRundownProtectionEx(&CallbackBlock->RundownProtect,
|
|
|
|
Count + 1);
|
2007-01-16 14:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare worked */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It failed, check if we had a block */
|
|
|
|
if (NewBlock)
|
|
|
|
{
|
2015-04-24 19:40:38 +00:00
|
|
|
/* We did, remove the references that we had added */
|
2007-01-16 14:20:19 +00:00
|
|
|
ExfReleaseRundownProtectionEx(&NewBlock->RundownProtect,
|
|
|
|
MAX_FAST_REFS + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return failure */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExpDeleteCallback(IN PVOID Object)
|
|
|
|
{
|
|
|
|
/* Sanity check */
|
|
|
|
ASSERT(IsListEmpty(&((PCALLBACK_OBJECT)Object)->RegisteredCallbacks));
|
|
|
|
}
|
1998-08-28 23:24:25 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/*++
|
|
|
|
* @name ExpInitializeCallbacks
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
|
|
|
* Creates the Callback Object as a valid Object Type in the Kernel.
|
2006-06-28 13:53:30 +00:00
|
|
|
* Internal function, subject to further review
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @return TRUE if the Callback Object Type was successfully created.
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @remarks None
|
|
|
|
*
|
|
|
|
*--*/
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2020-05-23 13:56:10 +00:00
|
|
|
BOOLEAN
|
2006-06-28 13:53:30 +00:00
|
|
|
NTAPI
|
2004-06-19 08:48:01 +00:00
|
|
|
ExpInitializeCallbacks(VOID)
|
|
|
|
{
|
2006-06-28 13:53:30 +00:00
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
|
|
|
NTSTATUS Status;
|
|
|
|
UNICODE_STRING DirName = RTL_CONSTANT_STRING(L"\\Callback");
|
|
|
|
UNICODE_STRING CallbackName;
|
|
|
|
UNICODE_STRING Name;
|
|
|
|
OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
|
|
|
|
HANDLE DirectoryHandle;
|
|
|
|
ULONG i;
|
|
|
|
|
2007-01-16 14:20:19 +00:00
|
|
|
/* Setup lightweight callback lock */
|
|
|
|
ExpCallBackFlush.Value = 0;
|
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Initialize the Callback Object type */
|
2006-06-28 13:53:30 +00:00
|
|
|
RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
|
|
|
|
RtlInitUnicodeString(&Name, L"Callback");
|
|
|
|
ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
|
2007-01-16 04:16:31 +00:00
|
|
|
ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
|
2006-06-28 13:53:30 +00:00
|
|
|
ObjectTypeInitializer.GenericMapping = ExpCallbackMapping;
|
|
|
|
ObjectTypeInitializer.PoolType = NonPagedPool;
|
2007-01-16 04:16:31 +00:00
|
|
|
ObjectTypeInitializer.DeleteProcedure = ExpDeleteCallback;
|
|
|
|
ObjectTypeInitializer.ValidAccessMask = CALLBACK_ALL_ACCESS;
|
|
|
|
Status = ObCreateObjectType(&Name,
|
|
|
|
&ObjectTypeInitializer,
|
|
|
|
NULL,
|
|
|
|
&ExCallbackObjectType);
|
|
|
|
if (!NT_SUCCESS(Status)) return FALSE;
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Initialize the Object */
|
2007-01-16 04:16:31 +00:00
|
|
|
InitializeObjectAttributes(&ObjectAttributes,
|
|
|
|
&DirName,
|
|
|
|
OBJ_CASE_INSENSITIVE | OBJ_PERMANENT,
|
|
|
|
NULL,
|
|
|
|
SePublicDefaultSd);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Create the Object Directory */
|
2007-01-16 04:16:31 +00:00
|
|
|
Status = NtCreateDirectoryObject(&DirectoryHandle,
|
|
|
|
DIRECTORY_ALL_ACCESS,
|
|
|
|
&ObjectAttributes);
|
|
|
|
if (!NT_SUCCESS(Status)) return FALSE;
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Close Handle... */
|
|
|
|
NtClose(DirectoryHandle);
|
|
|
|
|
|
|
|
/* Initialize Event used when unregistering */
|
|
|
|
KeInitializeEvent(&ExpCallbackEvent, NotificationEvent, 0);
|
|
|
|
|
|
|
|
/* Default NT Kernel Callbacks. */
|
2007-01-16 04:16:31 +00:00
|
|
|
for (i = 0; ExpInitializeCallback[i].CallbackObject; i++)
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
|
|
|
/* Create the name from the structure */
|
|
|
|
RtlInitUnicodeString(&CallbackName, ExpInitializeCallback[i].Name);
|
|
|
|
|
|
|
|
/* Initialize the Object Attributes Structure */
|
2007-01-16 04:16:31 +00:00
|
|
|
InitializeObjectAttributes(&ObjectAttributes,
|
|
|
|
&CallbackName,
|
|
|
|
OBJ_PERMANENT | OBJ_CASE_INSENSITIVE,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Create the Callback Object */
|
2007-01-16 04:16:31 +00:00
|
|
|
Status = ExCreateCallback(ExpInitializeCallback[i].CallbackObject,
|
|
|
|
&ObjectAttributes,
|
|
|
|
TRUE,
|
|
|
|
TRUE);
|
|
|
|
if (!NT_SUCCESS(Status)) return FALSE;
|
2006-06-28 13:53:30 +00:00
|
|
|
}
|
2007-01-16 04:16:31 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Everything successful */
|
2007-01-16 04:16:31 +00:00
|
|
|
return TRUE;
|
2004-06-19 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/*++
|
|
|
|
* @name ExCreateCallback
|
|
|
|
* @implemented
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
|
|
|
* Opens or creates a Callback Object. Creates only if Create is true.
|
2015-04-24 19:40:38 +00:00
|
|
|
* Allows multiple Callback Functions to be registered only if
|
2006-06-28 13:53:30 +00:00
|
|
|
* AllowMultipleCallbacks is true.
|
|
|
|
* See: http://www.osronline.com/ddkx/kmarch/k102_967m.htm
|
|
|
|
* http://www.osronline.com/article.cfm?id=24
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallbackObject
|
|
|
|
* Pointer that will receive the Callback Object.
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallbackName
|
|
|
|
* Name of Callback
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param Create
|
|
|
|
* Determines if the object will be created if it doesn't exit
|
|
|
|
*
|
|
|
|
* @param AllowMultipleCallbacks
|
|
|
|
* Determines if more then one registered callback function
|
|
|
|
* can be attached to this Callback Object.
|
|
|
|
*
|
|
|
|
* @return STATUS_SUCESS if not failed.
|
|
|
|
*
|
|
|
|
* @remarks Must be called at IRQL = PASSIVE_LEVEL
|
|
|
|
*
|
|
|
|
*--*/
|
2000-07-04 01:30:18 +00:00
|
|
|
NTSTATUS
|
2006-06-28 13:53:30 +00:00
|
|
|
NTAPI
|
|
|
|
ExCreateCallback(OUT PCALLBACK_OBJECT *CallbackObject,
|
|
|
|
IN POBJECT_ATTRIBUTES ObjectAttributes,
|
|
|
|
IN BOOLEAN Create,
|
|
|
|
IN BOOLEAN AllowMultipleCallbacks)
|
2000-07-04 01:30:18 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
PCALLBACK_OBJECT Callback = NULL;
|
|
|
|
NTSTATUS Status;
|
|
|
|
HANDLE Handle = NULL;
|
2006-06-28 13:53:30 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Open a handle to the callback if it exists */
|
|
|
|
if (ObjectAttributes->ObjectName)
|
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Open the handle */
|
2006-06-28 13:53:30 +00:00
|
|
|
Status = ObOpenObjectByName(ObjectAttributes,
|
|
|
|
ExCallbackObjectType,
|
|
|
|
KernelMode,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&Handle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Otherwise, fail */
|
2006-06-28 13:53:30 +00:00
|
|
|
Status = STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We weren't able to open it...should we create it? */
|
2007-01-16 04:16:31 +00:00
|
|
|
if (!(NT_SUCCESS(Status)) && (Create))
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Create the object */
|
2006-06-28 13:53:30 +00:00
|
|
|
Status = ObCreateObject(KernelMode,
|
|
|
|
ExCallbackObjectType,
|
|
|
|
ObjectAttributes,
|
|
|
|
KernelMode,
|
|
|
|
NULL,
|
|
|
|
sizeof(CALLBACK_OBJECT),
|
|
|
|
0,
|
|
|
|
0,
|
2007-01-16 04:16:31 +00:00
|
|
|
(PVOID *)&Callback);
|
|
|
|
if (NT_SUCCESS(Status))
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Set it up */
|
2009-08-24 18:19:53 +00:00
|
|
|
Callback->Signature = 'llaC';
|
2007-01-16 04:16:31 +00:00
|
|
|
KeInitializeSpinLock(&Callback->Lock);
|
|
|
|
InitializeListHead(&Callback->RegisteredCallbacks);
|
|
|
|
Callback->AllowMultipleCallbacks = AllowMultipleCallbacks;
|
|
|
|
|
|
|
|
/* Insert the object into the object namespace */
|
2006-06-28 13:53:30 +00:00
|
|
|
Status = ObInsertObject(Callback,
|
|
|
|
NULL,
|
|
|
|
FILE_READ_DATA,
|
|
|
|
0,
|
|
|
|
NULL,
|
2007-01-16 04:16:31 +00:00
|
|
|
&Handle);
|
2006-06-28 13:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Check if we have success until here */
|
|
|
|
if (NT_SUCCESS(Status))
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
|
|
|
/* Get a pointer to the new object from the handle we just got */
|
|
|
|
Status = ObReferenceObjectByHandle(Handle,
|
|
|
|
0,
|
|
|
|
ExCallbackObjectType,
|
|
|
|
KernelMode,
|
2012-06-14 22:10:34 +00:00
|
|
|
(PVOID *)&Callback,
|
2006-06-28 13:53:30 +00:00
|
|
|
NULL);
|
2007-01-16 04:16:31 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Close the Handle, since we now have the pointer */
|
|
|
|
ZwClose(Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Everything went fine, so return a pointer to the Object */
|
2012-12-19 23:49:13 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
*CallbackObject = Callback;
|
|
|
|
}
|
2006-06-28 13:53:30 +00:00
|
|
|
return Status;
|
2000-07-04 01:30:18 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/*++
|
|
|
|
* @name ExNotifyCallback
|
|
|
|
* @implemented
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
|
|
|
* Calls a function pointer (a registered callback)
|
2006-06-28 13:53:30 +00:00
|
|
|
* See: http://www.osronline.com/ddkx/kmarch/k102_2f5e.htm
|
|
|
|
* http://vmsone.com/~decuslib/vmssig/vmslt99b/nt/wdm-callback.txt
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallbackObject
|
|
|
|
* Which callback to call
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param Argument1
|
|
|
|
* Pointer/data to send to callback function
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param Argument2
|
|
|
|
* Pointer/data to send to callback function
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*
|
|
|
|
* @remarks None
|
|
|
|
*
|
|
|
|
*--*/
|
2000-07-04 01:30:18 +00:00
|
|
|
VOID
|
2006-06-28 13:53:30 +00:00
|
|
|
NTAPI
|
2007-01-16 04:16:31 +00:00
|
|
|
ExNotifyCallback(IN PCALLBACK_OBJECT CallbackObject,
|
2006-06-28 13:53:30 +00:00
|
|
|
IN PVOID Argument1,
|
|
|
|
IN PVOID Argument2)
|
2000-07-04 01:30:18 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
PLIST_ENTRY RegisteredCallbacks;
|
|
|
|
PCALLBACK_REGISTRATION CallbackRegistration;
|
|
|
|
KIRQL OldIrql;
|
|
|
|
|
|
|
|
/* Check if we don't have an object or registrations */
|
|
|
|
if (!(CallbackObject) ||
|
|
|
|
(IsListEmpty(&CallbackObject->RegisteredCallbacks)))
|
|
|
|
{
|
|
|
|
/* Don't notify */
|
|
|
|
return;
|
|
|
|
}
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Acquire the Lock */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeAcquireSpinLock(&CallbackObject->Lock, &OldIrql);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Enumerate through all the registered functions */
|
|
|
|
for (RegisteredCallbacks = CallbackObject->RegisteredCallbacks.Flink;
|
2004-06-19 08:48:01 +00:00
|
|
|
RegisteredCallbacks != &CallbackObject->RegisteredCallbacks;
|
|
|
|
RegisteredCallbacks = RegisteredCallbacks->Flink)
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
|
|
|
/* Get a pointer to a Callback Registration from the List Entries */
|
|
|
|
CallbackRegistration = CONTAINING_RECORD(RegisteredCallbacks,
|
|
|
|
CALLBACK_REGISTRATION,
|
2007-01-16 04:16:31 +00:00
|
|
|
Link);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Don't bother doing notification if it's pending to be deleted */
|
|
|
|
if (!CallbackRegistration->UnregisterWaiting)
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Mark the Callback in use, so it won't get deleted */
|
|
|
|
CallbackRegistration->Busy += 1;
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Release the Spinlock before making the call */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeReleaseSpinLock(&CallbackObject->Lock, OldIrql);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Call the Registered Function */
|
2007-01-16 04:16:31 +00:00
|
|
|
CallbackRegistration->CallbackFunction(CallbackRegistration->
|
|
|
|
CallbackContext,
|
|
|
|
Argument1,
|
|
|
|
Argument2);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Get SpinLock back */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeAcquireSpinLock(&CallbackObject->Lock, &OldIrql);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* We are not in use anymore */
|
2007-01-16 04:16:31 +00:00
|
|
|
CallbackRegistration->Busy -= 1;
|
2006-06-28 13:53:30 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Check if removal is pending and we're not active */
|
|
|
|
if ((CallbackRegistration->UnregisterWaiting) &&
|
|
|
|
!(CallbackRegistration->Busy))
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Signal the callback event */
|
2006-06-28 13:53:30 +00:00
|
|
|
KeSetEvent(&ExpCallbackEvent, 0, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-16 04:16:31 +00:00
|
|
|
|
|
|
|
/* Release the Callback Object */
|
|
|
|
KeReleaseSpinLock(&CallbackObject->Lock, OldIrql);
|
2000-07-04 01:30:18 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/*++
|
|
|
|
* @name ExRegisterCallback
|
|
|
|
* @implemented
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* Allows a function to associate a callback pointer (Function) to
|
|
|
|
* a created Callback object
|
|
|
|
* See: DDK, OSR, links in ExNotifyCallback
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallbackObject
|
|
|
|
* The Object Created with ExCreateCallBack
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallBackFunction
|
|
|
|
* Pointer to the function to be called back
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallBackContext
|
|
|
|
* Block of memory that can contain user-data which will be
|
|
|
|
* passed on to the callback
|
|
|
|
*
|
|
|
|
* @return A handle to a Callback Registration Structure (MSDN Documentation)
|
|
|
|
*
|
|
|
|
* @remarks None
|
|
|
|
*
|
|
|
|
*--*/
|
2000-07-04 01:30:18 +00:00
|
|
|
PVOID
|
2006-06-28 13:53:30 +00:00
|
|
|
NTAPI
|
2007-01-16 04:16:31 +00:00
|
|
|
ExRegisterCallback(IN PCALLBACK_OBJECT CallbackObject,
|
2006-06-28 13:53:30 +00:00
|
|
|
IN PCALLBACK_FUNCTION CallbackFunction,
|
|
|
|
IN PVOID CallbackContext)
|
2000-07-04 01:30:18 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
PCALLBACK_REGISTRATION CallbackRegistration = NULL;
|
|
|
|
KIRQL OldIrql;
|
2006-06-28 13:53:30 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Sanity checks */
|
|
|
|
ASSERT(CallbackFunction);
|
|
|
|
ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Create reference to Callback Object */
|
2007-01-16 04:16:31 +00:00
|
|
|
ObReferenceObject(CallbackObject);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Allocate memory for the structure */
|
2007-01-16 04:16:31 +00:00
|
|
|
CallbackRegistration = ExAllocatePoolWithTag(NonPagedPool,
|
|
|
|
sizeof(CALLBACK_REGISTRATION),
|
2012-06-14 22:06:24 +00:00
|
|
|
TAG_CALLBACK_REGISTRATION);
|
2007-01-16 04:16:31 +00:00
|
|
|
if (!CallbackRegistration)
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Dereference and fail */
|
2006-06-28 13:53:30 +00:00
|
|
|
ObDereferenceObject (CallbackObject);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create Callback Registration */
|
2007-01-16 04:16:31 +00:00
|
|
|
CallbackRegistration->CallbackObject = CallbackObject;
|
|
|
|
CallbackRegistration->CallbackFunction = CallbackFunction;
|
|
|
|
CallbackRegistration->CallbackContext = CallbackContext;
|
|
|
|
CallbackRegistration->Busy = 0;
|
|
|
|
CallbackRegistration->UnregisterWaiting = FALSE;
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Acquire SpinLock */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeAcquireSpinLock(&CallbackObject->Lock, &OldIrql);
|
2006-06-28 13:53:30 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Check if 1) No Callbacks registered or 2) Multiple Callbacks allowed */
|
|
|
|
if ((CallbackObject->AllowMultipleCallbacks) ||
|
|
|
|
(IsListEmpty(&CallbackObject->RegisteredCallbacks)))
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Register the callback */
|
|
|
|
InsertTailList(&CallbackObject->RegisteredCallbacks,
|
|
|
|
&CallbackRegistration->Link);
|
|
|
|
|
|
|
|
/* Release SpinLock */
|
|
|
|
KeReleaseSpinLock(&CallbackObject->Lock, OldIrql);
|
2006-06-28 13:53:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Release SpinLock */
|
|
|
|
KeReleaseSpinLock(&CallbackObject->Lock, OldIrql);
|
|
|
|
|
|
|
|
/* Free the registration */
|
2012-06-14 22:06:24 +00:00
|
|
|
ExFreePoolWithTag(CallbackRegistration, TAG_CALLBACK_REGISTRATION);
|
2006-06-28 13:53:30 +00:00
|
|
|
CallbackRegistration = NULL;
|
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Dereference the object */
|
|
|
|
ObDereferenceObject(CallbackObject);
|
|
|
|
}
|
2006-06-28 13:53:30 +00:00
|
|
|
|
|
|
|
/* Return handle to Registration Object */
|
|
|
|
return (PVOID)CallbackRegistration;
|
2000-07-04 01:30:18 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/*++
|
|
|
|
* @name ExUnregisterCallback
|
|
|
|
* @implemented
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
|
|
|
* Deregisters a CallBack
|
2006-06-28 13:53:30 +00:00
|
|
|
* See: DDK, OSR, links in ExNotifyCallback
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @param CallbackRegistration
|
|
|
|
* Callback Registration Handle
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @return None
|
2004-06-19 08:48:01 +00:00
|
|
|
*
|
2006-06-28 13:53:30 +00:00
|
|
|
* @remarks None
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExUnregisterCallback(IN PVOID CallbackRegistrationHandle)
|
2000-07-04 01:30:18 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
PCALLBACK_REGISTRATION CallbackRegistration;
|
|
|
|
PCALLBACK_OBJECT CallbackObject;
|
|
|
|
KIRQL OldIrql;
|
|
|
|
ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Convert Handle to valid Structure Pointer */
|
2007-01-16 04:16:31 +00:00
|
|
|
CallbackRegistration = (PCALLBACK_REGISTRATION)CallbackRegistrationHandle;
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Get the Callback Object */
|
|
|
|
CallbackObject = CallbackRegistration->CallbackObject;
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Lock the Object */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeAcquireSpinLock (&CallbackObject->Lock, &OldIrql);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2007-01-16 04:16:31 +00:00
|
|
|
/* We can't Delete the Callback if it's in use */
|
|
|
|
while (CallbackRegistration->Busy)
|
2006-06-28 13:53:30 +00:00
|
|
|
{
|
2007-01-16 04:16:31 +00:00
|
|
|
/* Let everyone else know we're unregistering */
|
|
|
|
CallbackRegistration->UnregisterWaiting = TRUE;
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* We are going to wait for the event, so the Lock isn't necessary */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeReleaseSpinLock(&CallbackObject->Lock, OldIrql);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Make sure the event is cleared */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeClearEvent(&ExpCallbackEvent);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Wait for the Event */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeWaitForSingleObject(&ExpCallbackEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* We need the Lock again */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeAcquireSpinLock(&CallbackObject->Lock, &OldIrql);
|
2006-06-28 13:53:30 +00:00
|
|
|
}
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Remove the Callback */
|
2007-01-16 04:16:31 +00:00
|
|
|
RemoveEntryList(&CallbackRegistration->Link);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* It's now safe to release the lock */
|
2007-01-16 04:16:31 +00:00
|
|
|
KeReleaseSpinLock(&CallbackObject->Lock, OldIrql);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Delete this registration */
|
2012-06-14 22:06:24 +00:00
|
|
|
ExFreePoolWithTag(CallbackRegistration, TAG_CALLBACK_REGISTRATION);
|
2004-06-19 08:48:01 +00:00
|
|
|
|
2006-06-28 13:53:30 +00:00
|
|
|
/* Remove the reference */
|
|
|
|
ObDereferenceObject(CallbackObject);
|
2000-07-04 01:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|