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. pcmk__format_named_time
  13. crm_xml_add
  14. crm_xml_replace
  15. crm_xml_add_int
  16. crm_xml_add_ms
  17. crm_xml_add_ll
  18. crm_xml_add_timeval
  19. crm_element_value
  20. crm_element_value_int
  21. crm_element_value_ll
  22. crm_element_value_ms
  23. crm_element_value_epoch
  24. crm_element_value_timeval
  25. crm_element_value_copy
  26. hash2smartfield
  27. hash2field
  28. hash2metafield
  29. crm_create_nvpair_xml
  30. hash2nvpair
  31. xml2list
  32. pcmk__xe_set_bool_attr
  33. pcmk__xe_get_bool_attr
  34. pcmk__xe_attr_is_true
  35. pcmk_scan_nvpair
  36. pcmk_format_nvpair
  37. pcmk_format_named_time

   1 /*
   2  * Copyright 2004-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 <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 /*!
 290  * \internal
 291  * \brief Format a name/time pair.
 292  *
 293  * See pcmk__format_nvpair() for more details.
 294  *
 295  * \note The caller is responsible for freeing the return value after use.
 296  *
 297  * \param[in]     name       The name for the time.
 298  * \param[in]     epoch_time The time to format.
 299  *
 300  * \return Newly allocated string with name/value pair
 301  */
 302 char *
 303 pcmk__format_named_time(const char *name, time_t epoch_time)
     /* [previous][next][first][last][top][bottom][index][help] */
 304 {
 305     const char *now_str = pcmk__epoch2str(&epoch_time);
 306 
 307     return crm_strdup_printf("%s=\"%s\"", name, now_str ? now_str : "");
 308 }
 309 
 310 // XML attribute handling
 311 
 312 /*!
 313  * \brief Create an XML attribute with specified name and value
 314  *
 315  * \param[in,out] node   XML node to modify
 316  * \param[in]     name   Attribute name to set
 317  * \param[in]     value  Attribute value to set
 318  *
 319  * \return New value on success, \c NULL otherwise
 320  * \note This does nothing if node, name, or value are \c NULL or empty.
 321  */
 322 const char *
 323 crm_xml_add(xmlNode *node, const char *name, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 324 {
 325     bool dirty = FALSE;
 326     xmlAttr *attr = NULL;
 327 
 328     CRM_CHECK(node != NULL, return NULL);
 329     CRM_CHECK(name != NULL, return NULL);
 330 
 331     if (value == NULL) {
 332         return NULL;
 333     }
 334 
 335     if (pcmk__tracking_xml_changes(node, FALSE)) {
 336         const char *old = crm_element_value(node, name);
 337 
 338         if (old == NULL || value == NULL || strcmp(old, value) != 0) {
 339             dirty = TRUE;
 340         }
 341     }
 342 
 343     if (dirty && (pcmk__check_acl(node, name, pcmk__xf_acl_create) == FALSE)) {
 344         crm_trace("Cannot add %s=%s to %s", name, value, node->name);
 345         return NULL;
 346     }
 347 
 348     attr = xmlSetProp(node, (pcmkXmlStr) name, (pcmkXmlStr) value);
 349     if (dirty) {
 350         pcmk__mark_xml_attr_dirty(attr);
 351     }
 352 
 353     CRM_CHECK(attr && attr->children && attr->children->content, return NULL);
 354     return (char *)attr->children->content;
 355 }
 356 
 357 /*!
 358  * \brief Replace an XML attribute with specified name and (possibly NULL) value
 359  *
 360  * \param[in,out] node   XML node to modify
 361  * \param[in]     name   Attribute name to set
 362  * \param[in]     value  Attribute value to set
 363  *
 364  * \return New value on success, \c NULL otherwise
 365  * \note This does nothing if node or name is \c NULL or empty.
 366  */
 367 const char *
 368 crm_xml_replace(xmlNode *node, const char *name, const char *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370     bool dirty = FALSE;
 371     xmlAttr *attr = NULL;
 372     const char *old_value = NULL;
 373 
 374     CRM_CHECK(node != NULL, return NULL);
 375     CRM_CHECK(name != NULL && name[0] != 0, return NULL);
 376 
 377     old_value = crm_element_value(node, name);
 378 
 379     /* Could be re-setting the same value */
 380     CRM_CHECK(old_value != value, return value);
 381 
 382     if (pcmk__check_acl(node, name, pcmk__xf_acl_write) == FALSE) {
 383         /* Create a fake object linked to doc->_private instead? */
 384         crm_trace("Cannot replace %s=%s to %s", name, value, node->name);
 385         return NULL;
 386 
 387     } else if (old_value && !value) {
 388         xml_remove_prop(node, name);
 389         return NULL;
 390     }
 391 
 392     if (pcmk__tracking_xml_changes(node, FALSE)) {
 393         if (!old_value || !value || !strcmp(old_value, value)) {
 394             dirty = TRUE;
 395         }
 396     }
 397 
 398     attr = xmlSetProp(node, (pcmkXmlStr) name, (pcmkXmlStr) value);
 399     if (dirty) {
 400         pcmk__mark_xml_attr_dirty(attr);
 401     }
 402     CRM_CHECK(attr && attr->children && attr->children->content, return NULL);
 403     return (char *) attr->children->content;
 404 }
 405 
 406 /*!
 407  * \brief Create an XML attribute with specified name and integer value
 408  *
 409  * This is like \c crm_xml_add() but taking an integer value.
 410  *
 411  * \param[in,out] node   XML node to modify
 412  * \param[in]     name   Attribute name to set
 413  * \param[in]     value  Attribute value to set
 414  *
 415  * \return New value as string on success, \c NULL otherwise
 416  * \note This does nothing if node or name are \c NULL or empty.
 417  */
 418 const char *
 419 crm_xml_add_int(xmlNode *node, const char *name, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 {
 421     char *number = pcmk__itoa(value);
 422     const char *added = crm_xml_add(node, name, number);
 423 
 424     free(number);
 425     return added;
 426 }
 427 
 428 /*!
 429  * \brief Create an XML attribute with specified name and unsigned value
 430  *
 431  * This is like \c crm_xml_add() but taking a guint value.
 432  *
 433  * \param[in,out] node   XML node to modify
 434  * \param[in]     name   Attribute name to set
 435  * \param[in]     ms     Attribute value to set
 436  *
 437  * \return New value as string on success, \c NULL otherwise
 438  * \note This does nothing if node or name are \c NULL or empty.
 439  */
 440 const char *
 441 crm_xml_add_ms(xmlNode *node, const char *name, guint ms)
     /* [previous][next][first][last][top][bottom][index][help] */
 442 {
 443     char *number = crm_strdup_printf("%u", ms);
 444     const char *added = crm_xml_add(node, name, number);
 445 
 446     free(number);
 447     return added;
 448 }
 449 
 450 // Maximum size of null-terminated string representation of 64-bit integer
 451 // -9223372036854775808
 452 #define LLSTRSIZE 21
 453 
 454 /*!
 455  * \brief Create an XML attribute with specified name and long long int value
 456  *
 457  * This is like \c crm_xml_add() but taking a long long int value. It is a
 458  * useful equivalent for defined types like time_t, etc.
 459  *
 460  * \param[in,out] xml    XML node to modify
 461  * \param[in]     name   Attribute name to set
 462  * \param[in]     value  Attribute value to set
 463  *
 464  * \return New value as string on success, \c NULL otherwise
 465  * \note This does nothing if xml or name are \c NULL or empty.
 466  *       This does not support greater than 64-bit values.
 467  */
 468 const char *
 469 crm_xml_add_ll(xmlNode *xml, const char *name, long long value)
     /* [previous][next][first][last][top][bottom][index][help] */
 470 {
 471     char s[LLSTRSIZE] = { '\0', };
 472 
 473     if (snprintf(s, LLSTRSIZE, "%lld", (long long) value) == LLSTRSIZE) {
 474         return NULL;
 475     }
 476     return crm_xml_add(xml, name, s);
 477 }
 478 
 479 /*!
 480  * \brief Create XML attributes for seconds and microseconds
 481  *
 482  * This is like \c crm_xml_add() but taking a struct timeval.
 483  *
 484  * \param[in,out] xml        XML node to modify
 485  * \param[in]     name_sec   Name of XML attribute for seconds
 486  * \param[in]     name_usec  Name of XML attribute for microseconds (or NULL)
 487  * \param[in]     value      Time value to set
 488  *
 489  * \return New seconds value as string on success, \c NULL otherwise
 490  * \note This does nothing if xml, name_sec, or value is \c NULL.
 491  */
 492 const char *
 493 crm_xml_add_timeval(xmlNode *xml, const char *name_sec, const char *name_usec,
     /* [previous][next][first][last][top][bottom][index][help] */
 494                     const struct timeval *value)
 495 {
 496     const char *added = NULL;
 497 
 498     if (xml && name_sec && value) {
 499         added = crm_xml_add_ll(xml, name_sec, (long long) value->tv_sec);
 500         if (added && name_usec) {
 501             // Any error is ignored (we successfully added seconds)
 502             crm_xml_add_ll(xml, name_usec, (long long) value->tv_usec);
 503         }
 504     }
 505     return added;
 506 }
 507 
 508 /*!
 509  * \brief Retrieve the value of an XML attribute
 510  *
 511  * \param[in] data   XML node to check
 512  * \param[in] name   Attribute name to check
 513  *
 514  * \return Value of specified attribute (may be \c NULL)
 515  */
 516 const char *
 517 crm_element_value(const xmlNode *data, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519     xmlAttr *attr = NULL;
 520 
 521     if (data == NULL) {
 522         crm_err("Couldn't find %s in NULL", name ? name : "<null>");
 523         CRM_LOG_ASSERT(data != NULL);
 524         return NULL;
 525 
 526     } else if (name == NULL) {
 527         crm_err("Couldn't find NULL in %s", crm_element_name(data));
 528         return NULL;
 529     }
 530 
 531     /* The first argument to xmlHasProp() has always been const,
 532      * but libxml2 <2.9.2 didn't declare that, so cast it
 533      */
 534     attr = xmlHasProp((xmlNode *) data, (pcmkXmlStr) name);
 535     if (!attr || !attr->children) {
 536         return NULL;
 537     }
 538     return (const char *) attr->children->content;
 539 }
 540 
 541 /*!
 542  * \brief Retrieve the integer value of an XML attribute
 543  *
 544  * This is like \c crm_element_value() but getting the value as an integer.
 545  *
 546  * \param[in]  data  XML node to check
 547  * \param[in]  name  Attribute name to check
 548  * \param[out] dest  Where to store element value
 549  *
 550  * \return 0 on success, -1 otherwise
 551  */
 552 int
 553 crm_element_value_int(const xmlNode *data, const char *name, int *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 554 {
 555     const char *value = NULL;
 556 
 557     CRM_CHECK(dest != NULL, return -1);
 558     value = crm_element_value(data, name);
 559     if (value) {
 560         long long value_ll;
 561 
 562         if ((pcmk__scan_ll(value, &value_ll, 0LL) != pcmk_rc_ok)
 563             || (value_ll < INT_MIN) || (value_ll > INT_MAX)) {
 564             *dest = PCMK__PARSE_INT_DEFAULT;
 565         } else {
 566             *dest = (int) value_ll;
 567             return 0;
 568         }
 569     }
 570     return -1;
 571 }
 572 
 573 /*!
 574  * \brief Retrieve the long long integer value of an XML attribute
 575  *
 576  * This is like \c crm_element_value() but getting the value as a long long int.
 577  *
 578  * \param[in]  data  XML node to check
 579  * \param[in]  name  Attribute name to check
 580  * \param[out] dest  Where to store element value
 581  *
 582  * \return 0 on success, -1 otherwise
 583  */
 584 int
 585 crm_element_value_ll(const xmlNode *data, const char *name, long long *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 586 {
 587     const char *value = NULL;
 588 
 589     CRM_CHECK(dest != NULL, return -1);
 590     value = crm_element_value(data, name);
 591     if ((value != NULL)
 592         && (pcmk__scan_ll(value, dest, PCMK__PARSE_INT_DEFAULT) == pcmk_rc_ok)) {
 593         return 0;
 594     }
 595     return -1;
 596 }
 597 
 598 /*!
 599  * \brief Retrieve the millisecond value of an XML attribute
 600  *
 601  * This is like \c crm_element_value() but returning the value as a guint.
 602  *
 603  * \param[in]  data   XML node to check
 604  * \param[in]  name   Attribute name to check
 605  * \param[out] dest   Where to store attribute value
 606  *
 607  * \return \c pcmk_ok on success, -1 otherwise
 608  */
 609 int
 610 crm_element_value_ms(const xmlNode *data, const char *name, guint *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 611 {
 612     const char *value = NULL;
 613     long long value_ll;
 614 
 615     CRM_CHECK(dest != NULL, return -1);
 616     *dest = 0;
 617     value = crm_element_value(data, name);
 618     if ((pcmk__scan_ll(value, &value_ll, 0LL) != pcmk_rc_ok)
 619         || (value_ll < 0) || (value_ll > G_MAXUINT)) {
 620         return -1;
 621     }
 622     *dest = (guint) value_ll;
 623     return pcmk_ok;
 624 }
 625 
 626 /*!
 627  * \brief Retrieve the seconds-since-epoch value of an XML attribute
 628  *
 629  * This is like \c crm_element_value() but returning the value as a time_t.
 630  *
 631  * \param[in]  xml    XML node to check
 632  * \param[in]  name   Attribute name to check
 633  * \param[out] dest   Where to store attribute value
 634  *
 635  * \return \c pcmk_ok on success, -1 otherwise
 636  */
 637 int
 638 crm_element_value_epoch(const xmlNode *xml, const char *name, time_t *dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 639 {
 640     long long value_ll = 0;
 641 
 642     if (crm_element_value_ll(xml, name, &value_ll) < 0) {
 643         return -1;
 644     }
 645 
 646     /* Unfortunately, we can't do any bounds checking, since time_t has neither
 647      * standardized bounds nor constants defined for them.
 648      */
 649     *dest = (time_t) value_ll;
 650     return pcmk_ok;
 651 }
 652 
 653 /*!
 654  * \brief Retrieve the value of XML second/microsecond attributes as time
 655  *
 656  * This is like \c crm_element_value() but returning value as a struct timeval.
 657  *
 658  * \param[in]  xml        XML to parse
 659  * \param[in]  name_sec   Name of XML attribute for seconds
 660  * \param[in]  name_usec  Name of XML attribute for microseconds
 661  * \param[out] dest       Where to store result
 662  *
 663  * \return \c pcmk_ok on success, -errno on error
 664  * \note Values default to 0 if XML or XML attribute does not exist
 665  */
 666 int
 667 crm_element_value_timeval(const xmlNode *xml, const char *name_sec,
     /* [previous][next][first][last][top][bottom][index][help] */
 668                           const char *name_usec, struct timeval *dest)
 669 {
 670     long long value_i = 0;
 671 
 672     CRM_CHECK(dest != NULL, return -EINVAL);
 673     dest->tv_sec = 0;
 674     dest->tv_usec = 0;
 675 
 676     if (xml == NULL) {
 677         return pcmk_ok;
 678     }
 679 
 680     /* Unfortunately, we can't do any bounds checking, since there are no
 681      * constants provided for the bounds of time_t and suseconds_t, and
 682      * calculating them isn't worth the effort. If there are XML values
 683      * beyond the native sizes, there will probably be worse problems anyway.
 684      */
 685 
 686     // Parse seconds
 687     errno = 0;
 688     if (crm_element_value_ll(xml, name_sec, &value_i) < 0) {
 689         return -errno;
 690     }
 691     dest->tv_sec = (time_t) value_i;
 692 
 693     // Parse microseconds
 694     if (crm_element_value_ll(xml, name_usec, &value_i) < 0) {
 695         return -errno;
 696     }
 697     dest->tv_usec = (suseconds_t) value_i;
 698 
 699     return pcmk_ok;
 700 }
 701 
 702 /*!
 703  * \brief Retrieve a copy of the value of an XML attribute
 704  *
 705  * This is like \c crm_element_value() but allocating new memory for the result.
 706  *
 707  * \param[in] data   XML node to check
 708  * \param[in] name   Attribute name to check
 709  *
 710  * \return Value of specified attribute (may be \c NULL)
 711  * \note The caller is responsible for freeing the result.
 712  */
 713 char *
 714 crm_element_value_copy(const xmlNode *data, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 715 {
 716     char *value_copy = NULL;
 717 
 718     pcmk__str_update(&value_copy, crm_element_value(data, name));
 719     return value_copy;
 720 }
 721 
 722 /*!
 723  * \brief Add hash table entry to XML as (possibly legacy) name/value
 724  *
 725  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 726  * and value, with an XML node passed as user data, and adds an XML attribute
 727  * with the specified name and value if it does not already exist. If the key
 728  * name starts with a digit, this will instead add a \<param name=NAME
 729  * value=VALUE/> child to the XML (for legacy compatibility with heartbeat).
 730  *
 731  * \param[in]     key        Key of hash table entry
 732  * \param[in]     value      Value of hash table entry
 733  * \param[in,out] user_data  XML node
 734  */
 735 void
 736 hash2smartfield(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 737 {
 738     const char *name = key;
 739     const char *s_value = value;
 740 
 741     xmlNode *xml_node = user_data;
 742 
 743     if (isdigit(name[0])) {
 744         xmlNode *tmp = create_xml_node(xml_node, XML_TAG_PARAM);
 745 
 746         crm_xml_add(tmp, XML_NVPAIR_ATTR_NAME, name);
 747         crm_xml_add(tmp, XML_NVPAIR_ATTR_VALUE, s_value);
 748 
 749     } else if (crm_element_value(xml_node, name) == NULL) {
 750         crm_xml_add(xml_node, name, s_value);
 751         crm_trace("dumped: %s=%s", name, s_value);
 752 
 753     } else {
 754         crm_trace("duplicate: %s=%s", name, s_value);
 755     }
 756 }
 757 
 758 /*!
 759  * \brief Set XML attribute based on hash table entry
 760  *
 761  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 762  * and value, with an XML node passed as user data, and adds an XML attribute
 763  * with the specified name and value if it does not already exist.
 764  *
 765  * \param[in]     key        Key of hash table entry
 766  * \param[in]     value      Value of hash table entry
 767  * \param[in,out] user_data  XML node
 768  */
 769 void
 770 hash2field(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 771 {
 772     const char *name = key;
 773     const char *s_value = value;
 774 
 775     xmlNode *xml_node = user_data;
 776 
 777     if (crm_element_value(xml_node, name) == NULL) {
 778         crm_xml_add(xml_node, name, s_value);
 779 
 780     } else {
 781         crm_trace("duplicate: %s=%s", name, s_value);
 782     }
 783 }
 784 
 785 /*!
 786  * \brief Set XML attribute based on hash table entry, as meta-attribute name
 787  *
 788  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 789  * and value, with an XML node passed as user data, and adds an XML attribute
 790  * with the meta-attribute version of the specified name and value if it does
 791  * not already exist and if the name does not appear to be cluster-internal.
 792  *
 793  * \param[in]     key        Key of hash table entry
 794  * \param[in]     value      Value of hash table entry
 795  * \param[in,out] user_data  XML node
 796  */
 797 void
 798 hash2metafield(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 799 {
 800     char *crm_name = NULL;
 801 
 802     if (key == NULL || value == NULL) {
 803         return;
 804     }
 805 
 806     /* Filter out cluster-generated attributes that contain a '#' or ':'
 807      * (like fail-count and last-failure).
 808      */
 809     for (crm_name = key; *crm_name; ++crm_name) {
 810         if ((*crm_name == '#') || (*crm_name == ':')) {
 811             return;
 812         }
 813     }
 814 
 815     crm_name = crm_meta_name(key);
 816     hash2field(crm_name, value, user_data);
 817     free(crm_name);
 818 }
 819 
 820 // nvpair handling
 821 
 822 /*!
 823  * \brief Create an XML name/value pair
 824  *
 825  * \param[in,out] parent  If not \c NULL, make new XML node a child of this one
 826  * \param[in]     id      Set this as XML ID (or NULL to auto-generate)
 827  * \param[in]     name    Name to use
 828  * \param[in]     value   Value to use
 829  *
 830  * \return New XML object on success, \c NULL otherwise
 831  */
 832 xmlNode *
 833 crm_create_nvpair_xml(xmlNode *parent, const char *id, const char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
 834                       const char *value)
 835 {
 836     xmlNode *nvp;
 837 
 838     /* id can be NULL so we auto-generate one, and name can be NULL if this
 839      * will be used to delete a name/value pair by ID, but both can't be NULL
 840      */
 841     CRM_CHECK(id || name, return NULL);
 842 
 843     nvp = create_xml_node(parent, XML_CIB_TAG_NVPAIR);
 844     CRM_CHECK(nvp, return NULL);
 845 
 846     if (id) {
 847         crm_xml_add(nvp, XML_ATTR_ID, id);
 848     } else {
 849         const char *parent_id = ID(parent);
 850 
 851         crm_xml_set_id(nvp, "%s-%s",
 852                        (parent_id? parent_id : XML_CIB_TAG_NVPAIR), name);
 853     }
 854     crm_xml_add(nvp, XML_NVPAIR_ATTR_NAME, name);
 855     crm_xml_add(nvp, XML_NVPAIR_ATTR_VALUE, value);
 856     return nvp;
 857 }
 858 
 859 /*!
 860  * \brief Add XML nvpair element based on hash table entry
 861  *
 862  * Suitable for \c g_hash_table_foreach(), this function takes a hash table key
 863  * and value, with an XML node passed as the user data, and adds an \c nvpair
 864  * XML element with the specified name and value.
 865  *
 866  * \param[in]     key        Key of hash table entry
 867  * \param[in]     value      Value of hash table entry
 868  * \param[in,out] user_data  XML node
 869  */
 870 void
 871 hash2nvpair(gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 872 {
 873     const char *name = key;
 874     const char *s_value = value;
 875     xmlNode *xml_node = user_data;
 876 
 877     crm_create_nvpair_xml(xml_node, name, name, s_value);
 878     crm_trace("dumped: name=%s value=%s", name, s_value);
 879 }
 880 
 881 /*!
 882  * \brief Retrieve XML attributes as a hash table
 883  *
 884  * Given an XML element, this will look for any \<attributes> element child,
 885  * creating a hash table of (newly allocated string) name/value pairs taken
 886  * first from the attributes element's NAME=VALUE XML attributes, and then
 887  * from any \<param name=NAME value=VALUE> children of attributes.
 888  *
 889  * \param[in]  XML node to parse
 890  *
 891  * \return Hash table with name/value pairs
 892  * \note It is the caller's responsibility to free the result using
 893  *       \c g_hash_table_destroy().
 894  */
 895 GHashTable *
 896 xml2list(const xmlNode *parent)
     /* [previous][next][first][last][top][bottom][index][help] */
 897 {
 898     xmlNode *child = NULL;
 899     xmlAttrPtr pIter = NULL;
 900     xmlNode *nvpair_list = NULL;
 901     GHashTable *nvpair_hash = pcmk__strkey_table(free, free);
 902 
 903     CRM_CHECK(parent != NULL, return nvpair_hash);
 904 
 905     nvpair_list = find_xml_node(parent, XML_TAG_ATTRS, FALSE);
 906     if (nvpair_list == NULL) {
 907         crm_trace("No attributes in %s", crm_element_name(parent));
 908         crm_log_xml_trace(parent, "No attributes for resource op");
 909     }
 910 
 911     crm_log_xml_trace(nvpair_list, "Unpacking");
 912 
 913     for (pIter = pcmk__xe_first_attr(nvpair_list); pIter != NULL;
 914          pIter = pIter->next) {
 915 
 916         const char *p_name = (const char *)pIter->name;
 917         const char *p_value = pcmk__xml_attr_value(pIter);
 918 
 919         crm_trace("Added %s=%s", p_name, p_value);
 920 
 921         g_hash_table_insert(nvpair_hash, strdup(p_name), strdup(p_value));
 922     }
 923 
 924     for (child = pcmk__xml_first_child(nvpair_list); child != NULL;
 925          child = pcmk__xml_next(child)) {
 926 
 927         if (strcmp((const char *)child->name, XML_TAG_PARAM) == 0) {
 928             const char *key = crm_element_value(child, XML_NVPAIR_ATTR_NAME);
 929             const char *value = crm_element_value(child, XML_NVPAIR_ATTR_VALUE);
 930 
 931             crm_trace("Added %s=%s", key, value);
 932             if (key != NULL && value != NULL) {
 933                 g_hash_table_insert(nvpair_hash, strdup(key), strdup(value));
 934             }
 935         }
 936     }
 937 
 938     return nvpair_hash;
 939 }
 940 
 941 void
 942 pcmk__xe_set_bool_attr(xmlNodePtr node, const char *name, bool value)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944     crm_xml_add(node, name, value ? XML_BOOLEAN_TRUE : XML_BOOLEAN_FALSE);
 945 }
 946 
 947 int
 948 pcmk__xe_get_bool_attr(const xmlNode *node, const char *name, bool *value)
     /* [previous][next][first][last][top][bottom][index][help] */
 949 {
 950     const char *xml_value = NULL;
 951     int ret, rc;
 952 
 953     if (node == NULL) {
 954         return ENODATA;
 955     } else if (name == NULL || value == NULL) {
 956         return EINVAL;
 957     }
 958 
 959     xml_value = crm_element_value(node, name);
 960 
 961     if (xml_value == NULL) {
 962         return ENODATA;
 963     }
 964 
 965     rc = crm_str_to_boolean(xml_value, &ret);
 966     if (rc == 1) {
 967         *value = ret;
 968         return pcmk_rc_ok;
 969     } else {
 970         return pcmk_rc_unknown_format;
 971     }
 972 }
 973 
 974 bool
 975 pcmk__xe_attr_is_true(const xmlNode *node, const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 976 {
 977     bool value = false;
 978     int rc;
 979 
 980     rc = pcmk__xe_get_bool_attr(node, name, &value);
 981     return rc == pcmk_rc_ok && value == true;
 982 }
 983 
 984 // Deprecated functions kept only for backward API compatibility
 985 // LCOV_EXCL_START
 986 
 987 #include <crm/common/util_compat.h>
 988 
 989 int
 990 pcmk_scan_nvpair(const char *input, char **name, char **value)
     /* [previous][next][first][last][top][bottom][index][help] */
 991 {
 992     return pcmk__scan_nvpair(input, name, value);
 993 }
 994 
 995 char *
 996 pcmk_format_nvpair(const char *name, const char *value,
     /* [previous][next][first][last][top][bottom][index][help] */
 997                    const char *units)
 998 {
 999     return pcmk__format_nvpair(name, value, units);
1000 }
1001 
1002 char *
1003 pcmk_format_named_time(const char *name, time_t epoch_time)
     /* [previous][next][first][last][top][bottom][index][help] */
1004 {
1005     return pcmk__format_named_time(name, epoch_time);
1006 }
1007 
1008 // LCOV_EXCL_STOP
1009 // End deprecated API

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