The errno variable depends on the stream or, more precisely, in the streaming environment, is a stream-local value or a value for the stream string, so what is done with errno in this stream will not affect errno in other streams.
The purpose of saving and restoring errno code is to hide any error set by the write() system call in myhandler() . But if write() fails, it can set errno some new value - it will not be zero, but that's all you can say - but the code you request restores the value before calling write() after the call write() , so the fact that a write has occurred is "invisible" in the sense that it does not affect errno for this stream.
The signal handler function itself can be interrupted by signals that are not blocked by the signal mask for the signal to which it is responding. It can also be carried over. Hardware interruptions may also occur, but the code will be very difficult to notice these effects.
On Linux, you can find /usr/include/bits/errno.h errno macro definition (wrapped in more #ifdef code than shown here):
extern int *__errno_location (void) __THROW __attribute__ ((__const__)); # if !defined _LIBC || defined _LIBC_REENTRANT # define errno (*__errno_location ()) # endif
source share