147 lines
3.9 KiB
C++
147 lines
3.9 KiB
C++
#include <Void/System/SignalWatcher.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cerrno>
|
|
#include <climits>
|
|
#include <csignal>
|
|
#include <cstring>
|
|
#include <format>
|
|
#include <utility>
|
|
|
|
#ifdef __linux__
|
|
#include <poll.h>
|
|
#include <pthread.h>
|
|
#include <sys/signalfd.h>
|
|
#include <unistd.h>
|
|
#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> 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>
|
|
SignalWatcher::create(std::vector<int> 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>();
|
|
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<std::optional<int>>
|
|
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<int>(std::clamp<std::int64_t>(
|
|
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<int>{};
|
|
}
|
|
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<ssize_t>(sizeof(info))) {
|
|
return Void::Result::fail(
|
|
std::format("Process::SignalWatcher::wait: read failed: {}",
|
|
std::strerror(errno)));
|
|
}
|
|
return std::optional<int>{static_cast<int>(info.ssi_signo)};
|
|
#else
|
|
(void)timeout;
|
|
return Void::Result::fail(
|
|
"Process::SignalWatcher is currently supported only on Linux");
|
|
#endif
|
|
}
|
|
|
|
Void::Result::Result<std::optional<int>> SignalWatcher::poll()
|
|
{
|
|
return wait(std::chrono::milliseconds{0});
|
|
}
|
|
} // namespace Void::Process
|