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