[libvirt] [PATCH 09/15] Move loop device FDs into virLXCControllerPtr object

Daniel P. Berrange berrange at redhat.com
Tue Jul 3 15:58:48 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

Move the list of loop device FDs into the virLXCControllerPtr
object and make sure that virLXCControllerStopInit will
close them all

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 src/lxc/lxc_controller.c |   50 +++++++++++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 16 deletions(-)

diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 06d31d3..d6002c4 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -119,6 +119,9 @@ struct _virLXCController {
 
     size_t nconsoles;
     virLXCControllerConsolePtr consoles;
+
+    size_t nloopDevs;
+    int *loopDevFds;
 };
 
 static void virLXCControllerFree(virLXCControllerPtr ctrl);
@@ -162,11 +165,33 @@ no_memory:
 }
 
 
+static int virLXCControllerCloseLoopDevices(virLXCControllerPtr ctrl,
+                                            bool force)
+{
+    size_t i;
+
+    for (i = 0 ; i < ctrl->nloopDevs ; i++) {
+        if (force) {
+            VIR_FORCE_CLOSE(ctrl->loopDevFds[i]);
+        } else {
+            if (VIR_CLOSE(ctrl->loopDevFds[i]) < 0) {
+                virReportSystemError(errno, "%s",
+                                     _("Unable to close loop device"));
+                return -1;
+            }
+        }
+    }
+
+    return 0;
+}
+
+
 static void virLXCControllerStopInit(virLXCControllerPtr ctrl)
 {
     if (ctrl->initpid == 0)
         return;
 
+    virLXCControllerCloseLoopDevices(ctrl, true);
     virPidAbort(ctrl->initpid);
     ctrl->initpid = 0;
 }
@@ -406,28 +431,28 @@ cleanup:
 }
 
 
-static int lxcSetupLoopDevices(virDomainDefPtr def, size_t *nloopDevs, int **loopDevs)
+static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
 {
     size_t i;
     int ret = -1;
 
-    for (i = 0 ; i < def->nfss ; i++) {
+    for (i = 0 ; i < ctrl->def->nfss ; i++) {
         int fd;
 
-        if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_FILE)
+        if (ctrl->def->fss[i]->type != VIR_DOMAIN_FS_TYPE_FILE)
             continue;
 
-        fd = lxcSetupLoopDevice(def->fss[i]);
+        fd = lxcSetupLoopDevice(ctrl->def->fss[i]);
         if (fd < 0)
             goto cleanup;
 
         VIR_DEBUG("Saving loop fd %d", fd);
-        if (VIR_REALLOC_N(*loopDevs, *nloopDevs+1) < 0) {
+        if (VIR_EXPAND_N(ctrl->loopDevFds, ctrl->nloopDevs, 1) < 0) {
             VIR_FORCE_CLOSE(fd);
             virReportOOMError();
             goto cleanup;
         }
-        (*loopDevs)[(*nloopDevs)++] = fd;
+        ctrl->loopDevFds[ctrl->nloopDevs - 1] = fd;
     }
 
     VIR_DEBUG("Setup all loop devices");
@@ -1528,8 +1553,6 @@ virLXCControllerRun(virLXCControllerPtr ctrl,
     virDomainFSDefPtr root;
     char *devpts = NULL;
     char *devptmx = NULL;
-    size_t nloopDevs = 0;
-    int *loopDevs = NULL;
     size_t i;
     char *mount_options = NULL;
 
@@ -1550,7 +1573,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl,
         goto cleanup;
     }
 
-    if (lxcSetupLoopDevices(ctrl->def, &nloopDevs, &loopDevs) < 0)
+    if (virLXCControllerSetupLoopDevices(ctrl) < 0)
         goto cleanup;
 
     root = virDomainGetRootFilesystem(ctrl->def);
@@ -1702,9 +1725,8 @@ virLXCControllerRun(virLXCControllerPtr ctrl,
     /* Now the container is fully setup... */
 
     /* ...we can close the loop devices... */
-
-    for (i = 0 ; i < nloopDevs ; i++)
-        VIR_FORCE_CLOSE(loopDevs[i]);
+    if (virLXCControllerCloseLoopDevices(ctrl, false) < 0)
+        goto cleanup;
 
     /* ...and reduce our privileges */
     if (lxcControllerClearCapabilities() < 0)
@@ -1739,10 +1761,6 @@ cleanup:
         VIR_FREE(containerTTYPaths[i]);
     VIR_FREE(containerTTYPaths);
 
-    for (i = 0 ; i < nloopDevs ; i++)
-        VIR_FORCE_CLOSE(loopDevs[i]);
-    VIR_FREE(loopDevs);
-
     virLXCControllerStopInit(ctrl);
 
     return rc;
-- 
1.7.10.4




More information about the libvir-list mailing list