diff options
Diffstat (limited to '')
-rw-r--r-- | utils/libservice/src/device.cpp | 61 | ||||
-rw-r--r-- | utils/libservice/src/handle.cpp | 5 | ||||
-rw-r--r-- | utils/libservice/src/service.cpp | 113 | ||||
-rw-r--r-- | utils/libservice/src/service_handle.cpp | 3 | ||||
-rw-r--r-- | utils/libservice/src/service_manager.cpp | 10 | ||||
-rw-r--r-- | utils/libservice/src/windows_error.cpp | 14 |
6 files changed, 110 insertions, 96 deletions
diff --git a/utils/libservice/src/device.cpp b/utils/libservice/src/device.cpp index 3cfb760..4ae25a5 100644 --- a/utils/libservice/src/device.cpp +++ b/utils/libservice/src/device.cpp @@ -6,13 +6,12 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/device.hpp" -#include "libservice/handle.hpp" -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include <Windows.h> +#include <cstddef> + #include <string> #include <system_error> #include <utility> @@ -23,18 +22,20 @@ namespace libservice { 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); + 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, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } return Handle(raw); @@ -46,15 +47,15 @@ namespace libservice return Device(open_device(path)); } - DWORD Device::get_required_output_size( - DWORD code, + std::size_t Device::get_required_output_size( + Code code, const void* in_buf, - DWORD in_buf_size) const + std::size_t in_buf_size) const { DWORD nbreq; - DWORD ret = DeviceIoControl( - static_cast<HANDLE>(m_handle), + std::size_t nbwritten = DeviceIoControl( + handle, code, const_cast<void*>(in_buf), in_buf_size, @@ -63,7 +64,7 @@ namespace libservice &nbreq, NULL); - if (0 == ret) + if (0 == nbwritten) { const auto ec = GetLastError(); @@ -73,24 +74,25 @@ namespace libservice return nbreq; default: - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } - return ret; + return nbwritten; } - DWORD Device::send_control_code( - DWORD code, + std::size_t Device::send_control_code( + Code code, const void* in_buf, - DWORD in_buf_size, + std::size_t in_buf_size, void* out_buf, - DWORD out_buf_size) const + std::size_t out_buf_size) const { DWORD nbreq; - DWORD ret = DeviceIoControl( - static_cast<HANDLE>(m_handle), + std::size_t nbwritten = DeviceIoControl( + handle, code, const_cast<void*>(in_buf), in_buf_size, @@ -99,13 +101,14 @@ namespace libservice &nbreq, NULL); - if (0 == ret) + if (0 == nbwritten) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ret; + return nbwritten; } void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT diff --git a/utils/libservice/src/handle.cpp b/utils/libservice/src/handle.cpp index 2f9a65f..bc3dc35 100644 --- a/utils/libservice/src/handle.cpp +++ b/utils/libservice/src/handle.cpp @@ -6,8 +6,9 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/handle.hpp" +#include "libservice/all.hpp" + +#include <utility> namespace libservice { diff --git a/utils/libservice/src/service.cpp b/utils/libservice/src/service.cpp index efe645f..57bb568 100644 --- a/utils/libservice/src/service.cpp +++ b/utils/libservice/src/service.cpp @@ -6,15 +6,12 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/service.hpp" -#include "libservice/service_handle.hpp" -#include "libservice/service_manager.hpp" -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include <Windows.h> #include <string> +#include <system_error> #include <utility> #include <vector> @@ -22,20 +19,23 @@ namespace libservice { namespace { - ServiceHandle open_service(const ServiceManager& mgr, const std::string& name) + ServiceHandle open_service( + const ServiceManager& mgr, + const std::string& name) { const auto raw = OpenServiceA( - static_cast<SC_HANDLE>(mgr), + mgr, name.c_str(), SERVICE_ALL_ACCESS); if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ServiceHandle(raw); + return raw; } ServiceHandle install_service( @@ -44,7 +44,7 @@ namespace libservice const std::string& bin_path) { const auto raw = CreateServiceA( - static_cast<SC_HANDLE>(mgr), + mgr, name.c_str(), name.c_str(), SERVICE_ALL_ACCESS, @@ -61,18 +61,20 @@ namespace libservice if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ServiceHandle(raw); + return raw; } void start_service(const ServiceHandle& handle) { - if (!StartService(static_cast<SC_HANDLE>(handle), 0, NULL)) + if (!StartService(handle, 0, NULL)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } @@ -80,28 +82,32 @@ namespace libservice { SERVICE_STATUS service_status; - if (!ControlService(static_cast<SC_HANDLE>(handle), SERVICE_CONTROL_STOP, &service_status)) + if (!ControlService(handle, SERVICE_CONTROL_STOP, &service_status)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } void uninstall_service(const ServiceHandle& handle) { - if (!DeleteService(static_cast<SC_HANDLE>(handle))) + if (!DeleteService(handle)) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } } - bool does_service_exist(const ServiceManager& mgr, - const std::string& name) + bool service_exists( + const ServiceManager& mgr, + const std::string& name) { - const auto raw = OpenService(static_cast<SC_HANDLE>(mgr), - name.c_str(), - SERVICE_QUERY_STATUS); + const auto raw = OpenServiceA( + mgr, + name.c_str(), + SERVICE_QUERY_STATUS); if (NULL != raw) { @@ -117,23 +123,25 @@ namespace libservice return false; default: - throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + 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; - if (!QueryServiceStatusEx(static_cast<SC_HANDLE>(handle), - SC_STATUS_PROCESS_INFO, - reinterpret_cast<BYTE*>(&status), - sizeof(status), - &nbreq)) + const auto buf_ptr = reinterpret_cast<BYTE*>(&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, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } return status; @@ -143,7 +151,7 @@ namespace libservice { return query_service_status(handle).dwCurrentState; } - + SERVICE_STATUS_PROCESS wait_for_service_state( const ServiceHandle& handle, const DWORD desired_state) @@ -183,31 +191,36 @@ namespace libservice return status; } } + return status; } } - Service Service::open(const ServiceManager& mgr, const std::string& name) + Service Service::open( + const ServiceManager& mgr, + const std::string& name) { - return Service(open_service(mgr, name)); + return open_service(mgr, name); } - Service Service::install(const ServiceManager& mgr, - const std::string& name, - const std::string& bin_path) + Service Service::install( + const ServiceManager& mgr, + const std::string& name, + const std::string& bin_path) { - return Service(install_service(mgr, name, bin_path)); + return install_service(mgr, name, bin_path); } - bool Service::does_exist(const ServiceManager& mgr, - const std::string& name) + bool Service::exists( + const ServiceManager& mgr, + const std::string& name) { - return does_service_exist(mgr, name); + return service_exists(mgr, name); } void Service::start() const { - const auto state = query_service_state(m_handle); + const auto state = query_service_state(handle); switch (state) { @@ -215,37 +228,37 @@ namespace libservice break; case SERVICE_STOP_PENDING: - wait_for_service_state(m_handle, SERVICE_STOPPED); + wait_for_service_state(handle, SERVICE_STOPPED); break; default: return; } - start_service(m_handle); - wait_for_service_state(m_handle, SERVICE_RUNNING); + start_service(handle); + wait_for_service_state(handle, SERVICE_RUNNING); } void Service::stop() const { - switch (query_service_state(m_handle)) + switch (query_service_state(handle)) { case SERVICE_STOPPED: return; case SERVICE_STOP_PENDING: - wait_for_service_state(m_handle, SERVICE_STOPPED); + wait_for_service_state(handle, SERVICE_STOPPED); return; } - stop_service(m_handle); - wait_for_service_state(m_handle, SERVICE_STOPPED); + stop_service(handle); + wait_for_service_state(handle, SERVICE_STOPPED); } void Service::uninstall() const { stop(); - uninstall_service(m_handle); + uninstall_service(handle); } void swap(Service& a, Service& b) LIBSERVICE_NOEXCEPT diff --git a/utils/libservice/src/service_handle.cpp b/utils/libservice/src/service_handle.cpp index 21fb78c..ab85bc8 100644 --- a/utils/libservice/src/service_handle.cpp +++ b/utils/libservice/src/service_handle.cpp @@ -6,8 +6,7 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/service_handle.hpp" +#include "libservice/all.hpp" #include <utility> diff --git a/utils/libservice/src/service_manager.cpp b/utils/libservice/src/service_manager.cpp index b3c0b96..d815089 100644 --- a/utils/libservice/src/service_manager.cpp +++ b/utils/libservice/src/service_manager.cpp @@ -6,10 +6,7 @@ * See LICENSE.txt for details. */ -#include "libservice/common.hpp" -#include "libservice/service_handle.hpp" -#include "libservice/service_manager.hpp" -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include <Windows.h> @@ -25,10 +22,11 @@ namespace libservice if (NULL == raw) { const auto ec = GetLastError(); - throw std::system_error(ec, WinErrorCategory::get()); + throw std::system_error( + ec, WindowsErrorCategory::get(), LIBSERVICE_ERROR_PREFIX); } - return ServiceManager(ServiceHandle(raw)); + return ServiceHandle(raw); } void swap(ServiceManager& a, ServiceManager& b) LIBSERVICE_NOEXCEPT diff --git a/utils/libservice/src/windows_error.cpp b/utils/libservice/src/windows_error.cpp index 1e28345..2a54366 100644 --- a/utils/libservice/src/windows_error.cpp +++ b/utils/libservice/src/windows_error.cpp @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "libservice/windows_error.hpp" +#include "libservice/all.hpp" #include <Windows.h> @@ -14,14 +14,14 @@ namespace libservice { - std::string WinErrorCategory::message(int code) const + std::string WindowsErrorCategory::message(int code) const { char* buf_ptr; - DWORD written = FormatMessageA( + const auto nbwritten = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, + | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), @@ -29,13 +29,13 @@ namespace libservice 0, NULL); - if (0 == written) + if (0 == nbwritten) { LocalFree(buf_ptr); return "Couldn't format error message"; } - std::string str(buf_ptr, written - 2); + std::string str(buf_ptr, nbwritten - 2); LocalFree(buf_ptr); return str; } |