]> sourceware.org Git - lvm2.git/commitdiff
deamonize: restore detection of errors
authorZdenek Kabelac <zkabelac@redhat.com>
Wed, 6 Sep 2017 09:47:53 +0000 (11:47 +0200)
committerZdenek Kabelac <zkabelac@redhat.com>
Wed, 6 Sep 2017 09:47:53 +0000 (11:47 +0200)
Keep forked environment for daemon more strick and check even
for nearly impossible to happen errors.

daemons/clvmd/clvmd.c
libdaemon/server/daemon-server.c

index 46f5f4408c10dac98d97e7f9f5d86eb16c89c7df..9dbda89cce1e2d1a4822d0d134fe5b669279776e 100644 (file)
@@ -1111,12 +1111,18 @@ static void be_daemon(int timeout)
        }
 
        /* Detach ourself from the calling environment */
-       (void) dup2(devnull, STDIN_FILENO);
-       (void) dup2(devnull, STDOUT_FILENO);
-       (void) dup2(devnull, STDERR_FILENO);
+       if ((dup2(devnull, STDIN_FILENO) == -1) ||
+           (dup2(devnull, STDOUT_FILENO) == -1) ||
+           (dup2(devnull, STDERR_FILENO) == -1)) {
+               perror("Error setting terminal FDs to /dev/null");
+               log_error("Error setting terminal FDs to /dev/null: %m");
+               exit(5);
+       }
 
-       if (devnull > STDERR_FILENO)
-               (void) close(devnull);
+       if ((devnull > STDERR_FILENO) && close(devnull)) {
+               log_sys_error("close", "/dev/null");
+               exit(7);
+       }
 
        if (chdir("/")) {
                log_error("Error setting current directory to /: %m");
index 0950c2e16481958e50a628ab5448d5636ba4d2ec..2ffdf402d69a4d7ddc2a92d271cd49c9c78832be 100644 (file)
@@ -377,15 +377,22 @@ static void _daemonise(daemon_state s)
                exit(WEXITSTATUS(child_status));
        }
 
-       if (chdir("/"))
+       if (chdir("/")) {
+               perror("Cannot chdir to /");
                exit(1);
+       }
 
-       (void) dup2(fd, STDIN_FILENO);
-       (void) dup2(fd, STDOUT_FILENO);
-       (void) dup2(fd, STDERR_FILENO);
+       if ((dup2(fd, STDIN_FILENO) == -1) ||
+           (dup2(fd, STDOUT_FILENO) == -1) ||
+           (dup2(fd, STDERR_FILENO) == -1)) {
+               perror("Error setting terminal FDs to /dev/null");
+               exit(2);
+       }
 
-       if (fd > STDERR_FILENO)
-               (void) close(fd);
+       if ((fd > STDERR_FILENO) && close(fd)) {
+               perror("Failed to close /dev/null descriptor");
+               exit(3);
+       }
 
        /* Switch to sysconf(_SC_OPEN_MAX) ?? */
        if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
This page took 0.035918 seconds and 5 git commands to generate.