text
stringlengths 0
234
|
---|
.\" have taken the same level of care in the production of this manual, |
.\" which is licensed free of charge, as they might when working |
.\" professionally. |
.\" |
.\" formatted or processed versions of this manual, if unaccompanied by |
.\" the source, must acknowledge the copyright and authors of this work. |
.\" %%%license_end |
.\" |
.th signal-safety 7 2021-03-22 "linux" "linux programmer's manual" |
.sh name |
signal-safety \- async-signal-safe functions |
.sh description |
an |
.i async-signal-safe |
function is one that can be safely called from within a signal handler. |
many functions are |
.i not |
async-signal-safe. |
in particular, |
nonreentrant functions are generally unsafe to call from a signal handler. |
.pp |
the kinds of issues that render a function |
unsafe can be quickly understood when one considers |
the implementation of the |
.i stdio |
library, all of whose functions are not async-signal-safe. |
.pp |
when performing buffered i/o on a file, the |
.i 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 |
.i stdio |
function such as |
.br 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 |
.br printf (3), |
then the second call to |
.br printf (3) |
will operate on inconsistent data, with unpredictable results. |
.pp |
to avoid problems with unsafe functions, there are two possible choices: |
.ip 1. 3 |
ensure that |
(a) the signal handler calls only async-signal-safe functions, |
and |
(b) the signal handler itself is reentrant |
with respect to global variables in the main program. |
.ip 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. |
.pp |
generally, the second choice is difficult in programs of any complexity, |
so the first choice is taken. |
.pp |
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.) |
.pp |
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). |
.pp |
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. |
.pp |
.ts |
lb lb |
l l. |
function notes |
\fbabort\fp(3) added in posix.1-2001 tc1 |
\fbaccept\fp(2) |
\fbaccess\fp(2) |
\fbaio_error\fp(3) |
\fbaio_return\fp(3) |
\fbaio_suspend\fp(3) see notes below |
\fbalarm\fp(2) |
\fbbind\fp(2) |
\fbcfgetispeed\fp(3) |
\fbcfgetospeed\fp(3) |
\fbcfsetispeed\fp(3) |
\fbcfsetospeed\fp(3) |
\fbchdir\fp(2) |
\fbchmod\fp(2) |
\fbchown\fp(2) |
\fbclock_gettime\fp(2) |
\fbclose\fp(2) |
\fbconnect\fp(2) |
\fbcreat\fp(2) |
\fbdup\fp(2) |
\fbdup2\fp(2) |
Subsets and Splits