render / rpms / libvirt

Forked from rpms/libvirt 4 months ago
Clone
c480ed
From 0f27acb41229aa29d539f1975c36c78f75fdf11c Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <0f27acb41229aa29d539f1975c36c78f75fdf11c@dist-git>
c480ed
From: Michal Privoznik <mprivozn@redhat.com>
c480ed
Date: Tue, 30 Jul 2019 15:30:53 +0200
c480ed
Subject: [PATCH] vircommand: Separate mass FD closing into a function
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
I will optimize this code a bit in the next commit. But for that
c480ed
it is better if the code lives in a separate function.
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
(cherry picked from commit c1a9bfbbba48fea44fdfbe532e084c5323c9c9b3)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1721434
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Message-Id: <6de04910a1dc7c2e5554983b504b1a9932411a60.1564493409.git.mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircommand.c | 52 ++++++++++++++++++++++++++++---------------
c480ed
 1 file changed, 34 insertions(+), 18 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
c480ed
index d328431373..8ae9a952a3 100644
c480ed
--- a/src/util/vircommand.c
c480ed
+++ b/src/util/vircommand.c
c480ed
@@ -491,6 +491,37 @@ virExecCommon(virCommandPtr cmd, gid_t *groups, int ngroups)
c480ed
     return ret;
c480ed
 }
c480ed
 
c480ed
+static int
c480ed
+virCommandMassClose(virCommandPtr cmd,
c480ed
+                    int childin,
c480ed
+                    int childout,
c480ed
+                    int childerr)
c480ed
+{
c480ed
+    int openmax = sysconf(_SC_OPEN_MAX);
c480ed
+    int fd;
c480ed
+    int tmpfd;
c480ed
+
c480ed
+    if (openmax < 0) {
c480ed
+        virReportSystemError(errno,  "%s",
c480ed
+                             _("sysconf(_SC_OPEN_MAX) failed"));
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    for (fd = 3; fd < openmax; fd++) {
c480ed
+        if (fd == childin || fd == childout || fd == childerr)
c480ed
+            continue;
c480ed
+        if (!virCommandFDIsSet(cmd, fd)) {
c480ed
+            tmpfd = fd;
c480ed
+            VIR_MASS_CLOSE(tmpfd);
c480ed
+        } else if (virSetInherit(fd, true) < 0) {
c480ed
+            virReportSystemError(errno, _("failed to preserve fd %d"), fd);
c480ed
+            return -1;
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
 /*
c480ed
  * virExec:
c480ed
  * @cmd virCommandPtr containing all information about the program to
c480ed
@@ -500,13 +531,12 @@ static int
c480ed
 virExec(virCommandPtr cmd)
c480ed
 {
c480ed
     pid_t pid;
c480ed
-    int null = -1, fd, openmax;
c480ed
+    int null = -1;
c480ed
     int pipeout[2] = {-1, -1};
c480ed
     int pipeerr[2] = {-1, -1};
c480ed
     int childin = cmd->infd;
c480ed
     int childout = -1;
c480ed
     int childerr = -1;
c480ed
-    int tmpfd;
c480ed
     VIR_AUTOFREE(char *) binarystr = NULL;
c480ed
     const char *binary = NULL;
c480ed
     int ret;
c480ed
@@ -612,23 +642,9 @@ virExec(virCommandPtr cmd)
c480ed
     if (cmd->mask)
c480ed
         umask(cmd->mask);
c480ed
     ret = EXIT_CANCELED;
c480ed
-    openmax = sysconf(_SC_OPEN_MAX);
c480ed
-    if (openmax < 0) {
c480ed
-        virReportSystemError(errno,  "%s",
c480ed
-                             _("sysconf(_SC_OPEN_MAX) failed"));
c480ed
+
c480ed
+    if (virCommandMassClose(cmd, childin, childout, childerr) < 0)
c480ed
         goto fork_error;
c480ed
-    }
c480ed
-    for (fd = 3; fd < openmax; fd++) {
c480ed
-        if (fd == childin || fd == childout || fd == childerr)
c480ed
-            continue;
c480ed
-        if (!virCommandFDIsSet(cmd, fd)) {
c480ed
-            tmpfd = fd;
c480ed
-            VIR_MASS_CLOSE(tmpfd);
c480ed
-        } else if (virSetInherit(fd, true) < 0) {
c480ed
-            virReportSystemError(errno, _("failed to preserve fd %d"), fd);
c480ed
-            goto fork_error;
c480ed
-        }
c480ed
-    }
c480ed
 
c480ed
     if (prepareStdFd(childin, STDIN_FILENO) < 0) {
c480ed
         virReportSystemError(errno,
c480ed
-- 
c480ed
2.22.0
c480ed