587943
From f4e3d77c94906a062641c7bf34243049de521a87 Mon Sep 17 00:00:00 2001
587943
From: Klaus Wenninger <klaus.wenninger@aon.at>
587943
Date: Wed, 3 Feb 2021 13:25:22 +0100
587943
Subject: [PATCH] Fix: crm_mon: detect when run on remote-node
587943
587943
---
587943
 daemons/execd/remoted_proxy.c     | 17 +++++++
587943
 daemons/pacemakerd/pacemakerd.c   |  6 +--
587943
 include/crm/common/ipc_internal.h |  2 +
587943
 lib/common/ipc_server.c           | 26 ++++++++++
587943
 tools/crm_mon.c                   | 99 ++++++++++++++++++++++++---------------
587943
 5 files changed, 106 insertions(+), 44 deletions(-)
587943
587943
diff --git a/daemons/execd/remoted_proxy.c b/daemons/execd/remoted_proxy.c
587943
index 9329fa6..0fe39bf 100644
587943
--- a/daemons/execd/remoted_proxy.c
587943
+++ b/daemons/execd/remoted_proxy.c
587943
@@ -29,6 +29,7 @@ static qb_ipcs_service_t *cib_shm = NULL;
587943
 static qb_ipcs_service_t *attrd_ipcs = NULL;
587943
 static qb_ipcs_service_t *crmd_ipcs = NULL;
587943
 static qb_ipcs_service_t *stonith_ipcs = NULL;
587943
+static qb_ipcs_service_t *pacemakerd_ipcs = NULL;
587943
 
587943
 // An IPC provider is a cluster node controller connecting as a client
587943
 static GList *ipc_providers = NULL;
587943
@@ -126,6 +127,12 @@ stonith_proxy_accept(qb_ipcs_connection_t * c, uid_t uid, gid_t gid)
587943
 }
587943
 
587943
 static int32_t
587943
+pacemakerd_proxy_accept(qb_ipcs_connection_t * c, uid_t uid, gid_t gid)
587943
+{
587943
+    return -EREMOTEIO;
587943
+}
587943
+
587943
+static int32_t
587943
 cib_proxy_accept_rw(qb_ipcs_connection_t * c, uid_t uid, gid_t gid)
587943
 {
587943
     return ipc_proxy_accept(c, uid, gid, PCMK__SERVER_BASED_RW);
587943
@@ -356,6 +363,14 @@ static struct qb_ipcs_service_handlers stonith_proxy_callbacks = {
587943
     .connection_destroyed = ipc_proxy_destroy
587943
 };
587943
 
587943
+static struct qb_ipcs_service_handlers pacemakerd_proxy_callbacks = {
587943
+    .connection_accept = pacemakerd_proxy_accept,
587943
+    .connection_created = NULL,
587943
+    .msg_process = NULL,
587943
+    .connection_closed = NULL,
587943
+    .connection_destroyed = NULL
587943
+};
587943
+
587943
 static struct qb_ipcs_service_handlers cib_proxy_callbacks_ro = {
587943
     .connection_accept = cib_proxy_accept_ro,
587943
     .connection_created = NULL,
587943
@@ -422,6 +437,7 @@ ipc_proxy_init(void)
587943
                           &cib_proxy_callbacks_rw);
587943
     pcmk__serve_attrd_ipc(&attrd_ipcs, &attrd_proxy_callbacks);
587943
     pcmk__serve_fenced_ipc(&stonith_ipcs, &stonith_proxy_callbacks);
587943
+    pcmk__serve_pacemakerd_ipc(&pacemakerd_ipcs, &pacemakerd_proxy_callbacks);
587943
     crmd_ipcs = pcmk__serve_controld_ipc(&crmd_proxy_callbacks);
587943
     if (crmd_ipcs == NULL) {
587943
         crm_err("Failed to create controller: exiting and inhibiting respawn");
587943
@@ -444,6 +460,7 @@ ipc_proxy_cleanup(void)
587943
     pcmk__stop_based_ipc(cib_ro, cib_rw, cib_shm);
587943
     qb_ipcs_destroy(attrd_ipcs);
587943
     qb_ipcs_destroy(stonith_ipcs);
587943
+    qb_ipcs_destroy(pacemakerd_ipcs);
587943
     qb_ipcs_destroy(crmd_ipcs);
587943
     cib_ro = NULL;
587943
     cib_rw = NULL;
587943
diff --git a/daemons/pacemakerd/pacemakerd.c b/daemons/pacemakerd/pacemakerd.c
587943
index 509b0f8..4572b70 100644
587943
--- a/daemons/pacemakerd/pacemakerd.c
587943
+++ b/daemons/pacemakerd/pacemakerd.c
587943
@@ -1287,11 +1287,7 @@ main(int argc, char **argv)
587943
 
587943
     // Don't build CRM_RSCTMP_DIR, pacemaker-execd will do it
587943
 
587943
-    ipcs = mainloop_add_ipc_server(CRM_SYSTEM_MCP, QB_IPC_NATIVE, &mcp_ipc_callbacks);
587943
-    if (ipcs == NULL) {
587943
-        crm_err("Couldn't start IPC server");
587943
-        crm_exit(CRM_EX_OSERR);
587943
-    }
587943
+    pcmk__serve_pacemakerd_ipc(&ipcs, &mcp_ipc_callbacks);
587943
 
587943
 #ifdef SUPPORT_COROSYNC
587943
     /* Allows us to block shutdown */
587943
diff --git a/include/crm/common/ipc_internal.h b/include/crm/common/ipc_internal.h
587943
index cf935f3..fb82ce1 100644
587943
--- a/include/crm/common/ipc_internal.h
587943
+++ b/include/crm/common/ipc_internal.h
587943
@@ -221,6 +221,8 @@ void pcmk__serve_attrd_ipc(qb_ipcs_service_t **ipcs,
587943
                            struct qb_ipcs_service_handlers *cb);
587943
 void pcmk__serve_fenced_ipc(qb_ipcs_service_t **ipcs,
587943
                             struct qb_ipcs_service_handlers *cb);
587943
+void pcmk__serve_pacemakerd_ipc(qb_ipcs_service_t **ipcs,
587943
+                                struct qb_ipcs_service_handlers *cb);
587943
 qb_ipcs_service_t *pcmk__serve_controld_ipc(struct qb_ipcs_service_handlers *cb);
587943
 
587943
 void pcmk__serve_based_ipc(qb_ipcs_service_t **ipcs_ro,
587943
diff --git a/lib/common/ipc_server.c b/lib/common/ipc_server.c
587943
index 4d3e954..b3aaf8e 100644
587943
--- a/lib/common/ipc_server.c
587943
+++ b/lib/common/ipc_server.c
587943
@@ -922,6 +922,32 @@ pcmk__serve_fenced_ipc(qb_ipcs_service_t **ipcs,
587943
 }
587943
 
587943
 /*!
587943
+ * \internal
587943
+ * \brief Add an IPC server to the main loop for the pacemakerd API
587943
+ *
587943
+ * \param[in] cb  IPC callbacks
587943
+ *
587943
+ * \note This function exits with CRM_EX_OSERR if unable to create the servers.
587943
+ */
587943
+void
587943
+pcmk__serve_pacemakerd_ipc(qb_ipcs_service_t **ipcs,
587943
+                       struct qb_ipcs_service_handlers *cb)
587943
+{
587943
+    *ipcs = mainloop_add_ipc_server(CRM_SYSTEM_MCP, QB_IPC_NATIVE, cb);
587943
+
587943
+    if (*ipcs == NULL) {
587943
+        crm_err("Couldn't start pacemakerd IPC server");
587943
+        crm_warn("Verify pacemaker and pacemaker_remote are not both enabled.");
587943
+        /* sub-daemons are observed by pacemakerd. Thus we exit CRM_EX_FATAL
587943
+         * if we want to prevent pacemakerd from restarting them.
587943
+         * With pacemakerd we leave the exit-code shown to e.g. systemd
587943
+         * to what it was prior to moving the code here from pacemakerd.c
587943
+         */
587943
+        crm_exit(CRM_EX_OSERR);
587943
+    }
587943
+}
587943
+
587943
+/*!
587943
  * \brief Check whether string represents a client name used by cluster daemons
587943
  *
587943
  * \param[in] name  String to check
587943
diff --git a/tools/crm_mon.c b/tools/crm_mon.c
587943
index d4d4ac3..e58fed2 100644
587943
--- a/tools/crm_mon.c
587943
+++ b/tools/crm_mon.c
587943
@@ -83,6 +83,8 @@ static gchar **processed_args = NULL;
587943
 static time_t last_refresh = 0;
587943
 crm_trigger_t *refresh_trigger = NULL;
587943
 
587943
+static gboolean on_remote_node = FALSE;
587943
+
587943
 int interactive_fence_level = 0;
587943
 
587943
 static pcmk__supported_format_t formats[] = {
587943
@@ -988,48 +990,63 @@ pacemakerd_status(void)
587943
     }
587943
     pcmk_register_ipc_callback(pacemakerd_api, pacemakerd_event_cb, (void *) &state);
587943
     rc = pcmk_connect_ipc(pacemakerd_api, pcmk_ipc_dispatch_poll);
587943
-    if (rc == pcmk_rc_ok) {
587943
-        rc = pcmk_pacemakerd_api_ping(pacemakerd_api, crm_system_name);
587943
-        if (rc == pcmk_rc_ok) {
587943
-            rc = pcmk_poll_ipc(pacemakerd_api, options.reconnect_msec/2);
587943
+    switch (rc) {
587943
+        case pcmk_rc_ok:
587943
+            rc = pcmk_pacemakerd_api_ping(pacemakerd_api, crm_system_name);
587943
             if (rc == pcmk_rc_ok) {
587943
-                pcmk_dispatch_ipc(pacemakerd_api);
587943
-                rc = ENOTCONN;
587943
-                if ((output_format == mon_output_console) ||
587943
-                    (output_format == mon_output_plain)) {
587943
-                    switch (state) {
587943
-                        case pcmk_pacemakerd_state_running:
587943
-                            rc = pcmk_rc_ok;
587943
-                            break;
587943
-                        case pcmk_pacemakerd_state_starting_daemons:
587943
-                            print_as(output_format ,"Pacemaker daemons starting ...\n");
587943
-                            break;
587943
-                        case pcmk_pacemakerd_state_wait_for_ping:
587943
-                            print_as(output_format ,"Waiting for startup-trigger from SBD ...\n");
587943
-                            break;
587943
-                        case pcmk_pacemakerd_state_shutting_down:
587943
-                            print_as(output_format ,"Pacemaker daemons shutting down ...\n");
587943
-                            break;
587943
-                        case pcmk_pacemakerd_state_shutdown_complete:
587943
-                            /* assuming pacemakerd doesn't dispatch any pings after entering
587943
-                            * that state unless it is waiting for SBD
587943
-                            */
587943
-                            print_as(output_format ,"Pacemaker daemons shut down - reporting to SBD ...\n");
587943
-                            break;
587943
-                        default:
587943
-                            break;
587943
-                    }
587943
-                } else {
587943
-                    switch (state) {
587943
-                        case pcmk_pacemakerd_state_running:
587943
-                            rc = pcmk_rc_ok;
587943
-                            break;
587943
-                        default:
587943
-                            break;
587943
+                rc = pcmk_poll_ipc(pacemakerd_api, options.reconnect_msec/2);
587943
+                if (rc == pcmk_rc_ok) {
587943
+                    pcmk_dispatch_ipc(pacemakerd_api);
587943
+                    rc = ENOTCONN;
587943
+                    if ((output_format == mon_output_console) ||
587943
+                        (output_format == mon_output_plain)) {
587943
+                        switch (state) {
587943
+                            case pcmk_pacemakerd_state_running:
587943
+                                rc = pcmk_rc_ok;
587943
+                                break;
587943
+                            case pcmk_pacemakerd_state_starting_daemons:
587943
+                                print_as(output_format ,"Pacemaker daemons starting ...\n");
587943
+                                break;
587943
+                            case pcmk_pacemakerd_state_wait_for_ping:
587943
+                                print_as(output_format ,"Waiting for startup-trigger from SBD ...\n");
587943
+                                break;
587943
+                            case pcmk_pacemakerd_state_shutting_down:
587943
+                                print_as(output_format ,"Pacemaker daemons shutting down ...\n");
587943
+                                break;
587943
+                            case pcmk_pacemakerd_state_shutdown_complete:
587943
+                                /* assuming pacemakerd doesn't dispatch any pings after entering
587943
+                                * that state unless it is waiting for SBD
587943
+                                */
587943
+                                print_as(output_format ,"Pacemaker daemons shut down - reporting to SBD ...\n");
587943
+                                break;
587943
+                            default:
587943
+                                break;
587943
+                        }
587943
+                    } else {
587943
+                        switch (state) {
587943
+                            case pcmk_pacemakerd_state_running:
587943
+                                rc = pcmk_rc_ok;
587943
+                                break;
587943
+                            default:
587943
+                                break;
587943
+                        }
587943
                     }
587943
                 }
587943
             }
587943
-        }
587943
+            break;
587943
+        case EREMOTEIO:
587943
+            rc = pcmk_rc_ok;
587943
+            on_remote_node = TRUE;
587943
+#if CURSES_ENABLED
587943
+            /* just show this if refresh is gonna remove all traces */
587943
+            if (output_format == mon_output_console) {
587943
+                print_as(output_format ,
587943
+                    "Running on remote-node waiting to be connected by cluster ...\n");
587943
+            }
587943
+#endif
587943
+            break;
587943
+        default:
587943
+            break;
587943
     }
587943
     pcmk_free_ipc_api(pacemakerd_api);
587943
     /* returning with ENOTCONN triggers a retry */
587943
@@ -1348,7 +1365,11 @@ handle_connection_failures(int rc)
587943
                     pcmk_rc_str(rc));
587943
         rc = MON_STATUS_CRIT;
587943
     } else if (rc == ENOTCONN) {
587943
-        g_set_error(&error, PCMK__EXITC_ERROR, CRM_EX_ERROR, "Error: cluster is not available on this node");
587943
+        if (on_remote_node) {
587943
+            g_set_error(&error, PCMK__EXITC_ERROR, CRM_EX_ERROR, "Error: remote-node not connected to cluster");
587943
+        } else {
587943
+            g_set_error(&error, PCMK__EXITC_ERROR, CRM_EX_ERROR, "Error: cluster is not available on this node");
587943
+        }
587943
         rc = pcmk_rc2exitc(rc);
587943
     } else {
587943
         g_set_error(&error, PCMK__EXITC_ERROR, CRM_EX_ERROR, "Connection to cluster failed: %s", pcmk_rc_str(rc));
587943
-- 
587943
1.8.3.1
587943