Blame SOURCES/autofs-5.1.1-implement-reinit-in-yp-lookup-module.patch

019928
autofs-5.1.1 - implement reinit in yp lookup module
019928
019928
From: Ian Kent <raven@themaw.net>
019928
019928
Refactor the yp lookup module to add an implementation for the newly
019928
added reinit entry point.
019928
019928
Signed-off-by: Ian Kent <raven@themaw.net>
019928
---
019928
 modules/lookup_yp.c |   93 +++++++++++++++++++++++++++++++++++++++------------
019928
 1 file changed, 71 insertions(+), 22 deletions(-)
019928
019928
diff --git a/modules/lookup_yp.c b/modules/lookup_yp.c
019928
index 1e5a7ed..e31c2cf 100644
019928
--- a/modules/lookup_yp.c
019928
+++ b/modules/lookup_yp.c
019928
@@ -103,27 +103,18 @@ static unsigned int get_map_order(const char *domain, const char *map)
019928
 	return (unsigned int) last_changed;
019928
 }
019928
 
019928
-int lookup_init(const char *mapfmt,
019928
-		int argc, const char *const *argv, void **context)
019928
+static int do_init(const char *mapfmt,
019928
+		   int argc, const char *const *argv,
019928
+		   struct lookup_context *ctxt, unsigned int reinit)
019928
 {
019928
-	struct lookup_context *ctxt;
019928
 	char buf[MAX_ERR_BUF];
019928
 	int err;
019928
-
019928
-	*context = NULL;
019928
-
019928
-	ctxt = malloc(sizeof(struct lookup_context));
019928
-	if (!ctxt) {
019928
-		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
-		logerr(MODPREFIX "malloc: %s", estr);
019928
-		return 1;
019928
-	}
019928
-	memset(ctxt, 0, sizeof(struct lookup_context));
019928
+	int ret = 0;
019928
 
019928
 	if (argc < 1) {
019928
-		free(ctxt);
019928
 		logerr(MODPREFIX "no map name");
019928
-		return 1;
019928
+		ret = 1;
019928
+		goto out;
019928
 	}
019928
 	ctxt->mapname = argv[0];
019928
 	ctxt->check_defaults = 1;
019928
@@ -138,15 +129,15 @@ int lookup_init(const char *mapfmt,
019928
 		if (err) {
019928
 			logerr(MODPREFIX
019928
 			      "map %s: %s", ctxt->mapname, yperr_string(err));
019928
-			free(ctxt);
019928
-			return 1;
019928
+			ret = 1;
019928
+			goto out;
019928
 		}
019928
 		ctxt->domainname = strdup(domainname);
019928
 		if (!ctxt->domainname) {
019928
 			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
 			logerr(MODPREFIX "strdup: %s", estr);
019928
-			free(ctxt);
019928
-			return 1;
019928
+			ret = 1;
019928
+			goto out;
019928
 		}
019928
 	}
019928
 
019928
@@ -155,12 +146,45 @@ int lookup_init(const char *mapfmt,
019928
 	if (!mapfmt)
019928
 		mapfmt = MAPFMT_DEFAULT;
019928
 
019928
-	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
-	if (!ctxt->parse) {
019928
+	if (reinit) {
019928
+		ret = reinit_parse(ctxt->parse, mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
+		if (ret)
019928
+			logmsg(MODPREFIX "failed to reinit parse context");
019928
+	} else {
019928
+		ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
+		if (!ctxt->parse) {
019928
+			logmsg(MODPREFIX "failed to open parse context");
019928
+			ret = 1;
019928
+		}
019928
+	}
019928
+out:
019928
+	if (ret && ctxt->domainname)
019928
+		free(ctxt->domainname);
019928
+
019928
+	return ret;
019928
+}
019928
+
019928
+int lookup_init(const char *mapfmt,
019928
+		int argc, const char *const *argv, void **context)
019928
+{
019928
+	struct lookup_context *ctxt;
019928
+	char buf[MAX_ERR_BUF];
019928
+
019928
+	*context = NULL;
019928
+
019928
+	ctxt = malloc(sizeof(struct lookup_context));
019928
+	if (!ctxt) {
019928
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
+		logerr(MODPREFIX "malloc: %s", estr);
019928
+		return 1;
019928
+	}
019928
+	memset(ctxt, 0, sizeof(struct lookup_context));
019928
+
019928
+	if (do_init(mapfmt, argc, argv, ctxt, 0)) {
019928
 		free(ctxt);
019928
-		logmsg(MODPREFIX "failed to open parse context");
019928
 		return 1;
019928
 	}
019928
+
019928
 	*context = ctxt;
019928
 
019928
 	return 0;
019928
@@ -169,6 +193,31 @@ int lookup_init(const char *mapfmt,
019928
 int lookup_reinit(const char *mapfmt,
019928
 		  int argc, const char *const *argv, void **context)
019928
 {
019928
+	struct lookup_context *ctxt = (struct lookup_context *) *context;
019928
+	struct lookup_context *new;
019928
+	char buf[MAX_ERR_BUF];
019928
+	int ret;
019928
+
019928
+	new = malloc(sizeof(struct lookup_context));
019928
+	if (!new) {
019928
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
+		logerr(MODPREFIX "malloc: %s", estr);
019928
+		return 1;
019928
+	}
019928
+	memset(new, 0, sizeof(struct lookup_context));
019928
+
019928
+	new->parse = ctxt->parse;
019928
+	ret = do_init(mapfmt, argc, argv, new, 1);
019928
+	if (ret) {
019928
+		free(new);
019928
+		return 1;
019928
+	}
019928
+
019928
+	*context = new;
019928
+
019928
+	free(ctxt->domainname);
019928
+	free(ctxt);
019928
+
019928
 	return 0;
019928
 }
019928