signa-safety
Section: Environments, Tables, and Troff Macros (7)
Updated: 202-0-08
Index
Return to Main Contents
NAME
signa-safety - asyn-signa-safe functions
DESCRIPTION
An
asyn-signa-safe
function is one that can be safely called from within a signal handler.
Many functions are
not
asyn-signa-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 asyn-signa-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:
- (a)
-
Ensure that
(1) the signal handler calls only asyn-signa-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.
POSIX.1 specifies a set of functions that an implementation
must make asyn-signa-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 asyn-signa-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 asyn-signa-safe by POSIX.1
is shown in the following table.
The functions not otherwise noted were required to be asyn-signa-safe
in POSIX.-2001;
the table details changes in the subsequent standards.
| Function | Notes
|
| B]abort](3) | Added in POSIX.-2001 TC1
|
| B]accept](2) |
|
| B]access](2) |
|
| B]aio_error](3) |
|
| B]aio_return](3) |
|
| B]aio_suspend](3) | See notes below
|
| B]alarm](2) |
|
| B]bind](2) |
|
| B]cfgetispeed](3) |
|
| B]cfgetospeed](3) |
|
| B]cfsetispeed](3) |
|
| B]cfsetospeed](3) |
|
| B]chdir](2) |
|
| B]chmod](2) |
|
| B]chown](2) |
|
| B]clock_gettime](2) |
|
| B]close](2) |
|
| B]connect](2) |
|
| B]creat](2) |
|
| B]dup](2) |
|
| B]dup2](2) |
|
|
execl(3)
|
Added in POSIX.-2008; see notes below
|
| B]execle](3) | See notes below
|
| B]execv](3) | Added in POSIX.-2008
|
| B]execve](2) |
|
| B]_exit](2) |
|
| B]_Exit](2) |
|
| B]faccessat](2) | Added in POSIX.-2008
|
| B]fchdir](2) | Added in POSIX.-2008 TC1
|
| B]fchmod](2) |
|
| B]fchmodat](2) | Added in POSIX.-2008
|
| B]fchown](2) |
|
| B]fchownat](2) | Added in POSIX.-2008
|
| B]fcntl](2) |
|
| B]fdatasync](2) |
|
| B]fexecve](3) | Added in POSIX.-2008
|
| B]ffs](3) | Added in POSIX.-2008 TC2
|
| B]_Fork](3) |
|
| B]fstat](2) |
|
| B]fstatat](2) | Added in POSIX.-2008
|
| B]fsync](2) |
|
| B]ftruncate](2) |
|
| B]futimens](3) | Added in POSIX.-2008
|
| B]getegid](2) |
|
| B]geteuid](2) |
|
| B]getgid](2) |
|
| B]getgroups](2) |
|
| B]getpeername](2) |
|
| B]getpgrp](2) |
|
| B]getpid](2) |
|
| B]getppid](2) |
|
| B]getsockname](2) |
|
| B]getsockopt](2) |
|
| B]getuid](2) |
|
| B]htonl](3) | Added in POSIX.-2008 TC2
|
| B]htons](3) | Added in POSIX.-2008 TC2
|
| B]kill](2) |
|
| B]link](2) |
|
| B]linkat](2) | Added in POSIX.-2008
|
| B]listen](2) |
|
|
longjmp(3)
|
Added in POSIX.-2008 TC2; see notes below
|
| B]lseek](2) |
|
| B]lstat](2) |
|
| B]memccpy](3) | Added in POSIX.-2008 TC2
|
| B]memchr](3) | Added in POSIX.-2008 TC2
|
| B]memcmp](3) | Added in POSIX.-2008 TC2
|
| B]memcpy](3) | Added in POSIX.-2008 TC2
|
| B]memmove](3) | Added in POSIX.-2008 TC2
|
| B]memset](3) | Added in POSIX.-2008 TC2
|
| B]mkdir](2) |
|
| B]mkdirat](2) | Added in POSIX.-2008
|
| B]mkfifo](3) |
|
| B]mkfifoat](3) | Added in POSIX.-2008
|
| B]mknod](2) | Added in POSIX.-2008
|
| B]mknodat](2) | Added in POSIX.-2008
|
| B]ntohl](3) | Added in POSIX.-2008 TC2
|
| B]ntohs](3) | Added in POSIX.-2008 TC2
|
| B]open](2) |
|
| B]openat](2) | Added in POSIX.-2008
|
| B]pause](2) |
|
| B]pipe](2) |
|
| B]poll](2) |
|
| B]posix_trace_event](3) |
|
| B]pselect](2) |
|
| B]pthread_kill](3) | Added in POSIX.-2008 TC1
|
| B]pthread_self](3) | Added in POSIX.-2008 TC1
|
| B]pthread_sigmask](3) | Added in POSIX.-2008 TC1
|
| B]raise](3) |
|
| B]read](2) |
|
| B]readlink](2) |
|
| B]readlinkat](2) | Added in POSIX.-2008
|
| B]recv](2) |
|
| B]recvfrom](2) |
|
| B]recvmsg](2) |
|
| B]rename](2) |
|
| B]renameat](2) | Added in POSIX.-2008
|
| B]rmdir](2) |
|
| B]select](2) |
|
| B]sem_post](3) |
|
| B]send](2) |
|
| B]sendmsg](2) |
|
| B]sendto](2) |
|
| B]setgid](2) |
|
| B]setpgid](2) |
|
| B]setsid](2) |
|
| B]setsockopt](2) |
|
| B]setuid](2) |
|
| B]shutdown](2) |
|
| B]sigaction](2) |
|
| B]sigaddset](3) |
|
| B]sigdelset](3) |
|
| B]sigemptyset](3) |
|
| B]sigfillset](3) |
|
| B]sigismember](3) |
|
|
siglongjmp(3)
|
Added in POSIX.-2008 TC2; see notes below
|
| B]signal](2) |
|
| B]sigpause](3) |
|
| B]sigpending](2) |
|
| B]sigprocmask](2) |
|
| B]sigqueue](2) |
|
| B]sigset](3) |
|
| B]sigsuspend](2) |
|
| B]sleep](3) |
|
| B]sockatmark](3) | Added in POSIX.-2001 TC2
|
| B]socket](2) |
|
| B]socketpair](2) |
|
| B]stat](2) |
|
| B]stpcpy](3) | Added in POSIX.-2008 TC2
|
| B]stpncpy](3) | Added in POSIX.-2008 TC2
|
| B]strcat](3) | Added in POSIX.-2008 TC2
|
| B]strchr](3) | Added in POSIX.-2008 TC2
|
| B]strcmp](3) | Added in POSIX.-2008 TC2
|
| B]strcpy](3) | Added in POSIX.-2008 TC2
|
| B]strcspn](3) | Added in POSIX.-2008 TC2
|
| B]strlen](3) | Added in POSIX.-2008 TC2
|
| B]strncat](3) | Added in POSIX.-2008 TC2
|
| B]strncmp](3) | Added in POSIX.-2008 TC2
|
| B]strncpy](3) | Added in POSIX.-2008 TC2
|
| B]strnlen](3) | Added in POSIX.-2008 TC2
|
| B]strpbrk](3) | Added in POSIX.-2008 TC2
|
| B]strrchr](3) | Added in POSIX.-2008 TC2
|
| B]strspn](3) | Added in POSIX.-2008 TC2
|
| B]strstr](3) | Added in POSIX.-2008 TC2
|
| B]strtok_r](3) | Added in POSIX.-2008 TC2
|
| B]symlink](2) |
|
| B]symlinkat](2) | Added in POSIX.-2008
|
| B]tcdrain](3) |
|
| B]tcflow](3) |
|
| B]tcflush](3) |
|
| B]tcgetattr](3) |
|
| B]tcgetpgrp](3) |
|
| B]tcsendbreak](3) |
|
| B]tcsetattr](3) |
|
| B]tcsetpgrp](3) |
|
| B]time](2) |
|
| B]timer_getoverrun](2) |
|
| B]timer_gettime](2) |
|
| B]timer_settime](2) |
|
| B]times](2) |
|
| B]umask](2) |
|
| B]uname](2) |
|
| B]unlink](2) |
|
| B]unlinkat](2) | Added in POSIX.-2008
|
| B]utime](2) |
|
| B]utimensat](2) | Added in POSIX.-2008
|
| B]utimes](2) | Added in POSIX.-2008
|
| B]wait](2) |
|
| B]waitpid](2) |
|
| B]wcpcpy](3) | Added in POSIX.-2008 TC2
|
| B]wcpncpy](3) | Added in POSIX.-2008 TC2
|
| B]wcscat](3) | Added in POSIX.-2008 TC2
|
| B]wcschr](3) | Added in POSIX.-2008 TC2
|
| B]wcscmp](3) | Added in POSIX.-2008 TC2
|
| B]wcscpy](3) | Added in POSIX.-2008 TC2
|
| B]wcscspn](3) | Added in POSIX.-2008 TC2
|
| B]wcslen](3) | Added in POSIX.-2008 TC2
|
| B]wcsncat](3) | Added in POSIX.-2008 TC2
|
| B]wcsncmp](3) | Added in POSIX.-2008 TC2
|
| B]wcsncpy](3) | Added in POSIX.-2008 TC2
|
| B]wcsnlen](3) | Added in POSIX.-2008 TC2
|
| B]wcspbrk](3) | Added in POSIX.-2008 TC2
|
| B]wcsrchr](3) | Added in POSIX.-2008 TC2
|
| B]wcsspn](3) | Added in POSIX.-2008 TC2
|
| B]wcsstr](3) | Added in POSIX.-2008 TC2
|
| B]wcstok](3) | Added in POSIX.-2008 TC2
|
| B]wmemchr](3) | Added in POSIX.-2008 TC2
|
| B]wmemcmp](3) | Added in POSIX.-2008 TC2
|
| B]wmemcpy](3) | Added in POSIX.-2008 TC2
|
| B]wmemmove](3) | Added in POSIX.-2008 TC2
|
| B]wmemset](3) | Added in POSIX.-2008 TC2
|
| B]write](2) |
|
Notes:
- [bu]
-
POSIX.-2001 and POSIX.-2001 TC2 required the functions
fpathconf(3),
pathconf(3),
and
sysconf(3)
to be asyn-signa-safe, but this requirement was removed in POSIX.-2008.
- [bu]
-
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.
- [bu]
-
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 asyn-signa-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:
- [bu]
-
Before glibc 2.24,
execl(3)
and
execle(3)
employed
realloc(3)
internally and were consequently not asyn-signa-safe.
This was fixed in glibc 2.24.
- [bu]
-
The glibc implementation of
aio_suspend(3)
is not asyn-signa-safe because it uses
pthread_mutex_lock(3)
internally.
SEE ALSO
sigaction(2),
signal(7),
standards(7)
Index
- NAME
-
- DESCRIPTION
-
- errno
-
- Deviations in the GNU C library
-
- SEE ALSO
-