pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pcmk_fence.c
Go to the documentation of this file.
1 /*
2  * Copyright 2009-2020 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 #include <crm/common/mainloop.h>
12 #include <crm/common/output.h>
13 #include <crm/common/results.h>
14 #include <crm/stonith-ng.h>
15 #include <crm/fencing/internal.h>
16 #include <glib.h>
17 #include <libxml/tree.h>
18 #include <pacemaker.h>
19 #include <pcmki/pcmki_output.h>
20 #include <pcmki/pcmki_fence.h>
21 
22 static int st_opts = st_opt_sync_call | st_opt_allow_suicide;
23 
24 static GMainLoop *mainloop = NULL;
25 
26 static struct {
28  const char *target;
29  const char *action;
30  char *name;
31  unsigned int timeout;
32  unsigned int tolerance;
33  int delay;
34  int rc;
35 } async_fence_data;
36 
37 static int
38 handle_level(stonith_t *st, char *target, int fence_level,
39  stonith_key_value_t *devices, bool added) {
40  char *node = NULL;
41  char *pattern = NULL;
42  char *name = NULL;
43  char *value = NULL;
44  int rc = pcmk_rc_ok;
45 
46  if (target == NULL) {
47  // Not really possible, but makes static analysis happy
48  return EINVAL;
49  }
50 
51  /* Determine if targeting by attribute, node name pattern or node name */
52  value = strchr(target, '=');
53  if (value != NULL) {
54  name = target;
55  *value++ = '\0';
56  } else if (*target == '@') {
57  pattern = target + 1;
58  } else {
59  node = target;
60  }
61 
62  /* Register or unregister level as appropriate */
63  if (added) {
64  rc = st->cmds->register_level_full(st, st_opts, node, pattern,
65  name, value, fence_level,
66  devices);
67  } else {
68  rc = st->cmds->remove_level_full(st, st_opts, node, pattern,
69  name, value, fence_level);
70  }
71 
72  return pcmk_legacy2rc(rc);
73 }
74 
75 static void
76 notify_callback(stonith_t * st, stonith_event_t * e)
77 {
78  if (e->result != pcmk_ok) {
79  return;
80  }
81 
82  if (safe_str_eq(async_fence_data.target, e->target) &&
83  safe_str_eq(async_fence_data.action, e->action)) {
84 
85  async_fence_data.rc = e->result;
86  g_main_loop_quit(mainloop);
87  }
88 }
89 
90 static void
91 fence_callback(stonith_t * stonith, stonith_callback_data_t * data)
92 {
93  async_fence_data.rc = data->rc;
94 
95  g_main_loop_quit(mainloop);
96 }
97 
98 static gboolean
99 async_fence_helper(gpointer user_data)
100 {
101  stonith_t *st = async_fence_data.st;
102  int call_id = 0;
103  int rc = stonith_api_connect_retry(st, async_fence_data.name, 10);
104 
105  if (rc != pcmk_ok) {
106  fprintf(stderr, "Could not connect to fencer: %s\n", pcmk_strerror(rc));
107  g_main_loop_quit(mainloop);
108  return TRUE;
109  }
110 
111  st->cmds->register_notification(st, T_STONITH_NOTIFY_FENCE, notify_callback);
112 
113  call_id = st->cmds->fence_with_delay(st,
115  async_fence_data.target,
116  async_fence_data.action,
117  async_fence_data.timeout/1000,
118  async_fence_data.tolerance/1000,
119  async_fence_data.delay);
120 
121  if (call_id < 0) {
122  g_main_loop_quit(mainloop);
123  return TRUE;
124  }
125 
126  st->cmds->register_callback(st,
127  call_id,
128  async_fence_data.timeout/1000,
129  st_opt_timeout_updates, NULL, "callback", fence_callback);
130 
131  return TRUE;
132 }
133 
134 int
135 pcmk__fence_action(stonith_t *st, const char *target, const char *action,
136  const char *name, unsigned int timeout, unsigned int tolerance,
137  int delay)
138 {
139  crm_trigger_t *trig;
140 
141  async_fence_data.st = st;
142  async_fence_data.name = strdup(name);
143  async_fence_data.target = target;
144  async_fence_data.action = action;
145  async_fence_data.timeout = timeout;
146  async_fence_data.tolerance = tolerance;
147  async_fence_data.delay = delay;
148  async_fence_data.rc = pcmk_err_generic;
149 
150  trig = mainloop_add_trigger(G_PRIORITY_HIGH, async_fence_helper, NULL);
151  mainloop_set_trigger(trig);
152 
153  mainloop = g_main_loop_new(NULL, FALSE);
154  g_main_loop_run(mainloop);
155 
156  free(async_fence_data.name);
157 
158  return pcmk_legacy2rc(async_fence_data.rc);
159 }
160 
161 #ifdef BUILD_PUBLIC_LIBPACEMAKER
162 int
163 pcmk_fence_action(stonith_t *st, const char *target, const char *action,
164  const char *name, unsigned int timeout, unsigned int tolerance,
165  int delay)
166 {
167  return pcmk__fence_action(st, target, action, name, timeout, tolerance, delay);
168 }
169 #endif
170 
171 int
173  unsigned int timeout, bool quiet, int verbose,
174  bool broadcast, bool cleanup) {
175  stonith_history_t *history = NULL, *hp, *latest = NULL;
176  int rc = pcmk_rc_ok;
177 
178  if (!quiet) {
179  if (cleanup) {
180  out->info(out, "cleaning up fencing-history%s%s",
181  target ? " for node " : "", target ? target : "");
182  }
183  if (broadcast) {
184  out->info(out, "gather fencing-history from all nodes");
185  }
186  }
187 
188  rc = st->cmds->history(st, st_opts | (cleanup ? st_opt_cleanup : 0) |
189  (broadcast ? st_opt_broadcast : 0),
190  safe_str_eq(target, "*") ? NULL : target,
191  &history, timeout/1000);
192 
193  if (cleanup) {
194  // Cleanup doesn't return a history list
195  stonith_history_free(history);
196  return pcmk_legacy2rc(rc);
197  }
198 
199  out->begin_list(out, "event", "events", "Fencing history");
200 
201  history = stonith__sort_history(history);
202  for (hp = history; hp; hp = hp->next) {
203  if (hp->state == st_done) {
204  latest = hp;
205  }
206 
207  if (quiet || !verbose) {
208  continue;
209  }
210 
211  out->message(out, "stonith-event", hp, 1, stonith__later_succeeded(hp, history));
212  out->increment_list(out);
213  }
214 
215  if (latest) {
216  if (quiet && out->supports_quiet) {
217  out->info(out, "%lld", (long long) latest->completed);
218  } else if (!verbose) { // already printed if verbose
219  out->message(out, "stonith-event", latest, 0, FALSE);
220  out->increment_list(out);
221  }
222  }
223 
224  out->end_list(out);
225 
226  stonith_history_free(history);
227  return pcmk_legacy2rc(rc);
228 }
229 
230 #ifdef BUILD_PUBLIC_LIBPACEMAKER
231 int
232 pcmk_fence_history(xmlNodePtr *xml, stonith_t *st, char *target, unsigned int timeout,
233  bool quiet, int verbose, bool broadcast, bool cleanup) {
234  pcmk__output_t *out = NULL;
235  int rc = pcmk_rc_ok;
236 
237  rc = pcmk__out_prologue(&out, xml);
238  if (rc != pcmk_rc_ok) {
239  return rc;
240  }
241 
242  rc = pcmk__fence_history(out, st, target, timeout, quiet, verbose,
243  broadcast, cleanup);
244  pcmk__out_epilogue(out, xml, rc);
245  return rc;
246 }
247 #endif
248 
249 int
250 pcmk__fence_installed(pcmk__output_t *out, stonith_t *st, unsigned int timeout) {
251  stonith_key_value_t *devices = NULL;
252  int rc = pcmk_rc_ok;
253 
254  rc = st->cmds->list_agents(st, st_opt_sync_call, NULL, &devices, timeout/1000);
255  /* list_agents returns a negative error code or a positive number of agents. */
256  if (rc < 0) {
257  return pcmk_legacy2rc(rc);
258  }
259 
260  out->begin_list(out, "fence device", "fence devices", "Installed fence devices");
261  for (stonith_key_value_t *dIter = devices; dIter; dIter = dIter->next) {
262  out->list_item(out, "device", "%s", dIter->value);
263  }
264  out->end_list(out);
265 
266  stonith_key_value_freeall(devices, 1, 1);
267  return pcmk_rc_ok;
268 }
269 
270 #ifdef BUILD_PUBLIC_LIBPACEMAKER
271 int
272 pcmk_fence_installed(xmlNodePtr *xml, stonith_t *st, unsigned int timeout) {
273  pcmk__output_t *out = NULL;
274  int rc = pcmk_rc_ok;
275 
276  rc = pcmk__out_prologue(&out, xml);
277  if (rc != pcmk_rc_ok) {
278  return rc;
279  }
280 
281  rc = pcmk__fence_installed(out, st, timeout);
282  pcmk__out_epilogue(out, xml, rc);
283  return rc;
284 }
285 #endif
286 
287 int
288 pcmk__fence_last(pcmk__output_t *out, const char *target, bool as_nodeid) {
289  time_t when = 0;
290 
291  if (target == NULL) {
292  return pcmk_rc_ok;
293  }
294 
295  if (as_nodeid) {
296  when = stonith_api_time(atol(target), NULL, FALSE);
297  } else {
298  when = stonith_api_time(0, target, FALSE);
299  }
300 
301  return out->message(out, "last-fenced", target, when);
302 }
303 
304 #ifdef BUILD_PUBLIC_LIBPACEMAKER
305 int
306 pcmk_fence_last(xmlNodePtr *xml, const char *target, bool as_nodeid) {
307  pcmk__output_t *out = NULL;
308  int rc = pcmk_rc_ok;
309 
310  rc = pcmk__out_prologue(&out, xml);
311  if (rc != pcmk_rc_ok) {
312  return rc;
313  }
314 
315  rc = pcmk__fence_last(out, target, as_nodeid);
316  pcmk__out_epilogue(out, xml, rc);
317  return rc;
318 }
319 #endif
320 
321 int
323  const char *device_id, unsigned int timeout) {
324  GList *targets = NULL;
325  char *lists = NULL;
326  int rc = pcmk_rc_ok;
327 
328  rc = st->cmds->list(st, st_opts, device_id, &lists, timeout/1000);
329  if (rc != pcmk_rc_ok) {
330  return pcmk_legacy2rc(rc);
331  }
332 
333  targets = stonith__parse_targets(lists);
334 
335  out->begin_list(out, "fence target", "fence targets", "Fence Targets");
336  while (targets != NULL) {
337  out->list_item(out, NULL, "%s", (const char *) targets->data);
338  targets = targets->next;
339  }
340  out->end_list(out);
341 
342  free(lists);
343  return rc;
344 }
345 
346 #ifdef BUILD_PUBLIC_LIBPACEMAKER
347 int
348 pcmk_fence_list_targets(xmlNodePtr *xml, stonith_t *st, const char *device_id,
349  unsigned int timeout) {
350  pcmk__output_t *out = NULL;
351  int rc = pcmk_rc_ok;
352 
353  rc = pcmk__out_prologue(&out, xml);
354  if (rc != pcmk_rc_ok) {
355  return rc;
356  }
357 
358  rc = pcmk__fence_list_targets(out, st, device_id, timeout);
359  pcmk__out_epilogue(out, xml, rc);
360  return rc;
361 }
362 #endif
363 
364 int
366  unsigned int timeout) {
367  char *buffer = NULL;
368  int rc = st->cmds->metadata(st, st_opt_sync_call, agent, NULL, &buffer,
369  timeout/1000);
370 
371  if (rc != pcmk_rc_ok) {
372  return pcmk_legacy2rc(rc);
373  }
374 
375  out->output_xml(out, "metadata", buffer);
376  free(buffer);
377  return rc;
378 }
379 
380 #ifdef BUILD_PUBLIC_LIBPACEMAKER
381 int
382 pcmk_fence_metadata(xmlNodePtr *xml, stonith_t *st, char *agent,
383  unsigned int timeout) {
384  pcmk__output_t *out = NULL;
385  int rc = pcmk_rc_ok;
386 
387  rc = pcmk__out_prologue(&out, xml);
388  if (rc != pcmk_rc_ok) {
389  return rc;
390  }
391 
392  rc = pcmk__fence_metadata(out, st, agent, timeout);
393  pcmk__out_epilogue(out, xml, rc);
394  return rc;
395 }
396 #endif
397 
398 int
400  unsigned int timeout) {
401  stonith_key_value_t *devices = NULL;
402  int rc = pcmk_rc_ok;
403 
404  rc = st->cmds->query(st, st_opts, target, &devices, timeout/1000);
405  /* query returns a negative error code or a positive number of results. */
406  if (rc < 0) {
407  return pcmk_legacy2rc(rc);
408  }
409 
410  out->begin_list(out, "fence device", "fence devices", "Registered fence devices");
411  for (stonith_key_value_t *dIter = devices; dIter; dIter = dIter->next) {
412  out->list_item(out, "device", "%s", dIter->value);
413  }
414  out->end_list(out);
415 
416  stonith_key_value_freeall(devices, 1, 1);
417 
418  /* Return pcmk_rc_ok here, not the number of results. Callers probably
419  * don't care.
420  */
421  return pcmk_rc_ok;
422 }
423 
424 #ifdef BUILD_PUBLIC_LIBPACEMAKER
425 int
426 pcmk_fence_registered(xmlNodePtr *xml, stonith_t *st, char *target,
427  unsigned int timeout) {
428  pcmk__output_t *out = NULL;
429  int rc = pcmk_rc_ok;
430 
431  rc = pcmk__out_prologue(&out, xml);
432  if (rc != pcmk_rc_ok) {
433  return rc;
434  }
435 
436  rc = pcmk__fence_registered(out, st, target, timeout);
437  pcmk__out_epilogue(out, xml, rc);
438  return rc;
439 }
440 #endif
441 
442 int
443 pcmk__fence_register_level(stonith_t *st, char *target, int fence_level,
444  stonith_key_value_t *devices) {
445  return handle_level(st, target, fence_level, devices, true);
446 }
447 
448 #ifdef BUILD_PUBLIC_LIBPACEMAKER
449 int
450 pcmk_fence_register_level(stonith_t *st, char *target, int fence_level,
451  stonith_key_value_t *devices) {
452  return pcmk__fence_register_level(st, target, fence_level, devices);
453 }
454 #endif
455 
456 int
457 pcmk__fence_unregister_level(stonith_t *st, char *target, int fence_level) {
458  return handle_level(st, target, fence_level, NULL, false);
459 }
460 
461 #ifdef BUILD_PUBLIC_LIBPACEMAKER
462 int
463 pcmk_fence_unregister_level(stonith_t *st, char *target, int fence_level) {
464  return pcmk__fence_unregister_level(st, target, fence_level);
465 }
466 #endif
467 
468 int
469 pcmk__fence_validate(pcmk__output_t *out, stonith_t *st, const char *agent,
470  const char *id, stonith_key_value_t *params,
471  unsigned int timeout) {
472  char *output = NULL;
473  char *error_output = NULL;
474  int rc;
475 
476  rc = st->cmds->validate(st, st_opt_sync_call, id, NULL, agent, params,
477  timeout/1000, &output, &error_output);
478  out->message(out, "validate", agent, id, output, error_output, rc);
479  return pcmk_legacy2rc(rc);
480 }
481 
482 #ifdef BUILD_PUBLIC_LIBPACEMAKER
483 int
484 pcmk_fence_validate(xmlNodePtr *xml, stonith_t *st, const char *agent,
485  const char *id, stonith_key_value_t *params,
486  unsigned int timeout) {
487  pcmk__output_t *out = NULL;
488  int rc = pcmk_rc_ok;
489 
490  rc = pcmk__out_prologue(&out, xml);
491  if (rc != pcmk_rc_ok) {
492  return rc;
493  }
494 
495  rc = pcmk__fence_validate(out, st, agent, id, params, timeout);
496  pcmk__out_epilogue(out, xml, rc);
497  return rc;
498 }
499 #endif
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:260
struct stonith_history_s * next
Definition: stonith-ng.h:106
const char * pcmk_strerror(int rc)
Definition: results.c:55
int(* register_notification)(stonith_t *st, const char *event, void(*notify)(stonith_t *st, stonith_event_t *e))
Definition: stonith-ng.h:297
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:367
Formatted output for pacemaker tools.
int pcmk__fence_last(pcmk__output_t *out, const char *target, bool as_nodeid)
When was a device last fenced?
Definition: pcmk_fence.c:288
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:295
stonith_history_t * stonith__sort_history(stonith_history_t *history)
Definition: st_client.c:2578
struct stonith_key_value_s * next
Definition: stonith-ng.h:95
bool supports_quiet
Does this formatter support a special quiet mode?
Definition: output.h:174
void mainloop_set_trigger(crm_trigger_t *source)
Definition: mainloop.c:171
#define pcmk_err_generic
Definition: results.h:70
int(* message)(pcmk__output_t *out, const char *message_id,...)
Definition: output.h:311
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:233
int pcmk__fence_registered(pcmk__output_t *out, stonith_t *st, char *target, unsigned int timeout)
List registered fence devices.
Definition: pcmk_fence.c:399
int pcmk__fence_action(stonith_t *st, const char *target, const char *action, const char *name, unsigned int timeout, unsigned int tolerance, int delay)
Perform a STONITH action.
Definition: pcmk_fence.c:135
Wrappers for and extensions to glib mainloop.
const char * action
Definition: pcmk_fence.c:29
int stonith_api_connect_retry(stonith_t *st, const char *name, int max_attempts)
Make a blocking connection attempt to the fencer.
Definition: st_client.c:2190
int pcmk__fence_unregister_level(stonith_t *st, char *target, int fence_level)
Unregister a fencing level for a specific node, node regex, or attribute.
Definition: pcmk_fence.c:457
struct trigger_s crm_trigger_t
Definition: mainloop.h:31
int pcmk__out_prologue(pcmk__output_t **out, xmlNodePtr *xml)
Definition: pcmk_output.c:24
GList * stonith__parse_targets(const char *hosts)
Definition: st_client.c:2507
void(* end_list)(pcmk__output_t *out)
Definition: output.h:432
stonith_t * st
Definition: pcmk_fence.c:27
int rc
Definition: pcmk_fence.c:34
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:319
unsigned int tolerance
Definition: pcmk_fence.c:32
void(* begin_list)(pcmk__output_t *out, const char *singular_noun, const char *plural_noun, const char *format,...) G_GNUC_PRINTF(4
Definition: output.h:392
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:211
gboolean stonith__later_succeeded(stonith_history_t *event, stonith_history_t *top_history)
Definition: st_client.c:2547
crm_trigger_t * mainloop_add_trigger(int priority, int(*dispatch)(gpointer user_data), gpointer userdata)
Definition: mainloop.c:159
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:224
void stonith_key_value_freeall(stonith_key_value_t *kvp, int keys, int values)
Definition: st_client.c:2238
int pcmk_legacy2rc(int legacy_rc)
Definition: results.c:443
int pcmk__fence_metadata(pcmk__output_t *out, stonith_t *st, char *agent, unsigned int timeout)
Get metadata for a resource.
Definition: pcmk_fence.c:365
int pcmk__fence_list_targets(pcmk__output_t *out, stonith_t *st, const char *device_id, unsigned int timeout)
List nodes that can be fenced.
Definition: pcmk_fence.c:322
void pcmk__out_epilogue(pcmk__output_t *out, xmlNodePtr *xml, int retval)
Definition: pcmk_output.c:42
Function and executable result codes.
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:347
int pcmk__fence_register_level(stonith_t *st, char *target, int fence_level, stonith_key_value_t *devices)
Register a fencing level for a specific node, node regex, or attribute.
Definition: pcmk_fence.c:443
const char * target
Definition: pcmk_fence.c:28
int(* fence_with_delay)(stonith_t *st, int options, const char *node, const char *action, int timeout, int tolerance, int delay)
Issue a fencing action against a node with requested fencing delay.
Definition: stonith-ng.h:412
int pcmk__fence_history(pcmk__output_t *out, stonith_t *st, char *target, unsigned int timeout, bool quiet, int verbose, bool broadcast, bool cleanup)
List the fencing operations that have occurred for a specific node.
Definition: pcmk_fence.c:172
void(*) void(*) void(* increment_list)(pcmk__output_t *out)
Definition: output.h:420
void(* info)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
Definition: output.h:347
stonith_api_operations_t * cmds
Definition: stonith-ng.h:425
int delay
Definition: pcmk_fence.c:33
int pcmk__fence_installed(pcmk__output_t *out, stonith_t *st, unsigned int timeout)
List all installed STONITH agents.
Definition: pcmk_fence.c:250
void stonith_history_free(stonith_history_t *history)
Definition: st_client.c:1170
Fencing aka. STONITH.
This structure contains everything that makes up a single output formatter.
Definition: output.h:153
char data[0]
Definition: internal.h:90
#define pcmk_ok
Definition: results.h:67
time_t stonith_api_time(uint32_t nodeid, const char *uname, bool in_progress)
Definition: st_client.c:2302
#define T_STONITH_NOTIFY_FENCE
Definition: stonith-ng.h:30
int pcmk__fence_validate(pcmk__output_t *out, stonith_t *st, const char *agent, const char *id, stonith_key_value_t *params, unsigned int timeout)
Validate a STONITH device configuration.
Definition: pcmk_fence.c:469
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:390
void(*) void(*) void(* output_xml)(pcmk__output_t *out, const char *name, const char *buf)
Definition: output.h:371
#define safe_str_eq(a, b)
Definition: util.h:65
char * name
Definition: pcmk_fence.c:30
unsigned int timeout
Definition: pcmk_fence.c:31
void(*) void(* list_item)(pcmk__output_t *out, const char *name, const char *format,...) G_GNUC_PRINTF(3
Definition: output.h:405