render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
3e5111
From 6744458379e73967a95507d3c0b91b52a62fc3ec Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <6744458379e73967a95507d3c0b91b52a62fc3ec@dist-git>
3e5111
From: Jiri Denemark <jdenemar@redhat.com>
3e5111
Date: Thu, 27 Apr 2017 11:54:44 +0200
3e5111
Subject: [PATCH] locking: Add support for sanlock_strerror
3e5111
3e5111
The recently added sanlock_strerror function can be used to translate
3e5111
sanlock's numeric errors into human readable strings.
3e5111
3e5111
https://bugzilla.redhat.com/show_bug.cgi?id=1409511
3e5111
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
(cherry picked from commit 23377c539b72a7fc4e2749a068711fe1f626998d)
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 m4/virt-sanlock.m4                |   7 ++
3e5111
 src/locking/lock_driver_sanlock.c | 154 ++++++++++++++++++++++++++------------
3e5111
 2 files changed, 115 insertions(+), 46 deletions(-)
3e5111
3e5111
diff --git a/m4/virt-sanlock.m4 b/m4/virt-sanlock.m4
3e5111
index e4476cae4..00de7980e 100644
3e5111
--- a/m4/virt-sanlock.m4
3e5111
+++ b/m4/virt-sanlock.m4
3e5111
@@ -61,6 +61,13 @@ AC_DEFUN([LIBVIRT_CHECK_SANLOCK],[
3e5111
         [whether sanlock supports sanlock_write_lockspace])
3e5111
     fi
3e5111
 
3e5111
+    AC_CHECK_LIB([sanlock_client], [sanlock_strerror],
3e5111
+                 [sanlock_strerror=yes], [sanlock_strerror=no])
3e5111
+    if test "x$sanlock_strerror" = "xyes" ; then
3e5111
+      AC_DEFINE_UNQUOTED([HAVE_SANLOCK_STRERROR], 1,
3e5111
+        [whether sanlock supports sanlock_strerror])
3e5111
+    fi
3e5111
+
3e5111
     CPPFLAGS="$old_cppflags"
3e5111
     LIBS="$old_libs"
3e5111
   fi
3e5111
diff --git a/src/locking/lock_driver_sanlock.c b/src/locking/lock_driver_sanlock.c
3e5111
index 280219f72..b5e69c472 100644
3e5111
--- a/src/locking/lock_driver_sanlock.c
3e5111
+++ b/src/locking/lock_driver_sanlock.c
3e5111
@@ -97,6 +97,25 @@ struct _virLockManagerSanlockPrivate {
3e5111
     bool registered;
3e5111
 };
3e5111
 
3e5111
+
3e5111
+static bool
3e5111
+ATTRIBUTE_NONNULL(2)
3e5111
+virLockManagerSanlockError(int err,
3e5111
+                           char **message)
3e5111
+{
3e5111
+    if (err <= -200) {
3e5111
+#if HAVE_SANLOCK_STRERROR
3e5111
+        ignore_value(VIR_STRDUP_QUIET(*message, sanlock_strerror(err)));
3e5111
+#else
3e5111
+        ignore_value(virAsprintfQuiet(message, _("sanlock error %d"), err));
3e5111
+#endif
3e5111
+        return true;
3e5111
+    } else {
3e5111
+        return false;
3e5111
+    }
3e5111
+}
3e5111
+
3e5111
+
3e5111
 /*
3e5111
  * sanlock plugin for the libvirt virLockManager API
3e5111
  */
3e5111
@@ -263,14 +282,17 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
3e5111
             }
3e5111
 
3e5111
             if ((rv = sanlock_align(&ls.host_id_disk)) < 0) {
3e5111
-                if (rv <= -200)
3e5111
+                char *err = NULL;
3e5111
+                if (virLockManagerSanlockError(rv, &err)) {
3e5111
                     virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                                   _("Unable to query sector size %s: error %d"),
3e5111
-                                   path, rv);
3e5111
-                else
3e5111
+                                   _("Unable to query sector size %s: %s"),
3e5111
+                                   path, NULLSTR(err));
3e5111
+                    VIR_FREE(err);
3e5111
+                } else {
3e5111
                     virReportSystemError(-rv,
3e5111
                                          _("Unable to query sector size %s"),
3e5111
                                          path);
3e5111
+                }
3e5111
                 goto error_unlink;
3e5111
             }
3e5111
 
3e5111
@@ -292,14 +314,17 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
3e5111
             }
3e5111
 
3e5111
             if ((rv = virLockManagerSanlockInitLockspace(driver, &ls) < 0)) {
3e5111
-                if (rv <= -200)
3e5111
+                char *err = NULL;
3e5111
+                if (virLockManagerSanlockError(rv, &err)) {
3e5111
                     virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                                   _("Unable to initialize lockspace %s: error %d"),
3e5111
-                                   path, rv);
3e5111
-                else
3e5111
+                                   _("Unable to initialize lockspace %s: %s"),
3e5111
+                                   path, NULLSTR(err));
3e5111
+                    VIR_FREE(err);
3e5111
+                } else {
3e5111
                     virReportSystemError(-rv,
3e5111
                                          _("Unable to initialize lockspace %s"),
3e5111
                                          path);
3e5111
+                }
3e5111
                 goto error_unlink;
3e5111
             }
3e5111
             VIR_DEBUG("Lockspace %s has been initialized", path);
3e5111
@@ -362,14 +387,17 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
3e5111
             goto retry;
3e5111
         }
3e5111
         if (-rv != EEXIST) {
3e5111
-            if (rv <= -200)
3e5111
+            char *err = NULL;
3e5111
+            if (virLockManagerSanlockError(rv, &err)) {
3e5111
                 virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                               _("Unable to add lockspace %s: error %d"),
3e5111
-                               path, rv);
3e5111
-            else
3e5111
+                               _("Unable to add lockspace %s: %s"),
3e5111
+                               path, NULLSTR(err));
3e5111
+                VIR_FREE(err);
3e5111
+            } else {
3e5111
                 virReportSystemError(-rv,
3e5111
                                      _("Unable to add lockspace %s"),
3e5111
                                      path);
3e5111
+            }
3e5111
             goto error;
3e5111
         } else {
3e5111
             VIR_DEBUG("Lockspace %s is already registered", path);
3e5111
@@ -694,14 +722,17 @@ virLockManagerSanlockCreateLease(virLockManagerSanlockDriverPtr driver,
3e5111
             }
3e5111
 
3e5111
             if ((rv = sanlock_align(&res->disks[0])) < 0) {
3e5111
-                if (rv <= -200)
3e5111
+                char *err = NULL;
3e5111
+                if (virLockManagerSanlockError(rv, &err)) {
3e5111
                     virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                                   _("Unable to query sector size %s: error %d"),
3e5111
-                                   res->disks[0].path, rv);
3e5111
-                else
3e5111
+                                   _("Unable to query sector size %s: %s"),
3e5111
+                                   res->disks[0].path, NULLSTR(err));
3e5111
+                    VIR_FREE(err);
3e5111
+                } else {
3e5111
                     virReportSystemError(-rv,
3e5111
                                          _("Unable to query sector size %s"),
3e5111
                                          res->disks[0].path);
3e5111
+                }
3e5111
                 goto error_unlink;
3e5111
             }
3e5111
 
3e5111
@@ -723,14 +754,17 @@ virLockManagerSanlockCreateLease(virLockManagerSanlockDriverPtr driver,
3e5111
             }
3e5111
 
3e5111
             if ((rv = sanlock_init(NULL, res, 0, 0)) < 0) {
3e5111
-                if (rv <= -200)
3e5111
+                char *err = NULL;
3e5111
+                if (virLockManagerSanlockError(rv, &err)) {
3e5111
                     virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                                   _("Unable to initialize lease %s: error %d"),
3e5111
-                                   res->disks[0].path, rv);
3e5111
-                else
3e5111
+                                   _("Unable to initialize lease %s: %s"),
3e5111
+                                   res->disks[0].path, NULLSTR(err));
3e5111
+                    VIR_FREE(err);
3e5111
+                } else {
3e5111
                     virReportSystemError(-rv,
3e5111
                                          _("Unable to initialize lease %s"),
3e5111
                                          res->disks[0].path);
3e5111
+                }
3e5111
                 goto error_unlink;
3e5111
             }
3e5111
             VIR_DEBUG("Lease %s has been initialized", res->disks[0].path);
3e5111
@@ -867,10 +901,12 @@ virLockManagerSanlockRegisterKillscript(int sock,
3e5111
     }
3e5111
 
3e5111
     if ((rv = sanlock_killpath(sock, 0, path, args)) < 0) {
3e5111
-        if (rv <= -200) {
3e5111
+        char *err = NULL;
3e5111
+        if (virLockManagerSanlockError(rv, &err)) {
3e5111
             virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                           _("Failed to register lock failure action:"
3e5111
-                             " error %d"), rv);
3e5111
+                           _("Failed to register lock failure action: %s"),
3e5111
+                           NULLSTR(err));
3e5111
+            VIR_FREE(err);
3e5111
         } else {
3e5111
             virReportSystemError(-rv, "%s",
3e5111
                                  _("Failed to register lock failure"
3e5111
@@ -934,13 +970,16 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
3e5111
     if (priv->vm_pid == getpid()) {
3e5111
         VIR_DEBUG("Register sanlock %d", flags);
3e5111
         if ((sock = sanlock_register()) < 0) {
3e5111
-            if (sock <= -200)
3e5111
+            char *err = NULL;
3e5111
+            if (virLockManagerSanlockError(sock, &err)) {
3e5111
                 virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                               _("Failed to open socket to sanlock daemon: error %d"),
3e5111
-                               sock);
3e5111
-            else
3e5111
+                               _("Failed to open socket to sanlock daemon: %s"),
3e5111
+                               NULLSTR(err));
3e5111
+                VIR_FREE(err);
3e5111
+            } else {
3e5111
                 virReportSystemError(-sock, "%s",
3e5111
                                      _("Failed to open socket to sanlock daemon"));
3e5111
+            }
3e5111
             goto error;
3e5111
         }
3e5111
 
3e5111
@@ -971,14 +1010,17 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
3e5111
         if ((rv = sanlock_state_to_args((char *)state,
3e5111
                                         &res_count,
3e5111
                                         &res_args)) < 0) {
3e5111
-            if (rv <= -200)
3e5111
+            char *err = NULL;
3e5111
+            if (virLockManagerSanlockError(rv, &err)) {
3e5111
                 virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                               _("Unable to parse lock state %s: error %d"),
3e5111
-                               state, rv);
3e5111
-            else
3e5111
+                               _("Unable to parse lock state %s: %s"),
3e5111
+                               state, NULLSTR(err));
3e5111
+                VIR_FREE(err);
3e5111
+            } else {
3e5111
                 virReportSystemError(-rv,
3e5111
                                      _("Unable to parse lock state %s"),
3e5111
                                      state);
3e5111
+            }
3e5111
             goto error;
3e5111
         }
3e5111
         res_free = true;
3e5111
@@ -992,12 +1034,16 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
3e5111
         if ((rv = sanlock_acquire(sock, priv->vm_pid, 0,
3e5111
                                   priv->res_count, priv->res_args,
3e5111
                                   opt)) < 0) {
3e5111
-            if (rv <= -200)
3e5111
+            char *err = NULL;
3e5111
+            if (virLockManagerSanlockError(rv, &err)) {
3e5111
                 virReportError(VIR_ERR_RESOURCE_BUSY,
3e5111
-                               _("Failed to acquire lock: error %d"), rv);
3e5111
-            else
3e5111
+                               _("Failed to acquire lock: %s"),
3e5111
+                               NULLSTR(err));
3e5111
+                VIR_FREE(err);
3e5111
+            } else {
3e5111
                 virReportSystemError(-rv, "%s",
3e5111
                                      _("Failed to acquire lock"));
3e5111
+            }
3e5111
             goto error;
3e5111
         }
3e5111
     }
3e5111
@@ -1016,12 +1062,16 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
3e5111
 
3e5111
     if (flags & VIR_LOCK_MANAGER_ACQUIRE_RESTRICT) {
3e5111
         if ((rv = sanlock_restrict(sock, SANLK_RESTRICT_ALL)) < 0) {
3e5111
-            if (rv <= -200)
3e5111
+            char *err = NULL;
3e5111
+            if (virLockManagerSanlockError(rv, &err)) {
3e5111
                 virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                               _("Failed to restrict process: error %d"), rv);
3e5111
-            else
3e5111
+                               _("Failed to restrict process: %s"),
3e5111
+                               NULLSTR(err));
3e5111
+                VIR_FREE(err);
3e5111
+            } else {
3e5111
                 virReportSystemError(-rv, "%s",
3e5111
                                      _("Failed to restrict process"));
3e5111
+            }
3e5111
             goto error;
3e5111
         }
3e5111
     }
3e5111
@@ -1068,12 +1118,16 @@ static int virLockManagerSanlockRelease(virLockManagerPtr lock,
3e5111
 
3e5111
     if (state) {
3e5111
         if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) {
3e5111
-            if (rv <= -200)
3e5111
+            char *err = NULL;
3e5111
+            if (virLockManagerSanlockError(rv, &err)) {
3e5111
                 virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                               _("Failed to inquire lock: error %d"), rv);
3e5111
-            else
3e5111
+                               _("Failed to inquire lock: %s"),
3e5111
+                               NULLSTR(err));
3e5111
+                VIR_FREE(err);
3e5111
+            } else {
3e5111
                 virReportSystemError(-rv, "%s",
3e5111
                                      _("Failed to inquire lock"));
3e5111
+            }
3e5111
             return -1;
3e5111
         }
3e5111
 
3e5111
@@ -1083,12 +1137,16 @@ static int virLockManagerSanlockRelease(virLockManagerPtr lock,
3e5111
 
3e5111
     if ((rv = sanlock_release(-1, priv->vm_pid, 0, res_count,
3e5111
                               priv->res_args)) < 0) {
3e5111
-        if (rv <= -200)
3e5111
+        char *err = NULL;
3e5111
+        if (virLockManagerSanlockError(rv, &err)) {
3e5111
             virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                           _("Failed to release lock: error %d"), rv);
3e5111
-        else
3e5111
+                           _("Failed to release lock: %s"),
3e5111
+                           NULLSTR(err));
3e5111
+            VIR_FREE(err);
3e5111
+        } else {
3e5111
             virReportSystemError(-rv, "%s",
3e5111
                                  _("Failed to release lock"));
3e5111
+        }
3e5111
         return -1;
3e5111
     }
3e5111
 
3e5111
@@ -1118,12 +1176,16 @@ static int virLockManagerSanlockInquire(virLockManagerPtr lock,
3e5111
     }
3e5111
 
3e5111
     if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) {
3e5111
-        if (rv <= -200)
3e5111
+        char *err;
3e5111
+        if (virLockManagerSanlockError(rv, &err)) {
3e5111
             virReportError(VIR_ERR_INTERNAL_ERROR,
3e5111
-                           _("Failed to inquire lock: error %d"), rv);
3e5111
-        else
3e5111
+                           _("Failed to inquire lock: %s"),
3e5111
+                           NULLSTR(err));
3e5111
+            VIR_FREE(err);
3e5111
+        } else {
3e5111
             virReportSystemError(-rv, "%s",
3e5111
                                  _("Failed to inquire lock"));
3e5111
+        }
3e5111
         return -1;
3e5111
     }
3e5111
 
3e5111
-- 
3e5111
2.12.2
3e5111