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 2021-03-22 "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.)
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).
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
90 the table details changes in the subsequent standards.
96 \fBabort\fP(3) Added in POSIX.1-2001 TC1
101 \fBaio_suspend\fP(3) See notes below
111 \fBclock_gettime\fP(2)
118 Added in POSIX.1-2008; see notes below
120 \fBexecle\fP(3) See notes below
121 \fBexecv\fP(3) Added in POSIX.1-2008
125 \fBfaccessat\fP(2) Added in POSIX.1-2008
126 \fBfchdir\fP(2) Added in POSIX.1-2008 TC1
128 \fBfchmodat\fP(2) Added in POSIX.1-2008
130 \fBfchownat\fP(2) Added in POSIX.1-2008
133 \fBfexecve\fP(3) Added in POSIX.1-2008
134 \fBffs\fP(3) Added in POSIX.1-2008 TC2
135 \fBfork\fP(2) See notes below
137 \fBfstatat\fP(2) Added in POSIX.1-2008
140 \fBfutimens\fP(3) Added in POSIX.1-2008
152 \fBhtonl\fP(3) Added in POSIX.1-2008 TC2
153 \fBhtons\fP(3) Added in POSIX.1-2008 TC2
156 \fBlinkat\fP(2) Added in POSIX.1-2008
159 Added in POSIX.1-2008 TC2; see notes below
163 \fBmemccpy\fP(3) Added in POSIX.1-2008 TC2
164 \fBmemchr\fP(3) Added in POSIX.1-2008 TC2
165 \fBmemcmp\fP(3) Added in POSIX.1-2008 TC2
166 \fBmemcpy\fP(3) Added in POSIX.1-2008 TC2
167 \fBmemmove\fP(3) Added in POSIX.1-2008 TC2
168 \fBmemset\fP(3) Added in POSIX.1-2008 TC2
170 \fBmkdirat\fP(2) Added in POSIX.1-2008
172 \fBmkfifoat\fP(3) Added in POSIX.1-2008
173 \fBmknod\fP(2) Added in POSIX.1-2008
174 \fBmknodat\fP(2) Added in POSIX.1-2008
175 \fBntohl\fP(3) Added in POSIX.1-2008 TC2
176 \fBntohs\fP(3) Added in POSIX.1-2008 TC2
178 \fBopenat\fP(2) Added in POSIX.1-2008
182 \fBposix_trace_event\fP(3)
184 \fBpthread_kill\fP(3) Added in POSIX.1-2008 TC1
185 \fBpthread_self\fP(3) Added in POSIX.1-2008 TC1
186 \fBpthread_sigmask\fP(3) Added in POSIX.1-2008 TC1
190 \fBreadlinkat\fP(2) Added in POSIX.1-2008
195 \fBrenameat\fP(2) Added in POSIX.1-2008
214 \fBsiglongjmp\fP(3) T{
215 Added in POSIX.1-2008 TC2; see notes below
225 \fBsockatmark\fP(3) Added in POSIX.1-2001 TC2
229 \fBstpcpy\fP(3) Added in POSIX.1-2008 TC2
230 \fBstpncpy\fP(3) Added in POSIX.1-2008 TC2
231 \fBstrcat\fP(3) Added in POSIX.1-2008 TC2
232 \fBstrchr\fP(3) Added in POSIX.1-2008 TC2
233 \fBstrcmp\fP(3) Added in POSIX.1-2008 TC2
234 \fBstrcpy\fP(3) Added in POSIX.1-2008 TC2
235 \fBstrcspn\fP(3) Added in POSIX.1-2008 TC2
236 \fBstrlen\fP(3) Added in POSIX.1-2008 TC2
237 \fBstrncat\fP(3) Added in POSIX.1-2008 TC2
238 \fBstrncmp\fP(3) Added in POSIX.1-2008 TC2
239 \fBstrncpy\fP(3) Added in POSIX.1-2008 TC2
240 \fBstrnlen\fP(3) Added in POSIX.1-2008 TC2
241 \fBstrpbrk\fP(3) Added in POSIX.1-2008 TC2
242 \fBstrrchr\fP(3) Added in POSIX.1-2008 TC2
243 \fBstrspn\fP(3) Added in POSIX.1-2008 TC2
244 \fBstrstr\fP(3) Added in POSIX.1-2008 TC2
245 \fBstrtok_r\fP(3) Added in POSIX.1-2008 TC2
247 \fBsymlinkat\fP(2) Added in POSIX.1-2008
257 \fBtimer_getoverrun\fP(2)
258 \fBtimer_gettime\fP(2)
259 \fBtimer_settime\fP(2)
264 \fBunlinkat\fP(2) Added in POSIX.1-2008
266 \fButimensat\fP(2) Added in POSIX.1-2008
267 \fButimes\fP(2) Added in POSIX.1-2008
270 \fBwcpcpy\fP(3) Added in POSIX.1-2008 TC2
271 \fBwcpncpy\fP(3) Added in POSIX.1-2008 TC2
272 \fBwcscat\fP(3) Added in POSIX.1-2008 TC2
273 \fBwcschr\fP(3) Added in POSIX.1-2008 TC2
274 \fBwcscmp\fP(3) Added in POSIX.1-2008 TC2
275 \fBwcscpy\fP(3) Added in POSIX.1-2008 TC2
276 \fBwcscspn\fP(3) Added in POSIX.1-2008 TC2
277 \fBwcslen\fP(3) Added in POSIX.1-2008 TC2
278 \fBwcsncat\fP(3) Added in POSIX.1-2008 TC2
279 \fBwcsncmp\fP(3) Added in POSIX.1-2008 TC2
280 \fBwcsncpy\fP(3) Added in POSIX.1-2008 TC2
281 \fBwcsnlen\fP(3) Added in POSIX.1-2008 TC2
282 \fBwcspbrk\fP(3) Added in POSIX.1-2008 TC2
283 \fBwcsrchr\fP(3) Added in POSIX.1-2008 TC2
284 \fBwcsspn\fP(3) Added in POSIX.1-2008 TC2
285 \fBwcsstr\fP(3) Added in POSIX.1-2008 TC2
286 \fBwcstok\fP(3) Added in POSIX.1-2008 TC2
287 \fBwmemchr\fP(3) Added in POSIX.1-2008 TC2
288 \fBwmemcmp\fP(3) Added in POSIX.1-2008 TC2
289 \fBwmemcpy\fP(3) Added in POSIX.1-2008 TC2
290 \fBwmemmove\fP(3) Added in POSIX.1-2008 TC2
291 \fBwmemset\fP(3) Added in POSIX.1-2008 TC2
297 POSIX.1-2001 and POSIX.1-2001 TC2 required the functions
302 to be async-signal-safe, but this requirement was removed in POSIX.1-2008.
304 If a signal handler interrupts the execution of an unsafe function,
305 and the handler terminates via a call to
309 and the program subsequently calls an unsafe function,
310 then the behavior of the program is undefined.
312 POSIX.1-2001 TC1 clarified
313 that if an application calls
315 from a signal handler and any of the fork handlers registered by
316 .BR pthread_atfork (3)
317 calls a function that is not async-signal-safe, the behavior is undefined.
318 A future revision of the standard
319 .\" http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt
322 from the list of async-signal-safe functions.
325 Asynchronous signal handlers that call functions which are cancellation
326 points and nest over regions of deferred cancellation may trigger
327 cancellation whose behavior is as if asynchronous cancellation had
328 occurred and may cause application state to become inconsistent.
331 Fetching and setting the value of
333 is async-signal-safe provided that the signal handler saves
335 on entry and restores its value before returning.
337 .SS Deviations in the GNU C library
338 The following known deviations from the standard occur in
347 internally and were consequently not async-signal-safe.
348 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=19534
349 This was fixed in glibc 2.24.
351 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=13172
352 The glibc implementation of
354 is not async-signal-safe because it uses
355 .BR pthread_mutex_lock (3)