Blame SOURCES/017-fencing-reasons.patch

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