root/lib/fencing/st_client.c

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

DEFINITIONS

This source file includes following definitions.
  1. log_action
  2. stonith_connection_destroy
  3. create_device_registration_xml
  4. stonith_api_register_device
  5. stonith_api_remove_device
  6. stonith_api_remove_level_full
  7. stonith_api_remove_level
  8. create_level_registration_xml
  9. stonith_api_register_level_full
  10. stonith_api_register_level
  11. append_arg
  12. append_config_arg
  13. append_host_specific_args
  14. make_args
  15. st_child_term
  16. st_child_kill
  17. stonith_action_clear_tracking_data
  18. stonith_action_destroy
  19. stonith_action_create
  20. read_output
  21. update_remaining_timeout
  22. stonith_action_async_done
  23. internal_stonith_action_execute
  24. stonith_action_execute_async
  25. stonith_action_execute
  26. stonith_api_device_list
  27. strdup_null
  28. stonith_plugin
  29. stonith_api_device_metadata
  30. stonith_api_query
  31. stonith_api_call
  32. stonith_api_list
  33. stonith_api_monitor
  34. stonith_api_status
  35. stonith_api_fence
  36. stonith_api_confirm
  37. stonith_api_history
  38. is_redhat_agent
  39. get_stonith_provider
  40. stonithlib_GCompareFunc
  41. stonith_create_op
  42. stonith_destroy_op_callback
  43. stonith_api_signoff
  44. stonith_api_signon
  45. stonith_set_notification
  46. stonith_api_add_notification
  47. stonith_api_del_notification
  48. stonith_async_timeout_handler
  49. set_callback_timeout
  50. update_callback_timeout
  51. invoke_callback
  52. stonith_api_add_callback
  53. stonith_api_del_callback
  54. stonith_dump_pending_op
  55. stonith_dump_pending_callbacks
  56. stonith_perform_callback
  57. xml_to_event
  58. event_free
  59. stonith_send_notification
  60. stonith_send_command
  61. stonith_dispatch
  62. stonith_dispatch_internal
  63. stonith_api_free
  64. stonith_api_delete
  65. stonith_api_new
  66. stonith_key_value_add
  67. stonith_key_value_freeall
  68. stonith_api_kick
  69. stonith_api_time
  70. i_hate_pils

   1 /*
   2  * Copyright (c) 2004 Andrew Beekhof <andrew@beekhof.net>
   3  *
   4  * This library is free software; you can redistribute it and/or
   5  * modify it under the terms of the GNU Lesser General Public
   6  * License as published by the Free Software Foundation; either
   7  * version 2.1 of the License, or (at your option) any later version.
   8  *
   9  * This library is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12  * Lesser General Public License for more details.
  13  *
  14  * You should have received a copy of the GNU Lesser General Public
  15  * License along with this library; if not, write to the Free Software
  16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17  *
  18  */
  19 #include <crm_internal.h>
  20 #include <unistd.h>
  21 #include <stdlib.h>
  22 #include <stdio.h>
  23 #include <stdarg.h>
  24 #include <string.h>
  25 #include <ctype.h>
  26 
  27 #include <sys/stat.h>
  28 #include <sys/types.h>
  29 #include <sys/wait.h>
  30 
  31 #include <glib.h>
  32 #include <dirent.h>
  33 #include <libgen.h>             /* Add it for compiling on OSX */
  34 
  35 #include <crm/crm.h>
  36 #include <crm/stonith-ng.h>
  37 #include <crm/fencing/internal.h>
  38 #include <crm/msg_xml.h>
  39 #include <crm/common/xml.h>
  40 
  41 #ifdef HAVE_STONITH_STONITH_H
  42 #  include <stonith/stonith.h>
  43 #  define LHA_STONITH_LIBRARY "libstonith.so.1"
  44 static void *lha_agents_lib = NULL;
  45 #endif
  46 
  47 #include <crm/common/mainloop.h>
  48 
  49 CRM_TRACE_INIT_DATA(stonith);
  50 
  51 struct stonith_action_s {
  52     /*! user defined data */
  53     char *agent;
  54     char *action;
  55     char *victim;
  56     char *args;
  57     int timeout;
  58     int async;
  59     void *userdata;
  60     void (*done_cb) (GPid pid, gint status, const char *output, gpointer user_data);
  61 
  62     /*! internal async track data */
  63     int fd_stdout;
  64     int fd_stderr;
  65     int last_timeout_signo;
  66 
  67     /*! internal timing information */
  68     time_t initial_start_time;
  69     int tries;
  70     int remaining_timeout;
  71     guint timer_sigterm;
  72     guint timer_sigkill;
  73     int max_retries;
  74 
  75     /* device output data */
  76     GPid pid;
  77     int rc;
  78     char *output;
  79     char *error;
  80 };
  81 
  82 typedef struct stonith_private_s {
  83     char *token;
  84     crm_ipc_t *ipc;
  85     mainloop_io_t *source;
  86     GHashTable *stonith_op_callback_table;
  87     GList *notify_list;
  88 
  89     void (*op_callback) (stonith_t * st, stonith_callback_data_t * data);
  90 
  91 } stonith_private_t;
  92 
  93 typedef struct stonith_notify_client_s {
  94     const char *event;
  95     const char *obj_id;         /* implement one day */
  96     const char *obj_type;       /* implement one day */
  97     void (*notify) (stonith_t * st, stonith_event_t * e);
  98 
  99 } stonith_notify_client_t;
 100 
 101 typedef struct stonith_callback_client_s {
 102     void (*callback) (stonith_t * st, stonith_callback_data_t * data);
 103     const char *id;
 104     void *user_data;
 105     gboolean only_success;
 106     gboolean allow_timeout_updates;
 107     struct timer_rec_s *timer;
 108 
 109 } stonith_callback_client_t;
 110 
 111 struct notify_blob_s {
 112     stonith_t *stonith;
 113     xmlNode *xml;
 114 };
 115 
 116 struct timer_rec_s {
 117     int call_id;
 118     int timeout;
 119     guint ref;
 120     stonith_t *stonith;
 121 };
 122 
 123 typedef int (*stonith_op_t) (const char *, int, const char *, xmlNode *,
 124                              xmlNode *, xmlNode *, xmlNode **, xmlNode **);
 125 
 126 #if HAVE_STONITH_STONITH_H
 127 static const char META_TEMPLATE[] =
 128     "<?xml version=\"1.0\"?>\n"
 129     "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
 130     "<resource-agent name=\"%s\">\n"
 131     "  <version>1.0</version>\n"
 132     "  <longdesc lang=\"en\">\n"
 133     "%s\n"
 134     "  </longdesc>\n"
 135     "  <shortdesc lang=\"en\">%s</shortdesc>\n"
 136     "%s\n"
 137     "  <actions>\n"
 138     "    <action name=\"start\"   timeout=\"20\" />\n"
 139     "    <action name=\"stop\"    timeout=\"15\" />\n"
 140     "    <action name=\"status\"  timeout=\"20\" />\n"
 141     "    <action name=\"monitor\" timeout=\"20\" interval=\"3600\"/>\n"
 142     "    <action name=\"meta-data\"  timeout=\"15\" />\n"
 143     "  </actions>\n"
 144     "  <special tag=\"heartbeat\">\n"
 145     "    <version>2.0</version>\n" "  </special>\n" "</resource-agent>\n";
 146 #endif
 147 
 148 bool stonith_dispatch(stonith_t * st);
 149 int stonith_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata);
 150 void stonith_perform_callback(stonith_t * stonith, xmlNode * msg, int call_id, int rc);
 151 xmlNode *stonith_create_op(int call_id, const char *token, const char *op, xmlNode * data,
 152                            int call_options);
 153 int stonith_send_command(stonith_t * stonith, const char *op, xmlNode * data,
 154                          xmlNode ** output_data, int call_options, int timeout);
 155 
 156 static void stonith_connection_destroy(gpointer user_data);
 157 static void stonith_send_notification(gpointer data, gpointer user_data);
 158 static int internal_stonith_action_execute(stonith_action_t * action);
 159 static void log_action(stonith_action_t *action, pid_t pid);
 160 
 161 static void
 162 log_action(stonith_action_t *action, pid_t pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164     if (action->output) {
 165         /* Logging the whole string confuses syslog when the string is xml */
 166         char *prefix = crm_strdup_printf("%s[%d] stdout:", action->agent, pid);
 167 
 168         crm_log_output(LOG_TRACE, prefix, action->output);
 169         free(prefix);
 170     }
 171 
 172     if (action->error) {
 173         /* Logging the whole string confuses syslog when the string is xml */
 174         char *prefix = crm_strdup_printf("%s[%d] stderr:", action->agent, pid);
 175 
 176         crm_log_output(LOG_WARNING, prefix, action->error);
 177         free(prefix);
 178     }
 179 }
 180 
 181 static void
 182 stonith_connection_destroy(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184     stonith_t *stonith = user_data;
 185     stonith_private_t *native = NULL;
 186     struct notify_blob_s blob;
 187 
 188     crm_trace("Sending destroyed notification");
 189     blob.stonith = stonith;
 190     blob.xml = create_xml_node(NULL, "notify");
 191 
 192     native = stonith->private;
 193     native->ipc = NULL;
 194     native->source = NULL;
 195 
 196     stonith->state = stonith_disconnected;
 197     crm_xml_add(blob.xml, F_TYPE, T_STONITH_NOTIFY);
 198     crm_xml_add(blob.xml, F_SUBTYPE, T_STONITH_NOTIFY_DISCONNECT);
 199 
 200     g_list_foreach(native->notify_list, stonith_send_notification, &blob);
 201     free_xml(blob.xml);
 202 }
 203 
 204 xmlNode *
 205 create_device_registration_xml(const char *id, const char *namespace, const char *agent,
     /* [previous][next][first][last][top][bottom][index][help] */
 206                                stonith_key_value_t * params, const char *rsc_provides)
 207 {
 208     xmlNode *data = create_xml_node(NULL, F_STONITH_DEVICE);
 209     xmlNode *args = create_xml_node(data, XML_TAG_ATTRS);
 210 
 211 #if HAVE_STONITH_STONITH_H
 212     namespace = get_stonith_provider(agent, namespace);
 213     if (safe_str_eq(namespace, "heartbeat")) {
 214         hash2field((gpointer) "plugin", (gpointer) agent, args);
 215         agent = "fence_legacy";
 216     }
 217 #endif
 218 
 219     crm_xml_add(data, XML_ATTR_ID, id);
 220     crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
 221     crm_xml_add(data, "agent", agent);
 222     crm_xml_add(data, "namespace", namespace);
 223     if (rsc_provides) {
 224         crm_xml_add(data, "rsc_provides", rsc_provides);
 225     }
 226 
 227     for (; params; params = params->next) {
 228         hash2field((gpointer) params->key, (gpointer) params->value, args);
 229     }
 230 
 231     return data;
 232 }
 233 
 234 static int
 235 stonith_api_register_device(stonith_t * st, int call_options,
     /* [previous][next][first][last][top][bottom][index][help] */
 236                             const char *id, const char *namespace, const char *agent,
 237                             stonith_key_value_t * params)
 238 {
 239     int rc = 0;
 240     xmlNode *data = NULL;
 241 
 242     data = create_device_registration_xml(id, namespace, agent, params, NULL);
 243 
 244     rc = stonith_send_command(st, STONITH_OP_DEVICE_ADD, data, NULL, call_options, 0);
 245     free_xml(data);
 246 
 247     return rc;
 248 }
 249 
 250 static int
 251 stonith_api_remove_device(stonith_t * st, int call_options, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253     int rc = 0;
 254     xmlNode *data = NULL;
 255 
 256     data = create_xml_node(NULL, F_STONITH_DEVICE);
 257     crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
 258     crm_xml_add(data, XML_ATTR_ID, name);
 259     rc = stonith_send_command(st, STONITH_OP_DEVICE_DEL, data, NULL, call_options, 0);
 260     free_xml(data);
 261 
 262     return rc;
 263 }
 264 
 265 static int
 266 stonith_api_remove_level_full(stonith_t *st, int options,
     /* [previous][next][first][last][top][bottom][index][help] */
 267                               const char *node, const char *pattern,
 268                               const char *attr, const char *value, int level)
 269 {
 270     int rc = 0;
 271     xmlNode *data = NULL;
 272 
 273     CRM_CHECK(node || pattern || (attr && value), return -EINVAL);
 274 
 275     data = create_xml_node(NULL, XML_TAG_FENCING_LEVEL);
 276     crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
 277 
 278     if (node) {
 279         crm_xml_add(data, XML_ATTR_STONITH_TARGET, node);
 280 
 281     } else if (pattern) {
 282         crm_xml_add(data, XML_ATTR_STONITH_TARGET_PATTERN, pattern);
 283 
 284     } else {
 285         crm_xml_add(data, XML_ATTR_STONITH_TARGET_ATTRIBUTE, attr);
 286         crm_xml_add(data, XML_ATTR_STONITH_TARGET_VALUE, value);
 287     }
 288 
 289     crm_xml_add_int(data, XML_ATTR_STONITH_INDEX, level);
 290     rc = stonith_send_command(st, STONITH_OP_LEVEL_DEL, data, NULL, options, 0);
 291     free_xml(data);
 292 
 293     return rc;
 294 }
 295 
 296 static int
 297 stonith_api_remove_level(stonith_t * st, int options, const char *node, int level)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299     return stonith_api_remove_level_full(st, options, node,
 300                                          NULL, NULL, NULL, level);
 301 }
 302 
 303 /*!
 304  * \internal
 305  * \brief Create XML for stonithd topology level registration request
 306  *
 307  * \param[in] node        If not NULL, target level by this node name
 308  * \param[in] pattern     If not NULL, target by node name using this regex
 309  * \param[in] attr        If not NULL, target by this node attribute
 310  * \param[in] value       If not NULL, target by this node attribute value
 311  * \param[in] level       Index number of level to register
 312  * \param[in] device_list List of devices in level
 313  *
 314  * \return Newly allocated XML tree on success, NULL otherwise
 315  *
 316  * \note The caller should set only one of node, pattern or attr/value.
 317  */
 318 xmlNode *
 319 create_level_registration_xml(const char *node, const char *pattern,
     /* [previous][next][first][last][top][bottom][index][help] */
 320                               const char *attr, const char *value,
 321                               int level, stonith_key_value_t *device_list)
 322 {
 323     int len = 0;
 324     char *list = NULL;
 325     xmlNode *data;
 326 
 327     CRM_CHECK(node || pattern || (attr && value), return NULL);
 328 
 329     data = create_xml_node(NULL, XML_TAG_FENCING_LEVEL);
 330     CRM_CHECK(data, return NULL);
 331 
 332     crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
 333     crm_xml_add_int(data, XML_ATTR_ID, level);
 334     crm_xml_add_int(data, XML_ATTR_STONITH_INDEX, level);
 335 
 336     if (node) {
 337         crm_xml_add(data, XML_ATTR_STONITH_TARGET, node);
 338 
 339     } else if (pattern) {
 340         crm_xml_add(data, XML_ATTR_STONITH_TARGET_PATTERN, pattern);
 341 
 342     } else {
 343         crm_xml_add(data, XML_ATTR_STONITH_TARGET_ATTRIBUTE, attr);
 344         crm_xml_add(data, XML_ATTR_STONITH_TARGET_VALUE, value);
 345     }
 346 
 347     for (; device_list; device_list = device_list->next) {
 348 
 349         int adding = strlen(device_list->value);
 350         if(list) {
 351             adding++;                                      /* +1 space */
 352         }
 353 
 354         crm_trace("Adding %s (%dc) at offset %d", device_list->value, adding, len);
 355         list = realloc_safe(list, len + adding + 1);       /* +1 EOS */
 356         if (list == NULL) {
 357             crm_perror(LOG_CRIT, "Could not create device list");
 358             free_xml(data);
 359             return NULL;
 360         }
 361         sprintf(list + len, "%s%s", len?",":"", device_list->value);
 362         len += adding;
 363     }
 364 
 365     crm_xml_add(data, XML_ATTR_STONITH_DEVICES, list);
 366 
 367     free(list);
 368     return data;
 369 }
 370 
 371 static int
 372 stonith_api_register_level_full(stonith_t * st, int options, const char *node,
     /* [previous][next][first][last][top][bottom][index][help] */
 373                                 const char *pattern,
 374                                 const char *attr, const char *value,
 375                                 int level, stonith_key_value_t *device_list)
 376 {
 377     int rc = 0;
 378     xmlNode *data = create_level_registration_xml(node, pattern, attr, value,
 379                                                   level, device_list);
 380     CRM_CHECK(data != NULL, return -EINVAL);
 381 
 382     rc = stonith_send_command(st, STONITH_OP_LEVEL_ADD, data, NULL, options, 0);
 383     free_xml(data);
 384 
 385     return rc;
 386 }
 387 
 388 static int
 389 stonith_api_register_level(stonith_t * st, int options, const char *node, int level,
     /* [previous][next][first][last][top][bottom][index][help] */
 390                            stonith_key_value_t * device_list)
 391 {
 392     return stonith_api_register_level_full(st, options, node, NULL, NULL, NULL,
 393                                            level, device_list);
 394 }
 395 
 396 static void
 397 append_arg(const char *key, const char *value, char **args)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399     int len = 3;                /* =, \n, \0 */
 400     int last = 0;
 401 
 402     CRM_CHECK(key != NULL, return);
 403     CRM_CHECK(value != NULL, return);
 404 
 405     if (strstr(key, "pcmk_")) {
 406         return;
 407     } else if (strstr(key, CRM_META)) {
 408         return;
 409     } else if (safe_str_eq(key, "crm_feature_set")) {
 410         return;
 411     }
 412 
 413     len += strlen(key);
 414     len += strlen(value);
 415     if (*args != NULL) {
 416         last = strlen(*args);
 417     }
 418 
 419     *args = realloc_safe(*args, last + len);
 420     crm_trace("Appending: %s=%s", key, value);
 421     sprintf((*args) + last, "%s=%s\n", key, value);
 422 }
 423 
 424 static void
 425 append_config_arg(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 426 {
 427     /* stonithd will filter action out when it registers the device,
 428      * but ignore it here just in case any other library callers
 429      * fail to do so.
 430      */
 431     if (safe_str_neq(key, STONITH_ATTR_ACTION_OP)) {
 432         append_arg(key, value, user_data);
 433         return;
 434     }
 435 }
 436 
 437 static void
 438 append_host_specific_args(const char *victim, const char *map, GHashTable * params, char **arg_list)
     /* [previous][next][first][last][top][bottom][index][help] */
 439 {
 440     char *name = NULL;
 441     int last = 0, lpc = 0, max = 0;
 442 
 443     if (map == NULL) {
 444         /* The best default there is for now... */
 445         crm_debug("Using default arg map: port=uname");
 446         append_arg("port", victim, arg_list);
 447         return;
 448     }
 449 
 450     max = strlen(map);
 451     crm_debug("Processing arg map: %s", map);
 452     for (; lpc < max + 1; lpc++) {
 453         if (isalpha(map[lpc])) {
 454             /* keep going */
 455 
 456         } else if (map[lpc] == '=' || map[lpc] == ':') {
 457             free(name);
 458             name = calloc(1, 1 + lpc - last);
 459             memcpy(name, map + last, lpc - last);
 460             crm_debug("Got name: %s", name);
 461             last = lpc + 1;
 462 
 463         } else if (map[lpc] == 0 || map[lpc] == ',' || isspace(map[lpc])) {
 464             char *param = NULL;
 465             const char *value = NULL;
 466 
 467             param = calloc(1, 1 + lpc - last);
 468             memcpy(param, map + last, lpc - last);
 469             last = lpc + 1;
 470 
 471             crm_debug("Got key: %s", param);
 472             if (name == NULL) {
 473                 crm_err("Misparsed '%s', found '%s' without a name", map, param);
 474                 free(param);
 475                 continue;
 476             }
 477 
 478             if (safe_str_eq(param, "uname")) {
 479                 value = victim;
 480             } else {
 481                 char *key = crm_meta_name(param);
 482 
 483                 value = g_hash_table_lookup(params, key);
 484                 free(key);
 485             }
 486 
 487             if (value) {
 488                 crm_debug("Setting '%s'='%s' (%s) for %s", name, value, param, victim);
 489                 append_arg(name, value, arg_list);
 490 
 491             } else {
 492                 crm_err("No node attribute '%s' for '%s'", name, victim);
 493             }
 494 
 495             free(name);
 496             name = NULL;
 497             free(param);
 498             if (map[lpc] == 0) {
 499                 break;
 500             }
 501 
 502         } else if (isspace(map[lpc])) {
 503             last = lpc;
 504         }
 505     }
 506     free(name);
 507 }
 508 
 509 static char *
 510 make_args(const char *agent, const char *action, const char *victim, uint32_t victim_nodeid, GHashTable * device_args,
     /* [previous][next][first][last][top][bottom][index][help] */
 511           GHashTable * port_map)
 512 {
 513     char buffer[512];
 514     char *arg_list = NULL;
 515     const char *value = NULL;
 516 
 517     CRM_CHECK(action != NULL, return NULL);
 518 
 519     snprintf(buffer, sizeof(buffer), "pcmk_%s_action", action);
 520     if (device_args) {
 521         value = g_hash_table_lookup(device_args, buffer);
 522     }
 523 
 524     if (value == NULL && device_args) {
 525         /* @COMPAT deprecated since 1.1.6 */
 526         snprintf(buffer, sizeof(buffer), "pcmk_%s_cmd", action);
 527         value = g_hash_table_lookup(device_args, buffer);
 528     }
 529 
 530     if (value == NULL && device_args && safe_str_eq(action, "off")) {
 531         /* @COMPAT deprecated since 1.1.8 */
 532         value = g_hash_table_lookup(device_args, "pcmk_poweroff_action");
 533     }
 534 
 535     if (value) {
 536         crm_info("Substituting action '%s' for requested operation '%s'", value, action);
 537         action = value;
 538     }
 539 
 540     append_arg(STONITH_ATTR_ACTION_OP, action, &arg_list);
 541     if (victim && device_args) {
 542         const char *alias = victim;
 543         const char *param = g_hash_table_lookup(device_args, STONITH_ATTR_HOSTARG);
 544 
 545         if (port_map && g_hash_table_lookup(port_map, victim)) {
 546             alias = g_hash_table_lookup(port_map, victim);
 547         }
 548 
 549         /* Always supply the node's name too:
 550          *    https://fedorahosted.org/cluster/wiki/FenceAgentAPI
 551          */
 552         append_arg("nodename", victim, &arg_list);
 553         if (victim_nodeid) {
 554             char nodeid_str[33] = { 0, };
 555             if (snprintf(nodeid_str, 33, "%u", (unsigned int)victim_nodeid)) {
 556                 crm_info("For stonith action (%s) for victim %s, adding nodeid (%s) to parameters",
 557                          action, victim, nodeid_str);
 558                 append_arg("nodeid", nodeid_str, &arg_list);
 559             }
 560         }
 561 
 562         /* Check if we need to supply the victim in any other form */
 563         if(safe_str_eq(agent, "fence_legacy")) {
 564             value = agent;
 565 
 566         } else if (param == NULL) {
 567             // @COMPAT config < 1.1.6
 568             // pcmk_arg_map is deprecated in favor of pcmk_host_argument
 569             const char *map = g_hash_table_lookup(device_args, STONITH_ATTR_ARGMAP);
 570 
 571             if (map == NULL) {
 572                 param = "port";
 573                 value = g_hash_table_lookup(device_args, param);
 574 
 575             } else {
 576                 append_host_specific_args(alias, map, device_args, &arg_list);
 577                 value = map;    /* Nothing more to do */
 578             }
 579 
 580         } else if (safe_str_eq(param, "none")) {
 581             value = param;      /* Nothing more to do */
 582 
 583         } else {
 584             value = g_hash_table_lookup(device_args, param);
 585         }
 586 
 587         /* Don't overwrite explictly set values for $param */
 588         if (value == NULL || safe_str_eq(value, "dynamic")) {
 589             crm_debug("Performing %s action for node '%s' as '%s=%s'", action, victim, param,
 590                       alias);
 591             append_arg(param, alias, &arg_list);
 592         }
 593     }
 594 
 595     if (device_args) {
 596         g_hash_table_foreach(device_args, append_config_arg, &arg_list);
 597     }
 598 
 599     return arg_list;
 600 }
 601 
 602 static gboolean
 603 st_child_term(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 604 {
 605     int rc = 0;
 606     stonith_action_t *track = data;
 607 
 608     crm_info("Child %d timed out, sending SIGTERM", track->pid);
 609     track->timer_sigterm = 0;
 610     track->last_timeout_signo = SIGTERM;
 611     rc = kill(-track->pid, SIGTERM);
 612     if (rc < 0) {
 613         crm_perror(LOG_ERR, "Couldn't send SIGTERM to %d", track->pid);
 614     }
 615     return FALSE;
 616 }
 617 
 618 static gboolean
 619 st_child_kill(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 620 {
 621     int rc = 0;
 622     stonith_action_t *track = data;
 623 
 624     crm_info("Child %d timed out, sending SIGKILL", track->pid);
 625     track->timer_sigkill = 0;
 626     track->last_timeout_signo = SIGKILL;
 627     rc = kill(-track->pid, SIGKILL);
 628     if (rc < 0) {
 629         crm_perror(LOG_ERR, "Couldn't send SIGKILL to %d", track->pid);
 630     }
 631     return FALSE;
 632 }
 633 
 634 static void
 635 stonith_action_clear_tracking_data(stonith_action_t * action)
     /* [previous][next][first][last][top][bottom][index][help] */
 636 {
 637     if (action->timer_sigterm > 0) {
 638         g_source_remove(action->timer_sigterm);
 639         action->timer_sigterm = 0;
 640     }
 641     if (action->timer_sigkill > 0) {
 642         g_source_remove(action->timer_sigkill);
 643         action->timer_sigkill = 0;
 644     }
 645     if (action->fd_stdout) {
 646         close(action->fd_stdout);
 647         action->fd_stdout = 0;
 648     }
 649     if (action->fd_stderr) {
 650         close(action->fd_stderr);
 651         action->fd_stderr = 0;
 652     }
 653     free(action->output);
 654     action->output = NULL;
 655     free(action->error);
 656     action->error = NULL;
 657     action->rc = 0;
 658     action->pid = 0;
 659     action->last_timeout_signo = 0;
 660 }
 661 
 662 static void
 663 stonith_action_destroy(stonith_action_t * action)
     /* [previous][next][first][last][top][bottom][index][help] */
 664 {
 665     stonith_action_clear_tracking_data(action);
 666     free(action->agent);
 667     free(action->args);
 668     free(action->action);
 669     free(action->victim);
 670     free(action);
 671 }
 672 
 673 #define FAILURE_MAX_RETRIES 2
 674 stonith_action_t *
 675 stonith_action_create(const char *agent,
     /* [previous][next][first][last][top][bottom][index][help] */
 676                       const char *_action,
 677                       const char *victim,
 678                       uint32_t victim_nodeid,
 679                       int timeout, GHashTable * device_args, GHashTable * port_map)
 680 {
 681     stonith_action_t *action;
 682 
 683     action = calloc(1, sizeof(stonith_action_t));
 684     crm_debug("Initiating action %s for agent %s (target=%s)", _action, agent, victim);
 685     action->args = make_args(agent, _action, victim, victim_nodeid, device_args, port_map);
 686     action->agent = strdup(agent);
 687     action->action = strdup(_action);
 688     if (victim) {
 689         action->victim = strdup(victim);
 690     }
 691     action->timeout = action->remaining_timeout = timeout;
 692     action->max_retries = FAILURE_MAX_RETRIES;
 693 
 694     if (device_args) {
 695         char buffer[512];
 696         const char *value = NULL;
 697 
 698         snprintf(buffer, sizeof(buffer), "pcmk_%s_retries", _action);
 699         value = g_hash_table_lookup(device_args, buffer);
 700 
 701         if (value) {
 702             action->max_retries = atoi(value);
 703         }
 704     }
 705 
 706     return action;
 707 }
 708 
 709 #define READ_MAX 500
 710 static char *
 711 read_output(int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
 712 {
 713     char buffer[READ_MAX];
 714     char *output = NULL;
 715     int len = 0;
 716     int more = 0;
 717 
 718     if (!fd) {
 719         return NULL;
 720     }
 721 
 722     do {
 723         errno = 0;
 724         memset(&buffer, 0, READ_MAX);
 725         more = read(fd, buffer, READ_MAX - 1);
 726 
 727         if (more > 0) {
 728             buffer[more] = 0; /* Make sure it's nul-terminated for logging
 729                               * 'more' is always less than our buffer size
 730                               */
 731             output = realloc_safe(output, len + more + 1);
 732             snprintf(output + len, more + 1, "%s", buffer);
 733             len += more;
 734         }
 735 
 736     } while (more == (READ_MAX - 1) || (more < 0 && errno == EINTR));
 737 
 738     return output;
 739 }
 740 
 741 static gboolean
 742 update_remaining_timeout(stonith_action_t * action)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744     int diff = time(NULL) - action->initial_start_time;
 745 
 746     if (action->tries >= action->max_retries) {
 747         crm_info("Attempted to execute agent %s (%s) the maximum number of times (%d) allowed",
 748                  action->agent, action->action, action->max_retries);
 749         action->remaining_timeout = 0;
 750     } else if ((action->rc != -ETIME) && diff < (action->timeout * 0.7)) {
 751         /* only set remaining timeout period if there is 30%
 752          * or greater of the original timeout period left */
 753         action->remaining_timeout = action->timeout - diff;
 754     } else {
 755         action->remaining_timeout = 0;
 756     }
 757     return action->remaining_timeout ? TRUE : FALSE;
 758 }
 759 
 760 static void
 761 stonith_action_async_done(mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode)
     /* [previous][next][first][last][top][bottom][index][help] */
 762 {
 763     stonith_action_t *action = mainloop_child_userdata(p);
 764 
 765     if (action->timer_sigterm > 0) {
 766         g_source_remove(action->timer_sigterm);
 767         action->timer_sigterm = 0;
 768     }
 769     if (action->timer_sigkill > 0) {
 770         g_source_remove(action->timer_sigkill);
 771         action->timer_sigkill = 0;
 772     }
 773 
 774     action->output = read_output(action->fd_stdout);
 775     action->error = read_output(action->fd_stderr);
 776 
 777     if (action->last_timeout_signo) {
 778         action->rc = -ETIME;
 779         crm_notice("Child process %d performing action '%s' timed out with signal %d",
 780                    pid, action->action, action->last_timeout_signo);
 781 
 782     } else if (signo) {
 783         action->rc = -ECONNABORTED;
 784         crm_notice("Child process %d performing action '%s' timed out with signal %d",
 785                    pid, action->action, signo);
 786 
 787     } else {
 788         crm_debug("Child process %d performing action '%s' exited with rc %d",
 789                   pid, action->action, exitcode);
 790         if (exitcode > 0) {
 791             /* Try to provide a useful error code based on the fence agent's
 792              * error output.
 793              */
 794             if (action->error == NULL) {
 795                 exitcode = -ENODATA;
 796 
 797             } else if (strstr(action->error, "imed out")) {
 798                 /* Some agents have their own internal timeouts */
 799                 exitcode = -ETIMEDOUT;
 800 
 801             } else if (strstr(action->error, "Unrecognised action")) {
 802                 exitcode = -EOPNOTSUPP;
 803 
 804             } else {
 805                 exitcode = -pcmk_err_generic;
 806             }
 807         }
 808         action->rc = exitcode;
 809     }
 810 
 811     log_action(action, pid);
 812 
 813     if (action->rc != pcmk_ok && update_remaining_timeout(action)) {
 814         int rc = internal_stonith_action_execute(action);
 815         if (rc == pcmk_ok) {
 816             return;
 817         }
 818     }
 819 
 820     if (action->done_cb) {
 821         action->done_cb(pid, action->rc, action->output, action->userdata);
 822     }
 823 
 824     stonith_action_destroy(action);
 825 }
 826 
 827 static int
 828 internal_stonith_action_execute(stonith_action_t * action)
     /* [previous][next][first][last][top][bottom][index][help] */
 829 {
 830     int pid, status = 0, len, rc = -EPROTO;
 831     int ret;
 832     int total = 0;
 833     int p_read_fd, p_write_fd;  /* parent read/write file descriptors */
 834     int c_read_fd, c_write_fd;  /* child read/write file descriptors */
 835     int c_stderr_fd, p_stderr_fd; /* parent/child side file descriptors for stderr */
 836     int fd1[2];
 837     int fd2[2];
 838     int fd3[2];
 839     int is_retry = 0;
 840 
 841     /* clear any previous tracking data */
 842     stonith_action_clear_tracking_data(action);
 843 
 844     if (!action->tries) {
 845         action->initial_start_time = time(NULL);
 846     }
 847     action->tries++;
 848 
 849     if (action->tries > 1) {
 850         crm_info("Attempt %d to execute %s (%s). remaining timeout is %d",
 851                  action->tries, action->agent, action->action, action->remaining_timeout);
 852         is_retry = 1;
 853     }
 854 
 855     c_read_fd = c_write_fd = p_read_fd = p_write_fd = c_stderr_fd = p_stderr_fd = -1;
 856 
 857     if (action->args == NULL || action->agent == NULL)
 858         goto fail;
 859     len = strlen(action->args);
 860 
 861     if (pipe(fd1))
 862         goto fail;
 863     p_read_fd = fd1[0];
 864     c_write_fd = fd1[1];
 865 
 866     if (pipe(fd2))
 867         goto fail;
 868     c_read_fd = fd2[0];
 869     p_write_fd = fd2[1];
 870 
 871     if (pipe(fd3))
 872         goto fail;
 873     p_stderr_fd = fd3[0];
 874     c_stderr_fd = fd3[1];
 875 
 876     crm_debug("forking");
 877     pid = fork();
 878     if (pid < 0) {
 879         rc = -ECHILD;
 880         goto fail;
 881     }
 882 
 883     if (!pid) {
 884         /* child */
 885         setpgid(0, 0);
 886 
 887         close(1);
 888         /* coverity[leaked_handle] False positive */
 889         if (dup(c_write_fd) < 0)
 890             goto fail;
 891         close(2);
 892         /* coverity[leaked_handle] False positive */
 893         if (dup(c_stderr_fd) < 0)
 894             goto fail;
 895         close(0);
 896         /* coverity[leaked_handle] False positive */
 897         if (dup(c_read_fd) < 0)
 898             goto fail;
 899 
 900         /* keep c_stderr_fd open so parent can report all errors. */
 901         /* keep c_write_fd open so hostlist can be sent to parent. */
 902         close(c_read_fd);
 903         close(p_read_fd);
 904         close(p_write_fd);
 905         close(p_stderr_fd);
 906 
 907         /* keep retries from executing out of control */
 908         if (is_retry) {
 909             sleep(1);
 910         }
 911         execlp(action->agent, action->agent, NULL);
 912         exit(EXIT_FAILURE);
 913     }
 914 
 915     /* parent */
 916     action->pid = pid;
 917     ret = crm_set_nonblocking(p_read_fd);
 918     if (ret < 0) {
 919         crm_notice("Could not set output of %s to be non-blocking: %s "
 920                    CRM_XS " rc=%d",
 921                    action->agent, pcmk_strerror(rc), rc);
 922     }
 923     ret = crm_set_nonblocking(p_stderr_fd);
 924     if (ret < 0) {
 925         crm_notice("Could not set error output of %s to be non-blocking: %s "
 926                    CRM_XS " rc=%d",
 927                    action->agent, pcmk_strerror(rc), rc);
 928     }
 929 
 930     do {
 931         crm_debug("sending args");
 932         ret = write(p_write_fd, action->args + total, len - total);
 933         if (ret > 0) {
 934             total += ret;
 935         }
 936 
 937     } while (errno == EINTR && total < len);
 938 
 939     if (total != len) {
 940         crm_perror(LOG_ERR, "Sent %d not %d bytes", total, len);
 941         if (ret >= 0) {
 942             rc = -ECOMM;
 943         }
 944         goto fail;
 945     }
 946 
 947     close(p_write_fd); p_write_fd = -1;
 948 
 949     /* async */
 950     if (action->async) {
 951         action->fd_stdout = p_read_fd;
 952         action->fd_stderr = p_stderr_fd;
 953         mainloop_child_add(pid, 0/* Move the timeout here? */, action->action, action, stonith_action_async_done);
 954         crm_trace("Op: %s on %s, pid: %d, timeout: %ds", action->action, action->agent, pid,
 955                   action->remaining_timeout);
 956         action->last_timeout_signo = 0;
 957         if (action->remaining_timeout) {
 958             action->timer_sigterm =
 959                 g_timeout_add(1000 * action->remaining_timeout, st_child_term, action);
 960             action->timer_sigkill =
 961                 g_timeout_add(1000 * (action->remaining_timeout + 5), st_child_kill, action);
 962         } else {
 963             crm_err("No timeout set for stonith operation %s with device %s",
 964                     action->action, action->agent);
 965         }
 966 
 967         close(c_write_fd);
 968         close(c_read_fd);
 969         close(c_stderr_fd);
 970         return 0;
 971 
 972     } else {
 973         /* sync */
 974         int timeout = action->remaining_timeout + 1;
 975         pid_t p = 0;
 976 
 977         while (action->remaining_timeout < 0 || timeout > 0) {
 978             p = waitpid(pid, &status, WNOHANG);
 979             if (p > 0) {
 980                 break;
 981             }
 982             sleep(1);
 983             timeout--;
 984         }
 985 
 986         if (timeout == 0) {
 987             int killrc = kill(-pid, SIGKILL);
 988 
 989             if (killrc && errno != ESRCH) {
 990                 crm_err("kill(%d, KILL) failed: %s (%d)", pid, pcmk_strerror(errno), errno);
 991             }
 992             /*
 993              * From sigprocmask(2):
 994              * It is not possible to block SIGKILL or SIGSTOP.  Attempts to do so are silently ignored.
 995              *
 996              * This makes it safe to skip WNOHANG here
 997              */
 998             p = waitpid(pid, &status, 0);
 999         }
1000 
1001         if (p <= 0) {
1002             crm_perror(LOG_ERR, "waitpid(%d)", pid);
1003 
1004         } else if (p != pid) {
1005             crm_err("Waited for %d, got %d", pid, p);
1006         }
1007 
1008         action->output = read_output(p_read_fd);
1009         action->error = read_output(p_stderr_fd);
1010 
1011         action->rc = -ECONNABORTED;
1012 
1013         log_action(action, pid);
1014 
1015         rc = action->rc;
1016         if (timeout == 0) {
1017             action->rc = -ETIME;
1018         } else if (WIFEXITED(status)) {
1019             crm_debug("result = %d", WEXITSTATUS(status));
1020             action->rc = -WEXITSTATUS(status);
1021             rc = 0;
1022 
1023         } else if (WIFSIGNALED(status)) {
1024             crm_err("call %s for %s exited due to signal %d", action->action, action->agent,
1025                     WTERMSIG(status));
1026 
1027         } else {
1028             crm_err("call %s for %s returned unexpected status %#x",
1029                     action->action, action->agent, status);
1030         }
1031     }
1032 
1033   fail:
1034 
1035     if (p_read_fd >= 0) {
1036         close(p_read_fd);
1037     }
1038     if (p_write_fd >= 0) {
1039         close(p_write_fd);
1040     }
1041     if (p_stderr_fd >= 0) {
1042         close(p_stderr_fd);
1043     }
1044 
1045     if (c_read_fd >= 0) {
1046         close(c_read_fd);
1047     }
1048     if (c_write_fd >= 0) {
1049         close(c_write_fd);
1050     }
1051     if (c_stderr_fd >= 0) {
1052         close(c_stderr_fd);
1053     }
1054 
1055     return rc;
1056 }
1057 
1058 GPid
1059 stonith_action_execute_async(stonith_action_t * action,
     /* [previous][next][first][last][top][bottom][index][help] */
1060                              void *userdata,
1061                              void (*done) (GPid pid, int rc, const char *output,
1062                                            gpointer user_data))
1063 {
1064     int rc = 0;
1065 
1066     if (!action) {
1067         return -1;
1068     }
1069 
1070     action->userdata = userdata;
1071     action->done_cb = done;
1072     action->async = 1;
1073 
1074     rc = internal_stonith_action_execute(action);
1075 
1076     return rc < 0 ? rc : action->pid;
1077 }
1078 
1079 int
1080 stonith_action_execute(stonith_action_t * action, int *agent_result, char **output)
     /* [previous][next][first][last][top][bottom][index][help] */
1081 {
1082     int rc = 0;
1083 
1084     if (!action) {
1085         return -1;
1086     }
1087 
1088     do {
1089         rc = internal_stonith_action_execute(action);
1090         if (rc == pcmk_ok) {
1091             /* success! */
1092             break;
1093         }
1094         /* keep retrying while we have time left */
1095     } while (update_remaining_timeout(action));
1096 
1097     if (rc) {
1098         /* error */
1099         return rc;
1100     }
1101 
1102     if (agent_result) {
1103         *agent_result = action->rc;
1104     }
1105     if (output) {
1106         *output = action->output;
1107         action->output = NULL;  /* handed it off, do not free */
1108     }
1109 
1110     stonith_action_destroy(action);
1111     return rc;
1112 }
1113 
1114 static int
1115 stonith_api_device_list(stonith_t * stonith, int call_options, const char *namespace,
     /* [previous][next][first][last][top][bottom][index][help] */
1116                         stonith_key_value_t ** devices, int timeout)
1117 {
1118     int count = 0;
1119 
1120     if (devices == NULL) {
1121         crm_err("Parameter error: stonith_api_device_list");
1122         return -EFAULT;
1123     }
1124 
1125     /* Include Heartbeat agents */
1126     if (namespace == NULL || safe_str_eq("heartbeat", namespace)) {
1127 #if HAVE_STONITH_STONITH_H
1128         static gboolean need_init = TRUE;
1129 
1130         char **entry = NULL;
1131         char **type_list = NULL;
1132         static char **(*type_list_fn) (void) = NULL;
1133         static void (*type_free_fn) (char **) = NULL;
1134 
1135         if (need_init) {
1136             need_init = FALSE;
1137             type_list_fn =
1138                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_types", FALSE);
1139             type_free_fn =
1140                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_free_hostlist",
1141                                       FALSE);
1142         }
1143 
1144         if (type_list_fn) {
1145             type_list = (*type_list_fn) ();
1146         }
1147 
1148         for (entry = type_list; entry != NULL && *entry; ++entry) {
1149             crm_trace("Added: %s", *entry);
1150             *devices = stonith_key_value_add(*devices, NULL, *entry);
1151             count++;
1152         }
1153         if (type_list && type_free_fn) {
1154             (*type_free_fn) (type_list);
1155         }
1156 #else
1157         if (namespace != NULL) {
1158             return -EINVAL;     /* Heartbeat agents not supported */
1159         }
1160 #endif
1161     }
1162 
1163     /* Include Red Hat agents, basically: ls -1 @sbin_dir@/fence_* */
1164     if (namespace == NULL || safe_str_eq("redhat", namespace)) {
1165         struct dirent **namelist;
1166         int file_num = scandir(RH_STONITH_DIR, &namelist, 0, alphasort);
1167 
1168         if (file_num > 0) {
1169             struct stat prop;
1170             char buffer[FILENAME_MAX + 1];
1171 
1172             while (file_num--) {
1173                 if ('.' == namelist[file_num]->d_name[0]) {
1174                     free(namelist[file_num]);
1175                     continue;
1176 
1177                 } else if (!crm_starts_with(namelist[file_num]->d_name,
1178                                             RH_STONITH_PREFIX)) {
1179                     free(namelist[file_num]);
1180                     continue;
1181                 }
1182 
1183                 snprintf(buffer, FILENAME_MAX, "%s/%s", RH_STONITH_DIR, namelist[file_num]->d_name);
1184                 if (stat(buffer, &prop) == 0 && S_ISREG(prop.st_mode)) {
1185                     *devices = stonith_key_value_add(*devices, NULL, namelist[file_num]->d_name);
1186                     count++;
1187                 }
1188 
1189                 free(namelist[file_num]);
1190             }
1191             free(namelist);
1192         }
1193     }
1194 
1195     return count;
1196 }
1197 
1198 #if HAVE_STONITH_STONITH_H
1199 static inline char *
1200 strdup_null(const char *val)
     /* [previous][next][first][last][top][bottom][index][help] */
1201 {
1202     if (val) {
1203         return strdup(val);
1204     }
1205     return NULL;
1206 }
1207 
1208 static void
1209 stonith_plugin(int priority, const char *fmt, ...) __attribute__((__format__ (__printf__, 2, 3)));
1210 
1211 static void
1212 stonith_plugin(int priority, const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
1213 {
1214     int err = errno;
1215 
1216     va_list ap;
1217     int len = 0;
1218     char *string = NULL;
1219 
1220     va_start(ap, format);
1221 
1222     len = vasprintf (&string, format, ap);
1223     CRM_ASSERT(len > 0);
1224 
1225     do_crm_log_alias(priority, __FILE__, __func__, __LINE__, "%s", string);
1226 
1227     free(string);
1228     errno = err;
1229 }
1230 #endif
1231 
1232 static int
1233 stonith_api_device_metadata(stonith_t * stonith, int call_options, const char *agent,
     /* [previous][next][first][last][top][bottom][index][help] */
1234                             const char *namespace, char **output, int timeout)
1235 {
1236     int rc = 0;
1237     char *buffer = NULL;
1238     const char *provider = get_stonith_provider(agent, namespace);
1239 
1240     crm_trace("looking up %s/%s metadata", agent, provider);
1241 
1242     /* By having this in a library, we can access it from stonith_admin
1243      *  when neither lrmd or stonith-ng are running
1244      * Important for the crm shell's validations...
1245      */
1246 
1247     if (safe_str_eq(provider, "redhat")) {
1248         stonith_action_t *action = stonith_action_create(agent, "metadata", NULL, 0, 5, NULL, NULL);
1249         int exec_rc = stonith_action_execute(action, &rc, &buffer);
1250         xmlNode *xml = NULL;
1251         xmlNode *actions = NULL;
1252         xmlXPathObject *xpathObj = NULL;
1253 
1254         if (exec_rc < 0 || rc != 0 || buffer == NULL) {
1255             crm_warn("Could not obtain metadata for %s", agent);
1256             crm_debug("Query failed: %d %d: %s", exec_rc, rc, crm_str(buffer));
1257             free(buffer);       /* Just in case */
1258             return -EINVAL;
1259         }
1260 
1261         xml = string2xml(buffer);
1262         if(xml == NULL) {
1263             crm_warn("Metadata for %s is invalid", agent);
1264             free(buffer);
1265             return -EINVAL;
1266         }
1267 
1268         xpathObj = xpath_search(xml, "//actions");
1269         if (numXpathResults(xpathObj) > 0) {
1270             actions = getXpathResult(xpathObj, 0);
1271         }
1272 
1273         freeXpathObject(xpathObj);
1274 
1275         /* Now fudge the metadata so that the start/stop actions appear */
1276         xpathObj = xpath_search(xml, "//action[@name='stop']");
1277         if (numXpathResults(xpathObj) <= 0) {
1278             xmlNode *tmp = NULL;
1279 
1280             tmp = create_xml_node(actions, "action");
1281             crm_xml_add(tmp, "name", "stop");
1282             crm_xml_add(tmp, "timeout", CRM_DEFAULT_OP_TIMEOUT_S);
1283 
1284             tmp = create_xml_node(actions, "action");
1285             crm_xml_add(tmp, "name", "start");
1286             crm_xml_add(tmp, "timeout", CRM_DEFAULT_OP_TIMEOUT_S);
1287         }
1288 
1289         freeXpathObject(xpathObj);
1290 
1291         /* Now fudge the metadata so that the port isn't required in the configuration */
1292         xpathObj = xpath_search(xml, "//parameter[@name='port']");
1293         if (numXpathResults(xpathObj) > 0) {
1294             /* We'll fill this in */
1295             xmlNode *tmp = getXpathResult(xpathObj, 0);
1296 
1297             crm_xml_add(tmp, "required", "0");
1298         }
1299 
1300         freeXpathObject(xpathObj);
1301         free(buffer);
1302         buffer = dump_xml_formatted_with_text(xml);
1303         free_xml(xml);
1304         if (!buffer) {
1305             return -EINVAL;
1306         }
1307 
1308     } else {
1309 #if !HAVE_STONITH_STONITH_H
1310         return -EINVAL;         /* Heartbeat agents not supported */
1311 #else
1312         int bufferlen = 0;
1313         static const char *no_parameter_info = "<!-- no value -->";
1314 
1315         Stonith *stonith_obj = NULL;
1316 
1317         static gboolean need_init = TRUE;
1318         static Stonith *(*st_new_fn) (const char *) = NULL;
1319         static const char *(*st_info_fn) (Stonith *, int) = NULL;
1320         static void (*st_del_fn) (Stonith *) = NULL;
1321         static void (*st_log_fn) (Stonith *, PILLogFun) = NULL;
1322 
1323         if (need_init) {
1324             need_init = FALSE;
1325             st_new_fn =
1326                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_new", FALSE);
1327             st_del_fn =
1328                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_delete",
1329                                       FALSE);
1330             st_log_fn =
1331                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_set_log",
1332                                       FALSE);
1333             st_info_fn =
1334                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_get_info",
1335                                       FALSE);
1336         }
1337 
1338         if (lha_agents_lib && st_new_fn && st_del_fn && st_info_fn && st_log_fn) {
1339             char *xml_meta_longdesc = NULL;
1340             char *xml_meta_shortdesc = NULL;
1341 
1342             char *meta_param = NULL;
1343             char *meta_longdesc = NULL;
1344             char *meta_shortdesc = NULL;
1345 
1346             stonith_obj = (*st_new_fn) (agent);
1347             if (stonith_obj) {
1348                 (*st_log_fn) (stonith_obj, (PILLogFun) & stonith_plugin);
1349                 meta_longdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEDESCR));
1350                 if (meta_longdesc == NULL) {
1351                     crm_warn("no long description in %s's metadata.", agent);
1352                     meta_longdesc = strdup(no_parameter_info);
1353                 }
1354 
1355                 meta_shortdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEID));
1356                 if (meta_shortdesc == NULL) {
1357                     crm_warn("no short description in %s's metadata.", agent);
1358                     meta_shortdesc = strdup(no_parameter_info);
1359                 }
1360 
1361                 meta_param = strdup_null((*st_info_fn) (stonith_obj, ST_CONF_XML));
1362                 if (meta_param == NULL) {
1363                     crm_warn("no list of parameters in %s's metadata.", agent);
1364                     meta_param = strdup(no_parameter_info);
1365                 }
1366                 (*st_del_fn) (stonith_obj);
1367             } else {
1368                 return -EINVAL; /* Heartbeat agents not supported */
1369             }
1370 
1371             xml_meta_longdesc =
1372                 (char *)xmlEncodeEntitiesReentrant(NULL, (const unsigned char *)meta_longdesc);
1373             xml_meta_shortdesc =
1374                 (char *)xmlEncodeEntitiesReentrant(NULL, (const unsigned char *)meta_shortdesc);
1375 
1376             bufferlen = strlen(META_TEMPLATE) + strlen(agent)
1377                 + strlen(xml_meta_longdesc) + strlen(xml_meta_shortdesc)
1378                 + strlen(meta_param) + 1;
1379 
1380             buffer = calloc(1, bufferlen);
1381             snprintf(buffer, bufferlen - 1, META_TEMPLATE,
1382                      agent, xml_meta_longdesc, xml_meta_shortdesc, meta_param);
1383 
1384             xmlFree(xml_meta_longdesc);
1385             xmlFree(xml_meta_shortdesc);
1386 
1387             free(meta_shortdesc);
1388             free(meta_longdesc);
1389             free(meta_param);
1390         }
1391 #endif
1392     }
1393 
1394     if (output) {
1395         *output = buffer;
1396 
1397     } else {
1398         free(buffer);
1399     }
1400 
1401     return rc;
1402 }
1403 
1404 static int
1405 stonith_api_query(stonith_t * stonith, int call_options, const char *target,
     /* [previous][next][first][last][top][bottom][index][help] */
1406                   stonith_key_value_t ** devices, int timeout)
1407 {
1408     int rc = 0, lpc = 0, max = 0;
1409 
1410     xmlNode *data = NULL;
1411     xmlNode *output = NULL;
1412     xmlXPathObjectPtr xpathObj = NULL;
1413 
1414     CRM_CHECK(devices != NULL, return -EINVAL);
1415 
1416     data = create_xml_node(NULL, F_STONITH_DEVICE);
1417     crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
1418     crm_xml_add(data, F_STONITH_TARGET, target);
1419     crm_xml_add(data, F_STONITH_ACTION, "off");
1420     rc = stonith_send_command(stonith, STONITH_OP_QUERY, data, &output, call_options, timeout);
1421 
1422     if (rc < 0) {
1423         return rc;
1424     }
1425 
1426     xpathObj = xpath_search(output, "//@agent");
1427     if (xpathObj) {
1428         max = numXpathResults(xpathObj);
1429 
1430         for (lpc = 0; lpc < max; lpc++) {
1431             xmlNode *match = getXpathResult(xpathObj, lpc);
1432 
1433             CRM_LOG_ASSERT(match != NULL);
1434             if(match != NULL) {
1435                 xmlChar *match_path = xmlGetNodePath(match);
1436 
1437                 crm_info("%s[%d] = %s", "//@agent", lpc, match_path);
1438                 free(match_path);
1439                 *devices = stonith_key_value_add(*devices, NULL, crm_element_value(match, XML_ATTR_ID));
1440             }
1441         }
1442 
1443         freeXpathObject(xpathObj);
1444     }
1445 
1446     free_xml(output);
1447     free_xml(data);
1448     return max;
1449 }
1450 
1451 static int
1452 stonith_api_call(stonith_t * stonith,
     /* [previous][next][first][last][top][bottom][index][help] */
1453                  int call_options,
1454                  const char *id,
1455                  const char *action, const char *victim, int timeout, xmlNode ** output)
1456 {
1457     int rc = 0;
1458     xmlNode *data = NULL;
1459 
1460     data = create_xml_node(NULL, F_STONITH_DEVICE);
1461     crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
1462     crm_xml_add(data, F_STONITH_DEVICE, id);
1463     crm_xml_add(data, F_STONITH_ACTION, action);
1464     crm_xml_add(data, F_STONITH_TARGET, victim);
1465 
1466     rc = stonith_send_command(stonith, STONITH_OP_EXEC, data, output, call_options, timeout);
1467     free_xml(data);
1468 
1469     return rc;
1470 }
1471 
1472 static int
1473 stonith_api_list(stonith_t * stonith, int call_options, const char *id, char **list_info,
     /* [previous][next][first][last][top][bottom][index][help] */
1474                  int timeout)
1475 {
1476     int rc;
1477     xmlNode *output = NULL;
1478 
1479     rc = stonith_api_call(stonith, call_options, id, "list", NULL, timeout, &output);
1480 
1481     if (output && list_info) {
1482         const char *list_str;
1483 
1484         list_str = crm_element_value(output, "st_output");
1485 
1486         if (list_str) {
1487             *list_info = strdup(list_str);
1488         }
1489     }
1490 
1491     if (output) {
1492         free_xml(output);
1493     }
1494 
1495     return rc;
1496 }
1497 
1498 static int
1499 stonith_api_monitor(stonith_t * stonith, int call_options, const char *id, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1500 {
1501     return stonith_api_call(stonith, call_options, id, "monitor", NULL, timeout, NULL);
1502 }
1503 
1504 static int
1505 stonith_api_status(stonith_t * stonith, int call_options, const char *id, const char *port,
     /* [previous][next][first][last][top][bottom][index][help] */
1506                    int timeout)
1507 {
1508     return stonith_api_call(stonith, call_options, id, "status", port, timeout, NULL);
1509 }
1510 
1511 static int
1512 stonith_api_fence(stonith_t * stonith, int call_options, const char *node, const char *action,
     /* [previous][next][first][last][top][bottom][index][help] */
1513                   int timeout, int tolerance)
1514 {
1515     int rc = 0;
1516     xmlNode *data = NULL;
1517 
1518     data = create_xml_node(NULL, __FUNCTION__);
1519     crm_xml_add(data, F_STONITH_TARGET, node);
1520     crm_xml_add(data, F_STONITH_ACTION, action);
1521     crm_xml_add_int(data, F_STONITH_TIMEOUT, timeout);
1522     crm_xml_add_int(data, F_STONITH_TOLERANCE, tolerance);
1523 
1524     rc = stonith_send_command(stonith, STONITH_OP_FENCE, data, NULL, call_options, timeout);
1525     free_xml(data);
1526 
1527     return rc;
1528 }
1529 
1530 static int
1531 stonith_api_confirm(stonith_t * stonith, int call_options, const char *target)
     /* [previous][next][first][last][top][bottom][index][help] */
1532 {
1533     return stonith_api_fence(stonith, call_options | st_opt_manual_ack, target, "off", 0, 0);
1534 }
1535 
1536 static int
1537 stonith_api_history(stonith_t * stonith, int call_options, const char *node,
     /* [previous][next][first][last][top][bottom][index][help] */
1538                     stonith_history_t ** history, int timeout)
1539 {
1540     int rc = 0;
1541     xmlNode *data = NULL;
1542     xmlNode *output = NULL;
1543     stonith_history_t *last = NULL;
1544 
1545     *history = NULL;
1546 
1547     if (node) {
1548         data = create_xml_node(NULL, __FUNCTION__);
1549         crm_xml_add(data, F_STONITH_TARGET, node);
1550     }
1551 
1552     rc = stonith_send_command(stonith, STONITH_OP_FENCE_HISTORY, data, &output,
1553                               call_options | st_opt_sync_call, timeout);
1554     free_xml(data);
1555 
1556     if (rc == 0) {
1557         xmlNode *op = NULL;
1558         xmlNode *reply = get_xpath_object("//" F_STONITH_HISTORY_LIST, output, LOG_ERR);
1559 
1560         for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
1561             stonith_history_t *kvp;
1562 
1563             kvp = calloc(1, sizeof(stonith_history_t));
1564             kvp->target = crm_element_value_copy(op, F_STONITH_TARGET);
1565             kvp->action = crm_element_value_copy(op, F_STONITH_ACTION);
1566             kvp->origin = crm_element_value_copy(op, F_STONITH_ORIGIN);
1567             kvp->delegate = crm_element_value_copy(op, F_STONITH_DELEGATE);
1568             kvp->client = crm_element_value_copy(op, F_STONITH_CLIENTNAME);
1569             crm_element_value_int(op, F_STONITH_DATE, &kvp->completed);
1570             crm_element_value_int(op, F_STONITH_STATE, &kvp->state);
1571 
1572             if (last) {
1573                 last->next = kvp;
1574             } else {
1575                 *history = kvp;
1576             }
1577             last = kvp;
1578         }
1579     }
1580     return rc;
1581 }
1582 
1583 gboolean
1584 is_redhat_agent(const char *agent)
     /* [previous][next][first][last][top][bottom][index][help] */
1585 {
1586     int rc = 0;
1587     struct stat prop;
1588     char buffer[FILENAME_MAX + 1];
1589 
1590     snprintf(buffer, FILENAME_MAX, "%s/%s", RH_STONITH_DIR, agent);
1591     rc = stat(buffer, &prop);
1592     if (rc >= 0 && S_ISREG(prop.st_mode)) {
1593         return TRUE;
1594     }
1595     return FALSE;
1596 }
1597 
1598 const char *
1599 get_stonith_provider(const char *agent, const char *provider)
     /* [previous][next][first][last][top][bottom][index][help] */
1600 {
1601     /* This function sucks */
1602     if (is_redhat_agent(agent)) {
1603         return "redhat";
1604 
1605 #if HAVE_STONITH_STONITH_H
1606     } else {
1607         Stonith *stonith_obj = NULL;
1608 
1609         static gboolean need_init = TRUE;
1610         static Stonith *(*st_new_fn) (const char *) = NULL;
1611         static void (*st_del_fn) (Stonith *) = NULL;
1612 
1613         if (need_init) {
1614             need_init = FALSE;
1615             st_new_fn =
1616                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_new", FALSE);
1617             st_del_fn =
1618                 find_library_function(&lha_agents_lib, LHA_STONITH_LIBRARY, "stonith_delete",
1619                                       FALSE);
1620         }
1621 
1622         if (lha_agents_lib && st_new_fn && st_del_fn) {
1623             stonith_obj = (*st_new_fn) (agent);
1624             if (stonith_obj) {
1625                 (*st_del_fn) (stonith_obj);
1626                 return "heartbeat";
1627             }
1628         }
1629 #endif
1630     }
1631 
1632     if (safe_str_eq(provider, "internal")) {
1633         return provider;
1634 
1635     } else {
1636         crm_err("No such device: %s", agent);
1637         return NULL;
1638     }
1639 }
1640 
1641 static gint
1642 stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
     /* [previous][next][first][last][top][bottom][index][help] */
1643 {
1644     int rc = 0;
1645     const stonith_notify_client_t *a_client = a;
1646     const stonith_notify_client_t *b_client = b;
1647 
1648     CRM_CHECK(a_client->event != NULL && b_client->event != NULL, return 0);
1649     rc = strcmp(a_client->event, b_client->event);
1650     if (rc == 0) {
1651         if (a_client->notify == NULL || b_client->notify == NULL) {
1652             return 0;
1653 
1654         } else if (a_client->notify == b_client->notify) {
1655             return 0;
1656 
1657         } else if (((long)a_client->notify) < ((long)b_client->notify)) {
1658             crm_err("callbacks for %s are not equal: %p vs. %p",
1659                     a_client->event, a_client->notify, b_client->notify);
1660             return -1;
1661         }
1662         crm_err("callbacks for %s are not equal: %p vs. %p",
1663                 a_client->event, a_client->notify, b_client->notify);
1664         return 1;
1665     }
1666     return rc;
1667 }
1668 
1669 xmlNode *
1670 stonith_create_op(int call_id, const char *token, const char *op, xmlNode * data, int call_options)
     /* [previous][next][first][last][top][bottom][index][help] */
1671 {
1672     xmlNode *op_msg = create_xml_node(NULL, "stonith_command");
1673 
1674     CRM_CHECK(op_msg != NULL, return NULL);
1675     CRM_CHECK(token != NULL, return NULL);
1676 
1677     crm_xml_add(op_msg, F_XML_TAGNAME, "stonith_command");
1678 
1679     crm_xml_add(op_msg, F_TYPE, T_STONITH_NG);
1680     crm_xml_add(op_msg, F_STONITH_CALLBACK_TOKEN, token);
1681     crm_xml_add(op_msg, F_STONITH_OPERATION, op);
1682     crm_xml_add_int(op_msg, F_STONITH_CALLID, call_id);
1683     crm_trace("Sending call options: %.8lx, %d", (long)call_options, call_options);
1684     crm_xml_add_int(op_msg, F_STONITH_CALLOPTS, call_options);
1685 
1686     if (data != NULL) {
1687         add_message_xml(op_msg, F_STONITH_CALLDATA, data);
1688     }
1689 
1690     return op_msg;
1691 }
1692 
1693 static void
1694 stonith_destroy_op_callback(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
1695 {
1696     stonith_callback_client_t *blob = data;
1697 
1698     if (blob->timer && blob->timer->ref > 0) {
1699         g_source_remove(blob->timer->ref);
1700     }
1701     free(blob->timer);
1702     free(blob);
1703 }
1704 
1705 static int
1706 stonith_api_signoff(stonith_t * stonith)
     /* [previous][next][first][last][top][bottom][index][help] */
1707 {
1708     stonith_private_t *native = stonith->private;
1709 
1710     crm_debug("Signing out of the STONITH Service");
1711 
1712     if (native->source != NULL) {
1713         /* Attached to mainloop */
1714         mainloop_del_ipc_client(native->source);
1715         native->source = NULL;
1716         native->ipc = NULL;
1717 
1718     } else if (native->ipc) {
1719         /* Not attached to mainloop */
1720         crm_ipc_t *ipc = native->ipc;
1721 
1722         native->ipc = NULL;
1723         crm_ipc_close(ipc);
1724         crm_ipc_destroy(ipc);
1725     }
1726 
1727     free(native->token); native->token = NULL;
1728     stonith->state = stonith_disconnected;
1729     return pcmk_ok;
1730 }
1731 
1732 static int
1733 stonith_api_signon(stonith_t * stonith, const char *name, int *stonith_fd)
     /* [previous][next][first][last][top][bottom][index][help] */
1734 {
1735     int rc = pcmk_ok;
1736     stonith_private_t *native = stonith->private;
1737 
1738     static struct ipc_client_callbacks st_callbacks = {
1739         .dispatch = stonith_dispatch_internal,
1740         .destroy = stonith_connection_destroy
1741     };
1742 
1743     crm_trace("Connecting command channel");
1744 
1745     stonith->state = stonith_connected_command;
1746     if (stonith_fd) {
1747         /* No mainloop */
1748         native->ipc = crm_ipc_new("stonith-ng", 0);
1749 
1750         if (native->ipc && crm_ipc_connect(native->ipc)) {
1751             *stonith_fd = crm_ipc_get_fd(native->ipc);
1752         } else if (native->ipc) {
1753             crm_perror(LOG_ERR, "Connection to STONITH manager failed");
1754             rc = -ENOTCONN;
1755         }
1756 
1757     } else {
1758         /* With mainloop */
1759         native->source =
1760             mainloop_add_ipc_client("stonith-ng", G_PRIORITY_MEDIUM, 0, stonith, &st_callbacks);
1761         native->ipc = mainloop_get_ipc_client(native->source);
1762     }
1763 
1764     if (native->ipc == NULL) {
1765         crm_debug("Could not connect to the Stonith API");
1766         rc = -ENOTCONN;
1767     }
1768 
1769     if (rc == pcmk_ok) {
1770         xmlNode *reply = NULL;
1771         xmlNode *hello = create_xml_node(NULL, "stonith_command");
1772 
1773         crm_xml_add(hello, F_TYPE, T_STONITH_NG);
1774         crm_xml_add(hello, F_STONITH_OPERATION, CRM_OP_REGISTER);
1775         crm_xml_add(hello, F_STONITH_CLIENTNAME, name);
1776         rc = crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1, &reply);
1777 
1778         if (rc < 0) {
1779             crm_perror(LOG_DEBUG, "Couldn't complete registration with the fencing API: %d", rc);
1780             rc = -ECOMM;
1781 
1782         } else if (reply == NULL) {
1783             crm_err("Did not receive registration reply");
1784             rc = -EPROTO;
1785 
1786         } else {
1787             const char *msg_type = crm_element_value(reply, F_STONITH_OPERATION);
1788             const char *tmp_ticket = crm_element_value(reply, F_STONITH_CLIENTID);
1789 
1790             if (safe_str_neq(msg_type, CRM_OP_REGISTER)) {
1791                 crm_err("Invalid registration message: %s", msg_type);
1792                 crm_log_xml_err(reply, "Bad reply");
1793                 rc = -EPROTO;
1794 
1795             } else if (tmp_ticket == NULL) {
1796                 crm_err("No registration token provided");
1797                 crm_log_xml_err(reply, "Bad reply");
1798                 rc = -EPROTO;
1799 
1800             } else {
1801                 crm_trace("Obtained registration token: %s", tmp_ticket);
1802                 native->token = strdup(tmp_ticket);
1803                 rc = pcmk_ok;
1804             }
1805         }
1806 
1807         free_xml(reply);
1808         free_xml(hello);
1809     }
1810 
1811     if (rc == pcmk_ok) {
1812 #if HAVE_MSGFROMIPC_TIMEOUT
1813         stonith->call_timeout = MAX_IPC_DELAY;
1814 #endif
1815         crm_debug("Connection to STONITH successful");
1816         return pcmk_ok;
1817     }
1818 
1819     crm_debug("Connection to STONITH failed: %s", pcmk_strerror(rc));
1820     stonith->cmds->disconnect(stonith);
1821     return rc;
1822 }
1823 
1824 static int
1825 stonith_set_notification(stonith_t * stonith, const char *callback, int enabled)
     /* [previous][next][first][last][top][bottom][index][help] */
1826 {
1827     int rc = pcmk_ok;
1828     xmlNode *notify_msg = create_xml_node(NULL, __FUNCTION__);
1829     stonith_private_t *native = stonith->private;
1830 
1831     if (stonith->state != stonith_disconnected) {
1832 
1833         crm_xml_add(notify_msg, F_STONITH_OPERATION, T_STONITH_NOTIFY);
1834         if (enabled) {
1835             crm_xml_add(notify_msg, F_STONITH_NOTIFY_ACTIVATE, callback);
1836         } else {
1837             crm_xml_add(notify_msg, F_STONITH_NOTIFY_DEACTIVATE, callback);
1838         }
1839 
1840         rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response, -1, NULL);
1841         if (rc < 0) {
1842             crm_perror(LOG_DEBUG, "Couldn't register for fencing notifications: %d", rc);
1843             rc = -ECOMM;
1844         } else {
1845             rc = pcmk_ok;
1846         }
1847     }
1848 
1849     free_xml(notify_msg);
1850     return rc;
1851 }
1852 
1853 static int
1854 stonith_api_add_notification(stonith_t * stonith, const char *event,
     /* [previous][next][first][last][top][bottom][index][help] */
1855                              void (*callback) (stonith_t * stonith, stonith_event_t * e))
1856 {
1857     GList *list_item = NULL;
1858     stonith_notify_client_t *new_client = NULL;
1859     stonith_private_t *private = NULL;
1860 
1861     private = stonith->private;
1862     crm_trace("Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
1863 
1864     new_client = calloc(1, sizeof(stonith_notify_client_t));
1865     new_client->event = event;
1866     new_client->notify = callback;
1867 
1868     list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1869 
1870     if (list_item != NULL) {
1871         crm_warn("Callback already present");
1872         free(new_client);
1873         return -ENOTUNIQ;
1874 
1875     } else {
1876         private->notify_list = g_list_append(private->notify_list, new_client);
1877 
1878         stonith_set_notification(stonith, event, 1);
1879 
1880         crm_trace("Callback added (%d)", g_list_length(private->notify_list));
1881     }
1882     return pcmk_ok;
1883 }
1884 
1885 static int
1886 stonith_api_del_notification(stonith_t * stonith, const char *event)
     /* [previous][next][first][last][top][bottom][index][help] */
1887 {
1888     GList *list_item = NULL;
1889     stonith_notify_client_t *new_client = NULL;
1890     stonith_private_t *private = NULL;
1891 
1892     crm_debug("Removing callback for %s events", event);
1893 
1894     private = stonith->private;
1895     new_client = calloc(1, sizeof(stonith_notify_client_t));
1896     new_client->event = event;
1897     new_client->notify = NULL;
1898 
1899     list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1900 
1901     stonith_set_notification(stonith, event, 0);
1902 
1903     if (list_item != NULL) {
1904         stonith_notify_client_t *list_client = list_item->data;
1905 
1906         private->notify_list = g_list_remove(private->notify_list, list_client);
1907         free(list_client);
1908 
1909         crm_trace("Removed callback");
1910 
1911     } else {
1912         crm_trace("Callback not present");
1913     }
1914     free(new_client);
1915     return pcmk_ok;
1916 }
1917 
1918 static gboolean
1919 stonith_async_timeout_handler(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
1920 {
1921     struct timer_rec_s *timer = data;
1922 
1923     crm_err("Async call %d timed out after %dms", timer->call_id, timer->timeout);
1924     stonith_perform_callback(timer->stonith, NULL, timer->call_id, -ETIME);
1925 
1926     /* Always return TRUE, never remove the handler
1927      * We do that in stonith_del_callback()
1928      */
1929     return TRUE;
1930 }
1931 
1932 static void
1933 set_callback_timeout(stonith_callback_client_t * callback, stonith_t * stonith, int call_id,
     /* [previous][next][first][last][top][bottom][index][help] */
1934                      int timeout)
1935 {
1936     struct timer_rec_s *async_timer = callback->timer;
1937 
1938     if (timeout <= 0) {
1939         return;
1940     }
1941 
1942     if (!async_timer) {
1943         async_timer = calloc(1, sizeof(struct timer_rec_s));
1944         callback->timer = async_timer;
1945     }
1946 
1947     async_timer->stonith = stonith;
1948     async_timer->call_id = call_id;
1949     /* Allow a fair bit of grace to allow the server to tell us of a timeout
1950      * This is only a fallback
1951      */
1952     async_timer->timeout = (timeout + 60) * 1000;
1953     if (async_timer->ref) {
1954         g_source_remove(async_timer->ref);
1955     }
1956     async_timer->ref =
1957         g_timeout_add(async_timer->timeout, stonith_async_timeout_handler, async_timer);
1958 }
1959 
1960 static void
1961 update_callback_timeout(int call_id, int timeout, stonith_t * st)
     /* [previous][next][first][last][top][bottom][index][help] */
1962 {
1963     stonith_callback_client_t *callback = NULL;
1964     stonith_private_t *private = st->private;
1965 
1966     callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1967     if (!callback || !callback->allow_timeout_updates) {
1968         return;
1969     }
1970 
1971     set_callback_timeout(callback, st, call_id, timeout);
1972 }
1973 
1974 static void
1975 invoke_callback(stonith_t * st, int call_id, int rc, void *userdata,
     /* [previous][next][first][last][top][bottom][index][help] */
1976                 void (*callback) (stonith_t * st, stonith_callback_data_t * data))
1977 {
1978     stonith_callback_data_t data = { 0, };
1979 
1980     data.call_id = call_id;
1981     data.rc = rc;
1982     data.userdata = userdata;
1983 
1984     callback(st, &data);
1985 }
1986 
1987 static int
1988 stonith_api_add_callback(stonith_t * stonith, int call_id, int timeout, int options,
     /* [previous][next][first][last][top][bottom][index][help] */
1989                          void *user_data, const char *callback_name,
1990                          void (*callback) (stonith_t * st, stonith_callback_data_t * data))
1991 {
1992     stonith_callback_client_t *blob = NULL;
1993     stonith_private_t *private = NULL;
1994 
1995     CRM_CHECK(stonith != NULL, return -EINVAL);
1996     CRM_CHECK(stonith->private != NULL, return -EINVAL);
1997     private = stonith->private;
1998 
1999     if (call_id == 0) {
2000         private->op_callback = callback;
2001 
2002     } else if (call_id < 0) {
2003         if (!(options & st_opt_report_only_success)) {
2004             crm_trace("Call failed, calling %s: %s", callback_name, pcmk_strerror(call_id));
2005             invoke_callback(stonith, call_id, call_id, user_data, callback);
2006         } else {
2007             crm_warn("STONITH call failed: %s", pcmk_strerror(call_id));
2008         }
2009         return FALSE;
2010     }
2011 
2012     blob = calloc(1, sizeof(stonith_callback_client_t));
2013     blob->id = callback_name;
2014     blob->only_success = (options & st_opt_report_only_success) ? TRUE : FALSE;
2015     blob->user_data = user_data;
2016     blob->callback = callback;
2017     blob->allow_timeout_updates = (options & st_opt_timeout_updates) ? TRUE : FALSE;
2018 
2019     if (timeout > 0) {
2020         set_callback_timeout(blob, stonith, call_id, timeout);
2021     }
2022 
2023     g_hash_table_insert(private->stonith_op_callback_table, GINT_TO_POINTER(call_id), blob);
2024     crm_trace("Added callback to %s for call %d", callback_name, call_id);
2025 
2026     return TRUE;
2027 }
2028 
2029 static int
2030 stonith_api_del_callback(stonith_t * stonith, int call_id, bool all_callbacks)
     /* [previous][next][first][last][top][bottom][index][help] */
2031 {
2032     stonith_private_t *private = stonith->private;
2033 
2034     if (all_callbacks) {
2035         private->op_callback = NULL;
2036         g_hash_table_destroy(private->stonith_op_callback_table);
2037         private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2038                                                                    NULL,
2039                                                                    stonith_destroy_op_callback);
2040 
2041     } else if (call_id == 0) {
2042         private->op_callback = NULL;
2043 
2044     } else {
2045         g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2046     }
2047     return pcmk_ok;
2048 }
2049 
2050 static void
2051 stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2052 {
2053     int call = GPOINTER_TO_INT(key);
2054     stonith_callback_client_t *blob = value;
2055 
2056     crm_debug("Call %d (%s): pending", call, crm_str(blob->id));
2057 }
2058 
2059 void
2060 stonith_dump_pending_callbacks(stonith_t * stonith)
     /* [previous][next][first][last][top][bottom][index][help] */
2061 {
2062     stonith_private_t *private = stonith->private;
2063 
2064     if (private->stonith_op_callback_table == NULL) {
2065         return;
2066     }
2067     return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
2068 }
2069 
2070 void
2071 stonith_perform_callback(stonith_t * stonith, xmlNode * msg, int call_id, int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
2072 {
2073     stonith_private_t *private = NULL;
2074     stonith_callback_client_t *blob = NULL;
2075     stonith_callback_client_t local_blob;
2076 
2077     CRM_CHECK(stonith != NULL, return);
2078     CRM_CHECK(stonith->private != NULL, return);
2079 
2080     private = stonith->private;
2081 
2082     local_blob.id = NULL;
2083     local_blob.callback = NULL;
2084     local_blob.user_data = NULL;
2085     local_blob.only_success = FALSE;
2086 
2087     if (msg != NULL) {
2088         crm_element_value_int(msg, F_STONITH_RC, &rc);
2089         crm_element_value_int(msg, F_STONITH_CALLID, &call_id);
2090     }
2091 
2092     CRM_CHECK(call_id > 0, crm_log_xml_err(msg, "Bad result"));
2093 
2094     blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2095 
2096     if (blob != NULL) {
2097         local_blob = *blob;
2098         blob = NULL;
2099 
2100         stonith_api_del_callback(stonith, call_id, FALSE);
2101 
2102     } else {
2103         crm_trace("No callback found for call %d", call_id);
2104         local_blob.callback = NULL;
2105     }
2106 
2107     if (local_blob.callback != NULL && (rc == pcmk_ok || local_blob.only_success == FALSE)) {
2108         crm_trace("Invoking callback %s for call %d", crm_str(local_blob.id), call_id);
2109         invoke_callback(stonith, call_id, rc, local_blob.user_data, local_blob.callback);
2110 
2111     } else if (private->op_callback == NULL && rc != pcmk_ok) {
2112         crm_warn("STONITH command failed: %s", pcmk_strerror(rc));
2113         crm_log_xml_debug(msg, "Failed STONITH Update");
2114     }
2115 
2116     if (private->op_callback != NULL) {
2117         crm_trace("Invoking global callback for call %d", call_id);
2118         invoke_callback(stonith, call_id, rc, NULL, private->op_callback);
2119     }
2120     crm_trace("OP callback activated.");
2121 }
2122 
2123 /*
2124  <notify t="st_notify" subt="st_device_register" st_op="st_device_register" st_rc="0" >
2125    <st_calldata >
2126      <stonith_command t="stonith-ng" st_async_id="088fb640-431a-48b9-b2fc-c4ff78d0a2d9" st_op="st_device_register" st_callid="2" st_callopt="4096" st_timeout="0" st_clientid="088fb640-431a-48b9-b2fc-c4ff78d0a2d9" st_clientname="stonith-test" >
2127        <st_calldata >
2128          <st_device_id id="test-id" origin="create_device_registration_xml" agent="fence_virsh" namespace="stonith-ng" >
2129            <attributes ipaddr="localhost" pcmk-portmal="some-host=pcmk-1 pcmk-3=3,4" login="root" identity_file="/root/.ssh/id_dsa" />
2130          </st_device_id>
2131        </st_calldata>
2132      </stonith_command>
2133    </st_calldata>
2134  </notify>
2135 
2136  <notify t="st_notify" subt="st_notify_fence" st_op="st_notify_fence" st_rc="0" >
2137    <st_calldata >
2138      <st_notify_fence st_rc="0" st_target="some-host" st_op="st_fence" st_delegate="test-id" st_origin="61dd7759-e229-4be7-b1f8-ef49dd14d9f0" />
2139    </st_calldata>
2140  </notify>
2141 */
2142 static stonith_event_t *
2143 xml_to_event(xmlNode * msg)
     /* [previous][next][first][last][top][bottom][index][help] */
2144 {
2145     stonith_event_t *event = calloc(1, sizeof(stonith_event_t));
2146     const char *ntype = crm_element_value(msg, F_SUBTYPE);
2147     char *data_addr = crm_strdup_printf("//%s", ntype);
2148     xmlNode *data = get_xpath_object(data_addr, msg, LOG_DEBUG);
2149 
2150     crm_log_xml_trace(msg, "stonith_notify");
2151 
2152     crm_element_value_int(msg, F_STONITH_RC, &(event->result));
2153 
2154     if (safe_str_eq(ntype, T_STONITH_NOTIFY_FENCE)) {
2155         event->operation = crm_element_value_copy(msg, F_STONITH_OPERATION);
2156 
2157         if (data) {
2158             event->origin = crm_element_value_copy(data, F_STONITH_ORIGIN);
2159             event->action = crm_element_value_copy(data, F_STONITH_ACTION);
2160             event->target = crm_element_value_copy(data, F_STONITH_TARGET);
2161             event->executioner = crm_element_value_copy(data, F_STONITH_DELEGATE);
2162             event->id = crm_element_value_copy(data, F_STONITH_REMOTE_OP_ID);
2163             event->client_origin = crm_element_value_copy(data, F_STONITH_CLIENTNAME);
2164             event->device = crm_element_value_copy(data, F_STONITH_DEVICE);
2165 
2166         } else {
2167             crm_err("No data for %s event", ntype);
2168             crm_log_xml_notice(msg, "BadEvent");
2169         }
2170     }
2171 
2172     free(data_addr);
2173     return event;
2174 }
2175 
2176 static void
2177 event_free(stonith_event_t * event)
     /* [previous][next][first][last][top][bottom][index][help] */
2178 {
2179     free(event->id);
2180     free(event->type);
2181     free(event->message);
2182     free(event->operation);
2183     free(event->origin);
2184     free(event->action);
2185     free(event->target);
2186     free(event->executioner);
2187     free(event->device);
2188     free(event->client_origin);
2189     free(event);
2190 }
2191 
2192 static void
2193 stonith_send_notification(gpointer data, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
2194 {
2195     struct notify_blob_s *blob = user_data;
2196     stonith_notify_client_t *entry = data;
2197     stonith_event_t *st_event = NULL;
2198     const char *event = NULL;
2199 
2200     if (blob->xml == NULL) {
2201         crm_warn("Skipping callback - NULL message");
2202         return;
2203     }
2204 
2205     event = crm_element_value(blob->xml, F_SUBTYPE);
2206 
2207     if (entry == NULL) {
2208         crm_warn("Skipping callback - NULL callback client");
2209         return;
2210 
2211     } else if (entry->notify == NULL) {
2212         crm_warn("Skipping callback - NULL callback");
2213         return;
2214 
2215     } else if (safe_str_neq(entry->event, event)) {
2216         crm_trace("Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
2217         return;
2218     }
2219 
2220     st_event = xml_to_event(blob->xml);
2221 
2222     crm_trace("Invoking callback for %p/%s event...", entry, event);
2223     entry->notify(blob->stonith, st_event);
2224     crm_trace("Callback invoked...");
2225 
2226     event_free(st_event);
2227 }
2228 
2229 int
2230 stonith_send_command(stonith_t * stonith, const char *op, xmlNode * data, xmlNode ** output_data,
     /* [previous][next][first][last][top][bottom][index][help] */
2231                      int call_options, int timeout)
2232 {
2233     int rc = 0;
2234     int reply_id = -1;
2235     enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
2236 
2237     xmlNode *op_msg = NULL;
2238     xmlNode *op_reply = NULL;
2239 
2240     stonith_private_t *native = stonith->private;
2241 
2242     if (stonith->state == stonith_disconnected) {
2243         return -ENOTCONN;
2244     }
2245 
2246     if (output_data != NULL) {
2247         *output_data = NULL;
2248     }
2249 
2250     if (op == NULL) {
2251         crm_err("No operation specified");
2252         return -EINVAL;
2253     }
2254 
2255     if (call_options & st_opt_sync_call) {
2256         ipc_flags |= crm_ipc_client_response;
2257     }
2258 
2259     stonith->call_id++;
2260     /* prevent call_id from being negative (or zero) and conflicting
2261      *    with the stonith_errors enum
2262      * use 2 because we use it as (stonith->call_id - 1) below
2263      */
2264     if (stonith->call_id < 1) {
2265         stonith->call_id = 1;
2266     }
2267 
2268     CRM_CHECK(native->token != NULL,;
2269         );
2270     op_msg = stonith_create_op(stonith->call_id, native->token, op, data, call_options);
2271     if (op_msg == NULL) {
2272         return -EINVAL;
2273     }
2274 
2275     crm_xml_add_int(op_msg, F_STONITH_TIMEOUT, timeout);
2276     crm_trace("Sending %s message to STONITH service, Timeout: %ds", op, timeout);
2277 
2278     rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, 1000 * (timeout + 60), &op_reply);
2279     free_xml(op_msg);
2280 
2281     if (rc < 0) {
2282         crm_perror(LOG_ERR, "Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
2283         rc = -ECOMM;
2284         goto done;
2285     }
2286 
2287     crm_log_xml_trace(op_reply, "Reply");
2288 
2289     if (!(call_options & st_opt_sync_call)) {
2290         crm_trace("Async call %d, returning", stonith->call_id);
2291         CRM_CHECK(stonith->call_id != 0, return -EPROTO);
2292         free_xml(op_reply);
2293 
2294         return stonith->call_id;
2295     }
2296 
2297     rc = pcmk_ok;
2298     crm_element_value_int(op_reply, F_STONITH_CALLID, &reply_id);
2299 
2300     if (reply_id == stonith->call_id) {
2301         crm_trace("Synchronous reply %d received", reply_id);
2302 
2303         if (crm_element_value_int(op_reply, F_STONITH_RC, &rc) != 0) {
2304             rc = -ENOMSG;
2305         }
2306 
2307         if ((call_options & st_opt_discard_reply) || output_data == NULL) {
2308             crm_trace("Discarding reply");
2309 
2310         } else {
2311             *output_data = op_reply;
2312             op_reply = NULL;    /* Prevent subsequent free */
2313         }
2314 
2315     } else if (reply_id <= 0) {
2316         crm_err("Received bad reply: No id set");
2317         crm_log_xml_err(op_reply, "Bad reply");
2318         free_xml(op_reply);
2319         rc = -ENOMSG;
2320 
2321     } else {
2322         crm_err("Received bad reply: %d (wanted %d)", reply_id, stonith->call_id);
2323         crm_log_xml_err(op_reply, "Old reply");
2324         free_xml(op_reply);
2325         rc = -ENOMSG;
2326     }
2327 
2328   done:
2329     if (crm_ipc_connected(native->ipc) == FALSE) {
2330         crm_err("STONITH disconnected");
2331         stonith->state = stonith_disconnected;
2332     }
2333 
2334     free_xml(op_reply);
2335     return rc;
2336 }
2337 
2338 /* Not used with mainloop */
2339 bool
2340 stonith_dispatch(stonith_t * st)
     /* [previous][next][first][last][top][bottom][index][help] */
2341 {
2342     gboolean stay_connected = TRUE;
2343     stonith_private_t *private = NULL;
2344 
2345     CRM_ASSERT(st != NULL);
2346     private = st->private;
2347 
2348     while (crm_ipc_ready(private->ipc)) {
2349 
2350         if (crm_ipc_read(private->ipc) > 0) {
2351             const char *msg = crm_ipc_buffer(private->ipc);
2352 
2353             stonith_dispatch_internal(msg, strlen(msg), st);
2354         }
2355 
2356         if (crm_ipc_connected(private->ipc) == FALSE) {
2357             crm_err("Connection closed");
2358             stay_connected = FALSE;
2359         }
2360     }
2361 
2362     return stay_connected;
2363 }
2364 
2365 int
2366 stonith_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata)
     /* [previous][next][first][last][top][bottom][index][help] */
2367 {
2368     const char *type = NULL;
2369     struct notify_blob_s blob;
2370 
2371     stonith_t *st = userdata;
2372     stonith_private_t *private = NULL;
2373 
2374     CRM_ASSERT(st != NULL);
2375     private = st->private;
2376 
2377     blob.stonith = st;
2378     blob.xml = string2xml(buffer);
2379     if (blob.xml == NULL) {
2380         crm_warn("Received a NULL msg from STONITH service: %s.", buffer);
2381         return 0;
2382     }
2383 
2384     /* do callbacks */
2385     type = crm_element_value(blob.xml, F_TYPE);
2386     crm_trace("Activating %s callbacks...", type);
2387 
2388     if (safe_str_eq(type, T_STONITH_NG)) {
2389         stonith_perform_callback(st, blob.xml, 0, 0);
2390 
2391     } else if (safe_str_eq(type, T_STONITH_NOTIFY)) {
2392         g_list_foreach(private->notify_list, stonith_send_notification, &blob);
2393     } else if (safe_str_eq(type, T_STONITH_TIMEOUT_VALUE)) {
2394         int call_id = 0;
2395         int timeout = 0;
2396 
2397         crm_element_value_int(blob.xml, F_STONITH_TIMEOUT, &timeout);
2398         crm_element_value_int(blob.xml, F_STONITH_CALLID, &call_id);
2399 
2400         update_callback_timeout(call_id, timeout, st);
2401     } else {
2402         crm_err("Unknown message type: %s", type);
2403         crm_log_xml_warn(blob.xml, "BadReply");
2404     }
2405 
2406     free_xml(blob.xml);
2407     return 1;
2408 }
2409 
2410 static int
2411 stonith_api_free(stonith_t * stonith)
     /* [previous][next][first][last][top][bottom][index][help] */
2412 {
2413     int rc = pcmk_ok;
2414 
2415     crm_trace("Destroying %p", stonith);
2416 
2417     if (stonith->state != stonith_disconnected) {
2418         crm_trace("Disconnecting %p first", stonith);
2419         rc = stonith->cmds->disconnect(stonith);
2420     }
2421 
2422     if (stonith->state == stonith_disconnected) {
2423         stonith_private_t *private = stonith->private;
2424 
2425         crm_trace("Removing %d callbacks", g_hash_table_size(private->stonith_op_callback_table));
2426         g_hash_table_destroy(private->stonith_op_callback_table);
2427 
2428         crm_trace("Destroying %d notification clients", g_list_length(private->notify_list));
2429         g_list_free_full(private->notify_list, free);
2430 
2431         free(stonith->private);
2432         free(stonith->cmds);
2433         free(stonith);
2434 
2435     } else {
2436         crm_err("Not free'ing active connection: %s (%d)", pcmk_strerror(rc), rc);
2437     }
2438 
2439     return rc;
2440 }
2441 
2442 void
2443 stonith_api_delete(stonith_t * stonith)
     /* [previous][next][first][last][top][bottom][index][help] */
2444 {
2445     crm_trace("Destroying %p", stonith);
2446     if(stonith) {
2447         stonith->cmds->free(stonith);
2448     }
2449 }
2450 
2451 stonith_t *
2452 stonith_api_new(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2453 {
2454     stonith_t *new_stonith = NULL;
2455     stonith_private_t *private = NULL;
2456 
2457     new_stonith = calloc(1, sizeof(stonith_t));
2458     private = calloc(1, sizeof(stonith_private_t));
2459     new_stonith->private = private;
2460 
2461     private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2462                                                                NULL, stonith_destroy_op_callback);
2463     private->notify_list = NULL;
2464 
2465     new_stonith->call_id = 1;
2466     new_stonith->state = stonith_disconnected;
2467 
2468     new_stonith->cmds = calloc(1, sizeof(stonith_api_operations_t));
2469 
2470 /* *INDENT-OFF* */
2471     new_stonith->cmds->free       = stonith_api_free;
2472     new_stonith->cmds->connect    = stonith_api_signon;
2473     new_stonith->cmds->disconnect = stonith_api_signoff;
2474 
2475     new_stonith->cmds->list       = stonith_api_list;
2476     new_stonith->cmds->monitor    = stonith_api_monitor;
2477     new_stonith->cmds->status     = stonith_api_status;
2478     new_stonith->cmds->fence      = stonith_api_fence;
2479     new_stonith->cmds->confirm    = stonith_api_confirm;
2480     new_stonith->cmds->history    = stonith_api_history;
2481 
2482     new_stonith->cmds->list_agents  = stonith_api_device_list;
2483     new_stonith->cmds->metadata     = stonith_api_device_metadata;
2484 
2485     new_stonith->cmds->query           = stonith_api_query;
2486     new_stonith->cmds->remove_device   = stonith_api_remove_device;
2487     new_stonith->cmds->register_device = stonith_api_register_device;
2488 
2489     new_stonith->cmds->remove_level          = stonith_api_remove_level;
2490     new_stonith->cmds->remove_level_full     = stonith_api_remove_level_full;
2491     new_stonith->cmds->register_level        = stonith_api_register_level;
2492     new_stonith->cmds->register_level_full   = stonith_api_register_level_full;
2493 
2494     new_stonith->cmds->remove_callback       = stonith_api_del_callback;
2495     new_stonith->cmds->register_callback     = stonith_api_add_callback;
2496     new_stonith->cmds->remove_notification   = stonith_api_del_notification;
2497     new_stonith->cmds->register_notification = stonith_api_add_notification;
2498 /* *INDENT-ON* */
2499 
2500     return new_stonith;
2501 }
2502 
2503 stonith_key_value_t *
2504 stonith_key_value_add(stonith_key_value_t * head, const char *key, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
2505 {
2506     stonith_key_value_t *p, *end;
2507 
2508     p = calloc(1, sizeof(stonith_key_value_t));
2509     if (key) {
2510         p->key = strdup(key);
2511     }
2512     if (value) {
2513         p->value = strdup(value);
2514     }
2515 
2516     end = head;
2517     while (end && end->next) {
2518         end = end->next;
2519     }
2520 
2521     if (end) {
2522         end->next = p;
2523     } else {
2524         head = p;
2525     }
2526 
2527     return head;
2528 }
2529 
2530 void
2531 stonith_key_value_freeall(stonith_key_value_t * head, int keys, int values)
     /* [previous][next][first][last][top][bottom][index][help] */
2532 {
2533     stonith_key_value_t *p;
2534 
2535     while (head) {
2536         p = head->next;
2537         if (keys) {
2538             free(head->key);
2539         }
2540         if (values) {
2541             free(head->value);
2542         }
2543         free(head);
2544         head = p;
2545     }
2546 }
2547 
2548 #define api_log_open() openlog("stonith-api", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON)
2549 #define api_log(level, fmt, args...) syslog(level, "%s: "fmt, __FUNCTION__, args)
2550 
2551 int
2552 stonith_api_kick(uint32_t nodeid, const char *uname, int timeout, bool off)
     /* [previous][next][first][last][top][bottom][index][help] */
2553 {
2554     char *name = NULL;
2555     const char *action = "reboot";
2556 
2557     int rc = -EPROTO;
2558     stonith_t *st = NULL;
2559     enum stonith_call_options opts = st_opt_sync_call | st_opt_allow_suicide;
2560 
2561     api_log_open();
2562     st = stonith_api_new();
2563     if (st) {
2564         rc = st->cmds->connect(st, "stonith-api", NULL);
2565         if(rc != pcmk_ok) {
2566             api_log(LOG_ERR, "Connection failed, could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname, pcmk_strerror(rc), rc);
2567         }
2568     }
2569 
2570     if (uname != NULL) {
2571         name = strdup(uname);
2572 
2573     } else if (nodeid > 0) {
2574         opts |= st_opt_cs_nodeid;
2575         name = crm_itoa(nodeid);
2576     }
2577 
2578     if (off) {
2579         action = "off";
2580     }
2581 
2582     if (rc == pcmk_ok) {
2583         rc = st->cmds->fence(st, opts, name, action, timeout, 0);
2584         if(rc != pcmk_ok) {
2585             api_log(LOG_ERR, "Could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname, pcmk_strerror(rc), rc);
2586         } else {
2587             api_log(LOG_NOTICE, "Node %u/%s kicked: %s ", nodeid, uname, action);
2588         }
2589     }
2590 
2591     if (st) {
2592         st->cmds->disconnect(st);
2593         stonith_api_delete(st);
2594     }
2595 
2596     free(name);
2597     return rc;
2598 }
2599 
2600 time_t
2601 stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
     /* [previous][next][first][last][top][bottom][index][help] */
2602 {
2603     int rc = 0;
2604     char *name = NULL;
2605 
2606     time_t when = 0;
2607     stonith_t *st = NULL;
2608     stonith_history_t *history, *hp = NULL;
2609     enum stonith_call_options opts = st_opt_sync_call;
2610 
2611     st = stonith_api_new();
2612     if (st) {
2613         rc = st->cmds->connect(st, "stonith-api", NULL);
2614         if(rc != pcmk_ok) {
2615             api_log(LOG_NOTICE, "Connection failed: %s (%d)", pcmk_strerror(rc), rc);
2616         }
2617     }
2618 
2619     if (uname != NULL) {
2620         name = strdup(uname);
2621 
2622     } else if (nodeid > 0) {
2623         opts |= st_opt_cs_nodeid;
2624         name = crm_itoa(nodeid);
2625     }
2626 
2627     if (st && rc == pcmk_ok) {
2628         int entries = 0;
2629         int progress = 0;
2630         int completed = 0;
2631 
2632         rc = st->cmds->history(st, opts, name, &history, 120);
2633 
2634         for (hp = history; hp; hp = hp->next) {
2635             entries++;
2636             if (in_progress) {
2637                 progress++;
2638                 if (hp->state != st_done && hp->state != st_failed) {
2639                     when = time(NULL);
2640                 }
2641 
2642             } else if (hp->state == st_done) {
2643                 completed++;
2644                 if (hp->completed > when) {
2645                     when = hp->completed;
2646                 }
2647             }
2648         }
2649 
2650         if(rc == pcmk_ok) {
2651             api_log(LOG_INFO, "Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
2652         } else {
2653             api_log(LOG_ERR, "Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname, pcmk_strerror(rc), rc);
2654         }
2655     }
2656 
2657     if (st) {
2658         st->cmds->disconnect(st);
2659         stonith_api_delete(st);
2660     }
2661 
2662     if(when) {
2663         api_log(LOG_INFO, "Node %u/%s last kicked at: %ld", nodeid, uname, (long int)when);
2664     }
2665     free(name);
2666     return when;
2667 }
2668 
2669 #if HAVE_STONITH_STONITH_H
2670 #  include <pils/plugin.h>
2671 
2672 const char *i_hate_pils(int rc);
2673 
2674 const char *
2675 i_hate_pils(int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
2676 {
2677     return PIL_strerror(rc);
2678 }
2679 #endif

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