OSDN Git Service

41edd4d377108bc4581a354fb02d77b27021f8bd
[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 2013-02-09 "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 .SH ERRORS
103 .TP
104 .B EINVAL
105 .I signum
106 is invalid.
107 .SH CONFORMING TO
108 C89, C99, POSIX.1-2001.
109 .SH NOTES
110 The effects of
111 .BR signal ()
112 in a multithreaded process are unspecified.
113 .PP
114 According to POSIX, the behavior of a process is undefined after it
115 ignores a
116 .BR SIGFPE ,
117 .BR SIGILL ,
118 or
119 .B SIGSEGV
120 signal that was not generated by
121 .BR kill (2)
122 or
123 .BR raise (3).
124 Integer division by zero has undefined result.
125 On some architectures it will generate a
126 .B SIGFPE
127 signal.
128 (Also dividing the most negative integer by \-1 may generate
129 .BR SIGFPE .)
130 Ignoring this signal might lead to an endless loop.
131 .PP
132 See
133 .BR sigaction (2)
134 for details on what happens when
135 .B SIGCHLD
136 is set to
137 .BR SIG_IGN .
138 .PP
139 See
140 .BR signal (7)
141 for a list of the async-signal-safe functions that can be
142 safely called from inside a signal handler.
143 .PP
144 The use of
145 .I sighandler_t
146 is a GNU extension, exposed if
147 .B _GNU_SOURCE
148 is defined;
149 .\" libc4 and libc5 define
150 .\" .IR SignalHandler ;
151 glibc also defines (the BSD-derived)
152 .I sig_t
153 if
154 .B _BSD_SOURCE
155 is defined.
156 Without use of such a type, the declaration of
157 .BR signal ()
158 is the somewhat harder to read:
159 .in +4n
160 .nf
161
162 .BI "void ( *" signal "(int " signum ", void (*" handler ")(int)) ) (int);"
163 .fi
164 .in
165 .SS Portability
166 The only portable use of
167 .BR signal ()
168 is to set a signal's disposition to
169 .BR SIG_DFL
170 or
171 .BR SIG_IGN .
172 The semantics when using
173 .BR signal ()
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.
177
178 POSIX.1 solved the portability mess by specifying
179 .BR sigaction (2),
180 which provides explicit control of the semantics when a
181 signal handler is invoked; use that interface instead of
182 .BR signal ().
183
184 In the original UNIX systems, when a handler that was established using
185 .BR signal ()
186 was invoked by the delivery of a signal,
187 the disposition of the signal would be reset to
188 .BR SIG_DFL ,
189 and the system did not block delivery of further instances of the signal.
190 This is equivalent to calling
191 .BR sigaction (2)
192 with the following flags:
193
194     sa.sa_flags = SA_RESETHAND | SA_NODEFER;
195
196 System V also provides these semantics for
197 .BR signal ().
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.
202
203 BSD improved on this situation, but unfortunately also
204 changed the semantics of the existing
205 .BR signal ()
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
213 .BR signal (7)).
214 The BSD semantics are equivalent to calling
215 .BR sigaction (2)
216 with the following flags:
217
218     sa.sa_flags = SA_RESTART;
219
220 The situation on Linux is as follows:
221 .IP * 2
222 The kernel's
223 .BR signal ()
224 system call provides System V semantics.
225 .IP *
226 By default, in glibc 2 and later, the
227 .BR signal ()
228 wrapper function does not invoke the kernel system call.
229 Instead, it calls
230 .BR sigaction (2)
231 using flags that supply BSD semantics.
232 This default behavior is provided as long as the
233 .B _BSD_SOURCE
234 feature test macro is defined.
235 By default,
236 .B _BSD_SOURCE
237 is defined;
238 it is also implicitly defined if one defines
239 .BR _GNU_SOURCE ,
240 and can of course be explicitly defined.
241 .sp
242 On glibc 2 and later, if the
243 .B _BSD_SOURCE
244 feature test macro is not defined, then
245 .BR signal ()
246 provides System V semantics.
247 (The default implicit definition of
248 .B _BSD_SOURCE
249 is not provided if one invokes
250 .BR gcc (1)
251 in one of its standard modes
252 .RI ( -std=xxx " or " -ansi )
253 or defines various other feature test macros such as
254 .BR _POSIX_SOURCE ,
255 .BR _XOPEN_SOURCE ,
256 or
257 .BR _SVID_SOURCE ;
258 see
259 .BR feature_test_macros (7).)
260 .\"
261 .\" System V semantics are also provided if one uses the separate
262 .\" .BR sysv_signal (3)
263 .\" function.
264 .IP *
265 The
266 .BR signal ()
267 function in Linux libc4 and libc5 provide System V semantics.
268 If one on a libc5 system includes
269 .I <bsd/signal.h>
270 instead of
271 .IR <signal.h> ,
272 then
273 .BR signal ()
274 provides BSD semantics.
275 .SH SEE ALSO
276 .BR kill (1),
277 .BR alarm (2),
278 .BR kill (2),
279 .BR killpg (2),
280 .BR pause (2),
281 .BR sigaction (2),
282 .BR signalfd (2),
283 .BR sigpending (2),
284 .BR sigprocmask (2),
285 .BR sigsuspend (2),
286 .BR bsd_signal (3),
287 .BR raise (3),
288 .BR siginterrupt (3),
289 .BR sigqueue (3),
290 .BR sigsetops (3),
291 .BR sigvec (3),
292 .BR sysv_signal (3),
293 .BR signal (7)