Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interrupt handlers should care about errno #802

Open
franzhollerer opened this issue Mar 23, 2020 · 0 comments
Open

Interrupt handlers should care about errno #802

franzhollerer opened this issue Mar 23, 2020 · 0 comments

Comments

@franzhollerer
Copy link
Contributor

When using signal handlers it is essential to care that called functions are async-signal-safe. Beside that, if the called function may update errno, the signal handler should save and restore the value of errno.

To cite "The Linux Programming Interface" by Michael Kerrisk, ISBN-13: 978-1-59327-220-3, Chapter 21, page 427:

Use of errno inside signal handlers

Because they may update errno, use of function listed in Table 21-1 can never-
theless render a signal handler nonreentrant, since they may overwrite the errno
value that was set by a function called from the main program. The workaround is
to save the value of errno on entry to a signal handler that uses any of the function
in Table 21-1 and restore the errno value on exit from the handler, as in the following-
ing example:

void
handler(int sig)
{
        int savedErrno;

        savedErrno = errno;

        /* Now we can execute a function that might modify errno */

        errno = savedErrno;
}

The referenced Table 21-1 is a list of async-signal-safe functions as specified by POSIX.

This problem affects (probably among others) the example interrupt.c:

static void s_signal_handler (int signal_value)
{
    int rc = write (s_fd, S_NOTIFY_MSG, sizeof(S_NOTIFY_MSG));
    if (rc != sizeof(S_NOTIFY_MSG)) {
        write (STDOUT_FILENO, S_ERROR_MSG, sizeof(S_ERROR_MSG)-1);
        exit(1);
    }
}

write() may update errno. Thus the signal handler should save and restore the errno value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant