root/daemons/controld/controld_execd.c

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

DEFINITIONS

This source file includes following definitions.
  1. lrm_connection_destroy
  2. make_stop_id
  3. copy_instance_keys
  4. copy_meta_keys
  5. history_remove_recurring_op
  6. history_free_recurring_ops
  7. history_free
  8. update_history_cache
  9. send_task_ok_ack
  10. op_node_name
  11. lrm_op_callback
  12. try_local_executor_connect
  13. do_lrm_control
  14. lrm_state_verify_stopped
  15. build_parameter_list
  16. append_restart_list
  17. append_secure_list
  18. build_operation_update
  19. is_rsc_active
  20. build_active_RAs
  21. controld_query_executor_state
  22. controld_rc2event
  23. controld_trigger_delete_refresh
  24. notify_deleted
  25. lrm_remove_deleted_rsc
  26. lrm_remove_deleted_op
  27. delete_rsc_entry
  28. erase_lrm_history_by_op
  29. erase_lrm_history_by_id
  30. last_failed_matches_op
  31. lrm_clear_last_failure
  32. cancel_op
  33. cancel_action_by_key
  34. cancel_op_key
  35. get_lrm_resource
  36. delete_resource
  37. get_fake_call_id
  38. fake_op_status
  39. force_reprobe
  40. synthesize_lrmd_failure
  41. lrm_op_target
  42. fail_lrm_resource
  43. handle_reprobe_op
  44. do_lrm_cancel
  45. do_lrm_delete
  46. new_metadata_cb_data
  47. free_metadata_cb_data
  48. metadata_complete
  49. do_lrm_invoke
  50. construct_op
  51. controld_ack_event_directly
  52. verify_stopped
  53. stop_recurring_action_by_rsc
  54. stop_recurring_actions
  55. record_pending_op
  56. do_lrm_rsc_op
  57. cib_rsc_callback
  58. should_preserve_lock
  59. do_update_resource
  60. do_lrm_event
  61. unescape_newlines
  62. did_lrm_rsc_op_fail
  63. log_executor_event
  64. process_lrm_event

   1 /*
   2  * Copyright 2004-2022 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 <regex.h>
  13 #include <sys/param.h>
  14 #include <sys/types.h>
  15 #include <sys/wait.h>
  16 
  17 #include <crm/crm.h>
  18 #include <crm/lrmd.h>           // lrmd_event_data_t, lrmd_rsc_info_t, etc.
  19 #include <crm/services.h>
  20 #include <crm/msg_xml.h>
  21 #include <crm/common/xml.h>
  22 #include <crm/pengine/rules.h>
  23 #include <crm/lrmd_internal.h>
  24 
  25 #include <pacemaker-internal.h>
  26 #include <pacemaker-controld.h>
  27 
  28 #define START_DELAY_THRESHOLD 5 * 60 * 1000
  29 #define MAX_LRM_REG_FAILS 30
  30 
  31 struct delete_event_s {
  32     int rc;
  33     const char *rsc;
  34     lrm_state_t *lrm_state;
  35 };
  36 
  37 extern pcmk__output_t *logger_out;
  38 
  39 static gboolean is_rsc_active(lrm_state_t * lrm_state, const char *rsc_id);
  40 static gboolean build_active_RAs(lrm_state_t * lrm_state, xmlNode * rsc_list);
  41 static gboolean stop_recurring_actions(gpointer key, gpointer value, gpointer user_data);
  42 
  43 static lrmd_event_data_t *construct_op(const lrm_state_t *lrm_state,
  44                                        const xmlNode *rsc_op,
  45                                        const char *rsc_id,
  46                                        const char *operation);
  47 static void do_lrm_rsc_op(lrm_state_t *lrm_state, lrmd_rsc_info_t *rsc,
  48                           xmlNode *msg, struct ra_metadata_s *md);
  49 
  50 static gboolean lrm_state_verify_stopped(lrm_state_t * lrm_state, enum crmd_fsa_state cur_state,
  51                                          int log_level);
  52 static int do_update_resource(const char *node_name, lrmd_rsc_info_t *rsc,
  53                               lrmd_event_data_t *op, time_t lock_time);
  54 
  55 static void
  56 lrm_connection_destroy(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58     if (pcmk_is_set(fsa_input_register, R_LRM_CONNECTED)) {
  59         crm_crit("Connection to executor failed");
  60         register_fsa_input(C_FSA_INTERNAL, I_ERROR, NULL);
  61         controld_clear_fsa_input_flags(R_LRM_CONNECTED);
  62 
  63     } else {
  64         crm_info("Disconnected from executor");
  65     }
  66 
  67 }
  68 
  69 static char *
  70 make_stop_id(const char *rsc, int call_id)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72     return crm_strdup_printf("%s:%d", rsc, call_id);
  73 }
  74 
  75 static void
  76 copy_instance_keys(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78     if (strstr(key, CRM_META "_") == NULL) {
  79         g_hash_table_replace(user_data, strdup((const char *)key), strdup((const char *)value));
  80     }
  81 }
  82 
  83 static void
  84 copy_meta_keys(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86     if (strstr(key, CRM_META "_") != NULL) {
  87         g_hash_table_replace(user_data, strdup((const char *)key), strdup((const char *)value));
  88     }
  89 }
  90 
  91 /*!
  92  * \internal
  93  * \brief Remove a recurring operation from a resource's history
  94  *
  95  * \param[in,out] history  Resource history to modify
  96  * \param[in]     op       Operation to remove
  97  *
  98  * \return TRUE if the operation was found and removed, FALSE otherwise
  99  */
 100 static gboolean
 101 history_remove_recurring_op(rsc_history_t *history, const lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103     GList *iter;
 104 
 105     for (iter = history->recurring_op_list; iter != NULL; iter = iter->next) {
 106         lrmd_event_data_t *existing = iter->data;
 107 
 108         if ((op->interval_ms == existing->interval_ms)
 109             && pcmk__str_eq(op->rsc_id, existing->rsc_id, pcmk__str_none)
 110             && pcmk__str_eq(op->op_type, existing->op_type, pcmk__str_casei)) {
 111 
 112             history->recurring_op_list = g_list_delete_link(history->recurring_op_list, iter);
 113             lrmd_free_event(existing);
 114             return TRUE;
 115         }
 116     }
 117     return FALSE;
 118 }
 119 
 120 /*!
 121  * \internal
 122  * \brief Free all recurring operations in resource history
 123  *
 124  * \param[in,out] history  Resource history to modify
 125  */
 126 static void
 127 history_free_recurring_ops(rsc_history_t *history)
     /* [previous][next][first][last][top][bottom][index][help] */
 128 {
 129     GList *iter;
 130 
 131     for (iter = history->recurring_op_list; iter != NULL; iter = iter->next) {
 132         lrmd_free_event(iter->data);
 133     }
 134     g_list_free(history->recurring_op_list);
 135     history->recurring_op_list = NULL;
 136 }
 137 
 138 /*!
 139  * \internal
 140  * \brief Free resource history
 141  *
 142  * \param[in,out] history  Resource history to free
 143  */
 144 void
 145 history_free(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147     rsc_history_t *history = (rsc_history_t*)data;
 148 
 149     if (history->stop_params) {
 150         g_hash_table_destroy(history->stop_params);
 151     }
 152 
 153     /* Don't need to free history->rsc.id because it's set to history->id */
 154     free(history->rsc.type);
 155     free(history->rsc.standard);
 156     free(history->rsc.provider);
 157 
 158     lrmd_free_event(history->failed);
 159     lrmd_free_event(history->last);
 160     free(history->id);
 161     history_free_recurring_ops(history);
 162     free(history);
 163 }
 164 
 165 static void
 166 update_history_cache(lrm_state_t * lrm_state, lrmd_rsc_info_t * rsc, lrmd_event_data_t * op)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168     int target_rc = 0;
 169     rsc_history_t *entry = NULL;
 170 
 171     if (op->rsc_deleted) {
 172         crm_debug("Purged history for '%s' after %s", op->rsc_id, op->op_type);
 173         controld_delete_resource_history(op->rsc_id, lrm_state->node_name,
 174                                          NULL, crmd_cib_smart_opt());
 175         return;
 176     }
 177 
 178     if (pcmk__str_eq(op->op_type, RSC_NOTIFY, pcmk__str_casei)) {
 179         return;
 180     }
 181 
 182     crm_debug("Updating history for '%s' with %s op", op->rsc_id, op->op_type);
 183 
 184     entry = g_hash_table_lookup(lrm_state->resource_history, op->rsc_id);
 185     if (entry == NULL && rsc) {
 186         entry = calloc(1, sizeof(rsc_history_t));
 187         entry->id = strdup(op->rsc_id);
 188         g_hash_table_insert(lrm_state->resource_history, entry->id, entry);
 189 
 190         entry->rsc.id = entry->id;
 191         entry->rsc.type = strdup(rsc->type);
 192         entry->rsc.standard = strdup(rsc->standard);
 193         pcmk__str_update(&entry->rsc.provider, rsc->provider);
 194 
 195     } else if (entry == NULL) {
 196         crm_info("Resource %s no longer exists, not updating cache", op->rsc_id);
 197         return;
 198     }
 199 
 200     entry->last_callid = op->call_id;
 201     target_rc = rsc_op_expected_rc(op);
 202     if (op->op_status == PCMK_EXEC_CANCELLED) {
 203         if (op->interval_ms > 0) {
 204             crm_trace("Removing cancelled recurring op: " PCMK__OP_FMT,
 205                       op->rsc_id, op->op_type, op->interval_ms);
 206             history_remove_recurring_op(entry, op);
 207             return;
 208         } else {
 209             crm_trace("Skipping " PCMK__OP_FMT " rc=%d, status=%d",
 210                       op->rsc_id, op->op_type, op->interval_ms, op->rc,
 211                       op->op_status);
 212         }
 213 
 214     } else if (did_rsc_op_fail(op, target_rc)) {
 215         /* Store failed monitors here, otherwise the block below will cause them
 216          * to be forgotten when a stop happens.
 217          */
 218         if (entry->failed) {
 219             lrmd_free_event(entry->failed);
 220         }
 221         entry->failed = lrmd_copy_event(op);
 222 
 223     } else if (op->interval_ms == 0) {
 224         if (entry->last) {
 225             lrmd_free_event(entry->last);
 226         }
 227         entry->last = lrmd_copy_event(op);
 228 
 229         if (op->params && pcmk__strcase_any_of(op->op_type, CRMD_ACTION_START,
 230                                                CRMD_ACTION_RELOAD,
 231                                                CRMD_ACTION_RELOAD_AGENT,
 232                                                CRMD_ACTION_STATUS, NULL)) {
 233             if (entry->stop_params) {
 234                 g_hash_table_destroy(entry->stop_params);
 235             }
 236             entry->stop_params = pcmk__strkey_table(free, free);
 237 
 238             g_hash_table_foreach(op->params, copy_instance_keys, entry->stop_params);
 239         }
 240     }
 241 
 242     if (op->interval_ms > 0) {
 243         /* Ensure there are no duplicates */
 244         history_remove_recurring_op(entry, op);
 245 
 246         crm_trace("Adding recurring op: " PCMK__OP_FMT,
 247                   op->rsc_id, op->op_type, op->interval_ms);
 248         entry->recurring_op_list = g_list_prepend(entry->recurring_op_list, lrmd_copy_event(op));
 249 
 250     } else if (entry->recurring_op_list && !pcmk__str_eq(op->op_type, RSC_STATUS, pcmk__str_casei)) {
 251         crm_trace("Dropping %d recurring ops because of: " PCMK__OP_FMT,
 252                   g_list_length(entry->recurring_op_list), op->rsc_id,
 253                   op->op_type, op->interval_ms);
 254         history_free_recurring_ops(entry);
 255     }
 256 }
 257 
 258 /*!
 259  * \internal
 260  * \brief Send a direct OK ack for a resource task
 261  *
 262  * \param[in] lrm_state  LRM connection
 263  * \param[in] input      Input message being ack'ed
 264  * \param[in] rsc_id     ID of affected resource
 265  * \param[in] rsc        Affected resource (if available)
 266  * \param[in] task       Operation task being ack'ed
 267  * \param[in] ack_host   Name of host to send ack to
 268  * \param[in] ack_sys    IPC system name to ack
 269  */
 270 static void
 271 send_task_ok_ack(const lrm_state_t *lrm_state, const ha_msg_input_t *input,
     /* [previous][next][first][last][top][bottom][index][help] */
 272                  const char *rsc_id, const lrmd_rsc_info_t *rsc,
 273                  const char *task, const char *ack_host, const char *ack_sys)
 274 {
 275     lrmd_event_data_t *op = construct_op(lrm_state, input->xml, rsc_id, task);
 276 
 277     lrmd__set_result(op, PCMK_OCF_OK, PCMK_EXEC_DONE, NULL);
 278     controld_ack_event_directly(ack_host, ack_sys, rsc, op, rsc_id);
 279     lrmd_free_event(op);
 280 }
 281 
 282 static inline const char *
 283 op_node_name(lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 284 {
 285     return op->remote_nodename? op->remote_nodename : fsa_our_uname;
 286 }
 287 
 288 void
 289 lrm_op_callback(lrmd_event_data_t * op)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291     CRM_CHECK(op != NULL, return);
 292     switch (op->type) {
 293         case lrmd_event_disconnect:
 294             if (op->remote_nodename == NULL) {
 295                 /* If this is the local executor IPC connection, set the right
 296                  * bits in the controller when the connection goes down.
 297                  */
 298                 lrm_connection_destroy();
 299             }
 300             break;
 301 
 302         case lrmd_event_exec_complete:
 303             {
 304                 lrm_state_t *lrm_state = lrm_state_find(op_node_name(op));
 305 
 306                 CRM_ASSERT(lrm_state != NULL);
 307                 process_lrm_event(lrm_state, op, NULL, NULL);
 308             }
 309             break;
 310 
 311         default:
 312             break;
 313     }
 314 }
 315 
 316 static void
 317 try_local_executor_connect(long long action, fsa_data_t *msg_data,
     /* [previous][next][first][last][top][bottom][index][help] */
 318                            lrm_state_t *lrm_state)
 319 {
 320     int rc = pcmk_rc_ok;
 321 
 322     crm_debug("Connecting to the local executor");
 323 
 324     // If we can connect, great
 325     rc = controld_connect_local_executor(lrm_state);
 326     if (rc == pcmk_rc_ok) {
 327         controld_set_fsa_input_flags(R_LRM_CONNECTED);
 328         crm_info("Connection to the local executor established");
 329         return;
 330     }
 331 
 332     // Otherwise, if we can try again, set a timer to do so
 333     if (lrm_state->num_lrm_register_fails < MAX_LRM_REG_FAILS) {
 334         crm_warn("Failed to connect to the local executor %d time%s "
 335                  "(%d max): %s", lrm_state->num_lrm_register_fails,
 336                  pcmk__plural_s(lrm_state->num_lrm_register_fails),
 337                  MAX_LRM_REG_FAILS, pcmk_rc_str(rc));
 338         controld_start_timer(wait_timer);
 339         crmd_fsa_stall(FALSE);
 340         return;
 341     }
 342 
 343     // Otherwise give up
 344     crm_err("Failed to connect to the executor the max allowed "
 345             "%d time%s: %s", lrm_state->num_lrm_register_fails,
 346             pcmk__plural_s(lrm_state->num_lrm_register_fails),
 347             pcmk_rc_str(rc));
 348     register_fsa_error(C_FSA_INTERNAL, I_ERROR, NULL);
 349 }
 350 
 351 /*       A_LRM_CONNECT  */
 352 void
 353 do_lrm_control(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
 354                enum crmd_fsa_cause cause,
 355                enum crmd_fsa_state cur_state,
 356                enum crmd_fsa_input current_input, fsa_data_t * msg_data)
 357 {
 358     /* This only pertains to local executor connections. Remote connections are
 359      * handled as resources within the scheduler. Connecting and disconnecting
 360      * from remote executor instances is handled differently.
 361      */
 362 
 363     lrm_state_t *lrm_state = NULL;
 364 
 365     if(fsa_our_uname == NULL) {
 366         return; /* Nothing to do */
 367     }
 368     lrm_state = lrm_state_find_or_create(fsa_our_uname);
 369     if (lrm_state == NULL) {
 370         register_fsa_error(C_FSA_INTERNAL, I_ERROR, NULL);
 371         return;
 372     }
 373 
 374     if (action & A_LRM_DISCONNECT) {
 375         if (lrm_state_verify_stopped(lrm_state, cur_state, LOG_INFO) == FALSE) {
 376             if (action == A_LRM_DISCONNECT) {
 377                 crmd_fsa_stall(FALSE);
 378                 return;
 379             }
 380         }
 381 
 382         controld_clear_fsa_input_flags(R_LRM_CONNECTED);
 383         crm_info("Disconnecting from the executor");
 384         lrm_state_disconnect(lrm_state);
 385         lrm_state_reset_tables(lrm_state, FALSE);
 386         crm_notice("Disconnected from the executor");
 387     }
 388 
 389     if (action & A_LRM_CONNECT) {
 390         try_local_executor_connect(action, msg_data, lrm_state);
 391     }
 392 
 393     if (action & ~(A_LRM_CONNECT | A_LRM_DISCONNECT)) {
 394         crm_err("Unexpected action %s in %s", fsa_action2string(action),
 395                 __func__);
 396     }
 397 }
 398 
 399 static gboolean
 400 lrm_state_verify_stopped(lrm_state_t * lrm_state, enum crmd_fsa_state cur_state, int log_level)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402     int counter = 0;
 403     gboolean rc = TRUE;
 404     const char *when = "lrm disconnect";
 405 
 406     GHashTableIter gIter;
 407     const char *key = NULL;
 408     rsc_history_t *entry = NULL;
 409     active_op_t *pending = NULL;
 410 
 411     crm_debug("Checking for active resources before exit");
 412 
 413     if (cur_state == S_TERMINATE) {
 414         log_level = LOG_ERR;
 415         when = "shutdown";
 416 
 417     } else if (pcmk_is_set(fsa_input_register, R_SHUTDOWN)) {
 418         when = "shutdown... waiting";
 419     }
 420 
 421     if (lrm_state->pending_ops && lrm_state_is_connected(lrm_state) == TRUE) {
 422         guint removed = g_hash_table_foreach_remove(
 423             lrm_state->pending_ops, stop_recurring_actions, lrm_state);
 424         guint nremaining = g_hash_table_size(lrm_state->pending_ops);
 425 
 426         if (removed || nremaining) {
 427             crm_notice("Stopped %u recurring operation%s at %s (%u remaining)",
 428                        removed, pcmk__plural_s(removed), when, nremaining);
 429         }
 430     }
 431 
 432     if (lrm_state->pending_ops) {
 433         g_hash_table_iter_init(&gIter, lrm_state->pending_ops);
 434         while (g_hash_table_iter_next(&gIter, NULL, (void **)&pending)) {
 435             /* Ignore recurring actions in the shutdown calculations */
 436             if (pending->interval_ms == 0) {
 437                 counter++;
 438             }
 439         }
 440     }
 441 
 442     if (counter > 0) {
 443         do_crm_log(log_level, "%d pending executor operation%s at %s",
 444                    counter, pcmk__plural_s(counter), when);
 445 
 446         if ((cur_state == S_TERMINATE)
 447             || !pcmk_is_set(fsa_input_register, R_SENT_RSC_STOP)) {
 448             g_hash_table_iter_init(&gIter, lrm_state->pending_ops);
 449             while (g_hash_table_iter_next(&gIter, (gpointer*)&key, (gpointer*)&pending)) {
 450                 do_crm_log(log_level, "Pending action: %s (%s)", key, pending->op_key);
 451             }
 452 
 453         } else {
 454             rc = FALSE;
 455         }
 456         return rc;
 457     }
 458 
 459     if (lrm_state->resource_history == NULL) {
 460         return rc;
 461     }
 462 
 463     if (pcmk_is_set(fsa_input_register, R_SHUTDOWN)) {
 464         /* At this point we're not waiting, we're just shutting down */
 465         when = "shutdown";
 466     }
 467 
 468     counter = 0;
 469     g_hash_table_iter_init(&gIter, lrm_state->resource_history);
 470     while (g_hash_table_iter_next(&gIter, NULL, (gpointer*)&entry)) {
 471         if (is_rsc_active(lrm_state, entry->id) == FALSE) {
 472             continue;
 473         }
 474 
 475         counter++;
 476         if (log_level == LOG_ERR) {
 477             crm_info("Found %s active at %s", entry->id, when);
 478         } else {
 479             crm_trace("Found %s active at %s", entry->id, when);
 480         }
 481         if (lrm_state->pending_ops) {
 482             GHashTableIter hIter;
 483 
 484             g_hash_table_iter_init(&hIter, lrm_state->pending_ops);
 485             while (g_hash_table_iter_next(&hIter, (gpointer*)&key, (gpointer*)&pending)) {
 486                 if (pcmk__str_eq(entry->id, pending->rsc_id, pcmk__str_none)) {
 487                     crm_notice("%sction %s (%s) incomplete at %s",
 488                                pending->interval_ms == 0 ? "A" : "Recurring a",
 489                                key, pending->op_key, when);
 490                 }
 491             }
 492         }
 493     }
 494 
 495     if (counter) {
 496         crm_err("%d resource%s active at %s",
 497                 counter, (counter == 1)? " was" : "s were", when);
 498     }
 499 
 500     return rc;
 501 }
 502 
 503 /*!
 504  * \internal
 505  * \brief Build XML and string of parameters meeting some criteria, for digest
 506  *
 507  * \param[in]  op          Executor event with parameter table to use
 508  * \param[in]  metadata    Parsed meta-data for executed resource agent
 509  * \param[in]  param_type  Flag used for selection criteria
 510  * \param[out] result      Will be set to newly created XML with selected
 511  *                         parameters as attributes
 512  *
 513  * \return Newly allocated space-separated string of parameter names
 514  * \note Selection criteria varies by param_type: for the restart digest, we
 515  *       want parameters that are *not* marked reloadable (OCF 1.1) or that
 516  *       *are* marked unique (pre-1.1), for both string and XML results; for the
 517  *       secure digest, we want parameters that *are* marked private for the
 518  *       string, but parameters that are *not* marked private for the XML.
 519  * \note It is the caller's responsibility to free the string return value with
 520  *       \p g_string_free() and the XML result with \p free_xml().
 521  */
 522 static GString *
 523 build_parameter_list(const lrmd_event_data_t *op,
     /* [previous][next][first][last][top][bottom][index][help] */
 524                      const struct ra_metadata_s *metadata,
 525                      enum ra_param_flags_e param_type, xmlNode **result)
 526 {
 527     GString *list = NULL;
 528 
 529     *result = create_xml_node(NULL, XML_TAG_PARAMS);
 530 
 531     /* Consider all parameters only except private ones to be consistent with
 532      * what scheduler does with calculate_secure_digest().
 533      */
 534     if (param_type == ra_param_private
 535         && compare_version(fsa_our_dc_version, "3.16.0") >= 0) {
 536         g_hash_table_foreach(op->params, hash2field, *result);
 537         pcmk__filter_op_for_digest(*result);
 538     }
 539 
 540     for (GList *iter = metadata->ra_params; iter != NULL; iter = iter->next) {
 541         struct ra_param_s *param = (struct ra_param_s *) iter->data;
 542 
 543         bool accept_for_list = false;
 544         bool accept_for_xml = false;
 545 
 546         switch (param_type) {
 547             case ra_param_reloadable:
 548                 accept_for_list = !pcmk_is_set(param->rap_flags, param_type);
 549                 accept_for_xml = accept_for_list;
 550                 break;
 551 
 552             case ra_param_unique:
 553                 accept_for_list = pcmk_is_set(param->rap_flags, param_type);
 554                 accept_for_xml = accept_for_list;
 555                 break;
 556 
 557             case ra_param_private:
 558                 accept_for_list = pcmk_is_set(param->rap_flags, param_type);
 559                 accept_for_xml = !accept_for_list;
 560                 break;
 561         }
 562 
 563         if (accept_for_list) {
 564             crm_trace("Attr %s is %s", param->rap_name, ra_param_flag2text(param_type));
 565 
 566             if (list == NULL) {
 567                 // We will later search for " WORD ", so start list with a space
 568                 pcmk__add_word(&list, 256, " ");
 569             }
 570             pcmk__add_word(&list, 0, param->rap_name);
 571 
 572         } else {
 573             crm_trace("Rejecting %s for %s", param->rap_name, ra_param_flag2text(param_type));
 574         }
 575 
 576         if (accept_for_xml) {
 577             const char *v = g_hash_table_lookup(op->params, param->rap_name);
 578 
 579             if (v != NULL) {
 580                 crm_trace("Adding attr %s=%s to the xml result", param->rap_name, v);
 581                 crm_xml_add(*result, param->rap_name, v);
 582             }
 583 
 584         } else {
 585             crm_trace("Removing attr %s from the xml result", param->rap_name);
 586             xml_remove_prop(*result, param->rap_name);
 587         }
 588     }
 589 
 590     if (list != NULL) {
 591         // We will later search for " WORD ", so end list with a space
 592         pcmk__add_word(&list, 0, " ");
 593     }
 594     return list;
 595 }
 596 
 597 static void
 598 append_restart_list(lrmd_event_data_t *op, struct ra_metadata_s *metadata,
     /* [previous][next][first][last][top][bottom][index][help] */
 599                     xmlNode *update, const char *version)
 600 {
 601     GString *list = NULL;
 602     char *digest = NULL;
 603     xmlNode *restart = NULL;
 604 
 605     CRM_LOG_ASSERT(op->params != NULL);
 606 
 607     if (op->interval_ms > 0) {
 608         /* monitors are not reloadable */
 609         return;
 610     }
 611 
 612     if (pcmk_is_set(metadata->ra_flags, ra_supports_reload_agent)) {
 613         // Add parameters not marked reloadable to the "op-force-restart" list
 614         list = build_parameter_list(op, metadata, ra_param_reloadable,
 615                                     &restart);
 616 
 617     } else if (pcmk_is_set(metadata->ra_flags, ra_supports_legacy_reload)) {
 618         /* @COMPAT pre-OCF-1.1 resource agents
 619          *
 620          * Before OCF 1.1, Pacemaker abused "unique=0" to indicate
 621          * reloadability. Add any parameters with unique="1" to the
 622          * "op-force-restart" list.
 623          */
 624         list = build_parameter_list(op, metadata, ra_param_unique, &restart);
 625 
 626     } else {
 627         // Resource does not support agent reloads
 628         return;
 629     }
 630 
 631     digest = calculate_operation_digest(restart, version);
 632     /* Add "op-force-restart" and "op-restart-digest" to indicate the resource supports reload,
 633      * no matter if it actually supports any parameters with unique="1"). */
 634     crm_xml_add(update, XML_LRM_ATTR_OP_RESTART,
 635                 (list == NULL)? "" : (const char *) list->str);
 636     crm_xml_add(update, XML_LRM_ATTR_RESTART_DIGEST, digest);
 637 
 638     if ((list != NULL) && (list->len > 0)) {
 639         crm_trace("%s: %s, %s", op->rsc_id, digest, (const char *) list->str);
 640     } else {
 641         crm_trace("%s: %s", op->rsc_id, digest);
 642     }
 643 
 644     if (list != NULL) {
 645         g_string_free(list, TRUE);
 646     }
 647     free_xml(restart);
 648     free(digest);
 649 }
 650 
 651 static void
 652 append_secure_list(lrmd_event_data_t *op, struct ra_metadata_s *metadata,
     /* [previous][next][first][last][top][bottom][index][help] */
 653                    xmlNode *update, const char *version)
 654 {
 655     GString *list = NULL;
 656     char *digest = NULL;
 657     xmlNode *secure = NULL;
 658 
 659     CRM_LOG_ASSERT(op->params != NULL);
 660 
 661     /*
 662      * To keep XML_LRM_ATTR_OP_SECURE short, we want it to contain the
 663      * secure parameters but XML_LRM_ATTR_SECURE_DIGEST to be based on
 664      * the insecure ones
 665      */
 666     list = build_parameter_list(op, metadata, ra_param_private, &secure);
 667 
 668     if (list != NULL) {
 669         digest = calculate_operation_digest(secure, version);
 670         crm_xml_add(update, XML_LRM_ATTR_OP_SECURE, (const char *) list->str);
 671         crm_xml_add(update, XML_LRM_ATTR_SECURE_DIGEST, digest);
 672 
 673         crm_trace("%s: %s, %s", op->rsc_id, digest, (const char *) list->str);
 674         g_string_free(list, TRUE);
 675     } else {
 676         crm_trace("%s: no secure parameters", op->rsc_id);
 677     }
 678 
 679     free_xml(secure);
 680     free(digest);
 681 }
 682 
 683 static gboolean
 684 build_operation_update(xmlNode * parent, const lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
 685                        lrmd_event_data_t *op, const char *node_name,
 686                        const char *src)
 687 {
 688     int target_rc = 0;
 689     xmlNode *xml_op = NULL;
 690     struct ra_metadata_s *metadata = NULL;
 691     const char *caller_version = NULL;
 692     lrm_state_t *lrm_state = NULL;
 693 
 694     if (op == NULL) {
 695         return FALSE;
 696     }
 697 
 698     target_rc = rsc_op_expected_rc(op);
 699 
 700     caller_version = g_hash_table_lookup(op->params, XML_ATTR_CRM_VERSION);
 701     CRM_CHECK(caller_version != NULL, caller_version = CRM_FEATURE_SET);
 702 
 703     xml_op = pcmk__create_history_xml(parent, op, caller_version, target_rc,
 704                                       fsa_our_uname, src);
 705     if (xml_op == NULL) {
 706         return TRUE;
 707     }
 708 
 709     if ((rsc == NULL) || (op->params == NULL)
 710         || !crm_op_needs_metadata(rsc->standard, op->op_type)) {
 711 
 712         crm_trace("No digests needed for %s action on %s (params=%p rsc=%p)",
 713                   op->op_type, op->rsc_id, op->params, rsc);
 714         return TRUE;
 715     }
 716 
 717     lrm_state = lrm_state_find(node_name);
 718     if (lrm_state == NULL) {
 719         crm_warn("Cannot calculate digests for operation " PCMK__OP_FMT
 720                  " because we have no connection to executor for %s",
 721                  op->rsc_id, op->op_type, op->interval_ms, node_name);
 722         return TRUE;
 723     }
 724 
 725     /* Ideally the metadata is cached, and the agent is just a fallback.
 726      *
 727      * @TODO Go through all callers and ensure they get metadata asynchronously
 728      * first.
 729      */
 730     metadata = controld_get_rsc_metadata(lrm_state, rsc,
 731                                          controld_metadata_from_agent
 732                                          |controld_metadata_from_cache);
 733     if (metadata == NULL) {
 734         return TRUE;
 735     }
 736 
 737     crm_trace("Including additional digests for %s:%s:%s",
 738               rsc->standard, rsc->provider, rsc->type);
 739     append_restart_list(op, metadata, xml_op, caller_version);
 740     append_secure_list(op, metadata, xml_op, caller_version);
 741 
 742     return TRUE;
 743 }
 744 
 745 static gboolean
 746 is_rsc_active(lrm_state_t * lrm_state, const char *rsc_id)
     /* [previous][next][first][last][top][bottom][index][help] */
 747 {
 748     rsc_history_t *entry = NULL;
 749 
 750     entry = g_hash_table_lookup(lrm_state->resource_history, rsc_id);
 751     if (entry == NULL || entry->last == NULL) {
 752         return FALSE;
 753     }
 754 
 755     crm_trace("Processing %s: %s.%d=%d", rsc_id, entry->last->op_type,
 756               entry->last->interval_ms, entry->last->rc);
 757     if (entry->last->rc == PCMK_OCF_OK && pcmk__str_eq(entry->last->op_type, CRMD_ACTION_STOP, pcmk__str_casei)) {
 758         return FALSE;
 759 
 760     } else if (entry->last->rc == PCMK_OCF_OK
 761                && pcmk__str_eq(entry->last->op_type, CRMD_ACTION_MIGRATE, pcmk__str_casei)) {
 762         // A stricter check is too complex ... leave that to the scheduler
 763         return FALSE;
 764 
 765     } else if (entry->last->rc == PCMK_OCF_NOT_RUNNING) {
 766         return FALSE;
 767 
 768     } else if ((entry->last->interval_ms == 0)
 769                && (entry->last->rc == PCMK_OCF_NOT_CONFIGURED)) {
 770         /* Badly configured resources can't be reliably stopped */
 771         return FALSE;
 772     }
 773 
 774     return TRUE;
 775 }
 776 
 777 static gboolean
 778 build_active_RAs(lrm_state_t * lrm_state, xmlNode * rsc_list)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780     GHashTableIter iter;
 781     rsc_history_t *entry = NULL;
 782 
 783     g_hash_table_iter_init(&iter, lrm_state->resource_history);
 784     while (g_hash_table_iter_next(&iter, NULL, (void **)&entry)) {
 785 
 786         GList *gIter = NULL;
 787         xmlNode *xml_rsc = create_xml_node(rsc_list, XML_LRM_TAG_RESOURCE);
 788 
 789         crm_xml_add(xml_rsc, XML_ATTR_ID, entry->id);
 790         crm_xml_add(xml_rsc, XML_ATTR_TYPE, entry->rsc.type);
 791         crm_xml_add(xml_rsc, XML_AGENT_ATTR_CLASS, entry->rsc.standard);
 792         crm_xml_add(xml_rsc, XML_AGENT_ATTR_PROVIDER, entry->rsc.provider);
 793 
 794         if (entry->last && entry->last->params) {
 795             const char *container = g_hash_table_lookup(entry->last->params, CRM_META"_"XML_RSC_ATTR_CONTAINER);
 796             if (container) {
 797                 crm_trace("Resource %s is a part of container resource %s", entry->id, container);
 798                 crm_xml_add(xml_rsc, XML_RSC_ATTR_CONTAINER, container);
 799             }
 800         }
 801         build_operation_update(xml_rsc, &(entry->rsc), entry->failed, lrm_state->node_name,
 802                                __func__);
 803         build_operation_update(xml_rsc, &(entry->rsc), entry->last, lrm_state->node_name,
 804                                __func__);
 805         for (gIter = entry->recurring_op_list; gIter != NULL; gIter = gIter->next) {
 806             build_operation_update(xml_rsc, &(entry->rsc), gIter->data, lrm_state->node_name,
 807                                    __func__);
 808         }
 809     }
 810 
 811     return FALSE;
 812 }
 813 
 814 xmlNode *
 815 controld_query_executor_state(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 816 {
 817     xmlNode *xml_state = NULL;
 818     xmlNode *xml_data = NULL;
 819     xmlNode *rsc_list = NULL;
 820     crm_node_t *peer = NULL;
 821     lrm_state_t *lrm_state = lrm_state_find(fsa_our_uname);
 822 
 823     if (!lrm_state) {
 824         crm_err("Could not find executor state for node %s", fsa_our_uname);
 825         return NULL;
 826     }
 827 
 828     peer = crm_get_peer_full(0, lrm_state->node_name, CRM_GET_PEER_ANY);
 829     CRM_CHECK(peer != NULL, return NULL);
 830 
 831     xml_state = create_node_state_update(peer,
 832                                          node_update_cluster|node_update_peer,
 833                                          NULL, __func__);
 834     if (xml_state == NULL) {
 835         return NULL;
 836     }
 837 
 838     xml_data = create_xml_node(xml_state, XML_CIB_TAG_LRM);
 839     crm_xml_add(xml_data, XML_ATTR_ID, peer->uuid);
 840     rsc_list = create_xml_node(xml_data, XML_LRM_TAG_RESOURCES);
 841 
 842     /* Build a list of active (not always running) resources */
 843     build_active_RAs(lrm_state, rsc_list);
 844 
 845     crm_log_xml_trace(xml_state, "Current executor state");
 846 
 847     return xml_state;
 848 }
 849 
 850 /*!
 851  * \internal
 852  * \brief Map standard Pacemaker return code to operation status and OCF code
 853  *
 854  * \param[out] event  Executor event whose status and return code should be set
 855  * \param[in]  rc     Standard Pacemaker return code
 856  */
 857 void
 858 controld_rc2event(lrmd_event_data_t *event, int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
 859 {
 860     /* This is called for cleanup requests from controller peers/clients, not
 861      * for resource actions, so no exit reason is needed.
 862      */
 863     switch (rc) {
 864         case pcmk_rc_ok:
 865             lrmd__set_result(event, PCMK_OCF_OK, PCMK_EXEC_DONE, NULL);
 866             break;
 867         case EACCES:
 868             lrmd__set_result(event, PCMK_OCF_INSUFFICIENT_PRIV,
 869                              PCMK_EXEC_ERROR, NULL);
 870             break;
 871         default:
 872             lrmd__set_result(event, PCMK_OCF_UNKNOWN_ERROR, PCMK_EXEC_ERROR,
 873                              NULL);
 874             break;
 875     }
 876 }
 877 
 878 /*!
 879  * \internal
 880  * \brief Trigger a new transition after CIB status was deleted
 881  *
 882  * If a CIB status delete was not expected (as part of the transition graph),
 883  * trigger a new transition by updating the (arbitrary) "last-lrm-refresh"
 884  * cluster property.
 885  *
 886  * \param[in] from_sys  IPC name that requested the delete
 887  * \param[in] rsc_id    Resource whose status was deleted (for logging only)
 888  */
 889 void
 890 controld_trigger_delete_refresh(const char *from_sys, const char *rsc_id)
     /* [previous][next][first][last][top][bottom][index][help] */
 891 {
 892     if (!pcmk__str_eq(from_sys, CRM_SYSTEM_TENGINE, pcmk__str_casei)) {
 893         char *now_s = crm_strdup_printf("%lld", (long long) time(NULL));
 894 
 895         crm_debug("Triggering a refresh after %s cleaned %s", from_sys, rsc_id);
 896         cib__update_node_attr(logger_out, fsa_cib_conn, cib_none, XML_CIB_TAG_CRMCONFIG,
 897                               NULL, NULL, NULL, NULL, "last-lrm-refresh", now_s, NULL, NULL);
 898         free(now_s);
 899     }
 900 }
 901 
 902 static void
 903 notify_deleted(lrm_state_t * lrm_state, ha_msg_input_t * input, const char *rsc_id, int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
 904 {
 905     lrmd_event_data_t *op = NULL;
 906     const char *from_sys = crm_element_value(input->msg, F_CRM_SYS_FROM);
 907     const char *from_host = crm_element_value(input->msg, F_CRM_HOST_FROM);
 908 
 909     crm_info("Notifying %s on %s that %s was%s deleted",
 910              from_sys, (from_host? from_host : "localhost"), rsc_id,
 911              ((rc == pcmk_ok)? "" : " not"));
 912     op = construct_op(lrm_state, input->xml, rsc_id, CRMD_ACTION_DELETE);
 913     controld_rc2event(op, pcmk_legacy2rc(rc));
 914     controld_ack_event_directly(from_host, from_sys, NULL, op, rsc_id);
 915     lrmd_free_event(op);
 916     controld_trigger_delete_refresh(from_sys, rsc_id);
 917 }
 918 
 919 static gboolean
 920 lrm_remove_deleted_rsc(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 921 {
 922     struct delete_event_s *event = user_data;
 923     struct pending_deletion_op_s *op = value;
 924 
 925     if (pcmk__str_eq(event->rsc, op->rsc, pcmk__str_none)) {
 926         notify_deleted(event->lrm_state, op->input, event->rsc, event->rc);
 927         return TRUE;
 928     }
 929     return FALSE;
 930 }
 931 
 932 static gboolean
 933 lrm_remove_deleted_op(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 934 {
 935     const char *rsc = user_data;
 936     active_op_t *pending = value;
 937 
 938     if (pcmk__str_eq(rsc, pending->rsc_id, pcmk__str_none)) {
 939         crm_info("Removing op %s:%d for deleted resource %s",
 940                  pending->op_key, pending->call_id, rsc);
 941         return TRUE;
 942     }
 943     return FALSE;
 944 }
 945 
 946 static void
 947 delete_rsc_entry(lrm_state_t * lrm_state, ha_msg_input_t * input, const char *rsc_id,
     /* [previous][next][first][last][top][bottom][index][help] */
 948                  GHashTableIter * rsc_gIter, int rc, const char *user_name)
 949 {
 950     struct delete_event_s event;
 951 
 952     CRM_CHECK(rsc_id != NULL, return);
 953 
 954     if (rc == pcmk_ok) {
 955         char *rsc_id_copy = strdup(rsc_id);
 956 
 957         if (rsc_gIter) {
 958             g_hash_table_iter_remove(rsc_gIter);
 959         } else {
 960             g_hash_table_remove(lrm_state->resource_history, rsc_id_copy);
 961         }
 962         controld_delete_resource_history(rsc_id_copy, lrm_state->node_name,
 963                                          user_name, crmd_cib_smart_opt());
 964         g_hash_table_foreach_remove(lrm_state->pending_ops, lrm_remove_deleted_op, rsc_id_copy);
 965         free(rsc_id_copy);
 966     }
 967 
 968     if (input) {
 969         notify_deleted(lrm_state, input, rsc_id, rc);
 970     }
 971 
 972     event.rc = rc;
 973     event.rsc = rsc_id;
 974     event.lrm_state = lrm_state;
 975     g_hash_table_foreach_remove(lrm_state->deletion_ops, lrm_remove_deleted_rsc, &event);
 976 }
 977 
 978 /*!
 979  * \internal
 980  * \brief Erase an LRM history entry from the CIB, given the operation data
 981  *
 982  * \param[in] op         Operation whose history should be deleted
 983  */
 984 static void
 985 erase_lrm_history_by_op(const lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 986 {
 987     xmlNode *xml_top = NULL;
 988 
 989     CRM_CHECK(op != NULL, return);
 990 
 991     xml_top = create_xml_node(NULL, XML_LRM_TAG_RSC_OP);
 992     crm_xml_add_int(xml_top, XML_LRM_ATTR_CALLID, op->call_id);
 993     crm_xml_add(xml_top, XML_ATTR_TRANSITION_KEY, op->user_data);
 994 
 995     if (op->interval_ms > 0) {
 996         char *op_id = pcmk__op_key(op->rsc_id, op->op_type, op->interval_ms);
 997 
 998         /* Avoid deleting last_failure too (if it was a result of this recurring op failing) */
 999         crm_xml_add(xml_top, XML_ATTR_ID, op_id);
1000         free(op_id);
1001     }
1002 
1003     crm_debug("Erasing resource operation history for " PCMK__OP_FMT " (call=%d)",
1004               op->rsc_id, op->op_type, op->interval_ms, op->call_id);
1005 
1006     fsa_cib_conn->cmds->remove(fsa_cib_conn, XML_CIB_TAG_STATUS, xml_top,
1007                                cib_quorum_override);
1008 
1009     crm_log_xml_trace(xml_top, "op:cancel");
1010     free_xml(xml_top);
1011 }
1012 
1013 /* Define xpath to find LRM resource history entry by node and resource */
1014 #define XPATH_HISTORY                                   \
1015     "/" XML_TAG_CIB "/" XML_CIB_TAG_STATUS              \
1016     "/" XML_CIB_TAG_STATE "[@" XML_ATTR_UNAME "='%s']"  \
1017     "/" XML_CIB_TAG_LRM "/" XML_LRM_TAG_RESOURCES       \
1018     "/" XML_LRM_TAG_RESOURCE "[@" XML_ATTR_ID "='%s']"  \
1019     "/" XML_LRM_TAG_RSC_OP
1020 
1021 /* ... and also by operation key */
1022 #define XPATH_HISTORY_ID XPATH_HISTORY \
1023     "[@" XML_ATTR_ID "='%s']"
1024 
1025 /* ... and also by operation key and operation call ID */
1026 #define XPATH_HISTORY_CALL XPATH_HISTORY \
1027     "[@" XML_ATTR_ID "='%s' and @" XML_LRM_ATTR_CALLID "='%d']"
1028 
1029 /* ... and also by operation key and original operation key */
1030 #define XPATH_HISTORY_ORIG XPATH_HISTORY \
1031     "[@" XML_ATTR_ID "='%s' and @" XML_LRM_ATTR_TASK_KEY "='%s']"
1032 
1033 /*!
1034  * \internal
1035  * \brief Erase an LRM history entry from the CIB, given operation identifiers
1036  *
1037  * \param[in] lrm_state  LRM state of the node to clear history for
1038  * \param[in] rsc_id     Name of resource to clear history for
1039  * \param[in] key        Operation key of operation to clear history for
1040  * \param[in] orig_op    If specified, delete only if it has this original op
1041  * \param[in] call_id    If specified, delete entry only if it has this call ID
1042  */
1043 static void
1044 erase_lrm_history_by_id(const lrm_state_t *lrm_state, const char *rsc_id,
     /* [previous][next][first][last][top][bottom][index][help] */
1045                         const char *key, const char *orig_op, int call_id)
1046 {
1047     char *op_xpath = NULL;
1048 
1049     CRM_CHECK((rsc_id != NULL) && (key != NULL), return);
1050 
1051     if (call_id > 0) {
1052         op_xpath = crm_strdup_printf(XPATH_HISTORY_CALL,
1053                                      lrm_state->node_name, rsc_id, key,
1054                                      call_id);
1055 
1056     } else if (orig_op) {
1057         op_xpath = crm_strdup_printf(XPATH_HISTORY_ORIG,
1058                                      lrm_state->node_name, rsc_id, key,
1059                                      orig_op);
1060     } else {
1061         op_xpath = crm_strdup_printf(XPATH_HISTORY_ID,
1062                                      lrm_state->node_name, rsc_id, key);
1063     }
1064 
1065     crm_debug("Erasing resource operation history for %s on %s (call=%d)",
1066               key, rsc_id, call_id);
1067     fsa_cib_conn->cmds->remove(fsa_cib_conn, op_xpath, NULL,
1068                                cib_quorum_override | cib_xpath);
1069     free(op_xpath);
1070 }
1071 
1072 static inline gboolean
1073 last_failed_matches_op(rsc_history_t *entry, const char *op, guint interval_ms)
     /* [previous][next][first][last][top][bottom][index][help] */
1074 {
1075     if (entry == NULL) {
1076         return FALSE;
1077     }
1078     if (op == NULL) {
1079         return TRUE;
1080     }
1081     return (pcmk__str_eq(op, entry->failed->op_type, pcmk__str_casei)
1082             && (interval_ms == entry->failed->interval_ms));
1083 }
1084 
1085 /*!
1086  * \internal
1087  * \brief Clear a resource's last failure
1088  *
1089  * Erase a resource's last failure on a particular node from both the
1090  * LRM resource history in the CIB, and the resource history remembered
1091  * for the LRM state.
1092  *
1093  * \param[in] rsc_id      Resource name
1094  * \param[in] node_name   Node name
1095  * \param[in] operation   If specified, only clear if matching this operation
1096  * \param[in] interval_ms If operation is specified, it has this interval
1097  */
1098 void
1099 lrm_clear_last_failure(const char *rsc_id, const char *node_name,
     /* [previous][next][first][last][top][bottom][index][help] */
1100                        const char *operation, guint interval_ms)
1101 {
1102     char *op_key = NULL;
1103     char *orig_op_key = NULL;
1104     lrm_state_t *lrm_state = NULL;
1105 
1106     lrm_state = lrm_state_find(node_name);
1107     if (lrm_state == NULL) {
1108         return;
1109     }
1110 
1111     /* Erase from CIB */
1112     op_key = pcmk__op_key(rsc_id, "last_failure", 0);
1113     if (operation) {
1114         orig_op_key = pcmk__op_key(rsc_id, operation, interval_ms);
1115     }
1116     erase_lrm_history_by_id(lrm_state, rsc_id, op_key, orig_op_key, 0);
1117     free(op_key);
1118     free(orig_op_key);
1119 
1120     /* Remove from memory */
1121     if (lrm_state->resource_history) {
1122         rsc_history_t *entry = g_hash_table_lookup(lrm_state->resource_history,
1123                                                    rsc_id);
1124 
1125         if (last_failed_matches_op(entry, operation, interval_ms)) {
1126             lrmd_free_event(entry->failed);
1127             entry->failed = NULL;
1128         }
1129     }
1130 }
1131 
1132 /* Returns: gboolean - cancellation is in progress */
1133 static gboolean
1134 cancel_op(lrm_state_t * lrm_state, const char *rsc_id, const char *key, int op, gboolean remove)
     /* [previous][next][first][last][top][bottom][index][help] */
1135 {
1136     int rc = pcmk_ok;
1137     char *local_key = NULL;
1138     active_op_t *pending = NULL;
1139 
1140     CRM_CHECK(op != 0, return FALSE);
1141     CRM_CHECK(rsc_id != NULL, return FALSE);
1142     if (key == NULL) {
1143         local_key = make_stop_id(rsc_id, op);
1144         key = local_key;
1145     }
1146     pending = g_hash_table_lookup(lrm_state->pending_ops, key);
1147 
1148     if (pending) {
1149         if (remove && !pcmk_is_set(pending->flags, active_op_remove)) {
1150             controld_set_active_op_flags(pending, active_op_remove);
1151             crm_debug("Scheduling %s for removal", key);
1152         }
1153 
1154         if (pcmk_is_set(pending->flags, active_op_cancelled)) {
1155             crm_debug("Operation %s already cancelled", key);
1156             free(local_key);
1157             return FALSE;
1158         }
1159         controld_set_active_op_flags(pending, active_op_cancelled);
1160 
1161     } else {
1162         crm_info("No pending op found for %s", key);
1163         free(local_key);
1164         return FALSE;
1165     }
1166 
1167     crm_debug("Cancelling op %d for %s (%s)", op, rsc_id, key);
1168     rc = lrm_state_cancel(lrm_state, pending->rsc_id, pending->op_type,
1169                           pending->interval_ms);
1170     if (rc == pcmk_ok) {
1171         crm_debug("Op %d for %s (%s): cancelled", op, rsc_id, key);
1172         free(local_key);
1173         return TRUE;
1174     }
1175 
1176     crm_debug("Op %d for %s (%s): Nothing to cancel", op, rsc_id, key);
1177     /* The caller needs to make sure the entry is
1178      * removed from the pending_ops list
1179      *
1180      * Usually by returning TRUE inside the worker function
1181      * supplied to g_hash_table_foreach_remove()
1182      *
1183      * Not removing the entry from pending_ops will block
1184      * the node from shutting down
1185      */
1186     free(local_key);
1187     return FALSE;
1188 }
1189 
1190 struct cancel_data {
1191     gboolean done;
1192     gboolean remove;
1193     const char *key;
1194     lrmd_rsc_info_t *rsc;
1195     lrm_state_t *lrm_state;
1196 };
1197 
1198 static gboolean
1199 cancel_action_by_key(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
1200 {
1201     gboolean remove = FALSE;
1202     struct cancel_data *data = user_data;
1203     active_op_t *op = value;
1204 
1205     if (pcmk__str_eq(op->op_key, data->key, pcmk__str_none)) {
1206         data->done = TRUE;
1207         remove = !cancel_op(data->lrm_state, data->rsc->id, key, op->call_id, data->remove);
1208     }
1209     return remove;
1210 }
1211 
1212 static gboolean
1213 cancel_op_key(lrm_state_t * lrm_state, lrmd_rsc_info_t * rsc, const char *key, gboolean remove)
     /* [previous][next][first][last][top][bottom][index][help] */
1214 {
1215     guint removed = 0;
1216     struct cancel_data data;
1217 
1218     CRM_CHECK(rsc != NULL, return FALSE);
1219     CRM_CHECK(key != NULL, return FALSE);
1220 
1221     data.key = key;
1222     data.rsc = rsc;
1223     data.done = FALSE;
1224     data.remove = remove;
1225     data.lrm_state = lrm_state;
1226 
1227     removed = g_hash_table_foreach_remove(lrm_state->pending_ops, cancel_action_by_key, &data);
1228     crm_trace("Removed %u op cache entries, new size: %u",
1229               removed, g_hash_table_size(lrm_state->pending_ops));
1230     return data.done;
1231 }
1232 
1233 /*!
1234  * \internal
1235  * \brief Retrieve resource information from LRM
1236  *
1237  * \param[in,out]  lrm_state  Executor connection state to use
1238  * \param[in]      rsc_xml    XML containing resource configuration
1239  * \param[in]      do_create  If true, register resource if not already
1240  * \param[out]     rsc_info   Where to store information obtained from executor
1241  *
1242  * \retval pcmk_ok   Success (and rsc_info holds newly allocated result)
1243  * \retval -EINVAL   Required information is missing from arguments
1244  * \retval -ENOTCONN No active connection to LRM
1245  * \retval -ENODEV   Resource not found
1246  * \retval -errno    Error communicating with executor when registering resource
1247  *
1248  * \note Caller is responsible for freeing result on success.
1249  */
1250 static int
1251 get_lrm_resource(lrm_state_t *lrm_state, const xmlNode *rsc_xml,
     /* [previous][next][first][last][top][bottom][index][help] */
1252                  gboolean do_create, lrmd_rsc_info_t **rsc_info)
1253 {
1254     const char *id = ID(rsc_xml);
1255 
1256     CRM_CHECK(lrm_state && rsc_xml && rsc_info, return -EINVAL);
1257     CRM_CHECK(id, return -EINVAL);
1258 
1259     if (lrm_state_is_connected(lrm_state) == FALSE) {
1260         return -ENOTCONN;
1261     }
1262 
1263     crm_trace("Retrieving resource information for %s from the executor", id);
1264     *rsc_info = lrm_state_get_rsc_info(lrm_state, id, 0);
1265 
1266     // If resource isn't known by ID, try clone name, if provided
1267     if (!*rsc_info) {
1268         const char *long_id = crm_element_value(rsc_xml, XML_ATTR_ID_LONG);
1269 
1270         if (long_id) {
1271             *rsc_info = lrm_state_get_rsc_info(lrm_state, long_id, 0);
1272         }
1273     }
1274 
1275     if ((*rsc_info == NULL) && do_create) {
1276         const char *class = crm_element_value(rsc_xml, XML_AGENT_ATTR_CLASS);
1277         const char *provider = crm_element_value(rsc_xml, XML_AGENT_ATTR_PROVIDER);
1278         const char *type = crm_element_value(rsc_xml, XML_ATTR_TYPE);
1279         int rc;
1280 
1281         crm_trace("Registering resource %s with the executor", id);
1282         rc = lrm_state_register_rsc(lrm_state, id, class, provider, type,
1283                                     lrmd_opt_drop_recurring);
1284         if (rc != pcmk_ok) {
1285             fsa_data_t *msg_data = NULL;
1286 
1287             crm_err("Could not register resource %s with the executor on %s: %s "
1288                     CRM_XS " rc=%d",
1289                     id, lrm_state->node_name, pcmk_strerror(rc), rc);
1290 
1291             /* Register this as an internal error if this involves the local
1292              * executor. Otherwise, we're likely dealing with an unresponsive
1293              * remote node, which is not an FSA failure.
1294              */
1295             if (lrm_state_is_local(lrm_state) == TRUE) {
1296                 register_fsa_error(C_FSA_INTERNAL, I_FAIL, NULL);
1297             }
1298             return rc;
1299         }
1300 
1301         *rsc_info = lrm_state_get_rsc_info(lrm_state, id, 0);
1302     }
1303     return *rsc_info? pcmk_ok : -ENODEV;
1304 }
1305 
1306 static void
1307 delete_resource(lrm_state_t * lrm_state,
     /* [previous][next][first][last][top][bottom][index][help] */
1308                 const char *id,
1309                 lrmd_rsc_info_t * rsc,
1310                 GHashTableIter * gIter,
1311                 const char *sys,
1312                 const char *user,
1313                 ha_msg_input_t * request,
1314                 gboolean unregister)
1315 {
1316     int rc = pcmk_ok;
1317 
1318     crm_info("Removing resource %s from executor for %s%s%s",
1319              id, sys, (user? " as " : ""), (user? user : ""));
1320 
1321     if (rsc && unregister) {
1322         rc = lrm_state_unregister_rsc(lrm_state, id, 0);
1323     }
1324 
1325     if (rc == pcmk_ok) {
1326         crm_trace("Resource %s deleted from executor", id);
1327     } else if (rc == -EINPROGRESS) {
1328         crm_info("Deletion of resource '%s' from executor is pending", id);
1329         if (request) {
1330             struct pending_deletion_op_s *op = NULL;
1331             char *ref = crm_element_value_copy(request->msg, XML_ATTR_REFERENCE);
1332 
1333             op = calloc(1, sizeof(struct pending_deletion_op_s));
1334             op->rsc = strdup(rsc->id);
1335             op->input = copy_ha_msg_input(request);
1336             g_hash_table_insert(lrm_state->deletion_ops, ref, op);
1337         }
1338         return;
1339     } else {
1340         crm_warn("Could not delete '%s' from executor for %s%s%s: %s "
1341                  CRM_XS " rc=%d", id, sys, (user? " as " : ""),
1342                  (user? user : ""), pcmk_strerror(rc), rc);
1343     }
1344 
1345     delete_rsc_entry(lrm_state, request, id, gIter, rc, user);
1346 }
1347 
1348 static int
1349 get_fake_call_id(lrm_state_t *lrm_state, const char *rsc_id)
     /* [previous][next][first][last][top][bottom][index][help] */
1350 {
1351     int call_id = 999999999;
1352     rsc_history_t *entry = NULL;
1353 
1354     if(lrm_state) {
1355         entry = g_hash_table_lookup(lrm_state->resource_history, rsc_id);
1356     }
1357 
1358     /* Make sure the call id is greater than the last successful operation,
1359      * otherwise the failure will not result in a possible recovery of the resource
1360      * as it could appear the failure occurred before the successful start */
1361     if (entry) {
1362         call_id = entry->last_callid + 1;
1363     }
1364 
1365     if (call_id < 0) {
1366         call_id = 1;
1367     }
1368     return call_id;
1369 }
1370 
1371 static void
1372 fake_op_status(lrm_state_t *lrm_state, lrmd_event_data_t *op, int op_status,
     /* [previous][next][first][last][top][bottom][index][help] */
1373                enum ocf_exitcode op_exitcode, const char *exit_reason)
1374 {
1375     op->call_id = get_fake_call_id(lrm_state, op->rsc_id);
1376     op->t_run = time(NULL);
1377     op->t_rcchange = op->t_run;
1378     lrmd__set_result(op, op_exitcode, op_status, exit_reason);
1379 }
1380 
1381 static void
1382 force_reprobe(lrm_state_t *lrm_state, const char *from_sys,
     /* [previous][next][first][last][top][bottom][index][help] */
1383               const char *from_host, const char *user_name,
1384               gboolean is_remote_node)
1385 {
1386     GHashTableIter gIter;
1387     rsc_history_t *entry = NULL;
1388 
1389     crm_info("Clearing resource history on node %s", lrm_state->node_name);
1390     g_hash_table_iter_init(&gIter, lrm_state->resource_history);
1391     while (g_hash_table_iter_next(&gIter, NULL, (void **)&entry)) {
1392         /* only unregister the resource during a reprobe if it is not a remote connection
1393          * resource. otherwise unregistering the connection will terminate remote-node
1394          * membership */
1395         gboolean unregister = TRUE;
1396 
1397         if (is_remote_lrmd_ra(NULL, NULL, entry->id)) {
1398             lrm_state_t *remote_lrm_state = lrm_state_find(entry->id);
1399             if (remote_lrm_state) {
1400                 /* when forcing a reprobe, make sure to clear remote node before
1401                  * clearing the remote node's connection resource */ 
1402                 force_reprobe(remote_lrm_state, from_sys, from_host, user_name, TRUE);
1403             }
1404             unregister = FALSE;
1405         }
1406 
1407         delete_resource(lrm_state, entry->id, &entry->rsc, &gIter, from_sys,
1408                         user_name, NULL, unregister);
1409     }
1410 
1411     /* Now delete the copy in the CIB */
1412     controld_delete_node_state(lrm_state->node_name, controld_section_lrm,
1413                                cib_scope_local);
1414 
1415     // @COMPAT DCs < 1.1.14 need this deleted (in case it was explicitly false)
1416     update_attrd(lrm_state->node_name, CRM_OP_PROBED, NULL, user_name, is_remote_node);
1417 }
1418 
1419 /*!
1420  * \internal
1421  * \brief Fail a requested action without actually executing it
1422  *
1423  * For an action that can't be executed, process it similarly to an actual
1424  * execution result, with specified error status (except for notify actions,
1425  * which will always be treated as successful).
1426  *
1427  * \param[in,out] lrm_state    Executor connection that action is for
1428  * \param[in]     action       Action XML from request
1429  * \param[in]     rc           Desired return code to use
1430  * \param[in]     op_status    Desired operation status to use
1431  * \param[in]     exit_reason  Human-friendly detail, if error
1432  */
1433 static void
1434 synthesize_lrmd_failure(lrm_state_t *lrm_state, const xmlNode *action,
     /* [previous][next][first][last][top][bottom][index][help] */
1435                         int op_status, enum ocf_exitcode rc,
1436                         const char *exit_reason)
1437 {
1438     lrmd_event_data_t *op = NULL;
1439     const char *operation = crm_element_value(action, XML_LRM_ATTR_TASK);
1440     const char *target_node = crm_element_value(action, XML_LRM_ATTR_TARGET);
1441     xmlNode *xml_rsc = find_xml_node(action, XML_CIB_TAG_RESOURCE, TRUE);
1442 
1443     if ((xml_rsc == NULL) || (ID(xml_rsc) == NULL)) {
1444         /* @TODO Should we do something else, like direct ack? */
1445         crm_info("Can't fake %s failure (%d) on %s without resource configuration",
1446                  crm_element_value(action, XML_LRM_ATTR_TASK_KEY), rc,
1447                  target_node);
1448         return;
1449 
1450     } else if(operation == NULL) {
1451         /* This probably came from crm_resource -C, nothing to do */
1452         crm_info("Can't fake %s failure (%d) on %s without operation",
1453                  ID(xml_rsc), rc, target_node);
1454         return;
1455     }
1456 
1457     op = construct_op(lrm_state, action, ID(xml_rsc), operation);
1458 
1459     if (pcmk__str_eq(operation, RSC_NOTIFY, pcmk__str_casei)) { // Notifications can't fail
1460         fake_op_status(lrm_state, op, PCMK_EXEC_DONE, PCMK_OCF_OK, NULL);
1461     } else {
1462         fake_op_status(lrm_state, op, op_status, rc, exit_reason);
1463     }
1464 
1465     crm_info("Faking " PCMK__OP_FMT " result (%d) on %s",
1466              op->rsc_id, op->op_type, op->interval_ms, op->rc, target_node);
1467 
1468     // Process the result as if it came from the LRM
1469     process_lrm_event(lrm_state, op, NULL, action);
1470     lrmd_free_event(op);
1471 }
1472 
1473 /*!
1474  * \internal
1475  * \brief Get target of an LRM operation
1476  *
1477  * \param[in] xml  LRM operation data XML
1478  *
1479  * \return LRM operation target node name (local node or Pacemaker Remote node)
1480  */
1481 static const char *
1482 lrm_op_target(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
1483 {
1484     const char *target = NULL;
1485 
1486     if (xml) {
1487         target = crm_element_value(xml, XML_LRM_ATTR_TARGET);
1488     }
1489     if (target == NULL) {
1490         target = fsa_our_uname;
1491     }
1492     return target;
1493 }
1494 
1495 static void
1496 fail_lrm_resource(xmlNode *xml, lrm_state_t *lrm_state, const char *user_name,
     /* [previous][next][first][last][top][bottom][index][help] */
1497                   const char *from_host, const char *from_sys)
1498 {
1499     lrmd_event_data_t *op = NULL;
1500     lrmd_rsc_info_t *rsc = NULL;
1501     xmlNode *xml_rsc = find_xml_node(xml, XML_CIB_TAG_RESOURCE, TRUE);
1502 
1503     CRM_CHECK(xml_rsc != NULL, return);
1504 
1505     /* The executor simply executes operations and reports the results, without
1506      * any concept of success or failure, so to fail a resource, we must fake
1507      * what a failure looks like.
1508      *
1509      * To do this, we create a fake executor operation event for the resource,
1510      * and pass that event to the executor client callback so it will be
1511      * processed as if it came from the executor.
1512      */
1513     op = construct_op(lrm_state, xml, ID(xml_rsc), "asyncmon");
1514 
1515     free((char*) op->user_data);
1516     op->user_data = NULL;
1517     op->interval_ms = 0;
1518 
1519     if (user_name && !pcmk__is_privileged(user_name)) {
1520         crm_err("%s does not have permission to fail %s", user_name, ID(xml_rsc));
1521         fake_op_status(lrm_state, op, PCMK_EXEC_ERROR,
1522                        PCMK_OCF_INSUFFICIENT_PRIV,
1523                        "Unprivileged user cannot fail resources");
1524         controld_ack_event_directly(from_host, from_sys, NULL, op, ID(xml_rsc));
1525         lrmd_free_event(op);
1526         return;
1527     }
1528 
1529 
1530     if (get_lrm_resource(lrm_state, xml_rsc, TRUE, &rsc) == pcmk_ok) {
1531         crm_info("Failing resource %s...", rsc->id);
1532         fake_op_status(lrm_state, op, PCMK_EXEC_DONE, PCMK_OCF_UNKNOWN_ERROR,
1533                        "Simulated failure");
1534         process_lrm_event(lrm_state, op, NULL, xml);
1535         op->rc = PCMK_OCF_OK; // The request to fail the resource succeeded
1536         lrmd_free_rsc_info(rsc);
1537 
1538     } else {
1539         crm_info("Cannot find/create resource in order to fail it...");
1540         crm_log_xml_warn(xml, "bad input");
1541         fake_op_status(lrm_state, op, PCMK_EXEC_ERROR, PCMK_OCF_UNKNOWN_ERROR,
1542                        "Cannot fail unknown resource");
1543     }
1544 
1545     controld_ack_event_directly(from_host, from_sys, NULL, op, ID(xml_rsc));
1546     lrmd_free_event(op);
1547 }
1548 
1549 static void
1550 handle_reprobe_op(lrm_state_t *lrm_state, const char *from_sys,
     /* [previous][next][first][last][top][bottom][index][help] */
1551                   const char *from_host, const char *user_name,
1552                   gboolean is_remote_node)
1553 {
1554     crm_notice("Forcing the status of all resources to be redetected");
1555     force_reprobe(lrm_state, from_sys, from_host, user_name, is_remote_node);
1556 
1557     if (!pcmk__strcase_any_of(from_sys, CRM_SYSTEM_PENGINE, CRM_SYSTEM_TENGINE, NULL)) {
1558 
1559         xmlNode *reply = create_request(CRM_OP_INVOKE_LRM, NULL, from_host,
1560                                         from_sys, CRM_SYSTEM_LRMD,
1561                                         fsa_our_uuid);
1562 
1563         crm_debug("ACK'ing re-probe from %s (%s)", from_sys, from_host);
1564 
1565         if (relay_message(reply, TRUE) == FALSE) {
1566             crm_log_xml_err(reply, "Unable to route reply");
1567         }
1568         free_xml(reply);
1569     }
1570 }
1571 
1572 static bool do_lrm_cancel(ha_msg_input_t *input, lrm_state_t *lrm_state,
     /* [previous][next][first][last][top][bottom][index][help] */
1573               lrmd_rsc_info_t *rsc, const char *from_host, const char *from_sys)
1574 {
1575     char *op_key = NULL;
1576     char *meta_key = NULL;
1577     int call = 0;
1578     const char *call_id = NULL;
1579     const char *op_task = NULL;
1580     guint interval_ms = 0;
1581     gboolean in_progress = FALSE;
1582     xmlNode *params = find_xml_node(input->xml, XML_TAG_ATTRS, TRUE);
1583 
1584     CRM_CHECK(params != NULL, return FALSE);
1585 
1586     meta_key = crm_meta_name(XML_LRM_ATTR_TASK);
1587     op_task = crm_element_value(params, meta_key);
1588     free(meta_key);
1589     CRM_CHECK(op_task != NULL, return FALSE);
1590 
1591     meta_key = crm_meta_name(XML_LRM_ATTR_INTERVAL_MS);
1592     if (crm_element_value_ms(params, meta_key, &interval_ms) != pcmk_ok) {
1593         free(meta_key);
1594         return FALSE;
1595     }
1596     free(meta_key);
1597 
1598     op_key = pcmk__op_key(rsc->id, op_task, interval_ms);
1599 
1600     meta_key = crm_meta_name(XML_LRM_ATTR_CALLID);
1601     call_id = crm_element_value(params, meta_key);
1602     free(meta_key);
1603 
1604     crm_debug("Scheduler requested op %s (call=%s) be cancelled",
1605               op_key, (call_id? call_id : "NA"));
1606     pcmk__scan_min_int(call_id, &call, 0);
1607     if (call == 0) {
1608         // Normal case when the scheduler cancels a recurring op
1609         in_progress = cancel_op_key(lrm_state, rsc, op_key, TRUE);
1610 
1611     } else {
1612         // Normal case when the scheduler cancels an orphan op
1613         in_progress = cancel_op(lrm_state, rsc->id, NULL, call, TRUE);
1614     }
1615 
1616     // Acknowledge cancellation operation if for a remote connection resource
1617     if (!in_progress || is_remote_lrmd_ra(NULL, NULL, rsc->id)) {
1618         char *op_id = make_stop_id(rsc->id, call);
1619 
1620         if (is_remote_lrmd_ra(NULL, NULL, rsc->id) == FALSE) {
1621             crm_info("Nothing known about operation %d for %s", call, op_key);
1622         }
1623         erase_lrm_history_by_id(lrm_state, rsc->id, op_key, NULL, call);
1624         send_task_ok_ack(lrm_state, input, rsc->id, rsc, op_task,
1625                          from_host, from_sys);
1626 
1627         /* needed at least for cancellation of a remote operation */
1628         g_hash_table_remove(lrm_state->pending_ops, op_id);
1629         free(op_id);
1630 
1631     } else {
1632         /* No ack is needed since abcdaa8, but peers with older versions
1633          * in a rolling upgrade need one. We didn't bump the feature set
1634          * at that commit, so we can only compare against the previous
1635          * CRM version (3.0.8). If any peers have feature set 3.0.9 but
1636          * not abcdaa8, they will time out waiting for the ack (no
1637          * released versions of Pacemaker are affected).
1638          */
1639         const char *peer_version = crm_element_value(params, XML_ATTR_CRM_VERSION);
1640 
1641         if (compare_version(peer_version, "3.0.8") <= 0) {
1642             crm_info("Sending compatibility ack for %s cancellation to %s (CRM version %s)",
1643                      op_key, from_host, peer_version);
1644             send_task_ok_ack(lrm_state, input, rsc->id, rsc, op_task,
1645                              from_host, from_sys);
1646         }
1647     }
1648 
1649     free(op_key);
1650     return TRUE;
1651 }
1652 
1653 static void
1654 do_lrm_delete(ha_msg_input_t *input, lrm_state_t *lrm_state,
     /* [previous][next][first][last][top][bottom][index][help] */
1655               lrmd_rsc_info_t *rsc, const char *from_sys, const char *from_host,
1656               bool crm_rsc_delete, const char *user_name)
1657 {
1658     gboolean unregister = TRUE;
1659     int cib_rc = controld_delete_resource_history(rsc->id, lrm_state->node_name,
1660                                                   user_name,
1661                                                   cib_dryrun|cib_sync_call);
1662 
1663     if (cib_rc != pcmk_rc_ok) {
1664         lrmd_event_data_t *op = NULL;
1665 
1666         op = construct_op(lrm_state, input->xml, rsc->id, CRMD_ACTION_DELETE);
1667 
1668         /* These are resource clean-ups, not actions, so no exit reason is
1669          * needed.
1670          */
1671         lrmd__set_result(op, pcmk_rc2ocf(cib_rc), PCMK_EXEC_ERROR, NULL);
1672         controld_ack_event_directly(from_host, from_sys, NULL, op, rsc->id);
1673         lrmd_free_event(op);
1674         return;
1675     }
1676 
1677     if (crm_rsc_delete && is_remote_lrmd_ra(NULL, NULL, rsc->id)) {
1678         unregister = FALSE;
1679     }
1680 
1681     delete_resource(lrm_state, rsc->id, rsc, NULL, from_sys,
1682                     user_name, input, unregister);
1683 }
1684 
1685 // User data for asynchronous metadata execution
1686 struct metadata_cb_data {
1687     lrmd_rsc_info_t *rsc;   // Copy of resource information
1688     xmlNode *input_xml;     // Copy of FSA input XML
1689 };
1690 
1691 static struct metadata_cb_data *
1692 new_metadata_cb_data(lrmd_rsc_info_t *rsc, xmlNode *input_xml)
     /* [previous][next][first][last][top][bottom][index][help] */
1693 {
1694     struct metadata_cb_data *data = NULL;
1695 
1696     data = calloc(1, sizeof(struct metadata_cb_data));
1697     CRM_ASSERT(data != NULL);
1698     data->input_xml = copy_xml(input_xml);
1699     data->rsc = lrmd_copy_rsc_info(rsc);
1700     return data;
1701 }
1702 
1703 static void
1704 free_metadata_cb_data(struct metadata_cb_data *data)
     /* [previous][next][first][last][top][bottom][index][help] */
1705 {
1706     lrmd_free_rsc_info(data->rsc);
1707     free_xml(data->input_xml);
1708     free(data);
1709 }
1710 
1711 /*!
1712  * \internal
1713  * \brief Execute an action after metadata has been retrieved
1714  *
1715  * \param[in] pid        Ignored
1716  * \param[in] result     Result of metadata action
1717  * \param[in] user_data  Metadata callback data
1718  */
1719 static void
1720 metadata_complete(int pid, const pcmk__action_result_t *result, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
1721 {
1722     struct metadata_cb_data *data = (struct metadata_cb_data *) user_data;
1723 
1724     struct ra_metadata_s *md = NULL;
1725     lrm_state_t *lrm_state = lrm_state_find(lrm_op_target(data->input_xml));
1726 
1727     if ((lrm_state != NULL) && pcmk__result_ok(result)) {
1728         md = controld_cache_metadata(lrm_state->metadata_cache, data->rsc,
1729                                      result->action_stdout);
1730     }
1731     do_lrm_rsc_op(lrm_state, data->rsc, data->input_xml, md);
1732     free_metadata_cb_data(data);
1733 }
1734 
1735 /*       A_LRM_INVOKE   */
1736 void
1737 do_lrm_invoke(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
1738               enum crmd_fsa_cause cause,
1739               enum crmd_fsa_state cur_state,
1740               enum crmd_fsa_input current_input, fsa_data_t * msg_data)
1741 {
1742     lrm_state_t *lrm_state = NULL;
1743     const char *crm_op = NULL;
1744     const char *from_sys = NULL;
1745     const char *from_host = NULL;
1746     const char *operation = NULL;
1747     ha_msg_input_t *input = fsa_typed_data(fsa_dt_ha_msg);
1748     const char *user_name = NULL;
1749     const char *target_node = NULL;
1750     gboolean is_remote_node = FALSE;
1751     bool crm_rsc_delete = FALSE;
1752 
1753     target_node = lrm_op_target(input->xml);
1754     is_remote_node = !pcmk__str_eq(target_node, fsa_our_uname,
1755                                    pcmk__str_casei);
1756 
1757     lrm_state = lrm_state_find(target_node);
1758     if ((lrm_state == NULL) && is_remote_node) {
1759         crm_err("Failing action because local node has never had connection to remote node %s",
1760                 target_node);
1761         synthesize_lrmd_failure(NULL, input->xml, PCMK_EXEC_NOT_CONNECTED,
1762                                 PCMK_OCF_UNKNOWN_ERROR,
1763                                 "Local node has no connection to remote");
1764         return;
1765     }
1766     CRM_ASSERT(lrm_state != NULL);
1767 
1768     user_name = pcmk__update_acl_user(input->msg, F_CRM_USER, NULL);
1769     crm_op = crm_element_value(input->msg, F_CRM_TASK);
1770     from_sys = crm_element_value(input->msg, F_CRM_SYS_FROM);
1771     if (!pcmk__str_eq(from_sys, CRM_SYSTEM_TENGINE, pcmk__str_none)) {
1772         from_host = crm_element_value(input->msg, F_CRM_HOST_FROM);
1773     }
1774 
1775     if (pcmk__str_eq(crm_op, CRM_OP_LRM_DELETE, pcmk__str_none)) {
1776         if (!pcmk__str_eq(from_sys, CRM_SYSTEM_TENGINE, pcmk__str_none)) {
1777             crm_rsc_delete = TRUE; // from crm_resource
1778         }
1779         operation = CRMD_ACTION_DELETE;
1780 
1781     } else if (input->xml != NULL) {
1782         operation = crm_element_value(input->xml, XML_LRM_ATTR_TASK);
1783     }
1784 
1785     CRM_CHECK(!pcmk__str_empty(crm_op) || !pcmk__str_empty(operation), return);
1786 
1787     crm_trace("'%s' execution request from %s as %s user",
1788               pcmk__s(crm_op, operation),
1789               pcmk__s(from_sys, "unknown subsystem"),
1790               pcmk__s(user_name, "current"));
1791 
1792     if (pcmk__str_eq(crm_op, CRM_OP_LRM_FAIL, pcmk__str_none)) {
1793         fail_lrm_resource(input->xml, lrm_state, user_name, from_host,
1794                           from_sys);
1795 
1796     } else if (pcmk__str_eq(crm_op, CRM_OP_LRM_REFRESH, pcmk__str_none)) {
1797         /* @COMPAT This can only be sent by crm_resource --refresh on a
1798          * Pacemaker Remote node running Pacemaker 1.1.9, which is extremely
1799          * unlikely. It previously would cause the controller to re-write its
1800          * resource history to the CIB. Just ignore it.
1801          */
1802         crm_notice("Ignoring refresh request from Pacemaker Remote 1.1.9 node");
1803 
1804     // @COMPAT DCs <1.1.14 in a rolling upgrade might schedule this op
1805     } else if (pcmk__str_eq(operation, CRM_OP_PROBED, pcmk__str_none)) {
1806         update_attrd(lrm_state->node_name, CRM_OP_PROBED, XML_BOOLEAN_TRUE,
1807                      user_name, is_remote_node);
1808 
1809     } else if (pcmk__str_eq(crm_op, CRM_OP_REPROBE, pcmk__str_none)
1810                || pcmk__str_eq(operation, CRM_OP_REPROBE, pcmk__str_none)) {
1811         handle_reprobe_op(lrm_state, from_sys, from_host, user_name,
1812                           is_remote_node);
1813 
1814     } else if (operation != NULL) {
1815         lrmd_rsc_info_t *rsc = NULL;
1816         xmlNode *xml_rsc = find_xml_node(input->xml, XML_CIB_TAG_RESOURCE, TRUE);
1817         gboolean create_rsc = !pcmk__str_eq(operation, CRMD_ACTION_DELETE,
1818                                             pcmk__str_none);
1819         int rc;
1820 
1821         // We can't return anything meaningful without a resource ID
1822         CRM_CHECK(xml_rsc && ID(xml_rsc), return);
1823 
1824         rc = get_lrm_resource(lrm_state, xml_rsc, create_rsc, &rsc);
1825         if (rc == -ENOTCONN) {
1826             synthesize_lrmd_failure(lrm_state, input->xml,
1827                                     PCMK_EXEC_NOT_CONNECTED,
1828                                     PCMK_OCF_UNKNOWN_ERROR,
1829                                     "Not connected to remote executor");
1830             return;
1831 
1832         } else if ((rc < 0) && !create_rsc) {
1833             /* Delete of malformed or nonexistent resource
1834              * (deleting something that does not exist is a success)
1835              */
1836             crm_notice("Not registering resource '%s' for a %s event "
1837                        CRM_XS " get-rc=%d (%s) transition-key=%s",
1838                        ID(xml_rsc), operation,
1839                        rc, pcmk_strerror(rc), ID(input->xml));
1840             delete_rsc_entry(lrm_state, input, ID(xml_rsc), NULL, pcmk_ok,
1841                              user_name);
1842             return;
1843 
1844         } else if (rc == -EINVAL) {
1845             // Resource operation on malformed resource
1846             crm_err("Invalid resource definition for %s", ID(xml_rsc));
1847             crm_log_xml_warn(input->msg, "invalid resource");
1848             synthesize_lrmd_failure(lrm_state, input->xml, PCMK_EXEC_ERROR,
1849                                     PCMK_OCF_NOT_CONFIGURED, // fatal error
1850                                     "Invalid resource definition");
1851             return;
1852 
1853         } else if (rc < 0) {
1854             // Error communicating with the executor
1855             crm_err("Could not register resource '%s' with executor: %s "
1856                     CRM_XS " rc=%d",
1857                     ID(xml_rsc), pcmk_strerror(rc), rc);
1858             crm_log_xml_warn(input->msg, "failed registration");
1859             synthesize_lrmd_failure(lrm_state, input->xml, PCMK_EXEC_ERROR,
1860                                     PCMK_OCF_INVALID_PARAM, // hard error
1861                                     "Could not register resource with executor");
1862             return;
1863         }
1864 
1865         if (pcmk__str_eq(operation, CRMD_ACTION_CANCEL, pcmk__str_none)) {
1866             if (!do_lrm_cancel(input, lrm_state, rsc, from_host, from_sys)) {
1867                 crm_log_xml_warn(input->xml, "Bad command");
1868             }
1869 
1870         } else if (pcmk__str_eq(operation, CRMD_ACTION_DELETE, pcmk__str_none)) {
1871             do_lrm_delete(input, lrm_state, rsc, from_sys, from_host,
1872                           crm_rsc_delete, user_name);
1873 
1874         } else {
1875             struct ra_metadata_s *md = NULL;
1876 
1877             /* Getting metadata from cache is OK except for start actions --
1878              * always refresh from the agent for those, in case the resource
1879              * agent was updated.
1880              *
1881              * @TODO Only refresh metadata for starts if the agent actually
1882              * changed (using something like inotify, or a hash or modification
1883              * time of the agent executable).
1884              */
1885             if (strcmp(operation, CRMD_ACTION_START) != 0) {
1886                 md = controld_get_rsc_metadata(lrm_state, rsc,
1887                                                controld_metadata_from_cache);
1888             }
1889 
1890             if ((md == NULL) && crm_op_needs_metadata(rsc->standard,
1891                                                       operation)) {
1892                 /* Most likely, we'll need the agent metadata to record the
1893                  * pending operation and the operation result. Get it now rather
1894                  * than wait until then, so the metadata action doesn't eat into
1895                  * the real action's timeout.
1896                  *
1897                  * @TODO Metadata is retrieved via direct execution of the
1898                  * agent, which has a couple of related issues: the executor
1899                  * should execute agents, not the controller; and metadata for
1900                  * Pacemaker Remote nodes should be collected on those nodes,
1901                  * not locally.
1902                  */
1903                 struct metadata_cb_data *data = NULL;
1904 
1905                 data = new_metadata_cb_data(rsc, input->xml);
1906                 crm_info("Retrieving metadata for %s (%s%s%s:%s) asynchronously",
1907                          rsc->id, rsc->standard,
1908                          ((rsc->provider == NULL)? "" : ":"),
1909                          ((rsc->provider == NULL)? "" : rsc->provider),
1910                          rsc->type);
1911                 (void) lrmd__metadata_async(rsc, metadata_complete,
1912                                             (void *) data);
1913             } else {
1914                 do_lrm_rsc_op(lrm_state, rsc, input->xml, md);
1915             }
1916         }
1917 
1918         lrmd_free_rsc_info(rsc);
1919 
1920     } else {
1921         crm_err("Invalid execution request: unknown command '%s' (bug?)",
1922                 crm_op);
1923         register_fsa_error(C_FSA_INTERNAL, I_ERROR, NULL);
1924     }
1925 }
1926 
1927 static lrmd_event_data_t *
1928 construct_op(const lrm_state_t *lrm_state, const xmlNode *rsc_op,
     /* [previous][next][first][last][top][bottom][index][help] */
1929              const char *rsc_id, const char *operation)
1930 {
1931     lrmd_event_data_t *op = NULL;
1932     const char *op_delay = NULL;
1933     const char *op_timeout = NULL;
1934     GHashTable *params = NULL;
1935 
1936     xmlNode *primitive = NULL;
1937     const char *class = NULL;
1938 
1939     const char *transition = NULL;
1940 
1941     CRM_ASSERT(rsc_id && operation);
1942 
1943     op = lrmd_new_event(rsc_id, operation, 0);
1944     op->type = lrmd_event_exec_complete;
1945     op->timeout = 0;
1946     op->start_delay = 0;
1947     lrmd__set_result(op, PCMK_OCF_UNKNOWN, PCMK_EXEC_PENDING, NULL);
1948 
1949     if (rsc_op == NULL) {
1950         CRM_LOG_ASSERT(pcmk__str_eq(CRMD_ACTION_STOP, operation, pcmk__str_casei));
1951         op->user_data = NULL;
1952         /* the stop_all_resources() case
1953          * by definition there is no DC (or they'd be shutting
1954          *   us down).
1955          * So we should put our version here.
1956          */
1957         op->params = pcmk__strkey_table(free, free);
1958 
1959         g_hash_table_insert(op->params, strdup(XML_ATTR_CRM_VERSION), strdup(CRM_FEATURE_SET));
1960 
1961         crm_trace("Constructed %s op for %s", operation, rsc_id);
1962         return op;
1963     }
1964 
1965     params = xml2list(rsc_op);
1966     g_hash_table_remove(params, CRM_META "_op_target_rc");
1967 
1968     op_delay = crm_meta_value(params, XML_OP_ATTR_START_DELAY);
1969     pcmk__scan_min_int(op_delay, &op->start_delay, 0);
1970 
1971     op_timeout = crm_meta_value(params, XML_ATTR_TIMEOUT);
1972     pcmk__scan_min_int(op_timeout, &op->timeout, 0);
1973 
1974     if (pcmk__guint_from_hash(params, CRM_META "_" XML_LRM_ATTR_INTERVAL_MS, 0,
1975                               &(op->interval_ms)) != pcmk_rc_ok) {
1976         op->interval_ms = 0;
1977     }
1978 
1979     /* Use pcmk_monitor_timeout instead of meta timeout for stonith
1980        recurring monitor, if set */
1981     primitive = find_xml_node(rsc_op, XML_CIB_TAG_RESOURCE, FALSE);
1982     class = crm_element_value(primitive, XML_AGENT_ATTR_CLASS);
1983 
1984     if (pcmk_is_set(pcmk_get_ra_caps(class), pcmk_ra_cap_fence_params)
1985             && pcmk__str_eq(operation, CRMD_ACTION_STATUS, pcmk__str_casei)
1986             && (op->interval_ms > 0)) {
1987 
1988         op_timeout = g_hash_table_lookup(params, "pcmk_monitor_timeout");
1989         if (op_timeout != NULL) {
1990             op->timeout = crm_get_msec(op_timeout);
1991         }
1992     }
1993 
1994     if (!pcmk__str_eq(operation, RSC_STOP, pcmk__str_casei)) {
1995         op->params = params;
1996 
1997     } else {
1998         rsc_history_t *entry = NULL;
1999 
2000         if (lrm_state) {
2001             entry = g_hash_table_lookup(lrm_state->resource_history, rsc_id);
2002         }
2003 
2004         /* If we do not have stop parameters cached, use
2005          * whatever we are given */
2006         if (!entry || !entry->stop_params) {
2007             op->params = params;
2008         } else {
2009             /* Copy the cached parameter list so that we stop the resource
2010              * with the old attributes, not the new ones */
2011             op->params = pcmk__strkey_table(free, free);
2012 
2013             g_hash_table_foreach(params, copy_meta_keys, op->params);
2014             g_hash_table_foreach(entry->stop_params, copy_instance_keys, op->params);
2015             g_hash_table_destroy(params);
2016             params = NULL;
2017         }
2018     }
2019 
2020     /* sanity */
2021     if (op->timeout <= 0) {
2022         op->timeout = op->interval_ms;
2023     }
2024     if (op->start_delay < 0) {
2025         op->start_delay = 0;
2026     }
2027 
2028     transition = crm_element_value(rsc_op, XML_ATTR_TRANSITION_KEY);
2029     CRM_CHECK(transition != NULL, return op);
2030 
2031     op->user_data = strdup(transition);
2032 
2033     if (op->interval_ms != 0) {
2034         if (pcmk__strcase_any_of(operation, CRMD_ACTION_START, CRMD_ACTION_STOP, NULL)) {
2035             crm_err("Start and Stop actions cannot have an interval: %u",
2036                     op->interval_ms);
2037             op->interval_ms = 0;
2038         }
2039     }
2040 
2041     crm_trace("Constructed %s op for %s: interval=%u",
2042               operation, rsc_id, op->interval_ms);
2043 
2044     return op;
2045 }
2046 
2047 /*!
2048  * \internal
2049  * \brief Send a (synthesized) event result
2050  *
2051  * Reply with a synthesized event result directly, as opposed to going through
2052  * the executor.
2053  *
2054  * \param[in]     to_host  Host to send result to
2055  * \param[in]     to_sys   IPC name to send result (NULL for transition engine)
2056  * \param[in]     rsc      Type information about resource the result is for
2057  * \param[in,out] op       Event with result to send
2058  * \param[in]     rsc_id   ID of resource the result is for
2059  */
2060 void
2061 controld_ack_event_directly(const char *to_host, const char *to_sys,
     /* [previous][next][first][last][top][bottom][index][help] */
2062                             const lrmd_rsc_info_t *rsc, lrmd_event_data_t *op,
2063                             const char *rsc_id)
2064 {
2065     xmlNode *reply = NULL;
2066     xmlNode *update, *iter;
2067     crm_node_t *peer = NULL;
2068 
2069     CRM_CHECK(op != NULL, return);
2070     if (op->rsc_id == NULL) {
2071         CRM_ASSERT(rsc_id != NULL);
2072         op->rsc_id = strdup(rsc_id);
2073     }
2074     if (to_sys == NULL) {
2075         to_sys = CRM_SYSTEM_TENGINE;
2076     }
2077 
2078     peer = crm_get_peer(0, fsa_our_uname);
2079     update = create_node_state_update(peer, node_update_none, NULL,
2080                                       __func__);
2081 
2082     iter = create_xml_node(update, XML_CIB_TAG_LRM);
2083     crm_xml_add(iter, XML_ATTR_ID, fsa_our_uuid);
2084     iter = create_xml_node(iter, XML_LRM_TAG_RESOURCES);
2085     iter = create_xml_node(iter, XML_LRM_TAG_RESOURCE);
2086 
2087     crm_xml_add(iter, XML_ATTR_ID, op->rsc_id);
2088 
2089     build_operation_update(iter, rsc, op, fsa_our_uname, __func__);
2090     reply = create_request(CRM_OP_INVOKE_LRM, update, to_host, to_sys, CRM_SYSTEM_LRMD, NULL);
2091 
2092     crm_log_xml_trace(update, "[direct ACK]");
2093 
2094     crm_debug("ACK'ing resource op " PCMK__OP_FMT " from %s: %s",
2095               op->rsc_id, op->op_type, op->interval_ms, op->user_data,
2096               crm_element_value(reply, XML_ATTR_REFERENCE));
2097 
2098     if (relay_message(reply, TRUE) == FALSE) {
2099         crm_log_xml_err(reply, "Unable to route reply");
2100     }
2101 
2102     free_xml(update);
2103     free_xml(reply);
2104 }
2105 
2106 gboolean
2107 verify_stopped(enum crmd_fsa_state cur_state, int log_level)
     /* [previous][next][first][last][top][bottom][index][help] */
2108 {
2109     gboolean res = TRUE;
2110     GList *lrm_state_list = lrm_state_get_list();
2111     GList *state_entry;
2112 
2113     for (state_entry = lrm_state_list; state_entry != NULL; state_entry = state_entry->next) {
2114         lrm_state_t *lrm_state = state_entry->data;
2115 
2116         if (!lrm_state_verify_stopped(lrm_state, cur_state, log_level)) {
2117             /* keep iterating through all even when false is returned */
2118             res = FALSE;
2119         }
2120     }
2121 
2122     controld_set_fsa_input_flags(R_SENT_RSC_STOP);
2123     g_list_free(lrm_state_list); lrm_state_list = NULL;
2124     return res;
2125 }
2126 
2127 struct stop_recurring_action_s {
2128     lrmd_rsc_info_t *rsc;
2129     lrm_state_t *lrm_state;
2130 };
2131 
2132 static gboolean
2133 stop_recurring_action_by_rsc(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2134 {
2135     gboolean remove = FALSE;
2136     struct stop_recurring_action_s *event = user_data;
2137     active_op_t *op = value;
2138 
2139     if ((op->interval_ms != 0)
2140         && pcmk__str_eq(op->rsc_id, event->rsc->id, pcmk__str_none)) {
2141 
2142         crm_debug("Cancelling op %d for %s (%s)", op->call_id, op->rsc_id, (char*)key);
2143         remove = !cancel_op(event->lrm_state, event->rsc->id, key, op->call_id, FALSE);
2144     }
2145 
2146     return remove;
2147 }
2148 
2149 static gboolean
2150 stop_recurring_actions(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2151 {
2152     gboolean remove = FALSE;
2153     lrm_state_t *lrm_state = user_data;
2154     active_op_t *op = value;
2155 
2156     if (op->interval_ms != 0) {
2157         crm_info("Cancelling op %d for %s (%s)", op->call_id, op->rsc_id,
2158                  (const char *) key);
2159         remove = !cancel_op(lrm_state, op->rsc_id, key, op->call_id, FALSE);
2160     }
2161 
2162     return remove;
2163 }
2164 
2165 static void
2166 record_pending_op(const char *node_name, lrmd_rsc_info_t *rsc, lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
2167 {
2168     const char *record_pending = NULL;
2169 
2170     CRM_CHECK(node_name != NULL, return);
2171     CRM_CHECK(rsc != NULL, return);
2172     CRM_CHECK(op != NULL, return);
2173 
2174     // Never record certain operation types as pending
2175     if ((op->op_type == NULL) || (op->params == NULL)
2176         || !controld_action_is_recordable(op->op_type)) {
2177         return;
2178     }
2179 
2180     // defaults to true
2181     record_pending = crm_meta_value(op->params, XML_OP_ATTR_PENDING);
2182     if (record_pending && !crm_is_true(record_pending)) {
2183         return;
2184     }
2185 
2186     op->call_id = -1;
2187     lrmd__set_result(op, PCMK_OCF_UNKNOWN, PCMK_EXEC_PENDING, NULL);
2188 
2189     op->t_run = time(NULL);
2190     op->t_rcchange = op->t_run;
2191 
2192     /* write a "pending" entry to the CIB, inhibit notification */
2193     crm_debug("Recording pending op " PCMK__OP_FMT " on %s in the CIB",
2194               op->rsc_id, op->op_type, op->interval_ms, node_name);
2195 
2196     do_update_resource(node_name, rsc, op, 0);
2197 }
2198 
2199 static void
2200 do_lrm_rsc_op(lrm_state_t *lrm_state, lrmd_rsc_info_t *rsc, xmlNode *msg,
     /* [previous][next][first][last][top][bottom][index][help] */
2201               struct ra_metadata_s *md)
2202 {
2203     int rc;
2204     int call_id = 0;
2205     char *op_id = NULL;
2206     lrmd_event_data_t *op = NULL;
2207     lrmd_key_value_t *params = NULL;
2208     fsa_data_t *msg_data = NULL;
2209     const char *transition = NULL;
2210     const char *operation = NULL;
2211     gboolean stop_recurring = FALSE;
2212     const char *nack_reason = NULL;
2213 
2214     CRM_CHECK((rsc != NULL) && (msg != NULL), return);
2215 
2216     operation = crm_element_value(msg, XML_LRM_ATTR_TASK);
2217     CRM_CHECK(!pcmk__str_empty(operation), return);
2218 
2219     transition = crm_element_value(msg, XML_ATTR_TRANSITION_KEY);
2220     if (pcmk__str_empty(transition)) {
2221         crm_log_xml_err(msg, "Missing transition number");
2222     }
2223 
2224     if (lrm_state == NULL) {
2225         // This shouldn't be possible, but provide a failsafe just in case
2226         crm_err("Cannot execute %s of %s: No executor connection "
2227                 CRM_XS " transition_key=%s",
2228                 operation, rsc->id, pcmk__s(transition, ""));
2229         synthesize_lrmd_failure(NULL, msg, PCMK_EXEC_INVALID,
2230                                 PCMK_OCF_UNKNOWN_ERROR,
2231                                 "No executor connection");
2232         return;
2233     }
2234 
2235     if (pcmk__str_any_of(operation, CRMD_ACTION_RELOAD,
2236                          CRMD_ACTION_RELOAD_AGENT, NULL)) {
2237         /* Pre-2.1.0 DCs will schedule reload actions only, and 2.1.0+ DCs
2238          * will schedule reload-agent actions only. In either case, we need
2239          * to map that to whatever the resource agent actually supports.
2240          * Default to the OCF 1.1 name.
2241          */
2242         if ((md != NULL)
2243             && pcmk_is_set(md->ra_flags, ra_supports_legacy_reload)) {
2244             operation = CRMD_ACTION_RELOAD;
2245         } else {
2246             operation = CRMD_ACTION_RELOAD_AGENT;
2247         }
2248     }
2249 
2250     op = construct_op(lrm_state, msg, rsc->id, operation);
2251     CRM_CHECK(op != NULL, return);
2252 
2253     if (is_remote_lrmd_ra(NULL, NULL, rsc->id)
2254         && (op->interval_ms == 0)
2255         && strcmp(operation, CRMD_ACTION_MIGRATE) == 0) {
2256 
2257         /* pcmk remote connections are a special use case.
2258          * We never ever want to stop monitoring a connection resource until
2259          * the entire migration has completed. If the connection is unexpectedly
2260          * severed, even during a migration, this is an event we must detect.*/
2261         stop_recurring = FALSE;
2262 
2263     } else if ((op->interval_ms == 0)
2264         && strcmp(operation, CRMD_ACTION_STATUS) != 0
2265         && strcmp(operation, CRMD_ACTION_NOTIFY) != 0) {
2266 
2267         /* stop any previous monitor operations before changing the resource state */
2268         stop_recurring = TRUE;
2269     }
2270 
2271     if (stop_recurring == TRUE) {
2272         guint removed = 0;
2273         struct stop_recurring_action_s data;
2274 
2275         data.rsc = rsc;
2276         data.lrm_state = lrm_state;
2277         removed = g_hash_table_foreach_remove(
2278             lrm_state->pending_ops, stop_recurring_action_by_rsc, &data);
2279 
2280         if (removed) {
2281             crm_debug("Stopped %u recurring operation%s in preparation for "
2282                       PCMK__OP_FMT, removed, pcmk__plural_s(removed),
2283                       rsc->id, operation, op->interval_ms);
2284         }
2285     }
2286 
2287     /* now do the op */
2288     crm_notice("Requesting local execution of %s operation for %s on %s "
2289                CRM_XS " transition_key=%s op_key=" PCMK__OP_FMT,
2290                crm_action_str(op->op_type, op->interval_ms), rsc->id, lrm_state->node_name,
2291                pcmk__s(transition, ""), rsc->id, operation, op->interval_ms);
2292 
2293     if (pcmk_is_set(fsa_input_register, R_SHUTDOWN)
2294         && pcmk__str_eq(operation, RSC_START, pcmk__str_casei)) {
2295 
2296         register_fsa_input(C_SHUTDOWN, I_SHUTDOWN, NULL);
2297         nack_reason = "Not attempting start due to shutdown in progress";
2298 
2299     } else if (fsa_state != S_NOT_DC
2300                && fsa_state != S_POLICY_ENGINE /* Recalculating */
2301                && fsa_state != S_TRANSITION_ENGINE
2302                && !pcmk__str_eq(operation, CRMD_ACTION_STOP, pcmk__str_casei)) {
2303         nack_reason = "Controller cannot attempt actions at this time";
2304     }
2305 
2306     if (nack_reason != NULL) {
2307         crm_notice("Discarding attempt to perform action %s on %s in state %s (shutdown=%s)",
2308                    operation, rsc->id, fsa_state2string(fsa_state),
2309                    pcmk__btoa(pcmk_is_set(fsa_input_register, R_SHUTDOWN)));
2310 
2311         lrmd__set_result(op, PCMK_OCF_UNKNOWN_ERROR, PCMK_EXEC_INVALID,
2312                          nack_reason);
2313         controld_ack_event_directly(NULL, NULL, rsc, op, rsc->id);
2314         lrmd_free_event(op);
2315         free(op_id);
2316         return;
2317     }
2318 
2319     record_pending_op(lrm_state->node_name, rsc, op);
2320 
2321     op_id = pcmk__op_key(rsc->id, op->op_type, op->interval_ms);
2322 
2323     if (op->interval_ms > 0) {
2324         /* cancel it so we can then restart it without conflict */
2325         cancel_op_key(lrm_state, rsc, op_id, FALSE);
2326     }
2327 
2328     if (op->params) {
2329         char *key = NULL;
2330         char *value = NULL;
2331         GHashTableIter iter;
2332 
2333         g_hash_table_iter_init(&iter, op->params);
2334         while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
2335             params = lrmd_key_value_add(params, key, value);
2336         }
2337     }
2338 
2339     rc = controld_execute_resource_agent(lrm_state, rsc->id, op->op_type,
2340                                          op->user_data, op->interval_ms,
2341                                          op->timeout, op->start_delay, params,
2342                                          &call_id);
2343     if (rc == pcmk_rc_ok) {
2344         /* record all operations so we can wait
2345          * for them to complete during shutdown
2346          */
2347         char *call_id_s = make_stop_id(rsc->id, call_id);
2348         active_op_t *pending = NULL;
2349 
2350         pending = calloc(1, sizeof(active_op_t));
2351         crm_trace("Recording pending op: %d - %s %s", call_id, op_id, call_id_s);
2352 
2353         pending->call_id = call_id;
2354         pending->interval_ms = op->interval_ms;
2355         pending->op_type = strdup(operation);
2356         pending->op_key = strdup(op_id);
2357         pending->rsc_id = strdup(rsc->id);
2358         pending->start_time = time(NULL);
2359         pcmk__str_update(&pending->user_data, op->user_data);
2360         if (crm_element_value_epoch(msg, XML_CONFIG_ATTR_SHUTDOWN_LOCK,
2361                                     &(pending->lock_time)) != pcmk_ok) {
2362             pending->lock_time = 0;
2363         }
2364         g_hash_table_replace(lrm_state->pending_ops, call_id_s, pending);
2365 
2366         if ((op->interval_ms > 0)
2367             && (op->start_delay > START_DELAY_THRESHOLD)) {
2368             int target_rc = PCMK_OCF_OK;
2369 
2370             crm_info("Faking confirmation of %s: execution postponed for over 5 minutes", op_id);
2371             decode_transition_key(op->user_data, NULL, NULL, NULL, &target_rc);
2372             lrmd__set_result(op, target_rc, PCMK_EXEC_DONE, NULL);
2373             controld_ack_event_directly(NULL, NULL, rsc, op, rsc->id);
2374         }
2375 
2376         pending->params = op->params;
2377         op->params = NULL;
2378 
2379     } else if (lrm_state_is_local(lrm_state)) {
2380         crm_err("Could not initiate %s action for resource %s locally: %s "
2381                 CRM_XS " rc=%d", operation, rsc->id, pcmk_rc_str(rc), rc);
2382         fake_op_status(lrm_state, op, PCMK_EXEC_NOT_CONNECTED,
2383                        PCMK_OCF_UNKNOWN_ERROR, pcmk_rc_str(rc));
2384         process_lrm_event(lrm_state, op, NULL, NULL);
2385         register_fsa_error(C_FSA_INTERNAL, I_FAIL, NULL);
2386 
2387     } else {
2388         crm_err("Could not initiate %s action for resource %s remotely on %s: "
2389                 "%s " CRM_XS " rc=%d",
2390                 operation, rsc->id, lrm_state->node_name, pcmk_rc_str(rc), rc);
2391         fake_op_status(lrm_state, op, PCMK_EXEC_NOT_CONNECTED,
2392                        PCMK_OCF_UNKNOWN_ERROR, pcmk_rc_str(rc));
2393         process_lrm_event(lrm_state, op, NULL, NULL);
2394     }
2395 
2396     free(op_id);
2397     lrmd_free_event(op);
2398 }
2399 
2400 int last_resource_update = 0;
2401 
2402 static void
2403 cib_rsc_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2404 {
2405     switch (rc) {
2406         case pcmk_ok:
2407         case -pcmk_err_diff_failed:
2408         case -pcmk_err_diff_resync:
2409             crm_trace("Resource update %d complete: rc=%d", call_id, rc);
2410             break;
2411         default:
2412             crm_warn("Resource update %d failed: (rc=%d) %s", call_id, rc, pcmk_strerror(rc));
2413     }
2414 
2415     if (call_id == last_resource_update) {
2416         last_resource_update = 0;
2417         trigger_fsa();
2418     }
2419 }
2420 
2421 /* Only successful stops, and probes that found the resource inactive, get locks
2422  * recorded in the history. This ensures the resource stays locked to the node
2423  * until it is active there again after the node comes back up.
2424  */
2425 static bool
2426 should_preserve_lock(lrmd_event_data_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
2427 {
2428     if (!controld_shutdown_lock_enabled) {
2429         return false;
2430     }
2431     if (!strcmp(op->op_type, RSC_STOP) && (op->rc == PCMK_OCF_OK)) {
2432         return true;
2433     }
2434     if (!strcmp(op->op_type, RSC_STATUS) && (op->rc == PCMK_OCF_NOT_RUNNING)) {
2435         return true;
2436     }
2437     return false;
2438 }
2439 
2440 static int
2441 do_update_resource(const char *node_name, lrmd_rsc_info_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
2442                    lrmd_event_data_t *op, time_t lock_time)
2443 {
2444 /*
2445   <status>
2446   <nodes_status id=uname>
2447   <lrm>
2448   <lrm_resources>
2449   <lrm_resource id=...>
2450   </...>
2451 */
2452     int rc = pcmk_ok;
2453     xmlNode *update, *iter = NULL;
2454     int call_opt = crmd_cib_smart_opt();
2455     const char *uuid = NULL;
2456 
2457     CRM_CHECK(op != NULL, return 0);
2458 
2459     iter = create_xml_node(iter, XML_CIB_TAG_STATUS);
2460     update = iter;
2461     iter = create_xml_node(iter, XML_CIB_TAG_STATE);
2462 
2463     if (pcmk__str_eq(node_name, fsa_our_uname, pcmk__str_casei)) {
2464         uuid = fsa_our_uuid;
2465 
2466     } else {
2467         /* remote nodes uuid and uname are equal */
2468         uuid = node_name;
2469         pcmk__xe_set_bool_attr(iter, XML_NODE_IS_REMOTE, true);
2470     }
2471 
2472     CRM_LOG_ASSERT(uuid != NULL);
2473     if(uuid == NULL) {
2474         rc = -EINVAL;
2475         goto done;
2476     }
2477 
2478     crm_xml_add(iter, XML_ATTR_UUID,  uuid);
2479     crm_xml_add(iter, XML_ATTR_UNAME, node_name);
2480     crm_xml_add(iter, XML_ATTR_ORIGIN, __func__);
2481 
2482     iter = create_xml_node(iter, XML_CIB_TAG_LRM);
2483     crm_xml_add(iter, XML_ATTR_ID, uuid);
2484 
2485     iter = create_xml_node(iter, XML_LRM_TAG_RESOURCES);
2486     iter = create_xml_node(iter, XML_LRM_TAG_RESOURCE);
2487     crm_xml_add(iter, XML_ATTR_ID, op->rsc_id);
2488 
2489     build_operation_update(iter, rsc, op, node_name, __func__);
2490 
2491     if (rsc) {
2492         const char *container = NULL;
2493 
2494         crm_xml_add(iter, XML_ATTR_TYPE, rsc->type);
2495         crm_xml_add(iter, XML_AGENT_ATTR_CLASS, rsc->standard);
2496         crm_xml_add(iter, XML_AGENT_ATTR_PROVIDER, rsc->provider);
2497         if (lock_time != 0) {
2498             /* Actions on a locked resource should either preserve the lock by
2499              * recording it with the action result, or clear it.
2500              */
2501             if (!should_preserve_lock(op)) {
2502                 lock_time = 0;
2503             }
2504             crm_xml_add_ll(iter, XML_CONFIG_ATTR_SHUTDOWN_LOCK,
2505                            (long long) lock_time);
2506         }
2507 
2508         if (op->params) {
2509             container = g_hash_table_lookup(op->params, CRM_META"_"XML_RSC_ATTR_CONTAINER);
2510         }
2511         if (container) {
2512             crm_trace("Resource %s is a part of container resource %s", op->rsc_id, container);
2513             crm_xml_add(iter, XML_RSC_ATTR_CONTAINER, container);
2514         }
2515 
2516     } else {
2517         crm_warn("Resource %s no longer exists in the executor", op->rsc_id);
2518         controld_ack_event_directly(NULL, NULL, rsc, op, op->rsc_id);
2519         goto cleanup;
2520     }
2521 
2522     crm_log_xml_trace(update, __func__);
2523 
2524     /* make it an asynchronous call and be done with it
2525      *
2526      * Best case:
2527      *   the resource state will be discovered during
2528      *   the next signup or election.
2529      *
2530      * Bad case:
2531      *   we are shutting down and there is no DC at the time,
2532      *   but then why were we shutting down then anyway?
2533      *   (probably because of an internal error)
2534      *
2535      * Worst case:
2536      *   we get shot for having resources "running" that really weren't
2537      *
2538      * the alternative however means blocking here for too long, which
2539      * isn't acceptable
2540      */
2541     fsa_cib_update(XML_CIB_TAG_STATUS, update, call_opt, rc, NULL);
2542 
2543     if (rc > 0) {
2544         last_resource_update = rc;
2545     }
2546   done:
2547     /* the return code is a call number, not an error code */
2548     crm_trace("Sent resource state update message: %d for %s=%u on %s",
2549               rc, op->op_type, op->interval_ms, op->rsc_id);
2550     fsa_register_cib_callback(rc, FALSE, NULL, cib_rsc_callback);
2551 
2552   cleanup:
2553     free_xml(update);
2554     return rc;
2555 }
2556 
2557 void
2558 do_lrm_event(long long action,
     /* [previous][next][first][last][top][bottom][index][help] */
2559              enum crmd_fsa_cause cause,
2560              enum crmd_fsa_state cur_state, enum crmd_fsa_input cur_input, fsa_data_t * msg_data)
2561 {
2562     CRM_CHECK(FALSE, return);
2563 }
2564 
2565 static char *
2566 unescape_newlines(const char *string)
     /* [previous][next][first][last][top][bottom][index][help] */
2567 {
2568     char *pch = NULL;
2569     char *ret = NULL;
2570     static const char *escaped_newline = "\\n";
2571 
2572     if (!string) {
2573         return NULL;
2574     }
2575 
2576     ret = strdup(string);
2577     pch = strstr(ret, escaped_newline);
2578     while (pch != NULL) {
2579         /* Replace newline escape pattern with actual newline (and a space so we
2580          * don't have to shuffle the rest of the buffer)
2581          */
2582         pch[0] = '\n';
2583         pch[1] = ' ';
2584         pch = strstr(pch, escaped_newline);
2585     }
2586 
2587     return ret;
2588 }
2589 
2590 static bool
2591 did_lrm_rsc_op_fail(lrm_state_t *lrm_state, const char * rsc_id,
     /* [previous][next][first][last][top][bottom][index][help] */
2592                     const char * op_type, guint interval_ms)
2593 {
2594     rsc_history_t *entry = NULL;
2595 
2596     CRM_CHECK(lrm_state != NULL, return FALSE);
2597     CRM_CHECK(rsc_id != NULL, return FALSE);
2598     CRM_CHECK(op_type != NULL, return FALSE);
2599 
2600     entry = g_hash_table_lookup(lrm_state->resource_history, rsc_id);
2601     if (entry == NULL || entry->failed == NULL) {
2602         return FALSE;
2603     }
2604 
2605     if (pcmk__str_eq(entry->failed->rsc_id, rsc_id, pcmk__str_none)
2606         && pcmk__str_eq(entry->failed->op_type, op_type, pcmk__str_casei)
2607         && entry->failed->interval_ms == interval_ms) {
2608         return TRUE;
2609     }
2610 
2611     return FALSE;
2612 }
2613 
2614 /*!
2615  * \internal
2616  * \brief Log the result of an executor action (actual or synthesized)
2617  *
2618  * \param[in] op         Executor action to log result for
2619  * \param[in] op_key     Operation key for action
2620  * \param[in] node_name  Name of node action was performed on, if known
2621  * \param[in] update_id  Call ID for CIB update (or 0 if none)
2622  * \param[in] confirmed  Whether to log that graph action was confirmed
2623  */
2624 static void
2625 log_executor_event(const lrmd_event_data_t *op, const char *op_key,
     /* [previous][next][first][last][top][bottom][index][help] */
2626                    const char *node_name, int update_id, gboolean confirmed)
2627 {
2628     int log_level = LOG_ERR;
2629     GString *str = g_string_sized_new(100); // reasonable starting size
2630 
2631     pcmk__g_strcat(str,
2632                    "Result of ", crm_action_str(op->op_type, op->interval_ms),
2633                    " operation for ", op->rsc_id, NULL);
2634 
2635     if (node_name != NULL) {
2636         pcmk__g_strcat(str, " on ", node_name, NULL);
2637     }
2638 
2639     switch (op->op_status) {
2640         case PCMK_EXEC_DONE:
2641             log_level = LOG_NOTICE;
2642             pcmk__g_strcat(str, ": ", services_ocf_exitcode_str(op->rc), NULL);
2643             break;
2644 
2645         case PCMK_EXEC_TIMEOUT:
2646             pcmk__g_strcat(str,
2647                            ": ", pcmk_exec_status_str(op->op_status), " after ",
2648                            pcmk__readable_interval(op->timeout), NULL);
2649             break;
2650 
2651         case PCMK_EXEC_CANCELLED:
2652             log_level = LOG_INFO;
2653             // Fall through
2654         default:
2655             pcmk__g_strcat(str, ": ", pcmk_exec_status_str(op->op_status),
2656                            NULL);
2657     }
2658 
2659     if ((op->exit_reason != NULL)
2660         && ((op->op_status != PCMK_EXEC_DONE) || (op->rc != PCMK_OCF_OK))) {
2661 
2662         pcmk__g_strcat(str, " (", op->exit_reason, ")", NULL);
2663     }
2664 
2665     g_string_append(str, " " CRM_XS);
2666     if (update_id != 0) {
2667         g_string_append_printf(str, " CIB update %d,", update_id);
2668     }
2669     g_string_append_printf(str, " graph action %sconfirmed; call=%d key=%s",
2670                            (confirmed? "" : "un"), op->call_id, op_key);
2671     if (op->op_status == PCMK_EXEC_DONE) {
2672         g_string_append_printf(str, " rc=%d", op->rc);
2673     }
2674 
2675     do_crm_log(log_level, "%s", str->str);
2676     g_string_free(str, TRUE);
2677 
2678     /* The services library has already logged the output at info or debug
2679      * level, so just raise to notice if it looks like a failure.
2680      */
2681     if ((op->output != NULL) && (op->rc != PCMK_OCF_OK)) {
2682         char *prefix = crm_strdup_printf(PCMK__OP_FMT "@%s output",
2683                                          op->rsc_id, op->op_type,
2684                                          op->interval_ms, node_name);
2685 
2686         crm_log_output(LOG_NOTICE, prefix, op->output);
2687         free(prefix);
2688     }
2689 }
2690 
2691 void
2692 process_lrm_event(lrm_state_t *lrm_state, lrmd_event_data_t *op,
     /* [previous][next][first][last][top][bottom][index][help] */
2693                   active_op_t *pending, const xmlNode *action_xml)
2694 {
2695     char *op_id = NULL;
2696     char *op_key = NULL;
2697 
2698     int update_id = 0;
2699     gboolean remove = FALSE;
2700     gboolean removed = FALSE;
2701     bool need_direct_ack = FALSE;
2702     lrmd_rsc_info_t *rsc = NULL;
2703     const char *node_name = NULL;
2704 
2705     CRM_CHECK(op != NULL, return);
2706     CRM_CHECK(op->rsc_id != NULL, return);
2707 
2708     // Remap new status codes for older DCs
2709     if (compare_version(fsa_our_dc_version, "3.2.0") < 0) {
2710         switch (op->op_status) {
2711             case PCMK_EXEC_NOT_CONNECTED:
2712                 lrmd__set_result(op, PCMK_OCF_CONNECTION_DIED,
2713                                  PCMK_EXEC_ERROR, op->exit_reason);
2714                 break;
2715             case PCMK_EXEC_INVALID:
2716                 lrmd__set_result(op, CRM_DIRECT_NACK_RC, PCMK_EXEC_ERROR,
2717                                  op->exit_reason);
2718                 break;
2719             default:
2720                 break;
2721         }
2722     }
2723 
2724     op_id = make_stop_id(op->rsc_id, op->call_id);
2725     op_key = pcmk__op_key(op->rsc_id, op->op_type, op->interval_ms);
2726 
2727     // Get resource info if available (from executor state or action XML)
2728     if (lrm_state) {
2729         rsc = lrm_state_get_rsc_info(lrm_state, op->rsc_id, 0);
2730     }
2731     if ((rsc == NULL) && action_xml) {
2732         xmlNode *xml = find_xml_node(action_xml, XML_CIB_TAG_RESOURCE, TRUE);
2733 
2734         const char *standard = crm_element_value(xml, XML_AGENT_ATTR_CLASS);
2735         const char *provider = crm_element_value(xml, XML_AGENT_ATTR_PROVIDER);
2736         const char *type = crm_element_value(xml, XML_ATTR_TYPE);
2737 
2738         if (standard && type) {
2739             crm_info("%s agent information not cached, using %s%s%s:%s from action XML",
2740                      op->rsc_id, standard,
2741                      (provider? ":" : ""), (provider? provider : ""), type);
2742             rsc = lrmd_new_rsc_info(op->rsc_id, standard, provider, type);
2743         } else {
2744             crm_err("Can't process %s result because %s agent information not cached or in XML",
2745                     op_key, op->rsc_id);
2746         }
2747     }
2748 
2749     // Get node name if available (from executor state or action XML)
2750     if (lrm_state) {
2751         node_name = lrm_state->node_name;
2752     } else if (action_xml) {
2753         node_name = crm_element_value(action_xml, XML_LRM_ATTR_TARGET);
2754     }
2755 
2756     if(pending == NULL) {
2757         remove = TRUE;
2758         if (lrm_state) {
2759             pending = g_hash_table_lookup(lrm_state->pending_ops, op_id);
2760         }
2761     }
2762 
2763     if (op->op_status == PCMK_EXEC_ERROR) {
2764         switch(op->rc) {
2765             case PCMK_OCF_NOT_RUNNING:
2766             case PCMK_OCF_RUNNING_PROMOTED:
2767             case PCMK_OCF_DEGRADED:
2768             case PCMK_OCF_DEGRADED_PROMOTED:
2769                 // Leave it to the TE/scheduler to decide if this is an error
2770                 op->op_status = PCMK_EXEC_DONE;
2771                 break;
2772             default:
2773                 /* Nothing to do */
2774                 break;
2775         }
2776     }
2777 
2778     if (op->op_status != PCMK_EXEC_CANCELLED) {
2779         /* We might not record the result, so directly acknowledge it to the
2780          * originator instead, so it doesn't time out waiting for the result
2781          * (especially important if part of a transition).
2782          */
2783         need_direct_ack = TRUE;
2784 
2785         if (controld_action_is_recordable(op->op_type)) {
2786             if (node_name && rsc) {
2787                 // We should record the result, and happily, we can
2788                 update_id = do_update_resource(node_name, rsc, op,
2789                                                pending? pending->lock_time : 0);
2790                 need_direct_ack = FALSE;
2791 
2792             } else if (op->rsc_deleted) {
2793                 /* We shouldn't record the result (likely the resource was
2794                  * refreshed, cleaned, or removed while this operation was
2795                  * in flight).
2796                  */
2797                 crm_notice("Not recording %s result in CIB because "
2798                            "resource information was removed since it was initiated",
2799                            op_key);
2800             } else {
2801                 /* This shouldn't be possible; the executor didn't consider the
2802                  * resource deleted, but we couldn't find resource or node
2803                  * information.
2804                  */
2805                 crm_err("Unable to record %s result in CIB: %s", op_key,
2806                         (node_name? "No resource information" : "No node name"));
2807             }
2808         }
2809 
2810     } else if (op->interval_ms == 0) {
2811         /* A non-recurring operation was cancelled. Most likely, the
2812          * never-initiated action was removed from the executor's pending
2813          * operations list upon resource removal.
2814          */
2815         need_direct_ack = TRUE;
2816 
2817     } else if (pending == NULL) {
2818         /* This recurring operation was cancelled, but was not pending. No
2819          * transition actions are waiting on it, nothing needs to be done.
2820          */
2821 
2822     } else if (op->user_data == NULL) {
2823         /* This recurring operation was cancelled and pending, but we don't
2824          * have a transition key. This should never happen.
2825          */
2826         crm_err("Recurring operation %s was cancelled without transition information",
2827                 op_key);
2828 
2829     } else if (pcmk_is_set(pending->flags, active_op_remove)) {
2830         /* This recurring operation was cancelled (by us) and pending, and we
2831          * have been waiting for it to finish.
2832          */
2833         if (lrm_state) {
2834             erase_lrm_history_by_op(op);
2835         }
2836 
2837         /* If the recurring operation had failed, the lrm_rsc_op is recorded as
2838          * "last_failure" which won't get erased from the cib given the logic on
2839          * purpose in erase_lrm_history_by_op(). So that the cancel action won't
2840          * have a chance to get confirmed by DC with process_op_deletion().
2841          * Cluster transition would get stuck waiting for the remaining action
2842          * timer to time out.
2843          *
2844          * Directly acknowledge the cancel operation in this case.
2845          */
2846         if (did_lrm_rsc_op_fail(lrm_state, pending->rsc_id,
2847                                 pending->op_type, pending->interval_ms)) {
2848             need_direct_ack = TRUE;
2849         }
2850 
2851     } else if (op->rsc_deleted) {
2852         /* This recurring operation was cancelled (but not by us, and the
2853          * executor does not have resource information, likely due to resource
2854          * cleanup, refresh, or removal) and pending.
2855          */
2856         crm_debug("Recurring op %s was cancelled due to resource deletion",
2857                   op_key);
2858         need_direct_ack = TRUE;
2859 
2860     } else {
2861         /* This recurring operation was cancelled (but not by us, likely by the
2862          * executor before stopping the resource) and pending. We don't need to
2863          * do anything special.
2864          */
2865     }
2866 
2867     if (need_direct_ack) {
2868         controld_ack_event_directly(NULL, NULL, NULL, op, op->rsc_id);
2869     }
2870 
2871     if(remove == FALSE) {
2872         /* The caller will do this afterwards, but keep the logging consistent */
2873         removed = TRUE;
2874 
2875     } else if (lrm_state && ((op->interval_ms == 0)
2876                              || (op->op_status == PCMK_EXEC_CANCELLED))) {
2877 
2878         gboolean found = g_hash_table_remove(lrm_state->pending_ops, op_id);
2879 
2880         if (op->interval_ms != 0) {
2881             removed = TRUE;
2882         } else if (found) {
2883             removed = TRUE;
2884             crm_trace("Op %s (call=%d, stop-id=%s, remaining=%u): Confirmed",
2885                       op_key, op->call_id, op_id,
2886                       g_hash_table_size(lrm_state->pending_ops));
2887         }
2888     }
2889 
2890     log_executor_event(op, op_key, node_name, update_id, removed);
2891 
2892     if (lrm_state) {
2893         if (!pcmk__str_eq(op->op_type, RSC_METADATA, pcmk__str_casei)) {
2894             crmd_alert_resource_op(lrm_state->node_name, op);
2895         } else if (rsc && (op->rc == PCMK_OCF_OK)) {
2896             char *metadata = unescape_newlines(op->output);
2897 
2898             controld_cache_metadata(lrm_state->metadata_cache, rsc, metadata);
2899             free(metadata);
2900         }
2901     }
2902 
2903     if (op->rsc_deleted) {
2904         crm_info("Deletion of resource '%s' complete after %s", op->rsc_id, op_key);
2905         if (lrm_state) {
2906             delete_rsc_entry(lrm_state, NULL, op->rsc_id, NULL, pcmk_ok, NULL);
2907         }
2908     }
2909 
2910     /* If a shutdown was escalated while operations were pending,
2911      * then the FSA will be stalled right now... allow it to continue
2912      */
2913     mainloop_set_trigger(fsa_source);
2914     if (lrm_state && rsc) {
2915         update_history_cache(lrm_state, rsc, op);
2916     }
2917 
2918     lrmd_free_rsc_info(rsc);
2919     free(op_key);
2920     free(op_id);
2921 }

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