[libvirt PATCH 06/10] lxc: use g_autoptr for virCgroup

Pavel Hrdina phrdina at redhat.com
Thu Oct 8 14:26:59 UTC 2020


Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/lxc/lxc_cgroup.c    | 18 +++++++-----------
 src/lxc/lxc_container.c | 37 ++++++++++++++++---------------------
 src/lxc/lxc_process.c   |  6 +-----
 3 files changed, 24 insertions(+), 37 deletions(-)

diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index b80a8911f9..3c546861f1 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -145,31 +145,27 @@ static int virLXCCgroupGetMemStat(virCgroupPtr cgroup,
 
 int virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
 {
-    int ret = -1;
-    virCgroupPtr cgroup;
+    g_autoptr(virCgroup) cgroup = NULL;
 
     if (virCgroupNewSelf(&cgroup) < 0)
         return -1;
 
     if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
-        goto cleanup;
+        return -1;
 
     if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
-        goto cleanup;
+        return -1;
 
     if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
-        goto cleanup;
+        return -1;
 
     if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
-        goto cleanup;
+        return -1;
 
     if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
- cleanup:
-    virCgroupFree(cgroup);
-    return ret;
+    return 0;
 }
 
 
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 913f4de26a..2a5f8711c4 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -1594,8 +1594,7 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
                                       size_t nttyPaths,
                                       virSecurityManagerPtr securityDriver)
 {
-    virCgroupPtr cgroup = NULL;
-    int ret = -1;
+    g_autoptr(virCgroup) cgroup = NULL;
     g_autofree char *sec_mount_options = NULL;
     g_autofree char *stateDir = NULL;
 
@@ -1607,69 +1606,65 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
     /* Before pivoting we need to identify any
      * cgroups controllers that are mounted */
     if (virCgroupNewSelf(&cgroup) < 0)
-        goto cleanup;
+        return -1;
 
     if (virFileResolveAllLinks(LXC_STATE_DIR, &stateDir) < 0)
-        goto cleanup;
+        return -1;
 
     /* Ensure the root filesystem is mounted */
     if (lxcContainerPrepareRoot(vmDef, root, sec_mount_options) < 0)
-        goto cleanup;
+        return -1;
 
     /* Gives us a private root, leaving all parent OS mounts on /.oldroot */
     if (lxcContainerPivotRoot(root) < 0)
-        goto cleanup;
+        return -1;
 
     /* FIXME: we should find a way to unmount these mounts for container
      * even user namespace is enabled. */
     if (STREQ(root->src->path, "/") && (!vmDef->idmap.nuidmap) &&
         lxcContainerUnmountForSharedRoot(stateDir, vmDef->name) < 0)
-        goto cleanup;
+        return -1;
 
     /* Mounts the core /proc, /sys, etc filesystems */
     if (lxcContainerMountBasicFS(vmDef->idmap.nuidmap,
                                  !lxcNeedNetworkNamespace(vmDef)) < 0)
-        goto cleanup;
+        return -1;
 
     /* Ensure entire root filesystem (except /.oldroot) is readonly */
     if (root->readonly &&
         lxcContainerSetReadOnly() < 0)
-        goto cleanup;
+        return -1;
 
     /* Mounts /proc/meminfo etc sysinfo */
     if (lxcContainerMountProcFuse(vmDef, stateDir) < 0)
-        goto cleanup;
+        return -1;
 
     /* Now we can re-mount the cgroups controllers in the
      * same configuration as before */
     if (virCgroupBindMount(cgroup, "/.oldroot/", sec_mount_options) < 0)
-        goto cleanup;
+        return -1;
 
     /* Mounts /dev */
     if (lxcContainerMountFSDev(vmDef, stateDir) < 0)
-        goto cleanup;
+        return -1;
 
     /* Mounts /dev/pts */
     if (lxcContainerMountFSDevPTS(vmDef, stateDir) < 0)
-        goto cleanup;
+        return -1;
 
     /* Setup device nodes in /dev/ */
     if (lxcContainerSetupDevices(ttyPaths, nttyPaths) < 0)
-        goto cleanup;
+        return -1;
 
     /* Sets up any non-root mounts from guest config */
     if (lxcContainerMountAllFS(vmDef, sec_mount_options) < 0)
-        goto cleanup;
+        return -1;
 
    /* Gets rid of all remaining mounts from host OS, including /.oldroot itself */
     if (lxcContainerUnmountSubtree("/.oldroot", true) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
-
- cleanup:
-    virCgroupFree(cgroup);
-    return ret;
+    return 0;
 }
 
 static int lxcContainerResolveAllSymlinks(virDomainDefPtr vmDef)
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index a98a090893..e392d98f5d 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -1194,7 +1194,7 @@ int virLXCProcessStart(virConnectPtr conn,
     virCapsPtr caps = NULL;
     virErrorPtr err = NULL;
     virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver);
-    virCgroupPtr selfcgroup;
+    g_autoptr(virCgroup) selfcgroup = NULL;
     int status;
     g_autofree char *pidfile = NULL;
 
@@ -1203,26 +1203,22 @@ int virLXCProcessStart(virConnectPtr conn,
 
     if (!virCgroupHasController(selfcgroup,
                                 VIR_CGROUP_CONTROLLER_CPUACCT)) {
-        virCgroupFree(selfcgroup);
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to find 'cpuacct' cgroups controller mount"));
         return -1;
     }
     if (!virCgroupHasController(selfcgroup,
                                 VIR_CGROUP_CONTROLLER_DEVICES)) {
-        virCgroupFree(selfcgroup);
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to find 'devices' cgroups controller mount"));
         return -1;
     }
     if (!virCgroupHasController(selfcgroup,
                                 VIR_CGROUP_CONTROLLER_MEMORY)) {
-        virCgroupFree(selfcgroup);
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("Unable to find 'memory' cgroups controller mount"));
         return -1;
     }
-    virCgroupFree(selfcgroup);
 
     if (vm->def->nconsoles == 0) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-- 
2.26.2




More information about the libvir-list mailing list