OSDN Git Service

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