pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
st_rhcs.c
Go to the documentation of this file.
1/*
2 * Copyright 2004-2025 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 Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
12#include <dirent.h>
13#include <stdio.h>
14#include <string.h>
15#include <sys/stat.h>
16
17#include <glib.h>
18#include <libxml/xpath.h> // xmlXPathObject, etc.
19
20#include <crm/crm.h>
21#include <crm/common/xml.h>
22#include <crm/stonith-ng.h>
24
25#include "fencing_private.h"
26
36static int
37rhcs_agent_filter(const struct dirent *entry)
38{
39 char *buf = NULL;
40 struct stat sb;
41 int rc = 0;
42
43 if (!pcmk__starts_with(entry->d_name, "fence_")) {
44 goto done;
45 }
46
47 // glibc doesn't enforce PATH_MAX, so don't limit buf size
48 buf = crm_strdup_printf(PCMK__FENCE_BINDIR "/%s", entry->d_name);
49 if ((stat(buf, &sb) != 0) || !S_ISREG(sb.st_mode)) {
50 goto done;
51 }
52
53 rc = 1;
54
55done:
56 free(buf);
57 return rc;
58}
59
68int
70{
71 struct dirent **namelist = NULL;
72 const int file_num = scandir(PCMK__FENCE_BINDIR, &namelist,
73 rhcs_agent_filter, alphasort);
74
75 if (file_num < 0) {
76 int rc = errno;
77
78 crm_err("Could not list " PCMK__FENCE_BINDIR ": %s", pcmk_rc_str(rc));
79 free(namelist);
80 return 0;
81 }
82
83 for (int i = 0; i < file_num; i++) {
84 *devices = stonith__key_value_add(*devices, NULL, namelist[i]->d_name);
85 free(namelist[i]);
86 }
87 free(namelist);
88 return file_num;
89}
90
91static void
92stonith_rhcs_parameter_not_required(xmlNode *metadata, const char *parameter)
93{
94 char *xpath = NULL;
95 xmlXPathObject *xpathObj = NULL;
96
97 CRM_CHECK(metadata != NULL, return);
98 CRM_CHECK(parameter != NULL, return);
99
100 xpath = crm_strdup_printf("//" PCMK_XE_PARAMETER "[@" PCMK_XA_NAME "='%s']",
101 parameter);
102 /* Fudge metadata so that the parameter isn't required in config
103 * Pacemaker handles and adds it */
104 xpathObj = pcmk__xpath_search(metadata->doc, xpath);
105 if (pcmk__xpath_num_results(xpathObj) > 0) {
106 xmlNode *tmp = pcmk__xpath_result(xpathObj, 0);
107
108 if (tmp != NULL) {
109 crm_xml_add(tmp, "required", "0");
110 }
111 }
112 xmlXPathFreeObject(xpathObj);
113 free(xpath);
114}
115
123static int
124stonith__rhcs_get_metadata(const char *agent, int timeout_sec,
125 xmlNode **metadata)
126{
127 xmlNode *xml = NULL;
128 xmlNode *actions = NULL;
129 xmlXPathObject *xpathObj = NULL;
132 NULL, timeout_sec, NULL,
133 NULL, NULL);
134 int rc = stonith__execute(action);
136
137 if (result == NULL) {
138 if (rc < 0) {
139 crm_warn("Could not execute metadata action for %s: %s "
140 QB_XS " rc=%d", agent, pcmk_strerror(rc), rc);
141 }
143 return rc;
144 }
145
147 crm_warn("Could not execute metadata action for %s: %s",
148 agent, pcmk_exec_status_str(result->execution_status));
151 return rc;
152 }
153
154 if (!pcmk__result_ok(result)) {
155 crm_warn("Metadata action for %s returned error code %d",
156 agent, result->exit_status);
159 return rc;
160 }
161
162 if (result->action_stdout == NULL) {
163 crm_warn("Metadata action for %s returned no data", agent);
165 return -ENODATA;
166 }
167
170
171 if (xml == NULL) {
172 crm_warn("Metadata for %s is invalid", agent);
174 }
175
176 xpathObj = pcmk__xpath_search(xml->doc, "//" PCMK_XE_ACTIONS);
177 if (pcmk__xpath_num_results(xpathObj) > 0) {
178 actions = pcmk__xpath_result(xpathObj, 0);
179 }
180 xmlXPathFreeObject(xpathObj);
181
182 // Add start and stop (implemented by pacemaker, not agent) to meta-data
183 xpathObj = pcmk__xpath_search(xml->doc,
184 "//" PCMK_XE_ACTION
185 "[@" PCMK_XA_NAME "='" PCMK_ACTION_STOP "']");
186 if (pcmk__xpath_num_results(xpathObj) == 0) {
187 xmlNode *tmp = NULL;
188 const char *timeout_str = NULL;
189
191
192 tmp = pcmk__xe_create(actions, PCMK_XE_ACTION);
194 crm_xml_add(tmp, PCMK_META_TIMEOUT, timeout_str);
195
196 tmp = pcmk__xe_create(actions, PCMK_XE_ACTION);
198 crm_xml_add(tmp, PCMK_META_TIMEOUT, timeout_str);
199 }
200 xmlXPathFreeObject(xpathObj);
201
202 // Fudge metadata so parameters are not required in config (pacemaker adds them)
203 stonith_rhcs_parameter_not_required(xml, STONITH_ATTR_ACTION_OP);
204 stonith_rhcs_parameter_not_required(xml, "plug");
205 stonith_rhcs_parameter_not_required(xml, "port");
206
207 if (metadata) {
208 *metadata = xml;
209
210 } else {
211 pcmk__xml_free(xml);
212 }
213
214 return pcmk_ok;
215}
216
224int
225stonith__rhcs_metadata(const char *agent, int timeout_sec, char **output)
226{
227 GString *buffer = NULL;
228 xmlNode *xml = NULL;
229
230 int rc = stonith__rhcs_get_metadata(agent, timeout_sec, &xml);
231
232 if (rc != pcmk_ok) {
233 goto done;
234 }
235
236 buffer = g_string_sized_new(1024);
238
239 if (pcmk__str_empty(buffer->str)) {
241 goto done;
242 }
243
244 if (output != NULL) {
245 pcmk__str_update(output, buffer->str);
246 }
247
248done:
249 if (buffer != NULL) {
250 g_string_free(buffer, TRUE);
251 }
252 pcmk__xml_free(xml);
253 return rc;
254}
255
256bool
257stonith__agent_is_rhcs(const char *agent)
258{
259 struct stat prop;
260 char *buffer = crm_strdup_printf(PCMK__FENCE_BINDIR "/%s", agent);
261 int rc = stat(buffer, &prop);
262
263 free(buffer);
264 return (rc >= 0) && S_ISREG(prop.st_mode);
265}
266
267int
268stonith__rhcs_validate(stonith_t *st, int call_options, const char *target,
269 const char *agent, GHashTable *params,
270 const char * host_arg, int timeout,
271 char **output, char **error_output)
272{
273 int rc = pcmk_ok;
274 int remaining_timeout = timeout;
275 xmlNode *metadata = NULL;
276 stonith_action_t *action = NULL;
278
279 if (host_arg == NULL) {
280 time_t start_time = time(NULL);
281
282 rc = stonith__rhcs_get_metadata(agent, remaining_timeout, &metadata);
283
284 if (rc == pcmk_ok) {
285 host_arg = stonith__default_host_arg(metadata);
286 crm_trace("Using '%s' as default " PCMK_STONITH_HOST_ARGUMENT
287 " for %s",
288 pcmk__s(host_arg, PCMK_VALUE_NONE), agent);
289 }
290
291 pcmk__xml_free(metadata);
292
293 remaining_timeout -= time(NULL) - start_time;
294
295 if (rc == -ETIME || remaining_timeout <= 0 ) {
296 return -ETIME;
297 }
298
299 } else if (pcmk__str_eq(host_arg, PCMK_VALUE_NONE, pcmk__str_casei)) {
300 host_arg = NULL;
301 }
302
304 remaining_timeout, params, NULL, host_arg);
305
308
309 if (result != NULL) {
311
312 // Take ownership of output so stonith__destroy_action() doesn't free it
313 if (output != NULL) {
314 *output = result->action_stdout;
315 result->action_stdout = NULL;
316 }
317 if (error_output != NULL) {
318 *error_output = result->action_stderr;
319 result->action_stderr = NULL;
320 }
321 }
323 return rc;
324}
#define PCMK_ACTION_STOP
Definition actions.h:66
#define PCMK_ACTION_METADATA
Definition actions.h:48
#define PCMK_ACTION_START
Definition actions.h:63
#define PCMK_DEFAULT_ACTION_TIMEOUT_MS
Default timeout (in milliseconds) for non-metadata actions.
Definition actions.h:33
#define PCMK_ACTION_VALIDATE_ALL
Definition actions.h:68
#define PCMK_STONITH_HOST_ARGUMENT
Definition agents.h:40
#define PCMK__FENCE_BINDIR
Definition config.h:487
A dumping ground.
const char * stonith__default_host_arg(xmlNode *metadata)
Definition st_client.c:2529
void stonith__destroy_action(stonith_action_t *action)
Definition st_actions.c:206
stonith_key_value_t * stonith__key_value_add(stonith_key_value_t *head, const char *key, const char *value)
Definition st_client.c:1995
pcmk__action_result_t * stonith__action_result(stonith_action_t *action)
Definition st_actions.c:231
struct stonith_action_s stonith_action_t
Definition internal.h:55
int stonith__result2rc(const pcmk__action_result_t *result)
Definition st_actions.c:316
#define STONITH_ATTR_ACTION_OP
Definition internal.h:111
stonith_action_t * stonith__action_create(const char *agent, const char *action_name, const char *target, int timeout_sec, GHashTable *device_args, GHashTable *port_map, const char *host_arg)
Definition st_actions.c:253
G_GNUC_INTERNAL int stonith__execute(stonith_action_t *action)
Definition st_actions.c:706
const char * pcmk__readable_interval(guint interval_ms)
Definition iso8601.c:2210
#define crm_warn(fmt, args...)
Definition logging.h:360
#define CRM_CHECK(expr, failure_action)
Definition logging.h:213
#define crm_err(fmt, args...)
Definition logging.h:357
#define crm_trace(fmt, args...)
Definition logging.h:370
#define PCMK_VALUE_NONE
Definition options.h:180
#define PCMK_META_TIMEOUT
Definition options.h:115
unsigned int timeout
Definition pcmk_fence.c:34
stonith_t * st
Definition pcmk_fence.c:30
const char * action
Definition pcmk_fence.c:32
pcmk__action_result_t result
Definition pcmk_fence.c:37
const char * target
Definition pcmk_fence.c:31
#define ENODATA
Definition portability.h:61
#define ETIME
Definition portability.h:66
const char * pcmk_strerror(int rc)
Definition results.c:257
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Definition results.c:617
#define pcmk_err_schema_validation
Definition results.h:71
#define pcmk_ok
Definition results.h:65
int pcmk_rc2legacy(int rc)
Definition results.c:662
@ PCMK_EXEC_DONE
Action completed, result is known.
Definition results.h:311
bool stonith__agent_is_rhcs(const char *agent)
Definition st_rhcs.c:257
int stonith__rhcs_validate(stonith_t *st, int call_options, const char *target, const char *agent, GHashTable *params, const char *host_arg, int timeout, char **output, char **error_output)
Definition st_rhcs.c:268
int stonith__rhcs_metadata(const char *agent, int timeout_sec, char **output)
Retrieve metadata for RHCS-compatible fence agent.
Definition st_rhcs.c:225
int stonith__list_rhcs_agents(stonith_key_value_t **devices)
Definition st_rhcs.c:69
Fencing aka. STONITH.
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition strings.c:558
void pcmk__str_update(char **str, const char *value)
Definition strings.c:1280
@ pcmk__str_casei
enum pcmk_exec_status execution_status
Key-value pair list node.
Definition stonith-ng.h:162
Fencer API connection object.
Definition stonith-ng.h:657
Wrappers for and extensions to libxml2.
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
@ pcmk__xml_fmt_pretty
Include indentation and newlines.
@ pcmk__xml_fmt_text
Include XML text nodes.
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816
void pcmk__xml_string(const xmlNode *data, uint32_t options, GString *buffer, int depth)
Definition xml_io.c:370
xmlNode * pcmk__xml_parse(const char *input)
Definition xml_io.c:167
#define PCMK_XE_ACTIONS
Definition xml_names.h:61
#define PCMK_XE_ACTION
Definition xml_names.h:60
#define PCMK_XA_NAME
Definition xml_names.h:330
#define PCMK_XE_PARAMETER
Definition xml_names.h:158
xmlXPathObject * pcmk__xpath_search(xmlDoc *doc, const char *path)
Definition xpath.c:137
xmlNode * pcmk__xpath_result(xmlXPathObject *xpath_obj, int index)
Definition xpath.c:65