Adapt to VIR_ALLOC and virAsprintf in src/nwfilter/*

This commit is contained in:
Michal Privoznik 2013-07-04 12:12:44 +02:00
parent 9729d847b5
commit 8abed96d21
5 changed files with 33 additions and 91 deletions

View File

@ -303,10 +303,8 @@ virNWFilterSnoopActivate(virNWFilterSnoopReqPtr req)
{ {
char *key; char *key;
if (virAsprintf(&key, "%p-%d", req, req->ifindex) < 0) { if (virAsprintf(&key, "%p-%d", req, req->ifindex) < 0)
virReportOOMError();
return NULL; return NULL;
}
virNWFilterSnoopActiveLock(); virNWFilterSnoopActiveLock();
@ -583,10 +581,8 @@ virNWFilterSnoopReqNew(const char *ifkey)
return NULL; return NULL;
} }
if (VIR_ALLOC(req) < 0) { if (VIR_ALLOC(req) < 0)
virReportOOMError();
return NULL; return NULL;
}
req->threadStatus = THREAD_STATUS_NONE; req->threadStatus = THREAD_STATUS_NONE;
@ -762,10 +758,8 @@ virNWFilterSnoopReqLeaseAdd(virNWFilterSnoopReqPtr req,
virNWFilterSnoopReqUnlock(req); virNWFilterSnoopReqUnlock(req);
if (VIR_ALLOC(pl) < 0) { if (VIR_ALLOC(pl) < 0)
virReportOOMError();
return -1; return -1;
}
*pl = *plnew; *pl = *plnew;
/* protect req->threadkey */ /* protect req->threadkey */
@ -1091,10 +1085,8 @@ virNWFilterSnoopDHCPOpen(const char *ifname, virMacAddr *mac,
* more unlikely parameters first, then go for the MAC * more unlikely parameters first, then go for the MAC
*/ */
if (virAsprintf(&ext_filter, if (virAsprintf(&ext_filter,
"%s and ether src %s", filter, macaddr) < 0) { "%s and ether src %s", filter, macaddr) < 0)
virReportOOMError();
return NULL; return NULL;
}
} else { } else {
/* /*
* Some DHCP servers respond via MAC broadcast; we rely on later * Some DHCP servers respond via MAC broadcast; we rely on later
@ -1195,10 +1187,8 @@ virNWFilterSnoopDHCPDecodeJobSubmit(virThreadPoolPtr pool,
if (len <= MIN_VALID_DHCP_PKT_SIZE || len > sizeof(job->packet)) if (len <= MIN_VALID_DHCP_PKT_SIZE || len > sizeof(job->packet))
return 0; return 0;
if (VIR_ALLOC(job) < 0) { if (VIR_ALLOC(job) < 0)
virReportOOMError();
return -1; return -1;
}
memcpy(job->packet, pep, len); memcpy(job->packet, pep, len);
job->caplen = len; job->caplen = len;
@ -1633,10 +1623,8 @@ virNWFilterDHCPSnoopReq(virNWFilterTechDriverPtr techdriver,
VIR_STRDUP(req->linkdev, linkdev) < 0) VIR_STRDUP(req->linkdev, linkdev) < 0)
goto exit_snoopreqput; goto exit_snoopreqput;
if (!req->vars || tmp < 0) { if (!req->vars || tmp < 0)
virReportOOMError();
goto exit_snoopreqput; goto exit_snoopreqput;
}
/* check that all tools are available for applying the filters (late) */ /* check that all tools are available for applying the filters (late) */
if (!techdriver->canApplyBasicRules()) { if (!techdriver->canApplyBasicRules()) {
@ -1781,7 +1769,6 @@ virNWFilterSnoopLeaseFileWrite(int lfd, const char *ifkey,
ifkey, ipstr, dhcpstr); ifkey, ipstr, dhcpstr);
if (len < 0) { if (len < 0) {
virReportOOMError();
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
@ -2089,10 +2076,8 @@ virNWFilterDHCPSnoopInit(void)
if (!virNWFilterSnoopState.ifnameToKey || if (!virNWFilterSnoopState.ifnameToKey ||
!virNWFilterSnoopState.snoopReqs || !virNWFilterSnoopState.snoopReqs ||
!virNWFilterSnoopState.active) { !virNWFilterSnoopState.active)
virReportOOMError();
goto err_exit; goto err_exit;
}
virNWFilterSnoopLeaseFileLoad(); virNWFilterSnoopLeaseFileLoad();
virNWFilterSnoopLeaseFileOpen(); virNWFilterSnoopLeaseFileOpen();

View File

@ -178,10 +178,8 @@ nwfilterStateInitialize(bool privileged,
sysbus = virDBusGetSystemBus(); sysbus = virDBusGetSystemBus();
#endif /* WITH_DBUS */ #endif /* WITH_DBUS */
if (VIR_ALLOC(driverState) < 0) { if (VIR_ALLOC(driverState) < 0)
virReportOOMError();
return -1; return -1;
}
if (virMutexInit(&driverState->lock) < 0) if (virMutexInit(&driverState->lock) < 0)
goto err_free_driverstate; goto err_free_driverstate;
@ -227,7 +225,7 @@ nwfilterStateInitialize(bool privileged,
if (virAsprintf(&driverState->configDir, if (virAsprintf(&driverState->configDir,
"%s/nwfilter", base) == -1) "%s/nwfilter", base) == -1)
goto out_of_memory; goto error;
VIR_FREE(base); VIR_FREE(base);
@ -240,9 +238,6 @@ nwfilterStateInitialize(bool privileged,
return 0; return 0;
out_of_memory:
virReportOOMError();
error: error:
VIR_FREE(base); VIR_FREE(base);
nwfilterDriverUnlock(driverState); nwfilterDriverUnlock(driverState);
@ -518,10 +513,8 @@ nwfilterConnectListAllNWFilters(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(tmp_filters, driver->nwfilters.count + 1) < 0) { if (VIR_ALLOC_N(tmp_filters, driver->nwfilters.count + 1) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
for (i = 0; i < driver->nwfilters.count; i++) { for (i = 0; i < driver->nwfilters.count; i++) {
obj = driver->nwfilters.objs[i]; obj = driver->nwfilters.objs[i];

View File

@ -494,10 +494,8 @@ ebiptablesAddRuleInst(virNWFilterRuleInstPtr res,
{ {
ebiptablesRuleInstPtr inst; ebiptablesRuleInstPtr inst;
if (VIR_ALLOC(inst) < 0) { if (VIR_ALLOC(inst) < 0)
virReportOOMError();
return -1; return -1;
}
inst->commandTemplate = commandTemplate; inst->commandTemplate = commandTemplate;
inst->neededProtocolChain = neededChain; inst->neededProtocolChain = neededChain;
@ -3016,10 +3014,8 @@ ebtablesCreateTmpSubChain(ebiptablesRuleInstPtr *inst,
break; break;
} }
if (!protostr) { if (!protostr)
virReportOOMError();
return -1; return -1;
}
virBufferAsprintf(&buf, virBufferAsprintf(&buf,
CMD_DEF("$EBT -t nat -F %s") CMD_SEPARATOR CMD_DEF("$EBT -t nat -F %s") CMD_SEPARATOR
@ -3393,10 +3389,8 @@ ebtablesApplyDHCPOnlyRules(const char *ifname,
dhcpserver = virNWFilterVarValueGetNthValue(dhcpsrvrs, idx); dhcpserver = virNWFilterVarValueGetNthValue(dhcpsrvrs, idx);
if (virAsprintf(&srcIPParam, "--ip-src %s", dhcpserver) < 0) { if (virAsprintf(&srcIPParam, "--ip-src %s", dhcpserver) < 0)
virReportOOMError();
goto tear_down_tmpebchains; goto tear_down_tmpebchains;
}
} }
/* /*
@ -3720,10 +3714,8 @@ ebiptablesApplyNewRules(const char *ifname,
if (inst == NULL) if (inst == NULL)
nruleInstances = 0; nruleInstances = 0;
if (!chains_in_set || !chains_out_set) { if (!chains_in_set || !chains_out_set)
virReportOOMError();
goto exit_free_sets; goto exit_free_sets;
}
if (nruleInstances > 1 && inst) if (nruleInstances > 1 && inst)
qsort(inst, nruleInstances, sizeof(inst[0]), qsort(inst, nruleInstances, sizeof(inst[0]),
@ -3736,16 +3728,12 @@ ebiptablesApplyNewRules(const char *ifname,
const char *name = inst[i]->neededProtocolChain; const char *name = inst[i]->neededProtocolChain;
if (inst[i]->chainprefix == CHAINPREFIX_HOST_IN_TEMP) { if (inst[i]->chainprefix == CHAINPREFIX_HOST_IN_TEMP) {
if (virHashUpdateEntry(chains_in_set, name, if (virHashUpdateEntry(chains_in_set, name,
&inst[i]->chainPriority) < 0) { &inst[i]->chainPriority) < 0)
virReportOOMError();
goto exit_free_sets; goto exit_free_sets;
}
} else { } else {
if (virHashUpdateEntry(chains_out_set, name, if (virHashUpdateEntry(chains_out_set, name,
&inst[i]->chainPriority) < 0) { &inst[i]->chainPriority) < 0)
virReportOOMError();
goto exit_free_sets; goto exit_free_sets;
}
} }
} }
} }
@ -4210,19 +4198,15 @@ ebiptablesDriverInitWithFirewallD(void)
} else { } else {
VIR_INFO("firewalld support enabled for nwfilter"); VIR_INFO("firewalld support enabled for nwfilter");
ignore_value(virAsprintf(&ebtables_cmd_path, if (virAsprintf(&ebtables_cmd_path,
"%s --direct --passthrough eb", "%s --direct --passthrough eb",
firewall_cmd_path)); firewall_cmd_path) < 0 ||
ignore_value(virAsprintf(&iptables_cmd_path, virAsprintf(&iptables_cmd_path,
"%s --direct --passthrough ipv4", "%s --direct --passthrough ipv4",
firewall_cmd_path)); firewall_cmd_path) < 0 ||
ignore_value(virAsprintf(&ip6tables_cmd_path, virAsprintf(&ip6tables_cmd_path,
"%s --direct --passthrough ipv6", "%s --direct --passthrough ipv6",
firewall_cmd_path)); firewall_cmd_path) < 0) {
if (!ebtables_cmd_path || !iptables_cmd_path ||
!ip6tables_cmd_path) {
virReportOOMError();
VIR_FREE(ebtables_cmd_path); VIR_FREE(ebtables_cmd_path);
VIR_FREE(iptables_cmd_path); VIR_FREE(iptables_cmd_path);
VIR_FREE(ip6tables_cmd_path); VIR_FREE(ip6tables_cmd_path);

View File

@ -110,10 +110,8 @@ int
virNWFilterRuleInstAddData(virNWFilterRuleInstPtr res, virNWFilterRuleInstAddData(virNWFilterRuleInstPtr res,
void *data) void *data)
{ {
if (VIR_REALLOC_N(res->data, res->ndata+1) < 0) { if (VIR_REALLOC_N(res->data, res->ndata+1) < 0)
virReportOOMError();
return -1; return -1;
}
res->data[res->ndata++] = data; res->data[res->ndata++] = data;
return 0; return 0;
} }
@ -201,10 +199,8 @@ virNWFilterHashTablePtr
virNWFilterCreateVarHashmap(char *macaddr, virNWFilterCreateVarHashmap(char *macaddr,
const virNWFilterVarValuePtr ipaddr) { const virNWFilterVarValuePtr ipaddr) {
virNWFilterHashTablePtr table = virNWFilterHashTableCreate(0); virNWFilterHashTablePtr table = virNWFilterHashTableCreate(0);
if (!table) { if (!table)
virReportOOMError();
return NULL; return NULL;
}
if (virNWFilterVarHashmapAddStdValues(table, macaddr, ipaddr) < 0) { if (virNWFilterVarHashmapAddStdValues(table, macaddr, ipaddr) < 0) {
virNWFilterHashTableFree(table); virNWFilterHashTableFree(table);
@ -303,10 +299,8 @@ virNWFilterRuleInstantiate(virNWFilterTechDriverPtr techdriver,
int i; int i;
virNWFilterRuleInstPtr ret; virNWFilterRuleInstPtr ret;
if (VIR_ALLOC(ret) < 0) { if (VIR_ALLOC(ret) < 0)
virReportOOMError();
return NULL; return NULL;
}
ret->techdriver = techdriver; ret->techdriver = techdriver;
@ -340,10 +334,8 @@ virNWFilterCreateVarsFrom(virNWFilterHashTablePtr vars1,
virNWFilterHashTablePtr vars2) virNWFilterHashTablePtr vars2)
{ {
virNWFilterHashTablePtr res = virNWFilterHashTableCreate(0); virNWFilterHashTablePtr res = virNWFilterHashTableCreate(0);
if (!res) { if (!res)
virReportOOMError();
return NULL; return NULL;
}
if (virNWFilterHashTablePutAll(vars1, res) < 0) if (virNWFilterHashTablePutAll(vars1, res) < 0)
goto err_exit; goto err_exit;
@ -415,7 +407,6 @@ _virNWFilterInstantiateRec(virNWFilterTechDriverPtr techdriver,
} }
if (VIR_REALLOC_N(*insts, (*nEntries)+1) < 0) { if (VIR_REALLOC_N(*insts, (*nEntries)+1) < 0) {
virReportOOMError();
rc = -1; rc = -1;
break; break;
} }
@ -441,7 +432,6 @@ _virNWFilterInstantiateRec(virNWFilterTechDriverPtr techdriver,
virNWFilterCreateVarsFrom(inc->params, virNWFilterCreateVarsFrom(inc->params,
vars); vars);
if (!tmpvars) { if (!tmpvars) {
virReportOOMError();
rc = -1; rc = -1;
virNWFilterObjUnlock(obj); virNWFilterObjUnlock(obj);
break; break;
@ -553,7 +543,6 @@ virNWFilterDetermineMissingVarsRec(virNWFilterDefPtr filter,
virNWFilterCreateVarsFrom(inc->params, virNWFilterCreateVarsFrom(inc->params,
vars); vars);
if (!tmpvars) { if (!tmpvars) {
virReportOOMError();
rc = -1; rc = -1;
virNWFilterObjUnlock(obj); virNWFilterObjUnlock(obj);
break; break;
@ -611,10 +600,8 @@ virNWFilterRuleInstancesToArray(int nEntries,
if ((*nptrs) == 0) if ((*nptrs) == 0)
return 0; return 0;
if (VIR_ALLOC_N((*ptrs), (*nptrs)) < 0) { if (VIR_ALLOC_N((*ptrs), (*nptrs)) < 0)
virReportOOMError();
return -1; return -1;
}
(*nptrs) = 0; (*nptrs) = 0;
@ -674,7 +661,6 @@ virNWFilterInstantiate(const unsigned char *vmuuid ATTRIBUTE_UNUSED,
virNWFilterHashTablePtr missing_vars = virNWFilterHashTableCreate(0); virNWFilterHashTablePtr missing_vars = virNWFilterHashTableCreate(0);
if (!missing_vars) { if (!missing_vars) {
virReportOOMError();
rc = -1; rc = -1;
goto err_exit; goto err_exit;
} }

View File

@ -143,10 +143,8 @@ virNWFilterLockIface(const char *ifname) {
ifaceLock = virHashLookup(ifaceLockMap, ifname); ifaceLock = virHashLookup(ifaceLockMap, ifname);
if (!ifaceLock) { if (!ifaceLock) {
if (VIR_ALLOC(ifaceLock) < 0) { if (VIR_ALLOC(ifaceLock) < 0)
virReportOOMError();
goto err_exit; goto err_exit;
}
if (virMutexInitRecursive(&ifaceLock->lock) < 0) { if (virMutexInitRecursive(&ifaceLock->lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -699,16 +697,12 @@ virNWFilterLearnIPAddress(virNWFilterTechDriverPtr techdriver,
return -1; return -1;
} }
if (VIR_ALLOC(req) < 0) { if (VIR_ALLOC(req) < 0)
virReportOOMError();
goto err_no_req; goto err_no_req;
}
ht = virNWFilterHashTableCreate(0); ht = virNWFilterHashTableCreate(0);
if (ht == NULL) { if (ht == NULL)
virReportOOMError();
goto err_free_req; goto err_free_req;
}
if (virNWFilterHashTablePutAll(filterparams, ht) < 0) if (virNWFilterHashTablePutAll(filterparams, ht) < 0)
goto err_free_ht; goto err_free_ht;