4.10. SigAction Class

The C++ class SigAction implements a C++ wrapper around struct sigaction. SigAction class also provides a range of all possible operation that can be performed on it, including sigaction(2) system call.


typedef struct sigaction SIGACTION;

typedef void (*C_SIG_HANDLER)( int );

class SigAction

    SigAction (C_SIG_HANDLER handler_, SigSet* sig_mask_ = 0, int flags_ = 0);
    SigAction (C_SIG_HANDLER handler_, int signum_, SigSet* sig_mask_ = 0,
               int flags_ = 0);
    int register_action (int signum_, SigAction* oaction_ = 0);
    int restore_action (int signum_, SigAction& oaction_);
    int retrieve_action (int signum_);
    void action (SIGACTION * sa_);
    SIGACTION * action (); 

    void flags (int new_flags_);
    int flags ();

    void mask (SigSet & mask_set_);
    SigSet mask ();

    void handler (C_SIG_HANDLER sha_);
    C_SIG_HANDLER handler ();

    operator SIGACTION *();

The structure struct sigaction is defined as:

struct sigaction {
    void      (*sa_handler) ();
    sigset_t  sa_mask;
    int       sa_flags;
    void      (*sa_sigaction) (int, siginfo_t*, void*);

The structure is used to set all the details of what your process should do when a signal arrives. It encapsulates an action to be taken upon delivery of a signal.

The most important member of the structure is sa_handler, which is a pointer to a signal handling function. This function is called whenever a process receives the signal.

Some of the member functions of SigAction class take a parameter of the type C_SIG_HANDLER as an argument. The following are the acceptable values:

The sa_mask mask of the signal action specifies a set of signals to be blocked from deliverty to the process while the signal handler is active. Prior an entry into the signal handler function, this set of signals is added to the set of signals already blocked from delivery to the process. In addition, a signal that caused the handler to be executed is also be blocked (SIGSTOP and SIGKILL cannot be blocked - this is enforced by the underlying operating system).

The sa_flags is the set of flags ORed together that allows to modify the delivery of the signal. POSIX.1 specification defines only SA_NOCLDSTOP flag. All other flags are system-dependent. Consult your local sigaction(2) man page for details.

Because SigAction is a wrapper around sigaction(2), when sig_handler returns (and before anything else), the operating system restores the current disposition for the signal. To reset the signal's disposition to SIG_DFL, SUN's Solaris uses flag SA_RESETHAND, and Linux uses SA_ONESHOT.