root/lib/common/output_xml.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. xml_free_priv
  2. xml_init
  3. add_error_node
  4. xml_finish
  5. xml_reset
  6. xml_subprocess_output
  7. xml_version
  8. G_GNUC_PRINTF
  9. G_GNUC_PRINTF
  10. xml_output_xml
  11. G_GNUC_PRINTF
  12. G_GNUC_PRINTF
  13. xml_increment_list
  14. xml_end_list
  15. xml_is_quiet
  16. xml_spacer
  17. xml_progress
  18. pcmk__mk_xml_output
  19. pcmk__output_xml_create_parent
  20. pcmk__output_xml_add_node
  21. pcmk__output_create_xml_node
  22. pcmk__output_create_xml_text_node
  23. pcmk__output_xml_push_parent
  24. pcmk__output_xml_pop_parent
  25. pcmk__output_xml_peek_parent

   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 
  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 /* 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  */
  74 typedef 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;
  81 } private_data_t;
  82 
  83 static void
  84 xml_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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 
 143 static void
 144 add_error_node(gpointer data, gpointer user_data) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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 
 204 static void
 205 xml_reset(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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,
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 361     /* This function intentially left blank */
 362 }
 363 
 364 static void
 365 xml_end_list(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 386     return false;
 387 }
 388 
 389 static void
 390 xml_spacer(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 391     /* This function intentionally left blank */
 392 }
 393 
 394 static void
 395 xml_progress(pcmk__output_t *out, bool end) {
     /* [previous][next][first][last][top][bottom][index][help] */
 396     /* This function intentionally left blank */
 397 }
 398 
 399 pcmk__output_t *
 400 pcmk__mk_xml_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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, ...) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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, ...) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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 }

/* [previous][next][first][last][top][bottom][index][help] */