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);
917 ret = fcntl(p_read_fd, F_SETFL, fcntl(p_read_fd, F_GETFL, 0) | O_NONBLOCK);
919 crm_perror(LOG_NOTICE,
"Could not change the output of %s to be non-blocking",
922 ret = fcntl(p_stderr_fd, F_SETFL, fcntl(p_stderr_fd, F_GETFL, 0) | O_NONBLOCK);
924 crm_perror(LOG_NOTICE,
"Could not change the stderr of %s to be non-blocking",
930 ret = write(p_write_fd, action->args + total, len - total);
935 }
while (errno == EINTR && total < len);
938 crm_perror(LOG_ERR,
"Sent %d not %d bytes", total, len);
945 close(p_write_fd); p_write_fd = -1;
949 action->fd_stdout = p_read_fd;
950 action->fd_stderr = p_stderr_fd;
952 crm_trace(
"Op: %s on %s, pid: %d, timeout: %ds", action->action, action->agent, pid,
953 action->remaining_timeout);
954 action->last_timeout_signo = 0;
955 if (action->remaining_timeout) {
956 action->timer_sigterm =
957 g_timeout_add(1000 * action->remaining_timeout, st_child_term, action);
958 action->timer_sigkill =
959 g_timeout_add(1000 * (action->remaining_timeout + 5), st_child_kill, action);
961 crm_err(
"No timeout set for stonith operation %s with device %s",
962 action->action, action->agent);
972 int timeout = action->remaining_timeout + 1;
975 while (action->remaining_timeout < 0 || timeout > 0) {
976 p = waitpid(pid, &status, WNOHANG);
985 int killrc = kill(-pid, SIGKILL);
987 if (killrc && errno != ESRCH) {
996 p = waitpid(pid, &status, 0);
1002 }
else if (p != pid) {
1003 crm_err(
"Waited for %d, got %d", pid, p);
1006 action->output = read_output(p_read_fd);
1007 action->error = read_output(p_stderr_fd);
1009 action->rc = -ECONNABORTED;
1011 log_action(action, pid);
1015 action->rc = -
ETIME;
1016 }
else if (WIFEXITED(status)) {
1017 crm_debug(
"result = %d", WEXITSTATUS(status));
1018 action->rc = -WEXITSTATUS(status);
1021 }
else if (WIFSIGNALED(status)) {
1022 crm_err(
"call %s for %s exited due to signal %d", action->action, action->agent,
1026 crm_err(
"call %s for %s returned unexpected status %#x",
1027 action->action, action->agent, status);
1033 if (p_read_fd >= 0) {
1036 if (p_write_fd >= 0) {
1039 if (p_stderr_fd >= 0) {
1043 if (c_read_fd >= 0) {
1046 if (c_write_fd >= 0) {
1049 if (c_stderr_fd >= 0) {
1059 void (*done) (GPid pid,
int rc,
const char *output,
1060 gpointer user_data))
1068 action->userdata = userdata;
1069 action->done_cb = done;
1072 rc = internal_stonith_action_execute(action);
1074 return rc < 0 ? rc : action->pid;
1087 rc = internal_stonith_action_execute(action);
1093 }
while (update_remaining_timeout(action));
1101 *agent_result = action->rc;
1104 *output = action->output;
1105 action->output = NULL;
1108 stonith_action_destroy(action);
1113 stonith_api_device_list(
stonith_t * stonith,
int call_options,
const char *
namespace,
1118 if (devices == NULL) {
1119 crm_err(
"Parameter error: stonith_api_device_list");
1124 if (
namespace == NULL ||
safe_str_eq(
"heartbeat",
namespace)) {
1125 #if HAVE_STONITH_STONITH_H 1126 static gboolean need_init = TRUE;
1128 char **entry = NULL;
1129 char **type_list = NULL;
1130 static char **(*type_list_fn) (void) = NULL;
1131 static void (*type_free_fn) (
char **) = NULL;
1143 type_list = (*type_list_fn) ();
1146 for (entry = type_list; entry != NULL && *entry; ++entry) {
1151 if (type_list && type_free_fn) {
1152 (*type_free_fn) (type_list);
1155 if (
namespace != NULL) {
1162 if (
namespace == NULL ||
safe_str_eq(
"redhat",
namespace)) {
1163 struct dirent **namelist;
1168 char buffer[FILENAME_MAX + 1];
1170 while (file_num--) {
1171 if (
'.' == namelist[file_num]->d_name[0]) {
1172 free(namelist[file_num]);
1177 free(namelist[file_num]);
1181 snprintf(buffer, FILENAME_MAX,
"%s/%s",
RH_STONITH_DIR, namelist[file_num]->d_name);
1182 if (stat(buffer, &prop) == 0 && S_ISREG(prop.st_mode)) {
1187 free(namelist[file_num]);
1196 #if HAVE_STONITH_STONITH_H 1197 static inline char *
1198 strdup_null(
const char *val)
1207 stonith_plugin(
int priority,
const char *fmt, ...)
__attribute__((__format__ (__printf__, 2, 3)));
1210 stonith_plugin(
int priority,
const char *format, ...)
1216 char *
string = NULL;
1218 va_start(ap, format);
1220 len = vasprintf (&
string, format, ap);
1231 stonith_api_device_metadata(
stonith_t * stonith,
int call_options,
const char *agent,
1232 const char *
namespace,
char **output,
int timeout)
1235 char *buffer = NULL;
1238 crm_trace(
"looking up %s/%s metadata", agent, provider);
1248 xmlNode *xml = NULL;
1249 xmlNode *actions = NULL;
1250 xmlXPathObject *xpathObj = NULL;
1252 if (exec_rc < 0 || rc != 0 || buffer == NULL) {
1253 crm_warn(
"Could not obtain metadata for %s", agent);
1261 crm_warn(
"Metadata for %s is invalid", agent);
1267 if (numXpathResults(xpathObj) > 0) {
1274 xpathObj =
xpath_search(xml,
"//action[@name='stop']");
1275 if (numXpathResults(xpathObj) <= 0) {
1276 xmlNode *tmp = NULL;
1290 xpathObj =
xpath_search(xml,
"//parameter[@name='port']");
1291 if (numXpathResults(xpathObj) > 0) {
1307 #if !HAVE_STONITH_STONITH_H 1311 static const char *no_parameter_info =
"<!-- no value -->";
1313 Stonith *stonith_obj = NULL;
1315 static gboolean need_init = TRUE;
1316 static Stonith *(*st_new_fn) (
const char *) = NULL;
1317 static const char *(*st_info_fn) (Stonith *, int) = NULL;
1318 static void (*st_del_fn) (Stonith *) = NULL;
1319 static void (*st_log_fn) (Stonith *, PILLogFun) = NULL;
1336 if (lha_agents_lib && st_new_fn && st_del_fn && st_info_fn && st_log_fn) {
1337 char *xml_meta_longdesc = NULL;
1338 char *xml_meta_shortdesc = NULL;
1340 char *meta_param = NULL;
1341 char *meta_longdesc = NULL;
1342 char *meta_shortdesc = NULL;
1344 stonith_obj = (*st_new_fn) (agent);
1346 (*st_log_fn) (stonith_obj, (PILLogFun) & stonith_plugin);
1347 meta_longdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEDESCR));
1348 if (meta_longdesc == NULL) {
1349 crm_warn(
"no long description in %s's metadata.", agent);
1350 meta_longdesc = strdup(no_parameter_info);
1353 meta_shortdesc = strdup_null((*st_info_fn) (stonith_obj, ST_DEVICEID));
1354 if (meta_shortdesc == NULL) {
1355 crm_warn(
"no short description in %s's metadata.", agent);
1356 meta_shortdesc = strdup(no_parameter_info);
1359 meta_param = strdup_null((*st_info_fn) (stonith_obj, ST_CONF_XML));
1360 if (meta_param == NULL) {
1361 crm_warn(
"no list of parameters in %s's metadata.", agent);
1362 meta_param = strdup(no_parameter_info);
1364 (*st_del_fn) (stonith_obj);
1370 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_longdesc);
1371 xml_meta_shortdesc =
1372 (
char *)xmlEncodeEntitiesReentrant(NULL, (
const unsigned char *)meta_shortdesc);
1374 bufferlen = strlen(META_TEMPLATE) + strlen(agent)
1375 + strlen(xml_meta_longdesc) + strlen(xml_meta_shortdesc)
1376 + strlen(meta_param) + 1;
1378 buffer = calloc(1, bufferlen);
1379 snprintf(buffer, bufferlen - 1, META_TEMPLATE,
1380 agent, xml_meta_longdesc, xml_meta_shortdesc, meta_param);
1382 xmlFree(xml_meta_longdesc);
1383 xmlFree(xml_meta_shortdesc);
1385 free(meta_shortdesc);
1386 free(meta_longdesc);
1403 stonith_api_query(
stonith_t * stonith,
int call_options,
const char *target,
1406 int rc = 0, lpc = 0, max = 0;
1408 xmlNode *data = NULL;
1409 xmlNode *output = NULL;
1410 xmlXPathObjectPtr xpathObj = NULL;
1412 CRM_CHECK(devices != NULL,
return -EINVAL);
1426 max = numXpathResults(xpathObj);
1428 for (lpc = 0; lpc < max; lpc++) {
1433 xmlChar *match_path = xmlGetNodePath(match);
1435 crm_info(
"%s[%d] = %s",
"//@agent", lpc, match_path);
1453 const char *action,
const char *victim,
int timeout, xmlNode ** output)
1456 xmlNode *data = NULL;
1471 stonith_api_list(
stonith_t * stonith,
int call_options,
const char *
id,
char **list_info,
1475 xmlNode *output = NULL;
1477 rc = stonith_api_call(stonith, call_options,
id,
"list", NULL, timeout, &output);
1479 if (output && list_info) {
1480 const char *list_str;
1485 *list_info = strdup(list_str);
1497 stonith_api_monitor(
stonith_t * stonith,
int call_options,
const char *
id,
int timeout)
1499 return stonith_api_call(stonith, call_options,
id,
"monitor", NULL, timeout, NULL);
1503 stonith_api_status(
stonith_t * stonith,
int call_options,
const char *
id,
const char *port,
1506 return stonith_api_call(stonith, call_options,
id,
"status", port, timeout, NULL);
1510 stonith_api_fence(
stonith_t * stonith,
int call_options,
const char *node,
const char *action,
1511 int timeout,
int tolerance)
1514 xmlNode *data = NULL;
1529 stonith_api_confirm(
stonith_t * stonith,
int call_options,
const char *target)
1531 return stonith_api_fence(stonith, call_options |
st_opt_manual_ack, target,
"off", 0, 0);
1535 stonith_api_history(
stonith_t * stonith,
int call_options,
const char *node,
1539 xmlNode *data = NULL;
1540 xmlNode *output = NULL;
1558 for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
1586 char buffer[FILENAME_MAX + 1];
1589 rc = stat(buffer, &prop);
1590 if (rc >= 0 && S_ISREG(prop.st_mode)) {
1603 #if HAVE_STONITH_STONITH_H 1605 Stonith *stonith_obj = NULL;
1607 static gboolean need_init = TRUE;
1608 static Stonith *(*st_new_fn) (
const char *) = NULL;
1609 static void (*st_del_fn) (Stonith *) = NULL;
1620 if (lha_agents_lib && st_new_fn && st_del_fn) {
1621 stonith_obj = (*st_new_fn) (agent);
1623 (*st_del_fn) (stonith_obj);
1634 crm_err(
"No such device: %s", agent);
1640 stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
1646 CRM_CHECK(a_client->event != NULL && b_client->event != NULL,
return 0);
1647 rc = strcmp(a_client->event, b_client->event);
1649 if (a_client->notify == NULL || b_client->notify == NULL) {
1652 }
else if (a_client->notify == b_client->notify) {
1655 }
else if (((
long)a_client->notify) < ((
long)b_client->notify)) {
1656 crm_err(
"callbacks for %s are not equal: %p vs. %p",
1657 a_client->event, a_client->notify, b_client->notify);
1660 crm_err(
"callbacks for %s are not equal: %p vs. %p",
1661 a_client->event, a_client->notify, b_client->notify);
1681 crm_trace(
"Sending call options: %.8lx, %d", (
long)call_options, call_options);
1692 stonith_destroy_op_callback(gpointer data)
1696 if (blob->timer && blob->timer->ref > 0) {
1697 g_source_remove(blob->timer->ref);
1704 stonith_api_signoff(
stonith_t * stonith)
1708 crm_debug(
"Signing out of the STONITH Service");
1710 if (native->source != NULL) {
1713 native->source = NULL;
1716 }
else if (native->ipc) {
1725 free(native->token); native->token = NULL;
1731 stonith_api_signon(
stonith_t * stonith,
const char *name,
int *stonith_fd)
1738 .destroy = stonith_connection_destroy
1741 crm_trace(
"Connecting command channel");
1750 }
else if (native->ipc) {
1751 crm_perror(LOG_ERR,
"Connection to STONITH manager failed");
1762 if (native->ipc == NULL) {
1763 crm_debug(
"Could not connect to the Stonith API");
1768 xmlNode *reply = NULL;
1777 crm_perror(LOG_DEBUG,
"Couldn't complete registration with the fencing API: %d", rc);
1780 }
else if (reply == NULL) {
1781 crm_err(
"Did not receive registration reply");
1789 crm_err(
"Invalid registration message: %s", msg_type);
1793 }
else if (tmp_ticket == NULL) {
1794 crm_err(
"No registration token provided");
1799 crm_trace(
"Obtained registration token: %s", tmp_ticket);
1800 native->token = strdup(tmp_ticket);
1810 #if HAVE_MSGFROMIPC_TIMEOUT 1813 crm_debug(
"Connection to STONITH successful");
1823 stonith_set_notification(
stonith_t * stonith,
const char *callback,
int enabled)
1840 crm_perror(LOG_DEBUG,
"Couldn't register for fencing notifications: %d", rc);
1852 stonith_api_add_notification(
stonith_t * stonith,
const char *event,
1855 GList *list_item = NULL;
1860 crm_trace(
"Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
1863 new_client->event = event;
1864 new_client->notify = callback;
1866 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1868 if (list_item != NULL) {
1869 crm_warn(
"Callback already present");
1874 private->notify_list = g_list_append(private->notify_list, new_client);
1876 stonith_set_notification(stonith, event, 1);
1878 crm_trace(
"Callback added (%d)", g_list_length(private->notify_list));
1884 stonith_api_del_notification(
stonith_t * stonith,
const char *event)
1886 GList *list_item = NULL;
1890 crm_debug(
"Removing callback for %s events", event);
1894 new_client->event = event;
1895 new_client->notify = NULL;
1897 list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1899 stonith_set_notification(stonith, event, 0);
1901 if (list_item != NULL) {
1904 private->notify_list = g_list_remove(private->notify_list, list_client);
1917 stonith_async_timeout_handler(gpointer data)
1934 struct timer_rec_s *async_timer = callback->timer;
1941 async_timer = calloc(1,
sizeof(
struct timer_rec_s));
1942 callback->timer = async_timer;
1950 async_timer->
timeout = (timeout + 60) * 1000;
1951 if (async_timer->
ref) {
1952 g_source_remove(async_timer->
ref);
1955 g_timeout_add(async_timer->
timeout, stonith_async_timeout_handler, async_timer);
1959 update_callback_timeout(
int call_id,
int timeout,
stonith_t * st)
1964 callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1965 if (!callback || !callback->allow_timeout_updates) {
1969 set_callback_timeout(callback, st, call_id, timeout);
1973 invoke_callback(
stonith_t * st,
int call_id,
int rc,
void *userdata,
1982 callback(st, &data);
1986 stonith_api_add_callback(
stonith_t * stonith,
int call_id,
int timeout,
int options,
1987 void *user_data,
const char *callback_name,
1993 CRM_CHECK(stonith != NULL,
return -EINVAL);
1998 private->op_callback = callback;
2000 }
else if (call_id < 0) {
2003 invoke_callback(stonith, call_id, call_id, user_data, callback);
2011 blob->id = callback_name;
2013 blob->user_data = user_data;
2014 blob->callback = callback;
2018 set_callback_timeout(blob, stonith, call_id, timeout);
2021 g_hash_table_insert(private->stonith_op_callback_table, GINT_TO_POINTER(call_id), blob);
2022 crm_trace(
"Added callback to %s for call %d", callback_name, call_id);
2028 stonith_api_del_callback(
stonith_t * stonith,
int call_id,
bool all_callbacks)
2032 if (all_callbacks) {
2033 private->op_callback = NULL;
2034 g_hash_table_destroy(private->stonith_op_callback_table);
2035 private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2037 stonith_destroy_op_callback);
2039 }
else if (call_id == 0) {
2040 private->op_callback = NULL;
2043 g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2049 stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
2051 int call = GPOINTER_TO_INT(key);
2062 if (private->stonith_op_callback_table == NULL) {
2065 return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
2080 local_blob.id = NULL;
2081 local_blob.callback = NULL;
2082 local_blob.user_data = NULL;
2083 local_blob.only_success = FALSE;
2092 blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
2098 stonith_api_del_callback(stonith, call_id, FALSE);
2101 crm_trace(
"No callback found for call %d", call_id);
2102 local_blob.callback = NULL;
2105 if (local_blob.callback != NULL && (rc ==
pcmk_ok || local_blob.only_success == FALSE)) {
2106 crm_trace(
"Invoking callback %s for call %d",
crm_str(local_blob.id), call_id);
2107 invoke_callback(stonith, call_id, rc, local_blob.user_data, local_blob.callback);
2109 }
else if (private->op_callback == NULL && rc !=
pcmk_ok) {
2114 if (private->op_callback != NULL) {
2115 crm_trace(
"Invoking global callback for call %d", call_id);
2116 invoke_callback(stonith, call_id, rc, NULL, private->op_callback);
2141 xml_to_event(xmlNode * msg)
2165 crm_err(
"No data for %s event", ntype);
2191 stonith_send_notification(gpointer data, gpointer user_data)
2193 struct notify_blob_s *blob = user_data;
2196 const char *
event = NULL;
2198 if (blob->xml == NULL) {
2199 crm_warn(
"Skipping callback - NULL message");
2205 if (entry == NULL) {
2206 crm_warn(
"Skipping callback - NULL callback client");
2209 }
else if (entry->notify == NULL) {
2210 crm_warn(
"Skipping callback - NULL callback");
2214 crm_trace(
"Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
2218 st_event = xml_to_event(blob->xml);
2220 crm_trace(
"Invoking callback for %p/%s event...", entry, event);
2221 entry->notify(blob->stonith, st_event);
2224 event_free(st_event);
2229 int call_options,
int timeout)
2235 xmlNode *op_msg = NULL;
2236 xmlNode *op_reply = NULL;
2244 if (output_data != NULL) {
2245 *output_data = NULL;
2249 crm_err(
"No operation specified");
2269 if (op_msg == NULL) {
2274 crm_trace(
"Sending %s message to STONITH service, Timeout: %ds", op, timeout);
2276 rc =
crm_ipc_send(native->ipc, op_msg, ipc_flags, 1000 * (timeout + 60), &op_reply);
2280 crm_perror(LOG_ERR,
"Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
2287 if (!(call_options & st_opt_sync_call)) {
2298 if (reply_id == stonith->
call_id) {
2299 crm_trace(
"Synchronous reply %d received", reply_id);
2309 *output_data = op_reply;
2313 }
else if (reply_id <= 0) {
2314 crm_err(
"Received bad reply: No id set");
2320 crm_err(
"Received bad reply: %d (wanted %d)", reply_id, stonith->
call_id);
2328 crm_err(
"STONITH disconnected");
2340 gboolean stay_connected = TRUE;
2356 stay_connected = FALSE;
2360 return stay_connected;
2366 const char *
type = NULL;
2367 struct notify_blob_s blob;
2373 private = st->private;
2377 if (blob.xml == NULL) {
2378 crm_warn(
"Received a NULL msg from STONITH service: %s.", buffer);
2384 crm_trace(
"Activating %s callbacks...", type);
2390 g_list_foreach(private->notify_list, stonith_send_notification, &blob);
2398 update_callback_timeout(call_id, timeout, st);
2400 crm_err(
"Unknown message type: %s", type);
2416 crm_trace(
"Disconnecting %p first", stonith);
2423 crm_trace(
"Removing %d callbacks", g_hash_table_size(private->stonith_op_callback_table));
2424 g_hash_table_destroy(private->stonith_op_callback_table);
2426 crm_trace(
"Destroying %d notification clients", g_list_length(private->notify_list));
2427 g_list_free_full(private->notify_list, free);
2430 free(stonith->
cmds);
2455 new_stonith = calloc(1,
sizeof(
stonith_t));
2457 new_stonith->
private =
private;
2459 private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2460 NULL, stonith_destroy_op_callback);
2461 private->notify_list = NULL;
2469 new_stonith->
cmds->
free = stonith_api_free;
2473 new_stonith->
cmds->
list = stonith_api_list;
2475 new_stonith->
cmds->
status = stonith_api_status;
2476 new_stonith->
cmds->
fence = stonith_api_fence;
2481 new_stonith->
cmds->
metadata = stonith_api_device_metadata;
2483 new_stonith->
cmds->
query = stonith_api_query;
2508 p->
key = strdup(key);
2511 p->
value = strdup(value);
2515 while (end && end->
next) {
2546 #define api_log_open() openlog("stonith-api", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON) 2547 #define api_log(level, fmt, args...) syslog(level, "%s: "fmt, __FUNCTION__, args) 2553 const char *action =
"reboot";
2564 api_log(LOG_ERR,
"Connection failed, could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2568 if (uname != NULL) {
2569 name = strdup(uname);
2571 }
else if (nodeid > 0) {
2581 rc = st->
cmds->
fence(st, opts, name, action, timeout, 0);
2583 api_log(LOG_ERR,
"Could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname,
pcmk_strerror(rc), rc);
2585 api_log(LOG_NOTICE,
"Node %u/%s kicked: %s ", nodeid, uname, action);
2617 if (uname != NULL) {
2618 name = strdup(uname);
2620 }
else if (nodeid > 0) {
2630 rc = st->
cmds->
history(st, opts, name, &history, 120);
2632 for (hp = history; hp; hp = hp->
next) {
2649 api_log(LOG_INFO,
"Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
2651 api_log(LOG_ERR,
"Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname,
pcmk_strerror(rc), rc);
2661 api_log(LOG_INFO,
"Node %u/%s last kicked at: %ld", nodeid, uname, (
long int)when);
2667 #if HAVE_STONITH_STONITH_H 2668 # include <pils/plugin.h> 2670 const char *i_hate_pils(
int rc);
2675 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
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...)
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.
#define F_STONITH_CLIENTID
gboolean safe_str_neq(const char *a, const char *b)
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.
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.
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)
int(* query)(stonith_t *st, int options, const char *node, stonith_key_value_t **devices, int timeout)
Retrieve a list of registered stonith devices.
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)
#define F_STONITH_CALLBACK_TOKEN
#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(* 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...
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
#define T_STONITH_TIMEOUT_VALUE
#define CRM_LOG_ASSERT(expr)
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(* remove_callback)(stonith_t *st, int call_id, bool all_callbacks)
Remove a registered callback for a given call id.
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
#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)
time_t stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
int(* free)(stonith_t *st)
Destroy the stonith api structure.
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)
#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
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.
#define crm_trace(fmt, args...)
#define XML_ATTR_STONITH_INDEX
int(* fence)(stonith_t *st, int options, const char *node, const char *action, int timeout, int tolerance)
Issue a fencing action against a node.
enum crm_proc_flag __attribute__
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_log_xml_debug(xml, text)
#define XML_ATTR_STONITH_TARGET
Wrappers for and extensions to libxml2.
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define crm_log_xml_warn(xml, text)
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
const char * crm_element_value(xmlNode *data, const char *name)
int(* disconnect)(stonith_t *st)
Disconnect from the local stonith daemon.
int(* stonith_op_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
#define T_STONITH_NOTIFY_DISCONNECT
#define CRM_DEFAULT_OP_TIMEOUT_S
const char * get_stonith_provider(const char *agent, const char *provider)
#define STONITH_OP_DEVICE_DEL
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.
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)
int(* connect)(stonith_t *st, const char *name, int *stonith_fd)
Connect to the local stonith daemon.
#define api_log(level, fmt, args...)
#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))
int(* remove_device)(stonith_t *st, int options, const char *name)
Remove a registered stonith device with the local stonith daemon.
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(* register_notification)(stonith_t *st, const char *event, void(*notify)(stonith_t *st, stonith_event_t *e))
int crm_ipc_ready(crm_ipc_t *client)
Check whether an IPC connection is ready to be read.
int(* monitor)(stonith_t *st, int options, const char *id, int timeout)
Check to see if a local stonith device is reachable.
int(* remove_notification)(stonith_t *st, const char *event)
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(* 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.
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)
int(* remove_level)(stonith_t *st, int options, const char *node, int level)
Remove a fencing level for a specific node.
#define FAILURE_MAX_RETRIES
#define crm_log_xml_notice(xml, text)
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
#define STONITH_ATTR_ARGMAP
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
#define T_STONITH_NOTIFY_FENCE
int(* confirm)(stonith_t *st, int options, const char *node)
Manually confirm that a node is down.
#define crm_log_xml_trace(xml, text)
#define F_STONITH_OPERATION
mainloop_io_t * mainloop_add_ipc_client(const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
char * crm_itoa(int an_int)
#define safe_str_eq(a, b)
int(* metadata)(stonith_t *st, int options, const char *device, const char *namespace, char **output, int timeout)
Get the metadata documentation for a resource.
#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(* 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.
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
#define F_STONITH_HISTORY_LIST
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