pacemaker  2.0.3-4b1f869f0f
Scalable High-Availability cluster resource manager
st_rhcs.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2019 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 <stdio.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <glib.h>
16 #include <dirent.h>
17 
18 #include <crm/crm.h>
19 #include <crm/stonith-ng.h>
20 #include <crm/fencing/internal.h>
21 
30 int
32 {
33  // Essentially: ls -1 @sbin_dir@/fence_*
34 
35  int count = 0, i;
36  struct dirent **namelist;
37  const int file_num = scandir(RH_STONITH_DIR, &namelist, 0, alphasort);
38 
39 #if _POSIX_C_SOURCE < 200809L && !(defined(O_SEARCH) || defined(O_PATH))
40  char buffer[FILENAME_MAX + 1];
41 #elif defined(O_SEARCH)
42  const int dirfd = open(RH_STONITH_DIR, O_SEARCH);
43 #else
44  const int dirfd = open(RH_STONITH_DIR, O_PATH);
45 #endif
46 
47  for (i = 0; i < file_num; i++) {
48  struct stat prop;
49 
50  if (crm_starts_with(namelist[i]->d_name, RH_STONITH_PREFIX)) {
51 #if _POSIX_C_SOURCE < 200809L && !(defined(O_SEARCH) || defined(O_PATH))
52  snprintf(buffer, sizeof(buffer), "%s/%s", RH_STONITH_DIR,
53  namelist[i]->d_name);
54  if (stat(buffer, &prop) == 0 && S_ISREG(prop.st_mode)) {
55 #else
56  if (dirfd == -1) {
57  if (i == 0) {
58  crm_notice("Problem with listing %s directory"
59  CRM_XS "errno=%d", RH_STONITH_PREFIX, errno);
60  }
61  free(namelist[i]);
62  continue;
63  }
64  /* note: we can possibly prevent following symlinks here,
65  which may be a good idea, but fall on the nose when
66  these agents are moved elsewhere & linked back */
67  if (fstatat(dirfd, namelist[i]->d_name, &prop, 0) == 0
68  && S_ISREG(prop.st_mode)) {
69 #endif
70  *devices = stonith_key_value_add(*devices, NULL,
71  namelist[i]->d_name);
72  count++;
73  }
74  }
75  free(namelist[i]);
76  }
77  if (file_num > 0) {
78  free(namelist);
79  }
80 #if _POSIX_C_SOURCE >= 200809L || defined(O_SEARCH) || defined(O_PATH)
81  if (dirfd >= 0) {
82  close(dirfd);
83  }
84 #endif
85  return count;
86 }
87 
97 int
98 stonith__rhcs_metadata(const char *agent, int timeout, char **output)
99 {
100  char *buffer = NULL;
101  xmlNode *xml = NULL;
102  xmlNode *actions = NULL;
103  xmlXPathObject *xpathObj = NULL;
104  stonith_action_t *action = stonith_action_create(agent, "metadata", NULL, 0,
105  5, NULL, NULL);
106  int rc = stonith__execute(action);
107 
108  if (rc < 0) {
109  crm_warn("Could not execute metadata action for %s: %s "
110  CRM_XS " rc=%d", agent, pcmk_strerror(rc), rc);
111  stonith__destroy_action(action);
112  return rc;
113  }
114 
115  stonith__action_result(action, &rc, &buffer, NULL);
116  stonith__destroy_action(action);
117  if (rc < 0) {
118  crm_warn("Metadata action for %s failed: %s " CRM_XS "rc=%d",
119  agent, pcmk_strerror(rc), rc);
120  free(buffer);
121  return rc;
122  }
123 
124  if (buffer == NULL) {
125  crm_warn("Metadata action for %s returned no data", agent);
126  return -ENODATA;
127  }
128 
129  xml = string2xml(buffer);
130  free(buffer);
131  buffer = NULL;
132  if (xml == NULL) {
133  crm_warn("Metadata for %s is invalid", agent);
135  }
136 
137  xpathObj = xpath_search(xml, "//actions");
138  if (numXpathResults(xpathObj) > 0) {
139  actions = getXpathResult(xpathObj, 0);
140  }
141  freeXpathObject(xpathObj);
142 
143  // Add start and stop (implemented by pacemaker, not agent) to meta-data
144  xpathObj = xpath_search(xml, "//action[@name='stop']");
145  if (numXpathResults(xpathObj) <= 0) {
146  xmlNode *tmp = NULL;
147 
148  tmp = create_xml_node(actions, "action");
149  crm_xml_add(tmp, "name", "stop");
150  crm_xml_add(tmp, "timeout", CRM_DEFAULT_OP_TIMEOUT_S);
151 
152  tmp = create_xml_node(actions, "action");
153  crm_xml_add(tmp, "name", "start");
154  crm_xml_add(tmp, "timeout", CRM_DEFAULT_OP_TIMEOUT_S);
155  }
156  freeXpathObject(xpathObj);
157 
158  // Fudge metadata so port isn't required in config (pacemaker adds it)
159  xpathObj = xpath_search(xml, "//parameter[@name='port']");
160  if (numXpathResults(xpathObj) > 0) {
161  xmlNode *tmp = getXpathResult(xpathObj, 0);
162 
163  crm_xml_add(tmp, "required", "0");
164  }
165  freeXpathObject(xpathObj);
166 
167  buffer = dump_xml_formatted_with_text(xml);
168  free_xml(xml);
169  if (buffer == NULL) {
171  }
172  if (output) {
173  *output = buffer;
174  } else {
175  free(buffer);
176  }
177  return pcmk_ok;
178 }
179 
180 bool
181 stonith__agent_is_rhcs(const char *agent)
182 {
183  struct stat prop;
184  char *buffer = crm_strdup_printf(RH_STONITH_DIR "/%s", agent);
185  int rc = stat(buffer, &prop);
186 
187  free(buffer);
188  return (rc >= 0) && S_ISREG(prop.st_mode);
189 }
190 
191 int
192 stonith__rhcs_validate(stonith_t *st, int call_options, const char *target,
193  const char *agent, GHashTable *params, int timeout,
194  char **output, char **error_output)
195 {
196  int rc = pcmk_ok;
197  stonith_action_t *action = stonith_action_create(agent, "validate-all",
198  target, 0, timeout, params,
199  NULL);
200 
201  rc = stonith__execute(action);
202  if (rc == pcmk_ok) {
203  stonith__action_result(action, &rc, output, error_output);
204  }
205  stonith__destroy_action(action);
206  return rc;
207 }
CRM_DEFAULT_OP_TIMEOUT_S
#define CRM_DEFAULT_OP_TIMEOUT_S
Definition: util.h:138
stonith__execute
int stonith__execute(stonith_action_t *action)
Definition: st_client.c:897
stonith_key_value_add
stonith_key_value_t * stonith_key_value_add(stonith_key_value_t *kvp, const char *key, const char *value)
Definition: st_client.c:2188
alphasort
int alphasort(const void *dirent1, const void *dirent2)
stonith__rhcs_metadata
int stonith__rhcs_metadata(const char *agent, int timeout, char **output)
Execute RHCS-compatible agent's meta-data action.
Definition: st_rhcs.c:98
stonith__action_result
void stonith__action_result(stonith_action_t *action, int *rc, char **output, char **error_output)
Definition: st_client.c:620
create_xml_node
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:1970
pcmk_err_schema_validation
#define pcmk_err_schema_validation
Definition: results.h:62
pcmk_strerror
const char * pcmk_strerror(int rc)
Definition: results.c:188
crm_notice
#define crm_notice(fmt, args...)
Definition: logging.h:243
crm_warn
#define crm_warn(fmt, args...)
Definition: logging.h:242
free_xml
void free_xml(xmlNode *child)
Definition: xml.c:2130
stonith_key_value_s
Definition: stonith-ng.h:92
xpath_search
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
Definition: xpath.c:145
internal.h
stonith__list_rhcs_agents
int stonith__list_rhcs_agents(stonith_key_value_t **devices)
Definition: st_rhcs.c:31
RH_STONITH_PREFIX
#define RH_STONITH_PREFIX
Definition: config.h:553
CRM_XS
#define CRM_XS
Definition: logging.h:34
stonith__rhcs_validate
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
ENODATA
#define ENODATA
Definition: portability.h:158
crm_strdup_printf
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
stonith__destroy_action
void stonith__destroy_action(stonith_action_t *action)
Definition: st_client.c:589
string2xml
xmlNode * string2xml(const char *input)
Definition: xml.c:2174
crm_xml_add
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:313
stonith_action_create
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:649
getXpathResult
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
Definition: xpath.c:64
RH_STONITH_DIR
#define RH_STONITH_DIR
Definition: config.h:550
dump_xml_formatted_with_text
char * dump_xml_formatted_with_text(xmlNode *msg)
Definition: xml.c:3284
stonith-ng.h
Fencing aka. STONITH.
stonith__agent_is_rhcs
bool stonith__agent_is_rhcs(const char *agent)
Definition: st_rhcs.c:181
crm_starts_with
bool crm_starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
Definition: strings.c:263
crm_internal.h
stonith_action_t
struct stonith_action_s stonith_action_t
Definition: internal.h:19
freeXpathObject
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:45
crm.h
A dumping ground.
stonith_s
Definition: stonith-ng.h:405
pcmk_ok
#define pcmk_ok
Definition: results.h:57