From 7be3d976a223220e8e48896a401ac7e56e40ce62 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 27 Apr 2017 22:35:21 +0300 Subject: um: reorganize projects * libservice -> service * libsimple -> wrappers/simple * libnt_path_converter -> wrappers/special/nt_path_converter --- um/CMakeLists.txt | 6 +- um/README.md | 10 +- um/libnt_path_converter/CMakeLists.txt | 10 - um/libnt_path_converter/README.md | 31 --- .../include/libnt_path_converter/all.hpp | 8 - .../include/libnt_path_converter/device.hpp | 21 -- um/libnt_path_converter/src/device.cpp | 48 ---- um/libnt_path_converter/utils/CMakeLists.txt | 2 - um/libnt_path_converter/utils/convert_nt_path.cpp | 26 -- um/libservice/CMakeLists.txt | 10 - um/libservice/README.md | 53 ---- um/libservice/include/libservice/all.hpp | 15 -- um/libservice/include/libservice/common.hpp | 17 -- um/libservice/include/libservice/device.hpp | 75 ------ um/libservice/include/libservice/handle.hpp | 77 ------ um/libservice/include/libservice/service.hpp | 73 ------ .../include/libservice/service_handle.hpp | 76 ------ .../include/libservice/service_manager.hpp | 63 ----- um/libservice/include/libservice/singleton.hpp | 43 ---- um/libservice/include/libservice/windows_error.hpp | 31 --- um/libservice/src/device.cpp | 136 ---------- um/libservice/src/handle.cpp | 27 -- um/libservice/src/service.cpp | 276 --------------------- um/libservice/src/service_handle.cpp | 27 -- um/libservice/src/service_manager.cpp | 44 ---- um/libservice/src/windows_error.cpp | 39 --- um/libservice/test/CMakeLists.txt | 4 - um/libservice/test/windows_error.cpp | 29 --- um/libservice/utils/CMakeLists.txt | 11 - um/libservice/utils/install_service.cpp | 29 --- um/libservice/utils/start_service.cpp | 29 --- um/libservice/utils/stop_service.cpp | 29 --- um/libservice/utils/uninstall_service.cpp | 29 --- um/libsimple/CMakeLists.txt | 9 - um/libsimple/README.md | 43 ---- um/libsimple/include/libsimple/all.hpp | 8 - um/libsimple/include/libsimple/device.hpp | 19 -- um/libsimple/src/device.cpp | 37 --- um/libsimple/utils/CMakeLists.txt | 2 - um/libsimple/utils/exchange_ints.cpp | 44 ---- um/service/CMakeLists.txt | 10 + um/service/README.md | 53 ++++ um/service/include/libservice/all.hpp | 15 ++ um/service/include/libservice/common.hpp | 17 ++ um/service/include/libservice/device.hpp | 75 ++++++ um/service/include/libservice/handle.hpp | 77 ++++++ um/service/include/libservice/service.hpp | 73 ++++++ um/service/include/libservice/service_handle.hpp | 76 ++++++ um/service/include/libservice/service_manager.hpp | 63 +++++ um/service/include/libservice/singleton.hpp | 43 ++++ um/service/include/libservice/windows_error.hpp | 31 +++ um/service/src/device.cpp | 136 ++++++++++ um/service/src/handle.cpp | 27 ++ um/service/src/service.cpp | 276 +++++++++++++++++++++ um/service/src/service_handle.cpp | 27 ++ um/service/src/service_manager.cpp | 44 ++++ um/service/src/windows_error.cpp | 39 +++ um/service/test/CMakeLists.txt | 4 + um/service/test/windows_error.cpp | 29 +++ um/service/utils/CMakeLists.txt | 11 + um/service/utils/install_service.cpp | 29 +++ um/service/utils/start_service.cpp | 29 +++ um/service/utils/stop_service.cpp | 29 +++ um/service/utils/uninstall_service.cpp | 29 +++ um/wrappers/CMakeLists.txt | 2 + um/wrappers/simple/CMakeLists.txt | 9 + um/wrappers/simple/README.md | 43 ++++ um/wrappers/simple/include/libsimple/all.hpp | 8 + um/wrappers/simple/include/libsimple/device.hpp | 19 ++ um/wrappers/simple/src/device.cpp | 37 +++ um/wrappers/simple/utils/CMakeLists.txt | 2 + um/wrappers/simple/utils/exchange_ints.cpp | 44 ++++ um/wrappers/special/CMakeLists.txt | 1 + .../special/nt_path_converter/CMakeLists.txt | 10 + um/wrappers/special/nt_path_converter/README.md | 31 +++ .../include/libnt_path_converter/all.hpp | 8 + .../include/libnt_path_converter/device.hpp | 21 ++ .../special/nt_path_converter/src/device.cpp | 48 ++++ .../special/nt_path_converter/utils/CMakeLists.txt | 2 + .../nt_path_converter/utils/convert_nt_path.cpp | 26 ++ 80 files changed, 1560 insertions(+), 1559 deletions(-) delete mode 100644 um/libnt_path_converter/CMakeLists.txt delete mode 100644 um/libnt_path_converter/README.md delete mode 100644 um/libnt_path_converter/include/libnt_path_converter/all.hpp delete mode 100644 um/libnt_path_converter/include/libnt_path_converter/device.hpp delete mode 100644 um/libnt_path_converter/src/device.cpp delete mode 100644 um/libnt_path_converter/utils/CMakeLists.txt delete mode 100644 um/libnt_path_converter/utils/convert_nt_path.cpp delete mode 100644 um/libservice/CMakeLists.txt delete mode 100644 um/libservice/README.md delete mode 100644 um/libservice/include/libservice/all.hpp delete mode 100644 um/libservice/include/libservice/common.hpp delete mode 100644 um/libservice/include/libservice/device.hpp delete mode 100644 um/libservice/include/libservice/handle.hpp delete mode 100644 um/libservice/include/libservice/service.hpp delete mode 100644 um/libservice/include/libservice/service_handle.hpp delete mode 100644 um/libservice/include/libservice/service_manager.hpp delete mode 100644 um/libservice/include/libservice/singleton.hpp delete mode 100644 um/libservice/include/libservice/windows_error.hpp delete mode 100644 um/libservice/src/device.cpp delete mode 100644 um/libservice/src/handle.cpp delete mode 100644 um/libservice/src/service.cpp delete mode 100644 um/libservice/src/service_handle.cpp delete mode 100644 um/libservice/src/service_manager.cpp delete mode 100644 um/libservice/src/windows_error.cpp delete mode 100644 um/libservice/test/CMakeLists.txt delete mode 100644 um/libservice/test/windows_error.cpp delete mode 100644 um/libservice/utils/CMakeLists.txt delete mode 100644 um/libservice/utils/install_service.cpp delete mode 100644 um/libservice/utils/start_service.cpp delete mode 100644 um/libservice/utils/stop_service.cpp delete mode 100644 um/libservice/utils/uninstall_service.cpp delete mode 100644 um/libsimple/CMakeLists.txt delete mode 100644 um/libsimple/README.md delete mode 100644 um/libsimple/include/libsimple/all.hpp delete mode 100644 um/libsimple/include/libsimple/device.hpp delete mode 100644 um/libsimple/src/device.cpp delete mode 100644 um/libsimple/utils/CMakeLists.txt delete mode 100644 um/libsimple/utils/exchange_ints.cpp create mode 100644 um/service/CMakeLists.txt create mode 100644 um/service/README.md create mode 100644 um/service/include/libservice/all.hpp create mode 100644 um/service/include/libservice/common.hpp create mode 100644 um/service/include/libservice/device.hpp create mode 100644 um/service/include/libservice/handle.hpp create mode 100644 um/service/include/libservice/service.hpp create mode 100644 um/service/include/libservice/service_handle.hpp create mode 100644 um/service/include/libservice/service_manager.hpp create mode 100644 um/service/include/libservice/singleton.hpp create mode 100644 um/service/include/libservice/windows_error.hpp create mode 100644 um/service/src/device.cpp create mode 100644 um/service/src/handle.cpp create mode 100644 um/service/src/service.cpp create mode 100644 um/service/src/service_handle.cpp create mode 100644 um/service/src/service_manager.cpp create mode 100644 um/service/src/windows_error.cpp create mode 100644 um/service/test/CMakeLists.txt create mode 100644 um/service/test/windows_error.cpp create mode 100644 um/service/utils/CMakeLists.txt create mode 100644 um/service/utils/install_service.cpp create mode 100644 um/service/utils/start_service.cpp create mode 100644 um/service/utils/stop_service.cpp create mode 100644 um/service/utils/uninstall_service.cpp create mode 100644 um/wrappers/CMakeLists.txt create mode 100644 um/wrappers/simple/CMakeLists.txt create mode 100644 um/wrappers/simple/README.md create mode 100644 um/wrappers/simple/include/libsimple/all.hpp create mode 100644 um/wrappers/simple/include/libsimple/device.hpp create mode 100644 um/wrappers/simple/src/device.cpp create mode 100644 um/wrappers/simple/utils/CMakeLists.txt create mode 100644 um/wrappers/simple/utils/exchange_ints.cpp create mode 100644 um/wrappers/special/CMakeLists.txt create mode 100644 um/wrappers/special/nt_path_converter/CMakeLists.txt create mode 100644 um/wrappers/special/nt_path_converter/README.md create mode 100644 um/wrappers/special/nt_path_converter/include/libnt_path_converter/all.hpp create mode 100644 um/wrappers/special/nt_path_converter/include/libnt_path_converter/device.hpp create mode 100644 um/wrappers/special/nt_path_converter/src/device.cpp create mode 100644 um/wrappers/special/nt_path_converter/utils/CMakeLists.txt create mode 100644 um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp (limited to 'um') diff --git a/um/CMakeLists.txt b/um/CMakeLists.txt index 31c8511..f7d8b24 100644 --- a/um/CMakeLists.txt +++ b/um/CMakeLists.txt @@ -1,6 +1,4 @@ project(windows7_drivers_utils) -add_subdirectory(libservice) - -add_subdirectory(libnt_path_converter) -add_subdirectory(libsimple) +add_subdirectory(service) +add_subdirectory(wrappers) diff --git a/um/README.md b/um/README.md index f18091a..e926e9a 100644 --- a/um/README.md +++ b/um/README.md @@ -7,10 +7,10 @@ A couple of usage examples are included along with the drivers. * [libsimple]: [simple] driver usage examples. * [libnt_path_converter]: [nt_path_converter] driver usage examples. -[libservice]: libservice/README.md -[libsimple]: libsimple/README.md +[libservice]: service/README.md +[libsimple]: wrappers/simple/README.md [simple]: ../km/src/simple -[libnt_path_converter]: libnt_path_converter/README.md +[libnt_path_converter]: wrappers/special/nt_path_converter/README.md [nt_path_converter]: ../km/src/special/nt_path_converter Building the utilities @@ -22,12 +22,12 @@ For example, using Visual Studio 2013 Update 4 for Windows Desktop (targetting x86): > cd - C:\workspace\build\windows7-drivers + C:\workspace\build\windows7-drivers\um > cmake -G "Visual Studio 12 2013" C:\workspace\personal\windows7-drivers\um ... - > msbuild windows7_drivers_utils.sln + > cmake --build . --config release ... See also diff --git a/um/libnt_path_converter/CMakeLists.txt b/um/libnt_path_converter/CMakeLists.txt deleted file mode 100644 index 69f85d0..0000000 --- a/um/libnt_path_converter/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -project(libnt_path_converter) -file(GLOB libnt_path_converter_sources "src/*.cpp") -file(GLOB_RECURSE libnt_path_converter_headers "include/*.hpp") -add_library(libnt_path_converter - ${libnt_path_converter_sources} - ${libnt_path_converter_headers}) -target_link_libraries(libnt_path_converter libservice) -target_include_directories(libnt_path_converter PUBLIC include/) - -add_subdirectory(utils) diff --git a/um/libnt_path_converter/README.md b/um/libnt_path_converter/README.md deleted file mode 100644 index 74653dc..0000000 --- a/um/libnt_path_converter/README.md +++ /dev/null @@ -1,31 +0,0 @@ -nt_path_converter driver utilities -================================== - -[nt_path_converter] driver usage examples. - -[nt_path_converter]: ../../km/src/special/nt_path_converter - -Usage ------ - -### convert_nt_path.exe - - Usage: convert_nt_path.exe [NT_PATH...] - -Converts a NT-style path to a DOS-style path. -The NT namespace can be explored using the [WinObj] utility. -For example: - - > convert_nt_path.exe \Device\HarddiskVolume2\Windows - C:\Windows - -[WinObj]: https://technet.microsoft.com/en-us/library/bb896657.aspx - -See also --------- - -* [Building the utilities] -* [License] - -[Building the utilities]: ../README.md#building-the-utilities -[License]: ../../README.md#license diff --git a/um/libnt_path_converter/include/libnt_path_converter/all.hpp b/um/libnt_path_converter/include/libnt_path_converter/all.hpp deleted file mode 100644 index 474b802..0000000 --- a/um/libnt_path_converter/include/libnt_path_converter/all.hpp +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "device.hpp" diff --git a/um/libnt_path_converter/include/libnt_path_converter/device.hpp b/um/libnt_path_converter/include/libnt_path_converter/device.hpp deleted file mode 100644 index e1d75fb..0000000 --- a/um/libnt_path_converter/include/libnt_path_converter/device.hpp +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "libservice/all.hpp" - -#include - -namespace libnt_path_converter -{ - class Device : libservice::Device - { - public: - Device(); - - std::wstring convert_nt_path(const std::wstring&); - }; -} diff --git a/um/libnt_path_converter/src/device.cpp b/um/libnt_path_converter/src/device.cpp deleted file mode 100644 index 90cd12f..0000000 --- a/um/libnt_path_converter/src/device.cpp +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libnt_path_converter/device.hpp" - -#include "libservice/all.hpp" - -#include - -#include -#include - -namespace libnt_path_converter -{ - namespace - { - const char* const device_path = "\\\\.\\nt_path_converter"; - const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } - - Device::Device() - : libservice::Device(libservice::Device::open(device_path)) - { } - - 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); - - const auto nbreq = get_required_output_size( - control_code, - in_buf, - in_buf_size); - - std::vector output(nbreq); - - send_control_code( - control_code, - in_buf, - in_buf_size, - output.data(), - nbreq); - - return reinterpret_cast(output.data()); - } -} diff --git a/um/libnt_path_converter/utils/CMakeLists.txt b/um/libnt_path_converter/utils/CMakeLists.txt deleted file mode 100644 index 9915995..0000000 --- a/um/libnt_path_converter/utils/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(convert_nt_path convert_nt_path.cpp) -target_link_libraries(convert_nt_path libnt_path_converter) diff --git a/um/libnt_path_converter/utils/convert_nt_path.cpp b/um/libnt_path_converter/utils/convert_nt_path.cpp deleted file mode 100644 index 0055db2..0000000 --- a/um/libnt_path_converter/utils/convert_nt_path.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libnt_path_converter/all.hpp" - -#include -#include - -int wmain(int argc, wchar_t* argv[]) -{ - try - { - libnt_path_converter::Device dev; - for (int i = 1; i < argc; ++i) - std::wcout << dev.convert_nt_path(argv[i]) << L"\n"; - } - catch (const std::exception& e) - { - std::wcerr << e.what() << "\n"; - return 1; - } - - return 0; -} diff --git a/um/libservice/CMakeLists.txt b/um/libservice/CMakeLists.txt deleted file mode 100644 index 34b3d13..0000000 --- a/um/libservice/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -file(GLOB libservice_sources "src/*.cpp") -file(GLOB_RECURSE libservice_headers "include/*.hpp") -add_library(libservice - ${libservice_sources} - ${libservice_headers}) -target_include_directories(libservice PUBLIC include) -target_compile_definitions(libservice PRIVATE NOMINMAX) - -add_subdirectory(test) -add_subdirectory(utils) diff --git a/um/libservice/README.md b/um/libservice/README.md deleted file mode 100644 index f9fb601..0000000 --- a/um/libservice/README.md +++ /dev/null @@ -1,53 +0,0 @@ -Driver management utilities -=========================== - -Utilities to load/unload the drivers. - -Usage ------ - -### install_service.exe - - Usage: install_service.exe NAME SYS_PATH - -Installs a driver as a service. -The same as - - > sc create NAME type= kernel binPath= SYS_PATH - -### start_service.exe - - Usage: start_service.exe NAME - -Starts the service `NAME` (loading the corresponding driver). -The same as - - > net start NAME - -### stop_service.exe - - Usage: stop_service.exe NAME - -Stops the service `NAME` (unloading the corresponding driver). -The same as - - > net stop NAME - -### uninstall_service.exe - - Usage: uninstall_service.exe NAME - -Uninstalls the service `NAME`, wiping the corresponding record from the -registry. -The same as - - > sc delete NAME - -See also --------- - -* [Building the utilities] -* [License] - -[Building the utilities]: ../README.md#building-the-utilities -[License]: ../../README.md#license diff --git a/um/libservice/include/libservice/all.hpp b/um/libservice/include/libservice/all.hpp deleted file mode 100644 index a5761e0..0000000 --- a/um/libservice/include/libservice/all.hpp +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" -#include "device.hpp" -#include "handle.hpp" -#include "service.hpp" -#include "service_handle.hpp" -#include "service_manager.hpp" -#include "singleton.hpp" -#include "windows_error.hpp" diff --git a/um/libservice/include/libservice/common.hpp b/um/libservice/include/libservice/common.hpp deleted file mode 100644 index a1c46fb..0000000 --- a/um/libservice/include/libservice/common.hpp +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#define LIBSERVICE_FILE_PATH __FILE__ -#define LIBSERVICE_LINE_NUMBER __LINE__ -#define LIBSERVICE_FUNCTION_NAME __FUNCTION__ - -#define LIBSERVICE_TO_STRING(s) LIBSERVICE_TO_STRING_(s) -#define LIBSERVICE_TO_STRING_(s) #s - -#define LIBSERVICE_LINE_NUMBER_STRING LIBSERVICE_TO_STRING(LIBSERVICE_LINE_NUMBER) - -#define LIBSERVICE_NOEXCEPT throw() diff --git a/um/libservice/include/libservice/device.hpp b/um/libservice/include/libservice/device.hpp deleted file mode 100644 index ac292c8..0000000 --- a/um/libservice/include/libservice/device.hpp +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" -#include "handle.hpp" - -#include - -#include - -#include -#include - -namespace libservice -{ - class Device - { - public: - typedef DWORD Code; - - static Device open(const std::string& path); - - Device(Device&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - Device& operator=(Device other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } - - void swap(Device& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } - - std::size_t get_required_output_size( - Code code, - const void* in_buf, - std::size_t in_buf_size) const; - - 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; - - private: - Device(Handle handle) - : handle(std::move(handle)) - { } - - Handle handle; - - Device(const Device&) = delete; - }; - - void swap(Device&, Device&) LIBSERVICE_NOEXCEPT; -} - -namespace std -{ - template <> - void swap( - libservice::Device&, - libservice::Device&) LIBSERVICE_NOEXCEPT; -} diff --git a/um/libservice/include/libservice/handle.hpp b/um/libservice/include/libservice/handle.hpp deleted file mode 100644 index 5e351d2..0000000 --- a/um/libservice/include/libservice/handle.hpp +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" - -#include - -#include -#include -#include - -namespace libservice -{ - class Handle - { - public: - Handle() = default; - - Handle(HANDLE raw) - : impl(raw) - { } - - Handle(Handle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } - - operator bool() const - { - return static_cast(impl); - } - - operator HANDLE() const - { - return impl.get(); - } - - void swap(Handle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } - - private: - struct Deleter - { - void operator()(HANDLE raw) - { - CloseHandle(raw); - } - }; - - std::unique_ptr::type, Deleter> impl; - - Handle(const Handle&) = delete; - }; - - void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT; -} - -namespace std -{ - template <> - void swap( - libservice::Handle& a, - libservice::Handle& b) LIBSERVICE_NOEXCEPT; -} diff --git a/um/libservice/include/libservice/service.hpp b/um/libservice/include/libservice/service.hpp deleted file mode 100644 index 089f790..0000000 --- a/um/libservice/include/libservice/service.hpp +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" -#include "service_handle.hpp" -#include "service_manager.hpp" - -#include -#include - -namespace libservice -{ - class Service - { - public: - static bool exists( - const ServiceManager&, - const std::string& name); - - static Service open( - const ServiceManager&, - const std::string& name); - - static Service install( - const ServiceManager&, - const std::string& name, - const std::string& bin_path); - - void start() const; - void stop() const; - void uninstall() const; - - Service(Service&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - Service& operator=(Service other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } - - void swap(Service& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } - - private: - Service(ServiceHandle handle) - : handle(std::move(handle)) - { } - - ServiceHandle handle; - - Service(const Service&) = delete; - }; - - void swap(Service&, Service&) LIBSERVICE_NOEXCEPT; -} - -namespace std -{ - template <> - void swap( - libservice::Service&, - libservice::Service&) LIBSERVICE_NOEXCEPT; -} diff --git a/um/libservice/include/libservice/service_handle.hpp b/um/libservice/include/libservice/service_handle.hpp deleted file mode 100644 index 2883ff3..0000000 --- a/um/libservice/include/libservice/service_handle.hpp +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" - -#include - -#include -#include - -namespace libservice -{ - class ServiceHandle - { - public: - ServiceHandle() = default; - - ServiceHandle(SC_HANDLE raw) - : impl(raw) - { } - - ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } - - operator bool() const - { - return static_cast(impl); - } - - operator SC_HANDLE() const - { - return impl.get(); - } - - void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } - - private: - struct Deleter - { - void operator()(SC_HANDLE raw) - { - CloseServiceHandle(raw); - } - }; - - std::unique_ptr impl; - - ServiceHandle(const ServiceHandle&) = delete; - }; - - void swap(ServiceHandle&, ServiceHandle&) LIBSERVICE_NOEXCEPT; -} - -namespace std -{ - template <> - void swap( - libservice::ServiceHandle&, - libservice::ServiceHandle&) LIBSERVICE_NOEXCEPT; -} diff --git a/um/libservice/include/libservice/service_manager.hpp b/um/libservice/include/libservice/service_manager.hpp deleted file mode 100644 index 80e6f12..0000000 --- a/um/libservice/include/libservice/service_manager.hpp +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" -#include "service_handle.hpp" - -#include - -#include - -namespace libservice -{ - class ServiceManager - { - public: - static ServiceManager open(); - - ServiceManager(ServiceManager&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - ServiceManager& operator=(ServiceManager other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } - - void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } - - operator SC_HANDLE() const - { - return handle; - } - - private: - ServiceManager(ServiceHandle handle) - : handle(std::move(handle)) - { } - - ServiceHandle handle; - - ServiceManager(const ServiceManager&) = delete; - }; - - void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT; -} - -namespace std -{ - template <> - void swap( - libservice::ServiceManager&, - libservice::ServiceManager&) LIBSERVICE_NOEXCEPT; -} diff --git a/um/libservice/include/libservice/singleton.hpp b/um/libservice/include/libservice/singleton.hpp deleted file mode 100644 index 1c7b1a7..0000000 --- a/um/libservice/include/libservice/singleton.hpp +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include - -namespace libservice -{ - template - class Singleton - { - public: - static DerivedT& get() - { - std::call_once(initialized, initialize); - return get_unsafe(); - } - - protected: - Singleton() = default; - virtual ~Singleton() = default; - - private: - static void initialize() - { - get_unsafe(); - } - - static DerivedT& get_unsafe() - { - static DerivedT instance; - return instance; - } - - static std::once_flag initialized; - }; - - template - std::once_flag Singleton::initialized; -} diff --git a/um/libservice/include/libservice/windows_error.hpp b/um/libservice/include/libservice/windows_error.hpp deleted file mode 100644 index f7ac90e..0000000 --- a/um/libservice/include/libservice/windows_error.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "common.hpp" -#include "singleton.hpp" - -#include -#include - -namespace libservice -{ - class WindowsErrorCategory - : public std::error_category - , public Singleton - { - public: - const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; } - - std::string message(int) const; - - private: - friend class Singleton; - }; -} - -#define LIBSERVICE_ERROR_PREFIX \ - "Error in function '" LIBSERVICE_FUNCTION_NAME "' at file '" LIBSERVICE_FILE_PATH "', line " LIBSERVICE_LINE_NUMBER_STRING diff --git a/um/libservice/src/device.cpp b/um/libservice/src/device.cpp deleted file mode 100644 index 5643106..0000000 --- a/um/libservice/src/device.cpp +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -#include - -#include -#include -#include -#include -#include - -namespace libservice -{ - 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); - - if (INVALID_HANDLE_VALUE == raw) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - - return Handle(raw); - } - } - - Device Device::open(const std::string& path) - { - return Device(open_device(path)); - } - - std::size_t Device::get_required_output_size( - Code code, - const void* in_buf, - std::size_t in_buf_size) const - { - DWORD nbreq; - - if (in_buf_size > std::numeric_limits::max()) - throw std::range_error("input buffer size is too large"); - - std::size_t nbwritten = DeviceIoControl( - handle, - code, - const_cast(in_buf), - static_cast(in_buf_size), - NULL, - 0, - &nbreq, - NULL); - - if (0 == nbwritten) - { - const auto ec = GetLastError(); - - switch (ec) - { - case ERROR_MORE_DATA: - return nbreq; - - default: - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - } - - return nbwritten; - } - - 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; - - if (in_buf_size > std::numeric_limits::max()) - throw std::range_error("input buffer size is too large"); - if (out_buf_size > std::numeric_limits::max()) - throw std::range_error("output buffer size is too large"); - - std::size_t nbwritten = DeviceIoControl( - handle, - code, - const_cast(in_buf), - static_cast(in_buf_size), - out_buf, - static_cast(out_buf_size), - &nbreq, - NULL); - - if (0 == nbwritten) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - - return nbwritten; - } - - void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} - -namespace std -{ - template <> - void swap( - libservice::Device& a, - libservice::Device& b) - { - a.swap(b); - } -} diff --git a/um/libservice/src/handle.cpp b/um/libservice/src/handle.cpp deleted file mode 100644 index 7567781..0000000 --- a/um/libservice/src/handle.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -namespace libservice -{ - void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} - -namespace std -{ - template <> - void swap( - libservice::Handle& a, - libservice::Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} diff --git a/um/libservice/src/service.cpp b/um/libservice/src/service.cpp deleted file mode 100644 index 8b5e043..0000000 --- a/um/libservice/src/service.cpp +++ /dev/null @@ -1,276 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -#include -#include -#include -#include - -namespace libservice -{ - namespace - { - ServiceHandle open_service( - const ServiceManager& mgr, - const std::string& name) - { - const auto raw = OpenServiceA( - mgr, - name.c_str(), - SERVICE_ALL_ACCESS); - - if (NULL == raw) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - - return raw; - } - - 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); - - if (NULL == raw) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - - return raw; - } - - void start_service(const ServiceHandle& handle) - { - if (!StartService(handle, 0, NULL)) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - } - - void stop_service(const ServiceHandle& handle) - { - SERVICE_STATUS service_status; - - if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - } - - void uninstall_service(const ServiceHandle& handle) - { - if (!DeleteService(handle)) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - } - - bool service_exists( - const ServiceManager& mgr, - const std::string& name) - { - const auto raw = OpenServiceA( - mgr, - name.c_str(), - SERVICE_QUERY_STATUS); - - if (NULL != raw) - { - ServiceHandle handle(raw); - return true; - } - - const auto ec = GetLastError(); - - switch (ec) - { - case ERROR_SERVICE_DOES_NOT_EXIST: - return false; - - default: - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - } - - SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) - { - SERVICE_STATUS_PROCESS status; - DWORD nbreq; - - const auto buf_ptr = reinterpret_cast(&status); - const auto buf_size = sizeof(status); - - if (!QueryServiceStatusEx( - handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - - return status; - } - - DWORD query_service_state(const ServiceHandle& handle) - { - return query_service_status(handle).dwCurrentState; - } - - SERVICE_STATUS_PROCESS wait_for_service_state( - const ServiceHandle& handle, - const DWORD desired_state) - { - auto status = query_service_status(handle); - - DWORD old_timestamp = GetTickCount(); - - DWORD old_check_point = status.dwCheckPoint; - DWORD old_wait_hint = status.dwWaitHint; - - while (desired_state != status.dwCurrentState) - { - DWORD wait_time = old_wait_hint / 10; - - if (wait_time < 1000) - wait_time = 1000; - else if (wait_time > 10000) - wait_time = 10000; - - Sleep(wait_time); - - status = query_service_status(handle); - - if (desired_state == status.dwCurrentState) - break; - - 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; - } - } - - return status; - } - } - - Service Service::open( - const ServiceManager& mgr, - const std::string& name) - { - return open_service(mgr, name); - } - - Service Service::install( - const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) - { - return install_service(mgr, name, bin_path); - } - - bool Service::exists( - const ServiceManager& mgr, - const std::string& name) - { - return service_exists(mgr, name); - } - - void Service::start() const - { - const auto state = query_service_state(handle); - - switch (state) - { - case SERVICE_STOPPED: - break; - - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - break; - - default: - return; - } - - start_service(handle); - wait_for_service_state(handle, SERVICE_RUNNING); - } - - void Service::stop() const - { - switch (query_service_state(handle)) - { - case SERVICE_STOPPED: - return; - - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - return; - } - - stop_service(handle); - wait_for_service_state(handle, SERVICE_STOPPED); - } - - void Service::uninstall() const - { - stop(); - uninstall_service(handle); - } - - void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} - -namespace std -{ - template <> - void swap( - libservice::Service& a, - libservice::Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} diff --git a/um/libservice/src/service_handle.cpp b/um/libservice/src/service_handle.cpp deleted file mode 100644 index adce7f0..0000000 --- a/um/libservice/src/service_handle.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -namespace libservice -{ - void swap(ServiceHandle& a, ServiceHandle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} - -namespace std -{ - template <> - void swap( - libservice::ServiceHandle& a, - libservice::ServiceHandle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} diff --git a/um/libservice/src/service_manager.cpp b/um/libservice/src/service_manager.cpp deleted file mode 100644 index 61662de..0000000 --- a/um/libservice/src/service_manager.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -#include -#include - -namespace libservice -{ - ServiceManager ServiceManager::open() - { - SC_HANDLE raw = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); - - if (NULL == raw) - { - const auto ec = GetLastError(); - throw std::system_error( - ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); - } - - return ServiceHandle(raw); - } - - void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} - -namespace std -{ - template <> - void swap( - libservice::ServiceManager& a, - libservice::ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } -} diff --git a/um/libservice/src/windows_error.cpp b/um/libservice/src/windows_error.cpp deleted file mode 100644 index f27018b..0000000 --- a/um/libservice/src/windows_error.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -#include - -namespace libservice -{ - std::string WindowsErrorCategory::message(int code) const - { - char* buf_ptr; - - 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(&buf_ptr), - 0, - NULL); - - if (0 == nbwritten) - { - LocalFree(buf_ptr); - return "Couldn't format error message"; - } - - std::string str(buf_ptr, nbwritten - 2); - LocalFree(buf_ptr); - return str; - } -} diff --git a/um/libservice/test/CMakeLists.txt b/um/libservice/test/CMakeLists.txt deleted file mode 100644 index 4780deb..0000000 --- a/um/libservice/test/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -add_executable(libservice_test_windows_error windows_error.cpp) -target_link_libraries(libservice_test_windows_error libservice) -set_target_properties(libservice_test_windows_error PROPERTIES - OUTPUT_NAME windows_error) diff --git a/um/libservice/test/windows_error.cpp b/um/libservice/test/windows_error.cpp deleted file mode 100644 index b909670..0000000 --- a/um/libservice/test/windows_error.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include - -#include -#include -#include - -int main() -{ - try - { - throw std::system_error( - ERROR_FILE_NOT_FOUND, - libservice::WindowsErrorCategory::get(), - LIBSERVICE_ERROR_PREFIX); - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - return 0; -} diff --git a/um/libservice/utils/CMakeLists.txt b/um/libservice/utils/CMakeLists.txt deleted file mode 100644 index 061ab25..0000000 --- a/um/libservice/utils/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -add_executable(install_service install_service.cpp) -target_link_libraries(install_service libservice) - -add_executable(start_service start_service.cpp) -target_link_libraries(start_service libservice) - -add_executable(stop_service stop_service.cpp) -target_link_libraries(stop_service libservice) - -add_executable(uninstall_service uninstall_service.cpp) -target_link_libraries(uninstall_service libservice) diff --git a/um/libservice/utils/install_service.cpp b/um/libservice/utils/install_service.cpp deleted file mode 100644 index f36af75..0000000 --- a/um/libservice/utils/install_service.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include -#include - -int main(int argc, char* argv[]) -{ - if (argc != 3) - { - std::cout << "Usage: " << argv[0] << " NAME SYS_PATH\n"; - return 1; - } - - try - { - libservice::Service::install(libservice::ServiceManager::open(), argv[1], argv[2]); - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - return 0; -} diff --git a/um/libservice/utils/start_service.cpp b/um/libservice/utils/start_service.cpp deleted file mode 100644 index fafee53..0000000 --- a/um/libservice/utils/start_service.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include -#include - -int main(int argc, char* argv[]) -{ - if (argc != 2) - { - std::cout << "Usage: " << argv[0] << " NAME\n"; - return 1; - } - - try - { - libservice::Service::open(libservice::ServiceManager::open(), argv[1]).start(); - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - return 0; -} diff --git a/um/libservice/utils/stop_service.cpp b/um/libservice/utils/stop_service.cpp deleted file mode 100644 index 800c7a9..0000000 --- a/um/libservice/utils/stop_service.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include -#include - -int main(int argc, char* argv[]) -{ - if (argc != 2) - { - std::cout << "Usage: " << argv[0] << " NAME\n"; - return 1; - } - - try - { - libservice::Service::open(libservice::ServiceManager::open(), argv[1]).stop(); - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - return 0; -} diff --git a/um/libservice/utils/uninstall_service.cpp b/um/libservice/utils/uninstall_service.cpp deleted file mode 100644 index 395bb51..0000000 --- a/um/libservice/utils/uninstall_service.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libservice/all.hpp" - -#include -#include - -int main(int argc, char* argv[]) -{ - if (argc != 2) - { - std::cout << "Usage: " << argv[0] << " NAME\n"; - return 1; - } - - try - { - libservice::Service::open(libservice::ServiceManager::open(), argv[1]).uninstall(); - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - return 0; -} diff --git a/um/libsimple/CMakeLists.txt b/um/libsimple/CMakeLists.txt deleted file mode 100644 index ff61bd7..0000000 --- a/um/libsimple/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -file(GLOB_RECURSE libsimple_headers "include/*.hpp") -file(GLOB libsimple_sources "src/*.cpp") -add_library(libsimple - ${libsimple_sources} - ${libsimple_headers}) -target_link_libraries(libsimple libservice) -target_include_directories(libsimple PUBLIC include/) - -add_subdirectory(utils) diff --git a/um/libsimple/README.md b/um/libsimple/README.md deleted file mode 100644 index ac00d63..0000000 --- a/um/libsimple/README.md +++ /dev/null @@ -1,43 +0,0 @@ -simple driver utilities -======================= - -[simple] driver usage examples. - -[simple]: ../../km/src/simple - -Usage ------ - -### exchange_ints.exe - -``` -Usage: exchange_ints.exe N -``` - -Parses its argument as an `unsigned int` and exchanges it with the one stored -in [simple] driver's memory. -For example: - -``` -> exchange_ints.exe 1 -42 -``` - -``` -> exchange_ints.exe 32 -1 -``` - -``` -> exchange_ints.exe 100500 -32 -``` - -See also --------- - -* [Building the utilities] -* [License] - -[Building the utilities]: ../README.md#building-the-utilities -[License]: ../../README.md#license diff --git a/um/libsimple/include/libsimple/all.hpp b/um/libsimple/include/libsimple/all.hpp deleted file mode 100644 index 474b802..0000000 --- a/um/libsimple/include/libsimple/all.hpp +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "device.hpp" diff --git a/um/libsimple/include/libsimple/device.hpp b/um/libsimple/include/libsimple/device.hpp deleted file mode 100644 index 3318818..0000000 --- a/um/libsimple/include/libsimple/device.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#pragma once - -#include "libservice/all.hpp" - -namespace libsimple -{ - class Device : libservice::Device - { - public: - Device(); - - unsigned int exchange_ints(unsigned int) const; - }; -} diff --git a/um/libsimple/src/device.cpp b/um/libsimple/src/device.cpp deleted file mode 100644 index 57e6963..0000000 --- a/um/libsimple/src/device.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libsimple/all.hpp" - -#include "libservice/all.hpp" - -#include - -namespace libsimple -{ - namespace - { - const char* const device_path = "\\\\.\\simple_device1"; - const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } - - Device::Device() - : libservice::Device(libservice::Device::open(device_path)) - { } - - unsigned int Device::exchange_ints(unsigned int src) const - { - unsigned int dest; - - send_control_code( - exchange_ints_ctl_code, - &src, - sizeof(src), - &dest, - sizeof(dest)); - - return dest; - } -} diff --git a/um/libsimple/utils/CMakeLists.txt b/um/libsimple/utils/CMakeLists.txt deleted file mode 100644 index 8824e3e..0000000 --- a/um/libsimple/utils/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(exchange_ints exchange_ints.cpp) -target_link_libraries(exchange_ints libsimple) diff --git a/um/libsimple/utils/exchange_ints.cpp b/um/libsimple/utils/exchange_ints.cpp deleted file mode 100644 index 2935b2f..0000000 --- a/um/libsimple/utils/exchange_ints.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2015 Egor Tensin -// This file is part of the "Windows 7 drivers" project. -// For details, see https://github.com/egor-tensin/windows7-drivers. -// Distributed under the MIT License. - -#include "libsimple/all.hpp" - -#include -#include -#include -#include - -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 - { - unsigned int src; - - if (argc != 2 || !parse_int(src, argv[1])) - { - std::cout << "Usage: " << argv[0] << " N\n"; - return 1; - } - - std::cout << libsimple::Device().exchange_ints(src) << "\n"; - } - catch (const std::exception& e) - { - std::cerr << e.what() << "\n"; - return 1; - } - - return 0; -} diff --git a/um/service/CMakeLists.txt b/um/service/CMakeLists.txt new file mode 100644 index 0000000..34b3d13 --- /dev/null +++ b/um/service/CMakeLists.txt @@ -0,0 +1,10 @@ +file(GLOB libservice_sources "src/*.cpp") +file(GLOB_RECURSE libservice_headers "include/*.hpp") +add_library(libservice + ${libservice_sources} + ${libservice_headers}) +target_include_directories(libservice PUBLIC include) +target_compile_definitions(libservice PRIVATE NOMINMAX) + +add_subdirectory(test) +add_subdirectory(utils) diff --git a/um/service/README.md b/um/service/README.md new file mode 100644 index 0000000..f9fb601 --- /dev/null +++ b/um/service/README.md @@ -0,0 +1,53 @@ +Driver management utilities +=========================== + +Utilities to load/unload the drivers. + +Usage +----- + +### install_service.exe + + Usage: install_service.exe NAME SYS_PATH + +Installs a driver as a service. +The same as + + > sc create NAME type= kernel binPath= SYS_PATH + +### start_service.exe + + Usage: start_service.exe NAME + +Starts the service `NAME` (loading the corresponding driver). +The same as + + > net start NAME + +### stop_service.exe + + Usage: stop_service.exe NAME + +Stops the service `NAME` (unloading the corresponding driver). +The same as + + > net stop NAME + +### uninstall_service.exe + + Usage: uninstall_service.exe NAME + +Uninstalls the service `NAME`, wiping the corresponding record from the +registry. +The same as + + > sc delete NAME + +See also +-------- + +* [Building the utilities] +* [License] + +[Building the utilities]: ../README.md#building-the-utilities +[License]: ../../README.md#license diff --git a/um/service/include/libservice/all.hpp b/um/service/include/libservice/all.hpp new file mode 100644 index 0000000..a5761e0 --- /dev/null +++ b/um/service/include/libservice/all.hpp @@ -0,0 +1,15 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" +#include "device.hpp" +#include "handle.hpp" +#include "service.hpp" +#include "service_handle.hpp" +#include "service_manager.hpp" +#include "singleton.hpp" +#include "windows_error.hpp" diff --git a/um/service/include/libservice/common.hpp b/um/service/include/libservice/common.hpp new file mode 100644 index 0000000..a1c46fb --- /dev/null +++ b/um/service/include/libservice/common.hpp @@ -0,0 +1,17 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#define LIBSERVICE_FILE_PATH __FILE__ +#define LIBSERVICE_LINE_NUMBER __LINE__ +#define LIBSERVICE_FUNCTION_NAME __FUNCTION__ + +#define LIBSERVICE_TO_STRING(s) LIBSERVICE_TO_STRING_(s) +#define LIBSERVICE_TO_STRING_(s) #s + +#define LIBSERVICE_LINE_NUMBER_STRING LIBSERVICE_TO_STRING(LIBSERVICE_LINE_NUMBER) + +#define LIBSERVICE_NOEXCEPT throw() diff --git a/um/service/include/libservice/device.hpp b/um/service/include/libservice/device.hpp new file mode 100644 index 0000000..ac292c8 --- /dev/null +++ b/um/service/include/libservice/device.hpp @@ -0,0 +1,75 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" +#include "handle.hpp" + +#include + +#include + +#include +#include + +namespace libservice +{ + class Device + { + public: + typedef DWORD Code; + + static Device open(const std::string& path); + + Device(Device&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + Device& operator=(Device other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + void swap(Device& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(handle, other.handle); + } + + std::size_t get_required_output_size( + Code code, + const void* in_buf, + std::size_t in_buf_size) const; + + 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; + + private: + Device(Handle handle) + : handle(std::move(handle)) + { } + + Handle handle; + + Device(const Device&) = delete; + }; + + void swap(Device&, Device&) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::Device&, + libservice::Device&) LIBSERVICE_NOEXCEPT; +} diff --git a/um/service/include/libservice/handle.hpp b/um/service/include/libservice/handle.hpp new file mode 100644 index 0000000..5e351d2 --- /dev/null +++ b/um/service/include/libservice/handle.hpp @@ -0,0 +1,77 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" + +#include + +#include +#include +#include + +namespace libservice +{ + class Handle + { + public: + Handle() = default; + + Handle(HANDLE raw) + : impl(raw) + { } + + Handle(Handle&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + operator bool() const + { + return static_cast(impl); + } + + operator HANDLE() const + { + return impl.get(); + } + + void swap(Handle& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(impl, other.impl); + } + + private: + struct Deleter + { + void operator()(HANDLE raw) + { + CloseHandle(raw); + } + }; + + std::unique_ptr::type, Deleter> impl; + + Handle(const Handle&) = delete; + }; + + void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::Handle& a, + libservice::Handle& b) LIBSERVICE_NOEXCEPT; +} diff --git a/um/service/include/libservice/service.hpp b/um/service/include/libservice/service.hpp new file mode 100644 index 0000000..089f790 --- /dev/null +++ b/um/service/include/libservice/service.hpp @@ -0,0 +1,73 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" +#include "service_handle.hpp" +#include "service_manager.hpp" + +#include +#include + +namespace libservice +{ + class Service + { + public: + static bool exists( + const ServiceManager&, + const std::string& name); + + static Service open( + const ServiceManager&, + const std::string& name); + + static Service install( + const ServiceManager&, + const std::string& name, + const std::string& bin_path); + + void start() const; + void stop() const; + void uninstall() const; + + Service(Service&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + Service& operator=(Service other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + void swap(Service& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(handle, other.handle); + } + + private: + Service(ServiceHandle handle) + : handle(std::move(handle)) + { } + + ServiceHandle handle; + + Service(const Service&) = delete; + }; + + void swap(Service&, Service&) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::Service&, + libservice::Service&) LIBSERVICE_NOEXCEPT; +} diff --git a/um/service/include/libservice/service_handle.hpp b/um/service/include/libservice/service_handle.hpp new file mode 100644 index 0000000..2883ff3 --- /dev/null +++ b/um/service/include/libservice/service_handle.hpp @@ -0,0 +1,76 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" + +#include + +#include +#include + +namespace libservice +{ + class ServiceHandle + { + public: + ServiceHandle() = default; + + ServiceHandle(SC_HANDLE raw) + : impl(raw) + { } + + ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + operator bool() const + { + return static_cast(impl); + } + + operator SC_HANDLE() const + { + return impl.get(); + } + + void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(impl, other.impl); + } + + private: + struct Deleter + { + void operator()(SC_HANDLE raw) + { + CloseServiceHandle(raw); + } + }; + + std::unique_ptr impl; + + ServiceHandle(const ServiceHandle&) = delete; + }; + + void swap(ServiceHandle&, ServiceHandle&) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::ServiceHandle&, + libservice::ServiceHandle&) LIBSERVICE_NOEXCEPT; +} diff --git a/um/service/include/libservice/service_manager.hpp b/um/service/include/libservice/service_manager.hpp new file mode 100644 index 0000000..80e6f12 --- /dev/null +++ b/um/service/include/libservice/service_manager.hpp @@ -0,0 +1,63 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" +#include "service_handle.hpp" + +#include + +#include + +namespace libservice +{ + class ServiceManager + { + public: + static ServiceManager open(); + + ServiceManager(ServiceManager&& other) LIBSERVICE_NOEXCEPT + { + swap(other); + } + + ServiceManager& operator=(ServiceManager other) LIBSERVICE_NOEXCEPT + { + swap(other); + return *this; + } + + void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT + { + using std::swap; + swap(handle, other.handle); + } + + operator SC_HANDLE() const + { + return handle; + } + + private: + ServiceManager(ServiceHandle handle) + : handle(std::move(handle)) + { } + + ServiceHandle handle; + + ServiceManager(const ServiceManager&) = delete; + }; + + void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT; +} + +namespace std +{ + template <> + void swap( + libservice::ServiceManager&, + libservice::ServiceManager&) LIBSERVICE_NOEXCEPT; +} diff --git a/um/service/include/libservice/singleton.hpp b/um/service/include/libservice/singleton.hpp new file mode 100644 index 0000000..1c7b1a7 --- /dev/null +++ b/um/service/include/libservice/singleton.hpp @@ -0,0 +1,43 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include + +namespace libservice +{ + template + class Singleton + { + public: + static DerivedT& get() + { + std::call_once(initialized, initialize); + return get_unsafe(); + } + + protected: + Singleton() = default; + virtual ~Singleton() = default; + + private: + static void initialize() + { + get_unsafe(); + } + + static DerivedT& get_unsafe() + { + static DerivedT instance; + return instance; + } + + static std::once_flag initialized; + }; + + template + std::once_flag Singleton::initialized; +} diff --git a/um/service/include/libservice/windows_error.hpp b/um/service/include/libservice/windows_error.hpp new file mode 100644 index 0000000..f7ac90e --- /dev/null +++ b/um/service/include/libservice/windows_error.hpp @@ -0,0 +1,31 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "common.hpp" +#include "singleton.hpp" + +#include +#include + +namespace libservice +{ + class WindowsErrorCategory + : public std::error_category + , public Singleton + { + public: + const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; } + + std::string message(int) const; + + private: + friend class Singleton; + }; +} + +#define LIBSERVICE_ERROR_PREFIX \ + "Error in function '" LIBSERVICE_FUNCTION_NAME "' at file '" LIBSERVICE_FILE_PATH "', line " LIBSERVICE_LINE_NUMBER_STRING diff --git a/um/service/src/device.cpp b/um/service/src/device.cpp new file mode 100644 index 0000000..5643106 --- /dev/null +++ b/um/service/src/device.cpp @@ -0,0 +1,136 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +#include + +#include +#include +#include +#include +#include + +namespace libservice +{ + 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); + + if (INVALID_HANDLE_VALUE == raw) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + + return Handle(raw); + } + } + + Device Device::open(const std::string& path) + { + return Device(open_device(path)); + } + + std::size_t Device::get_required_output_size( + Code code, + const void* in_buf, + std::size_t in_buf_size) const + { + DWORD nbreq; + + if (in_buf_size > std::numeric_limits::max()) + throw std::range_error("input buffer size is too large"); + + std::size_t nbwritten = DeviceIoControl( + handle, + code, + const_cast(in_buf), + static_cast(in_buf_size), + NULL, + 0, + &nbreq, + NULL); + + if (0 == nbwritten) + { + const auto ec = GetLastError(); + + switch (ec) + { + case ERROR_MORE_DATA: + return nbreq; + + default: + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + } + + return nbwritten; + } + + 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; + + if (in_buf_size > std::numeric_limits::max()) + throw std::range_error("input buffer size is too large"); + if (out_buf_size > std::numeric_limits::max()) + throw std::range_error("output buffer size is too large"); + + std::size_t nbwritten = DeviceIoControl( + handle, + code, + const_cast(in_buf), + static_cast(in_buf_size), + out_buf, + static_cast(out_buf_size), + &nbreq, + NULL); + + if (0 == nbwritten) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + + return nbwritten; + } + + void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} + +namespace std +{ + template <> + void swap( + libservice::Device& a, + libservice::Device& b) + { + a.swap(b); + } +} diff --git a/um/service/src/handle.cpp b/um/service/src/handle.cpp new file mode 100644 index 0000000..7567781 --- /dev/null +++ b/um/service/src/handle.cpp @@ -0,0 +1,27 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +namespace libservice +{ + void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} + +namespace std +{ + template <> + void swap( + libservice::Handle& a, + libservice::Handle& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} diff --git a/um/service/src/service.cpp b/um/service/src/service.cpp new file mode 100644 index 0000000..8b5e043 --- /dev/null +++ b/um/service/src/service.cpp @@ -0,0 +1,276 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +#include +#include +#include +#include + +namespace libservice +{ + namespace + { + ServiceHandle open_service( + const ServiceManager& mgr, + const std::string& name) + { + const auto raw = OpenServiceA( + mgr, + name.c_str(), + SERVICE_ALL_ACCESS); + + if (NULL == raw) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + + return raw; + } + + 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); + + if (NULL == raw) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + + return raw; + } + + void start_service(const ServiceHandle& handle) + { + if (!StartService(handle, 0, NULL)) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + } + + void stop_service(const ServiceHandle& handle) + { + SERVICE_STATUS service_status; + + if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + } + + void uninstall_service(const ServiceHandle& handle) + { + if (!DeleteService(handle)) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + } + + bool service_exists( + const ServiceManager& mgr, + const std::string& name) + { + const auto raw = OpenServiceA( + mgr, + name.c_str(), + SERVICE_QUERY_STATUS); + + if (NULL != raw) + { + ServiceHandle handle(raw); + return true; + } + + const auto ec = GetLastError(); + + switch (ec) + { + case ERROR_SERVICE_DOES_NOT_EXIST: + return false; + + default: + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + } + + SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) + { + SERVICE_STATUS_PROCESS status; + DWORD nbreq; + + const auto buf_ptr = reinterpret_cast(&status); + const auto buf_size = sizeof(status); + + if (!QueryServiceStatusEx( + handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + + return status; + } + + DWORD query_service_state(const ServiceHandle& handle) + { + return query_service_status(handle).dwCurrentState; + } + + SERVICE_STATUS_PROCESS wait_for_service_state( + const ServiceHandle& handle, + const DWORD desired_state) + { + auto status = query_service_status(handle); + + DWORD old_timestamp = GetTickCount(); + + DWORD old_check_point = status.dwCheckPoint; + DWORD old_wait_hint = status.dwWaitHint; + + while (desired_state != status.dwCurrentState) + { + DWORD wait_time = old_wait_hint / 10; + + if (wait_time < 1000) + wait_time = 1000; + else if (wait_time > 10000) + wait_time = 10000; + + Sleep(wait_time); + + status = query_service_status(handle); + + if (desired_state == status.dwCurrentState) + break; + + 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; + } + } + + return status; + } + } + + Service Service::open( + const ServiceManager& mgr, + const std::string& name) + { + return open_service(mgr, name); + } + + Service Service::install( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path) + { + return install_service(mgr, name, bin_path); + } + + bool Service::exists( + const ServiceManager& mgr, + const std::string& name) + { + return service_exists(mgr, name); + } + + void Service::start() const + { + const auto state = query_service_state(handle); + + switch (state) + { + case SERVICE_STOPPED: + break; + + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + break; + + default: + return; + } + + start_service(handle); + wait_for_service_state(handle, SERVICE_RUNNING); + } + + void Service::stop() const + { + switch (query_service_state(handle)) + { + case SERVICE_STOPPED: + return; + + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + return; + } + + stop_service(handle); + wait_for_service_state(handle, SERVICE_STOPPED); + } + + void Service::uninstall() const + { + stop(); + uninstall_service(handle); + } + + void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} + +namespace std +{ + template <> + void swap( + libservice::Service& a, + libservice::Service& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} diff --git a/um/service/src/service_handle.cpp b/um/service/src/service_handle.cpp new file mode 100644 index 0000000..adce7f0 --- /dev/null +++ b/um/service/src/service_handle.cpp @@ -0,0 +1,27 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +namespace libservice +{ + void swap(ServiceHandle& a, ServiceHandle& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} + +namespace std +{ + template <> + void swap( + libservice::ServiceHandle& a, + libservice::ServiceHandle& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} diff --git a/um/service/src/service_manager.cpp b/um/service/src/service_manager.cpp new file mode 100644 index 0000000..61662de --- /dev/null +++ b/um/service/src/service_manager.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +#include +#include + +namespace libservice +{ + ServiceManager ServiceManager::open() + { + SC_HANDLE raw = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); + + if (NULL == raw) + { + const auto ec = GetLastError(); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + } + + return ServiceHandle(raw); + } + + void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} + +namespace std +{ + template <> + void swap( + libservice::ServiceManager& a, + libservice::ServiceManager& b) LIBSERVICE_NOEXCEPT + { + a.swap(b); + } +} diff --git a/um/service/src/windows_error.cpp b/um/service/src/windows_error.cpp new file mode 100644 index 0000000..f27018b --- /dev/null +++ b/um/service/src/windows_error.cpp @@ -0,0 +1,39 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +#include + +namespace libservice +{ + std::string WindowsErrorCategory::message(int code) const + { + char* buf_ptr; + + 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(&buf_ptr), + 0, + NULL); + + if (0 == nbwritten) + { + LocalFree(buf_ptr); + return "Couldn't format error message"; + } + + std::string str(buf_ptr, nbwritten - 2); + LocalFree(buf_ptr); + return str; + } +} diff --git a/um/service/test/CMakeLists.txt b/um/service/test/CMakeLists.txt new file mode 100644 index 0000000..4780deb --- /dev/null +++ b/um/service/test/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(libservice_test_windows_error windows_error.cpp) +target_link_libraries(libservice_test_windows_error libservice) +set_target_properties(libservice_test_windows_error PROPERTIES + OUTPUT_NAME windows_error) diff --git a/um/service/test/windows_error.cpp b/um/service/test/windows_error.cpp new file mode 100644 index 0000000..b909670 --- /dev/null +++ b/um/service/test/windows_error.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include + +#include +#include +#include + +int main() +{ + try + { + throw std::system_error( + ERROR_FILE_NOT_FOUND, + libservice::WindowsErrorCategory::get(), + LIBSERVICE_ERROR_PREFIX); + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + return 0; +} diff --git a/um/service/utils/CMakeLists.txt b/um/service/utils/CMakeLists.txt new file mode 100644 index 0000000..061ab25 --- /dev/null +++ b/um/service/utils/CMakeLists.txt @@ -0,0 +1,11 @@ +add_executable(install_service install_service.cpp) +target_link_libraries(install_service libservice) + +add_executable(start_service start_service.cpp) +target_link_libraries(start_service libservice) + +add_executable(stop_service stop_service.cpp) +target_link_libraries(stop_service libservice) + +add_executable(uninstall_service uninstall_service.cpp) +target_link_libraries(uninstall_service libservice) diff --git a/um/service/utils/install_service.cpp b/um/service/utils/install_service.cpp new file mode 100644 index 0000000..f36af75 --- /dev/null +++ b/um/service/utils/install_service.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include +#include + +int main(int argc, char* argv[]) +{ + if (argc != 3) + { + std::cout << "Usage: " << argv[0] << " NAME SYS_PATH\n"; + return 1; + } + + try + { + libservice::Service::install(libservice::ServiceManager::open(), argv[1], argv[2]); + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + return 0; +} diff --git a/um/service/utils/start_service.cpp b/um/service/utils/start_service.cpp new file mode 100644 index 0000000..fafee53 --- /dev/null +++ b/um/service/utils/start_service.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include +#include + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + std::cout << "Usage: " << argv[0] << " NAME\n"; + return 1; + } + + try + { + libservice::Service::open(libservice::ServiceManager::open(), argv[1]).start(); + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + return 0; +} diff --git a/um/service/utils/stop_service.cpp b/um/service/utils/stop_service.cpp new file mode 100644 index 0000000..800c7a9 --- /dev/null +++ b/um/service/utils/stop_service.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include +#include + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + std::cout << "Usage: " << argv[0] << " NAME\n"; + return 1; + } + + try + { + libservice::Service::open(libservice::ServiceManager::open(), argv[1]).stop(); + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + return 0; +} diff --git a/um/service/utils/uninstall_service.cpp b/um/service/utils/uninstall_service.cpp new file mode 100644 index 0000000..395bb51 --- /dev/null +++ b/um/service/utils/uninstall_service.cpp @@ -0,0 +1,29 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libservice/all.hpp" + +#include +#include + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + std::cout << "Usage: " << argv[0] << " NAME\n"; + return 1; + } + + try + { + libservice::Service::open(libservice::ServiceManager::open(), argv[1]).uninstall(); + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + return 0; +} diff --git a/um/wrappers/CMakeLists.txt b/um/wrappers/CMakeLists.txt new file mode 100644 index 0000000..834a76b --- /dev/null +++ b/um/wrappers/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(simple) +add_subdirectory(special) diff --git a/um/wrappers/simple/CMakeLists.txt b/um/wrappers/simple/CMakeLists.txt new file mode 100644 index 0000000..ff61bd7 --- /dev/null +++ b/um/wrappers/simple/CMakeLists.txt @@ -0,0 +1,9 @@ +file(GLOB_RECURSE libsimple_headers "include/*.hpp") +file(GLOB libsimple_sources "src/*.cpp") +add_library(libsimple + ${libsimple_sources} + ${libsimple_headers}) +target_link_libraries(libsimple libservice) +target_include_directories(libsimple PUBLIC include/) + +add_subdirectory(utils) diff --git a/um/wrappers/simple/README.md b/um/wrappers/simple/README.md new file mode 100644 index 0000000..51a46cb --- /dev/null +++ b/um/wrappers/simple/README.md @@ -0,0 +1,43 @@ +simple driver utilities +======================= + +[simple] driver usage examples. + +[simple]: ../../../km/src/simple + +Usage +----- + +### exchange_ints.exe + +``` +Usage: exchange_ints.exe N +``` + +Parses its argument as an `unsigned int` and exchanges it with the one stored +in [simple] driver's memory. +For example: + +``` +> exchange_ints.exe 1 +42 +``` + +``` +> exchange_ints.exe 32 +1 +``` + +``` +> exchange_ints.exe 100500 +32 +``` + +See also +-------- + +* [Building the utilities] +* [License] + +[Building the utilities]: ../../README.md#building-the-utilities +[License]: ../../../README.md#license diff --git a/um/wrappers/simple/include/libsimple/all.hpp b/um/wrappers/simple/include/libsimple/all.hpp new file mode 100644 index 0000000..474b802 --- /dev/null +++ b/um/wrappers/simple/include/libsimple/all.hpp @@ -0,0 +1,8 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "device.hpp" diff --git a/um/wrappers/simple/include/libsimple/device.hpp b/um/wrappers/simple/include/libsimple/device.hpp new file mode 100644 index 0000000..3318818 --- /dev/null +++ b/um/wrappers/simple/include/libsimple/device.hpp @@ -0,0 +1,19 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "libservice/all.hpp" + +namespace libsimple +{ + class Device : libservice::Device + { + public: + Device(); + + unsigned int exchange_ints(unsigned int) const; + }; +} diff --git a/um/wrappers/simple/src/device.cpp b/um/wrappers/simple/src/device.cpp new file mode 100644 index 0000000..57e6963 --- /dev/null +++ b/um/wrappers/simple/src/device.cpp @@ -0,0 +1,37 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libsimple/all.hpp" + +#include "libservice/all.hpp" + +#include + +namespace libsimple +{ + namespace + { + const char* const device_path = "\\\\.\\simple_device1"; + const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); + } + + Device::Device() + : libservice::Device(libservice::Device::open(device_path)) + { } + + unsigned int Device::exchange_ints(unsigned int src) const + { + unsigned int dest; + + send_control_code( + exchange_ints_ctl_code, + &src, + sizeof(src), + &dest, + sizeof(dest)); + + return dest; + } +} diff --git a/um/wrappers/simple/utils/CMakeLists.txt b/um/wrappers/simple/utils/CMakeLists.txt new file mode 100644 index 0000000..8824e3e --- /dev/null +++ b/um/wrappers/simple/utils/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(exchange_ints exchange_ints.cpp) +target_link_libraries(exchange_ints libsimple) diff --git a/um/wrappers/simple/utils/exchange_ints.cpp b/um/wrappers/simple/utils/exchange_ints.cpp new file mode 100644 index 0000000..2935b2f --- /dev/null +++ b/um/wrappers/simple/utils/exchange_ints.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libsimple/all.hpp" + +#include +#include +#include +#include + +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 + { + unsigned int src; + + if (argc != 2 || !parse_int(src, argv[1])) + { + std::cout << "Usage: " << argv[0] << " N\n"; + return 1; + } + + std::cout << libsimple::Device().exchange_ints(src) << "\n"; + } + catch (const std::exception& e) + { + std::cerr << e.what() << "\n"; + return 1; + } + + return 0; +} diff --git a/um/wrappers/special/CMakeLists.txt b/um/wrappers/special/CMakeLists.txt new file mode 100644 index 0000000..ed48e00 --- /dev/null +++ b/um/wrappers/special/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(nt_path_converter) diff --git a/um/wrappers/special/nt_path_converter/CMakeLists.txt b/um/wrappers/special/nt_path_converter/CMakeLists.txt new file mode 100644 index 0000000..69f85d0 --- /dev/null +++ b/um/wrappers/special/nt_path_converter/CMakeLists.txt @@ -0,0 +1,10 @@ +project(libnt_path_converter) +file(GLOB libnt_path_converter_sources "src/*.cpp") +file(GLOB_RECURSE libnt_path_converter_headers "include/*.hpp") +add_library(libnt_path_converter + ${libnt_path_converter_sources} + ${libnt_path_converter_headers}) +target_link_libraries(libnt_path_converter libservice) +target_include_directories(libnt_path_converter PUBLIC include/) + +add_subdirectory(utils) diff --git a/um/wrappers/special/nt_path_converter/README.md b/um/wrappers/special/nt_path_converter/README.md new file mode 100644 index 0000000..71b4948 --- /dev/null +++ b/um/wrappers/special/nt_path_converter/README.md @@ -0,0 +1,31 @@ +nt_path_converter driver utilities +================================== + +[nt_path_converter] driver usage examples. + +[nt_path_converter]: ../../../../km/src/special/nt_path_converter + +Usage +----- + +### convert_nt_path.exe + + Usage: convert_nt_path.exe [NT_PATH...] + +Converts a NT-style path to a DOS-style path. +The NT namespace can be explored using the [WinObj] utility. +For example: + + > convert_nt_path.exe \Device\HarddiskVolume2\Windows + C:\Windows + +[WinObj]: https://technet.microsoft.com/en-us/library/bb896657.aspx + +See also +-------- + +* [Building the utilities] +* [License] + +[Building the utilities]: ../../../README.md#building-the-utilities +[License]: ../../../../README.md#license diff --git a/um/wrappers/special/nt_path_converter/include/libnt_path_converter/all.hpp b/um/wrappers/special/nt_path_converter/include/libnt_path_converter/all.hpp new file mode 100644 index 0000000..474b802 --- /dev/null +++ b/um/wrappers/special/nt_path_converter/include/libnt_path_converter/all.hpp @@ -0,0 +1,8 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "device.hpp" diff --git a/um/wrappers/special/nt_path_converter/include/libnt_path_converter/device.hpp b/um/wrappers/special/nt_path_converter/include/libnt_path_converter/device.hpp new file mode 100644 index 0000000..e1d75fb --- /dev/null +++ b/um/wrappers/special/nt_path_converter/include/libnt_path_converter/device.hpp @@ -0,0 +1,21 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#pragma once + +#include "libservice/all.hpp" + +#include + +namespace libnt_path_converter +{ + class Device : libservice::Device + { + public: + Device(); + + std::wstring convert_nt_path(const std::wstring&); + }; +} diff --git a/um/wrappers/special/nt_path_converter/src/device.cpp b/um/wrappers/special/nt_path_converter/src/device.cpp new file mode 100644 index 0000000..90cd12f --- /dev/null +++ b/um/wrappers/special/nt_path_converter/src/device.cpp @@ -0,0 +1,48 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libnt_path_converter/device.hpp" + +#include "libservice/all.hpp" + +#include + +#include +#include + +namespace libnt_path_converter +{ + namespace + { + const char* const device_path = "\\\\.\\nt_path_converter"; + const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); + } + + Device::Device() + : libservice::Device(libservice::Device::open(device_path)) + { } + + 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); + + const auto nbreq = get_required_output_size( + control_code, + in_buf, + in_buf_size); + + std::vector output(nbreq); + + send_control_code( + control_code, + in_buf, + in_buf_size, + output.data(), + nbreq); + + return reinterpret_cast(output.data()); + } +} diff --git a/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt b/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt new file mode 100644 index 0000000..9915995 --- /dev/null +++ b/um/wrappers/special/nt_path_converter/utils/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(convert_nt_path convert_nt_path.cpp) +target_link_libraries(convert_nt_path libnt_path_converter) diff --git a/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp b/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp new file mode 100644 index 0000000..0055db2 --- /dev/null +++ b/um/wrappers/special/nt_path_converter/utils/convert_nt_path.cpp @@ -0,0 +1,26 @@ +// Copyright (c) 2015 Egor Tensin +// This file is part of the "Windows 7 drivers" project. +// For details, see https://github.com/egor-tensin/windows7-drivers. +// Distributed under the MIT License. + +#include "libnt_path_converter/all.hpp" + +#include +#include + +int wmain(int argc, wchar_t* argv[]) +{ + try + { + libnt_path_converter::Device dev; + for (int i = 1; i < argc; ++i) + std::wcout << dev.convert_nt_path(argv[i]) << L"\n"; + } + catch (const std::exception& e) + { + std::wcerr << e.what() << "\n"; + return 1; + } + + return 0; +} -- cgit v1.2.3