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

019928
autofs-5.1.1 - implement reinit in program lookup module
019928
019928
From: Ian Kent <raven@themaw.net>
019928
019928
Refactor the program 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_program.c |   98 ++++++++++++++++++++++++++++++++++++----------
019928
 1 file changed, 76 insertions(+), 22 deletions(-)
019928
019928
diff --git a/modules/lookup_program.c b/modules/lookup_program.c
019928
index fa4f54d..3e9c448 100644
019928
--- a/modules/lookup_program.c
019928
+++ b/modules/lookup_program.c
019928
@@ -49,53 +49,82 @@ struct parse_context {
019928
 
019928
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
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
-
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
+	int ret = 0;
019928
 
019928
 	if (argc < 1) {
019928
 		logmsg(MODPREFIX "No map name");
019928
-		free(ctxt);
019928
-		return 1;
019928
+		ret = 1;
019928
+		goto out;
019928
 	}
019928
 	ctxt->mapname = argv[0];
019928
 
019928
 	if (ctxt->mapname[0] != '/') {
019928
 		logmsg(MODPREFIX "program map %s is not an absolute pathname",
019928
 		     ctxt->mapname);
019928
-		free(ctxt);
019928
-		return 1;
019928
+		ret = 1;
019928
+		goto out;
019928
 	}
019928
 
019928
 	if (access(ctxt->mapname, X_OK)) {
019928
 		logmsg(MODPREFIX "program map %s missing or not executable",
019928
 		     ctxt->mapname);
019928
-		free(ctxt);
019928
-		return 1;
019928
+		ret = 1;
019928
+		goto out;
019928
 	}
019928
 
019928
 	if (!mapfmt)
019928
 		mapfmt = MAPFMT_DEFAULT;
019928
 
019928
 	ctxt->mapfmt = strdup(mapfmt);
019928
+	if (!ctxt->mapfmt) {
019928
+		logmsg(MODPREFIX "failed to allocate storage for map format");
019928
+		ret = 1;
019928
+		goto out;
019928
+	}
019928
 
019928
-	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
-	if (!ctxt->parse) {
019928
-		logmsg(MODPREFIX "failed to open parse context");
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->mapfmt)
019928
+		free(ctxt->mapfmt);
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
 		return 1;
019928
 	}
019928
+
019928
 	*context = ctxt;
019928
 
019928
 	return 0;
019928
@@ -104,6 +133,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->mapfmt);
019928
+	free(ctxt);
019928
+
019928
 	return 0;
019928
 }
019928