Blob Blame History Raw
autofs-5.1.1 - implement reinit in hesiod lookup module

From: Ian Kent <raven@themaw.net>

Refactor the hesiod lookup module to add an implementation for the newly
added reinit entry point.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 modules/lookup_hesiod.c |   96 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 74 insertions(+), 22 deletions(-)

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