Blame SOURCES/autofs-5.1.6-refactor-sss-getautomntbyname.patch

1b50e3
autofs-5.1.6 - refactor sss getautomntbyname()
1b50e3
1b50e3
From: Ian Kent <raven@themaw.net>
1b50e3
1b50e3
Refactor the sss getautomntbyname_r() to consolidate the error handling
1b50e3
in one location.
1b50e3
1b50e3
Also add the missing ECONREFUSED handling while we are at it.
1b50e3
1b50e3
Signed-off-by: Ian Kent <raven@themaw.net>
1b50e3
---
1b50e3
 CHANGELOG            |    1 
1b50e3
 modules/lookup_sss.c |  110 +++++++++++++++++++++++++++++---------------------
1b50e3
 2 files changed, 65 insertions(+), 46 deletions(-)
1b50e3
1b50e3
diff --git a/CHANGELOG b/CHANGELOG
1b50e3
index e48e2c0..8b662d7 100644
1b50e3
--- a/CHANGELOG
1b50e3
+++ b/CHANGELOG
1b50e3
@@ -106,6 +106,7 @@ xx/xx/2018 autofs-5.1.5
1b50e3
 - move readall into struct master.
1b50e3
 - sss introduce a flag to indicate map being read.
1b50e3
 - update sss timeout documentation.
1b50e3
+- refactor sss getautomntbyname().
1b50e3
 
1b50e3
 19/12/2017 autofs-5.1.4
1b50e3
 - fix spec file url.
1b50e3
diff --git a/modules/lookup_sss.c b/modules/lookup_sss.c
1b50e3
index 3c0ffde..1a56ea1 100644
1b50e3
--- a/modules/lookup_sss.c
1b50e3
+++ b/modules/lookup_sss.c
1b50e3
@@ -566,6 +566,39 @@ free:
1b50e3
 	return err;
1b50e3
 }
1b50e3
 
1b50e3
+static int getautomntbyname(unsigned int logopt,
1b50e3
+			    struct lookup_context *ctxt,
1b50e3
+			    char *key, char **value, void *sss_ctxt)
1b50e3
+{
1b50e3
+	char buf[MAX_ERR_BUF];
1b50e3
+	char *estr;
1b50e3
+	int ret = NSS_STATUS_UNAVAIL;
1b50e3
+
1b50e3
+	ret = ctxt->getautomntbyname_r(key, value, sss_ctxt);
1b50e3
+	if (ret) {
1b50e3
+		/* Host has gone down */
1b50e3
+		if (ret == ECONNREFUSED)
1b50e3
+			return NSS_STATUS_UNKNOWN;
1b50e3
+
1b50e3
+		if (ret != ENOENT)
1b50e3
+			goto error;
1b50e3
+
1b50e3
+		ret = NSS_STATUS_NOTFOUND;
1b50e3
+		goto free;
1b50e3
+	}
1b50e3
+	return ret;
1b50e3
+
1b50e3
+error:
1b50e3
+	estr = strerror_r(ret, buf, MAX_ERR_BUF);
1b50e3
+	error(logopt, MODPREFIX "getautomntbyname: %s", estr);
1b50e3
+free:
1b50e3
+	if (*value) {
1b50e3
+		free(*value);
1b50e3
+		*value = NULL;
1b50e3
+	}
1b50e3
+	return ret;
1b50e3
+}
1b50e3
+
1b50e3
 int lookup_read_master(struct master *master, time_t age, void *context)
1b50e3
 {
1b50e3
 	struct lookup_context *ctxt = (struct lookup_context *) context;
1b50e3
@@ -755,7 +788,6 @@ static int lookup_one(struct autofs_point *ap,
1b50e3
 	struct mapent *we;
1b50e3
 	void *sss_ctxt = NULL;
1b50e3
 	time_t age = monotonic_time(NULL);
1b50e3
-	char buf[MAX_ERR_BUF];
1b50e3
 	char *value = NULL;
1b50e3
 	char *s_key;
1b50e3
 	int ret;
1b50e3
@@ -770,61 +802,47 @@ static int lookup_one(struct autofs_point *ap,
1b50e3
 	if (ret)
1b50e3
 		return ret;
1b50e3
 
1b50e3
-	ret = ctxt->getautomntbyname_r(qKey, &value, sss_ctxt);
1b50e3
-	if (ret && ret != ENOENT) {
1b50e3
-		char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
1b50e3
-		error(ap->logopt,
1b50e3
-		      MODPREFIX "getautomntbyname_r: %s", estr);
1b50e3
+	ret = getautomntbyname(ap->logopt, ctxt, qKey, &value, sss_ctxt);
1b50e3
+	if (ret == NSS_STATUS_NOTFOUND)
1b50e3
+		goto wild;
1b50e3
+	if (ret) {
1b50e3
 		endautomntent(ap->logopt, ctxt, &sss_ctxt);
1b50e3
-		if (value)
1b50e3
-			free(value);
1b50e3
-		return NSS_STATUS_UNAVAIL;
1b50e3
+		return ret;
1b50e3
 	}
1b50e3
-	if (ret != ENOENT) {
1b50e3
-		/*
1b50e3
-		 * TODO: implement sun % hack for key translation for
1b50e3
-		 * mixed case keys in schema that are single case only.
1b50e3
-		 */
1b50e3
-		s_key = sanitize_path(qKey, qKey_len, ap->type, ap->logopt);
1b50e3
-		if (!s_key) {
1b50e3
-			free(value);
1b50e3
-			value = NULL;
1b50e3
-			goto wild;
1b50e3
-		}
1b50e3
-		cache_writelock(mc);
1b50e3
-		ret = cache_update(mc, source, s_key, value, age);
1b50e3
-		cache_unlock(mc);
1b50e3
-		endautomntent(ap->logopt, ctxt, &sss_ctxt);
1b50e3
-		free(s_key);
1b50e3
+
1b50e3
+	/*
1b50e3
+	 * TODO: implement sun % hack for key translation for
1b50e3
+	 * mixed case keys in schema that are single case only.
1b50e3
+	 */
1b50e3
+	s_key = sanitize_path(qKey, qKey_len, ap->type, ap->logopt);
1b50e3
+	if (!s_key) {
1b50e3
 		free(value);
1b50e3
-		return NSS_STATUS_SUCCESS;
1b50e3
+		value = NULL;
1b50e3
+		goto wild;
1b50e3
 	}
1b50e3
+	cache_writelock(mc);
1b50e3
+	ret = cache_update(mc, source, s_key, value, age);
1b50e3
+	cache_unlock(mc);
1b50e3
+	endautomntent(ap->logopt, ctxt, &sss_ctxt);
1b50e3
+	free(s_key);
1b50e3
+	free(value);
1b50e3
+	return NSS_STATUS_SUCCESS;
1b50e3
 
1b50e3
 wild:
1b50e3
-	ret = ctxt->getautomntbyname_r("/", &value, sss_ctxt);
1b50e3
-	if (ret && ret != ENOENT) {
1b50e3
-		char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
1b50e3
-		error(ap->logopt,
1b50e3
-		      MODPREFIX "getautomntbyname_r: %s", estr);
1b50e3
-		endautomntent(ap->logopt, ctxt, &sss_ctxt);
1b50e3
-		if (value)
1b50e3
-			free(value);
1b50e3
-		return NSS_STATUS_UNAVAIL;
1b50e3
-	}
1b50e3
-	if (ret == ENOENT) {
1b50e3
-		ret = ctxt->getautomntbyname_r("*", &value, sss_ctxt);
1b50e3
-		if (ret && ret != ENOENT) {
1b50e3
-			char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
1b50e3
-			error(ap->logopt,
1b50e3
-			      MODPREFIX "getautomntbyname_r: %s", estr);
1b50e3
+	ret = getautomntbyname(ap->logopt, ctxt, "/", &value, sss_ctxt);
1b50e3
+	if (ret) {
1b50e3
+		if (ret != NSS_STATUS_NOTFOUND) {
1b50e3
 			endautomntent(ap->logopt, ctxt, &sss_ctxt);
1b50e3
-			if (value)
1b50e3
-				free(value);
1b50e3
-			return NSS_STATUS_UNAVAIL;
1b50e3
+			return ret;
1b50e3
+		}
1b50e3
+		ret = getautomntbyname(ap->logopt, ctxt, "*", &value, sss_ctxt);
1b50e3
+		if (ret && ret != NSS_STATUS_NOTFOUND) {
1b50e3
+			endautomntent(ap->logopt, ctxt, &sss_ctxt);
1b50e3
+			return ret;
1b50e3
 		}
1b50e3
 	}
1b50e3
 
1b50e3
-	if (ret == ENOENT) {
1b50e3
+	if (ret == NSS_STATUS_NOTFOUND) {
1b50e3
 		/* Failed to find wild entry, update cache if needed */
1b50e3
 		cache_writelock(mc);
1b50e3
 		we = cache_lookup_distinct(mc, "*");