diff options
| author | Egor Tensin <egor@tensin.name> | 2026-06-15 14:05:29 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-06-15 14:12:29 +0200 |
| commit | b3eef140da853414776c8e03a70629a613a25ed0 (patch) | |
| tree | dcdb314d2fdd92f501c09f3ae5681a731cb00dee /um | |
| parent | wdk10: disable the packaging target (diff) | |
| download | windows7-drivers-b3eef140da853414776c8e03a70629a613a25ed0.tar.gz windows7-drivers-b3eef140da853414776c8e03a70629a613a25ed0.zip | |
workflows/ci: add clang-format job
Diffstat (limited to 'um')
22 files changed, 623 insertions, 774 deletions
diff --git a/um/service/include/service/device.hpp b/um/service/include/service/device.hpp index ee46354..29d03c1 100644 --- a/um/service/include/service/device.hpp +++ b/um/service/include/service/device.hpp @@ -11,71 +11,64 @@ #include <Windows.h> #include <cstddef> - #include <string> #include <utility> -namespace service -{ - class Device - { - public: - typedef DWORD Code; +namespace service { - static Device open(const std::string& path); +class Device { +public: + typedef DWORD Code; - Device(Device&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } + static Device open(const std::string& path); - Device& operator=(Device other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + Device(Device&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(Device& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } + Device& operator=(Device other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - std::size_t get_required_output_size( - Code code, - const void* in_buf, - std::size_t in_buf_size) const; + void swap(Device& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(handle, other.handle); + } - std::size_t send_control_code( - Code code, - const void* in_buf, - std::size_t in_buf_size, - void* out_buf, - std::size_t out_buf_size) const; + std::size_t get_required_output_size( + Code code, + const void* in_buf, + std::size_t in_buf_size + ) const; - private: - Device(Handle handle) - : handle(std::move(handle)) - { } + std::size_t send_control_code( + Code code, + const void* in_buf, + std::size_t in_buf_size, + void* out_buf, + std::size_t out_buf_size + ) const; - Handle handle; +private: + Device(Handle handle) : handle(std::move(handle)) {} - Device(const Device&) = delete; - }; + Handle handle; - inline void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + Device(const Device&) = delete; +}; + +inline void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::Device& a, - service::Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::Device& a, service::Device& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/handle.hpp b/um/service/include/service/handle.hpp index 2ca3754..ef4c152 100644 --- a/um/service/include/service/handle.hpp +++ b/um/service/include/service/handle.hpp @@ -13,71 +13,59 @@ #include <type_traits> #include <utility> -namespace service -{ - class Handle - { - public: - Handle() = default; +namespace service { - Handle(HANDLE raw) - : impl(raw) - { } +class Handle { +public: + Handle() = default; - Handle(Handle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + Handle(HANDLE raw) : impl(raw) {} - operator bool() const - { - return static_cast<bool>(impl); - } + Handle(Handle&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - operator HANDLE() const - { - return impl.get(); - } + Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - void swap(Handle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } + operator bool() const { + return static_cast<bool>(impl); + } - private: - struct Deleter - { - void operator()(HANDLE raw) - { - CloseHandle(raw); - } - }; + operator HANDLE() const { + return impl.get(); + } - std::unique_ptr<std::remove_pointer<HANDLE>::type, Deleter> impl; + void swap(Handle& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(impl, other.impl); + } - Handle(const Handle&) = delete; +private: + struct Deleter { + void operator()(HANDLE raw) { + CloseHandle(raw); + } }; - inline void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + std::unique_ptr<std::remove_pointer<HANDLE>::type, Deleter> impl; + + Handle(const Handle&) = delete; +}; + +inline void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::Handle& a, - service::Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::Handle& a, service::Handle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/service.hpp b/um/service/include/service/service.hpp index 2d5f776..58f2fa4 100644 --- a/um/service/include/service/service.hpp +++ b/um/service/include/service/service.hpp @@ -12,68 +12,57 @@ #include <string> #include <utility> -namespace service -{ - class Service - { - public: - static bool exists( - const ServiceManager&, - const std::string& name); +namespace service { - static Service open( - const ServiceManager&, - const std::string& name); +class Service { +public: + static bool exists(const ServiceManager&, const std::string& name); - static Service install( - const ServiceManager&, - const std::string& name, - const std::string& bin_path); + static Service open(const ServiceManager&, const std::string& name); - void start() const; - void stop() const; - void uninstall() const; + static Service install( + const ServiceManager&, + const std::string& name, + const std::string& bin_path + ); - Service(Service&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } + void start() const; + void stop() const; + void uninstall() const; - Service& operator=(Service other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + Service(Service&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(Service& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } + Service& operator=(Service other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - private: - Service(ServiceHandle handle) - : handle(std::move(handle)) - { } + void swap(Service& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(handle, other.handle); + } - ServiceHandle handle; +private: + Service(ServiceHandle handle) : handle(std::move(handle)) {} - Service(const Service&) = delete; - }; + ServiceHandle handle; - inline void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + Service(const Service&) = delete; +}; + +inline void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::Service& a, - service::Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::Service& a, service::Service& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/service_handle.hpp b/um/service/include/service/service_handle.hpp index 4be58c6..38e6a96 100644 --- a/um/service/include/service/service_handle.hpp +++ b/um/service/include/service/service_handle.hpp @@ -12,71 +12,59 @@ #include <memory> #include <utility> -namespace service -{ - class ServiceHandle - { - public: - ServiceHandle() = default; +namespace service { - ServiceHandle(SC_HANDLE raw) - : impl(raw) - { } +class ServiceHandle { +public: + ServiceHandle() = default; - ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } - - ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + ServiceHandle(SC_HANDLE raw) : impl(raw) {} - operator bool() const - { - return static_cast<bool>(impl); - } + ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - operator SC_HANDLE() const - { - return impl.get(); - } + ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } + operator bool() const { + return static_cast<bool>(impl); + } - private: - struct Deleter - { - void operator()(SC_HANDLE raw) - { - CloseServiceHandle(raw); - } - }; + operator SC_HANDLE() const { + return impl.get(); + } - std::unique_ptr<SC_HANDLE__, Deleter> impl; + void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(impl, other.impl); + } - ServiceHandle(const ServiceHandle&) = delete; +private: + struct Deleter { + void operator()(SC_HANDLE raw) { + CloseServiceHandle(raw); + } }; - inline void swap(ServiceHandle& a, ServiceHandle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + std::unique_ptr<SC_HANDLE__, Deleter> impl; + + ServiceHandle(const ServiceHandle&) = delete; +}; + +inline void swap(ServiceHandle& a, ServiceHandle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::ServiceHandle& a, - service::ServiceHandle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::ServiceHandle& a, service::ServiceHandle& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/service_manager.hpp b/um/service/include/service/service_manager.hpp index 2aa9312..a408d12 100644 --- a/um/service/include/service/service_manager.hpp +++ b/um/service/include/service/service_manager.hpp @@ -12,58 +12,49 @@ #include <utility> -namespace service -{ - class ServiceManager - { - public: - static ServiceManager open(); +namespace service { - ServiceManager(ServiceManager&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } +class ServiceManager { +public: + static ServiceManager open(); - ServiceManager& operator=(ServiceManager other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } + ServiceManager(ServiceManager&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(handle, other.handle); - } + ServiceManager& operator=(ServiceManager other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - operator SC_HANDLE() const - { - return handle; - } + void swap(ServiceManager& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(handle, other.handle); + } - private: - ServiceManager(ServiceHandle handle) - : handle(std::move(handle)) - { } + operator SC_HANDLE() const { + return handle; + } - ServiceHandle handle; +private: + ServiceManager(ServiceHandle handle) : handle(std::move(handle)) {} - ServiceManager(const ServiceManager&) = delete; - }; + ServiceHandle handle; - inline void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + ServiceManager(const ServiceManager&) = delete; +}; + +inline void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } -namespace std -{ - template <> - inline void swap( - service::ServiceManager& a, - service::ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } +} // namespace service + +namespace std { + +template <> +inline void swap(service::ServiceManager& a, service::ServiceManager& b) LIBSERVICE_NOEXCEPT { + a.swap(b); } + +} // namespace std diff --git a/um/service/include/service/singleton.hpp b/um/service/include/service/singleton.hpp index c76d54b..3eaacbf 100644 --- a/um/service/include/service/singleton.hpp +++ b/um/service/include/service/singleton.hpp @@ -7,37 +7,34 @@ #include <mutex> -namespace service -{ - template <typename DerivedT> - class Singleton - { - public: - static DerivedT& get() - { - std::call_once(initialized, initialize); - return get_unsafe(); - } +namespace service { - protected: - Singleton() = default; - virtual ~Singleton() = default; +template <typename DerivedT> +class Singleton { +public: + static DerivedT& get() { + std::call_once(initialized, initialize); + return get_unsafe(); + } - private: - static void initialize() - { - get_unsafe(); - } +protected: + Singleton() = default; + virtual ~Singleton() = default; - static DerivedT& get_unsafe() - { - static DerivedT instance; - return instance; - } +private: + static void initialize() { + get_unsafe(); + } - static std::once_flag initialized; - }; + static DerivedT& get_unsafe() { + static DerivedT instance; + return instance; + } - template <typename DerivedT> - std::once_flag Singleton<DerivedT>::initialized; -} + static std::once_flag initialized; +}; + +template <typename DerivedT> +std::once_flag Singleton<DerivedT>::initialized; + +} // namespace service diff --git a/um/service/include/service/windows_error.hpp b/um/service/include/service/windows_error.hpp index ab46cfd..d884ea7 100644 --- a/um/service/include/service/windows_error.hpp +++ b/um/service/include/service/windows_error.hpp @@ -14,45 +14,32 @@ #include <string> #include <system_error> -namespace service -{ - namespace windows_error - { - class Category - : public std::error_category - , public Singleton<Category> - { - public: - const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; } +namespace service { +namespace windows_error { - std::string message(int) const; +class Category + : public std::error_category + , public Singleton<Category> { +public: + const char* name() const LIBSERVICE_NOEXCEPT { + return "Windows"; + } - private: - friend class Singleton<Category>; - }; + std::string message(int) const; - inline std::string build_message_prefix( - const char* file, - int line, - const char* function) - { - std::ostringstream oss; - oss << "Error in function '" << function - << "' at file '" << file - << "', line " << line; - return oss.str(); - } +private: + friend class Singleton<Category>; +}; - inline std::system_error make( - DWORD code, - const char* file, - int line, - const char* function) - { - return std::system_error( - code, - Category::get(), - build_message_prefix(file, line, function)); - } - } +inline std::string build_message_prefix(const char* file, int line, const char* function) { + std::ostringstream oss; + oss << "Error in function '" << function << "' at file '" << file << "', line " << line; + return oss.str(); } + +inline std::system_error make(DWORD code, const char* file, int line, const char* function) { + return std::system_error(code, Category::get(), build_message_prefix(file, line, function)); +} + +} // namespace windows_error +} // namespace service diff --git a/um/service/src/device.cpp b/um/service/src/device.cpp index 870930f..2634a9a 100644 --- a/um/service/src/device.cpp +++ b/um/service/src/device.cpp @@ -8,108 +8,105 @@ #include <Windows.h> #include <cstddef> - #include <limits> #include <stdexcept> #include <string> -namespace service -{ - namespace - { - Handle open_device(const std::string& path) - { - const auto raw = CreateFileA( - path.c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); +namespace service { +namespace { - if (INVALID_HANDLE_VALUE == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +Handle open_device(const std::string& path) { + const auto raw = CreateFileA( + path.c_str(), + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL + ); - return Handle(raw); - } + if (INVALID_HANDLE_VALUE == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } - Device Device::open(const std::string& path) - { - return Device(open_device(path)); - } + return Handle(raw); +} - std::size_t Device::get_required_output_size( - Code code, - const void* in_buf, - std::size_t in_buf_size) const - { - DWORD nbreq; +} // namespace - if (in_buf_size > std::numeric_limits<DWORD>::max()) - throw std::range_error("input buffer size is too large"); +Device Device::open(const std::string& path) { + return Device(open_device(path)); +} - std::size_t nbwritten = DeviceIoControl( - handle, - code, - const_cast<void*>(in_buf), - static_cast<DWORD>(in_buf_size), - NULL, - 0, - &nbreq, - NULL); +std::size_t Device::get_required_output_size( + Code code, + const void* in_buf, + std::size_t in_buf_size +) const { + DWORD nbreq; - if (0 == nbwritten) - { - const auto ec = GetLastError(); + if (in_buf_size > std::numeric_limits<DWORD>::max()) + throw std::range_error("input buffer size is too large"); - switch (ec) - { - case ERROR_MORE_DATA: - return nbreq; + std::size_t nbwritten = DeviceIoControl( + handle, + code, + const_cast<void*>(in_buf), + static_cast<DWORD>(in_buf_size), + NULL, + 0, + &nbreq, + NULL + ); - default: - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + if (0 == nbwritten) { + const auto ec = GetLastError(); - return nbwritten; + switch (ec) { + case ERROR_MORE_DATA: + return nbreq; + + default: + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } } - std::size_t Device::send_control_code( - Code code, - const void* in_buf, - std::size_t in_buf_size, - void* out_buf, - std::size_t out_buf_size) const - { - DWORD nbreq; + return nbwritten; +} - if (in_buf_size > std::numeric_limits<DWORD>::max()) - throw std::range_error("input buffer size is too large"); - if (out_buf_size > std::numeric_limits<DWORD>::max()) - throw std::range_error("output buffer size is too large"); +std::size_t Device::send_control_code( + Code code, + const void* in_buf, + std::size_t in_buf_size, + void* out_buf, + std::size_t out_buf_size +) const { + DWORD nbreq; - std::size_t nbwritten = DeviceIoControl( - handle, - code, - const_cast<void*>(in_buf), - static_cast<DWORD>(in_buf_size), - out_buf, - static_cast<DWORD>(out_buf_size), - &nbreq, - NULL); + if (in_buf_size > std::numeric_limits<DWORD>::max()) + throw std::range_error("input buffer size is too large"); + if (out_buf_size > std::numeric_limits<DWORD>::max()) + throw std::range_error("output buffer size is too large"); - if (0 == nbwritten) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } + std::size_t nbwritten = DeviceIoControl( + handle, + code, + const_cast<void*>(in_buf), + static_cast<DWORD>(in_buf_size), + out_buf, + static_cast<DWORD>(out_buf_size), + &nbreq, + NULL + ); - return nbwritten; + if (0 == nbwritten) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } + + return nbwritten; } + +} // namespace service diff --git a/um/service/src/service.cpp b/um/service/src/service.cpp index b7384ce..698a9d9 100644 --- a/um/service/src/service.cpp +++ b/um/service/src/service.cpp @@ -9,242 +9,203 @@ #include <string> -namespace service -{ - namespace - { - ServiceHandle open_service( - const ServiceManager& mgr, - const std::string& name) - { - const auto raw = OpenServiceA( - mgr, - name.c_str(), - SERVICE_ALL_ACCESS); +namespace service { +namespace { - if (NULL == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +ServiceHandle open_service(const ServiceManager& mgr, const std::string& name) { + const auto raw = OpenServiceA(mgr, name.c_str(), SERVICE_ALL_ACCESS); - return raw; - } + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - ServiceHandle install_service( - const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) - { - const auto raw = CreateServiceA( - mgr, - name.c_str(), - name.c_str(), - SERVICE_ALL_ACCESS, - SERVICE_KERNEL_DRIVER, - SERVICE_DEMAND_START, - SERVICE_ERROR_NORMAL, - bin_path.c_str(), - NULL, - NULL, - NULL, - NULL, - NULL); + return raw; +} - if (NULL == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +ServiceHandle install_service( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path +) { + const auto raw = CreateServiceA( + mgr, + name.c_str(), + name.c_str(), + SERVICE_ALL_ACCESS, + SERVICE_KERNEL_DRIVER, + SERVICE_DEMAND_START, + SERVICE_ERROR_NORMAL, + bin_path.c_str(), + NULL, + NULL, + NULL, + NULL, + NULL + ); - return raw; - } + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - void start_service(const ServiceHandle& handle) - { - if (!StartService(handle, 0, NULL)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + return raw; +} - void stop_service(const ServiceHandle& handle) - { - SERVICE_STATUS service_status; +void start_service(const ServiceHandle& handle) { + if (!StartService(handle, 0, NULL)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } +void stop_service(const ServiceHandle& handle) { + SERVICE_STATUS service_status; - void uninstall_service(const ServiceHandle& handle) - { - if (!DeleteService(handle)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - bool service_exists( - const ServiceManager& mgr, - const std::string& name) - { - const auto raw = OpenServiceA( - mgr, - name.c_str(), - SERVICE_QUERY_STATUS); +void uninstall_service(const ServiceHandle& handle) { + if (!DeleteService(handle)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - if (NULL != raw) - { - ServiceHandle handle(raw); - return true; - } +bool service_exists(const ServiceManager& mgr, const std::string& name) { + const auto raw = OpenServiceA(mgr, name.c_str(), SERVICE_QUERY_STATUS); - const auto ec = GetLastError(); + if (NULL != raw) { + ServiceHandle handle(raw); + return true; + } - switch (ec) - { - case ERROR_SERVICE_DOES_NOT_EXIST: - return false; + const auto ec = GetLastError(); - default: - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + switch (ec) { + case ERROR_SERVICE_DOES_NOT_EXIST: + return false; - SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) - { - SERVICE_STATUS_PROCESS status; - DWORD nbreq; + default: + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - const auto buf_ptr = reinterpret_cast<BYTE*>(&status); - const auto buf_size = sizeof(status); +SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) { + SERVICE_STATUS_PROCESS status; + DWORD nbreq; - if (!QueryServiceStatusEx( - handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } + const auto buf_ptr = reinterpret_cast<BYTE*>(&status); + const auto buf_size = sizeof(status); - return status; - } + if (!QueryServiceStatusEx(handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - DWORD query_service_state(const ServiceHandle& handle) - { - return query_service_status(handle).dwCurrentState; - } + return status; +} - SERVICE_STATUS_PROCESS wait_for_service_state( - const ServiceHandle& handle, - const DWORD desired_state) - { - auto status = query_service_status(handle); +DWORD query_service_state(const ServiceHandle& handle) { + return query_service_status(handle).dwCurrentState; +} - DWORD old_timestamp = GetTickCount(); +SERVICE_STATUS_PROCESS wait_for_service_state( + const ServiceHandle& handle, + const DWORD desired_state +) { + auto status = query_service_status(handle); - DWORD old_check_point = status.dwCheckPoint; - DWORD old_wait_hint = status.dwWaitHint; + DWORD old_timestamp = GetTickCount(); - while (desired_state != status.dwCurrentState) - { - DWORD wait_time = old_wait_hint / 10; + DWORD old_check_point = status.dwCheckPoint; + DWORD old_wait_hint = status.dwWaitHint; - if (wait_time < 1000) - wait_time = 1000; - else if (wait_time > 10000) - wait_time = 10000; + while (desired_state != status.dwCurrentState) { + DWORD wait_time = old_wait_hint / 10; - Sleep(wait_time); + if (wait_time < 1000) + wait_time = 1000; + else if (wait_time > 10000) + wait_time = 10000; - status = query_service_status(handle); + Sleep(wait_time); - if (desired_state == status.dwCurrentState) - break; + status = query_service_status(handle); - if (status.dwCheckPoint > old_check_point) - { - old_timestamp = GetTickCount(); + if (desired_state == status.dwCurrentState) + break; - old_check_point = status.dwCheckPoint; - old_wait_hint = status.dwWaitHint; - } - else if (GetTickCount() - old_timestamp > old_wait_hint) - { - return status; - } - } + if (status.dwCheckPoint > old_check_point) { + old_timestamp = GetTickCount(); + old_check_point = status.dwCheckPoint; + old_wait_hint = status.dwWaitHint; + } else if (GetTickCount() - old_timestamp > old_wait_hint) { return status; } } - Service Service::open( - const ServiceManager& mgr, - const std::string& name) - { - return open_service(mgr, name); - } + return status; +} - Service Service::install( - const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) - { - return install_service(mgr, name, bin_path); - } +} // namespace - bool Service::exists( - const ServiceManager& mgr, - const std::string& name) - { - return service_exists(mgr, name); - } +Service Service::open(const ServiceManager& mgr, const std::string& name) { + return open_service(mgr, name); +} - void Service::start() const - { - const auto state = query_service_state(handle); +Service Service::install( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path +) { + return install_service(mgr, name, bin_path); +} - switch (state) - { - case SERVICE_STOPPED: - break; +bool Service::exists(const ServiceManager& mgr, const std::string& name) { + return service_exists(mgr, name); +} - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - break; +void Service::start() const { + const auto state = query_service_state(handle); - default: - return; - } + switch (state) { + case SERVICE_STOPPED: + break; - start_service(handle); - wait_for_service_state(handle, SERVICE_RUNNING); + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + break; + + default: + return; } - void Service::stop() const - { - switch (query_service_state(handle)) - { - case SERVICE_STOPPED: - return; + start_service(handle); + wait_for_service_state(handle, SERVICE_RUNNING); +} - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - return; - } +void Service::stop() const { + switch (query_service_state(handle)) { + case SERVICE_STOPPED: + return; - stop_service(handle); - wait_for_service_state(handle, SERVICE_STOPPED); + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + return; } - void Service::uninstall() const - { - stop(); - uninstall_service(handle); - } + stop_service(handle); + wait_for_service_state(handle, SERVICE_STOPPED); } + +void Service::uninstall() const { + stop(); + uninstall_service(handle); +} + +} // namespace service diff --git a/um/service/src/service_manager.cpp b/um/service/src/service_manager.cpp index 48c6bfe..b0e58fe 100644 --- a/um/service/src/service_manager.cpp +++ b/um/service/src/service_manager.cpp @@ -7,18 +7,17 @@ #include <Windows.h> -namespace service -{ - ServiceManager ServiceManager::open() - { - const auto raw = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); +namespace service { - if (NULL == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +ServiceManager ServiceManager::open() { + const auto raw = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); - return ServiceHandle(raw); + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } + + return ServiceHandle(raw); } + +} // namespace service diff --git a/um/service/src/windows_error.cpp b/um/service/src/windows_error.cpp index c228536..c6cca1e 100644 --- a/um/service/src/windows_error.cpp +++ b/um/service/src/windows_error.cpp @@ -9,34 +9,31 @@ #include <string> -namespace service -{ - namespace windows_error - { - std::string Category::message(int code) const - { - char* buf_ptr; +namespace service { +namespace windows_error { - const auto nbwritten = FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast<char*>(&buf_ptr), - 0, - NULL); +std::string Category::message(int code) const { + char* buf_ptr; - if (0 == nbwritten) - { - LocalFree(buf_ptr); - return "Couldn't format the error message"; - } + const auto nbwritten = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<char*>(&buf_ptr), + 0, + NULL + ); - std::string str(buf_ptr, nbwritten - 2); - LocalFree(buf_ptr); - return str; - } + if (0 == nbwritten) { + LocalFree(buf_ptr); + return "Couldn't format the error message"; } + + std::string str(buf_ptr, nbwritten - 2); + LocalFree(buf_ptr); + return str; } + +} // namespace windows_error +} // namespace service diff --git a/um/service/test/windows_error.cpp b/um/service/test/windows_error.cpp index 1334620..5f8d457 100644 --- a/um/service/test/windows_error.cpp +++ b/um/service/test/windows_error.cpp @@ -10,16 +10,10 @@ #include <exception> #include <iostream> -int main() -{ - try - { - throw service::windows_error::make( - ERROR_FILE_NOT_FOUND, - __FILE__, __LINE__, __FUNCTION__); - } - catch (const std::exception& e) - { +int main() { + try { + throw service::windows_error::make(ERROR_FILE_NOT_FOUND, __FILE__, __LINE__, __FUNCTION__); + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/install_service.cpp b/um/service/utils/install_service.cpp index c452e0d..5af98ef 100644 --- a/um/service/utils/install_service.cpp +++ b/um/service/utils/install_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 3) - { +int main(int argc, char* argv[]) { + if (argc != 3) { std::cout << "Usage: " << argv[0] << " NAME SYS_PATH\n"; return 1; } - try - { + try { service::Service::install(service::ServiceManager::open(), argv[1], argv[2]); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/start_service.cpp b/um/service/utils/start_service.cpp index 79c2d24..1743ece 100644 --- a/um/service/utils/start_service.cpp +++ b/um/service/utils/start_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 2) - { +int main(int argc, char* argv[]) { + if (argc != 2) { std::cout << "Usage: " << argv[0] << " NAME\n"; return 1; } - try - { + try { service::Service::open(service::ServiceManager::open(), argv[1]).start(); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/stop_service.cpp b/um/service/utils/stop_service.cpp index d9ccde9..fcfa67e 100644 --- a/um/service/utils/stop_service.cpp +++ b/um/service/utils/stop_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 2) - { +int main(int argc, char* argv[]) { + if (argc != 2) { std::cout << "Usage: " << argv[0] << " NAME\n"; return 1; } - try - { + try { service::Service::open(service::ServiceManager::open(), argv[1]).stop(); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/service/utils/uninstall_service.cpp b/um/service/utils/uninstall_service.cpp index aa5af85..d7c105c 100644 --- a/um/service/utils/uninstall_service.cpp +++ b/um/service/utils/uninstall_service.cpp @@ -8,20 +8,15 @@ #include <exception> #include <iostream> -int main(int argc, char* argv[]) -{ - if (argc != 2) - { +int main(int argc, char* argv[]) { + if (argc != 2) { std::cout << "Usage: " << argv[0] << " NAME\n"; return 1; } - try - { + try { service::Service::open(service::ServiceManager::open(), argv[1]).uninstall(); - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/wrappers/nt_namespace/include/nt_namespace/device.hpp b/um/wrappers/nt_namespace/include/nt_namespace/device.hpp index a3d725c..3856f2e 100644 --- a/um/wrappers/nt_namespace/include/nt_namespace/device.hpp +++ b/um/wrappers/nt_namespace/include/nt_namespace/device.hpp @@ -9,13 +9,13 @@ #include <string> -namespace nt_namespace -{ - class Device : service::Device - { - public: - Device(); +namespace nt_namespace { - std::wstring convert_nt_path(const std::wstring&); - }; -} +class Device : service::Device { +public: + Device(); + + std::wstring convert_nt_path(const std::wstring&); +}; + +} // namespace nt_namespace diff --git a/um/wrappers/nt_namespace/src/device.cpp b/um/wrappers/nt_namespace/src/device.cpp index 4f9021b..4e1d7b0 100644 --- a/um/wrappers/nt_namespace/src/device.cpp +++ b/um/wrappers/nt_namespace/src/device.cpp @@ -12,39 +12,35 @@ #include <string> #include <vector> -namespace nt_namespace -{ - namespace - { - const auto device_path = "\\\\.\\nt_namespace"; - const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } +namespace nt_namespace { +namespace { - Device::Device() - : service::Device(service::Device::open(device_path)) - { } +const auto device_path = "\\\\.\\nt_namespace"; +const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - std::wstring Device::convert_nt_path(const std::wstring& src) - { - const auto in_buf = src.c_str(); - const auto in_buf_size = (src.size() + 1) * sizeof(wchar_t); +} // namespace - static_assert(sizeof(control_code) == sizeof(service::Device::Code), "CTL_CODE() must return DWORDs"); +Device::Device() : service::Device(service::Device::open(device_path)) {} - const auto nbreq = get_required_output_size( - static_cast<service::Device::Code>(control_code), - in_buf, - in_buf_size); +std::wstring Device::convert_nt_path(const std::wstring& src) { + const auto in_buf = src.c_str(); + const auto in_buf_size = (src.size() + 1) * sizeof(wchar_t); - std::vector<unsigned char> output(nbreq); + static_assert( + sizeof(control_code) == sizeof(service::Device::Code), "CTL_CODE() must return DWORDs" + ); - send_control_code( - static_cast<service::Device::Code>(control_code), - in_buf, - in_buf_size, - output.data(), - nbreq); + const auto nbreq = get_required_output_size( + static_cast<service::Device::Code>(control_code), in_buf, in_buf_size + ); - return reinterpret_cast<wchar_t*>(output.data()); - } + std::vector<unsigned char> output(nbreq); + + send_control_code( + static_cast<service::Device::Code>(control_code), in_buf, in_buf_size, output.data(), nbreq + ); + + return reinterpret_cast<wchar_t*>(output.data()); } + +} // namespace nt_namespace diff --git a/um/wrappers/nt_namespace/utils/convert_nt_path.cpp b/um/wrappers/nt_namespace/utils/convert_nt_path.cpp index ec215dc..b4500ee 100644 --- a/um/wrappers/nt_namespace/utils/convert_nt_path.cpp +++ b/um/wrappers/nt_namespace/utils/convert_nt_path.cpp @@ -8,16 +8,12 @@ #include <exception> #include <iostream> -int wmain(int argc, wchar_t* argv[]) -{ - try - { +int wmain(int argc, wchar_t* argv[]) { + try { nt_namespace::Device dev; for (int i = 1; i < argc; ++i) std::wcout << dev.convert_nt_path(argv[i]) << L"\n"; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/um/wrappers/simple/include/simple/device.hpp b/um/wrappers/simple/include/simple/device.hpp index acbda76..5d18516 100644 --- a/um/wrappers/simple/include/simple/device.hpp +++ b/um/wrappers/simple/include/simple/device.hpp @@ -7,13 +7,13 @@ #include "service/all.hpp" -namespace simple -{ - class Device : service::Device - { - public: - Device(); +namespace simple { - unsigned int exchange_ints(unsigned int) const; - }; -} +class Device : service::Device { +public: + Device(); + + unsigned int exchange_ints(unsigned int) const; +}; + +} // namespace simple diff --git a/um/wrappers/simple/src/device.cpp b/um/wrappers/simple/src/device.cpp index d1a0d42..1388c55 100644 --- a/um/wrappers/simple/src/device.cpp +++ b/um/wrappers/simple/src/device.cpp @@ -3,37 +3,38 @@ // For details, see https://github.com/egor-tensin/windows7-drivers. // Distributed under the MIT License. -#include "simple/all.hpp" - #include "service/all.hpp" +#include "simple/all.hpp" #include <Windows.h> -namespace simple -{ - namespace - { - const auto device_path = "\\\\.\\simple_device1"; - const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } +namespace simple { +namespace { - Device::Device() - : service::Device(service::Device::open(device_path)) - { } +const auto device_path = "\\\\.\\simple_device1"; +const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - unsigned int Device::exchange_ints(unsigned int src) const - { - static_assert(sizeof(exchange_ints_ctl_code) == sizeof(service::Device::Code), "CTL_CODE() must produce DWORDs"); +} // namespace - unsigned int dest; +Device::Device() : service::Device(service::Device::open(device_path)) {} - send_control_code( - static_cast<service::Device::Code>(exchange_ints_ctl_code), - &src, - sizeof(src), - &dest, - sizeof(dest)); +unsigned int Device::exchange_ints(unsigned int src) const { + static_assert( + sizeof(exchange_ints_ctl_code) == sizeof(service::Device::Code), + "CTL_CODE() must produce DWORDs" + ); - return dest; - } + unsigned int dest; + + send_control_code( + static_cast<service::Device::Code>(exchange_ints_ctl_code), + &src, + sizeof(src), + &dest, + sizeof(dest) + ); + + return dest; } + +} // namespace simple diff --git a/um/wrappers/simple/utils/exchange_ints.cpp b/um/wrappers/simple/utils/exchange_ints.cpp index cf8d70c..59f8dab 100644 --- a/um/wrappers/simple/utils/exchange_ints.cpp +++ b/um/wrappers/simple/utils/exchange_ints.cpp @@ -10,32 +10,27 @@ #include <sstream> #include <string> -namespace -{ - bool parse_int(unsigned int& dest, const std::string& src) - { - std::istringstream iss(src); - char c; - return iss >> dest && !iss.get(c); - } +namespace { + +bool parse_int(unsigned int& dest, const std::string& src) { + std::istringstream iss(src); + char c; + return iss >> dest && !iss.get(c); } -int main(int argc, char* argv[]) -{ - try - { +} // namespace + +int main(int argc, char* argv[]) { + try { unsigned int src = 0; - if (argc != 2 || !parse_int(src, argv[1])) - { + if (argc != 2 || !parse_int(src, argv[1])) { std::cout << "Usage: " << argv[0] << " N\n"; return 1; } std::cout << simple::Device().exchange_ints(src) << "\n"; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } |
