]> granicus.if.org Git - sudo/commitdiff
Before exec, restore state of signal handlers to be the same as
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 22 Nov 2002 18:33:47 +0000 (18:33 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 22 Nov 2002 18:33:47 +0000 (18:33 +0000)
when we were initialy invoked instead of just reseting to SIG_DFL.
Fixes a problem when using sudo with nohup.  Based on a patch from
Paul Markham.

sudo.c

diff --git a/sudo.c b/sudo.c
index 568371bee7e200c3c26dd8cc890fb4b70bfa9370..1ceec693799a0cb53239cace29dd7cbc672b4b2e 100644 (file)
--- a/sudo.c
+++ b/sudo.c
@@ -157,7 +157,7 @@ main(argc, argv, envp)
     int sudo_mode;
     int pwflag;
     char **new_environ;
-    sigaction_t sa;
+    sigaction_t sa, saved_sa_int, saved_sa_quit, saved_sa_tstp, saved_sa_chld;
     extern int printmatches;
     extern char **environ;
 
@@ -181,18 +181,22 @@ main(argc, argv, envp)
     }
 
     /*
-     * Ignore keyboard-generated signals so the user cannot interrupt
-     * us at some point and avoid the logging.
+     * Signal setup:
+     * Ignore keyboard-generated signals so the user cannot interrupt
+     *  us at some point and avoid the logging.
+     *  Install handler to wait for children when they exit.
      */
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = SA_RESTART;
     sa.sa_handler = SIG_IGN;
-    (void) sigaction(SIGINT, &sa, NULL);
-    (void) sigaction(SIGQUIT, &sa, NULL);
-    (void) sigaction(SIGTSTP, &sa, NULL);
+    (void) sigaction(SIGINT, &sa, &saved_sa_int);
+    (void) sigaction(SIGQUIT, &sa, &saved_sa_quit);
+    (void) sigaction(SIGTSTP, &sa, &saved_sa_tstp);
+    sa.sa_handler = reapchild;
+    (void) sigaction(SIGCHLD, &sa, &saved_sa_chld);
 
     /*
-     * Setup signal handlers, turn off core dumps, and close open files.
+     * Turn off core dumps, close open files and setup set_perms().
      */
     initial_setup();
     setpwent();
@@ -379,14 +383,6 @@ main(argc, argv, envp)
                "please report this error at http://courtesan.com/sudo/bugs/");
        }
 
-       /* Reset signal handlers before we exec. */
-       sigemptyset(&sa.sa_mask);
-       sa.sa_flags = SA_RESTART;
-       sa.sa_handler = SIG_DFL;
-       (void) sigaction(SIGINT, &sa, NULL);
-       (void) sigaction(SIGQUIT, &sa, NULL);
-       (void) sigaction(SIGTSTP, &sa, NULL);
-
        /* Override user's umask if configured to do so. */
        if (def_ival(I_UMASK) != 0777)
            (void) umask(def_mode(I_UMASK));
@@ -406,6 +402,12 @@ main(argc, argv, envp)
        /* Install the new environment. */
        environ = new_environ;
 
+       /* Restore signal handlers before we exec. */
+       (void) sigaction(SIGINT, &saved_sa_int, NULL);
+       (void) sigaction(SIGQUIT, &saved_sa_quit, NULL);
+       (void) sigaction(SIGTSTP, &saved_sa_tstp, NULL);
+       (void) sigaction(SIGCHLD, &saved_sa_chld, NULL);
+
 #ifndef PROFILING
        if ((sudo_mode & MODE_BACKGROUND) && fork() > 0)
            exit(0);
@@ -871,7 +873,6 @@ initial_setup()
 #ifdef HAVE_SETRLIMIT
     struct rlimit rl;
 #endif
-    sigaction_t sa;
 
 #if defined(RLIMIT_CORE) && !defined(SUDO_DEVEL)
     /*
@@ -900,12 +901,6 @@ initial_setup()
     for (fd = maxfd; fd > STDERR_FILENO; fd--)
        (void) close(fd);
 
-    /* Catch children as they die... */
-    sigemptyset(&sa.sa_mask);
-    sa.sa_flags = SA_RESTART;
-    sa.sa_handler = reapchild;
-    (void) sigaction(SIGCHLD, &sa, NULL);
-
     /* Set set_perms pointer to the correct function */
 #if !defined(NO_SAVED_IDS) && defined(_SC_SAVED_IDS) && defined(_SC_VERSION)
     if (sysconf(_SC_SAVED_IDS) == 1 && sysconf(_SC_VERSION) >= 199009)