[channels,parallel] unify IRP processing logging

This commit is contained in:
akallabeth 2024-08-23 10:25:09 +02:00
parent d81dd9d9ff
commit df04e4c888
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5

View File

@ -53,6 +53,7 @@
#include <freerdp/constants.h> #include <freerdp/constants.h>
#include <freerdp/channels/rdpdr.h> #include <freerdp/channels/rdpdr.h>
#include <freerdp/channels/log.h> #include <freerdp/channels/log.h>
#include <freerdp/utils/rdpdr_utils.h>
#define TAG CHANNELS_TAG("drive.client") #define TAG CHANNELS_TAG("drive.client")
@ -67,6 +68,7 @@ typedef struct
HANDLE thread; HANDLE thread;
wMessageQueue* queue; wMessageQueue* queue;
rdpContext* rdpcontext; rdpContext* rdpcontext;
wLog* log;
} PARALLEL_DEVICE; } PARALLEL_DEVICE;
/** /**
@ -166,7 +168,7 @@ static UINT parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
if (!buffer) if (!buffer)
{ {
WLog_ERR(TAG, "malloc failed!"); WLog_Print(parallel->log, WLOG_ERROR, "malloc failed!");
return CHANNEL_RC_NO_MEMORY; return CHANNEL_RC_NO_MEMORY;
} }
@ -190,7 +192,7 @@ static UINT parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
{ {
if (!Stream_EnsureRemainingCapacity(irp->output, Length)) if (!Stream_EnsureRemainingCapacity(irp->output, Length))
{ {
WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!"); WLog_Print(parallel->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
free(buffer); free(buffer);
return CHANNEL_RC_NO_MEMORY; return CHANNEL_RC_NO_MEMORY;
} }
@ -271,7 +273,7 @@ static UINT parallel_process_irp_device_control(PARALLEL_DEVICE* parallel, IRP*
*/ */
static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp) static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
{ {
UINT error = 0; UINT error = ERROR_INTERNAL_ERROR;
WINPR_ASSERT(parallel); WINPR_ASSERT(parallel);
WINPR_ASSERT(irp); WINPR_ASSERT(irp);
@ -279,57 +281,42 @@ static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
switch (irp->MajorFunction) switch (irp->MajorFunction)
{ {
case IRP_MJ_CREATE: case IRP_MJ_CREATE:
if ((error = parallel_process_irp_create(parallel, irp))) error = parallel_process_irp_create(parallel, irp);
{
WLog_ERR(TAG, "parallel_process_irp_create failed with error %" PRIu32 "!", error);
return error;
}
break; break;
case IRP_MJ_CLOSE: case IRP_MJ_CLOSE:
if ((error = parallel_process_irp_close(parallel, irp))) error = parallel_process_irp_close(parallel, irp);
{
WLog_ERR(TAG, "parallel_process_irp_close failed with error %" PRIu32 "!", error);
return error;
}
break; break;
case IRP_MJ_READ: case IRP_MJ_READ:
if ((error = parallel_process_irp_read(parallel, irp))) error = parallel_process_irp_read(parallel, irp);
{
WLog_ERR(TAG, "parallel_process_irp_read failed with error %" PRIu32 "!", error);
return error;
}
break; break;
case IRP_MJ_WRITE: case IRP_MJ_WRITE:
if ((error = parallel_process_irp_write(parallel, irp))) error = parallel_process_irp_write(parallel, irp);
{
WLog_ERR(TAG, "parallel_process_irp_write failed with error %" PRIu32 "!", error);
return error;
}
break; break;
case IRP_MJ_DEVICE_CONTROL: case IRP_MJ_DEVICE_CONTROL:
if ((error = parallel_process_irp_device_control(parallel, irp))) error = parallel_process_irp_device_control(parallel, irp);
{
WLog_ERR(TAG, "parallel_process_irp_device_control failed with error %" PRIu32 "!",
error);
return error;
}
break; break;
default: default:
irp->IoStatus = STATUS_NOT_SUPPORTED; irp->IoStatus = STATUS_NOT_SUPPORTED;
return irp->Complete(irp); error = irp->Complete(irp);
break;
} }
return CHANNEL_RC_OK; DWORD level = WLOG_TRACE;
if (error)
level = WLOG_WARN;
WLog_Print(parallel->log, level,
"[%s|0x%08" PRIx32 "] completed with %s [0x%08" PRIx32 "] (IoStatus %s [0x%08" PRIx32
"])",
rdpdr_irp_string(irp->MajorFunction), irp->MajorFunction, WTSErrorToString(error),
error, NtStatus2Tag(irp->IoStatus), irp->IoStatus);
return error;
} }
static DWORD WINAPI parallel_thread_func(LPVOID arg) static DWORD WINAPI parallel_thread_func(LPVOID arg)
@ -342,7 +329,7 @@ static DWORD WINAPI parallel_thread_func(LPVOID arg)
{ {
if (!MessageQueue_Wait(parallel->queue)) if (!MessageQueue_Wait(parallel->queue))
{ {
WLog_ERR(TAG, "MessageQueue_Wait failed!"); WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_Wait failed!");
error = ERROR_INTERNAL_ERROR; error = ERROR_INTERNAL_ERROR;
break; break;
} }
@ -350,7 +337,7 @@ static DWORD WINAPI parallel_thread_func(LPVOID arg)
wMessage message = { 0 }; wMessage message = { 0 };
if (!MessageQueue_Peek(parallel->queue, &message, TRUE)) if (!MessageQueue_Peek(parallel->queue, &message, TRUE))
{ {
WLog_ERR(TAG, "MessageQueue_Peek failed!"); WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_Peek failed!");
error = ERROR_INTERNAL_ERROR; error = ERROR_INTERNAL_ERROR;
break; break;
} }
@ -360,9 +347,11 @@ static DWORD WINAPI parallel_thread_func(LPVOID arg)
IRP* irp = (IRP*)message.wParam; IRP* irp = (IRP*)message.wParam;
if ((error = parallel_process_irp(parallel, irp))) error = parallel_process_irp(parallel, irp);
if (error)
{ {
WLog_ERR(TAG, "parallel_process_irp failed with error %" PRIu32 "!", error); WLog_Print(parallel->log, WLOG_ERROR,
"parallel_process_irp failed with error %" PRIu32 "!", error);
break; break;
} }
} }
@ -383,9 +372,11 @@ static UINT parallel_irp_request(DEVICE* device, IRP* irp)
{ {
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device; PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device;
WINPR_ASSERT(parallel);
if (!MessageQueue_Post(parallel->queue, NULL, 0, (void*)irp, NULL)) if (!MessageQueue_Post(parallel->queue, NULL, 0, (void*)irp, NULL))
{ {
WLog_ERR(TAG, "MessageQueue_Post failed!"); WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_Post failed!");
return ERROR_INTERNAL_ERROR; return ERROR_INTERNAL_ERROR;
} }
@ -399,21 +390,24 @@ static UINT parallel_irp_request(DEVICE* device, IRP* irp)
*/ */
static UINT parallel_free(DEVICE* device) static UINT parallel_free(DEVICE* device)
{ {
UINT error = 0;
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device; PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device;
if (!MessageQueue_PostQuit(parallel->queue, 0) || if (parallel)
(WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED))
{ {
error = GetLastError(); if (!MessageQueue_PostQuit(parallel->queue, 0) ||
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error); (WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED))
return error; {
} const UINT error = GetLastError();
WLog_Print(parallel->log, WLOG_ERROR,
"WaitForSingleObject failed with error %" PRIu32 "!", error);
return error;
}
CloseHandle(parallel->thread); CloseHandle(parallel->thread);
Stream_Free(parallel->device.data, TRUE); Stream_Free(parallel->device.data, TRUE);
MessageQueue_Free(parallel->queue); MessageQueue_Free(parallel->queue);
free(parallel); free(parallel);
}
return CHANNEL_RC_OK; return CHANNEL_RC_OK;
} }
@ -439,24 +433,24 @@ static void parallel_message_free(void* obj)
*/ */
FREERDP_ENTRY_POINT(UINT parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)) FREERDP_ENTRY_POINT(UINT parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
{ {
char* name = NULL;
char* path = NULL;
size_t length = 0;
RDPDR_PARALLEL* device = NULL;
PARALLEL_DEVICE* parallel = NULL; PARALLEL_DEVICE* parallel = NULL;
UINT error = 0; UINT error = 0;
WINPR_ASSERT(pEntryPoints); WINPR_ASSERT(pEntryPoints);
device = (RDPDR_PARALLEL*)pEntryPoints->device; RDPDR_PARALLEL* device = (RDPDR_PARALLEL*)pEntryPoints->device;
WINPR_ASSERT(device); WINPR_ASSERT(device);
name = device->device.Name; wLog* log = WLog_Get(TAG);
path = device->Path; WINPR_ASSERT(log);
char* name = device->device.Name;
char* path = device->Path;
if (!name || (name[0] == '*') || !path) if (!name || (name[0] == '*') || !path)
{ {
/* TODO: implement auto detection of parallel ports */ /* TODO: implement auto detection of parallel ports */
WLog_Print(log, WLOG_WARN, "Autodetection not implemented, no ports will be redirected");
return CHANNEL_RC_INITIALIZATION_ERROR; return CHANNEL_RC_INITIALIZATION_ERROR;
} }
@ -466,21 +460,22 @@ FREERDP_ENTRY_POINT(UINT parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINT
if (!parallel) if (!parallel)
{ {
WLog_ERR(TAG, "calloc failed!"); WLog_Print(log, WLOG_ERROR, "calloc failed!");
return CHANNEL_RC_NO_MEMORY; return CHANNEL_RC_NO_MEMORY;
} }
parallel->log = log;
parallel->device.type = RDPDR_DTYP_PARALLEL; parallel->device.type = RDPDR_DTYP_PARALLEL;
parallel->device.name = name; parallel->device.name = name;
parallel->device.IRPRequest = parallel_irp_request; parallel->device.IRPRequest = parallel_irp_request;
parallel->device.Free = parallel_free; parallel->device.Free = parallel_free;
parallel->rdpcontext = pEntryPoints->rdpcontext; parallel->rdpcontext = pEntryPoints->rdpcontext;
length = strlen(name); const size_t length = strlen(name);
parallel->device.data = Stream_New(NULL, length + 1); parallel->device.data = Stream_New(NULL, length + 1);
if (!parallel->device.data) if (!parallel->device.data)
{ {
WLog_ERR(TAG, "Stream_New failed!"); WLog_Print(parallel->log, WLOG_ERROR, "Stream_New failed!");
error = CHANNEL_RC_NO_MEMORY; error = CHANNEL_RC_NO_MEMORY;
goto error_out; goto error_out;
} }
@ -493,7 +488,7 @@ FREERDP_ENTRY_POINT(UINT parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINT
if (!parallel->queue) if (!parallel->queue)
{ {
WLog_ERR(TAG, "MessageQueue_New failed!"); WLog_Print(parallel->log, WLOG_ERROR, "MessageQueue_New failed!");
error = CHANNEL_RC_NO_MEMORY; error = CHANNEL_RC_NO_MEMORY;
goto error_out; goto error_out;
} }
@ -502,16 +497,18 @@ FREERDP_ENTRY_POINT(UINT parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINT
WINPR_ASSERT(obj); WINPR_ASSERT(obj);
obj->fnObjectFree = parallel_message_free; obj->fnObjectFree = parallel_message_free;
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*)parallel))) error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &parallel->device);
if (error)
{ {
WLog_ERR(TAG, "RegisterDevice failed with error %" PRIu32 "!", error); WLog_Print(parallel->log, WLOG_ERROR, "RegisterDevice failed with error %" PRIu32 "!",
error);
goto error_out; goto error_out;
} }
if (!(parallel->thread = parallel->thread = CreateThread(NULL, 0, parallel_thread_func, parallel, 0, NULL);
CreateThread(NULL, 0, parallel_thread_func, (void*)parallel, 0, NULL))) if (!parallel->thread)
{ {
WLog_ERR(TAG, "CreateThread failed!"); WLog_Print(parallel->log, WLOG_ERROR, "CreateThread failed!");
error = ERROR_INTERNAL_ERROR; error = ERROR_INTERNAL_ERROR;
goto error_out; goto error_out;
} }
@ -519,8 +516,7 @@ FREERDP_ENTRY_POINT(UINT parallel_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINT
return CHANNEL_RC_OK; return CHANNEL_RC_OK;
error_out: error_out:
MessageQueue_Free(parallel->queue); if (parallel)
Stream_Free(parallel->device.data, TRUE); parallel_free(&parallel->device);
free(parallel);
return error; return error;
} }