render / rpms / libvirt

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