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

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