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 virLXCCgroupGetMeminfo(virLXCMeminfoPtr meminfo)
{ {
int ret = -1; g_autoptr(virCgroup) cgroup = NULL;
virCgroupPtr cgroup;
if (virCgroupNewSelf(&cgroup) < 0) if (virCgroupNewSelf(&cgroup) < 0)
return -1; return -1;
if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0) if (virLXCCgroupGetMemStat(cgroup, meminfo) < 0)
goto cleanup; return -1;
if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0) if (virLXCCgroupGetMemTotal(cgroup, meminfo) < 0)
goto cleanup; return -1;
if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0) if (virLXCCgroupGetMemUsage(cgroup, meminfo) < 0)
goto cleanup; return -1;
if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0) if (virLXCCgroupGetMemSwapTotal(cgroup, meminfo) < 0)
goto cleanup; return -1;
if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0) if (virLXCCgroupGetMemSwapUsage(cgroup, meminfo) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
virCgroupFree(cgroup);
return ret;
} }

View File

@ -1594,8 +1594,7 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
size_t nttyPaths, size_t nttyPaths,
virSecurityManagerPtr securityDriver) virSecurityManagerPtr securityDriver)
{ {
virCgroupPtr cgroup = NULL; g_autoptr(virCgroup) cgroup = NULL;
int ret = -1;
g_autofree char *sec_mount_options = NULL; g_autofree char *sec_mount_options = NULL;
g_autofree char *stateDir = NULL; g_autofree char *stateDir = NULL;
@ -1607,69 +1606,65 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
/* Before pivoting we need to identify any /* Before pivoting we need to identify any
* cgroups controllers that are mounted */ * cgroups controllers that are mounted */
if (virCgroupNewSelf(&cgroup) < 0) if (virCgroupNewSelf(&cgroup) < 0)
goto cleanup; return -1;
if (virFileResolveAllLinks(LXC_STATE_DIR, &stateDir) < 0) if (virFileResolveAllLinks(LXC_STATE_DIR, &stateDir) < 0)
goto cleanup; return -1;
/* Ensure the root filesystem is mounted */ /* Ensure the root filesystem is mounted */
if (lxcContainerPrepareRoot(vmDef, root, sec_mount_options) < 0) if (lxcContainerPrepareRoot(vmDef, root, sec_mount_options) < 0)
goto cleanup; return -1;
/* Gives us a private root, leaving all parent OS mounts on /.oldroot */ /* Gives us a private root, leaving all parent OS mounts on /.oldroot */
if (lxcContainerPivotRoot(root) < 0) if (lxcContainerPivotRoot(root) < 0)
goto cleanup; return -1;
/* FIXME: we should find a way to unmount these mounts for container /* FIXME: we should find a way to unmount these mounts for container
* even user namespace is enabled. */ * even user namespace is enabled. */
if (STREQ(root->src->path, "/") && (!vmDef->idmap.nuidmap) && if (STREQ(root->src->path, "/") && (!vmDef->idmap.nuidmap) &&
lxcContainerUnmountForSharedRoot(stateDir, vmDef->name) < 0) lxcContainerUnmountForSharedRoot(stateDir, vmDef->name) < 0)
goto cleanup; return -1;
/* Mounts the core /proc, /sys, etc filesystems */ /* Mounts the core /proc, /sys, etc filesystems */
if (lxcContainerMountBasicFS(vmDef->idmap.nuidmap, if (lxcContainerMountBasicFS(vmDef->idmap.nuidmap,
!lxcNeedNetworkNamespace(vmDef)) < 0) !lxcNeedNetworkNamespace(vmDef)) < 0)
goto cleanup; return -1;
/* Ensure entire root filesystem (except /.oldroot) is readonly */ /* Ensure entire root filesystem (except /.oldroot) is readonly */
if (root->readonly && if (root->readonly &&
lxcContainerSetReadOnly() < 0) lxcContainerSetReadOnly() < 0)
goto cleanup; return -1;
/* Mounts /proc/meminfo etc sysinfo */ /* Mounts /proc/meminfo etc sysinfo */
if (lxcContainerMountProcFuse(vmDef, stateDir) < 0) if (lxcContainerMountProcFuse(vmDef, stateDir) < 0)
goto cleanup; return -1;
/* Now we can re-mount the cgroups controllers in the /* Now we can re-mount the cgroups controllers in the
* same configuration as before */ * same configuration as before */
if (virCgroupBindMount(cgroup, "/.oldroot/", sec_mount_options) < 0) if (virCgroupBindMount(cgroup, "/.oldroot/", sec_mount_options) < 0)
goto cleanup; return -1;
/* Mounts /dev */ /* Mounts /dev */
if (lxcContainerMountFSDev(vmDef, stateDir) < 0) if (lxcContainerMountFSDev(vmDef, stateDir) < 0)
goto cleanup; return -1;
/* Mounts /dev/pts */ /* Mounts /dev/pts */
if (lxcContainerMountFSDevPTS(vmDef, stateDir) < 0) if (lxcContainerMountFSDevPTS(vmDef, stateDir) < 0)
goto cleanup; return -1;
/* Setup device nodes in /dev/ */ /* Setup device nodes in /dev/ */
if (lxcContainerSetupDevices(ttyPaths, nttyPaths) < 0) if (lxcContainerSetupDevices(ttyPaths, nttyPaths) < 0)
goto cleanup; return -1;
/* Sets up any non-root mounts from guest config */ /* Sets up any non-root mounts from guest config */
if (lxcContainerMountAllFS(vmDef, sec_mount_options) < 0) if (lxcContainerMountAllFS(vmDef, sec_mount_options) < 0)
goto cleanup; return -1;
/* Gets rid of all remaining mounts from host OS, including /.oldroot itself */ /* Gets rid of all remaining mounts from host OS, including /.oldroot itself */
if (lxcContainerUnmountSubtree("/.oldroot", true) < 0) if (lxcContainerUnmountSubtree("/.oldroot", true) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
virCgroupFree(cgroup);
return ret;
} }
static int lxcContainerResolveAllSymlinks(virDomainDefPtr vmDef) static int lxcContainerResolveAllSymlinks(virDomainDefPtr vmDef)

View File

@ -1194,7 +1194,7 @@ int virLXCProcessStart(virConnectPtr conn,
virCapsPtr caps = NULL; virCapsPtr caps = NULL;
virErrorPtr err = NULL; virErrorPtr err = NULL;
virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver); virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver);
virCgroupPtr selfcgroup; g_autoptr(virCgroup) selfcgroup = NULL;
int status; int status;
g_autofree char *pidfile = NULL; g_autofree char *pidfile = NULL;
@ -1203,26 +1203,22 @@ int virLXCProcessStart(virConnectPtr conn,
if (!virCgroupHasController(selfcgroup, if (!virCgroupHasController(selfcgroup,
VIR_CGROUP_CONTROLLER_CPUACCT)) { VIR_CGROUP_CONTROLLER_CPUACCT)) {
virCgroupFree(selfcgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to find 'cpuacct' cgroups controller mount")); _("Unable to find 'cpuacct' cgroups controller mount"));
return -1; return -1;
} }
if (!virCgroupHasController(selfcgroup, if (!virCgroupHasController(selfcgroup,
VIR_CGROUP_CONTROLLER_DEVICES)) { VIR_CGROUP_CONTROLLER_DEVICES)) {
virCgroupFree(selfcgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to find 'devices' cgroups controller mount")); _("Unable to find 'devices' cgroups controller mount"));
return -1; return -1;
} }
if (!virCgroupHasController(selfcgroup, if (!virCgroupHasController(selfcgroup,
VIR_CGROUP_CONTROLLER_MEMORY)) { VIR_CGROUP_CONTROLLER_MEMORY)) {
virCgroupFree(selfcgroup);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to find 'memory' cgroups controller mount")); _("Unable to find 'memory' cgroups controller mount"));
return -1; return -1;
} }
virCgroupFree(selfcgroup);
if (vm->def->nconsoles == 0) { if (vm->def->nconsoles == 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",