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 sprintf(list + len,
"%s%s", len?
",":
"", device_list->
value);
368 stonith_api_register_level_full(
stonith_t * st,
int options,
const char *node,
370 const char *attr,
const char *value,
385 stonith_api_register_level(
stonith_t * st,
int options,
const char *node,
int level,
388 return stonith_api_register_level_full(st, options, node, NULL, NULL, NULL,
393 append_arg(gpointer key, gpointer value, gpointer user_data)
397 char **args = user_data;
402 if (strstr(key,
"pcmk_")) {
411 len += strlen(value);
413 last = strlen(*args);
416 *args = realloc_safe(*args, last + len);
417 crm_trace(
"Appending: %s=%s", (
char *)key, (
char *)value);
418 sprintf((*args) + last,
"%s=%s\n", (
char *)key, (
char *)value);
422 append_const_arg(
const char *key,
const char *value,
char **arg_list)
426 char *glib_sucks_key = strdup(key);
427 char *glib_sucks_value = strdup(value);
429 append_arg(glib_sucks_key, glib_sucks_value, arg_list);
431 free(glib_sucks_value);
432 free(glib_sucks_key);
437 append_host_specific_args(
const char *victim,
const char *map, GHashTable * params,
char **arg_list)
440 int last = 0, lpc = 0, max = 0;
444 crm_debug(
"Using default arg map: port=uname");
445 append_const_arg(
"port", victim, arg_list);
450 crm_debug(
"Processing arg map: %s", map);
451 for (; lpc < max + 1; lpc++) {
452 if (isalpha(map[lpc])) {
455 }
else if (map[lpc] ==
'=' || map[lpc] ==
':') {
457 name = calloc(1, 1 + lpc - last);
458 memcpy(name, map + last, lpc - last);
462 }
else if (map[lpc] == 0 || map[lpc] ==
',' || isspace(map[lpc])) {
464 const char *value = NULL;
466 param = calloc(1, 1 + lpc - last);
467 memcpy(param, map + last, lpc - last);
472 crm_err(
"Misparsed '%s', found '%s' without a name", map, param);
482 value = g_hash_table_lookup(params, key);
487 crm_debug(
"Setting '%s'='%s' (%s) for %s", name, value, param, victim);
488 append_const_arg(name, value, arg_list);
491 crm_err(
"No node attribute '%s' for '%s'", name, victim);
501 }
else if (isspace(map[lpc])) {
509 make_args(
const char *agent,
const char *action,
const char *victim,
uint32_t victim_nodeid, GHashTable * device_args,
510 GHashTable * port_map)
513 char *arg_list = NULL;
514 const char *value = NULL;
515 const char *_action = action;
520 snprintf(buffer, 511,
"pcmk_%s_action", action);
522 value = g_hash_table_lookup(device_args, buffer);
525 if (value == NULL && device_args) {
527 snprintf(buffer, 511,
"pcmk_%s_cmd", action);
528 value = g_hash_table_lookup(device_args, buffer);
531 if (value == NULL && device_args &&
safe_str_eq(action,
"off")) {
533 value = g_hash_table_lookup(device_args,
"pcmk_poweroff_action");
537 crm_info(
"Substituting action '%s' for requested operation '%s'", value, action);
542 if (victim && device_args) {
543 const char *alias = victim;
546 if (port_map && g_hash_table_lookup(port_map, victim)) {
547 alias = g_hash_table_lookup(port_map, victim);
553 append_const_arg(
"nodename", victim, &arg_list);
555 char nodeid_str[33] = { 0, };
556 if (snprintf(nodeid_str, 33,
"%u", (
unsigned int)victim_nodeid)) {
557 crm_info(
"For stonith action (%s) for victim %s, adding nodeid (%d) to parameters",
558 action, victim, nodeid_str);
559 append_const_arg(
"nodeid", nodeid_str, &arg_list);
567 }
else if (param == NULL) {
572 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_const_arg(param, alias, &arg_list);
596 g_hash_table_foreach(device_args, append_arg, &arg_list);
615 st_child_term(gpointer data)
620 crm_info(
"Child %d timed out, sending SIGTERM", track->pid);
621 track->timer_sigterm = 0;
622 track->last_timeout_signo = SIGTERM;
623 rc = kill(-track->pid, SIGTERM);
625 crm_perror(LOG_ERR,
"Couldn't send SIGTERM to %d", track->pid);
631 st_child_kill(gpointer data)
636 crm_info(
"Child %d timed out, sending SIGKILL", track->pid);
637 track->timer_sigkill = 0;
638 track->last_timeout_signo = SIGKILL;
639 rc = kill(-track->pid, SIGKILL);
641 crm_perror(LOG_ERR,
"Couldn't send SIGKILL to %d", track->pid);
649 if (action->timer_sigterm > 0) {
650 g_source_remove(action->timer_sigterm);
651 action->timer_sigterm = 0;
653 if (action->timer_sigkill > 0) {
654 g_source_remove(action->timer_sigkill);
655 action->timer_sigkill = 0;
657 if (action->fd_stdout) {
658 close(action->fd_stdout);
659 action->fd_stdout = 0;
661 if (action->fd_stderr) {
662 close(action->fd_stderr);
663 action->fd_stderr = 0;
665 free(action->output);
666 action->output = NULL;
668 action->error = NULL;
671 action->last_timeout_signo = 0;
677 stonith_action_clear_tracking_data(action);
680 free(action->action);
681 free(action->victim);
685 #define FAILURE_MAX_RETRIES 2
691 int timeout, GHashTable * device_args, GHashTable * port_map)
696 crm_debug(
"Initiating action %s for agent %s (target=%s)", _action, agent, victim);
697 action->args = make_args(agent, _action, victim, victim_nodeid, device_args, port_map);
698 action->agent = strdup(agent);
699 action->action = strdup(_action);
701 action->victim = strdup(victim);
703 action->timeout = action->remaining_timeout = timeout;
708 const char *value = NULL;
710 snprintf(buffer, 511,
"pcmk_%s_retries", _action);
711 value = g_hash_table_lookup(device_args, buffer);
714 action->max_retries = atoi(value);
737 more = read(fd, buffer,
READ_MAX - 1);
743 output = realloc_safe(output, len + more + 1);
744 snprintf(output + len, more + 1,
"%s", buffer);
748 }
while (more == (
READ_MAX - 1) || (more < 0 && errno == EINTR));
756 int diff = time(NULL) - action->initial_start_time;
758 if (action->tries >= action->max_retries) {
759 crm_info(
"Attempted to execute agent %s (%s) the maximum number of times (%d) allowed",
760 action->agent, action->action, action->max_retries);
761 action->remaining_timeout = 0;
762 }
else if ((action->rc != -
ETIME) && diff < (action->timeout * 0.7)) {
765 action->remaining_timeout = action->timeout - diff;
767 action->remaining_timeout = 0;
769 return action->remaining_timeout ? TRUE : FALSE;
773 stonith_action_async_done(
mainloop_child_t * p, pid_t pid,
int core,
int signo,
int exitcode)
777 if (action->timer_sigterm > 0) {
778 g_source_remove(action->timer_sigterm);
779 action->timer_sigterm = 0;
781 if (action->timer_sigkill > 0) {
782 g_source_remove(action->timer_sigkill);
783 action->timer_sigkill = 0;
786 action->output = read_output(action->fd_stdout);
787 action->error = read_output(action->fd_stderr);
789 if (action->last_timeout_signo) {
791 crm_notice(
"Child process %d performing action '%s' timed out with signal %d",
792 pid, action->action, action->last_timeout_signo);
795 action->rc = -ECONNABORTED;
796 crm_notice(
"Child process %d performing action '%s' timed out with signal %d",
797 pid, action->action, signo);
800 crm_debug(
"Child process %d performing action '%s' exited with rc %d",
801 pid, action->action, exitcode);
806 if (action->error == NULL) {
809 }
else if (strstr(action->error,
"imed out")) {
811 exitcode = -ETIMEDOUT;
813 }
else if (strstr(action->error,
"Unrecognised action")) {
814 exitcode = -EOPNOTSUPP;
820 action->rc = exitcode;
823 log_action(action, pid);
825 if (action->rc !=
pcmk_ok && update_remaining_timeout(action)) {
826 int rc = internal_stonith_action_execute(action);
832 if (action->done_cb) {
833 action->done_cb(pid, action->rc, action->output, action->userdata);
836 stonith_action_destroy(action);
842 int pid, status, len, rc = -EPROTO;
845 int p_read_fd, p_write_fd;
846 int c_read_fd, c_write_fd;
847 int c_stderr_fd, p_stderr_fd;
854 stonith_action_clear_tracking_data(action);
856 if (!action->tries) {
857 action->initial_start_time = time(NULL);
861 if (action->tries > 1) {
862 crm_info(
"Attempt %d to execute %s (%s). remaining timeout is %d",
863 action->tries, action->agent, action->action, action->remaining_timeout);
867 c_read_fd = c_write_fd = p_read_fd = p_write_fd = c_stderr_fd = p_stderr_fd = -1;
869 if (action->args == NULL || action->agent == NULL)
871 len = strlen(action->args);
885 p_stderr_fd = fd3[0];
886 c_stderr_fd = fd3[1];
901 if (dup(c_write_fd) < 0)
905 if (dup(c_stderr_fd) < 0)
909 if (dup(c_read_fd) < 0)
923 execlp(action->agent, action->agent, NULL);
929 ret = fcntl(p_read_fd, F_SETFL, fcntl(p_read_fd, F_GETFL, 0) | O_NONBLOCK);
931 crm_perror(LOG_NOTICE,
"Could not change the output of %s to be non-blocking",
934 ret = fcntl(p_stderr_fd, F_SETFL, fcntl(p_stderr_fd, F_GETFL, 0) | O_NONBLOCK);
936 crm_perror(LOG_NOTICE,
"Could not change the stderr of %s to be non-blocking",
942 ret = write(p_write_fd, action->args + total, len - total);
947 }
while (errno == EINTR && total < len);
950 crm_perror(LOG_ERR,
"Sent %d not %d bytes", total, len);
957 close(p_write_fd); p_write_fd = -1;
961 action->fd_stdout = p_read_fd;
962 action->fd_stderr = p_stderr_fd;
964 crm_trace(
"Op: %s on %s, pid: %d, timeout: %ds", action->action, action->agent, pid,
965 action->remaining_timeout);
966 action->last_timeout_signo = 0;
967 if (action->remaining_timeout) {
968 action->timer_sigterm =
969 g_timeout_add(1000 * action->remaining_timeout, st_child_term, action);
970 action->timer_sigkill =
971 g_timeout_add(1000 * (action->remaining_timeout + 5), st_child_kill, action);
973 crm_err(
"No timeout set for stonith operation %s with device %s",
974 action->action, action->agent);
984 int timeout = action->remaining_timeout + 1;
987 while (action->remaining_timeout < 0 || timeout > 0) {
988 p = waitpid(pid, &status, WNOHANG);
997 int killrc = kill(-pid, SIGKILL);
999 if (killrc && errno != ESRCH) {
1008 p = waitpid(pid, &status, 0);
1014 }
else if (p != pid) {
1015 crm_err(
"Waited for %d, got %d", pid, p);
1018 action->output = read_output(p_read_fd);
1019 action->error = read_output(p_stderr_fd);
1021 action->rc = -ECONNABORTED;
1023 log_action(action, pid);
1027 action->rc = -
ETIME;
1028 }
else if (WIFEXITED(status)) {
1029 crm_debug(
"result = %d", WEXITSTATUS(status));
1030 action->rc = -WEXITSTATUS(status);
1033 }
else if (WIFSIGNALED(status)) {
1034 crm_err(
"call %s for %s exited due to signal %d", action->action, action->agent,
1038 crm_err(
"call %s for %s exited abnormally. stopped=%d, continued=%d",
1039 action->action, action->agent, WIFSTOPPED(status), WIFCONTINUED(status));
1045 if (p_read_fd >= 0) {
1048 if (p_write_fd >= 0) {
1051 if (p_stderr_fd >= 0) {
1055 if (c_read_fd >= 0) {
1058 if (c_write_fd >= 0) {
1061 if (c_stderr_fd >= 0) {
1071 void (*done) (GPid pid,
int rc,
const char *output,
1072 gpointer user_data))
1080 action->userdata = userdata;
1081 action->done_cb = done;
1084 rc = internal_stonith_action_execute(action);
1086 return rc < 0 ? rc : action->pid;
1099 rc = internal_stonith_action_execute(action);
1105 }
while (update_remaining_timeout(action));
1113 *agent_result = action->rc;
1116 *output = action->output;
1117 action->output = NULL;
1120 stonith_action_destroy(action);
1125 stonith_api_device_list(
stonith_t * stonith,
int call_options,
const char *
namespace,
1130 if (devices == NULL) {
1131 crm_err(
"Parameter error: stonith_api_device_list");
1136 if (
namespace == NULL ||
safe_str_eq(
"heartbeat",
namespace)) {
1137 #if HAVE_STONITH_STONITH_H
1138 static gboolean need_init = TRUE;
1140 char **entry = NULL;
1141 char **type_list = NULL;
1142 static char **(*type_list_fn) (void) = NULL;
1143 static void (*type_free_fn) (
char **) = NULL;
1155 type_list = (*type_list_fn) ();
1158 for (entry = type_list; entry != NULL && *entry; ++entry) {
1163 if (type_list && type_free_fn) {
1164 (*type_free_fn) (type_list);
1167 if (
namespace != NULL) {
1174 if (
namespace == NULL ||
safe_str_eq(
"redhat",
namespace)) {
1175 struct dirent **namelist;
1180 char buffer[FILENAME_MAX + 1];
1182 while (file_num--) {
1183 if (
'.' == namelist[file_num]->d_name[0]) {
1184 free(namelist[file_num]);
1189 free(namelist[file_num]);
1193 snprintf(buffer, FILENAME_MAX,
"%s/%s",
RH_STONITH_DIR, namelist[file_num]->d_name);
1194 if (stat(buffer, &prop) == 0 && S_ISREG(prop.st_mode)) {
1199 free(namelist[file_num]);
1208 #if HAVE_STONITH_STONITH_H
1209 static inline char *
1210 strdup_null(
const char *val)
1219 stonith_plugin(
int priority,
const char *fmt, ...)
__attribute__((__format__ (__printf__, 2, 3)));
1222 stonith_plugin(
int priority, const
char *format, ...)
1228 char *
string = NULL;
1230 va_start(ap, format);
1232 len = vasprintf (&
string, format, ap);
1243 stonith_api_device_metadata(
stonith_t * stonith,
int call_options,
const char *agent,
1244 const char *
namespace,
char **output,
int timeout)
1247 char *buffer = NULL;
1250 crm_trace(
"looking up %s/%s metadata", agent, provider);
1260 xmlNode *xml = NULL;
1261 xmlNode *actions = NULL;
1262 xmlXPathObject *xpathObj = NULL;
1264 if (exec_rc < 0 || rc != 0 || buffer == NULL) {
1265 crm_warn(
"Could not obtain metadata for %s", agent);
1273 crm_warn(
"Metadata for %s is invalid", agent);
1279 if (numXpathResults(xpathObj) > 0) {
1286 xpathObj =
xpath_search(xml,
"//action[@name='stop']");
1287 if (numXpathResults(xpathObj) <= 0) {
1288 xmlNode *tmp = NULL;
1302 xpathObj =
xpath_search(xml,
"//parameter[@name='port']");
1303 if (numXpathResults(xpathObj) > 0) {
1319 #if !HAVE_STONITH_STONITH_H
1323 static const char *no_parameter_info =
"<!-- no value -->";
1325 Stonith *stonith_obj = NULL;
1327 static gboolean need_init = TRUE;
1328 static Stonith *(*st_new_fn) (
const char *) = NULL;
1329 static const char *(*st_info_fn) (Stonith *, int) = NULL;
1330 static void (*st_del_fn) (Stonith *) = NULL;
1331 static void (*st_log_fn) (Stonith *, PILLogFun) = NULL;
1348 if (lha_agents_lib && st_new_fn && st_del_fn && st_info_fn && st_log_fn) {
1349 char *xml_meta_longdesc = NULL;
1350 char *xml_meta_shortdesc = NULL;
1352 char *meta_param = NULL;
1353 char *meta_longdesc = NULL;
1354 char *meta_shortdesc = NULL;
1356 stonith_obj = (*st_new_fn) (agent);
1358 (*st_log_fn) (stonith_obj, (PILLogFun) & stonith_plugin);
1359 meta_longdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEDESCR));
1360 if (meta_longdesc == NULL) {
1361 crm_warn(
"no long description in %s's metadata.", agent);
1362 meta_longdesc = strdup(no_parameter_info);
1365 meta_shortdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEID));
1366 if (meta_shortdesc == NULL) {
1367 crm_warn(
"no short description in %s's metadata.", agent);
1368 meta_shortdesc = strdup(no_parameter_info);
1371 meta_param = strdup_null((*st_info_fn) (stonith_obj, ST_CONF_XML));
1372 if (meta_param == NULL) {
1373 crm_warn(
"no list of parameters in %s's metadata.", agent);
1374 meta_param = strdup(no_parameter_info);
1376 (*st_del_fn) (stonith_obj);
1382 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_longdesc);
1383 xml_meta_shortdesc =
1384 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_shortdesc);
1386 bufferlen = strlen(META_TEMPLATE) + strlen(agent)
1387 + strlen(xml_meta_longdesc) + strlen(xml_meta_shortdesc)
1388 + strlen(meta_param) + 1;
1390 buffer = calloc(1, bufferlen);
1391 snprintf(buffer, bufferlen - 1, META_TEMPLATE,
1392 agent, xml_meta_longdesc, xml_meta_shortdesc, meta_param);
1394 xmlFree(xml_meta_longdesc);
1395 xmlFree(xml_meta_shortdesc);
1397 free(meta_shortdesc);
1398 free(meta_longdesc);
1415 stonith_api_query(
stonith_t * stonith,
int call_options,
const char *target,
1418 int rc = 0, lpc = 0, max = 0;
1420 xmlNode *data = NULL;
1421 xmlNode *output = NULL;
1422 xmlXPathObjectPtr xpathObj = NULL;
1424 CRM_CHECK(devices != NULL,
return -EINVAL);
1438 max = numXpathResults(xpathObj);
1440 for (lpc = 0; lpc < max; lpc++) {
1445 xmlChar *match_path = xmlGetNodePath(match);
1447 crm_info(
"%s[%d] = %s",
"//@agent", lpc, match_path);
1465 const char *action,
const char *victim,
int timeout, xmlNode ** output)
1468 xmlNode *data = NULL;
1483 stonith_api_list(
stonith_t * stonith,
int call_options,
const char *
id,
char **list_info,
1487 xmlNode *output = NULL;
1489 rc = stonith_api_call(stonith, call_options,
id,
"list", NULL, timeout, &output);
1491 if (output && list_info) {
1492 const char *list_str;
1497 *list_info = strdup(list_str);
1509 stonith_api_monitor(
stonith_t * stonith,
int call_options,
const char *
id,
int timeout)
1511 return stonith_api_call(stonith, call_options,
id,
"monitor", NULL, timeout, NULL);
1515 stonith_api_status(
stonith_t * stonith,
int call_options,
const char *
id,
const char *port,
1518 return stonith_api_call(stonith, call_options,
id,
"status", port, timeout, NULL);
1522 stonith_api_fence(
stonith_t * stonith,
int call_options,
const char *node,
const char *action,
1523 int timeout,
int tolerance)
1526 xmlNode *data = NULL;
1541 stonith_api_confirm(
stonith_t * stonith,
int call_options,
const char *target)
1543 return stonith_api_fence(stonith, call_options |
st_opt_manual_ack, target,
"off", 0, 0);
1547 stonith_api_history(
stonith_t * stonith,
int call_options,
const char *node,
1551 xmlNode *data = NULL;
1552 xmlNode *output = NULL;
1570 for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
1598 char buffer[FILENAME_MAX + 1];
1601 rc = stat(buffer, &prop);
1602 if (rc >= 0 && S_ISREG(prop.st_mode)) {
1615 #if HAVE_STONITH_STONITH_H
1617 Stonith *stonith_obj = NULL;
1619 static gboolean need_init = TRUE;
1620 static Stonith *(*st_new_fn) (
const char *) = NULL;
1621 static void (*st_del_fn) (Stonith *) = NULL;
1632 if (lha_agents_lib && st_new_fn && st_del_fn) {
1633 stonith_obj = (*st_new_fn) (agent);
1635 (*st_del_fn) (stonith_obj);
1646 crm_err(
"No such device: %s", agent);
1652 stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
1658 CRM_CHECK(a_client->event != NULL && b_client->event != NULL,
return 0);
1659 rc = strcmp(a_client->event, b_client->event);
1661 if (a_client->notify == NULL || b_client->notify == NULL) {
1664 }
else if (a_client->notify == b_client->notify) {
1667 }
else if (((
long)a_client->notify) < ((
long)b_client->notify)) {
1668 crm_err(
"callbacks for %s are not equal: %p vs. %p",
1669 a_client->event, a_client->notify, b_client->notify);
1672 crm_err(
"callbacks for %s are not equal: %p vs. %p",
1673 a_client->event, a_client->notify, b_client->notify);
1693 crm_trace(
"Sending call options: %.8lx, %d", (
long)call_options, call_options);
1704 stonith_destroy_op_callback(gpointer data)
1708 if (blob->timer && blob->timer->ref > 0) {
1709 g_source_remove(blob->timer->ref);
1716 stonith_api_signoff(
stonith_t * stonith)
1720 crm_debug(
"Signing out of the STONITH Service");
1722 if (native->source != NULL) {
1725 native->source = NULL;
1728 }
else if (native->ipc) {
1737 free(native->token); native->token = NULL;
1743 stonith_api_signon(
stonith_t * stonith,
const char *name,
int *stonith_fd)
1750 .destroy = stonith_connection_destroy
1753 crm_trace(
"Connecting command channel");
1762 }
else if (native->ipc) {
1763 crm_perror(LOG_ERR,
"Connection to STONITH manager failed");
1774 if (native->ipc == NULL) {
1775 crm_debug(
"Could not connect to the Stonith API");
1780 xmlNode *reply = NULL;
1789 crm_perror(LOG_DEBUG,
"Couldn't complete registration with the fencing API: %d", rc);
1792 }
else if (reply == NULL) {
1793 crm_err(
"Did not receive registration reply");
1801 crm_err(
"Invalid registration message: %s", msg_type);
1805 }
else if (tmp_ticket == NULL) {
1806 crm_err(
"No registration token provided");
1811 crm_trace(
"Obtained registration token: %s", tmp_ticket);
1812 native->token = strdup(tmp_ticket);
1822 #if HAVE_MSGFROMIPC_TIMEOUT
1825 crm_debug(
"Connection to STONITH successful");
1835 stonith_set_notification(
stonith_t * stonith,
const char *callback,
int enabled)
1852 crm_perror(LOG_DEBUG,
"Couldn't register for fencing notifications: %d", rc);
1864 stonith_api_add_notification(
stonith_t * stonith,
const char *event,
1867 GList *list_item = NULL;
1872 crm_trace(
"Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
1875 new_client->event = event;
1876 new_client->notify = callback;
1878 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1880 if (list_item != NULL) {
1881 crm_warn(
"Callback already present");
1886 private->notify_list = g_list_append(private->notify_list, new_client);
1888 stonith_set_notification(stonith, event, 1);
1890 crm_trace(
"Callback added (%d)", g_list_length(private->notify_list));
1896 stonith_api_del_notification(
stonith_t * stonith,
const char *event)
1898 GList *list_item = NULL;
1902 crm_debug(
"Removing callback for %s events", event);
1906 new_client->event = event;
1907 new_client->notify = NULL;
1909 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1911 stonith_set_notification(stonith, event, 0);
1913 if (list_item != NULL) {
1916 private->notify_list = g_list_remove(private->notify_list, list_client);
1929 stonith_async_timeout_handler(gpointer data)
1946 struct timer_rec_s *async_timer = callback->timer;
1953 async_timer = calloc(1,
sizeof(
struct timer_rec_s));
1954 callback->timer = async_timer;
1962 async_timer->
timeout = (timeout + 60) * 1000;
1963 if (async_timer->
ref) {
1964 g_source_remove(async_timer->
ref);
1967 g_timeout_add(async_timer->
timeout, stonith_async_timeout_handler, async_timer);
1971 update_callback_timeout(
int call_id,
int timeout,
stonith_t * st)
1976 callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1977 if (!callback || !callback->allow_timeout_updates) {
1981 set_callback_timeout(callback, st, call_id, timeout);
1985 invoke_callback(
stonith_t * st,
int call_id,
int rc,
void *userdata,
1994 callback(st, &data);
1998 stonith_api_add_callback(
stonith_t * stonith,
int call_id,
int timeout,
int options,
1999 void *user_data,
const char *callback_name,
2005 CRM_CHECK(stonith != NULL,
return -EINVAL);
2010 private->op_callback = callback;
2012 }
else if (call_id < 0) {
2015 invoke_callback(stonith, call_id, call_id, user_data, callback);
2023 blob->id = callback_name;
2025 blob->user_data = user_data;
2026 blob->callback = callback;
2030 set_callback_timeout(blob, stonith, call_id, timeout);
2033 g_hash_table_insert(private->stonith_op_callback_table, GINT_TO_POINTER(call_id), blob);
2034 crm_trace(
"Added callback to %s for call %d", callback_name, call_id);
2040 stonith_api_del_callback(
stonith_t * stonith,
int call_id,
bool all_callbacks)
2044 if (all_callbacks) {
2045 private->op_callback = NULL;
2046 g_hash_table_destroy(private->stonith_op_callback_table);
2047 private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2049 stonith_destroy_op_callback);
2051 }
else if (call_id == 0) {
2052 private->op_callback = NULL;
2055 g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2061 stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
2063 int call = GPOINTER_TO_INT(key);
2074 if (private->stonith_op_callback_table == NULL) {
2077 return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
2092 local_blob.id = NULL;
2093 local_blob.callback = NULL;
2094 local_blob.user_data = NULL;
2095 local_blob.only_success = FALSE;
2104 blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2110 stonith_api_del_callback(stonith, call_id, FALSE);
2113 crm_trace(
"No callback found for call %d", call_id);
2114 local_blob.callback = NULL;
2117 if (local_blob.callback != NULL && (rc ==
pcmk_ok || local_blob.only_success == FALSE)) {
2118 crm_trace(
"Invoking callback %s for call %d",
crm_str(local_blob.id), call_id);
2119 invoke_callback(stonith, call_id, rc, local_blob.user_data, local_blob.callback);
2121 }
else if (private->op_callback == NULL && rc !=
pcmk_ok) {
2126 if (private->op_callback != NULL) {
2127 crm_trace(
"Invoking global callback for call %d", call_id);
2128 invoke_callback(stonith, call_id, rc, NULL, private->op_callback);
2153 xml_to_event(xmlNode * msg)
2177 crm_err(
"No data for %s event", ntype);
2203 stonith_send_notification(gpointer data, gpointer user_data)
2205 struct notify_blob_s *blob = user_data;
2208 const char *
event = NULL;
2210 if (blob->xml == NULL) {
2211 crm_warn(
"Skipping callback - NULL message");
2217 if (entry == NULL) {
2218 crm_warn(
"Skipping callback - NULL callback client");
2221 }
else if (entry->notify == NULL) {
2222 crm_warn(
"Skipping callback - NULL callback");
2226 crm_trace(
"Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
2230 st_event = xml_to_event(blob->xml);
2232 crm_trace(
"Invoking callback for %p/%s event...", entry, event);
2233 entry->notify(blob->stonith, st_event);
2236 event_free(st_event);
2241 int call_options,
int timeout)
2247 xmlNode *op_msg = NULL;
2248 xmlNode *op_reply = NULL;
2256 if (output_data != NULL) {
2257 *output_data = NULL;
2261 crm_err(
"No operation specified");
2281 if (op_msg == NULL) {
2286 crm_trace(
"Sending %s message to STONITH service, Timeout: %ds", op, timeout);
2288 rc =
crm_ipc_send(native->ipc, op_msg, ipc_flags, 1000 * (timeout + 60), &op_reply);
2292 crm_perror(LOG_ERR,
"Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
2299 if (!(call_options & st_opt_sync_call)) {
2310 if (reply_id == stonith->
call_id) {
2311 crm_trace(
"Syncronous reply %d received", reply_id);
2321 *output_data = op_reply;
2325 }
else if (reply_id <= 0) {
2326 crm_err(
"Received bad reply: No id set");
2332 crm_err(
"Received bad reply: %d (wanted %d)", reply_id, stonith->
call_id);
2340 crm_err(
"STONITH disconnected");
2352 gboolean stay_connected = TRUE;
2368 stay_connected = FALSE;
2372 return stay_connected;
2378 const char *
type = NULL;
2379 struct notify_blob_s blob;
2385 private = st->private;
2389 if (blob.xml == NULL) {
2390 crm_warn(
"Received a NULL msg from STONITH service: %s.", buffer);
2396 crm_trace(
"Activating %s callbacks...", type);
2402 g_list_foreach(private->notify_list, stonith_send_notification, &blob);
2410 update_callback_timeout(call_id, timeout, st);
2412 crm_err(
"Unknown message type: %s", type);
2428 crm_trace(
"Disconnecting %p first", stonith);
2435 crm_trace(
"Removing %d callbacks", g_hash_table_size(private->stonith_op_callback_table));
2436 g_hash_table_destroy(private->stonith_op_callback_table);
2438 crm_trace(
"Destroying %d notification clients", g_list_length(private->notify_list));
2439 g_list_free_full(private->notify_list, free);
2442 free(stonith->
cmds);
2467 new_stonith = calloc(1,
sizeof(
stonith_t));
2469 new_stonith->
private =
private;
2471 private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2472 NULL, stonith_destroy_op_callback);
2473 private->notify_list = NULL;
2481 new_stonith->
cmds->
free = stonith_api_free;
2485 new_stonith->
cmds->
list = stonith_api_list;
2487 new_stonith->
cmds->
status = stonith_api_status;
2488 new_stonith->
cmds->
fence = stonith_api_fence;
2493 new_stonith->
cmds->
metadata = stonith_api_device_metadata;
2495 new_stonith->
cmds->
query = stonith_api_query;
2520 p->
key = strdup(key);
2523 p->
value = strdup(value);
2527 while (end && end->
next) {
2558 #define api_log_open() openlog("stonith-api", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON)
2559 #define api_log(level, fmt, args...) syslog(level, "%s: "fmt, __FUNCTION__, args)
2565 const char *action =
"reboot";
2576 api_log(LOG_ERR,
"Connection failed, could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2580 if (uname != NULL) {
2581 name = strdup(uname);
2583 }
else if (nodeid > 0) {
2593 rc = st->
cmds->
fence(st, opts, name, action, timeout, 0);
2595 api_log(LOG_ERR,
"Could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2597 api_log(LOG_NOTICE,
"Node %u/%s kicked: %s ", nodeid, uname, action);
2629 if (uname != NULL) {
2630 name = strdup(uname);
2632 }
else if (nodeid > 0) {
2642 rc = st->
cmds->
history(st, opts, name, &history, 120);
2644 for (hp = history; hp; hp = hp->
next) {
2661 api_log(LOG_INFO,
"Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
2663 api_log(LOG_ERR,
"Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname,
pcmk_strerror(rc), rc);
2673 api_log(LOG_INFO,
"Node %u/%s last kicked at: %ld", nodeid, uname, (
long int)when);
2679 #if HAVE_STONITH_STONITH_H
2680 # include <pils/plugin.h>
2682 const char *i_hate_pils(
int rc);
2687 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
int stonith_action_execute(stonith_action_t *action, int *agent_result, char **output)
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.
int(* metadata)(stonith_t *st, int options, const char *device, const char *namespace, char **output, int timeout)
Get the metadata documentation for a resource.
stonith_key_value_t * stonith_key_value_add(stonith_key_value_t *head, const char *key, const char *value)
void stonith_dump_pending_callbacks(stonith_t *stonith)
#define crm_log_output(level, prefix, output)
int crm_ipc_get_fd(crm_ipc_t *client)
const char * pcmk_strerror(int rc)
xmlNode * create_device_registration_xml(const char *id, const char *namespace, const char *agent, stonith_key_value_t *params, const char *rsc_provides)
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
void stonith_key_value_freeall(stonith_key_value_t *head, int keys, int values)
char * crm_element_value_copy(xmlNode *data, const char *name)
#define F_STONITH_TIMEOUT
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.
#define STONITH_OP_LEVEL_DEL
#define STONITH_OP_DEVICE_ADD
char * crm_meta_name(const char *field)
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 **)
int(* remove_callback)(stonith_t *st, int call_id, bool all_callbacks)
Remove a registered callback for a given call id.
time_t stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
void hash2field(gpointer key, gpointer value, gpointer user_data)
#define XML_ATTR_STONITH_TARGET_PATTERN
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_warn(fmt, args...)
#define STONITH_ATTR_HOSTARG
bool stonith_dispatch(stonith_t *st)
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__
#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)
#define T_STONITH_NOTIFY_DISCONNECT
const char * get_stonith_provider(const char *agent, const char *provider)
#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
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
void stonith_api_delete(stonith_t *stonith)
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
GPid stonith_action_execute_async(stonith_action_t *action, void *userdata, void(*done)(GPid pid, int rc, const char *output, gpointer user_data))
struct stonith_private_s stonith_private_t
gboolean is_redhat_agent(const char *agent)
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)
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
#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)
stonith_t * stonith_api_new(void)
#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.
#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)
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
CRM_TRACE_INIT_DATA(stonith)
#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
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)
#define F_STONITH_TOLERANCE