Blame SOURCES/017-fencing-reasons.patch

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