root/daemons/fenced/fenced_commands.c

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

DEFINITIONS

This source file includes following definitions.
  1. is_action_required
  2. get_action_delay_max
  3. get_action_delay_base
  4. get_action_timeout
  5. free_async_command
  6. create_async_command
  7. get_action_limit
  8. get_active_cmds
  9. fork_cb
  10. get_agent_metadata_cb
  11. report_internal_result
  12. stonith_device_execute
  13. stonith_device_dispatch
  14. start_delay_helper
  15. schedule_stonith_command
  16. free_device
  17. free_device_list
  18. init_device_list
  19. build_port_aliases
  20. free_metadata_cache
  21. init_metadata_cache
  22. get_agent_metadata
  23. is_nodeid_required
  24. add_action
  25. read_action_metadata
  26. map_action
  27. xml2device_params
  28. target_list_type
  29. build_device_from_xml
  30. schedule_internal_command
  31. status_search_cb
  32. dynamic_list_search_cb
  33. device_params_diff
  34. device_has_duplicate
  35. stonith_device_register
  36. stonith_device_remove
  37. count_active_levels
  38. free_topology_entry
  39. free_topology_list
  40. init_topology_list
  41. stonith_level_key
  42. unpack_level_kind
  43. parse_device_list
  44. fenced_register_level
  45. fenced_unregister_level
  46. list_to_string
  47. execute_agent_action
  48. search_devices_record_result
  49. localhost_is_eligible
  50. can_fence_host_with_device
  51. search_devices
  52. get_capable_devices
  53. add_action_specific_attributes
  54. add_disallowed
  55. add_action_reply
  56. stonith_query_capable_device_cb
  57. log_async_result
  58. send_async_reply
  59. cancel_stonith_command
  60. st_child_done
  61. sort_device_priority
  62. stonith_fence_get_devices_cb
  63. fence_locally
  64. fenced_construct_reply
  65. construct_async_reply
  66. fencing_peer_active
  67. set_fencing_completed
  68. check_alternate_host
  69. stonith_send_reply
  70. remove_relay_op
  71. is_privileged
  72. handle_register_request
  73. handle_agent_request
  74. handle_update_timeout_request
  75. handle_query_request
  76. handle_notify_request
  77. handle_relay_request
  78. handle_fence_request
  79. handle_history_request
  80. handle_device_add_request
  81. handle_device_delete_request
  82. handle_level_add_request
  83. handle_level_delete_request
  84. handle_cache_request
  85. handle_unknown_request
  86. fenced_register_handlers
  87. fenced_unregister_handlers
  88. handle_request
  89. handle_reply
  90. stonith_command

   1 /*
   2  * Copyright 2009-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <sys/param.h>
  13 #include <stdio.h>
  14 #include <sys/types.h>
  15 #include <sys/wait.h>
  16 #include <sys/stat.h>
  17 #include <unistd.h>
  18 #include <sys/utsname.h>
  19 
  20 #include <stdlib.h>
  21 #include <errno.h>
  22 #include <fcntl.h>
  23 #include <ctype.h>
  24 
  25 #include <crm/crm.h>
  26 #include <crm/msg_xml.h>
  27 #include <crm/common/ipc.h>
  28 #include <crm/common/ipc_internal.h>
  29 #include <crm/cluster/internal.h>
  30 #include <crm/common/mainloop.h>
  31 
  32 #include <crm/stonith-ng.h>
  33 #include <crm/fencing/internal.h>
  34 #include <crm/common/xml.h>
  35 
  36 #include <pacemaker-fenced.h>
  37 
  38 GHashTable *device_list = NULL;
  39 GHashTable *topology = NULL;
  40 GList *cmd_list = NULL;
  41 
  42 static GHashTable *fenced_handlers = NULL;
  43 
  44 struct device_search_s {
  45     /* target of fence action */
  46     char *host;
  47     /* requested fence action */
  48     char *action;
  49     /* timeout to use if a device is queried dynamically for possible targets */
  50     int per_device_timeout;
  51     /* number of registered fencing devices at time of request */
  52     int replies_needed;
  53     /* number of device replies received so far */
  54     int replies_received;
  55     /* whether the target is eligible to perform requested action (or off) */
  56     bool allow_suicide;
  57 
  58     /* private data to pass to search callback function */
  59     void *user_data;
  60     /* function to call when all replies have been received */
  61     void (*callback) (GList * devices, void *user_data);
  62     /* devices capable of performing requested action (or off if remapping) */
  63     GList *capable;
  64 };
  65 
  66 static gboolean stonith_device_dispatch(gpointer user_data);
  67 static void st_child_done(int pid, const pcmk__action_result_t *result,
  68                           void *user_data);
  69 static void stonith_send_reply(xmlNode * reply, int call_options, const char *remote_peer,
  70                                pcmk__client_t *client);
  71 
  72 static void search_devices_record_result(struct device_search_s *search, const char *device,
  73                                          gboolean can_fence);
  74 
  75 static int get_agent_metadata(const char *agent, xmlNode **metadata);
  76 static void read_action_metadata(stonith_device_t *device);
  77 static enum fenced_target_by unpack_level_kind(xmlNode *level);
  78 
  79 typedef struct async_command_s {
  80 
  81     int id;
  82     int pid;
  83     int fd_stdout;
  84     int options;
  85     int default_timeout; /* seconds */
  86     int timeout; /* seconds */
  87 
  88     int start_delay; /* seconds */
  89     int delay_id;
  90 
  91     char *op;
  92     char *origin;
  93     char *client;
  94     char *client_name;
  95     char *remote_op_id;
  96 
  97     char *victim;
  98     uint32_t victim_nodeid;
  99     char *action;
 100     char *device;
 101 
 102     GList *device_list;
 103     GList *device_next;
 104 
 105     void *internal_user_data;
 106     void (*done_cb) (int pid, const pcmk__action_result_t *result,
 107                      void *user_data);
 108     guint timer_sigterm;
 109     guint timer_sigkill;
 110     /*! If the operation timed out, this is the last signal
 111      *  we sent to the process to get it to terminate */
 112     int last_timeout_signo;
 113 
 114     stonith_device_t *active_on;
 115     stonith_device_t *activating_on;
 116 } async_command_t;
 117 
 118 static xmlNode *construct_async_reply(async_command_t *cmd,
 119                                       const pcmk__action_result_t *result);
 120 
 121 static gboolean
 122 is_action_required(const char *action, stonith_device_t *device)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124     return device && device->automatic_unfencing && pcmk__str_eq(action, "on",
 125                                                                  pcmk__str_casei);
 126 }
 127 
 128 static int
 129 get_action_delay_max(stonith_device_t * device, const char * action)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131     const char *value = NULL;
 132     int delay_max = 0;
 133 
 134     if (!pcmk__is_fencing_action(action)) {
 135         return 0;
 136     }
 137 
 138     value = g_hash_table_lookup(device->params, PCMK_STONITH_DELAY_MAX);
 139     if (value) {
 140        delay_max = crm_parse_interval_spec(value) / 1000;
 141     }
 142 
 143     return delay_max;
 144 }
 145 
 146 static int
 147 get_action_delay_base(stonith_device_t *device, const char *action, const char *victim)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149     char *hash_value = NULL;
 150     int delay_base = 0;
 151 
 152     if (!pcmk__is_fencing_action(action)) {
 153         return 0;
 154     }
 155 
 156     hash_value = g_hash_table_lookup(device->params, PCMK_STONITH_DELAY_BASE);
 157 
 158     if (hash_value) {
 159         char *value = strdup(hash_value);
 160         char *valptr = value;
 161 
 162         CRM_ASSERT(value != NULL);
 163 
 164         if (victim) {
 165             for (char *val = strtok(value, "; \t"); val != NULL; val = strtok(NULL, "; \t")) {
 166                 char *mapval = strchr(val, ':');
 167 
 168                 if (mapval == NULL || mapval[1] == 0) {
 169                     crm_err("pcmk_delay_base: empty value in mapping", val);
 170                     continue;
 171                 }
 172 
 173                 if (mapval != val && strncasecmp(victim, val, (size_t)(mapval - val)) == 0) {
 174                     value = mapval + 1;
 175                     crm_debug("pcmk_delay_base mapped to %s for %s", value, victim);
 176                     break;
 177                 }
 178             }
 179         }
 180 
 181         if (strchr(value, ':') == 0) {
 182            delay_base = crm_parse_interval_spec(value) / 1000;
 183         }
 184 
 185         free(valptr);
 186     }
 187 
 188     return delay_base;
 189 }
 190 
 191 /*!
 192  * \internal
 193  * \brief Override STONITH timeout with pcmk_*_timeout if available
 194  *
 195  * \param[in] device           STONITH device to use
 196  * \param[in] action           STONITH action name
 197  * \param[in] default_timeout  Timeout to use if device does not have
 198  *                             a pcmk_*_timeout parameter for action
 199  *
 200  * \return Value of pcmk_(action)_timeout if available, otherwise default_timeout
 201  * \note For consistency, it would be nice if reboot/off/on timeouts could be
 202  *       set the same way as start/stop/monitor timeouts, i.e. with an
 203  *       <operation> entry in the fencing resource configuration. However that
 204  *       is insufficient because fencing devices may be registered directly via
 205  *       the fencer's register_device() API instead of going through the CIB
 206  *       (e.g. stonith_admin uses it for its -R option, and the executor uses it
 207  *       to ensure a device is registered when a command is issued). As device
 208  *       properties, pcmk_*_timeout parameters can be grabbed by the fencer when
 209  *       the device is registered, whether by CIB change or API call.
 210  */
 211 static int
 212 get_action_timeout(stonith_device_t * device, const char *action, int default_timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214     if (action && device && device->params) {
 215         char buffer[64] = { 0, };
 216         const char *value = NULL;
 217 
 218         /* If "reboot" was requested but the device does not support it,
 219          * we will remap to "off", so check timeout for "off" instead
 220          */
 221         if (pcmk__str_eq(action, "reboot", pcmk__str_casei)
 222             && !pcmk_is_set(device->flags, st_device_supports_reboot)) {
 223             crm_trace("%s doesn't support reboot, using timeout for off instead",
 224                       device->id);
 225             action = "off";
 226         }
 227 
 228         /* If the device config specified an action-specific timeout, use it */
 229         snprintf(buffer, sizeof(buffer), "pcmk_%s_timeout", action);
 230         value = g_hash_table_lookup(device->params, buffer);
 231         if (value) {
 232             return atoi(value);
 233         }
 234     }
 235     return default_timeout;
 236 }
 237 
 238 static void
 239 free_async_command(async_command_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241     if (!cmd) {
 242         return;
 243     }
 244 
 245     if (cmd->delay_id) {
 246         g_source_remove(cmd->delay_id);
 247     }
 248 
 249     cmd_list = g_list_remove(cmd_list, cmd);
 250 
 251     g_list_free_full(cmd->device_list, free);
 252     free(cmd->device);
 253     free(cmd->action);
 254     free(cmd->victim);
 255     free(cmd->remote_op_id);
 256     free(cmd->client);
 257     free(cmd->client_name);
 258     free(cmd->origin);
 259     free(cmd->op);
 260     free(cmd);
 261 }
 262 
 263 static async_command_t *
 264 create_async_command(xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
 265 {
 266     async_command_t *cmd = NULL;
 267     xmlNode *op = get_xpath_object("//@" F_STONITH_ACTION, msg, LOG_ERR);
 268     const char *action = crm_element_value(op, F_STONITH_ACTION);
 269 
 270     CRM_CHECK(action != NULL, crm_log_xml_warn(msg, "NoAction"); return NULL);
 271 
 272     crm_log_xml_trace(msg, "Command");
 273     cmd = calloc(1, sizeof(async_command_t));
 274     crm_element_value_int(msg, F_STONITH_CALLID, &(cmd->id));
 275     crm_element_value_int(msg, F_STONITH_CALLOPTS, &(cmd->options));
 276     crm_element_value_int(msg, F_STONITH_TIMEOUT, &(cmd->default_timeout));
 277     cmd->timeout = cmd->default_timeout;
 278     // Value -1 means disable any static/random fencing delays
 279     crm_element_value_int(msg, F_STONITH_DELAY, &(cmd->start_delay));
 280 
 281     cmd->origin = crm_element_value_copy(msg, F_ORIG);
 282     cmd->remote_op_id = crm_element_value_copy(msg, F_STONITH_REMOTE_OP_ID);
 283     cmd->client = crm_element_value_copy(msg, F_STONITH_CLIENTID);
 284     cmd->client_name = crm_element_value_copy(msg, F_STONITH_CLIENTNAME);
 285     cmd->op = crm_element_value_copy(msg, F_STONITH_OPERATION);
 286     cmd->action = strdup(action);
 287     cmd->victim = crm_element_value_copy(op, F_STONITH_TARGET);
 288     cmd->device = crm_element_value_copy(op, F_STONITH_DEVICE);
 289 
 290     CRM_CHECK(cmd->op != NULL, crm_log_xml_warn(msg, "NoOp"); free_async_command(cmd); return NULL);
 291     CRM_CHECK(cmd->client != NULL, crm_log_xml_warn(msg, "NoClient"));
 292 
 293     cmd->done_cb = st_child_done;
 294     cmd_list = g_list_append(cmd_list, cmd);
 295     return cmd;
 296 }
 297 
 298 static int
 299 get_action_limit(stonith_device_t * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301     const char *value = NULL;
 302     int action_limit = 1;
 303 
 304     value = g_hash_table_lookup(device->params, PCMK_STONITH_ACTION_LIMIT);
 305     if ((value == NULL)
 306         || (pcmk__scan_min_int(value, &action_limit, INT_MIN) != pcmk_rc_ok)
 307         || (action_limit == 0)) {
 308         action_limit = 1;
 309     }
 310     return action_limit;
 311 }
 312 
 313 static int
 314 get_active_cmds(stonith_device_t * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 315 {
 316     int counter = 0;
 317     GList *gIter = NULL;
 318     GList *gIterNext = NULL;
 319 
 320     CRM_CHECK(device != NULL, return 0);
 321 
 322     for (gIter = cmd_list; gIter != NULL; gIter = gIterNext) {
 323         async_command_t *cmd = gIter->data;
 324 
 325         gIterNext = gIter->next;
 326 
 327         if (cmd->active_on == device) {
 328             counter++;
 329         }
 330     }
 331 
 332     return counter;
 333 }
 334 
 335 static void
 336 fork_cb(int pid, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338     async_command_t *cmd = (async_command_t *) user_data;
 339     stonith_device_t * device =
 340         /* in case of a retry we've done the move from
 341            activating_on to active_on already
 342          */
 343         cmd->activating_on?cmd->activating_on:cmd->active_on;
 344 
 345     CRM_ASSERT(device);
 346     crm_debug("Operation '%s' [%d]%s%s using %s now running with %ds timeout",
 347               cmd->action, pid,
 348               ((cmd->victim == NULL)? "" : " targeting "),
 349               ((cmd->victim == NULL)? "" : cmd->victim),
 350               device->id, cmd->timeout);
 351     cmd->active_on = device;
 352     cmd->activating_on = NULL;
 353 }
 354 
 355 static int
 356 get_agent_metadata_cb(gpointer data) {
     /* [previous][next][first][last][top][bottom][index][help] */
 357     stonith_device_t *device = data;
 358     guint period_ms;
 359 
 360     switch (get_agent_metadata(device->agent, &device->agent_metadata)) {
 361         case pcmk_rc_ok:
 362             if (device->agent_metadata) {
 363                 read_action_metadata(device);
 364                 stonith__device_parameter_flags(&(device->flags), device->id,
 365                                         device->agent_metadata);
 366             }
 367             return G_SOURCE_REMOVE;
 368 
 369         case EAGAIN:
 370             period_ms = pcmk__mainloop_timer_get_period(device->timer);
 371             if (period_ms < 160 * 1000) {
 372                 mainloop_timer_set_period(device->timer, 2 * period_ms);
 373             }
 374             return G_SOURCE_CONTINUE;
 375 
 376         default:
 377             return G_SOURCE_REMOVE;
 378     }
 379 }
 380 
 381 /*!
 382  * \internal
 383  * \brief Call a command's action callback for an internal (not library) result
 384  *
 385  * \param[in] cmd               Command to report result for
 386  * \param[in] execution_status  Execution status to use for result
 387  * \param[in] exit_status       Exit status to use for result
 388  * \param[in] exit_reason       Exit reason to use for result
 389  */
 390 static void
 391 report_internal_result(async_command_t *cmd, int exit_status,
     /* [previous][next][first][last][top][bottom][index][help] */
 392                        int execution_status, const char *exit_reason)
 393 {
 394     pcmk__action_result_t result = PCMK__UNKNOWN_RESULT;
 395 
 396     pcmk__set_result(&result, exit_status, execution_status, exit_reason);
 397     cmd->done_cb(0, &result, cmd);
 398     pcmk__reset_result(&result);
 399 }
 400 
 401 static gboolean
 402 stonith_device_execute(stonith_device_t * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 403 {
 404     int exec_rc = 0;
 405     const char *action_str = NULL;
 406     const char *host_arg = NULL;
 407     async_command_t *cmd = NULL;
 408     stonith_action_t *action = NULL;
 409     int active_cmds = 0;
 410     int action_limit = 0;
 411     GList *gIter = NULL;
 412     GList *gIterNext = NULL;
 413 
 414     CRM_CHECK(device != NULL, return FALSE);
 415 
 416     active_cmds = get_active_cmds(device);
 417     action_limit = get_action_limit(device);
 418     if (action_limit > -1 && active_cmds >= action_limit) {
 419         crm_trace("%s is over its action limit of %d (%u active action%s)",
 420                   device->id, action_limit, active_cmds,
 421                   pcmk__plural_s(active_cmds));
 422         return TRUE;
 423     }
 424 
 425     for (gIter = device->pending_ops; gIter != NULL; gIter = gIterNext) {
 426         async_command_t *pending_op = gIter->data;
 427 
 428         gIterNext = gIter->next;
 429 
 430         if (pending_op && pending_op->delay_id) {
 431             crm_trace("Operation '%s'%s%s using %s was asked to run too early, "
 432                       "waiting for start delay of %ds",
 433                       pending_op->action,
 434                       ((pending_op->victim == NULL)? "" : " targeting "),
 435                       ((pending_op->victim == NULL)? "" : pending_op->victim),
 436                       device->id, pending_op->start_delay);
 437             continue;
 438         }
 439 
 440         device->pending_ops = g_list_remove_link(device->pending_ops, gIter);
 441         g_list_free_1(gIter);
 442 
 443         cmd = pending_op;
 444         break;
 445     }
 446 
 447     if (cmd == NULL) {
 448         crm_trace("No actions using %s are needed", device->id);
 449         return TRUE;
 450     }
 451 
 452     if (pcmk__str_any_of(device->agent, STONITH_WATCHDOG_AGENT,
 453                          STONITH_WATCHDOG_AGENT_INTERNAL, NULL)) {
 454         if (pcmk__is_fencing_action(cmd->action)) {
 455             if (node_does_watchdog_fencing(stonith_our_uname)) {
 456                 pcmk__panic(__func__);
 457                 goto done;
 458             }
 459         } else {
 460             crm_info("Faking success for %s watchdog operation", cmd->action);
 461             report_internal_result(cmd, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
 462             goto done;
 463         }
 464     }
 465 
 466 #if SUPPORT_CIBSECRETS
 467     exec_rc = pcmk__substitute_secrets(device->id, device->params);
 468     if (exec_rc != pcmk_rc_ok) {
 469         if (pcmk__str_eq(cmd->action, "stop", pcmk__str_casei)) {
 470             crm_info("Proceeding with stop operation for %s "
 471                      "despite being unable to load CIB secrets (%s)",
 472                      device->id, pcmk_rc_str(exec_rc));
 473         } else {
 474             crm_err("Considering %s unconfigured "
 475                     "because unable to load CIB secrets: %s",
 476                      device->id, pcmk_rc_str(exec_rc));
 477             report_internal_result(cmd, CRM_EX_ERROR, PCMK_EXEC_NO_SECRETS,
 478                                    "Failed to get CIB secrets");
 479             goto done;
 480         }
 481     }
 482 #endif
 483 
 484     action_str = cmd->action;
 485     if (pcmk__str_eq(cmd->action, "reboot", pcmk__str_casei)
 486         && !pcmk_is_set(device->flags, st_device_supports_reboot)) {
 487 
 488         crm_notice("Remapping 'reboot' action%s%s using %s to 'off' "
 489                    "because agent '%s' does not support reboot",
 490                    ((cmd->victim == NULL)? "" : " targeting "),
 491                    ((cmd->victim == NULL)? "" : cmd->victim),
 492                    device->id, device->agent);
 493         action_str = "off";
 494     }
 495 
 496     if (pcmk_is_set(device->flags, st_device_supports_parameter_port)) {
 497         host_arg = "port";
 498 
 499     } else if (pcmk_is_set(device->flags, st_device_supports_parameter_plug)) {
 500         host_arg = "plug";
 501     }
 502 
 503     action = stonith_action_create(device->agent,
 504                                    action_str,
 505                                    cmd->victim,
 506                                    cmd->victim_nodeid,
 507                                    cmd->timeout, device->params,
 508                                    device->aliases, host_arg);
 509 
 510     /* for async exec, exec_rc is negative for early error exit
 511        otherwise handling of success/errors is done via callbacks */
 512     cmd->activating_on = device;
 513     exec_rc = stonith_action_execute_async(action, (void *)cmd,
 514                                            cmd->done_cb, fork_cb);
 515     if (exec_rc < 0) {
 516         cmd->activating_on = NULL;
 517         cmd->done_cb(0, stonith__action_result(action), cmd);
 518         stonith__destroy_action(action);
 519     }
 520 
 521 done:
 522     /* Device might get triggered to work by multiple fencing commands
 523      * simultaneously. Trigger the device again to make sure any
 524      * remaining concurrent commands get executed. */
 525     if (device->pending_ops) {
 526         mainloop_set_trigger(device->work);
 527     }
 528     return TRUE;
 529 }
 530 
 531 static gboolean
 532 stonith_device_dispatch(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 533 {
 534     return stonith_device_execute(user_data);
 535 }
 536 
 537 static gboolean
 538 start_delay_helper(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 539 {
 540     async_command_t *cmd = data;
 541     stonith_device_t *device = NULL;
 542 
 543     cmd->delay_id = 0;
 544     device = cmd->device ? g_hash_table_lookup(device_list, cmd->device) : NULL;
 545 
 546     if (device) {
 547         mainloop_set_trigger(device->work);
 548     }
 549 
 550     return FALSE;
 551 }
 552 
 553 static void
 554 schedule_stonith_command(async_command_t * cmd, stonith_device_t * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 555 {
 556     int delay_max = 0;
 557     int delay_base = 0;
 558     int requested_delay = cmd->start_delay;
 559 
 560     CRM_CHECK(cmd != NULL, return);
 561     CRM_CHECK(device != NULL, return);
 562 
 563     if (cmd->device) {
 564         free(cmd->device);
 565     }
 566 
 567     if (device->include_nodeid && cmd->victim) {
 568         crm_node_t *node = crm_get_peer(0, cmd->victim);
 569 
 570         cmd->victim_nodeid = node->id;
 571     }
 572 
 573     cmd->device = strdup(device->id);
 574     cmd->timeout = get_action_timeout(device, cmd->action, cmd->default_timeout);
 575 
 576     if (cmd->remote_op_id) {
 577         crm_debug("Scheduling '%s' action%s%s using %s for remote peer %s "
 578                   "with op id %.8s and timeout %ds",
 579                   cmd->action,
 580                   cmd->victim ? " targeting " : "", cmd->victim ? cmd->victim : "",
 581                   device->id, cmd->origin, cmd->remote_op_id, cmd->timeout);
 582     } else {
 583         crm_debug("Scheduling '%s' action%s%s using %s for %s with timeout %ds",
 584                   cmd->action,
 585                   cmd->victim ? " targeting " : "", cmd->victim ? cmd->victim : "",
 586                   device->id, cmd->client, cmd->timeout);
 587     }
 588 
 589     device->pending_ops = g_list_append(device->pending_ops, cmd);
 590     mainloop_set_trigger(device->work);
 591 
 592     // Value -1 means disable any static/random fencing delays
 593     if (requested_delay < 0) {
 594         return;
 595     }
 596 
 597     delay_max = get_action_delay_max(device, cmd->action);
 598     delay_base = get_action_delay_base(device, cmd->action, cmd->victim);
 599     if (delay_max == 0) {
 600         delay_max = delay_base;
 601     }
 602     if (delay_max < delay_base) {
 603         crm_warn(PCMK_STONITH_DELAY_BASE " (%ds) is larger than "
 604                  PCMK_STONITH_DELAY_MAX " (%ds) for %s using %s "
 605                  "(limiting to maximum delay)",
 606                  delay_base, delay_max, cmd->action, device->id);
 607         delay_base = delay_max;
 608     }
 609     if (delay_max > 0) {
 610         // coverity[dont_call] We're not using rand() for security
 611         cmd->start_delay +=
 612             ((delay_max != delay_base)?(rand() % (delay_max - delay_base)):0)
 613             + delay_base;
 614     }
 615 
 616     if (cmd->start_delay > 0) {
 617         crm_notice("Delaying '%s' action%s%s using %s for %ds " CRM_XS
 618                    " timeout=%ds requested_delay=%ds base=%ds max=%ds",
 619                    cmd->action,
 620                    cmd->victim ? " targeting " : "", cmd->victim ? cmd->victim : "",
 621                    device->id, cmd->start_delay, cmd->timeout,
 622                    requested_delay, delay_base, delay_max);
 623         cmd->delay_id =
 624             g_timeout_add_seconds(cmd->start_delay, start_delay_helper, cmd);
 625     }
 626 }
 627 
 628 static void
 629 free_device(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 630 {
 631     GList *gIter = NULL;
 632     stonith_device_t *device = data;
 633 
 634     g_hash_table_destroy(device->params);
 635     g_hash_table_destroy(device->aliases);
 636 
 637     for (gIter = device->pending_ops; gIter != NULL; gIter = gIter->next) {
 638         async_command_t *cmd = gIter->data;
 639 
 640         crm_warn("Removal of device '%s' purged operation '%s'", device->id, cmd->action);
 641         report_internal_result(cmd, CRM_EX_ERROR, PCMK_EXEC_NO_FENCE_DEVICE,
 642                                "Device was removed before action could be executed");
 643     }
 644     g_list_free(device->pending_ops);
 645 
 646     g_list_free_full(device->targets, free);
 647 
 648     if (device->timer) {
 649         mainloop_timer_stop(device->timer);
 650         mainloop_timer_del(device->timer);
 651     }
 652 
 653     mainloop_destroy_trigger(device->work);
 654 
 655     free_xml(device->agent_metadata);
 656     free(device->namespace);
 657     free(device->on_target_actions);
 658     free(device->agent);
 659     free(device->id);
 660     free(device);
 661 }
 662 
 663 void free_device_list(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 664 {
 665     if (device_list != NULL) {
 666         g_hash_table_destroy(device_list);
 667         device_list = NULL;
 668     }
 669 }
 670 
 671 void
 672 init_device_list(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 673 {
 674     if (device_list == NULL) {
 675         device_list = pcmk__strkey_table(NULL, free_device);
 676     }
 677 }
 678 
 679 static GHashTable *
 680 build_port_aliases(const char *hostmap, GList ** targets)
     /* [previous][next][first][last][top][bottom][index][help] */
 681 {
 682     char *name = NULL;
 683     int last = 0, lpc = 0, max = 0, added = 0;
 684     GHashTable *aliases = pcmk__strikey_table(free, free);
 685 
 686     if (hostmap == NULL) {
 687         return aliases;
 688     }
 689 
 690     max = strlen(hostmap);
 691     for (; lpc <= max; lpc++) {
 692         switch (hostmap[lpc]) {
 693                 /* Skip escaped chars */
 694             case '\\':
 695                 lpc++;
 696                 break;
 697 
 698                 /* Assignment chars */
 699             case '=':
 700             case ':':
 701                 if (lpc > last) {
 702                     free(name);
 703                     name = calloc(1, 1 + lpc - last);
 704                     memcpy(name, hostmap + last, lpc - last);
 705                 }
 706                 last = lpc + 1;
 707                 break;
 708 
 709                 /* Delimeter chars */
 710                 /* case ',': Potentially used to specify multiple ports */
 711             case 0:
 712             case ';':
 713             case ' ':
 714             case '\t':
 715                 if (name) {
 716                     char *value = NULL;
 717                     int k = 0;
 718 
 719                     value = calloc(1, 1 + lpc - last);
 720                     memcpy(value, hostmap + last, lpc - last);
 721 
 722                     for (int i = 0; value[i] != '\0'; i++) {
 723                         if (value[i] != '\\') {
 724                             value[k++] = value[i];
 725                         }
 726                     }
 727                     value[k] = '\0';
 728 
 729                     crm_debug("Adding alias '%s'='%s'", name, value);
 730                     g_hash_table_replace(aliases, name, value);
 731                     if (targets) {
 732                         *targets = g_list_append(*targets, strdup(value));
 733                     }
 734                     value = NULL;
 735                     name = NULL;
 736                     added++;
 737 
 738                 } else if (lpc > last) {
 739                     crm_debug("Parse error at offset %d near '%s'", lpc - last, hostmap + last);
 740                 }
 741 
 742                 last = lpc + 1;
 743                 break;
 744         }
 745 
 746         if (hostmap[lpc] == 0) {
 747             break;
 748         }
 749     }
 750 
 751     if (added == 0) {
 752         crm_info("No host mappings detected in '%s'", hostmap);
 753     }
 754 
 755     free(name);
 756     return aliases;
 757 }
 758 
 759 GHashTable *metadata_cache = NULL;
 760 
 761 void
 762 free_metadata_cache(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 763     if (metadata_cache != NULL) {
 764         g_hash_table_destroy(metadata_cache);
 765         metadata_cache = NULL;
 766     }
 767 }
 768 
 769 static void
 770 init_metadata_cache(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 771     if (metadata_cache == NULL) {
 772         metadata_cache = pcmk__strkey_table(free, free);
 773     }
 774 }
 775 
 776 int
 777 get_agent_metadata(const char *agent, xmlNode ** metadata)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779     char *buffer = NULL;
 780 
 781     if (metadata == NULL) {
 782         return EINVAL;
 783     }
 784     *metadata = NULL;
 785     if (pcmk__str_eq(agent, STONITH_WATCHDOG_AGENT_INTERNAL, pcmk__str_none)) {
 786         return pcmk_rc_ok;
 787     }
 788     init_metadata_cache();
 789     buffer = g_hash_table_lookup(metadata_cache, agent);
 790     if (buffer == NULL) {
 791         stonith_t *st = stonith_api_new();
 792         int rc;
 793 
 794         if (st == NULL) {
 795             crm_warn("Could not get agent meta-data: "
 796                      "API memory allocation failed");
 797             return EAGAIN;
 798         }
 799         rc = st->cmds->metadata(st, st_opt_sync_call, agent,
 800                                 NULL, &buffer, 10);
 801         stonith_api_delete(st);
 802         if (rc || !buffer) {
 803             crm_err("Could not retrieve metadata for fencing agent %s", agent);
 804             return EAGAIN;
 805         }
 806         g_hash_table_replace(metadata_cache, strdup(agent), buffer);
 807     }
 808 
 809     *metadata = string2xml(buffer);
 810     return pcmk_rc_ok;
 811 }
 812 
 813 static gboolean
 814 is_nodeid_required(xmlNode * xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 815 {
 816     xmlXPathObjectPtr xpath = NULL;
 817 
 818     if (stand_alone) {
 819         return FALSE;
 820     }
 821 
 822     if (!xml) {
 823         return FALSE;
 824     }
 825 
 826     xpath = xpath_search(xml, "//parameter[@name='nodeid']");
 827     if (numXpathResults(xpath)  <= 0) {
 828         freeXpathObject(xpath);
 829         return FALSE;
 830     }
 831 
 832     freeXpathObject(xpath);
 833     return TRUE;
 834 }
 835 
 836 #define MAX_ACTION_LEN 256
 837 
 838 static char *
 839 add_action(char *actions, const char *action)
     /* [previous][next][first][last][top][bottom][index][help] */
 840 {
 841     int offset = 0;
 842 
 843     if (actions == NULL) {
 844         actions = calloc(1, MAX_ACTION_LEN);
 845     } else {
 846         offset = strlen(actions);
 847     }
 848 
 849     if (offset > 0) {
 850         offset += snprintf(actions+offset, MAX_ACTION_LEN - offset, " ");
 851     }
 852     offset += snprintf(actions+offset, MAX_ACTION_LEN - offset, "%s", action);
 853 
 854     return actions;
 855 }
 856 
 857 static void
 858 read_action_metadata(stonith_device_t *device)
     /* [previous][next][first][last][top][bottom][index][help] */
 859 {
 860     xmlXPathObjectPtr xpath = NULL;
 861     int max = 0;
 862     int lpc = 0;
 863 
 864     if (device->agent_metadata == NULL) {
 865         return;
 866     }
 867 
 868     xpath = xpath_search(device->agent_metadata, "//action");
 869     max = numXpathResults(xpath);
 870 
 871     if (max <= 0) {
 872         freeXpathObject(xpath);
 873         return;
 874     }
 875 
 876     for (lpc = 0; lpc < max; lpc++) {
 877         const char *action = NULL;
 878         xmlNode *match = getXpathResult(xpath, lpc);
 879 
 880         CRM_LOG_ASSERT(match != NULL);
 881         if(match == NULL) { continue; };
 882 
 883         action = crm_element_value(match, "name");
 884 
 885         if(pcmk__str_eq(action, "list", pcmk__str_casei)) {
 886             stonith__set_device_flags(device->flags, device->id,
 887                                       st_device_supports_list);
 888         } else if(pcmk__str_eq(action, "status", pcmk__str_casei)) {
 889             stonith__set_device_flags(device->flags, device->id,
 890                                       st_device_supports_status);
 891         } else if(pcmk__str_eq(action, "reboot", pcmk__str_casei)) {
 892             stonith__set_device_flags(device->flags, device->id,
 893                                       st_device_supports_reboot);
 894         } else if (pcmk__str_eq(action, "on", pcmk__str_casei)) {
 895             /* "automatic" means the cluster will unfence node when it joins */
 896             /* "required" is a deprecated synonym for "automatic" */
 897             if (pcmk__xe_attr_is_true(match, "automatic") || pcmk__xe_attr_is_true(match, "required")) {
 898                 device->automatic_unfencing = TRUE;
 899             }
 900         }
 901 
 902         if (action && pcmk__xe_attr_is_true(match, "on_target")) {
 903             device->on_target_actions = add_action(device->on_target_actions, action);
 904         }
 905     }
 906 
 907     freeXpathObject(xpath);
 908 }
 909 
 910 /*!
 911  * \internal
 912  * \brief Set a pcmk_*_action parameter if not already set
 913  *
 914  * \param[in,out] params  Device parameters
 915  * \param[in]     action  Name of action
 916  * \param[in]     value   Value to use if action is not already set
 917  */
 918 static void
 919 map_action(GHashTable *params, const char *action, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 920 {
 921     char *key = crm_strdup_printf("pcmk_%s_action", action);
 922 
 923     if (g_hash_table_lookup(params, key)) {
 924         crm_warn("Ignoring %s='%s', see %s instead",
 925                  STONITH_ATTR_ACTION_OP, value, key);
 926         free(key);
 927     } else {
 928         crm_warn("Mapping %s='%s' to %s='%s'",
 929                  STONITH_ATTR_ACTION_OP, value, key, value);
 930         g_hash_table_insert(params, key, strdup(value));
 931     }
 932 }
 933 
 934 /*!
 935  * \internal
 936  * \brief Create device parameter table from XML
 937  *
 938  * \param[in]     name    Device name (used for logging only)
 939  * \param[in,out] params  Device parameters
 940  */
 941 static GHashTable *
 942 xml2device_params(const char *name, xmlNode *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944     GHashTable *params = xml2list(dev);
 945     const char *value;
 946 
 947     /* Action should never be specified in the device configuration,
 948      * but we support it for users who are familiar with other software
 949      * that worked that way.
 950      */
 951     value = g_hash_table_lookup(params, STONITH_ATTR_ACTION_OP);
 952     if (value != NULL) {
 953         crm_warn("%s has '%s' parameter, which should never be specified in configuration",
 954                  name, STONITH_ATTR_ACTION_OP);
 955 
 956         if (*value == '\0') {
 957             crm_warn("Ignoring empty '%s' parameter", STONITH_ATTR_ACTION_OP);
 958 
 959         } else if (strcmp(value, "reboot") == 0) {
 960             crm_warn("Ignoring %s='reboot' (see stonith-action cluster property instead)",
 961                      STONITH_ATTR_ACTION_OP);
 962 
 963         } else if (strcmp(value, "off") == 0) {
 964             map_action(params, "reboot", value);
 965 
 966         } else {
 967             map_action(params, "off", value);
 968             map_action(params, "reboot", value);
 969         }
 970 
 971         g_hash_table_remove(params, STONITH_ATTR_ACTION_OP);
 972     }
 973 
 974     return params;
 975 }
 976 
 977 static const char *
 978 target_list_type(stonith_device_t * dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 979 {
 980     const char *check_type = NULL;
 981 
 982     check_type = g_hash_table_lookup(dev->params, PCMK_STONITH_HOST_CHECK);
 983 
 984     if (check_type == NULL) {
 985 
 986         if (g_hash_table_lookup(dev->params, PCMK_STONITH_HOST_LIST)) {
 987             check_type = "static-list";
 988         } else if (g_hash_table_lookup(dev->params, PCMK_STONITH_HOST_MAP)) {
 989             check_type = "static-list";
 990         } else if (pcmk_is_set(dev->flags, st_device_supports_list)) {
 991             check_type = "dynamic-list";
 992         } else if (pcmk_is_set(dev->flags, st_device_supports_status)) {
 993             check_type = "status";
 994         } else {
 995             check_type = PCMK__VALUE_NONE;
 996         }
 997     }
 998 
 999     return check_type;
1000 }
1001 
1002 static stonith_device_t *
1003 build_device_from_xml(xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
1004 {
1005     const char *value;
1006     xmlNode *dev = get_xpath_object("//" F_STONITH_DEVICE, msg, LOG_ERR);
1007     stonith_device_t *device = NULL;
1008     char *agent = crm_element_value_copy(dev, "agent");
1009 
1010     CRM_CHECK(agent != NULL, return device);
1011 
1012     device = calloc(1, sizeof(stonith_device_t));
1013 
1014     CRM_CHECK(device != NULL, {free(agent); return device;});
1015 
1016     device->id = crm_element_value_copy(dev, XML_ATTR_ID);
1017     device->agent = agent;
1018     device->namespace = crm_element_value_copy(dev, "namespace");
1019     device->params = xml2device_params(device->id, dev);
1020 
1021     value = g_hash_table_lookup(device->params, PCMK_STONITH_HOST_LIST);
1022     if (value) {
1023         device->targets = stonith__parse_targets(value);
1024     }
1025 
1026     value = g_hash_table_lookup(device->params, PCMK_STONITH_HOST_MAP);
1027     device->aliases = build_port_aliases(value, &(device->targets));
1028 
1029     value = target_list_type(device);
1030     if (!pcmk__str_eq(value, "static-list", pcmk__str_casei) && device->targets) {
1031         /* Other than "static-list", dev-> targets is unnecessary. */
1032         g_list_free_full(device->targets, free);
1033         device->targets = NULL;
1034     }
1035     switch (get_agent_metadata(device->agent, &device->agent_metadata)) {
1036         case pcmk_rc_ok:
1037             if (device->agent_metadata) {
1038                 read_action_metadata(device);
1039                 stonith__device_parameter_flags(&(device->flags), device->id,
1040                                                 device->agent_metadata);
1041             }
1042             break;
1043 
1044         case EAGAIN:
1045             if (device->timer == NULL) {
1046                 device->timer = mainloop_timer_add("get_agent_metadata", 10 * 1000,
1047                                            TRUE, get_agent_metadata_cb, device);
1048             }
1049             if (!mainloop_timer_running(device->timer)) {
1050                 mainloop_timer_start(device->timer);
1051             }
1052             break;
1053 
1054         default:
1055             break;
1056     }
1057 
1058     value = g_hash_table_lookup(device->params, "nodeid");
1059     if (!value) {
1060         device->include_nodeid = is_nodeid_required(device->agent_metadata);
1061     }
1062 
1063     value = crm_element_value(dev, "rsc_provides");
1064     if (pcmk__str_eq(value, "unfencing", pcmk__str_casei)) {
1065         device->automatic_unfencing = TRUE;
1066     }
1067 
1068     if (is_action_required("on", device)) {
1069         crm_info("Fencing device '%s' requires unfencing", device->id);
1070     }
1071 
1072     if (device->on_target_actions) {
1073         crm_info("Fencing device '%s' requires actions (%s) to be executed "
1074                  "on target", device->id, device->on_target_actions);
1075     }
1076 
1077     device->work = mainloop_add_trigger(G_PRIORITY_HIGH, stonith_device_dispatch, device);
1078     /* TODO: Hook up priority */
1079 
1080     return device;
1081 }
1082 
1083 static void
1084 schedule_internal_command(const char *origin,
     /* [previous][next][first][last][top][bottom][index][help] */
1085                           stonith_device_t * device,
1086                           const char *action,
1087                           const char *victim,
1088                           int timeout,
1089                           void *internal_user_data,
1090                           void (*done_cb) (int pid,
1091                                            const pcmk__action_result_t *result,
1092                                            void *user_data))
1093 {
1094     async_command_t *cmd = NULL;
1095 
1096     cmd = calloc(1, sizeof(async_command_t));
1097 
1098     cmd->id = -1;
1099     cmd->default_timeout = timeout ? timeout : 60;
1100     cmd->timeout = cmd->default_timeout;
1101     cmd->action = strdup(action);
1102     pcmk__str_update(&cmd->victim, victim);
1103     cmd->device = strdup(device->id);
1104     cmd->origin = strdup(origin);
1105     cmd->client = strdup(crm_system_name);
1106     cmd->client_name = strdup(crm_system_name);
1107 
1108     cmd->internal_user_data = internal_user_data;
1109     cmd->done_cb = done_cb; /* cmd, not internal_user_data, is passed to 'done_cb' as the userdata */
1110 
1111     schedule_stonith_command(cmd, device);
1112 }
1113 
1114 // Fence agent status commands use custom exit status codes
1115 enum fence_status_code {
1116     fence_status_invalid    = -1,
1117     fence_status_active     = 0,
1118     fence_status_unknown    = 1,
1119     fence_status_inactive   = 2,
1120 };
1121 
1122 static void
1123 status_search_cb(int pid, const pcmk__action_result_t *result, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
1124 {
1125     async_command_t *cmd = user_data;
1126     struct device_search_s *search = cmd->internal_user_data;
1127     stonith_device_t *dev = cmd->device ? g_hash_table_lookup(device_list, cmd->device) : NULL;
1128     gboolean can = FALSE;
1129 
1130     free_async_command(cmd);
1131 
1132     if (!dev) {
1133         search_devices_record_result(search, NULL, FALSE);
1134         return;
1135     }
1136 
1137     mainloop_set_trigger(dev->work);
1138 
1139     if (result->execution_status != PCMK_EXEC_DONE) {
1140         crm_warn("Assuming %s cannot fence %s "
1141                  "because status could not be executed: %s%s%s%s",
1142                  dev->id, search->host,
1143                  pcmk_exec_status_str(result->execution_status),
1144                  ((result->exit_reason == NULL)? "" : " ("),
1145                  ((result->exit_reason == NULL)? "" : result->exit_reason),
1146                  ((result->exit_reason == NULL)? "" : ")"));
1147         search_devices_record_result(search, dev->id, FALSE);
1148         return;
1149     }
1150 
1151     switch (result->exit_status) {
1152         case fence_status_unknown:
1153             crm_trace("%s reported it cannot fence %s", dev->id, search->host);
1154             break;
1155 
1156         case fence_status_active:
1157         case fence_status_inactive:
1158             crm_trace("%s reported it can fence %s", dev->id, search->host);
1159             can = TRUE;
1160             break;
1161 
1162         default:
1163             crm_warn("Assuming %s cannot fence %s "
1164                      "(status returned unknown code %d)",
1165                      dev->id, search->host, result->exit_status);
1166             break;
1167     }
1168     search_devices_record_result(search, dev->id, can);
1169 }
1170 
1171 static void
1172 dynamic_list_search_cb(int pid, const pcmk__action_result_t *result,
     /* [previous][next][first][last][top][bottom][index][help] */
1173                        void *user_data)
1174 {
1175     async_command_t *cmd = user_data;
1176     struct device_search_s *search = cmd->internal_user_data;
1177     stonith_device_t *dev = cmd->device ? g_hash_table_lookup(device_list, cmd->device) : NULL;
1178     gboolean can_fence = FALSE;
1179 
1180     free_async_command(cmd);
1181 
1182     /* Host/alias must be in the list output to be eligible to be fenced
1183      *
1184      * Will cause problems if down'd nodes aren't listed or (for virtual nodes)
1185      *  if the guest is still listed despite being moved to another machine
1186      */
1187     if (!dev) {
1188         search_devices_record_result(search, NULL, FALSE);
1189         return;
1190     }
1191 
1192     mainloop_set_trigger(dev->work);
1193 
1194     if (pcmk__result_ok(result)) {
1195         crm_info("Refreshing target list for %s", dev->id);
1196         g_list_free_full(dev->targets, free);
1197         dev->targets = stonith__parse_targets(result->action_stdout);
1198         dev->targets_age = time(NULL);
1199 
1200     } else if (dev->targets != NULL) {
1201         if (result->execution_status == PCMK_EXEC_DONE) {
1202             crm_info("Reusing most recent target list for %s "
1203                      "because list returned error code %d",
1204                      dev->id, result->exit_status);
1205         } else {
1206             crm_info("Reusing most recent target list for %s "
1207                      "because list could not be executed: %s%s%s%s",
1208                      dev->id, pcmk_exec_status_str(result->execution_status),
1209                      ((result->exit_reason == NULL)? "" : " ("),
1210                      ((result->exit_reason == NULL)? "" : result->exit_reason),
1211                      ((result->exit_reason == NULL)? "" : ")"));
1212         }
1213 
1214     } else { // We have never successfully executed list
1215         if (result->execution_status == PCMK_EXEC_DONE) {
1216             crm_warn("Assuming %s cannot fence %s "
1217                      "because list returned error code %d",
1218                      dev->id, search->host, result->exit_status);
1219         } else {
1220             crm_warn("Assuming %s cannot fence %s "
1221                      "because list could not be executed: %s%s%s%s",
1222                      dev->id, search->host,
1223                      pcmk_exec_status_str(result->execution_status),
1224                      ((result->exit_reason == NULL)? "" : " ("),
1225                      ((result->exit_reason == NULL)? "" : result->exit_reason),
1226                      ((result->exit_reason == NULL)? "" : ")"));
1227         }
1228 
1229         /* Fall back to pcmk_host_check="status" if the user didn't explicitly
1230          * specify "dynamic-list".
1231          */
1232         if (g_hash_table_lookup(dev->params, PCMK_STONITH_HOST_CHECK) == NULL) {
1233             crm_notice("Switching to pcmk_host_check='status' for %s", dev->id);
1234             g_hash_table_replace(dev->params, strdup(PCMK_STONITH_HOST_CHECK),
1235                                  strdup("status"));
1236         }
1237     }
1238 
1239     if (dev->targets) {
1240         const char *alias = g_hash_table_lookup(dev->aliases, search->host);
1241 
1242         if (!alias) {
1243             alias = search->host;
1244         }
1245         if (pcmk__str_in_list(alias, dev->targets, pcmk__str_casei)) {
1246             can_fence = TRUE;
1247         }
1248     }
1249     search_devices_record_result(search, dev->id, can_fence);
1250 }
1251 
1252 /*!
1253  * \internal
1254  * \brief Returns true if any key in first is not in second or second has a different value for key
1255  */
1256 static int
1257 device_params_diff(GHashTable *first, GHashTable *second) {
     /* [previous][next][first][last][top][bottom][index][help] */
1258     char *key = NULL;
1259     char *value = NULL;
1260     GHashTableIter gIter;
1261 
1262     g_hash_table_iter_init(&gIter, first);
1263     while (g_hash_table_iter_next(&gIter, (void **)&key, (void **)&value)) {
1264 
1265         if(strstr(key, "CRM_meta") == key) {
1266             continue;
1267         } else if(strcmp(key, "crm_feature_set") == 0) {
1268             continue;
1269         } else {
1270             char *other_value = g_hash_table_lookup(second, key);
1271 
1272             if (!other_value || !pcmk__str_eq(other_value, value, pcmk__str_casei)) {
1273                 crm_trace("Different value for %s: %s != %s", key, other_value, value);
1274                 return 1;
1275             }
1276         }
1277     }
1278 
1279     return 0;
1280 }
1281 
1282 /*!
1283  * \internal
1284  * \brief Checks to see if an identical device already exists in the device_list
1285  */
1286 static stonith_device_t *
1287 device_has_duplicate(stonith_device_t * device)
     /* [previous][next][first][last][top][bottom][index][help] */
1288 {
1289     stonith_device_t *dup = g_hash_table_lookup(device_list, device->id);
1290 
1291     if (!dup) {
1292         crm_trace("No match for %s", device->id);
1293         return NULL;
1294 
1295     } else if (!pcmk__str_eq(dup->agent, device->agent, pcmk__str_casei)) {
1296         crm_trace("Different agent: %s != %s", dup->agent, device->agent);
1297         return NULL;
1298     }
1299 
1300     /* Use calculate_operation_digest() here? */
1301     if (device_params_diff(device->params, dup->params) ||
1302         device_params_diff(dup->params, device->params)) {
1303         return NULL;
1304     }
1305 
1306     crm_trace("Match");
1307     return dup;
1308 }
1309 
1310 int
1311 stonith_device_register(xmlNode * msg, const char **desc, gboolean from_cib)
     /* [previous][next][first][last][top][bottom][index][help] */
1312 {
1313     stonith_device_t *dup = NULL;
1314     stonith_device_t *device = build_device_from_xml(msg);
1315     guint ndevices = 0;
1316     int rv = pcmk_ok;
1317 
1318     CRM_CHECK(device != NULL, return -ENOMEM);
1319 
1320     /* do we have a watchdog-device? */
1321     if (pcmk__str_eq(device->id, STONITH_WATCHDOG_ID, pcmk__str_none) ||
1322         pcmk__str_any_of(device->agent, STONITH_WATCHDOG_AGENT,
1323                      STONITH_WATCHDOG_AGENT_INTERNAL, NULL)) do {
1324         if (stonith_watchdog_timeout_ms <= 0) {
1325             crm_err("Ignoring watchdog fence device without "
1326                     "stonith-watchdog-timeout set.");
1327             rv = -ENODEV;
1328             /* fall through to cleanup & return */
1329         } else if (!pcmk__str_any_of(device->agent, STONITH_WATCHDOG_AGENT,
1330                                  STONITH_WATCHDOG_AGENT_INTERNAL, NULL)) {
1331             crm_err("Ignoring watchdog fence device with unknown "
1332                     "agent '%s' unequal '" STONITH_WATCHDOG_AGENT "'.",
1333                     device->agent?device->agent:"");
1334             rv = -ENODEV;
1335             /* fall through to cleanup & return */
1336         } else if (!pcmk__str_eq(device->id, STONITH_WATCHDOG_ID,
1337                                  pcmk__str_none)) {
1338             crm_err("Ignoring watchdog fence device "
1339                     "named %s !='"STONITH_WATCHDOG_ID"'.",
1340                     device->id?device->id:"");
1341             rv = -ENODEV;
1342             /* fall through to cleanup & return */
1343         } else {
1344             if (pcmk__str_eq(device->agent, STONITH_WATCHDOG_AGENT,
1345                              pcmk__str_none)) {
1346                 /* this either has an empty list or the targets
1347                    configured for watchdog-fencing
1348                  */
1349                 g_list_free_full(stonith_watchdog_targets, free);
1350                 stonith_watchdog_targets = device->targets;
1351                 device->targets = NULL;
1352             }
1353             if (node_does_watchdog_fencing(stonith_our_uname)) {
1354                 g_list_free_full(device->targets, free);
1355                 device->targets = stonith__parse_targets(stonith_our_uname);
1356                 g_hash_table_replace(device->params,
1357                                      strdup(PCMK_STONITH_HOST_LIST),
1358                                      strdup(stonith_our_uname));
1359                 /* proceed as with any other stonith-device */
1360                 break;
1361             }
1362 
1363             crm_debug("Skip registration of watchdog fence device on node not in host-list.");
1364             /* cleanup and fall through to more cleanup and return */
1365             device->targets = NULL;
1366             stonith_device_remove(device->id, from_cib);
1367         }
1368         free_device(device);
1369         return rv;
1370     } while (0);
1371 
1372     dup = device_has_duplicate(device);
1373     if (dup) {
1374         ndevices = g_hash_table_size(device_list);
1375         crm_debug("Device '%s' already in device list (%d active device%s)",
1376                   device->id, ndevices, pcmk__plural_s(ndevices));
1377         free_device(device);
1378         device = dup;
1379         dup = g_hash_table_lookup(device_list, device->id);
1380         dup->dirty = FALSE;
1381 
1382     } else {
1383         stonith_device_t *old = g_hash_table_lookup(device_list, device->id);
1384 
1385         if (from_cib && old && old->api_registered) {
1386             /* If the cib is writing over an entry that is shared with a stonith client,
1387              * copy any pending ops that currently exist on the old entry to the new one.
1388              * Otherwise the pending ops will be reported as failures
1389              */
1390             crm_info("Overwriting existing entry for %s from CIB", device->id);
1391             device->pending_ops = old->pending_ops;
1392             device->api_registered = TRUE;
1393             old->pending_ops = NULL;
1394             if (device->pending_ops) {
1395                 mainloop_set_trigger(device->work);
1396             }
1397         }
1398         g_hash_table_replace(device_list, device->id, device);
1399 
1400         ndevices = g_hash_table_size(device_list);
1401         crm_notice("Added '%s' to device list (%d active device%s)",
1402                    device->id, ndevices, pcmk__plural_s(ndevices));
1403     }
1404     if (desc) {
1405         *desc = device->id;
1406     }
1407 
1408     if (from_cib) {
1409         device->cib_registered = TRUE;
1410     } else {
1411         device->api_registered = TRUE;
1412     }
1413 
1414     return pcmk_ok;
1415 }
1416 
1417 void
1418 stonith_device_remove(const char *id, bool from_cib)
     /* [previous][next][first][last][top][bottom][index][help] */
1419 {
1420     stonith_device_t *device = g_hash_table_lookup(device_list, id);
1421     guint ndevices = 0;
1422 
1423     if (!device) {
1424         ndevices = g_hash_table_size(device_list);
1425         crm_info("Device '%s' not found (%d active device%s)",
1426                  id, ndevices, pcmk__plural_s(ndevices));
1427         return;
1428     }
1429 
1430     if (from_cib) {
1431         device->cib_registered = FALSE;
1432     } else {
1433         device->verified = FALSE;
1434         device->api_registered = FALSE;
1435     }
1436 
1437     if (!device->cib_registered && !device->api_registered) {
1438         g_hash_table_remove(device_list, id);
1439         ndevices = g_hash_table_size(device_list);
1440         crm_info("Removed '%s' from device list (%d active device%s)",
1441                  id, ndevices, pcmk__plural_s(ndevices));
1442     } else {
1443         crm_trace("Not removing '%s' from device list (%d active) because "
1444                   "still registered via:%s%s",
1445                   id, g_hash_table_size(device_list),
1446                   (device->cib_registered? " cib" : ""),
1447                   (device->api_registered? " api" : ""));
1448     }
1449 }
1450 
1451 /*!
1452  * \internal
1453  * \brief Return the number of stonith levels registered for a node
1454  *
1455  * \param[in] tp  Node's topology table entry
1456  *
1457  * \return Number of non-NULL levels in topology entry
1458  * \note This function is used only for log messages.
1459  */
1460 static int
1461 count_active_levels(stonith_topology_t * tp)
     /* [previous][next][first][last][top][bottom][index][help] */
1462 {
1463     int lpc = 0;
1464     int count = 0;
1465 
1466     for (lpc = 0; lpc < ST_LEVEL_MAX; lpc++) {
1467         if (tp->levels[lpc] != NULL) {
1468             count++;
1469         }
1470     }
1471     return count;
1472 }
1473 
1474 static void
1475 free_topology_entry(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
1476 {
1477     stonith_topology_t *tp = data;
1478 
1479     int lpc = 0;
1480 
1481     for (lpc = 0; lpc < ST_LEVEL_MAX; lpc++) {
1482         if (tp->levels[lpc] != NULL) {
1483             g_list_free_full(tp->levels[lpc], free);
1484         }
1485     }
1486     free(tp->target);
1487     free(tp->target_value);
1488     free(tp->target_pattern);
1489     free(tp->target_attribute);
1490     free(tp);
1491 }
1492 
1493 void
1494 free_topology_list(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1495 {
1496     if (topology != NULL) {
1497         g_hash_table_destroy(topology);
1498         topology = NULL;
1499     }
1500 }
1501 
1502 void
1503 init_topology_list(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1504 {
1505     if (topology == NULL) {
1506         topology = pcmk__strkey_table(NULL, free_topology_entry);
1507     }
1508 }
1509 
1510 char *
1511 stonith_level_key(xmlNode *level, enum fenced_target_by mode)
     /* [previous][next][first][last][top][bottom][index][help] */
1512 {
1513     if (mode == fenced_target_by_unknown) {
1514         mode = unpack_level_kind(level);
1515     }
1516     switch (mode) {
1517         case fenced_target_by_name:
1518             return crm_element_value_copy(level, XML_ATTR_STONITH_TARGET);
1519 
1520         case fenced_target_by_pattern:
1521             return crm_element_value_copy(level, XML_ATTR_STONITH_TARGET_PATTERN);
1522 
1523         case fenced_target_by_attribute:
1524             return crm_strdup_printf("%s=%s",
1525                 crm_element_value(level, XML_ATTR_STONITH_TARGET_ATTRIBUTE),
1526                 crm_element_value(level, XML_ATTR_STONITH_TARGET_VALUE));
1527 
1528         default:
1529             return crm_strdup_printf("unknown-%s", ID(level));
1530     }
1531 }
1532 
1533 /*!
1534  * \internal
1535  * \brief Parse target identification from topology level XML
1536  *
1537  * \param[in] level  Topology level XML to parse
1538  *
1539  * \return How to identify target of \p level
1540  */
1541 static int
1542 unpack_level_kind(xmlNode *level)
     /* [previous][next][first][last][top][bottom][index][help] */
1543 {
1544     if (crm_element_value(level, XML_ATTR_STONITH_TARGET) != NULL) {
1545         return fenced_target_by_name;
1546     }
1547     if (crm_element_value(level, XML_ATTR_STONITH_TARGET_PATTERN) != NULL) {
1548         return fenced_target_by_pattern;
1549     }
1550     if (!stand_alone /* if standalone, there's no attribute manager */
1551         && (crm_element_value(level, XML_ATTR_STONITH_TARGET_ATTRIBUTE) != NULL)
1552         && (crm_element_value(level, XML_ATTR_STONITH_TARGET_VALUE) == NULL)) {
1553         return fenced_target_by_attribute;
1554     }
1555     return fenced_target_by_unknown;
1556 }
1557 
1558 static stonith_key_value_t *
1559 parse_device_list(const char *devices)
     /* [previous][next][first][last][top][bottom][index][help] */
1560 {
1561     int lpc = 0;
1562     int max = 0;
1563     int last = 0;
1564     stonith_key_value_t *output = NULL;
1565 
1566     if (devices == NULL) {
1567         return output;
1568     }
1569 
1570     max = strlen(devices);
1571     for (lpc = 0; lpc <= max; lpc++) {
1572         if (devices[lpc] == ',' || devices[lpc] == 0) {
1573             char *line = strndup(devices + last, lpc - last);
1574 
1575             output = stonith_key_value_add(output, NULL, line);
1576             free(line);
1577 
1578             last = lpc + 1;
1579         }
1580     }
1581 
1582     return output;
1583 }
1584 
1585 /*!
1586  * \internal
1587  * \brief Register a fencing topology level for a target
1588  *
1589  * Given an XML request specifying the target name, level index, and device IDs
1590  * for the level, this will create an entry for the target in the global topology
1591  * table if one does not already exist, then append the specified device IDs to
1592  * the entry's device list for the specified level.
1593  *
1594  * \param[in]  msg     XML request for STONITH level registration
1595  * \param[out] desc    If not NULL, set to string representation "TARGET[LEVEL]"
1596  * \param[out] result  Where to set result of registration
1597  */
1598 void
1599 fenced_register_level(xmlNode *msg, char **desc, pcmk__action_result_t *result)
     /* [previous][next][first][last][top][bottom][index][help] */
1600 {
1601     int id = 0;
1602     xmlNode *level;
1603     enum fenced_target_by mode;
1604     char *target;
1605 
1606     stonith_topology_t *tp;
1607     stonith_key_value_t *dIter = NULL;
1608     stonith_key_value_t *devices = NULL;
1609 
1610     CRM_CHECK(result != NULL, return);
1611 
1612     if (msg == NULL) {
1613         fenced_set_protocol_error(result);
1614         return;
1615     }
1616 
1617     /* Allow the XML here to point to the level tag directly, or wrapped in
1618      * another tag. If directly, don't search by xpath, because it might give
1619      * multiple hits (e.g. if the XML is the CIB).
1620      */
1621     if (pcmk__str_eq(TYPE(msg), XML_TAG_FENCING_LEVEL, pcmk__str_casei)) {
1622         level = msg;
1623     } else {
1624         level = get_xpath_object("//" XML_TAG_FENCING_LEVEL, msg, LOG_WARNING);
1625     }
1626     if (level == NULL) {
1627         fenced_set_protocol_error(result);
1628         return;
1629     }
1630 
1631     mode = unpack_level_kind(level);
1632     target = stonith_level_key(level, mode);
1633     crm_element_value_int(level, XML_ATTR_STONITH_INDEX, &id);
1634 
1635     if (desc) {
1636         *desc = crm_strdup_printf("%s[%d]", target, id);
1637     }
1638 
1639     // Ensure a valid target was specified
1640     if (mode == fenced_target_by_unknown) {
1641         crm_warn("Ignoring registration for topology level '%s' "
1642                  "without valid target", crm_str(ID(level)));
1643         free(target);
1644         crm_log_xml_info(level, "Bad level");
1645         pcmk__format_result(result, CRM_EX_INVALID_PARAM, PCMK_EXEC_INVALID,
1646                             "Invalid target for topology level '%s'",
1647                             crm_str(ID(level)));
1648         return;
1649     }
1650 
1651     // Ensure level ID is in allowed range
1652     if ((id <= 0) || (id >= ST_LEVEL_MAX)) {
1653         crm_warn("Ignoring topology registration for %s with invalid level %d",
1654                   target, id);
1655         free(target);
1656         crm_log_xml_info(level, "Bad level");
1657         pcmk__format_result(result, CRM_EX_INVALID_PARAM, PCMK_EXEC_INVALID,
1658                             "Invalid level number '%s' for topology level '%s'",
1659                             crm_str(crm_element_value(level,
1660                                                       XML_ATTR_STONITH_INDEX)),
1661                             crm_str(ID(level)));
1662         return;
1663     }
1664 
1665     /* Find or create topology table entry */
1666     tp = g_hash_table_lookup(topology, target);
1667     if (tp == NULL) {
1668         tp = calloc(1, sizeof(stonith_topology_t));
1669         if (tp == NULL) {
1670             pcmk__set_result(result, CRM_EX_ERROR, PCMK_EXEC_ERROR,
1671                              strerror(ENOMEM));
1672             return;
1673         }
1674         tp->kind = mode;
1675         tp->target = target;
1676         tp->target_value = crm_element_value_copy(level, XML_ATTR_STONITH_TARGET_VALUE);
1677         tp->target_pattern = crm_element_value_copy(level, XML_ATTR_STONITH_TARGET_PATTERN);
1678         tp->target_attribute = crm_element_value_copy(level, XML_ATTR_STONITH_TARGET_ATTRIBUTE);
1679 
1680         g_hash_table_replace(topology, tp->target, tp);
1681         crm_trace("Added %s (%d) to the topology (%d active entries)",
1682                   target, (int) mode, g_hash_table_size(topology));
1683     } else {
1684         free(target);
1685     }
1686 
1687     if (tp->levels[id] != NULL) {
1688         crm_info("Adding to the existing %s[%d] topology entry",
1689                  tp->target, id);
1690     }
1691 
1692     devices = parse_device_list(crm_element_value(level, XML_ATTR_STONITH_DEVICES));
1693     for (dIter = devices; dIter; dIter = dIter->next) {
1694         const char *device = dIter->value;
1695 
1696         crm_trace("Adding device '%s' for %s[%d]", device, tp->target, id);
1697         tp->levels[id] = g_list_append(tp->levels[id], strdup(device));
1698     }
1699     stonith_key_value_freeall(devices, 1, 1);
1700 
1701     {
1702         int nlevels = count_active_levels(tp);
1703 
1704         crm_info("Target %s has %d active fencing level%s",
1705                  tp->target, nlevels, pcmk__plural_s(nlevels));
1706     }
1707 
1708     pcmk__set_result(result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
1709 }
1710 
1711 /*!
1712  * \internal
1713  * \brief Unregister a fencing topology level for a target
1714  *
1715  * Given an XML request specifying the target name and level index (or 0 for all
1716  * levels), this will remove any corresponding entry for the target from the
1717  * global topology table.
1718  *
1719  * \param[in]  msg     XML request for STONITH level registration
1720  * \param[out] desc    If not NULL, set to string representation "TARGET[LEVEL]"
1721  * \param[out] result  Where to set result of unregistration
1722  */
1723 void
1724 fenced_unregister_level(xmlNode *msg, char **desc,
     /* [previous][next][first][last][top][bottom][index][help] */
1725                         pcmk__action_result_t *result)
1726 {
1727     int id = -1;
1728     stonith_topology_t *tp;
1729     char *target;
1730     xmlNode *level = NULL;
1731 
1732     CRM_CHECK(result != NULL, return);
1733 
1734     if (msg == NULL) {
1735         fenced_set_protocol_error(result);
1736         return;
1737     }
1738 
1739     // Unlike additions, removal requests should always have one level tag
1740     level = get_xpath_object("//" XML_TAG_FENCING_LEVEL, msg, LOG_WARNING);
1741     if (level == NULL) {
1742         fenced_set_protocol_error(result);
1743         return;
1744     }
1745 
1746     target = stonith_level_key(level, fenced_target_by_unknown);
1747     crm_element_value_int(level, XML_ATTR_STONITH_INDEX, &id);
1748 
1749     // Ensure level ID is in allowed range
1750     if ((id < 0) || (id >= ST_LEVEL_MAX)) {
1751         crm_warn("Ignoring topology unregistration for %s with invalid level %d",
1752                   target, id);
1753         free(target);
1754         crm_log_xml_info(level, "Bad level");
1755         pcmk__format_result(result, CRM_EX_INVALID_PARAM, PCMK_EXEC_INVALID,
1756                             "Invalid level number '%s' for topology level '%s'",
1757                             crm_str(crm_element_value(level,
1758                                                       XML_ATTR_STONITH_INDEX)),
1759                             crm_str(ID(level)));
1760         return;
1761     }
1762 
1763     if (desc) {
1764         *desc = crm_strdup_printf("%s[%d]", target, id);
1765     }
1766 
1767     tp = g_hash_table_lookup(topology, target);
1768     if (tp == NULL) {
1769         guint nentries = g_hash_table_size(topology);
1770 
1771         crm_info("No fencing topology found for %s (%d active %s)",
1772                  target, nentries,
1773                  pcmk__plural_alt(nentries, "entry", "entries"));
1774 
1775     } else if (id == 0 && g_hash_table_remove(topology, target)) {
1776         guint nentries = g_hash_table_size(topology);
1777 
1778         crm_info("Removed all fencing topology entries related to %s "
1779                  "(%d active %s remaining)", target, nentries,
1780                  pcmk__plural_alt(nentries, "entry", "entries"));
1781 
1782     } else if (tp->levels[id] != NULL) {
1783         guint nlevels;
1784 
1785         g_list_free_full(tp->levels[id], free);
1786         tp->levels[id] = NULL;
1787 
1788         nlevels = count_active_levels(tp);
1789         crm_info("Removed level %d from fencing topology for %s "
1790                  "(%d active level%s remaining)",
1791                  id, target, nlevels, pcmk__plural_s(nlevels));
1792     }
1793 
1794     free(target);
1795     pcmk__set_result(result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
1796 }
1797 
1798 static char *
1799 list_to_string(GList *list, const char *delim, gboolean terminate_with_delim)
     /* [previous][next][first][last][top][bottom][index][help] */
1800 {
1801     int max = g_list_length(list);
1802     size_t delim_len = delim?strlen(delim):0;
1803     size_t alloc_size = 1 + (max?((max-1+(terminate_with_delim?1:0))*delim_len):0);
1804     char *rv;
1805     GList *gIter;
1806 
1807     for (gIter = list; gIter != NULL; gIter = gIter->next) {
1808         const char *value = (const char *) gIter->data;
1809 
1810         alloc_size += strlen(value);
1811     }
1812     rv = calloc(alloc_size, sizeof(char));
1813     if (rv) {
1814         char *pos = rv;
1815         const char *lead_delim = "";
1816 
1817         for (gIter = list; gIter != NULL; gIter = gIter->next) {
1818             const char *value = (const char *) gIter->data;
1819 
1820             pos = &pos[sprintf(pos, "%s%s", lead_delim, value)];
1821             lead_delim = delim;
1822         }
1823         if (max && terminate_with_delim) {
1824             sprintf(pos, "%s", delim);
1825         }
1826     }
1827     return rv;
1828 }
1829 
1830 /*!
1831  * \internal
1832  * \brief Execute a fence agent action directly (and asynchronously)
1833  *
1834  * Handle a STONITH_OP_EXEC API message by scheduling a requested agent action
1835  * directly on a specified device. Only list, monitor, and status actions are
1836  * expected to use this call, though it should work with any agent command.
1837  *
1838  * \param[in]  msg     Request XML specifying action
1839  * \param[out] result  Where to store result of action
1840  *
1841  * \note If the action is monitor, the device must be registered via the API
1842  *       (CIB registration is not sufficient), because monitor should not be
1843  *       possible unless the device is "started" (API registered).
1844  */
1845 static void
1846 execute_agent_action(xmlNode *msg, pcmk__action_result_t *result)
     /* [previous][next][first][last][top][bottom][index][help] */
1847 {
1848     xmlNode *dev = get_xpath_object("//" F_STONITH_DEVICE, msg, LOG_ERR);
1849     xmlNode *op = get_xpath_object("//@" F_STONITH_ACTION, msg, LOG_ERR);
1850     const char *id = crm_element_value(dev, F_STONITH_DEVICE);
1851     const char *action = crm_element_value(op, F_STONITH_ACTION);
1852     async_command_t *cmd = NULL;
1853     stonith_device_t *device = NULL;
1854 
1855     if ((id == NULL) || (action == NULL)) {
1856         crm_info("Malformed API action request: device %s, action %s",
1857                  (id? id : "not specified"),
1858                  (action? action : "not specified"));
1859         fenced_set_protocol_error(result);
1860         return;
1861     }
1862 
1863     if (pcmk__str_eq(id, STONITH_WATCHDOG_ID, pcmk__str_none)) {
1864         // Watchdog agent actions are implemented internally
1865         if (stonith_watchdog_timeout_ms <= 0) {
1866             pcmk__set_result(result, CRM_EX_ERROR, PCMK_EXEC_NO_FENCE_DEVICE,
1867                              "Watchdog fence device not configured");
1868             return;
1869 
1870         } else if (pcmk__str_eq(action, "list", pcmk__str_casei)) {
1871             pcmk__set_result(result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
1872             pcmk__set_result_output(result,
1873                                     list_to_string(stonith_watchdog_targets,
1874                                                    "\n", TRUE),
1875                                     NULL);
1876             return;
1877 
1878         } else if (pcmk__str_eq(action, "monitor", pcmk__str_casei)) {
1879             pcmk__set_result(result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
1880             return;
1881         }
1882     }
1883 
1884     device = g_hash_table_lookup(device_list, id);
1885     if (device == NULL) {
1886         crm_info("Ignoring API '%s' action request because device %s not found",
1887                  action, id);
1888         pcmk__format_result(result, CRM_EX_ERROR, PCMK_EXEC_NO_FENCE_DEVICE,
1889                             "'%s' not found", id);
1890         return;
1891 
1892     } else if (!device->api_registered && !strcmp(action, "monitor")) {
1893         // Monitors may run only on "started" (API-registered) devices
1894         crm_info("Ignoring API '%s' action request because device %s not active",
1895                  action, id);
1896         pcmk__format_result(result, CRM_EX_ERROR, PCMK_EXEC_NO_FENCE_DEVICE,
1897                             "'%s' not active", id);
1898         return;
1899     }
1900 
1901     cmd = create_async_command(msg);
1902     if (cmd == NULL) {
1903         fenced_set_protocol_error(result);
1904         return;
1905     }
1906 
1907     schedule_stonith_command(cmd, device);
1908     pcmk__set_result(result, CRM_EX_OK, PCMK_EXEC_PENDING, NULL);
1909 }
1910 
1911 static void
1912 search_devices_record_result(struct device_search_s *search, const char *device, gboolean can_fence)
     /* [previous][next][first][last][top][bottom][index][help] */
1913 {
1914     search->replies_received++;
1915 
1916     if (can_fence && device) {
1917         search->capable = g_list_append(search->capable, strdup(device));
1918     }
1919 
1920     if (search->replies_needed == search->replies_received) {
1921 
1922         guint ndevices = g_list_length(search->capable);
1923 
1924         crm_debug("Search found %d device%s that can perform '%s' targeting %s",
1925                   ndevices, pcmk__plural_s(ndevices),
1926                   (search->action? search->action : "unknown action"),
1927                   (search->host? search->host : "any node"));
1928 
1929         search->callback(search->capable, search->user_data);
1930         free(search->host);
1931         free(search->action);
1932         free(search);
1933     }
1934 }
1935 
1936 /*!
1937  * \internal
1938  * \brief Check whether the local host is allowed to execute a fencing action
1939  *
1940  * \param[in] device         Fence device to check
1941  * \param[in] action         Fence action to check
1942  * \param[in] target         Hostname of fence target
1943  * \param[in] allow_suicide  Whether self-fencing is allowed for this operation
1944  *
1945  * \return TRUE if local host is allowed to execute action, FALSE otherwise
1946  */
1947 static gboolean
1948 localhost_is_eligible(const stonith_device_t *device, const char *action,
     /* [previous][next][first][last][top][bottom][index][help] */
1949                       const char *target, gboolean allow_suicide)
1950 {
1951     gboolean localhost_is_target = pcmk__str_eq(target, stonith_our_uname,
1952                                                 pcmk__str_casei);
1953 
1954     if (device && action && device->on_target_actions
1955         && strstr(device->on_target_actions, action)) {
1956         if (!localhost_is_target) {
1957             crm_trace("Operation '%s' using %s can only be executed for "
1958                       "local host, not %s", action, device->id, target);
1959             return FALSE;
1960         }
1961 
1962     } else if (localhost_is_target && !allow_suicide) {
1963         crm_trace("'%s' operation does not support self-fencing", action);
1964         return FALSE;
1965     }
1966     return TRUE;
1967 }
1968 
1969 static void
1970 can_fence_host_with_device(stonith_device_t * dev, struct device_search_s *search)
     /* [previous][next][first][last][top][bottom][index][help] */
1971 {
1972     gboolean can = FALSE;
1973     const char *check_type = NULL;
1974     const char *host = search->host;
1975     const char *alias = NULL;
1976 
1977     CRM_LOG_ASSERT(dev != NULL);
1978 
1979     if (dev == NULL) {
1980         goto search_report_results;
1981     } else if (host == NULL) {
1982         can = TRUE;
1983         goto search_report_results;
1984     }
1985 
1986     /* Short-circuit query if this host is not allowed to perform the action */
1987     if (pcmk__str_eq(search->action, "reboot", pcmk__str_casei)) {
1988         /* A "reboot" *might* get remapped to "off" then "on", so short-circuit
1989          * only if all three are disallowed. If only one or two are disallowed,
1990          * we'll report that with the results. We never allow suicide for
1991          * remapped "on" operations because the host is off at that point.
1992          */
1993         if (!localhost_is_eligible(dev, "reboot", host, search->allow_suicide)
1994             && !localhost_is_eligible(dev, "off", host, search->allow_suicide)
1995             && !localhost_is_eligible(dev, "on", host, FALSE)) {
1996             goto search_report_results;
1997         }
1998     } else if (!localhost_is_eligible(dev, search->action, host,
1999                                       search->allow_suicide)) {
2000         goto search_report_results;
2001     }
2002 
2003     alias = g_hash_table_lookup(dev->aliases, host);
2004     if (alias == NULL) {
2005         alias = host;
2006     }
2007 
2008     check_type = target_list_type(dev);
2009 
2010     if (pcmk__str_eq(check_type, PCMK__VALUE_NONE, pcmk__str_casei)) {
2011         can = TRUE;
2012 
2013     } else if (pcmk__str_eq(check_type, "static-list", pcmk__str_casei)) {
2014 
2015         /* Presence in the hostmap is sufficient
2016          * Only use if all hosts on which the device can be active can always fence all listed hosts
2017          */
2018 
2019         if (pcmk__str_in_list(host, dev->targets, pcmk__str_casei)) {
2020             can = TRUE;
2021         } else if (g_hash_table_lookup(dev->params, PCMK_STONITH_HOST_MAP)
2022                    && g_hash_table_lookup(dev->aliases, host)) {
2023             can = TRUE;
2024         }
2025 
2026     } else if (pcmk__str_eq(check_type, "dynamic-list", pcmk__str_casei)) {
2027         time_t now = time(NULL);
2028 
2029         if (dev->targets == NULL || dev->targets_age + 60 < now) {
2030             int device_timeout = get_action_timeout(dev, "list", search->per_device_timeout);
2031 
2032             if (device_timeout > search->per_device_timeout) {
2033                 crm_notice("Since the pcmk_list_timeout(%ds) parameter of %s is larger than stonith-timeout(%ds), timeout may occur",
2034                     device_timeout, dev->id, search->per_device_timeout);
2035             }
2036 
2037             crm_trace("Running '%s' to check whether %s is eligible to fence %s (%s)",
2038                       check_type, dev->id, search->host, search->action);
2039 
2040             schedule_internal_command(__func__, dev, "list", NULL,
2041                                       search->per_device_timeout, search, dynamic_list_search_cb);
2042 
2043             /* we'll respond to this search request async in the cb */
2044             return;
2045         }
2046 
2047         if (pcmk__str_in_list(alias, dev->targets, pcmk__str_casei)) {
2048             can = TRUE;
2049         }
2050 
2051     } else if (pcmk__str_eq(check_type, "status", pcmk__str_casei)) {
2052         int device_timeout = get_action_timeout(dev, check_type, search->per_device_timeout);
2053 
2054         if (device_timeout > search->per_device_timeout) {
2055             crm_notice("Since the pcmk_status_timeout(%ds) parameter of %s is larger than stonith-timeout(%ds), timeout may occur",
2056                 device_timeout, dev->id, search->per_device_timeout);
2057         }
2058 
2059         crm_trace("Running '%s' to check whether %s is eligible to fence %s (%s)",
2060                   check_type, dev->id, search->host, search->action);
2061         schedule_internal_command(__func__, dev, "status", search->host,
2062                                   search->per_device_timeout, search, status_search_cb);
2063         /* we'll respond to this search request async in the cb */
2064         return;
2065     } else {
2066         crm_err("Invalid value for " PCMK_STONITH_HOST_CHECK ": %s", check_type);
2067         check_type = "Invalid " PCMK_STONITH_HOST_CHECK;
2068     }
2069 
2070     if (pcmk__str_eq(host, alias, pcmk__str_casei)) {
2071         crm_notice("%s is%s eligible to fence (%s) %s: %s",
2072                    dev->id, (can? "" : " not"), search->action, host,
2073                    check_type);
2074     } else {
2075         crm_notice("%s is%s eligible to fence (%s) %s (aka. '%s'): %s",
2076                    dev->id, (can? "" : " not"), search->action, host, alias,
2077                    check_type);
2078     }
2079 
2080   search_report_results:
2081     search_devices_record_result(search, dev ? dev->id : NULL, can);
2082 }
2083 
2084 static void
2085 search_devices(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2086 {
2087     stonith_device_t *dev = value;
2088     struct device_search_s *search = user_data;
2089 
2090     can_fence_host_with_device(dev, search);
2091 }
2092 
2093 #define DEFAULT_QUERY_TIMEOUT 20
2094 static void
2095 get_capable_devices(const char *host, const char *action, int timeout, bool suicide, void *user_data,
     /* [previous][next][first][last][top][bottom][index][help] */
2096                     void (*callback) (GList * devices, void *user_data))
2097 {
2098     struct device_search_s *search;
2099     guint ndevices = g_hash_table_size(device_list);
2100 
2101     if (ndevices == 0) {
2102         callback(NULL, user_data);
2103         return;
2104     }
2105 
2106     search = calloc(1, sizeof(struct device_search_s));
2107     if (!search) {
2108         crm_crit("Cannot search for capable fence devices: %s",
2109                  strerror(ENOMEM));
2110         callback(NULL, user_data);
2111         return;
2112     }
2113 
2114     pcmk__str_update(&search->host, host);
2115     pcmk__str_update(&search->action, action);
2116     search->per_device_timeout = timeout;
2117     search->allow_suicide = suicide;
2118     search->callback = callback;
2119     search->user_data = user_data;
2120 
2121     /* We are guaranteed this many replies, even if a device is
2122      * unregistered while the search is in progress.
2123      */
2124     search->replies_needed = ndevices;
2125 
2126     crm_debug("Searching %d device%s to see which can execute '%s' targeting %s",
2127               ndevices, pcmk__plural_s(ndevices),
2128               (search->action? search->action : "unknown action"),
2129               (search->host? search->host : "any node"));
2130     g_hash_table_foreach(device_list, search_devices, search);
2131 }
2132 
2133 struct st_query_data {
2134     xmlNode *reply;
2135     char *remote_peer;
2136     char *client_id;
2137     char *target;
2138     char *action;
2139     int call_options;
2140 };
2141 
2142 /*!
2143  * \internal
2144  * \brief Add action-specific attributes to query reply XML
2145  *
2146  * \param[in,out] xml     XML to add attributes to
2147  * \param[in]     action  Fence action
2148  * \param[in]     device  Fence device
2149  * \param[in]     target  Fence target
2150  */
2151 static void
2152 add_action_specific_attributes(xmlNode *xml, const char *action,
     /* [previous][next][first][last][top][bottom][index][help] */
2153                                stonith_device_t *device, const char *target)
2154 {
2155     int action_specific_timeout;
2156     int delay_max;
2157     int delay_base;
2158 
2159     CRM_CHECK(xml && action && device, return);
2160 
2161     if (is_action_required(action, device)) {
2162         crm_trace("Action '%s' is required using %s", action, device->id);
2163         crm_xml_add_int(xml, F_STONITH_DEVICE_REQUIRED, 1);
2164     }
2165 
2166     action_specific_timeout = get_action_timeout(device, action, 0);
2167     if (action_specific_timeout) {
2168         crm_trace("Action '%s' has timeout %dms using %s",
2169                   action, action_specific_timeout, device->id);
2170         crm_xml_add_int(xml, F_STONITH_ACTION_TIMEOUT, action_specific_timeout);
2171     }
2172 
2173     delay_max = get_action_delay_max(device, action);
2174     if (delay_max > 0) {
2175         crm_trace("Action '%s' has maximum random delay %dms using %s",
2176                   action, delay_max, device->id);
2177         crm_xml_add_int(xml, F_STONITH_DELAY_MAX, delay_max / 1000);
2178     }
2179 
2180     delay_base = get_action_delay_base(device, action, target);
2181     if (delay_base > 0) {
2182         crm_xml_add_int(xml, F_STONITH_DELAY_BASE, delay_base / 1000);
2183     }
2184 
2185     if ((delay_max > 0) && (delay_base == 0)) {
2186         crm_trace("Action '%s' has maximum random delay %dms using %s",
2187                   action, delay_max, device->id);
2188     } else if ((delay_max == 0) && (delay_base > 0)) {
2189         crm_trace("Action '%s' has a static delay of %dms using %s",
2190                   action, delay_base, device->id);
2191     } else if ((delay_max > 0) && (delay_base > 0)) {
2192         crm_trace("Action '%s' has a minimum delay of %dms and a randomly chosen "
2193                   "maximum delay of %dms using %s",
2194                   action, delay_base, delay_max, device->id);
2195     }
2196 }
2197 
2198 /*!
2199  * \internal
2200  * \brief Add "disallowed" attribute to query reply XML if appropriate
2201  *
2202  * \param[in,out] xml            XML to add attribute to
2203  * \param[in]     action         Fence action
2204  * \param[in]     device         Fence device
2205  * \param[in]     target         Fence target
2206  * \param[in]     allow_suicide  Whether self-fencing is allowed
2207  */
2208 static void
2209 add_disallowed(xmlNode *xml, const char *action, stonith_device_t *device,
     /* [previous][next][first][last][top][bottom][index][help] */
2210                const char *target, gboolean allow_suicide)
2211 {
2212     if (!localhost_is_eligible(device, action, target, allow_suicide)) {
2213         crm_trace("Action '%s' using %s is disallowed for local host",
2214                   action, device->id);
2215         pcmk__xe_set_bool_attr(xml, F_STONITH_ACTION_DISALLOWED, true);
2216     }
2217 }
2218 
2219 /*!
2220  * \internal
2221  * \brief Add child element with action-specific values to query reply XML
2222  *
2223  * \param[in,out] xml            XML to add attribute to
2224  * \param[in]     action         Fence action
2225  * \param[in]     device         Fence device
2226  * \param[in]     target         Fence target
2227  * \param[in]     allow_suicide  Whether self-fencing is allowed
2228  */
2229 static void
2230 add_action_reply(xmlNode *xml, const char *action, stonith_device_t *device,
     /* [previous][next][first][last][top][bottom][index][help] */
2231                const char *target, gboolean allow_suicide)
2232 {
2233     xmlNode *child = create_xml_node(xml, F_STONITH_ACTION);
2234 
2235     crm_xml_add(child, XML_ATTR_ID, action);
2236     add_action_specific_attributes(child, action, device, target);
2237     add_disallowed(child, action, device, target, allow_suicide);
2238 }
2239 
2240 static void
2241 stonith_query_capable_device_cb(GList * devices, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2242 {
2243     struct st_query_data *query = user_data;
2244     int available_devices = 0;
2245     xmlNode *dev = NULL;
2246     xmlNode *list = NULL;
2247     GList *lpc = NULL;
2248     pcmk__client_t *client = NULL;
2249 
2250     if (query->client_id != NULL) {
2251         client = pcmk__find_client_by_id(query->client_id);
2252         if ((client == NULL) && (query->remote_peer == NULL)) {
2253             crm_trace("Skipping reply to %s: no longer a client",
2254                       query->client_id);
2255             goto done;
2256         }
2257     }
2258 
2259     /* Pack the results into XML */
2260     list = create_xml_node(NULL, __func__);
2261     crm_xml_add(list, F_STONITH_TARGET, query->target);
2262     for (lpc = devices; lpc != NULL; lpc = lpc->next) {
2263         stonith_device_t *device = g_hash_table_lookup(device_list, lpc->data);
2264         const char *action = query->action;
2265 
2266         if (!device) {
2267             /* It is possible the device got unregistered while
2268              * determining who can fence the target */
2269             continue;
2270         }
2271 
2272         available_devices++;
2273 
2274         dev = create_xml_node(list, F_STONITH_DEVICE);
2275         crm_xml_add(dev, XML_ATTR_ID, device->id);
2276         crm_xml_add(dev, "namespace", device->namespace);
2277         crm_xml_add(dev, "agent", device->agent);
2278         crm_xml_add_int(dev, F_STONITH_DEVICE_VERIFIED, device->verified);
2279 
2280         /* If the originating fencer wants to reboot the node, and we have a
2281          * capable device that doesn't support "reboot", remap to "off" instead.
2282          */
2283         if (!pcmk_is_set(device->flags, st_device_supports_reboot)
2284             && pcmk__str_eq(query->action, "reboot", pcmk__str_casei)) {
2285             crm_trace("%s doesn't support reboot, using values for off instead",
2286                       device->id);
2287             action = "off";
2288         }
2289 
2290         /* Add action-specific values if available */
2291         add_action_specific_attributes(dev, action, device, query->target);
2292         if (pcmk__str_eq(query->action, "reboot", pcmk__str_casei)) {
2293             /* A "reboot" *might* get remapped to "off" then "on", so after
2294              * sending the "reboot"-specific values in the main element, we add
2295              * sub-elements for "off" and "on" values.
2296              *
2297              * We short-circuited earlier if "reboot", "off" and "on" are all
2298              * disallowed for the local host. However if only one or two are
2299              * disallowed, we send back the results and mark which ones are
2300              * disallowed. If "reboot" is disallowed, this might cause problems
2301              * with older fencer versions, which won't check for it. Older
2302              * versions will ignore "off" and "on", so they are not a problem.
2303              */
2304             add_disallowed(dev, action, device, query->target,
2305                            pcmk_is_set(query->call_options, st_opt_allow_suicide));
2306             add_action_reply(dev, "off", device, query->target,
2307                              pcmk_is_set(query->call_options, st_opt_allow_suicide));
2308             add_action_reply(dev, "on", device, query->target, FALSE);
2309         }
2310 
2311         /* A query without a target wants device parameters */
2312         if (query->target == NULL) {
2313             xmlNode *attrs = create_xml_node(dev, XML_TAG_ATTRS);
2314 
2315             g_hash_table_foreach(device->params, hash2field, attrs);
2316         }
2317     }
2318 
2319     crm_xml_add_int(list, F_STONITH_AVAILABLE_DEVICES, available_devices);
2320     if (query->target) {
2321         crm_debug("Found %d matching device%s for target '%s'",
2322                   available_devices, pcmk__plural_s(available_devices),
2323                   query->target);
2324     } else {
2325         crm_debug("%d device%s installed",
2326                   available_devices, pcmk__plural_s(available_devices));
2327     }
2328 
2329     if (list != NULL) {
2330         crm_log_xml_trace(list, "Add query results");
2331         add_message_xml(query->reply, F_STONITH_CALLDATA, list);
2332     }
2333 
2334     stonith_send_reply(query->reply, query->call_options, query->remote_peer,
2335                        client);
2336 
2337 done:
2338     free_xml(query->reply);
2339     free(query->remote_peer);
2340     free(query->client_id);
2341     free(query->target);
2342     free(query->action);
2343     free(query);
2344     free_xml(list);
2345     g_list_free_full(devices, free);
2346 }
2347 
2348 /*!
2349  * \internal
2350  * \brief Log the result of an asynchronous command
2351  *
2352  * \param[in] cmd        Command the result is for
2353  * \param[in] result     Result of command
2354  * \param[in] pid        Process ID of command, if available
2355  * \param[in] next       Alternate device that will be tried if command failed
2356  * \param[in] op_merged  Whether this command was merged with an earlier one
2357  */
2358 static void
2359 log_async_result(async_command_t *cmd, const pcmk__action_result_t *result,
     /* [previous][next][first][last][top][bottom][index][help] */
2360                  int pid, const char *next, bool op_merged)
2361 {
2362     int log_level = LOG_ERR;
2363     int output_log_level = LOG_NEVER;
2364     guint devices_remaining = g_list_length(cmd->device_next);
2365 
2366     GString *msg = g_string_sized_new(80); // Reasonable starting size
2367 
2368     // Choose log levels appropriately if we have a result
2369     if (pcmk__result_ok(result)) {
2370         log_level = (cmd->victim == NULL)? LOG_DEBUG : LOG_NOTICE;
2371         if ((result->action_stdout != NULL)
2372             && !pcmk__str_eq(cmd->action, "metadata", pcmk__str_casei)) {
2373             output_log_level = LOG_DEBUG;
2374         }
2375         next = NULL;
2376     } else {
2377         log_level = (cmd->victim == NULL)? LOG_NOTICE : LOG_ERR;
2378         if ((result->action_stdout != NULL)
2379             && !pcmk__str_eq(cmd->action, "metadata", pcmk__str_casei)) {
2380             output_log_level = LOG_WARNING;
2381         }
2382     }
2383 
2384     // Build the log message piece by piece
2385     g_string_printf(msg, "Operation '%s' ", cmd->action);
2386     if (pid != 0) {
2387         g_string_append_printf(msg, "[%d] ", pid);
2388     }
2389     if (cmd->victim != NULL) {
2390         g_string_append_printf(msg, "targeting %s ", cmd->victim);
2391     }
2392     g_string_append_printf(msg, "using %s ", cmd->device);
2393 
2394     // Add exit status or execution status as appropriate
2395     if (result->execution_status == PCMK_EXEC_DONE) {
2396         g_string_append_printf(msg, "returned %d", result->exit_status);
2397     } else {
2398         g_string_append_printf(msg, "could not be executed: %s",
2399                                pcmk_exec_status_str(result->execution_status));
2400     }
2401 
2402     // Add exit reason and next device if appropriate
2403     if (result->exit_reason != NULL) {
2404         g_string_append_printf(msg, " (%s)", result->exit_reason);
2405     }
2406     if (next != NULL) {
2407         g_string_append_printf(msg, ", retrying with %s", next);
2408     }
2409     if (devices_remaining > 0) {
2410         g_string_append_printf(msg, " (%u device%s remaining)",
2411                                (unsigned int) devices_remaining,
2412                                pcmk__plural_s(devices_remaining));
2413     }
2414     g_string_append_printf(msg, " " CRM_XS " %scall %d from %s",
2415                            (op_merged? "merged " : ""), cmd->id,
2416                            cmd->client_name);
2417 
2418     // Log the result
2419     do_crm_log(log_level, "%s", msg->str);
2420     g_string_free(msg, TRUE);
2421 
2422     // Log the output (which may have multiple lines), if appropriate
2423     if (output_log_level != LOG_NEVER) {
2424         char *prefix = crm_strdup_printf("%s[%d]", cmd->device, pid);
2425 
2426         crm_log_output(output_log_level, prefix, result->action_stdout);
2427         free(prefix);
2428     }
2429 }
2430 
2431 /*!
2432  * \internal
2433  * \brief Reply to requester after asynchronous command completion
2434  *
2435  * \param[in] cmd      Command that completed
2436  * \param[in] result   Result of command
2437  * \param[in] pid      Process ID of command, if available
2438  * \param[in] merged   If true, command was merged with another, not executed
2439  */
2440 static void
2441 send_async_reply(async_command_t *cmd, const pcmk__action_result_t *result,
     /* [previous][next][first][last][top][bottom][index][help] */
2442                  int pid, bool merged)
2443 {
2444     xmlNode *reply = NULL;
2445     pcmk__client_t *client = NULL;
2446 
2447     CRM_CHECK((cmd != NULL) && (result != NULL), return);
2448 
2449     log_async_result(cmd, result, pid, NULL, merged);
2450 
2451     if (cmd->client != NULL) {
2452         client = pcmk__find_client_by_id(cmd->client);
2453         if ((client == NULL) && (cmd->origin == NULL)) {
2454             crm_trace("Skipping reply to %s: no longer a client", cmd->client);
2455             return;
2456         }
2457     }
2458 
2459     reply = construct_async_reply(cmd, result);
2460     if (merged) {
2461         pcmk__xe_set_bool_attr(reply, F_STONITH_MERGED, true);
2462     }
2463 
2464     if (!stand_alone && pcmk__is_fencing_action(cmd->action)
2465         && pcmk__str_eq(cmd->origin, cmd->victim, pcmk__str_casei)) {
2466         /* The target was also the originator, so broadcast the result on its
2467          * behalf (since it will be unable to).
2468          */
2469         crm_trace("Broadcast '%s' result for %s (target was also originator)",
2470                   cmd->action, cmd->victim);
2471         crm_xml_add(reply, F_SUBTYPE, "broadcast");
2472         crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY);
2473         send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE);
2474     } else {
2475         // Reply only to the originator
2476         stonith_send_reply(reply, cmd->options, cmd->origin, client);
2477     }
2478 
2479     crm_log_xml_trace(reply, "Reply");
2480     free_xml(reply);
2481 
2482     if (stand_alone) {
2483         /* Do notification with a clean data object */
2484         xmlNode *notify_data = create_xml_node(NULL, T_STONITH_NOTIFY_FENCE);
2485 
2486         stonith__xe_set_result(notify_data, result);
2487         crm_xml_add(notify_data, F_STONITH_TARGET, cmd->victim);
2488         crm_xml_add(notify_data, F_STONITH_OPERATION, cmd->op);
2489         crm_xml_add(notify_data, F_STONITH_DELEGATE, "localhost");
2490         crm_xml_add(notify_data, F_STONITH_DEVICE, cmd->device);
2491         crm_xml_add(notify_data, F_STONITH_REMOTE_OP_ID, cmd->remote_op_id);
2492         crm_xml_add(notify_data, F_STONITH_ORIGIN, cmd->client);
2493 
2494         fenced_send_notification(T_STONITH_NOTIFY_FENCE, result, notify_data);
2495         fenced_send_notification(T_STONITH_NOTIFY_HISTORY, NULL, NULL);
2496     }
2497 }
2498 
2499 static void
2500 cancel_stonith_command(async_command_t * cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2501 {
2502     stonith_device_t *device;
2503 
2504     CRM_CHECK(cmd != NULL, return);
2505 
2506     if (!cmd->device) {
2507         return;
2508     }
2509 
2510     device = g_hash_table_lookup(device_list, cmd->device);
2511 
2512     if (device) {
2513         crm_trace("Cancel scheduled '%s' action using %s",
2514                   cmd->action, device->id);
2515         device->pending_ops = g_list_remove(device->pending_ops, cmd);
2516     }
2517 }
2518 
2519 static void
2520 st_child_done(int pid, const pcmk__action_result_t *result, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2521 {
2522     stonith_device_t *device = NULL;
2523     stonith_device_t *next_device = NULL;
2524     async_command_t *cmd = user_data;
2525 
2526     GList *gIter = NULL;
2527     GList *gIterNext = NULL;
2528 
2529     CRM_CHECK(cmd != NULL, return);
2530 
2531     cmd->active_on = NULL;
2532 
2533     /* The device is ready to do something else now */
2534     device = g_hash_table_lookup(device_list, cmd->device);
2535     if (device) {
2536         if (!device->verified && pcmk__result_ok(result) &&
2537             (pcmk__strcase_any_of(cmd->action, "list", "monitor", "status", NULL))) {
2538 
2539             device->verified = TRUE;
2540         }
2541 
2542         mainloop_set_trigger(device->work);
2543     }
2544 
2545     if (pcmk__result_ok(result)) {
2546         GList *iter;
2547         /* see if there are any required devices left to execute for this op */
2548         for (iter = cmd->device_next; iter != NULL; iter = iter->next) {
2549             next_device = g_hash_table_lookup(device_list, iter->data);
2550 
2551             if (next_device != NULL && is_action_required(cmd->action, next_device)) {
2552                 cmd->device_next = iter->next;
2553                 break;
2554             }
2555             next_device = NULL;
2556         }
2557 
2558     } else if ((cmd->device_next != NULL)
2559                && !is_action_required(cmd->action, device)) {
2560         /* if this device didn't work out, see if there are any others we can try.
2561          * if the failed device was 'required', we can't pick another device. */
2562         next_device = g_hash_table_lookup(device_list, cmd->device_next->data);
2563         cmd->device_next = cmd->device_next->next;
2564     }
2565 
2566     /* this operation requires more fencing, hooray! */
2567     if (next_device) {
2568         log_async_result(cmd, result, pid, next_device->id, false);
2569         schedule_stonith_command(cmd, next_device);
2570         /* Prevent cmd from being freed */
2571         cmd = NULL;
2572         goto done;
2573     }
2574 
2575     send_async_reply(cmd, result, pid, false);
2576 
2577     if (!pcmk__result_ok(result)) {
2578         goto done;
2579     }
2580 
2581     /* Check to see if any operations are scheduled to do the exact
2582      * same thing that just completed.  If so, rather than
2583      * performing the same fencing operation twice, return the result
2584      * of this operation for all pending commands it matches. */
2585     for (gIter = cmd_list; gIter != NULL; gIter = gIterNext) {
2586         async_command_t *cmd_other = gIter->data;
2587 
2588         gIterNext = gIter->next;
2589 
2590         if (cmd == cmd_other) {
2591             continue;
2592         }
2593 
2594         /* A pending scheduled command matches the command that just finished if.
2595          * 1. The client connections are different.
2596          * 2. The node victim is the same.
2597          * 3. The fencing action is the same.
2598          * 4. The device scheduled to execute the action is the same.
2599          */
2600         if (pcmk__str_eq(cmd->client, cmd_other->client, pcmk__str_casei) ||
2601             !pcmk__str_eq(cmd->victim, cmd_other->victim, pcmk__str_casei) ||
2602             !pcmk__str_eq(cmd->action, cmd_other->action, pcmk__str_casei) ||
2603             !pcmk__str_eq(cmd->device, cmd_other->device, pcmk__str_casei)) {
2604 
2605             continue;
2606         }
2607 
2608         /* Duplicate merging will do the right thing for either type of remapped
2609          * reboot. If the executing fencer remapped an unsupported reboot to
2610          * off, then cmd->action will be reboot and will be merged with any
2611          * other reboot requests. If the originating fencer remapped a
2612          * topology reboot to off then on, we will get here once with
2613          * cmd->action "off" and once with "on", and they will be merged
2614          * separately with similar requests.
2615          */
2616         crm_notice("Merging fencing action '%s' targeting %s originating from "
2617                    "client %s with identical fencing request from client %s",
2618                    cmd_other->action, cmd_other->victim, cmd_other->client_name,
2619                    cmd->client_name);
2620 
2621         cmd_list = g_list_remove_link(cmd_list, gIter);
2622 
2623         send_async_reply(cmd_other, result, pid, true);
2624         cancel_stonith_command(cmd_other);
2625 
2626         free_async_command(cmd_other);
2627         g_list_free_1(gIter);
2628     }
2629 
2630   done:
2631     free_async_command(cmd);
2632 }
2633 
2634 static gint
2635 sort_device_priority(gconstpointer a, gconstpointer b)
     /* [previous][next][first][last][top][bottom][index][help] */
2636 {
2637     const stonith_device_t *dev_a = a;
2638     const stonith_device_t *dev_b = b;
2639 
2640     if (dev_a->priority > dev_b->priority) {
2641         return -1;
2642     } else if (dev_a->priority < dev_b->priority) {
2643         return 1;
2644     }
2645     return 0;
2646 }
2647 
2648 static void
2649 stonith_fence_get_devices_cb(GList * devices, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2650 {
2651     async_command_t *cmd = user_data;
2652     stonith_device_t *device = NULL;
2653     guint ndevices = g_list_length(devices);
2654 
2655     crm_info("Found %d matching device%s for target '%s'",
2656              ndevices, pcmk__plural_s(ndevices), cmd->victim);
2657 
2658     if (devices != NULL) {
2659         /* Order based on priority */
2660         devices = g_list_sort(devices, sort_device_priority);
2661         device = g_hash_table_lookup(device_list, devices->data);
2662     }
2663 
2664     if (device == NULL) { // No device found
2665         pcmk__action_result_t result = PCMK__UNKNOWN_RESULT;
2666 
2667         pcmk__format_result(&result, CRM_EX_ERROR, PCMK_EXEC_NO_FENCE_DEVICE,
2668                             "No device configured for target '%s'",
2669                             cmd->victim);
2670         send_async_reply(cmd, &result, 0, false);
2671         pcmk__reset_result(&result);
2672         free_async_command(cmd);
2673         g_list_free_full(devices, free);
2674 
2675     } else { // Device found, schedule it for fencing
2676         cmd->device_list = devices;
2677         cmd->device_next = devices->next;
2678         schedule_stonith_command(cmd, device);
2679     }
2680 }
2681 
2682 /*!
2683  * \internal
2684  * \brief Execute a fence action via the local node
2685  *
2686  * \param[in]  msg     Fencing request
2687  * \param[out] result  Where to store result of fence action
2688  */
2689 static void
2690 fence_locally(xmlNode *msg, pcmk__action_result_t *result)
     /* [previous][next][first][last][top][bottom][index][help] */
2691 {
2692     const char *device_id = NULL;
2693     stonith_device_t *device = NULL;
2694     async_command_t *cmd = create_async_command(msg);
2695     xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, msg, LOG_ERR);
2696 
2697     CRM_CHECK(result != NULL, return);
2698 
2699     if (cmd == NULL) {
2700         fenced_set_protocol_error(result);
2701         return;
2702     }
2703 
2704     device_id = crm_element_value(dev, F_STONITH_DEVICE);
2705     if (device_id != NULL) {
2706         device = g_hash_table_lookup(device_list, device_id);
2707         if (device == NULL) {
2708             crm_err("Requested device '%s' is not available", device_id);
2709             pcmk__format_result(result, CRM_EX_ERROR, PCMK_EXEC_NO_FENCE_DEVICE,
2710                                 "Requested device '%s' not found", device_id);
2711             return;
2712         }
2713         schedule_stonith_command(cmd, device);
2714 
2715     } else {
2716         const char *host = crm_element_value(dev, F_STONITH_TARGET);
2717 
2718         if (pcmk_is_set(cmd->options, st_opt_cs_nodeid)) {
2719             int nodeid = 0;
2720             crm_node_t *node = NULL;
2721 
2722             pcmk__scan_min_int(host, &nodeid, 0);
2723             node = pcmk__search_known_node_cache(nodeid, NULL, CRM_GET_PEER_ANY);
2724             if (node != NULL) {
2725                 host = node->uname;
2726             }
2727         }
2728 
2729         /* If we get to here, then self-fencing is implicitly allowed */
2730         get_capable_devices(host, cmd->action, cmd->default_timeout,
2731                             TRUE, cmd, stonith_fence_get_devices_cb);
2732     }
2733 
2734     pcmk__set_result(result, CRM_EX_OK, PCMK_EXEC_PENDING, NULL);
2735 }
2736 
2737 /*!
2738  * \internal
2739  * \brief Build an XML reply for a fencing operation
2740  *
2741  * \param[in] request  Request that reply is for
2742  * \param[in] data     If not NULL, add to reply as call data
2743  * \param[in] result   Full result of fencing operation
2744  *
2745  * \return Newly created XML reply
2746  * \note The caller is responsible for freeing the result.
2747  * \note This has some overlap with construct_async_reply(), but that copies
2748  *       values from an async_command_t, whereas this one copies them from the
2749  *       request.
2750  */
2751 xmlNode *
2752 fenced_construct_reply(xmlNode *request, xmlNode *data,
     /* [previous][next][first][last][top][bottom][index][help] */
2753                        pcmk__action_result_t *result)
2754 {
2755     xmlNode *reply = NULL;
2756 
2757     reply = create_xml_node(NULL, T_STONITH_REPLY);
2758 
2759     crm_xml_add(reply, "st_origin", __func__);
2760     crm_xml_add(reply, F_TYPE, T_STONITH_NG);
2761     stonith__xe_set_result(reply, result);
2762 
2763     if (request == NULL) {
2764         /* Most likely, this is the result of a stonith operation that was
2765          * initiated before we came up. Unfortunately that means we lack enough
2766          * information to provide clients with a full result.
2767          *
2768          * @TODO Maybe synchronize this information at start-up?
2769          */
2770         crm_warn("Missing request information for client notifications for "
2771                  "operation with result '%s' (initiated before we came up?)",
2772                  pcmk_exec_status_str(result->execution_status));
2773 
2774     } else {
2775         const char *name = NULL;
2776         const char *value = NULL;
2777 
2778         // Attributes to copy from request to reply
2779         const char *names[] = {
2780             F_STONITH_OPERATION,
2781             F_STONITH_CALLID,
2782             F_STONITH_CLIENTID,
2783             F_STONITH_CLIENTNAME,
2784             F_STONITH_REMOTE_OP_ID,
2785             F_STONITH_CALLOPTS
2786         };
2787 
2788         for (int lpc = 0; lpc < PCMK__NELEM(names); lpc++) {
2789             name = names[lpc];
2790             value = crm_element_value(request, name);
2791             crm_xml_add(reply, name, value);
2792         }
2793         if (data != NULL) {
2794             add_message_xml(reply, F_STONITH_CALLDATA, data);
2795         }
2796     }
2797     return reply;
2798 }
2799 
2800 /*!
2801  * \internal
2802  * \brief Build an XML reply to an asynchronous fencing command
2803  *
2804  * \param[in] cmd     Fencing command that reply is for
2805  * \param[in] result  Command result
2806  */
2807 static xmlNode *
2808 construct_async_reply(async_command_t *cmd, const pcmk__action_result_t *result)
     /* [previous][next][first][last][top][bottom][index][help] */
2809 {
2810     xmlNode *reply = create_xml_node(NULL, T_STONITH_REPLY);
2811 
2812     crm_xml_add(reply, "st_origin", __func__);
2813     crm_xml_add(reply, F_TYPE, T_STONITH_NG);
2814     crm_xml_add(reply, F_STONITH_OPERATION, cmd->op);
2815     crm_xml_add(reply, F_STONITH_DEVICE, cmd->device);
2816     crm_xml_add(reply, F_STONITH_REMOTE_OP_ID, cmd->remote_op_id);
2817     crm_xml_add(reply, F_STONITH_CLIENTID, cmd->client);
2818     crm_xml_add(reply, F_STONITH_CLIENTNAME, cmd->client_name);
2819     crm_xml_add(reply, F_STONITH_TARGET, cmd->victim);
2820     crm_xml_add(reply, F_STONITH_ACTION, cmd->op);
2821     crm_xml_add(reply, F_STONITH_ORIGIN, cmd->origin);
2822     crm_xml_add_int(reply, F_STONITH_CALLID, cmd->id);
2823     crm_xml_add_int(reply, F_STONITH_CALLOPTS, cmd->options);
2824 
2825     stonith__xe_set_result(reply, result);
2826     return reply;
2827 }
2828 
2829 bool fencing_peer_active(crm_node_t *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
2830 {
2831     if (peer == NULL) {
2832         return FALSE;
2833     } else if (peer->uname == NULL) {
2834         return FALSE;
2835     } else if (pcmk_is_set(peer->processes, crm_get_cluster_proc())) {
2836         return TRUE;
2837     }
2838     return FALSE;
2839 }
2840 
2841 void
2842 set_fencing_completed(remote_fencing_op_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
2843 {
2844     struct timespec tv;
2845 
2846     qb_util_timespec_from_epoch_get(&tv);
2847     op->completed = tv.tv_sec;
2848     op->completed_nsec = tv.tv_nsec;
2849 }
2850 
2851 /*!
2852  * \internal
2853  * \brief Look for alternate node needed if local node shouldn't fence target
2854  *
2855  * \param[in] target  Node that must be fenced
2856  *
2857  * \return Name of an alternate node that should fence \p target if any,
2858  *         or NULL otherwise
2859  */
2860 static const char *
2861 check_alternate_host(const char *target)
     /* [previous][next][first][last][top][bottom][index][help] */
2862 {
2863     const char *alternate_host = NULL;
2864 
2865     crm_trace("Checking if we (%s) can fence %s", stonith_our_uname, target);
2866     if (find_topology_for_host(target) && pcmk__str_eq(target, stonith_our_uname, pcmk__str_casei)) {
2867         GHashTableIter gIter;
2868         crm_node_t *entry = NULL;
2869 
2870         g_hash_table_iter_init(&gIter, crm_peer_cache);
2871         while (g_hash_table_iter_next(&gIter, NULL, (void **)&entry)) {
2872             crm_trace("Checking for %s.%d != %s", entry->uname, entry->id, target);
2873             if (fencing_peer_active(entry)
2874                 && !pcmk__str_eq(entry->uname, target, pcmk__str_casei)) {
2875                 alternate_host = entry->uname;
2876                 break;
2877             }
2878         }
2879         if (alternate_host == NULL) {
2880             crm_err("No alternate host available to handle request "
2881                     "for self-fencing with topology");
2882             g_hash_table_iter_init(&gIter, crm_peer_cache);
2883             while (g_hash_table_iter_next(&gIter, NULL, (void **)&entry)) {
2884                 crm_notice("Peer[%d] %s", entry->id, entry->uname);
2885             }
2886         }
2887     }
2888 
2889     return alternate_host;
2890 }
2891 
2892 /*!
2893  * \internal
2894  * \brief Send a reply to a CPG peer or IPC client
2895  *
2896  * \param[in] reply         XML reply to send
2897  * \param[in] call_options  Send synchronously if st_opt_sync_call is set here
2898  * \param[in] remote_peer   If not NULL, name of peer node to send CPG reply
2899  * \param[in] client        If not NULL, client to send IPC reply
2900  */
2901 static void
2902 stonith_send_reply(xmlNode *reply, int call_options, const char *remote_peer,
     /* [previous][next][first][last][top][bottom][index][help] */
2903                    pcmk__client_t *client)
2904 {
2905     CRM_CHECK((reply != NULL) && ((remote_peer != NULL) || (client != NULL)),
2906               return);
2907 
2908     if (remote_peer == NULL) {
2909         do_local_reply(reply, client, call_options);
2910     } else {
2911         send_cluster_message(crm_get_peer(0, remote_peer), crm_msg_stonith_ng,
2912                              reply, FALSE);
2913     }
2914 }
2915 
2916 static void 
2917 remove_relay_op(xmlNode * request)
     /* [previous][next][first][last][top][bottom][index][help] */
2918 {
2919     xmlNode *dev = get_xpath_object("//@" F_STONITH_ACTION, request, LOG_TRACE);
2920     const char *relay_op_id = NULL; 
2921     const char *op_id = NULL;
2922     const char *client_name = NULL;
2923     const char *target = NULL; 
2924     remote_fencing_op_t *relay_op = NULL; 
2925 
2926     if (dev) { 
2927         target = crm_element_value(dev, F_STONITH_TARGET); 
2928     }
2929 
2930     relay_op_id = crm_element_value(request, F_STONITH_REMOTE_OP_ID_RELAY);
2931     op_id = crm_element_value(request, F_STONITH_REMOTE_OP_ID);
2932     client_name = crm_element_value(request, F_STONITH_CLIENTNAME);
2933 
2934     /* Delete RELAY operation. */
2935     if (relay_op_id && target && pcmk__str_eq(target, stonith_our_uname, pcmk__str_casei)) {
2936         relay_op = g_hash_table_lookup(stonith_remote_op_list, relay_op_id);
2937 
2938         if (relay_op) {
2939             GHashTableIter iter;
2940             remote_fencing_op_t *list_op = NULL; 
2941             g_hash_table_iter_init(&iter, stonith_remote_op_list);
2942 
2943             /* If the operation to be deleted is registered as a duplicate, delete the registration. */
2944             while (g_hash_table_iter_next(&iter, NULL, (void **)&list_op)) {
2945                 GList *dup_iter = NULL;
2946                 if (list_op != relay_op) {
2947                     for (dup_iter = list_op->duplicates; dup_iter != NULL; dup_iter = dup_iter->next) {
2948                         remote_fencing_op_t *other = dup_iter->data;
2949                         if (other == relay_op) {
2950                             other->duplicates = g_list_remove(other->duplicates, relay_op);
2951                             break;
2952                         }
2953                     }
2954                 }
2955             }
2956             crm_debug("Deleting relay op %s ('%s' targeting %s for %s), "
2957                       "replaced by op %s ('%s' targeting %s for %s)",
2958                       relay_op->id, relay_op->action, relay_op->target,
2959                       relay_op->client_name, op_id, relay_op->action, target,
2960                       client_name);
2961 
2962             g_hash_table_remove(stonith_remote_op_list, relay_op_id);
2963         }
2964     }
2965 }
2966 
2967 /*!
2968  * \internal
2969  * \brief Check whether an API request was sent by a privileged user
2970  *
2971  * API commands related to fencing configuration may be done only by privileged
2972  * IPC users (i.e. root or hacluster), because all other users should go through
2973  * the CIB to have ACLs applied. If no client was given, this is a peer request,
2974  * which is always allowed.
2975  *
2976  * \param[in] c   IPC client that sent request (or NULL if sent by CPG peer)
2977  * \param[in] op  Requested API operation (for logging only)
2978  *
2979  * \return true if sender is peer or privileged client, otherwise false
2980  */
2981 static inline bool
2982 is_privileged(pcmk__client_t *c, const char *op)
     /* [previous][next][first][last][top][bottom][index][help] */
2983 {
2984     if ((c == NULL) || pcmk_is_set(c->flags, pcmk__client_privileged)) {
2985         return true;
2986     } else {
2987         crm_warn("Rejecting IPC request '%s' from unprivileged client %s",
2988                  crm_str(op), pcmk__client_name(c));
2989         return false;
2990     }
2991 }
2992 
2993 // CRM_OP_REGISTER
2994 static xmlNode *
2995 handle_register_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
2996 {
2997     xmlNode *reply = create_xml_node(NULL, "reply");
2998 
2999     CRM_ASSERT(request->ipc_client != NULL);
3000     crm_xml_add(reply, F_STONITH_OPERATION, CRM_OP_REGISTER);
3001     crm_xml_add(reply, F_STONITH_CLIENTID, request->ipc_client->id);
3002     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3003     pcmk__set_request_flags(request, pcmk__request_reuse_options);
3004     return reply;
3005 }
3006 
3007 // STONITH_OP_EXEC
3008 static xmlNode *
3009 handle_agent_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3010 {
3011     execute_agent_action(request->xml, &request->result);
3012     if (request->result.execution_status == PCMK_EXEC_PENDING) {
3013         return NULL;
3014     }
3015     return fenced_construct_reply(request->xml, NULL, &request->result);
3016 }
3017 
3018 // STONITH_OP_TIMEOUT_UPDATE
3019 static xmlNode *
3020 handle_update_timeout_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3021 {
3022     const char *call_id = crm_element_value(request->xml, F_STONITH_CALLID);
3023     const char *client_id = crm_element_value(request->xml, F_STONITH_CLIENTID);
3024     int op_timeout = 0;
3025 
3026     crm_element_value_int(request->xml, F_STONITH_TIMEOUT, &op_timeout);
3027     do_stonith_async_timeout_update(client_id, call_id, op_timeout);
3028     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3029     return NULL;
3030 }
3031 
3032 // STONITH_OP_QUERY
3033 static xmlNode *
3034 handle_query_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3035 {
3036     int timeout = 0;
3037     xmlNode *dev = NULL;
3038     const char *action = NULL;
3039     const char *target = NULL;
3040     const char *client_id = crm_element_value(request->xml, F_STONITH_CLIENTID);
3041     struct st_query_data *query = NULL;
3042 
3043     if (request->peer != NULL) {
3044         // Record it for the future notification
3045         create_remote_stonith_op(client_id, request->xml, TRUE);
3046     }
3047 
3048     /* Delete the DC node RELAY operation. */
3049     remove_relay_op(request->xml);
3050 
3051     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3052 
3053     dev = get_xpath_object("//@" F_STONITH_ACTION, request->xml, LOG_NEVER);
3054     if (dev != NULL) {
3055         const char *device = crm_element_value(dev, F_STONITH_DEVICE);
3056 
3057         if (pcmk__str_eq(device, "manual_ack", pcmk__str_casei)) {
3058             return NULL; // No query or reply necessary
3059         }
3060         target = crm_element_value(dev, F_STONITH_TARGET);
3061         action = crm_element_value(dev, F_STONITH_ACTION);
3062     }
3063 
3064     crm_log_xml_debug(request->xml, "Query");
3065 
3066     query = calloc(1, sizeof(struct st_query_data));
3067     CRM_ASSERT(query != NULL);
3068 
3069     query->reply = fenced_construct_reply(request->xml, NULL, &request->result);
3070     pcmk__str_update(&query->remote_peer, request->peer);
3071     pcmk__str_update(&query->client_id, client_id);
3072     pcmk__str_update(&query->target, target);
3073     pcmk__str_update(&query->action, action);
3074     query->call_options = request->call_options;
3075 
3076     crm_element_value_int(request->xml, F_STONITH_TIMEOUT, &timeout);
3077     get_capable_devices(target, action, timeout,
3078                         pcmk_is_set(query->call_options, st_opt_allow_suicide),
3079                         query, stonith_query_capable_device_cb);
3080     return NULL;
3081 }
3082 
3083 // T_STONITH_NOTIFY
3084 static xmlNode *
3085 handle_notify_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3086 {
3087     const char *flag_name = NULL;
3088 
3089     CRM_ASSERT(request->ipc_client != NULL);
3090     flag_name = crm_element_value(request->xml, F_STONITH_NOTIFY_ACTIVATE);
3091     if (flag_name != NULL) {
3092         crm_debug("Enabling %s callbacks for client %s",
3093                   flag_name, pcmk__request_origin(request));
3094         pcmk__set_client_flags(request->ipc_client, get_stonith_flag(flag_name));
3095     }
3096 
3097     flag_name = crm_element_value(request->xml, F_STONITH_NOTIFY_DEACTIVATE);
3098     if (flag_name != NULL) {
3099         crm_debug("Disabling %s callbacks for client %s",
3100                   flag_name, pcmk__request_origin(request));
3101         pcmk__clear_client_flags(request->ipc_client,
3102                                  get_stonith_flag(flag_name));
3103     }
3104 
3105     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3106     pcmk__set_request_flags(request, pcmk__request_reuse_options);
3107 
3108     return pcmk__ipc_create_ack(request->ipc_flags, "ack", CRM_EX_OK);
3109 }
3110 
3111 // STONITH_OP_RELAY
3112 static xmlNode *
3113 handle_relay_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3114 {
3115     xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, request->xml,
3116                                     LOG_TRACE);
3117 
3118     crm_notice("Received forwarded fencing request from "
3119                "%s %s to fence (%s) peer %s",
3120                pcmk__request_origin_type(request),
3121                pcmk__request_origin(request),
3122                crm_element_value(dev, F_STONITH_ACTION),
3123                crm_element_value(dev, F_STONITH_TARGET));
3124 
3125     if (initiate_remote_stonith_op(NULL, request->xml, FALSE) == NULL) {
3126         fenced_set_protocol_error(&request->result);
3127         return fenced_construct_reply(request->xml, NULL, &request->result);
3128     }
3129 
3130     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_PENDING, NULL);
3131     return NULL;
3132 }
3133 
3134 // STONITH_OP_FENCE
3135 static xmlNode *
3136 handle_fence_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3137 {
3138     if ((request->peer != NULL) || stand_alone) {
3139         fence_locally(request->xml, &request->result);
3140 
3141     } else if (pcmk_is_set(request->call_options, st_opt_manual_ack)) {
3142         switch (fenced_handle_manual_confirmation(request->ipc_client,
3143                                                   request->xml)) {
3144             case pcmk_rc_ok:
3145                 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE,
3146                                  NULL);
3147                 break;
3148             case EINPROGRESS:
3149                 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_PENDING,
3150                                  NULL);
3151                 break;
3152             default:
3153                 fenced_set_protocol_error(&request->result);
3154                 break;
3155         }
3156 
3157     } else {
3158         const char *alternate_host = NULL;
3159         xmlNode *dev = get_xpath_object("//@" F_STONITH_TARGET, request->xml,
3160                                         LOG_TRACE);
3161         const char *target = crm_element_value(dev, F_STONITH_TARGET);
3162         const char *action = crm_element_value(dev, F_STONITH_ACTION);
3163         const char *device = crm_element_value(dev, F_STONITH_DEVICE);
3164 
3165         if (request->ipc_client != NULL) {
3166             int tolerance = 0;
3167 
3168             crm_notice("Client %s wants to fence (%s) %s using %s",
3169                        pcmk__request_origin(request), action,
3170                        target, (device? device : "any device"));
3171             crm_element_value_int(dev, F_STONITH_TOLERANCE, &tolerance);
3172             if (stonith_check_fence_tolerance(tolerance, target, action)) {
3173                 pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE,
3174                                  NULL);
3175                 return fenced_construct_reply(request->xml, NULL,
3176                                               &request->result);
3177             }
3178 
3179         } else {
3180             crm_notice("Peer %s wants to fence (%s) '%s' with device '%s'",
3181                        request->peer, action, target,
3182                        (device == NULL)? "(any)" : device);
3183         }
3184 
3185         alternate_host = check_alternate_host(target);
3186 
3187         if ((alternate_host != NULL) && (request->ipc_client != NULL)) {
3188             const char *client_id = NULL;
3189             remote_fencing_op_t *op = NULL;
3190 
3191             crm_notice("Forwarding self-fencing request to peer %s "
3192                        "due to topology", alternate_host);
3193 
3194             if (request->ipc_client->id == 0) {
3195                 client_id = crm_element_value(request->xml, F_STONITH_CLIENTID);
3196             } else {
3197                 client_id = request->ipc_client->id;
3198             }
3199 
3200             /* Create a duplicate fencing operation to relay with the client ID.
3201              * When a query response is received, this operation should be
3202              * deleted to avoid keeping the duplicate around.
3203              */
3204             op = create_remote_stonith_op(client_id, request->xml, FALSE);
3205 
3206             crm_xml_add(request->xml, F_STONITH_OPERATION, STONITH_OP_RELAY);
3207             crm_xml_add(request->xml, F_STONITH_CLIENTID,
3208                         request->ipc_client->id);
3209             crm_xml_add(request->xml, F_STONITH_REMOTE_OP_ID, op->id);
3210             send_cluster_message(crm_get_peer(0, alternate_host),
3211                                  crm_msg_stonith_ng, request->xml, FALSE);
3212             pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_PENDING,
3213                              NULL);
3214 
3215         } else if (initiate_remote_stonith_op(request->ipc_client, request->xml,
3216                                               FALSE) == NULL) {
3217             fenced_set_protocol_error(&request->result);
3218 
3219         } else {
3220             pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_PENDING,
3221                              NULL);
3222         }
3223     }
3224 
3225     if (request->result.execution_status == PCMK_EXEC_PENDING) {
3226         return NULL;
3227     }
3228     return fenced_construct_reply(request->xml, NULL, &request->result);
3229 }
3230 
3231 // STONITH_OP_FENCE_HISTORY
3232 static xmlNode *
3233 handle_history_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3234 {
3235     xmlNode *reply = NULL;
3236     xmlNode *data = NULL;
3237 
3238     stonith_fence_history(request->xml, &data, request->peer,
3239                           request->call_options);
3240     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3241     if (!pcmk_is_set(request->call_options, st_opt_discard_reply)) {
3242         /* When the local node broadcasts its history, it sets
3243          * st_opt_discard_reply and doesn't need a reply.
3244          */
3245         reply = fenced_construct_reply(request->xml, data, &request->result);
3246     }
3247     free_xml(data);
3248     return reply;
3249 }
3250 
3251 // STONITH_OP_DEVICE_ADD
3252 static xmlNode *
3253 handle_device_add_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3254 {
3255     const char *device_id = NULL;
3256     const char *op = crm_element_value(request->xml, F_STONITH_OPERATION);
3257 
3258     if (is_privileged(request->ipc_client, op)) {
3259         int rc = stonith_device_register(request->xml, &device_id, FALSE);
3260 
3261         pcmk__set_result(&request->result,
3262                          ((rc == pcmk_ok)? CRM_EX_OK : CRM_EX_ERROR),
3263                          stonith__legacy2status(rc),
3264                          ((rc == pcmk_ok)? NULL : pcmk_strerror(rc)));
3265     } else {
3266         pcmk__set_result(&request->result, CRM_EX_INSUFFICIENT_PRIV,
3267                          PCMK_EXEC_INVALID,
3268                          "Unprivileged users must register device via CIB");
3269     }
3270     fenced_send_device_notification(op, &request->result, device_id);
3271     return fenced_construct_reply(request->xml, NULL, &request->result);
3272 }
3273 
3274 // STONITH_OP_DEVICE_DEL
3275 static xmlNode *
3276 handle_device_delete_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3277 {
3278     xmlNode *dev = get_xpath_object("//" F_STONITH_DEVICE, request->xml,
3279                                     LOG_ERR);
3280     const char *device_id = crm_element_value(dev, XML_ATTR_ID);
3281     const char *op = crm_element_value(request->xml, F_STONITH_OPERATION);
3282 
3283     if (is_privileged(request->ipc_client, op)) {
3284         stonith_device_remove(device_id, false);
3285         pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3286     } else {
3287         pcmk__set_result(&request->result, CRM_EX_INSUFFICIENT_PRIV,
3288                          PCMK_EXEC_INVALID,
3289                          "Unprivileged users must delete device via CIB");
3290     }
3291     fenced_send_device_notification(op, &request->result, device_id);
3292     return fenced_construct_reply(request->xml, NULL, &request->result);
3293 }
3294 
3295 // STONITH_OP_LEVEL_ADD
3296 static xmlNode *
3297 handle_level_add_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3298 {
3299     char *device_id = NULL;
3300     const char *op = crm_element_value(request->xml, F_STONITH_OPERATION);
3301 
3302     if (is_privileged(request->ipc_client, op)) {
3303         fenced_register_level(request->xml, &device_id, &request->result);
3304     } else {
3305         pcmk__set_result(&request->result, CRM_EX_INSUFFICIENT_PRIV,
3306                          PCMK_EXEC_INVALID,
3307                          "Unprivileged users must add level via CIB");
3308     }
3309     fenced_send_level_notification(op, &request->result, device_id);
3310     free(device_id);
3311     return fenced_construct_reply(request->xml, NULL, &request->result);
3312 }
3313 
3314 // STONITH_OP_LEVEL_DEL
3315 static xmlNode *
3316 handle_level_delete_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3317 {
3318     char *device_id = NULL;
3319     const char *op = crm_element_value(request->xml, F_STONITH_OPERATION);
3320 
3321     if (is_privileged(request->ipc_client, op)) {
3322         fenced_unregister_level(request->xml, &device_id, &request->result);
3323     } else {
3324         pcmk__set_result(&request->result, CRM_EX_INSUFFICIENT_PRIV,
3325                          PCMK_EXEC_INVALID,
3326                          "Unprivileged users must delete level via CIB");
3327     }
3328     fenced_send_level_notification(op, &request->result, device_id);
3329     return fenced_construct_reply(request->xml, NULL, &request->result);
3330 }
3331 
3332 // CRM_OP_RM_NODE_CACHE
3333 static xmlNode *
3334 handle_cache_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3335 {
3336     int node_id = 0;
3337     const char *name = NULL;
3338 
3339     crm_element_value_int(request->xml, XML_ATTR_ID, &node_id);
3340     name = crm_element_value(request->xml, XML_ATTR_UNAME);
3341     reap_crm_member(node_id, name);
3342     pcmk__set_result(&request->result, CRM_EX_OK, PCMK_EXEC_DONE, NULL);
3343     return NULL;
3344 }
3345 
3346 static xmlNode *
3347 handle_unknown_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3348 {
3349     const char *op = crm_element_value(request->xml, F_STONITH_OPERATION);
3350 
3351     crm_err("Unknown IPC request %s from %s %s",
3352             op, pcmk__request_origin_type(request),
3353             pcmk__request_origin(request));
3354     pcmk__format_result(&request->result, CRM_EX_PROTOCOL, PCMK_EXEC_INVALID,
3355                         "Unknown IPC request type '%s' (bug?)", crm_str(op));
3356     return fenced_construct_reply(request->xml, NULL, &request->result);
3357 }
3358 
3359 static void
3360 fenced_register_handlers(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3361 {
3362     pcmk__server_command_t handlers[] = {
3363         { CRM_OP_REGISTER, handle_register_request },
3364         { STONITH_OP_EXEC, handle_agent_request },
3365         { STONITH_OP_TIMEOUT_UPDATE, handle_update_timeout_request },
3366         { STONITH_OP_QUERY, handle_query_request },
3367         { T_STONITH_NOTIFY, handle_notify_request },
3368         { STONITH_OP_RELAY, handle_relay_request },
3369         { STONITH_OP_FENCE, handle_fence_request },
3370         { STONITH_OP_FENCE_HISTORY, handle_history_request },
3371         { STONITH_OP_DEVICE_ADD, handle_device_add_request },
3372         { STONITH_OP_DEVICE_DEL, handle_device_delete_request },
3373         { STONITH_OP_LEVEL_ADD, handle_level_add_request },
3374         { STONITH_OP_LEVEL_DEL, handle_level_delete_request },
3375         { CRM_OP_RM_NODE_CACHE, handle_cache_request },
3376         { NULL, handle_unknown_request },
3377     };
3378 
3379     fenced_handlers = pcmk__register_handlers(handlers);
3380 }
3381 
3382 void
3383 fenced_unregister_handlers(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3384 {
3385     if (fenced_handlers != NULL) {
3386         g_hash_table_destroy(fenced_handlers);
3387         fenced_handlers = NULL;
3388     }
3389 }
3390 
3391 static void
3392 handle_request(pcmk__request_t *request)
     /* [previous][next][first][last][top][bottom][index][help] */
3393 {
3394     xmlNode *reply = NULL;
3395     const char *reason = NULL;
3396 
3397     if (fenced_handlers == NULL) {
3398         fenced_register_handlers();
3399     }
3400     reply = pcmk__process_request(request, fenced_handlers);
3401     if (reply != NULL) {
3402         if (pcmk_is_set(request->flags, pcmk__request_reuse_options)
3403             && (request->ipc_client != NULL)) {
3404             /* Certain IPC-only commands must reuse the call options from the
3405              * original request rather than the ones set by stonith_send_reply()
3406              * -> do_local_reply().
3407              */
3408             pcmk__ipc_send_xml(request->ipc_client, request->ipc_id, reply,
3409                                request->ipc_flags);
3410             request->ipc_client->request_id = 0;
3411         } else {
3412             stonith_send_reply(reply, request->call_options,
3413                                request->peer, request->ipc_client);
3414         }
3415         free_xml(reply);
3416     }
3417 
3418     reason = request->result.exit_reason;
3419     crm_debug("Processed %s request from %s %s: %s%s%s%s",
3420               request->op, pcmk__request_origin_type(request),
3421               pcmk__request_origin(request),
3422               pcmk_exec_status_str(request->result.execution_status),
3423               (reason == NULL)? "" : " (",
3424               (reason == NULL)? "" : reason,
3425               (reason == NULL)? "" : ")");
3426 }
3427 
3428 static void
3429 handle_reply(pcmk__client_t *client, xmlNode *request, const char *remote_peer)
     /* [previous][next][first][last][top][bottom][index][help] */
3430 {
3431     // Copy, because request might be freed before we want to log this
3432     char *op = crm_element_value_copy(request, F_STONITH_OPERATION);
3433 
3434     if (pcmk__str_eq(op, STONITH_OP_QUERY, pcmk__str_none)) {
3435         process_remote_stonith_query(request);
3436     } else if (pcmk__str_any_of(op, T_STONITH_NOTIFY, STONITH_OP_FENCE, NULL)) {
3437         fenced_process_fencing_reply(request);
3438     } else {
3439         crm_err("Ignoring unknown %s reply from %s %s",
3440                 crm_str(op), ((client == NULL)? "peer" : "client"),
3441                 ((client == NULL)? remote_peer : pcmk__client_name(client)));
3442         crm_log_xml_warn(request, "UnknownOp");
3443         free(op);
3444         return;
3445     }
3446     crm_debug("Processed %s reply from %s %s",
3447               op, ((client == NULL)? "peer" : "client"),
3448               ((client == NULL)? remote_peer : pcmk__client_name(client)));
3449     free(op);
3450 }
3451 
3452 /*!
3453  * \internal
3454  * \brief Handle a message from an IPC client or CPG peer
3455  *
3456  * \param[in] client      If not NULL, IPC client that sent message
3457  * \param[in] id          If from IPC client, IPC message ID
3458  * \param[in] flags       Message flags
3459  * \param[in] message     Message XML
3460  * \param[in] remote_peer If not NULL, CPG peer that sent message
3461  */
3462 void
3463 stonith_command(pcmk__client_t *client, uint32_t id, uint32_t flags,
     /* [previous][next][first][last][top][bottom][index][help] */
3464                 xmlNode *message, const char *remote_peer)
3465 {
3466     int call_options = st_opt_none;
3467     bool is_reply = false;
3468 
3469     CRM_CHECK(message != NULL, return);
3470 
3471     if (get_xpath_object("//" T_STONITH_REPLY, message, LOG_NEVER) != NULL) {
3472         is_reply = true;
3473     }
3474     crm_element_value_int(message, F_STONITH_CALLOPTS, &call_options);
3475     crm_debug("Processing %ssynchronous %s %s %u from %s %s",
3476               pcmk_is_set(call_options, st_opt_sync_call)? "" : "a",
3477               crm_element_value(message, F_STONITH_OPERATION),
3478               (is_reply? "reply" : "request"), id,
3479               ((client == NULL)? "peer" : "client"),
3480               ((client == NULL)? remote_peer : pcmk__client_name(client)));
3481 
3482     if (pcmk_is_set(call_options, st_opt_sync_call)) {
3483         CRM_ASSERT(client == NULL || client->request_id == id);
3484     }
3485 
3486     if (is_reply) {
3487         handle_reply(client, message, remote_peer);
3488     } else {
3489         pcmk__request_t request = {
3490             .ipc_client     = client,
3491             .ipc_id         = id,
3492             .ipc_flags      = flags,
3493             .peer           = remote_peer,
3494             .xml            = message,
3495             .call_options   = call_options,
3496             .result         = PCMK__UNKNOWN_RESULT,
3497         };
3498 
3499         request.op = crm_element_value(request.xml, F_STONITH_OPERATION);
3500         CRM_CHECK(request.op != NULL, return);
3501 
3502         if (pcmk_is_set(request.call_options, st_opt_sync_call)) {
3503             pcmk__set_request_flags(&request, pcmk__request_sync);
3504         }
3505 
3506         handle_request(&request);
3507         pcmk__reset_result(&request.result);
3508     }
3509 }

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