[netcf-devel] [PATCH 2/4] Use ERR_NOMEM macro to indicate allocation failure
David Lutterkort
lutter at redhat.com
Thu Sep 10 19:28:44 UTC 2009
A separate macro is more convenient, and avoids headaches when using things
like ERR_COND_BAIL(r < 0, ncf, ENOMEM) since ENOMEM is also defined in
errno.h.
---
src/drv_initscripts.c | 26 +++++++++++++-------------
src/dutil.c | 20 ++++++++++----------
src/dutil_linux.c | 12 ++++++------
src/internal.h | 9 +++++++++
src/netcf.c | 2 +-
5 files changed, 39 insertions(+), 30 deletions(-)
diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 1006bcf..86bf475 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -201,7 +201,7 @@ static char *find_ifcfg_path(struct netcf *ncf, const char *name) {
/* if ifcfg-NAME exists, use that */
r = xasprintf(&path, "%s/ifcfg-%s", network_scripts_path, name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
nmatches = aug_match(aug, path, NULL);
ERR_COND_BAIL(nmatches < 0, ncf, EOTHER);
@@ -247,7 +247,7 @@ static int uniq_ifcfg_paths(struct netcf *ncf,
/* List unique device names */
r = ALLOC_N(devnames, ndevs);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
for (int i=0; i < ndevs; i++) {
const char *name = NULL;
@@ -266,7 +266,7 @@ static int uniq_ifcfg_paths(struct netcf *ncf,
/* Find canonical config for each device name */
r = ALLOC_N(*intf, ndevnames);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
for (int i= 0; i < ndevnames; i++) {
(*intf)[i] = find_ifcfg_path(ncf, devnames[i]);
ERR_BAIL(ncf);
@@ -526,7 +526,7 @@ static int list_interface_ids(struct netcf *ncf,
if (is_qualified) {
if (names) {
names[nqualified] = strdup(name);
- ERR_COND_BAIL(names[nqualified] == NULL, ncf, ENOMEM);
+ ERR_NOMEM(names[nqualified] == NULL, ncf);
}
nqualified++;
}
@@ -566,7 +566,7 @@ struct netcf_if *drv_lookup_by_name(struct netcf *ncf, const char *name) {
goto done;
name_dup = strdup(name);
- ERR_COND_BAIL(name_dup == NULL, ncf, ENOMEM);
+ ERR_NOMEM(name_dup == NULL, ncf);
nif = make_netcf_if(ncf, name_dup);
ERR_BAIL(ncf);
@@ -656,7 +656,7 @@ static int aug_put_xml(struct netcf *ncf, xmlDocPtr xml) {
toplevel = 0;
}
r = xasprintf(&lpath, "%s/%s", path, label);
- ERR_THROW(r < 0, ncf, ENOMEM, NULL);
+ ERR_NOMEM(r < 0, ncf);
r = aug_set(aug, lpath, value);
ERR_THROW(r < 0, ncf, EOTHER,
@@ -765,7 +765,7 @@ static int bridge_slaves(struct netcf *ncf, const char *name, char ***slaves) {
(*slaves)[i] = strdup(dev);
free(p);
- ERR_COND_BAIL(slaves[i] == NULL, ncf, ENOMEM);
+ ERR_NOMEM(slaves[i] == NULL, ncf);
}
return nslaves;
error:
@@ -793,7 +793,7 @@ struct netcf_if *drv_define(struct netcf *ncf, const char *xml_str) {
r = xasprintf(&path,
"%s[ DEVICE = '%s' or BRIDGE = '%s' or MASTER = '%s']",
ifcfg_path, name, name, name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
r = aug_rm(aug, path);
ERR_COND_BAIL(r < 0, ncf, EOTHER);
@@ -842,7 +842,7 @@ int drv_undefine(struct netcf_if *nif) {
r = xasprintf(&path,
"%s[ DEVICE = '%s' or BRIDGE = '%s' or MASTER = '%s']",
ifcfg_path, nif->name, nif->name, nif->name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
r = aug_rm(aug, path);
ERR_COND_BAIL(r < 0, ncf, EOTHER);
@@ -881,12 +881,12 @@ int drv_lookup_by_mac_string(struct netcf *ncf, const char *mac,
}
r = ALLOC_N(names, nmatches);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
int cnt = 0;
for (int i = 0; i < nmatches; i++) {
r = xasprintf(&ifcfg, "%s[DEVICE = '%s']", ifcfg_path, matches[i]);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
if (! is_slave(ncf, ifcfg))
names[cnt++] = matches[i];
@@ -894,7 +894,7 @@ int drv_lookup_by_mac_string(struct netcf *ncf, const char *mac,
}
for (int i=0; i < cnt && i < maxifaces; i++) {
char *name = strdup(names[i]);
- ERR_COND_BAIL(name == NULL, ncf, ENOMEM);
+ ERR_NOMEM(name == NULL, ncf);
ifaces[i] = make_netcf_if(ncf, name);
ERR_BAIL(ncf);
}
@@ -924,7 +924,7 @@ const char *drv_mac_string(struct netcf_if *nif) {
if (nif->mac == NULL || STRNEQ(nif->mac, mac)) {
FREE(nif->mac);
nif->mac = strdup(mac);
- ERR_COND_BAIL(nif->mac == NULL, ncf, ENOMEM);
+ ERR_NOMEM(nif->mac == NULL, ncf);
}
/* fallthrough intentional */
error:
diff --git a/src/dutil.c b/src/dutil.c
index c97a426..24516a8 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -74,7 +74,7 @@ struct augeas *get_augeas(struct netcf *ncf) {
char *path;
r = xasprintf(&path, "%s/lenses", ncf->data_dir);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
aug = aug_init(ncf->root, path, AUG_NO_MODL_AUTOLOAD);
FREE(path);
@@ -140,7 +140,7 @@ int defnode(struct netcf *ncf, const char *name, const char *value,
va_end (ap);
if (r < 0)
expr = NULL;
- ERR_THROW(r < 0, ncf, ENOMEM, "failed to format node expression");
+ ERR_NOMEM(r < 0, ncf);
r = aug_defnode(aug, name, expr, value, &created);
ERR_THROW(r < 0, ncf, EOTHER, "failed to define node %s", name);
@@ -165,7 +165,7 @@ int aug_fmt_match(struct netcf *ncf, char ***matches, const char *fmt, ...) {
va_end(args);
if (r < 0) {
path = NULL;
- ERR_COND_BAIL(1, ncf, ENOMEM);
+ ERR_NOMEM(1, ncf);
}
r = aug_match(aug, path, matches);
@@ -193,7 +193,7 @@ xsltStylesheetPtr parse_stylesheet(struct netcf *ncf,
int r;
r = xasprintf(&path, "%s/xml/%s", ncf->data_dir, fname);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
// FIXME: Error checking ??
result = xsltParseStylesheetFile(BAD_CAST path);
@@ -219,12 +219,12 @@ xmlDocPtr apply_stylesheet(struct netcf *ncf, xsltStylesheetPtr style,
int r;
ctxt = xsltNewTransformContext(style, doc);
- ERR_COND_BAIL(ctxt == NULL, ncf, ENOMEM);
+ ERR_NOMEM(ctxt == NULL, ncf);
xsltSetTransformErrorFunc(ctxt, ncf, apply_stylesheet_error);
r = xslt_register_exts(ctxt);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
res = xsltApplyStylesheetUser(style, doc, NULL, NULL, NULL, ctxt);
if ((ctxt->state == XSLT_STATE_ERROR) ||
@@ -251,7 +251,7 @@ char *apply_stylesheet_to_string(struct netcf *ncf, xsltStylesheetPtr style,
r = xsltSaveResultToString((xmlChar **) &result, &result_len,
doc_xfm, style);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
xmlFreeDoc(doc_xfm);
return result;
@@ -278,7 +278,7 @@ xmlRelaxNGPtr rng_parse(struct netcf *ncf, const char *fname) {
int r;
r = xasprintf(&path, "%s/xml/%s", ncf->data_dir, fname);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
ctxt = xmlRelaxNGNewParserCtxt(path);
xmlRelaxNGSetParserErrors(ctxt, rng_error, rng_error, ncf);
@@ -329,7 +329,7 @@ xmlDocPtr parse_xml(struct netcf *ncf, const char *xml_str) {
/* Set up a parser context so we can catch the details of XML errors. */
pctxt = xmlNewParserCtxt();
- ERR_COND_BAIL(pctxt == NULL || pctxt->sax == NULL, ncf, ENOMEM);
+ ERR_NOMEM(pctxt == NULL || pctxt->sax == NULL, ncf);
pctxt->sax->error = catch_xml_error;
pctxt->_private = ncf;
@@ -392,7 +392,7 @@ struct netcf_if *make_netcf_if(struct netcf *ncf, char *name) {
struct netcf_if *result = NULL;
r = make_ref(result);
- ERR_THROW(r < 0, ncf, ENOMEM, NULL);
+ ERR_NOMEM(r < 0, ncf);
result->ncf = ref(ncf);
result->name = name;
return result;
diff --git a/src/dutil_linux.c b/src/dutil_linux.c
index 864bced..154a8b1 100644
--- a/src/dutil_linux.c
+++ b/src/dutil_linux.c
@@ -46,7 +46,7 @@ int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches) {
char *path = NULL, *mac_lower = NULL;
mac_lower = strdup(mac);
- ERR_COND_BAIL(mac_lower == NULL, ncf, ENOMEM);
+ ERR_NOMEM(mac_lower == NULL, ncf);
for (char *s = mac_lower; *s != '\0'; s++)
*s = c_tolower(*s);
@@ -61,7 +61,7 @@ int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches) {
/* Replace with freeable copy */
n = strdup(n);
- ERR_COND_BAIL(n == NULL, ncf, ENOMEM);
+ ERR_NOMEM(n == NULL, ncf);
free((*matches)[i]);
(*matches)[i] = n;
@@ -82,7 +82,7 @@ int aug_get_mac(struct netcf *ncf, const char *intf, const char **mac) {
struct augeas *aug = get_augeas(ncf);
r = xasprintf(&path, "/files/sys/class/net/%s/address/content", intf);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
r = aug_get(aug, path, mac);
/* Messages for a aug_match-fail are handled outside this function */
@@ -112,7 +112,7 @@ void modprobed_alias_bond(struct netcf *ncf, const char *name) {
in Augeas, it's too convoluted */
r = xasprintf(&path,
"/files/etc/modprobe.d/netcf.conf/alias[last()]", name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
nmatches = aug_match(aug, path, NULL);
if (nmatches > 0) {
r = aug_insert(aug, path, "alias", 0);
@@ -125,7 +125,7 @@ void modprobed_alias_bond(struct netcf *ncf, const char *name) {
r = xasprintf(&path,
"/files/etc/modprobe.d/*/alias[ . = '%s']/modulename",
name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
r = aug_set(aug, path, "bonding");
ERR_COND_BAIL(r < 0, ncf, EOTHER);
@@ -143,7 +143,7 @@ void modprobed_unalias_bond(struct netcf *ncf, const char *name) {
r = xasprintf(&path,
"/files/etc/modprobe.d/*/alias[ . = '%s'][modulename = 'bonding']",
name);
- ERR_COND_BAIL(r < 0, ncf, ENOMEM);
+ ERR_NOMEM(r < 0, ncf);
r = aug_rm(aug, path);
ERR_COND_BAIL(r < 0, ncf, EOTHER);
diff --git a/src/internal.h b/src/internal.h
index 9e2f5cd..7205e72 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -112,6 +112,15 @@
ERR_BAIL(ncf); \
} while(0)
+/* Special version of ERR_COND_BAIL(cond, ncf, ENOMEM) to avoid
+ * problems since ENOMEM is defined in errno.h
+ */
+#define ERR_NOMEM(cond, ncf) \
+ if (cond) { \
+ (ncf)->errcode = NETCF_ENOMEM; \
+ goto error; \
+ }
+
#define ERR_THROW(cond, ncf, err, fmt ...) \
do { \
if (cond) { \
diff --git a/src/netcf.c b/src/netcf.c
index 5977329..8073af5 100644
--- a/src/netcf.c
+++ b/src/netcf.c
@@ -247,7 +247,7 @@ int run_program(struct netcf *ncf, const char *const *argv) {
char *argv_str = argv_to_string(argv);
int ret = -1;
- ERR_COND_BAIL(argv_str == NULL, ncf, ENOMEM);
+ ERR_NOMEM(argv_str == NULL, ncf);
/* BIG FIXME!!! Before any general release, this *must* be
* replaced with a call to a function similar to libVirt's
--
1.6.2.5
More information about the netcf-devel
mailing list