5.13. SignalsA signal is an indication that an event has occurred. Typically, a process that receives a signal handles that signal through either a default mechanism or a signal handler. POSIX defines a set of signals and signal-handling functions that are implemented in Linux. This section compares and contrasts signal support between Linux 2.6 and AIX. 5.13.1. Signal ActionsEach signal has an associated default action associated with it. A default action is what the Linux kernel does for the process when a signal arrives for that process. The default actions are as follows:
A process can also install a signal handler to run when a signal is delivered to it. The signal handler takes the place of the default action associated with each signal. Signals occur as a consequence of an event. Here are some examples that can cause a signal to be sent to a process:
5.13.2. Simple SignalsThe simplest signal-handling function Linux supports is the original ISO C standard signal() API. It allows the calling process to change the action for a particular signal, to either ignore or restore the default signal action, or install a function that is called when that particular signal is received by the process. This section defines the signal() APIs available in GNU/Linux and the differences compared to AIX. 5.13.2.1. AIX Prototype#include <signal.h> int sigaction (int Signal, struct sigaction *Action, struct sigaction *OAction); int sigprocmask (int How, const sigset_t *Set, sigset *OSet); int sigpending (sigset_t *Set); int sigsuspend (const sigset_t *SignalMask); int sigpause (int SignalMask); 5.13.2.2. Linux Prototype#include <signal.h> int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); int sigpending(sigset_t *set); int sigsuspend(const sigset_t *mask); int sigpause(int sigmask); 5.13.2.3. Detail Comparison**Not Compatible** Signal values are different between AIX and Linux. The remaining documentation is taken from the Linux man page. The sigaction system call is used to change the action taken by a process on receipt of a specific signal. signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP. If act is non-null, the new action for signal signum is installed from act. If oldact is non-null, the previous action is saved in oldact. The sigaction structure is defined as something like this: struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); } On some architectures, a union is involved; do not assign to both sa_handler and sa_sigaction. The sa_restorer element is obsolete and should not be used. POSIX does not specify an sa_restorer element. sa_handler specifies the action to be associated with signum and may be SIG_DFL for the default action, SIG_IGN to ignore this signal, or a pointer to a signal-handling function. sa_mask gives a mask of signals that should be blocked during execution of the signal handler. In addition, the signal that triggered the handler will be blocked, unless the SA_NODEFER or SA_NOMASK flags are used. sa_flags specifies a set of flags that modify the behavior of the signal-handling process. It is formed by the bitwise OR of zero or more of the following:
The siginfo_t parameter to sa_sigaction is a struct with the following elements: siginfo_t { int si_signo; /* Signal number */ int si_errno; /* An errno value */ int si_code; /* Signal code */ pid_t si_pid; /* Sending process ID */ uid_t si_uid; /* Real user ID of sending process */ int si_status; /* Exit value or signal */ clock_t si_utime; /* User time consumed */ clock_t si_stime; /* System time consumed */ sigval_t si_value; /* Signal value */ int si_int; /* POSIX.1b signal */ void * si_ptr; /* POSIX.1b signal */ void * si_addr; /* Memory location which caused fault */ int si_band; /* Band event */ int si_fd; /* File descriptor */ } si_signo, si_errno, and si_code are defined for all signals. The rest of the struct may be a union, so one should only read the fields that are meaningful for the given signal. Kill, POSIX.1b signals, and SIGCHLD fill in si_pid and si_uid. SIGCHLD also fills in si_status, si_utime, and si_stime. si_int and si_ptr are specified by the sender of the POSIX.1b signal. SIGILL, SIGFPE, SIGSEGV, and SIGBUS fill in si_addr with the address of the fault. SIGPOLL fills in si_band and si_fd. si_code indicates why this signal was sent. It is a value, not a bitmask. Table 5-8 lists the values possible for any signal.
The sigprocmask call is used to change the list of currently blocked signals. The behavior of the call is dependent on the value of how, as follows:
If oldset is non-null, the previous value of the signal mask is stored in oldset. The sigpending call allows the examination of pending signals (ones that have been raised while blocked). The signal mask of pending signals is stored in set. The sigsuspend call temporarily replaces the signal mask for the process with that given by mask and then suspends the process until a signal is received. 5.13.2.4. Return ValueThe functions sigaction, sigprocmask, and sigpending return 0 on success and 1 on error. The function sigsuspend always returns 1, normally with the error EINTR. 5.13.2.5. Errors
5.13.2.6. Signal ValuesSignal values are defined differently between Linux and AIX. Linux signals are essentially a fully contained subset of AIX signals. The only exception is SIGSTKFLT. No code was found in the Linux kernel to implement the SIGSTKFLT signal. Table 5-9 shows the differences in signal values between AIX and Linux.
Implementation of correct and reliable signals has been in place for many years now, where an installed signal handler remains persistent and is not reset by the kernel. The POSIX standards provided a fairly well-defined set of interfaces for using signals in code, and today the Linux implementation of signals is fully POSIX-compliant. |