When we write a signal handler that may change the errno, should we save errno at the beginning of the signal handler and restore the errno at the end of it? Just like below:
void signal_handler(int signo){
int temp_errno = errno;
*** //code here may change the errno
errno = temp_errno;
}
The glibc documentation says:
So go ahead and do that.
To those reading this who can't rewrite their signal handlers, there may be a workaround. If you're writing a multi-threaded program using pthreads,
errno
will be in thread-local storage. So you can possibly dedicate one thread to handle a signal and block the signal in all other threads.