render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 0f27acb41229aa29d539f1975c36c78f75fdf11c Mon Sep 17 00:00:00 2001
Message-Id: <0f27acb41229aa29d539f1975c36c78f75fdf11c@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Tue, 30 Jul 2019 15:30:53 +0200
Subject: [PATCH] vircommand: Separate mass FD closing into a function
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

I will optimize this code a bit in the next commit. But for that
it is better if the code lives in a separate function.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit c1a9bfbbba48fea44fdfbe532e084c5323c9c9b3)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1721434

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Message-Id: <6de04910a1dc7c2e5554983b504b1a9932411a60.1564493409.git.mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircommand.c | 52 ++++++++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index d328431373..8ae9a952a3 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -491,6 +491,37 @@ virExecCommon(virCommandPtr cmd, gid_t *groups, int ngroups)
     return ret;
 }
 
+static int
+virCommandMassClose(virCommandPtr cmd,
+                    int childin,
+                    int childout,
+                    int childerr)
+{
+    int openmax = sysconf(_SC_OPEN_MAX);
+    int fd;
+    int tmpfd;
+
+    if (openmax < 0) {
+        virReportSystemError(errno,  "%s",
+                             _("sysconf(_SC_OPEN_MAX) failed"));
+        return -1;
+    }
+
+    for (fd = 3; fd < openmax; fd++) {
+        if (fd == childin || fd == childout || fd == childerr)
+            continue;
+        if (!virCommandFDIsSet(cmd, fd)) {
+            tmpfd = fd;
+            VIR_MASS_CLOSE(tmpfd);
+        } else if (virSetInherit(fd, true) < 0) {
+            virReportSystemError(errno, _("failed to preserve fd %d"), fd);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
 /*
  * virExec:
  * @cmd virCommandPtr containing all information about the program to
@@ -500,13 +531,12 @@ static int
 virExec(virCommandPtr cmd)
 {
     pid_t pid;
-    int null = -1, fd, openmax;
+    int null = -1;
     int pipeout[2] = {-1, -1};
     int pipeerr[2] = {-1, -1};
     int childin = cmd->infd;
     int childout = -1;
     int childerr = -1;
-    int tmpfd;
     VIR_AUTOFREE(char *) binarystr = NULL;
     const char *binary = NULL;
     int ret;
@@ -612,23 +642,9 @@ virExec(virCommandPtr cmd)
     if (cmd->mask)
         umask(cmd->mask);
     ret = EXIT_CANCELED;
-    openmax = sysconf(_SC_OPEN_MAX);
-    if (openmax < 0) {
-        virReportSystemError(errno,  "%s",
-                             _("sysconf(_SC_OPEN_MAX) failed"));
+
+    if (virCommandMassClose(cmd, childin, childout, childerr) < 0)
         goto fork_error;
-    }
-    for (fd = 3; fd < openmax; fd++) {
-        if (fd == childin || fd == childout || fd == childerr)
-            continue;
-        if (!virCommandFDIsSet(cmd, fd)) {
-            tmpfd = fd;
-            VIR_MASS_CLOSE(tmpfd);
-        } else if (virSetInherit(fd, true) < 0) {
-            virReportSystemError(errno, _("failed to preserve fd %d"), fd);
-            goto fork_error;
-        }
-    }
 
     if (prepareStdFd(childin, STDIN_FILENO) < 0) {
         virReportSystemError(errno,
-- 
2.22.0