This source file includes following definitions.
- xml_free_priv
- xml_init
- add_error_node
- xml_finish
- xml_reset
- xml_subprocess_output
- xml_version
- G_GNUC_PRINTF
- G_GNUC_PRINTF
- xml_output_xml
- G_GNUC_PRINTF
- G_GNUC_PRINTF
- xml_increment_list
- xml_end_list
- xml_is_quiet
- xml_spacer
- xml_progress
- pcmk__mk_xml_output
- pcmk__output_xml_create_parent
- pcmk__output_xml_add_node
- pcmk__output_create_xml_node
- pcmk__output_create_xml_text_node
- pcmk__output_xml_push_parent
- pcmk__output_xml_pop_parent
- pcmk__output_xml_peek_parent
1
2
3
4
5
6
7
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
18 #include <crm/common/cmdline_internal.h>
19 #include <crm/common/xml.h>
20
21 static gboolean legacy_xml = FALSE;
22 static gboolean simple_list = FALSE;
23 static gboolean substitute = FALSE;
24
25 GOptionEntry 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
39 typedef struct subst_s {
40 const char *from;
41 const char *to;
42 } subst_t;
43
44 static 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
69
70
71
72
73
74 typedef struct private_data_s {
75
76 xmlNode *root;
77 GQueue *parent_q;
78 GSList *errors;
79
80 bool legacy_xml;
81 } private_data_t;
82
83 static void
84 xml_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
100 static bool
101 xml_init(pcmk__output_t *out) {
102 private_data_t *priv = NULL;
103
104 CRM_ASSERT(out != NULL);
105
106
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
135
136
137
138 priv->legacy_xml = legacy_xml;
139
140 return true;
141 }
142
143 static void
144 add_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
150 static void
151 xml_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
159
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
204 static void
205 xml_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
215 static void
216 xml_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
243 static void
244 xml_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
257 G_GNUC_PRINTF(2, 3)
258 static void
259 xml_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
276 G_GNUC_PRINTF(2, 3)
277 static int
278 xml_info(pcmk__output_t *out, const char *format, ...) {
279 return pcmk_rc_no_output;
280 }
281
282 static void
283 xml_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
289 parent = pcmk__output_create_xml_node(out, name, NULL);
290 cdata_node = xmlNewCDataBlock(getDocPtr(parent), (pcmkXmlStr) buf, strlen(buf));
291 xmlAddChild(parent, cdata_node);
292 }
293
294 G_GNUC_PRINTF(4, 5)
295 static void
296 xml_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) {
324 pcmk__output_xml_create_parent(out, name, NULL);
325 } else {
326 pcmk__output_xml_create_parent(out, "list",
327 "name", name,
328 NULL);
329 }
330
331 g_free(name);
332 free(buf);
333 }
334
335 G_GNUC_PRINTF(3, 4)
336 static void
337 xml_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
359 static void
360 xml_increment_list(pcmk__output_t *out) {
361
362 }
363
364 static void
365 xml_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
384 static bool
385 xml_is_quiet(pcmk__output_t *out) {
386 return false;
387 }
388
389 static void
390 xml_spacer(pcmk__output_t *out) {
391
392 }
393
394 static void
395 xml_progress(pcmk__output_t *out, bool end) {
396
397 }
398
399 pcmk__output_t *
400 pcmk__mk_xml_output(char **argv) {
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
415 retval->register_message = pcmk__register_message;
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
437 xmlNodePtr
438 pcmk__output_xml_create_parent(pcmk__output_t *out, const char *name, ...) {
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
451 pcmk__output_xml_push_parent(out, node);
452 return node;
453 }
454
455 void
456 pcmk__output_xml_add_node(pcmk__output_t *out, xmlNodePtr node) {
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
468 xmlNodePtr
469 pcmk__output_create_xml_node(pcmk__output_t *out, const char *name, ...) {
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
487 xmlNodePtr
488 pcmk__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
499 void
500 pcmk__output_xml_push_parent(pcmk__output_t *out, xmlNodePtr parent) {
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
512 void
513 pcmk__output_xml_pop_parent(pcmk__output_t *out) {
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
525 xmlNodePtr
526 pcmk__output_xml_peek_parent(pcmk__output_t *out) {
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
535 return g_queue_peek_tail(priv->parent_q);
536 }