Good afternoon
I have a kmdf driver that can read and write to a file.
Through a custom application I can send IOCTL requests. If I send a request from one process to write, from another process to read, everything is fine. But if I send a read request and then a write request, both requests do not complete and seem to hang.
This is the loop I have in my custom application
do
{
printf("Enter command (read/write): ");
scanf("%s", command);
bufLength = 25;
if (strcmp(command, "read") == 0)
{
if (!DoFileRead(hDevice, bufLength))
{
break;
}
}
else if (strcmp(command, "write") == 0)
{
if (!DoFileWrite(hDevice, bufLength))
{
break;
}
}
}
WHILE(TRUE);
Handler code in the driver
VOID
FileEvtIoDeviceControl(
IN WDFQUEUE Queue,
IN WDFREQUEST Request,
IN size_t OutputBufferLength,
IN size_t InputBufferLength,
IN ULONG IoControlCode
)
{
NTSTATUS status = STATUS_SUCCESS;
NTSTATUS rstatus = STATUS_SUCCESS;
IO_STATUS_BLOCK ioStatus;
PCHAR inBuf = NULL;
PCHAR outBuf = NULL;
PCHAR data = "this String is from Device Driver !!!";
ULONG datalen = (ULONG) strlen(data)+1;
PCHAR buffer = NULL;
PREQUEST_CONTEXT reqContext = NULL;
size_t bufSize;
PCONTROL_DEVICE_EXTENSION devExt;
FILE_POSITION_INFORMATION position;
ULONG_PTR bytesWritten = 0;
ULONG_PTR bytesRead = 0;
WDFDEVICE device;
UNREFERENCED_PARAMETER( Queue );
PAGED_CODE();
switch (IoControlCode)
{
case IOCTL_WRITE:
status = WdfRequestRetrieveInputBuffer(Request, 0, &inBuf, &bufSize);
if(!NT_SUCCESS(status)) {
status = STATUS_INSUFFICIENT_RESOURCES;
break;
}
devExt = ControlGetData(WdfIoQueueGetDevice(Queue));
if(devExt->FileHandle) {
position.CurrentByteOffset.QuadPart = 0;
status = ZwSetInformationFile(devExt->FileHandle,
&ioStatus,
&position,
sizeof(FILE_POSITION_INFORMATION),
FilePositionInformation);
if (NT_SUCCESS(status))
{
status = ZwWriteFile(devExt->FileHandle, NULL,
NULL, NULL, &ioStatus, inBuf,
(ULONG)InputBufferLength, 0, NULL);
if (!NT_SUCCESS(status))
{
//...
}
status = ioStatus.Status;
bytesWritten = ioStatus.Information;
}
}
WdfRequestCompleteWithInformation(Request, status, bytesWritten);
if (devExt->PendingRequest) {
rstatus = WdfRequestRetrieveOutputBuffer(devExt->PendingRequest, 0, &outBuf, &bufSize);
if (!NT_SUCCESS(rstatus))
{
WdfRequestComplete(devExt->PendingRequest, rstatus);
return;
}
position.CurrentByteOffset.QuadPart = 0;
rstatus = ZwSetInformationFile(devExt->FileHandle,
&ioStatus,
&position,
sizeof(FILE_POSITION_INFORMATION),
FilePositionInformation);
if (NT_SUCCESS(rstatus))
{
rstatus = ZwReadFile(devExt->FileHandle,NULL,NULL,NULL,
&ioStatus,outBuf,(ULONG)OutputBufferLength,
0,NULL
);
if (!NT_SUCCESS(rstatus))
{
//...
}
else
{
WdfRequestCompleteWithInformation(devExt->PendingRequest, rstatus, ioStatus.Information);
devExt->isDataAdded = FALSE;
devExt->PendingRequest = NULL;
}
}
} else {
devExt->isDataAdded = TRUE;
}
return;
break;
case IOCTL_READ:
status = WdfRequestRetrieveOutputBuffer(Request, 0, &outBuf, &bufSize);
if(!NT_SUCCESS(status)) {
WdfRequestComplete(Request, status);
return;
}
devExt = ControlGetData(WdfIoQueueGetDevice(Queue));
if(devExt->FileHandle)
{
if (!devExt->isDataAdded)
{
if (!devExt->PendingRequest)
{
KdPrint(("FileEvtIoRead, data not available...n"));
devExt->PendingRequest = Request;
}
else
{
WdfRequestComplete(Request, STATUS_UNSUCCESSFUL);
}
return;
}
position.CurrentByteOffset.QuadPart = 0;
status = ZwSetInformationFile(devExt->FileHandle,
&ioStatus,
&position,
sizeof(FILE_POSITION_INFORMATION),
FilePositionInformation);
if (NT_SUCCESS(status)) {
status = ZwReadFile (devExt->FileHandle,NULL,NULL,NULL,
&ioStatus,outBuf,(ULONG)OutputBufferLength,
0,NULL
);
if (!NT_SUCCESS(status)) {
//...
}
else
{
devExt->isDataAdded = FALSE;
}
status = ioStatus.Status;
bytesRead = ioStatus.Information;
}
}
WdfRequestSetInformation(Request, bytesRead);
break;
default:
status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
WdfRequestComplete( Request, status);
}
Tried changing the end of the queries, but nothing works
Артём Варнавский is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.