Previous | Contents | Index |
Specifies the action to take upon delivery of a signal.
#include <signal.h>int sigaction (int sig, const struct sigaction *action, struct sigaction *o_action);
sig
The signal for which the action is to be taken.action
A pointer to a sigaction structure that describes the action to take when you receive the signal specified by the sig argument.o_action
A pointer to a sigaction structure. When the sigaction function returns from a call, the action previously attached to the specified signal is stored in this structure.
When a process requests the sigaction function, the process can both examine and specify what action to perform when the specified signal is delivered. The arguments determine the behavior of the sigaction function as follows:
- Specifying the sig argument identifies the affected signal. Use any one of the signal values defined in the <signal.h> header file, except SIGKILL.
If sig is SIGCHLD and the SA_NOCLDSTOP flag is not set in sa_flags , then a SIGCHLD signal is generated for the calling process whenever any of its child processes stop. If sig is SIGCHLD and the SA_NOCLDSTOP flag is set in sa_flags , then SIGCHLD signal is not generated in this way.- Specifying the action argument, if not null, points to a sigaction structure that defines what action to perform when the signal is received. If the action argument is null, signal handling remains unchanged, so you can use the call to inquire about the current handling of the signal.
- Specifying the o_action argument, if not null, points to a sigaction structure that contains the action previously attached to the specified signal.
The sigaction structure consists of the following members:
void (*sa_handler)(int); sigset_t sa_mask; int sa_flags;The sigaction structure members are defined as follows:
sa_handler This member can contain the following values:
- SIG_DFL -- Specifies the default action taken when the signal is delivered.
- SIG_IGN -- Specifies that the signal has no effect on the receiving process.
- Function pointer -- Requests to catch the signal. The signal causes the function call.
sa_mask This member can request that individual signals, in addition to those in the process signal mask, are blocked from delivery while the signal handler function specified by the sa_handler member is executing. sa_flags This member can set the flags to enable further control over the actions taken when a signal is delivered. The sa_flags member of the sigaction structure has the following values:
SA_ONSTACK Setting this bit causes the system to run the signal catching function on the signal stack specified by the sigstack function. If this bit is not set, the function runs on the stack of the process where the signal is delivered. SA_RESETHAND Setting this bit resets the signal to SIG_DFL. Be aware that you cannot automatically reset SIGILL and SIGTRAP. SA_NODEFER Setting this bit does not automatically block the signal as it is caught. SA_NOCLDSTOP If this bit is set and the sig argument is equal to SIGCHLD and a child process of the calling process stops, then a SIGCHLD signal is sent to the calling process only if SA_NOCLDSTOP is not set for SIGCHLD. When a signal is caught by a signal-catching function installed by sigaction , a new signal mask is calculated and installed for the duration of the signal-catching function (or until a call to either sigprocmask or sigsuspend is made. This mask is formed by taking the union of the current signal mask and the value of the sa_mask for the signal being delivered unless SA_NODEFER or SA_RESETHAND is set, and then including the signal being delivered. If and when the user's signal handler returns normally, the original signal mask is restored.
Once an action is installed for a specific signal, it remains installed until another action is explicitly requested (by another call to sigaction ), until the SA_RESETHAND flag causes resetting of the handler, or until one of the exec functions is called.
If the previous action for a specified signal had been established by signal , the values of the fields returned in the structure pointed to by the o_action argument of sigaction are unspecified, and in particular o_action-> sa_handler is not necessarily the same value passed to signal . However, if a pointer to the same structure or a copy thereof is passed to a subsequent call to sigaction by means of the action argument of sigaction ), the signal is handled as if the original call to signal were repeated.
If sigaction fails, no new signal handler is installed.
It is unspecified whether an attempt to set the action for a signal that cannot be caught or ignored to SIG_DFL is ignored or causes an error to be returned with errno set to EINVAL.
See Section 4.2 for more information on signal handling.
Note
The sigvec and signal functions are provided for compatibility to old UNIX systems; their function is a subset of that available with the sigaction function.See also sigstack , sigvec , signal , wait , read , and write .
0 Indicates success. - 1 Indicates an error; A new signal handler is not installed. errno is set to one of the following values:
- EFAULT -- The action or o_action argument points to a location outside of the allocated address space of the process.
- EINVAL -- The sig argument is not a valid signal number. Or an attempt was made to ignore or supply a handler for the SIGKILL, SIGSTOP, and SIGCONT signals.
Adds the specified individual signal.
#include <signal.h>int sigaddset (sigset_t *set, int sig_number);
set
The signal set.sig_number
The individual signal.
The sigaddset function manipulates sets of signals. This function operates on data objects that you can address by the application, not on any set of signals known to the system. For example, this function does not operate on the set blocked from delivery to a process or the set pending for a process.The sigaddset function adds the individual signal specified by sig_number from the signal set specified by set.
The following example shows how to generate and use a signal mask that blocks only the SIGINT signal from delivery:
#include <signal.h> int return_value; sigset_t newset; . . . sigemptyset(&newset); sigaddset(&newset, SIGINT); return_value = sigprocmask (SIG_SETMASK, &newset, NULL);
0 Indicates success. - 1 Indicates an error; errno is set to the following value:
- EINVAL -- The value of sig_number is not a valid signal number.
Adds the signals in mask to the current set of signals being blocked from delivery.
#include <signal.h>int sigblock (int mask);
mask
The signals to be blocked.
Signal i is blocked if the i - 1 bit in mask is a 1. For example, to add the protection-violation signal to the set of blocked signals, use the following line:
sigblock(1 << (SIGBUS - 1));You can express signals in mnemonics (such as SIGBUS for a protection violation) or numbers as defined in the <signal.h> header file, and you can express combinations of signals by using the bitwise OR operator (|).
x Indicates the previous set of masked signals.
Deletes a specified individual signal.
#include <signal.h>int sigdelset (sigset_t *set, int sig_number;)
set
The signal set.sig_number
The individual signal.
The sigdelset function deletes the individual signal specified by sig_number from the signal set specified by set.
This function operates on data objects that you can address by the application, not on any set of signals known to the system. For example, this function does not operate on the set blocked from delivery to a process or the set pending for a process.
0 Indicates success. - 1 Indicates an error; errno is set to the following value:
- EINVAL -- The value of sig_number is not a valid signal number.
Initializes the signal set to exclude all signals.
#include <signal.h>int sigemptyset (sigset_t *set);
set
The signal set.
The sigemptyset function initializes the signal set pointed to by set such that you exclude all signals. A call to sigemptyset or sigfillset must be made at least once for each object of type sigset_t prior to any other use of that object.This function operates on data objects that you can address by the application, not on any set of signals known to the system. For example, this function does not operate on the set blocked from delivery to a process or the set pending for a process.
See also sigfillset .
The following example shows how to generate and use a signal mask that blocks only the SIGINT signal from delivery:
#include <signal.h> int return_value; sigset_t newset; . . . sigemptyset(&newset); sigaddset(&newset, SIGINT); return_value = sigprocmask (SIG_SETMASK, &newset, NULL);
0 Indicates success. - 1 Indicates an error; the global errno is set to indicate the error.
Initializes the signal set to include all signals.
#include <signal.h>int sigfillset (sigset_t *set);
set
The signal set.
The sigfillset function initializes the signal set pointed to by set such that you include all signals. A call to sigemptyset or sigfillset must be made at least once for each object of type sigset_t prior to any other use of that object.This function operates on data objects that you can address by the application, not on any set of signals known to the system. For example, this function does not operate on the set blocked from delivery to a process or the set pending for a process.
See also sigemptyset .
0 Indicates success. - 1 Indicates an error; errno is set to the following value:
- EINVAL -- The value of the sig_number argument is not a valid signal number.
Adds the specified signal to the calling process's signal mask.
#include <signal.h>int sighold (int signal);
signal
The specified signal. The signal argument can be assigned any of the signals defined in the <signal.h> header file, except SIGKILL and SIGSTOP.
The sighold , sigrelse , and sigignore functions provide simplified signal management:
- The sighold function adds signal to the calling process's signal mask.
- The sigrelse function removes signal from the calling process's signal mask.
- The sigignore function sets the disposition of signal to SIG_IGN.
The sighold function, in conjunction with sigrelse and sigpause , can be used to establish critical regions of code that require the delivery of a signal to be temporarily deferred.
Upon success, the sighold function returns a value of 0. Otherwise, a value of - 1 is returned, and errno is set to indicate the error.
Note
These interfaces are provided for compatibility only. New programs should use sigaction and sigprocmask to control the disposition of signals.
0 Indicates success. - 1 Indicates an error; errno is set to the following value:
- EINVAL -- The value of the signal argument is either an invalid signal number or SIGKILL.
Sets the disposition of the specified signal to SIG_IGN.
#include <signal.h>int sigignore (int signal);
signal
The specified signal. The signal argument can be assigned any of the signals defined in the <signal.h> header file, except SIGKILL and SIGSTOP.
The sighold , sigrelse , and sigignore functions provide simplified signal management:
- The sighold function adds signal to the calling process's signal mask.
- The sigrelse function removes signal from the calling process's signal mask.
- The sigignore function sets the disposition of signal to SIG_IGN.
The sighold function, in conjunction with sigrelse and sigpause , can be used to establish critical regions of code that require the delivery of a signal to be temporarily deferred.
Upon success, the sigignore function returns a value of 0. Otherwise, a value of - 1 is returned, and errno is set to indicate the error.
Note
These interfaces are provided for compatibility only. New programs should use sigaction and sigprocmask to control the disposition of signals.
0 Indicates success. - 1 Indicates an error; errno is set to the following value:
- EINVAL -- The value of the signal argument is either an invalid signal number or SIGKILL, or an attempt is made to catch a signal that cannot be caught or to ignore a signal that cannot be ignored.
Tests whether a specified signal is a member of the signal set.
#include <signal.h>int sigismember (const sigset_t *set, int sig_number);
set
The signal set.sig_number
The individual signal.
The sigismember function tests whether sig_number is a member of the signal set pointed to by set.This function operates on data objects that you can address by the application, not on any set of signals known to the system. For example, this function does not operate on the set blocked from delivery to a process or the set pending for a process.
1 Indicates success. The specified signal is a member of the specified set. 0 Indicates an error. The specified signal is not a member of the specified set.
Nonlocal goto with signal handling.
#include <setjmp.h>void siglongjmp (sigjmp_buf env, int value);
env
An address for a sigjmp_buf structure.value
A nonzero value.
The siglongjmp function restores the environment saved by the most recent call to sigsetjmp in the same process with the corresponding sigjmp_buf argument.All accessible objects have values when siglongjmp is called, with one exception: values of objects of automatic storage duration that changed between the sigsetjmp call and siglongjmp call are indeterminate.
Because it bypasses the usual function call and return mechanisms, siglongjmp executes correctly during interrupts, signals, and any of their associated functions. However, if you invoke siglongjmp from a nested signal handler (for example, from a function invoked as a result of a signal raised during the handling of another signal), the behavior is undefined.
The siglongjmp function restores the saved signal mask only if you initialize the env argument by a call to sigsetjmp with a nonzero savemask argument.
After siglongjmp is completed, program execution continues as if the corresponding call of sigsetjmp just returned the value specified by value. The siglongjmp function cannot cause sigsetjmp to return 0 (zero); if value is 0, sigsetjmp returns 1
See also sigsetjmp .
Previous | Next | Contents | Index |