87# include <initializer_list>
144 for (
int sig : signals)
145 sigaddset(&
set_, sig);
163 sigaddset(&
set_, signo);
171 sigdelset(&
set_, signo);
192 [[nodiscard]]
bool contains(
int signo)
const noexcept
194 return sigismember(&
set_, signo) == 1;
198 [[nodiscard]] sigset_t *
get() noexcept {
return &
set_; }
201 [[nodiscard]]
const sigset_t *
get() const noexcept {
return &
set_; }
204 operator sigset_t *()
noexcept {
return &
set_; }
207 operator const sigset_t *()
const noexcept {
return &
set_; }
245 if (sigprocmask(SIG_BLOCK, signals.
get(), &
old_mask_) < 0)
246 throw SignalError(
"sigprocmask(SIG_BLOCK) failed", -1, errno);
269 sigprocmask(SIG_SETMASK, &
old_mask_,
nullptr);
289 other.active_ =
false;
297 sigprocmask(SIG_SETMASK, &
old_mask_,
nullptr);
300 other.active_ =
false;
372 struct sigaction act;
374 act.sa_handler = func;
375 sigemptyset(&act.sa_mask);
378 if (signo == SIGALRM)
381 act.sa_flags |= SA_INTERRUPT;
387 act.sa_flags |= SA_RESTART;
419 struct sigaction act;
421 act.sa_handler = func;
422 sigemptyset(&act.sa_mask);
425 if (signo == SIGALRM)
428 act.sa_flags |= SA_INTERRUPT;
434 act.sa_flags |= SA_RESTART;
440 std::cerr <<
"Cannot modify the action associated with the signal "
441 << signo << std::endl;
471 bool restart_calls =
true)
476 throw SignalError(
"Cannot modify signal action for signal " +
477 std::to_string(signo) +
": " + std::strerror(error),
504 bool restart_calls,
int & error_out)
526 std::cerr <<
"Cannot restore signal handler for signal "
589 other.active_ =
false;
610 other.active_ =
false;
638 int result = sigwait(signals.
get(), &sig);
654 return raise(signo) == 0;
669 case SIGHUP:
return "SIGHUP";
670 case SIGINT:
return "SIGINT";
671 case SIGQUIT:
return "SIGQUIT";
672 case SIGILL:
return "SIGILL";
673 case SIGABRT:
return "SIGABRT";
674 case SIGFPE:
return "SIGFPE";
675 case SIGKILL:
return "SIGKILL";
676 case SIGSEGV:
return "SIGSEGV";
677 case SIGPIPE:
return "SIGPIPE";
678 case SIGALRM:
return "SIGALRM";
679 case SIGTERM:
return "SIGTERM";
680 case SIGUSR1:
return "SIGUSR1";
681 case SIGUSR2:
return "SIGUSR2";
682 case SIGCHLD:
return "SIGCHLD";
683 case SIGCONT:
return "SIGCONT";
684 case SIGSTOP:
return "SIGSTOP";
685 case SIGTSTP:
return "SIGTSTP";
686 case SIGTTIN:
return "SIGTTIN";
687 case SIGTTOU:
return "SIGTTOU";
689 case SIGBUS:
return "SIGBUS";
692 case SIGTRAP:
return "SIGTRAP";
695 case SIGURG:
return "SIGURG";
698 case SIGXCPU:
return "SIGXCPU";
701 case SIGXFSZ:
return "SIGXFSZ";
704 case SIGVTALRM:
return "SIGVTALRM";
707 case SIGPROF:
return "SIGPROF";
710 case SIGWINCH:
return "SIGWINCH";
713 case SIGIO:
return "SIGIO";
716 case SIGSYS:
return "SIGSYS";
718 default:
return "Signal " + std::to_string(signo);
732 const char * desc = strsignal(signo);
int wait_for_signal(const SignalSet &signals)
Utility to wait for a signal.
bool send_signal_to_self(int signo) noexcept
Sends a signal to the current process.
std::string signal_description(int signo)
Returns a human-readable description of a signal.
std::string signal_name(int signo)
Returns the name of a signal.
Core header for the Aleph-w library.
RAII wrapper for temporarily blocking signals.
SignalBlocker(int signo)
Blocks a single signal.
SignalBlocker(SignalBlocker &&other) noexcept
SignalBlocker & operator=(const SignalBlocker &)=delete
void release() noexcept
Releases ownership - the mask won't be restored on destruction.
SignalBlocker(std::initializer_list< int > signals)
Blocks the signals specified in the initializer list.
const sigset_t & previous_mask() const noexcept
Returns the previous signal mask that will be restored.
SignalBlocker(const SignalBlocker &)=delete
SignalBlocker & operator=(SignalBlocker &&other) noexcept
SignalBlocker(const SignalSet &signals)
Blocks the signals in the given set.
~SignalBlocker()
Restores the previous signal mask.
Exception thrown when a signal operation fails.
int signal_number() const noexcept
Returns the signal number involved in the error.
SignalError(const std::string &msg, int signo=-1, int err=0)
Constructs a SignalError.
int error_code() const noexcept
Returns the errno value at the time of failure.
C++ wrapper for sigset_t with a fluent interface.
SignalSet & clear() noexcept
Clears the set (removes all signals).
static SignalSet full() noexcept
Creates a full signal set (all signals).
static SignalSet empty() noexcept
Creates an empty signal set.
SignalSet() noexcept
Creates an empty signal set.
sigset_t * get() noexcept
Returns a pointer to the underlying sigset_t.
const sigset_t * get() const noexcept
Returns a const pointer to the underlying sigset_t.
SignalSet(std::initializer_list< int > signals) noexcept
Creates a signal set from an initializer list.
bool contains(int signo) const noexcept
Checks if a signal is in the set.
SignalSet & fill() noexcept
Fills the set with all signals.
SignalSet & add(int signo) noexcept
Adds a signal to the set.
SignalSet & remove(int signo) noexcept
Removes a signal from the set.
RAII wrapper for POSIX signal handler registration.
struct sigaction old_action_
Signal(NoAbortTag, int signo, Handler func, bool restart_calls, int &error_out)
int signal_number() const noexcept
Returns the signal number being handled.
bool restarts_calls() const noexcept
Returns whether SA_RESTART is enabled for this handler.
Signal(Signal &&other) noexcept
Move constructor.
Signal & operator=(Signal &&other) noexcept
Move assignment operator.
Signal & operator=(const Signal &)=delete
void release() noexcept
Releases ownership of the signal handler.
Signal(int signo, Handler func, bool restart_calls=true)
Installs a signal handler.
Signal(const Signal &)=delete
const struct sigaction & previous_action() const noexcept
Returns the previous sigaction structure.
static Signal try_create(int signo, Handler func, bool restart_calls, int &error_out)
Attempts to create a Signal object.
bool is_active() const noexcept
Returns whether this object will restore the handler on destruction.
void(*)(int) Handler
Type alias for signal handler function.
~Signal()
Restores the previous signal handler.
static Signal create(int signo, Handler func, bool restart_calls=true)
Creates a Signal object or throws on failure.
Handler previous_handler() const noexcept
Returns the previous handler that will be restored.