Adapt to VIR_ALLOC and virAsprintf in src/network/*

This commit is contained in:
Michal Privoznik 2013-07-04 12:12:21 +02:00
parent 95cc21491c
commit 9729d847b5

View File

@ -192,17 +192,17 @@ networkRemoveInactive(struct network_driver *driver,
goto cleanup; goto cleanup;
if (!(radvdconfigfile = networkRadvdConfigFileName(def->name))) if (!(radvdconfigfile = networkRadvdConfigFileName(def->name)))
goto no_memory; goto cleanup;
if (!(radvdpidbase = networkRadvdPidfileBasename(def->name))) if (!(radvdpidbase = networkRadvdPidfileBasename(def->name)))
goto no_memory; goto cleanup;
if (!(configfile = networkDnsmasqConfigFileName(def->name))) if (!(configfile = networkDnsmasqConfigFileName(def->name)))
goto no_memory; goto cleanup;
if (!(statusfile if (!(statusfile
= virNetworkConfigFile(driverState->stateDir, def->name))) = virNetworkConfigFile(driverState->stateDir, def->name)))
goto no_memory; goto cleanup;
/* dnsmasq */ /* dnsmasq */
dnsmasqDelete(dctx); dnsmasqDelete(dctx);
@ -229,10 +229,6 @@ cleanup:
VIR_FREE(statusfile); VIR_FREE(statusfile);
dnsmasqContextFree(dctx); dnsmasqContextFree(dctx);
return ret; return ret;
no_memory:
virReportOOMError();
goto cleanup;
} }
static char * static char *
@ -282,10 +278,8 @@ networkFindActiveConfigs(struct network_driver *driver)
&obj->dnsmasqPid, &obj->dnsmasqPid,
dnsmasqCapsGetBinaryPath(driver->dnsmasqCaps))); dnsmasqCapsGetBinaryPath(driver->dnsmasqCaps)));
if (!(radvdpidbase = networkRadvdPidfileBasename(obj->def->name))) { if (!(radvdpidbase = networkRadvdPidfileBasename(obj->def->name)))
virReportOOMError();
goto cleanup; goto cleanup;
}
ignore_value(virPidFileReadIfAlive(driverState->pidDir, radvdpidbase, ignore_value(virPidFileReadIfAlive(driverState->pidDir, radvdpidbase,
&obj->radvdPid, RADVD)); &obj->radvdPid, RADVD));
VIR_FREE(radvdpidbase); VIR_FREE(radvdpidbase);
@ -415,7 +409,7 @@ networkStateInitialize(bool privileged,
"%s/dnsmasq/lib", rundir) < 0) || "%s/dnsmasq/lib", rundir) < 0) ||
(virAsprintf(&driverState->radvdStateDir, (virAsprintf(&driverState->radvdStateDir,
"%s/radvd/lib", rundir) < 0)) { "%s/radvd/lib", rundir) < 0)) {
goto out_of_memory; goto error;
} }
} }
@ -470,8 +464,6 @@ cleanup:
VIR_FREE(rundir); VIR_FREE(rundir);
return ret; return ret;
out_of_memory:
virReportOOMError();
error: error:
if (driverState) if (driverState)
networkDriverUnlock(driverState); networkDriverUnlock(driverState);
@ -784,24 +776,15 @@ networkDnsmasqConfContents(virNetworkObjPtr network,
for (ii = 0; ii < dns->nsrvs; ii++) { for (ii = 0; ii < dns->nsrvs; ii++) {
if (dns->srvs[ii].service && dns->srvs[ii].protocol) { if (dns->srvs[ii].service && dns->srvs[ii].protocol) {
if (dns->srvs[ii].port) { if (dns->srvs[ii].port &&
if (virAsprintf(&recordPort, "%d", dns->srvs[ii].port) < 0) { virAsprintf(&recordPort, "%d", dns->srvs[ii].port) < 0)
virReportOOMError(); goto cleanup;
goto cleanup; if (dns->srvs[ii].priority &&
} virAsprintf(&recordPriority, "%d", dns->srvs[ii].priority) < 0)
} goto cleanup;
if (dns->srvs[ii].priority) { if (dns->srvs[ii].weight &&
if (virAsprintf(&recordPriority, "%d", dns->srvs[ii].priority) < 0) { virAsprintf(&recordWeight, "%d", dns->srvs[ii].weight) < 0)
virReportOOMError(); goto cleanup;
goto cleanup;
}
}
if (dns->srvs[ii].weight) {
if (virAsprintf(&recordWeight, "%d", dns->srvs[ii].weight) < 0) {
virReportOOMError();
goto cleanup;
}
}
if (virAsprintf(&record, "%s.%s.%s,%s,%s,%s,%s", if (virAsprintf(&record, "%s.%s.%s,%s,%s,%s,%s",
dns->srvs[ii].service, dns->srvs[ii].service,
@ -810,10 +793,8 @@ networkDnsmasqConfContents(virNetworkObjPtr network,
dns->srvs[ii].target ? dns->srvs[ii].target : "", dns->srvs[ii].target ? dns->srvs[ii].target : "",
recordPort ? recordPort : "", recordPort ? recordPort : "",
recordPriority ? recordPriority : "", recordPriority ? recordPriority : "",
recordWeight ? recordWeight : "") < 0) { recordWeight ? recordWeight : "") < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
virBufferAsprintf(&configbuf, "srv-host=%s\n", record); virBufferAsprintf(&configbuf, "srv-host=%s\n", record);
VIR_FREE(record); VIR_FREE(record);
@ -929,10 +910,8 @@ networkDnsmasqConfContents(virNetworkObjPtr network,
if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) { if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) {
char *bootserver = virSocketAddrFormat(&ipdef->bootserver); char *bootserver = virSocketAddrFormat(&ipdef->bootserver);
if (!bootserver) { if (!bootserver)
virReportOOMError();
goto cleanup; goto cleanup;
}
virBufferAsprintf(&configbuf, "dhcp-boot=%s%s%s\n", virBufferAsprintf(&configbuf, "dhcp-boot=%s%s%s\n",
ipdef->bootfile, ",,", bootserver); ipdef->bootfile, ",,", bootserver);
VIR_FREE(bootserver); VIR_FREE(bootserver);
@ -946,10 +925,8 @@ networkDnsmasqConfContents(virNetworkObjPtr network,
if (nbleases > 0) { if (nbleases > 0) {
char *leasefile = networkDnsmasqLeaseFileName(network->def->name); char *leasefile = networkDnsmasqLeaseFileName(network->def->name);
if (!leasefile) { if (!leasefile)
virReportOOMError();
goto cleanup; goto cleanup;
}
virBufferAsprintf(&configbuf, "dhcp-leasefile=%s\n", leasefile); virBufferAsprintf(&configbuf, "dhcp-leasefile=%s\n", leasefile);
VIR_FREE(leasefile); VIR_FREE(leasefile);
virBufferAsprintf(&configbuf, "dhcp-lease-max=%d\n", nbleases); virBufferAsprintf(&configbuf, "dhcp-lease-max=%d\n", nbleases);
@ -1027,10 +1004,8 @@ networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network,
goto cleanup; goto cleanup;
/* construct the filename */ /* construct the filename */
if (!(configfile = networkDnsmasqConfigFileName(network->def->name))) { if (!(configfile = networkDnsmasqConfigFileName(network->def->name)))
virReportOOMError();
goto cleanup; goto cleanup;
}
/* Write the file */ /* Write the file */
if (virFileWriteStr(configfile, configstr, 0600) < 0) { if (virFileWriteStr(configfile, configstr, 0600) < 0) {
@ -1077,10 +1052,8 @@ networkStartDhcpDaemon(struct network_driver *driver,
} }
if (!(pidfile = virPidFileBuildPath(driverState->pidDir, if (!(pidfile = virPidFileBuildPath(driverState->pidDir,
network->def->name))) { network->def->name)))
virReportOOMError();
goto cleanup; goto cleanup;
}
if (virFileMakePath(driverState->dnsmasqStateDir) < 0) { if (virFileMakePath(driverState->dnsmasqStateDir) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -1330,10 +1303,8 @@ networkRadvdConfWrite(virNetworkObjPtr network, char **configFile)
} }
/* construct the filename */ /* construct the filename */
if (!(*configFile = networkRadvdConfigFileName(network->def->name))) { if (!(*configFile = networkRadvdConfigFileName(network->def->name)))
virReportOOMError();
goto cleanup; goto cleanup;
}
/* write the file */ /* write the file */
if (virFileWriteStr(*configFile, configStr, 0600) < 0) { if (virFileWriteStr(*configFile, configStr, 0600) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -1395,14 +1366,10 @@ networkStartRadvd(struct network_driver *driver ATTRIBUTE_UNUSED,
} }
/* construct pidfile name */ /* construct pidfile name */
if (!(radvdpidbase = networkRadvdPidfileBasename(network->def->name))) { if (!(radvdpidbase = networkRadvdPidfileBasename(network->def->name)))
virReportOOMError();
goto cleanup; goto cleanup;
} if (!(pidfile = virPidFileBuildPath(driverState->pidDir, radvdpidbase)))
if (!(pidfile = virPidFileBuildPath(driverState->pidDir, radvdpidbase))) {
virReportOOMError();
goto cleanup; goto cleanup;
}
if (networkRadvdConfWrite(network, &configfile) < 0) if (networkRadvdConfWrite(network, &configfile) < 0)
goto cleanup; goto cleanup;
@ -2178,10 +2145,8 @@ networkSetIPv6Sysctls(virNetworkObjPtr network)
* the network. * the network.
*/ */
if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/disable_ipv6", if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/disable_ipv6",
network->def->bridge) < 0) { network->def->bridge) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
if (access(field, W_OK) < 0 && errno == ENOENT) { if (access(field, W_OK) < 0 && errno == ENOENT) {
VIR_DEBUG("ipv6 appears to already be disabled on %s", VIR_DEBUG("ipv6 appears to already be disabled on %s",
@ -2207,10 +2172,8 @@ networkSetIPv6Sysctls(virNetworkObjPtr network)
* their own router advertisements. * their own router advertisements.
*/ */
if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/accept_ra", if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/accept_ra",
network->def->bridge) < 0) { network->def->bridge) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
if (virFileWriteStr(field, "0", 0) < 0) { if (virFileWriteStr(field, "0", 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -2223,10 +2186,8 @@ networkSetIPv6Sysctls(virNetworkObjPtr network)
* definition), must always have autoconf=0. * definition), must always have autoconf=0.
*/ */
if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/autoconf", if (virAsprintf(&field, SYSCTL_PATH "/net/ipv6/conf/%s/autoconf",
network->def->bridge) < 0) { network->def->bridge) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
if (virFileWriteStr(field, "0", 0) < 0) { if (virFileWriteStr(field, "0", 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -2435,10 +2396,8 @@ networkStartNetworkVirtual(struct network_driver *driver,
* address. * address.
*/ */
macTapIfName = networkBridgeDummyNicName(network->def->bridge); macTapIfName = networkBridgeDummyNicName(network->def->bridge);
if (!macTapIfName) { if (!macTapIfName)
virReportOOMError();
goto err0; goto err0;
}
/* Keep tun fd open and interface up to allow for IPv6 DAD to happen */ /* Keep tun fd open and interface up to allow for IPv6 DAD to happen */
if (virNetDevTapCreateInBridgePort(network->def->bridge, if (virNetDevTapCreateInBridgePort(network->def->bridge,
&macTapIfName, &network->def->mac, &macTapIfName, &network->def->mac,
@ -2599,9 +2558,7 @@ static int networkShutdownNetworkVirtual(struct network_driver *driver ATTRIBUTE
kill(network->radvdPid, SIGTERM); kill(network->radvdPid, SIGTERM);
/* attempt to delete the pidfile we created */ /* attempt to delete the pidfile we created */
if (!(radvdpidbase = networkRadvdPidfileBasename(network->def->name))) { if ((radvdpidbase = networkRadvdPidfileBasename(network->def->name))) {
virReportOOMError();
} else {
virPidFileDelete(driverState->pidDir, radvdpidbase); virPidFileDelete(driverState->pidDir, radvdpidbase);
VIR_FREE(radvdpidbase); VIR_FREE(radvdpidbase);
} }
@ -2612,9 +2569,7 @@ static int networkShutdownNetworkVirtual(struct network_driver *driver ATTRIBUTE
if (network->def->mac_specified) { if (network->def->mac_specified) {
char *macTapIfName = networkBridgeDummyNicName(network->def->bridge); char *macTapIfName = networkBridgeDummyNicName(network->def->bridge);
if (!macTapIfName) { if (macTapIfName) {
virReportOOMError();
} else {
ignore_value(virNetDevTapDelete(macTapIfName)); ignore_value(virNetDevTapDelete(macTapIfName));
VIR_FREE(macTapIfName); VIR_FREE(macTapIfName);
} }
@ -3774,10 +3729,8 @@ networkCreateInterfacePool(virNetworkDefPtr netdef) {
goto finish; goto finish;
} }
if ((VIR_ALLOC_N(netdef->forward.ifs, num_virt_fns)) < 0) { if (VIR_ALLOC_N(netdef->forward.ifs, num_virt_fns) < 0)
virReportOOMError();
goto finish; goto finish;
}
netdef->forward.nifs = num_virt_fns; netdef->forward.nifs = num_virt_fns;
@ -3877,12 +3830,9 @@ networkAllocateActualDevice(virDomainNetDefPtr iface)
bandwidth = portgroup->bandwidth; bandwidth = portgroup->bandwidth;
if (bandwidth) { if (bandwidth) {
if (!iface->data.network.actual if (!iface->data.network.actual &&
&& (VIR_ALLOC(iface->data.network.actual) < 0)) { VIR_ALLOC(iface->data.network.actual) < 0)
virReportOOMError();
goto error; goto error;
}
if (virNetDevBandwidthCopy(&iface->data.network.actual->bandwidth, if (virNetDevBandwidthCopy(&iface->data.network.actual->bandwidth,
bandwidth) < 0) bandwidth) < 0)
goto error; goto error;
@ -3897,11 +3847,9 @@ networkAllocateActualDevice(virDomainNetDefPtr iface)
vlan = &netdef->vlan; vlan = &netdef->vlan;
if (vlan) { if (vlan) {
if (!iface->data.network.actual if (!iface->data.network.actual &&
&& (VIR_ALLOC(iface->data.network.actual) < 0)) { VIR_ALLOC(iface->data.network.actual) < 0)
virReportOOMError();
goto error; goto error;
}
if (virNetDevVlanCopy(&iface->data.network.actual->vlan, vlan) < 0) if (virNetDevVlanCopy(&iface->data.network.actual->vlan, vlan) < 0)
goto error; goto error;
} }
@ -3926,11 +3874,9 @@ networkAllocateActualDevice(virDomainNetDefPtr iface)
* is VIR_DOMAIN_NET_TYPE_BRIDGE * is VIR_DOMAIN_NET_TYPE_BRIDGE
*/ */
if (!iface->data.network.actual if (!iface->data.network.actual &&
&& (VIR_ALLOC(iface->data.network.actual) < 0)) { VIR_ALLOC(iface->data.network.actual) < 0)
virReportOOMError();
goto error; goto error;
}
iface->data.network.actual->type = actualType = VIR_DOMAIN_NET_TYPE_BRIDGE; iface->data.network.actual->type = actualType = VIR_DOMAIN_NET_TYPE_BRIDGE;
if (VIR_STRDUP(iface->data.network.actual->data.bridge.brname, if (VIR_STRDUP(iface->data.network.actual->data.bridge.brname,
@ -3964,11 +3910,9 @@ networkAllocateActualDevice(virDomainNetDefPtr iface)
virDomainHostdevSubsysPciBackendType backend; virDomainHostdevSubsysPciBackendType backend;
if (!iface->data.network.actual if (!iface->data.network.actual &&
&& (VIR_ALLOC(iface->data.network.actual) < 0)) { VIR_ALLOC(iface->data.network.actual) < 0)
virReportOOMError();
goto error; goto error;
}
iface->data.network.actual->type = actualType = VIR_DOMAIN_NET_TYPE_HOSTDEV; iface->data.network.actual->type = actualType = VIR_DOMAIN_NET_TYPE_HOSTDEV;
if (netdef->forward.npfs > 0 && netdef->forward.nifs <= 0 && if (netdef->forward.npfs > 0 && netdef->forward.nifs <= 0 &&
@ -4053,11 +3997,9 @@ networkAllocateActualDevice(virDomainNetDefPtr iface)
* VIR_DOMAIN_NET_TYPE_DIRECT. * VIR_DOMAIN_NET_TYPE_DIRECT.
*/ */
if (!iface->data.network.actual if (!iface->data.network.actual &&
&& (VIR_ALLOC(iface->data.network.actual) < 0)) { VIR_ALLOC(iface->data.network.actual) < 0)
virReportOOMError();
goto error; goto error;
}
/* Set type=direct and appropriate <source mode='xxx'/> */ /* Set type=direct and appropriate <source mode='xxx'/> */
iface->data.network.actual->type = actualType = VIR_DOMAIN_NET_TYPE_DIRECT; iface->data.network.actual->type = actualType = VIR_DOMAIN_NET_TYPE_DIRECT;