25aafc
From 42f610677fc7d38d687ec0703b4c7552f922b598 Mon Sep 17 00:00:00 2001
25aafc
Message-Id: <42f610677fc7d38d687ec0703b4c7552f922b598@dist-git>
25aafc
From: Michal Privoznik <mprivozn@redhat.com>
25aafc
Date: Wed, 9 Oct 2019 14:27:43 +0200
25aafc
Subject: [PATCH] vircommand: Separate mass FD closing into a function
25aafc
MIME-Version: 1.0
25aafc
Content-Type: text/plain; charset=UTF-8
25aafc
Content-Transfer-Encoding: 8bit
25aafc
25aafc
I will optimize this code a bit in the next commit. But for that
25aafc
it is better if the code lives in a separate function.
25aafc
25aafc
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
25aafc
Reviewed-by: Ján Tomko <jtomko@redhat.com>
25aafc
(cherry picked from commit c1a9bfbbba48fea44fdfbe532e084c5323c9c9b3)
25aafc
25aafc
https://bugzilla.redhat.com/show_bug.cgi?id=1759904
25aafc
25aafc
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
25aafc
Message-Id: <0198a865d008ec9fb80376062049e05522413d3e.1570623892.git.mprivozn@redhat.com>
25aafc
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
25aafc
---
25aafc
 src/util/vircommand.c | 52 ++++++++++++++++++++++++++++---------------
25aafc
 1 file changed, 34 insertions(+), 18 deletions(-)
25aafc
25aafc
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
25aafc
index f539bafab6..02dbe0fc76 100644
25aafc
--- a/src/util/vircommand.c
25aafc
+++ b/src/util/vircommand.c
25aafc
@@ -492,6 +492,37 @@ virExecCommon(virCommandPtr cmd, gid_t *groups, int ngroups)
25aafc
     return ret;
25aafc
 }
25aafc
 
25aafc
+static int
25aafc
+virCommandMassClose(virCommandPtr cmd,
25aafc
+                    int childin,
25aafc
+                    int childout,
25aafc
+                    int childerr)
25aafc
+{
25aafc
+    int openmax = sysconf(_SC_OPEN_MAX);
25aafc
+    int fd;
25aafc
+    int tmpfd;
25aafc
+
25aafc
+    if (openmax < 0) {
25aafc
+        virReportSystemError(errno,  "%s",
25aafc
+                             _("sysconf(_SC_OPEN_MAX) failed"));
25aafc
+        return -1;
25aafc
+    }
25aafc
+
25aafc
+    for (fd = 3; fd < openmax; fd++) {
25aafc
+        if (fd == childin || fd == childout || fd == childerr)
25aafc
+            continue;
25aafc
+        if (!virCommandFDIsSet(cmd, fd)) {
25aafc
+            tmpfd = fd;
25aafc
+            VIR_MASS_CLOSE(tmpfd);
25aafc
+        } else if (virSetInherit(fd, true) < 0) {
25aafc
+            virReportSystemError(errno, _("failed to preserve fd %d"), fd);
25aafc
+            return -1;
25aafc
+        }
25aafc
+    }
25aafc
+
25aafc
+    return 0;
25aafc
+}
25aafc
+
25aafc
 /*
25aafc
  * virExec:
25aafc
  * @cmd virCommandPtr containing all information about the program to
25aafc
@@ -501,13 +532,12 @@ static int
25aafc
 virExec(virCommandPtr cmd)
25aafc
 {
25aafc
     pid_t pid;
25aafc
-    int null = -1, fd, openmax;
25aafc
+    int null = -1;
25aafc
     int pipeout[2] = {-1, -1};
25aafc
     int pipeerr[2] = {-1, -1};
25aafc
     int childin = cmd->infd;
25aafc
     int childout = -1;
25aafc
     int childerr = -1;
25aafc
-    int tmpfd;
25aafc
     char *binarystr = NULL;
25aafc
     const char *binary = NULL;
25aafc
     int ret;
25aafc
@@ -616,23 +646,9 @@ virExec(virCommandPtr cmd)
25aafc
     if (cmd->mask)
25aafc
         umask(cmd->mask);
25aafc
     ret = EXIT_CANCELED;
25aafc
-    openmax = sysconf(_SC_OPEN_MAX);
25aafc
-    if (openmax < 0) {
25aafc
-        virReportSystemError(errno,  "%s",
25aafc
-                             _("sysconf(_SC_OPEN_MAX) failed"));
25aafc
+
25aafc
+    if (virCommandMassClose(cmd, childin, childout, childerr) < 0)
25aafc
         goto fork_error;
25aafc
-    }
25aafc
-    for (fd = 3; fd < openmax; fd++) {
25aafc
-        if (fd == childin || fd == childout || fd == childerr)
25aafc
-            continue;
25aafc
-        if (!virCommandFDIsSet(cmd, fd)) {
25aafc
-            tmpfd = fd;
25aafc
-            VIR_MASS_CLOSE(tmpfd);
25aafc
-        } else if (virSetInherit(fd, true) < 0) {
25aafc
-            virReportSystemError(errno, _("failed to preserve fd %d"), fd);
25aafc
-            goto fork_error;
25aafc
-        }
25aafc
-    }
25aafc
 
25aafc
     if (prepareStdFd(childin, STDIN_FILENO) < 0) {
25aafc
         virReportSystemError(errno,
25aafc
-- 
25aafc
2.23.0
25aafc