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:
Member Type Member Name Description void(*) (int) sa_handler SIG_DFL, SIG_IGN or pointer to a function. sigset_t sa_mask Additional set of signals to be blocked during execution of signal-catching function. int sa_flags Special flags to affect behaviour of signal. void(*) (int, siginfo_t *, void *) sa_sigaction Signal-catching function.
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 process' 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.
If the SA_SIGINFO flag (see below) is cleared in the sa_flags field of the sigaction structure, the sa_handler field identifies the action to be associated with the specified signal. If the SA_SIGINFO flag is set in the sa_flags field, the sa_sigaction field specifies a signal-catching function. If the SA_SIGINFO bit is cleared and the sa_handler field specifies a signal-catching function, or if the SA_SIGINFO bit is set, the sa_mask field identifies a set of signals that will be added to the signal mask of the thread before the signal-catching function is invoked.
The sa_flags field can be used to modify the behaviour of the specified signal.
The following flags, defined in the header <signal.h>, can be set in sa_flags:
- Do not generate SIGCHLD when children stop.
- ot generate SIGCHLD when children stop.
- If set and an alternate signal stack has been declared with sigaltstack() or sigstack(), the signal will be delivered to the calling process on that stack. Otherwise, the signal will be delivered on the current stack.
- If set, the disposition of the signal will be reset to SIG_DFL and the SA_SIGINFO flag will be cleared on entry to the signal handler.
Otherwise, the disposition of the signal will not be modified on entry to the signal handler.
- SIGILL and SIGTRAP cannot be automatically reset when delivered; the system silently enforces this restriction.
In addition, if this flag is set, sigaction() behaves as if the SA_NODEFER flag were also set.
- This flag affects the behaviour of interruptible functions; that is, those specified to fail with errno set to [EINTR]. If set, and a function specified as interruptible is interrupted by this signal, the function will restart and will not fail with [EINTR] unless otherwise specified. If the flag is not set, interruptible functions interrupted by this signal will fail with errno set to [EINTR].
- If cleared and the signal is caught, the signal-catching function will be entered as:where signo is the only argument to the signal catching function. In this case the sa_handler member must be used to describe the signal catching function and the application must not modify the sa_sigaction member.
void func(int signo);
If SA_SIGINFO is set and the signal is caught, the signal-catching function will be entered as:where two additional arguments are passed to the signal catching function. The second argument will point to an object of type siginfo_t explaining the reason why the signal was generated; the third argument can be cast to a pointer to an object of type ucontext_t to refer to the receiving process' context that was interrupted when the signal was delivered. In this case the sa_sigaction member must be used to describe the signal catching function and the application must not modify the sa_handler member.
void func(int signo, siginfo_t *info, void *context);
The si_signo member contains the system-generated signal number.
The si_errno member may contain implementation-dependent additional error information; if non-zero, it contains an error number identifying the condition that caused the signal to be generated.
The si_code member contains a code identifying the cause of the signal. If the value of si_code is less than or equal to 0, then the signal was generated by a process and si_pid and si_uid respectively indicate the process ID and the real user ID of the sender. The <signal.h> header description contains information about the signal specific contents of the elements of the siginfo_t type.
- If set, and sig equals SIGCHLD, child processes of the calling processes will not be transformed into zombie processes when they terminate. If the calling process subsequently waits for its children, and the process has no unwaited for children that were transformed into zombie processes, it will block until all of its children terminate, and wait(), wait3(), waitid() and waitpid() will fail and set errno to [ECHILD]. Otherwise, terminating child processes will be transformed into zombie processes, unless SIGCHLD is set to SIG_IGN.
- If set and sig is caught, sig will not be added to the process' signal mask on entry to the signal handler unless it is included in sa_mask. Otherwise, sig will always be added to the process' signal mask on entry to the signal handler.
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].
If SA_SIGINFO is not set in sa_flags, then the disposition of subsequent occurrences of sig when it is already pending is implementation-dependent; the signal-catching function will be invoked with a single argument. If the implementation supports the Realtime Signals Extension option, and if SA_SIGINFO is set in sa_flags, then subsequent occurrences of sig generated by sigqueue() or as a result of any signal-generating function that supports the specification of an application-defined value (when sig is already pending) will be queued in FIFO order until delivered or accepted; the signal-catching function will be invoked with three arguments. The application specified value is passed to the signal-catching function as the si_value member of the siginfo_t structure.
Signal Generation and Delivery
A signal is said to be generated for (or sent to) a process or thread when the event that causes the signal first occurs. Examples of such events include detection of hardware faults, timer expiration, signals generated via the sigevent structure and terminal activity, as well as invocations of kill() and sigqueue() functions. In some circumstances, the same event generates signals for multiple processes.
At the time of generation, a determination is made whether the signal has been generated for the process or for a specific thread within the process. Signals which are generated by some action attributable to a particular thread, such as a hardware fault, are generated for the thread that caused the signal to be generated. Signals that are generated in association with a process ID or process group ID or an asynchronous event such as terminal activity are generated for the process.
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. A signal is said to be accepted by a process when the signal is selected and returned by one of the sigwait() functions.
During the time between the generation of a signal and its delivery or acceptance, 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 it is unblocked, it is accepted when it is selected and returned by a call to the sigwait() function, or the action associated with it is set to ignore the signal. Signals generated for the process will be delivered to exactly one of those threads within the process which is in a call to a sigwait() function selecting that signal or has not blocked delivery of the signal. If there are no threads in a call to a sigwait() function selecting that signal, and if all threads within the process block delivery of the signal, the signal will remain pending on the process until a thread calls a sigwait() function selecting that signal, a thread unblocks delivery of the signal, or the action associated with the signal 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 initialised from that of its parent or creating thread, or from the corresponding thread in the parent process if the thread was created as the result of a call to fork(). 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 or accepted more than once in circumstances other than those in which queueing is required under the Realtime Signals Extension option. The order in which multiple, simultaneously pending signals outside the range SIGRTMIN to SIGRTMAX are delivered to or accepted by 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.
An implementation will document any condition not specified by this document under which the implementation generates signals.
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. The sigevent structure is defined in <signal.h> and contains at least the following members:
Member Type Member Name Description int sigev_notify Notification type int sigev_signo Signal number union sigval sigev_value Signal value void(*)(unsigned sigval) sigev_notify_function Notification function (pthread_attr_t*) sigev_notify_attributes Notification attributes
The sigev_notify member specifies the notification mechanism to use when an asynchronous event occurs. This document defines the following values for the sigev_notify member:
- No asynchronous notification will be delivered when the event of interest occurs.
- The signal specified in sigev_signo will be generated for the process when the event of interest occurs. If the implementation supports the Realtime Signals Extension option and if the SA_SIGINFO flag is set for that signal number, then the signal will be queued to the process and the value specified in sigev_value will be the si_value component of the generated signal. If SA_SIGINFO is not set for that signal number, it is unspecified whether the signal is queued and what value, if any, is sent.
- A notification function will be called to perform notification.
An implementation may define additional notification mechanisms.
The sigev_signo member specifies the signal to be generated. The sigev_value member is the application-defined value to be passed to the signal-catching function at the time of the signal delivery or to be returned at signal acceptance as the si_value member of the siginfo_t structure.
The sigval union is defined in <signal.h> and contains at least the following members:
Member Type Member Name Description int sival_int Integer signal value void* sival_ptr Pointer signal value
The sival_int member is used when the application-defined value is of type int; the sival_ptr member is used when the application-defined value is a pointer.
If the Realtime Signals Extension option is supported:
- When a signal is generated by the sigqueue() function or any signal-generating function that supports the specification of an application-defined value, the signal will be marked pending 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. It is unspecified whether signals so generated are queued when the SA_SIGINFO flag is not set for that signal.
Signals generated by the kill() function 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 queuing, have no effect on signals already queued for the same signal number.
- When multiple unblocked signals, all in the range SIGRTMIN to SIGRTMAX, are pending, the behaviour will be as if the implementation delivers the pending unblocked signal with the lowest signal number within that range. No other ordering of signal delivery is specified.
If, when a pending signal is delivered, there are additional signals queued to that signal number, the signal remains pending. Otherwise, the pending indication is reset.
Multi-threaded programs can use an alternate event notification mechanism:
- When a notification is processed, and the sigev_notify member of the sigevent structure has the value SIGEV_THREAD, the function sigev_notify_function is called with parameter sigev_value.
The function will be executed in an environment as if it were the start_routine for a newly created thread with thread attributes specified by sigev_notify_attributes. If sigev_notify_attributes is NULL, the behaviour will as if the thread were created with the detachstate attribute set to PTHREAD_CREATE_DETACHED. Supplying an attributes structure with a detachstate attribute of PTHREAD_CREATE_JOINABLE results in undefined behaviour. The signal mask of this thread is implementation-dependent.
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
- The default actions for the signals defined in this specification are specified under <signal.h>. If the Realtime Signals Extension option is supported, the default actions for the realtime signals in the range SIGRTMIN to SIGRTMAX are to terminate the process abnormally.
- If the default action is to stop the process, the execution of that process is temporarily suspended. When a process stops, a SIGCHLD signal will be generated for its parent process, unless the parent process has set the SA_NOCLDSTOP flag. While a process is stopped, any additional signals that are sent to the process will not be delivered until the process is continued, except SIGKILL which always terminates the receiving process. A process that is a member of an orphaned process group will not be allowed to stop in response to the SIGTSTP, SIGTTIN or SIGTTOU signals. In cases where delivery of one of these signals would stop such a process, the signal will be discarded.
- Setting a signal action to SIG_DFL for a signal that is pending, and whose default action is to ignore the signal (for example, SIGCHLD), will cause the pending signal to be discarded, whether or not it is blocked. If the Realtime Signals Extension option is supported, any queued values pending will be discarded and the resources used to queue them will be released and made available to queue other signals.
- SIG_IGN - ignore signal
- Delivery of the signal will have no effect on the process. The behaviour of a process is undefined after it ignores a SIGFPE, SIGILL, SIGSEGV or SIGBUS signal that was not generated by kill(), sigqueue() or raise().
- The system will not allow the action for the signals SIGKILL or SIGSTOP to be set to SIG_IGN.
- Setting a signal action to SIG_IGN for a signal that is pending will cause the pending signal to be discarded, whether or not it is blocked.
- If a process sets the action for the SIGCHLD signal to SIG_IGN, the behaviour is unspecified, except as specified below. If the action for the SIGCHLD signal is set to SIG_IGN, child processes of the calling processes will not be transformed into zombie processes when they terminate. If the calling process subsequently waits for its children, and the process has no unwaited for children that were transformed into zombie processes, it will block until all of its children terminate, and wait(), wait3(), waitid() and waitpid() will fail and set errno to [ECHILD]. If the Realtime Signals Extension option is supported, any queued values pending will be discarded and the resources used to queue them will be released and made available to queue other signals.
- pointer to a function - catch signal
- On delivery of the signal, the receiving process is to execute the signal-catching function at the specified address. After returning from the signal-catching function, the receiving process will resume execution at the point at which it was interrupted. If the SA_SIGINFO flag for the signal is cleared, the signal-catching function will be entered as a C language function call as follows:If the SA_SIGINFO flag for the signal is set, the signal-catching function will be entered as a C language function call as follows:
void func(int signo);where func is the specified signal-catching function, signo is the signal number of the signal being delivered, and info is a pointer to a siginfo_t structure defined in <signal.h> containing at least the following member(s):
void func(int signo, siginfo_t *info, void *context);
The si_signo member contains the signal number. This is the same as the signo parameter. The si_code member contains a code identifying the cause of the signal. The following values are defined for si_code:
Member Type Member Name Description int si_signo Signal number int si_code Cause of the signal union sigval si_value Signal value
- The signal was sent by the kill() function. The implementation may set si_code to SI_USER if the signal was sent by the raise() or abort() functions or any similar functions provided as implementation extensions.
- The signal was sent by the sigqueue() function.
- The signal was generated by the expiration of a timer set by timer_settime().
- The signal was generated by the completion of an asynchronous I/O request.
- The signal was generated by the arrival of a message on an empty message queue.
If the signal was not generated by one of the functions or events listed above, the si_code will be set to an implementation-dependent value that is not equal to any of the values defined above.
If the Realtime Signals Extension is supported, and si_code is one of SI_QUEUE, SI_TIMER, SI_ASYNCIO, or SI_MESGQ, then si_value contains the application-specified signal value. Otherwise, the contents of si_value are undefined.
- The behaviour of a process is undefined after it returns normally from a signal-catching function for a SIGBUS, SIGFPE, SIGILL or SIGSEGV signal that was not generated by kill(), sigqueue() or raise().
- The system will not allow a process to catch the signals SIGKILL and SIGSTOP.
- If a process establishes a signal-catching function for the SIGCHLD signal while it has a terminated child process for which it has not waited, it is unspecified whether a SIGCHLD signal is generated to indicate that child process.
- When signal-catching functions are invoked asynchronously with process execution, the behaviour of some of the functions defined by this document is unspecified if they are called from a signal-catching function.
The following table defines a set of interfaces that are either reentrant or not interruptible by signals and are async-signal safe. Therefore applications may invoke them, without restriction, from signal-catching functions:
_exit() access() alarm() cfgetispeed() cfgetospeed() cfsetispeed() cfsetospeed() chdir() chmod() chown() close() creat() dup() dup2() execle() execve() fcntl() fork() fpathconf() fstat() fsync() getegid() geteuid() getgid() getgroups() getpgrp() getpid() getppid() getuid() kill() link() lseek() mkdir() mkfifo() open() pathconf() pause() pipe() raise() read() rename() rmdir() setgid() setpgid() setsid() setuid() sigaction() sigaddset() sigdelset() sigemptyset() sigfillset () sigismember() signal() sigpending() sigprocmask() sigsuspend() sleep() stat() sysconf() tcdrain() tcflow() tcflush() tcgetattr() tcgetpgrp() tcsendbreak() tcsetattr() tcsetpgrp() time() times() umask() uname() unlink() utime() wait() waitpid() write()
aio_error() clock_gettime() sigpause() timer_getoverrun() aio_return() fdatasync() sigqueue() timer_gettime() aio_suspend() sem_post() sigset() timer_settime()
All functions not in the above table are considered to be unsafe with respect to signals. In the presence of signals, all functions defined by this specification will behave as defined when called from or interrupted by a signal-catching function, with a single exception: when a signal interrupts an unsafe function and the signal-catching function calls an unsafe function, the behaviour is undefined.
When a signal is delivered to a thread, if the action of that signal specifies termination, stop, or continue, the entire process will be terminated, stopped, or continued, respectively.
Signal Effects on Other FunctionsSignals affect the behaviour of certain functions defined by this specification 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 behaviour of the interrupted function will be as described individually for that function. Signals that are ignored will not affect the behaviour of any function; signals that are blocked will not affect the behaviour of any function until they are unblocked and then delivered, except as specified for the sigpending() and the sigwait() functions.
The result of the use of sigaction() and a sigwait() function concurrently within a process on the same signal is unspecified.
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 sig argument is not a valid signal number or an attempt is made to catch a signal that cannot be caught or ignore a signal that cannot be ignored.
The sigaction() function may fail if:
- An attempt was made to set the action to SIG_DFL for a signal that cannot be caught or ignored (or both).
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 behaviour of reentrant interfaces, as defined in the description, is as specified by this specification, regardless of invocation from a signal-catching function. This is the only intended meaning of the statement that reentrant interfaces 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 description. The fact that any specific interface is listed as reentrant does not necessarily mean that invocation of that interface 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 (see semget(), sem_init(), sem_open(), and so on). Note in particular that even the "safe" functions may modify errno; the signal-catching function, if not executing as an independent thread, 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 interfaces. 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 behaviour 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() or raise(), the behaviour 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.
The ISO POSIX-1 standard 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 ucontext_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 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.
bsd_signal(), kill(), _longjmp(), longjmp(), raise(), semget(), sem_init(), sem_open(), sigaddset(), sigaltstack(), sigdelset(), sigemptyset(), sigfillset(), sigismember(), signal(), sigprocmask(), sigsuspend(), wait(), wait3(), waitid(), waitpid(), <signal.h>, <ucontext.h>.
Derived from the POSIX.1-1988 standard.