This source file includes following definitions.
- ra_param_free
- metadata_free
- metadata_cache_new
- metadata_cache_free
- metadata_cache_reset
- valid_version_format
- metadata_cache_fini
- ra_version_from_xml
- ra_param_from_xml
- log_ra_ocf_version
- metadata_cache_update
- controld_get_rsc_metadata
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <glib.h>
14 #include <regex.h>
15
16 #include <crm/crm.h>
17 #include <crm/lrmd.h>
18
19 #include <pacemaker-controld.h>
20
21 #if ENABLE_VERSIONED_ATTRS
22 static regex_t *version_format_regex = NULL;
23 #endif
24
25 static void
26 ra_param_free(void *param)
27 {
28 if (param) {
29 struct ra_param_s *p = (struct ra_param_s *) param;
30
31 if (p->rap_name) {
32 free(p->rap_name);
33 }
34 free(param);
35 }
36 }
37
38 static void
39 metadata_free(void *metadata)
40 {
41 if (metadata) {
42 struct ra_metadata_s *md = (struct ra_metadata_s *) metadata;
43
44 if (md->ra_version) {
45 free(md->ra_version);
46 }
47 g_list_free_full(md->ra_params, ra_param_free);
48 free(metadata);
49 }
50 }
51
52 GHashTable *
53 metadata_cache_new()
54 {
55 return pcmk__strkey_table(free, metadata_free);
56 }
57
58 void
59 metadata_cache_free(GHashTable *mdc)
60 {
61 if (mdc) {
62 crm_trace("Destroying metadata cache with %d members", g_hash_table_size(mdc));
63 g_hash_table_destroy(mdc);
64 }
65 }
66
67 void
68 metadata_cache_reset(GHashTable *mdc)
69 {
70 if (mdc) {
71 crm_trace("Resetting metadata cache with %d members",
72 g_hash_table_size(mdc));
73 g_hash_table_remove_all(mdc);
74 }
75 }
76
77 #if ENABLE_VERSIONED_ATTRS
78 static gboolean
79 valid_version_format(const char *version)
80 {
81 if (version == NULL) {
82 return FALSE;
83 }
84
85 if (version_format_regex == NULL) {
86
87
88
89
90
91 const char *regex_string = "^[[:digit:]]+([.][[:digit:]]+)*$";
92
93 version_format_regex = calloc(1, sizeof(regex_t));
94 regcomp(version_format_regex, regex_string, REG_EXTENDED | REG_NOSUB);
95
96
97
98
99
100
101 CRM_CHECK(version_format_regex != NULL, return TRUE);
102 }
103
104 return regexec(version_format_regex, version, 0, NULL, 0) == 0;
105 }
106 #endif
107
108 void
109 metadata_cache_fini()
110 {
111 #if ENABLE_VERSIONED_ATTRS
112 if (version_format_regex) {
113 regfree(version_format_regex);
114 free(version_format_regex);
115 version_format_regex = NULL;
116 }
117 #endif
118 }
119
120 #if ENABLE_VERSIONED_ATTRS
121 static char *
122 ra_version_from_xml(xmlNode *metadata_xml, const lrmd_rsc_info_t *rsc)
123 {
124 const char *version = crm_element_value(metadata_xml, XML_ATTR_VERSION);
125
126 if (version == NULL) {
127 crm_debug("Metadata for %s:%s:%s does not specify a version",
128 rsc->standard, rsc->provider, rsc->type);
129 version = PCMK_DEFAULT_AGENT_VERSION;
130
131 } else if (!valid_version_format(version)) {
132 crm_notice("%s:%s:%s metadata version has unrecognized format",
133 rsc->standard, rsc->provider, rsc->type);
134 version = PCMK_DEFAULT_AGENT_VERSION;
135
136 } else {
137 crm_debug("Metadata for %s:%s:%s has version %s",
138 rsc->standard, rsc->provider, rsc->type, version);
139 }
140 return strdup(version);
141 }
142 #endif
143
144 static struct ra_param_s *
145 ra_param_from_xml(xmlNode *param_xml)
146 {
147 const char *param_name = crm_element_value(param_xml, "name");
148 const char *value;
149 struct ra_param_s *p;
150
151 p = calloc(1, sizeof(struct ra_param_s));
152 if (p == NULL) {
153 crm_crit("Could not allocate memory for resource metadata");
154 return NULL;
155 }
156
157 p->rap_name = strdup(param_name);
158 if (p->rap_name == NULL) {
159 crm_crit("Could not allocate memory for resource metadata");
160 free(p);
161 return NULL;
162 }
163
164 value = crm_element_value(param_xml, "reloadable");
165 if (crm_is_true(value)) {
166 controld_set_ra_param_flags(p, ra_param_reloadable);
167 }
168
169 value = crm_element_value(param_xml, "unique");
170 if (crm_is_true(value)) {
171 controld_set_ra_param_flags(p, ra_param_unique);
172 }
173
174 value = crm_element_value(param_xml, "private");
175 if (crm_is_true(value)) {
176 controld_set_ra_param_flags(p, ra_param_private);
177 }
178 return p;
179 }
180
181 static void
182 log_ra_ocf_version(const char *ra_key, const char *ra_ocf_version)
183 {
184 if (pcmk__str_empty(ra_ocf_version)) {
185 crm_warn("%s does not advertise OCF version supported", ra_key);
186
187 } else if (compare_version(ra_ocf_version, "2") >= 0) {
188 crm_warn("%s supports OCF version %s (this Pacemaker version supports "
189 PCMK_OCF_VERSION " and might not work properly with agent)",
190 ra_key, ra_ocf_version);
191
192 } else if (compare_version(ra_ocf_version, PCMK_OCF_VERSION) > 0) {
193 crm_info("%s supports OCF version %s (this Pacemaker version supports "
194 PCMK_OCF_VERSION " and might not use all agent features)",
195 ra_key, ra_ocf_version);
196
197 } else {
198 crm_debug("%s supports OCF version %s", ra_key, ra_ocf_version);
199 }
200 }
201
202 struct ra_metadata_s *
203 metadata_cache_update(GHashTable *mdc, lrmd_rsc_info_t *rsc,
204 const char *metadata_str)
205 {
206 char *key = NULL;
207 xmlNode *metadata = NULL;
208 xmlNode *match = NULL;
209 struct ra_metadata_s *md = NULL;
210 bool any_private_params = false;
211 bool ocf1_1 = false;
212
213 CRM_CHECK(mdc && rsc && metadata_str, return NULL);
214
215 key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
216 if (!key) {
217 crm_crit("Could not allocate memory for resource metadata");
218 goto err;
219 }
220
221 metadata = string2xml(metadata_str);
222 if (!metadata) {
223 crm_err("Metadata for %s:%s:%s is not valid XML",
224 rsc->standard, rsc->provider, rsc->type);
225 goto err;
226 }
227
228 md = calloc(1, sizeof(struct ra_metadata_s));
229 if (md == NULL) {
230 crm_crit("Could not allocate memory for resource metadata");
231 goto err;
232 }
233
234 #if ENABLE_VERSIONED_ATTRS
235 md->ra_version = ra_version_from_xml(metadata, rsc);
236 #endif
237
238 if (strcmp(rsc->standard, PCMK_RESOURCE_CLASS_OCF) == 0) {
239 xmlChar *content = NULL;
240 xmlNode *version_element = first_named_child(metadata, "version");
241
242 if (version_element != NULL) {
243 content = xmlNodeGetContent(version_element);
244 }
245 log_ra_ocf_version(key, (const char *) content);
246 if (content != NULL) {
247 ocf1_1 = (compare_version((const char *) content, "1.1") >= 0);
248 xmlFree(content);
249 }
250 }
251
252
253 match = first_named_child(metadata, "actions");
254 for (match = first_named_child(match, "action"); match != NULL;
255 match = crm_next_same_xml(match)) {
256
257 const char *action_name = crm_element_value(match, "name");
258
259 if (pcmk__str_eq(action_name, CRMD_ACTION_RELOAD_AGENT,
260 pcmk__str_none)) {
261 if (ocf1_1) {
262 controld_set_ra_flags(md, key, ra_supports_reload_agent);
263 } else {
264 crm_notice("reload-agent action will not be used with %s "
265 "because it does not support OCF 1.1 or later", key);
266 }
267
268 } else if (!ocf1_1 && pcmk__str_eq(action_name, CRMD_ACTION_RELOAD,
269 pcmk__str_casei)) {
270 controld_set_ra_flags(md, key, ra_supports_legacy_reload);
271 }
272 }
273
274
275 match = first_named_child(metadata, "parameters");
276 for (match = first_named_child(match, "parameter"); match != NULL;
277 match = crm_next_same_xml(match)) {
278
279 const char *param_name = crm_element_value(match, "name");
280
281 if (param_name == NULL) {
282 crm_warn("Metadata for %s:%s:%s has parameter without a name",
283 rsc->standard, rsc->provider, rsc->type);
284 } else {
285 struct ra_param_s *p = ra_param_from_xml(match);
286
287 if (p == NULL) {
288 goto err;
289 }
290 if (pcmk_is_set(p->rap_flags, ra_param_private)) {
291 any_private_params = true;
292 }
293 md->ra_params = g_list_prepend(md->ra_params, p);
294 }
295 }
296
297
298
299
300
301
302 if (!any_private_params) {
303 for (GList *iter = md->ra_params; iter != NULL; iter = iter->next) {
304 struct ra_param_s *p = iter->data;
305
306 if (pcmk__str_any_of(p->rap_name, "password", "passwd", "user",
307 NULL)) {
308 controld_set_ra_param_flags(p, ra_param_private);
309 }
310 }
311 }
312
313 g_hash_table_replace(mdc, key, md);
314 free_xml(metadata);
315 return md;
316
317 err:
318 free(key);
319 free_xml(metadata);
320 metadata_free(md);
321 return NULL;
322 }
323
324
325
326
327
328
329
330
331
332
333
334
335 struct ra_metadata_s *
336 controld_get_rsc_metadata(lrm_state_t *lrm_state, lrmd_rsc_info_t *rsc,
337 uint32_t source)
338 {
339 struct ra_metadata_s *metadata = NULL;
340 char *metadata_str = NULL;
341 char *key = NULL;
342 int rc = pcmk_ok;
343
344 CRM_CHECK((lrm_state != NULL) && (rsc != NULL), return NULL);
345
346 if (pcmk_is_set(source, controld_metadata_from_cache)) {
347 key = crm_generate_ra_key(rsc->standard, rsc->provider, rsc->type);
348 if (key != NULL) {
349 metadata = g_hash_table_lookup(lrm_state->metadata_cache, key);
350 free(key);
351 }
352 if (metadata != NULL) {
353 return metadata;
354 }
355 }
356
357 if (!pcmk_is_set(source, controld_metadata_from_agent)) {
358 return NULL;
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372
373 rc = lrm_state_get_metadata(lrm_state, rsc->standard, rsc->provider,
374 rsc->type, &metadata_str, 0);
375 if (rc != pcmk_ok) {
376 crm_warn("Failed to get metadata for %s (%s:%s:%s): %s",
377 rsc->id, rsc->standard, rsc->provider, rsc->type,
378 pcmk_strerror(rc));
379 return NULL;
380 }
381
382 metadata = metadata_cache_update(lrm_state->metadata_cache, rsc,
383 metadata_str);
384 free(metadata_str);
385 if (metadata == NULL) {
386 crm_warn("Failed to update metadata for %s (%s:%s:%s)",
387 rsc->id, rsc->standard, rsc->provider, rsc->type);
388 }
389 return metadata;
390 }