diff --git a/include/crm/common/rules_internal.h b/include/crm/common/rules_internal.h index 9144db5a64..9d4e99c5e0 100644 --- a/include/crm/common/rules_internal.h +++ b/include/crm/common/rules_internal.h @@ -1,27 +1,63 @@ /* * Copyright 2004-2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ #ifndef PCMK__CRM_COMMON_RULES_INTERNAL__H #define PCMK__CRM_COMMON_RULES_INTERNAL__H #include // regmatch_t #include // xmlNode #include // enum expression_type #include // crm_time_t +// How node attribute values may be compared in rules +enum pcmk__comparison { + pcmk__comparison_unknown, + pcmk__comparison_defined, + pcmk__comparison_undefined, + pcmk__comparison_eq, + pcmk__comparison_ne, + pcmk__comparison_lt, + pcmk__comparison_lte, + pcmk__comparison_gt, + pcmk__comparison_gte, +}; + +// How node attribute values may be parsed in rules +enum pcmk__type { + pcmk__type_unknown, + pcmk__type_string, + pcmk__type_integer, + pcmk__type_number, + pcmk__type_version, +}; + +// Where to obtain reference value for a node attribute comparison +enum pcmk__reference_source { + pcmk__source_unknown, + pcmk__source_literal, + pcmk__source_instance_attrs, + pcmk__source_meta_attrs, +}; + enum expression_type pcmk__expression_type(const xmlNode *expr); +enum pcmk__comparison pcmk__parse_comparison(const char *op); +enum pcmk__type pcmk__parse_type(const char *type, enum pcmk__comparison op, + const char *value1, const char *value2); +enum pcmk__reference_source pcmk__parse_source(const char *source); +int pcmk__cmp_by_type(const char *value1, const char *value2, + enum pcmk__type type); char *pcmk__replace_submatches(const char *string, const char *match, const regmatch_t submatches[], int nmatches); int pcmk__evaluate_date_expression(const xmlNode *date_expression, const crm_time_t *now, crm_time_t *next_change); #endif // PCMK__CRM_COMMON_RULES_INTERNAL__H diff --git a/lib/common/rules.c b/lib/common/rules.c index e53bd154e1..bf1c87bc79 100644 --- a/lib/common/rules.c +++ b/lib/common/rules.c @@ -1,707 +1,890 @@ /* * Copyright 2004-2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ #include #include // NULL, size_t #include // bool #include // isdigit() #include // regmatch_t #include // uint32_t #include // PRIu32 #include // gboolean, FALSE #include // xmlNode #include #include #include #include #include "crmcommon_private.h" /*! * \internal * \brief Get the expression type corresponding to given expression XML * * \param[in] expr Rule expression XML * * \return Expression type corresponding to \p expr */ enum expression_type pcmk__expression_type(const xmlNode *expr) { const char *name = NULL; // Expression types based on element name if (pcmk__xe_is(expr, PCMK_XE_DATE_EXPRESSION)) { return pcmk__subexpr_datetime; } else if (pcmk__xe_is(expr, PCMK_XE_RSC_EXPRESSION)) { return pcmk__subexpr_resource; } else if (pcmk__xe_is(expr, PCMK_XE_OP_EXPRESSION)) { return pcmk__subexpr_operation; } else if (pcmk__xe_is(expr, PCMK_XE_RULE)) { return pcmk__subexpr_rule; } else if (!pcmk__xe_is(expr, PCMK_XE_EXPRESSION)) { return pcmk__subexpr_unknown; } // Expression types based on node attribute name name = crm_element_value(expr, PCMK_XA_ATTRIBUTE); if (pcmk__str_any_of(name, CRM_ATTR_UNAME, CRM_ATTR_KIND, CRM_ATTR_ID, NULL)) { return pcmk__subexpr_location; } return pcmk__subexpr_attribute; } /*! * \internal * \brief Get parent XML element's ID for logging purposes * * \param[in] xml XML of a subelement * * \return ID of \p xml's parent for logging purposes (guaranteed non-NULL) */ static const char * loggable_parent_id(const xmlNode *xml) { // Default if called without parent (likely for unit testing) const char *parent_id = "implied"; if ((xml != NULL) && (xml->parent != NULL)) { parent_id = pcmk__xe_id(xml->parent); if (parent_id == NULL) { // Not possible with schema validation enabled parent_id = "without ID"; } } return parent_id; } /*! * \internal * \brief Get the moon phase corresponding to a given date/time * * \param[in] now Date/time to get moon phase for * * \return Phase of the moon corresponding to \p now, where 0 is the new moon * and 7 is the full moon * \deprecated This feature has been deprecated since 2.1.6. */ static int phase_of_the_moon(const crm_time_t *now) { /* As per the nethack rules: * - A moon period is 29.53058 days ~= 30 * - A year is 365.2422 days * - Number of days moon phase advances on first day of year compared to * preceding year is (365.2422 - 12 * 29.53058) ~= 11 * - Number of years until same phases fall on the same days of the month * is 18.6 ~= 19 * - Moon phase on first day of year (epact) ~= (11 * (year%19) + 29) % 30 * (29 as initial condition) * - Current phase in days = first day phase + days elapsed in year * - 6 moons ~= 177 days ~= 8 reported phases * 22 (+ 11/22 for rounding) */ uint32_t epact, diy, goldn; uint32_t y; crm_time_get_ordinal(now, &y, &diy); goldn = (y % 19) + 1; epact = (11 * goldn + 18) % 30; if (((epact == 25) && (goldn > 11)) || (epact == 24)) { epact++; } return (((((diy + epact) * 6) + 11) % 177) / 22) & 7; } /*! * \internal * \brief Check an integer value against a range from a date specification * * \param[in] date_spec XML of PCMK_XE_DATE_SPEC element to check * \param[in] id XML ID for logging purposes * \param[in] attr Name of XML attribute with range to check against * \param[in] value Value to compare against range * * \return Standard Pacemaker return code (specifically, pcmk_rc_before_range, * pcmk_rc_after_range, or pcmk_rc_ok to indicate that result is either * within range or undetermined) * \note We return pcmk_rc_ok for an undetermined result so we can continue * checking the next range attribute. */ static int check_range(const xmlNode *date_spec, const char *id, const char *attr, uint32_t value) { int rc = pcmk_rc_ok; const char *range = crm_element_value(date_spec, attr); long long low, high; if (range == NULL) { // Attribute not present goto bail; } if (pcmk__parse_ll_range(range, &low, &high) != pcmk_rc_ok) { // Invalid range /* @COMPAT When we can break behavioral backward compatibility, treat * the entire rule as not passing. */ pcmk__config_err("Ignoring " PCMK_XE_DATE_SPEC " %s attribute %s because '%s' is not a valid range", id, attr, range); } else if ((low != -1) && (value < low)) { rc = pcmk_rc_before_range; } else if ((high != -1) && (value > high)) { rc = pcmk_rc_after_range; } bail: crm_trace("Checked " PCMK_XE_DATE_SPEC " %s %s='%s' for %" PRIu32 ": %s", id, attr, pcmk__s(range, ""), value, pcmk_rc_str(rc)); return rc; } /*! * \internal * \brief Evaluate a date specification for a given date/time * * \param[in] date_spec XML of PCMK_XE_DATE_SPEC element to evaluate * \param[in] now Time to check * * \return Standard Pacemaker return code (specifically, EINVAL for NULL * arguments, pcmk_rc_ok if time matches specification, or * pcmk_rc_before_range, pcmk_rc_after_range, or pcmk_rc_op_unsatisfied * as appropriate to how time relates to specification) */ int pcmk__evaluate_date_spec(const xmlNode *date_spec, const crm_time_t *now) { const char *id = NULL; const char *parent_id = loggable_parent_id(date_spec); // Range attributes that can be specified for a PCMK_XE_DATE_SPEC element struct range { const char *attr; uint32_t value; } ranges[] = { { PCMK_XA_YEARS, 0U }, { PCMK_XA_MONTHS, 0U }, { PCMK_XA_MONTHDAYS, 0U }, { PCMK_XA_HOURS, 0U }, { PCMK_XA_MINUTES, 0U }, { PCMK_XA_SECONDS, 0U }, { PCMK_XA_YEARDAYS, 0U }, { PCMK_XA_WEEKYEARS, 0U }, { PCMK_XA_WEEKS, 0U }, { PCMK_XA_WEEKDAYS, 0U }, { PCMK__XA_MOON, 0U }, }; if ((date_spec == NULL) || (now == NULL)) { return EINVAL; } // Get specification ID (for logging) id = pcmk__xe_id(date_spec); if (pcmk__str_empty(id)) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * fail the specification */ pcmk__config_warn(PCMK_XE_DATE_SPEC " subelement of " PCMK_XE_DATE_EXPRESSION " %s has no " PCMK_XA_ID, parent_id); id = "without ID"; // for logging } // Year, month, day crm_time_get_gregorian(now, &(ranges[0].value), &(ranges[1].value), &(ranges[2].value)); // Hour, minute, second crm_time_get_timeofday(now, &(ranges[3].value), &(ranges[4].value), &(ranges[5].value)); // Year (redundant) and day of year crm_time_get_ordinal(now, &(ranges[0].value), &(ranges[6].value)); // Week year, week of week year, day of week crm_time_get_isoweek(now, &(ranges[7].value), &(ranges[8].value), &(ranges[9].value)); // Moon phase (deprecated) ranges[10].value = phase_of_the_moon(now); if (crm_element_value(date_spec, PCMK__XA_MOON) != NULL) { pcmk__config_warn("Support for '" PCMK__XA_MOON "' in " PCMK_XE_DATE_SPEC " elements (such as %s) is " "deprecated and will be removed in a future release " "of Pacemaker", id); } for (int i = 0; i < PCMK__NELEM(ranges); ++i) { int rc = check_range(date_spec, id, ranges[i].attr, ranges[i].value); if (rc != pcmk_rc_ok) { return rc; } } // All specified ranges passed, or none were given (also considered a pass) return pcmk_rc_ok; } #define ADD_COMPONENT(component) do { \ int sub_rc = pcmk__add_time_from_xml(*end, component, duration); \ if (sub_rc != pcmk_rc_ok) { \ /* @COMPAT return sub_rc when we can break compatibility */ \ pcmk__config_warn("Ignoring %s in " PCMK_XE_DURATION " %s " \ "because it is invalid", \ pcmk__time_component_attr(component), id); \ rc = sub_rc; \ } \ } while (0) /*! * \internal * \brief Given a duration and a start time, calculate the end time * * \param[in] duration XML of PCMK_XE_DURATION element * \param[in] start Start time * \param[out] end Where to store end time (\p *end must be NULL * initially) * * \return Standard Pacemaker return code * \note The caller is responsible for freeing \p *end using crm_time_free(). */ int pcmk__unpack_duration(const xmlNode *duration, const crm_time_t *start, crm_time_t **end) { int rc = pcmk_rc_ok; const char *id = NULL; const char *parent_id = loggable_parent_id(duration); if ((start == NULL) || (duration == NULL) || (end == NULL) || (*end != NULL)) { return EINVAL; } // Get duration ID (for logging) id = pcmk__xe_id(duration); if (pcmk__str_empty(id)) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error instead */ pcmk__config_warn(PCMK_XE_DURATION " subelement of " PCMK_XE_DATE_EXPRESSION " %s has no " PCMK_XA_ID, parent_id); id = "without ID"; } *end = pcmk_copy_time(start); ADD_COMPONENT(pcmk__time_years); ADD_COMPONENT(pcmk__time_months); ADD_COMPONENT(pcmk__time_weeks); ADD_COMPONENT(pcmk__time_days); ADD_COMPONENT(pcmk__time_hours); ADD_COMPONENT(pcmk__time_minutes); ADD_COMPONENT(pcmk__time_seconds); return rc; } /*! * \internal * \brief Evaluate a range check for a given date/time * * \param[in] date_expression XML of PCMK_XE_DATE_EXPRESSION element * \param[in] id Expression ID for logging purposes * \param[in] now Date/time to compare * \param[in,out] next_change If not NULL, set this to when the evaluation * will change, if known and earlier than the * original value * * \return Standard Pacemaker return code */ static int evaluate_in_range(const xmlNode *date_expression, const char *id, const crm_time_t *now, crm_time_t *next_change) { crm_time_t *start = NULL; crm_time_t *end = NULL; if (pcmk__xe_get_datetime(date_expression, PCMK_XA_START, &start) != pcmk_rc_ok) { /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Ignoring " PCMK_XA_START " in " PCMK_XE_DATE_EXPRESSION " %s because it is invalid", id); } if (pcmk__xe_get_datetime(date_expression, PCMK_XA_END, &end) != pcmk_rc_ok) { /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Ignoring " PCMK_XA_END " in " PCMK_XE_DATE_EXPRESSION " %s because it is invalid", id); } if ((start == NULL) && (end == NULL)) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s as not " "passing because in_range requires at least one of " PCMK_XA_START " or " PCMK_XA_END, id); return pcmk_rc_undetermined; } if (end == NULL) { xmlNode *duration = first_named_child(date_expression, PCMK_XE_DURATION); if (duration != NULL) { /* @COMPAT When we can break behavioral backward compatibility, * return the result of this if not OK */ pcmk__unpack_duration(duration, start, &end); } } if ((start != NULL) && (crm_time_compare(now, start) < 0)) { pcmk__set_time_if_earlier(next_change, start); crm_time_free(start); crm_time_free(end); return pcmk_rc_before_range; } if (end != NULL) { if (crm_time_compare(now, end) > 0) { crm_time_free(start); crm_time_free(end); return pcmk_rc_after_range; } // Evaluation doesn't change until second after end if (next_change != NULL) { crm_time_add_seconds(end, 1); pcmk__set_time_if_earlier(next_change, end); } } crm_time_free(start); crm_time_free(end); return pcmk_rc_within_range; } /*! * \internal * \brief Evaluate a greater-than check for a given date/time * * \param[in] date_expression XML of PCMK_XE_DATE_EXPRESSION element * \param[in] id Expression ID for logging purposes * \param[in] now Date/time to compare * \param[in,out] next_change If not NULL, set this to when the evaluation * will change, if known and earlier than the * original value * * \return Standard Pacemaker return code */ static int evaluate_gt(const xmlNode *date_expression, const char *id, const crm_time_t *now, crm_time_t *next_change) { crm_time_t *start = NULL; if (pcmk__xe_get_datetime(date_expression, PCMK_XA_START, &start) != pcmk_rc_ok) { /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s as not " "passing because " PCMK_XA_START " is invalid", id); return pcmk_rc_undetermined; } if (start == NULL) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s as not " "passing because " PCMK_VALUE_GT " requires " PCMK_XA_START, id); return pcmk_rc_undetermined; } if (crm_time_compare(now, start) > 0) { crm_time_free(start); return pcmk_rc_within_range; } // Evaluation doesn't change until second after start time crm_time_add_seconds(start, 1); pcmk__set_time_if_earlier(next_change, start); crm_time_free(start); return pcmk_rc_before_range; } /*! * \internal * \brief Evaluate a less-than check for a given date/time * * \param[in] date_expression XML of PCMK_XE_DATE_EXPRESSION element * \param[in] id Expression ID for logging purposes * \param[in] now Date/time to compare * \param[in,out] next_change If not NULL, set this to when the evaluation * will change, if known and earlier than the * original value * * \return Standard Pacemaker return code */ static int evaluate_lt(const xmlNode *date_expression, const char *id, const crm_time_t *now, crm_time_t *next_change) { crm_time_t *end = NULL; if (pcmk__xe_get_datetime(date_expression, PCMK_XA_END, &end) != pcmk_rc_ok) { /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s as not " "passing because " PCMK_XA_END " is invalid", id); return pcmk_rc_undetermined; } if (end == NULL) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s as not " "passing because " PCMK_VALUE_GT " requires " PCMK_XA_END, id); return pcmk_rc_undetermined; } if (crm_time_compare(now, end) < 0) { pcmk__set_time_if_earlier(next_change, end); crm_time_free(end); return pcmk_rc_within_range; } crm_time_free(end); return pcmk_rc_after_range; } /*! * \internal * \brief Evaluate a rule's date expression for a given date/time * * \param[in] date_expression XML of a PCMK_XE_DATE_EXPRESSION element * \param[in] now Time to use for evaluation * \param[in,out] next_change If not NULL, set this to when the evaluation * will change, if known and earlier than the * original value * * \return Standard Pacemaker return code (unlike most other evaluation * functions, this can return either pcmk_rc_ok or pcmk_rc_within_range * on success) */ int pcmk__evaluate_date_expression(const xmlNode *date_expression, const crm_time_t *now, crm_time_t *next_change) { const char *id = NULL; const char *op = NULL; int rc = pcmk_rc_undetermined; if ((date_expression == NULL) || (now == NULL)) { return EINVAL; } // Get expression ID (for logging) id = pcmk__xe_id(date_expression); if (pcmk__str_empty(id)) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn(PCMK_XE_DATE_EXPRESSION " element has no " PCMK_XA_ID); id = "without ID"; // for logging } op = crm_element_value(date_expression, PCMK_XA_OPERATION); if (pcmk__str_eq(op, PCMK_VALUE_IN_RANGE, pcmk__str_null_matches|pcmk__str_casei)) { rc = evaluate_in_range(date_expression, id, now, next_change); } else if (pcmk__str_eq(op, PCMK_VALUE_DATE_SPEC, pcmk__str_casei)) { xmlNode *date_spec = first_named_child(date_expression, PCMK_XE_DATE_SPEC); if (date_spec == NULL) { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s " "as not passing because " PCMK_VALUE_DATE_SPEC " operations require a " PCMK_XE_DATE_SPEC " subelement", id); } else { // @TODO set next_change appropriately rc = pcmk__evaluate_date_spec(date_spec, now); } } else if (pcmk__str_eq(op, PCMK_VALUE_GT, pcmk__str_casei)) { rc = evaluate_gt(date_expression, id, now, next_change); } else if (pcmk__str_eq(op, PCMK_VALUE_LT, pcmk__str_casei)) { rc = evaluate_lt(date_expression, id, now, next_change); } else { // Not possible with schema validation enabled /* @COMPAT When we can break behavioral backward compatibility, * return pcmk_rc_unpack_error */ pcmk__config_warn("Treating " PCMK_XE_DATE_EXPRESSION " %s as not passing because '%s' is not a valid " PCMK_XE_OPERATION, op); } crm_trace(PCMK_XE_DATE_EXPRESSION " %s (%s): %s (%d)", id, op, pcmk_rc_str(rc), rc); return rc; } /*! * \internal * \brief Go through submatches in a string, either counting how many bytes * would be needed for the expansion, or performing the expansion, * as requested * * \param[in] string String possibly containing submatch variables * \param[in] match String that matched the regular expression * \param[in] submatches Regular expression submatches (as set by regexec()) * \param[in] nmatches Number of entries in \p submatches[] * \param[out] expansion If not NULL, expand string here (must be * pre-allocated to appropriate size) * \param[out] nbytes If not NULL, set to size needed for expansion * * \return true if any expansion is needed, otherwise false */ static bool process_submatches(const char *string, const char *match, const regmatch_t submatches[], int nmatches, char *expansion, size_t *nbytes) { bool expanded = false; const char *src = string; if (nbytes != NULL) { *nbytes = 1; // Include space for terminator } while (*src != '\0') { int submatch = 0; size_t match_len = 0; if ((src[0] != '%') || !isdigit(src[1])) { /* src does not point to the first character of a %N sequence, * so expand this character as-is */ if (expansion != NULL) { *expansion++ = *src; } if (nbytes != NULL) { ++(*nbytes); } ++src; continue; } submatch = src[1] - '0'; src += 2; // Skip over %N sequence in source string expanded = true; // Expansion will be different from source // Omit sequence from expansion unless it has a non-empty match if ((nmatches <= submatch) // Not enough submatches || (submatches[submatch].rm_so < 0) // Pattern did not match || (submatches[submatch].rm_eo <= submatches[submatch].rm_so)) { // Match was empty continue; } match_len = submatches[submatch].rm_eo - submatches[submatch].rm_so; if (nbytes != NULL) { *nbytes += match_len; } if (expansion != NULL) { memcpy(expansion, match + submatches[submatch].rm_so, match_len); expansion += match_len; } } return expanded; } /*! * \internal * \brief Expand any regular expression submatches (%0-%9) in a string * * \param[in] string String possibly containing submatch variables * \param[in] match String that matched the regular expression * \param[in] submatches Regular expression submatches (as set by regexec()) * \param[in] nmatches Number of entries in \p submatches[] * * \return Newly allocated string identical to \p string with submatches * expanded on success, or NULL if no expansions were needed * \note The caller is responsible for freeing the result with free() */ char * pcmk__replace_submatches(const char *string, const char *match, const regmatch_t submatches[], int nmatches) { size_t nbytes = 0; char *result = NULL; if (pcmk__str_empty(string)) { return NULL; // Nothing to expand } // Calculate how much space will be needed for expanded string if (!process_submatches(string, match, submatches, nmatches, NULL, &nbytes)) { return NULL; // No expansions needed } // Allocate enough space for expanded string result = pcmk__assert_alloc(nbytes, sizeof(char)); // Expand submatches (void) process_submatches(string, match, submatches, nmatches, result, NULL); return result; } + +/*! + * \internal + * \brief Parse a comparison type from a string + * + * \param[in] op String with comparison type (valid values are + * \c PCMK_VALUE_DEFINED, \c PCMK_VALUE_NOT_DEFINED, + * \c PCMK_VALUE_EQ, \c PCMK_VALUE_NE, + * \c PCMK_VALUE_LT, \c PCMK_VALUE_LTE, + * \c PCMK_VALUE_GT, or \c PCMK_VALUE_GTE) + * + * \return Comparison type corresponding to \p op + */ +enum pcmk__comparison +pcmk__parse_comparison(const char *op) +{ + if (pcmk__str_eq(op, PCMK_VALUE_DEFINED, pcmk__str_casei)) { + return pcmk__comparison_defined; + + } else if (pcmk__str_eq(op, PCMK_VALUE_NOT_DEFINED, pcmk__str_casei)) { + return pcmk__comparison_undefined; + + } else if (pcmk__str_eq(op, PCMK_VALUE_EQ, pcmk__str_casei)) { + return pcmk__comparison_eq; + + } else if (pcmk__str_eq(op, PCMK_VALUE_NE, pcmk__str_casei)) { + return pcmk__comparison_ne; + + } else if (pcmk__str_eq(op, PCMK_VALUE_LT, pcmk__str_casei)) { + return pcmk__comparison_lt; + + } else if (pcmk__str_eq(op, PCMK_VALUE_LTE, pcmk__str_casei)) { + return pcmk__comparison_lte; + + } else if (pcmk__str_eq(op, PCMK_VALUE_GT, pcmk__str_casei)) { + return pcmk__comparison_gt; + + } else if (pcmk__str_eq(op, PCMK_VALUE_GTE, pcmk__str_casei)) { + return pcmk__comparison_gte; + } + + return pcmk__comparison_unknown; +} + +/*! + * \internal + * \brief Parse a value type from a string + * + * \param[in] type String with value type (valid values are NULL, + * \c PCMK_VALUE_STRING, \c PCMK_VALUE_INTEGER, + * \c PCMK_VALUE_NUMBER, and \c PCMK_VALUE_VERSION) + * \param[in] op Operation type (used only to select default) + * \param[in] value1 First value being compared (used only to select default) + * \param[in] value2 Second value being compared (used only to select default) + */ +enum pcmk__type +pcmk__parse_type(const char *type, enum pcmk__comparison op, + const char *value1, const char *value2) +{ + if (type == NULL) { + switch (op) { + case pcmk__comparison_lt: + case pcmk__comparison_lte: + case pcmk__comparison_gt: + case pcmk__comparison_gte: + if (((value1 != NULL) && (strchr(value1, '.') != NULL)) + || ((value2 != NULL) && (strchr(value2, '.') != NULL))) { + return pcmk__type_number; + } + return pcmk__type_integer; + + default: + return pcmk__type_string; + } + } + + if (pcmk__str_eq(type, PCMK_VALUE_STRING, pcmk__str_casei)) { + return pcmk__type_string; + + } else if (pcmk__str_eq(type, PCMK_VALUE_INTEGER, pcmk__str_casei)) { + return pcmk__type_integer; + + } else if (pcmk__str_eq(type, PCMK_VALUE_NUMBER, pcmk__str_casei)) { + return pcmk__type_number; + + } else if (pcmk__str_eq(type, PCMK_VALUE_VERSION, pcmk__str_casei)) { + return pcmk__type_version; + } + + return pcmk__type_unknown; +} + +/*! + * \internal + * \brief Compare two strings according to a given type + * + * \param[in] value1 String with first value to compare + * \param[in] value2 String with second value to compare + * \param[in] type How to interpret the values + * + * \return Standard comparison result (a negative integer if \p value1 is + * lesser, 0 if the values are equal, and a positive integer if + * \p value1 is greater) + */ +int +pcmk__cmp_by_type(const char *value1, const char *value2, enum pcmk__type type) +{ + // NULL compares as less than non-NULL + if (value2 == NULL) { + return (value1 == NULL)? 0 : 1; + } + if (value1 == NULL) { + return -1; + } + + switch (type) { + case pcmk__type_string: + return strcasecmp(value1, value2); + + case pcmk__type_integer: + { + long long integer1; + long long integer2; + + if ((pcmk__scan_ll(value1, &integer1, 0LL) != pcmk_rc_ok) + || (pcmk__scan_ll(value2, &integer2, 0LL) != pcmk_rc_ok)) { + crm_warn("Comparing '%s' and '%s' as strings because " + "invalid as integers", value1, value2); + return strcasecmp(value1, value2); + } + return (integer1 < integer2)? -1 : (integer1 > integer2)? 1 : 0; + } + break; + + case pcmk__type_number: + { + double num1; + double num2; + + if ((pcmk__scan_double(value1, &num1, NULL, NULL) != pcmk_rc_ok) + || (pcmk__scan_double(value2, &num2, NULL, + NULL) != pcmk_rc_ok)) { + crm_warn("Comparing '%s' and '%s' as strings because invalid as " + "numbers", value1, value2); + return strcasecmp(value1, value2); + } + return (num1 < num2)? -1 : (num1 > num2)? 1 : 0; + } + break; + + case pcmk__type_version: + return compare_version(value1, value2); + + default: // Invalid type + return 0; + } +} + +/*! + * \internal + * \brief Parse a reference value source from a string + * + * \param[in] source String indicating reference value source + * + * \return Reference value source corresponding to \p source + */ +enum pcmk__reference_source +pcmk__parse_source(const char *source) +{ + if (pcmk__str_eq(source, PCMK_VALUE_LITERAL, + pcmk__str_casei|pcmk__str_null_matches)) { + return pcmk__source_literal; + + } else if (pcmk__str_eq(source, PCMK_VALUE_PARAM, pcmk__str_casei)) { + return pcmk__source_instance_attrs; + + } else if (pcmk__str_eq(source, PCMK_VALUE_META, pcmk__str_casei)) { + return pcmk__source_meta_attrs; + + } else { + return pcmk__source_unknown; + } +} diff --git a/lib/common/strings.c b/lib/common/strings.c index 59cb2c47b1..7839549ad9 100644 --- a/lib/common/strings.c +++ b/lib/common/strings.c @@ -1,1451 +1,1413 @@ /* * Copyright 2004-2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ #include "crm/common/results.h" #include #ifndef _GNU_SOURCE # define _GNU_SOURCE #endif #include #include #include #include #include #include // DBL_MIN #include #include #include /*! * \internal * \brief Scan a long long integer from a string * * \param[in] text String to scan * \param[out] result If not NULL, where to store scanned value * \param[in] default_value Value to use if text is NULL or invalid * \param[out] end_text If not NULL, where to store pointer to first * non-integer character * * \return Standard Pacemaker return code (\c pcmk_rc_ok on success, * \c EINVAL on failed string conversion due to invalid input, * or \c EOVERFLOW on arithmetic overflow) * \note Sets \c errno on error */ static int scan_ll(const char *text, long long *result, long long default_value, char **end_text) { long long local_result = default_value; char *local_end_text = NULL; int rc = pcmk_rc_ok; errno = 0; if (text != NULL) { local_result = strtoll(text, &local_end_text, 10); if (errno == ERANGE) { rc = EOVERFLOW; crm_warn("Integer parsed from '%s' was clipped to %lld", text, local_result); } else if (errno != 0) { rc = errno; local_result = default_value; crm_warn("Could not parse integer from '%s' (using %lld instead): " "%s", text, default_value, pcmk_rc_str(rc)); } else if (local_end_text == text) { rc = EINVAL; local_result = default_value; crm_warn("Could not parse integer from '%s' (using %lld instead): " "No digits found", text, default_value); } if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) { crm_warn("Characters left over after parsing '%s': '%s'", text, local_end_text); } errno = rc; } if (end_text != NULL) { *end_text = local_end_text; } if (result != NULL) { *result = local_result; } return rc; } /*! * \internal * \brief Scan a long long integer value from a string * * \param[in] text The string to scan (may be NULL) * \param[out] result Where to store result (or NULL to ignore) * \param[in] default_value Value to use if text is NULL or invalid * * \return Standard Pacemaker return code */ int pcmk__scan_ll(const char *text, long long *result, long long default_value) { long long local_result = default_value; int rc = pcmk_rc_ok; if (text != NULL) { rc = scan_ll(text, &local_result, default_value, NULL); if (rc != pcmk_rc_ok) { local_result = default_value; } } if (result != NULL) { *result = local_result; } return rc; } /*! * \internal * \brief Scan an integer value from a string, constrained to a minimum * * \param[in] text The string to scan (may be NULL) * \param[out] result Where to store result (or NULL to ignore) * \param[in] minimum Value to use as default and minimum * * \return Standard Pacemaker return code * \note If the value is larger than the maximum integer, EOVERFLOW will be * returned and \p result will be set to the maximum integer. */ int pcmk__scan_min_int(const char *text, int *result, int minimum) { int rc; long long result_ll; rc = pcmk__scan_ll(text, &result_ll, (long long) minimum); if (result_ll < (long long) minimum) { crm_warn("Clipped '%s' to minimum acceptable value %d", text, minimum); result_ll = (long long) minimum; } else if (result_ll > INT_MAX) { crm_warn("Clipped '%s' to maximum integer %d", text, INT_MAX); result_ll = (long long) INT_MAX; rc = EOVERFLOW; } if (result != NULL) { *result = (int) result_ll; } return rc; } /*! * \internal * \brief Scan a TCP port number from a string * * \param[in] text The string to scan * \param[out] port Where to store result (or NULL to ignore) * * \return Standard Pacemaker return code * \note \p port will be -1 if \p text is NULL or invalid */ int pcmk__scan_port(const char *text, int *port) { long long port_ll; int rc = pcmk__scan_ll(text, &port_ll, -1LL); if ((text != NULL) && (rc == pcmk_rc_ok) // wasn't default or invalid && ((port_ll < 0LL) || (port_ll > 65535LL))) { crm_warn("Ignoring port specification '%s' " "not in valid range (0-65535)", text); rc = (port_ll < 0LL)? pcmk_rc_before_range : pcmk_rc_after_range; port_ll = -1LL; } if (port != NULL) { *port = (int) port_ll; } return rc; } /*! * \internal * \brief Scan a double-precision floating-point value from a string * * \param[in] text The string to parse * \param[out] result Parsed value on success, or * \c PCMK__PARSE_DBL_DEFAULT on error * \param[in] default_text Default string to parse if \p text is * \c NULL * \param[out] end_text If not \c NULL, where to store a pointer * to the position immediately after the * value * * \return Standard Pacemaker return code (\c pcmk_rc_ok on success, * \c EINVAL on failed string conversion due to invalid input, * \c EOVERFLOW on arithmetic overflow, \c pcmk_rc_underflow * on arithmetic underflow, or \c errno from \c strtod() on * other parse errors) */ int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text) { int rc = pcmk_rc_ok; char *local_end_text = NULL; CRM_ASSERT(result != NULL); *result = PCMK__PARSE_DBL_DEFAULT; text = (text != NULL) ? text : default_text; if (text == NULL) { rc = EINVAL; crm_debug("No text and no default conversion value supplied"); } else { errno = 0; *result = strtod(text, &local_end_text); if (errno == ERANGE) { /* * Overflow: strtod() returns +/- HUGE_VAL and sets errno to * ERANGE * * Underflow: strtod() returns "a value whose magnitude is * no greater than the smallest normalized * positive" double. Whether ERANGE is set is * implementation-defined. */ const char *over_under; if (QB_ABS(*result) > DBL_MIN) { rc = EOVERFLOW; over_under = "over"; } else { rc = pcmk_rc_underflow; over_under = "under"; } crm_debug("Floating-point value parsed from '%s' would %sflow " "(using %g instead)", text, over_under, *result); } else if (errno != 0) { rc = errno; // strtod() set *result = 0 on parse failure *result = PCMK__PARSE_DBL_DEFAULT; crm_debug("Could not parse floating-point value from '%s' (using " "%.1f instead): %s", text, PCMK__PARSE_DBL_DEFAULT, pcmk_rc_str(rc)); } else if (local_end_text == text) { // errno == 0, but nothing was parsed rc = EINVAL; *result = PCMK__PARSE_DBL_DEFAULT; crm_debug("Could not parse floating-point value from '%s' (using " "%.1f instead): No digits found", text, PCMK__PARSE_DBL_DEFAULT); } else if (QB_ABS(*result) <= DBL_MIN) { /* * errno == 0 and text was parsed, but value might have * underflowed. * * ERANGE might not be set for underflow. Check magnitude * of *result, but also make sure the input number is not * actually zero (0 <= DBL_MIN is not underflow). * * This check must come last. A parse failure in strtod() * also sets *result == 0, so a parse failure would match * this test condition prematurely. */ for (const char *p = text; p != local_end_text; p++) { if (strchr("0.eE", *p) == NULL) { rc = pcmk_rc_underflow; crm_debug("Floating-point value parsed from '%s' would " "underflow (using %g instead)", text, *result); break; } } } else { crm_trace("Floating-point value parsed successfully from " "'%s': %g", text, *result); } if ((end_text == NULL) && !pcmk__str_empty(local_end_text)) { crm_debug("Characters left over after parsing '%s': '%s'", text, local_end_text); } } if (end_text != NULL) { *end_text = local_end_text; } return rc; } /*! * \internal * \brief Parse a guint from a string stored in a hash table * * \param[in] table Hash table to search * \param[in] key Hash table key to use to retrieve string * \param[in] default_val What to use if key has no entry in table * \param[out] result If not NULL, where to store parsed integer * * \return Standard Pacemaker return code */ int pcmk__guint_from_hash(GHashTable *table, const char *key, guint default_val, guint *result) { const char *value; long long value_ll; int rc = pcmk_rc_ok; CRM_CHECK((table != NULL) && (key != NULL), return EINVAL); if (result != NULL) { *result = default_val; } value = g_hash_table_lookup(table, key); if (value == NULL) { return pcmk_rc_ok; } rc = pcmk__scan_ll(value, &value_ll, 0LL); if (rc != pcmk_rc_ok) { return rc; } if ((value_ll < 0) || (value_ll > G_MAXUINT)) { crm_warn("Could not parse non-negative integer from %s", value); return ERANGE; } if (result != NULL) { *result = (guint) value_ll; } return pcmk_rc_ok; } /*! * \brief Parse a time+units string and return milliseconds equivalent * * \param[in] input String with a nonnegative number and optional unit * (optionally with whitespace before and/or after the * number). If missing, the unit defaults to seconds. * * \return Milliseconds corresponding to string expression, or * \c PCMK__PARSE_INT_DEFAULT on error */ long long crm_get_msec(const char *input) { char *units = NULL; // Do not free; will point to part of input long long multiplier = 1000; long long divisor = 1; long long msec = PCMK__PARSE_INT_DEFAULT; if (input == NULL) { return PCMK__PARSE_INT_DEFAULT; } // Skip initial whitespace while (isspace(*input)) { input++; } // Reject negative and unparsable inputs scan_ll(input, &msec, -1, &units); if (msec < 0) { return PCMK__PARSE_INT_DEFAULT; } /* If the number is a decimal, scan_ll() reads only the integer part. Skip * any remaining digits or decimal characters. * * @COMPAT Well-formed and malformed decimals are both accepted inputs. For * example, "3.14 ms" and "3.1.4 ms" are treated the same as "3ms" and * parsed successfully. At a compatibility break, decide if this is still * desired. */ while (isdigit(*units) || (*units == '.')) { units++; } // Skip any additional whitespace after the number while (isspace(*units)) { units++; } /* @COMPAT Use exact comparisons. Currently, we match too liberally, and the * second strncasecmp() in each case is redundant. */ if ((*units == '\0') || (strncasecmp(units, "s", 1) == 0) || (strncasecmp(units, "sec", 3) == 0)) { multiplier = 1000; divisor = 1; } else if ((strncasecmp(units, "ms", 2) == 0) || (strncasecmp(units, "msec", 4) == 0)) { multiplier = 1; divisor = 1; } else if ((strncasecmp(units, "us", 2) == 0) || (strncasecmp(units, "usec", 4) == 0)) { multiplier = 1; divisor = 1000; } else if ((strncasecmp(units, "m", 1) == 0) || (strncasecmp(units, "min", 3) == 0)) { multiplier = 60 * 1000; divisor = 1; } else if ((strncasecmp(units, "h", 1) == 0) || (strncasecmp(units, "hr", 2) == 0)) { multiplier = 60 * 60 * 1000; divisor = 1; } else { // Invalid units return PCMK__PARSE_INT_DEFAULT; } // Apply units, capping at LLONG_MAX if (msec > (LLONG_MAX / multiplier)) { return LLONG_MAX; } return (msec * multiplier) / divisor; } /*! * \brief Parse milliseconds from a Pacemaker interval specification * * \param[in] input Pacemaker time interval specification (a bare number * of seconds; a number with a unit, optionally with * whitespace before and/or after the number; or an ISO * 8601 duration) * \param[out] result_ms Where to store milliseconds equivalent of \p input on * success (limited to the range of an unsigned integer), * or 0 if \p input is \c NULL or invalid * * \return Standard Pacemaker return code (specifically, \c pcmk_rc_ok if * \p input is valid or \c NULL, and \c EINVAL otherwise) */ int pcmk_parse_interval_spec(const char *input, guint *result_ms) { long long msec = PCMK__PARSE_INT_DEFAULT; int rc = pcmk_rc_ok; if (input == NULL) { msec = 0; goto done; } if (input[0] == 'P') { crm_time_t *period_s = crm_time_parse_duration(input); if (period_s != NULL) { msec = 1000 * crm_time_get_seconds(period_s); crm_time_free(period_s); } } else { msec = crm_get_msec(input); } if (msec == PCMK__PARSE_INT_DEFAULT) { crm_warn("Using 0 instead of invalid interval specification '%s'", input); msec = 0; rc = EINVAL; } done: if (result_ms != NULL) { *result_ms = (msec >= G_MAXUINT)? G_MAXUINT : (guint) msec; } return rc; } gboolean crm_is_true(const char *s) { gboolean ret = FALSE; return (crm_str_to_boolean(s, &ret) < 0)? FALSE : ret; } int crm_str_to_boolean(const char *s, int *ret) { if (s == NULL) { return -1; } if (pcmk__strcase_any_of(s, PCMK_VALUE_TRUE, "on", "yes", "y", "1", NULL)) { if (ret != NULL) { *ret = TRUE; } return 1; } if (pcmk__strcase_any_of(s, PCMK_VALUE_FALSE, "off", "no", "n", "0", NULL)) { if (ret != NULL) { *ret = FALSE; } return 1; } return -1; } /*! * \internal * \brief Replace any trailing newlines in a string with \0's * * \param[in,out] str String to trim * * \return \p str */ char * pcmk__trim(char *str) { int len; if (str == NULL) { return str; } for (len = strlen(str) - 1; len >= 0 && str[len] == '\n'; len--) { str[len] = '\0'; } return str; } /*! * \brief Check whether a string starts with a certain sequence * * \param[in] str String to check * \param[in] prefix Sequence to match against beginning of \p str * * \return \c true if \p str begins with match, \c false otherwise * \note This is equivalent to !strncmp(s, prefix, strlen(prefix)) * but is likely less efficient when prefix is a string literal * if the compiler optimizes away the strlen() at compile time, * and more efficient otherwise. */ bool pcmk__starts_with(const char *str, const char *prefix) { const char *s = str; const char *p = prefix; if (!s || !p) { return false; } while (*s && *p) { if (*s++ != *p++) { return false; } } return (*p == 0); } static inline bool ends_with(const char *s, const char *match, bool as_extension) { if (pcmk__str_empty(match)) { return true; } else if (s == NULL) { return false; } else { size_t slen, mlen; /* Besides as_extension, we could also check !strchr(&match[1], match[0]) but that would be inefficient. */ if (as_extension) { s = strrchr(s, match[0]); return (s == NULL)? false : !strcmp(s, match); } mlen = strlen(match); slen = strlen(s); return ((slen >= mlen) && !strcmp(s + slen - mlen, match)); } } /*! * \internal * \brief Check whether a string ends with a certain sequence * * \param[in] s String to check * \param[in] match Sequence to match against end of \p s * * \return \c true if \p s ends case-sensitively with match, \c false otherwise * \note pcmk__ends_with_ext() can be used if the first character of match * does not recur in match. */ bool pcmk__ends_with(const char *s, const char *match) { return ends_with(s, match, false); } /*! * \internal * \brief Check whether a string ends with a certain "extension" * * \param[in] s String to check * \param[in] match Extension to match against end of \p s, that is, * its first character must not occur anywhere * in the rest of that very sequence (example: file * extension where the last dot is its delimiter, * e.g., ".html"); incorrect results may be * returned otherwise. * * \return \c true if \p s ends (verbatim, i.e., case sensitively) * with "extension" designated as \p match (including empty * string), \c false otherwise * * \note Main incentive to prefer this function over \c pcmk__ends_with() * where possible is the efficiency (at the cost of added * restriction on \p match as stated; the complexity class * remains the same, though: BigO(M+N) vs. BigO(M+2N)). */ bool pcmk__ends_with_ext(const char *s, const char *match) { return ends_with(s, match, true); } /*! * \internal * \brief Create a hash of a string suitable for use with GHashTable * * \param[in] v String to hash * * \return A hash of \p v compatible with g_str_hash() before glib 2.28 * \note glib changed their hash implementation: * * https://gitlab.gnome.org/GNOME/glib/commit/354d655ba8a54b754cb5a3efb42767327775696c * * Note that the new g_str_hash is presumably a *better* hash (it's actually * a correct implementation of DJB's hash), but we need to preserve existing * behaviour, because the hash key ultimately determines the "sort" order * when iterating through GHashTables, which affects allocation of scores to * clone instances when iterating through rsc->allowed_nodes. It (somehow) * also appears to have some minor impact on the ordering of a few * pseudo_event IDs in the transition graph. */ static guint pcmk__str_hash(gconstpointer v) { const signed char *p; guint32 h = 0; for (p = v; *p != '\0'; p++) h = (h << 5) - h + *p; return h; } /*! * \internal * \brief Create a hash table with case-sensitive strings as keys * * \param[in] key_destroy_func Function to free a key * \param[in] value_destroy_func Function to free a value * * \return Newly allocated hash table * \note It is the caller's responsibility to free the result, using * g_hash_table_destroy(). */ GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func) { return g_hash_table_new_full(pcmk__str_hash, g_str_equal, key_destroy_func, value_destroy_func); } /*! * \internal * \brief Insert string copies into a hash table as key and value * * \param[in,out] table Hash table to add to * \param[in] name String to add a copy of as key * \param[in] value String to add a copy of as value * * \note This asserts on invalid arguments or memory allocation failure. */ void pcmk__insert_dup(GHashTable *table, const char *name, const char *value) { CRM_ASSERT((table != NULL) && (name != NULL)); g_hash_table_insert(table, pcmk__str_copy(name), pcmk__str_copy(value)); } /* used with hash tables where case does not matter */ static gboolean pcmk__strcase_equal(gconstpointer a, gconstpointer b) { return pcmk__str_eq((const char *)a, (const char *)b, pcmk__str_casei); } static guint pcmk__strcase_hash(gconstpointer v) { const signed char *p; guint32 h = 0; for (p = v; *p != '\0'; p++) h = (h << 5) - h + g_ascii_tolower(*p); return h; } /*! * \internal * \brief Create a hash table with case-insensitive strings as keys * * \param[in] key_destroy_func Function to free a key * \param[in] value_destroy_func Function to free a value * * \return Newly allocated hash table * \note It is the caller's responsibility to free the result, using * g_hash_table_destroy(). */ GHashTable * pcmk__strikey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func) { return g_hash_table_new_full(pcmk__strcase_hash, pcmk__strcase_equal, key_destroy_func, value_destroy_func); } static void copy_str_table_entry(gpointer key, gpointer value, gpointer user_data) { if (key && value && user_data) { pcmk__insert_dup((GHashTable *) user_data, (const char *) key, (const char *) value); } } /*! * \internal * \brief Copy a hash table that uses dynamically allocated strings * * \param[in,out] old_table Hash table to duplicate * * \return New hash table with copies of everything in \p old_table * \note This assumes the hash table uses dynamically allocated strings -- that * is, both the key and value free functions are free(). */ GHashTable * pcmk__str_table_dup(GHashTable *old_table) { GHashTable *new_table = NULL; if (old_table) { new_table = pcmk__strkey_table(free, free); g_hash_table_foreach(old_table, copy_str_table_entry, new_table); } return new_table; } /*! * \internal * \brief Add a word to a string list of words * * \param[in,out] list Pointer to current string list (may not be \p NULL) * \param[in] init_size \p list will be initialized to at least this size, * if it needs initialization (if 0, use GLib's default * initial string size) * \param[in] word String to add to \p list (\p list will be * unchanged if this is \p NULL or the empty string) * \param[in] separator String to separate words in \p list * (a space will be used if this is NULL) * * \note \p word may contain \p separator, though that would be a bad idea if * the string needs to be parsed later. */ void pcmk__add_separated_word(GString **list, size_t init_size, const char *word, const char *separator) { CRM_ASSERT(list != NULL); if (pcmk__str_empty(word)) { return; } if (*list == NULL) { if (init_size > 0) { *list = g_string_sized_new(init_size); } else { *list = g_string_new(NULL); } } if ((*list)->len == 0) { // Don't add a separator before the first word in the list separator = ""; } else if (separator == NULL) { // Default to space-separated separator = " "; } g_string_append(*list, separator); g_string_append(*list, word); } /*! * \internal * \brief Compress data * * \param[in] data Data to compress * \param[in] length Number of characters of data to compress * \param[in] max Maximum size of compressed data (or 0 to estimate) * \param[out] result Where to store newly allocated compressed result * \param[out] result_len Where to store actual compressed length of result * * \return Standard Pacemaker return code */ int pcmk__compress(const char *data, unsigned int length, unsigned int max, char **result, unsigned int *result_len) { int rc; char *compressed = NULL; char *uncompressed = strdup(data); #ifdef CLOCK_MONOTONIC struct timespec after_t; struct timespec before_t; #endif if (max == 0) { max = (length * 1.01) + 601; // Size guaranteed to hold result } #ifdef CLOCK_MONOTONIC clock_gettime(CLOCK_MONOTONIC, &before_t); #endif compressed = pcmk__assert_alloc((size_t) max, sizeof(char)); *result_len = max; rc = BZ2_bzBuffToBuffCompress(compressed, result_len, uncompressed, length, CRM_BZ2_BLOCKS, 0, CRM_BZ2_WORK); rc = pcmk__bzlib2rc(rc); free(uncompressed); if (rc != pcmk_rc_ok) { crm_err("Compression of %d bytes failed: %s " CRM_XS " rc=%d", length, pcmk_rc_str(rc), rc); free(compressed); return rc; } #ifdef CLOCK_MONOTONIC clock_gettime(CLOCK_MONOTONIC, &after_t); crm_trace("Compressed %d bytes into %d (ratio %d:1) in %.0fms", length, *result_len, length / (*result_len), (after_t.tv_sec - before_t.tv_sec) * 1000 + (after_t.tv_nsec - before_t.tv_nsec) / 1e6); #else crm_trace("Compressed %d bytes into %d (ratio %d:1)", length, *result_len, length / (*result_len)); #endif *result = compressed; return pcmk_rc_ok; } char * crm_strdup_printf(char const *format, ...) { va_list ap; int len = 0; char *string = NULL; va_start(ap, format); len = vasprintf (&string, format, ap); CRM_ASSERT(len > 0); va_end(ap); return string; } int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end) { char *remainder = NULL; int rc = pcmk_rc_ok; CRM_ASSERT(start != NULL && end != NULL); *start = PCMK__PARSE_INT_DEFAULT; *end = PCMK__PARSE_INT_DEFAULT; crm_trace("Attempting to decode: [%s]", srcstring); if (pcmk__str_eq(srcstring, "", pcmk__str_null_matches)) { return ENODATA; } else if (pcmk__str_eq(srcstring, "-", pcmk__str_none)) { return pcmk_rc_bad_input; } /* String starts with a dash, so this is either a range with * no beginning or garbage. * */ if (*srcstring == '-') { int rc = scan_ll(srcstring+1, end, PCMK__PARSE_INT_DEFAULT, &remainder); if (rc != pcmk_rc_ok || *remainder != '\0') { return pcmk_rc_bad_input; } else { return pcmk_rc_ok; } } rc = scan_ll(srcstring, start, PCMK__PARSE_INT_DEFAULT, &remainder); if (rc != pcmk_rc_ok) { return rc; } if (*remainder && *remainder == '-') { if (*(remainder+1)) { char *more_remainder = NULL; int rc = scan_ll(remainder+1, end, PCMK__PARSE_INT_DEFAULT, &more_remainder); if (rc != pcmk_rc_ok) { return rc; } else if (*more_remainder != '\0') { return pcmk_rc_bad_input; } } } else if (*remainder && *remainder != '-') { *start = PCMK__PARSE_INT_DEFAULT; return pcmk_rc_bad_input; } else { /* The input string contained only one number. Set start and end * to the same value and return pcmk_rc_ok. This gives the caller * a way to tell this condition apart from a range with no end. */ *end = *start; } return pcmk_rc_ok; } /*! * \internal * \brief Find a string in a list of strings * * \note This function takes the same flags and has the same behavior as * pcmk__str_eq(). * * \note No matter what input string or flags are provided, an empty * list will always return FALSE. * * \param[in] s String to search for * \param[in] lst List to search * \param[in] flags A bitfield of pcmk__str_flags to modify operation * * \return \c TRUE if \p s is in \p lst, or \c FALSE otherwise */ gboolean pcmk__str_in_list(const gchar *s, const GList *lst, uint32_t flags) { for (const GList *ele = lst; ele != NULL; ele = ele->next) { if (pcmk__str_eq(s, ele->data, flags)) { return TRUE; } } return FALSE; } static bool str_any_of(const char *s, va_list args, uint32_t flags) { if (s == NULL) { return pcmk_is_set(flags, pcmk__str_null_matches); } while (1) { const char *ele = va_arg(args, const char *); if (ele == NULL) { break; } else if (pcmk__str_eq(s, ele, flags)) { return true; } } return false; } /*! * \internal * \brief Is a string a member of a list of strings? * * \param[in] s String to search for in \p ... * \param[in] ... Strings to compare \p s against. The final string * must be NULL. * * \note The comparison is done case-insensitively. The function name is * meant to be reminiscent of strcasecmp. * * \return \c true if \p s is in \p ..., or \c false otherwise */ bool pcmk__strcase_any_of(const char *s, ...) { va_list ap; bool rc; va_start(ap, s); rc = str_any_of(s, ap, pcmk__str_casei); va_end(ap); return rc; } /*! * \internal * \brief Is a string a member of a list of strings? * * \param[in] s String to search for in \p ... * \param[in] ... Strings to compare \p s against. The final string * must be NULL. * * \note The comparison is done taking case into account. * * \return \c true if \p s is in \p ..., or \c false otherwise */ bool pcmk__str_any_of(const char *s, ...) { va_list ap; bool rc; va_start(ap, s); rc = str_any_of(s, ap, pcmk__str_none); va_end(ap); return rc; } -/*! - * \internal - * \brief Check whether a character is in any of a list of strings - * - * \param[in] ch Character (ASCII) to search for - * \param[in] ... Strings to search. Final argument must be - * \c NULL. - * - * \return \c true if any of \p ... contain \p ch, \c false otherwise - * \note \p ... must contain at least one argument (\c NULL). - */ -bool -pcmk__char_in_any_str(int ch, ...) -{ - bool rc = false; - va_list ap; - - /* - * Passing a char to va_start() can generate compiler warnings, - * so ch is declared as an int. - */ - va_start(ap, ch); - - while (1) { - const char *ele = va_arg(ap, const char *); - - if (ele == NULL) { - break; - } else if (strchr(ele, ch) != NULL) { - rc = true; - break; - } - } - - va_end(ap); - return rc; -} - /*! * \internal * \brief Sort strings, with numeric portions sorted numerically * * Sort two strings case-insensitively like strcasecmp(), but with any numeric * portions of the string sorted numerically. This is particularly useful for * node names (for example, "node10" will sort higher than "node9" but lower * than "remotenode9"). * * \param[in] s1 First string to compare (must not be NULL) * \param[in] s2 Second string to compare (must not be NULL) * * \retval -1 \p s1 comes before \p s2 * \retval 0 \p s1 and \p s2 are equal * \retval 1 \p s1 comes after \p s2 */ int pcmk__numeric_strcasecmp(const char *s1, const char *s2) { CRM_ASSERT((s1 != NULL) && (s2 != NULL)); while (*s1 && *s2) { if (isdigit(*s1) && isdigit(*s2)) { // If node names contain a number, sort numerically char *end1 = NULL; char *end2 = NULL; long num1 = strtol(s1, &end1, 10); long num2 = strtol(s2, &end2, 10); // allow ordering e.g. 007 > 7 size_t len1 = end1 - s1; size_t len2 = end2 - s2; if (num1 < num2) { return -1; } else if (num1 > num2) { return 1; } else if (len1 < len2) { return -1; } else if (len1 > len2) { return 1; } s1 = end1; s2 = end2; } else { // Compare non-digits case-insensitively int lower1 = tolower(*s1); int lower2 = tolower(*s2); if (lower1 < lower2) { return -1; } else if (lower1 > lower2) { return 1; } ++s1; ++s2; } } if (!*s1 && *s2) { return -1; } else if (*s1 && !*s2) { return 1; } return 0; } /*! * \internal * \brief Sort strings. * * This is your one-stop function for string comparison. By default, this * function works like \p g_strcmp0. That is, like \p strcmp but a \p NULL * string sorts before a non-NULL string. * * The \p pcmk__str_none flag produces the default behavior. Behavior can be * changed with various flags: * * - \p pcmk__str_regex - The second string is a regular expression that the * first string will be matched against. * - \p pcmk__str_casei - By default, comparisons are done taking case into * account. This flag makes comparisons case- * insensitive. This can be combined with * \p pcmk__str_regex. * - \p pcmk__str_null_matches - If one string is \p NULL and the other is not, * still return \p 0. * - \p pcmk__str_star_matches - If one string is \p "*" and the other is not, * still return \p 0. * * \param[in] s1 First string to compare * \param[in] s2 Second string to compare, or a regular expression to * match if \p pcmk__str_regex is set * \param[in] flags A bitfield of \p pcmk__str_flags to modify operation * * \retval negative \p s1 is \p NULL or comes before \p s2 * \retval 0 \p s1 and \p s2 are equal, or \p s1 is found in \p s2 if * \c pcmk__str_regex is set * \retval positive \p s2 is \p NULL or \p s1 comes after \p s2, or \p s2 * is an invalid regular expression, or \p s1 was not found * in \p s2 if \p pcmk__str_regex is set. */ int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags) { /* If this flag is set, the second string is a regex. */ if (pcmk_is_set(flags, pcmk__str_regex)) { regex_t r_patt; int reg_flags = REG_EXTENDED | REG_NOSUB; int regcomp_rc = 0; int rc = 0; if (s1 == NULL || s2 == NULL) { return 1; } if (pcmk_is_set(flags, pcmk__str_casei)) { reg_flags |= REG_ICASE; } regcomp_rc = regcomp(&r_patt, s2, reg_flags); if (regcomp_rc != 0) { rc = 1; crm_err("Bad regex '%s' for update: %s", s2, strerror(regcomp_rc)); } else { rc = regexec(&r_patt, s1, 0, NULL, 0); regfree(&r_patt); if (rc != 0) { rc = 1; } } return rc; } /* If the strings are the same pointer, return 0 immediately. */ if (s1 == s2) { return 0; } /* If this flag is set, return 0 if either (or both) of the input strings * are NULL. If neither one is NULL, we need to continue and compare * them normally. */ if (pcmk_is_set(flags, pcmk__str_null_matches)) { if (s1 == NULL || s2 == NULL) { return 0; } } /* Handle the cases where one is NULL and the str_null_matches flag is not set. * A NULL string always sorts to the beginning. */ if (s1 == NULL) { return -1; } else if (s2 == NULL) { return 1; } /* If this flag is set, return 0 if either (or both) of the input strings * are "*". If neither one is, we need to continue and compare them * normally. */ if (pcmk_is_set(flags, pcmk__str_star_matches)) { if (strcmp(s1, "*") == 0 || strcmp(s2, "*") == 0) { return 0; } } if (pcmk_is_set(flags, pcmk__str_casei)) { return strcasecmp(s1, s2); } else { return strcmp(s1, s2); } } /*! * \internal * \brief Update a dynamically allocated string with a new value * * Given a dynamically allocated string and a new value for it, if the string * is different from the new value, free the string and replace it with either a * newly allocated duplicate of the value or NULL as appropriate. * * \param[in,out] str Pointer to dynamically allocated string * \param[in] value New value to duplicate (or NULL) * * \note The caller remains responsibile for freeing \p *str. */ void pcmk__str_update(char **str, const char *value) { if ((str != NULL) && !pcmk__str_eq(*str, value, pcmk__str_none)) { free(*str); *str = pcmk__str_copy(value); } } /*! * \internal * \brief Append a list of strings to a destination \p GString * * \param[in,out] buffer Where to append the strings (must not be \p NULL) * \param[in] ... A NULL-terminated list of strings * * \note This tends to be more efficient than a single call to * \p g_string_append_printf(). */ void pcmk__g_strcat(GString *buffer, ...) { va_list ap; CRM_ASSERT(buffer != NULL); va_start(ap, buffer); while (true) { const char *ele = va_arg(ap, const char *); if (ele == NULL) { break; } g_string_append(buffer, ele); } va_end(ap); } // Deprecated functions kept only for backward API compatibility // LCOV_EXCL_START #include gboolean safe_str_neq(const char *a, const char *b) { if (a == b) { return FALSE; } else if (a == NULL || b == NULL) { return TRUE; } else if (strcasecmp(a, b) == 0) { return FALSE; } return TRUE; } gboolean crm_str_eq(const char *a, const char *b, gboolean use_case) { if (use_case) { return g_strcmp0(a, b) == 0; /* TODO - Figure out which calls, if any, really need to be case independent */ } else if (a == b) { return TRUE; } else if (a == NULL || b == NULL) { /* shouldn't be comparing NULLs */ return FALSE; } else if (strcasecmp(a, b) == 0) { return TRUE; } return FALSE; } char * crm_itoa_stack(int an_int, char *buffer, size_t len) { if (buffer != NULL) { snprintf(buffer, len, "%d", an_int); } return buffer; } guint g_str_hash_traditional(gconstpointer v) { return pcmk__str_hash(v); } gboolean crm_strcase_equal(gconstpointer a, gconstpointer b) { return pcmk__strcase_equal(a, b); } guint crm_strcase_hash(gconstpointer v) { return pcmk__strcase_hash(v); } GHashTable * crm_str_table_dup(GHashTable *old_table) { return pcmk__str_table_dup(old_table); } long long crm_parse_ll(const char *text, const char *default_text) { long long result; if (text == NULL) { text = default_text; if (text == NULL) { crm_err("No default conversion value supplied"); errno = EINVAL; return PCMK__PARSE_INT_DEFAULT; } } scan_ll(text, &result, PCMK__PARSE_INT_DEFAULT, NULL); return result; } int crm_parse_int(const char *text, const char *default_text) { long long result = crm_parse_ll(text, default_text); if (result < INT_MIN) { // If errno is ERANGE, crm_parse_ll() has already logged a message if (errno != ERANGE) { crm_err("Conversion of %s was clipped: %lld", text, result); errno = ERANGE; } return INT_MIN; } else if (result > INT_MAX) { // If errno is ERANGE, crm_parse_ll() has already logged a message if (errno != ERANGE) { crm_err("Conversion of %s was clipped: %lld", text, result); errno = ERANGE; } return INT_MAX; } return (int) result; } char * crm_strip_trailing_newline(char *str) { return pcmk__trim(str); } int pcmk_numeric_strcasecmp(const char *s1, const char *s2) { return pcmk__numeric_strcasecmp(s1, s2); } // LCOV_EXCL_STOP // End deprecated API diff --git a/lib/common/tests/rules/Makefile.am b/lib/common/tests/rules/Makefile.am index 7e8c38c391..084a0fda27 100644 --- a/lib/common/tests/rules/Makefile.am +++ b/lib/common/tests/rules/Makefile.am @@ -1,19 +1,23 @@ # # Copyright 2020-2024 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # include $(top_srcdir)/mk/tap.mk include $(top_srcdir)/mk/unittest.mk # Add "_test" to the end of all test program names to simplify .gitignore. -check_PROGRAMS = pcmk__evaluate_date_expression_test \ +check_PROGRAMS = pcmk__cmp_by_type_test \ + pcmk__evaluate_date_expression_test \ pcmk__evaluate_date_spec_test \ + pcmk__parse_comparison_test \ + pcmk__parse_source_test \ + pcmk__parse_type_test \ pcmk__replace_submatches_test \ pcmk__unpack_duration_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/rules/pcmk__cmp_by_type_test.c b/lib/common/tests/rules/pcmk__cmp_by_type_test.c new file mode 100644 index 0000000000..d4bd98454f --- /dev/null +++ b/lib/common/tests/rules/pcmk__cmp_by_type_test.c @@ -0,0 +1,101 @@ +/* + * Copyright 2024 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include + +#include // INT_MIN, INT_MAX + +#include // crm_strdup_printf() +#include +#include + +static void +null_compares_lesser(void **state) +{ + assert_int_equal(pcmk__cmp_by_type(NULL, NULL, pcmk__type_string), 0); + assert_true(pcmk__cmp_by_type("0", NULL, pcmk__type_integer) > 0); + assert_true(pcmk__cmp_by_type(NULL, "0", pcmk__type_number) < 0); +} + +static void +invalid_compares_equal(void **state) +{ + assert_int_equal(pcmk__cmp_by_type("0", "1", pcmk__type_unknown), 0); + assert_int_equal(pcmk__cmp_by_type("hi", "bye", pcmk__type_unknown), 0); + assert_int_equal(pcmk__cmp_by_type("-1.0", "2.0", pcmk__type_unknown), 0); +} + +static void +compare_string_type(void **state) +{ + assert_int_equal(pcmk__cmp_by_type("bye", "bye", pcmk__type_string), 0); + assert_int_equal(pcmk__cmp_by_type("bye", "BYE", pcmk__type_string), 0); + assert_true(pcmk__cmp_by_type("bye", "hello", pcmk__type_string) < 0); + assert_true(pcmk__cmp_by_type("bye", "HELLO", pcmk__type_string) < 0); + assert_true(pcmk__cmp_by_type("bye", "boo", pcmk__type_string) > 0); + assert_true(pcmk__cmp_by_type("bye", "Boo", pcmk__type_string) > 0); +} + +static void +compare_integer_type(void **state) +{ + char *int_min = crm_strdup_printf("%d", INT_MIN); + char *int_max = crm_strdup_printf("%d", INT_MAX); + + assert_int_equal(pcmk__cmp_by_type("0", "0", pcmk__type_integer), 0); + assert_true(pcmk__cmp_by_type("0", "1", pcmk__type_integer) < 0); + assert_true(pcmk__cmp_by_type("1", "0", pcmk__type_integer) > 0); + assert_true(pcmk__cmp_by_type("3999", "399", pcmk__type_integer) > 0); + assert_true(pcmk__cmp_by_type(int_min, int_max, pcmk__type_integer) < 0); + assert_true(pcmk__cmp_by_type(int_max, int_min, pcmk__type_integer) > 0); + free(int_min); + free(int_max); + + // Non-integers compare as strings + assert_int_equal(pcmk__cmp_by_type("0", "x", pcmk__type_integer), + pcmk__cmp_by_type("0", "x", pcmk__type_string)); + assert_int_equal(pcmk__cmp_by_type("x", "0", pcmk__type_integer), + pcmk__cmp_by_type("x", "0", pcmk__type_string)); + assert_int_equal(pcmk__cmp_by_type("x", "X", pcmk__type_integer), + pcmk__cmp_by_type("x", "X", pcmk__type_string)); +} + +static void +compare_number_type(void **state) +{ + assert_int_equal(pcmk__cmp_by_type("0", "0.0", pcmk__type_number), 0); + assert_true(pcmk__cmp_by_type("0.345", "0.5", pcmk__type_number) < 0); + assert_true(pcmk__cmp_by_type("5", "3.1", pcmk__type_number) > 0); + assert_true(pcmk__cmp_by_type("3999", "399", pcmk__type_number) > 0); + + // Non-numbers compare as strings + assert_int_equal(pcmk__cmp_by_type("0.0", "x", pcmk__type_number), + pcmk__cmp_by_type("0.0", "x", pcmk__type_string)); + assert_int_equal(pcmk__cmp_by_type("x", "0.0", pcmk__type_number), + pcmk__cmp_by_type("x", "0.0", pcmk__type_string)); + assert_int_equal(pcmk__cmp_by_type("x", "X", pcmk__type_number), + pcmk__cmp_by_type("x", "X", pcmk__type_string)); +} + +static void +compare_version_type(void **state) +{ + assert_int_equal(pcmk__cmp_by_type("1.0", "1.0", pcmk__type_version), 0); + assert_true(pcmk__cmp_by_type("1.0.0", "1.0.1", pcmk__type_version) < 0); + assert_true(pcmk__cmp_by_type("5.0", "3.1.15", pcmk__type_version) > 0); + assert_true(pcmk__cmp_by_type("3999", "399", pcmk__type_version) > 0); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(null_compares_lesser), + cmocka_unit_test(invalid_compares_equal), + cmocka_unit_test(compare_string_type), + cmocka_unit_test(compare_integer_type), + cmocka_unit_test(compare_number_type), + cmocka_unit_test(compare_version_type)) diff --git a/lib/common/tests/rules/pcmk__parse_comparison_test.c b/lib/common/tests/rules/pcmk__parse_comparison_test.c new file mode 100644 index 0000000000..2a9426657b --- /dev/null +++ b/lib/common/tests/rules/pcmk__parse_comparison_test.c @@ -0,0 +1,71 @@ +/* + * Copyright 2024 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include + +#include + +#include +#include + +static void +null_unknown(void **state) +{ + assert_int_equal(pcmk__parse_comparison(NULL), pcmk__comparison_unknown); +} + +static void +invalid(void **state) +{ + assert_int_equal(pcmk__parse_comparison("nope"), pcmk__comparison_unknown); +} + +static void +valid(void **state) +{ + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_DEFINED), + pcmk__comparison_defined); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_NOT_DEFINED), + pcmk__comparison_undefined); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_EQ), + pcmk__comparison_eq); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_NE), + pcmk__comparison_ne); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_LT), + pcmk__comparison_lt); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_LTE), + pcmk__comparison_lte); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_GT), + pcmk__comparison_gt); + + assert_int_equal(pcmk__parse_comparison(PCMK_VALUE_GTE), + pcmk__comparison_gte); +} + +static void +case_insensitive(void **state) +{ + assert_int_equal(pcmk__parse_comparison("DEFINED"), + pcmk__comparison_defined); + + assert_int_equal(pcmk__parse_comparison("Not_Defined"), + pcmk__comparison_undefined); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(null_unknown), + cmocka_unit_test(invalid), + cmocka_unit_test(valid), + cmocka_unit_test(case_insensitive)) diff --git a/lib/common/tests/rules/pcmk__parse_source_test.c b/lib/common/tests/rules/pcmk__parse_source_test.c new file mode 100644 index 0000000000..72c65bf8f5 --- /dev/null +++ b/lib/common/tests/rules/pcmk__parse_source_test.c @@ -0,0 +1,61 @@ +/* + * Copyright 2024 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include + +#include + +#include +#include + +static void +default_literal(void **state) +{ + assert_int_equal(pcmk__parse_source(NULL), pcmk__source_literal); +} + +static void +invalid(void **state) +{ + assert_int_equal(pcmk__parse_source(""), pcmk__source_unknown); + assert_int_equal(pcmk__parse_source(" "), pcmk__source_unknown); + assert_int_equal(pcmk__parse_source("params"), pcmk__source_unknown); +} + +static void +valid(void **state) +{ + assert_int_equal(pcmk__parse_source(PCMK_VALUE_LITERAL), + pcmk__source_literal); + + assert_int_equal(pcmk__parse_source(PCMK_VALUE_PARAM), + pcmk__source_instance_attrs); + + assert_int_equal(pcmk__parse_source(PCMK_VALUE_META), + pcmk__source_meta_attrs); +} + +static void +case_insensitive(void **state) +{ + assert_int_equal(pcmk__parse_source("LITERAL"), + pcmk__source_literal); + + assert_int_equal(pcmk__parse_source("Param"), + pcmk__source_instance_attrs); + + assert_int_equal(pcmk__parse_source("MeTa"), + pcmk__source_meta_attrs); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(default_literal), + cmocka_unit_test(invalid), + cmocka_unit_test(valid), + cmocka_unit_test(case_insensitive)) diff --git a/lib/common/tests/rules/pcmk__parse_type_test.c b/lib/common/tests/rules/pcmk__parse_type_test.c new file mode 100644 index 0000000000..f33ab214fa --- /dev/null +++ b/lib/common/tests/rules/pcmk__parse_type_test.c @@ -0,0 +1,126 @@ +/* + * Copyright 2024 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + */ + +#include + +#include + +#include +#include + +static void +invalid(void **state) +{ + assert_int_equal(pcmk__parse_type("nope", pcmk__comparison_unknown, + NULL, NULL), + pcmk__type_unknown); +} + +static void +valid(void **state) +{ + assert_int_equal(pcmk__parse_type(PCMK_VALUE_STRING, + pcmk__comparison_unknown, NULL, NULL), + pcmk__type_string); + + assert_int_equal(pcmk__parse_type(PCMK_VALUE_INTEGER, + pcmk__comparison_unknown, NULL, NULL), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(PCMK_VALUE_NUMBER, + pcmk__comparison_unknown, NULL, NULL), + pcmk__type_number); + + assert_int_equal(pcmk__parse_type(PCMK_VALUE_VERSION, + pcmk__comparison_unknown, NULL, NULL), + pcmk__type_version); +} + +static void +case_insensitive(void **state) +{ + assert_int_equal(pcmk__parse_type("STRING", pcmk__comparison_unknown, + NULL, NULL), + pcmk__type_string); + + assert_int_equal(pcmk__parse_type("Integer", pcmk__comparison_unknown, + NULL, NULL), + pcmk__type_integer); +} + +static void +default_number(void **state) +{ + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_lt, "1.0", "2.5"), + pcmk__type_number); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_lte, "1.", "2"), + pcmk__type_number); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gt, "1", ".5"), + pcmk__type_number); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gte, "1.0", "2"), + pcmk__type_number); +} + +static void +default_integer(void **state) +{ + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_lt, "1", "2"), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_lte, "1", "2"), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gt, "1", "2"), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gte, "1", "2"), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gte, NULL, NULL), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gte, "1", NULL), + pcmk__type_integer); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_gte, NULL, "2.5"), + pcmk__type_number); +} + +static void +default_string(void **state) +{ + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_unknown, + NULL, NULL), + pcmk__type_string); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_defined, + NULL, NULL), + pcmk__type_string); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_undefined, + NULL, NULL), + pcmk__type_string); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_eq, NULL, NULL), + pcmk__type_string); + + assert_int_equal(pcmk__parse_type(NULL, pcmk__comparison_ne, NULL, NULL), + pcmk__type_string); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(invalid), + cmocka_unit_test(valid), + cmocka_unit_test(case_insensitive), + cmocka_unit_test(default_number), + cmocka_unit_test(default_integer), + cmocka_unit_test(default_string)) diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index e66af0d35f..439b6bf392 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,41 +1,40 @@ # -# Copyright 2020-2023 the Pacemaker project contributors +# Copyright 2020-2024 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # include $(top_srcdir)/mk/tap.mk include $(top_srcdir)/mk/unittest.mk # Add "_test" to the end of all test program names to simplify .gitignore. check_PROGRAMS = crm_get_msec_test \ crm_is_true_test \ crm_str_to_boolean_test \ pcmk__add_word_test \ pcmk__btoa_test \ - pcmk__char_in_any_str_test \ pcmk__compress_test \ pcmk__ends_with_test \ pcmk__g_strcat_test \ pcmk__guint_from_hash_test \ pcmk__numeric_strcasecmp_test \ pcmk__parse_ll_range_test \ pcmk__s_test \ pcmk__scan_double_test \ pcmk__scan_ll_test \ pcmk__scan_min_int_test \ pcmk__scan_port_test \ pcmk__starts_with_test \ pcmk__str_any_of_test \ pcmk__str_in_list_test \ pcmk__str_table_dup_test \ pcmk__str_update_test \ pcmk__strcmp_test \ pcmk__strkey_table_test \ pcmk__strikey_table_test \ pcmk__trim_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/strings/pcmk__char_in_any_str_test.c b/lib/common/tests/strings/pcmk__char_in_any_str_test.c deleted file mode 100644 index e70dfb4e11..0000000000 --- a/lib/common/tests/strings/pcmk__char_in_any_str_test.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2020-2021 the Pacemaker project contributors - * - * The version control history for this file may have further details. - * - * This source code is licensed under the GNU General Public License version 2 - * or later (GPLv2+) WITHOUT ANY WARRANTY. - */ - -#include - -#include - -static void -empty_list(void **state) -{ - assert_false(pcmk__char_in_any_str('x', NULL)); - assert_false(pcmk__char_in_any_str('\0', NULL)); -} - -static void -null_char(void **state) -{ - assert_true(pcmk__char_in_any_str('\0', "xxx", "yyy", NULL)); - assert_true(pcmk__char_in_any_str('\0', "", NULL)); -} - -static void -in_list(void **state) -{ - assert_true(pcmk__char_in_any_str('x', "aaa", "bbb", "xxx", NULL)); -} - -static void -not_in_list(void **state) -{ - assert_false(pcmk__char_in_any_str('x', "aaa", "bbb", NULL)); - assert_false(pcmk__char_in_any_str('A', "aaa", "bbb", NULL)); - assert_false(pcmk__char_in_any_str('x', "", NULL)); -} - -PCMK__UNIT_TEST(NULL, NULL, - cmocka_unit_test(empty_list), - cmocka_unit_test(null_char), - cmocka_unit_test(in_list), - cmocka_unit_test(not_in_list)) diff --git a/lib/pengine/rules.c b/lib/pengine/rules.c index 0db8ab78db..1e4360ec04 100644 --- a/lib/pengine/rules.c +++ b/lib/pengine/rules.c @@ -1,934 +1,883 @@ /* * Copyright 2004-2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ #include #include #include #include #include #include #include #include #include #include #include #include #include CRM_TRACE_INIT_DATA(pe_rules); /*! * \brief Evaluate any rules contained by given XML element * * \param[in,out] xml XML element to check for rules * \param[in] node_hash Node attributes to use to evaluate expressions * \param[in] now Time to use when evaluating expressions * \param[out] next_change If not NULL, set to when evaluation will change * * \return TRUE if no rules, or any of rules present is in effect, else FALSE */ gboolean pe_evaluate_rules(xmlNode *ruleset, GHashTable *node_hash, crm_time_t *now, crm_time_t *next_change) { pe_rule_eval_data_t rule_data = { .node_hash = node_hash, .now = now, .match_data = NULL, .rsc_data = NULL, .op_data = NULL }; return pe_eval_rules(ruleset, &rule_data, next_change); } gboolean pe_test_rule(xmlNode *rule, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now, crm_time_t *next_change, pe_match_data_t *match_data) { pe_rule_eval_data_t rule_data = { .node_hash = node_hash, .now = now, .match_data = match_data, .rsc_data = NULL, .op_data = NULL }; return pe_eval_expr(rule, &rule_data, next_change); } /*! * \brief Evaluate one rule subelement (pass/fail) * * A rule element may contain another rule, a node attribute expression, or a * date expression. Given any one of those, evaluate it and return whether it * passed. * * \param[in,out] expr Rule subelement XML * \param[in] node_hash Node attributes to use when evaluating expression * \param[in] role Ignored (deprecated) * \param[in] now Time to use when evaluating expression * \param[out] next_change If not NULL, set to when evaluation will change * \param[in] match_data If not NULL, resource back-references and params * * \return TRUE if expression is in effect under given conditions, else FALSE */ gboolean pe_test_expression(xmlNode *expr, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now, crm_time_t *next_change, pe_match_data_t *match_data) { pe_rule_eval_data_t rule_data = { .node_hash = node_hash, .now = now, .match_data = match_data, .rsc_data = NULL, .op_data = NULL }; return pe_eval_subexpr(expr, &rule_data, next_change); } // Information about a block of nvpair elements typedef struct sorted_set_s { int score; // This block's score for sorting const char *name; // This block's ID const char *special_name; // ID that should sort first xmlNode *attr_set; // This block gboolean overwrite; // Whether existing values will be overwritten } sorted_set_t; static gint sort_pairs(gconstpointer a, gconstpointer b) { const sorted_set_t *pair_a = a; const sorted_set_t *pair_b = b; if (a == NULL && b == NULL) { return 0; } else if (a == NULL) { return 1; } else if (b == NULL) { return -1; } if (pcmk__str_eq(pair_a->name, pair_a->special_name, pcmk__str_casei)) { return -1; } else if (pcmk__str_eq(pair_b->name, pair_a->special_name, pcmk__str_casei)) { return 1; } /* If we're overwriting values, we want lowest score first, so the highest * score is processed last; if we're not overwriting values, we want highest * score first, so nothing else overwrites it. */ if (pair_a->score < pair_b->score) { return pair_a->overwrite? -1 : 1; } else if (pair_a->score > pair_b->score) { return pair_a->overwrite? 1 : -1; } return 0; } static void populate_hash(xmlNode * nvpair_list, GHashTable * hash, gboolean overwrite, xmlNode * top) { const char *name = NULL; const char *value = NULL; const char *old_value = NULL; xmlNode *list = nvpair_list; xmlNode *an_attr = NULL; if (pcmk__xe_is(list->children, PCMK__XE_ATTRIBUTES)) { list = list->children; } for (an_attr = pcmk__xe_first_child(list); an_attr != NULL; an_attr = pcmk__xe_next(an_attr)) { if (pcmk__xe_is(an_attr, PCMK_XE_NVPAIR)) { xmlNode *ref_nvpair = expand_idref(an_attr, top); name = crm_element_value(an_attr, PCMK_XA_NAME); if ((name == NULL) && (ref_nvpair != NULL)) { name = crm_element_value(ref_nvpair, PCMK_XA_NAME); } value = crm_element_value(an_attr, PCMK_XA_VALUE); if ((value == NULL) && (ref_nvpair != NULL)) { value = crm_element_value(ref_nvpair, PCMK_XA_VALUE); } if (name == NULL || value == NULL) { continue; } old_value = g_hash_table_lookup(hash, name); if (pcmk__str_eq(value, "#default", pcmk__str_casei)) { if (old_value) { crm_trace("Letting %s default (removing explicit value \"%s\")", name, value); g_hash_table_remove(hash, name); } continue; } else if (old_value == NULL) { crm_trace("Setting %s=\"%s\"", name, value); pcmk__insert_dup(hash, name, value); } else if (overwrite) { crm_trace("Setting %s=\"%s\" (overwriting old value \"%s\")", name, value, old_value); pcmk__insert_dup(hash, name, value); } } } } typedef struct unpack_data_s { gboolean overwrite; void *hash; crm_time_t *next_change; const pe_rule_eval_data_t *rule_data; xmlNode *top; } unpack_data_t; static void unpack_attr_set(gpointer data, gpointer user_data) { sorted_set_t *pair = data; unpack_data_t *unpack_data = user_data; if (!pe_eval_rules(pair->attr_set, unpack_data->rule_data, unpack_data->next_change)) { return; } crm_trace("Adding attributes from %s (score %d) %s overwrite", pair->name, pair->score, (unpack_data->overwrite? "with" : "without")); populate_hash(pair->attr_set, unpack_data->hash, unpack_data->overwrite, unpack_data->top); } /*! * \internal * \brief Create a sorted list of nvpair blocks * * \param[in,out] top XML document root (used to expand id-ref's) * \param[in] xml_obj XML element containing blocks of nvpair elements * \param[in] set_name If not NULL, only get blocks of this element * \param[in] always_first If not NULL, sort block with this ID as first * * \return List of sorted_set_t entries for nvpair blocks */ static GList * make_pairs(xmlNode *top, const xmlNode *xml_obj, const char *set_name, const char *always_first, gboolean overwrite) { GList *unsorted = NULL; if (xml_obj == NULL) { return NULL; } for (xmlNode *attr_set = pcmk__xe_first_child(xml_obj); attr_set != NULL; attr_set = pcmk__xe_next(attr_set)) { if ((set_name == NULL) || pcmk__xe_is(attr_set, set_name)) { const char *score = NULL; sorted_set_t *pair = NULL; xmlNode *expanded_attr_set = expand_idref(attr_set, top); if (expanded_attr_set == NULL) { continue; // Not possible with schema validation enabled } pair = pcmk__assert_alloc(1, sizeof(sorted_set_t)); pair->name = pcmk__xe_id(expanded_attr_set); pair->special_name = always_first; pair->attr_set = expanded_attr_set; pair->overwrite = overwrite; score = crm_element_value(expanded_attr_set, PCMK_XA_SCORE); pair->score = char2score(score); unsorted = g_list_prepend(unsorted, pair); } } return g_list_sort(unsorted, sort_pairs); } /*! * \brief Extract nvpair blocks contained by an XML element into a hash table * * \param[in,out] top XML document root (used to expand id-ref's) * \param[in] xml_obj XML element containing blocks of nvpair elements * \param[in] set_name If not NULL, only use blocks of this element * \param[in] rule_data Matching parameters to use when unpacking * \param[out] hash Where to store extracted name/value pairs * \param[in] always_first If not NULL, process block with this ID first * \param[in] overwrite Whether to replace existing values with same name * \param[out] next_change If not NULL, set to when evaluation will change */ void pe_eval_nvpairs(xmlNode *top, const xmlNode *xml_obj, const char *set_name, const pe_rule_eval_data_t *rule_data, GHashTable *hash, const char *always_first, gboolean overwrite, crm_time_t *next_change) { GList *pairs = make_pairs(top, xml_obj, set_name, always_first, overwrite); if (pairs) { unpack_data_t data = { .hash = hash, .overwrite = overwrite, .next_change = next_change, .top = top, .rule_data = rule_data }; g_list_foreach(pairs, unpack_attr_set, &data); g_list_free_full(pairs, free); } } /*! * \brief Extract nvpair blocks contained by an XML element into a hash table * * \param[in,out] top XML document root (used to expand id-ref's) * \param[in] xml_obj XML element containing blocks of nvpair elements * \param[in] set_name Element name to identify nvpair blocks * \param[in] node_hash Node attributes to use when evaluating rules * \param[out] hash Where to store extracted name/value pairs * \param[in] always_first If not NULL, process block with this ID first * \param[in] overwrite Whether to replace existing values with same name * \param[in] now Time to use when evaluating rules * \param[out] next_change If not NULL, set to when evaluation will change */ void pe_unpack_nvpairs(xmlNode *top, const xmlNode *xml_obj, const char *set_name, GHashTable *node_hash, GHashTable *hash, const char *always_first, gboolean overwrite, crm_time_t *now, crm_time_t *next_change) { pe_rule_eval_data_t rule_data = { .node_hash = node_hash, .now = now, .match_data = NULL, .rsc_data = NULL, .op_data = NULL }; pe_eval_nvpairs(top, xml_obj, set_name, &rule_data, hash, always_first, overwrite, next_change); } /*! * \brief Evaluate rules * * \param[in,out] ruleset XML possibly containing rule sub-elements * \param[in] rule_data * \param[out] next_change If not NULL, set to when evaluation will change * * \return TRUE if there are no rules or */ gboolean pe_eval_rules(xmlNode *ruleset, const pe_rule_eval_data_t *rule_data, crm_time_t *next_change) { // If there are no rules, pass by default gboolean ruleset_default = TRUE; for (xmlNode *rule = first_named_child(ruleset, PCMK_XE_RULE); rule != NULL; rule = crm_next_same_xml(rule)) { ruleset_default = FALSE; if (pe_eval_expr(rule, rule_data, next_change)) { /* Only the deprecated PCMK__XE_LIFETIME element of location * constraints may contain more than one rule at the top level -- * the schema limits a block of nvpairs to a single top-level rule. * So, this effectively means that a lifetime is active if any rule * it contains is active. */ return TRUE; } } return ruleset_default; } /*! * \brief Evaluate all of a rule's expressions * * \param[in,out] rule XML containing a rule definition or its id-ref * \param[in] rule_data Matching parameters to check against rule * \param[out] next_change If not NULL, set to when evaluation will change * * \return TRUE if \p rule_data passes \p rule, otherwise FALSE */ gboolean pe_eval_expr(xmlNode *rule, const pe_rule_eval_data_t *rule_data, crm_time_t *next_change) { xmlNode *expr = NULL; gboolean test = TRUE; gboolean empty = TRUE; gboolean passed = TRUE; gboolean do_and = TRUE; const char *value = NULL; rule = expand_idref(rule, NULL); if (rule == NULL) { return FALSE; // Not possible with schema validation enabled } value = crm_element_value(rule, PCMK_XA_BOOLEAN_OP); if (pcmk__str_eq(value, PCMK_VALUE_OR, pcmk__str_casei)) { do_and = FALSE; passed = FALSE; } else if (!pcmk__str_eq(value, PCMK_VALUE_AND, pcmk__str_null_matches|pcmk__str_casei)) { pcmk__config_warn("Rule %s has invalid " PCMK_XA_BOOLEAN_OP " value '%s', using default ('" PCMK_VALUE_AND "')", pcmk__xe_id(rule), value); } crm_trace("Testing rule %s", pcmk__xe_id(rule)); for (expr = pcmk__xe_first_child(rule); expr != NULL; expr = pcmk__xe_next(expr)) { test = pe_eval_subexpr(expr, rule_data, next_change); empty = FALSE; if (test && do_and == FALSE) { crm_trace("Expression %s/%s passed", pcmk__xe_id(rule), pcmk__xe_id(expr)); return TRUE; } else if (test == FALSE && do_and) { crm_trace("Expression %s/%s failed", pcmk__xe_id(rule), pcmk__xe_id(expr)); return FALSE; } } if (empty) { pcmk__config_err("Ignoring rule %s because it contains no expressions", pcmk__xe_id(rule)); } crm_trace("Rule %s %s", pcmk__xe_id(rule), passed ? "passed" : "failed"); return passed; } /*! * \brief Evaluate a single rule expression, including any subexpressions * * \param[in,out] expr XML containing a rule expression * \param[in] rule_data Matching parameters to check against expression * \param[out] next_change If not NULL, set to when evaluation will change * * \return TRUE if \p rule_data passes \p expr, otherwise FALSE */ gboolean pe_eval_subexpr(xmlNode *expr, const pe_rule_eval_data_t *rule_data, crm_time_t *next_change) { gboolean accept = FALSE; const char *uname = NULL; switch (pcmk__expression_type(expr)) { case pcmk__subexpr_rule: accept = pe_eval_expr(expr, rule_data, next_change); break; case pcmk__subexpr_attribute: case pcmk__subexpr_location: /* these expressions can never succeed if there is * no node to compare with */ if (rule_data->node_hash != NULL) { accept = pe__eval_attr_expr(expr, rule_data); } break; case pcmk__subexpr_datetime: switch (pcmk__evaluate_date_expression(expr, rule_data->now, next_change)) { case pcmk_rc_within_range: case pcmk_rc_ok: accept = TRUE; break; default: accept = FALSE; break; } break; case pcmk__subexpr_resource: accept = pe__eval_rsc_expr(expr, rule_data); break; case pcmk__subexpr_operation: accept = pe__eval_op_expr(expr, rule_data); break; default: CRM_CHECK(FALSE /* bad type */ , return FALSE); accept = FALSE; } if (rule_data->node_hash) { uname = g_hash_table_lookup(rule_data->node_hash, CRM_ATTR_UNAME); } crm_trace("Expression %s %s on %s", pcmk__xe_id(expr), (accept? "passed" : "failed"), pcmk__s(uname, "all nodes")); return accept; } -/*! - * \internal - * \brief Compare two values in a rule's node attribute expression - * - * \param[in] l_val Value on left-hand side of comparison - * \param[in] r_val Value on right-hand side of comparison - * \param[in] type How to interpret the values (allowed values: - * \c PCMK_VALUE_STRING, \c PCMK_VALUE_INTEGER, - * \c PCMK_VALUE_NUMBER, \c PCMK_VALUE_VERSION, \c NULL) - * \param[in] op Type of comparison - * - * \return -1 if (l_val < r_val), - * 0 if (l_val == r_val), - * 1 if (l_val > r_val) - */ -static int -compare_attr_expr_vals(const char *l_val, const char *r_val, const char *type, - const char *op) -{ - int cmp = 0; - - if (l_val != NULL && r_val != NULL) { - if (type == NULL) { - if (pcmk__strcase_any_of(op, - PCMK_VALUE_LT, PCMK_VALUE_LTE, - PCMK_VALUE_GT, PCMK_VALUE_GTE, NULL)) { - if (pcmk__char_in_any_str('.', l_val, r_val, NULL)) { - type = PCMK_VALUE_NUMBER; - } else { - type = PCMK_VALUE_INTEGER; - } - - } else { - type = PCMK_VALUE_STRING; - } - crm_trace("Defaulting to %s based comparison for '%s' op", type, op); - } - - if (pcmk__str_eq(type, PCMK_VALUE_STRING, pcmk__str_casei)) { - cmp = strcasecmp(l_val, r_val); - - } else if (pcmk__str_eq(type, PCMK_VALUE_INTEGER, pcmk__str_casei)) { - long long l_val_num; - int rc1 = pcmk__scan_ll(l_val, &l_val_num, 0LL); - - long long r_val_num; - int rc2 = pcmk__scan_ll(r_val, &r_val_num, 0LL); - - if ((rc1 == pcmk_rc_ok) && (rc2 == pcmk_rc_ok)) { - if (l_val_num < r_val_num) { - cmp = -1; - } else if (l_val_num > r_val_num) { - cmp = 1; - } else { - cmp = 0; - } - - } else { - crm_debug("Integer parse error. Comparing %s and %s as strings", - l_val, r_val); - cmp = compare_attr_expr_vals(l_val, r_val, PCMK_VALUE_STRING, - op); - } - - } else if (pcmk__str_eq(type, PCMK_VALUE_NUMBER, pcmk__str_casei)) { - double l_val_num; - double r_val_num; - - int rc1 = pcmk__scan_double(l_val, &l_val_num, NULL, NULL); - int rc2 = pcmk__scan_double(r_val, &r_val_num, NULL, NULL); - - if (rc1 == pcmk_rc_ok && rc2 == pcmk_rc_ok) { - if (l_val_num < r_val_num) { - cmp = -1; - } else if (l_val_num > r_val_num) { - cmp = 1; - } else { - cmp = 0; - } - - } else { - crm_debug("Floating-point parse error. Comparing %s and %s as " - "strings", l_val, r_val); - cmp = compare_attr_expr_vals(l_val, r_val, PCMK_VALUE_STRING, - op); - } - - } else if (pcmk__str_eq(type, PCMK_VALUE_VERSION, pcmk__str_casei)) { - cmp = compare_version(l_val, r_val); - - } - - } else if (l_val == NULL && r_val == NULL) { - cmp = 0; - } else if (r_val == NULL) { - cmp = 1; - } else { // l_val == NULL && r_val != NULL - cmp = -1; - } - - return cmp; -} - /*! * \internal * \brief Check whether an attribute expression evaluates to \c true * * \param[in] l_val Value on left-hand side of comparison * \param[in] r_val Value on right-hand side of comparison - * \param[in] type How to interpret the values (allowed values: - * \c PCMK_VALUE_STRING, \c PCMK_VALUE_INTEGER, - * \c PCMK_VALUE_NUMBER, \c PCMK_VALUE_VERSION, \c NULL) + * \param[in] type How to interpret the values * \param[in] op Type of comparison. * * \return \c true if expression evaluates to \c true, \c false * otherwise */ static bool -accept_attr_expr(const char *l_val, const char *r_val, const char *type, - const char *op) +accept_attr_expr(const char *l_val, const char *r_val, enum pcmk__type type, + enum pcmk__comparison op) { int cmp; - if (pcmk__str_eq(op, PCMK_VALUE_DEFINED, pcmk__str_casei)) { - return (l_val != NULL); + switch (op) { + case pcmk__comparison_defined: + return (l_val != NULL); - } else if (pcmk__str_eq(op, PCMK_VALUE_NOT_DEFINED, pcmk__str_casei)) { - return (l_val == NULL); + case pcmk__comparison_undefined: + return (l_val == NULL); + default: + break; } - cmp = compare_attr_expr_vals(l_val, r_val, type, op); + cmp = pcmk__cmp_by_type(l_val, r_val, type); + + switch (op) { + case pcmk__comparison_eq: + return (cmp == 0); - if (pcmk__str_eq(op, PCMK_VALUE_EQ, pcmk__str_casei)) { - return (cmp == 0); + case pcmk__comparison_ne: + return (cmp != 0); - } else if (pcmk__str_eq(op, PCMK_VALUE_NE, pcmk__str_casei)) { - return (cmp != 0); + default: + break; + } - } else if (l_val == NULL || r_val == NULL) { + if ((l_val == NULL) || (r_val == NULL)) { // The comparison is meaningless from this point on return false; + } - } else if (pcmk__str_eq(op, PCMK_VALUE_LT, pcmk__str_casei)) { - return (cmp < 0); + switch (op) { + case pcmk__comparison_lt: + return (cmp < 0); - } else if (pcmk__str_eq(op, PCMK_VALUE_LTE, pcmk__str_casei)) { - return (cmp <= 0); + case pcmk__comparison_lte: + return (cmp <= 0); - } else if (pcmk__str_eq(op, PCMK_VALUE_GT, pcmk__str_casei)) { - return (cmp > 0); + case pcmk__comparison_gt: + return (cmp > 0); - } else if (pcmk__str_eq(op, PCMK_VALUE_GTE, pcmk__str_casei)) { - return (cmp >= 0); - } + case pcmk__comparison_gte: + return (cmp >= 0); - return false; // Should never reach this point + default: // Not possible with schema validation enabled + return false; + } } /*! * \internal * \brief Get correct value according to \c PCMK_XA_VALUE_SOURCE * - * \param[in] expr_id Rule expression ID (for logging only) - * \param[in] value value given in rule expression - * \param[in] value_source \c PCMK_XA_VALUE_SOURCE given in rule expressions - * \param[in] match_data If not NULL, resource back-references and params + * \param[in] value value given in rule expression + * \param[in] source Reference value source + * \param[in] match_data If not NULL, resource back-references and params */ static const char * -expand_value_source(const char *expr_id, const char *value, - const char *value_source, const pe_match_data_t *match_data) +expand_value_source(const char *value, enum pcmk__reference_source source, + const pe_match_data_t *match_data) { GHashTable *table = NULL; if (pcmk__str_empty(value)) { - return NULL; // value_source is irrelevant + /* @COMPAT When we can break backward compatibility, drop this block so + * empty strings are treated as such (there should never be an empty + * string as an instance attribute or meta-attribute name, so those will + * get NULL anyway, but it could matter for literal comparisons) + */ + return NULL; + } - } else if (pcmk__str_eq(value_source, PCMK_VALUE_PARAM, pcmk__str_casei)) { - table = match_data->params; + switch (source) { + case pcmk__source_literal: + return value; - } else if (pcmk__str_eq(value_source, PCMK_VALUE_META, pcmk__str_casei)) { - table = match_data->meta; + case pcmk__source_instance_attrs: + table = match_data->params; + break; - } else { // literal - if (!pcmk__str_eq(value_source, PCMK_VALUE_LITERAL, - pcmk__str_null_matches|pcmk__str_casei)) { + case pcmk__source_meta_attrs: + table = match_data->meta; + break; - pcmk__config_warn("Expression %s has invalid " PCMK_XA_VALUE_SOURCE - " value '%s', using default " - "('" PCMK_VALUE_LITERAL "')", - pcmk__s(expr_id, "without ID"), value_source); - } - return value; + default: + return NULL; // Not possible } if (table == NULL) { return NULL; } return (const char *) g_hash_table_lookup(table, value); } /*! * \internal * \brief Evaluate a node attribute expression based on #uname, #id, #kind, * or a generic node attribute * * \param[in] expr XML of rule expression * \param[in] rule_data The match_data and node_hash members are used * * \return TRUE if rule_data satisfies the expression, FALSE otherwise */ gboolean pe__eval_attr_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data) { gboolean attr_allocated = FALSE; const char *h_val = NULL; const char *id = pcmk__xe_id(expr); const char *attr = crm_element_value(expr, PCMK_XA_ATTRIBUTE); - const char *op = crm_element_value(expr, PCMK_XA_OPERATION); - const char *type = crm_element_value(expr, PCMK_XA_TYPE); + const char *op = NULL; + const char *type_s = crm_element_value(expr, PCMK_XA_TYPE); const char *value = crm_element_value(expr, PCMK_XA_VALUE); const char *value_source = crm_element_value(expr, PCMK_XA_VALUE_SOURCE); + enum pcmk__comparison comparison = pcmk__comparison_unknown; + enum pcmk__type type = pcmk__type_unknown; + if (attr == NULL) { pcmk__config_err("Expression %s invalid: " PCMK_XA_ATTRIBUTE " not specified", pcmk__s(id, "without ID")); return FALSE; - } else if (op == NULL) { - pcmk__config_err("Expression %s invalid: " PCMK_XA_OPERATION - " not specified", pcmk__s(id, "without ID")); + } + + // Get and validate operation + op = crm_element_value(expr, PCMK_XA_OPERATION); + comparison = pcmk__parse_comparison(op); + if (comparison == pcmk__comparison_unknown) { + // Not possible with schema validation enabled + if (op == NULL) { + pcmk__config_err("Treating expression %s as not passing " + "because it has no " PCMK_XA_OPERATION, + pcmk__s(id, "without ID")); + } else { + pcmk__config_err("Treating expression %s as not passing " + "because '%s' is not a valid " PCMK_XA_OPERATION, + pcmk__s(id, "without ID"), op); + } return FALSE; } if (rule_data->match_data != NULL) { + enum pcmk__reference_source source = pcmk__source_unknown; + // Expand any regular expression submatches (%0-%9) in attribute name if (rule_data->match_data->re != NULL) { const char *match = rule_data->match_data->re->string; const regmatch_t *submatches = rule_data->match_data->re->pmatch; const int nmatches = rule_data->match_data->re->nregs; char *resolved_attr = pcmk__replace_submatches(attr, match, submatches, nmatches); if (resolved_attr != NULL) { attr = (const char *) resolved_attr; attr_allocated = TRUE; } } // Get value appropriate to PCMK_XA_VALUE_SOURCE - value = expand_value_source(id, value, value_source, - rule_data->match_data); + source = pcmk__parse_source(value_source); + if (source == pcmk__source_unknown) { + // Not possible with schema validation enabled + // @COMPAT Fail expression once we can break backward compatibility + pcmk__config_warn("Expression %s has invalid " PCMK_XA_VALUE_SOURCE + " value '%s', using default " + "('" PCMK_VALUE_LITERAL "')", + pcmk__s(id, "without ID"), value_source); + source = pcmk__source_literal; + } + value = expand_value_source(value, source, rule_data->match_data); } if (rule_data->node_hash != NULL) { h_val = (const char *)g_hash_table_lookup(rule_data->node_hash, attr); } if (attr_allocated) { free((char *)attr); attr = NULL; } - return accept_attr_expr(h_val, value, type, op); + // Get and validate value type (after expanding value) + type = pcmk__parse_type(type_s, comparison, h_val, value); + if (type == pcmk__type_unknown) { + /* Not possible with schema validation enabled + * + * @COMPAT When we can break behavioral backward compatibility, treat + * the expression as not passing. + */ + pcmk__config_warn("Non-empty node attribute values will be treated as " + "equal for expression %s because '%s' is not a " + "valid type", pcmk__s(id, "without ID"), type); + } + + return accept_attr_expr(h_val, value, type, comparison); } gboolean pe__eval_op_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data) { const char *name = crm_element_value(expr, PCMK_XA_NAME); const char *interval_s = crm_element_value(expr, PCMK_META_INTERVAL); guint interval_ms = 0U; crm_trace("Testing op_defaults expression: %s", pcmk__xe_id(expr)); if (rule_data->op_data == NULL) { crm_trace("No operations data provided"); return FALSE; } if (pcmk_parse_interval_spec(interval_s, &interval_ms) != pcmk_rc_ok) { crm_trace("Could not parse interval: %s", interval_s); return FALSE; } if ((interval_s != NULL) && (interval_ms != rule_data->op_data->interval)) { crm_trace("Interval doesn't match: %d != %d", interval_ms, rule_data->op_data->interval); return FALSE; } if (!pcmk__str_eq(name, rule_data->op_data->op_name, pcmk__str_none)) { crm_trace("Name doesn't match: %s != %s", name, rule_data->op_data->op_name); return FALSE; } return TRUE; } gboolean pe__eval_rsc_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data) { const char *class = crm_element_value(expr, PCMK_XA_CLASS); const char *provider = crm_element_value(expr, PCMK_XA_PROVIDER); const char *type = crm_element_value(expr, PCMK_XA_TYPE); crm_trace("Testing rsc_defaults expression: %s", pcmk__xe_id(expr)); if (rule_data->rsc_data == NULL) { crm_trace("No resource data provided"); return FALSE; } if (class != NULL && !pcmk__str_eq(class, rule_data->rsc_data->standard, pcmk__str_none)) { crm_trace("Class doesn't match: %s != %s", class, rule_data->rsc_data->standard); return FALSE; } if ((provider == NULL && rule_data->rsc_data->provider != NULL) || (provider != NULL && rule_data->rsc_data->provider == NULL) || !pcmk__str_eq(provider, rule_data->rsc_data->provider, pcmk__str_none)) { crm_trace("Provider doesn't match: %s != %s", provider, rule_data->rsc_data->provider); return FALSE; } if (type != NULL && !pcmk__str_eq(type, rule_data->rsc_data->agent, pcmk__str_none)) { crm_trace("Agent doesn't match: %s != %s", type, rule_data->rsc_data->agent); return FALSE; } return TRUE; } // Deprecated functions kept only for backward API compatibility // LCOV_EXCL_START #include gboolean test_ruleset(xmlNode *ruleset, GHashTable *node_hash, crm_time_t *now) { return pe_evaluate_rules(ruleset, node_hash, now, NULL); } gboolean test_rule(xmlNode * rule, GHashTable * node_hash, enum rsc_role_e role, crm_time_t * now) { return pe_test_rule(rule, node_hash, role, now, NULL, NULL); } gboolean pe_test_rule_re(xmlNode * rule, GHashTable * node_hash, enum rsc_role_e role, crm_time_t * now, pe_re_match_data_t * re_match_data) { pe_match_data_t match_data = { .re = re_match_data, .params = NULL, .meta = NULL, }; return pe_test_rule(rule, node_hash, role, now, NULL, &match_data); } gboolean pe_test_rule_full(xmlNode *rule, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now, pe_match_data_t *match_data) { return pe_test_rule(rule, node_hash, role, now, NULL, match_data); } gboolean test_expression(xmlNode * expr, GHashTable * node_hash, enum rsc_role_e role, crm_time_t * now) { return pe_test_expression(expr, node_hash, role, now, NULL, NULL); } gboolean pe_test_expression_re(xmlNode * expr, GHashTable * node_hash, enum rsc_role_e role, crm_time_t * now, pe_re_match_data_t * re_match_data) { pe_match_data_t match_data = { .re = re_match_data, .params = NULL, .meta = NULL, }; return pe_test_expression(expr, node_hash, role, now, NULL, &match_data); } gboolean pe_test_expression_full(xmlNode *expr, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now, pe_match_data_t *match_data) { return pe_test_expression(expr, node_hash, role, now, NULL, match_data); } void unpack_instance_attributes(xmlNode *top, xmlNode *xml_obj, const char *set_name, GHashTable *node_hash, GHashTable *hash, const char *always_first, gboolean overwrite, crm_time_t *now) { pe_rule_eval_data_t rule_data = { .node_hash = node_hash, .now = now, .match_data = NULL, .rsc_data = NULL, .op_data = NULL }; pe_eval_nvpairs(top, xml_obj, set_name, &rule_data, hash, always_first, overwrite, NULL); } enum expression_type find_expression_type(xmlNode *expr) { return pcmk__expression_type(expr); } char * pe_expand_re_matches(const char *string, const pe_re_match_data_t *match_data) { if (match_data == NULL) { return NULL; } return pcmk__replace_submatches(string, match_data->string, match_data->pmatch, match_data->nregs); } // LCOV_EXCL_STOP // End deprecated API