[NULL] Additions for the Null driver.

- Allow the driver to be unloaded, as on Windows.
- Use a static fast IO dispatch table, instead of allocating one and
  risking any potential memory allocation failure.
- Update the file header.
This commit is contained in:
Hermès Bélusca-Maïto 2018-04-22 22:13:52 +02:00
parent 63b6df6bc5
commit da5dcdcbf2
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -1,16 +1,19 @@
/* /*
* PROJECT: ReactOS Kernel * PROJECT: ReactOS Kernel
* LICENSE: GPL - See COPYING in the top level directory * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* FILE: drivers/base/null/null.c * PURPOSE: Null Device Driver
* PURPOSE: Null Device Driver * COPYRIGHT: Copyright 1998-2018 David Welch (welch@mcmail.com)
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) * Copyright 2007-2018 Alex Ionescu (alex.ionescu@reactos.org)
* David Welch (welch@mcmail.com)
*/ */
/* INCLUDES ******************************************************************/ /* INCLUDES ******************************************************************/
#include <wdm.h> #include <wdm.h>
/* GLOBALS *******************************************************************/
FAST_IO_DISPATCH FastIoDispatch;
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
NTSTATUS NTSTATUS
@ -20,6 +23,7 @@ NullQueryFileInformation(OUT PVOID Buffer,
IN FILE_INFORMATION_CLASS InformationClass) IN FILE_INFORMATION_CLASS InformationClass)
{ {
PFILE_STANDARD_INFORMATION StandardInfo = Buffer; PFILE_STANDARD_INFORMATION StandardInfo = Buffer;
PAGED_CODE(); PAGED_CODE();
/* We only support one class */ /* We only support one class */
@ -81,10 +85,11 @@ NTAPI
NullDispatch(IN PDEVICE_OBJECT DeviceObject, NullDispatch(IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp) IN PIRP Irp)
{ {
PIO_STACK_LOCATION IoStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS Status; NTSTATUS Status;
PIO_STACK_LOCATION IoStack = IoGetCurrentIrpStackLocation(Irp);
PFILE_OBJECT FileObject; PFILE_OBJECT FileObject;
ULONG Length; ULONG Length;
PAGED_CODE(); PAGED_CODE();
/* Get the file object and check what kind of request this is */ /* Get the file object and check what kind of request this is */
@ -150,21 +155,33 @@ NullDispatch(IN PDEVICE_OBJECT DeviceObject,
return Status; return Status;
} }
VOID
NTAPI
NullUnload(IN PDRIVER_OBJECT DriverObject)
{
PDEVICE_OBJECT DeviceObject = DriverObject->DeviceObject;
/* Delete the Null device */
IoDeleteDevice(DeviceObject);
}
NTSTATUS NTSTATUS
NTAPI NTAPI
DriverEntry(IN PDRIVER_OBJECT DriverObject, DriverEntry(IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath) IN PUNICODE_STRING RegistryPath)
{ {
NTSTATUS Status;
PDEVICE_OBJECT DeviceObject; PDEVICE_OBJECT DeviceObject;
UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\Null"); UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\Null");
NTSTATUS Status;
PFAST_IO_DISPATCH FastIoDispatch;
PAGED_CODE(); PAGED_CODE();
UNREFERENCED_PARAMETER(RegistryPath);
/* Page the driver */ /* Page the driver */
MmPageEntireDriver(DriverEntry); MmPageEntireDriver(DriverEntry);
/* Create null device */ /* Create the Null device */
Status = IoCreateDevice(DriverObject, Status = IoCreateDevice(DriverObject,
0, 0,
&DeviceName, &DeviceName,
@ -172,7 +189,8 @@ DriverEntry(IN PDRIVER_OBJECT DriverObject,
0, 0,
FALSE, FALSE,
&DeviceObject); &DeviceObject);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status))
return Status;
/* Register driver routines */ /* Register driver routines */
DriverObject->MajorFunction[IRP_MJ_CLOSE] = NullDispatch; DriverObject->MajorFunction[IRP_MJ_CLOSE] = NullDispatch;
@ -181,26 +199,16 @@ DriverEntry(IN PDRIVER_OBJECT DriverObject,
DriverObject->MajorFunction[IRP_MJ_READ] = NullDispatch; DriverObject->MajorFunction[IRP_MJ_READ] = NullDispatch;
DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] = NullDispatch; DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL] = NullDispatch;
DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = NullDispatch; DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = NullDispatch;
DriverObject->DriverUnload = NullUnload;
/* Allocate the fast I/O dispatch table */ /* Initialize the fast I/O dispatch table */
FastIoDispatch = ExAllocatePoolWithTag(NonPagedPool, RtlZeroMemory(&FastIoDispatch, sizeof(FastIoDispatch));
sizeof(FAST_IO_DISPATCH), FastIoDispatch.SizeOfFastIoDispatch = sizeof(FastIoDispatch);
'llun');
if (!FastIoDispatch)
{
/* Failed, cleanup */
IoDeleteDevice(DeviceObject);
return STATUS_INSUFFICIENT_RESOURCES;
}
/* Initialize it */
RtlZeroMemory(FastIoDispatch, sizeof(FAST_IO_DISPATCH));
FastIoDispatch->SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
/* Setup our pointers */ /* Setup our pointers */
FastIoDispatch->FastIoRead = NullRead; FastIoDispatch.FastIoRead = NullRead;
FastIoDispatch->FastIoWrite = NullWrite; FastIoDispatch.FastIoWrite = NullWrite;
DriverObject->FastIoDispatch = FastIoDispatch; DriverObject->FastIoDispatch = &FastIoDispatch;
/* Return success */ /* Return success */
return STATUS_SUCCESS; return STATUS_SUCCESS;