]> sourceware.org Git - lvm2.git/commitdiff
dmeventd: use standard reopening mechanism 1269915451
authorZdenek Kabelac <zkabelac@redhat.com>
Fri, 26 Apr 2024 21:53:29 +0000 (23:53 +0200)
committerZdenek Kabelac <zkabelac@redhat.com>
Fri, 26 Apr 2024 22:36:24 +0000 (00:36 +0200)
Apply same code for reopening /dev/null for 0,1,2 when daemonizing.

daemons/dmeventd/dmeventd.c

index e3e05331ee8b7368fbb5e7d4d219cc4eabd04972..f2d6ff3934daf99f5f94a30aaf38797c143597b3 100644 (file)
@@ -1943,7 +1943,7 @@ static void _remove_files_on_exit(void)
 
 static void _daemonize(void)
 {
-       int child_status;
+       int child_status, null_fd;
        pid_t pid;
        struct timeval tval;
        sigset_t my_sigset;
@@ -1998,13 +1998,20 @@ static void _daemonize(void)
 
        daemon_close_stray_fds("dmeventd", 0, -1, &custom_fds);
 
-       /* coverity[leaked_handle] dont't care */
-       if ((open("/dev/null", O_RDONLY) < 0) ||
-           (open("/dev/null", O_WRONLY) < 0) ||
-           (open("/dev/null", O_WRONLY) < 0))
+       if ((null_fd = open("/dev/null", O_RDWR)) < 0)
                exit(EXIT_DESC_OPEN_FAILURE);
 
+       if ((dup2(null_fd, STDIN_FILENO) == -1) ||
+           (dup2(null_fd, STDOUT_FILENO) == -1) ||
+           (dup2(null_fd, STDERR_FILENO) == -1))
+               exit(EXIT_DESC_OPEN_FAILURE);
+
+       if ((null_fd > STDERR_FILENO) && close(null_fd))
+               exit(EXIT_DESC_CLOSE_FAILURE);
+
        setsid();
+
+       /* coverity[leaked_handle] 'null_fd' handle is not leaking */
 }
 
 static int _reinstate_registrations(struct dm_event_fifos *fifos)
This page took 0.034556 seconds and 5 git commands to generate.