lxc: use g_autoptr for virCgroup

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Pavel Hrdina 2020-09-22 12:49:58 +02:00
parent ab8cc94ccc
commit a9bb02cfc9
3 changed files with 24 additions and 37 deletions

View File

@ -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;
}

View File

@ -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)

View File

@ -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",