pacemaker 2.1.5-a3f44794f94
Scalable High-Availability cluster resource manager
output_xml.c
Go to the documentation of this file.
1/*
2 * Copyright 2019-2022 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 <ctype.h>
13#include <stdarg.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <glib.h>
17
19#include <crm/common/xml.h>
20
21static gboolean legacy_xml = FALSE;
22static gboolean simple_list = FALSE;
23static gboolean substitute = FALSE;
24
25GOptionEntry pcmk__xml_output_entries[] = {
26 { "xml-legacy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &legacy_xml,
27 NULL,
28 NULL },
29 { "xml-simple-list", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &simple_list,
30 NULL,
31 NULL },
32 { "xml-substitute", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &substitute,
33 NULL,
34 NULL },
35
36 { NULL }
37};
38
39typedef struct subst_s {
40 const char *from;
41 const char *to;
43
44static subst_t substitutions[] = {
45 { "Active Resources", "resources" },
46 { "Allocation Scores", "allocations" },
47 { "Allocation Scores and Utilization Information", "allocations_utilizations" },
48 { "Cluster Summary", "summary" },
49 { "Current cluster status", "cluster_status" },
50 { "Executing Cluster Transition", "transition" },
51 { "Failed Resource Actions", "failures" },
52 { "Fencing History", "fence_history" },
53 { "Full List of Resources", "resources" },
54 { "Inactive Resources", "resources" },
55 { "Migration Summary", "node_history" },
56 { "Negative Location Constraints", "bans" },
57 { "Node Attributes", "node_attributes" },
58 { "Operations", "node_history" },
59 { "Resource Config", "resource_config" },
60 { "Resource Operations", "operations" },
61 { "Revised Cluster Status", "revised_cluster_status" },
62 { "Transition Summary", "actions" },
63 { "Utilization Information", "utilizations" },
64
65 { NULL, NULL }
66};
67
68/* The first several elements of this struct must be the same as the first
69 * several elements of private_data_s in lib/common/output_html.c. That
70 * struct gets passed to a bunch of the pcmk__output_xml_* functions which
71 * assume an XML private_data_s. Keeping them laid out the same means this
72 * still works.
73 */
74typedef struct private_data_s {
75 /* Begin members that must match the HTML version */
76 xmlNode *root;
77 GQueue *parent_q;
78 GSList *errors;
79 /* End members that must match the HTML version */
80 bool legacy_xml;
82
83static void
84xml_free_priv(pcmk__output_t *out) {
85 private_data_t *priv = NULL;
86
87 if (out == NULL || out->priv == NULL) {
88 return;
89 }
90
91 priv = out->priv;
92
93 free_xml(priv->root);
94 g_queue_free(priv->parent_q);
95 g_slist_free(priv->errors);
96 free(priv);
97 out->priv = NULL;
98}
99
100static bool
101xml_init(pcmk__output_t *out) {
102 private_data_t *priv = NULL;
103
104 CRM_ASSERT(out != NULL);
105
106 /* If xml_init was previously called on this output struct, just return. */
107 if (out->priv != NULL) {
108 return true;
109 } else {
110 out->priv = calloc(1, sizeof(private_data_t));
111 if (out->priv == NULL) {
112 return false;
113 }
114
115 priv = out->priv;
116 }
117
118 if (legacy_xml) {
119 priv->root = create_xml_node(NULL, "crm_mon");
120 crm_xml_add(priv->root, "version", PACEMAKER_VERSION);
121 } else {
122 priv->root = create_xml_node(NULL, "pacemaker-result");
123 crm_xml_add(priv->root, "api-version", PCMK__API_VERSION);
124
125 if (out->request != NULL) {
126 crm_xml_add(priv->root, "request", out->request);
127 }
128 }
129
130 priv->parent_q = g_queue_new();
131 priv->errors = NULL;
132 g_queue_push_tail(priv->parent_q, priv->root);
133
134 /* Copy this from the file-level variable. This means that it is only settable
135 * as a command line option, and that pcmk__output_new must be called after all
136 * command line processing is completed.
137 */
138 priv->legacy_xml = legacy_xml;
139
140 return true;
141}
142
143static void
144add_error_node(gpointer data, gpointer user_data) {
145 char *str = (char *) data;
146 xmlNodePtr node = (xmlNodePtr) user_data;
147 pcmk_create_xml_text_node(node, "error", str);
148}
149
150static void
151xml_finish(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest) {
152 private_data_t *priv = NULL;
153 xmlNodePtr node;
154
155 CRM_ASSERT(out != NULL);
156 priv = out->priv;
157
158 /* If root is NULL, xml_init failed and we are being called from pcmk__output_free
159 * in the pcmk__output_new path.
160 */
161 if (priv == NULL || priv->root == NULL) {
162 return;
163 }
164
165 if (legacy_xml) {
166 GSList *node = priv->errors;
167
168 if (exit_status != CRM_EX_OK) {
169 fprintf(stderr, "%s\n", crm_exit_str(exit_status));
170 }
171
172 while (node != NULL) {
173 fprintf(stderr, "%s\n", (char *) node->data);
174 node = node->next;
175 }
176 } else {
177 char *rc_as_str = pcmk__itoa(exit_status);
178
179 node = create_xml_node(priv->root, "status");
180 pcmk__xe_set_props(node, "code", rc_as_str,
181 "message", crm_exit_str(exit_status),
182 NULL);
183
184 if (g_slist_length(priv->errors) > 0) {
185 xmlNodePtr errors_node = create_xml_node(node, "errors");
186 g_slist_foreach(priv->errors, add_error_node, (gpointer) errors_node);
187 }
188
189 free(rc_as_str);
190 }
191
192 if (print) {
193 char *buf = dump_xml_formatted_with_text(priv->root);
194 fprintf(out->dest, "%s", buf);
195 fflush(out->dest);
196 free(buf);
197 }
198
199 if (copy_dest != NULL) {
200 *copy_dest = copy_xml(priv->root);
201 }
202}
203
204static void
205xml_reset(pcmk__output_t *out) {
206 CRM_ASSERT(out != NULL);
207
208 out->dest = freopen(NULL, "w", out->dest);
209 CRM_ASSERT(out->dest != NULL);
210
211 xml_free_priv(out);
212 xml_init(out);
213}
214
215static void
216xml_subprocess_output(pcmk__output_t *out, int exit_status,
217 const char *proc_stdout, const char *proc_stderr) {
218 xmlNodePtr node, child_node;
219 char *rc_as_str = NULL;
220
221 CRM_ASSERT(out != NULL);
222
223 rc_as_str = pcmk__itoa(exit_status);
224
225 node = pcmk__output_xml_create_parent(out, "command",
226 "code", rc_as_str,
227 NULL);
228
229 if (proc_stdout != NULL) {
230 child_node = pcmk_create_xml_text_node(node, "output", proc_stdout);
231 crm_xml_add(child_node, "source", "stdout");
232 }
233
234 if (proc_stderr != NULL) {
235 child_node = pcmk_create_xml_text_node(node, "output", proc_stderr);
236 crm_xml_add(child_node, "source", "stderr");
237 }
238
239 pcmk__output_xml_add_node(out, node);
240 free(rc_as_str);
241}
242
243static void
244xml_version(pcmk__output_t *out, bool extended) {
245 CRM_ASSERT(out != NULL);
246
247 pcmk__output_create_xml_node(out, "version",
248 "program", "Pacemaker",
249 "version", PACEMAKER_VERSION,
250 "author", "Andrew Beekhof and the "
251 "Pacemaker project contributors",
252 "build", BUILD_VERSION,
253 "features", CRM_FEATURES,
254 NULL);
255}
256
257G_GNUC_PRINTF(2, 3)
258static void
259xml_err(pcmk__output_t *out, const char *format, ...) {
260 private_data_t *priv = NULL;
261 int len = 0;
262 char *buf = NULL;
263 va_list ap;
264
265 CRM_ASSERT(out != NULL && out->priv != NULL);
266 priv = out->priv;
267
268 va_start(ap, format);
269 len = vasprintf(&buf, format, ap);
270 CRM_ASSERT(len > 0);
271 va_end(ap);
272
273 priv->errors = g_slist_append(priv->errors, buf);
274}
275
276G_GNUC_PRINTF(2, 3)
277static int
278xml_info(pcmk__output_t *out, const char *format, ...) {
279 return pcmk_rc_no_output;
280}
281
282static void
283xml_output_xml(pcmk__output_t *out, const char *name, const char *buf) {
284 xmlNodePtr parent = NULL;
285 xmlNodePtr cdata_node = NULL;
286
287 CRM_ASSERT(out != NULL);
288
290 cdata_node = xmlNewCDataBlock(getDocPtr(parent), (pcmkXmlStr) buf, strlen(buf));
291 xmlAddChild(parent, cdata_node);
292}
293
294G_GNUC_PRINTF(4, 5)
295static void
296xml_begin_list(pcmk__output_t *out, const char *singular_noun, const char *plural_noun,
297 const char *format, ...) {
298 va_list ap;
299 char *name = NULL;
300 char *buf = NULL;
301 int len;
302
303 CRM_ASSERT(out != NULL);
304
305 va_start(ap, format);
306 len = vasprintf(&buf, format, ap);
307 CRM_ASSERT(len >= 0);
308 va_end(ap);
309
310 if (substitute) {
311 for (subst_t *s = substitutions; s->from != NULL; s++) {
312 if (!strcmp(s->from, buf)) {
313 name = g_strdup(s->to);
314 break;
315 }
316 }
317 }
318
319 if (name == NULL) {
320 name = g_ascii_strdown(buf, -1);
321 }
322
323 if (legacy_xml || simple_list) {
325 } else {
327 "name", name,
328 NULL);
329 }
330
331 g_free(name);
332 free(buf);
333}
334
335G_GNUC_PRINTF(3, 4)
336static void
337xml_list_item(pcmk__output_t *out, const char *name, const char *format, ...) {
338 xmlNodePtr item_node = NULL;
339 va_list ap;
340 char *buf = NULL;
341 int len;
342
343 CRM_ASSERT(out != NULL);
344
345 va_start(ap, format);
346 len = vasprintf(&buf, format, ap);
347 CRM_ASSERT(len >= 0);
348 va_end(ap);
349
350 item_node = pcmk__output_create_xml_text_node(out, "item", buf);
351
352 if (name != NULL) {
353 crm_xml_add(item_node, "name", name);
354 }
355
356 free(buf);
357}
358
359static void
360xml_increment_list(pcmk__output_t *out) {
361 /* This function intentially left blank */
362}
363
364static void
365xml_end_list(pcmk__output_t *out) {
366 private_data_t *priv = NULL;
367
368 CRM_ASSERT(out != NULL && out->priv != NULL);
369 priv = out->priv;
370
371 if (priv->legacy_xml || simple_list) {
372 g_queue_pop_tail(priv->parent_q);
373 } else {
374 char *buf = NULL;
375 xmlNodePtr node;
376
377 node = g_queue_pop_tail(priv->parent_q);
378 buf = crm_strdup_printf("%lu", xmlChildElementCount(node));
379 crm_xml_add(node, "count", buf);
380 free(buf);
381 }
382}
383
384static bool
385xml_is_quiet(pcmk__output_t *out) {
386 return false;
387}
388
389static void
390xml_spacer(pcmk__output_t *out) {
391 /* This function intentionally left blank */
392}
393
394static void
395xml_progress(pcmk__output_t *out, bool end) {
396 /* This function intentionally left blank */
397}
398
401 pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
402
403 if (retval == NULL) {
404 return NULL;
405 }
406
407 retval->fmt_name = "xml";
408 retval->request = pcmk__quote_cmdline(argv);
409
410 retval->init = xml_init;
411 retval->free_priv = xml_free_priv;
412 retval->finish = xml_finish;
413 retval->reset = xml_reset;
414
416 retval->message = pcmk__call_message;
417
418 retval->subprocess_output = xml_subprocess_output;
419 retval->version = xml_version;
420 retval->info = xml_info;
421 retval->err = xml_err;
422 retval->output_xml = xml_output_xml;
423
424 retval->begin_list = xml_begin_list;
425 retval->list_item = xml_list_item;
426 retval->increment_list = xml_increment_list;
427 retval->end_list = xml_end_list;
428
429 retval->is_quiet = xml_is_quiet;
430 retval->spacer = xml_spacer;
431 retval->progress = xml_progress;
432 retval->prompt = pcmk__text_prompt;
433
434 return retval;
435}
436
437xmlNodePtr
439 va_list args;
440 xmlNodePtr node = NULL;
441
442 CRM_ASSERT(out != NULL);
443 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return NULL);
444
445 node = pcmk__output_create_xml_node(out, name, NULL);
446
447 va_start(args, name);
448 pcmk__xe_set_propv(node, args);
449 va_end(args);
450
452 return node;
453}
454
455void
457 private_data_t *priv = NULL;
458
459 CRM_ASSERT(out != NULL && out->priv != NULL);
460 CRM_ASSERT(node != NULL);
461 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return);
462
463 priv = out->priv;
464
465 xmlAddChild(g_queue_peek_tail(priv->parent_q), node);
466}
467
468xmlNodePtr
470 xmlNodePtr node = NULL;
471 private_data_t *priv = NULL;
472 va_list args;
473
474 CRM_ASSERT(out != NULL && out->priv != NULL);
475 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return NULL);
476
477 priv = out->priv;
478
479 node = create_xml_node(g_queue_peek_tail(priv->parent_q), name);
480 va_start(args, name);
481 pcmk__xe_set_propv(node, args);
482 va_end(args);
483
484 return node;
485}
486
487xmlNodePtr
488pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content) {
489 xmlNodePtr node = NULL;
490
491 CRM_ASSERT(out != NULL);
492 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return NULL);
493
494 node = pcmk__output_create_xml_node(out, name, NULL);
495 xmlNodeSetContent(node, (pcmkXmlStr) content);
496 return node;
497}
498
499void
501 private_data_t *priv = NULL;
502
503 CRM_ASSERT(out != NULL && out->priv != NULL);
504 CRM_ASSERT(parent != NULL);
505 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return);
506
507 priv = out->priv;
508
509 g_queue_push_tail(priv->parent_q, parent);
510}
511
512void
514 private_data_t *priv = NULL;
515
516 CRM_ASSERT(out != NULL && out->priv != NULL);
517 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return);
518
519 priv = out->priv;
520
521 CRM_ASSERT(g_queue_get_length(priv->parent_q) > 0);
522 g_queue_pop_tail(priv->parent_q);
523}
524
525xmlNodePtr
527 private_data_t *priv = NULL;
528
529 CRM_ASSERT(out != NULL && out->priv != NULL);
530 CRM_CHECK(pcmk__str_any_of(out->fmt_name, "xml", "html", NULL), return NULL);
531
532 priv = out->priv;
533
534 /* If queue is empty NULL will be returned */
535 return g_queue_peek_tail(priv->parent_q);
536}
const char * parent
Definition: cib.c:25
const char * name
Definition: cib.c:24
gchar * pcmk__quote_cmdline(gchar **argv)
Definition: cmdline.c:165
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
#define PACEMAKER_VERSION
Definition: config.h:502
#define CRM_FEATURES
Definition: config.h:33
#define BUILD_VERSION
Definition: config.h:8
char data[0]
Definition: cpg.c:10
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:227
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:323
struct private_data_s private_data_t
#define PCMK__API_VERSION
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
Definition: output_text.c:394
int pcmk__call_message(pcmk__output_t *out, const char *message_id,...)
Definition: output.c:131
void pcmk__register_message(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
Definition: output.c:153
GOptionEntry pcmk__xml_output_entries[]
Definition: output_xml.c:25
void pcmk__output_xml_pop_parent(pcmk__output_t *out)
Definition: output_xml.c:513
void pcmk__output_xml_push_parent(pcmk__output_t *out, xmlNodePtr parent)
Definition: output_xml.c:500
xmlNodePtr pcmk__output_xml_peek_parent(pcmk__output_t *out)
Definition: output_xml.c:526
xmlNodePtr pcmk__output_create_xml_node(pcmk__output_t *out, const char *name,...)
Definition: output_xml.c:469
void pcmk__output_xml_add_node(pcmk__output_t *out, xmlNodePtr node)
Definition: output_xml.c:456
xmlNodePtr pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name,...)
Definition: output_xml.c:438
struct subst_s subst_t
struct private_data_s private_data_t
pcmk__output_t * pcmk__mk_xml_output(char **argv)
Definition: output_xml.c:400
xmlNodePtr pcmk__output_create_xml_text_node(pcmk__output_t *out, const char *name, const char *content)
Definition: output_xml.c:488
#define CRM_ASSERT(expr)
Definition: results.h:42
@ CRM_EX_OK
Success.
Definition: results.h:234
@ pcmk_rc_no_output
Definition: results.h:118
const char * crm_exit_str(crm_exit_t exit_code)
Definition: results.c:615
enum crm_exit_e crm_exit_t
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:952
This structure contains everything that makes up a single output formatter.
void(*) void(*) void(* increment_list)(pcmk__output_t *out)
void(* end_list)(pcmk__output_t *out)
void(* version)(pcmk__output_t *out, bool extended)
int(* message)(pcmk__output_t *out, const char *message_id,...)
bool(* is_quiet)(pcmk__output_t *out)
const char * fmt_name
The name of this output formatter.
FILE * dest
Where output should be written.
int(*) void(*) void(* output_xml)(pcmk__output_t *out, const char *name, const char *buf)
void(* register_message)(pcmk__output_t *out, const char *message_id, pcmk__message_fn_t fn)
void(*) void(* list_item)(pcmk__output_t *out, const char *name, const char *format,...) G_GNUC_PRINTF(3
int(*) void(* err)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
void(* finish)(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest)
void(* prompt)(const char *prompt, bool echo, char **dest)
void(* subprocess_output)(pcmk__output_t *out, int exit_status, const char *proc_stdout, const char *proc_stderr)
void(* begin_list)(pcmk__output_t *out, const char *singular_noun, const char *plural_noun, const char *format,...) G_GNUC_PRINTF(4
bool(* init)(pcmk__output_t *out)
void * priv
Implementation-specific private data.
void(* spacer)(pcmk__output_t *out)
void(* progress)(pcmk__output_t *out, bool end)
void(* reset)(pcmk__output_t *out)
int(* info)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
void(* free_priv)(pcmk__output_t *out)
gchar * request
A copy of the request that generated this output.
Wrappers for and extensions to libxml2.
xmlNode * pcmk_create_xml_text_node(xmlNode *parent, const char *name, const char *content)
Definition: xml.c:774
const xmlChar * pcmkXmlStr
Definition: xml.h:50
char * dump_xml_formatted_with_text(xmlNode *msg)
Definition: xml.c:2089
xmlDoc * getDocPtr(xmlNode *node)
Definition: xml.c:711
void free_xml(xmlNode *child)
Definition: xml.c:885
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:891
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:749
void pcmk__xe_set_propv(xmlNodePtr node, va_list pairs)
Definition: xml.c:3085
void pcmk__xe_set_props(xmlNodePtr node,...) G_GNUC_NULL_TERMINATED
Definition: xml.c:3103