pacemaker  2.0.2-debe490
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
st_client.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2018 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This source code is licensed under the GNU Lesser General Public License
5  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
6  */
7 
8 #include <crm_internal.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <libgen.h>
15 
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 
20 #include <glib.h>
21 
22 #include <crm/crm.h>
23 #include <crm/stonith-ng.h>
24 #include <crm/fencing/internal.h>
25 #include <crm/msg_xml.h>
26 #include <crm/common/xml.h>
27 
28 #include <crm/common/mainloop.h>
29 
30 #if SUPPORT_CIBSECRETS
31 # include <crm/common/cib_secrets.h>
32 #endif
33 
34 CRM_TRACE_INIT_DATA(stonith);
35 
36 struct stonith_action_s {
38  char *agent;
39  char *action;
40  char *victim;
41  GHashTable *args;
42  int timeout;
43  int async;
44  void *userdata;
45  void (*done_cb) (GPid pid, gint status, const char *output, gpointer user_data);
46  void (*fork_cb) (GPid pid, gpointer user_data);
47 
48  svc_action_t *svc_action;
49 
51  time_t initial_start_time;
52  int tries;
53  int remaining_timeout;
54  int max_retries;
55 
56  /* device output data */
57  GPid pid;
58  int rc;
59  char *output;
60  char *error;
61 };
62 
63 typedef struct stonith_private_s {
64  char *token;
65  crm_ipc_t *ipc;
66  mainloop_io_t *source;
67  GHashTable *stonith_op_callback_table;
68  GList *notify_list;
69 
70  void (*op_callback) (stonith_t * st, stonith_callback_data_t * data);
71 
73 
74 typedef struct stonith_notify_client_s {
75  const char *event;
76  const char *obj_id; /* implement one day */
77  const char *obj_type; /* implement one day */
78  void (*notify) (stonith_t * st, stonith_event_t * e);
79 
81 
82 typedef struct stonith_callback_client_s {
83  void (*callback) (stonith_t * st, stonith_callback_data_t * data);
84  const char *id;
85  void *user_data;
86  gboolean only_success;
87  gboolean allow_timeout_updates;
88  struct timer_rec_s *timer;
89 
91 
92 struct notify_blob_s {
93  stonith_t *stonith;
94  xmlNode *xml;
95 };
96 
97 struct timer_rec_s {
98  int call_id;
99  int timeout;
100  guint ref;
102 };
103 
104 typedef int (*stonith_op_t) (const char *, int, const char *, xmlNode *,
105  xmlNode *, xmlNode *, xmlNode **, xmlNode **);
106 
107 bool stonith_dispatch(stonith_t * st);
108 xmlNode *stonith_create_op(int call_id, const char *token, const char *op, xmlNode * data,
109  int call_options);
110 int stonith_send_command(stonith_t * stonith, const char *op, xmlNode * data,
111  xmlNode ** output_data, int call_options, int timeout);
112 
113 static void stonith_connection_destroy(gpointer user_data);
114 static void stonith_send_notification(gpointer data, gpointer user_data);
115 static int internal_stonith_action_execute(stonith_action_t * action);
116 static void log_action(stonith_action_t *action, pid_t pid);
117 
126 stonith_text2namespace(const char *namespace_s)
127 {
128  if ((namespace_s == NULL) || !strcmp(namespace_s, "any")) {
129  return st_namespace_any;
130 
131  } else if (!strcmp(namespace_s, "redhat")
132  || !strcmp(namespace_s, "stonith-ng")) {
133  return st_namespace_rhcs;
134 
135  } else if (!strcmp(namespace_s, "internal")) {
136  return st_namespace_internal;
137 
138  } else if (!strcmp(namespace_s, "heartbeat")) {
139  return st_namespace_lha;
140  }
141  return st_namespace_invalid;
142 }
143 
151 const char *
153 {
154  switch (st_namespace) {
155  case st_namespace_any: return "any";
156  case st_namespace_rhcs: return "stonith-ng";
157  case st_namespace_internal: return "internal";
158  case st_namespace_lha: return "heartbeat";
159  default: break;
160  }
161  return "unsupported";
162 }
163 
173 stonith_get_namespace(const char *agent, const char *namespace_s)
174 {
175  if (safe_str_eq(namespace_s, "internal")) {
176  return st_namespace_internal;
177  }
178 
179  if (stonith__agent_is_rhcs(agent)) {
180  return st_namespace_rhcs;
181  }
182 
183 #if HAVE_STONITH_STONITH_H
184  if (stonith__agent_is_lha(agent)) {
185  return st_namespace_lha;
186  }
187 #endif
188 
189  crm_err("Unknown fence agent: %s", agent);
190  return st_namespace_invalid;
191 }
192 
193 static void
194 log_action(stonith_action_t *action, pid_t pid)
195 {
196  if (action->output) {
197  /* Logging the whole string confuses syslog when the string is xml */
198  char *prefix = crm_strdup_printf("%s[%d] stdout:", action->agent, pid);
199 
200  crm_log_output(LOG_TRACE, prefix, action->output);
201  free(prefix);
202  }
203 
204  if (action->error) {
205  /* Logging the whole string confuses syslog when the string is xml */
206  char *prefix = crm_strdup_printf("%s[%d] stderr:", action->agent, pid);
207 
208  crm_log_output(LOG_WARNING, prefix, action->error);
209  free(prefix);
210  }
211 }
212 
213 static void
214 stonith_connection_destroy(gpointer user_data)
215 {
216  stonith_t *stonith = user_data;
217  stonith_private_t *native = NULL;
218  struct notify_blob_s blob;
219 
220  crm_trace("Sending destroyed notification");
221  blob.stonith = stonith;
222  blob.xml = create_xml_node(NULL, "notify");
223 
224  native = stonith->st_private;
225  native->ipc = NULL;
226  native->source = NULL;
227 
228  stonith->state = stonith_disconnected;
229  crm_xml_add(blob.xml, F_TYPE, T_STONITH_NOTIFY);
231 
232  g_list_foreach(native->notify_list, stonith_send_notification, &blob);
233  free_xml(blob.xml);
234 }
235 
236 xmlNode *
237 create_device_registration_xml(const char *id, enum stonith_namespace namespace,
238  const char *agent, stonith_key_value_t *params,
239  const char *rsc_provides)
240 {
241  xmlNode *data = create_xml_node(NULL, F_STONITH_DEVICE);
242  xmlNode *args = create_xml_node(data, XML_TAG_ATTRS);
243 
244 #if HAVE_STONITH_STONITH_H
245  if (namespace == st_namespace_any) {
246  namespace = stonith_get_namespace(agent, NULL);
247  }
248  if (namespace == st_namespace_lha) {
249  hash2field((gpointer) "plugin", (gpointer) agent, args);
250  agent = "fence_legacy";
251  }
252 #endif
253 
254  crm_xml_add(data, XML_ATTR_ID, id);
255  crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
256  crm_xml_add(data, "agent", agent);
257  if ((namespace != st_namespace_any) && (namespace != st_namespace_invalid)) {
258  crm_xml_add(data, "namespace", stonith_namespace2text(namespace));
259  }
260  if (rsc_provides) {
261  crm_xml_add(data, "rsc_provides", rsc_provides);
262  }
263 
264  for (; params; params = params->next) {
265  hash2field((gpointer) params->key, (gpointer) params->value, args);
266  }
267 
268  return data;
269 }
270 
271 static int
272 stonith_api_register_device(stonith_t * st, int call_options,
273  const char *id, const char *namespace, const char *agent,
274  stonith_key_value_t * params)
275 {
276  int rc = 0;
277  xmlNode *data = NULL;
278 
280  agent, params, NULL);
281 
282  rc = stonith_send_command(st, STONITH_OP_DEVICE_ADD, data, NULL, call_options, 0);
283  free_xml(data);
284 
285  return rc;
286 }
287 
288 static int
289 stonith_api_remove_device(stonith_t * st, int call_options, const char *name)
290 {
291  int rc = 0;
292  xmlNode *data = NULL;
293 
294  data = create_xml_node(NULL, F_STONITH_DEVICE);
295  crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
296  crm_xml_add(data, XML_ATTR_ID, name);
297  rc = stonith_send_command(st, STONITH_OP_DEVICE_DEL, data, NULL, call_options, 0);
298  free_xml(data);
299 
300  return rc;
301 }
302 
303 static int
304 stonith_api_remove_level_full(stonith_t *st, int options,
305  const char *node, const char *pattern,
306  const char *attr, const char *value, int level)
307 {
308  int rc = 0;
309  xmlNode *data = NULL;
310 
311  CRM_CHECK(node || pattern || (attr && value), return -EINVAL);
312 
314  crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
315 
316  if (node) {
318 
319  } else if (pattern) {
321 
322  } else {
325  }
326 
328  rc = stonith_send_command(st, STONITH_OP_LEVEL_DEL, data, NULL, options, 0);
329  free_xml(data);
330 
331  return rc;
332 }
333 
334 static int
335 stonith_api_remove_level(stonith_t * st, int options, const char *node, int level)
336 {
337  return stonith_api_remove_level_full(st, options, node,
338  NULL, NULL, NULL, level);
339 }
340 
356 xmlNode *
357 create_level_registration_xml(const char *node, const char *pattern,
358  const char *attr, const char *value,
359  int level, stonith_key_value_t *device_list)
360 {
361  int len = 0;
362  char *list = NULL;
363  xmlNode *data;
364 
365  CRM_CHECK(node || pattern || (attr && value), return NULL);
366 
368  CRM_CHECK(data, return NULL);
369 
370  crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
371  crm_xml_add_int(data, XML_ATTR_ID, level);
373 
374  if (node) {
376 
377  } else if (pattern) {
379 
380  } else {
383  }
384 
385  for (; device_list; device_list = device_list->next) {
386 
387  int adding = strlen(device_list->value);
388  if(list) {
389  adding++; /* +1 space */
390  }
391 
392  crm_trace("Adding %s (%dc) at offset %d", device_list->value, adding, len);
393  list = realloc_safe(list, len + adding + 1); /* +1 EOS */
394  if (list == NULL) {
395  crm_perror(LOG_CRIT, "Could not create device list");
396  free_xml(data);
397  return NULL;
398  }
399  sprintf(list + len, "%s%s", len?",":"", device_list->value);
400  len += adding;
401  }
402 
404 
405  free(list);
406  return data;
407 }
408 
409 static int
410 stonith_api_register_level_full(stonith_t * st, int options, const char *node,
411  const char *pattern,
412  const char *attr, const char *value,
413  int level, stonith_key_value_t *device_list)
414 {
415  int rc = 0;
416  xmlNode *data = create_level_registration_xml(node, pattern, attr, value,
417  level, device_list);
418  CRM_CHECK(data != NULL, return -EINVAL);
419 
420  rc = stonith_send_command(st, STONITH_OP_LEVEL_ADD, data, NULL, options, 0);
421  free_xml(data);
422 
423  return rc;
424 }
425 
426 static int
427 stonith_api_register_level(stonith_t * st, int options, const char *node, int level,
428  stonith_key_value_t * device_list)
429 {
430  return stonith_api_register_level_full(st, options, node, NULL, NULL, NULL,
431  level, device_list);
432 }
433 
434 static void
435 append_arg(const char *key, const char *value, GHashTable **args)
436 {
437  CRM_CHECK(key != NULL, return);
438  CRM_CHECK(value != NULL, return);
439  CRM_CHECK(args != NULL, return);
440 
441  if (strstr(key, "pcmk_")) {
442  return;
443  } else if (strstr(key, CRM_META)) {
444  return;
445  } else if (safe_str_eq(key, "crm_feature_set")) {
446  return;
447  }
448 
449  if (!*args) {
450  *args = crm_str_table_new();
451  }
452 
453  CRM_CHECK(*args != NULL, return);
454  crm_trace("Appending: %s=%s", key, value);
455  g_hash_table_replace(*args, strdup(key), strdup(value));
456 }
457 
458 static void
459 append_config_arg(gpointer key, gpointer value, gpointer user_data)
460 {
461  /* The fencer will filter action out when it registers the device,
462  * but ignore it here just in case any other library callers
463  * fail to do so.
464  */
466  append_arg(key, value, user_data);
467  return;
468  }
469 }
470 
471 static GHashTable *
472 make_args(const char *agent, const char *action, const char *victim, uint32_t victim_nodeid, GHashTable * device_args,
473  GHashTable * port_map)
474 {
475  char buffer[512];
476  GHashTable *arg_list = NULL;
477  const char *value = NULL;
478 
479  CRM_CHECK(action != NULL, return NULL);
480 
481  snprintf(buffer, sizeof(buffer), "pcmk_%s_action", action);
482  if (device_args) {
483  value = g_hash_table_lookup(device_args, buffer);
484  }
485  if (value) {
486  crm_info("Substituting action '%s' for requested operation '%s'", value, action);
487  action = value;
488  }
489 
490  append_arg(STONITH_ATTR_ACTION_OP, action, &arg_list);
491  if (victim && device_args) {
492  const char *alias = victim;
493  const char *param = g_hash_table_lookup(device_args, STONITH_ATTR_HOSTARG);
494 
495  if (port_map && g_hash_table_lookup(port_map, victim)) {
496  alias = g_hash_table_lookup(port_map, victim);
497  }
498 
499  /* Always supply the node's name, too:
500  * https://github.com/ClusterLabs/fence-agents/blob/master/doc/FenceAgentAPI.md
501  */
502  append_arg("nodename", victim, &arg_list);
503  if (victim_nodeid) {
504  char nodeid_str[33] = { 0, };
505  if (snprintf(nodeid_str, 33, "%u", (unsigned int)victim_nodeid)) {
506  crm_info("For stonith action (%s) for victim %s, adding nodeid (%s) to parameters",
507  action, victim, nodeid_str);
508  append_arg("nodeid", nodeid_str, &arg_list);
509  }
510  }
511 
512  /* Check if we need to supply the victim in any other form */
513  if(safe_str_eq(agent, "fence_legacy")) {
514  value = agent;
515 
516  } else if (param == NULL) {
517  param = "port";
518  value = g_hash_table_lookup(device_args, param);
519 
520  } else if (safe_str_eq(param, "none")) {
521  value = param; /* Nothing more to do */
522 
523  } else {
524  value = g_hash_table_lookup(device_args, param);
525  }
526 
527  /* Don't overwrite explictly set values for $param */
528  if (value == NULL || safe_str_eq(value, "dynamic")) {
529  crm_debug("Performing %s action for node '%s' as '%s=%s'", action, victim, param,
530  alias);
531  append_arg(param, alias, &arg_list);
532  }
533  }
534 
535  if (device_args) {
536  g_hash_table_foreach(device_args, append_config_arg, &arg_list);
537  }
538 
539  return arg_list;
540 }
541 
548 void
550 {
551  if (action) {
552  free(action->agent);
553  if (action->args) {
554  g_hash_table_destroy(action->args);
555  }
556  free(action->action);
557  free(action->victim);
558  if (action->svc_action) {
559  services_action_free(action->svc_action);
560  }
561  free(action->output);
562  free(action->error);
563  free(action);
564  }
565 }
566 
579 void
580 stonith__action_result(stonith_action_t *action, int *rc, char **output,
581  char **error_output)
582 {
583  if (rc) {
584  *rc = pcmk_ok;
585  }
586  if (output) {
587  *output = NULL;
588  }
589  if (error_output) {
590  *error_output = NULL;
591  }
592  if (action != NULL) {
593  if (rc) {
594  *rc = action->rc;
595  }
596  if (output && action->output) {
597  *output = action->output;
598  action->output = NULL; // hand off memory management to caller
599  }
600  if (error_output && action->error) {
601  *error_output = action->error;
602  action->error = NULL; // hand off memory management to caller
603  }
604  }
605 }
606 
607 #define FAILURE_MAX_RETRIES 2
609 stonith_action_create(const char *agent,
610  const char *_action,
611  const char *victim,
612  uint32_t victim_nodeid,
613  int timeout, GHashTable * device_args, GHashTable * port_map)
614 {
615  stonith_action_t *action;
616 
617  action = calloc(1, sizeof(stonith_action_t));
618  action->args = make_args(agent, _action, victim, victim_nodeid, device_args, port_map);
619  crm_debug("Preparing '%s' action for %s using agent %s",
620  _action, (victim? victim : "no target"), agent);
621  action->agent = strdup(agent);
622  action->action = strdup(_action);
623  if (victim) {
624  action->victim = strdup(victim);
625  }
626  action->timeout = action->remaining_timeout = timeout;
627  action->max_retries = FAILURE_MAX_RETRIES;
628 
629  if (device_args) {
630  char buffer[512];
631  const char *value = NULL;
632 
633  snprintf(buffer, sizeof(buffer), "pcmk_%s_retries", _action);
634  value = g_hash_table_lookup(device_args, buffer);
635 
636  if (value) {
637  action->max_retries = atoi(value);
638  }
639  }
640 
641  return action;
642 }
643 
644 static gboolean
645 update_remaining_timeout(stonith_action_t * action)
646 {
647  int diff = time(NULL) - action->initial_start_time;
648 
649  if (action->tries >= action->max_retries) {
650  crm_info("Attempted to execute agent %s (%s) the maximum number of times (%d) allowed",
651  action->agent, action->action, action->max_retries);
652  action->remaining_timeout = 0;
653  } else if ((action->rc != -ETIME) && diff < (action->timeout * 0.7)) {
654  /* only set remaining timeout period if there is 30%
655  * or greater of the original timeout period left */
656  action->remaining_timeout = action->timeout - diff;
657  } else {
658  action->remaining_timeout = 0;
659  }
660  return action->remaining_timeout ? TRUE : FALSE;
661 }
662 
663 static int
664 svc_action_to_errno(svc_action_t *svc_action) {
665  int rv = pcmk_ok;
666 
667  if (svc_action->rc > 0) {
668  /* Try to provide a useful error code based on the fence agent's
669  * error output.
670  */
671  if (svc_action->rc == PCMK_OCF_TIMEOUT) {
672  rv = -ETIME;
673 
674  } else if (svc_action->stderr_data == NULL) {
675  rv = -ENODATA;
676 
677  } else if (strstr(svc_action->stderr_data, "imed out")) {
678  /* Some agents have their own internal timeouts */
679  rv = -ETIME;
680 
681  } else if (strstr(svc_action->stderr_data, "Unrecognised action")) {
682  rv = -EOPNOTSUPP;
683 
684  } else {
685  rv = -pcmk_err_generic;
686  }
687  }
688  return rv;
689 }
690 
691 static void
692 stonith_action_async_done(svc_action_t *svc_action)
693 {
694  stonith_action_t *action = (stonith_action_t *) svc_action->cb_data;
695 
696  action->rc = svc_action_to_errno(svc_action);
697  action->output = svc_action->stdout_data;
698  svc_action->stdout_data = NULL;
699  action->error = svc_action->stderr_data;
700  svc_action->stderr_data = NULL;
701 
702  svc_action->params = NULL;
703 
704  crm_debug("Child process %d performing action '%s' exited with rc %d",
705  action->pid, action->action, svc_action->rc);
706 
707  log_action(action, action->pid);
708 
709  if (action->rc != pcmk_ok && update_remaining_timeout(action)) {
710  int rc = internal_stonith_action_execute(action);
711  if (rc == pcmk_ok) {
712  return;
713  }
714  }
715 
716  if (action->done_cb) {
717  action->done_cb(action->pid, action->rc, action->output, action->userdata);
718  }
719 
720  action->svc_action = NULL; // don't remove our caller
721  stonith__destroy_action(action);
722 }
723 
724 static void
725 stonith_action_async_forked(svc_action_t *svc_action)
726 {
727  stonith_action_t *action = (stonith_action_t *) svc_action->cb_data;
728 
729  action->pid = svc_action->pid;
730  action->svc_action = svc_action;
731 
732  if (action->fork_cb) {
733  (action->fork_cb) (svc_action->pid, action->userdata);
734  }
735 
736  crm_trace("Child process %d performing action '%s' successfully forked",
737  action->pid, action->action);
738 }
739 
740 static int
741 internal_stonith_action_execute(stonith_action_t * action)
742 {
743  int rc = -EPROTO;
744  int is_retry = 0;
745  svc_action_t *svc_action = NULL;
746  static int stonith_sequence = 0;
747  char *buffer = NULL;
748 
749  if (!action->tries) {
750  action->initial_start_time = time(NULL);
751  }
752  action->tries++;
753 
754  if (action->tries > 1) {
755  crm_info("Attempt %d to execute %s (%s). remaining timeout is %d",
756  action->tries, action->agent, action->action, action->remaining_timeout);
757  is_retry = 1;
758  }
759 
760  if (action->args == NULL || action->agent == NULL)
761  goto fail;
762 
763  buffer = crm_strdup_printf(RH_STONITH_DIR "/%s", basename(action->agent));
764  svc_action = services_action_create_generic(buffer, NULL);
765  free(buffer);
766  svc_action->timeout = 1000 * action->remaining_timeout;
767  svc_action->standard = strdup(PCMK_RESOURCE_CLASS_STONITH);
768  svc_action->id = crm_strdup_printf("%s_%s_%d", basename(action->agent),
769  action->action, action->tries);
770  svc_action->agent = strdup(action->agent);
771  svc_action->sequence = stonith_sequence++;
772  svc_action->params = action->args;
773  svc_action->cb_data = (void *) action;
774 
775  /* keep retries from executing out of control and free previous results */
776  if (is_retry) {
777  free(action->output);
778  action->output = NULL;
779  free(action->error);
780  action->error = NULL;
781  sleep(1);
782  }
783 
784  if (action->async) {
785  /* async */
786  if(services_action_async_fork_notify(svc_action,
787  &stonith_action_async_done,
788  &stonith_action_async_forked) == FALSE) {
789  services_action_free(svc_action);
790  svc_action = NULL;
791  } else {
792  rc = 0;
793  }
794 
795  } else {
796  /* sync */
797  if (services_action_sync(svc_action)) {
798  rc = 0;
799  action->rc = svc_action_to_errno(svc_action);
800  action->output = svc_action->stdout_data;
801  svc_action->stdout_data = NULL;
802  action->error = svc_action->stderr_data;
803  svc_action->stderr_data = NULL;
804  } else {
805  action->rc = -ECONNABORTED;
806  rc = action->rc;
807  }
808 
809  svc_action->params = NULL;
810  services_action_free(svc_action);
811  }
812 
813  fail:
814  return rc;
815 }
816 
828 int
830  void *userdata,
831  void (*done) (GPid pid, int rc, const char *output,
832  gpointer user_data),
833  void (*fork_cb) (GPid pid, gpointer user_data))
834 {
835  if (!action) {
836  return -1;
837  }
838 
839  action->userdata = userdata;
840  action->done_cb = done;
841  action->fork_cb = fork_cb;
842  action->async = 1;
843 
844  return internal_stonith_action_execute(action);
845 }
846 
855 int
857 {
858  int rc = pcmk_ok;
859 
860  CRM_CHECK(action != NULL, return -EINVAL);
861 
862  // Keep trying until success, max retries, or timeout
863  do {
864  rc = internal_stonith_action_execute(action);
865  } while ((rc != pcmk_ok) && update_remaining_timeout(action));
866 
867  return rc;
868 }
869 
870 static int
871 stonith_api_device_list(stonith_t * stonith, int call_options, const char *namespace,
872  stonith_key_value_t ** devices, int timeout)
873 {
874  int count = 0;
875  enum stonith_namespace ns = stonith_text2namespace(namespace);
876 
877  if (devices == NULL) {
878  crm_err("Parameter error: stonith_api_device_list");
879  return -EFAULT;
880  }
881 
882 #if HAVE_STONITH_STONITH_H
883  // Include Linux-HA agents if requested
884  if ((ns == st_namespace_any) || (ns == st_namespace_lha)) {
885  count += stonith__list_lha_agents(devices);
886  }
887 #endif
888 
889  // Include Red Hat agents if requested
890  if ((ns == st_namespace_any) || (ns == st_namespace_rhcs)) {
891  count += stonith__list_rhcs_agents(devices);
892  }
893 
894  return count;
895 }
896 
897 static int
898 stonith_api_device_metadata(stonith_t * stonith, int call_options, const char *agent,
899  const char *namespace, char **output, int timeout)
900 {
901  /* By executing meta-data directly, we can get it from stonith_admin when
902  * the cluster is not running, which is important for higher-level tools.
903  */
904 
905  enum stonith_namespace ns = stonith_get_namespace(agent, namespace);
906 
907  crm_trace("Looking up metadata for %s agent %s",
908  stonith_namespace2text(ns), agent);
909 
910  switch (ns) {
911  case st_namespace_rhcs:
912  return stonith__rhcs_metadata(agent, timeout, output);
913 
914 #if HAVE_STONITH_STONITH_H
915  case st_namespace_lha:
916  return stonith__lha_metadata(agent, timeout, output);
917 #endif
918 
919  default:
920  errno = EINVAL;
921  crm_perror(LOG_ERR,
922  "Agent %s not found or does not support meta-data",
923  agent);
924  break;
925  }
926  return -EINVAL;
927 }
928 
929 static int
930 stonith_api_query(stonith_t * stonith, int call_options, const char *target,
931  stonith_key_value_t ** devices, int timeout)
932 {
933  int rc = 0, lpc = 0, max = 0;
934 
935  xmlNode *data = NULL;
936  xmlNode *output = NULL;
937  xmlXPathObjectPtr xpathObj = NULL;
938 
939  CRM_CHECK(devices != NULL, return -EINVAL);
940 
941  data = create_xml_node(NULL, F_STONITH_DEVICE);
942  crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
943  crm_xml_add(data, F_STONITH_TARGET, target);
944  crm_xml_add(data, F_STONITH_ACTION, "off");
945  rc = stonith_send_command(stonith, STONITH_OP_QUERY, data, &output, call_options, timeout);
946 
947  if (rc < 0) {
948  return rc;
949  }
950 
951  xpathObj = xpath_search(output, "//@agent");
952  if (xpathObj) {
953  max = numXpathResults(xpathObj);
954 
955  for (lpc = 0; lpc < max; lpc++) {
956  xmlNode *match = getXpathResult(xpathObj, lpc);
957 
958  CRM_LOG_ASSERT(match != NULL);
959  if(match != NULL) {
960  xmlChar *match_path = xmlGetNodePath(match);
961 
962  crm_info("%s[%d] = %s", "//@agent", lpc, match_path);
963  free(match_path);
964  *devices = stonith_key_value_add(*devices, NULL, crm_element_value(match, XML_ATTR_ID));
965  }
966  }
967 
968  freeXpathObject(xpathObj);
969  }
970 
971  free_xml(output);
972  free_xml(data);
973  return max;
974 }
975 
976 static int
977 stonith_api_call(stonith_t * stonith,
978  int call_options,
979  const char *id,
980  const char *action, const char *victim, int timeout, xmlNode ** output)
981 {
982  int rc = 0;
983  xmlNode *data = NULL;
984 
985  data = create_xml_node(NULL, F_STONITH_DEVICE);
986  crm_xml_add(data, F_STONITH_ORIGIN, __FUNCTION__);
987  crm_xml_add(data, F_STONITH_DEVICE, id);
988  crm_xml_add(data, F_STONITH_ACTION, action);
989  crm_xml_add(data, F_STONITH_TARGET, victim);
990 
991  rc = stonith_send_command(stonith, STONITH_OP_EXEC, data, output, call_options, timeout);
992  free_xml(data);
993 
994  return rc;
995 }
996 
997 static int
998 stonith_api_list(stonith_t * stonith, int call_options, const char *id, char **list_info,
999  int timeout)
1000 {
1001  int rc;
1002  xmlNode *output = NULL;
1003 
1004  rc = stonith_api_call(stonith, call_options, id, "list", NULL, timeout, &output);
1005 
1006  if (output && list_info) {
1007  const char *list_str;
1008 
1009  list_str = crm_element_value(output, "st_output");
1010 
1011  if (list_str) {
1012  *list_info = strdup(list_str);
1013  }
1014  }
1015 
1016  if (output) {
1017  free_xml(output);
1018  }
1019 
1020  return rc;
1021 }
1022 
1023 static int
1024 stonith_api_monitor(stonith_t * stonith, int call_options, const char *id, int timeout)
1025 {
1026  return stonith_api_call(stonith, call_options, id, "monitor", NULL, timeout, NULL);
1027 }
1028 
1029 static int
1030 stonith_api_status(stonith_t * stonith, int call_options, const char *id, const char *port,
1031  int timeout)
1032 {
1033  return stonith_api_call(stonith, call_options, id, "status", port, timeout, NULL);
1034 }
1035 
1036 static int
1037 stonith_api_fence(stonith_t * stonith, int call_options, const char *node, const char *action,
1038  int timeout, int tolerance)
1039 {
1040  int rc = 0;
1041  xmlNode *data = NULL;
1042 
1043  data = create_xml_node(NULL, __FUNCTION__);
1044  crm_xml_add(data, F_STONITH_TARGET, node);
1045  crm_xml_add(data, F_STONITH_ACTION, action);
1046  crm_xml_add_int(data, F_STONITH_TIMEOUT, timeout);
1047  crm_xml_add_int(data, F_STONITH_TOLERANCE, tolerance);
1048 
1049  rc = stonith_send_command(stonith, STONITH_OP_FENCE, data, NULL, call_options, timeout);
1050  free_xml(data);
1051 
1052  return rc;
1053 }
1054 
1055 static int
1056 stonith_api_confirm(stonith_t * stonith, int call_options, const char *target)
1057 {
1058  return stonith_api_fence(stonith, call_options | st_opt_manual_ack, target, "off", 0, 0);
1059 }
1060 
1061 static int
1062 stonith_api_history(stonith_t * stonith, int call_options, const char *node,
1063  stonith_history_t ** history, int timeout)
1064 {
1065  int rc = 0;
1066  xmlNode *data = NULL;
1067  xmlNode *output = NULL;
1068  stonith_history_t *last = NULL;
1069 
1070  *history = NULL;
1071 
1072  if (node) {
1073  data = create_xml_node(NULL, __FUNCTION__);
1074  crm_xml_add(data, F_STONITH_TARGET, node);
1075  }
1076 
1077  rc = stonith_send_command(stonith, STONITH_OP_FENCE_HISTORY, data, &output,
1078  call_options | st_opt_sync_call, timeout);
1079  free_xml(data);
1080 
1081  if (rc == 0) {
1082  xmlNode *op = NULL;
1083  xmlNode *reply = get_xpath_object("//" F_STONITH_HISTORY_LIST, output, LOG_TRACE);
1084 
1085  for (op = __xml_first_child(reply); op != NULL; op = __xml_next(op)) {
1086  stonith_history_t *kvp;
1087  int completed;
1088 
1089  kvp = calloc(1, sizeof(stonith_history_t));
1095  crm_element_value_int(op, F_STONITH_DATE, &completed);
1096  kvp->completed = (time_t) completed;
1098 
1099  if (last) {
1100  last->next = kvp;
1101  } else {
1102  *history = kvp;
1103  }
1104  last = kvp;
1105  }
1106  }
1107 
1108  free_xml(output);
1109 
1110  return rc;
1111 }
1112 
1114 {
1115  stonith_history_t *hp, *hp_old;
1116 
1117  for (hp = history; hp; hp_old = hp, hp = hp->next, free(hp_old)) {
1118  free(hp->target);
1119  free(hp->action);
1120  free(hp->origin);
1121  free(hp->delegate);
1122  free(hp->client);
1123  }
1124 }
1125 
1129 const char *
1130 get_stonith_provider(const char *agent, const char *provider)
1131 {
1132  return stonith_namespace2text(stonith_get_namespace(agent, provider));
1133 }
1134 
1135 static gint
1136 stonithlib_GCompareFunc(gconstpointer a, gconstpointer b)
1137 {
1138  int rc = 0;
1139  const stonith_notify_client_t *a_client = a;
1140  const stonith_notify_client_t *b_client = b;
1141 
1142  CRM_CHECK(a_client->event != NULL && b_client->event != NULL, return 0);
1143  rc = strcmp(a_client->event, b_client->event);
1144  if (rc == 0) {
1145  if (a_client->notify == NULL || b_client->notify == NULL) {
1146  return 0;
1147 
1148  } else if (a_client->notify == b_client->notify) {
1149  return 0;
1150 
1151  } else if (((long)a_client->notify) < ((long)b_client->notify)) {
1152  crm_err("callbacks for %s are not equal: %p vs. %p",
1153  a_client->event, a_client->notify, b_client->notify);
1154  return -1;
1155  }
1156  crm_err("callbacks for %s are not equal: %p vs. %p",
1157  a_client->event, a_client->notify, b_client->notify);
1158  return 1;
1159  }
1160  return rc;
1161 }
1162 
1163 xmlNode *
1164 stonith_create_op(int call_id, const char *token, const char *op, xmlNode * data, int call_options)
1165 {
1166  xmlNode *op_msg = create_xml_node(NULL, "stonith_command");
1167 
1168  CRM_CHECK(op_msg != NULL, return NULL);
1169  CRM_CHECK(token != NULL, return NULL);
1170 
1171  crm_xml_add(op_msg, F_XML_TAGNAME, "stonith_command");
1172 
1173  crm_xml_add(op_msg, F_TYPE, T_STONITH_NG);
1174  crm_xml_add(op_msg, F_STONITH_CALLBACK_TOKEN, token);
1175  crm_xml_add(op_msg, F_STONITH_OPERATION, op);
1176  crm_xml_add_int(op_msg, F_STONITH_CALLID, call_id);
1177  crm_trace("Sending call options: %.8lx, %d", (long)call_options, call_options);
1178  crm_xml_add_int(op_msg, F_STONITH_CALLOPTS, call_options);
1179 
1180  if (data != NULL) {
1181  add_message_xml(op_msg, F_STONITH_CALLDATA, data);
1182  }
1183 
1184  return op_msg;
1185 }
1186 
1187 static void
1188 stonith_destroy_op_callback(gpointer data)
1189 {
1191 
1192  if (blob->timer && blob->timer->ref > 0) {
1193  g_source_remove(blob->timer->ref);
1194  }
1195  free(blob->timer);
1196  free(blob);
1197 }
1198 
1199 static int
1200 stonith_api_signoff(stonith_t * stonith)
1201 {
1202  stonith_private_t *native = stonith->st_private;
1203 
1204  crm_debug("Disconnecting from the fencer");
1205 
1206  if (native->source != NULL) {
1207  /* Attached to mainloop */
1208  mainloop_del_ipc_client(native->source);
1209  native->source = NULL;
1210  native->ipc = NULL;
1211 
1212  } else if (native->ipc) {
1213  /* Not attached to mainloop */
1214  crm_ipc_t *ipc = native->ipc;
1215 
1216  native->ipc = NULL;
1217  crm_ipc_close(ipc);
1218  crm_ipc_destroy(ipc);
1219  }
1220 
1221  free(native->token); native->token = NULL;
1222  stonith->state = stonith_disconnected;
1223  return pcmk_ok;
1224 }
1225 
1226 static int
1227 stonith_api_del_callback(stonith_t * stonith, int call_id, bool all_callbacks)
1228 {
1229  stonith_private_t *private = stonith->st_private;
1230 
1231  if (all_callbacks) {
1232  private->op_callback = NULL;
1233  g_hash_table_destroy(private->stonith_op_callback_table);
1234  private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1235  NULL,
1236  stonith_destroy_op_callback);
1237 
1238  } else if (call_id == 0) {
1239  private->op_callback = NULL;
1240 
1241  } else {
1242  g_hash_table_remove(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1243  }
1244  return pcmk_ok;
1245 }
1246 
1247 static void
1248 invoke_callback(stonith_t * st, int call_id, int rc, void *userdata,
1249  void (*callback) (stonith_t * st, stonith_callback_data_t * data))
1250 {
1251  stonith_callback_data_t data = { 0, };
1252 
1253  data.call_id = call_id;
1254  data.rc = rc;
1255  data.userdata = userdata;
1256 
1257  callback(st, &data);
1258 }
1259 
1260 static void
1261 stonith_perform_callback(stonith_t * stonith, xmlNode * msg, int call_id, int rc)
1262 {
1263  stonith_private_t *private = NULL;
1264  stonith_callback_client_t *blob = NULL;
1265  stonith_callback_client_t local_blob;
1266 
1267  CRM_CHECK(stonith != NULL, return);
1268  CRM_CHECK(stonith->st_private != NULL, return);
1269 
1270  private = stonith->st_private;
1271 
1272  local_blob.id = NULL;
1273  local_blob.callback = NULL;
1274  local_blob.user_data = NULL;
1275  local_blob.only_success = FALSE;
1276 
1277  if (msg != NULL) {
1279  crm_element_value_int(msg, F_STONITH_CALLID, &call_id);
1280  }
1281 
1282  CRM_CHECK(call_id > 0, crm_log_xml_err(msg, "Bad result"));
1283 
1284  blob = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1285 
1286  if (blob != NULL) {
1287  local_blob = *blob;
1288  blob = NULL;
1289 
1290  stonith_api_del_callback(stonith, call_id, FALSE);
1291 
1292  } else {
1293  crm_trace("No callback found for call %d", call_id);
1294  local_blob.callback = NULL;
1295  }
1296 
1297  if (local_blob.callback != NULL && (rc == pcmk_ok || local_blob.only_success == FALSE)) {
1298  crm_trace("Invoking callback %s for call %d", crm_str(local_blob.id), call_id);
1299  invoke_callback(stonith, call_id, rc, local_blob.user_data, local_blob.callback);
1300 
1301  } else if (private->op_callback == NULL && rc != pcmk_ok) {
1302  crm_warn("Fencing command failed: %s", pcmk_strerror(rc));
1303  crm_log_xml_debug(msg, "Failed fence update");
1304  }
1305 
1306  if (private->op_callback != NULL) {
1307  crm_trace("Invoking global callback for call %d", call_id);
1308  invoke_callback(stonith, call_id, rc, NULL, private->op_callback);
1309  }
1310  crm_trace("OP callback activated.");
1311 }
1312 
1313 static gboolean
1314 stonith_async_timeout_handler(gpointer data)
1315 {
1316  struct timer_rec_s *timer = data;
1317 
1318  crm_err("Async call %d timed out after %dms", timer->call_id, timer->timeout);
1319  stonith_perform_callback(timer->stonith, NULL, timer->call_id, -ETIME);
1320 
1321  /* Always return TRUE, never remove the handler
1322  * We do that in stonith_del_callback()
1323  */
1324  return TRUE;
1325 }
1326 
1327 static void
1328 set_callback_timeout(stonith_callback_client_t * callback, stonith_t * stonith, int call_id,
1329  int timeout)
1330 {
1331  struct timer_rec_s *async_timer = callback->timer;
1332 
1333  if (timeout <= 0) {
1334  return;
1335  }
1336 
1337  if (!async_timer) {
1338  async_timer = calloc(1, sizeof(struct timer_rec_s));
1339  callback->timer = async_timer;
1340  }
1341 
1342  async_timer->stonith = stonith;
1343  async_timer->call_id = call_id;
1344  /* Allow a fair bit of grace to allow the server to tell us of a timeout
1345  * This is only a fallback
1346  */
1347  async_timer->timeout = (timeout + 60) * 1000;
1348  if (async_timer->ref) {
1349  g_source_remove(async_timer->ref);
1350  }
1351  async_timer->ref =
1352  g_timeout_add(async_timer->timeout, stonith_async_timeout_handler, async_timer);
1353 }
1354 
1355 static void
1356 update_callback_timeout(int call_id, int timeout, stonith_t * st)
1357 {
1358  stonith_callback_client_t *callback = NULL;
1359  stonith_private_t *private = st->st_private;
1360 
1361  callback = g_hash_table_lookup(private->stonith_op_callback_table, GINT_TO_POINTER(call_id));
1362  if (!callback || !callback->allow_timeout_updates) {
1363  return;
1364  }
1365 
1366  set_callback_timeout(callback, st, call_id, timeout);
1367 }
1368 
1369 static int
1370 stonith_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata)
1371 {
1372  const char *type = NULL;
1373  struct notify_blob_s blob;
1374 
1375  stonith_t *st = userdata;
1376  stonith_private_t *private = NULL;
1377 
1378  CRM_ASSERT(st != NULL);
1379  private = st->st_private;
1380 
1381  blob.stonith = st;
1382  blob.xml = string2xml(buffer);
1383  if (blob.xml == NULL) {
1384  crm_warn("Received malformed message from fencer: %s", buffer);
1385  return 0;
1386  }
1387 
1388  /* do callbacks */
1389  type = crm_element_value(blob.xml, F_TYPE);
1390  crm_trace("Activating %s callbacks...", type);
1391 
1392  if (safe_str_eq(type, T_STONITH_NG)) {
1393  stonith_perform_callback(st, blob.xml, 0, 0);
1394 
1395  } else if (safe_str_eq(type, T_STONITH_NOTIFY)) {
1396  g_list_foreach(private->notify_list, stonith_send_notification, &blob);
1397  } else if (safe_str_eq(type, T_STONITH_TIMEOUT_VALUE)) {
1398  int call_id = 0;
1399  int timeout = 0;
1400 
1401  crm_element_value_int(blob.xml, F_STONITH_TIMEOUT, &timeout);
1402  crm_element_value_int(blob.xml, F_STONITH_CALLID, &call_id);
1403 
1404  update_callback_timeout(call_id, timeout, st);
1405  } else {
1406  crm_err("Unknown message type: %s", type);
1407  crm_log_xml_warn(blob.xml, "BadReply");
1408  }
1409 
1410  free_xml(blob.xml);
1411  return 1;
1412 }
1413 
1414 static int
1415 stonith_api_signon(stonith_t * stonith, const char *name, int *stonith_fd)
1416 {
1417  int rc = pcmk_ok;
1418  stonith_private_t *native = stonith->st_private;
1419 
1420  static struct ipc_client_callbacks st_callbacks = {
1421  .dispatch = stonith_dispatch_internal,
1422  .destroy = stonith_connection_destroy
1423  };
1424 
1425  crm_trace("Connecting command channel");
1426 
1427  stonith->state = stonith_connected_command;
1428  if (stonith_fd) {
1429  /* No mainloop */
1430  native->ipc = crm_ipc_new("stonith-ng", 0);
1431 
1432  if (native->ipc && crm_ipc_connect(native->ipc)) {
1433  *stonith_fd = crm_ipc_get_fd(native->ipc);
1434  } else if (native->ipc) {
1435  crm_perror(LOG_ERR, "Connection to fencer failed");
1436  rc = -ENOTCONN;
1437  }
1438 
1439  } else {
1440  /* With mainloop */
1441  native->source =
1442  mainloop_add_ipc_client("stonith-ng", G_PRIORITY_MEDIUM, 0, stonith, &st_callbacks);
1443  native->ipc = mainloop_get_ipc_client(native->source);
1444  }
1445 
1446  if (native->ipc == NULL) {
1447  crm_debug("Could not connect to the Stonith API");
1448  rc = -ENOTCONN;
1449  }
1450 
1451  if (rc == pcmk_ok) {
1452  xmlNode *reply = NULL;
1453  xmlNode *hello = create_xml_node(NULL, "stonith_command");
1454 
1455  crm_xml_add(hello, F_TYPE, T_STONITH_NG);
1457  crm_xml_add(hello, F_STONITH_CLIENTNAME, name);
1458  rc = crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1, &reply);
1459 
1460  if (rc < 0) {
1461  crm_perror(LOG_DEBUG, "Couldn't complete registration with the fencing API: %d", rc);
1462  rc = -ECOMM;
1463 
1464  } else if (reply == NULL) {
1465  crm_err("Did not receive registration reply");
1466  rc = -EPROTO;
1467 
1468  } else {
1469  const char *msg_type = crm_element_value(reply, F_STONITH_OPERATION);
1470  const char *tmp_ticket = crm_element_value(reply, F_STONITH_CLIENTID);
1471 
1472  if (safe_str_neq(msg_type, CRM_OP_REGISTER)) {
1473  crm_err("Invalid registration message: %s", msg_type);
1474  crm_log_xml_err(reply, "Bad reply");
1475  rc = -EPROTO;
1476 
1477  } else if (tmp_ticket == NULL) {
1478  crm_err("No registration token provided");
1479  crm_log_xml_err(reply, "Bad reply");
1480  rc = -EPROTO;
1481 
1482  } else {
1483  crm_trace("Obtained registration token: %s", tmp_ticket);
1484  native->token = strdup(tmp_ticket);
1485  rc = pcmk_ok;
1486  }
1487  }
1488 
1489  free_xml(reply);
1490  free_xml(hello);
1491  }
1492 
1493  if (rc == pcmk_ok) {
1494 #if HAVE_MSGFROMIPC_TIMEOUT
1495  stonith->call_timeout = MAX_IPC_DELAY;
1496 #endif
1497  crm_debug("Connection to fencer successful");
1498  return pcmk_ok;
1499  }
1500 
1501  crm_debug("Connection to fencer failed: %s", pcmk_strerror(rc));
1502  stonith->cmds->disconnect(stonith);
1503  return rc;
1504 }
1505 
1506 static int
1507 stonith_set_notification(stonith_t * stonith, const char *callback, int enabled)
1508 {
1509  int rc = pcmk_ok;
1510  xmlNode *notify_msg = create_xml_node(NULL, __FUNCTION__);
1511  stonith_private_t *native = stonith->st_private;
1512 
1513  if (stonith->state != stonith_disconnected) {
1514 
1516  if (enabled) {
1517  crm_xml_add(notify_msg, F_STONITH_NOTIFY_ACTIVATE, callback);
1518  } else {
1519  crm_xml_add(notify_msg, F_STONITH_NOTIFY_DEACTIVATE, callback);
1520  }
1521 
1522  rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response, -1, NULL);
1523  if (rc < 0) {
1524  crm_perror(LOG_DEBUG, "Couldn't register for fencing notifications: %d", rc);
1525  rc = -ECOMM;
1526  } else {
1527  rc = pcmk_ok;
1528  }
1529  }
1530 
1531  free_xml(notify_msg);
1532  return rc;
1533 }
1534 
1535 static int
1536 stonith_api_add_notification(stonith_t * stonith, const char *event,
1537  void (*callback) (stonith_t * stonith, stonith_event_t * e))
1538 {
1539  GList *list_item = NULL;
1540  stonith_notify_client_t *new_client = NULL;
1541  stonith_private_t *private = NULL;
1542 
1543  private = stonith->st_private;
1544  crm_trace("Adding callback for %s events (%d)", event, g_list_length(private->notify_list));
1545 
1546  new_client = calloc(1, sizeof(stonith_notify_client_t));
1547  new_client->event = event;
1548  new_client->notify = callback;
1549 
1550  list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1551 
1552  if (list_item != NULL) {
1553  crm_warn("Callback already present");
1554  free(new_client);
1555  return -ENOTUNIQ;
1556 
1557  } else {
1558  private->notify_list = g_list_append(private->notify_list, new_client);
1559 
1560  stonith_set_notification(stonith, event, 1);
1561 
1562  crm_trace("Callback added (%d)", g_list_length(private->notify_list));
1563  }
1564  return pcmk_ok;
1565 }
1566 
1567 static int
1568 stonith_api_del_notification(stonith_t * stonith, const char *event)
1569 {
1570  GList *list_item = NULL;
1571  stonith_notify_client_t *new_client = NULL;
1572  stonith_private_t *private = NULL;
1573 
1574  crm_debug("Removing callback for %s events", event);
1575 
1576  private = stonith->st_private;
1577  new_client = calloc(1, sizeof(stonith_notify_client_t));
1578  new_client->event = event;
1579  new_client->notify = NULL;
1580 
1581  list_item = g_list_find_custom(private->notify_list, new_client, stonithlib_GCompareFunc);
1582 
1583  stonith_set_notification(stonith, event, 0);
1584 
1585  if (list_item != NULL) {
1586  stonith_notify_client_t *list_client = list_item->data;
1587 
1588  private->notify_list = g_list_remove(private->notify_list, list_client);
1589  free(list_client);
1590 
1591  crm_trace("Removed callback");
1592 
1593  } else {
1594  crm_trace("Callback not present");
1595  }
1596  free(new_client);
1597  return pcmk_ok;
1598 }
1599 
1600 static int
1601 stonith_api_add_callback(stonith_t * stonith, int call_id, int timeout, int options,
1602  void *user_data, const char *callback_name,
1603  void (*callback) (stonith_t * st, stonith_callback_data_t * data))
1604 {
1605  stonith_callback_client_t *blob = NULL;
1606  stonith_private_t *private = NULL;
1607 
1608  CRM_CHECK(stonith != NULL, return -EINVAL);
1609  CRM_CHECK(stonith->st_private != NULL, return -EINVAL);
1610  private = stonith->st_private;
1611 
1612  if (call_id == 0) {
1613  private->op_callback = callback;
1614 
1615  } else if (call_id < 0) {
1616  if (!(options & st_opt_report_only_success)) {
1617  crm_trace("Call failed, calling %s: %s", callback_name, pcmk_strerror(call_id));
1618  invoke_callback(stonith, call_id, call_id, user_data, callback);
1619  } else {
1620  crm_warn("Fencer call failed: %s", pcmk_strerror(call_id));
1621  }
1622  return FALSE;
1623  }
1624 
1625  blob = calloc(1, sizeof(stonith_callback_client_t));
1626  blob->id = callback_name;
1627  blob->only_success = (options & st_opt_report_only_success) ? TRUE : FALSE;
1628  blob->user_data = user_data;
1629  blob->callback = callback;
1630  blob->allow_timeout_updates = (options & st_opt_timeout_updates) ? TRUE : FALSE;
1631 
1632  if (timeout > 0) {
1633  set_callback_timeout(blob, stonith, call_id, timeout);
1634  }
1635 
1636  g_hash_table_insert(private->stonith_op_callback_table, GINT_TO_POINTER(call_id), blob);
1637  crm_trace("Added callback to %s for call %d", callback_name, call_id);
1638 
1639  return TRUE;
1640 }
1641 
1642 static void
1643 stonith_dump_pending_op(gpointer key, gpointer value, gpointer user_data)
1644 {
1645  int call = GPOINTER_TO_INT(key);
1646  stonith_callback_client_t *blob = value;
1647 
1648  crm_debug("Call %d (%s): pending", call, crm_str(blob->id));
1649 }
1650 
1651 void
1653 {
1654  stonith_private_t *private = stonith->st_private;
1655 
1656  if (private->stonith_op_callback_table == NULL) {
1657  return;
1658  }
1659  return g_hash_table_foreach(private->stonith_op_callback_table, stonith_dump_pending_op, NULL);
1660 }
1661 
1662 /*
1663  <notify t="st_notify" subt="st_device_register" st_op="st_device_register" st_rc="0" >
1664  <st_calldata >
1665  <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="cts-fence-helper" >
1666  <st_calldata >
1667  <st_device_id id="test-id" origin="create_device_registration_xml" agent="fence_virsh" namespace="stonith-ng" >
1668  <attributes ipaddr="localhost" pcmk-portmal="some-host=pcmk-1 pcmk-3=3,4" login="root" identity_file="/root/.ssh/id_dsa" />
1669  </st_device_id>
1670  </st_calldata>
1671  </stonith_command>
1672  </st_calldata>
1673  </notify>
1674 
1675  <notify t="st_notify" subt="st_notify_fence" st_op="st_notify_fence" st_rc="0" >
1676  <st_calldata >
1677  <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" />
1678  </st_calldata>
1679  </notify>
1680 */
1681 static stonith_event_t *
1682 xml_to_event(xmlNode * msg)
1683 {
1684  stonith_event_t *event = calloc(1, sizeof(stonith_event_t));
1685  const char *ntype = crm_element_value(msg, F_SUBTYPE);
1686  char *data_addr = crm_strdup_printf("//%s", ntype);
1687  xmlNode *data = get_xpath_object(data_addr, msg, LOG_DEBUG);
1688 
1689  crm_log_xml_trace(msg, "stonith_notify");
1690 
1691  crm_element_value_int(msg, F_STONITH_RC, &(event->result));
1692 
1693  if (safe_str_eq(ntype, T_STONITH_NOTIFY_FENCE)) {
1694  event->operation = crm_element_value_copy(msg, F_STONITH_OPERATION);
1695 
1696  if (data) {
1697  event->origin = crm_element_value_copy(data, F_STONITH_ORIGIN);
1698  event->action = crm_element_value_copy(data, F_STONITH_ACTION);
1699  event->target = crm_element_value_copy(data, F_STONITH_TARGET);
1700  event->executioner = crm_element_value_copy(data, F_STONITH_DELEGATE);
1702  event->client_origin = crm_element_value_copy(data, F_STONITH_CLIENTNAME);
1703  event->device = crm_element_value_copy(data, F_STONITH_DEVICE);
1704 
1705  } else {
1706  crm_err("No data for %s event", ntype);
1707  crm_log_xml_notice(msg, "BadEvent");
1708  }
1709  }
1710 
1711  free(data_addr);
1712  return event;
1713 }
1714 
1715 static void
1716 event_free(stonith_event_t * event)
1717 {
1718  free(event->id);
1719  free(event->type);
1720  free(event->message);
1721  free(event->operation);
1722  free(event->origin);
1723  free(event->action);
1724  free(event->target);
1725  free(event->executioner);
1726  free(event->device);
1727  free(event->client_origin);
1728  free(event);
1729 }
1730 
1731 static void
1732 stonith_send_notification(gpointer data, gpointer user_data)
1733 {
1734  struct notify_blob_s *blob = user_data;
1735  stonith_notify_client_t *entry = data;
1736  stonith_event_t *st_event = NULL;
1737  const char *event = NULL;
1738 
1739  if (blob->xml == NULL) {
1740  crm_warn("Skipping callback - NULL message");
1741  return;
1742  }
1743 
1744  event = crm_element_value(blob->xml, F_SUBTYPE);
1745 
1746  if (entry == NULL) {
1747  crm_warn("Skipping callback - NULL callback client");
1748  return;
1749 
1750  } else if (entry->notify == NULL) {
1751  crm_warn("Skipping callback - NULL callback");
1752  return;
1753 
1754  } else if (safe_str_neq(entry->event, event)) {
1755  crm_trace("Skipping callback - event mismatch %p/%s vs. %s", entry, entry->event, event);
1756  return;
1757  }
1758 
1759  st_event = xml_to_event(blob->xml);
1760 
1761  crm_trace("Invoking callback for %p/%s event...", entry, event);
1762  entry->notify(blob->stonith, st_event);
1763  crm_trace("Callback invoked...");
1764 
1765  event_free(st_event);
1766 }
1767 
1768 int
1769 stonith_send_command(stonith_t * stonith, const char *op, xmlNode * data, xmlNode ** output_data,
1770  int call_options, int timeout)
1771 {
1772  int rc = 0;
1773  int reply_id = -1;
1774  enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
1775 
1776  xmlNode *op_msg = NULL;
1777  xmlNode *op_reply = NULL;
1778 
1779  stonith_private_t *native = stonith->st_private;
1780 
1781  if (stonith->state == stonith_disconnected) {
1782  return -ENOTCONN;
1783  }
1784 
1785  if (output_data != NULL) {
1786  *output_data = NULL;
1787  }
1788 
1789  if (op == NULL) {
1790  crm_err("No operation specified");
1791  return -EINVAL;
1792  }
1793 
1794  if (call_options & st_opt_sync_call) {
1795  ipc_flags |= crm_ipc_client_response;
1796  }
1797 
1798  stonith->call_id++;
1799  /* prevent call_id from being negative (or zero) and conflicting
1800  * with the stonith_errors enum
1801  * use 2 because we use it as (stonith->call_id - 1) below
1802  */
1803  if (stonith->call_id < 1) {
1804  stonith->call_id = 1;
1805  }
1806 
1807  CRM_CHECK(native->token != NULL,;
1808  );
1809  op_msg = stonith_create_op(stonith->call_id, native->token, op, data, call_options);
1810  if (op_msg == NULL) {
1811  return -EINVAL;
1812  }
1813 
1814  crm_xml_add_int(op_msg, F_STONITH_TIMEOUT, timeout);
1815  crm_trace("Sending %s message to fencer with timeout %ds", op, timeout);
1816 
1817  rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, 1000 * (timeout + 60), &op_reply);
1818  free_xml(op_msg);
1819 
1820  if (rc < 0) {
1821  crm_perror(LOG_ERR, "Couldn't perform %s operation (timeout=%ds): %d", op, timeout, rc);
1822  rc = -ECOMM;
1823  goto done;
1824  }
1825 
1826  crm_log_xml_trace(op_reply, "Reply");
1827 
1828  if (!(call_options & st_opt_sync_call)) {
1829  crm_trace("Async call %d, returning", stonith->call_id);
1830  CRM_CHECK(stonith->call_id != 0, return -EPROTO);
1831  free_xml(op_reply);
1832 
1833  return stonith->call_id;
1834  }
1835 
1836  rc = pcmk_ok;
1837  crm_element_value_int(op_reply, F_STONITH_CALLID, &reply_id);
1838 
1839  if (reply_id == stonith->call_id) {
1840  crm_trace("Synchronous reply %d received", reply_id);
1841 
1842  if (crm_element_value_int(op_reply, F_STONITH_RC, &rc) != 0) {
1843  rc = -ENOMSG;
1844  }
1845 
1846  if ((call_options & st_opt_discard_reply) || output_data == NULL) {
1847  crm_trace("Discarding reply");
1848 
1849  } else {
1850  *output_data = op_reply;
1851  op_reply = NULL; /* Prevent subsequent free */
1852  }
1853 
1854  } else if (reply_id <= 0) {
1855  crm_err("Received bad reply: No id set");
1856  crm_log_xml_err(op_reply, "Bad reply");
1857  free_xml(op_reply);
1858  rc = -ENOMSG;
1859 
1860  } else {
1861  crm_err("Received bad reply: %d (wanted %d)", reply_id, stonith->call_id);
1862  crm_log_xml_err(op_reply, "Old reply");
1863  free_xml(op_reply);
1864  rc = -ENOMSG;
1865  }
1866 
1867  done:
1868  if (crm_ipc_connected(native->ipc) == FALSE) {
1869  crm_err("Fencer disconnected");
1870  stonith->state = stonith_disconnected;
1871  }
1872 
1873  free_xml(op_reply);
1874  return rc;
1875 }
1876 
1877 /* Not used with mainloop */
1878 bool
1880 {
1881  gboolean stay_connected = TRUE;
1882  stonith_private_t *private = NULL;
1883 
1884  CRM_ASSERT(st != NULL);
1885  private = st->st_private;
1886 
1887  while (crm_ipc_ready(private->ipc)) {
1888 
1889  if (crm_ipc_read(private->ipc) > 0) {
1890  const char *msg = crm_ipc_buffer(private->ipc);
1891 
1892  stonith_dispatch_internal(msg, strlen(msg), st);
1893  }
1894 
1895  if (crm_ipc_connected(private->ipc) == FALSE) {
1896  crm_err("Connection closed");
1897  stay_connected = FALSE;
1898  }
1899  }
1900 
1901  return stay_connected;
1902 }
1903 
1904 static int
1905 stonith_api_free(stonith_t * stonith)
1906 {
1907  int rc = pcmk_ok;
1908 
1909  crm_trace("Destroying %p", stonith);
1910 
1911  if (stonith->state != stonith_disconnected) {
1912  crm_trace("Disconnecting %p first", stonith);
1913  rc = stonith->cmds->disconnect(stonith);
1914  }
1915 
1916  if (stonith->state == stonith_disconnected) {
1917  stonith_private_t *private = stonith->st_private;
1918 
1919  crm_trace("Removing %d callbacks", g_hash_table_size(private->stonith_op_callback_table));
1920  g_hash_table_destroy(private->stonith_op_callback_table);
1921 
1922  crm_trace("Destroying %d notification clients", g_list_length(private->notify_list));
1923  g_list_free_full(private->notify_list, free);
1924 
1925  free(stonith->st_private);
1926  free(stonith->cmds);
1927  free(stonith);
1928 
1929  } else {
1930  crm_err("Not free'ing active connection: %s (%d)", pcmk_strerror(rc), rc);
1931  }
1932 
1933  return rc;
1934 }
1935 
1936 void
1938 {
1939  crm_trace("Destroying %p", stonith);
1940  if(stonith) {
1941  stonith->cmds->free(stonith);
1942  }
1943 }
1944 
1945 static int
1946 stonith_api_validate(stonith_t *st, int call_options, const char *rsc_id,
1947  const char *namespace_s, const char *agent,
1948  stonith_key_value_t *params, int timeout, char **output,
1949  char **error_output)
1950 {
1951  /* Validation should be done directly via the agent, so we can get it from
1952  * stonith_admin when the cluster is not running, which is important for
1953  * higher-level tools.
1954  */
1955 
1956  int rc = pcmk_ok;
1957 
1958  /* Use a dummy node name in case the agent requires a target. We assume the
1959  * actual target doesn't matter for validation purposes (if in practice,
1960  * that is incorrect, we will need to allow the caller to pass the target).
1961  */
1962  const char *target = "node1";
1963 
1964  GHashTable *params_table = crm_str_table_new();
1965 
1966  // Convert parameter list to a hash table
1967  for (; params; params = params->next) {
1968 
1969  // Strip out Pacemaker-implemented parameters
1970  if (!crm_starts_with(params->key, "pcmk_")
1971  && strcmp(params->key, "provides")
1972  && strcmp(params->key, "stonith-timeout")) {
1973  g_hash_table_insert(params_table, strdup(params->key),
1974  strdup(params->value));
1975  }
1976  }
1977 
1978 #if SUPPORT_CIBSECRETS
1979  rc = replace_secret_params(rsc_id, params_table);
1980  if (rc < 0) {
1981  crm_warn("Could not replace secret parameters for validation of %s: %s",
1982  agent, pcmk_strerror(rc));
1983  }
1984 #endif
1985 
1986  if (output) {
1987  *output = NULL;
1988  }
1989  if (error_output) {
1990  *error_output = NULL;
1991  }
1992 
1993  switch (stonith_get_namespace(agent, namespace_s)) {
1994  case st_namespace_rhcs:
1995  rc = stonith__rhcs_validate(st, call_options, target, agent,
1996  params_table, timeout, output,
1997  error_output);
1998  break;
1999 
2000 #if HAVE_STONITH_STONITH_H
2001  case st_namespace_lha:
2002  rc = stonith__lha_validate(st, call_options, target, agent,
2003  params_table, timeout, output,
2004  error_output);
2005  break;
2006 #endif
2007 
2008  default:
2009  rc = -EINVAL;
2010  errno = EINVAL;
2011  crm_perror(LOG_ERR,
2012  "Agent %s not found or does not support validation",
2013  agent);
2014  break;
2015  }
2016  g_hash_table_destroy(params_table);
2017  return rc;
2018 }
2019 
2020 stonith_t *
2022 {
2023  stonith_t *new_stonith = NULL;
2024  stonith_private_t *private = NULL;
2025 
2026  new_stonith = calloc(1, sizeof(stonith_t));
2027  private = calloc(1, sizeof(stonith_private_t));
2028  new_stonith->st_private = private;
2029 
2030  private->stonith_op_callback_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2031  NULL, stonith_destroy_op_callback);
2032  private->notify_list = NULL;
2033 
2034  new_stonith->call_id = 1;
2035  new_stonith->state = stonith_disconnected;
2036 
2037  new_stonith->cmds = calloc(1, sizeof(stonith_api_operations_t));
2038 
2039 /* *INDENT-OFF* */
2040  new_stonith->cmds->free = stonith_api_free;
2041  new_stonith->cmds->connect = stonith_api_signon;
2042  new_stonith->cmds->disconnect = stonith_api_signoff;
2043 
2044  new_stonith->cmds->list = stonith_api_list;
2045  new_stonith->cmds->monitor = stonith_api_monitor;
2046  new_stonith->cmds->status = stonith_api_status;
2047  new_stonith->cmds->fence = stonith_api_fence;
2048  new_stonith->cmds->confirm = stonith_api_confirm;
2049  new_stonith->cmds->history = stonith_api_history;
2050 
2051  new_stonith->cmds->list_agents = stonith_api_device_list;
2052  new_stonith->cmds->metadata = stonith_api_device_metadata;
2053 
2054  new_stonith->cmds->query = stonith_api_query;
2055  new_stonith->cmds->remove_device = stonith_api_remove_device;
2056  new_stonith->cmds->register_device = stonith_api_register_device;
2057 
2058  new_stonith->cmds->remove_level = stonith_api_remove_level;
2059  new_stonith->cmds->remove_level_full = stonith_api_remove_level_full;
2060  new_stonith->cmds->register_level = stonith_api_register_level;
2061  new_stonith->cmds->register_level_full = stonith_api_register_level_full;
2062 
2063  new_stonith->cmds->remove_callback = stonith_api_del_callback;
2064  new_stonith->cmds->register_callback = stonith_api_add_callback;
2065  new_stonith->cmds->remove_notification = stonith_api_del_notification;
2066  new_stonith->cmds->register_notification = stonith_api_add_notification;
2067 
2068  new_stonith->cmds->validate = stonith_api_validate;
2069 /* *INDENT-ON* */
2070 
2071  return new_stonith;
2072 }
2073 
2075 stonith_key_value_add(stonith_key_value_t * head, const char *key, const char *value)
2076 {
2077  stonith_key_value_t *p, *end;
2078 
2079  p = calloc(1, sizeof(stonith_key_value_t));
2080  if (key) {
2081  p->key = strdup(key);
2082  }
2083  if (value) {
2084  p->value = strdup(value);
2085  }
2086 
2087  end = head;
2088  while (end && end->next) {
2089  end = end->next;
2090  }
2091 
2092  if (end) {
2093  end->next = p;
2094  } else {
2095  head = p;
2096  }
2097 
2098  return head;
2099 }
2100 
2101 void
2102 stonith_key_value_freeall(stonith_key_value_t * head, int keys, int values)
2103 {
2105 
2106  while (head) {
2107  p = head->next;
2108  if (keys) {
2109  free(head->key);
2110  }
2111  if (values) {
2112  free(head->value);
2113  }
2114  free(head);
2115  head = p;
2116  }
2117 }
2118 
2119 #define api_log_open() openlog("stonith-api", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON)
2120 #define api_log(level, fmt, args...) syslog(level, "%s: "fmt, __FUNCTION__, args)
2121 
2122 int
2123 stonith_api_kick(uint32_t nodeid, const char *uname, int timeout, bool off)
2124 {
2125  char *name = NULL;
2126  const char *action = "reboot";
2127 
2128  int rc = -EPROTO;
2129  stonith_t *st = NULL;
2131 
2132  api_log_open();
2133  st = stonith_api_new();
2134  if (st) {
2135  rc = st->cmds->connect(st, "stonith-api", NULL);
2136  if(rc != pcmk_ok) {
2137  api_log(LOG_ERR, "Connection failed, could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname, pcmk_strerror(rc), rc);
2138  }
2139  }
2140 
2141  if (uname != NULL) {
2142  name = strdup(uname);
2143 
2144  } else if (nodeid > 0) {
2145  opts |= st_opt_cs_nodeid;
2146  name = crm_itoa(nodeid);
2147  }
2148 
2149  if (off) {
2150  action = "off";
2151  }
2152 
2153  if (rc == pcmk_ok) {
2154  rc = st->cmds->fence(st, opts, name, action, timeout, 0);
2155  if(rc != pcmk_ok) {
2156  api_log(LOG_ERR, "Could not kick (%s) node %u/%s : %s (%d)", action, nodeid, uname, pcmk_strerror(rc), rc);
2157  } else {
2158  api_log(LOG_NOTICE, "Node %u/%s kicked: %s ", nodeid, uname, action);
2159  }
2160  }
2161 
2162  if (st) {
2163  st->cmds->disconnect(st);
2164  stonith_api_delete(st);
2165  }
2166 
2167  free(name);
2168  return rc;
2169 }
2170 
2171 time_t
2172 stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
2173 {
2174  int rc = 0;
2175  char *name = NULL;
2176 
2177  time_t when = 0;
2178  stonith_t *st = NULL;
2179  stonith_history_t *history = NULL, *hp = NULL;
2181 
2182  st = stonith_api_new();
2183  if (st) {
2184  rc = st->cmds->connect(st, "stonith-api", NULL);
2185  if(rc != pcmk_ok) {
2186  api_log(LOG_NOTICE, "Connection failed: %s (%d)", pcmk_strerror(rc), rc);
2187  }
2188  }
2189 
2190  if (uname != NULL) {
2191  name = strdup(uname);
2192 
2193  } else if (nodeid > 0) {
2194  opts |= st_opt_cs_nodeid;
2195  name = crm_itoa(nodeid);
2196  }
2197 
2198  if (st && rc == pcmk_ok) {
2199  int entries = 0;
2200  int progress = 0;
2201  int completed = 0;
2202 
2203  rc = st->cmds->history(st, opts, name, &history, 120);
2204 
2205  for (hp = history; hp; hp = hp->next) {
2206  entries++;
2207  if (in_progress) {
2208  progress++;
2209  if (hp->state != st_done && hp->state != st_failed) {
2210  when = time(NULL);
2211  }
2212 
2213  } else if (hp->state == st_done) {
2214  completed++;
2215  if (hp->completed > when) {
2216  when = hp->completed;
2217  }
2218  }
2219  }
2220 
2221  stonith_history_free(history);
2222 
2223  if(rc == pcmk_ok) {
2224  api_log(LOG_INFO, "Found %d entries for %u/%s: %d in progress, %d completed", entries, nodeid, uname, progress, completed);
2225  } else {
2226  api_log(LOG_ERR, "Could not retrieve fence history for %u/%s: %s (%d)", nodeid, uname, pcmk_strerror(rc), rc);
2227  }
2228  }
2229 
2230  if (st) {
2231  st->cmds->disconnect(st);
2232  stonith_api_delete(st);
2233  }
2234 
2235  if(when) {
2236  api_log(LOG_INFO, "Node %u/%s last kicked at: %ld", nodeid, uname, (long int)when);
2237  }
2238  free(name);
2239  return when;
2240 }
2241 
2242 bool
2243 stonith_agent_exists(const char *agent, int timeout)
2244 {
2245  stonith_t *st = NULL;
2246  stonith_key_value_t *devices = NULL;
2247  stonith_key_value_t *dIter = NULL;
2248  bool rc = FALSE;
2249 
2250  if (agent == NULL) {
2251  return rc;
2252  }
2253 
2254  st = stonith_api_new();
2255  st->cmds->list_agents(st, st_opt_sync_call, NULL, &devices, timeout == 0 ? 120 : timeout);
2256 
2257  for (dIter = devices; dIter != NULL; dIter = dIter->next) {
2258  if (crm_str_eq(dIter->value, agent, TRUE)) {
2259  rc = TRUE;
2260  break;
2261  }
2262  }
2263 
2264  stonith_key_value_freeall(devices, 1, 1);
2265  stonith_api_delete(st);
2266  return rc;
2267 }
2268 
2269 const char *
2270 stonith_action_str(const char *action)
2271 {
2272  if (action == NULL) {
2273  return "fencing";
2274  } else if (!strcmp(action, "on")) {
2275  return "unfencing";
2276  } else if (!strcmp(action, "off")) {
2277  return "turning off";
2278  } else {
2279  return action;
2280  }
2281 }
#define LOG_TRACE
Definition: logging.h:26
void stonith__destroy_action(stonith_action_t *action)
Definition: st_client.c:549
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:156
char uname[MAX_NAME]
Definition: internal.h:87
int stonith_send_command(stonith_t *stonith, const char *op, xmlNode *data, xmlNode **output_data, int call_options, int timeout)
Definition: st_client.c:1769
#define XML_ATTR_STONITH_TARGET_ATTRIBUTE
Definition: msg_xml.h:397
struct stonith_action_s stonith_action_t
Definition: internal.h:18
#define F_STONITH_REMOTE_OP_ID
Definition: internal.h:60
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:263
struct stonith_history_s * next
Definition: stonith-ng.h:105
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
Definition: ipc.c:955
A dumping ground.
#define F_TYPE
Definition: msg_xml.h:30
#define F_STONITH_CLIENTID
Definition: internal.h:54
const char * pcmk_strerror(int rc)
Definition: results.c:188
#define ETIME
Definition: portability.h:162
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:157
void services_action_free(svc_action_t *op)
Definition: services.c:463
void stonith__action_result(stonith_action_t *action, int *rc, char **output, char **error_output)
Definition: st_client.c:580
stonith_t * stonith
Definition: st_client.c:101
int(* register_notification)(stonith_t *st, const char *event, void(*notify)(stonith_t *st, stonith_event_t *e))
Definition: stonith-ng.h:302
#define api_log_open()
Definition: st_client.c:2119
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:374
const char * get_stonith_provider(const char *agent, const char *provider)
Deprecated (use stonith_get_namespace() instead)
Definition: st_client.c:1130
void hash2field(gpointer key, gpointer value, gpointer user_data)
Set XML attribute based on hash table entry.
Definition: nvpair.c:615
int stonith__rhcs_metadata(const char *agent, int timeout, char **output)
Execute RHCS-compatible agent&#39;s meta-data action.
Definition: st_rhcs.c:98
char * standard
Definition: services.h:154
int stonith__execute(stonith_action_t *action)
Definition: st_client.c:856
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:357
#define crm_log_output(level, prefix, output)
Definition: logging.h:84
int crm_ipc_get_fd(crm_ipc_t *client)
Definition: ipc.c:1061
char * id
Definition: services.h:149
int stonith_api_kick(uint32_t nodeid, const char *uname, int timeout, bool off)
Definition: st_client.c:2123
#define F_SUBTYPE
Definition: msg_xml.h:26
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:300
#define F_STONITH_CALLBACK_TOKEN
Definition: internal.h:80
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:203
int stonith__lha_validate(stonith_t *st, int call_options, const char *target, const char *agent, GHashTable *params, int timeout, char **output, char **error_output)
Definition: st_lha.c:251
#define F_STONITH_CLIENTNAME
Definition: internal.h:81
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
Definition: nvpair.c:383
enum stonith_namespace stonith_get_namespace(const char *agent, const char *namespace_s)
Determine namespace of a fence agent.
Definition: st_client.c:173
xmlNode * stonith_create_op(int call_id, const char *token, const char *op, xmlNode *data, int call_options)
Definition: st_client.c:1164
#define XML_TAG_FENCING_LEVEL
Definition: msg_xml.h:392
#define STONITH_OP_FENCE
Definition: internal.h:122
struct stonith_key_value_s * next
Definition: stonith-ng.h:94
#define XML_TAG_ATTRS
Definition: msg_xml.h:165
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:32
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:244
svc_action_t * services_action_create_generic(const char *exec, const char *args[])
Definition: services.c:302
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:275
gboolean services_action_async_fork_notify(svc_action_t *op, void(*action_callback)(svc_action_t *), void(*action_fork_callback)(svc_action_t *))
Definition: services.c:731
#define pcmk_err_generic
Definition: results.h:60
#define F_STONITH_TIMEOUT
Definition: internal.h:63
int stonith__lha_metadata(const char *agent, int timeout, char **output)
Definition: st_lha.c:152
int(* free)(stonith_t *st)
Destroy the stonith api structure.
Definition: stonith-ng.h:142
#define MAX_IPC_DELAY
Definition: crm.h:56
#define T_STONITH_TIMEOUT_VALUE
Definition: internal.h:106
#define CRM_LOG_ASSERT(expr)
Definition: logging.h:142
stonith_namespace
Definition: stonith-ng.h:74
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:236
long crm_ipc_read(crm_ipc_t *client)
Definition: ipc.c:1170
uint32_t pid
Definition: internal.h:83
int call_id
Definition: internal.h:99
#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:290
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:459
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:220
Wrappers for and extensions to glib mainloop.
bool crm_starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:259
#define STONITH_OP_LEVEL_DEL
Definition: internal.h:128
bool stonith_dispatch(stonith_t *st)
Definition: st_client.c:1879
int stonith_action_execute_async(stonith_action_t *action, void *userdata, void(*done)(GPid pid, int rc, const char *output, gpointer user_data), void(*fork_cb)(GPid pid, gpointer user_data))
Definition: st_client.c:829
#define STONITH_OP_DEVICE_ADD
Definition: internal.h:124
#define STONITH_OP_EXEC
Definition: internal.h:119
stonith_t * stonith_api_new(void)
Definition: st_client.c:2021
#define CRM_OP_REGISTER
Definition: crm.h:120
xmlNode * string2xml(const char *input)
Definition: xml.c:2058
const char * crm_ipc_buffer(crm_ipc_t *client)
Definition: ipc.c:1217
int(* stonith_op_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
Definition: st_client.c:104
int stonith__list_lha_agents(stonith_key_value_t **devices)
Definition: st_lha.c:84
#define CRM_TRACE_INIT_DATA(name)
Definition: logging.h:111
uint32_t id
Definition: internal.h:82
#define F_STONITH_ACTION
Definition: internal.h:98
stonith_key_value_t * stonith_key_value_add(stonith_key_value_t *kvp, const char *key, const char *value)
Definition: st_client.c:2075
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:335
#define T_STONITH_NG
Definition: internal.h:101
int timeout
Definition: internal.h:100
#define XML_ATTR_STONITH_TARGET_PATTERN
Definition: msg_xml.h:396
#define crm_warn(fmt, args...)
Definition: logging.h:241
bool stonith__agent_is_rhcs(const char *agent)
Definition: st_rhcs.c:181
bool stonith_agent_exists(const char *agent, int timeout)
Definition: st_client.c:2243
#define STONITH_ATTR_HOSTARG
Definition: internal.h:109
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 asynchronous call.
Definition: stonith-ng.h:324
#define F_STONITH_CALLID
Definition: internal.h:56
#define crm_debug(fmt, args...)
Definition: logging.h:245
void log_action(unsigned int log_level, const char *pre_text, action_t *action, gboolean details)
#define STONITH_OP_LEVEL_ADD
Definition: internal.h:127
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:58
enum stonith_state state
Definition: stonith-ng.h:406
const char * stonith_action_str(const char *action)
Turn stonith action into a more readable string.
Definition: st_client.c:2270
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
Definition: nvpair.c:556
#define XML_ATTR_ID
Definition: msg_xml.h:96
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:423
#define STONITH_OP_QUERY
Definition: internal.h:121
gboolean services_action_sync(svc_action_t *op)
Definition: services.c:918
char * stdout_data
Definition: services.h:174
#define F_STONITH_DEVICE
Definition: internal.h:97
#define XML_ATTR_STONITH_DEVICES
Definition: msg_xml.h:398
GHashTable * params
Definition: services.h:159
guint ref
Definition: internal.h:101
#define crm_trace(fmt, args...)
Definition: logging.h:246
#define XML_ATTR_STONITH_INDEX
Definition: msg_xml.h:393
int(* metadata)(stonith_t *st, int options, const char *device, const char *provider, char **output, int timeout)
Get the metadata documentation for a resource.
Definition: stonith-ng.h:214
#define crm_log_xml_debug(xml, text)
Definition: logging.h:253
#define F_STONITH_RC
Definition: internal.h:61
char * agent
Definition: services.h:156
#define XML_ATTR_STONITH_TARGET
Definition: msg_xml.h:394
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&#39;s port is reachable.
Definition: stonith-ng.h:252
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:1890
#define F_STONITH_STATE
Definition: internal.h:93
#define crm_log_xml_warn(xml, text)
Definition: logging.h:250
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:191
int(* list_agents)(stonith_t *stonith, int call_options, const char *provider, stonith_key_value_t **devices, int timeout)
Retrieve a list of installed stonith agents.
Definition: stonith-ng.h:227
void stonith_key_value_freeall(stonith_key_value_t *kvp, int keys, int values)
Definition: st_client.c:2102
#define T_STONITH_NOTIFY_DISCONNECT
Definition: stonith-ng.h:29
#define STONITH_OP_DEVICE_DEL
Definition: internal.h:125
#define ECOMM
Definition: portability.h:138
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:849
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc.c:1038
#define XML_ATTR_STONITH_TARGET_VALUE
Definition: msg_xml.h:395
#define F_STONITH_TARGET
Definition: internal.h:59
struct stonith_notify_client_s stonith_notify_client_t
void stonith_dump_pending_callbacks(stonith_t *st)
Definition: st_client.c:1652
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
Definition: xml.c:2512
void free_xml(xmlNode *child)
Definition: xml.c:2014
#define api_log(level, fmt, args...)
Definition: st_client.c:2120
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:220
int sequence
Definition: services.h:168
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:353
#define STONITH_ATTR_ACTION_OP
Definition: internal.h:117
#define STONITH_OP_FENCE_HISTORY
Definition: internal.h:126
#define F_STONITH_CALLOPTS
Definition: internal.h:55
int call_timeout
Definition: stonith-ng.h:409
struct stonith_private_s stonith_private_t
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc.c:1075
#define PCMK_RESOURCE_CLASS_STONITH
Definition: services.h:49
stonith_call_options
Definition: stonith-ng.h:40
int crm_ipc_ready(crm_ipc_t *client)
Check whether an IPC connection is ready to be read.
Definition: ipc.c:1107
char * client_origin
Definition: stonith-ng.h:126
#define F_STONITH_DATE
Definition: internal.h:92
#define ENODATA
Definition: portability.h:158
int(* register_device)(stonith_t *st, int options, const char *id, const char *provider, const char *agent, stonith_key_value_t *params)
Register a stonith device with the local stonith daemon.
Definition: stonith-ng.h:179
int replace_secret_params(const char *rsc_id, GHashTable *params)
Definition: cib_secrets.c:90
#define crm_log_xml_err(xml, text)
Definition: logging.h:249
struct stonith_callback_client_s stonith_callback_client_t
void stonith_api_delete(stonith_t *st)
Definition: st_client.c:1937
int stonith__list_rhcs_agents(stonith_key_value_t **devices)
Definition: st_rhcs.c:31
#define crm_perror(level, fmt, args...)
Log a system error message.
Definition: logging.h:218
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:855
#define CRM_META
Definition: crm.h:49
#define crm_err(fmt, args...)
Definition: logging.h:240
#define G_PRIORITY_MEDIUM
Definition: mainloop.h:129
#define CRM_ASSERT(expr)
Definition: results.h:42
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
Definition: xpath.c:145
#define ENOTUNIQ
Definition: portability.h:134
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:281
stonith_api_operations_t * cmds
Definition: stonith-ng.h:412
int crm_ipc_send(crm_ipc_t *client, xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
Definition: ipc.c:1320
#define FAILURE_MAX_RETRIES
Definition: st_client.c:607
void stonith_history_free(stonith_history_t *history)
Definition: st_client.c:1113
#define crm_log_xml_notice(xml, text)
Definition: logging.h:251
Fencing aka. STONITH.
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
Definition: xpath.c:64
char * executioner
Definition: stonith-ng.h:121
int(* connect)(stonith_t *st, const char *name, int *stonith_fd)
Connect to the local stonith daemon.
Definition: stonith-ng.h:150
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
Definition: ipc.c:925
char * operation
Definition: stonith-ng.h:115
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:168
char data[0]
Definition: internal.h:92
#define crm_str(x)
Definition: logging.h:266
#define F_STONITH_ORIGIN
Definition: internal.h:90
#define pcmk_ok
Definition: results.h:57
int(* disconnect)(stonith_t *st)
Disconnect from the local stonith daemon.
Definition: stonith-ng.h:158
bool stonith__agent_is_lha(const char *agent)
Determine namespace of a fence agent.
Definition: st_lha.c:57
time_t stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
Definition: st_client.c:2172
#define T_STONITH_NOTIFY_FENCE
Definition: stonith-ng.h:30
void * st_private
Definition: stonith-ng.h:410
void * cb_data
Definition: services.h:182
#define T_STONITH_NOTIFY
Definition: internal.h:107
#define crm_log_xml_trace(xml, text)
Definition: logging.h:254
#define F_STONITH_OPERATION
Definition: internal.h:58
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:821
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:609
enum stonith_namespace stonith_text2namespace(const char *namespace_s)
Get agent namespace by name.
Definition: st_client.c:126
int(* validate)(stonith_t *st, int call_options, const char *rsc_id, const char *namespace_s, const char *agent, stonith_key_value_t *params, int timeout, char **output, char **error_output)
Validate an arbitrary stonith device configuration.
Definition: stonith-ng.h:397
#define safe_str_eq(a, b)
Definition: util.h:59
int stonith__rhcs_validate(stonith_t *st, int call_options, const char *target, const char *agent, GHashTable *params, int timeout, char **output, char **error_output)
Definition: st_rhcs.c:192
#define F_STONITH_NOTIFY_ACTIVATE
Definition: internal.h:83
#define F_XML_TAGNAME
Definition: msg_xml.h:38
const char * stonith_namespace2text(enum stonith_namespace st_namespace)
Get agent namespace name.
Definition: st_client.c:152
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:45
crm_ipc_flags
Definition: ipc.h:39
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc.c:1023
#define F_STONITH_CALLDATA
Definition: internal.h:57
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#define F_STONITH_DELEGATE
Definition: internal.h:85
#define crm_info(fmt, args...)
Definition: logging.h:243
int call_id
Definition: stonith-ng.h:408
xmlNode * create_device_registration_xml(const char *id, enum stonith_namespace namespace, const char *agent, stonith_key_value_t *params, const char *rsc_provides)
Definition: st_client.c:237
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
Definition: mainloop.h:75
#define F_STONITH_HISTORY_LIST
Definition: internal.h:91
int(* remove_notification)(stonith_t *st, const char *event)
Definition: stonith-ng.h:305
enum crm_ais_msg_types type
Definition: internal.h:85
#define F_STONITH_TOLERANCE
Definition: internal.h:64
char * stderr_data
Definition: services.h:173
#define RH_STONITH_DIR
Definition: config.h:544