namespaces.7: ffix
[man-pages.git] / man2 / signal.2
blob3d254eada22bfb781b77b888b97f389de3d82cfa
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>.
7 .\"
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.
12 .\"
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.
17 .\"
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
24 .\" professionally.
25 .\"
26 .\" Formatted or processed versions of this manual, if unaccompanied by
27 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" %%%LICENSE_END
29 .\"
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
33 .\"     various sections.
34 .\" 2008-07-11, mtk: rewrote and expanded portability discussion.
35 .\"
36 .TH SIGNAL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
37 .SH NAME
38 signal \- ANSI C signal handling
39 .SH SYNOPSIS
40 .nf
41 .B #include <signal.h>
42 .PP
43 .B typedef void (*sighandler_t)(int);
44 .PP
45 .BI "sighandler_t signal(int " signum ", sighandler_t " handler );
46 .fi
47 .SH DESCRIPTION
48 .BR WARNING :
49  the behavior of
50 .BR signal ()
51 varies across UNIX versions,
52 and has also varied historically across different versions of Linux.
53 \fBAvoid its use\fP: use
54 .BR sigaction (2)
55 instead.
56 See \fIPortability\fP below.
57 .PP
58 .BR signal ()
59 sets the disposition of the signal
60 .I signum
62 .IR handler ,
63 which is either
64 .BR SIG_IGN ,
65 .BR SIG_DFL ,
66 or the address of a programmer-defined function (a "signal handler").
67 .PP
68 If the signal
69 .I signum
70 is delivered to the process, then one of the following happens:
71 .TP 3
73 If the disposition is set to
74 .BR SIG_IGN ,
75 then the signal is ignored.
76 .TP
78 If the disposition is set to
79 .BR SIG_DFL ,
80 then the default action associated with the signal (see
81 .BR signal (7))
82 occurs.
83 .TP
85 If the disposition is set to a function,
86 then first either the disposition is reset to
87 .BR SIG_DFL ,
88 or the signal is blocked (see \fIPortability\fP below), and then
89 .I handler
90 is called with argument
91 .IR signum .
92 If invocation of the handler caused the signal to be blocked,
93 then the signal is unblocked upon return from the handler.
94 .PP
95 The signals
96 .B SIGKILL
97 and
98 .B SIGSTOP
99 cannot be caught or ignored.
100 .SH RETURN VALUE
101 .BR signal ()
102 returns the previous value of the signal handler
103 On failure, it returns
104 .BR SIG_ERR ,
106 .I errno
107 is set to indicate the error.
108 .SH ERRORS
110 .B EINVAL
111 .I signum
112 is invalid.
113 .SH CONFORMING TO
114 POSIX.1-2001, POSIX.1-2008, C89, C99.
115 .SH NOTES
116 The effects of
117 .BR signal ()
118 in a multithreaded process are unspecified.
120 According to POSIX, the behavior of a process is undefined after it
121 ignores a
122 .BR SIGFPE ,
123 .BR SIGILL ,
125 .B SIGSEGV
126 signal that was not generated by
127 .BR kill (2)
129 .BR raise (3).
130 Integer division by zero has undefined result.
131 On some architectures it will generate a
132 .B SIGFPE
133 signal.
134 (Also dividing the most negative integer by \-1 may generate
135 .BR SIGFPE .)
136 Ignoring this signal might lead to an endless loop.
139 .BR sigaction (2)
140 for details on what happens when the disposition
141 .B SIGCHLD
142 is set to
143 .BR SIG_IGN .
146 .BR signal\-safety (7)
147 for a list of the async-signal-safe functions that can be
148 safely called from inside a signal handler.
150 The use of
151 .I sighandler_t
152 is a GNU extension, exposed if
153 .B _GNU_SOURCE
154 is defined;
155 .\" libc4 and libc5 define
156 .\" .IR SignalHandler ;
157 glibc also defines (the BSD-derived)
158 .I sig_t
160 .B _BSD_SOURCE
161 (glibc 2.19 and earlier)
163 .BR _DEFAULT_SOURCE
164 (glibc 2.19 and later)
165 is defined.
166 Without use of such a type, the declaration of
167 .BR signal ()
168 is the somewhat harder to read:
170 .in +4n
172 .BI "void ( *" signal "(int " signum ", void (*" handler ")(int)) ) (int);"
175 .SS Portability
176 The only portable use of
177 .BR signal ()
178 is to set a signal's disposition to
179 .BR SIG_DFL
181 .BR SIG_IGN .
182 The semantics when using
183 .BR signal ()
184 to establish a signal handler vary across systems
185 (and POSIX.1 explicitly permits this variation);
186 .B do not use it for this purpose.
188 POSIX.1 solved the portability mess by specifying
189 .BR sigaction (2),
190 which provides explicit control of the semantics when a
191 signal handler is invoked; use that interface instead of
192 .BR signal ().
194 In the original UNIX systems, when a handler that was established using
195 .BR signal ()
196 was invoked by the delivery of a signal,
197 the disposition of the signal would be reset to
198 .BR SIG_DFL ,
199 and the system did not block delivery of further instances of the signal.
200 This is equivalent to calling
201 .BR sigaction (2)
202 with the following flags:
204 .in +4n
206 sa.sa_flags = SA_RESETHAND | SA_NODEFER;
210 System\ V also provides these semantics for
211 .BR signal ().
212 This was bad because the signal might be delivered again
213 before the handler had a chance to reestablish itself.
214 Furthermore, rapid deliveries of the same signal could
215 result in recursive invocations of the handler.
217 BSD improved on this situation, but unfortunately also
218 changed the semantics of the existing
219 .BR signal ()
220 interface while doing so.
221 On BSD, when a signal handler is invoked,
222 the signal disposition is not reset,
223 and further instances of the signal are blocked from
224 being delivered while the handler is executing.
225 Furthermore, certain blocking system calls are automatically
226 restarted if interrupted by a signal handler (see
227 .BR signal (7)).
228 The BSD semantics are equivalent to calling
229 .BR sigaction (2)
230 with the following flags:
232 .in +4n
234 sa.sa_flags = SA_RESTART;
238 The situation on Linux is as follows:
239 .IP * 2
240 The kernel's
241 .BR signal ()
242 system call provides System\ V semantics.
243 .IP *
244 By default, in glibc 2 and later, the
245 .BR signal ()
246 wrapper function does not invoke the kernel system call.
247 Instead, it calls
248 .BR sigaction (2)
249 using flags that supply BSD semantics.
250 This default behavior is provided as long as a suitable
251 feature test macro is defined:
252 .B _BSD_SOURCE
253 on glibc 2.19 and earlier or
254 .BR _DEFAULT_SOURCE
255 in glibc 2.19 and later.
256 (By default, these macros are defined; see
257 .BR feature_test_macros (7)
258 for details.)
259 If such a feature test macro is not defined, then
260 .BR signal ()
261 provides System\ V semantics.
263 .\" System V semantics are also provided if one uses the separate
264 .\" .BR sysv_signal (3)
265 .\" function.
266 .\" .IP *
267 .\" The
268 .\" .BR signal ()
269 .\" function in Linux libc4 and libc5 provide System\ V semantics.
270 .\" If one on a libc5 system includes
271 .\" .I <bsd/signal.h>
272 .\" instead of
273 .\" .IR <signal.h> ,
274 .\" then
275 .\" .BR signal ()
276 .\" provides BSD semantics.
277 .SH SEE ALSO
278 .BR kill (1),
279 .BR alarm (2),
280 .BR kill (2),
281 .BR pause (2),
282 .BR sigaction (2),
283 .BR signalfd (2),
284 .BR sigpending (2),
285 .BR sigprocmask (2),
286 .BR sigsuspend (2),
287 .BR bsd_signal (3),
288 .BR killpg (3),
289 .BR raise (3),
290 .BR siginterrupt (3),
291 .BR sigqueue (3),
292 .BR sigsetops (3),
293 .BR sigvec (3),
294 .BR sysv_signal (3),
295 .BR signal (7)