adjtimex.2: tfix
[man-pages.git] / man7 / signal-safety.7
blob451d07862b2afe7e30678cd1edc5ca4481114192
1 .\" Copyright (c) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH SIGNAL-SAFETY 7  2019-10-10 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 signal-safety \- async-signal-safe functions
28 .SH DESCRIPTION
30 .I async-signal-safe
31 function is one that can be safely called from within a signal handler.
32 Many functions are
33 .I not
34 async-signal-safe.
35 In particular,
36 nonreentrant functions are generally unsafe to call from a signal handler.
37 .PP
38 The kinds of issues that render a function
39 unsafe can be quickly understood when one considers
40 the implementation of the
41 .I stdio
42 library, all of whose functions are not async-signal-safe.
43 .PP
44 When performing buffered I/O on a file, the
45 .I stdio
46 functions must maintain a statically allocated data buffer
47 along with associated counters and indexes (or pointers)
48 that record the amount of data and the current position in the buffer.
49 Suppose that the main program is in the middle of a call to a
50 .I stdio
51 function such as
52 .BR printf (3)
53 where the buffer and associated variables have been partially updated.
54 If, at that moment,
55 the program is interrupted by a signal handler that also calls
56 .BR printf (3),
57 then the second call to
58 .BR printf (3)
59 will operate on inconsistent data, with unpredictable results.
60 .PP
61 To avoid problems with unsafe functions, there are two possible choices:
62 .IP 1. 3
63 Ensure that
64 (a) the signal handler calls only async-signal-safe functions,
65 and
66 (b) the signal handler itself is reentrant
67 with respect to global variables in the main program.
68 .IP 2.
69 Block signal delivery in the main program when calling functions
70 that are unsafe or operating on global data that is also accessed
71 by the signal handler.
72 .PP
73 Generally, the second choice is difficult in programs of any complexity,
74 so the first choice is taken.
75 .PP
76 POSIX.1 specifies a set of functions that an implementation
77 must make async-signal-safe.
78 (An implementation may provide safe implementations of additional functions,
79 but this is not required by the standard and other implementations
80 may not provide the same guarantees.)
81 .PP
82 In general, a function is async-signal-safe either because it is reentrant
83 or because it is atomic with respect to signals
84 (i.e., its execution can't be interrupted by a signal handler).
85 .PP
86 The set of functions required to be async-signal-safe by POSIX.1
87 is shown in the following table.
88 The functions not otherwise noted were required to be async-signal-safe
89 in POSIX.1-2001;
90 the table details changes in the subsequent standards.
91 .PP
92 .TS
93 lb lb
94 l l.
95 Function        Notes
96 \fBabort\fP(3)  Added in POSIX.1-2003
97 \fBaccept\fP(2)
98 \fBaccess\fP(2)
99 \fBaio_error\fP(3)
100 \fBaio_return\fP(3)
101 \fBaio_suspend\fP(3)    See notes below
102 \fBalarm\fP(2)
103 \fBbind\fP(2)
104 \fBcfgetispeed\fP(3)
105 \fBcfgetospeed\fP(3)
106 \fBcfsetispeed\fP(3)
107 \fBcfsetospeed\fP(3)
108 \fBchdir\fP(2)
109 \fBchmod\fP(2)
110 \fBchown\fP(2)
111 \fBclock_gettime\fP(2)
112 \fBclose\fP(2)
113 \fBconnect\fP(2)
114 \fBcreat\fP(2)
115 \fBdup\fP(2)
116 \fBdup2\fP(2)
117 \fBexecl\fP(3)  Added in POSIX.1-2008; see notes below
118 \fBexecle\fP(3) See notes below
119 \fBexecv\fP(3)  Added in POSIX.1-2008
120 \fBexecve\fP(2)
121 \fB_exit\fP(2)
122 \fB_Exit\fP(2)
123 \fBfaccessat\fP(2)      Added in POSIX.1-2008
124 \fBfchdir\fP(2) Added in POSIX.1-2013
125 \fBfchmod\fP(2)
126 \fBfchmodat\fP(2)       Added in POSIX.1-2008
127 \fBfchown\fP(2)
128 \fBfchownat\fP(2)       Added in POSIX.1-2008
129 \fBfcntl\fP(2)
130 \fBfdatasync\fP(2)
131 \fBfexecve\fP(3)        Added in POSIX.1-2008
132 \fBffs\fP(3)    Added in POSIX.1-2016
133 \fBfork\fP(2)   See notes below
134 \fBfstat\fP(2)
135 \fBfstatat\fP(2)        Added in POSIX.1-2008
136 \fBfsync\fP(2)
137 \fBftruncate\fP(2)
138 \fBfutimens\fP(3)       Added in POSIX.1-2008
139 \fBgetegid\fP(2)
140 \fBgeteuid\fP(2)
141 \fBgetgid\fP(2)
142 \fBgetgroups\fP(2)
143 \fBgetpeername\fP(2)
144 \fBgetpgrp\fP(2)
145 \fBgetpid\fP(2)
146 \fBgetppid\fP(2)
147 \fBgetsockname\fP(2)
148 \fBgetsockopt\fP(2)
149 \fBgetuid\fP(2)
150 \fBhtonl\fP(3)  Added in POSIX.1-2016
151 \fBhtons\fP(3)  Added in POSIX.1-2016
152 \fBkill\fP(2)
153 \fBlink\fP(2)
154 \fBlinkat\fP(2) Added in POSIX.1-2008
155 \fBlisten\fP(2)
156 \fBlongjmp\fP(3)        Added in POSIX.1-2016; see notes below
157 \fBlseek\fP(2)
158 \fBlstat\fP(2)
159 \fBmemccpy\fP(3)        Added in POSIX.1-2016
160 \fBmemchr\fP(3) Added in POSIX.1-2016
161 \fBmemcmp\fP(3) Added in POSIX.1-2016
162 \fBmemcpy\fP(3) Added in POSIX.1-2016
163 \fBmemmove\fP(3)        Added in POSIX.1-2016
164 \fBmemset\fP(3) Added in POSIX.1-2016
165 \fBmkdir\fP(2)
166 \fBmkdirat\fP(2)        Added in POSIX.1-2008
167 \fBmkfifo\fP(3)
168 \fBmkfifoat\fP(3)       Added in POSIX.1-2008
169 \fBmknod\fP(2)  Added in POSIX.1-2008
170 \fBmknodat\fP(2)        Added in POSIX.1-2008
171 \fBntohl\fP(3)  Added in POSIX.1-2016
172 \fBntohs\fP(3)  Added in POSIX.1-2016
173 \fBopen\fP(2)
174 \fBopenat\fP(2) Added in POSIX.1-2008
175 \fBpause\fP(2)
176 \fBpipe\fP(2)
177 \fBpoll\fP(2)
178 \fBposix_trace_event\fP(3)
179 \fBpselect\fP(2)
180 \fBpthread_kill\fP(3)   Added in POSIX.1-2013
181 \fBpthread_self\fP(3)   Added in POSIX.1-2013
182 \fBpthread_sigmask\fP(3)        Added in POSIX.1-2013
183 \fBraise\fP(3)
184 \fBread\fP(2)
185 \fBreadlink\fP(2)
186 \fBreadlinkat\fP(2)     Added in POSIX.1-2008
187 \fBrecv\fP(2)
188 \fBrecvfrom\fP(2)
189 \fBrecvmsg\fP(2)
190 \fBrename\fP(2)
191 \fBrenameat\fP(2)       Added in POSIX.1-2008
192 \fBrmdir\fP(2)
193 \fBselect\fP(2)
194 \fBsem_post\fP(3)
195 \fBsend\fP(2)
196 \fBsendmsg\fP(2)
197 \fBsendto\fP(2)
198 \fBsetgid\fP(2)
199 \fBsetpgid\fP(2)
200 \fBsetsid\fP(2)
201 \fBsetsockopt\fP(2)
202 \fBsetuid\fP(2)
203 \fBshutdown\fP(2)
204 \fBsigaction\fP(2)
205 \fBsigaddset\fP(3)
206 \fBsigdelset\fP(3)
207 \fBsigemptyset\fP(3)
208 \fBsigfillset\fP(3)
209 \fBsigismember\fP(3)
210 \fBsiglongjmp\fP(3)     Added in POSIX.1-2016; see notes below
211 \fBsignal\fP(2)
212 \fBsigpause\fP(3)
213 \fBsigpending\fP(2)
214 \fBsigprocmask\fP(2)
215 \fBsigqueue\fP(2)
216 \fBsigset\fP(3)
217 \fBsigsuspend\fP(2)
218 \fBsleep\fP(3)
219 \fBsockatmark\fP(3)     Added in POSIX.1-2004
220 \fBsocket\fP(2)
221 \fBsocketpair\fP(2)
222 \fBstat\fP(2)
223 \fBstpcpy\fP(3) Added in POSIX.1-2016
224 \fBstpncpy\fP(3)        Added in POSIX.1-2016
225 \fBstrcat\fP(3) Added in POSIX.1-2016
226 \fBstrchr\fP(3) Added in POSIX.1-2016
227 \fBstrcmp\fP(3) Added in POSIX.1-2016
228 \fBstrcpy\fP(3) Added in POSIX.1-2016
229 \fBstrcspn\fP(3)        Added in POSIX.1-2016
230 \fBstrlen\fP(3) Added in POSIX.1-2016
231 \fBstrncat\fP(3)        Added in POSIX.1-2016
232 \fBstrncmp\fP(3)        Added in POSIX.1-2016
233 \fBstrncpy\fP(3)        Added in POSIX.1-2016
234 \fBstrnlen\fP(3)        Added in POSIX.1-2016
235 \fBstrpbrk\fP(3)        Added in POSIX.1-2016
236 \fBstrrchr\fP(3)        Added in POSIX.1-2016
237 \fBstrspn\fP(3) Added in POSIX.1-2016
238 \fBstrstr\fP(3) Added in POSIX.1-2016
239 \fBstrtok_r\fP(3)       Added in POSIX.1-2016
240 \fBsymlink\fP(2)
241 \fBsymlinkat\fP(2)      Added in POSIX.1-2008
242 \fBtcdrain\fP(3)
243 \fBtcflow\fP(3)
244 \fBtcflush\fP(3)
245 \fBtcgetattr\fP(3)
246 \fBtcgetpgrp\fP(3)
247 \fBtcsendbreak\fP(3)
248 \fBtcsetattr\fP(3)
249 \fBtcsetpgrp\fP(3)
250 \fBtime\fP(2)
251 \fBtimer_getoverrun\fP(2)
252 \fBtimer_gettime\fP(2)
253 \fBtimer_settime\fP(2)
254 \fBtimes\fP(2)
255 \fBumask\fP(2)
256 \fBuname\fP(2)
257 \fBunlink\fP(2)
258 \fBunlinkat\fP(2)       Added in POSIX.1-2008
259 \fButime\fP(2)
260 \fButimensat\fP(2)      Added in POSIX.1-2008
261 \fButimes\fP(2) Added in POSIX.1-2008
262 \fBwait\fP(2)
263 \fBwaitpid\fP(2)
264 \fBwcpcpy\fP(3) Added in POSIX.1-2016
265 \fBwcpncpy\fP(3)        Added in POSIX.1-2016
266 \fBwcscat\fP(3) Added in POSIX.1-2016
267 \fBwcschr\fP(3) Added in POSIX.1-2016
268 \fBwcscmp\fP(3) Added in POSIX.1-2016
269 \fBwcscpy\fP(3) Added in POSIX.1-2016
270 \fBwcscspn\fP(3)        Added in POSIX.1-2016
271 \fBwcslen\fP(3) Added in POSIX.1-2016
272 \fBwcsncat\fP(3)        Added in POSIX.1-2016
273 \fBwcsncmp\fP(3)        Added in POSIX.1-2016
274 \fBwcsncpy\fP(3)        Added in POSIX.1-2016
275 \fBwcsnlen\fP(3)        Added in POSIX.1-2016
276 \fBwcspbrk\fP(3)        Added in POSIX.1-2016
277 \fBwcsrchr\fP(3)        Added in POSIX.1-2016
278 \fBwcsspn\fP(3) Added in POSIX.1-2016
279 \fBwcsstr\fP(3) Added in POSIX.1-2016
280 \fBwcstok\fP(3) Added in POSIX.1-2016
281 \fBwmemchr\fP(3)        Added in POSIX.1-2016
282 \fBwmemcmp\fP(3)        Added in POSIX.1-2016
283 \fBwmemcpy\fP(3)        Added in POSIX.1-2016
284 \fBwmemmove\fP(3)       Added in POSIX.1-2016
285 \fBwmemset\fP(3)        Added in POSIX.1-2016
286 \fBwrite\fP(2)
288 .sp 1
289 Notes:
290 .IP * 3
291 POSIX.1-2001 and POSIX.1-2004 required the functions
292 .BR fpathconf (3),
293 .BR pathconf (3),
295 .BR sysconf (3)
296 to be async-signal-safe, but this requirement was removed in POSIX.1-2008.
297 .IP *
298 If a signal handler interrupts the execution of an unsafe function,
299 and the handler terminates via a call to
300 .BR longjmp (3)
302 .BR siglongjmp (3)
303 and the program subsequently calls an unsafe function,
304 then the behavior of the program is undefined.
305 .IP *
306 POSIX.1-2003 clarified
307 that if an application calls
308 .BR fork (2)
309 from a signal handler and any of the fork handlers registered by
310 .BR pthread_atfork (3)
311 calls a function that is not async-signal-safe, the behavior is undefined.
312 A future revision of the standard
313 .\" http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt
314 is likely to remove
315 .BR fork (2)
316 from the list of async-signal-safe functions.
318 .IP * 3
319 Asynchronous signal handlers that call functions which are cancellation
320 points and nest over regions of deferred cancellation may trigger
321 cancellation whose behavior is as if asynchronous cancellation had
322 occurred and may cause application state to become inconsistent.
323 .SS Deviations in the GNU C library
324 The following known deviations from the standard occur in
325 the GNU C library:
326 .IP * 3
327 Before glibc 2.24,
328 .BR execl (3)
330 .BR execle (3)
331 employed
332 .BR realloc (3)
333 internally and were consequently not async-signal-safe.
334 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=19534
335 This was fixed in glibc 2.24.
336 .IP *
337 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=13172
338 The glibc implementation of
339 .BR aio_suspend (3)
340 is not async-signal-safe because it uses
341 .BR pthread_mutex_lock (3)
342 internally.
343 .SH SEE ALSO
344 .BR sigaction (2),
345 .BR signal (7),
346 .BR standards (7)