root/daemons/controld/controld_cib.c

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

DEFINITIONS

This source file includes following definitions.
  1. handle_cib_disconnect
  2. do_cib_updated
  3. controld_disconnect_cib_manager
  4. do_cib_control
  5. cib_op_timeout
  6. crmd_cib_smart_opt
  7. cib_delete_callback
  8. controld_node_state_deletion_strings
  9. controld_delete_node_state
  10. controld_delete_resource_history
  11. build_parameter_list
  12. append_restart_list
  13. append_secure_list
  14. controld_add_resource_history_xml_as
  15. controld_record_pending_op
  16. cib_rsc_callback
  17. should_preserve_lock
  18. controld_update_cib
  19. controld_update_resource_history
  20. controld_delete_action_history
  21. controld_cib_delete_last_failure
  22. controld_delete_action_history_by_key

   1 /*
   2  * Copyright 2004-2025 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 <unistd.h>  /* sleep */
  13 
  14 #include <crm/common/alerts_internal.h>
  15 #include <crm/common/xml.h>
  16 #include <crm/crm.h>
  17 #include <crm/lrmd_internal.h>
  18 
  19 #include <pacemaker-controld.h>
  20 
  21 // Call ID of the most recent in-progress CIB resource update (or 0 if none)
  22 static int pending_rsc_update = 0;
  23 
  24 /*!
  25  * \internal
  26  * \brief Respond to a dropped CIB connection
  27  *
  28  * \param[in] user_data  CIB connection that dropped
  29  */
  30 static void
  31 handle_cib_disconnect(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33     CRM_LOG_ASSERT(user_data == controld_globals.cib_conn);
  34 
  35     controld_trigger_fsa();
  36     controld_globals.cib_conn->state = cib_disconnected;
  37 
  38     if (pcmk_is_set(controld_globals.fsa_input_register, R_CIB_CONNECTED)) {
  39         // @TODO This should trigger a reconnect, not a shutdown
  40         crm_crit("Lost connection to the CIB manager, shutting down");
  41         register_fsa_input(C_FSA_INTERNAL, I_ERROR, NULL);
  42         controld_clear_fsa_input_flags(R_CIB_CONNECTED);
  43 
  44     } else { // Expected
  45         crm_info("Disconnected from the CIB manager");
  46     }
  47 }
  48 
  49 static void
  50 do_cib_updated(const char *event, xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     const xmlNode *patchset = NULL;
  53     const char *client_name = NULL;
  54 
  55     crm_debug("Received CIB diff notification: DC=%s", pcmk__btoa(AM_I_DC));
  56 
  57     if (cib__get_notify_patchset(msg, &patchset) != pcmk_rc_ok) {
  58         return;
  59     }
  60 
  61     if (pcmk__cib_element_in_patchset(patchset, PCMK_XE_ALERTS)
  62         || pcmk__cib_element_in_patchset(patchset, PCMK_XE_CRM_CONFIG)) {
  63 
  64         controld_trigger_config();
  65     }
  66 
  67     if (!AM_I_DC) {
  68         // We're not in control of the join sequence
  69         return;
  70     }
  71 
  72     client_name = crm_element_value(msg, PCMK__XA_CIB_CLIENTNAME);
  73     if (!cib__client_triggers_refresh(client_name)) {
  74         // The CIB is still accurate
  75         return;
  76     }
  77 
  78     if (pcmk__cib_element_in_patchset(patchset, PCMK_XE_NODES)
  79         || pcmk__cib_element_in_patchset(patchset, PCMK_XE_STATUS)) {
  80 
  81         /* An unsafe client modified the PCMK_XE_NODES or PCMK_XE_STATUS
  82          * section. Ensure the node list is up-to-date, and start the join
  83          * process again so we get everyone's current resource history.
  84          */
  85         if (client_name == NULL) {
  86             client_name = crm_element_value(msg, PCMK__XA_CIB_CLIENTID);
  87         }
  88         crm_notice("Populating nodes and starting an election after %s event "
  89                    "triggered by %s",
  90                    event, pcmk__s(client_name, "(unidentified client)"));
  91 
  92         populate_cib_nodes(controld_node_update_quick|controld_node_update_all,
  93                            __func__);
  94         register_fsa_input(C_FSA_INTERNAL, I_ELECTION, NULL);
  95     }
  96 }
  97 
  98 void
  99 controld_disconnect_cib_manager(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101     cib_t *cib_conn = controld_globals.cib_conn;
 102 
 103     pcmk__assert(cib_conn != NULL);
 104 
 105     crm_debug("Disconnecting from the CIB manager");
 106 
 107     controld_clear_fsa_input_flags(R_CIB_CONNECTED);
 108 
 109     cib_conn->cmds->del_notify_callback(cib_conn, PCMK__VALUE_CIB_DIFF_NOTIFY,
 110                                         do_cib_updated);
 111     cib_free_callbacks(cib_conn);
 112 
 113     if (cib_conn->state != cib_disconnected) {
 114         cib_conn->cmds->set_secondary(cib_conn, cib_discard_reply);
 115         cib_conn->cmds->signoff(cib_conn);
 116     }
 117 }
 118 
 119 /* A_CIB_STOP, A_CIB_START, O_CIB_RESTART */
 120 void
 121 do_cib_control(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
 122                enum crmd_fsa_cause cause,
 123                enum crmd_fsa_state cur_state,
 124                enum crmd_fsa_input current_input, fsa_data_t * msg_data)
 125 {
 126     static int cib_retries = 0;
 127 
 128     cib_t *cib_conn = controld_globals.cib_conn;
 129 
 130     void (*dnotify_fn) (gpointer user_data) = handle_cib_disconnect;
 131     void (*update_cb) (const char *event, xmlNodePtr msg) = do_cib_updated;
 132 
 133     int rc = pcmk_ok;
 134 
 135     pcmk__assert(cib_conn != NULL);
 136 
 137     if (pcmk_is_set(action, A_CIB_STOP)) {
 138         if ((cib_conn->state != cib_disconnected)
 139             && (pending_rsc_update != 0)) {
 140 
 141             crm_info("Waiting for resource update %d to complete",
 142                      pending_rsc_update);
 143             crmd_fsa_stall(FALSE);
 144             return;
 145         }
 146         controld_disconnect_cib_manager();
 147     }
 148 
 149     if (!pcmk_is_set(action, A_CIB_START)) {
 150         return;
 151     }
 152 
 153     if (cur_state == S_STOPPING) {
 154         crm_err("Ignoring request to connect to the CIB manager after "
 155                 "shutdown");
 156         return;
 157     }
 158 
 159     rc = cib_conn->cmds->signon(cib_conn, crm_system_name,
 160                                 cib_command_nonblocking);
 161 
 162     if (rc != pcmk_ok) {
 163         // A short wait that usually avoids stalling the FSA
 164         sleep(1);
 165         rc = cib_conn->cmds->signon(cib_conn, crm_system_name,
 166                                     cib_command_nonblocking);
 167     }
 168 
 169     if (rc != pcmk_ok) {
 170         crm_info("Could not connect to the CIB manager: %s", pcmk_strerror(rc));
 171 
 172     } else if (cib_conn->cmds->set_connection_dnotify(cib_conn,
 173                                                       dnotify_fn) != pcmk_ok) {
 174         crm_err("Could not set dnotify callback");
 175 
 176     } else if (cib_conn->cmds->add_notify_callback(cib_conn,
 177                                                    PCMK__VALUE_CIB_DIFF_NOTIFY,
 178                                                    update_cb) != pcmk_ok) {
 179         crm_err("Could not set CIB notification callback (update)");
 180 
 181     } else {
 182         controld_set_fsa_input_flags(R_CIB_CONNECTED);
 183         cib_retries = 0;
 184     }
 185 
 186     if (!pcmk_is_set(controld_globals.fsa_input_register, R_CIB_CONNECTED)) {
 187         cib_retries++;
 188 
 189         if (cib_retries < 30) {
 190             crm_warn("Couldn't complete CIB registration %d times... "
 191                      "pause and retry", cib_retries);
 192             controld_start_wait_timer();
 193             crmd_fsa_stall(FALSE);
 194 
 195         } else {
 196             crm_err("Could not complete CIB registration %d times... "
 197                     "hard error", cib_retries);
 198             register_fsa_error(C_FSA_INTERNAL, I_ERROR, NULL);
 199         }
 200     }
 201 }
 202 
 203 #define MIN_CIB_OP_TIMEOUT (30)
 204 
 205 /*!
 206  * \internal
 207  * \brief Get the timeout (in seconds) that should be used with CIB operations
 208  *
 209  * \return The maximum of 30 seconds, the value of the PCMK_cib_timeout
 210  *         environment variable, or 10 seconds times one more than the number of
 211  *         nodes in the cluster.
 212  */
 213 unsigned int
 214 cib_op_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216     unsigned int calculated_timeout = 10U * (pcmk__cluster_num_active_nodes()
 217                                              + pcmk__cluster_num_remote_nodes()
 218                                              + 1U);
 219 
 220     calculated_timeout = QB_MAX(calculated_timeout, MIN_CIB_OP_TIMEOUT);
 221     crm_trace("Calculated timeout: %s",
 222               pcmk__readable_interval(calculated_timeout * 1000));
 223 
 224     if (controld_globals.cib_conn) {
 225         controld_globals.cib_conn->call_timeout = calculated_timeout;
 226     }
 227     return calculated_timeout;
 228 }
 229 
 230 /*!
 231  * \internal
 232  * \brief Get CIB call options to use local scope if primary is unavailable
 233  *
 234  * \return CIB call options
 235  */
 236 int
 237 crmd_cib_smart_opt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 238 {
 239     int call_opt = cib_none;
 240 
 241     if ((controld_globals.fsa_state == S_ELECTION)
 242         || (controld_globals.fsa_state == S_PENDING)) {
 243         crm_info("Sending update to local CIB in state: %s",
 244                  fsa_state2string(controld_globals.fsa_state));
 245         cib__set_call_options(call_opt, "update", cib_none);
 246     }
 247     return call_opt;
 248 }
 249 
 250 static void
 251 cib_delete_callback(xmlNode *msg, int call_id, int rc, xmlNode *output,
     /* [previous][next][first][last][top][bottom][index][help] */
 252                     void *user_data)
 253 {
 254     char *desc = user_data;
 255 
 256     if (rc == 0) {
 257         crm_debug("Deletion of %s (via CIB call %d) succeeded", desc, call_id);
 258     } else {
 259         crm_warn("Deletion of %s (via CIB call %d) failed: %s " QB_XS " rc=%d",
 260                  desc, call_id, pcmk_strerror(rc), rc);
 261     }
 262 }
 263 
 264 // Searches for various portions of PCMK__XE_NODE_STATE to delete
 265 
 266 // Match a particular node's PCMK__XE_NODE_STATE (takes node name 1x)
 267 #define XPATH_NODE_STATE "//" PCMK__XE_NODE_STATE "[@" PCMK_XA_UNAME "='%s']"
 268 
 269 // Node's lrm section (name 1x)
 270 #define XPATH_NODE_LRM XPATH_NODE_STATE "/" PCMK__XE_LRM
 271 
 272 /* Node's PCMK__XE_LRM_RSC_OP entries and PCMK__XE_LRM_RESOURCE entries without
 273  * unexpired lock
 274  * (name 2x, (seconds_since_epoch - PCMK_OPT_SHUTDOWN_LOCK_LIMIT) 1x)
 275  */
 276 #define XPATH_NODE_LRM_UNLOCKED XPATH_NODE_STATE "//" PCMK__XE_LRM_RSC_OP   \
 277                                 "|" XPATH_NODE_STATE                        \
 278                                 "//" PCMK__XE_LRM_RESOURCE                  \
 279                                 "[not(@" PCMK_OPT_SHUTDOWN_LOCK ") "        \
 280                                     "or " PCMK_OPT_SHUTDOWN_LOCK "<%lld]"
 281 
 282 // Node's PCMK__XE_TRANSIENT_ATTRIBUTES section (name 1x)
 283 #define XPATH_NODE_ATTRS XPATH_NODE_STATE "/" PCMK__XE_TRANSIENT_ATTRIBUTES
 284 
 285 // Everything under PCMK__XE_NODE_STATE (name 1x)
 286 #define XPATH_NODE_ALL          XPATH_NODE_STATE "/*"
 287 
 288 /* Unlocked history + transient attributes
 289  * (name 2x, (seconds_since_epoch - PCMK_OPT_SHUTDOWN_LOCK_LIMIT) 1x, name 1x)
 290  */
 291 #define XPATH_NODE_ALL_UNLOCKED XPATH_NODE_LRM_UNLOCKED "|" XPATH_NODE_ATTRS
 292 
 293 /*!
 294  * \internal
 295  * \brief Get the XPath and description of a node state section to be deleted
 296  *
 297  * \param[in]  uname    Desired node
 298  * \param[in]  section  Subsection of \c PCMK__XE_NODE_STATE to be deleted
 299  * \param[out] xpath    Where to store XPath of \p section
 300  * \param[out] desc     If not \c NULL, where to store description of \p section
 301  */
 302 void
 303 controld_node_state_deletion_strings(const char *uname,
     /* [previous][next][first][last][top][bottom][index][help] */
 304                                      enum controld_section_e section,
 305                                      char **xpath, char **desc)
 306 {
 307     const char *desc_pre = NULL;
 308 
 309     // Shutdown locks that started before this time are expired
 310     long long expire = (long long) time(NULL)
 311                        - controld_globals.shutdown_lock_limit;
 312 
 313     switch (section) {
 314         case controld_section_lrm:
 315             *xpath = crm_strdup_printf(XPATH_NODE_LRM, uname);
 316             desc_pre = "resource history";
 317             break;
 318         case controld_section_lrm_unlocked:
 319             *xpath = crm_strdup_printf(XPATH_NODE_LRM_UNLOCKED,
 320                                        uname, uname, expire);
 321             desc_pre = "resource history (other than shutdown locks)";
 322             break;
 323         case controld_section_attrs:
 324             *xpath = crm_strdup_printf(XPATH_NODE_ATTRS, uname);
 325             desc_pre = "transient attributes";
 326             break;
 327         case controld_section_all:
 328             *xpath = crm_strdup_printf(XPATH_NODE_ALL, uname);
 329             desc_pre = "all state";
 330             break;
 331         case controld_section_all_unlocked:
 332             *xpath = crm_strdup_printf(XPATH_NODE_ALL_UNLOCKED,
 333                                        uname, uname, expire, uname);
 334             desc_pre = "all state (other than shutdown locks)";
 335             break;
 336         default:
 337             // We called this function incorrectly
 338             pcmk__assert(false);
 339             break;
 340     }
 341 
 342     if (desc != NULL) {
 343         *desc = crm_strdup_printf("%s for node %s", desc_pre, uname);
 344     }
 345 }
 346 
 347 /*!
 348  * \internal
 349  * \brief Delete subsection of a node's CIB \c PCMK__XE_NODE_STATE
 350  *
 351  * \param[in] uname    Desired node
 352  * \param[in] section  Subsection of \c PCMK__XE_NODE_STATE to delete
 353  * \param[in] options  CIB call options to use
 354  */
 355 void
 356 controld_delete_node_state(const char *uname, enum controld_section_e section,
     /* [previous][next][first][last][top][bottom][index][help] */
 357                            int options)
 358 {
 359     cib_t *cib = controld_globals.cib_conn;
 360     char *xpath = NULL;
 361     char *desc = NULL;
 362     int cib_rc = pcmk_ok;
 363 
 364     pcmk__assert((uname != NULL) && (cib != NULL));
 365 
 366     controld_node_state_deletion_strings(uname, section, &xpath, &desc);
 367 
 368     cib__set_call_options(options, "node state deletion",
 369                           cib_xpath|cib_multiple);
 370     cib_rc = cib->cmds->remove(cib, xpath, NULL, options);
 371     fsa_register_cib_callback(cib_rc, desc, cib_delete_callback);
 372     crm_info("Deleting %s (via CIB call %d) " QB_XS " xpath=%s",
 373              desc, cib_rc, xpath);
 374 
 375     // CIB library handles freeing desc
 376     free(xpath);
 377 }
 378 
 379 // Takes node name and resource ID
 380 #define XPATH_RESOURCE_HISTORY "//" PCMK__XE_NODE_STATE                 \
 381                                "[@" PCMK_XA_UNAME "='%s']/"             \
 382                                PCMK__XE_LRM "/" PCMK__XE_LRM_RESOURCES  \
 383                                "/" PCMK__XE_LRM_RESOURCE                \
 384                                "[@" PCMK_XA_ID "='%s']"
 385 // @TODO could add "and @PCMK_OPT_SHUTDOWN_LOCK" to limit to locks
 386 
 387 /*!
 388  * \internal
 389  * \brief Clear resource history from CIB for a given resource and node
 390  *
 391  * \param[in]  rsc_id        ID of resource to be cleared
 392  * \param[in]  node          Node whose resource history should be cleared
 393  * \param[in]  user_name     ACL user name to use
 394  * \param[in]  call_options  CIB call options
 395  *
 396  * \return Standard Pacemaker return code
 397  */
 398 int
 399 controld_delete_resource_history(const char *rsc_id, const char *node,
     /* [previous][next][first][last][top][bottom][index][help] */
 400                                  const char *user_name, int call_options)
 401 {
 402     char *desc = NULL;
 403     char *xpath = NULL;
 404     int rc = pcmk_rc_ok;
 405     cib_t *cib = controld_globals.cib_conn;
 406 
 407     CRM_CHECK((rsc_id != NULL) && (node != NULL), return EINVAL);
 408 
 409     desc = crm_strdup_printf("resource history for %s on %s", rsc_id, node);
 410     if (cib == NULL) {
 411         crm_err("Unable to clear %s: no CIB connection", desc);
 412         free(desc);
 413         return ENOTCONN;
 414     }
 415 
 416     // Ask CIB to delete the entry
 417     xpath = crm_strdup_printf(XPATH_RESOURCE_HISTORY, node, rsc_id);
 418 
 419     cib->cmds->set_user(cib, user_name);
 420     rc = cib->cmds->remove(cib, xpath, NULL, call_options|cib_xpath);
 421     cib->cmds->set_user(cib, NULL);
 422 
 423     if (rc < 0) {
 424         rc = pcmk_legacy2rc(rc);
 425         crm_err("Could not delete resource status of %s on %s%s%s: %s "
 426                 QB_XS " rc=%d", rsc_id, node,
 427                 (user_name? " for user " : ""), (user_name? user_name : ""),
 428                 pcmk_rc_str(rc), rc);
 429         free(desc);
 430         free(xpath);
 431         return rc;
 432     }
 433 
 434     if (pcmk_is_set(call_options, cib_sync_call)) {
 435         if (pcmk_is_set(call_options, cib_dryrun)) {
 436             crm_debug("Deletion of %s would succeed", desc);
 437         } else {
 438             crm_debug("Deletion of %s succeeded", desc);
 439         }
 440         free(desc);
 441 
 442     } else {
 443         crm_info("Clearing %s (via CIB call %d) " QB_XS " xpath=%s",
 444                  desc, rc, xpath);
 445         fsa_register_cib_callback(rc, desc, cib_delete_callback);
 446         // CIB library handles freeing desc
 447     }
 448 
 449     free(xpath);
 450     return pcmk_rc_ok;
 451 }
 452 
 453 /*!
 454  * \internal
 455  * \brief Build XML and string of parameters meeting some criteria, for digest
 456  *
 457  * \param[in]  op          Executor event with parameter table to use
 458  * \param[in]  metadata    Parsed meta-data for executed resource agent
 459  * \param[in]  param_type  Flag used for selection criteria
 460  * \param[out] result      Will be set to newly created XML with selected
 461  *                         parameters as attributes
 462  *
 463  * \return Newly allocated space-separated string of parameter names
 464  * \note Selection criteria varies by param_type: for the restart digest, we
 465  *       want parameters that are *not* marked reloadable (OCF 1.1) or that
 466  *       *are* marked unique (pre-1.1), for both string and XML results; for the
 467  *       secure digest, we want parameters that *are* marked private for the
 468  *       string, but parameters that are *not* marked private for the XML.
 469  * \note It is the caller's responsibility to free the string return value with
 470  *       \p g_string_free() and the XML result with \p pcmk__xml_free().
 471  */
 472 static GString *
 473 build_parameter_list(const lrmd_event_data_t *op,
     /* [previous][next][first][last][top][bottom][index][help] */
 474                      const struct ra_metadata_s *metadata,
 475                      enum ra_param_flags_e param_type, xmlNode **result)
 476 {
 477     GString *list = NULL;
 478 
 479     *result = pcmk__xe_create(NULL, PCMK_XE_PARAMETERS);
 480 
 481     /* Consider all parameters only except private ones to be consistent with
 482      * what scheduler does with calculate_secure_digest().
 483      */
 484     if (param_type == ra_param_private
 485         && compare_version(controld_globals.dc_version, "3.16.0") >= 0) {
 486         g_hash_table_foreach(op->params, hash2field, *result);
 487         pcmk__filter_op_for_digest(*result);
 488     }
 489 
 490     for (GList *iter = metadata->ra_params; iter != NULL; iter = iter->next) {
 491         struct ra_param_s *param = (struct ra_param_s *) iter->data;
 492 
 493         bool accept_for_list = false;
 494         bool accept_for_xml = false;
 495 
 496         switch (param_type) {
 497             case ra_param_reloadable:
 498                 accept_for_list = !pcmk_is_set(param->rap_flags, param_type);
 499                 accept_for_xml = accept_for_list;
 500                 break;
 501 
 502             case ra_param_unique:
 503                 accept_for_list = pcmk_is_set(param->rap_flags, param_type);
 504                 accept_for_xml = accept_for_list;
 505                 break;
 506 
 507             case ra_param_private:
 508                 accept_for_list = pcmk_is_set(param->rap_flags, param_type);
 509                 accept_for_xml = !accept_for_list;
 510                 break;
 511         }
 512 
 513         if (accept_for_list) {
 514             crm_trace("Attr %s is %s", param->rap_name, ra_param_flag2text(param_type));
 515 
 516             if (list == NULL) {
 517                 // We will later search for " WORD ", so start list with a space
 518                 pcmk__add_word(&list, 256, " ");
 519             }
 520             pcmk__add_word(&list, 0, param->rap_name);
 521 
 522         } else {
 523             crm_trace("Rejecting %s for %s", param->rap_name, ra_param_flag2text(param_type));
 524         }
 525 
 526         if (accept_for_xml) {
 527             const char *v = g_hash_table_lookup(op->params, param->rap_name);
 528 
 529             if (v != NULL) {
 530                 crm_trace("Adding attr %s=%s to the xml result", param->rap_name, v);
 531                 crm_xml_add(*result, param->rap_name, v);
 532             }
 533 
 534         } else {
 535             crm_trace("Removing attr %s from the xml result", param->rap_name);
 536             pcmk__xe_remove_attr(*result, param->rap_name);
 537         }
 538     }
 539 
 540     if (list != NULL) {
 541         // We will later search for " WORD ", so end list with a space
 542         pcmk__add_word(&list, 0, " ");
 543     }
 544     return list;
 545 }
 546 
 547 static void
 548 append_restart_list(lrmd_event_data_t *op, struct ra_metadata_s *metadata,
     /* [previous][next][first][last][top][bottom][index][help] */
 549                     xmlNode *update, const char *version)
 550 {
 551     GString *list = NULL;
 552     char *digest = NULL;
 553     xmlNode *restart = NULL;
 554 
 555     CRM_LOG_ASSERT(op->params != NULL);
 556 
 557     if (op->interval_ms > 0) {
 558         /* monitors are not reloadable */
 559         return;
 560     }
 561 
 562     if (pcmk_is_set(metadata->ra_flags, ra_supports_reload_agent)) {
 563         /* Add parameters not marked reloadable to the PCMK__XA_OP_FORCE_RESTART
 564          * list
 565          */
 566         list = build_parameter_list(op, metadata, ra_param_reloadable,
 567                                     &restart);
 568 
 569     } else if (pcmk_is_set(metadata->ra_flags, ra_supports_legacy_reload)) {
 570         /* @COMPAT pre-OCF-1.1 resource agents
 571          *
 572          * Before OCF 1.1, Pacemaker abused "unique=0" to indicate
 573          * reloadability. Add any parameters with unique="1" to the
 574          * PCMK__XA_OP_FORCE_RESTART list.
 575          */
 576         list = build_parameter_list(op, metadata, ra_param_unique, &restart);
 577 
 578     } else {
 579         // Resource does not support agent reloads
 580         return;
 581     }
 582 
 583     digest = pcmk__digest_operation(restart);
 584     /* Add PCMK__XA_OP_FORCE_RESTART and PCMK__XA_OP_RESTART_DIGEST to indicate
 585      * the resource supports reload, no matter if it actually supports any
 586      * reloadable parameters
 587      */
 588     crm_xml_add(update, PCMK__XA_OP_FORCE_RESTART,
 589                 (list == NULL)? "" : (const char *) list->str);
 590     crm_xml_add(update, PCMK__XA_OP_RESTART_DIGEST, digest);
 591 
 592     if ((list != NULL) && (list->len > 0)) {
 593         crm_trace("%s: %s, %s", op->rsc_id, digest, (const char *) list->str);
 594     } else {
 595         crm_trace("%s: %s", op->rsc_id, digest);
 596     }
 597 
 598     if (list != NULL) {
 599         g_string_free(list, TRUE);
 600     }
 601     pcmk__xml_free(restart);
 602     free(digest);
 603 }
 604 
 605 static void
 606 append_secure_list(lrmd_event_data_t *op, struct ra_metadata_s *metadata,
     /* [previous][next][first][last][top][bottom][index][help] */
 607                    xmlNode *update, const char *version)
 608 {
 609     GString *list = NULL;
 610     char *digest = NULL;
 611     xmlNode *secure = NULL;
 612 
 613     CRM_LOG_ASSERT(op->params != NULL);
 614 
 615     /* To keep PCMK__XA_OP_SECURE_PARAMS short, we want it to contain the secure
 616      * parameters but PCMK__XA_OP_SECURE_DIGEST to be based on the insecure ones
 617      */
 618     list = build_parameter_list(op, metadata, ra_param_private, &secure);
 619 
 620     if (list != NULL) {
 621         digest = pcmk__digest_operation(secure);
 622         crm_xml_add(update, PCMK__XA_OP_SECURE_PARAMS,
 623                     (const char *) list->str);
 624         crm_xml_add(update, PCMK__XA_OP_SECURE_DIGEST, digest);
 625 
 626         crm_trace("%s: %s, %s", op->rsc_id, digest, (const char *) list->str);
 627         g_string_free(list, TRUE);
 628     } else {
 629         crm_trace("%s: no secure parameters", op->rsc_id);
 630     }
 631 
 632     pcmk__xml_free(secure);
 633     free(digest);
 634 }
 635 
 636 /*!
 637  * \internal
 638  * \brief Create XML for a resource history entry
 639  *
 640  * \param[in]     func       Function name of caller
 641  * \param[in,out] parent     XML to add entry to
 642  * \param[in]     rsc        Affected resource
 643  * \param[in,out] op         Action to add an entry for (or NULL to do nothing)
 644  * \param[in]     node_name  Node where action occurred
 645  */
 646 void
 647 controld_add_resource_history_xml_as(const char *func, xmlNode *parent,
     /* [previous][next][first][last][top][bottom][index][help] */
 648                                      const lrmd_rsc_info_t *rsc,
 649                                      lrmd_event_data_t *op,
 650                                      const char *node_name)
 651 {
 652     int target_rc = 0;
 653     xmlNode *xml_op = NULL;
 654     struct ra_metadata_s *metadata = NULL;
 655     const char *caller_version = NULL;
 656     lrm_state_t *lrm_state = NULL;
 657 
 658     if (op == NULL) {
 659         return;
 660     }
 661 
 662     target_rc = rsc_op_expected_rc(op);
 663 
 664     caller_version = g_hash_table_lookup(op->params, PCMK_XA_CRM_FEATURE_SET);
 665     CRM_CHECK(caller_version != NULL, caller_version = CRM_FEATURE_SET);
 666 
 667     xml_op = pcmk__create_history_xml(parent, op, caller_version, target_rc,
 668                                       controld_globals.cluster->priv->node_name,
 669                                       func);
 670     if (xml_op == NULL) {
 671         return;
 672     }
 673 
 674     if ((rsc == NULL) || (op->params == NULL)
 675         || !crm_op_needs_metadata(rsc->standard, op->op_type)) {
 676 
 677         crm_trace("No digests needed for %s action on %s (params=%p rsc=%p)",
 678                   op->op_type, op->rsc_id, op->params, rsc);
 679         return;
 680     }
 681 
 682     lrm_state = controld_get_executor_state(node_name, false);
 683     if (lrm_state == NULL) {
 684         crm_warn("Cannot calculate digests for operation " PCMK__OP_FMT
 685                  " because we have no connection to executor for %s",
 686                  op->rsc_id, op->op_type, op->interval_ms, node_name);
 687         return;
 688     }
 689 
 690     /* Ideally the metadata is cached, and the agent is just a fallback.
 691      *
 692      * @TODO Go through all callers and ensure they get metadata asynchronously
 693      * first.
 694      */
 695     metadata = controld_get_rsc_metadata(lrm_state, rsc,
 696                                          controld_metadata_from_agent
 697                                          |controld_metadata_from_cache);
 698     if (metadata == NULL) {
 699         return;
 700     }
 701 
 702     crm_trace("Including additional digests for %s:%s:%s",
 703               rsc->standard, rsc->provider, rsc->type);
 704     append_restart_list(op, metadata, xml_op, caller_version);
 705     append_secure_list(op, metadata, xml_op, caller_version);
 706 
 707     return;
 708 }
 709 
 710 /*!
 711  * \internal
 712  * \brief Record an action as pending in the CIB, if appropriate
 713  *
 714  * \param[in]     node_name  Node where the action is pending
 715  * \param[in]     rsc        Resource that action is for
 716  * \param[in,out] op         Pending action
 717  *
 718  * \return true if action was recorded in CIB, otherwise false
 719  */
 720 bool
 721 controld_record_pending_op(const char *node_name, const lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 722                            lrmd_event_data_t *op)
 723 {
 724     const char *record_pending = NULL;
 725 
 726     CRM_CHECK((node_name != NULL) && (rsc != NULL) && (op != NULL),
 727               return false);
 728 
 729     // Never record certain operation types as pending
 730     if ((op->op_type == NULL) || (op->params == NULL)
 731         || !controld_action_is_recordable(op->op_type)) {
 732         return false;
 733     }
 734 
 735     // Check action's PCMK_META_RECORD_PENDING meta-attribute (defaults to true)
 736     record_pending = crm_meta_value(op->params, PCMK_META_RECORD_PENDING);
 737     if ((record_pending != NULL) && !crm_is_true(record_pending)) {
 738         pcmk__warn_once(pcmk__wo_record_pending,
 739                         "The " PCMK_META_RECORD_PENDING " option (for example, "
 740                         "for the %s resource's %s operation) is deprecated and "
 741                         "will be removed in a future release",
 742                         rsc->id, op->op_type);
 743         return false;
 744     }
 745 
 746     op->call_id = -1;
 747     op->t_run = time(NULL);
 748     op->t_rcchange = op->t_run;
 749 
 750     lrmd__set_result(op, PCMK_OCF_UNKNOWN, PCMK_EXEC_PENDING, NULL);
 751 
 752     crm_debug("Recording pending %s-interval %s for %s on %s in the CIB",
 753               pcmk__readable_interval(op->interval_ms), op->op_type, op->rsc_id,
 754               node_name);
 755     controld_update_resource_history(node_name, rsc, op, 0);
 756     return true;
 757 }
 758 
 759 static void
 760 cib_rsc_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 761 {
 762     switch (rc) {
 763         case pcmk_ok:
 764         case -pcmk_err_diff_failed:
 765         case -pcmk_err_diff_resync:
 766             crm_trace("Resource history update completed (call=%d rc=%d)",
 767                       call_id, rc);
 768             break;
 769         default:
 770             if (call_id > 0) {
 771                 crm_warn("Resource history update %d failed: %s "
 772                          QB_XS " rc=%d", call_id, pcmk_strerror(rc), rc);
 773             } else {
 774                 crm_warn("Resource history update failed: %s " QB_XS " rc=%d",
 775                          pcmk_strerror(rc), rc);
 776             }
 777     }
 778 
 779     if (call_id == pending_rsc_update) {
 780         pending_rsc_update = 0;
 781         controld_trigger_fsa();
 782     }
 783 }
 784 
 785 /* Only successful stops, and probes that found the resource inactive, get locks
 786  * recorded in the history. This ensures the resource stays locked to the node
 787  * until it is active there again after the node comes back up.
 788  */
 789 static bool
 790 should_preserve_lock(lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 791 {
 792     if (!pcmk_is_set(controld_globals.flags, controld_shutdown_lock_enabled)) {
 793         return false;
 794     }
 795     if (!strcmp(op->op_type, PCMK_ACTION_STOP) && (op->rc == PCMK_OCF_OK)) {
 796         return true;
 797     }
 798     if (!strcmp(op->op_type, PCMK_ACTION_MONITOR)
 799         && (op->rc == PCMK_OCF_NOT_RUNNING)) {
 800         return true;
 801     }
 802     return false;
 803 }
 804 
 805 /*!
 806  * \internal
 807  * \brief Request a CIB update
 808  *
 809  * \param[in]     section    Section of CIB to update
 810  * \param[in]     data       New XML of CIB section to update
 811  * \param[in]     options    CIB call options
 812  * \param[in]     callback   If not \c NULL, set this as the operation callback
 813  *
 814  * \return Standard Pacemaker return code
 815  *
 816  * \note If \p callback is \p cib_rsc_callback(), the CIB update's call ID is
 817  *       stored in \p pending_rsc_update on success.
 818  */
 819 int
 820 controld_update_cib(const char *section, xmlNode *data, int options,
     /* [previous][next][first][last][top][bottom][index][help] */
 821                     void (*callback)(xmlNode *, int, int, xmlNode *, void *))
 822 {
 823     cib_t *cib = controld_globals.cib_conn;
 824     int cib_rc = -ENOTCONN;
 825 
 826     pcmk__assert(data != NULL);
 827 
 828     if (cib != NULL) {
 829         cib_rc = cib->cmds->modify(cib, section, data, options);
 830         if (cib_rc >= 0) {
 831             crm_debug("Submitted CIB update %d for %s section",
 832                       cib_rc, section);
 833         }
 834     }
 835 
 836     if (callback == NULL) {
 837         if (cib_rc < 0) {
 838             crm_err("Failed to update CIB %s section: %s",
 839                     section, pcmk_rc_str(pcmk_legacy2rc(cib_rc)));
 840         }
 841 
 842     } else {
 843         if ((cib_rc >= 0) && (callback == cib_rsc_callback)) {
 844             /* Checking for a particular callback is a little hacky, but it
 845              * didn't seem worth adding an output argument for cib_rc for just
 846              * one use case.
 847              */
 848             pending_rsc_update = cib_rc;
 849         }
 850         fsa_register_cib_callback(cib_rc, NULL, callback);
 851     }
 852 
 853     return (cib_rc >= 0)? pcmk_rc_ok : pcmk_legacy2rc(cib_rc);
 854 }
 855 
 856 /*!
 857  * \internal
 858  * \brief Update resource history entry in CIB
 859  *
 860  * \param[in]     node_name  Node where action occurred
 861  * \param[in]     rsc        Resource that action is for
 862  * \param[in,out] op         Action to record
 863  * \param[in]     lock_time  If nonzero, when resource was locked to node
 864  *
 865  * \note On success, the CIB update's call ID will be stored in
 866  *       pending_rsc_update.
 867  */
 868 void
 869 controld_update_resource_history(const char *node_name,
     /* [previous][next][first][last][top][bottom][index][help] */
 870                                  const lrmd_rsc_info_t *rsc,
 871                                  lrmd_event_data_t *op, time_t lock_time)
 872 {
 873     xmlNode *update = NULL;
 874     xmlNode *xml = NULL;
 875     int call_opt = crmd_cib_smart_opt();
 876     const char *node_id = NULL;
 877     const char *container = NULL;
 878 
 879     CRM_CHECK((node_name != NULL) && (op != NULL), return);
 880 
 881     if (rsc == NULL) {
 882         crm_warn("Resource %s no longer exists in the executor", op->rsc_id);
 883         controld_ack_event_directly(NULL, NULL, rsc, op, op->rsc_id);
 884         return;
 885     }
 886 
 887     // <status>
 888     update = pcmk__xe_create(NULL, PCMK_XE_STATUS);
 889 
 890     //   <node_state ...>
 891     xml = pcmk__xe_create(update, PCMK__XE_NODE_STATE);
 892     if (controld_is_local_node(node_name)) {
 893         node_id = controld_globals.our_uuid;
 894     } else {
 895         node_id = node_name;
 896         pcmk__xe_set_bool_attr(xml, PCMK_XA_REMOTE_NODE, true);
 897     }
 898     crm_xml_add(xml, PCMK_XA_ID, node_id);
 899     crm_xml_add(xml, PCMK_XA_UNAME, node_name);
 900     crm_xml_add(xml, PCMK_XA_CRM_DEBUG_ORIGIN, __func__);
 901 
 902     //     <lrm ...>
 903     xml = pcmk__xe_create(xml, PCMK__XE_LRM);
 904     crm_xml_add(xml, PCMK_XA_ID, node_id);
 905 
 906     //       <lrm_resources>
 907     xml = pcmk__xe_create(xml, PCMK__XE_LRM_RESOURCES);
 908 
 909     //         <lrm_resource ...>
 910     xml = pcmk__xe_create(xml, PCMK__XE_LRM_RESOURCE);
 911     crm_xml_add(xml, PCMK_XA_ID, op->rsc_id);
 912     crm_xml_add(xml, PCMK_XA_CLASS, rsc->standard);
 913     crm_xml_add(xml, PCMK_XA_PROVIDER, rsc->provider);
 914     crm_xml_add(xml, PCMK_XA_TYPE, rsc->type);
 915     if (lock_time != 0) {
 916         /* Actions on a locked resource should either preserve the lock by
 917          * recording it with the action result, or clear it.
 918          */
 919         if (!should_preserve_lock(op)) {
 920             lock_time = 0;
 921         }
 922         crm_xml_add_ll(xml, PCMK_OPT_SHUTDOWN_LOCK, (long long) lock_time);
 923     }
 924     if (op->params != NULL) {
 925         container = g_hash_table_lookup(op->params,
 926                                         CRM_META "_" PCMK__META_CONTAINER);
 927         if (container != NULL) {
 928             crm_trace("Resource %s is a part of container resource %s",
 929                       op->rsc_id, container);
 930             crm_xml_add(xml, PCMK__META_CONTAINER, container);
 931         }
 932     }
 933 
 934     //           <lrm_resource_op ...> (possibly more than one)
 935     controld_add_resource_history_xml(xml, rsc, op, node_name);
 936 
 937     /* Update CIB asynchronously. Even if it fails, the resource state should be
 938      * discovered during the next election. Worst case, the node is wrongly
 939      * fenced for running a resource it isn't.
 940      */
 941     crm_log_xml_trace(update, __func__);
 942     controld_update_cib(PCMK_XE_STATUS, update, call_opt, cib_rsc_callback);
 943     pcmk__xml_free(update);
 944 }
 945 
 946 /*!
 947  * \internal
 948  * \brief Erase an LRM history entry from the CIB, given the operation data
 949  *
 950  * \param[in] op         Operation whose history should be deleted
 951  */
 952 void
 953 controld_delete_action_history(const lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 954 {
 955     xmlNode *xml_top = NULL;
 956 
 957     CRM_CHECK(op != NULL, return);
 958 
 959     xml_top = pcmk__xe_create(NULL, PCMK__XE_LRM_RSC_OP);
 960     crm_xml_add_int(xml_top, PCMK__XA_CALL_ID, op->call_id);
 961     crm_xml_add(xml_top, PCMK__XA_TRANSITION_KEY, op->user_data);
 962 
 963     if (op->interval_ms > 0) {
 964         char *op_id = pcmk__op_key(op->rsc_id, op->op_type, op->interval_ms);
 965 
 966         /* Avoid deleting last_failure too (if it was a result of this recurring op failing) */
 967         crm_xml_add(xml_top, PCMK_XA_ID, op_id);
 968         free(op_id);
 969     }
 970 
 971     crm_debug("Erasing resource operation history for " PCMK__OP_FMT " (call=%d)",
 972               op->rsc_id, op->op_type, op->interval_ms, op->call_id);
 973 
 974     controld_globals.cib_conn->cmds->remove(controld_globals.cib_conn,
 975                                             PCMK_XE_STATUS, xml_top, cib_none);
 976     crm_log_xml_trace(xml_top, "op:cancel");
 977     pcmk__xml_free(xml_top);
 978 }
 979 
 980 /* Define xpath to find LRM resource history entry by node and resource */
 981 #define XPATH_HISTORY                                   \
 982     "/" PCMK_XE_CIB "/" PCMK_XE_STATUS                  \
 983     "/" PCMK__XE_NODE_STATE "[@" PCMK_XA_UNAME "='%s']" \
 984     "/" PCMK__XE_LRM "/" PCMK__XE_LRM_RESOURCES         \
 985     "/" PCMK__XE_LRM_RESOURCE "[@" PCMK_XA_ID "='%s']"  \
 986     "/" PCMK__XE_LRM_RSC_OP
 987 
 988 /* ... and also by operation key */
 989 #define XPATH_HISTORY_ID XPATH_HISTORY "[@" PCMK_XA_ID "='%s']"
 990 
 991 /* ... and also by operation key and operation call ID */
 992 #define XPATH_HISTORY_CALL XPATH_HISTORY \
 993     "[@" PCMK_XA_ID "='%s' and @" PCMK__XA_CALL_ID "='%d']"
 994 
 995 /* ... and also by operation key and original operation key */
 996 #define XPATH_HISTORY_ORIG XPATH_HISTORY \
 997     "[@" PCMK_XA_ID "='%s' and @" PCMK__XA_OPERATION_KEY "='%s']"
 998 
 999 /*!
1000  * \internal
1001  * \brief Delete a last_failure resource history entry from the CIB
1002  *
1003  * \param[in] rsc_id       Name of resource to clear history for
1004  * \param[in] node         Name of node to clear history for
1005  * \param[in] action       If specified, delete only if this was failed action
1006  * \param[in] interval_ms  If \p action is specified, it has this interval
1007  */
1008 void
1009 controld_cib_delete_last_failure(const char *rsc_id, const char *node,
     /* [previous][next][first][last][top][bottom][index][help] */
1010                                  const char *action, guint interval_ms)
1011 {
1012     char *xpath = NULL;
1013     char *last_failure_key = NULL;
1014     CRM_CHECK((rsc_id != NULL) && (node != NULL), return);
1015 
1016     // Generate XPath to match desired entry
1017     last_failure_key = pcmk__op_key(rsc_id, "last_failure", 0);
1018     if (action == NULL) {
1019         xpath = crm_strdup_printf(XPATH_HISTORY_ID, node, rsc_id,
1020                                   last_failure_key);
1021     } else {
1022         char *action_key = pcmk__op_key(rsc_id, action, interval_ms);
1023 
1024         xpath = crm_strdup_printf(XPATH_HISTORY_ORIG, node, rsc_id,
1025                                   last_failure_key, action_key);
1026         free(action_key);
1027     }
1028     free(last_failure_key);
1029 
1030     controld_globals.cib_conn->cmds->remove(controld_globals.cib_conn, xpath,
1031                                             NULL, cib_xpath);
1032     free(xpath);
1033 }
1034 
1035 /*!
1036  * \internal
1037  * \brief Delete resource history entry from the CIB, given operation key
1038  *
1039  * \param[in] rsc_id     Name of resource to clear history for
1040  * \param[in] node       Name of node to clear history for
1041  * \param[in] key        Operation key of operation to clear history for
1042  * \param[in] call_id    If specified, delete entry only if it has this call ID
1043  */
1044 void
1045 controld_delete_action_history_by_key(const char *rsc_id, const char *node,
     /* [previous][next][first][last][top][bottom][index][help] */
1046                                       const char *key, int call_id)
1047 {
1048     char *xpath = NULL;
1049 
1050     CRM_CHECK((rsc_id != NULL) && (node != NULL) && (key != NULL), return);
1051 
1052     if (call_id > 0) {
1053         xpath = crm_strdup_printf(XPATH_HISTORY_CALL, node, rsc_id, key,
1054                                   call_id);
1055     } else {
1056         xpath = crm_strdup_printf(XPATH_HISTORY_ID, node, rsc_id, key);
1057     }
1058     controld_globals.cib_conn->cmds->remove(controld_globals.cib_conn, xpath,
1059                                             NULL, cib_xpath);
1060     free(xpath);
1061 }

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