lxc_container.c: avoid a leak on error paths

* src/lxc_container.c (lxcContainerMountBasicFS): Don't leak upon failure.
Add "cleanup:" label and change each post-allocation failure to
use "goto cleanup" rather than returning immediately.
This commit is contained in:
Jim Meyering 2009-09-04 16:12:35 +02:00
parent 1469bcf6c5
commit 3ef2e05c4d

View File

@ -1,6 +1,6 @@
/* /*
* Copyright IBM Corp. 2008 * Copyright IBM Corp. 2008
* Copyright Red Hat 2008 * Copyright Red Hat 2008-2009
* *
* lxc_container.c: file description * lxc_container.c: file description
* *
@ -384,12 +384,12 @@ static int lxcContainerMountBasicFS(virDomainFSDefPtr root)
{ "none", "/selinux", "selinuxfs" }, { "none", "/selinux", "selinuxfs" },
#endif #endif
}; };
int i, rc; int i, rc = -1;
char *devpts; char *devpts;
if (virAsprintf(&devpts, "/.oldroot%s/dev/pts", root->src) < 0) { if (virAsprintf(&devpts, "/.oldroot%s/dev/pts", root->src) < 0) {
virReportOOMError(NULL); virReportOOMError(NULL);
return -1; return rc;
} }
for (i = 0 ; i < ARRAY_CARDINALITY(mnts) ; i++) { for (i = 0 ; i < ARRAY_CARDINALITY(mnts) ; i++) {
@ -397,31 +397,35 @@ static int lxcContainerMountBasicFS(virDomainFSDefPtr root)
virReportSystemError(NULL, errno, virReportSystemError(NULL, errno,
_("failed to mkdir %s"), _("failed to mkdir %s"),
mnts[i].src); mnts[i].src);
return -1; goto cleanup;
} }
if (mount(mnts[i].src, mnts[i].dst, mnts[i].type, 0, NULL) < 0) { if (mount(mnts[i].src, mnts[i].dst, mnts[i].type, 0, NULL) < 0) {
virReportSystemError(NULL, errno, virReportSystemError(NULL, errno,
_("failed to mount %s on %s"), _("failed to mount %s on %s"),
mnts[i].type, mnts[i].type); mnts[i].type, mnts[i].type);
return -1; goto cleanup;
} }
} }
if ((rc = virFileMakePath("/dev/pts") < 0)) { if ((rc = virFileMakePath("/dev/pts") < 0)) {
virReportSystemError(NULL, rc, "%s", virReportSystemError(NULL, rc, "%s",
_("cannot create /dev/pts")); _("cannot create /dev/pts"));
return -1; goto cleanup;
} }
VIR_DEBUG("Trying to move %s to %s", devpts, "/dev/pts"); VIR_DEBUG("Trying to move %s to %s", devpts, "/dev/pts");
if ((rc = mount(devpts, "/dev/pts", NULL, MS_MOVE, NULL)) < 0) { if ((rc = mount(devpts, "/dev/pts", NULL, MS_MOVE, NULL)) < 0) {
virReportSystemError(NULL, errno, "%s", virReportSystemError(NULL, errno, "%s",
_("failed to mount /dev/pts in container")); _("failed to mount /dev/pts in container"));
return -1; goto cleanup;
} }
rc = 0;
cleanup:
VIR_FREE(devpts); VIR_FREE(devpts);
return 0; return rc;
} }
static int lxcContainerPopulateDevices(void) static int lxcContainerPopulateDevices(void)