]> sourceware.org Git - lvm2.git/commitdiff
cov: daemonize avoid leak of FD on error path
authorZdenek Kabelac <zkabelac@redhat.com>
Thu, 22 Apr 2021 13:05:31 +0000 (15:05 +0200)
committerZdenek Kabelac <zkabelac@redhat.com>
Fri, 23 Apr 2021 21:00:55 +0000 (23:00 +0200)
Use our common patter for reopening FDs to /dev/null
that avoids leaking FD on error path.

libdm/dm-tools/dmfilemapd.c

index e15f0015fdb15616e2972b213b30993de28342e5..2a86810edf968154d9b8b0768384af1d2c837582 100644 (file)
@@ -653,18 +653,21 @@ static int _daemonise(struct filemap_monitor *fm)
        }
 
        if (!_verbose) {
-               if (close(STDIN_FILENO))
-                       _early_log("Error closing stdin");
-               if (close(STDOUT_FILENO))
-                       _early_log("Error closing stdout");
-               if (close(STDERR_FILENO))
-                       _early_log("Error closing stderr");
-               if ((open("/dev/null", O_RDONLY) < 0) ||
-                   (open("/dev/null", O_WRONLY) < 0) ||
-                   (open("/dev/null", O_WRONLY) < 0)) {
-                       _early_log("Error opening stdio streams.");
+               if ((fd = open("/dev/null", O_RDWR)) == -1) {
+                       _early_log("Error opening /dev/null.");
                        return 0;
                }
+
+               if ((dup2(fd, STDIN_FILENO) == -1) ||
+                   (dup2(fd, STDOUT_FILENO) == -1) ||
+                   (dup2(fd, STDERR_FILENO) == -1)) {
+                       if (fd > STDERR_FILENO)
+                               (void) close(fd);
+                       _early_log("Error redirecting stdin/out/err to null.");
+                       return 0;
+               }
+               if (fd > STDERR_FILENO)
+                       (void) close(fd);
        }
        /* TODO: Use libdaemon/server/daemon-server.c _daemonise() */
        for (fd = (int) sysconf(_SC_OPEN_MAX) - 1; fd > STDERR_FILENO; fd--) {
This page took 0.039622 seconds and 5 git commands to generate.