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

63b9c2
autofs-5.1.6 - refactor sss getautomntent()
63b9c2
63b9c2
From: Ian Kent <raven@themaw.net>
63b9c2
63b9c2
Refactor the sss getautomntent_r() to consolidate the error handling in
63b9c2
one location.
63b9c2
63b9c2
Also add the missing ECONREFUSED handling while we are at it.
63b9c2
63b9c2
Signed-off-by: Ian Kent <raven@themaw.net>
63b9c2
---
63b9c2
 CHANGELOG            |    1 +
63b9c2
 modules/lookup_sss.c |   88 +++++++++++++++++++++++---------------------------
63b9c2
 2 files changed, 42 insertions(+), 47 deletions(-)
63b9c2
63b9c2
diff --git a/CHANGELOG b/CHANGELOG
63b9c2
index 5ccd787..ecd54e9 100644
63b9c2
--- a/CHANGELOG
63b9c2
+++ b/CHANGELOG
63b9c2
@@ -100,6 +100,7 @@ xx/xx/2018 autofs-5.1.5
63b9c2
 - fix retries check in setautomntent_wait().
63b9c2
 - refactor sss setautomntent().
63b9c2
 - improve sss setautomntent() error handling.
63b9c2
+- refactor sss getautomntent().
63b9c2
 
63b9c2
 19/12/2017 autofs-5.1.4
63b9c2
 - fix spec file url.
63b9c2
diff --git a/modules/lookup_sss.c b/modules/lookup_sss.c
63b9c2
index e1ed83c..5addd87 100644
63b9c2
--- a/modules/lookup_sss.c
63b9c2
+++ b/modules/lookup_sss.c
63b9c2
@@ -377,6 +377,40 @@ static int endautomntent(unsigned int logopt,
63b9c2
 	return ret;
63b9c2
 }
63b9c2
 
63b9c2
+static int getautomntent(unsigned int logopt,
63b9c2
+			 struct lookup_context *ctxt,
63b9c2
+			 char **key, char **value, int count, void *sss_ctxt)
63b9c2
+{
63b9c2
+	char buf[MAX_ERR_BUF];
63b9c2
+	char *estr;
63b9c2
+	int ret = NSS_STATUS_UNAVAIL;
63b9c2
+
63b9c2
+	ret = ctxt->getautomntent_r(key, value, sss_ctxt);
63b9c2
+	if (ret) {
63b9c2
+		/* Host has gone down */
63b9c2
+		if (ret == ECONNREFUSED)
63b9c2
+			return NSS_STATUS_UNKNOWN;
63b9c2
+		if (ret != ENOENT)
63b9c2
+			goto error;
63b9c2
+		if (!count) {
63b9c2
+			ret = NSS_STATUS_NOTFOUND;
63b9c2
+			goto free;
63b9c2
+		}
63b9c2
+		goto error;
63b9c2
+	}
63b9c2
+	return ret;
63b9c2
+
63b9c2
+error:
63b9c2
+	estr = strerror_r(ret, buf, MAX_ERR_BUF);
63b9c2
+	error(logopt, MODPREFIX "getautomntent: %s", estr);
63b9c2
+free:
63b9c2
+	if (*key)
63b9c2
+		free(*key);
63b9c2
+	if (*value)
63b9c2
+		free(*value);
63b9c2
+	return ret;
63b9c2
+}
63b9c2
+
63b9c2
 int lookup_read_master(struct master *master, time_t age, void *context)
63b9c2
 {
63b9c2
 	struct lookup_context *ctxt = (struct lookup_context *) context;
63b9c2
@@ -399,30 +433,12 @@ int lookup_read_master(struct master *master, time_t age, void *context)
63b9c2
 	while (1) {
63b9c2
 	        key = NULL;
63b9c2
 	        value = NULL;
63b9c2
-		ret = ctxt->getautomntent_r(&key, &value, sss_ctxt);
63b9c2
-		if (ret && ret != ENOENT) {
63b9c2
-			char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
63b9c2
-			error(logopt, MODPREFIX "getautomntent_r: %s", estr);
63b9c2
+		ret = getautomntent(logopt, ctxt, &key, &value, count, sss_ctxt);
63b9c2
+		if (ret) {
63b9c2
 			endautomntent(logopt, ctxt, &sss_ctxt);
63b9c2
-			if (key)
63b9c2
-				free(key);
63b9c2
-			if (value)
63b9c2
-				free(value);
63b9c2
-			return NSS_STATUS_UNAVAIL;
63b9c2
-		}
63b9c2
-		if (ret == ENOENT) {
63b9c2
-			if (!count) {
63b9c2
-				char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
63b9c2
-				error(logopt, MODPREFIX "getautomntent_r: %s", estr);
63b9c2
-				endautomntent(logopt, ctxt, &sss_ctxt);
63b9c2
-				if (key)
63b9c2
-					free(key);
63b9c2
-				if (value)
63b9c2
-					free(value);
63b9c2
-				return NSS_STATUS_NOTFOUND;
63b9c2
-			}
63b9c2
-			break;
63b9c2
+			return ret;
63b9c2
 		}
63b9c2
+
63b9c2
 		count++;
63b9c2
 
63b9c2
 		buffer_len = strlen(key) + 1 + strlen(value) + 2;
63b9c2
@@ -470,7 +486,6 @@ int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
63b9c2
 	struct map_source *source;
63b9c2
 	struct mapent_cache *mc;
63b9c2
 	void *sss_ctxt = NULL;
63b9c2
-	char buf[MAX_ERR_BUF];
63b9c2
 	char *key;
63b9c2
 	char *value = NULL;
63b9c2
 	char *s_key;
63b9c2
@@ -502,31 +517,10 @@ int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
63b9c2
 	while (1) {
63b9c2
 	        key = NULL;
63b9c2
 	        value = NULL;
63b9c2
-		ret = ctxt->getautomntent_r(&key, &value, sss_ctxt);
63b9c2
-		if (ret && ret != ENOENT) {
63b9c2
-			char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
63b9c2
-			error(ap->logopt,
63b9c2
-			      MODPREFIX "getautomntent_r: %s", estr);
63b9c2
+		ret = getautomntent(ap->logopt, ctxt, &key, &value, count, sss_ctxt);
63b9c2
+		if (ret) {
63b9c2
 			endautomntent(ap->logopt, ctxt, &sss_ctxt);
63b9c2
-			if (key)
63b9c2
-				free(key);
63b9c2
-			if (value)
63b9c2
-				free(value);
63b9c2
-			return NSS_STATUS_UNAVAIL;
63b9c2
-		}
63b9c2
-		if (ret == ENOENT) {
63b9c2
-			if (!count) {
63b9c2
-				char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
63b9c2
-				error(ap->logopt,
63b9c2
-				      MODPREFIX "getautomntent_r: %s", estr);
63b9c2
-				endautomntent(ap->logopt, ctxt, &sss_ctxt);
63b9c2
-				if (key)
63b9c2
-					free(key);
63b9c2
-				if (value)
63b9c2
-					free(value);
63b9c2
-				return NSS_STATUS_NOTFOUND;
63b9c2
-			}
63b9c2
-			break;
63b9c2
+			return ret;
63b9c2
 		}
63b9c2
 
63b9c2
 		/*