#include #include #include #include #include #include #include #include #ifdef __linux__ #include #include #include #include #endif namespace Void::Process { struct SignalWatcher::Impl { #ifdef __linux__ int descriptor{-1}; sigset_t previousMask{}; bool maskInstalled{false}; pthread_t owner{}; ~Impl() { if (descriptor >= 0) { ::close(descriptor); } if (maskInstalled && pthread_equal(owner, pthread_self()) != 0) { (void)::pthread_sigmask( SIG_SETMASK, &previousMask, nullptr); } } #else ~Impl() = default; #endif }; SignalWatcher::SignalWatcher(std::unique_ptr impl) noexcept : m_impl(std::move(impl)) { } SignalWatcher::~SignalWatcher() = default; SignalWatcher::SignalWatcher(SignalWatcher&&) noexcept = default; SignalWatcher& SignalWatcher::operator=(SignalWatcher&&) noexcept = default; Void::Result::Result SignalWatcher::create(std::vector signals) { #ifdef __linux__ if (signals.empty()) { signals = {SIGINT, SIGTERM}; } sigset_t mask{}; if (::sigemptyset(&mask) != 0) { return Void::Result::fail( std::format("Process::SignalWatcher: sigemptyset failed: {}", std::strerror(errno))); } for (const int signal : signals) { if (signal <= 0 || signal == SIGKILL || signal == SIGSTOP || ::sigaddset(&mask, signal) != 0) { return Void::Result::fail(std::format( "Process::SignalWatcher: signal {} cannot be watched", signal)); } } auto impl = std::make_unique(); impl->owner = ::pthread_self(); const int maskResult = ::pthread_sigmask(SIG_BLOCK, &mask, &impl->previousMask); if (maskResult != 0) { return Void::Result::fail(std::format( "Process::SignalWatcher: pthread_sigmask failed: {}", std::strerror(maskResult))); } impl->maskInstalled = true; impl->descriptor = ::signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK); if (impl->descriptor < 0) { return Void::Result::fail( std::format("Process::SignalWatcher: signalfd failed: {}", std::strerror(errno))); } return SignalWatcher{std::move(impl)}; #else (void)signals; return Void::Result::fail( "Process::SignalWatcher is currently supported only on Linux"); #endif } Void::Result::Result> SignalWatcher::wait(std::chrono::milliseconds timeout) { #ifdef __linux__ if (!m_impl || m_impl->descriptor < 0) { return Void::Result::fail( "Process::SignalWatcher::wait: watcher is moved-from"); } const int timeoutMs = timeout == std::chrono::milliseconds::max() ? -1 : static_cast(std::clamp( timeout.count(), 0, INT_MAX)); pollfd descriptor{m_impl->descriptor, POLLIN, 0}; int pollResult = 0; do { pollResult = ::poll(&descriptor, 1, timeoutMs); } while (pollResult < 0 && errno == EINTR); if (pollResult == 0) { return std::optional{}; } if (pollResult < 0) { return Void::Result::fail( std::format("Process::SignalWatcher::wait: poll failed: {}", std::strerror(errno))); } if ((descriptor.revents & POLLIN) == 0) { return Void::Result::fail( "Process::SignalWatcher::wait: signal descriptor closed"); } signalfd_siginfo info{}; const auto readCount = ::read(m_impl->descriptor, &info, sizeof(info)); if (readCount != static_cast(sizeof(info))) { return Void::Result::fail( std::format("Process::SignalWatcher::wait: read failed: {}", std::strerror(errno))); } return std::optional{static_cast(info.ssi_signo)}; #else (void)timeout; return Void::Result::fail( "Process::SignalWatcher is currently supported only on Linux"); #endif } Void::Result::Result> SignalWatcher::poll() { return wait(std::chrono::milliseconds{0}); } } // namespace Void::Process