From b3eef140da853414776c8e03a70629a613a25ed0 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 15 Jun 2026 14:05:29 +0200 Subject: workflows/ci: add clang-format job --- um/service/include/service/device.hpp | 115 ++++--- um/service/include/service/handle.hpp | 100 +++--- um/service/include/service/service.hpp | 111 +++---- um/service/include/service/service_handle.hpp | 100 +++--- um/service/include/service/service_manager.hpp | 93 +++--- um/service/include/service/singleton.hpp | 65 ++-- um/service/include/service/windows_error.hpp | 67 ++-- um/service/src/device.cpp | 181 +++++------ um/service/src/service.cpp | 359 +++++++++------------ um/service/src/service_manager.cpp | 21 +- um/service/src/windows_error.cpp | 55 ++-- um/service/test/windows_error.cpp | 14 +- um/service/utils/install_service.cpp | 13 +- um/service/utils/start_service.cpp | 13 +- um/service/utils/stop_service.cpp | 13 +- um/service/utils/uninstall_service.cpp | 13 +- .../nt_namespace/include/nt_namespace/device.hpp | 18 +- um/wrappers/nt_namespace/src/device.cpp | 66 ++-- um/wrappers/nt_namespace/utils/convert_nt_path.cpp | 10 +- um/wrappers/simple/include/simple/device.hpp | 18 +- um/wrappers/simple/src/device.cpp | 59 ++-- um/wrappers/simple/utils/exchange_ints.cpp | 29 +- 22 files changed, 691 insertions(+), 842 deletions(-) (limited to 'um') 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 #include - #include #include -namespace service -{ - 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; - }; - - inline void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); +namespace service { + +class Device { +public: + typedef DWORD Code; + + static Device open(const std::string& path); + + Device(Device&& other) LIBSERVICE_NOEXCEPT { + swap(other); } -} -namespace std -{ - template <> - inline void swap( - service::Device& a, - service::Device& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); + 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; +}; + +inline void swap(Device& a, 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 #include -namespace service -{ - class Handle - { - public: - Handle() = default; - - Handle(HANDLE raw) - : impl(raw) - { } - - Handle(Handle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } +namespace service { - Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } +class Handle { +public: + Handle() = default; - operator bool() const - { - return static_cast(impl); - } + Handle(HANDLE raw) : impl(raw) {} - operator HANDLE() const - { - return impl.get(); - } + Handle(Handle&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(Handle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } + Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - private: - struct Deleter - { - void operator()(HANDLE raw) - { - CloseHandle(raw); - } - }; + operator bool() const { + return static_cast(impl); + } - std::unique_ptr::type, Deleter> impl; + operator HANDLE() const { + return impl.get(); + } - Handle(const Handle&) = delete; + void swap(Handle& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(impl, other.impl); + } + +private: + struct Deleter { + void operator()(HANDLE raw) { + CloseHandle(raw); + } }; - inline void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); - } + std::unique_ptr::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 #include -namespace service -{ - 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; - }; - - inline void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); +namespace service { + +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; } -} -namespace std -{ - template <> - inline void swap( - service::Service& a, - service::Service& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); + 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; +}; + +inline void swap(Service& a, 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 #include -namespace service -{ - class ServiceHandle - { - public: - ServiceHandle() = default; - - ServiceHandle(SC_HANDLE raw) - : impl(raw) - { } - - ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT - { - swap(other); - } +namespace service { - ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT - { - swap(other); - return *this; - } +class ServiceHandle { +public: + ServiceHandle() = default; - operator bool() const - { - return static_cast(impl); - } + ServiceHandle(SC_HANDLE raw) : impl(raw) {} - operator SC_HANDLE() const - { - return impl.get(); - } + ServiceHandle(ServiceHandle&& other) LIBSERVICE_NOEXCEPT { + swap(other); + } - void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT - { - using std::swap; - swap(impl, other.impl); - } + ServiceHandle& operator=(ServiceHandle other) LIBSERVICE_NOEXCEPT { + swap(other); + return *this; + } - private: - struct Deleter - { - void operator()(SC_HANDLE raw) - { - CloseServiceHandle(raw); - } - }; + operator bool() const { + return static_cast(impl); + } - std::unique_ptr impl; + operator SC_HANDLE() const { + return impl.get(); + } - ServiceHandle(const ServiceHandle&) = delete; + void swap(ServiceHandle& other) LIBSERVICE_NOEXCEPT { + using std::swap; + swap(impl, other.impl); + } + +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 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 -namespace service -{ - 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; - }; - - inline void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); +namespace service { + +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); } -} -namespace std -{ - template <> - inline void swap( - service::ServiceManager& a, - service::ServiceManager& b) LIBSERVICE_NOEXCEPT - { - a.swap(b); + operator SC_HANDLE() const { + return handle; } + +private: + ServiceManager(ServiceHandle handle) : handle(std::move(handle)) {} + + ServiceHandle handle; + + ServiceManager(const ServiceManager&) = delete; +}; + +inline void swap(ServiceManager& a, 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 -namespace service -{ - 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; -} +namespace service { + +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; + +} // 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 #include -namespace service -{ - namespace windows_error - { - class Category - : public std::error_category - , public Singleton - { - public: - const char* name() const LIBSERVICE_NOEXCEPT { return "Windows"; } - - std::string message(int) const; - - private: - friend class Singleton; - }; - - 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 service { +namespace windows_error { + +class Category + : public std::error_category + , public Singleton { +public: + const char* name() const LIBSERVICE_NOEXCEPT { + return "Windows"; } + + std::string message(int) const; + +private: + friend class Singleton; +}; + +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 #include - #include #include #include -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); - - if (INVALID_HANDLE_VALUE == raw) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - - return Handle(raw); - } +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 + ); + + 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; - - 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 windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } +} // namespace - return nbwritten; - } +Device Device::open(const std::string& path) { + return Device(open_device(path)); +} - 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 windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); +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 windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); } + } - return nbwritten; + 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 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 -namespace service -{ - 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 windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - - return raw; - } +namespace service { +namespace { - 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 windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - - return raw; - } +ServiceHandle open_service(const ServiceManager& mgr, const std::string& name) { + const auto raw = OpenServiceA(mgr, name.c_str(), SERVICE_ALL_ACCESS); - void start_service(const ServiceHandle& handle) - { - if (!StartService(handle, 0, NULL)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - void stop_service(const ServiceHandle& handle) - { - SERVICE_STATUS service_status; + return raw; +} - if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) - { - 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 + ); + + if (NULL == raw) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - void uninstall_service(const ServiceHandle& handle) - { - if (!DeleteService(handle)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } - } + return raw; +} - 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 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__); + } +} - SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) - { - SERVICE_STATUS_PROCESS status; - DWORD nbreq; +void stop_service(const ServiceHandle& handle) { + SERVICE_STATUS service_status; - const auto buf_ptr = reinterpret_cast(&status); - const auto buf_size = sizeof(status); + if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - if (!QueryServiceStatusEx( - handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) - { - const auto ec = GetLastError(); - throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); - } +void uninstall_service(const ServiceHandle& handle) { + if (!DeleteService(handle)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - return status; - } +bool service_exists(const ServiceManager& mgr, const std::string& name) { + const auto raw = OpenServiceA(mgr, name.c_str(), SERVICE_QUERY_STATUS); - DWORD query_service_state(const ServiceHandle& handle) - { - return query_service_status(handle).dwCurrentState; - } + if (NULL != raw) { + ServiceHandle handle(raw); + return true; + } - SERVICE_STATUS_PROCESS wait_for_service_state( - const ServiceHandle& handle, - const DWORD desired_state) - { - auto status = query_service_status(handle); + const auto ec = GetLastError(); - DWORD old_timestamp = GetTickCount(); + switch (ec) { + case ERROR_SERVICE_DOES_NOT_EXIST: + return false; - DWORD old_check_point = status.dwCheckPoint; - DWORD old_wait_hint = status.dwWaitHint; + default: + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } +} - while (desired_state != status.dwCurrentState) - { - DWORD wait_time = old_wait_hint / 10; +SERVICE_STATUS_PROCESS query_service_status(const ServiceHandle& handle) { + SERVICE_STATUS_PROCESS status; + DWORD nbreq; - if (wait_time < 1000) - wait_time = 1000; - else if (wait_time > 10000) - wait_time = 10000; + const auto buf_ptr = reinterpret_cast(&status); + const auto buf_size = sizeof(status); - Sleep(wait_time); + if (!QueryServiceStatusEx(handle, SC_STATUS_PROCESS_INFO, buf_ptr, buf_size, &nbreq)) { + const auto ec = GetLastError(); + throw windows_error::make(ec, __FILE__, __LINE__, __FUNCTION__); + } - status = query_service_status(handle); + return status; +} - if (desired_state == status.dwCurrentState) - break; +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; - if (status.dwCheckPoint > old_check_point) - { - old_timestamp = GetTickCount(); + Sleep(wait_time); - old_check_point = status.dwCheckPoint; - old_wait_hint = status.dwWaitHint; - } - else if (GetTickCount() - old_timestamp > old_wait_hint) - { - return status; - } - } + 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; } } - 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); +} + +Service Service::install( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path +) { + return install_service(mgr, name, bin_path); +} - void Service::start() const - { - const auto state = query_service_state(handle); +bool Service::exists(const ServiceManager& mgr, const std::string& name) { + return service_exists(mgr, name); +} - switch (state) - { - case SERVICE_STOPPED: - break; +void Service::start() const { + const auto state = query_service_state(handle); - case SERVICE_STOP_PENDING: - wait_for_service_state(handle, SERVICE_STOPPED); - break; + switch (state) { + case SERVICE_STOPPED: + break; - default: - return; - } + case SERVICE_STOP_PENDING: + wait_for_service_state(handle, SERVICE_STOPPED); + break; - start_service(handle); - wait_for_service_state(handle, SERVICE_RUNNING); + 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 -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 -namespace service -{ - namespace windows_error - { - std::string Category::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 the error message"; - } - - std::string str(buf_ptr, nbwritten - 2); - LocalFree(buf_ptr); - return str; - } +namespace service { +namespace windows_error { + +std::string Category::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 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 #include -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 #include -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 #include -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 #include -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 #include -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 -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 #include -namespace nt_namespace -{ - namespace - { - const auto device_path = "\\\\.\\nt_namespace"; - const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } - - Device::Device() - : service::Device(service::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); - - static_assert(sizeof(control_code) == sizeof(service::Device::Code), "CTL_CODE() must return DWORDs"); - - const auto nbreq = get_required_output_size( - static_cast(control_code), - in_buf, - in_buf_size); - - std::vector output(nbreq); - - send_control_code( - static_cast(control_code), - in_buf, - in_buf_size, - output.data(), - nbreq); - - return reinterpret_cast(output.data()); - } +namespace nt_namespace { +namespace { + +const auto device_path = "\\\\.\\nt_namespace"; +const auto control_code = CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); + +} // namespace + +Device::Device() : service::Device(service::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); + + static_assert( + sizeof(control_code) == sizeof(service::Device::Code), "CTL_CODE() must return DWORDs" + ); + + const auto nbreq = get_required_output_size( + static_cast(control_code), in_buf, in_buf_size + ); + + std::vector output(nbreq); + + send_control_code( + static_cast(control_code), in_buf, in_buf_size, output.data(), nbreq + ); + + return reinterpret_cast(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 #include -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 -namespace simple -{ - namespace - { - const auto device_path = "\\\\.\\simple_device1"; - const auto exchange_ints_ctl_code = CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS); - } - - Device::Device() - : service::Device(service::Device::open(device_path)) - { } - - 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"); - - unsigned int dest; - - send_control_code( - static_cast(exchange_ints_ctl_code), - &src, - sizeof(src), - &dest, - sizeof(dest)); - - return dest; - } +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 + +Device::Device() : service::Device(service::Device::open(device_path)) {} + +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" + ); + + unsigned int dest; + + send_control_code( + static_cast(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 #include -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; } -- cgit v1.3