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

019928
autofs-5.1.1 - implement reinit in sss lookup module
019928
019928
From: Ian Kent <raven@themaw.net>
019928
019928
Refactor the sss 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_sss.c |  130 +++++++++++++++++++++++++++++++++++++-------------
019928
 1 file changed, 95 insertions(+), 35 deletions(-)
019928
019928
diff --git a/modules/lookup_sss.c b/modules/lookup_sss.c
019928
index c58a272..2f32e94 100644
019928
--- a/modules/lookup_sss.c
019928
+++ b/modules/lookup_sss.c
019928
@@ -56,39 +56,16 @@ 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 open_sss_lib(struct lookup_context *ctxt)
019928
 {
019928
-	struct lookup_context *ctxt;
019928
-	char buf[MAX_ERR_BUF];
019928
 	char dlbuf[PATH_MAX];
019928
 	char *estr;
019928
 	void *dh;
019928
 	size_t size;
019928
 
019928
-	*context = NULL;
019928
-
019928
-	ctxt = malloc(sizeof(struct lookup_context));
019928
-	if (!ctxt) {
019928
-		estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
-		logerr(MODPREFIX "malloc: %s", estr);
019928
-		return 1;
019928
-	}
019928
-
019928
-	if (argc < 1) {
019928
-		free(ctxt);
019928
-		logerr(MODPREFIX "No map name");
019928
-		return 1;
019928
-	}
019928
-	ctxt->mapname = argv[0];
019928
-
019928
-	if (!mapfmt)
019928
-		mapfmt = MAPFMT_DEFAULT;
019928
-
019928
 	size = snprintf(dlbuf, sizeof(dlbuf),
019928
 			"%s/%s.so", SSS_LIB_DIR, SSS_SO_NAME);
019928
 	if (size >= sizeof(dlbuf)) {
019928
-		free(ctxt);
019928
 		logmsg(MODPREFIX "sss library path too long");
019928
 		return 1;
019928
 	}
019928
@@ -96,7 +73,6 @@ int lookup_init(const char *mapfmt,
019928
 	dh = dlopen(dlbuf, RTLD_LAZY);
019928
 	if (!dh) {
019928
 		logerr(MODPREFIX "failed to open %s: %s", dlbuf, dlerror());
019928
-		free(ctxt);
019928
 		return 1;
019928
 	}
019928
 	ctxt->dlhandle = dh;
019928
@@ -117,15 +93,6 @@ int lookup_init(const char *mapfmt,
019928
 	if (!ctxt->setautomntent)
019928
 		goto lib_names_fail;
019928
 
019928
-	ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
-	if (!ctxt->parse) {
019928
-		logmsg(MODPREFIX "failed to open parse context");
019928
-		dlclose(dh);
019928
-		free(ctxt);
019928
-		return 1;
019928
-	}
019928
-	*context = ctxt;
019928
-
019928
 	return 0;
019928
 
019928
 lib_names_fail:
019928
@@ -134,13 +101,106 @@ lib_names_fail:
019928
 	else
019928
 		logerr(MODPREFIX "dlsym: %s", estr);
019928
 	dlclose(dh);
019928
-	free(ctxt);
019928
+
019928
 	return 1;
019928
 }
019928
 
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
+	int ret = 0;
019928
+
019928
+	if (argc < 1) {
019928
+		logerr(MODPREFIX "No map name");
019928
+		ret = 1;
019928
+		goto out;
019928
+	}
019928
+	ctxt->mapname = argv[0];
019928
+
019928
+	if (!mapfmt)
019928
+		mapfmt = MAPFMT_DEFAULT;
019928
+
019928
+	if (!reinit) {
019928
+		ret = open_sss_lib(ctxt);
019928
+		if (ret)
019928
+			goto out;
019928
+	}
019928
+
019928
+	if (reinit) {
019928
+		ret = reinit_parse(ctxt->parse, mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
+		if (ret)
019928
+			logmsg(MODPREFIX "failed to reinit parse context");
019928
+	} else {
019928
+		ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
019928
+		if (!ctxt->parse) {
019928
+			logmsg(MODPREFIX "failed to open parse context");
019928
+			dlclose(ctxt->dlhandle);
019928
+			ret = 1;
019928
+		}
019928
+	}
019928
+out:
019928
+	return ret;
019928
+}
019928
+
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
+	char *estr;
019928
+
019928
+	*context = NULL;
019928
+
019928
+	ctxt = malloc(sizeof(struct lookup_context));
019928
+	if (!ctxt) {
019928
+		estr = strerror_r(errno, buf, MAX_ERR_BUF);
019928
+		logerr(MODPREFIX "malloc: %s", estr);
019928
+		return 1;
019928
+	}
019928
+
019928
+	if (do_init(mapfmt, argc, argv, ctxt, 0)) {
019928
+		free(ctxt);
019928
+		return 1;
019928
+	}
019928
+
019928
+	*context = ctxt;
019928
+
019928
+	return 0;
019928
+}
019928
+
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
+
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
+	new->dlhandle = ctxt->dlhandle;
019928
+	new->setautomntent = ctxt->setautomntent;
019928
+	new->getautomntent_r = ctxt->getautomntent_r;
019928
+	new->getautomntbyname_r = ctxt->getautomntbyname_r;
019928
+	new->endautomntent = ctxt->endautomntent;
019928
+
019928
+	*context = new;
019928
+
019928
+	free(ctxt);
019928
+
019928
 	return 0;
019928
 }
019928