]> xenbits.xensource.com Git - libvirt.git/commitdiff
vircommand: Isolate FD dir parsing into a separate function
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 29 Aug 2023 06:48:56 +0000 (08:48 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 13 Sep 2024 12:50:43 +0000 (14:50 +0200)
So far, virCommandMassCloseGetFDsLinux() opens "/proc/self/fd",
iterates over it marking opened FDs in @fds bitmap. Well, we can
do the same on other systems (with altered path), like MacOS or
FreeBSD. Therefore, isolate dir iteration into a separate
function that accepts dir path as an argument.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/util/vircommand.c

index 8bd5e5e7711a5d192e767444fa52f5df390e08e4..5526499b4357dbb472907892d5d965be3140f69d 100644 (file)
@@ -473,16 +473,12 @@ virExecCommon(virCommand *cmd, gid_t *groups, int ngroups)
 }
 
 # ifdef __linux__
-/* On Linux, we can utilize procfs and read the table of opened
- * FDs and selectively close only those FDs we don't want to pass
- * onto child process (well, the one we will exec soon since this
- * is called from the child). */
 static int
-virCommandMassCloseGetFDsLinux(virBitmap *fds)
+virCommandMassCloseGetFDsDir(virBitmap *fds,
+                             const char *dirName)
 {
     g_autoptr(DIR) dp = NULL;
     struct dirent *entry;
-    const char *dirName = "/proc/self/fd";
     int rc;
 
     if (virDirOpen(&dp, dirName) < 0)
@@ -506,16 +502,22 @@ virCommandMassCloseGetFDsLinux(virBitmap *fds)
 
     return 0;
 }
-
-# else /* !__linux__ */
+# endif /* __linux__ */
 
 static int
-virCommandMassCloseGetFDsGeneric(virBitmap *fds)
+virCommandMassCloseGetFDs(virBitmap *fds)
 {
+# ifdef __linux__
+    /* On Linux, we can utilize procfs and read the table of opened
+     * FDs and selectively close only those FDs we don't want to pass
+     * onto child process (well, the one we will exec soon since this
+     * is called from the child). */
+    return virCommandMassCloseGetFDsDir(fds, "/proc/self/fd");
+# else
     virBitmapSetAll(fds);
     return 0;
+# endif
 }
-# endif /* !__linux__ */
 
 static int
 virCommandMassCloseFrom(virCommand *cmd,
@@ -544,13 +546,8 @@ virCommandMassCloseFrom(virCommand *cmd,
 
     fds = virBitmapNew(openmax);
 
-# ifdef __linux__
-    if (virCommandMassCloseGetFDsLinux(fds) < 0)
-        return -1;
-# else
-    if (virCommandMassCloseGetFDsGeneric(fds) < 0)
+    if (virCommandMassCloseGetFDs(fds) < 0)
         return -1;
-# endif
 
     lastfd = MAX(lastfd, childin);
     lastfd = MAX(lastfd, childout);