mount.2: SEE ALSO: add mount_setattr(2)
[man-pages.git] / man7 / signal-safety.7
blob9e8cd3fea107c1740f48daaa8ae9d09600aa7b9d
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  2021-03-22 "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-2001 TC1
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)  T{
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
122 \fBexecve\fP(2)
123 \fB_exit\fP(2)
124 \fB_Exit\fP(2)
125 \fBfaccessat\fP(2)      Added in POSIX.1-2008
126 \fBfchdir\fP(2) Added in POSIX.1-2008 TC1
127 \fBfchmod\fP(2)
128 \fBfchmodat\fP(2)       Added in POSIX.1-2008
129 \fBfchown\fP(2)
130 \fBfchownat\fP(2)       Added in POSIX.1-2008
131 \fBfcntl\fP(2)
132 \fBfdatasync\fP(2)
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
136 \fBfstat\fP(2)
137 \fBfstatat\fP(2)        Added in POSIX.1-2008
138 \fBfsync\fP(2)
139 \fBftruncate\fP(2)
140 \fBfutimens\fP(3)       Added in POSIX.1-2008
141 \fBgetegid\fP(2)
142 \fBgeteuid\fP(2)
143 \fBgetgid\fP(2)
144 \fBgetgroups\fP(2)
145 \fBgetpeername\fP(2)
146 \fBgetpgrp\fP(2)
147 \fBgetpid\fP(2)
148 \fBgetppid\fP(2)
149 \fBgetsockname\fP(2)
150 \fBgetsockopt\fP(2)
151 \fBgetuid\fP(2)
152 \fBhtonl\fP(3)  Added in POSIX.1-2008 TC2
153 \fBhtons\fP(3)  Added in POSIX.1-2008 TC2
154 \fBkill\fP(2)
155 \fBlink\fP(2)
156 \fBlinkat\fP(2) Added in POSIX.1-2008
157 \fBlisten\fP(2)
158 \fBlongjmp\fP(3)        T{
159 Added in POSIX.1-2008 TC2; see notes below
161 \fBlseek\fP(2)
162 \fBlstat\fP(2)
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
169 \fBmkdir\fP(2)
170 \fBmkdirat\fP(2)        Added in POSIX.1-2008
171 \fBmkfifo\fP(3)
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
177 \fBopen\fP(2)
178 \fBopenat\fP(2) Added in POSIX.1-2008
179 \fBpause\fP(2)
180 \fBpipe\fP(2)
181 \fBpoll\fP(2)
182 \fBposix_trace_event\fP(3)
183 \fBpselect\fP(2)
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
187 \fBraise\fP(3)
188 \fBread\fP(2)
189 \fBreadlink\fP(2)
190 \fBreadlinkat\fP(2)     Added in POSIX.1-2008
191 \fBrecv\fP(2)
192 \fBrecvfrom\fP(2)
193 \fBrecvmsg\fP(2)
194 \fBrename\fP(2)
195 \fBrenameat\fP(2)       Added in POSIX.1-2008
196 \fBrmdir\fP(2)
197 \fBselect\fP(2)
198 \fBsem_post\fP(3)
199 \fBsend\fP(2)
200 \fBsendmsg\fP(2)
201 \fBsendto\fP(2)
202 \fBsetgid\fP(2)
203 \fBsetpgid\fP(2)
204 \fBsetsid\fP(2)
205 \fBsetsockopt\fP(2)
206 \fBsetuid\fP(2)
207 \fBshutdown\fP(2)
208 \fBsigaction\fP(2)
209 \fBsigaddset\fP(3)
210 \fBsigdelset\fP(3)
211 \fBsigemptyset\fP(3)
212 \fBsigfillset\fP(3)
213 \fBsigismember\fP(3)
214 \fBsiglongjmp\fP(3)     T{
215 Added in POSIX.1-2008 TC2; see notes below
217 \fBsignal\fP(2)
218 \fBsigpause\fP(3)
219 \fBsigpending\fP(2)
220 \fBsigprocmask\fP(2)
221 \fBsigqueue\fP(2)
222 \fBsigset\fP(3)
223 \fBsigsuspend\fP(2)
224 \fBsleep\fP(3)
225 \fBsockatmark\fP(3)     Added in POSIX.1-2001 TC2
226 \fBsocket\fP(2)
227 \fBsocketpair\fP(2)
228 \fBstat\fP(2)
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
246 \fBsymlink\fP(2)
247 \fBsymlinkat\fP(2)      Added in POSIX.1-2008
248 \fBtcdrain\fP(3)
249 \fBtcflow\fP(3)
250 \fBtcflush\fP(3)
251 \fBtcgetattr\fP(3)
252 \fBtcgetpgrp\fP(3)
253 \fBtcsendbreak\fP(3)
254 \fBtcsetattr\fP(3)
255 \fBtcsetpgrp\fP(3)
256 \fBtime\fP(2)
257 \fBtimer_getoverrun\fP(2)
258 \fBtimer_gettime\fP(2)
259 \fBtimer_settime\fP(2)
260 \fBtimes\fP(2)
261 \fBumask\fP(2)
262 \fBuname\fP(2)
263 \fBunlink\fP(2)
264 \fBunlinkat\fP(2)       Added in POSIX.1-2008
265 \fButime\fP(2)
266 \fButimensat\fP(2)      Added in POSIX.1-2008
267 \fButimes\fP(2) Added in POSIX.1-2008
268 \fBwait\fP(2)
269 \fBwaitpid\fP(2)
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
292 \fBwrite\fP(2)
295 Notes:
296 .IP * 3
297 POSIX.1-2001 and POSIX.1-2001 TC2 required the functions
298 .BR fpathconf (3),
299 .BR pathconf (3),
301 .BR sysconf (3)
302 to be async-signal-safe, but this requirement was removed in POSIX.1-2008.
303 .IP *
304 If a signal handler interrupts the execution of an unsafe function,
305 and the handler terminates via a call to
306 .BR longjmp (3)
308 .BR siglongjmp (3)
309 and the program subsequently calls an unsafe function,
310 then the behavior of the program is undefined.
311 .IP *
312 POSIX.1-2001 TC1 clarified
313 that if an application calls
314 .BR fork (2)
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
320 is likely to remove
321 .BR fork (2)
322 from the list of async-signal-safe functions.
324 .IP * 3
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.
330 .SS errno
331 Fetching and setting the value of
332 .I errno
333 is async-signal-safe provided that the signal handler saves
334 .I errno
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
339 the GNU C library:
340 .IP * 3
341 Before glibc 2.24,
342 .BR execl (3)
344 .BR execle (3)
345 employed
346 .BR realloc (3)
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.
350 .IP *
351 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=13172
352 The glibc implementation of
353 .BR aio_suspend (3)
354 is not async-signal-safe because it uses
355 .BR pthread_mutex_lock (3)
356 internally.
357 .SH SEE ALSO
358 .BR sigaction (2),
359 .BR signal (7),
360 .BR standards (7)