Wiki Markup |
---|
According to \[[ISO/IEC 9899-1999| AA. C References#ISO/IEC 9899-1999]\] (see [undefined behavior 126| CC. Undefined Behavior#ub_126] of Annex J), the behavior a program is [undefined| BB. Definitions#undefined behavior] when |
the value of
errno
is referred to after a signal occurred other than as the result of calling theabort
orraise
function and the corresponding signal handler obtained aSIG_ERR
return from a call to thesignal
function.
A signal handler is allowed to call signal()
, and if that fails, signal()
returns SIG_ERR
and sets errno
to a positive value. However, if the event that caused a signal was external (not the result of the program calling abort()
or raise()
), the only functions the signal handler may call are _Exit()
or abort()
, or call signal()
on the signal currently being handled, and if signal()
fails, the value of errno
is indeterminate.
This rule is a special case of SIG31-C. Do not access or modify shared objects in signal handlers. The object designated by errno
is of static storage duration and is not a volatile sig_atomic_t
. As a result, performing any action that would require errno
to be set would normally cause undefined behavior. The C standard makes a special exception for errno
in this case, saying the only thing that is allowed to go wrong is that errno
takes on an indeterminate value. This makes it possible to call signal()
from within a signal handler without risking unrestricted undefined behavior, but the handler, and any code executed after the handler returns, must not depend on the value of errno
being meaningful.
...