root/lib/common/nvpair.c

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

DEFINITIONS

This source file includes following definitions.
  1. pcmk__new_nvpair
  2. pcmk__free_nvpair
  3. pcmk_prepend_nvpair
  4. pcmk_free_nvpairs
  5. pcmk__compare_nvpair
  6. pcmk_sort_nvpairs
  7. pcmk_xml_attrs2nvpairs
  8. pcmk__nvpair_add_xml_attr
  9. pcmk_nvpairs2xml_attrs
  10. pcmk__scan_nvpair
  11. pcmk__format_nvpair
  12. crm_xml_add
  13. crm_xml_replace
  14. crm_xml_add_int
  15. crm_xml_add_ms
  16. crm_xml_add_ll
  17. crm_xml_add_timeval
  18. crm_element_value
  19. crm_element_value_int
  20. crm_element_value_ll
  21. crm_element_value_ms
  22. crm_element_value_epoch
  23. crm_element_value_timeval
  24. crm_element_value_copy
  25. hash2smartfield
  26. hash2field
  27. hash2metafield
  28. crm_create_nvpair_xml
  29. hash2nvpair
  30. xml2list
  31. pcmk__xe_set_bool_attr
  32. pcmk__xe_get_bool_attr
  33. pcmk__xe_attr_is_true
  34. pcmk_scan_nvpair
  35. pcmk_format_nvpair
  36. pcmk_format_named_time

   1 /*
   2  * Copyright 2004-2023 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 <sys/types.h>
  14 #include <string.h>
  15 #include <ctype.h>
  16 #include <glib.h>
  17 #include <libxml/tree.h>
  18 
  19 #include <crm/crm.h>
  20 #include <crm/msg_xml.h>
  21 #include <crm/common/xml.h>
  22 #include <crm/common/xml_internal.h>
  23 #include "crmcommon_private.h"
  24 
  25 /*
  26  * This file isolates handling of three types of name/value pairs:
  27  *
  28  * - pcmk_nvpair_t data type
  29  * - XML attributes (<TAG ... NAME=VALUE ...>)
  30  * - XML nvpair elements (<nvpair id=ID name=NAME value=VALUE>)
  31  */
  32 
  33 // pcmk_nvpair_t handling
  34 
  35 /*!
  36  * \internal
  37  * \brief Allocate a new name/value pair
  38  *
  39  * \param[in] name   New name (required)
  40  * \param[in] value  New value
  41  *
  42  * \return Newly allocated name/value pair
  43  * \note The caller is responsible for freeing the result with
  44  *       \c pcmk__free_nvpair().
  45  */
  46 static pcmk_nvpair_t *
  47 pcmk__new_nvpair(const char *name, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49     pcmk_nvpair_t *nvpair = NULL;
  50 
  51     CRM_ASSERT(name);
  52 
  53     nvpair = calloc(1, sizeof(pcmk_nvpair_t));
  54     CRM_ASSERT(nvpair);
  55 
  56     pcmk__str_update(&nvpair->name, name);
  57     pcmk__str_update(&nvpair->value, value);
  58     return nvpair;
  59 }
  60 
  61 /*!
  62  * \internal
  63  * \brief Free a name/value pair
  64  *
  65  * \param[in,out] nvpair  Name/value pair to free
  66  */
  67 static void
  68 pcmk__free_nvpair(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70     if (data) {
  71         pcmk_nvpair_t *nvpair = data;
  72 
  73         free(nvpair->name);
  74         free(nvpair->value);
  75         free(nvpair);
  76     }
  77 }
  78 
  79 /*!
  80  * \brief Prepend a name/value pair to a list
  81  *
  82  * \param[in,out] nvpairs  List to modify
  83  * \param[in]     name     New entry's name
  84  * \param[in]     value    New entry's value
  85  *
  86  * \return New head of list
  87  * \note The caller is responsible for freeing the list with
  88  *       \c pcmk_free_nvpairs().
  89  */
  90 GSList *
  91 pcmk_prepend_nvpair(GSList *nvpairs, const char *name, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93     return g_slist_prepend(nvpairs, pcmk__new_nvpair(name, value));
  94 }
  95 
  96 /*!
  97  * \brief Free a list of name/value pairs
  98  *
  99  * \param[in,out] list  List to free
 100  */
 101 void
 102 pcmk_free_nvpairs(GSList *nvpairs)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104     g_slist_free_full(nvpairs, pcmk__free_nvpair);
 105 }
 106 
 107 /*!
 108  * \internal
 109  * \brief Compare two name/value pairs
 110  *
 111  * \param[in] a  First name/value pair to compare
 112  * \param[in] b  Second name/value pair to compare
 113  *
 114  * \return 0 if a == b, 1 if a > b, -1 if a < b
 115  */
 116 static gint
 117 pcmk__compare_nvpair(gconstpointer a, gconstpointer b)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119     int rc = 0;
 120     const pcmk_nvpair_t *pair_a = a;
 121     const pcmk_nvpair_t *pair_b = b;
 122 
 123     CRM_ASSERT(a != NULL);
 124     CRM_ASSERT(pair_a->name != NULL);
 125 
 126     CRM_ASSERT(b != NULL);
 127     CRM_ASSERT(pair_b->name != NULL);
 128 
 129     rc = strcmp(pair_a->name, pair_b->name);
 130     if (rc < 0) {
 131         return -1;
 132     } else if (rc > 0) {
 133         return 1;
 134     }
 135     return 0;
 136 }
 137 
 138 /*!
 139  * \brief Sort a list of name/value pairs
 140  *
 141  * \param[in,out] list  List to sort
 142  *
 143  * \return New head of list
 144  */
 145 GSList *
 146 pcmk_sort_nvpairs(GSList *list)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     return g_slist_sort(list, pcmk__compare_nvpair);
 149 }
 150 
 151 /*!
 152  * \brief Create a list of name/value pairs from an XML node's attributes
 153  *
 154  * \param[in]  XML to parse
 155  *
 156  * \return New list of name/value pairs
 157  * \note It is the caller's responsibility to free the list with
 158  *       \c pcmk_free_nvpairs().
 159  */
 160 GSList *
 161 pcmk_xml_attrs2nvpairs(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163     GSList *result = NULL;
 164 
 165     for (xmlAttrPtr iter = pcmk__xe_first_attr(xml); iter != NULL;
 166          iter = iter->next) {
 167 
 168         result = pcmk_prepend_nvpair(result,
 169                                      (const char *) iter->name,
 170                                      (const char *) pcmk__xml_attr_value(iter));
 171     }
 172     return result;
 173 }
 174 
 175 /*!
 176  * \internal
 177  * \brief Add an XML attribute corresponding to a name/value pair
 178  *
 179  * Suitable for glib list iterators, this function adds a NAME=VALUE
 180  * XML attribute based on a given name/value pair.
 181  *
 182  * \param[in]  data       Name/value pair
 183  * \param[out] user_data  XML node to add attributes to
 184  */
 185 static void
 186 pcmk__nvpair_add_xml_attr(gpointer data, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188     pcmk_nvpair_t *pair = data;
 189     xmlNode *parent = user_data;
 190 
 191     crm_xml_add(parent, pair->name, pair->value);
 192 }
 193 
 194 /*!
 195  * \brief Add XML attributes based on a list of name/value pairs
 196  *
 197  * \param[in,out] list  List of name/value pairs
 198  * \param[in,out] xml   XML node to add attributes to
 199  */
 200 void
 201 pcmk_nvpairs2xml_attrs(GSList *list, xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203     g_slist_foreach(list, pcmk__nvpair_add_xml_attr, xml);
 204 }
 205 
 206 // convenience function for name=value strings
 207 
 208 /*!
 209  * \internal
 210  * \brief Extract the name and value from an input string formatted as "name=value".
 211  * If unable to extract them, they are returned as NULL.
 212  *
 213  * \param[in]  input The input string, likely from the command line
 214  * \param[out] name  Everything before the first '=' in the input string
 215  * \param[out] value Everything after the first '=' in the input string
 216  *
 217  * \return 2 if both name and value could be extracted, 1 if only one could, and
 218  *         and error code otherwise
 219  */
 220 int
 221 pcmk__scan_nvpair(const char *input, char **name, char **value)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223 #ifdef HAVE_SSCANF_M
 224     *name = NULL;
 225     *value = NULL;
 226     if (sscanf(input, "%m[^=]=%m[^\n]", name, value) <= 0) {
 227         return -pcmk_err_bad_nvpair;
 228     }
 229 #else
 230     char *sep = NULL;
 231     *name = NULL;
 232     *value = NULL;
 233 
 234     sep = strstr(optarg, "=");
 235     if (sep == NULL) {
 236         return -pcmk_err_bad_nvpair;
 237     }
 238 
 239     *name = strndup(input, sep-input);
 240 
 241     if (*name == NULL) {
 242         return -ENOMEM;
 243     }
 244 
 245     /* If the last char in optarg is =, the user gave no
 246      * value for the option.  Leave it as NULL.
 247      */
 248     if (*(sep+1) != '\0') {
 249         *value = strdup(sep+1);
 250 
 251         if (*value == NULL) {
 252             return -ENOMEM;
 253         }
 254     }
 255 #endif
 256 
 257     if (*name != NULL && *value != NULL) {
 258         return 2;
 259     } else if (*name != NULL || *value != NULL) {
 260         return 1;
 261     } else {
 262         return -pcmk_err_bad_nvpair;
 263     }
 264 }
 265 
 266 /*!
 267  * \internal
 268  * \brief Format a name/value pair.
 269  *
 270  * Units can optionally be provided for the value.  Note that unlike most
 271  * formatting functions, this one returns the formatted string.  It is
 272  * assumed that the most common use of this function will be to build up
 273  * a string to be output as part of other functions.
 274  *
 275  * \note The caller is responsible for freeing the return value after use.
 276  *
 277  * \param[in]     name  The name of the nvpair.
 278  * \param[in]     value The value of the nvpair.
 279  * \param[in]     units Optional units for the value, or NULL.
 280  *
 281  * \return Newly allocated string with name/value pair
 282  */
 283 char *
 284 pcmk__format_nvpair(const char *name, const char *value, const char *units)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286     return crm_strdup_printf("%s=\"%s%s\"", name, value, units ? units : "");
 287 }
 288 
 289 // XML attribute handling
 290 
 291 /*!
 292  * \brief Create an XML attribute with specified name and value
 293  *
 294  * \param[in,out] node   XML node to modify
 295  * \param[in]     name   Attribute name to set
 296  * \param[in]     value  Attribute value to set
 297  *
 298  * \return New value on success, \c NULL otherwise
 299  * \note This does nothing if node, name, or value are \c NULL or empty.
 300  */
 301 const char *
 302 crm_xml_add(xmlNode *node, const char *name, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {
 304     bool dirty = FALSE;
 305     xmlAttr *attr = NULL;
 306 
 307     CRM_CHECK(node != NULL, return NULL);
 308     CRM_CHECK(name != NULL, return NULL);
 309 
 310     if (value == NULL) {
 311         return NULL;
 312     }
 313 
 314     if (pcmk__tracking_xml_changes(node, FALSE)) {
 315         const char *old = crm_element_value(node, name);
 316 
 317         if (old == NULL || value == NULL || strcmp(old, value) != 0) {
 318             dirty = TRUE;
 319         }
 320     }
 321 
 322     if (dirty && (pcmk__check_acl(node, name, pcmk__xf_acl_create) == FALSE)) {
 323         crm_trace("Cannot add %s=%s to %s", name, value, node->name);
 324         return NULL;
 325     }
 326 
 327     attr = xmlSetProp(node, (pcmkXmlStr) name, (pcmkXmlStr) value);
 328     if (dirty) {
 329         pcmk__mark_xml_attr_dirty(attr);
 330     }
 331 
 332     CRM_CHECK(attr && attr->children && attr->children->content, return NULL);
 333     return (char *)attr->children->content;
 334 }
 335 
 336 /*!
 337  * \brief Replace an XML attribute with specified name and (possibly NULL) value
 338  *
 339  * \param[in,out] node   XML node to modify
 340  * \param[in]     name   Attribute name to set
 341  * \param[in]     value  Attribute value to set
 342  *
 343  * \return New value on success, \c NULL otherwise
 344  * \note This does nothing if node or name is \c NULL or empty.
 345  */
 346 const char *
 347 crm_xml_replace(xmlNode *node, const char *name, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 348 {
 349     bool dirty = FALSE;
 350     xmlAttr *attr = NULL;
 351     const char *old_value = NULL;
 352 
 353     CRM_CHECK(node != NULL, return NULL);
 354     CRM_CHECK(name != NULL && name[0] != 0, return NULL);
 355 
 356     old_value = crm_element_value(node, name);
 357 
 358     /* Could be re-setting the same value */
 359     CRM_CHECK(old_value != value, return value);
 360 
 361     if (pcmk__check_acl(node, name, pcmk__xf_acl_write) == FALSE) {
 362         /* Create a fake object linked to doc->_private instead? */
 363         crm_trace("Cannot replace %s=%s to %s", name, value, node->name);
 364         return NULL;
 365 
 366     } else if (old_value && !value) {
 367         xml_remove_prop(node, name);
 368         return NULL;
 369     }
 370 
 371     if (pcmk__tracking_xml_changes(node, FALSE)) {
 372         if (!old_value || !value || !strcmp(old_value, value)) {
 373             dirty = TRUE;
 374         }
 375     }
 376 
 377     attr = xmlSetProp(node, (pcmkXmlStr) name, (pcmkXmlStr) value);
 378     if (dirty) {
 379         pcmk__mark_xml_attr_dirty(attr);
 380     }
 381     CRM_CHECK(attr && attr->children && attr->children->content, return NULL);
 382     return (char *) attr->children->content;
 383 }
 384 
 385 /*!
 386  * \brief Create an XML attribute with specified name and integer value
 387  *
 388  * This is like \c crm_xml_add() but taking an integer value.
 389  *
 390  * \param[in,out] node   XML node to modify
 391  * \param[in]     name   Attribute name to set
 392  * \param[in]     value  Attribute value to set
 393  *
 394  * \return New value as string on success, \c NULL otherwise
 395  * \note This does nothing if node or name are \c NULL or empty.
 396  */
 397 const char *
 398 crm_xml_add_int(xmlNode *node, const char *name, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 399 {
 400     char *number = pcmk__itoa(value);
 401     const char *added = crm_xml_add(node, name, number);
 402 
 403     free(number);
 404     return added;
 405 }
 406 
 407 /*!
 408  * \brief Create an XML attribute with specified name and unsigned value
 409  *
 410  * This is like \c crm_xml_add() but taking a guint value.
 411  *
 412  * \param[in,out] node   XML node to modify
 413  * \param[in]     name   Attribute name to set
 414  * \param[in]     ms     Attribute value to set
 415  *
 416  * \return New value as string on success, \c NULL otherwise
 417  * \note This does nothing if node or name are \c NULL or empty.
 418  */
 419 const char *
 420 crm_xml_add_ms(xmlNode *node, const char *name, guint ms)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422     char *number = crm_strdup_printf("%u", ms);
 423     const char *added = crm_xml_add(node, name, number);
 424 
 425     free(number);
 426     return added;
 427 }
 428 
 429 // Maximum size of null-terminated string representation of 64-bit integer
 430 // -9223372036854775808
 431 #define LLSTRSIZE 21
 432 
 433 /*!
 434  * \brief Create an XML attribute with specified name and long long int value
 435  *
 436  * This is like \c crm_xml_add() but taking a long long int value. It is a
 437  * useful equivalent for defined types like time_t, etc.
 438  *
 439  * \param[in,out] xml    XML node to modify
 440  * \param[in]     name   Attribute name to set
 441  * \param[in]     value  Attribute value to set
 442  *
 443  * \return New value as string on success, \c NULL otherwise
 444  * \note This does nothing if xml or name are \c NULL or empty.
 445  *       This does not support greater than 64-bit values.
 446  */
 447 const char *
 448 crm_xml_add_ll(xmlNode *xml, const char *name, long long value)
     /* [previous][next][first][last][top][bottom][index][help] */
 449 {
 450     char s[LLSTRSIZE] = { '\0', };
 451 
 452     if (snprintf(s, LLSTRSIZE, "%lld", (long long) value) == LLSTRSIZE) {
 453         return NULL;
 454     }
 455     return crm_xml_add(xml, name, s);
 456 }
 457 
 458 /*!
 459  * \brief Create XML attributes for seconds and microseconds
 460  *
 461  * This is like \c crm_xml_add() but taking a struct timeval.
 462  *
 463  * \param[in,out] xml        XML node to modify
 464  * \param[in]     name_sec   Name of XML attribute for seconds
 465  * \param[in]     name_usec  Name of XML attribute for microseconds (or NULL)
 466  * \param[in]     value      Time value to set
 467  *
 468  * \return New seconds value as string on success, \c NULL otherwise
 469  * \note This does nothing if xml, name_sec, or value is \c NULL.
 470  */
 471 const char *
 472 crm_xml_add_timeval(xmlNode *xml, const char *name_sec, const char *name_usec,
     /* [previous][next][first][last][top][bottom][index][help] */
 473                     const struct timeval *value)
 474 {
 475     const char *added = NULL;
 476 
 477     if (xml && name_sec && value) {
 478         added = crm_xml_add_ll(xml, name_sec, (long long) value->tv_sec);
 479         if (added && name_usec) {
 480             // Any error is ignored (we successfully added seconds)
 481             crm_xml_add_ll(xml, name_usec, (long long) value->tv_usec);
 482         }
 483     }
 484     return added;
 485 }
 486 
 487 /*!
 488  * \brief Retrieve the value of an XML attribute
 489  *
 490  * \param[in] data   XML node to check
 491  * \param[in] name   Attribute name to check
 492  *
 493  * \return Value of specified attribute (may be \c NULL)
 494  */
 495 const char *
 496 crm_element_value(const xmlNode *data, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 497 {
 498     xmlAttr *attr = NULL;
 499 
 500     if (data == NULL) {
 501         crm_err("Couldn't find %s in NULL", name ? name : "<null>");
 502         CRM_LOG_ASSERT(data != NULL);
 503         return NULL;
 504 
 505     } else if (name == NULL) {
 506         crm_err("Couldn't find NULL in %s", crm_element_name(data));
 507         return NULL;
 508     }
 509 
 510     /* The first argument to xmlHasProp() has always been const,
 511      * but libxml2 <2.9.2 didn't declare that, so cast it
 512      */
 513     attr = xmlHasProp((xmlNode *) data, (pcmkXmlStr) name);
 514     if (!attr || !attr->children) {
 515         return NULL;
 516     }
 517     return (const char *) attr->children->content;
 518 }
 519 
 520 /*!
 521  * \brief Retrieve the integer value of an XML attribute
 522  *
 523  * This is like \c crm_element_value() but getting the value as an integer.
 524  *
 525  * \param[in]  data  XML node to check
 526  * \param[in]  name  Attribute name to check
 527  * \param[out] dest  Where to store element value
 528  *
 529  * \return 0 on success, -1 otherwise
 530  */
 531 int
 532 crm_element_value_int(const xmlNode *data, const char *name, int *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 533 {
 534     const char *value = NULL;
 535 
 536     CRM_CHECK(dest != NULL, return -1);
 537     value = crm_element_value(data, name);
 538     if (value) {
 539         long long value_ll;
 540 
 541         if ((pcmk__scan_ll(value, &value_ll, 0LL) != pcmk_rc_ok)
 542             || (value_ll < INT_MIN) || (value_ll > INT_MAX)) {
 543             *dest = PCMK__PARSE_INT_DEFAULT;
 544         } else {
 545             *dest = (int) value_ll;
 546             return 0;
 547         }
 548     }
 549     return -1;
 550 }
 551 
 552 /*!
 553  * \brief Retrieve the long long integer value of an XML attribute
 554  *
 555  * This is like \c crm_element_value() but getting the value as a long long int.
 556  *
 557  * \param[in]  data  XML node to check
 558  * \param[in]  name  Attribute name to check
 559  * \param[out] dest  Where to store element value
 560  *
 561  * \return 0 on success, -1 otherwise
 562  */
 563 int
 564 crm_element_value_ll(const xmlNode *data, const char *name, long long *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 565 {
 566     const char *value = NULL;
 567 
 568     CRM_CHECK(dest != NULL, return -1);
 569     value = crm_element_value(data, name);
 570     if ((value != NULL)
 571         && (pcmk__scan_ll(value, dest, PCMK__PARSE_INT_DEFAULT) == pcmk_rc_ok)) {
 572         return 0;
 573     }
 574     return -1;
 575 }
 576 
 577 /*!
 578  * \brief Retrieve the millisecond value of an XML attribute
 579  *
 580  * This is like \c crm_element_value() but returning the value as a guint.
 581  *
 582  * \param[in]  data   XML node to check
 583  * \param[in]  name   Attribute name to check
 584  * \param[out] dest   Where to store attribute value
 585  *
 586  * \return \c pcmk_ok on success, -1 otherwise
 587  */
 588 int
 589 crm_element_value_ms(const xmlNode *data, const char *name, guint *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 590 {
 591     const char *value = NULL;
 592     long long value_ll;
 593 
 594     CRM_CHECK(dest != NULL, return -1);
 595     *dest = 0;
 596     value = crm_element_value(data, name);
 597     if ((pcmk__scan_ll(value, &value_ll, 0LL) != pcmk_rc_ok)
 598         || (value_ll < 0) || (value_ll > G_MAXUINT)) {
 599         return -1;
 600     }
 601     *dest = (guint) value_ll;
 602     return pcmk_ok;
 603 }
 604 
 605 /*!
 606  * \brief Retrieve the seconds-since-epoch value of an XML attribute
 607  *
 608  * This is like \c crm_element_value() but returning the value as a time_t.
 609  *
 610  * \param[in]  xml    XML node to check
 611  * \param[in]  name   Attribute name to check
 612  * \param[out] dest   Where to store attribute value
 613  *
 614  * \return \c pcmk_ok on success, -1 otherwise
 615  */
 616 int
 617 crm_element_value_epoch(const xmlNode *xml, const char *name, time_t *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 618 {
 619     long long value_ll = 0;
 620 
 621     if (crm_element_value_ll(xml, name, &value_ll) < 0) {
 622         return -1;
 623     }
 624 
 625     /* Unfortunately, we can't do any bounds checking, since time_t has neither
 626      * standardized bounds nor constants defined for them.
 627      */
 628     *dest = (time_t) value_ll;
 629     return pcmk_ok;
 630 }
 631 
 632 /*!
 633  * \brief Retrieve the value of XML second/microsecond attributes as time
 634  *
 635  * This is like \c crm_element_value() but returning value as a struct timeval.
 636  *
 637  * \param[in]  xml        XML to parse
 638  * \param[in]  name_sec   Name of XML attribute for seconds
 639  * \param[in]  name_usec  Name of XML attribute for microseconds
 640  * \param[out] dest       Where to store result
 641  *
 642  * \return \c pcmk_ok on success, -errno on error
 643  * \note Values default to 0 if XML or XML attribute does not exist
 644  */
 645 int
 646 crm_element_value_timeval(const xmlNode *xml, const char *name_sec,
     /* [previous][next][first][last][top][bottom][index][help] */
 647                           const char *name_usec, struct timeval *dest)
 648 {
 649     long long value_i = 0;
 650 
 651     CRM_CHECK(dest != NULL, return -EINVAL);
 652     dest->tv_sec = 0;
 653     dest->tv_usec = 0;
 654 
 655     if (xml == NULL) {
 656         return pcmk_ok;
 657     }
 658 
 659     /* Unfortunately, we can't do any bounds checking, since there are no
 660      * constants provided for the bounds of time_t and suseconds_t, and
 661      * calculating them isn't worth the effort. If there are XML values
 662      * beyond the native sizes, there will probably be worse problems anyway.
 663      */
 664 
 665     // Parse seconds
 666     errno = 0;
 667     if (crm_element_value_ll(xml, name_sec, &value_i) < 0) {
 668         return -errno;
 669     }
 670     dest->tv_sec = (time_t) value_i;
 671 
 672     // Parse microseconds
 673     if (crm_element_value_ll(xml, name_usec, &value_i) < 0) {
 674         return -errno;
 675     }
 676     dest->tv_usec = (suseconds_t) value_i;
 677 
 678     return pcmk_ok;
 679 }
 680 
 681 /*!
 682  * \brief Retrieve a copy of the value of an XML attribute
 683  *
 684  * This is like \c crm_element_value() but allocating new memory for the result.
 685  *
 686  * \param[in] data   XML node to check
 687  * \param[in] name   Attribute name to check
 688  *
 689  * \return Value of specified attribute (may be \c NULL)
 690  * \note The caller is responsible for freeing the result.
 691  */
 692 char *
 693 crm_element_value_copy(const xmlNode *data, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 694 {
 695     char *value_copy = NULL;
 696 
 697     pcmk__str_update(&value_copy, crm_element_value(data, name));
 698     return value_copy;
 699 }
 700 
 701 /*!
 702  * \brief Add hash table entry to XML as (possibly legacy) name/value
 703  *
 704  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 705  * and value, with an XML node passed as user data, and adds an XML attribute
 706  * with the specified name and value if it does not already exist. If the key
 707  * name starts with a digit, this will instead add a \<param name=NAME
 708  * value=VALUE/> child to the XML (for legacy compatibility with heartbeat).
 709  *
 710  * \param[in]     key        Key of hash table entry
 711  * \param[in]     value      Value of hash table entry
 712  * \param[in,out] user_data  XML node
 713  */
 714 void
 715 hash2smartfield(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 716 {
 717     const char *name = key;
 718     const char *s_value = value;
 719 
 720     xmlNode *xml_node = user_data;
 721 
 722     if (isdigit(name[0])) {
 723         xmlNode *tmp = create_xml_node(xml_node, XML_TAG_PARAM);
 724 
 725         crm_xml_add(tmp, XML_NVPAIR_ATTR_NAME, name);
 726         crm_xml_add(tmp, XML_NVPAIR_ATTR_VALUE, s_value);
 727 
 728     } else if (crm_element_value(xml_node, name) == NULL) {
 729         crm_xml_add(xml_node, name, s_value);
 730         crm_trace("dumped: %s=%s", name, s_value);
 731 
 732     } else {
 733         crm_trace("duplicate: %s=%s", name, s_value);
 734     }
 735 }
 736 
 737 /*!
 738  * \brief Set XML attribute based on hash table entry
 739  *
 740  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 741  * and value, with an XML node passed as user data, and adds an XML attribute
 742  * with the specified name and value if it does not already exist.
 743  *
 744  * \param[in]     key        Key of hash table entry
 745  * \param[in]     value      Value of hash table entry
 746  * \param[in,out] user_data  XML node
 747  */
 748 void
 749 hash2field(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 750 {
 751     const char *name = key;
 752     const char *s_value = value;
 753 
 754     xmlNode *xml_node = user_data;
 755 
 756     if (crm_element_value(xml_node, name) == NULL) {
 757         crm_xml_add(xml_node, name, s_value);
 758 
 759     } else {
 760         crm_trace("duplicate: %s=%s", name, s_value);
 761     }
 762 }
 763 
 764 /*!
 765  * \brief Set XML attribute based on hash table entry, as meta-attribute name
 766  *
 767  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 768  * and value, with an XML node passed as user data, and adds an XML attribute
 769  * with the meta-attribute version of the specified name and value if it does
 770  * not already exist and if the name does not appear to be cluster-internal.
 771  *
 772  * \param[in]     key        Key of hash table entry
 773  * \param[in]     value      Value of hash table entry
 774  * \param[in,out] user_data  XML node
 775  */
 776 void
 777 hash2metafield(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779     char *crm_name = NULL;
 780 
 781     if (key == NULL || value == NULL) {
 782         return;
 783     }
 784 
 785     /* Filter out cluster-generated attributes that contain a '#' or ':'
 786      * (like fail-count and last-failure).
 787      */
 788     for (crm_name = key; *crm_name; ++crm_name) {
 789         if ((*crm_name == '#') || (*crm_name == ':')) {
 790             return;
 791         }
 792     }
 793 
 794     crm_name = crm_meta_name(key);
 795     hash2field(crm_name, value, user_data);
 796     free(crm_name);
 797 }
 798 
 799 // nvpair handling
 800 
 801 /*!
 802  * \brief Create an XML name/value pair
 803  *
 804  * \param[in,out] parent  If not \c NULL, make new XML node a child of this one
 805  * \param[in]     id      Set this as XML ID (or NULL to auto-generate)
 806  * \param[in]     name    Name to use
 807  * \param[in]     value   Value to use
 808  *
 809  * \return New XML object on success, \c NULL otherwise
 810  */
 811 xmlNode *
 812 crm_create_nvpair_xml(xmlNode *parent, const char *id, const char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
 813                       const char *value)
 814 {
 815     xmlNode *nvp;
 816 
 817     /* id can be NULL so we auto-generate one, and name can be NULL if this
 818      * will be used to delete a name/value pair by ID, but both can't be NULL
 819      */
 820     CRM_CHECK(id || name, return NULL);
 821 
 822     nvp = create_xml_node(parent, XML_CIB_TAG_NVPAIR);
 823     CRM_CHECK(nvp, return NULL);
 824 
 825     if (id) {
 826         crm_xml_add(nvp, XML_ATTR_ID, id);
 827     } else {
 828         const char *parent_id = ID(parent);
 829 
 830         crm_xml_set_id(nvp, "%s-%s",
 831                        (parent_id? parent_id : XML_CIB_TAG_NVPAIR), name);
 832     }
 833     crm_xml_add(nvp, XML_NVPAIR_ATTR_NAME, name);
 834     crm_xml_add(nvp, XML_NVPAIR_ATTR_VALUE, value);
 835     return nvp;
 836 }
 837 
 838 /*!
 839  * \brief Add XML nvpair element based on hash table entry
 840  *
 841  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 842  * and value, with an XML node passed as the user data, and adds an \c nvpair
 843  * XML element with the specified name and value.
 844  *
 845  * \param[in]     key        Key of hash table entry
 846  * \param[in]     value      Value of hash table entry
 847  * \param[in,out] user_data  XML node
 848  */
 849 void
 850 hash2nvpair(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 851 {
 852     const char *name = key;
 853     const char *s_value = value;
 854     xmlNode *xml_node = user_data;
 855 
 856     crm_create_nvpair_xml(xml_node, name, name, s_value);
 857     crm_trace("dumped: name=%s value=%s", name, s_value);
 858 }
 859 
 860 /*!
 861  * \brief Retrieve XML attributes as a hash table
 862  *
 863  * Given an XML element, this will look for any \<attributes> element child,
 864  * creating a hash table of (newly allocated string) name/value pairs taken
 865  * first from the attributes element's NAME=VALUE XML attributes, and then
 866  * from any \<param name=NAME value=VALUE> children of attributes.
 867  *
 868  * \param[in]  XML node to parse
 869  *
 870  * \return Hash table with name/value pairs
 871  * \note It is the caller's responsibility to free the result using
 872  *       \c g_hash_table_destroy().
 873  */
 874 GHashTable *
 875 xml2list(const xmlNode *parent)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877     xmlNode *child = NULL;
 878     xmlAttrPtr pIter = NULL;
 879     xmlNode *nvpair_list = NULL;
 880     GHashTable *nvpair_hash = pcmk__strkey_table(free, free);
 881 
 882     CRM_CHECK(parent != NULL, return nvpair_hash);
 883 
 884     nvpair_list = find_xml_node(parent, XML_TAG_ATTRS, FALSE);
 885     if (nvpair_list == NULL) {
 886         crm_trace("No attributes in %s", crm_element_name(parent));
 887         crm_log_xml_trace(parent, "No attributes for resource op");
 888     }
 889 
 890     crm_log_xml_trace(nvpair_list, "Unpacking");
 891 
 892     for (pIter = pcmk__xe_first_attr(nvpair_list); pIter != NULL;
 893          pIter = pIter->next) {
 894 
 895         const char *p_name = (const char *)pIter->name;
 896         const char *p_value = pcmk__xml_attr_value(pIter);
 897 
 898         crm_trace("Added %s=%s", p_name, p_value);
 899 
 900         g_hash_table_insert(nvpair_hash, strdup(p_name), strdup(p_value));
 901     }
 902 
 903     for (child = pcmk__xml_first_child(nvpair_list); child != NULL;
 904          child = pcmk__xml_next(child)) {
 905 
 906         if (strcmp((const char *)child->name, XML_TAG_PARAM) == 0) {
 907             const char *key = crm_element_value(child, XML_NVPAIR_ATTR_NAME);
 908             const char *value = crm_element_value(child, XML_NVPAIR_ATTR_VALUE);
 909 
 910             crm_trace("Added %s=%s", key, value);
 911             if (key != NULL && value != NULL) {
 912                 g_hash_table_insert(nvpair_hash, strdup(key), strdup(value));
 913             }
 914         }
 915     }
 916 
 917     return nvpair_hash;
 918 }
 919 
 920 void
 921 pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value)
     /* [previous][next][first][last][top][bottom][index][help] */
 922 {
 923     crm_xml_add(node, name, value ? XML_BOOLEAN_TRUE : XML_BOOLEAN_FALSE);
 924 }
 925 
 926 int
 927 pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 928 {
 929     const char *xml_value = NULL;
 930     int ret, rc;
 931 
 932     if (node == NULL) {
 933         return ENODATA;
 934     } else if (name == NULL || value == NULL) {
 935         return EINVAL;
 936     }
 937 
 938     xml_value = crm_element_value(node, name);
 939 
 940     if (xml_value == NULL) {
 941         return ENODATA;
 942     }
 943 
 944     rc = crm_str_to_boolean(xml_value, &ret);
 945     if (rc == 1) {
 946         *value = ret;
 947         return pcmk_rc_ok;
 948     } else {
 949         return pcmk_rc_bad_input;
 950     }
 951 }
 952 
 953 bool
 954 pcmk__xe_attr_is_true(const xmlNode *node, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 955 {
 956     bool value = false;
 957     int rc;
 958 
 959     rc = pcmk__xe_get_bool_attr(node, name, &value);
 960     return rc == pcmk_rc_ok && value == true;
 961 }
 962 
 963 // Deprecated functions kept only for backward API compatibility
 964 // LCOV_EXCL_START
 965 
 966 #include <crm/common/util_compat.h>
 967 
 968 int
 969 pcmk_scan_nvpair(const char *input, char **name, char **value)
     /* [previous][next][first][last][top][bottom][index][help] */
 970 {
 971     return pcmk__scan_nvpair(input, name, value);
 972 }
 973 
 974 char *
 975 pcmk_format_nvpair(const char *name, const char *value,
     /* [previous][next][first][last][top][bottom][index][help] */
 976                    const char *units)
 977 {
 978     return pcmk__format_nvpair(name, value, units);
 979 }
 980 
 981 char *
 982 pcmk_format_named_time(const char *name, time_t epoch_time)
     /* [previous][next][first][last][top][bottom][index][help] */
 983 {
 984     char *now_s = pcmk__epoch2str(&epoch_time, 0);
 985     char *result = crm_strdup_printf("%s=\"%s\"", name, pcmk__s(now_s, ""));
 986 
 987     free(now_s);
 988     return result;
 989 }
 990 
 991 // LCOV_EXCL_STOP
 992 // End deprecated API

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