OSDN Git Service

(split) LDP_man-pages: update original to v3.34.
[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 .\" Permission is granted to make and distribute verbatim copies of this
9 .\" manual provided the copyright notice and this permission notice are
10 .\" preserved on all copies.
11 .\"
12 .\" Permission is granted to copy and distribute modified versions of this
13 .\" manual under the conditions for verbatim copying, provided that the
14 .\" entire resulting derived work is distributed under the terms of a
15 .\" permission notice identical to this one.
16 .\"
17 .\" Since the Linux kernel and libraries are constantly changing, this
18 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
19 .\" responsibility for errors or omissions, or for damages resulting from
20 .\" the use of the information contained herein.  The author(s) may not
21 .\" have taken the same level of care in the production of this manual,
22 .\" which is licensed free of charge, as they might when working
23 .\" professionally.
24 .\"
25 .\" Formatted or processed versions of this manual, if unaccompanied by
26 .\" the source, must acknowledge the copyright and authors of this work.
27 .\"
28 .\" Modified 2004-11-19, mtk:
29 .\" added pointer to sigaction.2 for details of ignoring SIGCHLD
30 .\" 2007-06-03, mtk: strengthened portability warning, and rewrote
31 .\"     various sections.
32 .\" 2008-07-11, mtk: rewrote and expanded portability discussion.
33 .\"
34 .TH SIGNAL 2 2008-07-11 "Linux" "Linux Programmer's Manual"
35 .SH NAME
36 signal \- ANSI C signal handling
37 .SH SYNOPSIS
38 .B #include <signal.h>
39 .sp
40 .B typedef void (*sighandler_t)(int);
41 .sp
42 .BI "sighandler_t signal(int " signum ", sighandler_t " handler );
43 .SH DESCRIPTION
44 The behavior of
45 .BR signal ()
46 varies across UNIX versions,
47 and has also varied historically across different versions of Linux.
48 \fBAvoid its use\fP: use
49 .BR sigaction (2)
50 instead.
51 See \fIPortability\fP below.
52
53 .BR signal ()
54 sets the disposition of the signal
55 .I signum
56 to
57 .IR handler ,
58 which is either
59 .BR SIG_IGN ,
60 .BR SIG_DFL ,
61 or the address of a programmer-defined function (a "signal handler").
62
63 If the signal
64 .I signum
65 is delivered to the process, then one of the following happens:
66 .TP 3
67 *
68 If the disposition is set to
69 .BR SIG_IGN ,
70 then the signal is ignored.
71 .TP
72 *
73 If the disposition is set to
74 .BR SIG_DFL ,
75 then the default action associated with the signal (see
76 .BR signal (7))
77 occurs.
78 .TP
79 *
80 If the disposition is set to a function,
81 then first either the disposition is reset to
82 .BR SIG_DFL ,
83 or the signal is blocked (see \fIPortability\fP below), and then
84 .I handler
85 is called with argument
86 .IR signum .
87 If invocation of the handler caused the signal to be blocked,
88 then the signal is unblocked upon return from the handler.
89 .PP
90 The signals
91 .B SIGKILL
92 and
93 .B SIGSTOP
94 cannot be caught or ignored.
95 .SH "RETURN VALUE"
96 .BR signal ()
97 returns the previous value of the signal handler, or
98 .B SIG_ERR
99 on error.
100 .SH ERRORS
101 .TP
102 .B EINVAL
103 .I signum
104 is invalid.
105 .SH "CONFORMING TO"
106 C89, C99, POSIX.1-2001.
107 .SH NOTES
108 The effects of
109 .BR signal ()
110 in a multithreaded process are unspecified.
111 .PP
112 According to POSIX, the behavior of a process is undefined after it
113 ignores a
114 .BR SIGFPE ,
115 .BR SIGILL ,
116 or
117 .B SIGSEGV
118 signal that was not generated by
119 .BR kill (2)
120 or
121 .BR raise (3).
122 Integer division by zero has undefined result.
123 On some architectures it will generate a
124 .B SIGFPE
125 signal.
126 (Also dividing the most negative integer by \-1 may generate
127 .BR SIGFPE .)
128 Ignoring this signal might lead to an endless loop.
129 .PP
130 See
131 .BR sigaction (2)
132 for details on what happens when
133 .B SIGCHLD
134 is set to
135 .BR SIG_IGN .
136 .PP
137 See
138 .BR signal (7)
139 for a list of the async-signal-safe functions that can be
140 safely called from inside a signal handler.
141 .PP
142 The use of
143 .I sighandler_t
144 is a GNU extension.
145 Various versions of libc predefine this type; libc4 and libc5 define
146 .IR SignalHandler ;
147 glibc defines
148 .I sig_t
149 and, when
150 .B _GNU_SOURCE
151 is defined, also
152 .IR sighandler_t .
153 Without use of such a type, the declaration of
154 .BR signal ()
155 is the somewhat harder to read:
156 .in +4n
157 .nf
158
159 .BI "void ( *" signal "(int " signum ", void (*" handler ")(int)) ) (int);"
160 .fi
161 .in
162 .SS Portability
163 The only portable use of
164 .BR signal ()
165 is to set a signal's disposition to
166 .BR SIG_DFL
167 or
168 .BR SIG_IGN .
169 The semantics when using
170 .BR signal ()
171 to establish a signal handler vary across systems
172 (and POSIX.1 explicitly permits this variation);
173 .B do not use it for this purpose.
174
175 POSIX.1 solved the portability mess by specifying
176 .BR sigaction (2),
177 which provides explicit control of the semantics when a
178 signal handler is invoked; use that interface instead of
179 .BR signal ().
180
181 In the original UNIX systems, when a handler that was established using
182 .BR signal ()
183 was invoked by the delivery of a signal,
184 the disposition of the signal would be reset to
185 .BR SIG_DFL ,
186 and the system did not block delivery of further instances of the signal.
187 System V also provides these semantics for
188 .BR signal ().
189 This was bad because the signal might be delivered again
190 before the handler had a chance to reestablish itself.
191 Furthermore, rapid deliveries of the same signal could
192 result in recursive invocations of the handler.
193
194 BSD improved on this situation by changing the semantics of
195 signal handling
196 (but, unfortunately, silently changed the semantics
197 when establishing a handler with
198 .BR signal ()).
199 On BSD, when a signal handler is invoked,
200 the signal disposition is not reset,
201 and further instances of the signal are blocked from
202 being delivered while the handler is executing.
203
204 The situation on Linux is as follows:
205 .IP * 2
206 The kernel's
207 .BR signal ()
208 system call provides System V semantics.
209 .IP *
210 By default, in glibc 2 and later, the
211 .BR signal ()
212 wrapper function does not invoke the kernel system call.
213 Instead, it calls
214 .BR sigaction (2)
215 using flags that supply BSD semantics.
216 This default behavior is provided as long as the
217 .B _BSD_SOURCE
218 feature test macro is defined.
219 By default,
220 .B _BSD_SOURCE
221 is defined;
222 it is also implicitly defined if one defines
223 .BR _GNU_SOURCE ,
224 and can of course be explicitly defined.
225 .sp
226 On glibc 2 and later, if the
227 .B _BSD_SOURCE
228 feature test macro is not defined, then
229 .BR signal ()
230 provides System V semantics.
231 (The default implicit definition of
232 .B _BSD_SOURCE
233 is not provided if one invokes
234 .BR gcc (1)
235 in one of its standard modes
236 .RI ( -std=xxx " or " -ansi )
237 or defines various other feature test macros such as
238 .BR _POSIX_SOURCE ,
239 .BR _XOPEN_SOURCE ,
240 or
241 .BR _SVID_SOURCE ;
242 see
243 .BR feature_test_macros (7).)
244 .\"
245 .\" System V semantics are also provided if one uses the separate
246 .\" .BR sysv_signal (3)
247 .\" function.
248 .IP *
249 The
250 .BR signal ()
251 function in Linux libc4 and libc5 provide System V semantics.
252 If one on a libc5 system includes
253 .I <bsd/signal.h>
254 instead of
255 .IR <signal.h> ,
256 then
257 .BR signal ()
258 provides BSD semantics.
259 .SH "SEE ALSO"
260 .BR kill (1),
261 .BR alarm (2),
262 .BR kill (2),
263 .BR killpg (2),
264 .BR pause (2),
265 .BR sigaction (2),
266 .BR signalfd (2),
267 .BR sigpending (2),
268 .BR sigprocmask (2),
269 .BR sigsuspend (2),
270 .BR bsd_signal (3),
271 .BR raise (3),
272 .BR siginterrupt (3),
273 .BR sigqueue (3),
274 .BR sigsetops (3),
275 .BR sigvec (3),
276 .BR sysv_signal (3),
277 .BR signal (7)