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