Fix uninitialized variable & error reporting in LXC veth setup

THe veth setup in LXC had a couple of flaws, first brInit did
not report any error when it failed. Second vethCreate() did
not correctly initialize the variable containing the return
code, so could report failure even when it succeeded.

* src/lxc/lxc_driver.c: Report error when brInit fails
* src/lxc/veth.c: Fix uninitialized variable
This commit is contained in:
Daniel P. Berrange 2011-03-17 15:54:24 +00:00
parent 83cc3d1d55
commit c59176c109
2 changed files with 28 additions and 13 deletions

View File

@ -1024,9 +1024,13 @@ static int lxcSetupInterfaces(virConnectPtr conn,
int rc = -1, i; int rc = -1, i;
char *bridge = NULL; char *bridge = NULL;
brControl *brctl = NULL; brControl *brctl = NULL;
int ret;
if (brInit(&brctl) != 0) if ((ret = brInit(&brctl)) != 0) {
virReportSystemError(ret, "%s",
_("Unable to initialize bridging"));
return -1; return -1;
}
for (i = 0 ; i < def->nnets ; i++) { for (i = 0 ; i < def->nnets ; i++) {
char *parentVeth; char *parentVeth;
@ -1095,7 +1099,7 @@ static int lxcSetupInterfaces(virConnectPtr conn,
goto error_exit; goto error_exit;
} }
if (0 != (rc = brAddInterface(brctl, bridge, parentVeth))) { if ((ret = brAddInterface(brctl, bridge, parentVeth)) != 0) {
virReportSystemError(rc, virReportSystemError(rc,
_("Failed to add %s device to %s"), _("Failed to add %s device to %s"),
parentVeth, bridge); parentVeth, bridge);

View File

@ -90,33 +90,40 @@ static int getFreeVethName(char **veth, int startDev)
*/ */
int vethCreate(char** veth1, char** veth2) int vethCreate(char** veth1, char** veth2)
{ {
int rc; int rc = -1;
const char *argv[] = { const char *argv[] = {
"ip", "link", "add", NULL, "type", "veth", "peer", "name", NULL, NULL "ip", "link", "add", NULL, "type", "veth", "peer", "name", NULL, NULL
}; };
int vethDev = 0; int vethDev = 0;
bool veth1_alloc = false; bool veth1_alloc = false;
bool veth2_alloc = false;
VIR_DEBUG("veth1: %s veth2: %s", NULLSTR(*veth1), NULLSTR(*veth2)); VIR_DEBUG("veth1: %s veth2: %s", NULLSTR(*veth1), NULLSTR(*veth2));
if (*veth1 == NULL) { if (*veth1 == NULL) {
vethDev = getFreeVethName(veth1, vethDev); if ((vethDev = getFreeVethName(veth1, vethDev)) < 0)
if (vethDev < 0) goto cleanup;
return vethDev;
VIR_DEBUG("Assigned veth1: %s", *veth1); VIR_DEBUG("Assigned veth1: %s", *veth1);
veth1_alloc = true; veth1_alloc = true;
} }
argv[3] = *veth1; argv[3] = *veth1;
while (*veth2 == NULL || STREQ(*veth1, *veth2)) { while (*veth2 == NULL) {
VIR_FREE(*veth2); if ((vethDev = getFreeVethName(veth2, vethDev + 1)) < 0) {
vethDev = getFreeVethName(veth2, vethDev + 1);
if (vethDev < 0) {
if (veth1_alloc) if (veth1_alloc)
VIR_FREE(*veth1); VIR_FREE(*veth1);
return vethDev; goto cleanup;
} }
/* Just make sure they didn't accidentally get same name */
if (STREQ(*veth1, *veth2)) {
vethDev++;
VIR_FREE(*veth2);
continue;
}
VIR_DEBUG("Assigned veth2: %s", *veth2); VIR_DEBUG("Assigned veth2: %s", *veth2);
veth2_alloc = true;
} }
argv[8] = *veth2; argv[8] = *veth2;
@ -124,10 +131,14 @@ int vethCreate(char** veth1, char** veth2)
if (virRun(argv, NULL) < 0) { if (virRun(argv, NULL) < 0) {
if (veth1_alloc) if (veth1_alloc)
VIR_FREE(*veth1); VIR_FREE(*veth1);
VIR_FREE(*veth2); if (veth2_alloc)
rc = -1; VIR_FREE(*veth2);
goto cleanup;
} }
rc = 0;
cleanup:
return rc; return rc;
} }