Saturday, October 22, 2011

sigaction in linux

sigaction Function / sigaction structure

The sigaction function allows us to examine or modify (or both) the action associated with a particular signal. This function supersedes the signal function from earlier releases of the UNIX System. Indeed, at the end of this section, we show an implementation of signal using sigaction.

#include <signal.h>

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

Returns: 0 if OK, 1 on error

The argument signo is the signal number whose action we are examining or modifying. If the act pointer is non-null, we are modifying the action. If the oact pointer is non-null, the system returns the previous action for the signal through the oact pointer. This function uses the following structure:

sigaction structure:

struct sigaction {
   * addr of signal handler, 
   void (*sa_handler)(int);
   * 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 the sa_handler field contains the address of a signal catching function (as opposed to the constants SIG_IGN or SIG_DFL), then the sa_mask field specifies a set of signals that are added to the signal mask of the process before the 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 are guaranteed 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 are usually 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.

Once we install an action for a given signal, that action remains installed until we explicitly change it by calling sigaction. Unlike earlier systems with their unreliable signals, POSIX.1 requires that a signal handler remain installed until explicitly changed.

The sa_flags field of the act structure specifies various options for the handling of this signal. Figure 10.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 XSI if it is defined as an XSI extension to the base.

The sa_sigaction field is an alternate signal handler used when the SA_SIGINFO flag is used withsigaction. Implementations might use the same storage for both the sa_sigaction field and the sa_handler field, so applications can use only one of these fields at a time.

Normally, the signal handler is called as 

void handler(int signo); 

but if the SA_SIGINFO flag is set, the signal handler is called as

void handler(int signo, siginfo_t *info, void *context);

No comments:

Post a Comment