Ian Kent 1cd346
autofs-5.1.1 - implement reinit in hesiod lookup module
Ian Kent 1cd346
Ian Kent 1cd346
From: Ian Kent <raven@themaw.net>
Ian Kent 1cd346
Ian Kent 1cd346
Refactor the hesiod lookup module to add an implementation for the newly
Ian Kent 1cd346
added reinit entry point.
Ian Kent 1cd346
Ian Kent 1cd346
Signed-off-by: Ian Kent <raven@themaw.net>
Ian Kent 1cd346
---
Ian Kent 1cd346
 modules/lookup_hesiod.c |   96 ++++++++++++++++++++++++++++++++++++-----------
Ian Kent 1cd346
 1 file changed, 74 insertions(+), 22 deletions(-)
Ian Kent 1cd346
Ian Kent 1cd346
diff --git a/modules/lookup_hesiod.c b/modules/lookup_hesiod.c
Ian Kent 1cd346
index de5ec08..c0f7f51 100644
Ian Kent 1cd346
--- a/modules/lookup_hesiod.c
Ian Kent 1cd346
+++ b/modules/lookup_hesiod.c
Ian Kent 1cd346
@@ -37,24 +37,12 @@ static pthread_mutex_t hesiod_mutex = PTHREAD_MUTEX_INITIALIZER;
Ian Kent 1cd346
 
Ian Kent 1cd346
 int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
Ian Kent 1cd346
 
Ian Kent 1cd346
-/* This initializes a context (persistent non-global data) for queries to
Ian Kent 1cd346
-   this module. */
Ian Kent 1cd346
-int lookup_init(const char *mapfmt,
Ian Kent 1cd346
-		int argc, const char *const *argv, void **context)
Ian Kent 1cd346
+static int do_init(const char *mapfmt,
Ian Kent 1cd346
+		   int argc, const char *const *argv,
Ian Kent 1cd346
+		   struct lookup_context *ctxt, unsigned int reinit)
Ian Kent 1cd346
 {
Ian Kent 1cd346
-	struct lookup_context *ctxt = NULL;
Ian Kent 1cd346
 	char buf[MAX_ERR_BUF];
Ian Kent 1cd346
-
Ian Kent 1cd346
-	*context = NULL;
Ian Kent 1cd346
-
Ian Kent 1cd346
-	/* If we can't build a context, bail. */
Ian Kent 1cd346
-	ctxt = malloc(sizeof(struct lookup_context));
Ian Kent 1cd346
-	if (!ctxt) {
Ian Kent 1cd346
-		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Ian Kent 1cd346
-		logerr(MODPREFIX "malloc: %s", estr);
Ian Kent 1cd346
-		return 1;
Ian Kent 1cd346
-	}
Ian Kent 1cd346
-	memset(ctxt, 0, sizeof(struct lookup_context));
Ian Kent 1cd346
+	int ret = 0;
Ian Kent 1cd346
 
Ian Kent 1cd346
 	/* Initialize the resolver. */
Ian Kent 1cd346
 	res_init();
Ian Kent 1cd346
@@ -63,7 +51,6 @@ int lookup_init(const char *mapfmt,
Ian Kent 1cd346
 	if (hesiod_init(&(ctxt->hesiod_context)) != 0) {
Ian Kent 1cd346
 		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Ian Kent 1cd346
 		logerr(MODPREFIX "hesiod_init(): %s", estr);
Ian Kent 1cd346
-		free(ctxt);
Ian Kent 1cd346
 		return 1;
Ian Kent 1cd346
 	}
Ian Kent 1cd346
 
Ian Kent 1cd346
@@ -75,9 +62,9 @@ int lookup_init(const char *mapfmt,
Ian Kent 1cd346
 		/* amd formated hesiod maps have a map name */
Ian Kent 1cd346
 		const char *mapname = argv[0];
Ian Kent 1cd346
 		if (strncmp(mapname, AMD_MAP_PREFIX, AMD_MAP_PREFIX_LEN)) {
Ian Kent 1cd346
+			hesiod_end(ctxt->hesiod_context);
Ian Kent 1cd346
 			logerr(MODPREFIX
Ian Kent 1cd346
 			      "incorrect prefix for hesiod map %s", mapname);
Ian Kent 1cd346
-			free(ctxt);
Ian Kent 1cd346
 			return 1;
Ian Kent 1cd346
 		}
Ian Kent 1cd346
 		ctxt->mapname = mapname;
Ian Kent 1cd346
@@ -85,13 +72,52 @@ int lookup_init(const char *mapfmt,
Ian Kent 1cd346
 		argv++;
Ian Kent 1cd346
 	}
Ian Kent 1cd346
 
Ian Kent 1cd346
-	/* Open the parser, if we can. */
Ian Kent 1cd346
-	ctxt->parser = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
Ian Kent 1cd346
-	if (!ctxt->parser) {
Ian Kent 1cd346
-		logerr(MODPREFIX "failed to open parse context");
Ian Kent 1cd346
+	if (reinit) {
Ian Kent 1cd346
+		ret = reinit_parse(ctxt->parser, mapfmt,
Ian Kent 1cd346
+				   MODPREFIX, argc - 1, argv - 1);
Ian Kent 1cd346
+		if (ret)
Ian Kent 1cd346
+			logerr(MODPREFIX "failed to reinit parse context");
Ian Kent 1cd346
+	} else {
Ian Kent 1cd346
+		ctxt->parser = open_parse(mapfmt,
Ian Kent 1cd346
+					  MODPREFIX, argc - 1, argv + 1);
Ian Kent 1cd346
+		if (!ctxt->parser) {
Ian Kent 1cd346
+			logerr(MODPREFIX "failed to open parse context");
Ian Kent 1cd346
+			ret = 1;
Ian Kent 1cd346
+		}
Ian Kent 1cd346
+	}
Ian Kent 1cd346
+
Ian Kent 1cd346
+	if (ret)
Ian Kent 1cd346
+		hesiod_end(ctxt->hesiod_context);
Ian Kent 1cd346
+
Ian Kent 1cd346
+	return ret;
Ian Kent 1cd346
+}
Ian Kent 1cd346
+
Ian Kent 1cd346
+/* This initializes a context (persistent non-global data) for queries to
Ian Kent 1cd346
+   this module. */
Ian Kent 1cd346
+int lookup_init(const char *mapfmt,
Ian Kent 1cd346
+		int argc, const char *const *argv, void **context)
Ian Kent 1cd346
+{
Ian Kent 1cd346
+	struct lookup_context *ctxt;
Ian Kent 1cd346
+	char buf[MAX_ERR_BUF];
Ian Kent 1cd346
+	int ret;
Ian Kent 1cd346
+
Ian Kent 1cd346
+	*context = NULL;
Ian Kent 1cd346
+
Ian Kent 1cd346
+	/* If we can't build a context, bail. */
Ian Kent 1cd346
+	ctxt = malloc(sizeof(struct lookup_context));
Ian Kent 1cd346
+	if (!ctxt) {
Ian Kent 1cd346
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Ian Kent 1cd346
+		logerr(MODPREFIX "malloc: %s", estr);
Ian Kent 1cd346
+		return 1;
Ian Kent 1cd346
+	}
Ian Kent 1cd346
+	memset(ctxt, 0, sizeof(struct lookup_context));
Ian Kent 1cd346
+
Ian Kent 1cd346
+	ret = do_init(mapfmt, argc, argv, ctxt, 0);
Ian Kent 1cd346
+	if (ret) {
Ian Kent 1cd346
 		free(ctxt);
Ian Kent 1cd346
 		return 1;
Ian Kent 1cd346
 	}
Ian Kent 1cd346
+
Ian Kent 1cd346
 	*context = ctxt;
Ian Kent 1cd346
 
Ian Kent 1cd346
 	return 0;
Ian Kent 1cd346
@@ -100,6 +126,32 @@ int lookup_init(const char *mapfmt,
Ian Kent 1cd346
 int lookup_reinit(const char *mapfmt,
Ian Kent 1cd346
 		  int argc, const char *const *argv, void **context)
Ian Kent 1cd346
 {
Ian Kent 1cd346
+	struct lookup_context *ctxt = (struct lookup_context *) *context;
Ian Kent 1cd346
+	struct lookup_context *new;
Ian Kent 1cd346
+	char buf[MAX_ERR_BUF];
Ian Kent 1cd346
+	int ret;
Ian Kent 1cd346
+
Ian Kent 1cd346
+	/* If we can't build a context, bail. */
Ian Kent 1cd346
+	new = malloc(sizeof(struct lookup_context));
Ian Kent 1cd346
+	if (!new) {
Ian Kent 1cd346
+		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Ian Kent 1cd346
+		logerr(MODPREFIX "malloc: %s", estr);
Ian Kent 1cd346
+		return 1;
Ian Kent 1cd346
+	}
Ian Kent 1cd346
+	memset(new, 0, sizeof(struct lookup_context));
Ian Kent 1cd346
+
Ian Kent 1cd346
+	new->parser = ctxt->parser;
Ian Kent 1cd346
+	ret = do_init(mapfmt, argc, argv, new, 1);
Ian Kent 1cd346
+	if (ret) {
Ian Kent 1cd346
+		free(new);
Ian Kent 1cd346
+		return 1;
Ian Kent 1cd346
+	}
Ian Kent 1cd346
+
Ian Kent 1cd346
+	*context = new;
Ian Kent 1cd346
+
Ian Kent 1cd346
+	hesiod_end(ctxt->hesiod_context);
Ian Kent 1cd346
+	free(ctxt);
Ian Kent 1cd346
+
Ian Kent 1cd346
 	return 0;
Ian Kent 1cd346
 }
Ian Kent 1cd346