]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
signal: Restore errno on exit from signal handler.
authorAndre Noll <maan@systemlinux.org>
Sat, 9 Feb 2013 18:29:14 +0000 (19:29 +0100)
committerAndre Noll <maan@systemlinux.org>
Sat, 9 Feb 2013 18:29:25 +0000 (19:29 +0100)
This probably is not necessary since generic_signal_handler() calls
exit(3) if the write to the signal pipe fails. However, nobody is
going to stop write(2) from setting errno also on success, so let's
play safe and always restore its value on exit.

signal.c

index b2317c9b3a51fefca4c490681424a2f8709563b2..b8f3bbbc9e1c8fda680edc57591abaf211d17091 100644 (file)
--- a/signal.c
+++ b/signal.c
@@ -51,15 +51,22 @@ err_out:
        exit(EXIT_FAILURE);
 }
 
        exit(EXIT_FAILURE);
 }
 
-/*
- * just write one integer to signal pipe
- */
+/* Write the signal number to signal pipe. */
 static void generic_signal_handler(int s)
 {
 static void generic_signal_handler(int s)
 {
+       /*
+        * Signal handlers that make system calls must save a copy of errno on
+        * entry to the handler and restore it on exit, to prevent the
+        * possibility of overwriting a errno value that had previously been
+        * set in the main program.
+        */
+       int save_errno = errno;
        ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
 
        ssize_t ret = write(signal_pipe[1], &s, sizeof(int));
 
-       if (ret == sizeof(int))
+       if (ret == sizeof(int)) {
+               errno = save_errno;
                return;
                return;
+       }
        if (ret < 0)
                PARA_EMERG_LOG("%s\n", strerror(errno));
        else
        if (ret < 0)
                PARA_EMERG_LOG("%s\n", strerror(errno));
        else