root/daemons/attrd/pacemaker-attrd.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. attrd_cpg_dispatch
  2. attrd_cpg_destroy
  3. attrd_cib_destroy_cb
  4. attrd_erase_cb
  5. attrd_erase_attrs
  6. attrd_cib_connect
  7. attrd_cib_init
  8. attrd_ipc_dispatch
  9. attrd_ipc_fini
  10. attrd_cluster_connect
  11. main

   1 /*
   2  * Copyright 2013-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <sys/param.h>
  13 #include <stdio.h>
  14 #include <sys/types.h>
  15 #include <sys/stat.h>
  16 #include <unistd.h>
  17 
  18 #include <stdlib.h>
  19 #include <errno.h>
  20 #include <fcntl.h>
  21 
  22 #include <crm/crm.h>
  23 #include <crm/cib/internal.h>
  24 #include <crm/msg_xml.h>
  25 #include <crm/pengine/rules.h>
  26 #include <crm/common/iso8601.h>
  27 #include <crm/common/ipc.h>
  28 #include <crm/common/ipc_internal.h>
  29 #include <crm/common/xml.h>
  30 #include <crm/cluster/internal.h>
  31 
  32 #include <crm/common/attrd_internal.h>
  33 #include "pacemaker-attrd.h"
  34 
  35 lrmd_t *the_lrmd = NULL;
  36 crm_cluster_t *attrd_cluster = NULL;
  37 crm_trigger_t *attrd_config_read = NULL;
  38 static crm_exit_t attrd_exit_status = CRM_EX_OK;
  39 
  40 static void
  41 attrd_cpg_dispatch(cpg_handle_t handle,
     /* [previous][next][first][last][top][bottom][index][help] */
  42                  const struct cpg_name *groupName,
  43                  uint32_t nodeid, uint32_t pid, void *msg, size_t msg_len)
  44 {
  45     uint32_t kind = 0;
  46     xmlNode *xml = NULL;
  47     const char *from = NULL;
  48     char *data = pcmk_message_common_cs(handle, nodeid, pid, msg, &kind, &from);
  49 
  50     if(data == NULL) {
  51         return;
  52     }
  53 
  54     if (kind == crm_class_cluster) {
  55         xml = string2xml(data);
  56     }
  57 
  58     if (xml == NULL) {
  59         crm_err("Bad message of class %d received from %s[%u]: '%.120s'", kind, from, nodeid, data);
  60     } else {
  61         crm_node_t *peer = crm_get_peer(nodeid, from);
  62 
  63         attrd_peer_message(peer, xml);
  64     }
  65 
  66     free_xml(xml);
  67     free(data);
  68 }
  69 
  70 static void
  71 attrd_cpg_destroy(gpointer unused)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73     if (attrd_shutting_down()) {
  74         crm_info("Corosync disconnection complete");
  75 
  76     } else {
  77         crm_crit("Lost connection to cluster layer, shutting down");
  78         attrd_exit_status = CRM_EX_DISCONNECT;
  79         attrd_shutdown(0);
  80     }
  81 }
  82 
  83 static void
  84 attrd_cib_destroy_cb(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86     cib_t *conn = user_data;
  87 
  88     conn->cmds->signoff(conn);  /* Ensure IPC is cleaned up */
  89 
  90     if (attrd_shutting_down()) {
  91         crm_info("Connection disconnection complete");
  92 
  93     } else {
  94         /* eventually this should trigger a reconnect, not a shutdown */
  95         crm_crit("Lost connection to the CIB manager, shutting down");
  96         attrd_exit_status = CRM_EX_DISCONNECT;
  97         attrd_shutdown(0);
  98     }
  99 
 100     return;
 101 }
 102 
 103 static void
 104 attrd_erase_cb(xmlNode *msg, int call_id, int rc, xmlNode *output,
     /* [previous][next][first][last][top][bottom][index][help] */
 105                void *user_data)
 106 {
 107     do_crm_log_unlikely((rc? LOG_NOTICE : LOG_DEBUG),
 108                         "Cleared transient attributes: %s "
 109                         CRM_XS " xpath=%s rc=%d",
 110                         pcmk_strerror(rc), (char *) user_data, rc);
 111 }
 112 
 113 #define XPATH_TRANSIENT "//node_state[@uname='%s']/" XML_TAG_TRANSIENT_NODEATTRS
 114 
 115 /*!
 116  * \internal
 117  * \brief Wipe all transient attributes for this node from the CIB
 118  *
 119  * Clear any previous transient node attributes from the CIB. This is
 120  * normally done by the DC's controller when this node leaves the cluster, but
 121  * this handles the case where the node restarted so quickly that the
 122  * cluster layer didn't notice.
 123  *
 124  * \todo If pacemaker-attrd respawns after crashing (see PCMK_respawned),
 125  *       ideally we'd skip this and sync our attributes from the writer.
 126  *       However, currently we reject any values for us that the writer has, in
 127  *       attrd_peer_update().
 128  */
 129 static void
 130 attrd_erase_attrs(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132     int call_id;
 133     char *xpath = crm_strdup_printf(XPATH_TRANSIENT, attrd_cluster->uname);
 134 
 135     crm_info("Clearing transient attributes from CIB " CRM_XS " xpath=%s",
 136              xpath);
 137 
 138     call_id = the_cib->cmds->remove(the_cib, xpath, NULL,
 139                                     cib_quorum_override | cib_xpath);
 140     the_cib->cmds->register_callback_full(the_cib, call_id, 120, FALSE, xpath,
 141                                           "attrd_erase_cb", attrd_erase_cb,
 142                                           free);
 143 }
 144 
 145 static int
 146 attrd_cib_connect(int max_retry)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     static int attempts = 0;
 149 
 150     int rc = -ENOTCONN;
 151 
 152     the_cib = cib_new();
 153     if (the_cib == NULL) {
 154         return -ENOTCONN;
 155     }
 156 
 157     do {
 158         if(attempts > 0) {
 159             sleep(attempts);
 160         }
 161 
 162         attempts++;
 163         crm_debug("Connection attempt %d to the CIB manager", attempts);
 164         rc = the_cib->cmds->signon(the_cib, T_ATTRD, cib_command);
 165 
 166     } while(rc != pcmk_ok && attempts < max_retry);
 167 
 168     if (rc != pcmk_ok) {
 169         crm_err("Connection to the CIB manager failed: %s " CRM_XS " rc=%d",
 170                 pcmk_strerror(rc), rc);
 171         goto cleanup;
 172     }
 173 
 174     crm_debug("Connected to the CIB manager after %d attempts", attempts);
 175 
 176     rc = the_cib->cmds->set_connection_dnotify(the_cib, attrd_cib_destroy_cb);
 177     if (rc != pcmk_ok) {
 178         crm_err("Could not set disconnection callback");
 179         goto cleanup;
 180     }
 181 
 182     rc = the_cib->cmds->add_notify_callback(the_cib, T_CIB_REPLACE_NOTIFY, attrd_cib_replaced_cb);
 183     if(rc != pcmk_ok) {
 184         crm_err("Could not set CIB notification callback");
 185         goto cleanup;
 186     }
 187 
 188     rc = the_cib->cmds->add_notify_callback(the_cib, T_CIB_DIFF_NOTIFY, attrd_cib_updated_cb);
 189     if (rc != pcmk_ok) {
 190         crm_err("Could not set CIB notification callback (update)");
 191         goto cleanup;
 192     }
 193 
 194     return pcmk_ok;
 195 
 196   cleanup:
 197     the_cib->cmds->signoff(the_cib);
 198     cib_delete(the_cib);
 199     the_cib = NULL;
 200     return -ENOTCONN;
 201 }
 202 
 203 /*!
 204  * \internal
 205  * \brief Prepare the CIB after cluster is connected
 206  */
 207 static void
 208 attrd_cib_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210     // We have no attribute values in memory, wipe the CIB to match
 211     attrd_erase_attrs();
 212 
 213     // Set a trigger for reading the CIB (for the alerts section)
 214     attrd_config_read = mainloop_add_trigger(G_PRIORITY_HIGH, attrd_read_options, NULL);
 215 
 216     // Always read the CIB at start-up
 217     mainloop_set_trigger(attrd_config_read);
 218 }
 219 
 220 static qb_ipcs_service_t *ipcs = NULL;
 221 
 222 static int32_t
 223 attrd_ipc_dispatch(qb_ipcs_connection_t * c, void *data, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
 224 {
 225     uint32_t id = 0;
 226     uint32_t flags = 0;
 227     pcmk__client_t *client = pcmk__find_client(c);
 228     xmlNode *xml = NULL;
 229     const char *op;
 230 
 231     // Sanity-check, and parse XML from IPC data
 232     CRM_CHECK((c != NULL) && (client != NULL), return 0);
 233     if (data == NULL) {
 234         crm_debug("No IPC data from PID %d", pcmk__client_pid(c));
 235         return 0;
 236     }
 237     xml = pcmk__client_data2xml(client, data, &id, &flags);
 238     if (xml == NULL) {
 239         crm_debug("Unrecognizable IPC data from PID %d", pcmk__client_pid(c));
 240         return 0;
 241     }
 242 
 243     CRM_ASSERT(client->user != NULL);
 244     pcmk__update_acl_user(xml, PCMK__XA_ATTR_USER, client->user);
 245 
 246     op = crm_element_value(xml, PCMK__XA_TASK);
 247 
 248     if (client->name == NULL) {
 249         const char *value = crm_element_value(xml, F_ORIG);
 250         client->name = crm_strdup_printf("%s.%d", value?value:"unknown", client->pid);
 251     }
 252 
 253     if (pcmk__str_eq(op, PCMK__ATTRD_CMD_PEER_REMOVE, pcmk__str_casei)) {
 254         attrd_send_ack(client, id, flags);
 255         attrd_client_peer_remove(client, xml);
 256 
 257     } else if (pcmk__str_eq(op, PCMK__ATTRD_CMD_CLEAR_FAILURE, pcmk__str_casei)) {
 258         attrd_send_ack(client, id, flags);
 259         attrd_client_clear_failure(xml);
 260 
 261     } else if (pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE, pcmk__str_casei)) {
 262         attrd_send_ack(client, id, flags);
 263         attrd_client_update(xml);
 264 
 265     } else if (pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE_BOTH, pcmk__str_casei)) {
 266         attrd_send_ack(client, id, flags);
 267         attrd_client_update(xml);
 268 
 269     } else if (pcmk__str_eq(op, PCMK__ATTRD_CMD_UPDATE_DELAY, pcmk__str_casei)) {
 270         attrd_send_ack(client, id, flags);
 271         attrd_client_update(xml);
 272 
 273     } else if (pcmk__str_eq(op, PCMK__ATTRD_CMD_REFRESH, pcmk__str_casei)) {
 274         attrd_send_ack(client, id, flags);
 275         attrd_client_refresh();
 276 
 277     } else if (pcmk__str_eq(op, PCMK__ATTRD_CMD_QUERY, pcmk__str_casei)) {
 278         /* queries will get reply, so no ack is necessary */
 279         attrd_client_query(client, id, flags, xml);
 280 
 281     } else {
 282         crm_info("Ignoring request from client %s with unknown operation %s",
 283                  pcmk__client_name(client), op);
 284     }
 285 
 286     free_xml(xml);
 287     return 0;
 288 }
 289 
 290 void
 291 attrd_ipc_fini(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 292 {
 293     if (ipcs != NULL) {
 294         pcmk__drop_all_clients(ipcs);
 295         qb_ipcs_destroy(ipcs);
 296         ipcs = NULL;
 297     }
 298 }
 299 
 300 static int
 301 attrd_cluster_connect(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303     attrd_cluster = calloc(1, sizeof(crm_cluster_t));
 304 
 305     attrd_cluster->destroy = attrd_cpg_destroy;
 306     attrd_cluster->cpg.cpg_deliver_fn = attrd_cpg_dispatch;
 307     attrd_cluster->cpg.cpg_confchg_fn = pcmk_cpg_membership;
 308 
 309     crm_set_status_callback(&attrd_peer_change_cb);
 310 
 311     if (crm_cluster_connect(attrd_cluster) == FALSE) {
 312         crm_err("Cluster connection failed");
 313         return -ENOTCONN;
 314     }
 315     return pcmk_ok;
 316 }
 317 
 318 static pcmk__cli_option_t long_options[] = {
 319     // long option, argument type, storage, short option, description, flags
 320     {
 321         "help",     no_argument, NULL, '?',
 322         "\tThis text", pcmk__option_default
 323     },
 324     {
 325         "verbose",  no_argument, NULL, 'V',
 326         "\tIncrease debug output", pcmk__option_default
 327     },
 328     { 0, 0, 0, 0 }
 329 };
 330 
 331 int
 332 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 333 {
 334     int flag = 0;
 335     int index = 0;
 336     int argerr = 0;
 337     crm_ipc_t *old_instance = NULL;
 338 
 339     attrd_init_mainloop();
 340     crm_log_preinit(NULL, argc, argv);
 341     pcmk__set_cli_options(NULL, "[options]", long_options,
 342                           "daemon for managing Pacemaker node attributes");
 343 
 344     mainloop_add_signal(SIGTERM, attrd_shutdown);
 345 
 346      while (1) {
 347         flag = pcmk__next_cli_option(argc, argv, &index, NULL);
 348         if (flag == -1)
 349             break;
 350 
 351         switch (flag) {
 352             case 'V':
 353                 crm_bump_log_level(argc, argv);
 354                 break;
 355             case 'h':          /* Help message */
 356                 pcmk__cli_help(flag, CRM_EX_OK);
 357                 break;
 358             default:
 359                 ++argerr;
 360                 break;
 361         }
 362     }
 363 
 364     if (optind > argc) {
 365         ++argerr;
 366     }
 367 
 368     if (argerr) {
 369         pcmk__cli_help('?', CRM_EX_USAGE);
 370     }
 371 
 372     crm_log_init(T_ATTRD, LOG_INFO, TRUE, FALSE, argc, argv, FALSE);
 373     crm_notice("Starting Pacemaker node attribute manager");
 374 
 375     old_instance = crm_ipc_new(T_ATTRD, 0);
 376     if (crm_ipc_connect(old_instance)) {
 377         /* IPC end-point already up */
 378         crm_ipc_close(old_instance);
 379         crm_ipc_destroy(old_instance);
 380         crm_err("pacemaker-attrd is already active, aborting startup");
 381         crm_exit(CRM_EX_OK);
 382     } else {
 383         /* not up or not authentic, we'll proceed either way */
 384         crm_ipc_destroy(old_instance);
 385         old_instance = NULL;
 386     }
 387 
 388     attributes = pcmk__strkey_table(NULL, free_attribute);
 389 
 390     /* Connect to the CIB before connecting to the cluster or listening for IPC.
 391      * This allows us to assume the CIB is connected whenever we process a
 392      * cluster or IPC message (which also avoids start-up race conditions).
 393      */
 394     if (attrd_cib_connect(30) != pcmk_ok) {
 395         attrd_exit_status = CRM_EX_FATAL;
 396         goto done;
 397     }
 398     crm_info("CIB connection active");
 399 
 400     if (attrd_cluster_connect() != pcmk_ok) {
 401         attrd_exit_status = CRM_EX_FATAL;
 402         goto done;
 403     }
 404     crm_info("Cluster connection active");
 405 
 406     // Initialization that requires the cluster to be connected
 407     attrd_election_init();
 408     attrd_cib_init();
 409 
 410     /* Set a private attribute for ourselves with the protocol version we
 411      * support. This lets all nodes determine the minimum supported version
 412      * across all nodes. It also ensures that the writer learns our node name,
 413      * so it can send our attributes to the CIB.
 414      */
 415     attrd_broadcast_protocol();
 416 
 417     attrd_init_ipc(&ipcs, attrd_ipc_dispatch);
 418     crm_notice("Pacemaker node attribute manager successfully started and accepting connections");
 419     attrd_run_mainloop();
 420 
 421   done:
 422     crm_info("Shutting down attribute manager");
 423 
 424     attrd_election_fini();
 425     attrd_ipc_fini();
 426     attrd_lrmd_disconnect();
 427     attrd_cib_disconnect();
 428     g_hash_table_destroy(attributes);
 429 
 430     crm_exit(attrd_exit_status);
 431 }

/* [previous][next][first][last][top][bottom][index][help] */