[libvirt] [PATCH v3 02/13] virprocess: Make virProcessRunInMountNamespace use virProcessRunInFork

Michal Privoznik mprivozn at redhat.com
Wed Oct 17 09:06:44 UTC 2018


Both virProcessRunInMountNamespace() and virProcessRunInFork()
look very similar. De-duplicate the code and make
virProcessRunInMountNamespace() call virProcessRunInFork().

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/util/virprocess.c | 62 +++++++++----------------------------------
 1 file changed, 12 insertions(+), 50 deletions(-)

diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index 51b9ccb1bb..3304879212 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -1073,11 +1073,17 @@ int virProcessGetStartTime(pid_t pid,
 #endif
 
 
-static int virProcessNamespaceHelper(int errfd,
-                                     pid_t pid,
-                                     virProcessNamespaceCallback cb,
+typedef struct _virProcessNamespaceHelperData virProcessNamespaceHelperData;
+struct _virProcessNamespaceHelperData {
+    pid_t pid;
+    virProcessNamespaceCallback cb;
+    void *opaque;
+};
+
+static int virProcessNamespaceHelper(pid_t pid ATTRIBUTE_UNUSED,
                                      void *opaque)
 {
+    virProcessNamespaceHelperData *data = opaque;
     int fd = -1;
     int ret = -1;
     VIR_AUTOFREE(char *) path = NULL;
@@ -1097,16 +1103,9 @@ static int virProcessNamespaceHelper(int errfd,
         goto cleanup;
     }
 
-    ret = cb(pid, opaque);
+    ret = data->cb(data->pid, data->opaque);
 
  cleanup:
-    if (ret < 0) {
-        virErrorPtr err = virGetLastError();
-        if (err) {
-            size_t len = strlen(err->message) + 1;
-            ignore_value(safewrite(errfd, err->message, len));
-        }
-    }
     VIR_FORCE_CLOSE(fd);
     return ret;
 }
@@ -1122,46 +1121,9 @@ virProcessRunInMountNamespace(pid_t pid,
                               virProcessNamespaceCallback cb,
                               void *opaque)
 {
-    int ret = -1;
-    pid_t child = -1;
-    int errfd[2] = { -1, -1 };
+    virProcessNamespaceHelperData data = {.pid = pid, .cb = cb, .opaque = opaque};
 
-    if (pipe2(errfd, O_CLOEXEC) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("Cannot create pipe for child"));
-        return -1;
-    }
-
-    if ((child = virFork()) < 0)
-        goto cleanup;
-
-    if (child == 0) {
-        VIR_FORCE_CLOSE(errfd[0]);
-        ret = virProcessNamespaceHelper(errfd[1], pid,
-                                        cb, opaque);
-        VIR_FORCE_CLOSE(errfd[1]);
-        _exit(ret < 0 ? EXIT_CANCELED : ret);
-    } else {
-        int status;
-        VIR_AUTOFREE(char *) buf = NULL;
-
-        VIR_FORCE_CLOSE(errfd[1]);
-        ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf));
-        ret = virProcessWait(child, &status, false);
-        if (!ret) {
-            ret = status == EXIT_CANCELED ? -1 : status;
-            if (ret) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("child reported: %s"),
-                               NULLSTR(buf));
-            }
-        }
-    }
-
- cleanup:
-    VIR_FORCE_CLOSE(errfd[0]);
-    VIR_FORCE_CLOSE(errfd[1]);
-    return ret;
+    return virProcessRunInFork(virProcessNamespaceHelper, &data);
 }
 
 
-- 
2.18.1




More information about the libvir-list mailing list