Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

350 Signals Chapter 10


#include <signal.h>

int sigaction(intsigno,const struct sigaction *restrict act,
struct sigaction *restrict oact);

Returns: 0 if OK,−1 on error

The argumentsignois the signal number whose action we areexamining or modifying.
If theactpointer is non-null, we aremodifying the action. If theoactpointer is non-null,
the system returns the previous action for the signal through theoactpointer.This
function uses the following structure:

struct sigaction {
void (*sa_handler)(int); /* addr of signal handler, */
/* or SIG_IGN, or SIG_DFL */
sigset_t sa_mask; /* additional signals to block */
int sa_flags; /* signal options, Figure 10.16 */

/* alternate handler */
void (*sa_sigaction)(int, siginfo_t *, void *);
};

When changing the action for a signal, if thesa_handler field contains the
address of a signal-catching function (as opposed to either of the constantsSIG_IGNor
SIG_DFL), then thesa_maskfield specifies a set of signals that areadded to the signal
mask of the process beforethe signal-catching function is called. If and when the
signal-catching function returns, the signal mask of the process is reset to its previous
value. This way, we are able to block certain signals whenever a signal handler is
invoked. The operating system includes the signal being delivered in the signal mask
when the handler is invoked. Hence, we areguaranteed that whenever we are
processing a given signal, another occurrence of that same signal is blocked until we’re
finished processing the first occurrence. Recall from Section 10.8 that additional
occurrences of the same signal areusually not queued. If the signal occurs five times
while it is blocked, when we unblock the signal, the signal-handling function for that
signal will usually be invoked only one time. (This characteristic was illustrated in the
previous example.)
Once we install an action for a given signal, that action remains installed until we
explicitly change it by callingsigaction.Unlike earlier systems with their unreliable
signals, POSIX.1 requires that a signal handler remain installed until explicitly changed.
Thesa_flagsfield of theactstructurespecifies various options for the handling of
this signal. Figure10.16 details the meaning of these options when set. The SUS
column contains • if the flag is defined as part of the base POSIX.1 specification, and
XSIif it is defined as part of the XSI option.
The sa_sigaction field is an alternative signal handler used when the
SA_SIGINFOflag is used withsigaction.Implementations might use the same
storage for both thesa_sigactionfield and thesa_handlerfield, so applications
can use only one of these fields at a time.
Free download pdf