NAME

signal-safety - async-signal-safe functions

DESCRIPTION

An async-signal-safe function is one that can be safely called from within a signal handler. 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.

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. Suppose that 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.

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

  1. 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.

  2. 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.

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.)

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.

Function Notes
fBabortfP(3) Added in POSIX.1-2001 TC1
fBacceptfP(2)
fBaccessfP(2)
fBaio_errorfP(3)
fBaio_returnfP(3)
fBaio_suspendfP(3) See notes below
fBalarmfP(2)
fBbindfP(2)
fBcfgetispeedfP(3)
fBcfgetospeedfP(3)
fBcfsetispeedfP(3)
fBcfsetospeedfP(3)
fBchdirfP(2)
fBchmodfP(2)
fBchownfP(2)
fBclock_gettimefP(2)
fBclosefP(2)
fBconnectfP(2)
fBcreatfP(2)
fBdupfP(2)
fBdup2fP(2)
fBexeclfP(3) Added in POSIX.1-2008; see notes below
fBexeclefP(3) See notes below
fBexecvfP(3) Added in POSIX.1-2008
fBexecvefP(2)
fB_exitfP(2)
fB_ExitfP(2)
fBfaccessatfP(2) Added in POSIX.1-2008
fBfchdirfP(2) Added in POSIX.1-2008 TC1
fBfchmodfP(2)
fBfchmodatfP(2) Added in POSIX.1-2008
fBfchownfP(2)
fBfchownatfP(2) Added in POSIX.1-2008
fBfcntlfP(2)
fBfdatasyncfP(2)
fBfexecvefP(3) Added in POSIX.1-2008
fBffsfP(3) Added in POSIX.1-2008 TC2
fBforkfP(2) See notes below
fBfstatfP(2)
fBfstatatfP(2) Added in POSIX.1-2008
fBfsyncfP(2)
fBftruncatefP(2)
fBfutimensfP(3) Added in POSIX.1-2008
fBgetegidfP(2)
fBgeteuidfP(2)
fBgetgidfP(2)
fBgetgroupsfP(2)
fBgetpeernamefP(2)
fBgetpgrpfP(2)
fBgetpidfP(2)
fBgetppidfP(2)
fBgetsocknamefP(2)
fBgetsockoptfP(2)
fBgetuidfP(2)
fBhtonlfP(3) Added in POSIX.1-2008 TC2
fBhtonsfP(3) Added in POSIX.1-2008 TC2
fBkillfP(2)
fBlinkfP(2)
fBlinkatfP(2) Added in POSIX.1-2008
fBlistenfP(2)
fBlongjmpfP(3) Added in POSIX.1-2008 TC2; see notes below
fBlseekfP(2)
fBlstatfP(2)
fBmemccpyfP(3) Added in POSIX.1-2008 TC2
fBmemchrfP(3) Added in POSIX.1-2008 TC2
fBmemcmpfP(3) Added in POSIX.1-2008 TC2
fBmemcpyfP(3) Added in POSIX.1-2008 TC2
fBmemmovefP(3) Added in POSIX.1-2008 TC2
fBmemsetfP(3) Added in POSIX.1-2008 TC2
fBmkdirfP(2)
fBmkdiratfP(2) Added in POSIX.1-2008
fBmkfifofP(3)
fBmkfifoatfP(3) Added in POSIX.1-2008
fBmknodfP(2) Added in POSIX.1-2008
fBmknodatfP(2) Added in POSIX.1-2008
fBntohlfP(3) Added in POSIX.1-2008 TC2
fBntohsfP(3) Added in POSIX.1-2008 TC2
fBopenfP(2)
fBopenatfP(2) Added in POSIX.1-2008
fBpausefP(2)
fBpipefP(2)
fBpollfP(2)
fBposix_trace_eventfP(3)
fBpselectfP(2)
fBpthread_killfP(3) Added in POSIX.1-2008 TC1
fBpthread_selffP(3) Added in POSIX.1-2008 TC1
fBpthread_sigmaskfP(3) Added in POSIX.1-2008 TC1
fBraisefP(3)
fBreadfP(2)
fBreadlinkfP(2)
fBreadlinkatfP(2) Added in POSIX.1-2008
fBrecvfP(2)
fBrecvfromfP(2)
fBrecvmsgfP(2)
fBrenamefP(2)
fBrenameatfP(2) Added in POSIX.1-2008
fBrmdirfP(2)
fBselectfP(2)
fBsem_postfP(3)
fBsendfP(2)
fBsendmsgfP(2)
fBsendtofP(2)
fBsetgidfP(2)
fBsetpgidfP(2)
fBsetsidfP(2)
fBsetsockoptfP(2)
fBsetuidfP(2)
fBshutdownfP(2)
fBsigactionfP(2)
fBsigaddsetfP(3)
fBsigdelsetfP(3)
fBsigemptysetfP(3)
fBsigfillsetfP(3)
fBsigismemberfP(3)
fBsiglongjmpfP(3) Added in POSIX.1-2008 TC2; see notes below
fBsignalfP(2)
fBsigpausefP(3)
fBsigpendingfP(2)
fBsigprocmaskfP(2)
fBsigqueuefP(2)
fBsigsetfP(3)
fBsigsuspendfP(2)
fBsleepfP(3)
fBsockatmarkfP(3) Added in POSIX.1-2001 TC2
fBsocketfP(2)
fBsocketpairfP(2)
fBstatfP(2)
fBstpcpyfP(3) Added in POSIX.1-2008 TC2
fBstpncpyfP(3) Added in POSIX.1-2008 TC2
fBstrcatfP(3) Added in POSIX.1-2008 TC2
fBstrchrfP(3) Added in POSIX.1-2008 TC2
fBstrcmpfP(3) Added in POSIX.1-2008 TC2
fBstrcpyfP(3) Added in POSIX.1-2008 TC2
fBstrcspnfP(3) Added in POSIX.1-2008 TC2
fBstrlenfP(3) Added in POSIX.1-2008 TC2
fBstrncatfP(3) Added in POSIX.1-2008 TC2
fBstrncmpfP(3) Added in POSIX.1-2008 TC2
fBstrncpyfP(3) Added in POSIX.1-2008 TC2
fBstrnlenfP(3) Added in POSIX.1-2008 TC2
fBstrpbrkfP(3) Added in POSIX.1-2008 TC2
fBstrrchrfP(3) Added in POSIX.1-2008 TC2
fBstrspnfP(3) Added in POSIX.1-2008 TC2
fBstrstrfP(3) Added in POSIX.1-2008 TC2
fBstrtok_rfP(3) Added in POSIX.1-2008 TC2
fBsymlinkfP(2)
fBsymlinkatfP(2) Added in POSIX.1-2008
fBtcdrainfP(3)
fBtcflowfP(3)
fBtcflushfP(3)
fBtcgetattrfP(3)
fBtcgetpgrpfP(3)
fBtcsendbreakfP(3)
fBtcsetattrfP(3)
fBtcsetpgrpfP(3)
fBtimefP(2)
fBtimer_getoverrunfP(2)
fBtimer_gettimefP(2)
fBtimer_settimefP(2)
fBtimesfP(2)
fBumaskfP(2)
fBunamefP(2)
fBunlinkfP(2)
fBunlinkatfP(2) Added in POSIX.1-2008
fButimefP(2)
fButimensatfP(2) Added in POSIX.1-2008
fButimesfP(2) Added in POSIX.1-2008
fBwaitfP(2)
fBwaitpidfP(2)
fBwcpcpyfP(3) Added in POSIX.1-2008 TC2
fBwcpncpyfP(3) Added in POSIX.1-2008 TC2
fBwcscatfP(3) Added in POSIX.1-2008 TC2
fBwcschrfP(3) Added in POSIX.1-2008 TC2
fBwcscmpfP(3) Added in POSIX.1-2008 TC2
fBwcscpyfP(3) Added in POSIX.1-2008 TC2
fBwcscspnfP(3) Added in POSIX.1-2008 TC2
fBwcslenfP(3) Added in POSIX.1-2008 TC2
fBwcsncatfP(3) Added in POSIX.1-2008 TC2
fBwcsncmpfP(3) Added in POSIX.1-2008 TC2
fBwcsncpyfP(3) Added in POSIX.1-2008 TC2
fBwcsnlenfP(3) Added in POSIX.1-2008 TC2
fBwcspbrkfP(3) Added in POSIX.1-2008 TC2
fBwcsrchrfP(3) Added in POSIX.1-2008 TC2
fBwcsspnfP(3) Added in POSIX.1-2008 TC2
fBwcsstrfP(3) Added in POSIX.1-2008 TC2
fBwcstokfP(3) Added in POSIX.1-2008 TC2
fBwmemchrfP(3) Added in POSIX.1-2008 TC2
fBwmemcmpfP(3) Added in POSIX.1-2008 TC2
fBwmemcpyfP(3) Added in POSIX.1-2008 TC2
fBwmemmovefP(3) Added in POSIX.1-2008 TC2
fBwmemsetfP(3) Added in POSIX.1-2008 TC2
fBwritefP(2)

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.

  • POSIX.1-2001 TC1 clarified that if an application calls fork(2) from a signal handler and any of the fork handlers registered by pthread_atfork(3) calls a function that is not async-signal-safe, the behavior is undefined. A future revision of the standard is likely to remove fork(2) from the list of async-signal-safe functions.

  • 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.

Deviations in the GNU C library

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

  • Before glibc 2.24, execl(3) and execle(3) employed realloc(3) internally and were consequently not async-signal-safe. This was fixed in glibc 2.24.

  • The glibc implementation of aio_suspend(3) is not async-signal-safe because it uses pthread_mutex_lock(3) internally.

SEE ALSO

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