]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Cleanup code in open_bpipe() when closing open files
authorEric Bollengier <eric@baculasystems.com>
Wed, 3 Jul 2024 17:19:50 +0000 (19:19 +0200)
committerEric Bollengier <eric@baculasystems.com>
Wed, 4 Dec 2024 08:14:26 +0000 (09:14 +0100)
Refactor the code that is responsible to close open files.

bacula/src/lib/bpipe.c

index 80de02714d3f6414ca7e8b61dfaac4ce30fba577..f0b2fcaf8280cb7dbb2525062433c8bf6068bb16 100644 (file)
 #include "bacula.h"
 #include "jcr.h"
 
-#ifdef HAVE_GETRLIMIT
-#include <sys/resource.h>
-#else
-/* If not available, use a wrapper that will not use it */
-#define getrlimit(a,b) -1
-#endif
-
 int execvp_errors[] = {
         EACCES,
         ENOEXEC,
@@ -217,20 +210,6 @@ BPIPE *open_bpipe(char *prog, int wait, const char *mode, char *envp[])
       return NULL;
    }
 
-   /* Many systems doesn't have the correct system call
-    * to determine the FD list to close.
-    */
-#if !defined(HAVE_FCNTL_F_CLOSEM) && !defined(HAVE_CLOSEFROM)
-   struct rlimit rl;
-   int64_t rlimitResult=0;
-
-   if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
-      rlimitResult = sysconf(_SC_OPEN_MAX);
-   } else {
-      rlimitResult = rl.rlim_max;
-   }
-#endif
-
    /* Start worker process */
    switch (bpipe->worker_pid = fork()) {
    case -1:                           /* error */
@@ -256,6 +235,7 @@ BPIPE *open_bpipe(char *prog, int wait, const char *mode, char *envp[])
       if (mode_map & MODE_WRITE) {
          close(writep[1]);
          dup2(writep[0], 0);          /* Dup our write to his stdin */
+        close(writep[0]);
       }
       if (mode_map & MODE_READ) {
          close(readp[0]);             /* Close unused child fds */
@@ -275,17 +255,16 @@ BPIPE *open_bpipe(char *prog, int wait, const char *mode, char *envp[])
             /* by default STDERR and STDOUT are merged */
             dup2(readp[1], 2);
          }
+        if (mode_map & MODE_READ) {
+           close(readp[1]);
+        }
+        if (mode_map & MODE_STDERR) {
+           close(errp[1]);
+        }
       }
 
-#if HAVE_FCNTL_F_CLOSEM
-      fcntl(3, F_CLOSEM);
-#elif HAVE_CLOSEFROM
-      closefrom(3);
-#else
-      for (i=rlimitResult; i >= 3; i--) {
-         close(i);
-      }
-#endif
+      /* Close everything after STDERR. Should be pretty fast when the OS implements CLOEXEC */
+      bclose_from(3);
 
       /* Setup the environment if requested, we do not use execvpe()
        * because it's not wildly available