Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-signal.H File Reference

POSIX signal handling utilities with RAII semantics. More...

#include <csignal>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
#include <initializer_list>
#include <aleph.H>
Include dependency graph for ah-signal.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  SignalError
 Exception thrown when a signal operation fails. More...
 
class  SignalSet
 C++ wrapper for sigset_t with a fluent interface. More...
 
class  SignalBlocker
 RAII wrapper for temporarily blocking signals. More...
 
class  Signal
 RAII wrapper for POSIX signal handler registration. More...
 
struct  Signal::NoAbortTag
 

Functions

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_name (int signo)
 Returns the name of a signal.
 
std::string signal_description (int signo)
 Returns a human-readable description of a signal.
 

Detailed Description

POSIX signal handling utilities with RAII semantics.

This header provides C++ wrappers for POSIX signal handling:

  • Signal: RAII wrapper for signal handler registration. Automatically restores the previous handler when the object is destroyed.
  • SignalSet: C++ wrapper for sigset_t with a fluent interface.
  • SignalBlocker: RAII utility to temporarily block signals.

Thread Safety

Signal handlers are process-wide (not per-thread). While the classes in this header are safe to use, be aware that:

  • Signal handlers affect the entire process
  • Signal masks can be set per-thread using pthread_sigmask
  • SignalBlocker uses sigprocmask (process-wide) by default

Example Usage

#include <ah-signal.H>
volatile sig_atomic_t got_signal = 0;
void handler(int signo) {
got_signal = signo;
}
int main() {
// Install handler - previous handler is saved
Signal sig(SIGINT, handler);
// ... do work ...
// When 'sig' goes out of scope, previous handler is restored
}
POSIX signal handling utilities with RAII semantics.
int main()
RAII wrapper for POSIX signal handler registration.
Definition ah-signal.H:354
Author
Leandro Rabindranath León

Definition in file ah-signal.H.

Function Documentation

◆ send_signal_to_self()

bool send_signal_to_self ( int  signo)
inlinenoexcept

Sends a signal to the current process.

Convenience wrapper for raise().

Parameters
signoThe signal to send.
Returns
true if successful, false otherwise.

Definition at line 652 of file ah-signal.H.

Referenced by TEST().

◆ signal_description()

std::string signal_description ( int  signo)
inline

Returns a human-readable description of a signal.

Uses strsignal() if available, otherwise returns the signal name.

Parameters
signoThe signal number.
Returns
A descriptive string for the signal.

Definition at line 730 of file ah-signal.H.

References signal_name().

◆ signal_name()

std::string signal_name ( int  signo)
inline

Returns the name of a signal.

Returns the standard POSIX signal name (e.g., "SIGINT", "SIGTERM").

Parameters
signoThe signal number.
Returns
A string with the signal name or "Signal N" if unknown.

Definition at line 665 of file ah-signal.H.

Referenced by signal_description(), and TEST().

◆ wait_for_signal()

int wait_for_signal ( const SignalSet signals)
inline

Utility to wait for a signal.

Blocks until one of the specified signals is delivered.

Parameters
signalsThe set of signals to wait for.
Returns
The signal number that was delivered.
Exceptions
SignalErrorif sigwait fails.

Example

SignalSet wait_for({SIGINT, SIGTERM});
SignalBlocker blocker(wait_for); // Block first, then wait
int sig = wait_for_signal(wait_for);
std::cout << "Received signal " << sig << std::endl;
int wait_for_signal(const SignalSet &signals)
Utility to wait for a signal.
Definition ah-signal.H:635
RAII wrapper for temporarily blocking signals.
Definition ah-signal.H:233
C++ wrapper for sigset_t with a fluent interface.
Definition ah-signal.H:133

Definition at line 635 of file ah-signal.H.

References SignalSet::get().