Fixing the errno race

The fix for the previous race is actually quite simple.

Whenever you have a signal handler with code within it that could possibly cause the errno value to change, save errno upon function entry and restore it just before returning from the handler.

Gain access to the errno variable simply by including its header file. Here is a quick example code snippet of a signal handler that does this:

<...>include <errno.h><...>static void handle_sigusr(int signum){    int myerrno = errno;    <... do the handling ...>    <... syscalls, etc ...>    errno = myerror;}

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.