render / rpms / libvirt

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