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

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