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

019928
autofs-5.1.1 - implement reinit in file lookup module
019928
019928
From: Ian Kent <raven@themaw.net>
019928
019928
Refactor the file 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_file.c |   85 +++++++++++++++++++++++++++++++++++++------------
019928
 1 file changed, 65 insertions(+), 20 deletions(-)
019928
019928
diff --git a/modules/lookup_file.c b/modules/lookup_file.c
019928
index c32a4cd..aed3cba 100644
019928
--- a/modules/lookup_file.c
019928
+++ b/modules/lookup_file.c
019928
@@ -50,23 +50,13 @@ struct lookup_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
-		free(ctxt);
019928
 		logerr(MODPREFIX "No map name");
019928
 		return 1;
019928
 	}
019928
@@ -74,14 +64,12 @@ int lookup_init(const char *mapfmt,
019928
 	ctxt->mapname = argv[0];
019928
 
019928
 	if (ctxt->mapname[0] != '/') {
019928
-		free(ctxt);
019928
 		logmsg(MODPREFIX
019928
 		     "file map %s is not an absolute pathname", argv[0]);
019928
 		return 1;
019928
 	}
019928
 
019928
 	if (access(ctxt->mapname, R_OK)) {
019928
-		free(ctxt);
019928
 		warn(LOGOPT_NONE, MODPREFIX
019928
 		    "file map %s missing or not readable", argv[0]);
019928
 		return 1;
019928
@@ -95,19 +83,51 @@ int lookup_init(const char *mapfmt,
019928
 
019928
 	ctxt->opts_argv = copy_argv(argc, (const char **) argv);
019928
 	if (ctxt->opts_argv == NULL) {
019928
-		free(ctxt);
019928
 		warn(LOGOPT_NONE, MODPREFIX "failed to duplicate options");
019928
 		return 1;
019928
 	}
019928
 	ctxt->opts_argc = argc;
019928
 
019928
-	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc, argv);
019928
-	if (!ctxt->parse) {
019928
+	if (reinit) {
019928
+		ret = reinit_parse(ctxt->parse, mapfmt, MODPREFIX, argc, argv);
019928
+		if (ret)
019928
+			logmsg(MODPREFIX "failed to reinit parse context");
019928
+	} else {
019928
+		ctxt->parse = open_parse(mapfmt, MODPREFIX, argc, argv);
019928
+		if (!ctxt->parse) {
019928
+			logmsg(MODPREFIX "failed to open parse context");
019928
+			ret = 1;
019928
+		}
019928
+	}
019928
+
019928
+	if (ret)
019928
 		free_argv(ctxt->opts_argc, ctxt->opts_argv);
019928
+
019928
+	return ret;
019928
+}
019928
+
019928
+int lookup_init(const char *mapfmt,
019928
+		int argc, const char *const *argv,
019928
+		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
@@ -116,6 +136,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_argv(ctxt->opts_argc, ctxt->opts_argv);
019928
+	free(ctxt);
019928
+
019928
 	return 0;
019928
 }
019928