Skip to content

Commit

Permalink
Fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Sep 26, 2023
1 parent 654de88 commit 8463b86
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion virtual-display-driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "virtual-display-driver"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[lib]
Expand Down
80 changes: 65 additions & 15 deletions virtual-display-driver/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use driver_ipc::Monitor;
use log::{error, LevelFilter};
use log::{error, info, LevelFilter};
use wdf_umdf::{
IddCxDeviceInitConfig, IddCxDeviceInitialize, IntoHelper, WdfDeviceCreate,
WdfDeviceInitSetPnpPowerEventCallbacks, WdfDriverCreate,
WdfDeviceInitSetPnpPowerEventCallbacks, WdfDeviceSetFailed, WdfDriverCreate,
};
use wdf_umdf_sys::{
IDD_CX_CLIENT_CONFIG, NTSTATUS, WDFDEVICE_INIT, WDFDRIVER__, WDFOBJECT, WDF_DRIVER_CONFIG,
WDF_OBJECT_ATTRIBUTES, WDF_PNPPOWER_EVENT_CALLBACKS, _DRIVER_OBJECT, _UNICODE_STRING,
IDD_CX_CLIENT_CONFIG, NTSTATUS, WDFDEVICE_INIT, WDFDRIVER__, WDFOBJECT,
WDF_DEVICE_FAILED_ACTION, WDF_DRIVER_CONFIG, WDF_OBJECT_ATTRIBUTES,
WDF_PNPPOWER_EVENT_CALLBACKS, _DRIVER_OBJECT, _UNICODE_STRING,
};
use winreg::{
enums::{HKEY_LOCAL_MACHINE, KEY_READ},
Expand All @@ -32,19 +33,68 @@ extern "C-unwind" fn DriverEntry(
driver_object: *mut _DRIVER_OBJECT,
registry_path: *mut _UNICODE_STRING,
) -> NTSTATUS {
let status = winlog::init(
"VirtualDisplayDriver",
if cfg!(debug_assertions) {
LevelFilter::Debug
} else {
LevelFilter::Info
},
)
.map(|_| NTSTATUS::STATUS_SUCCESS)
.unwrap_or(NTSTATUS::STATUS_UNSUCCESSFUL);
// During system bootup, `RegisterEventSourceW` fails and causes the driver to not bootup
// Pretty unfortunate, therefore, we will run this on a thread until it succeeds and let the rest of
// the driver start. I know this is suboptimal considering it's our main code to catch panics.
//
// It always starts immediately when the computer is already booted up.
// If you have a better solution, please by all means open an issue report
let init_log = || {
winlog::init(
"VirtualDisplayDriver",
if cfg!(debug_assertions) {
LevelFilter::Debug
} else {
LevelFilter::Info
},
)
.map(|_| NTSTATUS::STATUS_SUCCESS)
.unwrap_or(NTSTATUS::STATUS_UNSUCCESSFUL)
};

let status = init_log();

if !status.is_success() {
return status;
// Okay, let's try another method then
struct Sendable<T>(T);
unsafe impl<T> Send for Sendable<T> {}
unsafe impl<T> Sync for Sendable<T> {}

let device = Sendable(driver_object);

std::thread::spawn(move || {
#[allow(clippy::redundant_locals)]
let device = device;
let mut time_waited = 0u64;
// in ms
let sleep_for = 500;

loop {
let status = init_log();
std::thread::sleep(std::time::Duration::from_millis(sleep_for));

time_waited += sleep_for;

// if it succeeds, great. if it didn't conclude after
// 1 second in ms * 60 seconds * 5 minutes = 5 minutes
// Surely a users system is booted up before then?
let timeout = time_waited >= 1000 * 60 * 5;
if status.is_success() || timeout {
if timeout {
unsafe {
_ = WdfDeviceSetFailed(
device.0 as *mut _,
WDF_DEVICE_FAILED_ACTION::WdfDeviceFailedNoRestart,
);
}
} else {
info!("Service took {} seconds to start", time_waited / 1000);
}

break;
}
}
});
}

// set the panic hook to capture and log panics
Expand Down
22 changes: 20 additions & 2 deletions wdf-umdf/src/wdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::ffi::c_void;

use wdf_umdf_sys::{
NTSTATUS, PCUNICODE_STRING, PCWDF_OBJECT_CONTEXT_TYPE_INFO, PDRIVER_OBJECT, PWDFDEVICE_INIT,
PWDF_DRIVER_CONFIG, PWDF_OBJECT_ATTRIBUTES, WDFDEVICE, WDFDRIVER, WDFOBJECT, WDF_NO_HANDLE,
WDF_NO_OBJECT_ATTRIBUTES, WDF_OBJECT_ATTRIBUTES, _WDF_PNPPOWER_EVENT_CALLBACKS,
PWDF_DRIVER_CONFIG, PWDF_OBJECT_ATTRIBUTES, WDFDEVICE, WDFDRIVER, WDFOBJECT,
WDF_DEVICE_FAILED_ACTION, WDF_NO_HANDLE, WDF_NO_OBJECT_ATTRIBUTES, WDF_OBJECT_ATTRIBUTES,
_WDF_PNPPOWER_EVENT_CALLBACKS,
};

use crate::IntoHelper;
Expand Down Expand Up @@ -507,3 +508,20 @@ pub unsafe fn WdfObjectDelete(
)
}
}

/// # Safety
///
/// None. User is responsible for safety.
pub unsafe fn WdfDeviceSetFailed(
// in
Device: WDFDEVICE,
// in
FailedAction: WDF_DEVICE_FAILED_ACTION,
) -> Result<(), WdfError> {
WdfCall! {
WdfDeviceSetFailed(
Device,
FailedAction
)
}
}

0 comments on commit 8463b86

Please sign in to comment.