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

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