1 .\" Copyright (c) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH SIGNAL-SAFETY 7 2017-03-13 "Linux" "Linux Programmer's Manual"
27 signal-safety \- async-signal-safe functions
31 function is one that can be safely called from within a signal handler.
36 nonreentrant functions are generally unsafe to call from a signal handler.
38 The kinds of issues that render a function
39 unsafe can be quickly understood when one considers
40 the implementation of the
42 library, all of whose functions are not async-signal-safe.
44 When performing buffered I/O on a file, the
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
53 where the buffer and associated variables have been partially updated.
55 the program is interrupted by a signal handler that also calls
57 then the second call to
59 will operate on inconsistent data, with unpredictable results.
61 To avoid problems with unsafe functions, there are two possible choices:
64 (a) the signal handler calls only async-signal-safe functions,
66 (b) the signal handler itself is reentrant
67 with respect to global variables in the main program.
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.
73 Generally, the second choice is difficult in programs of any complexity,
74 so the first choice is taken.
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 In general, a function is async-signal-safe either because it is reentrant
82 or because it is atomic with respect to signals
83 (i.e., its execution can't be interrupted by a signal handler).
85 The set of functions required to be async-signal-safe by POSIX.1
86 is shown in the following table.
87 The functions not otherwise noted were required to be async-signal-safe
89 the table details changes in the subsequent standards.
95 \fBabort\fP(3) Added in POSIX.1-2003
100 \fBaio_suspend\fP(3) See notes below
110 \fBclock_gettime\fP(2)
116 \fBexecl\fP(3) Added in POSIX.1-2008; see notes below
117 \fBexecle\fP(3) See notes below
118 \fBexecv\fP(3) Added in POSIX.1-2008
122 \fBfaccessat\fP(2) Added in POSIX.1-2008
123 \fBfchdir\fP(2) Added in POSIX.1-2013
125 \fBfchmodat\fP(2) Added in POSIX.1-2008
127 \fBfchownat\fP(2) Added in POSIX.1-2008
130 \fBfexecve\fP(3) Added in POSIX.1-2008
131 \fBffs\fP(3) Added in POSIX.1-2016
132 \fBfork\fP(2) See notes below
134 \fBfstatat\fP(2) Added in POSIX.1-2008
137 \fBfutimens\fP(3) Added in POSIX.1-2008
149 \fBhtonl\fP(3) Added in POSIX.1-2016
150 \fBhtons\fP(3) Added in POSIX.1-2016
153 \fBlinkat\fP(2) Added in POSIX.1-2008
155 \fBlongjmp\fP(3) Added in POSIX.1-2016; see notes below
158 \fBmemccpy\fP(3) Added in POSIX.1-2016
159 \fBmemchr\fP(3) Added in POSIX.1-2016
160 \fBmemcmp\fP(3) Added in POSIX.1-2016
161 \fBmemcpy\fP(3) Added in POSIX.1-2016
162 \fBmemmove\fP(3) Added in POSIX.1-2016
163 \fBmemset\fP(3) Added in POSIX.1-2016
165 \fBmkdirat\fP(2) Added in POSIX.1-2008
167 \fBmkfifoat\fP(3) Added in POSIX.1-2008
168 \fBmknod\fP(2) Added in POSIX.1-2008
169 \fBmknodat\fP(2) Added in POSIX.1-2008
170 \fBntohl\fP(3) Added in POSIX.1-2016
171 \fBntohs\fP(3) Added in POSIX.1-2016
173 \fBopenat\fP(2) Added in POSIX.1-2008
177 \fBposix_trace_event\fP(3)
179 \fBpthread_kill\fP(3) Added in POSIX.1-2013
180 \fBpthread_self\fP(3) Added in POSIX.1-2013
181 \fBpthread_sigmask\fP(3) Added in POSIX.1-2013
185 \fBreadlinkat\fP(2) Added in POSIX.1-2008
190 \fBrenameat\fP(2) Added in POSIX.1-2008
209 \fBsiglongjmp\fP(3) Added in POSIX.1-2016; see notes below
218 \fBsockatmark\fP(3) Added in POSIX.1-2004
222 \fBstpcpy\fP(3) Added in POSIX.1-2016
223 \fBstpncpy\fP(3) Added in POSIX.1-2016
224 \fBstrcat\fP(3) Added in POSIX.1-2016
225 \fBstrchr\fP(3) Added in POSIX.1-2016
226 \fBstrcmp\fP(3) Added in POSIX.1-2016
227 \fBstrcpy\fP(3) Added in POSIX.1-2016
228 \fBstrcspn\fP(3) Added in POSIX.1-2016
229 \fBstrlen\fP(3) Added in POSIX.1-2016
230 \fBstrncat\fP(3) Added in POSIX.1-2016
231 \fBstrncmp\fP(3) Added in POSIX.1-2016
232 \fBstrncpy\fP(3) Added in POSIX.1-2016
233 \fBstrnlen\fP(3) Added in POSIX.1-2016
234 \fBstrpbrk\fP(3) Added in POSIX.1-2016
235 \fBstrrchr\fP(3) Added in POSIX.1-2016
236 \fBstrspn\fP(3) Added in POSIX.1-2016
237 \fBstrstr\fP(3) Added in POSIX.1-2016
238 \fBstrtok_r\fP(3) Added in POSIX.1-2016
240 \fBsymlinkat\fP(2) Added in POSIX.1-2008
250 \fBtimer_getoverrun\fP(2)
251 \fBtimer_gettime\fP(2)
252 \fBtimer_settime\fP(2)
257 \fBunlinkat\fP(2) Added in POSIX.1-2008
259 \fButimensat\fP(2) Added in POSIX.1-2008
260 \fButimes\fP(2) Added in POSIX.1-2008
263 \fBwcpcpy\fP(3) Added in POSIX.1-2016
264 \fBwcpncpy\fP(3) Added in POSIX.1-2016
265 \fBwcscat\fP(3) Added in POSIX.1-2016
266 \fBwcschr\fP(3) Added in POSIX.1-2016
267 \fBwcscmp\fP(3) Added in POSIX.1-2016
268 \fBwcscpy\fP(3) Added in POSIX.1-2016
269 \fBwcscspn\fP(3) Added in POSIX.1-2016
270 \fBwcslen\fP(3) Added in POSIX.1-2016
271 \fBwcsncat\fP(3) Added in POSIX.1-2016
272 \fBwcsncmp\fP(3) Added in POSIX.1-2016
273 \fBwcsncpy\fP(3) Added in POSIX.1-2016
274 \fBwcsnlen\fP(3) Added in POSIX.1-2016
275 \fBwcspbrk\fP(3) Added in POSIX.1-2016
276 \fBwcsrchr\fP(3) Added in POSIX.1-2016
277 \fBwcsspn\fP(3) Added in POSIX.1-2016
278 \fBwcsstr\fP(3) Added in POSIX.1-2016
279 \fBwcstok\fP(3) Added in POSIX.1-2016
280 \fBwmemchr\fP(3) Added in POSIX.1-2016
281 \fBwmemcmp\fP(3) Added in POSIX.1-2016
282 \fBwmemcpy\fP(3) Added in POSIX.1-2016
283 \fBwmemmove\fP(3) Added in POSIX.1-2016
284 \fBwmemset\fP(3) Added in POSIX.1-2016
290 POSIX.1-2001 and POSIX.1-2004 required the functions
295 to be async-signal-safe, but this requirement was removed in POSIX.1-2008.
297 If a signal handler interrupts the execution of an unsafe function,
298 and the handler terminates via a call to
302 and the program subsequently calls an unsafe function,
303 then the behavior of the program is undefined.
305 POSIX.1-2003 clarified
306 that if an application calls
308 from a signal handler and any of the fork handlers registered by
309 .BR pthread_atfork (3)
310 calls a function that is not async-signal-safe, the behavior is undefined.
311 A future revision of the standard
312 .\" http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt
315 from the list of async-signal-safe functions.
317 .SS Deviations in the GNU C library
318 The following known deviations from the standard occur in
327 internally and were consequently not async-signal-safe.
328 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=19534
329 This was fixed in glibc 2.24.
331 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=13172
332 The glibc implementation of
334 is not async-signal-safe because it uses
335 .BR pthread_mutex_lock (3)