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