1 .\" Copyright (c) 2000 Andries Brouwer <aeb@cwi.nl>
2 .\" and Copyright (c) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" and Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
5 .\" based on work by Rik Faith <faith@cs.unc.edu>
6 .\" and Mike Battersby <mike@starbug.apana.org.au>.
8 .\" %%%LICENSE_START(VERBATIM)
9 .\" Permission is granted to make and distribute verbatim copies of this
10 .\" manual provided the copyright notice and this permission notice are
11 .\" preserved on all copies.
13 .\" Permission is granted to copy and distribute modified versions of this
14 .\" manual under the conditions for verbatim copying, provided that the
15 .\" entire resulting derived work is distributed under the terms of a
16 .\" permission notice identical to this one.
18 .\" Since the Linux kernel and libraries are constantly changing, this
19 .\" manual page may be incorrect or out-of-date. The author(s) assume no
20 .\" responsibility for errors or omissions, or for damages resulting from
21 .\" the use of the information contained herein. The author(s) may not
22 .\" have taken the same level of care in the production of this manual,
23 .\" which is licensed free of charge, as they might when working
26 .\" Formatted or processed versions of this manual, if unaccompanied by
27 .\" the source, must acknowledge the copyright and authors of this work.
30 .\" Modified 2004-11-19, mtk:
31 .\" added pointer to sigaction.2 for details of ignoring SIGCHLD
32 .\" 2007-06-03, mtk: strengthened portability warning, and rewrote
34 .\" 2008-07-11, mtk: rewrote and expanded portability discussion.
36 .TH SIGNAL 2 2013-02-09 "Linux" "Linux Programmer's Manual"
38 signal \- ANSI C signal handling
40 .B #include <signal.h>
42 .B typedef void (*sighandler_t)(int);
44 .BI "sighandler_t signal(int " signum ", sighandler_t " handler );
48 varies across UNIX versions,
49 and has also varied historically across different versions of Linux.
50 \fBAvoid its use\fP: use
53 See \fIPortability\fP below.
56 sets the disposition of the signal
63 or the address of a programmer-defined function (a "signal handler").
67 is delivered to the process, then one of the following happens:
70 If the disposition is set to
72 then the signal is ignored.
75 If the disposition is set to
77 then the default action associated with the signal (see
82 If the disposition is set to a function,
83 then first either the disposition is reset to
85 or the signal is blocked (see \fIPortability\fP below), and then
87 is called with argument
89 If invocation of the handler caused the signal to be blocked,
90 then the signal is unblocked upon return from the handler.
96 cannot be caught or ignored.
99 returns the previous value of the signal handler, or
108 C89, C99, POSIX.1-2001.
112 in a multithreaded process are unspecified.
114 According to POSIX, the behavior of a process is undefined after it
120 signal that was not generated by
124 Integer division by zero has undefined result.
125 On some architectures it will generate a
128 (Also dividing the most negative integer by \-1 may generate
130 Ignoring this signal might lead to an endless loop.
134 for details on what happens when
141 for a list of the async-signal-safe functions that can be
142 safely called from inside a signal handler.
146 is a GNU extension, exposed if
149 .\" libc4 and libc5 define
150 .\" .IR SignalHandler ;
151 glibc also defines (the BSD-derived)
156 Without use of such a type, the declaration of
158 is the somewhat harder to read:
162 .BI "void ( *" signal "(int " signum ", void (*" handler ")(int)) ) (int);"
166 The only portable use of
168 is to set a signal's disposition to
172 The semantics when using
174 to establish a signal handler vary across systems
175 (and POSIX.1 explicitly permits this variation);
176 .B do not use it for this purpose.
178 POSIX.1 solved the portability mess by specifying
180 which provides explicit control of the semantics when a
181 signal handler is invoked; use that interface instead of
184 In the original UNIX systems, when a handler that was established using
186 was invoked by the delivery of a signal,
187 the disposition of the signal would be reset to
189 and the system did not block delivery of further instances of the signal.
190 This is equivalent to calling
192 with the following flags:
194 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
196 System V also provides these semantics for
198 This was bad because the signal might be delivered again
199 before the handler had a chance to reestablish itself.
200 Furthermore, rapid deliveries of the same signal could
201 result in recursive invocations of the handler.
203 BSD improved on this situation, but unfortunately also
204 changed the semantics of the existing
206 interface while doing so.
207 On BSD, when a signal handler is invoked,
208 the signal disposition is not reset,
209 and further instances of the signal are blocked from
210 being delivered while the handler is executing.
211 Furthermore, certain blocking system calls are automatically
212 restarted if interrupted by a signal handler (see
214 The BSD semantics are equivalent to calling
216 with the following flags:
218 sa.sa_flags = SA_RESTART;
220 The situation on Linux is as follows:
224 system call provides System V semantics.
226 By default, in glibc 2 and later, the
228 wrapper function does not invoke the kernel system call.
231 using flags that supply BSD semantics.
232 This default behavior is provided as long as the
234 feature test macro is defined.
238 it is also implicitly defined if one defines
240 and can of course be explicitly defined.
242 On glibc 2 and later, if the
244 feature test macro is not defined, then
246 provides System V semantics.
247 (The default implicit definition of
249 is not provided if one invokes
251 in one of its standard modes
252 .RI ( -std=xxx " or " -ansi )
253 or defines various other feature test macros such as
259 .BR feature_test_macros (7).)
261 .\" System V semantics are also provided if one uses the separate
262 .\" .BR sysv_signal (3)
267 function in Linux libc4 and libc5 provide System V semantics.
268 If one on a libc5 system includes
274 provides BSD semantics.
288 .BR siginterrupt (3),