Blame SOURCES/017-fencing-reasons.patch

533c21
From 523f62eb235836a01ea039c23ada261a494f7b32 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Wed, 10 Nov 2021 15:22:47 -0600
533c21
Subject: [PATCH 01/11] Feature: libpacemaker: improve result for high-level
533c21
 fencing API
533c21
533c21
Previously, pcmk__fencing_action()'s helpers for asynchronous fencing actions
533c21
initialized the result to a generic error, and then overrode that only on
533c21
success.
533c21
533c21
Now, set a detailed result for early failures, and use the full result when
533c21
available from the fencing API.
533c21
533c21
A standard return code is still returned to callers at this point.
533c21
---
533c21
 lib/pacemaker/pcmk_fence.c | 31 ++++++++++++++++++-------------
533c21
 1 file changed, 18 insertions(+), 13 deletions(-)
533c21
533c21
diff --git a/lib/pacemaker/pcmk_fence.c b/lib/pacemaker/pcmk_fence.c
533c21
index 7d6acd0de6..125e1b268b 100644
533c21
--- a/lib/pacemaker/pcmk_fence.c
533c21
+++ b/lib/pacemaker/pcmk_fence.c
533c21
@@ -32,8 +32,8 @@ static struct {
533c21
     unsigned int timeout;
533c21
     unsigned int tolerance;
533c21
     int delay;
533c21
-    int rc;
533c21
-} async_fence_data;
533c21
+    pcmk__action_result_t result;
533c21
+} async_fence_data = { NULL, };
533c21
 
533c21
 static int
533c21
 handle_level(stonith_t *st, char *target, int fence_level,
533c21
@@ -76,14 +76,13 @@ handle_level(stonith_t *st, char *target, int fence_level,
533c21
 static void
533c21
 notify_callback(stonith_t * st, stonith_event_t * e)
533c21
 {
533c21
-    if (e->result != pcmk_ok) {
533c21
-        return;
533c21
-    }
533c21
+    if (pcmk__str_eq(async_fence_data.target, e->target, pcmk__str_casei)
533c21
+        && pcmk__str_eq(async_fence_data.action, e->action, pcmk__str_casei)) {
533c21
 
533c21
-    if (pcmk__str_eq(async_fence_data.target, e->target, pcmk__str_casei) &&
533c21
-        pcmk__str_eq(async_fence_data.action, e->action, pcmk__str_casei)) {
533c21
-
533c21
-        async_fence_data.rc = e->result;
533c21
+        pcmk__set_result(&async_fence_data.result,
533c21
+                         stonith__event_exit_status(e),
533c21
+                         stonith__event_execution_status(e),
533c21
+                         stonith__event_exit_reason(e));
533c21
         g_main_loop_quit(mainloop);
533c21
     }
533c21
 }
533c21
@@ -91,8 +90,9 @@ notify_callback(stonith_t * st, stonith_event_t * e)
533c21
 static void
533c21
 fence_callback(stonith_t * stonith, stonith_callback_data_t * data)
533c21
 {
533c21
-    async_fence_data.rc = data->rc;
533c21
-
533c21
+    pcmk__set_result(&async_fence_data.result, stonith__exit_status(data),
533c21
+                     stonith__execution_status(data),
533c21
+                     stonith__exit_reason(data));
533c21
     g_main_loop_quit(mainloop);
533c21
 }
533c21
 
533c21
@@ -106,6 +106,8 @@ async_fence_helper(gpointer user_data)
533c21
     if (rc != pcmk_ok) {
533c21
         fprintf(stderr, "Could not connect to fencer: %s\n", pcmk_strerror(rc));
533c21
         g_main_loop_quit(mainloop);
533c21
+        pcmk__set_result(&async_fence_data.result, CRM_EX_ERROR,
533c21
+                         PCMK_EXEC_NOT_CONNECTED, NULL);
533c21
         return TRUE;
533c21
     }
533c21
 
533c21
@@ -121,6 +123,8 @@ async_fence_helper(gpointer user_data)
533c21
 
533c21
     if (call_id < 0) {
533c21
         g_main_loop_quit(mainloop);
533c21
+        pcmk__set_result(&async_fence_data.result, CRM_EX_ERROR,
533c21
+                         PCMK_EXEC_ERROR, pcmk_strerror(call_id));
533c21
         return TRUE;
533c21
     }
533c21
 
533c21
@@ -146,7 +150,8 @@ pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
     async_fence_data.timeout = timeout;
533c21
     async_fence_data.tolerance = tolerance;
533c21
     async_fence_data.delay = delay;
533c21
-    async_fence_data.rc = pcmk_err_generic;
533c21
+    pcmk__set_result(&async_fence_data.result, CRM_EX_ERROR, PCMK_EXEC_UNKNOWN,
533c21
+                     NULL);
533c21
 
533c21
     trig = mainloop_add_trigger(G_PRIORITY_HIGH, async_fence_helper, NULL);
533c21
     mainloop_set_trigger(trig);
533c21
@@ -156,7 +161,7 @@ pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
 
533c21
     free(async_fence_data.name);
533c21
 
533c21
-    return pcmk_legacy2rc(async_fence_data.rc);
533c21
+    return stonith__result2rc(&async_fence_data.result);
533c21
 }
533c21
 
533c21
 #ifdef BUILD_PUBLIC_LIBPACEMAKER
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 008868fae5d1b0d6d8dc61f7acfb3856801ddd52 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Fri, 10 Dec 2021 15:36:10 -0600
533c21
Subject: [PATCH 02/11] Refactor: libpacemaker: add exit reason to high-level
533c21
 fencing API
533c21
533c21
Nothing uses it as of this commit
533c21
---
533c21
 include/pacemaker.h         |  5 ++++-
533c21
 include/pcmki/pcmki_fence.h |  5 ++++-
533c21
 lib/pacemaker/pcmk_fence.c  | 10 +++++++---
533c21
 tools/stonith_admin.c       |  6 +++---
533c21
 4 files changed, 18 insertions(+), 8 deletions(-)
533c21
533c21
diff --git a/include/pacemaker.h b/include/pacemaker.h
533c21
index a8523c969e..0daa4c5945 100644
533c21
--- a/include/pacemaker.h
533c21
+++ b/include/pacemaker.h
533c21
@@ -189,12 +189,15 @@ int pcmk_list_nodes(xmlNodePtr *xml, char *node_types);
533c21
  *                      again.
533c21
  * \param[in] delay     Apply a fencing delay. Value -1 means disable also any
533c21
  *                      static/random fencing delays from pcmk_delay_base/max.
533c21
+ * \param[out] reason   If not NULL, where to put descriptive failure reason
533c21
  *
533c21
  * \return Standard Pacemaker return code
533c21
+ * \note If \p reason is not NULL, the caller is responsible for freeing its
533c21
+ *       returned value.
533c21
  */
533c21
 int pcmk_fence_action(stonith_t *st, const char *target, const char *action,
533c21
                       const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                      int delay);
533c21
+                      int delay, char **reason);
533c21
 
533c21
 /*!
533c21
  * \brief List the fencing operations that have occurred for a specific node.
533c21
diff --git a/include/pcmki/pcmki_fence.h b/include/pcmki/pcmki_fence.h
533c21
index d4cef68f5c..c3da0361d7 100644
533c21
--- a/include/pcmki/pcmki_fence.h
533c21
+++ b/include/pcmki/pcmki_fence.h
533c21
@@ -28,12 +28,15 @@
533c21
  *                      again.
533c21
  * \param[in] delay     Apply a fencing delay. Value -1 means disable also any
533c21
  *                      static/random fencing delays from pcmk_delay_base/max
533c21
+ * \param[out] reason   If not NULL, where to put descriptive failure reason
533c21
  *
533c21
  * \return Standard Pacemaker return code
533c21
+ * \note If \p reason is not NULL, the caller is responsible for freeing its
533c21
+ *       returned value.
533c21
  */
533c21
 int pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
                        const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                       int delay);
533c21
+                       int delay, char **reason);
533c21
 
533c21
 /*!
533c21
  * \brief List the fencing operations that have occurred for a specific node.
533c21
diff --git a/lib/pacemaker/pcmk_fence.c b/lib/pacemaker/pcmk_fence.c
533c21
index 125e1b268b..dbf084fb6b 100644
533c21
--- a/lib/pacemaker/pcmk_fence.c
533c21
+++ b/lib/pacemaker/pcmk_fence.c
533c21
@@ -139,7 +139,7 @@ async_fence_helper(gpointer user_data)
533c21
 int
533c21
 pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
                    const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                   int delay)
533c21
+                   int delay, char **reason)
533c21
 {
533c21
     crm_trigger_t *trig;
533c21
 
533c21
@@ -161,6 +161,9 @@ pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
 
533c21
     free(async_fence_data.name);
533c21
 
533c21
+    if ((reason != NULL) && (async_fence_data.result.exit_reason != NULL)) {
533c21
+        *reason = strdup(async_fence_data.result.exit_reason);
533c21
+    }
533c21
     return stonith__result2rc(&async_fence_data.result);
533c21
 }
533c21
 
533c21
@@ -168,9 +171,10 @@ pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
 int
533c21
 pcmk_fence_action(stonith_t *st, const char *target, const char *action,
533c21
                   const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                  int delay)
533c21
+                  int delay, char **reason)
533c21
 {
533c21
-    return pcmk__fence_action(st, target, action, name, timeout, tolerance, delay);
533c21
+    return pcmk__fence_action(st, target, action, name, timeout, tolerance,
533c21
+                              delay, reason);
533c21
 }
533c21
 #endif
533c21
 
533c21
diff --git a/tools/stonith_admin.c b/tools/stonith_admin.c
533c21
index 2d48326e1b..fdc7c46d49 100644
533c21
--- a/tools/stonith_admin.c
533c21
+++ b/tools/stonith_admin.c
533c21
@@ -571,17 +571,17 @@ main(int argc, char **argv)
533c21
 
533c21
         case 'B':
533c21
             rc = pcmk__fence_action(st, target, "reboot", name, options.timeout*1000,
533c21
-                                    options.tolerance*1000, options.delay);
533c21
+                                    options.tolerance*1000, options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'F':
533c21
             rc = pcmk__fence_action(st, target, "off", name, options.timeout*1000,
533c21
-                                    options.tolerance*1000, options.delay);
533c21
+                                    options.tolerance*1000, options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'U':
533c21
             rc = pcmk__fence_action(st, target, "on", name, options.timeout*1000,
533c21
-                                    options.tolerance*1000, options.delay);
533c21
+                                    options.tolerance*1000, options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'h':
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 7570510f9985ba75ef73fb824f28109e135ace0a Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Fri, 10 Dec 2021 15:40:48 -0600
533c21
Subject: [PATCH 03/11] Refactor: libpacemaker: rename high-level fencing API
533c21
533c21
Rename pcmk_fence_action() to pcmk_request_fencing(), and its internal
533c21
equivalent pcmk__fence_action() to pcmk__request_fencing(). The change is
533c21
backward-compatible because pcmk_fence_action() has not been exposed publicly
533c21
yet.
533c21
533c21
"Fence action" can be easily confused with libcrmservice actions, liblrmd
533c21
actions, libstonithd actions, scheduler actions, and so forth.
533c21
533c21
Also, the new name makes it clearer that the caller is requesting that the
533c21
cluster perform fencing, and not directly performing fencing.
533c21
---
533c21
 include/pacemaker.h         | 20 ++++++++++----------
533c21
 include/pcmki/pcmki_fence.h | 16 ++++++++--------
533c21
 lib/pacemaker/pcmk_fence.c  | 16 ++++++++--------
533c21
 tools/stonith_admin.c       | 18 ++++++++++++------
533c21
 4 files changed, 38 insertions(+), 32 deletions(-)
533c21
533c21
diff --git a/include/pacemaker.h b/include/pacemaker.h
533c21
index 0daa4c5945..e581f975a9 100644
533c21
--- a/include/pacemaker.h
533c21
+++ b/include/pacemaker.h
533c21
@@ -177,27 +177,27 @@ int pcmk_list_nodes(xmlNodePtr *xml, char *node_types);
533c21
 #ifdef BUILD_PUBLIC_LIBPACEMAKER
533c21
 
533c21
 /*!
533c21
- * \brief Perform a STONITH action.
533c21
+ * \brief Ask the cluster to perform fencing
533c21
  *
533c21
- * \param[in] st        A connection to the STONITH API.
533c21
- * \param[in] target    The node receiving the action.
533c21
- * \param[in] action    The action to perform.
533c21
+ * \param[in] st        A connection to the fencer API
533c21
+ * \param[in] target    The node that should be fenced
533c21
+ * \param[in] action    The fencing action (on, off, reboot) to perform
533c21
  * \param[in] name      Who requested the fence action?
533c21
- * \param[in] timeout   How long to wait for the operation to complete (in ms).
533c21
+ * \param[in] timeout   How long to wait for the operation to complete (in ms)
533c21
  * \param[in] tolerance If a successful action for \p target happened within
533c21
  *                      this many ms, return 0 without performing the action
533c21
- *                      again.
533c21
+ *                      again
533c21
  * \param[in] delay     Apply a fencing delay. Value -1 means disable also any
533c21
- *                      static/random fencing delays from pcmk_delay_base/max.
533c21
+ *                      static/random fencing delays from pcmk_delay_base/max
533c21
  * \param[out] reason   If not NULL, where to put descriptive failure reason
533c21
  *
533c21
  * \return Standard Pacemaker return code
533c21
  * \note If \p reason is not NULL, the caller is responsible for freeing its
533c21
  *       returned value.
533c21
  */
533c21
-int pcmk_fence_action(stonith_t *st, const char *target, const char *action,
533c21
-                      const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                      int delay, char **reason);
533c21
+int pcmk_request_fencing(stonith_t *st, const char *target, const char *action,
533c21
+                         const char *name, unsigned int timeout,
533c21
+                         unsigned int tolerance, int delay, char **reason);
533c21
 
533c21
 /*!
533c21
  * \brief List the fencing operations that have occurred for a specific node.
533c21
diff --git a/include/pcmki/pcmki_fence.h b/include/pcmki/pcmki_fence.h
533c21
index c3da0361d7..e3a7e27264 100644
533c21
--- a/include/pcmki/pcmki_fence.h
533c21
+++ b/include/pcmki/pcmki_fence.h
533c21
@@ -13,14 +13,14 @@
533c21
 #  include <crm/common/output_internal.h>
533c21
 
533c21
 /*!
533c21
- * \brief Perform a STONITH action.
533c21
+ * \brief Ask the cluster to perform fencing
533c21
  *
533c21
- * \note This is the internal version of pcmk_fence_action().  External users
533c21
+ * \note This is the internal version of pcmk_request_fencing(). External users
533c21
  *       of the pacemaker API should use that function instead.
533c21
  *
533c21
- * \param[in] st        A connection to the STONITH API.
533c21
- * \param[in] target    The node receiving the action.
533c21
- * \param[in] action    The action to perform.
533c21
+ * \param[in] st        A connection to the fencer API
533c21
+ * \param[in] target    The node that should be fenced
533c21
+ * \param[in] action    The fencing action (on, off, reboot) to perform
533c21
  * \param[in] name      Who requested the fence action?
533c21
  * \param[in] timeout   How long to wait for the operation to complete (in ms).
533c21
  * \param[in] tolerance If a successful action for \p target happened within
533c21
@@ -34,9 +34,9 @@
533c21
  * \note If \p reason is not NULL, the caller is responsible for freeing its
533c21
  *       returned value.
533c21
  */
533c21
-int pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
-                       const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                       int delay, char **reason);
533c21
+int pcmk__request_fencing(stonith_t *st, const char *target, const char *action,
533c21
+                          const char *name, unsigned int timeout,
533c21
+                          unsigned int tolerance, int delay, char **reason);
533c21
 
533c21
 /*!
533c21
  * \brief List the fencing operations that have occurred for a specific node.
533c21
diff --git a/lib/pacemaker/pcmk_fence.c b/lib/pacemaker/pcmk_fence.c
533c21
index dbf084fb6b..1b7feb54b2 100644
533c21
--- a/lib/pacemaker/pcmk_fence.c
533c21
+++ b/lib/pacemaker/pcmk_fence.c
533c21
@@ -137,9 +137,9 @@ async_fence_helper(gpointer user_data)
533c21
 }
533c21
 
533c21
 int
533c21
-pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
-                   const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                   int delay, char **reason)
533c21
+pcmk__request_fencing(stonith_t *st, const char *target, const char *action,
533c21
+                      const char *name, unsigned int timeout,
533c21
+                      unsigned int tolerance, int delay, char **reason)
533c21
 {
533c21
     crm_trigger_t *trig;
533c21
 
533c21
@@ -169,12 +169,12 @@ pcmk__fence_action(stonith_t *st, const char *target, const char *action,
533c21
 
533c21
 #ifdef BUILD_PUBLIC_LIBPACEMAKER
533c21
 int
533c21
-pcmk_fence_action(stonith_t *st, const char *target, const char *action,
533c21
-                  const char *name, unsigned int timeout, unsigned int tolerance,
533c21
-                  int delay, char **reason)
533c21
+pcmk_request_fencing(stonith_t *st, const char *target, const char *action,
533c21
+                     const char *name, unsigned int timeout,
533c21
+                     unsigned int tolerance, int delay, char **reason)
533c21
 {
533c21
-    return pcmk__fence_action(st, target, action, name, timeout, tolerance,
533c21
-                              delay, reason);
533c21
+    return pcmk__request_fencing(st, target, action, name, timeout, tolerance,
533c21
+                                 delay, reason);
533c21
 }
533c21
 #endif
533c21
 
533c21
diff --git a/tools/stonith_admin.c b/tools/stonith_admin.c
533c21
index fdc7c46d49..56948b3875 100644
533c21
--- a/tools/stonith_admin.c
533c21
+++ b/tools/stonith_admin.c
533c21
@@ -570,18 +570,24 @@ main(int argc, char **argv)
533c21
             break;
533c21
 
533c21
         case 'B':
533c21
-            rc = pcmk__fence_action(st, target, "reboot", name, options.timeout*1000,
533c21
-                                    options.tolerance*1000, options.delay, NULL);
533c21
+            rc = pcmk__request_fencing(st, target, "reboot", name,
533c21
+                                       options.timeout * 1000,
533c21
+                                       options.tolerance * 1000,
533c21
+                                       options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'F':
533c21
-            rc = pcmk__fence_action(st, target, "off", name, options.timeout*1000,
533c21
-                                    options.tolerance*1000, options.delay, NULL);
533c21
+            rc = pcmk__request_fencing(st, target, "off", name,
533c21
+                                       options.timeout * 1000,
533c21
+                                       options.tolerance * 1000,
533c21
+                                       options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'U':
533c21
-            rc = pcmk__fence_action(st, target, "on", name, options.timeout*1000,
533c21
-                                    options.tolerance*1000, options.delay, NULL);
533c21
+            rc = pcmk__request_fencing(st, target, "on", name,
533c21
+                                       options.timeout * 1000,
533c21
+                                       options.tolerance * 1000,
533c21
+                                       options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'h':
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 247eb303df934944c0b72b162bb661cee6e0ed8b Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Fri, 10 Dec 2021 15:52:37 -0600
533c21
Subject: [PATCH 04/11] Refactor: tools: drop unnecessary string duplication in
533c21
 stonith_admin
533c21
533c21
---
533c21
 tools/stonith_admin.c | 11 ++++-------
533c21
 1 file changed, 4 insertions(+), 7 deletions(-)
533c21
533c21
diff --git a/tools/stonith_admin.c b/tools/stonith_admin.c
533c21
index 56948b3875..c11e302e76 100644
533c21
--- a/tools/stonith_admin.c
533c21
+++ b/tools/stonith_admin.c
533c21
@@ -360,8 +360,6 @@ main(int argc, char **argv)
533c21
 
533c21
     pcmk__cli_init_logging("stonith_admin", args->verbosity);
533c21
 
533c21
-    name = strdup(crm_system_name);
533c21
-
533c21
     rc = pcmk__output_new(&out, args->output_ty, args->output_dest, argv);
533c21
     if (rc != pcmk_rc_ok) {
533c21
         exit_code = CRM_EX_ERROR;
533c21
@@ -496,7 +494,7 @@ main(int argc, char **argv)
533c21
     if (st == NULL) {
533c21
         rc = -ENOMEM;
533c21
     } else if (!no_connect) {
533c21
-        rc = st->cmds->connect(st, name, NULL);
533c21
+        rc = st->cmds->connect(st, crm_system_name, NULL);
533c21
     }
533c21
     if (rc < 0) {
533c21
         out->err(out, "Could not connect to fencer: %s", pcmk_strerror(rc));
533c21
@@ -570,21 +568,21 @@ main(int argc, char **argv)
533c21
             break;
533c21
 
533c21
         case 'B':
533c21
-            rc = pcmk__request_fencing(st, target, "reboot", name,
533c21
+            rc = pcmk__request_fencing(st, target, "reboot", crm_system_name,
533c21
                                        options.timeout * 1000,
533c21
                                        options.tolerance * 1000,
533c21
                                        options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'F':
533c21
-            rc = pcmk__request_fencing(st, target, "off", name,
533c21
+            rc = pcmk__request_fencing(st, target, "off", crm_system_name,
533c21
                                        options.timeout * 1000,
533c21
                                        options.tolerance * 1000,
533c21
                                        options.delay, NULL);
533c21
             break;
533c21
 
533c21
         case 'U':
533c21
-            rc = pcmk__request_fencing(st, target, "on", name,
533c21
+            rc = pcmk__request_fencing(st, target, "on", crm_system_name,
533c21
                                        options.timeout * 1000,
533c21
                                        options.tolerance * 1000,
533c21
                                        options.delay, NULL);
533c21
@@ -619,7 +617,6 @@ main(int argc, char **argv)
533c21
         out->finish(out, exit_code, true, NULL);
533c21
         pcmk__output_free(out);
533c21
     }
533c21
-    free(name);
533c21
     stonith_key_value_freeall(options.params, 1, 1);
533c21
 
533c21
     if (st != NULL) {
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From a7888bf6868d8d9d9c77f65ae9983cf748bb0548 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Fri, 10 Dec 2021 15:56:34 -0600
533c21
Subject: [PATCH 05/11] Refactor: tools: functionize requesting fencing in
533c21
 stonith_admin
533c21
533c21
... to reduce code duplication and improve readability
533c21
---
533c21
 tools/stonith_admin.c | 27 +++++++++++++++------------
533c21
 1 file changed, 15 insertions(+), 12 deletions(-)
533c21
533c21
diff --git a/tools/stonith_admin.c b/tools/stonith_admin.c
533c21
index c11e302e76..f738a9c888 100644
533c21
--- a/tools/stonith_admin.c
533c21
+++ b/tools/stonith_admin.c
533c21
@@ -331,6 +331,18 @@ build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
533c21
     return context;
533c21
 }
533c21
 
533c21
+// \return Standard Pacemaker return code
533c21
+static int
533c21
+request_fencing(stonith_t *st, const char *target, const char *command)
533c21
+{
533c21
+    int rc = pcmk__request_fencing(st, target, command, crm_system_name,
533c21
+                                       options.timeout * 1000,
533c21
+                                       options.tolerance * 1000,
533c21
+                                       options.delay, NULL);
533c21
+
533c21
+    return rc;
533c21
+}
533c21
+
533c21
 int
533c21
 main(int argc, char **argv)
533c21
 {
533c21
@@ -568,24 +580,15 @@ main(int argc, char **argv)
533c21
             break;
533c21
 
533c21
         case 'B':
533c21
-            rc = pcmk__request_fencing(st, target, "reboot", crm_system_name,
533c21
-                                       options.timeout * 1000,
533c21
-                                       options.tolerance * 1000,
533c21
-                                       options.delay, NULL);
533c21
+            rc = request_fencing(st, target, "reboot");
533c21
             break;
533c21
 
533c21
         case 'F':
533c21
-            rc = pcmk__request_fencing(st, target, "off", crm_system_name,
533c21
-                                       options.timeout * 1000,
533c21
-                                       options.tolerance * 1000,
533c21
-                                       options.delay, NULL);
533c21
+            rc = request_fencing(st, target, "off");
533c21
             break;
533c21
 
533c21
         case 'U':
533c21
-            rc = pcmk__request_fencing(st, target, "on", crm_system_name,
533c21
-                                       options.timeout * 1000,
533c21
-                                       options.tolerance * 1000,
533c21
-                                       options.delay, NULL);
533c21
+            rc = request_fencing(st, target, "on");
533c21
             break;
533c21
 
533c21
         case 'h':
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 2da32df780983ec1197e857eed5eeb5bf1101889 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Fri, 10 Dec 2021 16:05:19 -0600
533c21
Subject: [PATCH 06/11] Feature: tools: display failure reasons for
533c21
 stonith_admin fencing commands
533c21
533c21
Previously, stonith_admin's --fence/--unfence/--reboot options did not output
533c21
any error message on failure. Now, they do, including the exit reason, if
533c21
available.
533c21
---
533c21
 tools/stonith_admin.c | 30 +++++++++++++++++++++++++-----
533c21
 1 file changed, 25 insertions(+), 5 deletions(-)
533c21
533c21
diff --git a/tools/stonith_admin.c b/tools/stonith_admin.c
533c21
index f738a9c888..5590faf11e 100644
533c21
--- a/tools/stonith_admin.c
533c21
+++ b/tools/stonith_admin.c
533c21
@@ -333,13 +333,33 @@ build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
533c21
 
533c21
 // \return Standard Pacemaker return code
533c21
 static int
533c21
-request_fencing(stonith_t *st, const char *target, const char *command)
533c21
+request_fencing(stonith_t *st, const char *target, const char *command,
533c21
+                GError **error)
533c21
 {
533c21
+    char *reason = NULL;
533c21
     int rc = pcmk__request_fencing(st, target, command, crm_system_name,
533c21
                                        options.timeout * 1000,
533c21
                                        options.tolerance * 1000,
533c21
-                                       options.delay, NULL);
533c21
+                                       options.delay, &reason);
533c21
 
533c21
+    if (rc != pcmk_rc_ok) {
533c21
+        const char *rc_str = pcmk_rc_str(rc);
533c21
+
533c21
+        // If reason is identical to return code string, don't display it twice
533c21
+        if (pcmk__str_eq(rc_str, reason, pcmk__str_none)) {
533c21
+            free(reason);
533c21
+            reason = NULL;
533c21
+        }
533c21
+
533c21
+        g_set_error(error, PCMK__RC_ERROR, rc,
533c21
+                    "Couldn't %sfence %s: %s%s%s%s",
533c21
+                    ((strcmp(command, "on") == 0)? "un" : ""),
533c21
+                    target, pcmk_rc_str(rc),
533c21
+                    ((reason == NULL)? "" : " ("),
533c21
+                    ((reason == NULL)? "" : reason),
533c21
+                    ((reason == NULL)? "" : ")"));
533c21
+    }
533c21
+    free(reason);
533c21
     return rc;
533c21
 }
533c21
 
533c21
@@ -580,15 +600,15 @@ main(int argc, char **argv)
533c21
             break;
533c21
 
533c21
         case 'B':
533c21
-            rc = request_fencing(st, target, "reboot");
533c21
+            rc = request_fencing(st, target, "reboot", &error);
533c21
             break;
533c21
 
533c21
         case 'F':
533c21
-            rc = request_fencing(st, target, "off");
533c21
+            rc = request_fencing(st, target, "off", &error);
533c21
             break;
533c21
 
533c21
         case 'U':
533c21
-            rc = request_fencing(st, target, "on");
533c21
+            rc = request_fencing(st, target, "on", &error);
533c21
             break;
533c21
 
533c21
         case 'h':
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 2d99eba4c326d3b13dbbe446971ea5febd5d05be Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Fri, 10 Dec 2021 16:08:49 -0600
533c21
Subject: [PATCH 07/11] Feature: libpacemaker: return exit reason for fencer
533c21
 connection failures
533c21
533c21
... instead of outputting to stderr directly, so that the caller (i.e.
533c21
stonith_admin) can output the error in the correct output format.
533c21
---
533c21
 lib/pacemaker/pcmk_fence.c | 3 +--
533c21
 1 file changed, 1 insertion(+), 2 deletions(-)
533c21
533c21
diff --git a/lib/pacemaker/pcmk_fence.c b/lib/pacemaker/pcmk_fence.c
533c21
index 1b7feb54b2..d17b07cda2 100644
533c21
--- a/lib/pacemaker/pcmk_fence.c
533c21
+++ b/lib/pacemaker/pcmk_fence.c
533c21
@@ -104,10 +104,9 @@ async_fence_helper(gpointer user_data)
533c21
     int rc = stonith_api_connect_retry(st, async_fence_data.name, 10);
533c21
 
533c21
     if (rc != pcmk_ok) {
533c21
-        fprintf(stderr, "Could not connect to fencer: %s\n", pcmk_strerror(rc));
533c21
         g_main_loop_quit(mainloop);
533c21
         pcmk__set_result(&async_fence_data.result, CRM_EX_ERROR,
533c21
-                         PCMK_EXEC_NOT_CONNECTED, NULL);
533c21
+                         PCMK_EXEC_NOT_CONNECTED, pcmk_strerror(rc));
533c21
         return TRUE;
533c21
     }
533c21
 
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 4480ef0602f47450bdddfbde360a6a8327710927 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Mon, 17 Jan 2022 09:39:39 -0600
533c21
Subject: [PATCH 08/11] Low: libpacemaker: compare fence action names
533c21
 case-sensitively
533c21
533c21
---
533c21
 lib/pacemaker/pcmk_fence.c | 6 +++---
533c21
 1 file changed, 3 insertions(+), 3 deletions(-)
533c21
533c21
diff --git a/lib/pacemaker/pcmk_fence.c b/lib/pacemaker/pcmk_fence.c
533c21
index d17b07cda2..2a8f50a555 100644
533c21
--- a/lib/pacemaker/pcmk_fence.c
533c21
+++ b/lib/pacemaker/pcmk_fence.c
533c21
@@ -1,5 +1,5 @@
533c21
 /*
533c21
- * Copyright 2009-2021 the Pacemaker project contributors
533c21
+ * Copyright 2009-2022 the Pacemaker project contributors
533c21
  *
533c21
  * The version control history for this file may have further details.
533c21
  *
533c21
@@ -77,7 +77,7 @@ static void
533c21
 notify_callback(stonith_t * st, stonith_event_t * e)
533c21
 {
533c21
     if (pcmk__str_eq(async_fence_data.target, e->target, pcmk__str_casei)
533c21
-        && pcmk__str_eq(async_fence_data.action, e->action, pcmk__str_casei)) {
533c21
+        && pcmk__str_eq(async_fence_data.action, e->action, pcmk__str_none)) {
533c21
 
533c21
         pcmk__set_result(&async_fence_data.result,
533c21
                          stonith__event_exit_status(e),
533c21
@@ -549,7 +549,7 @@ pcmk__reduce_fence_history(stonith_history_t *history)
533c21
             if ((hp->state == st_done) || (hp->state == st_failed)) {
533c21
                 /* action not in progress */
533c21
                 if (pcmk__str_eq(hp->target, np->target, pcmk__str_casei) &&
533c21
-                    pcmk__str_eq(hp->action, np->action, pcmk__str_casei) &&
533c21
+                    pcmk__str_eq(hp->action, np->action, pcmk__str_none) &&
533c21
                     (hp->state == np->state) &&
533c21
                     ((hp->state == st_done) ||
533c21
                      pcmk__str_eq(hp->delegate, np->delegate, pcmk__str_casei))) {
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From fe4c65a3b9e715c2b535709f989f2369d3637b78 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Mon, 17 Jan 2022 09:45:24 -0600
533c21
Subject: [PATCH 09/11] Refactor: libpacemaker: avoid unnecessary string
533c21
 duplication
533c21
533c21
... and don't leave any dynamic memory hanging around
533c21
---
533c21
 lib/pacemaker/pcmk_fence.c | 11 ++++++++---
533c21
 1 file changed, 8 insertions(+), 3 deletions(-)
533c21
533c21
diff --git a/lib/pacemaker/pcmk_fence.c b/lib/pacemaker/pcmk_fence.c
533c21
index 2a8f50a555..260fa5ab8e 100644
533c21
--- a/lib/pacemaker/pcmk_fence.c
533c21
+++ b/lib/pacemaker/pcmk_fence.c
533c21
@@ -141,6 +141,7 @@ pcmk__request_fencing(stonith_t *st, const char *target, const char *action,
533c21
                       unsigned int tolerance, int delay, char **reason)
533c21
 {
533c21
     crm_trigger_t *trig;
533c21
+    int rc = pcmk_rc_ok;
533c21
 
533c21
     async_fence_data.st = st;
533c21
     async_fence_data.name = strdup(name);
533c21
@@ -160,10 +161,14 @@ pcmk__request_fencing(stonith_t *st, const char *target, const char *action,
533c21
 
533c21
     free(async_fence_data.name);
533c21
 
533c21
-    if ((reason != NULL) && (async_fence_data.result.exit_reason != NULL)) {
533c21
-        *reason = strdup(async_fence_data.result.exit_reason);
533c21
+    if (reason != NULL) {
533c21
+        // Give the caller ownership of the exit reason
533c21
+        *reason = async_fence_data.result.exit_reason;
533c21
+        async_fence_data.result.exit_reason = NULL;
533c21
     }
533c21
-    return stonith__result2rc(&async_fence_data.result);
533c21
+    rc = stonith__result2rc(&async_fence_data.result);
533c21
+    pcmk__reset_result(&async_fence_data.result);
533c21
+    return rc;
533c21
 }
533c21
 
533c21
 #ifdef BUILD_PUBLIC_LIBPACEMAKER
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 7b7af07796f05a1adabdac655582be2e17106f81 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Mon, 17 Jan 2022 10:07:10 -0600
533c21
Subject: [PATCH 10/11] Doc: libpacemaker: improve pcmk__request_fencing()
533c21
 doxygen block
533c21
533c21
---
533c21
 include/pacemaker.h         |  6 ++++--
533c21
 include/pcmki/pcmki_fence.h | 15 +++++++++------
533c21
 2 files changed, 13 insertions(+), 8 deletions(-)
533c21
533c21
diff --git a/include/pacemaker.h b/include/pacemaker.h
533c21
index e581f975a9..266a844892 100644
533c21
--- a/include/pacemaker.h
533c21
+++ b/include/pacemaker.h
533c21
@@ -187,8 +187,10 @@ int pcmk_list_nodes(xmlNodePtr *xml, char *node_types);
533c21
  * \param[in] tolerance If a successful action for \p target happened within
533c21
  *                      this many ms, return 0 without performing the action
533c21
  *                      again
533c21
- * \param[in] delay     Apply a fencing delay. Value -1 means disable also any
533c21
- *                      static/random fencing delays from pcmk_delay_base/max
533c21
+ * \param[in] delay     Apply this delay (in milliseconds) before initiating the
533c21
+ *                      fencing action (a value of -1 applies no delay and also
533c21
+ *                      disables any fencing delay from pcmk_delay_base and
533c21
+ *                      pcmk_delay_max)
533c21
  * \param[out] reason   If not NULL, where to put descriptive failure reason
533c21
  *
533c21
  * \return Standard Pacemaker return code
533c21
diff --git a/include/pcmki/pcmki_fence.h b/include/pcmki/pcmki_fence.h
533c21
index e3a7e27264..4a2fe3c481 100644
533c21
--- a/include/pcmki/pcmki_fence.h
533c21
+++ b/include/pcmki/pcmki_fence.h
533c21
@@ -1,5 +1,5 @@
533c21
 /*
533c21
- * Copyright 2019-2021 the Pacemaker project contributors
533c21
+ * Copyright 2019-2022 the Pacemaker project contributors
533c21
  *
533c21
  * The version control history for this file may have further details.
533c21
  *
533c21
@@ -22,17 +22,20 @@
533c21
  * \param[in] target    The node that should be fenced
533c21
  * \param[in] action    The fencing action (on, off, reboot) to perform
533c21
  * \param[in] name      Who requested the fence action?
533c21
- * \param[in] timeout   How long to wait for the operation to complete (in ms).
533c21
+ * \param[in] timeout   How long to wait for the operation to complete (in ms)
533c21
  * \param[in] tolerance If a successful action for \p target happened within
533c21
- *                      this many ms, return 0 without performing the action
533c21
- *                      again.
533c21
- * \param[in] delay     Apply a fencing delay. Value -1 means disable also any
533c21
- *                      static/random fencing delays from pcmk_delay_base/max
533c21
+ *                      this many milliseconds, return success without
533c21
+ *                      performing the action again
533c21
+ * \param[in] delay     Apply this delay (in milliseconds) before initiating the
533c21
+ *                      fencing action (a value of -1 applies no delay and also
533c21
+ *                      disables any fencing delay from pcmk_delay_base and
533c21
+ *                      pcmk_delay_max)
533c21
  * \param[out] reason   If not NULL, where to put descriptive failure reason
533c21
  *
533c21
  * \return Standard Pacemaker return code
533c21
  * \note If \p reason is not NULL, the caller is responsible for freeing its
533c21
  *       returned value.
533c21
+ * \todo delay is eventually used with g_timeout_add() and should be guint
533c21
  */
533c21
 int pcmk__request_fencing(stonith_t *st, const char *target, const char *action,
533c21
                           const char *name, unsigned int timeout,
533c21
-- 
533c21
2.27.0
533c21
533c21
533c21
From 61fb7271712e1246eb6d9472dc1afc7cd10e0a79 Mon Sep 17 00:00:00 2001
533c21
From: Ken Gaillot <kgaillot@redhat.com>
533c21
Date: Mon, 17 Jan 2022 10:18:02 -0600
533c21
Subject: [PATCH 11/11] Fix: tools: get stonith_admin -T option working again
533c21
533c21
Regression introduced in 2.0.3 by 3910b6fec
533c21
533c21
This reverts commit 247eb303df934944c0b72b162bb661cee6e0ed8b
533c21
("Refactor: tools: drop unnecessary string duplication in stonith_admin")
533c21
and fixes a regression introduced when stonith_admin was converted to use
533c21
GOption.
533c21
533c21
The -T option is intended to override the client name passed to the fencer API,
533c21
but the client name was set to the default (crm_system_name) after option
533c21
processing had already been done, so any value for -T was overwritten by the
533c21
default, and its memory was leaked.
533c21
533c21
This commit sets the default only if -T was not used.
533c21
---
533c21
 tools/stonith_admin.c | 15 ++++++++++-----
533c21
 1 file changed, 10 insertions(+), 5 deletions(-)
533c21
533c21
diff --git a/tools/stonith_admin.c b/tools/stonith_admin.c
533c21
index 5590faf11e..54774b6fee 100644
533c21
--- a/tools/stonith_admin.c
533c21
+++ b/tools/stonith_admin.c
533c21
@@ -337,10 +337,10 @@ request_fencing(stonith_t *st, const char *target, const char *command,
533c21
                 GError **error)
533c21
 {
533c21
     char *reason = NULL;
533c21
-    int rc = pcmk__request_fencing(st, target, command, crm_system_name,
533c21
-                                       options.timeout * 1000,
533c21
-                                       options.tolerance * 1000,
533c21
-                                       options.delay, &reason);
533c21
+    int rc = pcmk__request_fencing(st, target, command, name,
533c21
+                                   options.timeout * 1000,
533c21
+                                   options.tolerance * 1000,
533c21
+                                   options.delay, &reason);
533c21
 
533c21
     if (rc != pcmk_rc_ok) {
533c21
         const char *rc_str = pcmk_rc_str(rc);
533c21
@@ -392,6 +392,10 @@ main(int argc, char **argv)
533c21
 
533c21
     pcmk__cli_init_logging("stonith_admin", args->verbosity);
533c21
 
533c21
+    if (name == NULL) {
533c21
+        name = strdup(crm_system_name);
533c21
+    }
533c21
+
533c21
     rc = pcmk__output_new(&out, args->output_ty, args->output_dest, argv);
533c21
     if (rc != pcmk_rc_ok) {
533c21
         exit_code = CRM_EX_ERROR;
533c21
@@ -526,7 +530,7 @@ main(int argc, char **argv)
533c21
     if (st == NULL) {
533c21
         rc = -ENOMEM;
533c21
     } else if (!no_connect) {
533c21
-        rc = st->cmds->connect(st, crm_system_name, NULL);
533c21
+        rc = st->cmds->connect(st, name, NULL);
533c21
     }
533c21
     if (rc < 0) {
533c21
         out->err(out, "Could not connect to fencer: %s", pcmk_strerror(rc));
533c21
@@ -640,6 +644,7 @@ main(int argc, char **argv)
533c21
         out->finish(out, exit_code, true, NULL);
533c21
         pcmk__output_free(out);
533c21
     }
533c21
+    free(name);
533c21
     stonith_key_value_freeall(options.params, 1, 1);
533c21
 
533c21
     if (st != NULL) {
533c21
-- 
533c21
2.27.0
533c21