64302d
From 0d15568a538349ac41028db6b506d13dd23e8732 Mon Sep 17 00:00:00 2001
64302d
From: Chris Lumens <clumens@redhat.com>
64302d
Date: Tue, 14 Feb 2023 14:00:37 -0500
64302d
Subject: [PATCH] High: libcrmcommon: Fix handling node=NULL in
64302d
 pcmk__attrd_api_query.
64302d
64302d
According to the header file, if node is NULL, pcmk__attrd_api_query
64302d
should query the value of the given attribute on all cluster nodes.
64302d
This is also what the server expects and how attrd_updater is supposed
64302d
to work.
64302d
64302d
However, pcmk__attrd_api_query has no way of letting callers decide
64302d
whether they want to query all nodes or whether they want to use the
64302d
local node.  We were passing NULL for the node name, which it took to
64302d
mean it should look up the local node name.  This calls
64302d
pcmk__node_attr_target, which probes the local cluster name and returns
64302d
that to pcmk__attrd_api_query.  If it returns non-NULL, that value will
64302d
then be put into the XML IPC call which means the server will only
64302d
return the value for that node.
64302d
64302d
In testing this was usually fine.  However, in pratice, the methods
64302d
pcmk__node_attr_target uses to figure out the local cluster node name
64302d
involves checking the OCF_RESKEY_CRM_meta_on_node environment variable
64302d
among others.
64302d
64302d
This variable was never set in testing, but can be set in the real
64302d
world.  This leads to circumstances where the user did "attrd_updater -QA"
64302d
expecting to get the values on all nodes, but instead only got the value
64302d
on the local cluster node.
64302d
64302d
In pacemaker-2.1.4 and prior, pcmk__node_attr_target was simply never
64302d
called if the node was NULL but was called otherwise.
64302d
64302d
The fix is to modify pcmk__attrd_api_query to take an option for
64302d
querying all nodes.  If that's present, we'll query all nodes.  If it's
64302d
not present, we'll look at the given node name - NULL means look it up,
64302d
anything else means just that node.
64302d
64302d
Regression in 2.1.5 introduced by eb20a65577
64302d
---
64302d
 include/crm/common/attrd_internal.h     |  6 +++++-
64302d
 include/crm/common/ipc_attrd_internal.h |  7 +++++--
64302d
 lib/common/ipc_attrd.c                  | 12 ++++++++----
64302d
 tools/attrd_updater.c                   |  5 +++--
64302d
 4 files changed, 21 insertions(+), 9 deletions(-)
64302d
64302d
diff --git a/include/crm/common/attrd_internal.h b/include/crm/common/attrd_internal.h
64302d
index 389be48..7337c38 100644
64302d
--- a/include/crm/common/attrd_internal.h
64302d
+++ b/include/crm/common/attrd_internal.h
64302d
@@ -1,5 +1,5 @@
64302d
 /*
64302d
- * Copyright 2004-2022 the Pacemaker project contributors
64302d
+ * Copyright 2004-2023 the Pacemaker project contributors
64302d
  *
64302d
  * The version control history for this file may have further details.
64302d
  *
64302d
@@ -25,6 +25,10 @@ enum pcmk__node_attr_opts {
64302d
     pcmk__node_attr_perm           = (1 << 5),
64302d
     pcmk__node_attr_sync_local     = (1 << 6),
64302d
     pcmk__node_attr_sync_cluster   = (1 << 7),
64302d
+    // pcmk__node_attr_utilization is 8, but that has not been backported.
64302d
+    // I'm leaving the gap here in case we backport that in the future and
64302d
+    // also to avoid problems on mixed-version clusters.
64302d
+    pcmk__node_attr_query_all      = (1 << 9),
64302d
 };
64302d
 
64302d
 #define pcmk__set_node_attr_flags(node_attr_flags, flags_to_set) do {   \
64302d
diff --git a/include/crm/common/ipc_attrd_internal.h b/include/crm/common/ipc_attrd_internal.h
64302d
index 2c6713f..b1b7584 100644
64302d
--- a/include/crm/common/ipc_attrd_internal.h
64302d
+++ b/include/crm/common/ipc_attrd_internal.h
64302d
@@ -1,5 +1,5 @@
64302d
 /*
64302d
- * Copyright 2022 the Pacemaker project contributors
64302d
+ * Copyright 2022-2023 the Pacemaker project contributors
64302d
  *
64302d
  * The version control history for this file may have further details.
64302d
  *
64302d
@@ -110,10 +110,13 @@ int pcmk__attrd_api_purge(pcmk_ipc_api_t *api, const char *node);
64302d
  *
64302d
  * \param[in,out] api           Connection to pacemaker-attrd
64302d
  * \param[in]     node          Look up the attribute for this node
64302d
- *                              (or NULL for all nodes)
64302d
+ *                              (or NULL for the local node)
64302d
  * \param[in]     name          Attribute name
64302d
  * \param[in]     options       Bitmask of pcmk__node_attr_opts
64302d
  *
64302d
+ * \note Passing pcmk__node_attr_query_all will cause the function to query
64302d
+ *       the value of \p name on all nodes, regardless of the value of \p node.
64302d
+ *
64302d
  * \return Standard Pacemaker return code
64302d
  */
64302d
 int pcmk__attrd_api_query(pcmk_ipc_api_t *api, const char *node, const char *name,
64302d
diff --git a/lib/common/ipc_attrd.c b/lib/common/ipc_attrd.c
64302d
index 4606509..dece49b 100644
64302d
--- a/lib/common/ipc_attrd.c
64302d
+++ b/lib/common/ipc_attrd.c
64302d
@@ -1,5 +1,5 @@
64302d
 /*
64302d
- * Copyright 2011-2022 the Pacemaker project contributors
64302d
+ * Copyright 2011-2023 the Pacemaker project contributors
64302d
  *
64302d
  * The version control history for this file may have further details.
64302d
  *
64302d
@@ -332,10 +332,14 @@ pcmk__attrd_api_query(pcmk_ipc_api_t *api, const char *node, const char *name,
64302d
         return EINVAL;
64302d
     }
64302d
 
64302d
-    target = pcmk__node_attr_target(node);
64302d
+    if (pcmk_is_set(options, pcmk__node_attr_query_all)) {
64302d
+        node = NULL;
64302d
+    } else {
64302d
+        target = pcmk__node_attr_target(node);
64302d
 
64302d
-    if (target != NULL) {
64302d
-        node = target;
64302d
+        if (target != NULL) {
64302d
+            node = target;
64302d
+        }
64302d
     }
64302d
 
64302d
     request = create_attrd_op(NULL);
64302d
diff --git a/tools/attrd_updater.c b/tools/attrd_updater.c
64302d
index 3cd766d..cbd341d 100644
64302d
--- a/tools/attrd_updater.c
64302d
+++ b/tools/attrd_updater.c
64302d
@@ -376,6 +376,7 @@ attrd_event_cb(pcmk_ipc_api_t *attrd_api, enum pcmk_ipc_event event_type,
64302d
 static int
64302d
 send_attrd_query(pcmk__output_t *out, const char *attr_name, const char *attr_node, gboolean query_all)
64302d
 {
64302d
+    uint32_t options = pcmk__node_attr_none;
64302d
     pcmk_ipc_api_t *attrd_api = NULL;
64302d
     int rc = pcmk_rc_ok;
64302d
 
64302d
@@ -400,10 +401,10 @@ send_attrd_query(pcmk__output_t *out, const char *attr_name, const char *attr_no
64302d
 
64302d
     /* Decide which node(s) to query */
64302d
     if (query_all == TRUE) {
64302d
-        attr_node = NULL;
64302d
+        options |= pcmk__node_attr_query_all;
64302d
     }
64302d
 
64302d
-    rc = pcmk__attrd_api_query(attrd_api, attr_node, attr_name, 0);
64302d
+    rc = pcmk__attrd_api_query(attrd_api, attr_node, attr_name, options);
64302d
 
64302d
     if (rc != pcmk_rc_ok) {
64302d
         g_set_error(&error, PCMK__RC_ERROR, rc, "Could not query value of %s: %s (%d)",
64302d
-- 
64302d
2.31.1
64302d