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