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(gpointer key, gpointer value, gpointer user_data)
401 char **args = user_data;
406 if (strstr(key,
"pcmk_")) {
415 len += strlen(value);
417 last = strlen(*args);
420 *args = realloc_safe(*args, last + len);
421 crm_trace(
"Appending: %s=%s", (
char *)key, (
char *)value);
422 sprintf((*args) + last,
"%s=%s\n", (
char *)key, (
char *)value);
426 append_const_arg(
const char *key,
const char *value,
char **arg_list)
430 char *glib_sucks_key = strdup(key);
431 char *glib_sucks_value = strdup(value);
433 append_arg(glib_sucks_key, glib_sucks_value, arg_list);
435 free(glib_sucks_value);
436 free(glib_sucks_key);
441 append_host_specific_args(
const char *victim,
const char *map, GHashTable * params,
char **arg_list)
444 int last = 0, lpc = 0, max = 0;
448 crm_debug(
"Using default arg map: port=uname");
449 append_const_arg(
"port", victim, arg_list);
454 crm_debug(
"Processing arg map: %s", map);
455 for (; lpc < max + 1; lpc++) {
456 if (isalpha(map[lpc])) {
459 }
else if (map[lpc] ==
'=' || map[lpc] ==
':') {
461 name = calloc(1, 1 + lpc - last);
462 memcpy(name, map + last, lpc - last);
466 }
else if (map[lpc] == 0 || map[lpc] ==
',' || isspace(map[lpc])) {
468 const char *value = NULL;
470 param = calloc(1, 1 + lpc - last);
471 memcpy(param, map + last, lpc - last);
476 crm_err(
"Misparsed '%s', found '%s' without a name", map, param);
486 value = g_hash_table_lookup(params, key);
491 crm_debug(
"Setting '%s'='%s' (%s) for %s", name, value, param, victim);
492 append_const_arg(name, value, arg_list);
495 crm_err(
"No node attribute '%s' for '%s'", name, victim);
505 }
else if (isspace(map[lpc])) {
513 make_args(
const char *agent,
const char *action,
const char *victim,
uint32_t victim_nodeid, GHashTable * device_args,
514 GHashTable * port_map)
517 char *arg_list = NULL;
518 const char *value = NULL;
519 const char *_action = action;
524 snprintf(buffer, 511,
"pcmk_%s_action", action);
526 value = g_hash_table_lookup(device_args, buffer);
529 if (value == NULL && device_args) {
531 snprintf(buffer, 511,
"pcmk_%s_cmd", action);
532 value = g_hash_table_lookup(device_args, buffer);
535 if (value == NULL && device_args &&
safe_str_eq(action,
"off")) {
537 value = g_hash_table_lookup(device_args,
"pcmk_poweroff_action");
541 crm_info(
"Substituting action '%s' for requested operation '%s'", value, action);
546 if (victim && device_args) {
547 const char *alias = victim;
550 if (port_map && g_hash_table_lookup(port_map, victim)) {
551 alias = g_hash_table_lookup(port_map, victim);
557 append_const_arg(
"nodename", victim, &arg_list);
559 char nodeid_str[33] = { 0, };
560 if (snprintf(nodeid_str, 33,
"%u", (
unsigned int)victim_nodeid)) {
561 crm_info(
"For stonith action (%s) for victim %s, adding nodeid (%s) to parameters",
562 action, victim, nodeid_str);
563 append_const_arg(
"nodeid", nodeid_str, &arg_list);
571 }
else if (param == NULL) {
576 value = g_hash_table_lookup(device_args, param);
580 append_host_specific_args(alias, map, device_args, &arg_list);
588 value = g_hash_table_lookup(device_args, param);
592 if (value == NULL ||
safe_str_eq(value,
"dynamic")) {
593 crm_debug(
"Performing %s action for node '%s' as '%s=%s'", action, victim, param,
595 append_const_arg(param, alias, &arg_list);
600 g_hash_table_foreach(device_args, append_arg, &arg_list);
619 st_child_term(gpointer data)
624 crm_info(
"Child %d timed out, sending SIGTERM", track->pid);
625 track->timer_sigterm = 0;
626 track->last_timeout_signo = SIGTERM;
627 rc = kill(-track->pid, SIGTERM);
629 crm_perror(LOG_ERR,
"Couldn't send SIGTERM to %d", track->pid);
635 st_child_kill(gpointer data)
640 crm_info(
"Child %d timed out, sending SIGKILL", track->pid);
641 track->timer_sigkill = 0;
642 track->last_timeout_signo = SIGKILL;
643 rc = kill(-track->pid, SIGKILL);
645 crm_perror(LOG_ERR,
"Couldn't send SIGKILL to %d", track->pid);
653 if (action->timer_sigterm > 0) {
654 g_source_remove(action->timer_sigterm);
655 action->timer_sigterm = 0;
657 if (action->timer_sigkill > 0) {
658 g_source_remove(action->timer_sigkill);
659 action->timer_sigkill = 0;
661 if (action->fd_stdout) {
662 close(action->fd_stdout);
663 action->fd_stdout = 0;
665 if (action->fd_stderr) {
666 close(action->fd_stderr);
667 action->fd_stderr = 0;
669 free(action->output);
670 action->output = NULL;
672 action->error = NULL;
675 action->last_timeout_signo = 0;
681 stonith_action_clear_tracking_data(action);
684 free(action->action);
685 free(action->victim);
689 #define FAILURE_MAX_RETRIES 2
695 int timeout, GHashTable * device_args, GHashTable * port_map)
700 crm_debug(
"Initiating action %s for agent %s (target=%s)", _action, agent, victim);
701 action->args = make_args(agent, _action, victim, victim_nodeid, device_args, port_map);
702 action->agent = strdup(agent);
703 action->action = strdup(_action);
705 action->victim = strdup(victim);
707 action->timeout = action->remaining_timeout = timeout;
712 const char *value = NULL;
714 snprintf(buffer, 511,
"pcmk_%s_retries", _action);
715 value = g_hash_table_lookup(device_args, buffer);
718 action->max_retries = atoi(value);
741 more = read(fd, buffer,
READ_MAX - 1);
747 output = realloc_safe(output, len + more + 1);
748 snprintf(output + len, more + 1,
"%s", buffer);
752 }
while (more == (
READ_MAX - 1) || (more < 0 && errno == EINTR));
760 int diff = time(NULL) - action->initial_start_time;
762 if (action->tries >= action->max_retries) {
763 crm_info(
"Attempted to execute agent %s (%s) the maximum number of times (%d) allowed",
764 action->agent, action->action, action->max_retries);
765 action->remaining_timeout = 0;
766 }
else if ((action->rc != -
ETIME) && diff < (action->timeout * 0.7)) {
769 action->remaining_timeout = action->timeout - diff;
771 action->remaining_timeout = 0;
773 return action->remaining_timeout ? TRUE : FALSE;
777 stonith_action_async_done(
mainloop_child_t * p, pid_t pid,
int core,
int signo,
int exitcode)
781 if (action->timer_sigterm > 0) {
782 g_source_remove(action->timer_sigterm);
783 action->timer_sigterm = 0;
785 if (action->timer_sigkill > 0) {
786 g_source_remove(action->timer_sigkill);
787 action->timer_sigkill = 0;
790 action->output = read_output(action->fd_stdout);
791 action->error = read_output(action->fd_stderr);
793 if (action->last_timeout_signo) {
795 crm_notice(
"Child process %d performing action '%s' timed out with signal %d",
796 pid, action->action, action->last_timeout_signo);
799 action->rc = -ECONNABORTED;
800 crm_notice(
"Child process %d performing action '%s' timed out with signal %d",
801 pid, action->action, signo);
804 crm_debug(
"Child process %d performing action '%s' exited with rc %d",
805 pid, action->action, exitcode);
810 if (action->error == NULL) {
813 }
else if (strstr(action->error,
"imed out")) {
815 exitcode = -ETIMEDOUT;
817 }
else if (strstr(action->error,
"Unrecognised action")) {
818 exitcode = -EOPNOTSUPP;
824 action->rc = exitcode;
827 log_action(action, pid);
829 if (action->rc !=
pcmk_ok && update_remaining_timeout(action)) {
830 int rc = internal_stonith_action_execute(action);
836 if (action->done_cb) {
837 action->done_cb(pid, action->rc, action->output, action->userdata);
840 stonith_action_destroy(action);
846 int pid, status = 0, len, rc = -EPROTO;
849 int p_read_fd, p_write_fd;
850 int c_read_fd, c_write_fd;
851 int c_stderr_fd, p_stderr_fd;
858 stonith_action_clear_tracking_data(action);
860 if (!action->tries) {
861 action->initial_start_time = time(NULL);
865 if (action->tries > 1) {
866 crm_info(
"Attempt %d to execute %s (%s). remaining timeout is %d",
867 action->tries, action->agent, action->action, action->remaining_timeout);
871 c_read_fd = c_write_fd = p_read_fd = p_write_fd = c_stderr_fd = p_stderr_fd = -1;
873 if (action->args == NULL || action->agent == NULL)
875 len = strlen(action->args);
889 p_stderr_fd = fd3[0];
890 c_stderr_fd = fd3[1];
905 if (dup(c_write_fd) < 0)
909 if (dup(c_stderr_fd) < 0)
913 if (dup(c_read_fd) < 0)
927 execlp(action->agent, action->agent, NULL);
933 ret = fcntl(p_read_fd, F_SETFL, fcntl(p_read_fd, F_GETFL, 0) | O_NONBLOCK);
935 crm_perror(LOG_NOTICE,
"Could not change the output of %s to be non-blocking",
938 ret = fcntl(p_stderr_fd, F_SETFL, fcntl(p_stderr_fd, F_GETFL, 0) | O_NONBLOCK);
940 crm_perror(LOG_NOTICE,
"Could not change the stderr of %s to be non-blocking",
946 ret = write(p_write_fd, action->args + total, len - total);
951 }
while (errno == EINTR && total < len);
954 crm_perror(LOG_ERR,
"Sent %d not %d bytes", total, len);
961 close(p_write_fd); p_write_fd = -1;
965 action->fd_stdout = p_read_fd;
966 action->fd_stderr = p_stderr_fd;
968 crm_trace(
"Op: %s on %s, pid: %d, timeout: %ds", action->action, action->agent, pid,
969 action->remaining_timeout);
970 action->last_timeout_signo = 0;
971 if (action->remaining_timeout) {
972 action->timer_sigterm =
973 g_timeout_add(1000 * action->remaining_timeout, st_child_term, action);
974 action->timer_sigkill =
975 g_timeout_add(1000 * (action->remaining_timeout + 5), st_child_kill, action);
977 crm_err(
"No timeout set for stonith operation %s with device %s",
978 action->action, action->agent);
988 int timeout = action->remaining_timeout + 1;
991 while (action->remaining_timeout < 0 || timeout > 0) {
992 p = waitpid(pid, &status, WNOHANG);
1001 int killrc = kill(-pid, SIGKILL);
1003 if (killrc && errno != ESRCH) {
1012 p = waitpid(pid, &status, 0);
1018 }
else if (p != pid) {
1019 crm_err(
"Waited for %d, got %d", pid, p);
1022 action->output = read_output(p_read_fd);
1023 action->error = read_output(p_stderr_fd);
1025 action->rc = -ECONNABORTED;
1027 log_action(action, pid);
1031 action->rc = -
ETIME;
1032 }
else if (WIFEXITED(status)) {
1033 crm_debug(
"result = %d", WEXITSTATUS(status));
1034 action->rc = -WEXITSTATUS(status);
1037 }
else if (WIFSIGNALED(status)) {
1038 crm_err(
"call %s for %s exited due to signal %d", action->action, action->agent,
1042 crm_err(
"call %s for %s returned unexpected status %#x",
1043 action->action, action->agent, status);
1049 if (p_read_fd >= 0) {
1052 if (p_write_fd >= 0) {
1055 if (p_stderr_fd >= 0) {
1059 if (c_read_fd >= 0) {
1062 if (c_write_fd >= 0) {
1065 if (c_stderr_fd >= 0) {
1075 void (*done) (GPid pid,
int rc,
const char *output,
1076 gpointer user_data))
1084 action->userdata = userdata;
1085 action->done_cb = done;
1088 rc = internal_stonith_action_execute(action);
1090 return rc < 0 ? rc : action->pid;
1103 rc = internal_stonith_action_execute(action);
1109 }
while (update_remaining_timeout(action));
1117 *agent_result = action->rc;
1120 *output = action->output;
1121 action->output = NULL;
1124 stonith_action_destroy(action);
1129 stonith_api_device_list(
stonith_t * stonith,
int call_options,
const char *
namespace,
1134 if (devices == NULL) {
1135 crm_err(
"Parameter error: stonith_api_device_list");
1140 if (
namespace == NULL ||
safe_str_eq(
"heartbeat",
namespace)) {
1141 #if HAVE_STONITH_STONITH_H
1142 static gboolean need_init = TRUE;
1144 char **entry = NULL;
1145 char **type_list = NULL;
1146 static char **(*type_list_fn) (void) = NULL;
1147 static void (*type_free_fn) (
char **) = NULL;
1159 type_list = (*type_list_fn) ();
1162 for (entry = type_list; entry != NULL && *entry; ++entry) {
1167 if (type_list && type_free_fn) {
1168 (*type_free_fn) (type_list);
1171 if (
namespace != NULL) {
1178 if (
namespace == NULL ||
safe_str_eq(
"redhat",
namespace)) {
1179 struct dirent **namelist;
1184 char buffer[FILENAME_MAX + 1];
1186 while (file_num--) {
1187 if (
'.' == namelist[file_num]->d_name[0]) {
1188 free(namelist[file_num]);
1193 free(namelist[file_num]);
1197 snprintf(buffer, FILENAME_MAX,
"%s/%s",
RH_STONITH_DIR, namelist[file_num]->d_name);
1198 if (stat(buffer, &prop) == 0 && S_ISREG(prop.st_mode)) {
1203 free(namelist[file_num]);
1212 #if HAVE_STONITH_STONITH_H
1213 static inline char *
1214 strdup_null(
const char *val)
1223 stonith_plugin(
int priority,
const char *fmt, ...)
__attribute__((__format__ (__printf__, 2, 3)));
1226 stonith_plugin(
int priority, const
char *format, ...)
1232 char *
string = NULL;
1234 va_start(ap, format);
1236 len = vasprintf (&
string, format, ap);
1247 stonith_api_device_metadata(
stonith_t * stonith,
int call_options,
const char *agent,
1248 const char *
namespace,
char **output,
int timeout)
1251 char *buffer = NULL;
1254 crm_trace(
"looking up %s/%s metadata", agent, provider);
1264 xmlNode *xml = NULL;
1265 xmlNode *actions = NULL;
1266 xmlXPathObject *xpathObj = NULL;
1268 if (exec_rc < 0 || rc != 0 || buffer == NULL) {
1269 crm_warn(
"Could not obtain metadata for %s", agent);
1277 crm_warn(
"Metadata for %s is invalid", agent);
1283 if (numXpathResults(xpathObj) > 0) {
1290 xpathObj =
xpath_search(xml,
"//action[@name='stop']");
1291 if (numXpathResults(xpathObj) <= 0) {
1292 xmlNode *tmp = NULL;
1306 xpathObj =
xpath_search(xml,
"//parameter[@name='port']");
1307 if (numXpathResults(xpathObj) > 0) {
1323 #if !HAVE_STONITH_STONITH_H
1327 static const char *no_parameter_info =
"<!-- no value -->";
1329 Stonith *stonith_obj = NULL;
1331 static gboolean need_init = TRUE;
1332 static Stonith *(*st_new_fn) (
const char *) = NULL;
1333 static const char *(*st_info_fn) (Stonith *, int) = NULL;
1334 static void (*st_del_fn) (Stonith *) = NULL;
1335 static void (*st_log_fn) (Stonith *, PILLogFun) = NULL;
1352 if (lha_agents_lib && st_new_fn && st_del_fn && st_info_fn && st_log_fn) {
1353 char *xml_meta_longdesc = NULL;
1354 char *xml_meta_shortdesc = NULL;
1356 char *meta_param = NULL;
1357 char *meta_longdesc = NULL;
1358 char *meta_shortdesc = NULL;
1360 stonith_obj = (*st_new_fn) (agent);
1362 (*st_log_fn) (stonith_obj, (PILLogFun) & stonith_plugin);
1363 meta_longdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEDESCR));
1364 if (meta_longdesc == NULL) {
1365 crm_warn(
"no long description in %s's metadata.", agent);
1366 meta_longdesc = strdup(no_parameter_info);
1369 meta_shortdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEID));
1370 if (meta_shortdesc == NULL) {
1371 crm_warn(
"no short description in %s's metadata.", agent);
1372 meta_shortdesc = strdup(no_parameter_info);
1375 meta_param = strdup_null((*st_info_fn) (stonith_obj, ST_CONF_XML));
1376 if (meta_param == NULL) {
1377 crm_warn(
"no list of parameters in %s's metadata.", agent);
1378 meta_param = strdup(no_parameter_info);
1380 (*st_del_fn) (stonith_obj);
1386 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_longdesc);
1387 xml_meta_shortdesc =
1388 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_shortdesc);
1390 bufferlen = strlen(META_TEMPLATE) + strlen(agent)
1391 + strlen(xml_meta_longdesc) + strlen(xml_meta_shortdesc)
1392 + strlen(meta_param) + 1;
1394 buffer = calloc(1, bufferlen);
1395 snprintf(buffer, bufferlen - 1, META_TEMPLATE,
1396 agent, xml_meta_longdesc, xml_meta_shortdesc, meta_param);
1398 xmlFree(xml_meta_longdesc);
1399 xmlFree(xml_meta_shortdesc);
1401 free(meta_shortdesc);
1402 free(meta_longdesc);
1419 stonith_api_query(
stonith_t * stonith,
int call_options,
const char *target,
1422 int rc = 0, lpc = 0, max = 0;
1424 xmlNode *data = NULL;
1425 xmlNode *output = NULL;
1426 xmlXPathObjectPtr xpathObj = NULL;
1428 CRM_CHECK(devices != NULL,
return -EINVAL);
1442 max = numXpathResults(xpathObj);
1444 for (lpc = 0; lpc < max; lpc++) {
1449 xmlChar *match_path = xmlGetNodePath(match);
1451 crm_info(
"%s[%d] = %s",
"//@agent", lpc, match_path);
1469 const char *action,
const char *victim,
int timeout, xmlNode ** output)
1472 xmlNode *data = NULL;
1487 stonith_api_list(
stonith_t * stonith,
int call_options,
const char *
id,
char **list_info,
1491 xmlNode *output = NULL;
1493 rc = stonith_api_call(stonith, call_options,
id,
"list", NULL, timeout, &output);
1495 if (output && list_info) {
1496 const char *list_str;
1501 *list_info = strdup(list_str);
1513 stonith_api_monitor(
stonith_t * stonith,
int call_options,
const char *
id,
int timeout)
1515 return stonith_api_call(stonith, call_options,
id,
"monitor", NULL, timeout, NULL);
1519 stonith_api_status(
stonith_t * stonith,
int call_options,
const char *
id,
const char *port,
1522 return stonith_api_call(stonith, call_options,
id,
"status", port, timeout, NULL);
1526 stonith_api_fence(
stonith_t * stonith,
int call_options,
const char *node,
const char *action,
1527 int timeout,
int tolerance)
1530 xmlNode *data = NULL;
1545 stonith_api_confirm(
stonith_t * stonith,
int call_options,
const char *target)
1547 return stonith_api_fence(stonith, call_options |
st_opt_manual_ack, target,
"off", 0, 0);
1551 stonith_api_history(
stonith_t * stonith,
int call_options,
const char *node,
1555 xmlNode *data = NULL;
1556 xmlNode *output = NULL;
1574 for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
1602 char buffer[FILENAME_MAX + 1];
1605 rc = stat(buffer, &prop);
1606 if (rc >= 0 && S_ISREG(prop.st_mode)) {
1619 #if HAVE_STONITH_STONITH_H
1621 Stonith *stonith_obj = NULL;
1623 static gboolean need_init = TRUE;
1624 static Stonith *(*st_new_fn) (
const char *) = NULL;
1625 static void (*st_del_fn) (Stonith *) = NULL;
1636 if (lha_agents_lib && st_new_fn && st_del_fn) {
1637 stonith_obj = (*st_new_fn) (agent);
1639 (*st_del_fn) (stonith_obj);
1650 crm_err(
"No such device: %s", agent);
1656 stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
1662 CRM_CHECK(a_client->event != NULL && b_client->event != NULL,
return 0);
1663 rc = strcmp(a_client->event, b_client->event);
1665 if (a_client->notify == NULL || b_client->notify == NULL) {
1668 }
else if (a_client->notify == b_client->notify) {
1671 }
else if (((
long)a_client->notify) < ((
long)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);
1676 crm_err(
"callbacks for %s are not equal: %p vs. %p",
1677 a_client->event, a_client->notify, b_client->notify);
1697 crm_trace(
"Sending call options: %.8lx, %d", (
long)call_options, call_options);
1708 stonith_destroy_op_callback(gpointer data)
1712 if (blob->timer && blob->timer->ref > 0) {
1713 g_source_remove(blob->timer->ref);
1720 stonith_api_signoff(
stonith_t * stonith)
1724 crm_debug(
"Signing out of the STONITH Service");
1726 if (native->source != NULL) {
1729 native->source = NULL;
1732 }
else if (native->ipc) {
1741 free(native->token); native->token = NULL;
1747 stonith_api_signon(
stonith_t * stonith,
const char *name,
int *stonith_fd)
1754 .destroy = stonith_connection_destroy
1757 crm_trace(
"Connecting command channel");
1766 }
else if (native->ipc) {
1767 crm_perror(LOG_ERR,
"Connection to STONITH manager failed");
1778 if (native->ipc == NULL) {
1779 crm_debug(
"Could not connect to the Stonith API");
1784 xmlNode *reply = NULL;
1793 crm_perror(LOG_DEBUG,
"Couldn't complete registration with the fencing API: %d", rc);
1796 }
else if (reply == NULL) {
1797 crm_err(
"Did not receive registration reply");
1805 crm_err(
"Invalid registration message: %s", msg_type);
1809 }
else if (tmp_ticket == NULL) {
1810 crm_err(
"No registration token provided");
1815 crm_trace(
"Obtained registration token: %s", tmp_ticket);
1816 native->token = strdup(tmp_ticket);
1826 #if HAVE_MSGFROMIPC_TIMEOUT
1829 crm_debug(
"Connection to STONITH successful");
1839 stonith_set_notification(
stonith_t * stonith,
const char *callback,
int enabled)
1856 crm_perror(LOG_DEBUG,
"Couldn't register for fencing notifications: %d", rc);
1868 stonith_api_add_notification(
stonith_t * stonith,
const char *event,
1871 GList *list_item = NULL;
1876 crm_trace(
"Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
1879 new_client->event = event;
1880 new_client->notify = callback;
1882 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1884 if (list_item != NULL) {
1885 crm_warn(
"Callback already present");
1890 private->notify_list = g_list_append(private->notify_list, new_client);
1892 stonith_set_notification(stonith, event, 1);
1894 crm_trace(
"Callback added (%d)", g_list_length(private->notify_list));
1900 stonith_api_del_notification(
stonith_t * stonith,
const char *event)
1902 GList *list_item = NULL;
1906 crm_debug(
"Removing callback for %s events", event);
1910 new_client->event = event;
1911 new_client->notify = NULL;
1913 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1915 stonith_set_notification(stonith, event, 0);
1917 if (list_item != NULL) {
1920 private->notify_list = g_list_remove(private->notify_list, list_client);
1933 stonith_async_timeout_handler(gpointer data)
1950 struct timer_rec_s *async_timer = callback->timer;
1957 async_timer = calloc(1,
sizeof(
struct timer_rec_s));
1958 callback->timer = async_timer;
1966 async_timer->
timeout = (timeout + 60) * 1000;
1967 if (async_timer->
ref) {
1968 g_source_remove(async_timer->
ref);
1971 g_timeout_add(async_timer->
timeout, stonith_async_timeout_handler, async_timer);
1975 update_callback_timeout(
int call_id,
int timeout,
stonith_t * st)
1980 callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1981 if (!callback || !callback->allow_timeout_updates) {
1985 set_callback_timeout(callback, st, call_id, timeout);
1989 invoke_callback(
stonith_t * st,
int call_id,
int rc,
void *userdata,
1998 callback(st, &data);
2002 stonith_api_add_callback(
stonith_t * stonith,
int call_id,
int timeout,
int options,
2003 void *user_data,
const char *callback_name,
2009 CRM_CHECK(stonith != NULL,
return -EINVAL);
2014 private->op_callback = callback;
2016 }
else if (call_id < 0) {
2019 invoke_callback(stonith, call_id, call_id, user_data, callback);
2027 blob->id = callback_name;
2029 blob->user_data = user_data;
2030 blob->callback = callback;
2034 set_callback_timeout(blob, stonith, call_id, timeout);
2037 g_hash_table_insert(private->stonith_op_callback_table, GINT_TO_POINTER(call_id), blob);
2038 crm_trace(
"Added callback to %s for call %d", callback_name, call_id);
2044 stonith_api_del_callback(
stonith_t * stonith,
int call_id,
bool all_callbacks)
2048 if (all_callbacks) {
2049 private->op_callback = NULL;
2050 g_hash_table_destroy(private->stonith_op_callback_table);
2051 private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2053 stonith_destroy_op_callback);
2055 }
else if (call_id == 0) {
2056 private->op_callback = NULL;
2059 g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2065 stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
2067 int call = GPOINTER_TO_INT(key);
2078 if (private->stonith_op_callback_table == NULL) {
2081 return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
2096 local_blob.id = NULL;
2097 local_blob.callback = NULL;
2098 local_blob.user_data = NULL;
2099 local_blob.only_success = FALSE;
2108 blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2114 stonith_api_del_callback(stonith, call_id, FALSE);
2117 crm_trace(
"No callback found for call %d", call_id);
2118 local_blob.callback = NULL;
2121 if (local_blob.callback != NULL && (rc ==
pcmk_ok || local_blob.only_success == FALSE)) {
2122 crm_trace(
"Invoking callback %s for call %d",
crm_str(local_blob.id), call_id);
2123 invoke_callback(stonith, call_id, rc, local_blob.user_data, local_blob.callback);
2125 }
else if (private->op_callback == NULL && rc !=
pcmk_ok) {
2130 if (private->op_callback != NULL) {
2131 crm_trace(
"Invoking global callback for call %d", call_id);
2132 invoke_callback(stonith, call_id, rc, NULL, private->op_callback);
2157 xml_to_event(xmlNode * msg)
2181 crm_err(
"No data for %s event", ntype);
2207 stonith_send_notification(gpointer data, gpointer user_data)
2209 struct notify_blob_s *blob = user_data;
2212 const char *
event = NULL;
2214 if (blob->xml == NULL) {
2215 crm_warn(
"Skipping callback - NULL message");
2221 if (entry == NULL) {
2222 crm_warn(
"Skipping callback - NULL callback client");
2225 }
else if (entry->notify == NULL) {
2226 crm_warn(
"Skipping callback - NULL callback");
2230 crm_trace(
"Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
2234 st_event = xml_to_event(blob->xml);
2236 crm_trace(
"Invoking callback for %p/%s event...", entry, event);
2237 entry->notify(blob->stonith, st_event);
2240 event_free(st_event);
2245 int call_options,
int timeout)
2251 xmlNode *op_msg = NULL;
2252 xmlNode *op_reply = NULL;
2260 if (output_data != NULL) {
2261 *output_data = NULL;
2265 crm_err(
"No operation specified");
2285 if (op_msg == NULL) {
2290 crm_trace(
"Sending %s message to STONITH service, Timeout: %ds", op, timeout);
2292 rc =
crm_ipc_send(native->ipc, op_msg, ipc_flags, 1000 * (timeout + 60), &op_reply);
2296 crm_perror(LOG_ERR,
"Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
2303 if (!(call_options & st_opt_sync_call)) {
2314 if (reply_id == stonith->
call_id) {
2315 crm_trace(
"Synchronous reply %d received", reply_id);
2325 *output_data = op_reply;
2329 }
else if (reply_id <= 0) {
2330 crm_err(
"Received bad reply: No id set");
2336 crm_err(
"Received bad reply: %d (wanted %d)", reply_id, stonith->
call_id);
2344 crm_err(
"STONITH disconnected");
2356 gboolean stay_connected = TRUE;
2372 stay_connected = FALSE;
2376 return stay_connected;
2382 const char *
type = NULL;
2383 struct notify_blob_s blob;
2389 private = st->private;
2393 if (blob.xml == NULL) {
2394 crm_warn(
"Received a NULL msg from STONITH service: %s.", buffer);
2400 crm_trace(
"Activating %s callbacks...", type);
2406 g_list_foreach(private->notify_list, stonith_send_notification, &blob);
2414 update_callback_timeout(call_id, timeout, st);
2416 crm_err(
"Unknown message type: %s", type);
2432 crm_trace(
"Disconnecting %p first", stonith);
2439 crm_trace(
"Removing %d callbacks", g_hash_table_size(private->stonith_op_callback_table));
2440 g_hash_table_destroy(private->stonith_op_callback_table);
2442 crm_trace(
"Destroying %d notification clients", g_list_length(private->notify_list));
2443 g_list_free_full(private->notify_list, free);
2446 free(stonith->
cmds);
2471 new_stonith = calloc(1,
sizeof(
stonith_t));
2473 new_stonith->
private =
private;
2475 private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2476 NULL, stonith_destroy_op_callback);
2477 private->notify_list = NULL;
2485 new_stonith->
cmds->
free = stonith_api_free;
2489 new_stonith->
cmds->
list = stonith_api_list;
2491 new_stonith->
cmds->
status = stonith_api_status;
2492 new_stonith->
cmds->
fence = stonith_api_fence;
2497 new_stonith->
cmds->
metadata = stonith_api_device_metadata;
2499 new_stonith->
cmds->
query = stonith_api_query;
2524 p->
key = strdup(key);
2527 p->
value = strdup(value);
2531 while (end && end->
next) {
2562 #define api_log_open() openlog("stonith-api", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON)
2563 #define api_log(level, fmt, args...) syslog(level, "%s: "fmt, __FUNCTION__, args)
2569 const char *action =
"reboot";
2580 api_log(LOG_ERR,
"Connection failed, could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2584 if (uname != NULL) {
2585 name = strdup(uname);
2587 }
else if (nodeid > 0) {
2597 rc = st->
cmds->
fence(st, opts, name, action, timeout, 0);
2599 api_log(LOG_ERR,
"Could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2601 api_log(LOG_NOTICE,
"Node %u/%s kicked: %s ", nodeid, uname, action);
2633 if (uname != NULL) {
2634 name = strdup(uname);
2636 }
else if (nodeid > 0) {
2646 rc = st->
cmds->
history(st, opts, name, &history, 120);
2648 for (hp = history; hp; hp = hp->
next) {
2665 api_log(LOG_INFO,
"Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
2667 api_log(LOG_ERR,
"Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname,
pcmk_strerror(rc), rc);
2677 api_log(LOG_INFO,
"Node %u/%s last kicked at: %ld", nodeid, uname, (
long int)when);
2683 #if HAVE_STONITH_STONITH_H
2684 # include <pils/plugin.h>
2686 const char *i_hate_pils(
int rc);
2691 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)
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
#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