Blame SOURCES/0032-autofs-correlate-errors-for-different-protocol-versi.patch

c5e826
From 8a22d4ad45f5fc8e888be693539495093c2b3c35 Mon Sep 17 00:00:00 2001
c5e826
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
c5e826
Date: Wed, 4 Nov 2020 14:20:10 +0100
c5e826
Subject: [PATCH 17/18] autofs: correlate errors for different protocol
c5e826
 versions
c5e826
c5e826
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
c5e826
---
c5e826
 src/sss_client/autofs/autofs_test_client.c | 12 ++++++++
c5e826
 src/sss_client/autofs/sss_autofs.c         | 35 +++++++++++++++++++---
c5e826
 src/sss_client/autofs/sss_autofs.exports   |  9 +++---
c5e826
 src/sss_client/autofs/sss_autofs_private.h |  5 ++++
c5e826
 4 files changed, 53 insertions(+), 8 deletions(-)
c5e826
c5e826
diff --git a/src/sss_client/autofs/autofs_test_client.c b/src/sss_client/autofs/autofs_test_client.c
c5e826
index c5358233f..4b285151e 100644
c5e826
--- a/src/sss_client/autofs/autofs_test_client.c
c5e826
+++ b/src/sss_client/autofs/autofs_test_client.c
c5e826
@@ -45,10 +45,14 @@ int main(int argc, const char *argv[])
c5e826
     char *value = NULL;
c5e826
     char *pc_key = NULL;
c5e826
     int pc_setent = 0;
c5e826
+    int pc_protocol = 1;
c5e826
+    unsigned int protocol;
c5e826
+    unsigned int requested_protocol = 1;
c5e826
     struct poptOption long_options[] = {
c5e826
         POPT_AUTOHELP
c5e826
         { "by-name",  'n', POPT_ARG_STRING, &pc_key, 0, "Request map by name", NULL },
c5e826
         { "only-setent",  's', POPT_ARG_VAL, &pc_setent, 1, "Run only setent, do not enumerate", NULL },
c5e826
+        { "protocol",  'p', POPT_ARG_INT, &pc_protocol, 0, "Protocol version", NULL },
c5e826
         POPT_TABLEEND
c5e826
     };
c5e826
     poptContext pc = NULL;
c5e826
@@ -69,6 +73,14 @@ int main(int argc, const char *argv[])
c5e826
 
c5e826
     poptFreeContext(pc);
c5e826
 
c5e826
+    requested_protocol = pc_protocol;
c5e826
+    protocol = _sss_auto_protocol_version(requested_protocol);
c5e826
+    if (protocol != requested_protocol) {
c5e826
+        fprintf(stderr, "Unsupported protocol version: %d -> %d\n",
c5e826
+                requested_protocol, protocol);
c5e826
+        exit(EXIT_FAILURE);
c5e826
+    }
c5e826
+
c5e826
     ret = _sss_setautomntent(mapname, &ctx;;
c5e826
     if (ret) {
c5e826
         fprintf(stderr, "setautomntent failed [%d]: %s\n",
c5e826
diff --git a/src/sss_client/autofs/sss_autofs.c b/src/sss_client/autofs/sss_autofs.c
c5e826
index 482ff2c40..ef27cf895 100644
c5e826
--- a/src/sss_client/autofs/sss_autofs.c
c5e826
+++ b/src/sss_client/autofs/sss_autofs.c
c5e826
@@ -20,6 +20,7 @@
c5e826
 
c5e826
 #include <errno.h>
c5e826
 #include <stdlib.h>
c5e826
+#include <stdatomic.h>
c5e826
 
c5e826
 #include "sss_client/autofs/sss_autofs_private.h"
c5e826
 #include "sss_client/sss_cli.h"
c5e826
@@ -33,6 +34,32 @@
c5e826
 /* How many entries shall _sss_getautomntent_r retrieve at once */
c5e826
 #define GETAUTOMNTENT_MAX_ENTRIES   512
c5e826
 
c5e826
+static atomic_uint _protocol = 0;
c5e826
+
c5e826
+unsigned int _sss_auto_protocol_version(unsigned int requested)
c5e826
+{
c5e826
+    switch (requested) {
c5e826
+    case 0:
c5e826
+        /* EHOSTDOWN will be translated to ENOENT */
c5e826
+        _protocol = 0;
c5e826
+        return 0;
c5e826
+    default:
c5e826
+        /* There is no other protocol version at this point. */
c5e826
+        _protocol = 1;
c5e826
+        return 1;
c5e826
+    }
c5e826
+}
c5e826
+
c5e826
+/* Returns correct errno based on autofs version expectations. */
c5e826
+static errno_t errnop_to_errno(int errnop)
c5e826
+{
c5e826
+    if (errnop == EHOSTDOWN && _protocol == 0) {
c5e826
+        return ENOENT;
c5e826
+    }
c5e826
+
c5e826
+    return errnop;
c5e826
+}
c5e826
+
c5e826
 struct automtent {
c5e826
     char *mapname;
c5e826
     size_t cursor;
c5e826
@@ -93,7 +120,7 @@ _sss_setautomntent(const char *mapname, void **context)
c5e826
                                   &repbuf, &replen, &errnop);
c5e826
     if (ret != SSS_STATUS_SUCCESS) {
c5e826
         free(name);
c5e826
-        ret = errnop;
c5e826
+        ret = errnop_to_errno(errnop);
c5e826
         goto out;
c5e826
     }
c5e826
 
c5e826
@@ -310,7 +337,7 @@ _sss_getautomntent_r(char **key, char **value, void *context)
c5e826
                                   &repbuf, &replen, &errnop);
c5e826
     free(data);
c5e826
     if (ret != SSS_STATUS_SUCCESS) {
c5e826
-        ret = errnop;
c5e826
+        ret = errnop_to_errno(errnop);
c5e826
         goto out;
c5e826
     }
c5e826
 
c5e826
@@ -408,7 +435,7 @@ _sss_getautomntbyname_r(const char *key, char **value, void *context)
c5e826
                                   &repbuf, &replen, &errnop);
c5e826
     free(data);
c5e826
     if (ret != SSS_STATUS_SUCCESS) {
c5e826
-        ret = errnop;
c5e826
+        ret = errnop_to_errno(errnop);
c5e826
         goto out;
c5e826
     }
c5e826
 
c5e826
@@ -467,7 +494,7 @@ _sss_endautomntent(void **context)
c5e826
     ret = sss_autofs_make_request(SSS_AUTOFS_ENDAUTOMNTENT,
c5e826
                                   NULL, NULL, NULL, &errnop);
c5e826
     if (ret != SSS_STATUS_SUCCESS) {
c5e826
-        ret = errnop;
c5e826
+        ret = errnop_to_errno(errnop);
c5e826
         goto out;
c5e826
     }
c5e826
 
c5e826
diff --git a/src/sss_client/autofs/sss_autofs.exports b/src/sss_client/autofs/sss_autofs.exports
c5e826
index f9ce8f5b2..ec61f715e 100644
c5e826
--- a/src/sss_client/autofs/sss_autofs.exports
c5e826
+++ b/src/sss_client/autofs/sss_autofs.exports
c5e826
@@ -2,10 +2,11 @@ EXPORTED {
c5e826
 
c5e826
     # public functions
c5e826
     global:
c5e826
-                _sss_setautomntent;
c5e826
-                _sss_getautomntent_r;
c5e826
-                _sss_getautomntbyname_r;
c5e826
-                _sss_endautomntent;
c5e826
+        _sss_auto_protocol_version;
c5e826
+        _sss_setautomntent;
c5e826
+        _sss_getautomntent_r;
c5e826
+        _sss_getautomntbyname_r;
c5e826
+        _sss_endautomntent;
c5e826
 
c5e826
     # everything else is local
c5e826
     local:
c5e826
diff --git a/src/sss_client/autofs/sss_autofs_private.h b/src/sss_client/autofs/sss_autofs_private.h
c5e826
index 6459c1cc7..7fd49db1d 100644
c5e826
--- a/src/sss_client/autofs/sss_autofs_private.h
c5e826
+++ b/src/sss_client/autofs/sss_autofs_private.h
c5e826
@@ -21,6 +21,11 @@
c5e826
 #include <errno.h>
c5e826
 #include "util/util.h"
c5e826
 
c5e826
+/**
c5e826
+ * Choose an autofs protocol version to be used between autofs and sss_autofs.
c5e826
+ */
c5e826
+unsigned int _sss_auto_protocol_version(unsigned int requested);
c5e826
+
c5e826
 /**
c5e826
  * Selects a map for processing.
c5e826
  */
c5e826
-- 
c5e826
2.21.3
c5e826