signal-safety(7)

๐Ÿ’– NAME ๐Ÿ’–

signal-safety โ€” async-signal-safe functions.

Bestie, this one comes to you straight outta the Miscellaneous Information Manual, and it's genuinely one of the most important lil pages in section 7, no cap. ๐Ÿซถ


๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so. An async-signal-safe function is one that can be safely called from within a signal handler. That's the whole vibe. Many functions are not async-signal-safe. In particular, nonreentrant functions are generally unsafe to call from a signal handler. ๐Ÿ’€

The kinds of issues that render a function unsafe can be quickly understood when one considers the implementation of the stdio library, all of whose functions are not async-signal-safe. And yes, that means every single one. ๐Ÿ™ˆ

When performing buffered I/O on a file, the stdio functions must maintain a statically allocated data buffer along with associated counters and indexes (or pointers) that record the amount of data and the current position in the buffer. So picture this: the main program is in the middle of a call to a stdio function such as printf(3) where the buffer and associated variables have been partially updated. If, at that moment, the program is interrupted by a signal handler that also calls printf(3), then the second call to printf(3) will operate on inconsistent data, with unpredictable results. ๐Ÿงจ Not the slay. Not it.

To avoid problems with unsafe functions, there are two possible choices:

  • (a) Ensure that (1) the signal handler calls only async-signal-safe functions, and (2) the signal handler itself is reentrant with respect to global variables in the main program.
  • (b) Block signal delivery in the main program when calling functions that are unsafe or operating on global data that is also accessed by the signal handler.

Generally, the second choice is difficult in programs of any complexity, so the first choice is taken. Periodt. ๐Ÿ’…

POSIX.1 specifies a set of functions that an implementation must make async-signal-safe. (An implementation may provide safe implementations of additional functions, but this is not required by the standard and other implementations may not provide the same guarantees.) So like, a platform could be extra, but don't count on it. ๐Ÿ™…โ€โ™€๏ธ

In general, a function is async-signal-safe either because it is reentrant or because it is atomic with respect to signals (i.e., its execution can't be interrupted by a signal handler). ๐Ÿ‘‘

The set of functions required to be async-signal-safe by POSIX.1 is shown in the following table. The functions not otherwise noted were required to be async-signal-safe in POSIX.1-2001; the table details changes in the subsequent standards. So the plain ones are day-one icons, and the annotated ones got added later. ๐ŸŽ€

๐ŸŒˆ the list ๐ŸŒˆ

Notes: ๐Ÿ“

  • POSIX.1-2001 and POSIX.1-2001 TC2 required the functions fpathconf(3), pathconf(3), and sysconf(3) to be async-signal-safe, but this requirement was removed in POSIX.1-2008.
  • If a signal handler interrupts the execution of an unsafe function, and the handler terminates via a call to longjmp(3) or siglongjmp(3) and the program subsequently calls an unsafe function, then the behavior of the program is undefined. Like, undefined undefined. Don't do it. ๐Ÿ˜ญ
  • Asynchronous signal handlers that call functions which are cancelation points and nest over regions of deferred cancelation may trigger cancelation whose behavior is as if asynchronous cancelation had occurred and may cause application state to become inconsistent.

๐Ÿ“ errno ๐Ÿ“

Fetching and setting the value of errno is async-signal-safe provided that the signal handler saves errno on entry and restores its value before returning. Save it, restore it, that's the whole ritual. ๐Ÿ™


๐Ÿงš Deviations in the GNU C library ๐Ÿงš

The following known deviations from the standard occur in the GNU C library:


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

sigaction(2), signal(7), standards(7)


and that's the whole page, bestie ๐Ÿ’œ this one shipped in Linux man-pages 6.20, dated 2026-06-05 โ€” go keep your signal handlers reentrant, mwah ๐ŸŽ€๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms