diff options
34 files changed, 772 insertions, 941 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0ff8777 --- /dev/null +++ b/.clang-format @@ -0,0 +1,29 @@ +--- +Language: Cpp +BasedOnStyle: Chromium + +IndentWidth: 4 + +AccessModifierOffset: -4 +AlignAfterOpenBracket: BlockIndent +AlignConsecutiveMacros: AcrossComments +AllowShortFunctionsOnASingleLine: Empty +BinPackArguments: false +BinPackParameters: OnePerLine +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +BreakStringLiterals: false +ColumnLimit: 100 +IncludeBlocks: Regroup +PenaltyReturnTypeOnItsOwnLine: 1000 +SpacesBeforeTrailingComments: 1 +WrapNamespaceBodyWithEmptyLines: Always + +IncludeCategories: + - Regex: '^".+' + Priority: 1 + - Regex: '^<.*\.h>' + Priority: 2 + - Regex: '.*' + Priority: 3 +... diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d477b7b..fb3b2cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,25 @@ on: workflow_dispatch: jobs: + lint: + runs-on: ubuntu-latest + name: Linting + continue-on-error: ${{ github.ref != 'refs/heads/master' }} + steps: + - name: Checkout + uses: actions/checkout@v6 + with: + submodules: recursive + - name: Set up clang-format + uses: egor-tensin/setup-clang@v2 + with: + # To use the latest clang-format features: + version: 22 + - name: Run clang-format + uses: egor-tensin/clang-format@v1 + with: + version: 22 + build: strategy: matrix: diff --git a/km/src/minimal/main.c b/km/src/minimal/main.c index 219477a..0164692 100644 --- a/km/src/minimal/main.c +++ b/km/src/minimal/main.c @@ -7,15 +7,11 @@ #include <ntddk.h> -static void on_driver_unload(DRIVER_OBJECT *driver_object) -{ +static void on_driver_unload(DRIVER_OBJECT* driver_object) { UNREFERENCED_PARAMETER(driver_object); } -NTSTATUS DriverEntry( - DRIVER_OBJECT *driver_object, - UNICODE_STRING *registry_path) -{ +NTSTATUS DriverEntry(DRIVER_OBJECT* driver_object, UNICODE_STRING* registry_path) { UNREFERENCED_PARAMETER(registry_path); driver_object->DriverUnload = on_driver_unload; diff --git a/km/src/nt_namespace/device.c b/km/src/nt_namespace/device.c index 6d88dc4..afb9813 100644 --- a/km/src/nt_namespace/device.c +++ b/km/src/nt_namespace/device.c @@ -5,14 +5,14 @@ * Distributed under the MIT License. */ -#include "control_codes.h" #include "device.h" + +#include "control_codes.h" #include "nt2dos.h" #include <ntddk.h> -static NTSTATUS device_open(DEVICE_OBJECT *device_object, IRP *irp) -{ +static NTSTATUS device_open(DEVICE_OBJECT* device_object, IRP* irp) { NTSTATUS status = STATUS_SUCCESS; UNREFERENCED_PARAMETER(device_object); @@ -23,25 +23,22 @@ static NTSTATUS device_open(DEVICE_OBJECT *device_object, IRP *irp) return status; } -typedef NTSTATUS (*ioctl_handler)( - void *, unsigned long, - void *, unsigned long, - ULONG_PTR *); +typedef NTSTATUS (*ioctl_handler)(void*, unsigned long, void*, unsigned long, ULONG_PTR*); static NTSTATUS handle_convert_nt_path( - void *in_buf, + void* in_buf, unsigned long in_buf_size, - void *out_buf, + void* out_buf, unsigned long out_buf_size, - ULONG_PTR *nbwritten) -{ + ULONG_PTR* nbwritten +) { UNICODE_STRING uUnresolved, uResolved; NTSTATUS status = STATUS_SUCCESS; - DbgPrint("nt_namespace: unresolved path: %ws\n", (WCHAR *) in_buf); + DbgPrint("nt_namespace: unresolved path: %ws\n", (WCHAR*)in_buf); DbgPrint("nt_namespace: unresolved size: %lu\n", in_buf_size); - RtlInitUnicodeString(&uUnresolved, (WCHAR *) in_buf); + RtlInitUnicodeString(&uUnresolved, (WCHAR*)in_buf); status = nt2dos(&uResolved, &uUnresolved); if (!NT_SUCCESS(status)) @@ -52,8 +49,7 @@ static NTSTATUS handle_convert_nt_path( DbgPrint("nt_namespace: resolved path: %wZ\n", &uResolved); DbgPrint("nt_namespace: resolved size: %Iu\n", *nbwritten); - if (out_buf_size < *nbwritten) - { + if (out_buf_size < *nbwritten) { status = STATUS_BUFFER_OVERFLOW; goto free_resolved; } @@ -67,10 +63,9 @@ free_resolved: return status; } -static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) -{ - IO_STACK_LOCATION *io_stack_loc; - void* in_buf, *out_buf; +static NTSTATUS device_ioctl(DEVICE_OBJECT* device_object, IRP* irp) { + IO_STACK_LOCATION* io_stack_loc; + void *in_buf, *out_buf; unsigned long in_buf_size, out_buf_size; ioctl_handler handler; NTSTATUS status = STATUS_UNSUCCESSFUL; @@ -85,8 +80,7 @@ static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) in_buf_size = io_stack_loc->Parameters.DeviceIoControl.InputBufferLength; out_buf_size = io_stack_loc->Parameters.DeviceIoControl.OutputBufferLength; - switch (io_stack_loc->Parameters.DeviceIoControl.IoControlCode) - { + switch (io_stack_loc->Parameters.DeviceIoControl.IoControlCode) { case CONVERT_NT_PATH: handler = handle_convert_nt_path; break; @@ -96,10 +90,8 @@ static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) goto complete_request; } - status = irp->IoStatus.Status = handler( - in_buf, in_buf_size, - out_buf, out_buf_size, - &irp->IoStatus.Information); + status = irp->IoStatus.Status = + handler(in_buf, in_buf_size, out_buf, out_buf_size, &irp->IoStatus.Information); complete_request: IoCompleteRequest(irp, IO_NO_INCREMENT); @@ -107,25 +99,20 @@ complete_request: return status; } -typedef struct -{ - const wchar_t *path; - const wchar_t *symlink; -} -DeviceInfo; +typedef struct { + const wchar_t* path; + const wchar_t* symlink; +} DeviceInfo; -typedef struct -{ - DEVICE_OBJECT *object; +typedef struct { + DEVICE_OBJECT* object; UNICODE_STRING path; UNICODE_STRING symlink; -} -Device; +} Device; #define NUMOF_DEVICES 1 -static DeviceInfo devices_info[NUMOF_DEVICES] = -{ +static DeviceInfo devices_info[NUMOF_DEVICES] = { { L"\\Device\\nt_namespace", L"\\DosDevices\\nt_namespace", @@ -134,21 +121,18 @@ static DeviceInfo devices_info[NUMOF_DEVICES] = static Device devices[NUMOF_DEVICES]; -static void destroy_device(int i) -{ +static void destroy_device(int i) { IoDeleteSymbolicLink(&devices[i].symlink); IoDeleteDevice(devices[i].object); } -void destroy_devices() -{ +void destroy_devices() { int i; for (i = 0; i < NUMOF_DEVICES; ++i) destroy_device(i); } -static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) -{ +static NTSTATUS set_up_device(DRIVER_OBJECT* driver_object, int i) { NTSTATUS status = STATUS_SUCCESS; RtlInitUnicodeString(&devices[i].path, devices_info[i].path); @@ -161,7 +145,8 @@ static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, - &devices[i].object); + &devices[i].object + ); if (!NT_SUCCESS(status)) return status; @@ -169,8 +154,7 @@ static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) devices[i].object->Flags |= DO_BUFFERED_IO; devices[i].object->Flags &= ~DO_DEVICE_INITIALIZING; - if (!NT_SUCCESS(status = IoCreateSymbolicLink( - &devices[i].symlink, &devices[i].path))) + if (!NT_SUCCESS(status = IoCreateSymbolicLink(&devices[i].symlink, &devices[i].path))) goto delete_device; return status; @@ -181,8 +165,7 @@ delete_device: return status; } -NTSTATUS set_up_devices(DRIVER_OBJECT *driver_object) -{ +NTSTATUS set_up_devices(DRIVER_OBJECT* driver_object) { int i, j; NTSTATUS status = STATUS_SUCCESS; diff --git a/km/src/nt_namespace/device.h b/km/src/nt_namespace/device.h index 4f117e9..8cc2c1d 100644 --- a/km/src/nt_namespace/device.h +++ b/km/src/nt_namespace/device.h @@ -9,5 +9,5 @@ #include <ntddk.h> -NTSTATUS set_up_devices(DRIVER_OBJECT *); +NTSTATUS set_up_devices(DRIVER_OBJECT*); void destroy_devices(); diff --git a/km/src/nt_namespace/main.c b/km/src/nt_namespace/main.c index 4c7ef74..a8d0c14 100644 --- a/km/src/nt_namespace/main.c +++ b/km/src/nt_namespace/main.c @@ -9,17 +9,13 @@ #include <ntddk.h> -static void on_driver_unload(DRIVER_OBJECT *driver_object) -{ +static void on_driver_unload(DRIVER_OBJECT* driver_object) { UNREFERENCED_PARAMETER(driver_object); destroy_devices(); } -NTSTATUS DriverEntry( - DRIVER_OBJECT *driver_object, - UNICODE_STRING *registry_path) -{ +NTSTATUS DriverEntry(DRIVER_OBJECT* driver_object, UNICODE_STRING* registry_path) { UNREFERENCED_PARAMETER(registry_path); ExInitializeDriverRuntime(DrvRtPoolNxOptIn); diff --git a/km/src/nt_namespace/nt2dos.c b/km/src/nt_namespace/nt2dos.c index be2c2f5..2e6db6c 100644 --- a/km/src/nt_namespace/nt2dos.c +++ b/km/src/nt_namespace/nt2dos.c @@ -5,12 +5,13 @@ * Distributed under the MIT License. */ +/* clang-format off */ #include <ntifs.h> #include "nt2dos.h" +/* clang-format on */ -static PVOID allocate(SIZE_T nb) -{ +static PVOID allocate(SIZE_T nb) { #if !defined(NTDDI_WIN10) || NTDDI_VERSION < NTDDI_WIN10 return ExAllocatePoolWithTag(PagedPool, nb, '1l'); #else @@ -18,10 +19,7 @@ static PVOID allocate(SIZE_T nb) #endif } -static NTSTATUS get_object_name_info( - void *object, - OBJECT_NAME_INFORMATION **object_name_info) -{ +static NTSTATUS get_object_name_info(void* object, OBJECT_NAME_INFORMATION** object_name_info) { unsigned long nbneeded; NTSTATUS status = STATUS_SUCCESS; @@ -48,11 +46,10 @@ free_object_name_info: return status; } -NTSTATUS nt2dos(UNICODE_STRING *u_resolved, UNICODE_STRING *u_unresolved) -{ +NTSTATUS nt2dos(UNICODE_STRING* u_resolved, UNICODE_STRING* u_unresolved) { IO_STATUS_BLOCK io_status_block; - DEVICE_OBJECT *volume_object; - FILE_OBJECT *file_object; + DEVICE_OBJECT* volume_object; + FILE_OBJECT* file_object; HANDLE file; OBJECT_ATTRIBUTES file_attrs; OBJECT_NAME_INFORMATION *file_name_info, *volume_name_info; @@ -61,11 +58,8 @@ NTSTATUS nt2dos(UNICODE_STRING *u_resolved, UNICODE_STRING *u_unresolved) NTSTATUS status = STATUS_SUCCESS; InitializeObjectAttributes( - &file_attrs, - u_unresolved, - OBJ_CASE_INSENSITIVE | OBJ_OPENIF, - NULL, - NULL); + &file_attrs, u_unresolved, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, NULL, NULL + ); status = ZwCreateFile( &file, @@ -78,18 +72,15 @@ NTSTATUS nt2dos(UNICODE_STRING *u_resolved, UNICODE_STRING *u_unresolved) FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, - 0); + 0 + ); if (!NT_SUCCESS(status)) return status; status = ObReferenceObjectByHandle( - file, - FILE_READ_ATTRIBUTES, - *IoFileObjectType, - KernelMode, - &file_object, - NULL); + file, FILE_READ_ATTRIBUTES, *IoFileObjectType, KernelMode, &file_object, NULL + ); if (!NT_SUCCESS(status)) goto close_file; @@ -99,18 +90,15 @@ NTSTATUS nt2dos(UNICODE_STRING *u_resolved, UNICODE_STRING *u_unresolved) if (!NT_SUCCESS(status)) goto close_file; - if (file_object->Vpb == NULL) - { + if (file_object->Vpb == NULL) { u_resolved->Buffer = allocate(file_name_info->Name.Length); - if (u_resolved->Buffer == NULL) - { + if (u_resolved->Buffer == NULL) { status = STATUS_INSUFFICIENT_RESOURCES; goto free_file_name_info; } - RtlInitEmptyUnicodeString( - u_resolved, u_resolved->Buffer, file_name_info->Name.Length); + RtlInitEmptyUnicodeString(u_resolved, u_resolved->Buffer, file_name_info->Name.Length); RtlCopyUnicodeString(u_resolved, &file_name_info->Name); goto free_file_name_info; } @@ -126,12 +114,12 @@ NTSTATUS nt2dos(UNICODE_STRING *u_resolved, UNICODE_STRING *u_unresolved) if (!NT_SUCCESS(status)) goto free_volume_name_info; - u_resolved_size = file_name_info->Name.Length - volume_name_info->Name.Length + u_dos_name.Length; + u_resolved_size = + file_name_info->Name.Length - volume_name_info->Name.Length + u_dos_name.Length; u_resolved->Buffer = allocate(u_resolved_size); - if (u_resolved->Buffer == NULL) - { + if (u_resolved->Buffer == NULL) { status = STATUS_INSUFFICIENT_RESOURCES; goto free_dos_name; } diff --git a/km/src/nt_namespace/nt2dos.h b/km/src/nt_namespace/nt2dos.h index ce0b690..7f24b5b 100644 --- a/km/src/nt_namespace/nt2dos.h +++ b/km/src/nt_namespace/nt2dos.h @@ -9,4 +9,4 @@ #include <ntstrsafe.h> -NTSTATUS nt2dos(UNICODE_STRING *resolved, UNICODE_STRING *unresolved); +NTSTATUS nt2dos(UNICODE_STRING* resolved, UNICODE_STRING* unresolved); diff --git a/km/src/simple/device.c b/km/src/simple/device.c index f632a8d..ccc780d 100644 --- a/km/src/simple/device.c +++ b/km/src/simple/device.c @@ -9,8 +9,7 @@ #include <ntddk.h> -static NTSTATUS device_open(DEVICE_OBJECT *device_object, IRP *irp) -{ +static NTSTATUS device_open(DEVICE_OBJECT* device_object, IRP* irp) { NTSTATUS status = STATUS_SUCCESS; UNREFERENCED_PARAMETER(device_object); @@ -21,18 +20,15 @@ static NTSTATUS device_open(DEVICE_OBJECT *device_object, IRP *irp) return status; } -typedef NTSTATUS (*ioctl_handler)( - void *, unsigned long, - void *, unsigned long, - ULONG_PTR *); +typedef NTSTATUS (*ioctl_handler)(void*, unsigned long, void*, unsigned long, ULONG_PTR*); static NTSTATUS handle_say_hello( - void *in_buf, + void* in_buf, unsigned long in_buf_size, - void *out_buf, + void* out_buf, unsigned long out_buf_size, - ULONG_PTR *nbwritten) -{ + ULONG_PTR* nbwritten +) { UNREFERENCED_PARAMETER(in_buf); UNREFERENCED_PARAMETER(in_buf_size); UNREFERENCED_PARAMETER(out_buf); @@ -46,12 +42,12 @@ static NTSTATUS handle_say_hello( static unsigned int num = 42; static NTSTATUS handle_exchange_ints( - void *in_buf, + void* in_buf, unsigned long in_buf_size, - void *out_buf, + void* out_buf, unsigned long out_buf_size, - ULONG_PTR *nbwritten) -{ + ULONG_PTR* nbwritten +) { unsigned int temp_num; if (in_buf_size != sizeof(temp_num)) @@ -69,13 +65,12 @@ static NTSTATUS handle_exchange_ints( return STATUS_SUCCESS; } -#define SAY_HELLO CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define SAY_HELLO CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) #define EXCHANGE_INTS CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) -static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) -{ - IO_STACK_LOCATION *io_stack_loc; - void* in_buf, *out_buf; +static NTSTATUS device_ioctl(DEVICE_OBJECT* device_object, IRP* irp) { + IO_STACK_LOCATION* io_stack_loc; + void *in_buf, *out_buf; unsigned long in_buf_size, out_buf_size; ioctl_handler handler; NTSTATUS status = STATUS_UNSUCCESSFUL; @@ -90,8 +85,7 @@ static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) in_buf_size = io_stack_loc->Parameters.DeviceIoControl.InputBufferLength; out_buf_size = io_stack_loc->Parameters.DeviceIoControl.OutputBufferLength; - switch (io_stack_loc->Parameters.DeviceIoControl.IoControlCode) - { + switch (io_stack_loc->Parameters.DeviceIoControl.IoControlCode) { case SAY_HELLO: handler = handle_say_hello; break; @@ -105,10 +99,8 @@ static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) goto complete_request; } - status = irp->IoStatus.Status = handler( - in_buf, in_buf_size, - out_buf, out_buf_size, - &irp->IoStatus.Information); + status = irp->IoStatus.Status = + handler(in_buf, in_buf_size, out_buf, out_buf_size, &irp->IoStatus.Information); complete_request: IoCompleteRequest(irp, IO_NO_INCREMENT); @@ -116,25 +108,20 @@ complete_request: return status; } -typedef struct -{ - const wchar_t *path; - const wchar_t *symlink; -} -DeviceInfo; +typedef struct { + const wchar_t* path; + const wchar_t* symlink; +} DeviceInfo; -typedef struct -{ - DEVICE_OBJECT *object; +typedef struct { + DEVICE_OBJECT* object; UNICODE_STRING path; UNICODE_STRING symlink; -} -Device; +} Device; #define NUMOF_DEVICES 2 -static DeviceInfo devices_info[NUMOF_DEVICES] = -{ +static DeviceInfo devices_info[NUMOF_DEVICES] = { { L"\\Device\\simple_device1", L"\\DosDevices\\simple_device1", @@ -147,21 +134,18 @@ static DeviceInfo devices_info[NUMOF_DEVICES] = static Device devices[NUMOF_DEVICES]; -static void destroy_device(int i) -{ +static void destroy_device(int i) { IoDeleteSymbolicLink(&devices[i].symlink); IoDeleteDevice(devices[i].object); } -void destroy_devices() -{ +void destroy_devices() { int i; for (i = 0; i < NUMOF_DEVICES; ++i) destroy_device(i); } -static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) -{ +static NTSTATUS set_up_device(DRIVER_OBJECT* driver_object, int i) { NTSTATUS status = STATUS_SUCCESS; DbgPrint("Setting up device...\n"); @@ -178,7 +162,8 @@ static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, - &devices[i].object); + &devices[i].object + ); if (!NT_SUCCESS(status)) return status; @@ -186,8 +171,7 @@ static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) devices[i].object->Flags |= DO_BUFFERED_IO; devices[i].object->Flags &= ~DO_DEVICE_INITIALIZING; - if (!NT_SUCCESS(status = IoCreateSymbolicLink( - &devices[i].symlink, &devices[i].path))) + if (!NT_SUCCESS(status = IoCreateSymbolicLink(&devices[i].symlink, &devices[i].path))) goto delete_device; return status; @@ -198,8 +182,7 @@ delete_device: return status; } -NTSTATUS set_up_devices(DRIVER_OBJECT *driver_object) -{ +NTSTATUS set_up_devices(DRIVER_OBJECT* driver_object) { int i, j; NTSTATUS status = STATUS_SUCCESS; diff --git a/km/src/simple/device.h b/km/src/simple/device.h index 4f117e9..8cc2c1d 100644 --- a/km/src/simple/device.h +++ b/km/src/simple/device.h @@ -9,5 +9,5 @@ #include <ntddk.h> -NTSTATUS set_up_devices(DRIVER_OBJECT *); +NTSTATUS set_up_devices(DRIVER_OBJECT*); void destroy_devices(); diff --git a/km/src/simple/main.c b/km/src/simple/main.c index 5148727..100ad37 100644 --- a/km/src/simple/main.c +++ b/km/src/simple/main.c @@ -9,18 +9,14 @@ #include <ntddk.h> -static void on_driver_unload(DRIVER_OBJECT *driver_object) -{ +static void on_driver_unload(DRIVER_OBJECT* driver_object) { UNREFERENCED_PARAMETER(driver_object); DbgPrint("simple: unloading...\n"); destroy_devices(); } -NTSTATUS DriverEntry( - DRIVER_OBJECT *driver_object, - UNICODE_STRING *registry_path) -{ +NTSTATUS DriverEntry(DRIVER_OBJECT* driver_object, UNICODE_STRING* registry_path) { UNREFERENCED_PARAMETER(registry_path); DbgPrint("simple: loading...\n"); diff --git a/km/src/sysenter/main.c b/km/src/sysenter/main.c index 9eb9b28..c2d9144 100644 --- a/km/src/sysenter/main.c +++ b/km/src/sysenter/main.c @@ -8,10 +8,9 @@ #include <ntddk.h> static __int64 old_msr_value = 0; -static void *old_ki_fast_call_entry = NULL; +static void* old_ki_fast_call_entry = NULL; -static void __stdcall log_system_call() -{ +static void __stdcall log_system_call() { static LONG count = 0; static const LONG throttle = 10000; @@ -21,8 +20,7 @@ static void __stdcall log_system_call() DbgPrint("Another %ld of `sysenter`s (eax=)!\n", throttle); } -static void __declspec(naked) new_ki_fast_call_entry() -{ +static void __declspec(naked) new_ki_fast_call_entry() { __asm { pushad @@ -41,8 +39,7 @@ static void __declspec(naked) new_ki_fast_call_entry() #define IA32_SYSENTER_EIP 0x176 -static void hook_sysenter() -{ +static void hook_sysenter() { /* __asm { @@ -57,14 +54,13 @@ static void hook_sysenter() old_msr_value = __readmsr(IA32_SYSENTER_EIP); #pragma warning(push) -#pragma warning(disable: 4305) - old_ki_fast_call_entry = (void *) old_msr_value; +#pragma warning(disable : 4305) + old_ki_fast_call_entry = (void*)old_msr_value; #pragma warning(pop) __writemsr(IA32_SYSENTER_EIP, new_ki_fast_call_entry); } -static void unhook_sysenter() -{ +static void unhook_sysenter() { /* __asm { @@ -78,8 +74,7 @@ static void unhook_sysenter() __writemsr(IA32_SYSENTER_EIP, old_msr_value); } -static void on_driver_unload(DRIVER_OBJECT *driver_object) -{ +static void on_driver_unload(DRIVER_OBJECT* driver_object) { KTIMER timer; LARGE_INTEGER time_out; @@ -94,10 +89,7 @@ static void on_driver_unload(DRIVER_OBJECT *driver_object) KeWaitForSingleObject(&timer, Executive, KernelMode, FALSE, NULL); } -NTSTATUS DriverEntry( - DRIVER_OBJECT *driver_object, - UNICODE_STRING *registry_path) -{ +NTSTATUS DriverEntry(DRIVER_OBJECT* driver_object, UNICODE_STRING* registry_path) { UNREFERENCED_PARAMETER(registry_path); driver_object->DriverUnload = on_driver_unload; diff --git a/um/service/include/service/device.hpp b/um/service/include/service/device.hpp index ee46354..29d03c1 100644 --- a/um/service/include/service/device.hpp +++ b/um/service/include/service/device.hpp @@ -11,71 +11,64 @@ #include <Windows.h> #include <cstddef> - #include <string> #include <utility> -namespace service -{ - class Device - { - public: - typedef DWORD Code; +namespace service { - static Device open(const std::string& path); +class Device { +public: + typedef DWORD Code; - Device(Device&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } + static Device open(const std::string& path); - Device& operator=(Device other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + Device(Device&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(Device& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } + Device& operator=(Device other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - std::size_t get_required_output_size( - Code code, - const void* in_buf, - std::size_t in_buf_size) const; + void swap(Device& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(handle, other.handle); + } - std::size_t send_control_code( - Code code, - const void* in_buf, - std::size_t in_buf_size, - void* out_buf, - std::size_t out_buf_size) const; + std::size_t get_required_output_size( + Code code, + const void* in_buf, + std::size_t in_buf_size + ) const; - private: - Device(Handle handle) - : handle(std::move(handle)) - { } + std::size_t send_control_code( + Code code, + const void* in_buf, + std::size_t in_buf_size, + void* out_buf, + std::size_t out_buf_size + ) const; - Handle handle; +private: + Device(Handle handle) : handle(std::move(handle)) {} - Device(const Device&) = delete; - }; + Handle handle; - inline void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + Device(const Device&) = delete; +}; + +inline void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::Device& a, - service::Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::Device& a, service::Device& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/handle.hpp b/um/service/include/service/handle.hpp index 2ca3754..ef4c152 100644 --- a/um/service/include/service/handle.hpp +++ b/um/service/include/service/handle.hpp @@ -13,71 +13,59 @@ #include <type_traits> #include <utility> -namespace service -{ - class Handle - { - public: - Handle() = default; +namespace service { - Handle(HANDLE raw) - : impl(raw) - { } +class Handle { +public: + Handle() = default; - Handle(Handle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + Handle(HANDLE raw) : impl(raw) {} - operator bool() const - { - return static_cast<bool>(impl); - } + Handle(Handle&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - operator HANDLE() const - { - return impl.get(); - } + Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - void swap(Handle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } + operator bool() const { + return static_cast<bool>(impl); + } - private: - struct Deleter - { - void operator()(HANDLE raw) - { - CloseHandle(raw); - } - }; + operator HANDLE() const { + return impl.get(); + } - std::unique_ptr<std::remove_pointer<HANDLE>::type, Deleter> impl; + void swap(Handle& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(impl, other.impl); + } - Handle(const Handle&) = delete; +private: + struct Deleter { + void operator()(HANDLE raw) { + CloseHandle(raw); + } }; - inline void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + std::unique_ptr<std::remove_pointer<HANDLE>::type, Deleter> impl; + + Handle(const Handle&) = delete; +}; + +inline void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::Handle& a, - service::Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::Handle& a, service::Handle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/service.hpp b/um/service/include/service/service.hpp index 2d5f776..58f2fa4 100644 --- a/um/service/include/service/service.hpp +++ b/um/service/include/service/service.hpp @@ -12,68 +12,57 @@ #include <string> #include <utility> -namespace service -{ - class Service - { - public: - static bool exists( - const ServiceManager&, - const std::string& name); +namespace service { - static Service open( - const ServiceManager&, - const std::string& name); +class Service { +public: + static bool exists(const ServiceManager&, const std::string& name); - static Service install( - const ServiceManager&, - const std::string& name, - const std::string& bin_path); + static Service open(const ServiceManager&, const std::string& name); - void start() const; - void stop() const; - void uninstall() const; + static Service install( + const ServiceManager&, + const std::string& name, + const std::string& bin_path + ); - Service(Service&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } + void start() const; + void stop() const; + void uninstall() const; - Service& operator=(Service other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + Service(Service&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(Service& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } + Service& operator=(Service other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - private: - Service(ServiceHandle handle) - : handle(std::move(handle)) - { } + void swap(Service& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(handle, other.handle); + } - ServiceHandle handle; +private: + Service(ServiceHandle handle) : handle(std::move(handle)) {} - Service(const Service&) = delete; - }; + ServiceHandle handle; - inline void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + Service(const Service&) = delete; +}; + +inline void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::Service& a, - service::Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::Service& a, service::Service& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/service_handle.hpp b/um/service/include/service/service_handle.hpp index 4be58c6..38e6a96 100644 --- a/um/service/include/service/service_handle.hpp +++ b/um/service/include/service/service_handle.hpp @@ -12,71 +12,59 @@ #include <memory> #include <utility> -namespace service -{ - class ServiceHandle - { - public: - ServiceHandle() = default; +namespace service { - ServiceHandle(SC_HANDLE raw) - : impl(raw) - { } +class ServiceHandle { +public: + ServiceHandle() = default; - ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + ServiceHandle(SC_HANDLE raw) : impl(raw) {} - operator bool() const - { - return static_cast<bool>(impl); - } + ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - operator SC_HANDLE() const - { - return impl.get(); - } + ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } + operator bool() const { + return static_cast<bool>(impl); + } - private: - struct Deleter - { - void operator()(SC_HANDLE raw) - { - CloseServiceHandle(raw); - } - }; + operator SC_HANDLE() const { + return impl.get(); + } - std::unique_ptr<SC_HANDLE__, Deleter> impl; + void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(impl, other.impl); + } - ServiceHandle(const ServiceHandle&) = delete; +private: + struct Deleter { + void operator()(SC_HANDLE raw) { + CloseServiceHandle(raw); + } }; - inline void swap(ServiceHandle& a, ServiceHandle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + std::unique_ptr<SC_HANDLE__, Deleter> impl; + + ServiceHandle(const ServiceHandle&) = delete; +}; + +inline void swap(ServiceHandle& a, ServiceHandle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::ServiceHandle& a, - service::ServiceHandle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::ServiceHandle& a, service::ServiceHandle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/service_manager.hpp b/um/service/include/service/service_manager.hpp index 2aa9312..a408d12 100644 --- a/um/service/include/service/service_manager.hpp +++ b/um/service/include/service/service_manager.hpp @@ -12,58 +12,49 @@ #include <utility> -namespace service -{ - class ServiceManager - { - public: - static ServiceManager open(); +namespace service { - ServiceManager(ServiceManager&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } +class ServiceManager { +public: + static ServiceManager open(); - ServiceManager& operator=(ServiceManager other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + ServiceManager(ServiceManager&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } + ServiceManager& operator=(ServiceManager other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - operator SC_HANDLE() const - { - return handle; - } + void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(handle, other.handle); + } - private: - ServiceManager(ServiceHandle handle) - : handle(std::move(handle)) - { } + operator SC_HANDLE() const { + return handle; + } - ServiceHandle handle; +private: + ServiceManager(ServiceHandle handle) : handle(std::move(handle)) {} - ServiceManager(const ServiceManager&) = delete; - }; + ServiceHandle handle; - inline void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + ServiceManager(const ServiceManager&) = delete; +}; + +inline void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::ServiceManager& a, - service::ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::ServiceManager& a, service::ServiceManager& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/singleton.hpp b/um/service/include/service/singleton.hpp index c76d54b..3eaacbf 100644 --- a/um/service/include/service/singleton.hpp +++ b/um/service/include/service/singleton.hpp @@ -7,37 +7,34 @@ #include <mutex> -namespace service -{ - template <typename DerivedT> - class Singleton - { - public: - static DerivedT& get() - { - std::call_once(initialized, initialize); - return get_unsafe(); - } +namespace service { - protected: - Singleton() = default; - virtual ~Singleton() = default; +template <typename DerivedT> +class Singleton { +public: + static DerivedT& get() { + std::call_once(initialized, initialize); + return get_unsafe(); + } - private: - static void initialize() - { - get_unsafe(); - } +protected: + Singleton() = default; + virtual ~Singleton() = default; - static DerivedT& get_unsafe() - { - static DerivedT instance; - return instance; - } +private: + static void initialize() { + get_unsafe(); + } - static std::once_flag initialized; - }; + static DerivedT& get_unsafe() { + static DerivedT instance; + return instance; + } - template <typename DerivedT> - std::once_flag Singleton<DerivedT>::initialized; -} + static std::once_flag initialized; +}; + +template <typename DerivedT> +std::once_flag Singleton<DerivedT>::initialized; + +} // namespace service diff --git a/um/service/include/service/windows_error.hpp b/um/service/include/service/windows_error.hpp index ab46cfd..d884ea7 100644 --- a/um/service/include/service/windows_error.hpp +++ b/um/service/include/service/windows_error.hpp @@ -14,45 +14,32 @@ #include <string> #include <system_error> -namespace service -{ - namespace windows_error - { - class Category - : public std::error_category - , public Singleton<Category> - { - public: - const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; } +namespace service { +namespace windows_error { - std::string message(int) const; +class Category + : public std::error_category + , public Singleton<Category> { +public: + const char* name() const LIBSERVICE_NOEXCEPT { + return "Windows"; + } - private: - friend class Singleton<Category>; - }; + std::string message(int) const; - inline std::string build_message_prefix( - const char* file, - int line, - const char* function) - { - std::ostringstream oss; - oss << "Error in function '" << function - << "' at file '" << file - << "', line " << line; - return oss.str(); - } +private: + friend class Singleton<Category>; +}; - inline std::system_error make( - DWORD code, - const char* file, - int line, - const char* function) - { - return std::system_error( - code, - Category::get(), - build_message_prefix(file, line, function)); - } - } +inline std::string build_message_prefix(const char* file, int line, const char* function) { + std::ostringstream oss; + oss << "Error in function '" << function << "' at file '" << file << "', line " << line; + return oss.str(); } + +inline std::system_error make(DWORD code, const char* file, int line, const char* function) { + return std::system_error(code, Category::get(), build_message_prefix(file, line, function)); +} + +} // namespace windows_error +} // namespace service diff --git a/um/service/src/device.cpp b/um/service/src/device.cpp index 870930f..2634a9a 100644 --- a/um/service/src/device.cpp +++ b/um/service/src/device.cpp @@ -8,108 +8,105 @@ #include <Windows.h> #include <cstddef> - #include <limits> #include <stdexcept> #include <string> -namespace service -{ - namespace - { - Handle open_device(const std::string& path) - { - const auto raw = CreateFileA( - path.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); +namespace service { +namespace { - if (INVALID_HANDLE_VALUE == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +Handle open_device(const std::string& path) { + const auto raw = CreateFileA( + path.c_str(), + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL + ); - return Handle(raw); - } + if (INVALID_HANDLE_VALUE == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } - Device Device::open(const std::string& path) - { - return Device(open_device(path)); - } + return Handle(raw); +} - std::size_t Device::get_required_output_size( - Code code, - const void* in_buf, - std::size_t in_buf_size) const - { - DWORD nbreq; +} // namespace - if (in_buf_size > std::numeric_limits<DWORD>::max()) - throw std::range_error("input buffer size is too large"); +Device Device::open(const std::string& path) { + return Device(open_device(path)); +} - std::size_t nbwritten = DeviceIoControl( - handle, - code, - const_cast<void*>(in_buf), - static_cast<DWORD>(in_buf_size), - NULL, - 0, - &nbreq, - NULL); +std::size_t Device::get_required_output_size( + Code code, + const void* in_buf, + std::size_t in_buf_size +) const { + DWORD nbreq; - if (0 == nbwritten) - { - const auto ec = GetLastError(); + if (in_buf_size > std::numeric_limits<DWORD>::max()) + throw std::range_error("input buffer size is too large"); - switch (ec) - { - case ERROR_MORE_DATA: - return nbreq; + std::size_t nbwritten = DeviceIoControl( + handle, + code, + const_cast<void*>(in_buf), + static_cast<DWORD>(in_buf_size), + NULL, + 0, + &nbreq, + NULL + ); - default: - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + if (0 == nbwritten) { + const auto ec = GetLastError(); - return nbwritten; + switch (ec) { + case ERROR_MORE_DATA: + return nbreq; + + default: + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } } - std::size_t Device::send_control_code( - Code code, - const void* in_buf, - std::size_t in_buf_size, - void* out_buf, - std::size_t out_buf_size) const - { - DWORD nbreq; + return nbwritten; +} - if (in_buf_size > std::numeric_limits<DWORD>::max()) - throw std::range_error("input buffer size is too large"); - if (out_buf_size > std::numeric_limits<DWORD>::max()) - throw std::range_error("output buffer size is too large"); +std::size_t Device::send_control_code( + Code code, + const void* in_buf, + std::size_t in_buf_size, + void* out_buf, + std::size_t out_buf_size +) const { + DWORD nbreq; - std::size_t nbwritten = DeviceIoControl( - handle, - code, - const_cast<void*>(in_buf), - static_cast<DWORD>(in_buf_size), - out_buf, - static_cast<DWORD>(out_buf_size), - &nbreq, - NULL); + if (in_buf_size > std::numeric_limits<DWORD>::max()) + throw std::range_error("input buffer size is too large"); + if (out_buf_size > std::numeric_limits<DWORD>::max()) + throw std::range_error("output buffer size is too large"); - if (0 == nbwritten) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } + std::size_t nbwritten = DeviceIoControl( + handle, + code, + const_cast<void*>(in_buf), + static_cast<DWORD>(in_buf_size), + out_buf, + static_cast<DWORD>(out_buf_size), + &nbreq, + NULL + ); - return nbwritten; + if (0 == nbwritten) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } + + return nbwritten; } + +} // namespace service diff --git a/um/service/src/service.cpp b/um/service/src/service.cpp index b7384ce..698a9d9 100644 --- a/um/service/src/service.cpp +++ b/um/service/src/service.cpp @@ -9,242 +9,203 @@ #include <string> -namespace service -{ - namespace - { - ServiceHandle open_service( - const ServiceManager& mgr, - const std::string& name) - { - const auto raw = OpenServiceA( - mgr, - name.c_str(), - SERVICE_ALL_ACCESS); +namespace service { +namespace { - if (NULL == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +ServiceHandle open_service(const ServiceManager& mgr, const std::string& name) { + const auto raw = OpenServiceA(mgr, name.c_str(), SERVICE_ALL_ACCESS); - return raw; - } + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - ServiceHandle install_service( - const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) - { - const auto raw = CreateServiceA( - mgr, - name.c_str(), - name.c_str(), - SERVICE_ALL_ACCESS, - SERVICE_KERNEL_DRIVER, - SERVICE_DEMAND_START, - SERVICE_ERROR_NORMAL, - bin_path.c_str(), - NULL, - NULL, - NULL, - NULL, - NULL); + return raw; +} - if (NULL == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +ServiceHandle install_service( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path +) { + const auto raw = CreateServiceA( + mgr, + name.c_str(), + name.c_str(), + SERVICE_ALL_ACCESS, + SERVICE_KERNEL_DRIVER, + SERVICE_DEMAND_START, + SERVICE_ERROR_NORMAL, + bin_path.c_str(), + NULL, + NULL, + NULL, + NULL, + NULL + ); - return raw; - } + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - void start_service(const ServiceHandle& handle) - { - if (!StartService(handle, 0, NULL)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + return raw; +} - void stop_service(const ServiceHandle& handle) - { - SERVICE_STATUS service_status; +void start_service(const ServiceHandle& handle) { + if (!StartService(handle, 0, NULL)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } +void stop_service(const ServiceHandle& handle) { + SERVICE_STATUS service_status; - void uninstall_service(const ServiceHandle& handle) - { - if (!DeleteService(handle)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - bool service_exists( - const ServiceManager& mgr, - const std::string& name) - { - const auto raw = OpenServiceA( - mgr, - name.c_str(), - SERVICE_QUERY_STATUS); +void uninstall_service(const ServiceHandle& handle) { + if (!DeleteService(handle)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - if (NULL != raw) - { - ServiceHandle handle(raw); - return true; - } +bool service_exists(const ServiceManager& mgr, const std::string& name) { + const auto raw = OpenServiceA(mgr, name.c_str(), SERVICE_QUERY_STATUS); - const auto ec = GetLastError(); + if (NULL != raw) { + ServiceHandle handle(raw); + return true; + } - switch (ec) - { - case ERROR_SERVICE_DOES_NOT_EXIST: - return false; + const auto ec = GetLastError(); - default: - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + switch (ec) { + case ERROR_SERVICE_DOES_NOT_EXIST: + return false; - SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) - { - SERVICE_STATUS_PROCESS status; - DWORD nbreq; + default: + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - const auto buf_ptr = reinterpret_cast<BYTE*>(&status); - const auto buf_size = sizeof(status); +SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) { + SERVICE_STATUS_PROCESS status; + DWORD nbreq; - if (!QueryServiceStatusEx( - handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } + const auto buf_ptr = reinterpret_cast<BYTE*>(&status); + const auto buf_size = sizeof(status); - return status; - } + if (!QueryServiceStatusEx(handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - DWORD query_service_state(const ServiceHandle& handle) - { - return query_service_status(handle).dwCurrentState; - } + return status; +} - SERVICE_STATUS_PROCESS wait_for_service_state( - const ServiceHandle& handle, - const DWORD desired_state) - { - auto status = query_service_status(handle); +DWORD query_service_state(const ServiceHandle& handle) { + return query_service_status(handle).dwCurrentState; +} - DWORD old_timestamp = GetTickCount(); +SERVICE_STATUS_PROCESS wait_for_service_state( + const ServiceHandle& handle, + const DWORD desired_state +) { + auto status = query_service_status(handle); - DWORD old_check_point = status.dwCheckPoint; - DWORD old_wait_hint = status.dwWaitHint; + DWORD old_timestamp = GetTickCount(); - while (desired_state != status.dwCurrentState) - { - DWORD wait_time = old_wait_hint / 10; + DWORD old_check_point = status.dwCheckPoint; + DWORD old_wait_hint = status.dwWaitHint; - if (wait_time < 1000) - wait_time = 1000; - else if (wait_time > 10000) - wait_time = 10000; + while (desired_state != status.dwCurrentState) { + DWORD wait_time = old_wait_hint / 10; - Sleep(wait_time); + if (wait_time < 1000) + wait_time = 1000; + else if (wait_time > 10000) + wait_time = 10000; - status = query_service_status(handle); + Sleep(wait_time); - if (desired_state == status.dwCurrentState) - break; + status = query_service_status(handle); - if (status.dwCheckPoint > old_check_point) - { - old_timestamp = GetTickCount(); + if (desired_state == status.dwCurrentState) + break; - old_check_point = status.dwCheckPoint; - old_wait_hint = status.dwWaitHint; - } - else if (GetTickCount() - old_timestamp > old_wait_hint) - { - return status; - } - } + if (status.dwCheckPoint > old_check_point) { + old_timestamp = GetTickCount(); + old_check_point = status.dwCheckPoint; + old_wait_hint = status.dwWaitHint; + } else if (GetTickCount() - old_timestamp > old_wait_hint) { return status; } } - Service Service::open( - const ServiceManager& mgr, - const std::string& name) - { - return open_service(mgr, name); - } + return status; +} - Service Service::install( - const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) - { - return install_service(mgr, name, bin_path); - } +} // namespace - bool Service::exists( - const ServiceManager& mgr, - const std::string& name) - { - return service_exists(mgr, name); - } +Service Service::open(const ServiceManager& mgr, const std::string& name) { + return open_service(mgr, name); +} - void Service::start() const - { - const auto state = query_service_state(handle); +Service Service::install( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path +) { + return install_service(mgr, name, bin_path); +} - switch (state) - { - case SERVICE_STOPPED: - break; +bool Service::exists(const ServiceManager& mgr, const std::string& name) { + return service_exists(mgr, name); +} - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - break; +void Service::start() const { + const auto state = query_service_state(handle); - default: - return; - } + switch (state) { + case SERVICE_STOPPED: + break; - start_service(handle); - wait_for_service_state(handle, SERVICE_RUNNING); + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + break; + + default: + return; } - void Service::stop() const - { - switch (query_service_state(handle)) - { - case SERVICE_STOPPED: - return; + start_service(handle); + wait_for_service_state(handle, SERVICE_RUNNING); +} - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - return; - } +void Service::stop() const { + switch (query_service_state(handle)) { + case SERVICE_STOPPED: + return; - stop_service(handle); - wait_for_service_state(handle, SERVICE_STOPPED); + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + return; } - void Service::uninstall() const - { - stop(); - uninstall_service(handle); - } + stop_service(handle); + wait_for_service_state(handle, SERVICE_STOPPED); } + +void Service::uninstall() const { + stop(); + uninstall_service(handle); +} + +} // namespace service diff --git a/um/service/src/service_manager.cpp b/um/service/src/service_manager.cpp index 48c6bfe..b0e58fe 100644 --- a/um/service/src/service_manager.cpp +++ b/um/service/src/service_manager.cpp @@ -7,18 +7,17 @@ #include <Windows.h> -namespace service -{ - ServiceManager ServiceManager::open() - { - const auto raw = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); +namespace service { - if (NULL == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +ServiceManager ServiceManager::open() { + const auto raw = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); - return ServiceHandle(raw); + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } + + return ServiceHandle(raw); } + +} // namespace service diff --git a/um/service/src/windows_error.cpp b/um/service/src/windows_error.cpp index c228536..c6cca1e 100644 --- a/um/service/src/windows_error.cpp +++ b/um/service/src/windows_error.cpp @@ -9,34 +9,31 @@ #include <string> -namespace service -{ - namespace windows_error - { - std::string Category::message(int code) const - { - char* buf_ptr; +namespace service { +namespace windows_error { - const auto nbwritten = FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast<char*>(&buf_ptr), - 0, - NULL); +std::string Category::message(int code) const { + char* buf_ptr; - if (0 == nbwritten) - { - LocalFree(buf_ptr); - return "Couldn't format the error message"; - } + const auto nbwritten = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<char*>(&buf_ptr), + 0, + NULL + ); - std::string str(buf_ptr, nbwritten - 2); - LocalFree(buf_ptr); - return str; - } + if (0 == nbwritten) { + LocalFree(buf_ptr); + return "Couldn't format the error message"; } + + std::string str(buf_ptr, nbwritten - 2); + LocalFree(buf_ptr); + return str; } + +} // namespace windows_error +} // namespace service diff --git a/um/service/test/windows_error.cpp b/um/service/test/windows_error.cpp index 1334620..5f8d457 100644 --- a/um/service/test/windows_error.cpp +++ b/um/service/test/windows_error.cpp @@ -10,16 +10,10 @@ #include <exception> #include <iostream> -int main() -{ - try - { - throw service::windows_error::make( - ERROR_FILE_NOT_FOUND, - __FILE__, __LINE__, __FUNCTION__); - } - catch (const std::exception& e) - { +int main() { + try { + throw service::windows_error::make(ERROR_FILE_NOT_FOUND, __FILE__, __LINE__, __FUNCTION__); + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/install_service.cpp b/um/service/utils/install_service.cpp index c452e0d..5af98ef 100644 --- a/um/service/utils/install_service.cpp +++ b/um/service/utils/install_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 3) - { +int main(int argc, char* argv[]) { + if (argc != 3) { std::cout << "Usage: " << argv[0] << " NAME SYS_PATH\n"; return 1; } - try - { + try { service::Service::install(service::ServiceManager::open(), argv[1], argv[2]); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/start_service.cpp b/um/service/utils/start_service.cpp index 79c2d24..1743ece 100644 --- a/um/service/utils/start_service.cpp +++ b/um/service/utils/start_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 2) - { +int main(int argc, char* argv[]) { + if (argc != 2) { std::cout << "Usage: " << argv[0] << " NAME\n"; return 1; } - try - { + try { service::Service::open(service::ServiceManager::open(), argv[1]).start(); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/stop_service.cpp b/um/service/utils/stop_service.cpp index d9ccde9..fcfa67e 100644 --- a/um/service/utils/stop_service.cpp +++ b/um/service/utils/stop_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 2) - { +int main(int argc, char* argv[]) { + if (argc != 2) { std::cout << "Usage: " << argv[0] << " NAME\n"; return 1; } - try - { + try { service::Service::open(service::ServiceManager::open(), argv[1]).stop(); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/uninstall_service.cpp b/um/service/utils/uninstall_service.cpp index aa5af85..d7c105c 100644 --- a/um/service/utils/uninstall_service.cpp +++ b/um/service/utils/uninstall_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 2) - { +int main(int argc, char* argv[]) { + if (argc != 2) { std::cout << "Usage: " << argv[0] << " NAME\n"; return 1; } - try - { + try { service::Service::open(service::ServiceManager::open(), argv[1]).uninstall(); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/wrappers/nt_namespace/include/nt_namespace/device.hpp b/um/wrappers/nt_namespace/include/nt_namespace/device.hpp index a3d725c..3856f2e 100644 --- a/um/wrappers/nt_namespace/include/nt_namespace/device.hpp +++ b/um/wrappers/nt_namespace/include/nt_namespace/device.hpp @@ -9,13 +9,13 @@ #include <string> -namespace nt_namespace -{ - class Device : service::Device - { - public: - Device(); +namespace nt_namespace { - std::wstring convert_nt_path(const std::wstring&); - }; -} +class Device : service::Device { +public: + Device(); + + std::wstring convert_nt_path(const std::wstring&); +}; + +} // namespace nt_namespace diff --git a/um/wrappers/nt_namespace/src/device.cpp b/um/wrappers/nt_namespace/src/device.cpp index 4f9021b..4e1d7b0 100644 --- a/um/wrappers/nt_namespace/src/device.cpp +++ b/um/wrappers/nt_namespace/src/device.cpp @@ -12,39 +12,35 @@ #include <string> #include <vector> -namespace nt_namespace -{ - namespace - { - const auto device_path = "\\\\.\\nt_namespace"; - const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } +namespace nt_namespace { +namespace { - Device::Device() - : service::Device(service::Device::open(device_path)) - { } +const auto device_path = "\\\\.\\nt_namespace"; +const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - std::wstring Device::convert_nt_path(const std::wstring& src) - { - const auto in_buf = src.c_str(); - const auto in_buf_size = (src.size() + 1) * sizeof(wchar_t); +} // namespace - static_assert(sizeof(control_code) == sizeof(service::Device::Code), "CTL_CODE() must return DWORDs"); +Device::Device() : service::Device(service::Device::open(device_path)) {} - const auto nbreq = get_required_output_size( - static_cast<service::Device::Code>(control_code), - in_buf, - in_buf_size); +std::wstring Device::convert_nt_path(const std::wstring& src) { + const auto in_buf = src.c_str(); + const auto in_buf_size = (src.size() + 1) * sizeof(wchar_t); - std::vector<unsigned char> output(nbreq); + static_assert( + sizeof(control_code) == sizeof(service::Device::Code), "CTL_CODE() must return DWORDs" + ); - send_control_code( - static_cast<service::Device::Code>(control_code), - in_buf, - in_buf_size, - output.data(), - nbreq); + const auto nbreq = get_required_output_size( + static_cast<service::Device::Code>(control_code), in_buf, in_buf_size + ); - return reinterpret_cast<wchar_t*>(output.data()); - } + std::vector<unsigned char> output(nbreq); + + send_control_code( + static_cast<service::Device::Code>(control_code), in_buf, in_buf_size, output.data(), nbreq + ); + + return reinterpret_cast<wchar_t*>(output.data()); } + +} // namespace nt_namespace diff --git a/um/wrappers/nt_namespace/utils/convert_nt_path.cpp b/um/wrappers/nt_namespace/utils/convert_nt_path.cpp index ec215dc..b4500ee 100644 --- a/um/wrappers/nt_namespace/utils/convert_nt_path.cpp +++ b/um/wrappers/nt_namespace/utils/convert_nt_path.cpp @@ -8,16 +8,12 @@ #include <exception> #include <iostream> -int wmain(int argc, wchar_t* argv[]) -{ - try - { +int wmain(int argc, wchar_t* argv[]) { + try { nt_namespace::Device dev; for (int i = 1; i < argc; ++i) std::wcout << dev.convert_nt_path(argv[i]) << L"\n"; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/wrappers/simple/include/simple/device.hpp b/um/wrappers/simple/include/simple/device.hpp index acbda76..5d18516 100644 --- a/um/wrappers/simple/include/simple/device.hpp +++ b/um/wrappers/simple/include/simple/device.hpp @@ -7,13 +7,13 @@ #include "service/all.hpp" -namespace simple -{ - class Device : service::Device - { - public: - Device(); +namespace simple { - unsigned int exchange_ints(unsigned int) const; - }; -} +class Device : service::Device { +public: + Device(); + + unsigned int exchange_ints(unsigned int) const; +}; + +} // namespace simple diff --git a/um/wrappers/simple/src/device.cpp b/um/wrappers/simple/src/device.cpp index d1a0d42..1388c55 100644 --- a/um/wrappers/simple/src/device.cpp +++ b/um/wrappers/simple/src/device.cpp @@ -3,37 +3,38 @@ // For details, see https://github.com/egor-tensin/windows7-drivers. // Distributed under the MIT License. -#include "simple/all.hpp" - #include "service/all.hpp" +#include "simple/all.hpp" #include <Windows.h> -namespace simple -{ - namespace - { - const auto device_path = "\\\\.\\simple_device1"; - const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } +namespace simple { +namespace { - Device::Device() - : service::Device(service::Device::open(device_path)) - { } +const auto device_path = "\\\\.\\simple_device1"; +const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - unsigned int Device::exchange_ints(unsigned int src) const - { - static_assert(sizeof(exchange_ints_ctl_code) == sizeof(service::Device::Code), "CTL_CODE() must produce DWORDs"); +} // namespace - unsigned int dest; +Device::Device() : service::Device(service::Device::open(device_path)) {} - send_control_code( - static_cast<service::Device::Code>(exchange_ints_ctl_code), - &src, - sizeof(src), - &dest, - sizeof(dest)); +unsigned int Device::exchange_ints(unsigned int src) const { + static_assert( + sizeof(exchange_ints_ctl_code) == sizeof(service::Device::Code), + "CTL_CODE() must produce DWORDs" + ); - return dest; - } + unsigned int dest; + + send_control_code( + static_cast<service::Device::Code>(exchange_ints_ctl_code), + &src, + sizeof(src), + &dest, + sizeof(dest) + ); + + return dest; } + +} // namespace simple diff --git a/um/wrappers/simple/utils/exchange_ints.cpp b/um/wrappers/simple/utils/exchange_ints.cpp index cf8d70c..59f8dab 100644 --- a/um/wrappers/simple/utils/exchange_ints.cpp +++ b/um/wrappers/simple/utils/exchange_ints.cpp @@ -10,32 +10,27 @@ #include <sstream> #include <string> -namespace -{ - bool parse_int(unsigned int& dest, const std::string& src) - { - std::istringstream iss(src); - char c; - return iss >> dest && !iss.get(c); - } +namespace { + +bool parse_int(unsigned int& dest, const std::string& src) { + std::istringstream iss(src); + char c; + return iss >> dest && !iss.get(c); } -int main(int argc, char* argv[]) -{ - try - { +} // namespace + +int main(int argc, char* argv[]) { + try { unsigned int src = 0; - if (argc != 2 || !parse_int(src, argv[1])) - { + if (argc != 2 || !parse_int(src, argv[1])) { std::cout << "Usage: " << argv[0] << " N\n"; return 1; } std::cout << simple::Device().exchange_ints(src) << "\n"; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } |
