OpenBSD manual page server

Manual Page Search Parameters

SIGNAL(3) Library Functions Manual SIGNAL(3)

signal, bsd_signalsimplified software signal facilities

#include <signal.h>

void
(*signal(int sigcatch, void (*func)(int sigraised)))(int);

void
(*bsd_signal(int sigcatch, void (*func)(int sigraised)))(int);

The () and () facilities are simplified interfaces to the more general sigaction(2) facility. The bsd_signal() interface is provided for source compatibility only. It is mainly used on systems where the standard signal() does not have BSD semantics. On OpenBSD the two interfaces are identical.

Signals allow the manipulation of a process from outside its domain as well as allowing the process to manipulate itself or copies of itself (children). There are two general types of signals: those that cause termination of a process and those that do not. Signals which cause termination of a program might result from an irrecoverable error or might be the result of a user at a terminal typing the “interrupt” character.

Signals are used when a process is stopped because it wishes to access its controlling terminal while in the background (see tty(4)). Signals are optionally generated when a process resumes after being stopped, when the status of child processes changes, or when input is ready at the controlling terminal. Most signals result in the termination of the process receiving them if no action is taken; some signals instead cause the process receiving them to be stopped, or are simply discarded if the process has not requested otherwise.

Except for the SIGKILL and SIGSTOP signals, the () function allows for any signal to be caught, to be ignored, or to generate an interrupt. These signals are defined in the file <signal.h>:

terminate process terminal line hangup
terminate process interrupt program
create core image quit program
create core image illegal instruction
create core image trace trap
create core image abort(3) call (formerly SIGIOT)
create core image emulate instruction executed
create core image floating-point exception
terminate process kill program (cannot be caught or ignored)
create core image bus error
create core image segmentation violation
create core image system call given invalid argument
terminate process write on a pipe with no reader
terminate process real-time timer expired
terminate process software termination signal
discard signal urgent condition present on socket
stop process stop (cannot be caught or ignored)
stop process stop signal generated from keyboard
discard signal continue after stop
discard signal child status has changed
stop process background read attempted from controlling terminal
stop process background write attempted to controlling terminal
discard signal I/O is possible on a descriptor (see fcntl(2))
terminate process CPU time limit exceeded (see setrlimit(2))
terminate process file size limit exceeded (see setrlimit(2))
terminate process virtual time alarm (see setitimer(2))
terminate process profiling timer alarm (see setitimer(2))
discard signal window size change
discard signal status request from keyboard
terminate process user-defined signal 1
terminate process user-defined signal 2
discard signal thread AST

The func argument is a function to be called as the action upon receipt of the signal sigcatch. The function will be called with one argument, sigraised, which is the signal raised (thus the same function, func, can be used by more than one signal). To set the default action of the signal to occur as listed above, func should be SIG_DFL. A SIG_DFL resets the default action. To ignore the signal, func should be SIG_IGN. This will cause subsequent instances of the signal to be ignored and pending instances to be discarded. If SIG_IGN is not used, further occurrences of the signal are automatically blocked and func is called.

If the func is set to SIG_IGN for the SIGCHLD signal, the system will not create zombie processes when children of the calling process exit. If the calling process subsequently issues a wait(2) (or equivalent), it blocks until all of the calling process's child processes terminate, and then returns a value of -1 with errno set to ECHILD.

This differs from historical BSD behavior but is consistent with AT&T System V UNIX as well as the X/Open Portability Guide Issue 4, Version 2 (“XPG4.2”).

The handled signal is unblocked when func returns and the process continues from where it left off when the signal occurred.

Unlike previous signal facilities, the handler func() remains installed after a signal has been delivered.

For some system calls, if a signal is caught while the call is executing and the call is prematurely terminated, the call is automatically restarted. (The handler is installed using the SA_RESTART flag with sigaction(2).) The affected system calls include read(2), write(2), sendto(2), recvfrom(2), sendmsg(2), and recvmsg(2) on a communications channel or a low-speed device and during a ioctl(2) or wait(2). However, calls that have already committed are not restarted, but instead return a partial success (for example, a short read count). The siginterrupt(3) function can be used to change the system call restart behavior for a specific signal.

When a process which has installed signal handlers forks, the child process inherits the signals. All caught signals, as well as SIGCHLD, are reset to their default action by a call to the execve(2) function; other ignored signals remain ignored.

Signal handlers should be as minimal as possible, and use only signal-safe operations. The safest handlers only change a single variable of type volatile sig_atomic_t, which is inspected by an event loop. Other variables accessed inside the handler must be either const, or local to the handler. More complicated global variables (such as strings, structs, or lists) will require external methods to guarantee consistency, such as signal-blocking with sigprocmask(2).

More complicated handlers must restrict themselves to calling only the following list of signal-safe functions directly. Avoid abstracting the work to helper functions which are also called from other contexts because future coders will forget the signal-safe requirement.

Standard Interfaces:

(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), signal(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), and perhaps some others.

Extension Interfaces:

(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ().

Since signal-safe functions can encounter system call errors, errno should be protected inside the handler with the following pattern:

void
handler(int sig)
{
	int save_errno = errno;

	...
	errno = save_errno;
}

On OpenBSD, a few more functions are signal-safe (except when the format string contains floating-point arguments). These functions are expected to be unsafe on other systems, so be very cautious of the portability trap!

()
Safe.
()
Safe.
()
Safe.
()
Safe.
()
Safe if the syslog_data struct is initialized as a local variable.

The previous action is returned on a successful call. Otherwise, SIG_ERR is returned and the global variable errno is set to indicate the error.

signal() will fail and no action will take place if one of the following occurs:

[]
A specified signal is not a valid signal number.
[]
An attempt is made to ignore or supply a handler for SIGKILL or SIGSTOP.

kill(1), kill(2), ptrace(2), sigaction(2), sigaltstack(2), sigprocmask(2), sigsuspend(2), setjmp(3), siginterrupt(3), tty(4)

A signal() system call first appeared in Version 4 AT&T UNIX. In 4.2BSD, it was reimplemented as a wrapper around the former sigvec() system call, and for 4.3BSD-Reno, it was rewritten to use sigaction(2) instead.

July 14, 2024 OpenBSD-current