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

4d476f
autofs-5.1.1 - implement reinit in program lookup module
4d476f
4d476f
From: Ian Kent <raven@themaw.net>
4d476f
4d476f
Refactor the program lookup module to add an implementation for the newly
4d476f
added reinit entry point.
4d476f
4d476f
Signed-off-by: Ian Kent <raven@themaw.net>
4d476f
---
4d476f
 modules/lookup_program.c |   98 ++++++++++++++++++++++++++++++++++++----------
4d476f
 1 file changed, 76 insertions(+), 22 deletions(-)
4d476f
4d476f
diff --git a/modules/lookup_program.c b/modules/lookup_program.c
4d476f
index fa4f54d..3e9c448 100644
4d476f
--- a/modules/lookup_program.c
4d476f
+++ b/modules/lookup_program.c
4d476f
@@ -49,53 +49,82 @@ struct parse_context {
4d476f
 
4d476f
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
4d476f
 
4d476f
-int lookup_init(const char *mapfmt,
4d476f
-		int argc, const char *const *argv, void **context)
4d476f
+static int do_init(const char *mapfmt,
4d476f
+		   int argc, const char *const *argv,
4d476f
+		   struct lookup_context *ctxt, unsigned int reinit)
4d476f
 {
4d476f
-	struct lookup_context *ctxt;
4d476f
-	char buf[MAX_ERR_BUF];
4d476f
-
4d476f
-	*context = NULL;
4d476f
-
4d476f
-	ctxt = malloc(sizeof(struct lookup_context));
4d476f
-	if (!ctxt) {
4d476f
-		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
4d476f
-		logerr(MODPREFIX "malloc: %s", estr);
4d476f
-		return 1;
4d476f
-	}
4d476f
+	int ret = 0;
4d476f
 
4d476f
 	if (argc < 1) {
4d476f
 		logmsg(MODPREFIX "No map name");
4d476f
-		free(ctxt);
4d476f
-		return 1;
4d476f
+		ret = 1;
4d476f
+		goto out;
4d476f
 	}
4d476f
 	ctxt->mapname = argv[0];
4d476f
 
4d476f
 	if (ctxt->mapname[0] != '/') {
4d476f
 		logmsg(MODPREFIX "program map %s is not an absolute pathname",
4d476f
 		     ctxt->mapname);
4d476f
-		free(ctxt);
4d476f
-		return 1;
4d476f
+		ret = 1;
4d476f
+		goto out;
4d476f
 	}
4d476f
 
4d476f
 	if (access(ctxt->mapname, X_OK)) {
4d476f
 		logmsg(MODPREFIX "program map %s missing or not executable",
4d476f
 		     ctxt->mapname);
4d476f
-		free(ctxt);
4d476f
-		return 1;
4d476f
+		ret = 1;
4d476f
+		goto out;
4d476f
 	}
4d476f
 
4d476f
 	if (!mapfmt)
4d476f
 		mapfmt = MAPFMT_DEFAULT;
4d476f
 
4d476f
 	ctxt->mapfmt = strdup(mapfmt);
4d476f
+	if (!ctxt->mapfmt) {
4d476f
+		logmsg(MODPREFIX "failed to allocate storage for map format");
4d476f
+		ret = 1;
4d476f
+		goto out;
4d476f
+	}
4d476f
 
4d476f
-	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
4d476f
-	if (!ctxt->parse) {
4d476f
-		logmsg(MODPREFIX "failed to open parse context");
4d476f
+	if (reinit) {
4d476f
+		ret = reinit_parse(ctxt->parse, mapfmt, MODPREFIX, argc - 1, argv + 1);
4d476f
+		if (ret)
4d476f
+			logmsg(MODPREFIX "failed to reinit parse context");
4d476f
+	} else {
4d476f
+		ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
4d476f
+		if (!ctxt->parse) {
4d476f
+			logmsg(MODPREFIX "failed to open parse context");
4d476f
+			ret = 1;
4d476f
+		}
4d476f
+	}
4d476f
+out:
4d476f
+	if (ret && ctxt->mapfmt)
4d476f
+		free(ctxt->mapfmt);
4d476f
+
4d476f
+	return ret;
4d476f
+}
4d476f
+
4d476f
+int lookup_init(const char *mapfmt,
4d476f
+		int argc, const char *const *argv, void **context)
4d476f
+{
4d476f
+	struct lookup_context *ctxt;
4d476f
+	char buf[MAX_ERR_BUF];
4d476f
+
4d476f
+	*context = NULL;
4d476f
+
4d476f
+	ctxt = malloc(sizeof(struct lookup_context));
4d476f
+	if (!ctxt) {
4d476f
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
4d476f
+		logerr(MODPREFIX "malloc: %s", estr);
4d476f
+		return 1;
4d476f
+	}
4d476f
+	memset(ctxt, 0, sizeof(struct lookup_context));
4d476f
+
4d476f
+	if (do_init(mapfmt, argc, argv, ctxt, 0)) {
4d476f
 		free(ctxt);
4d476f
 		return 1;
4d476f
 	}
4d476f
+
4d476f
 	*context = ctxt;
4d476f
 
4d476f
 	return 0;
4d476f
@@ -104,6 +133,31 @@ int lookup_init(const char *mapfmt,
4d476f
 int lookup_reinit(const char *mapfmt,
4d476f
 		  int argc, const char *const *argv, void **context)
4d476f
 {
4d476f
+	struct lookup_context *ctxt = (struct lookup_context *) *context;
4d476f
+	struct lookup_context *new;
4d476f
+	char buf[MAX_ERR_BUF];
4d476f
+	int ret;
4d476f
+
4d476f
+	new = malloc(sizeof(struct lookup_context));
4d476f
+	if (!new) {
4d476f
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
4d476f
+		logerr(MODPREFIX "malloc: %s", estr);
4d476f
+		return 1;
4d476f
+	}
4d476f
+	memset(new, 0, sizeof(struct lookup_context));
4d476f
+
4d476f
+	new->parse = ctxt->parse;
4d476f
+	ret = do_init(mapfmt, argc, argv, new, 1);
4d476f
+	if (ret) {
4d476f
+		free(new);
4d476f
+		return 1;
4d476f
+	}
4d476f
+
4d476f
+	*context = new;
4d476f
+
4d476f
+	free(ctxt->mapfmt);
4d476f
+	free(ctxt);
4d476f
+
4d476f
 	return 0;
4d476f
 }
4d476f