28 #include <sys/types.h>
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;
51 struct stonith_action_s {
60 void (*done_cb) (GPid
pid, gint status,
const char *output, gpointer user_data);
65 int last_timeout_signo;
68 time_t initial_start_time;
70 int remaining_timeout;
82 typedef struct stonith_private_s {
86 GHashTable *stonith_op_callback_table;
93 typedef struct stonith_notify_client_s {
101 typedef struct stonith_callback_client_s {
105 gboolean only_success;
106 gboolean allow_timeout_updates;
111 struct notify_blob_s {
123 typedef int (*
stonith_op_t) (
const char *, int,
const char *, xmlNode *,
124 xmlNode *, xmlNode *, xmlNode **, xmlNode **);
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"
135 " <shortdesc lang=\"en\">%s</shortdesc>\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"
144 " <special tag=\"heartbeat\">\n"
145 " <version>2.0</version>\n" " </special>\n" "</resource-agent>\n";
154 xmlNode ** output_data,
int call_options,
int timeout);
156 static void stonith_connection_destroy(gpointer user_data);
157 static void stonith_send_notification(gpointer
data, gpointer user_data);
164 if (action->output) {
182 stonith_connection_destroy(gpointer user_data)
186 struct notify_blob_s blob;
188 crm_trace(
"Sending destroyed notification");
189 blob.stonith = stonith;
194 native->source = NULL;
200 g_list_foreach(native->notify_list, stonith_send_notification, &blob);
211 #if HAVE_STONITH_STONITH_H
214 hash2field((gpointer)
"plugin", (gpointer) agent, args);
215 agent =
"fence_legacy";
227 for (; params; params = params->
next) {
235 stonith_api_register_device(
stonith_t * st,
int call_options,
236 const char *
id,
const char *
namespace,
const char *agent,
240 xmlNode *
data = NULL;
251 stonith_api_remove_device(
stonith_t * st,
int call_options,
const char *name)
254 xmlNode *
data = NULL;
266 stonith_api_remove_level_full(
stonith_t *st,
int options,
267 const char *node,
const char *pattern,
268 const char *attr,
const char *value,
int level)
271 xmlNode *
data = NULL;
273 CRM_CHECK(node || pattern || (attr && value),
return -EINVAL);
281 }
else if (pattern) {
297 stonith_api_remove_level(
stonith_t * st,
int options,
const char *node,
int level)
299 return stonith_api_remove_level_full(st, options, node,
300 NULL, NULL, NULL, level);
320 const char *attr,
const char *value,
327 CRM_CHECK(node || pattern || (attr && value),
return NULL);
339 }
else if (pattern) {
347 for (; device_list; device_list = device_list->
next) {
349 int adding = strlen(device_list->
value);
354 crm_trace(
"Adding %s (%dc) at offset %d", device_list->
value, adding, len);
355 list = realloc_safe(list, len + adding + 1);
357 crm_perror(LOG_CRIT,
"Could not create device list");
361 sprintf(list + len,
"%s%s", len?
",":
"", device_list->
value);
372 stonith_api_register_level_full(
stonith_t * st,
int options,
const char *node,
374 const char *attr,
const char *value,
389 stonith_api_register_level(
stonith_t * st,
int options,
const char *node,
int level,
392 return stonith_api_register_level_full(st, options, node, NULL, NULL, NULL,
397 append_arg(
const char *key,
const char *value,
char **args)
405 if (strstr(key,
"pcmk_")) {
414 len += strlen(value);
416 last = strlen(*args);
419 *args = realloc_safe(*args, last + len);
420 crm_trace(
"Appending: %s=%s", key, value);
421 sprintf((*args) + last,
"%s=%s\n", key, value);
425 append_config_arg(gpointer key, gpointer value, gpointer user_data)
432 append_arg(key, value, user_data);
438 append_host_specific_args(
const char *victim,
const char *map, GHashTable * params,
char **arg_list)
441 int last = 0, lpc = 0, max = 0;
445 crm_debug(
"Using default arg map: port=uname");
446 append_arg(
"port", victim, arg_list);
451 crm_debug(
"Processing arg map: %s", map);
452 for (; lpc < max + 1; lpc++) {
453 if (isalpha(map[lpc])) {
456 }
else if (map[lpc] ==
'=' || map[lpc] ==
':') {
458 name = calloc(1, 1 + lpc - last);
459 memcpy(name, map + last, lpc - last);
463 }
else if (map[lpc] == 0 || map[lpc] ==
',' || isspace(map[lpc])) {
465 const char *value = NULL;
467 param = calloc(1, 1 + lpc - last);
468 memcpy(param, map + last, lpc - last);
473 crm_err(
"Misparsed '%s', found '%s' without a name", map, param);
483 value = g_hash_table_lookup(params, key);
488 crm_debug(
"Setting '%s'='%s' (%s) for %s", name, value, param, victim);
489 append_arg(name, value, arg_list);
492 crm_err(
"No node attribute '%s' for '%s'", name, victim);
502 }
else if (isspace(map[lpc])) {
510 make_args(
const char *agent,
const char *action,
const char *victim,
uint32_t victim_nodeid, GHashTable * device_args,
511 GHashTable * port_map)
514 char *arg_list = NULL;
515 const char *value = NULL;
519 snprintf(buffer,
sizeof(buffer),
"pcmk_%s_action", action);
521 value = g_hash_table_lookup(device_args, buffer);
524 if (value == NULL && device_args) {
526 snprintf(buffer,
sizeof(buffer),
"pcmk_%s_cmd", action);
527 value = g_hash_table_lookup(device_args, buffer);
530 if (value == NULL && device_args &&
safe_str_eq(action,
"off")) {
532 value = g_hash_table_lookup(device_args,
"pcmk_poweroff_action");
536 crm_info(
"Substituting action '%s' for requested operation '%s'", value, action);
541 if (victim && device_args) {
542 const char *alias = victim;
545 if (port_map && g_hash_table_lookup(port_map, victim)) {
546 alias = g_hash_table_lookup(port_map, victim);
552 append_arg(
"nodename", victim, &arg_list);
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);
566 }
else if (param == NULL) {
573 value = g_hash_table_lookup(device_args, param);
576 append_host_specific_args(alias, map, device_args, &arg_list);
584 value = g_hash_table_lookup(device_args, 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,
591 append_arg(param, alias, &arg_list);
596 g_hash_table_foreach(device_args, append_config_arg, &arg_list);
603 st_child_term(gpointer data)
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);
613 crm_perror(LOG_ERR,
"Couldn't send SIGTERM to %d", track->pid);
619 st_child_kill(gpointer data)
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);
629 crm_perror(LOG_ERR,
"Couldn't send SIGKILL to %d", track->pid);
637 if (action->timer_sigterm > 0) {
638 g_source_remove(action->timer_sigterm);
639 action->timer_sigterm = 0;
641 if (action->timer_sigkill > 0) {
642 g_source_remove(action->timer_sigkill);
643 action->timer_sigkill = 0;
645 if (action->fd_stdout) {
646 close(action->fd_stdout);
647 action->fd_stdout = 0;
649 if (action->fd_stderr) {
650 close(action->fd_stderr);
651 action->fd_stderr = 0;
653 free(action->output);
654 action->output = NULL;
656 action->error = NULL;
659 action->last_timeout_signo = 0;
665 stonith_action_clear_tracking_data(action);
668 free(action->action);
669 free(action->victim);
673 #define FAILURE_MAX_RETRIES 2
679 int timeout, GHashTable * device_args, GHashTable * port_map)
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);
689 action->victim = strdup(victim);
691 action->timeout = action->remaining_timeout = timeout;
696 const char *value = NULL;
698 snprintf(buffer,
sizeof(buffer),
"pcmk_%s_retries", _action);
699 value = g_hash_table_lookup(device_args, buffer);
702 action->max_retries = atoi(value);
725 more = read(fd, buffer,
READ_MAX - 1);
731 output = realloc_safe(output, len + more + 1);
732 snprintf(output + len, more + 1,
"%s", buffer);
736 }
while (more == (
READ_MAX - 1) || (more < 0 && errno == EINTR));
744 int diff = time(NULL) - action->initial_start_time;
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)) {
753 action->remaining_timeout = action->timeout - diff;
755 action->remaining_timeout = 0;
757 return action->remaining_timeout ? TRUE : FALSE;
761 stonith_action_async_done(
mainloop_child_t * p, pid_t pid,
int core,
int signo,
int exitcode)
765 if (action->timer_sigterm > 0) {
766 g_source_remove(action->timer_sigterm);
767 action->timer_sigterm = 0;
769 if (action->timer_sigkill > 0) {
770 g_source_remove(action->timer_sigkill);
771 action->timer_sigkill = 0;
774 action->output = read_output(action->fd_stdout);
775 action->error = read_output(action->fd_stderr);
777 if (action->last_timeout_signo) {
779 crm_notice(
"Child process %d performing action '%s' timed out with signal %d",
780 pid, action->action, action->last_timeout_signo);
783 action->rc = -ECONNABORTED;
784 crm_notice(
"Child process %d performing action '%s' timed out with signal %d",
785 pid, action->action, signo);
788 crm_debug(
"Child process %d performing action '%s' exited with rc %d",
789 pid, action->action, exitcode);
794 if (action->error == NULL) {
797 }
else if (strstr(action->error,
"imed out")) {
799 exitcode = -ETIMEDOUT;
801 }
else if (strstr(action->error,
"Unrecognised action")) {
802 exitcode = -EOPNOTSUPP;
808 action->rc = exitcode;
811 log_action(action, pid);
813 if (action->rc !=
pcmk_ok && update_remaining_timeout(action)) {
814 int rc = internal_stonith_action_execute(action);
820 if (action->done_cb) {
821 action->done_cb(pid, action->rc, action->output, action->userdata);
824 stonith_action_destroy(action);
830 int pid, status = 0, len, rc = -EPROTO;
833 int p_read_fd, p_write_fd;
834 int c_read_fd, c_write_fd;
835 int c_stderr_fd, p_stderr_fd;
842 stonith_action_clear_tracking_data(action);
844 if (!action->tries) {
845 action->initial_start_time = time(NULL);
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);
855 c_read_fd = c_write_fd = p_read_fd = p_write_fd = c_stderr_fd = p_stderr_fd = -1;
857 if (action->args == NULL || action->agent == NULL)
859 len = strlen(action->args);
873 p_stderr_fd = fd3[0];
874 c_stderr_fd = fd3[1];
889 if (dup(c_write_fd) < 0)
893 if (dup(c_stderr_fd) < 0)
897 if (dup(c_read_fd) < 0)
911 execlp(action->agent, action->agent, NULL);
919 crm_notice(
"Could not set output of %s to be non-blocking: %s "
925 crm_notice(
"Could not set error output of %s to be non-blocking: %s "
932 ret = write(p_write_fd, action->args + total, len - total);
937 }
while (errno == EINTR && total < len);
940 crm_perror(LOG_ERR,
"Sent %d not %d bytes", total, len);
947 close(p_write_fd); p_write_fd = -1;
951 action->fd_stdout = p_read_fd;
952 action->fd_stderr = p_stderr_fd;
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);
963 crm_err(
"No timeout set for stonith operation %s with device %s",
964 action->action, action->agent);
974 int timeout = action->remaining_timeout + 1;
977 while (action->remaining_timeout < 0 || timeout > 0) {
978 p = waitpid(pid, &status, WNOHANG);
987 int killrc = kill(-pid, SIGKILL);
989 if (killrc && errno != ESRCH) {
998 p = waitpid(pid, &status, 0);
1004 }
else if (p != pid) {
1005 crm_err(
"Waited for %d, got %d", pid, p);
1008 action->output = read_output(p_read_fd);
1009 action->error = read_output(p_stderr_fd);
1011 action->rc = -ECONNABORTED;
1013 log_action(action, pid);
1017 action->rc = -
ETIME;
1018 }
else if (WIFEXITED(status)) {
1019 crm_debug(
"result = %d", WEXITSTATUS(status));
1020 action->rc = -WEXITSTATUS(status);
1023 }
else if (WIFSIGNALED(status)) {
1024 crm_err(
"call %s for %s exited due to signal %d", action->action, action->agent,
1028 crm_err(
"call %s for %s returned unexpected status %#x",
1029 action->action, action->agent, status);
1035 if (p_read_fd >= 0) {
1038 if (p_write_fd >= 0) {
1041 if (p_stderr_fd >= 0) {
1045 if (c_read_fd >= 0) {
1048 if (c_write_fd >= 0) {
1051 if (c_stderr_fd >= 0) {
1061 void (*done) (GPid pid,
int rc,
const char *output,
1062 gpointer user_data))
1070 action->userdata = userdata;
1071 action->done_cb = done;
1074 rc = internal_stonith_action_execute(action);
1076 return rc < 0 ? rc : action->pid;
1089 rc = internal_stonith_action_execute(action);
1095 }
while (update_remaining_timeout(action));
1103 *agent_result = action->rc;
1106 *output = action->output;
1107 action->output = NULL;
1110 stonith_action_destroy(action);
1115 stonith_api_device_list(
stonith_t * stonith,
int call_options,
const char *
namespace,
1120 if (devices == NULL) {
1121 crm_err(
"Parameter error: stonith_api_device_list");
1126 if (
namespace == NULL ||
safe_str_eq(
"heartbeat",
namespace)) {
1127 #if HAVE_STONITH_STONITH_H
1128 static gboolean need_init = TRUE;
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;
1145 type_list = (*type_list_fn) ();
1148 for (entry = type_list; entry != NULL && *entry; ++entry) {
1153 if (type_list && type_free_fn) {
1154 (*type_free_fn) (type_list);
1157 if (
namespace != NULL) {
1164 if (
namespace == NULL ||
safe_str_eq(
"redhat",
namespace)) {
1165 struct dirent **namelist;
1170 char buffer[FILENAME_MAX + 1];
1172 while (file_num--) {
1173 if (
'.' == namelist[file_num]->d_name[0]) {
1174 free(namelist[file_num]);
1179 free(namelist[file_num]);
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)) {
1189 free(namelist[file_num]);
1198 #if HAVE_STONITH_STONITH_H
1199 static inline char *
1200 strdup_null(
const char *val)
1209 stonith_plugin(
int priority,
const char *fmt, ...)
__attribute__((__format__ (__printf__, 2, 3)));
1212 stonith_plugin(
int priority, const
char *format, ...)
1218 char *
string = NULL;
1220 va_start(ap, format);
1222 len = vasprintf (&
string, format, ap);
1233 stonith_api_device_metadata(
stonith_t * stonith,
int call_options,
const char *agent,
1234 const char *
namespace,
char **output,
int timeout)
1237 char *buffer = NULL;
1240 crm_trace(
"looking up %s/%s metadata", agent, provider);
1250 xmlNode *xml = NULL;
1251 xmlNode *actions = NULL;
1252 xmlXPathObject *xpathObj = NULL;
1254 if (exec_rc < 0 || rc != 0 || buffer == NULL) {
1255 crm_warn(
"Could not obtain metadata for %s", agent);
1263 crm_warn(
"Metadata for %s is invalid", agent);
1269 if (numXpathResults(xpathObj) > 0) {
1276 xpathObj =
xpath_search(xml,
"//action[@name='stop']");
1277 if (numXpathResults(xpathObj) <= 0) {
1278 xmlNode *tmp = NULL;
1292 xpathObj =
xpath_search(xml,
"//parameter[@name='port']");
1293 if (numXpathResults(xpathObj) > 0) {
1309 #if !HAVE_STONITH_STONITH_H
1313 static const char *no_parameter_info =
"<!-- no value -->";
1315 Stonith *stonith_obj = NULL;
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;
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;
1342 char *meta_param = NULL;
1343 char *meta_longdesc = NULL;
1344 char *meta_shortdesc = NULL;
1346 stonith_obj = (*st_new_fn) (agent);
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);
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);
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);
1366 (*st_del_fn) (stonith_obj);
1372 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_longdesc);
1373 xml_meta_shortdesc =
1374 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_shortdesc);
1376 bufferlen = strlen(META_TEMPLATE) + strlen(agent)
1377 + strlen(xml_meta_longdesc) + strlen(xml_meta_shortdesc)
1378 + strlen(meta_param) + 1;
1380 buffer = calloc(1, bufferlen);
1381 snprintf(buffer, bufferlen - 1, META_TEMPLATE,
1382 agent, xml_meta_longdesc, xml_meta_shortdesc, meta_param);
1384 xmlFree(xml_meta_longdesc);
1385 xmlFree(xml_meta_shortdesc);
1387 free(meta_shortdesc);
1388 free(meta_longdesc);
1405 stonith_api_query(
stonith_t * stonith,
int call_options,
const char *target,
1408 int rc = 0, lpc = 0, max = 0;
1410 xmlNode *data = NULL;
1411 xmlNode *output = NULL;
1412 xmlXPathObjectPtr xpathObj = NULL;
1414 CRM_CHECK(devices != NULL,
return -EINVAL);
1428 max = numXpathResults(xpathObj);
1430 for (lpc = 0; lpc < max; lpc++) {
1435 xmlChar *match_path = xmlGetNodePath(match);
1437 crm_info(
"%s[%d] = %s",
"//@agent", lpc, match_path);
1455 const char *action,
const char *victim,
int timeout, xmlNode ** output)
1458 xmlNode *data = NULL;
1473 stonith_api_list(
stonith_t * stonith,
int call_options,
const char *
id,
char **list_info,
1477 xmlNode *output = NULL;
1479 rc = stonith_api_call(stonith, call_options,
id,
"list", NULL, timeout, &output);
1481 if (output && list_info) {
1482 const char *list_str;
1487 *list_info = strdup(list_str);
1499 stonith_api_monitor(
stonith_t * stonith,
int call_options,
const char *
id,
int timeout)
1501 return stonith_api_call(stonith, call_options,
id,
"monitor", NULL, timeout, NULL);
1505 stonith_api_status(
stonith_t * stonith,
int call_options,
const char *
id,
const char *port,
1508 return stonith_api_call(stonith, call_options,
id,
"status", port, timeout, NULL);
1512 stonith_api_fence(
stonith_t * stonith,
int call_options,
const char *node,
const char *action,
1513 int timeout,
int tolerance)
1516 xmlNode *data = NULL;
1531 stonith_api_confirm(
stonith_t * stonith,
int call_options,
const char *target)
1533 return stonith_api_fence(stonith, call_options |
st_opt_manual_ack, target,
"off", 0, 0);
1537 stonith_api_history(
stonith_t * stonith,
int call_options,
const char *node,
1541 xmlNode *data = NULL;
1542 xmlNode *output = NULL;
1560 for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
1588 char buffer[FILENAME_MAX + 1];
1591 rc = stat(buffer, &prop);
1592 if (rc >= 0 && S_ISREG(prop.st_mode)) {
1605 #if HAVE_STONITH_STONITH_H
1607 Stonith *stonith_obj = NULL;
1609 static gboolean need_init = TRUE;
1610 static Stonith *(*st_new_fn) (
const char *) = NULL;
1611 static void (*st_del_fn) (Stonith *) = NULL;
1622 if (lha_agents_lib && st_new_fn && st_del_fn) {
1623 stonith_obj = (*st_new_fn) (agent);
1625 (*st_del_fn) (stonith_obj);
1636 crm_err(
"No such device: %s", agent);
1642 stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
1648 CRM_CHECK(a_client->event != NULL && b_client->event != NULL,
return 0);
1649 rc = strcmp(a_client->event, b_client->event);
1651 if (a_client->notify == NULL || b_client->notify == NULL) {
1654 }
else if (a_client->notify == b_client->notify) {
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);
1662 crm_err(
"callbacks for %s are not equal: %p vs. %p",
1663 a_client->event, a_client->notify, b_client->notify);
1683 crm_trace(
"Sending call options: %.8lx, %d", (
long)call_options, call_options);
1694 stonith_destroy_op_callback(gpointer data)
1698 if (blob->timer && blob->timer->ref > 0) {
1699 g_source_remove(blob->timer->ref);
1706 stonith_api_signoff(
stonith_t * stonith)
1710 crm_debug(
"Signing out of the STONITH Service");
1712 if (native->source != NULL) {
1715 native->source = NULL;
1718 }
else if (native->ipc) {
1727 free(native->token); native->token = NULL;
1733 stonith_api_signon(
stonith_t * stonith,
const char *name,
int *stonith_fd)
1740 .destroy = stonith_connection_destroy
1743 crm_trace(
"Connecting command channel");
1752 }
else if (native->ipc) {
1753 crm_perror(LOG_ERR,
"Connection to STONITH manager failed");
1764 if (native->ipc == NULL) {
1765 crm_debug(
"Could not connect to the Stonith API");
1770 xmlNode *reply = NULL;
1779 crm_perror(LOG_DEBUG,
"Couldn't complete registration with the fencing API: %d", rc);
1782 }
else if (reply == NULL) {
1783 crm_err(
"Did not receive registration reply");
1791 crm_err(
"Invalid registration message: %s", msg_type);
1795 }
else if (tmp_ticket == NULL) {
1796 crm_err(
"No registration token provided");
1801 crm_trace(
"Obtained registration token: %s", tmp_ticket);
1802 native->token = strdup(tmp_ticket);
1812 #if HAVE_MSGFROMIPC_TIMEOUT
1815 crm_debug(
"Connection to STONITH successful");
1825 stonith_set_notification(
stonith_t * stonith,
const char *callback,
int enabled)
1842 crm_perror(LOG_DEBUG,
"Couldn't register for fencing notifications: %d", rc);
1854 stonith_api_add_notification(
stonith_t * stonith,
const char *event,
1857 GList *list_item = NULL;
1862 crm_trace(
"Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
1865 new_client->event = event;
1866 new_client->notify = callback;
1868 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1870 if (list_item != NULL) {
1871 crm_warn(
"Callback already present");
1876 private->notify_list = g_list_append(private->notify_list, new_client);
1878 stonith_set_notification(stonith, event, 1);
1880 crm_trace(
"Callback added (%d)", g_list_length(private->notify_list));
1886 stonith_api_del_notification(
stonith_t * stonith,
const char *event)
1888 GList *list_item = NULL;
1892 crm_debug(
"Removing callback for %s events", event);
1896 new_client->event = event;
1897 new_client->notify = NULL;
1899 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1901 stonith_set_notification(stonith, event, 0);
1903 if (list_item != NULL) {
1906 private->notify_list = g_list_remove(private->notify_list, list_client);
1919 stonith_async_timeout_handler(gpointer data)
1936 struct timer_rec_s *async_timer = callback->timer;
1943 async_timer = calloc(1,
sizeof(
struct timer_rec_s));
1944 callback->timer = async_timer;
1952 async_timer->
timeout = (timeout + 60) * 1000;
1953 if (async_timer->
ref) {
1954 g_source_remove(async_timer->
ref);
1957 g_timeout_add(async_timer->
timeout, stonith_async_timeout_handler, async_timer);
1961 update_callback_timeout(
int call_id,
int timeout,
stonith_t * st)
1966 callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1967 if (!callback || !callback->allow_timeout_updates) {
1971 set_callback_timeout(callback, st, call_id, timeout);
1975 invoke_callback(
stonith_t * st,
int call_id,
int rc,
void *userdata,
1984 callback(st, &data);
1988 stonith_api_add_callback(
stonith_t * stonith,
int call_id,
int timeout,
int options,
1989 void *user_data,
const char *callback_name,
1995 CRM_CHECK(stonith != NULL,
return -EINVAL);
2000 private->op_callback = callback;
2002 }
else if (call_id < 0) {
2005 invoke_callback(stonith, call_id, call_id, user_data, callback);
2013 blob->id = callback_name;
2015 blob->user_data = user_data;
2016 blob->callback = callback;
2020 set_callback_timeout(blob, stonith, call_id, timeout);
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);
2030 stonith_api_del_callback(
stonith_t * stonith,
int call_id,
bool all_callbacks)
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,
2039 stonith_destroy_op_callback);
2041 }
else if (call_id == 0) {
2042 private->op_callback = NULL;
2045 g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2051 stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
2053 int call = GPOINTER_TO_INT(key);
2064 if (private->stonith_op_callback_table == NULL) {
2067 return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
2082 local_blob.id = NULL;
2083 local_blob.callback = NULL;
2084 local_blob.user_data = NULL;
2085 local_blob.only_success = FALSE;
2094 blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2100 stonith_api_del_callback(stonith, call_id, FALSE);
2103 crm_trace(
"No callback found for call %d", call_id);
2104 local_blob.callback = NULL;
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);
2111 }
else if (private->op_callback == NULL && rc !=
pcmk_ok) {
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);
2143 xml_to_event(xmlNode * msg)
2167 crm_err(
"No data for %s event", ntype);
2193 stonith_send_notification(gpointer data, gpointer user_data)
2195 struct notify_blob_s *blob = user_data;
2198 const char *
event = NULL;
2200 if (blob->xml == NULL) {
2201 crm_warn(
"Skipping callback - NULL message");
2207 if (entry == NULL) {
2208 crm_warn(
"Skipping callback - NULL callback client");
2211 }
else if (entry->notify == NULL) {
2212 crm_warn(
"Skipping callback - NULL callback");
2216 crm_trace(
"Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
2220 st_event = xml_to_event(blob->xml);
2222 crm_trace(
"Invoking callback for %p/%s event...", entry, event);
2223 entry->notify(blob->stonith, st_event);
2226 event_free(st_event);
2231 int call_options,
int timeout)
2237 xmlNode *op_msg = NULL;
2238 xmlNode *op_reply = NULL;
2246 if (output_data != NULL) {
2247 *output_data = NULL;
2251 crm_err(
"No operation specified");
2271 if (op_msg == NULL) {
2276 crm_trace(
"Sending %s message to STONITH service, Timeout: %ds", op, timeout);
2278 rc =
crm_ipc_send(native->ipc, op_msg, ipc_flags, 1000 * (timeout + 60), &op_reply);
2282 crm_perror(LOG_ERR,
"Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
2289 if (!(call_options & st_opt_sync_call)) {
2300 if (reply_id == stonith->
call_id) {
2301 crm_trace(
"Synchronous reply %d received", reply_id);
2311 *output_data = op_reply;
2315 }
else if (reply_id <= 0) {
2316 crm_err(
"Received bad reply: No id set");
2322 crm_err(
"Received bad reply: %d (wanted %d)", reply_id, stonith->
call_id);
2330 crm_err(
"STONITH disconnected");
2342 gboolean stay_connected = TRUE;
2358 stay_connected = FALSE;
2362 return stay_connected;
2368 const char *
type = NULL;
2369 struct notify_blob_s blob;
2375 private = st->private;
2379 if (blob.xml == NULL) {
2380 crm_warn(
"Received a NULL msg from STONITH service: %s.", buffer);
2386 crm_trace(
"Activating %s callbacks...", type);
2392 g_list_foreach(private->notify_list, stonith_send_notification, &blob);
2400 update_callback_timeout(call_id, timeout, st);
2402 crm_err(
"Unknown message type: %s", type);
2418 crm_trace(
"Disconnecting %p first", stonith);
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);
2428 crm_trace(
"Destroying %d notification clients", g_list_length(private->notify_list));
2429 g_list_free_full(private->notify_list, free);
2432 free(stonith->
cmds);
2457 new_stonith = calloc(1,
sizeof(
stonith_t));
2459 new_stonith->
private =
private;
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;
2471 new_stonith->
cmds->
free = stonith_api_free;
2475 new_stonith->
cmds->
list = stonith_api_list;
2477 new_stonith->
cmds->
status = stonith_api_status;
2478 new_stonith->
cmds->
fence = stonith_api_fence;
2483 new_stonith->
cmds->
metadata = stonith_api_device_metadata;
2485 new_stonith->
cmds->
query = stonith_api_query;
2510 p->
key = strdup(key);
2513 p->
value = strdup(value);
2517 while (end && end->
next) {
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)
2555 const char *action =
"reboot";
2566 api_log(LOG_ERR,
"Connection failed, could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2570 if (uname != NULL) {
2571 name = strdup(uname);
2573 }
else if (nodeid > 0) {
2583 rc = st->
cmds->
fence(st, opts, name, action, timeout, 0);
2585 api_log(LOG_ERR,
"Could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2587 api_log(LOG_NOTICE,
"Node %u/%s kicked: %s ", nodeid, uname, action);
2619 if (uname != NULL) {
2620 name = strdup(uname);
2622 }
else if (nodeid > 0) {
2632 rc = st->
cmds->
history(st, opts, name, &history, 120);
2634 for (hp = history; hp; hp = hp->
next) {
2651 api_log(LOG_INFO,
"Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
2653 api_log(LOG_ERR,
"Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname,
pcmk_strerror(rc), rc);
2663 api_log(LOG_INFO,
"Node %u/%s last kicked at: %ld", nodeid, uname, (
long int)when);
2669 #if HAVE_STONITH_STONITH_H
2670 # include <pils/plugin.h>
2672 const char *i_hate_pils(
int rc);
2677 return PIL_strerror(rc);
#define CRM_CHECK(expr, failure_action)
int stonith_send_command(stonith_t *stonith, const char *op, xmlNode *data, xmlNode **output_data, int call_options, int timeout)
#define XML_ATTR_STONITH_TARGET_ATTRIBUTE
struct stonith_action_s stonith_action_t
#define F_STONITH_REMOTE_OP_ID
int(* query)(stonith_t *st, int options, const char *node, stonith_key_value_t **devices, int timeout)
Retrieve a list of registered stonith devices.
struct stonith_history_s * next
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
void * find_library_function(void **handle, const char *lib, const char *fn, int fatal)
#define crm_notice(fmt, args...)
#define F_STONITH_CLIENTID
gboolean safe_str_neq(const char *a, const char *b)
int(* register_notification)(stonith_t *st, const char *event, void(*notify)(stonith_t *st, stonith_event_t *e))
int(* register_level_full)(stonith_t *st, int options, const char *node, const char *pattern, const char *attr, const char *value, int level, stonith_key_value_t *device_list)
Register fencing level for specific node, node regex or attribute.
const char * get_stonith_provider(const char *agent, const char *provider)
int(* metadata)(stonith_t *st, int options, const char *device, const char *namespace, char **output, int timeout)
Get the metadata documentation for a resource.
xmlNode * create_level_registration_xml(const char *node, const char *pattern, const char *attr, const char *value, int level, stonith_key_value_t *device_list)
#define crm_log_output(level, prefix, output)
int crm_ipc_get_fd(crm_ipc_t *client)
const char * pcmk_strerror(int rc)
int stonith_api_kick(uint32_t nodeid, const char *uname, int timeout, bool off)
int(* history)(stonith_t *st, int options, const char *node, stonith_history_t **output, int timeout)
Retrieve a list of fencing operations that have occurred for a specific node.
#define F_STONITH_CALLBACK_TOKEN
int(* register_level)(stonith_t *st, int options, const char *node, int level, stonith_key_value_t *device_list)
Register a fencing level containing the fencing devices to be used at that level for a specific node...
#define F_STONITH_CLIENTNAME
xmlNode * stonith_create_op(int call_id, const char *token, const char *op, xmlNode *data, int call_options)
#define XML_TAG_FENCING_LEVEL
struct stonith_key_value_s * next
int alphasort(const void *dirent1, const void *dirent2)
struct mainloop_io_s mainloop_io_t
int(* monitor)(stonith_t *st, int options, const char *id, int timeout)
Check to see if a local stonith device is reachable.
struct mainloop_child_s mainloop_child_t
char * crm_element_value_copy(xmlNode *data, const char *name)
#define F_STONITH_TIMEOUT
gboolean is_redhat_agent(const char *agent)
int(* free)(stonith_t *st)
Destroy the stonith api structure.
#define T_STONITH_TIMEOUT_VALUE
#define CRM_LOG_ASSERT(expr)
int(* list)(stonith_t *st, int options, const char *id, char **list_output, int timeout)
Retrieve string listing hosts and port assignments from a local stonith device.
long crm_ipc_read(crm_ipc_t *client)
#define do_crm_log_alias(level, file, function, line, fmt, args...)
Log a message as if it came from a different code location.
int stonith_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata)
#define F_STONITH_NOTIFY_DEACTIVATE
int(* confirm)(stonith_t *st, int options, const char *node)
Manually confirm that a node is down.
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
Wrappers for and extensions to glib mainloop.
bool crm_starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
#define STONITH_OP_LEVEL_DEL
bool stonith_dispatch(stonith_t *st)
#define STONITH_OP_DEVICE_ADD
char * crm_meta_name(const char *field)
stonith_t * stonith_api_new(void)
xmlNode * string2xml(const char *input)
const char * crm_ipc_buffer(crm_ipc_t *client)
int(* stonith_op_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
#define CRM_TRACE_INIT_DATA(name)
stonith_key_value_t * stonith_key_value_add(stonith_key_value_t *kvp, const char *key, const char *value)
int(* remove_callback)(stonith_t *st, int call_id, bool all_callbacks)
Remove a registered callback for a given call id.
void hash2field(gpointer key, gpointer value, gpointer user_data)
#define XML_ATTR_STONITH_TARGET_PATTERN
#define crm_warn(fmt, args...)
#define STONITH_ATTR_HOSTARG
int(* register_callback)(stonith_t *st, int call_id, int timeout, int options, void *userdata, const char *callback_name, void(*callback)(stonith_t *st, stonith_callback_data_t *data))
Register a callback to receive the result of an async call id.
#define crm_debug(fmt, args...)
void * mainloop_child_userdata(mainloop_child_t *child)
#define STONITH_OP_LEVEL_ADD
struct crm_ipc_s crm_ipc_t
#define XML_ATTR_STONITH_DEVICES
#define crm_trace(fmt, args...)
#define XML_ATTR_STONITH_INDEX
enum crm_proc_flag __attribute__
int crm_set_nonblocking(int fd)
#define crm_log_xml_debug(xml, text)
#define XML_ATTR_STONITH_TARGET
Wrappers for and extensions to libxml2.
int(* status)(stonith_t *st, int options, const char *id, const char *port, int timeout)
Check to see if a local stonith device's port is reachable.
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define crm_log_xml_warn(xml, text)
int(* remove_level)(stonith_t *st, int options, const char *node, int level)
Remove a fencing level for a specific node.
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
const char * crm_element_value(xmlNode *data, const char *name)
xmlNode * create_device_registration_xml(const char *id, const char *namespace, const char *agent, stonith_key_value_t *params, const char *rsc_provides)
void stonith_key_value_freeall(stonith_key_value_t *kvp, int keys, int values)
#define T_STONITH_NOTIFY_DISCONNECT
#define CRM_DEFAULT_OP_TIMEOUT_S
#define STONITH_OP_DEVICE_DEL
void mainloop_del_ipc_client(mainloop_io_t *client)
void crm_ipc_destroy(crm_ipc_t *client)
#define XML_ATTR_STONITH_TARGET_VALUE
struct stonith_notify_client_s stonith_notify_client_t
#define RH_STONITH_PREFIX
void stonith_dump_pending_callbacks(stonith_t *st)
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
GPid stonith_action_execute_async(stonith_action_t *action, void *userdata, void(*done)(GPid pid, int rc, const char *output, gpointer user_data))
void free_xml(xmlNode *child)
#define api_log(level, fmt, args...)
int(* remove_level_full)(stonith_t *st, int options, const char *node, const char *pattern, const char *attr, const char *value, int level)
Remove fencing level for specific node, node regex or attribute.
#define STONITH_ATTR_ACTION_OP
#define STONITH_OP_FENCE_HISTORY
#define F_STONITH_CALLOPTS
struct stonith_private_s stonith_private_t
bool crm_ipc_connected(crm_ipc_t *client)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
int crm_ipc_ready(crm_ipc_t *client)
Check whether an IPC connection is ready to be read.
void mainloop_child_add(pid_t pid, int timeout, const char *desc, void *userdata, void(*callback)(mainloop_child_t *p, pid_t pid, int core, int signo, int exitcode))
#define crm_log_xml_err(xml, text)
struct stonith_callback_client_s stonith_callback_client_t
void stonith_api_delete(stonith_t *st)
#define crm_perror(level, fmt, args...)
Log a system error message.
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
#define crm_err(fmt, args...)
#define G_PRIORITY_MEDIUM
void stonith_perform_callback(stonith_t *stonith, xmlNode *msg, int call_id, int rc)
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
int(* fence)(stonith_t *st, int options, const char *node, const char *action, int timeout, int tolerance)
Issue a fencing action against a node.
stonith_api_operations_t * cmds
int crm_ipc_send(crm_ipc_t *client, xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
#define FAILURE_MAX_RETRIES
#define crm_log_xml_notice(xml, text)
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
int(* connect)(stonith_t *st, const char *name, int *stonith_fd)
Connect to the local stonith daemon.
#define STONITH_ATTR_ARGMAP
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
int(* remove_device)(stonith_t *st, int options, const char *name)
Remove a registered stonith device with the local stonith daemon.
int(* disconnect)(stonith_t *st)
Disconnect from the local stonith daemon.
time_t stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
#define T_STONITH_NOTIFY_FENCE
#define crm_log_xml_trace(xml, text)
#define F_STONITH_OPERATION
int(* register_device)(stonith_t *st, int options, const char *id, const char *namespace, const char *agent, stonith_key_value_t *params)
Register a stonith device with the local stonith daemon.
mainloop_io_t * mainloop_add_ipc_client(const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
stonith_action_t * stonith_action_create(const char *agent, const char *_action, const char *victim, uint32_t victim_nodeid, int timeout, GHashTable *device_args, GHashTable *port_map)
int(* list_agents)(stonith_t *stonith, int call_options, const char *namespace, stonith_key_value_t **devices, int timeout)
Retrieve a list of installed stonith agents.
char * crm_itoa(int an_int)
#define safe_str_eq(a, b)
#define F_STONITH_NOTIFY_ACTIVATE
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
void freeXpathObject(xmlXPathObjectPtr xpathObj)
void crm_ipc_close(crm_ipc_t *client)
#define F_STONITH_CALLDATA
int stonith_action_execute(stonith_action_t *action, int *agent_result, char **output)
#define F_STONITH_DELEGATE
#define crm_info(fmt, args...)
char * dump_xml_formatted_with_text(xmlNode *msg)
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
#define F_STONITH_HISTORY_LIST
int(* remove_notification)(stonith_t *st, const char *event)
enum crm_ais_msg_types type
#define F_STONITH_TOLERANCE