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

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