|HP-UX Reference > S
HP-UX 11i Version 3: February 2007
sigaction — examine and change signal action
#include <signal.h> int sigaction ( int sig, const struct sigaction *act, struct sigaction *oact );
The sigaction() function allows the calling process to examine and/or specify the action to be associated with a specific signal. The argument sig specifies the signal; acceptable values are defined in <signal.h>.
The structure sigaction, used to describe an action to be taken, is defined in the header <signal.h> to include at least the following members:
If the argument act is not a null pointer, it points to a structure specifying the action to be associated with the specified signal. If the argument oact is not a null pointer, the action previously associated with the signal is stored in the location pointed to by the argument oact.
If the argument act is a null pointer, signal handling is unchanged; thus, the call can be used to enquire about the current handling of a given signal. The sa_handler field of the sigaction structure identifies the action to be associated with the specified signal. If the sa_handler field specifies a signal-catching function, the sa_mask field identifies a set of signals that will be added to the thread's signal mask before the signal-catching function is invoked. The SIGKILL and SIGSTOP signals will not be added to the signal mask using this mechanism; this restriction will be enforced by the system without causing an error to be indicated.
The sa_flags field can be used to modify the behavior of the specified signal. The following flags, defined in the header <signal.h>, can be set in sa_flags:
If sig is SIGCHLD and the SA_NOCLDSTOP flag is not set in sa_flags, and the implementation supports the SIGCHLD signal, then a SIGCHLD signal will be 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 the implementation will not generate a SIGCHLD signal in this way.
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 sig had been established by signal(), the values of the fields returned in the structure pointed to by oact are unspecified, and in particular oact->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() via the act argument, handling of the signal will be 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.
A signal is said to be generated for (or sent to) a process when the event that causes the signal first occurs. Examples of such events include detection of hardware faults, timer expiration and terminal activity, as well as the invocation of kill() and sigqueue(). In some circumstances, the same event generates signals for multiple processes.
Each process has an action to be taken in response to each signal defined by the system (see Signal Actions). A signal is said to be delivered to a process when the appropriate action for the process and signal is taken.
During the time between the generation of a signal and its delivery, the signal is said to be pending. Ordinarily, this interval cannot be detected by an application.
However, a signal can be blocked from delivery to a thread. If the action associated with a blocked signal is anything other than to ignore the signal, and if that signal is generated for the thread, the signal will remain pending until either it is unblocked or the action associated with it is set to ignore the signal.
If the action associated with a blocked signal is to ignore the signal and if that signal is generated for the process, it is unspecified whether the signal is discarded immediately upon generation or remains pending.
Each thread has a signal mask that defines the set of signals currently blocked from delivery to it. The signal mask for a thread is initialized from that of its parent. The sigaction(), sigprocmask(), and sigsuspend() functions control the manipulation of the signal mask.
The determination of which action is taken in response to a signal is made at the time the signal is delivered, allowing for any changes since the time of generation. This determination is independent of the means by which the signal was originally generated. If a subsequent occurrence of a pending signal is generated, it is implementation-dependent as to whether the signal is delivered more than once. The order in which multiple, simultaneously pending signals are delivered to a process is unspecified.
When any stop signal (SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU) is generated for a process, any pending SIGCONT signals for that process will be discarded. Conversely, when SIGCONT is generated for a process, all pending stop signals for that process will be discarded. When SIGCONT is generated for a process that is stopped, the process will be continued, even if the SIGCONT signal is blocked or ignored. If SIGCONT is blocked and not ignored, it will remain pending until it is either unblocked or a stop signal is generated for the process.
Some signal-generating functions, such as high-resolution timer expiration, asynchronous I/O completion, interprocess message arrival, and the sigqueue() function, support the specification of an application-defined value, either explicitly as a parameter to the function or in a sigevent structure parameter (see signal(5)).
Realtime Signals Extension
When a signal is generated by sigqueue() or any signal-generating function that supports the specification of an application-defined value, and if the SA_SIGINFO flag is set for that signal, the signal will be queued to the process along with the application-specified signal value.
Multiple occurrences of signals so generated are queued in FIFO order. When multiple unblocked signals, all in the range SIGRTMIN to SIGRTMAX, are pending, the implementation delivers the pending unblocked signal with the lowest signal number within that range.
The selection order between realtime and nonrealtime signals, or between multiple pending nonrealtime signals, is unspecified.
Signals generated by kill() or other events that cause signals to occur, such as detection of hardware faults, alarm() timer expiration, or terminal activity, and for which the implementation does not support queueing, will have no effect on signals already queued for the same signal number.
If, when a pending signal is delivered, there are additional signals to be queued to that signal number, the signal will remain pending. Otherwise, the pending indication will be reset.
An implementation will document any condition not specified by this document under which the implementation generates signals.
There are three types of action that can be associated with a signal: SIG_DFL, SIG_IGN or a pointer to a function. Initially, all signals will be set to SIG_DFL or SIG_IGN prior to entry of the main() routine (see the exec functions). The actions prescribed by these values are as follows:
SIG_DFL - signal-specific default action
SIG_IGN - ignore signal
Pointer to a function - catch signal
Signal Effects on Other Functions
Signals affect the behavior of certain functions (defined under the Async Signal Safe section of thread_safety(5)) if delivered to a process while it is executing such a function. If the action of the signal is to terminate the process, the process will be terminated and the function will not return.
If the action of the signal is to stop the process, the process will stop until continued or terminated. Generation of a SIGCONT signal for the process causes the process to be continued, and the original function will continue at the point the process was stopped.
If the action of the signal is to invoke a signal-catching function, the signal-catching function will be invoked; in this case the original function is said to be interrupted by the signal. If the signal-catching function executes a return statement, the behavior of the interrupted function will be as described individually for that function. Signals that are ignored will not affect the behavior of any function; signals that are blocked will not affect the behavior of any function until they are unblocked and then delivered.
The sigaction() function supersedes the signal() interface, and should be used in preference. In particular, sigaction() and signal() should not be used in the same process to control the same signal.
The behavior of reentrant functions, as defined in the description, is as specified by this document, regardless of invocation from a signal-catching function. This is the only intended meaning of the statement that reentrant functions may be used in signal-catching functions without restrictions. Applications must still consider all effects of such functions on such things as data structures, files and process state. In particular, application writers need to consider the restrictions on interactions when interrupting sleep() and interactions among multiple handles for a file descriptor.
The fact that any specific function is listed as reentrant does not necessarily mean that invocation of that function from a signal-catching function is recommended.
In order to prevent errors arising from interrupting non-reentrant function calls, applications should protect calls to these functions either by blocking the appropriate signals or through the use of some programmatic semaphore.
This document does not address the more general problem of synchronizing access to shared data structures. Note in particular that even the "safe" functions may modify the global variable errno; the signal-catching function may want to save and restore its value. Naturally, the same principles apply to the reentrancy of application routines and asynchronous data access.
Note that longjmp() and siglongjmp() are not in the list of reentrant functions. This is because the code executing after longjmp() and siglongjmp() can call any unsafe functions with the same danger as calling those unsafe functions directly from the signal handler. Applications that use longjmp() and siglongjmp() from within signal handlers require rigorous protection in order to be portable.
Many of the other functions that are excluded from the list are traditionally implemented using either malloc() or free() functions or the standard I/O library, both of which traditionally use data structures in a non-reentrant manner.
Because any combination of different functions using a common data structure can cause reentrancy problems, this document does not define the behavior when any unsafe function is called in a signal handler that interrupts an unsafe function.
If the signal occurs other than as the result of calling abort(), kill(), sigqueue(), or raise(), the behavior is undefined if the signal handler calls any function in the standard library other than one of the functions listed in the table above or refers to any object with static storage duration other than by assigning a value to a static storage duration variable of type volatile sig_atomic_t. Furthermore, if such a call fails, the value of errno is indeterminate.
Usually, the signal is executed on the stack that was in effect before the signal was delivered. An alternate stack may be specified to receive a subset of the signals being caught.
When the signal handler returns, the receiving process will resume execution at the point it was interrupted unless the signal handler makes other arrangements. If longjmp() or _longjmp() is used to leave the signal handler, then the signal mask must be explicitly restored by the process.
POSIX.4-1993 defines the third argument of a signal handling function when SA_SIGINFO is set as a void * instead of a ucontext_t *, but without requiring type checking. New applications should explicitly cast the third argument of the signal handling function to uncontext_t *.
The BSD optional four argument signal handling function is not supported by this specification. The BSD declaration would be
void handler(int sig, int code, struct sigcontext *scp, char *addr);
where sig is the signal number, code is additional information on certain signals, scp is a pointer to the sigcontext structure, and addr is additional address information. Much the same information is available in the objects pointed to by the second argument of the signal handler specified when SA_SIGINFO is set.
The signal disposition, catch/ignore/default, established by sigaction() is shared by all threads in the process.
If the signal disposition for sig is set to SIG_IGN or is set to SIG_DFL and the default action for sig is to ignore the signal, any instances of sig pending on the process or any of the threads will be discarded. The signals are discarded regardless of whether the signal is blocked by any of the threads.
For more information regarding signals and threads, see signal(5).
Upon successful completion, sigaction() returns 0. Otherwise -1 is returned, errno is set to indicate the error and no new signal-catching function will be installed.
The sigaction() function will fail if:
The sigaction() function may fail if:
The fpathconf() function is marked as an extension in the list of safe functions because it is not included in the corresponding list in the ISO POSIX-1 standard, but it is expected to be added in a future revision of that standard.