root/daemons/execd/execd_commands.c

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

DEFINITIONS

This source file includes following definitions.
  1. time_is_set
  2. get_current_time
  3. time_diff_ms
  4. cmd_original_times
  5. action_matches
  6. log_finished
  7. log_execute
  8. normalize_action_name
  9. build_rsc_from_xml
  10. create_lrmd_cmd
  11. stop_recurring_timer
  12. free_lrmd_cmd
  13. stonith_recurring_op_helper
  14. start_recurring_timer
  15. start_delay_helper
  16. find_duplicate_action
  17. merge_recurring_duplicate
  18. schedule_lrmd_cmd
  19. create_lrmd_reply
  20. send_client_notify
  21. send_cmd_complete_notify
  22. send_generic_notify
  23. cmd_reset
  24. cmd_finalize
  25. notify_one_client
  26. notify_of_new_client
  27. client_disconnect_cleanup
  28. action_complete
  29. stonith_action_complete
  30. lrmd_stonith_callback
  31. stonith_connection_failed
  32. execd_stonith_start
  33. execd_stonith_stop
  34. execd_stonith_monitor
  35. execute_stonith_action
  36. execute_nonstonith_action
  37. execute_resource_action
  38. free_rsc
  39. process_lrmd_signon
  40. process_lrmd_rsc_register
  41. process_lrmd_get_rsc_info
  42. process_lrmd_rsc_unregister
  43. process_lrmd_rsc_exec
  44. cancel_op
  45. cancel_all_recurring
  46. process_lrmd_rsc_cancel
  47. add_recurring_op_xml
  48. process_lrmd_get_recurring
  49. process_lrmd_message

   1 /*
   2  * Copyright 2012-2023 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 Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 #include <crm/fencing/internal.h>
  12 
  13 #include <glib.h>
  14 
  15 // Check whether we have a high-resolution monotonic clock
  16 #undef PCMK__TIME_USE_CGT
  17 #if HAVE_DECL_CLOCK_MONOTONIC && defined(CLOCK_MONOTONIC)
  18 #  define PCMK__TIME_USE_CGT
  19 #  include <time.h>  /* clock_gettime */
  20 #endif
  21 
  22 #include <unistd.h>
  23 
  24 #include <crm/crm.h>
  25 #include <crm/fencing/internal.h>
  26 #include <crm/services.h>
  27 #include <crm/services_internal.h>
  28 #include <crm/common/mainloop.h>
  29 #include <crm/common/ipc.h>
  30 #include <crm/common/ipc_internal.h>
  31 #include <crm/msg_xml.h>
  32 
  33 #include "pacemaker-execd.h"
  34 
  35 GHashTable *rsc_list = NULL;
  36 
  37 typedef struct lrmd_cmd_s {
  38     int timeout;
  39     guint interval_ms;
  40     int start_delay;
  41     int timeout_orig;
  42 
  43     int call_id;
  44 
  45     int call_opts;
  46     /* Timer ids, must be removed on cmd destruction. */
  47     int delay_id;
  48     int stonith_recurring_id;
  49 
  50     int rsc_deleted;
  51 
  52     int service_flags;
  53 
  54     char *client_id;
  55     char *origin;
  56     char *rsc_id;
  57     char *action;
  58     char *real_action;
  59     char *userdata_str;
  60 
  61     pcmk__action_result_t result;
  62 
  63     /* We can track operation queue time and run time, to be saved with the CIB
  64      * resource history (and displayed in cluster status). We need
  65      * high-resolution monotonic time for this purpose, so we use
  66      * clock_gettime(CLOCK_MONOTONIC, ...) (if available, otherwise this feature
  67      * is disabled).
  68      *
  69      * However, we also need epoch timestamps for recording the time the command
  70      * last ran and the time its return value last changed, for use in time
  71      * displays (as opposed to interval calculations). We keep time_t values for
  72      * this purpose.
  73      *
  74      * The last run time is used for both purposes, so we keep redundant
  75      * monotonic and epoch values for this. Technically the two could represent
  76      * different times, but since time_t has only second resolution and the
  77      * values are used for distinct purposes, that is not significant.
  78      */
  79 #ifdef PCMK__TIME_USE_CGT
  80     /* Recurring and systemd operations may involve more than one executor
  81      * command per operation, so they need info about the original and the most
  82      * recent.
  83      */
  84     struct timespec t_first_run;    // When op first ran
  85     struct timespec t_run;          // When op most recently ran
  86     struct timespec t_first_queue;  // When op was first queued
  87     struct timespec t_queue;        // When op was most recently queued
  88 #endif
  89     time_t epoch_last_run;          // Epoch timestamp of when op last ran
  90     time_t epoch_rcchange;          // Epoch timestamp of when rc last changed
  91 
  92     bool first_notify_sent;
  93     int last_notify_rc;
  94     int last_notify_op_status;
  95     int last_pid;
  96 
  97     GHashTable *params;
  98 } lrmd_cmd_t;
  99 
 100 static void cmd_finalize(lrmd_cmd_t * cmd, lrmd_rsc_t * rsc);
 101 static gboolean execute_resource_action(gpointer user_data);
 102 static void cancel_all_recurring(lrmd_rsc_t * rsc, const char *client_id);
 103 
 104 #ifdef PCMK__TIME_USE_CGT
 105 
 106 /*!
 107  * \internal
 108  * \brief Check whether a struct timespec has been set
 109  *
 110  * \param[in] timespec  Time to check
 111  *
 112  * \return true if timespec has been set (i.e. is nonzero), false otherwise
 113  */
 114 static inline bool
 115 time_is_set(const struct timespec *timespec)
     /* [previous][next][first][last][top][bottom][index][help] */
 116 {
 117     return (timespec != NULL) &&
 118            ((timespec->tv_sec != 0) || (timespec->tv_nsec != 0));
 119 }
 120 
 121 /*
 122  * \internal
 123  * \brief Set a timespec (and its original if unset) to the current time
 124  *
 125  * \param[out] t_current  Where to store current time
 126  * \param[out] t_orig     Where to copy t_current if unset
 127  */
 128 static void
 129 get_current_time(struct timespec *t_current, struct timespec *t_orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131     clock_gettime(CLOCK_MONOTONIC, t_current);
 132     if ((t_orig != NULL) && !time_is_set(t_orig)) {
 133         *t_orig = *t_current;
 134     }
 135 }
 136 
 137 /*!
 138  * \internal
 139  * \brief Return difference between two times in milliseconds
 140  *
 141  * \param[in] now  More recent time (or NULL to use current time)
 142  * \param[in] old  Earlier time
 143  *
 144  * \return milliseconds difference (or 0 if old is NULL or unset)
 145  *
 146  * \note Can overflow on 32bit machines when the differences is around
 147  *       24 days or more.
 148  */
 149 static int
 150 time_diff_ms(const struct timespec *now, const struct timespec *old)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152     int diff_ms = 0;
 153 
 154     if (time_is_set(old)) {
 155         struct timespec local_now = { 0, };
 156 
 157         if (now == NULL) {
 158             clock_gettime(CLOCK_MONOTONIC, &local_now);
 159             now = &local_now;
 160         }
 161         diff_ms = (now->tv_sec - old->tv_sec) * 1000
 162                   + (now->tv_nsec - old->tv_nsec) / 1000000;
 163     }
 164     return diff_ms;
 165 }
 166 
 167 /*!
 168  * \internal
 169  * \brief Reset a command's operation times to their original values.
 170  *
 171  * Reset a command's run and queued timestamps to the timestamps of the original
 172  * command, so we report the entire time since then and not just the time since
 173  * the most recent command (for recurring and systemd operations).
 174  *
 175  * \param[in,out] cmd  Executor command object to reset
 176  *
 177  * \note It's not obvious what the queued time should be for a systemd
 178  *       start/stop operation, which might go like this:
 179  *         initial command queued 5ms, runs 3s
 180  *         monitor command queued 10ms, runs 10s
 181  *         monitor command queued 10ms, runs 10s
 182  *       Is the queued time for that operation 5ms, 10ms or 25ms? The current
 183  *       implementation will report 5ms. If it's 25ms, then we need to
 184  *       subtract 20ms from the total exec time so as not to count it twice.
 185  *       We can implement that later if it matters to anyone ...
 186  */
 187 static void
 188 cmd_original_times(lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190     cmd->t_run = cmd->t_first_run;
 191     cmd->t_queue = cmd->t_first_queue;
 192 }
 193 #endif
 194 
 195 static inline bool
 196 action_matches(const lrmd_cmd_t *cmd, const char *action, guint interval_ms)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198     return (cmd->interval_ms == interval_ms)
 199            && pcmk__str_eq(cmd->action, action, pcmk__str_casei);
 200 }
 201 
 202 /*!
 203  * \internal
 204  * \brief Log the result of an asynchronous command
 205  *
 206  * \param[in] cmd            Command to log result for
 207  * \param[in] exec_time_ms   Execution time in milliseconds, if known
 208  * \param[in] queue_time_ms  Queue time in milliseconds, if known
 209  */
 210 static void
 211 log_finished(const lrmd_cmd_t *cmd, int exec_time_ms, int queue_time_ms)
     /* [previous][next][first][last][top][bottom][index][help] */
 212 {
 213     int log_level = LOG_INFO;
 214     GString *str = g_string_sized_new(100); // reasonable starting size
 215 
 216     if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR, pcmk__str_casei)) {
 217         log_level = LOG_DEBUG;
 218     }
 219 
 220     g_string_append_printf(str, "%s %s (call %d",
 221                            cmd->rsc_id, cmd->action, cmd->call_id);
 222     if (cmd->last_pid != 0) {
 223         g_string_append_printf(str, ", PID %d", cmd->last_pid);
 224     }
 225     if (cmd->result.execution_status == PCMK_EXEC_DONE) {
 226         g_string_append_printf(str, ") exited with status %d",
 227                                cmd->result.exit_status);
 228     } else {
 229         pcmk__g_strcat(str, ") could not be executed: ",
 230                        pcmk_exec_status_str(cmd->result.execution_status),
 231                        NULL);
 232     }
 233     if (cmd->result.exit_reason != NULL) {
 234         pcmk__g_strcat(str, " (", cmd->result.exit_reason, ")", NULL);
 235     }
 236 
 237 #ifdef PCMK__TIME_USE_CGT
 238     pcmk__g_strcat(str, " (execution time ",
 239                    pcmk__readable_interval(exec_time_ms), NULL);
 240     if (queue_time_ms > 0) {
 241         pcmk__g_strcat(str, " after being queued ",
 242                        pcmk__readable_interval(queue_time_ms), NULL);
 243     }
 244     g_string_append_c(str, ')');
 245 #endif
 246 
 247     do_crm_log(log_level, "%s", str->str);
 248     g_string_free(str, TRUE);
 249 }
 250 
 251 static void
 252 log_execute(lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 253 {
 254     int log_level = LOG_INFO;
 255 
 256     if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR, pcmk__str_casei)) {
 257         log_level = LOG_DEBUG;
 258     }
 259 
 260     do_crm_log(log_level, "executing - rsc:%s action:%s call_id:%d",
 261                cmd->rsc_id, cmd->action, cmd->call_id);
 262 }
 263 
 264 static const char *
 265 normalize_action_name(lrmd_rsc_t * rsc, const char *action)
     /* [previous][next][first][last][top][bottom][index][help] */
 266 {
 267     if (pcmk__str_eq(action, PCMK_ACTION_MONITOR, pcmk__str_casei) &&
 268         pcmk_is_set(pcmk_get_ra_caps(rsc->class), pcmk_ra_cap_status)) {
 269         return PCMK_ACTION_STATUS;
 270     }
 271     return action;
 272 }
 273 
 274 static lrmd_rsc_t *
 275 build_rsc_from_xml(xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
 276 {
 277     xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, msg, LOG_ERR);
 278     lrmd_rsc_t *rsc = NULL;
 279 
 280     rsc = calloc(1, sizeof(lrmd_rsc_t));
 281 
 282     crm_element_value_int(msg, F_LRMD_CALLOPTS, &rsc->call_opts);
 283 
 284     rsc->rsc_id = crm_element_value_copy(rsc_xml, F_LRMD_RSC_ID);
 285     rsc->class = crm_element_value_copy(rsc_xml, F_LRMD_CLASS);
 286     rsc->provider = crm_element_value_copy(rsc_xml, F_LRMD_PROVIDER);
 287     rsc->type = crm_element_value_copy(rsc_xml, F_LRMD_TYPE);
 288     rsc->work = mainloop_add_trigger(G_PRIORITY_HIGH, execute_resource_action,
 289                                      rsc);
 290 
 291     // Initialize fence device probes (to return "not running")
 292     pcmk__set_result(&rsc->fence_probe_result, CRM_EX_ERROR,
 293                      PCMK_EXEC_NO_FENCE_DEVICE, NULL);
 294     return rsc;
 295 }
 296 
 297 static lrmd_cmd_t *
 298 create_lrmd_cmd(xmlNode *msg, pcmk__client_t *client)
     /* [previous][next][first][last][top][bottom][index][help] */
 299 {
 300     int call_options = 0;
 301     xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, msg, LOG_ERR);
 302     lrmd_cmd_t *cmd = NULL;
 303 
 304     cmd = calloc(1, sizeof(lrmd_cmd_t));
 305 
 306     crm_element_value_int(msg, F_LRMD_CALLOPTS, &call_options);
 307     cmd->call_opts = call_options;
 308     cmd->client_id = strdup(client->id);
 309 
 310     crm_element_value_int(msg, F_LRMD_CALLID, &cmd->call_id);
 311     crm_element_value_ms(rsc_xml, F_LRMD_RSC_INTERVAL, &cmd->interval_ms);
 312     crm_element_value_int(rsc_xml, F_LRMD_TIMEOUT, &cmd->timeout);
 313     crm_element_value_int(rsc_xml, F_LRMD_RSC_START_DELAY, &cmd->start_delay);
 314     cmd->timeout_orig = cmd->timeout;
 315 
 316     cmd->origin = crm_element_value_copy(rsc_xml, F_LRMD_ORIGIN);
 317     cmd->action = crm_element_value_copy(rsc_xml, F_LRMD_RSC_ACTION);
 318     cmd->userdata_str = crm_element_value_copy(rsc_xml, F_LRMD_RSC_USERDATA_STR);
 319     cmd->rsc_id = crm_element_value_copy(rsc_xml, F_LRMD_RSC_ID);
 320 
 321     cmd->params = xml2list(rsc_xml);
 322 
 323     if (pcmk__str_eq(g_hash_table_lookup(cmd->params, "CRM_meta_on_fail"), "block", pcmk__str_casei)) {
 324         crm_debug("Setting flag to leave pid group on timeout and "
 325                   "only kill action pid for " PCMK__OP_FMT,
 326                   cmd->rsc_id, cmd->action, cmd->interval_ms);
 327         cmd->service_flags = pcmk__set_flags_as(__func__, __LINE__,
 328                                                 LOG_TRACE, "Action",
 329                                                 cmd->action, 0,
 330                                                 SVC_ACTION_LEAVE_GROUP,
 331                                                 "SVC_ACTION_LEAVE_GROUP");
 332     }
 333     return cmd;
 334 }
 335 
 336 static void
 337 stop_recurring_timer(lrmd_cmd_t *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339     if (cmd) {
 340         if (cmd->stonith_recurring_id) {
 341             g_source_remove(cmd->stonith_recurring_id);
 342         }
 343         cmd->stonith_recurring_id = 0;
 344     }
 345 }
 346 
 347 static void
 348 free_lrmd_cmd(lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 349 {
 350     stop_recurring_timer(cmd);
 351     if (cmd->delay_id) {
 352         g_source_remove(cmd->delay_id);
 353     }
 354     if (cmd->params) {
 355         g_hash_table_destroy(cmd->params);
 356     }
 357     pcmk__reset_result(&(cmd->result));
 358     free(cmd->origin);
 359     free(cmd->action);
 360     free(cmd->real_action);
 361     free(cmd->userdata_str);
 362     free(cmd->rsc_id);
 363     free(cmd->client_id);
 364     free(cmd);
 365 }
 366 
 367 static gboolean
 368 stonith_recurring_op_helper(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370     lrmd_cmd_t *cmd = data;
 371     lrmd_rsc_t *rsc;
 372 
 373     cmd->stonith_recurring_id = 0;
 374 
 375     if (!cmd->rsc_id) {
 376         return FALSE;
 377     }
 378 
 379     rsc = g_hash_table_lookup(rsc_list, cmd->rsc_id);
 380 
 381     CRM_ASSERT(rsc != NULL);
 382     /* take it out of recurring_ops list, and put it in the pending ops
 383      * to be executed */
 384     rsc->recurring_ops = g_list_remove(rsc->recurring_ops, cmd);
 385     rsc->pending_ops = g_list_append(rsc->pending_ops, cmd);
 386 #ifdef PCMK__TIME_USE_CGT
 387     get_current_time(&(cmd->t_queue), &(cmd->t_first_queue));
 388 #endif
 389     mainloop_set_trigger(rsc->work);
 390 
 391     return FALSE;
 392 }
 393 
 394 static inline void
 395 start_recurring_timer(lrmd_cmd_t *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 396 {
 397     if (cmd && (cmd->interval_ms > 0)) {
 398         cmd->stonith_recurring_id = g_timeout_add(cmd->interval_ms,
 399                                                   stonith_recurring_op_helper,
 400                                                   cmd);
 401     }
 402 }
 403 
 404 static gboolean
 405 start_delay_helper(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 406 {
 407     lrmd_cmd_t *cmd = data;
 408     lrmd_rsc_t *rsc = NULL;
 409 
 410     cmd->delay_id = 0;
 411     rsc = cmd->rsc_id ? g_hash_table_lookup(rsc_list, cmd->rsc_id) : NULL;
 412 
 413     if (rsc) {
 414         mainloop_set_trigger(rsc->work);
 415     }
 416 
 417     return FALSE;
 418 }
 419 
 420 /*!
 421  * \internal
 422  * \brief Check whether a list already contains the equivalent of a given action
 423  *
 424  * \param[in] action_list  List to search
 425  * \param[in] cmd          Action to search for
 426  */
 427 static lrmd_cmd_t *
 428 find_duplicate_action(const GList *action_list, const lrmd_cmd_t *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430     for (const GList *item = action_list; item != NULL; item = item->next) {
 431         lrmd_cmd_t *dup = item->data;
 432 
 433         if (action_matches(cmd, dup->action, dup->interval_ms)) {
 434             return dup;
 435         }
 436     }
 437     return NULL;
 438 }
 439 
 440 static bool
 441 merge_recurring_duplicate(lrmd_rsc_t * rsc, lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 442 {
 443     lrmd_cmd_t * dup = NULL;
 444     bool dup_pending = true;
 445 
 446     if (cmd->interval_ms == 0) {
 447         return false;
 448     }
 449 
 450     // Search for a duplicate of this action (in-flight or not)
 451     dup = find_duplicate_action(rsc->pending_ops, cmd);
 452     if (dup == NULL) {
 453         dup_pending = false;
 454         dup = find_duplicate_action(rsc->recurring_ops, cmd);
 455         if (dup == NULL) {
 456             return false;
 457         }
 458     }
 459 
 460     /* Do not merge fencing monitors marked for cancellation, so we can reply to
 461      * the cancellation separately.
 462      */
 463     if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
 464                      pcmk__str_casei)
 465         && (dup->result.execution_status == PCMK_EXEC_CANCELLED)) {
 466         return false;
 467     }
 468 
 469     /* This should not occur. If it does, we need to investigate how something
 470      * like this is possible in the controller.
 471      */
 472     crm_warn("Duplicate recurring op entry detected (" PCMK__OP_FMT
 473              "), merging with previous op entry",
 474              rsc->rsc_id, normalize_action_name(rsc, dup->action),
 475              dup->interval_ms);
 476 
 477     // Merge new action's call ID and user data into existing action
 478     dup->first_notify_sent = false;
 479     free(dup->userdata_str);
 480     dup->userdata_str = cmd->userdata_str;
 481     cmd->userdata_str = NULL;
 482     dup->call_id = cmd->call_id;
 483     free_lrmd_cmd(cmd);
 484     cmd = NULL;
 485 
 486     /* If dup is not pending, that means it has already executed at least once
 487      * and is waiting in the interval. In that case, stop waiting and initiate
 488      * a new instance now.
 489      */
 490     if (!dup_pending) {
 491         if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
 492                          pcmk__str_casei)) {
 493             stop_recurring_timer(dup);
 494             stonith_recurring_op_helper(dup);
 495         } else {
 496             services_action_kick(rsc->rsc_id,
 497                                  normalize_action_name(rsc, dup->action),
 498                                  dup->interval_ms);
 499         }
 500     }
 501     return true;
 502 }
 503 
 504 static void
 505 schedule_lrmd_cmd(lrmd_rsc_t * rsc, lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 506 {
 507     CRM_CHECK(cmd != NULL, return);
 508     CRM_CHECK(rsc != NULL, return);
 509 
 510     crm_trace("Scheduling %s on %s", cmd->action, rsc->rsc_id);
 511 
 512     if (merge_recurring_duplicate(rsc, cmd)) {
 513         // Equivalent of cmd has already been scheduled
 514         return;
 515     }
 516 
 517     /* The controller expects the executor to automatically cancel
 518      * recurring operations before a resource stops.
 519      */
 520     if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP, pcmk__str_casei)) {
 521         cancel_all_recurring(rsc, NULL);
 522     }
 523 
 524     rsc->pending_ops = g_list_append(rsc->pending_ops, cmd);
 525 #ifdef PCMK__TIME_USE_CGT
 526     get_current_time(&(cmd->t_queue), &(cmd->t_first_queue));
 527 #endif
 528     mainloop_set_trigger(rsc->work);
 529 
 530     if (cmd->start_delay) {
 531         cmd->delay_id = g_timeout_add(cmd->start_delay, start_delay_helper, cmd);
 532     }
 533 }
 534 
 535 static xmlNode *
 536 create_lrmd_reply(const char *origin, int rc, int call_id)
     /* [previous][next][first][last][top][bottom][index][help] */
 537 {
 538     xmlNode *reply = create_xml_node(NULL, T_LRMD_REPLY);
 539 
 540     crm_xml_add(reply, F_LRMD_ORIGIN, origin);
 541     crm_xml_add_int(reply, F_LRMD_RC, rc);
 542     crm_xml_add_int(reply, F_LRMD_CALLID, call_id);
 543     return reply;
 544 }
 545 
 546 static void
 547 send_client_notify(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549     xmlNode *update_msg = user_data;
 550     pcmk__client_t *client = value;
 551     int rc;
 552     int log_level = LOG_WARNING;
 553     const char *msg = NULL;
 554 
 555     CRM_CHECK(client != NULL, return);
 556     if (client->name == NULL) {
 557         crm_trace("Skipping notification to client without name");
 558         return;
 559     }
 560     if (pcmk_is_set(client->flags, pcmk__client_to_proxy)) {
 561         /* We only want to notify clients of the executor IPC API. If we are
 562          * running as Pacemaker Remote, we may have clients proxied to other
 563          * IPC services in the cluster, so skip those.
 564          */
 565         crm_trace("Skipping executor API notification to client %s",
 566                   pcmk__client_name(client));
 567         return;
 568     }
 569 
 570     rc = lrmd_server_send_notify(client, update_msg);
 571     if (rc == pcmk_rc_ok) {
 572         return;
 573     }
 574 
 575     switch (rc) {
 576         case ENOTCONN:
 577         case EPIPE: // Client exited without waiting for notification
 578             log_level = LOG_INFO;
 579             msg = "Disconnected";
 580             break;
 581 
 582         default:
 583             msg = pcmk_rc_str(rc);
 584             break;
 585     }
 586     do_crm_log(log_level, "Could not notify client %s: %s " CRM_XS " rc=%d",
 587                pcmk__client_name(client), msg, rc);
 588 }
 589 
 590 static void
 591 send_cmd_complete_notify(lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 592 {
 593     xmlNode *notify = NULL;
 594     int exec_time = 0;
 595     int queue_time = 0;
 596 
 597 #ifdef PCMK__TIME_USE_CGT
 598     exec_time = time_diff_ms(NULL, &(cmd->t_run));
 599     queue_time = time_diff_ms(&cmd->t_run, &(cmd->t_queue));
 600 #endif
 601     log_finished(cmd, exec_time, queue_time);
 602 
 603     /* If the originator requested to be notified only for changes in recurring
 604      * operation results, skip the notification if the result hasn't changed.
 605      */
 606     if (cmd->first_notify_sent
 607         && pcmk_is_set(cmd->call_opts, lrmd_opt_notify_changes_only)
 608         && (cmd->last_notify_rc == cmd->result.exit_status)
 609         && (cmd->last_notify_op_status == cmd->result.execution_status)) {
 610         return;
 611     }
 612 
 613     cmd->first_notify_sent = true;
 614     cmd->last_notify_rc = cmd->result.exit_status;
 615     cmd->last_notify_op_status = cmd->result.execution_status;
 616 
 617     notify = create_xml_node(NULL, T_LRMD_NOTIFY);
 618 
 619     crm_xml_add(notify, F_LRMD_ORIGIN, __func__);
 620     crm_xml_add_int(notify, F_LRMD_TIMEOUT, cmd->timeout);
 621     crm_xml_add_ms(notify, F_LRMD_RSC_INTERVAL, cmd->interval_ms);
 622     crm_xml_add_int(notify, F_LRMD_RSC_START_DELAY, cmd->start_delay);
 623     crm_xml_add_int(notify, F_LRMD_EXEC_RC, cmd->result.exit_status);
 624     crm_xml_add_int(notify, F_LRMD_OP_STATUS, cmd->result.execution_status);
 625     crm_xml_add_int(notify, F_LRMD_CALLID, cmd->call_id);
 626     crm_xml_add_int(notify, F_LRMD_RSC_DELETED, cmd->rsc_deleted);
 627 
 628     crm_xml_add_ll(notify, F_LRMD_RSC_RUN_TIME,
 629                    (long long) cmd->epoch_last_run);
 630     crm_xml_add_ll(notify, F_LRMD_RSC_RCCHANGE_TIME,
 631                    (long long) cmd->epoch_rcchange);
 632 #ifdef PCMK__TIME_USE_CGT
 633     crm_xml_add_int(notify, F_LRMD_RSC_EXEC_TIME, exec_time);
 634     crm_xml_add_int(notify, F_LRMD_RSC_QUEUE_TIME, queue_time);
 635 #endif
 636 
 637     crm_xml_add(notify, F_LRMD_OPERATION, LRMD_OP_RSC_EXEC);
 638     crm_xml_add(notify, F_LRMD_RSC_ID, cmd->rsc_id);
 639     if(cmd->real_action) {
 640         crm_xml_add(notify, F_LRMD_RSC_ACTION, cmd->real_action);
 641     } else {
 642         crm_xml_add(notify, F_LRMD_RSC_ACTION, cmd->action);
 643     }
 644     crm_xml_add(notify, F_LRMD_RSC_USERDATA_STR, cmd->userdata_str);
 645     crm_xml_add(notify, F_LRMD_RSC_EXIT_REASON, cmd->result.exit_reason);
 646 
 647     if (cmd->result.action_stderr != NULL) {
 648         crm_xml_add(notify, F_LRMD_RSC_OUTPUT, cmd->result.action_stderr);
 649 
 650     } else if (cmd->result.action_stdout != NULL) {
 651         crm_xml_add(notify, F_LRMD_RSC_OUTPUT, cmd->result.action_stdout);
 652     }
 653 
 654     if (cmd->params) {
 655         char *key = NULL;
 656         char *value = NULL;
 657         GHashTableIter iter;
 658 
 659         xmlNode *args = create_xml_node(notify, XML_TAG_ATTRS);
 660 
 661         g_hash_table_iter_init(&iter, cmd->params);
 662         while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
 663             hash2smartfield((gpointer) key, (gpointer) value, args);
 664         }
 665     }
 666     if ((cmd->client_id != NULL)
 667         && pcmk_is_set(cmd->call_opts, lrmd_opt_notify_orig_only)) {
 668 
 669         pcmk__client_t *client = pcmk__find_client_by_id(cmd->client_id);
 670 
 671         if (client != NULL) {
 672             send_client_notify(client->id, client, notify);
 673         }
 674     } else {
 675         pcmk__foreach_ipc_client(send_client_notify, notify);
 676     }
 677 
 678     free_xml(notify);
 679 }
 680 
 681 static void
 682 send_generic_notify(int rc, xmlNode * request)
     /* [previous][next][first][last][top][bottom][index][help] */
 683 {
 684     if (pcmk__ipc_client_count() != 0) {
 685         int call_id = 0;
 686         xmlNode *notify = NULL;
 687         xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
 688         const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
 689         const char *op = crm_element_value(request, F_LRMD_OPERATION);
 690 
 691         crm_element_value_int(request, F_LRMD_CALLID, &call_id);
 692 
 693         notify = create_xml_node(NULL, T_LRMD_NOTIFY);
 694         crm_xml_add(notify, F_LRMD_ORIGIN, __func__);
 695         crm_xml_add_int(notify, F_LRMD_RC, rc);
 696         crm_xml_add_int(notify, F_LRMD_CALLID, call_id);
 697         crm_xml_add(notify, F_LRMD_OPERATION, op);
 698         crm_xml_add(notify, F_LRMD_RSC_ID, rsc_id);
 699 
 700         pcmk__foreach_ipc_client(send_client_notify, notify);
 701 
 702         free_xml(notify);
 703     }
 704 }
 705 
 706 static void
 707 cmd_reset(lrmd_cmd_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 708 {
 709     cmd->last_pid = 0;
 710 #ifdef PCMK__TIME_USE_CGT
 711     memset(&cmd->t_run, 0, sizeof(cmd->t_run));
 712     memset(&cmd->t_queue, 0, sizeof(cmd->t_queue));
 713 #endif
 714     cmd->epoch_last_run = 0;
 715 
 716     pcmk__reset_result(&(cmd->result));
 717     cmd->result.execution_status = PCMK_EXEC_DONE;
 718 }
 719 
 720 static void
 721 cmd_finalize(lrmd_cmd_t * cmd, lrmd_rsc_t * rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 722 {
 723     crm_trace("Resource operation rsc:%s action:%s completed (%p %p)", cmd->rsc_id, cmd->action,
 724               rsc ? rsc->active : NULL, cmd);
 725 
 726     if (rsc && (rsc->active == cmd)) {
 727         rsc->active = NULL;
 728         mainloop_set_trigger(rsc->work);
 729     }
 730 
 731     if (!rsc) {
 732         cmd->rsc_deleted = 1;
 733     }
 734 
 735     /* reset original timeout so client notification has correct information */
 736     cmd->timeout = cmd->timeout_orig;
 737 
 738     send_cmd_complete_notify(cmd);
 739 
 740     if ((cmd->interval_ms != 0)
 741         && (cmd->result.execution_status == PCMK_EXEC_CANCELLED)) {
 742 
 743         if (rsc) {
 744             rsc->recurring_ops = g_list_remove(rsc->recurring_ops, cmd);
 745             rsc->pending_ops = g_list_remove(rsc->pending_ops, cmd);
 746         }
 747         free_lrmd_cmd(cmd);
 748     } else if (cmd->interval_ms == 0) {
 749         if (rsc) {
 750             rsc->pending_ops = g_list_remove(rsc->pending_ops, cmd);
 751         }
 752         free_lrmd_cmd(cmd);
 753     } else {
 754         /* Clear all the values pertaining just to the last iteration of a recurring op. */
 755         cmd_reset(cmd);
 756     }
 757 }
 758 
 759 struct notify_new_client_data {
 760     xmlNode *notify;
 761     pcmk__client_t *new_client;
 762 };
 763 
 764 static void
 765 notify_one_client(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 766 {
 767     pcmk__client_t *client = value;
 768     struct notify_new_client_data *data = user_data;
 769 
 770     if (!pcmk__str_eq(client->id, data->new_client->id, pcmk__str_casei)) {
 771         send_client_notify(key, (gpointer) client, (gpointer) data->notify);
 772     }
 773 }
 774 
 775 void
 776 notify_of_new_client(pcmk__client_t *new_client)
     /* [previous][next][first][last][top][bottom][index][help] */
 777 {
 778     struct notify_new_client_data data;
 779 
 780     data.new_client = new_client;
 781     data.notify = create_xml_node(NULL, T_LRMD_NOTIFY);
 782     crm_xml_add(data.notify, F_LRMD_ORIGIN, __func__);
 783     crm_xml_add(data.notify, F_LRMD_OPERATION, LRMD_OP_NEW_CLIENT);
 784     pcmk__foreach_ipc_client(notify_one_client, &data);
 785     free_xml(data.notify);
 786 }
 787 
 788 void
 789 client_disconnect_cleanup(const char *client_id)
     /* [previous][next][first][last][top][bottom][index][help] */
 790 {
 791     GHashTableIter iter;
 792     lrmd_rsc_t *rsc = NULL;
 793     char *key = NULL;
 794 
 795     g_hash_table_iter_init(&iter, rsc_list);
 796     while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & rsc)) {
 797         if (pcmk_all_flags_set(rsc->call_opts, lrmd_opt_drop_recurring)) {
 798             /* This client is disconnecting, drop any recurring operations
 799              * it may have initiated on the resource */
 800             cancel_all_recurring(rsc, client_id);
 801         }
 802     }
 803 }
 804 
 805 static void
 806 action_complete(svc_action_t * action)
     /* [previous][next][first][last][top][bottom][index][help] */
 807 {
 808     lrmd_rsc_t *rsc;
 809     lrmd_cmd_t *cmd = action->cb_data;
 810     enum ocf_exitcode code;
 811 
 812 #ifdef PCMK__TIME_USE_CGT
 813     const char *rclass = NULL;
 814     bool goagain = false;
 815 #endif
 816 
 817     if (!cmd) {
 818         crm_err("Completed executor action (%s) does not match any known operations",
 819                 action->id);
 820         return;
 821     }
 822 
 823 #ifdef PCMK__TIME_USE_CGT
 824     if (cmd->result.exit_status != action->rc) {
 825         cmd->epoch_rcchange = time(NULL);
 826     }
 827 #endif
 828 
 829     cmd->last_pid = action->pid;
 830 
 831     // Cast variable instead of function return to keep compilers happy
 832     code = services_result2ocf(action->standard, cmd->action, action->rc);
 833     pcmk__set_result(&(cmd->result), (int) code,
 834                      action->status, services__exit_reason(action));
 835 
 836     rsc = cmd->rsc_id ? g_hash_table_lookup(rsc_list, cmd->rsc_id) : NULL;
 837 
 838 #ifdef PCMK__TIME_USE_CGT
 839     if (rsc && pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_SERVICE, pcmk__str_casei)) {
 840         rclass = resources_find_service_class(rsc->type);
 841     } else if(rsc) {
 842         rclass = rsc->class;
 843     }
 844 
 845     if (pcmk__str_eq(rclass, PCMK_RESOURCE_CLASS_SYSTEMD, pcmk__str_casei)) {
 846         if (pcmk__result_ok(&(cmd->result))
 847             && pcmk__strcase_any_of(cmd->action, PCMK_ACTION_START,
 848                                     PCMK_ACTION_STOP, NULL)) {
 849             /* systemd returns from start and stop actions after the action
 850              * begins, not after it completes. We have to jump through a few
 851              * hoops so that we don't report 'complete' to the rest of pacemaker
 852              * until it's actually done.
 853              */
 854             goagain = true;
 855             cmd->real_action = cmd->action;
 856             cmd->action = strdup(PCMK_ACTION_MONITOR);
 857 
 858         } else if (cmd->real_action != NULL) {
 859             // This is follow-up monitor to check whether start/stop completed
 860             if (cmd->result.execution_status == PCMK_EXEC_PENDING) {
 861                 goagain = true;
 862 
 863             } else if (pcmk__result_ok(&(cmd->result))
 864                        && pcmk__str_eq(cmd->real_action, PCMK_ACTION_STOP,
 865                                        pcmk__str_casei)) {
 866                 goagain = true;
 867 
 868             } else {
 869                 int time_sum = time_diff_ms(NULL, &(cmd->t_first_run));
 870                 int timeout_left = cmd->timeout_orig - time_sum;
 871 
 872                 crm_debug("%s systemd %s is now complete (elapsed=%dms, "
 873                           "remaining=%dms): %s (%d)",
 874                           cmd->rsc_id, cmd->real_action, time_sum, timeout_left,
 875                           services_ocf_exitcode_str(cmd->result.exit_status),
 876                           cmd->result.exit_status);
 877                 cmd_original_times(cmd);
 878 
 879                 // Monitors may return "not running", but start/stop shouldn't
 880                 if ((cmd->result.execution_status == PCMK_EXEC_DONE)
 881                     && (cmd->result.exit_status == PCMK_OCF_NOT_RUNNING)) {
 882 
 883                     if (pcmk__str_eq(cmd->real_action, PCMK_ACTION_START,
 884                                      pcmk__str_casei)) {
 885                         cmd->result.exit_status = PCMK_OCF_UNKNOWN_ERROR;
 886                     } else if (pcmk__str_eq(cmd->real_action, PCMK_ACTION_STOP,
 887                                             pcmk__str_casei)) {
 888                         cmd->result.exit_status = PCMK_OCF_OK;
 889                     }
 890                 }
 891             }
 892         }
 893     }
 894 #endif
 895 
 896 #if SUPPORT_NAGIOS
 897     if (rsc && pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_NAGIOS, pcmk__str_casei)) {
 898         if (action_matches(cmd, PCMK_ACTION_MONITOR, 0)
 899             && pcmk__result_ok(&(cmd->result))) {
 900             /* Successfully executed --version for the nagios plugin */
 901             cmd->result.exit_status = PCMK_OCF_NOT_RUNNING;
 902 
 903         } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_START, pcmk__str_casei)
 904                    && !pcmk__result_ok(&(cmd->result))) {
 905 #ifdef PCMK__TIME_USE_CGT
 906             goagain = true;
 907 #endif
 908         }
 909     }
 910 #endif
 911 
 912 #ifdef PCMK__TIME_USE_CGT
 913     if (goagain) {
 914         int time_sum = time_diff_ms(NULL, &(cmd->t_first_run));
 915         int timeout_left = cmd->timeout_orig - time_sum;
 916         int delay = cmd->timeout_orig / 10;
 917 
 918         if(delay >= timeout_left && timeout_left > 20) {
 919             delay = timeout_left/2;
 920         }
 921 
 922         delay = QB_MIN(2000, delay);
 923         if (delay < timeout_left) {
 924             cmd->start_delay = delay;
 925             cmd->timeout = timeout_left;
 926 
 927             if (pcmk__result_ok(&(cmd->result))) {
 928                 crm_debug("%s %s may still be in progress: re-scheduling (elapsed=%dms, remaining=%dms, start_delay=%dms)",
 929                           cmd->rsc_id, cmd->real_action, time_sum, timeout_left, delay);
 930 
 931             } else if (cmd->result.execution_status == PCMK_EXEC_PENDING) {
 932                 crm_info("%s %s is still in progress: re-scheduling (elapsed=%dms, remaining=%dms, start_delay=%dms)",
 933                          cmd->rsc_id, cmd->action, time_sum, timeout_left, delay);
 934 
 935             } else {
 936                 crm_notice("%s %s failed '%s' (%d): re-scheduling (elapsed=%dms, remaining=%dms, start_delay=%dms)",
 937                            cmd->rsc_id, cmd->action,
 938                            services_ocf_exitcode_str(cmd->result.exit_status),
 939                            cmd->result.exit_status, time_sum, timeout_left,
 940                            delay);
 941             }
 942 
 943             cmd_reset(cmd);
 944             if(rsc) {
 945                 rsc->active = NULL;
 946             }
 947             schedule_lrmd_cmd(rsc, cmd);
 948 
 949             /* Don't finalize cmd, we're not done with it yet */
 950             return;
 951 
 952         } else {
 953             crm_notice("Giving up on %s %s (rc=%d): timeout (elapsed=%dms, remaining=%dms)",
 954                        cmd->rsc_id,
 955                        (cmd->real_action? cmd->real_action : cmd->action),
 956                        cmd->result.exit_status, time_sum, timeout_left);
 957             pcmk__set_result(&(cmd->result), PCMK_OCF_UNKNOWN_ERROR,
 958                              PCMK_EXEC_TIMEOUT,
 959                              "Investigate reason for timeout, and adjust "
 960                              "configured operation timeout if necessary");
 961             cmd_original_times(cmd);
 962         }
 963     }
 964 #endif
 965 
 966     pcmk__set_result_output(&(cmd->result), services__grab_stdout(action),
 967                             services__grab_stderr(action));
 968     cmd_finalize(cmd, rsc);
 969 }
 970 
 971 /*!
 972  * \internal
 973  * \brief Process the result of a fence device action (start, stop, or monitor)
 974  *
 975  * \param[in,out] cmd               Fence device action that completed
 976  * \param[in]     exit_status       Fencer API exit status for action
 977  * \param[in]     execution_status  Fencer API execution status for action
 978  * \param[in]     exit_reason       Human-friendly detail, if action failed
 979  */
 980 static void
 981 stonith_action_complete(lrmd_cmd_t *cmd, int exit_status,
     /* [previous][next][first][last][top][bottom][index][help] */
 982                         enum pcmk_exec_status execution_status,
 983                         const char *exit_reason)
 984 {
 985     // This can be NULL if resource was removed before command completed
 986     lrmd_rsc_t *rsc = g_hash_table_lookup(rsc_list, cmd->rsc_id);
 987 
 988     // Simplify fencer exit status to uniform exit status
 989     if (exit_status != CRM_EX_OK) {
 990         exit_status = PCMK_OCF_UNKNOWN_ERROR;
 991     }
 992 
 993     if (cmd->result.execution_status == PCMK_EXEC_CANCELLED) {
 994         /* An in-flight fence action was cancelled. The execution status is
 995          * already correct, so don't overwrite it.
 996          */
 997         execution_status = PCMK_EXEC_CANCELLED;
 998 
 999     } else {
1000         /* Some execution status codes have specific meanings for the fencer
1001          * that executor clients may not expect, so map them to a simple error
1002          * status.
1003          */
1004         switch (execution_status) {
1005             case PCMK_EXEC_NOT_CONNECTED:
1006             case PCMK_EXEC_INVALID:
1007                 execution_status = PCMK_EXEC_ERROR;
1008                 break;
1009 
1010             case PCMK_EXEC_NO_FENCE_DEVICE:
1011                 /* This should be possible only for probes in practice, but
1012                  * interpret for all actions to be safe.
1013                  */
1014                 if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR,
1015                                  pcmk__str_none)) {
1016                     exit_status = PCMK_OCF_NOT_RUNNING;
1017 
1018                 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP,
1019                                         pcmk__str_none)) {
1020                     exit_status = PCMK_OCF_OK;
1021 
1022                 } else {
1023                     exit_status = PCMK_OCF_NOT_INSTALLED;
1024                 }
1025                 execution_status = PCMK_EXEC_ERROR;
1026                 break;
1027 
1028             case PCMK_EXEC_NOT_SUPPORTED:
1029                 exit_status = PCMK_OCF_UNIMPLEMENT_FEATURE;
1030                 break;
1031 
1032             default:
1033                 break;
1034         }
1035     }
1036 
1037     pcmk__set_result(&cmd->result, exit_status, execution_status, exit_reason);
1038 
1039     // Certain successful actions change the known state of the resource
1040     if ((rsc != NULL) && pcmk__result_ok(&(cmd->result))) {
1041 
1042         if (pcmk__str_eq(cmd->action, PCMK_ACTION_START, pcmk__str_casei)) {
1043             pcmk__set_result(&rsc->fence_probe_result, CRM_EX_OK,
1044                              PCMK_EXEC_DONE, NULL); // "running"
1045 
1046         } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP,
1047                                 pcmk__str_casei)) {
1048             pcmk__set_result(&rsc->fence_probe_result, CRM_EX_ERROR,
1049                              PCMK_EXEC_NO_FENCE_DEVICE, NULL); // "not running"
1050         }
1051     }
1052 
1053     /* The recurring timer should not be running at this point in any case, but
1054      * as a failsafe, stop it if it is.
1055      */
1056     stop_recurring_timer(cmd);
1057 
1058     /* Reschedule this command if appropriate. If a recurring command is *not*
1059      * rescheduled, its status must be PCMK_EXEC_CANCELLED, otherwise it will
1060      * not be removed from recurring_ops by cmd_finalize().
1061      */
1062     if (rsc && (cmd->interval_ms > 0)
1063         && (cmd->result.execution_status != PCMK_EXEC_CANCELLED)) {
1064         start_recurring_timer(cmd);
1065     }
1066 
1067     cmd_finalize(cmd, rsc);
1068 }
1069 
1070 static void
1071 lrmd_stonith_callback(stonith_t * stonith, stonith_callback_data_t * data)
     /* [previous][next][first][last][top][bottom][index][help] */
1072 {
1073     if ((data == NULL) || (data->userdata == NULL)) {
1074         crm_err("Ignoring fence action result: "
1075                 "Invalid callback arguments (bug?)");
1076     } else {
1077         stonith_action_complete((lrmd_cmd_t *) data->userdata,
1078                                 stonith__exit_status(data),
1079                                 stonith__execution_status(data),
1080                                 stonith__exit_reason(data));
1081     }
1082 }
1083 
1084 void
1085 stonith_connection_failed(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1086 {
1087     GHashTableIter iter;
1088     lrmd_rsc_t *rsc = NULL;
1089 
1090     crm_warn("Connection to fencer lost (any pending operations for "
1091              "fence devices will be considered failed)");
1092 
1093     g_hash_table_iter_init(&iter, rsc_list);
1094     while (g_hash_table_iter_next(&iter, NULL, (gpointer *) &rsc)) {
1095         if (!pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
1096                           pcmk__str_none)) {
1097             continue;
1098         }
1099 
1100         /* If we registered this fence device, we don't know whether the
1101          * fencer still has the registration or not. Cause future probes to
1102          * return an error until the resource is stopped or started
1103          * successfully. This is especially important if the controller also
1104          * went away (possibly due to a cluster layer restart) and won't
1105          * receive our client notification of any monitors finalized below.
1106          */
1107         if (rsc->fence_probe_result.execution_status == PCMK_EXEC_DONE) {
1108             pcmk__set_result(&rsc->fence_probe_result, CRM_EX_ERROR,
1109                              PCMK_EXEC_NOT_CONNECTED,
1110                              "Lost connection to fencer");
1111         }
1112 
1113         // Consider any active, pending, or recurring operations as failed
1114 
1115         for (GList *op = rsc->recurring_ops; op != NULL; op = op->next) {
1116             lrmd_cmd_t *cmd = op->data;
1117 
1118             /* This won't free a recurring op but instead restart its timer.
1119              * If cmd is rsc->active, this will set rsc->active to NULL, so we
1120              * don't have to worry about finalizing it a second time below.
1121              */
1122             stonith_action_complete(cmd,
1123                                     CRM_EX_ERROR, PCMK_EXEC_NOT_CONNECTED,
1124                                     "Lost connection to fencer");
1125         }
1126 
1127         if (rsc->active != NULL) {
1128             rsc->pending_ops = g_list_prepend(rsc->pending_ops, rsc->active);
1129         }
1130         while (rsc->pending_ops != NULL) {
1131             // This will free the op and remove it from rsc->pending_ops
1132             stonith_action_complete((lrmd_cmd_t *) rsc->pending_ops->data,
1133                                     CRM_EX_ERROR, PCMK_EXEC_NOT_CONNECTED,
1134                                     "Lost connection to fencer");
1135         }
1136     }
1137 }
1138 
1139 /*!
1140  * \internal
1141  * \brief Execute a stonith resource "start" action
1142  *
1143  * Start a stonith resource by registering it with the fencer.
1144  * (Stonith agents don't have a start command.)
1145  *
1146  * \param[in,out] stonith_api  Connection to fencer
1147  * \param[in]     rsc          Stonith resource to start
1148  * \param[in]     cmd          Start command to execute
1149  *
1150  * \return pcmk_ok on success, -errno otherwise
1151  */
1152 static int
1153 execd_stonith_start(stonith_t *stonith_api, const lrmd_rsc_t *rsc,
     /* [previous][next][first][last][top][bottom][index][help] */
1154                     const lrmd_cmd_t *cmd)
1155 {
1156     char *key = NULL;
1157     char *value = NULL;
1158     stonith_key_value_t *device_params = NULL;
1159     int rc = pcmk_ok;
1160 
1161     // Convert command parameters to stonith API key/values
1162     if (cmd->params) {
1163         GHashTableIter iter;
1164 
1165         g_hash_table_iter_init(&iter, cmd->params);
1166         while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
1167             device_params = stonith_key_value_add(device_params, key, value);
1168         }
1169     }
1170 
1171     /* The fencer will automatically register devices via CIB notifications
1172      * when the CIB changes, but to avoid a possible race condition between
1173      * the fencer receiving the notification and the executor requesting that
1174      * resource, the executor registers the device as well. The fencer knows how
1175      * to handle duplicate registrations.
1176      */
1177     rc = stonith_api->cmds->register_device(stonith_api, st_opt_sync_call,
1178                                             cmd->rsc_id, rsc->provider,
1179                                             rsc->type, device_params);
1180 
1181     stonith_key_value_freeall(device_params, 1, 1);
1182     return rc;
1183 }
1184 
1185 /*!
1186  * \internal
1187  * \brief Execute a stonith resource "stop" action
1188  *
1189  * Stop a stonith resource by unregistering it with the fencer.
1190  * (Stonith agents don't have a stop command.)
1191  *
1192  * \param[in,out] stonith_api  Connection to fencer
1193  * \param[in]     rsc          Stonith resource to stop
1194  *
1195  * \return pcmk_ok on success, -errno otherwise
1196  */
1197 static inline int
1198 execd_stonith_stop(stonith_t *stonith_api, const lrmd_rsc_t *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
1199 {
1200     /* @TODO Failure would indicate a problem communicating with fencer;
1201      * perhaps we should try reconnecting and retrying a few times?
1202      */
1203     return stonith_api->cmds->remove_device(stonith_api, st_opt_sync_call,
1204                                             rsc->rsc_id);
1205 }
1206 
1207 /*!
1208  * \internal
1209  * \brief Initiate a stonith resource agent recurring "monitor" action
1210  *
1211  * \param[in,out] stonith_api  Connection to fencer
1212  * \param[in,out] rsc          Stonith resource to monitor
1213  * \param[in]     cmd          Monitor command being executed
1214  *
1215  * \return pcmk_ok if monitor was successfully initiated, -errno otherwise
1216  */
1217 static inline int
1218 execd_stonith_monitor(stonith_t *stonith_api, lrmd_rsc_t *rsc, lrmd_cmd_t *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1219 {
1220     int rc = stonith_api->cmds->monitor(stonith_api, 0, cmd->rsc_id,
1221                                         cmd->timeout / 1000);
1222 
1223     rc = stonith_api->cmds->register_callback(stonith_api, rc, 0, 0, cmd,
1224                                               "lrmd_stonith_callback",
1225                                               lrmd_stonith_callback);
1226     if (rc == TRUE) {
1227         rsc->active = cmd;
1228         rc = pcmk_ok;
1229     } else {
1230         rc = -pcmk_err_generic;
1231     }
1232     return rc;
1233 }
1234 
1235 static void
1236 execute_stonith_action(lrmd_rsc_t *rsc, lrmd_cmd_t *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1237 {
1238     int rc = 0;
1239     bool do_monitor = FALSE;
1240 
1241     stonith_t *stonith_api = get_stonith_connection();
1242 
1243     if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR, pcmk__str_casei)
1244         && (cmd->interval_ms == 0)) {
1245         // Probes don't require a fencer connection
1246         stonith_action_complete(cmd, rsc->fence_probe_result.exit_status,
1247                                 rsc->fence_probe_result.execution_status,
1248                                 rsc->fence_probe_result.exit_reason);
1249         return;
1250 
1251     } else if (stonith_api == NULL) {
1252         stonith_action_complete(cmd, PCMK_OCF_UNKNOWN_ERROR,
1253                                 PCMK_EXEC_NOT_CONNECTED,
1254                                 "No connection to fencer");
1255         return;
1256 
1257     } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_START, pcmk__str_casei)) {
1258         rc = execd_stonith_start(stonith_api, rsc, cmd);
1259         if (rc == pcmk_ok) {
1260             do_monitor = TRUE;
1261         }
1262 
1263     } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP, pcmk__str_casei)) {
1264         rc = execd_stonith_stop(stonith_api, rsc);
1265 
1266     } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR,
1267                             pcmk__str_casei)) {
1268         do_monitor = TRUE;
1269 
1270     } else {
1271         stonith_action_complete(cmd, PCMK_OCF_UNIMPLEMENT_FEATURE,
1272                                 PCMK_EXEC_ERROR,
1273                                 "Invalid fence device action (bug?)");
1274         return;
1275     }
1276 
1277     if (do_monitor) {
1278         rc = execd_stonith_monitor(stonith_api, rsc, cmd);
1279         if (rc == pcmk_ok) {
1280             // Don't clean up yet, we will find out result of the monitor later
1281             return;
1282         }
1283     }
1284 
1285     stonith_action_complete(cmd,
1286                             ((rc == pcmk_ok)? CRM_EX_OK : CRM_EX_ERROR),
1287                             stonith__legacy2status(rc),
1288                             ((rc == -pcmk_err_generic)? NULL : pcmk_strerror(rc)));
1289 }
1290 
1291 static void
1292 execute_nonstonith_action(lrmd_rsc_t *rsc, lrmd_cmd_t *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1293 {
1294     svc_action_t *action = NULL;
1295     GHashTable *params_copy = NULL;
1296 
1297     CRM_ASSERT(rsc);
1298     CRM_ASSERT(cmd);
1299 
1300     crm_trace("Creating action, resource:%s action:%s class:%s provider:%s agent:%s",
1301               rsc->rsc_id, cmd->action, rsc->class, rsc->provider, rsc->type);
1302 
1303 #if SUPPORT_NAGIOS
1304     /* Recurring operations are cancelled anyway for a stop operation */
1305     if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_NAGIOS, pcmk__str_casei)
1306         && pcmk__str_eq(cmd->action, PCMK_ACTION_STOP, pcmk__str_casei)) {
1307 
1308         cmd->result.exit_status = PCMK_OCF_OK;
1309         cmd_finalize(cmd, rsc);
1310         return;
1311     }
1312 #endif
1313 
1314     params_copy = pcmk__str_table_dup(cmd->params);
1315 
1316     action = services__create_resource_action(rsc->rsc_id, rsc->class, rsc->provider,
1317                                      rsc->type,
1318                                      normalize_action_name(rsc, cmd->action),
1319                                      cmd->interval_ms, cmd->timeout,
1320                                      params_copy, cmd->service_flags);
1321 
1322     if (action == NULL) {
1323         pcmk__set_result(&(cmd->result), PCMK_OCF_UNKNOWN_ERROR,
1324                          PCMK_EXEC_ERROR, strerror(ENOMEM));
1325         cmd_finalize(cmd, rsc);
1326         return;
1327     }
1328 
1329     if (action->rc != PCMK_OCF_UNKNOWN) {
1330         pcmk__set_result(&(cmd->result), action->rc, action->status,
1331                          services__exit_reason(action));
1332         services_action_free(action);
1333         cmd_finalize(cmd, rsc);
1334         return;
1335     }
1336 
1337     action->cb_data = cmd;
1338 
1339     if (services_action_async(action, action_complete)) {
1340         /* The services library has taken responsibility for the action. It
1341          * could be pending, blocked, or merged into a duplicate recurring
1342          * action, in which case the action callback (action_complete())
1343          * will be called when the action completes, otherwise the callback has
1344          * already been called.
1345          *
1346          * action_complete() calls cmd_finalize() which can free cmd, so cmd
1347          * cannot be used here.
1348          */
1349     } else {
1350         /* This is a recurring action that is not being cancelled and could not
1351          * be initiated. It has been rescheduled, and the action callback
1352          * (action_complete()) has been called, which in this case has already
1353          * called cmd_finalize(), which in this case should only reset (not
1354          * free) cmd.
1355          */
1356 
1357         pcmk__set_result(&(cmd->result), action->rc, action->status,
1358                          services__exit_reason(action));
1359         services_action_free(action);
1360     }
1361 }
1362 
1363 static gboolean
1364 execute_resource_action(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
1365 {
1366     lrmd_rsc_t *rsc = (lrmd_rsc_t *) user_data;
1367     lrmd_cmd_t *cmd = NULL;
1368 
1369     CRM_CHECK(rsc != NULL, return FALSE);
1370 
1371     if (rsc->active) {
1372         crm_trace("%s is still active", rsc->rsc_id);
1373         return TRUE;
1374     }
1375 
1376     if (rsc->pending_ops) {
1377         GList *first = rsc->pending_ops;
1378 
1379         cmd = first->data;
1380         if (cmd->delay_id) {
1381             crm_trace
1382                 ("Command %s %s was asked to run too early, waiting for start_delay timeout of %dms",
1383                  cmd->rsc_id, cmd->action, cmd->start_delay);
1384             return TRUE;
1385         }
1386         rsc->pending_ops = g_list_remove_link(rsc->pending_ops, first);
1387         g_list_free_1(first);
1388 
1389 #ifdef PCMK__TIME_USE_CGT
1390         get_current_time(&(cmd->t_run), &(cmd->t_first_run));
1391 #endif
1392         cmd->epoch_last_run = time(NULL);
1393     }
1394 
1395     if (!cmd) {
1396         crm_trace("Nothing further to do for %s", rsc->rsc_id);
1397         return TRUE;
1398     }
1399 
1400     rsc->active = cmd;          /* only one op at a time for a rsc */
1401     if (cmd->interval_ms) {
1402         rsc->recurring_ops = g_list_append(rsc->recurring_ops, cmd);
1403     }
1404 
1405     log_execute(cmd);
1406 
1407     if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH, pcmk__str_casei)) {
1408         execute_stonith_action(rsc, cmd);
1409     } else {
1410         execute_nonstonith_action(rsc, cmd);
1411     }
1412 
1413     return TRUE;
1414 }
1415 
1416 void
1417 free_rsc(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
1418 {
1419     GList *gIter = NULL;
1420     lrmd_rsc_t *rsc = data;
1421     int is_stonith = pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
1422                                   pcmk__str_casei);
1423 
1424     gIter = rsc->pending_ops;
1425     while (gIter != NULL) {
1426         GList *next = gIter->next;
1427         lrmd_cmd_t *cmd = gIter->data;
1428 
1429         /* command was never executed */
1430         cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1431         cmd_finalize(cmd, NULL);
1432 
1433         gIter = next;
1434     }
1435     /* frees list, but not list elements. */
1436     g_list_free(rsc->pending_ops);
1437 
1438     gIter = rsc->recurring_ops;
1439     while (gIter != NULL) {
1440         GList *next = gIter->next;
1441         lrmd_cmd_t *cmd = gIter->data;
1442 
1443         if (is_stonith) {
1444             cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1445             /* If a stonith command is in-flight, just mark it as cancelled;
1446              * it is not safe to finalize/free the cmd until the stonith api
1447              * says it has either completed or timed out.
1448              */
1449             if (rsc->active != cmd) {
1450                 cmd_finalize(cmd, NULL);
1451             }
1452         } else {
1453             /* This command is already handed off to service library,
1454              * let service library cancel it and tell us via the callback
1455              * when it is cancelled. The rsc can be safely destroyed
1456              * even if we are waiting for the cancel result */
1457             services_action_cancel(rsc->rsc_id,
1458                                    normalize_action_name(rsc, cmd->action),
1459                                    cmd->interval_ms);
1460         }
1461 
1462         gIter = next;
1463     }
1464     /* frees list, but not list elements. */
1465     g_list_free(rsc->recurring_ops);
1466 
1467     free(rsc->rsc_id);
1468     free(rsc->class);
1469     free(rsc->provider);
1470     free(rsc->type);
1471     mainloop_destroy_trigger(rsc->work);
1472 
1473     free(rsc);
1474 }
1475 
1476 static int
1477 process_lrmd_signon(pcmk__client_t *client, xmlNode *request, int call_id,
     /* [previous][next][first][last][top][bottom][index][help] */
1478                     xmlNode **reply)
1479 {
1480     int rc = pcmk_ok;
1481     time_t now = time(NULL);
1482     const char *protocol_version = crm_element_value(request, F_LRMD_PROTOCOL_VERSION);
1483     const char *start_state = pcmk__env_option(PCMK__ENV_NODE_START_STATE);
1484 
1485     if (compare_version(protocol_version, LRMD_MIN_PROTOCOL_VERSION) < 0) {
1486         crm_err("Cluster API version must be greater than or equal to %s, not %s",
1487                 LRMD_MIN_PROTOCOL_VERSION, protocol_version);
1488         rc = -EPROTO;
1489     }
1490 
1491     if (pcmk__xe_attr_is_true(request, F_LRMD_IS_IPC_PROVIDER)) {
1492 #ifdef PCMK__COMPILE_REMOTE
1493         if ((client->remote != NULL)
1494             && pcmk_is_set(client->flags,
1495                            pcmk__client_tls_handshake_complete)) {
1496 
1497             // This is a remote connection from a cluster node's controller
1498             ipc_proxy_add_provider(client);
1499         } else {
1500             rc = -EACCES;
1501         }
1502 #else
1503         rc = -EPROTONOSUPPORT;
1504 #endif
1505     }
1506 
1507     *reply = create_lrmd_reply(__func__, rc, call_id);
1508     crm_xml_add(*reply, F_LRMD_OPERATION, CRM_OP_REGISTER);
1509     crm_xml_add(*reply, F_LRMD_CLIENTID, client->id);
1510     crm_xml_add(*reply, F_LRMD_PROTOCOL_VERSION, LRMD_PROTOCOL_VERSION);
1511     crm_xml_add_ll(*reply, PCMK__XA_UPTIME, now - start_time);
1512 
1513     if (start_state) {
1514         crm_xml_add(*reply, PCMK__XA_NODE_START_STATE, start_state);
1515     }
1516 
1517     return rc;
1518 }
1519 
1520 static int
1521 process_lrmd_rsc_register(pcmk__client_t *client, uint32_t id, xmlNode *request)
     /* [previous][next][first][last][top][bottom][index][help] */
1522 {
1523     int rc = pcmk_ok;
1524     lrmd_rsc_t *rsc = build_rsc_from_xml(request);
1525     lrmd_rsc_t *dup = g_hash_table_lookup(rsc_list, rsc->rsc_id);
1526 
1527     if (dup &&
1528         pcmk__str_eq(rsc->class, dup->class, pcmk__str_casei) &&
1529         pcmk__str_eq(rsc->provider, dup->provider, pcmk__str_casei) && pcmk__str_eq(rsc->type, dup->type, pcmk__str_casei)) {
1530 
1531         crm_notice("Ignoring duplicate registration of '%s'", rsc->rsc_id);
1532         free_rsc(rsc);
1533         return rc;
1534     }
1535 
1536     g_hash_table_replace(rsc_list, rsc->rsc_id, rsc);
1537     crm_info("Cached agent information for '%s'", rsc->rsc_id);
1538     return rc;
1539 }
1540 
1541 static xmlNode *
1542 process_lrmd_get_rsc_info(xmlNode *request, int call_id)
     /* [previous][next][first][last][top][bottom][index][help] */
1543 {
1544     int rc = pcmk_ok;
1545     xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1546     const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1547     xmlNode *reply = NULL;
1548     lrmd_rsc_t *rsc = NULL;
1549 
1550     if (rsc_id == NULL) {
1551         rc = -ENODEV;
1552     } else {
1553         rsc = g_hash_table_lookup(rsc_list, rsc_id);
1554         if (rsc == NULL) {
1555             crm_info("Agent information for '%s' not in cache", rsc_id);
1556             rc = -ENODEV;
1557         }
1558     }
1559 
1560     reply = create_lrmd_reply(__func__, rc, call_id);
1561     if (rsc) {
1562         crm_xml_add(reply, F_LRMD_RSC_ID, rsc->rsc_id);
1563         crm_xml_add(reply, F_LRMD_CLASS, rsc->class);
1564         crm_xml_add(reply, F_LRMD_PROVIDER, rsc->provider);
1565         crm_xml_add(reply, F_LRMD_TYPE, rsc->type);
1566     }
1567     return reply;
1568 }
1569 
1570 static int
1571 process_lrmd_rsc_unregister(pcmk__client_t *client, uint32_t id,
     /* [previous][next][first][last][top][bottom][index][help] */
1572                             xmlNode *request)
1573 {
1574     int rc = pcmk_ok;
1575     lrmd_rsc_t *rsc = NULL;
1576     xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1577     const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1578 
1579     if (!rsc_id) {
1580         return -ENODEV;
1581     }
1582 
1583     rsc = g_hash_table_lookup(rsc_list, rsc_id);
1584     if (rsc == NULL) {
1585         crm_info("Ignoring unregistration of resource '%s', which is not registered",
1586                  rsc_id);
1587         return pcmk_ok;
1588     }
1589 
1590     if (rsc->active) {
1591         /* let the caller know there are still active ops on this rsc to watch for */
1592         crm_trace("Operation (%p) still in progress for unregistered resource %s",
1593                   rsc->active, rsc_id);
1594         rc = -EINPROGRESS;
1595     }
1596 
1597     g_hash_table_remove(rsc_list, rsc_id);
1598 
1599     return rc;
1600 }
1601 
1602 static int
1603 process_lrmd_rsc_exec(pcmk__client_t *client, uint32_t id, xmlNode *request)
     /* [previous][next][first][last][top][bottom][index][help] */
1604 {
1605     lrmd_rsc_t *rsc = NULL;
1606     lrmd_cmd_t *cmd = NULL;
1607     xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1608     const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1609     int call_id;
1610 
1611     if (!rsc_id) {
1612         return -EINVAL;
1613     }
1614     if (!(rsc = g_hash_table_lookup(rsc_list, rsc_id))) {
1615         crm_info("Resource '%s' not found (%d active resources)",
1616                  rsc_id, g_hash_table_size(rsc_list));
1617         return -ENODEV;
1618     }
1619 
1620     cmd = create_lrmd_cmd(request, client);
1621     call_id = cmd->call_id;
1622 
1623     /* Don't reference cmd after handing it off to be scheduled.
1624      * The cmd could get merged and freed. */
1625     schedule_lrmd_cmd(rsc, cmd);
1626 
1627     return call_id;
1628 }
1629 
1630 static int
1631 cancel_op(const char *rsc_id, const char *action, guint interval_ms)
     /* [previous][next][first][last][top][bottom][index][help] */
1632 {
1633     GList *gIter = NULL;
1634     lrmd_rsc_t *rsc = g_hash_table_lookup(rsc_list, rsc_id);
1635 
1636     /* How to cancel an action.
1637      * 1. Check pending ops list, if it hasn't been handed off
1638      *    to the service library or stonith recurring list remove
1639      *    it there and that will stop it.
1640      * 2. If it isn't in the pending ops list, then it's either a
1641      *    recurring op in the stonith recurring list, or the service
1642      *    library's recurring list.  Stop it there
1643      * 3. If not found in any lists, then this operation has either
1644      *    been executed already and is not a recurring operation, or
1645      *    never existed.
1646      */
1647     if (!rsc) {
1648         return -ENODEV;
1649     }
1650 
1651     for (gIter = rsc->pending_ops; gIter != NULL; gIter = gIter->next) {
1652         lrmd_cmd_t *cmd = gIter->data;
1653 
1654         if (action_matches(cmd, action, interval_ms)) {
1655             cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1656             cmd_finalize(cmd, rsc);
1657             return pcmk_ok;
1658         }
1659     }
1660 
1661     if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH, pcmk__str_casei)) {
1662         /* The service library does not handle stonith operations.
1663          * We have to handle recurring stonith operations ourselves. */
1664         for (gIter = rsc->recurring_ops; gIter != NULL; gIter = gIter->next) {
1665             lrmd_cmd_t *cmd = gIter->data;
1666 
1667             if (action_matches(cmd, action, interval_ms)) {
1668                 cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1669                 if (rsc->active != cmd) {
1670                     cmd_finalize(cmd, rsc);
1671                 }
1672                 return pcmk_ok;
1673             }
1674         }
1675     } else if (services_action_cancel(rsc_id,
1676                                       normalize_action_name(rsc, action),
1677                                       interval_ms) == TRUE) {
1678         /* The service library will tell the action_complete callback function
1679          * this action was cancelled, which will destroy the cmd and remove
1680          * it from the recurring_op list. Do not do that in this function
1681          * if the service library says it cancelled it. */
1682         return pcmk_ok;
1683     }
1684 
1685     return -EOPNOTSUPP;
1686 }
1687 
1688 static void
1689 cancel_all_recurring(lrmd_rsc_t * rsc, const char *client_id)
     /* [previous][next][first][last][top][bottom][index][help] */
1690 {
1691     GList *cmd_list = NULL;
1692     GList *cmd_iter = NULL;
1693 
1694     /* Notice a copy of each list is created when concat is called.
1695      * This prevents odd behavior from occurring when the cmd_list
1696      * is iterated through later on.  It is possible the cancel_op
1697      * function may end up modifying the recurring_ops and pending_ops
1698      * lists.  If we did not copy those lists, our cmd_list iteration
1699      * could get messed up.*/
1700     if (rsc->recurring_ops) {
1701         cmd_list = g_list_concat(cmd_list, g_list_copy(rsc->recurring_ops));
1702     }
1703     if (rsc->pending_ops) {
1704         cmd_list = g_list_concat(cmd_list, g_list_copy(rsc->pending_ops));
1705     }
1706     if (!cmd_list) {
1707         return;
1708     }
1709 
1710     for (cmd_iter = cmd_list; cmd_iter; cmd_iter = cmd_iter->next) {
1711         lrmd_cmd_t *cmd = cmd_iter->data;
1712 
1713         if (cmd->interval_ms == 0) {
1714             continue;
1715         }
1716 
1717         if (client_id && !pcmk__str_eq(cmd->client_id, client_id, pcmk__str_casei)) {
1718             continue;
1719         }
1720 
1721         cancel_op(rsc->rsc_id, cmd->action, cmd->interval_ms);
1722     }
1723     /* frees only the copied list data, not the cmds */
1724     g_list_free(cmd_list);
1725 }
1726 
1727 static int
1728 process_lrmd_rsc_cancel(pcmk__client_t *client, uint32_t id, xmlNode *request)
     /* [previous][next][first][last][top][bottom][index][help] */
1729 {
1730     xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1731     const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1732     const char *action = crm_element_value(rsc_xml, F_LRMD_RSC_ACTION);
1733     guint interval_ms = 0;
1734 
1735     crm_element_value_ms(rsc_xml, F_LRMD_RSC_INTERVAL, &interval_ms);
1736 
1737     if (!rsc_id || !action) {
1738         return -EINVAL;
1739     }
1740 
1741     return cancel_op(rsc_id, action, interval_ms);
1742 }
1743 
1744 static void
1745 add_recurring_op_xml(xmlNode *reply, lrmd_rsc_t *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
1746 {
1747     xmlNode *rsc_xml = create_xml_node(reply, F_LRMD_RSC);
1748 
1749     crm_xml_add(rsc_xml, F_LRMD_RSC_ID, rsc->rsc_id);
1750     for (GList *item = rsc->recurring_ops; item != NULL; item = item->next) {
1751         lrmd_cmd_t *cmd = item->data;
1752         xmlNode *op_xml = create_xml_node(rsc_xml, T_LRMD_RSC_OP);
1753 
1754         crm_xml_add(op_xml, F_LRMD_RSC_ACTION,
1755                     (cmd->real_action? cmd->real_action : cmd->action));
1756         crm_xml_add_ms(op_xml, F_LRMD_RSC_INTERVAL, cmd->interval_ms);
1757         crm_xml_add_int(op_xml, F_LRMD_TIMEOUT, cmd->timeout_orig);
1758     }
1759 }
1760 
1761 static xmlNode *
1762 process_lrmd_get_recurring(xmlNode *request, int call_id)
     /* [previous][next][first][last][top][bottom][index][help] */
1763 {
1764     int rc = pcmk_ok;
1765     const char *rsc_id = NULL;
1766     lrmd_rsc_t *rsc = NULL;
1767     xmlNode *reply = NULL;
1768     xmlNode *rsc_xml = NULL;
1769 
1770     // Resource ID is optional
1771     rsc_xml = first_named_child(request, F_LRMD_CALLDATA);
1772     if (rsc_xml) {
1773         rsc_xml = first_named_child(rsc_xml, F_LRMD_RSC);
1774     }
1775     if (rsc_xml) {
1776         rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1777     }
1778 
1779     // If resource ID is specified, resource must exist
1780     if (rsc_id != NULL) {
1781         rsc = g_hash_table_lookup(rsc_list, rsc_id);
1782         if (rsc == NULL) {
1783             crm_info("Resource '%s' not found (%d active resources)",
1784                      rsc_id, g_hash_table_size(rsc_list));
1785             rc = -ENODEV;
1786         }
1787     }
1788 
1789     reply = create_lrmd_reply(__func__, rc, call_id);
1790 
1791     // If resource ID is not specified, check all resources
1792     if (rsc_id == NULL) {
1793         GHashTableIter iter;
1794         char *key = NULL;
1795 
1796         g_hash_table_iter_init(&iter, rsc_list);
1797         while (g_hash_table_iter_next(&iter, (gpointer *) &key,
1798                                       (gpointer *) &rsc)) {
1799             add_recurring_op_xml(reply, rsc);
1800         }
1801     } else if (rsc) {
1802         add_recurring_op_xml(reply, rsc);
1803     }
1804     return reply;
1805 }
1806 
1807 void
1808 process_lrmd_message(pcmk__client_t *client, uint32_t id, xmlNode *request)
     /* [previous][next][first][last][top][bottom][index][help] */
1809 {
1810     int rc = pcmk_ok;
1811     int call_id = 0;
1812     const char *op = crm_element_value(request, F_LRMD_OPERATION);
1813     int do_reply = 0;
1814     int do_notify = 0;
1815     xmlNode *reply = NULL;
1816 
1817     /* Certain IPC commands may be done only by privileged users (i.e. root or
1818      * hacluster), because they would otherwise provide a means of bypassing
1819      * ACLs.
1820      */
1821     bool allowed = pcmk_is_set(client->flags, pcmk__client_privileged);
1822 
1823     crm_trace("Processing %s operation from %s", op, client->id);
1824     crm_element_value_int(request, F_LRMD_CALLID, &call_id);
1825 
1826     if (pcmk__str_eq(op, CRM_OP_IPC_FWD, pcmk__str_none)) {
1827 #ifdef PCMK__COMPILE_REMOTE
1828         if (allowed) {
1829             ipc_proxy_forward_client(client, request);
1830         } else {
1831             rc = -EACCES;
1832         }
1833 #else
1834         rc = -EPROTONOSUPPORT;
1835 #endif
1836         do_reply = 1;
1837     } else if (pcmk__str_eq(op, CRM_OP_REGISTER, pcmk__str_none)) {
1838         rc = process_lrmd_signon(client, request, call_id, &reply);
1839         do_reply = 1;
1840     } else if (pcmk__str_eq(op, LRMD_OP_RSC_REG, pcmk__str_none)) {
1841         if (allowed) {
1842             rc = process_lrmd_rsc_register(client, id, request);
1843             do_notify = 1;
1844         } else {
1845             rc = -EACCES;
1846         }
1847         do_reply = 1;
1848     } else if (pcmk__str_eq(op, LRMD_OP_RSC_INFO, pcmk__str_none)) {
1849         if (allowed) {
1850             reply = process_lrmd_get_rsc_info(request, call_id);
1851         } else {
1852             rc = -EACCES;
1853         }
1854         do_reply = 1;
1855     } else if (pcmk__str_eq(op, LRMD_OP_RSC_UNREG, pcmk__str_none)) {
1856         if (allowed) {
1857             rc = process_lrmd_rsc_unregister(client, id, request);
1858             /* don't notify anyone about failed un-registers */
1859             if (rc == pcmk_ok || rc == -EINPROGRESS) {
1860                 do_notify = 1;
1861             }
1862         } else {
1863             rc = -EACCES;
1864         }
1865         do_reply = 1;
1866     } else if (pcmk__str_eq(op, LRMD_OP_RSC_EXEC, pcmk__str_none)) {
1867         if (allowed) {
1868             rc = process_lrmd_rsc_exec(client, id, request);
1869         } else {
1870             rc = -EACCES;
1871         }
1872         do_reply = 1;
1873     } else if (pcmk__str_eq(op, LRMD_OP_RSC_CANCEL, pcmk__str_none)) {
1874         if (allowed) {
1875             rc = process_lrmd_rsc_cancel(client, id, request);
1876         } else {
1877             rc = -EACCES;
1878         }
1879         do_reply = 1;
1880     } else if (pcmk__str_eq(op, LRMD_OP_POKE, pcmk__str_none)) {
1881         do_notify = 1;
1882         do_reply = 1;
1883     } else if (pcmk__str_eq(op, LRMD_OP_CHECK, pcmk__str_none)) {
1884         if (allowed) {
1885             xmlNode *data = get_message_xml(request, F_LRMD_CALLDATA);
1886 
1887             CRM_LOG_ASSERT(data != NULL);
1888             pcmk__valid_sbd_timeout(crm_element_value(data, F_LRMD_WATCHDOG));
1889         } else {
1890             rc = -EACCES;
1891         }
1892     } else if (pcmk__str_eq(op, LRMD_OP_ALERT_EXEC, pcmk__str_none)) {
1893         if (allowed) {
1894             rc = process_lrmd_alert_exec(client, id, request);
1895         } else {
1896             rc = -EACCES;
1897         }
1898         do_reply = 1;
1899     } else if (pcmk__str_eq(op, LRMD_OP_GET_RECURRING, pcmk__str_none)) {
1900         if (allowed) {
1901             reply = process_lrmd_get_recurring(request, call_id);
1902         } else {
1903             rc = -EACCES;
1904         }
1905         do_reply = 1;
1906     } else {
1907         rc = -EOPNOTSUPP;
1908         do_reply = 1;
1909         crm_err("Unknown IPC request '%s' from client %s",
1910                 op, pcmk__client_name(client));
1911     }
1912 
1913     if (rc == -EACCES) {
1914         crm_warn("Rejecting IPC request '%s' from unprivileged client %s",
1915                  op, pcmk__client_name(client));
1916     }
1917 
1918     crm_debug("Processed %s operation from %s: rc=%d, reply=%d, notify=%d",
1919               op, client->id, rc, do_reply, do_notify);
1920 
1921     if (do_reply) {
1922         int send_rc = pcmk_rc_ok;
1923 
1924         if (reply == NULL) {
1925             reply = create_lrmd_reply(__func__, rc, call_id);
1926         }
1927         send_rc = lrmd_server_send_reply(client, id, reply);
1928         free_xml(reply);
1929         if (send_rc != pcmk_rc_ok) {
1930             crm_warn("Reply to client %s failed: %s " CRM_XS " rc=%d",
1931                      pcmk__client_name(client), pcmk_rc_str(send_rc), send_rc);
1932         }
1933     }
1934 
1935     if (do_notify) {
1936         send_generic_notify(rc, request);
1937     }
1938 }

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