From 078dc95eb36e435c0500a6889cf1614d0ba5f33a Mon Sep 17 00:00:00 2001
From: Frans van Dorsselaer <17404029+dorssel@users.noreply.github.com>
Date: Sun, 11 Feb 2024 01:06:27 +0100
Subject: [PATCH] Refactor driver custom actions
- also fixes VBoxUSBMon logging
---
.gitattributes | 6 -
.github/workflows/build-installer.yml | 6 -
CustomActions/CPPLINT.cfg | 9 --
CustomActions/CustomActions.def | 9 --
CustomActions/CustomActions.vcxproj | 97 ----------------
CustomActions/CustomActions.vcxproj.filters | 39 -------
CustomActions/actions.cpp | 90 ---------------
CustomActions/dllmain.cpp | 16 ---
CustomActions/stdafx.cpp | 5 -
CustomActions/stdafx.h | 25 ----
Installer/Drivers.wxs | 13 ++-
Installer/Server.wxs | 10 +-
UnitTests/NativeMethods.txt | 2 -
UnitTests/ParseTestBase.cs | 4 +-
UnitTests/Parse_attach_Tests.cs | 2 -
UnitTests/Parse_bind_Tests.cs | 2 -
UnitTests/Parse_detach_Tests.cs | 2 -
UnitTests/Parse_license_Tests.cs | 2 -
UnitTests/Parse_list_Tests.cs | 2 -
UnitTests/Parse_server_Tests.cs | 2 -
UnitTests/Parse_state_Tests.cs | 2 -
UnitTests/Parse_unbind_Tests.cs | 2 -
UnitTests/Parse_usbipd_Tests.cs | 2 -
UnitTests/Parse_wsl_Tests.cs | 2 -
UnitTests/Program_Tests.cs | 2 -
...mmandHandlers.cs => CommandHandlersCli.cs} | 89 +--------------
Usbipd/CommandHandlersInstaller.cs | 107 ++++++++++++++++++
Usbipd/CommandHandlersServer.cs | 79 +++++++++++++
Usbipd/ExitCode.cs | 14 +++
Usbipd/ICommandHandlers.cs | 29 +++++
Usbipd/NativeMethods.txt | 9 ++
Usbipd/Program.cs | 39 +++++--
Usbipd/Wsl.cs | 1 -
usbipd-win.sln | 6 -
34 files changed, 287 insertions(+), 439 deletions(-)
delete mode 100644 CustomActions/CPPLINT.cfg
delete mode 100644 CustomActions/CustomActions.def
delete mode 100644 CustomActions/CustomActions.vcxproj
delete mode 100644 CustomActions/CustomActions.vcxproj.filters
delete mode 100644 CustomActions/actions.cpp
delete mode 100644 CustomActions/dllmain.cpp
delete mode 100644 CustomActions/stdafx.cpp
delete mode 100644 CustomActions/stdafx.h
rename Usbipd/{CommandHandlers.cs => CommandHandlersCli.cs} (79%)
create mode 100644 Usbipd/CommandHandlersInstaller.cs
create mode 100644 Usbipd/CommandHandlersServer.cs
create mode 100644 Usbipd/ExitCode.cs
create mode 100644 Usbipd/ICommandHandlers.cs
diff --git a/.gitattributes b/.gitattributes
index 688dfcb..aa1c1a5 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -22,12 +22,6 @@
*.cs text eol=crlf
*.props text
-*.vcxproj text
-*.filters text
-*.cpp text
-*.h text
-*.def text
-
*.wixproj text
*.wxs text
*.xslt text
diff --git a/.github/workflows/build-installer.yml b/.github/workflows/build-installer.yml
index 7abf64e..9b5ea81 100644
--- a/.github/workflows/build-installer.yml
+++ b/.github/workflows/build-installer.yml
@@ -59,12 +59,6 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- - name: Add msbuild to PATH
- uses: microsoft/setup-msbuild@v2
-
- - name: Build (CustomActions)
- run: msbuild -p:Configuration=Release CustomActions
-
- name: Build (Installer)
run: dotnet build --configuration Release Installer
diff --git a/CustomActions/CPPLINT.cfg b/CustomActions/CPPLINT.cfg
deleted file mode 100644
index da4743a..0000000
--- a/CustomActions/CPPLINT.cfg
+++ /dev/null
@@ -1,9 +0,0 @@
-# SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
-#
-# SPDX-License-Identifier: GPL-3.0-only
-
-set noparent
-linelength=160
-
-# incompatible with pre-compiled headers
-filter=-build/include_subdir
diff --git a/CustomActions/CustomActions.def b/CustomActions/CustomActions.def
deleted file mode 100644
index 16d4209..0000000
--- a/CustomActions/CustomActions.def
+++ /dev/null
@@ -1,9 +0,0 @@
-; SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
-;
-; SPDX-License-Identifier: GPL-3.0-only
-
-LIBRARY
-
-EXPORTS
- InstallDrivers
- UninstallDrivers
diff --git a/CustomActions/CustomActions.vcxproj b/CustomActions/CustomActions.vcxproj
deleted file mode 100644
index e174ec6..0000000
--- a/CustomActions/CustomActions.vcxproj
+++ /dev/null
@@ -1,97 +0,0 @@
-
-
-
-
-
- Release
- x64
-
-
-
- 17.0
- Win32Proj
- {cbc206bd-0e45-4514-b148-9035c7459268}
- CustomActions
- 10.0
-
-
-
- DynamicLibrary
- false
- v143
- true
- Unicode
-
-
-
-
-
-
-
-
-
-
-
- false
- false
- bin\$(Platform)\$(Configuration)\
- obj\$(Platform)\$(Configuration)\
-
-
-
- Level3
- true
- true
- true
- NDEBUG;CUSTOMACTIONS_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
- true
- Use
- MultiThreaded
- stdcpp20
- stdc17
- MinSpace
- Size
- true
- None
-
-
- Windows
- true
- true
- false
- CustomActions.def
- true
- 10.0
-
-
-
- false
-
- UseLinkTimeCodeGeneration
-
-
-
- /NOIMPLIB /NOEXP %(AdditionalOptions)
-
-
-
-
-
-
-
-
-
- Create
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/CustomActions/CustomActions.vcxproj.filters b/CustomActions/CustomActions.vcxproj.filters
deleted file mode 100644
index 5189d10..0000000
--- a/CustomActions/CustomActions.vcxproj.filters
+++ /dev/null
@@ -1,39 +0,0 @@
-
-
-
-
-
- {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
- cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
-
-
- {93995380-89BD-4b04-88EB-625FBE52EBFB}
- h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
-
-
-
-
- Header Files
-
-
-
-
- Source Files
-
-
- Source Files
-
-
- Source Files
-
-
-
-
- Source Files
-
-
-
\ No newline at end of file
diff --git a/CustomActions/actions.cpp b/CustomActions/actions.cpp
deleted file mode 100644
index 094af5b..0000000
--- a/CustomActions/actions.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
-//
-// SPDX-License-Identifier: GPL-3.0-only
-
-#include "stdafx.h"
-
-using std::wstring;
-
-
-static void log(MSIHANDLE hInstall, wstring fmt, ...) {
- auto hRecord = MsiCreateRecord(0);
- if (!hRecord) {
- return;
- }
- va_list ap;
- va_start(ap, fmt);
- va_list ap_copy;
- va_copy(ap_copy, ap);
- size_t len = _vscwprintf(fmt.c_str(), ap_copy);
- va_end(ap_copy);
- wstring message;
- message.resize(len);
- vswprintf_s(message.data(), len + 1, fmt.c_str(), ap);
- va_end(ap);
- MsiRecordSetString(hRecord, 0, (L"CustomActions: " + message).c_str());
- MsiProcessMessage(hInstall, INSTALLMESSAGE_INFO, hRecord);
- MsiCloseHandle(hRecord);
-}
-
-
-static wstring get_property(MSIHANDLE hInstall, const wstring& name) {
- DWORD valueSize = 0;
- if (MsiGetProperty(hInstall, name.c_str(), (LPWSTR)L"", &valueSize) != ERROR_MORE_DATA) {
- return wstring();
- }
- ++valueSize; // add NUL
- auto value = std::make_unique(valueSize);
- if (MsiGetProperty(hInstall, name.c_str(), value.get(), &valueSize) != ERROR_SUCCESS) {
- return wstring();
- }
- return wstring(value.get(), valueSize);
-}
-
-
-// This action must run deferred, between InstallFiles and InstallFinalize.
-UINT __stdcall InstallDrivers(MSIHANDLE hInstall) {
- auto data = get_property(hInstall, L"CustomActionData");
- {
- // See: https://learn.microsoft.com/en-us/windows-hardware/drivers/install/preinstalling-driver-packages
- log(hInstall, L"Installing VBoxUSB");
- if (!SetupCopyOEMInf((data + L"Drivers\\VBoxUSB.inf").c_str(), NULL, SPOST_PATH, 0, NULL, 0, NULL, NULL)) {
- log(hInstall, L"ERROR installing VBoxUSB: 0x%08x", GetLastError());
- return ERROR_INSTALL_FAILURE;
- }
- }
- {
- log(hInstall, L"Installing VBoxUSBMon");
- SC_HANDLE manager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
- if (!manager) {
- log(hInstall, L"ERROR OpenSCManager: 0x%08x", GetLastError());
- return ERROR_INSTALL_FAILURE;
- }
- SC_HANDLE service = CreateService(manager, L"VBoxUSBMon", L"VirtualBox USB Monitor Service", GENERIC_ALL, SERVICE_KERNEL_DRIVER,
- SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, (data + L"Drivers\\VBoxUSBMon.sys").c_str(), NULL, NULL, NULL, NULL, NULL);
- if (!service) {
- log(hInstall, L"ERROR CreateService: 0x%08x", GetLastError());
- CloseServiceHandle(manager);
- return ERROR_INSTALL_FAILURE;
- }
- CloseServiceHandle(service);
- CloseServiceHandle(manager);
- }
- return ERROR_SUCCESS;
-}
-
-
-// This action must run deferred, between InstallFiles and InstallFinalize.
-UINT __stdcall UninstallDrivers(MSIHANDLE hInstall) {
- auto data = get_property(hInstall, L"CustomActionData");
- {
- BOOL need_reboot = FALSE;
- log(hInstall, L"Uninstalling VBoxUSB");
- if (!DiUninstallDriver(NULL, (data + L"Drivers\\VBoxUSB.inf").c_str(), 0, &need_reboot)) {
- log(hInstall, L"ERROR uninstalling VBoxUSB: 0x%08x", GetLastError());
- // continue
- }
- // ignore need_reboot
- }
- return ERROR_SUCCESS;
-}
diff --git a/CustomActions/dllmain.cpp b/CustomActions/dllmain.cpp
deleted file mode 100644
index dcfc61f..0000000
--- a/CustomActions/dllmain.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
-//
-// SPDX-License-Identifier: GPL-3.0-only
-
-#include "stdafx.h"
-
-BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
- switch (ul_reason_for_call) {
- case DLL_PROCESS_ATTACH:
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
-}
diff --git a/CustomActions/stdafx.cpp b/CustomActions/stdafx.cpp
deleted file mode 100644
index d2f4c93..0000000
--- a/CustomActions/stdafx.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
-//
-// SPDX-License-Identifier: GPL-3.0-only
-
-#include "stdafx.h"
diff --git a/CustomActions/stdafx.h b/CustomActions/stdafx.h
deleted file mode 100644
index f9ad1a3..0000000
--- a/CustomActions/stdafx.h
+++ /dev/null
@@ -1,25 +0,0 @@
-// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
-//
-// SPDX-License-Identifier: GPL-3.0-only
-
-#pragma once
-
-#include
-#include
-#include
-
-// Target Windows 10 or newer (including Windows Server 2019) only
-#define WINVER 0x0A00
-#define _WIN32_WINNT 0x0A00
-
-#define WIN32_LEAN_AND_MEAN
-
-// Windows Header Files
-#include
-#include
-#include
-#pragma comment(lib, "msi.lib")
-#include
-#pragma comment(lib, "newdev.lib")
-#include
-#pragma comment(lib, "setupapi.lib")
diff --git a/Installer/Drivers.wxs b/Installer/Drivers.wxs
index f147265..0533349 100644
--- a/Installer/Drivers.wxs
+++ b/Installer/Drivers.wxs
@@ -4,7 +4,10 @@ SPDX-FileCopyrightText: 2020 Frans van Dorsselaer
SPDX-License-Identifier: GPL-3.0-only
-->
-
+
@@ -22,6 +25,14 @@ SPDX-License-Identifier: GPL-3.0-only
+
diff --git a/Installer/Server.wxs b/Installer/Server.wxs
index 52aaeb1..548fa45 100644
--- a/Installer/Server.wxs
+++ b/Installer/Server.wxs
@@ -90,19 +90,17 @@ SPDX-License-Identifier: GPL-3.0-only
-
-
-
-
+
+
-
-
+
+