29 #include <sys/types.h> 46 gboolean ruleset_default = TRUE;
49 for (rule = __xml_first_child(ruleset); rule != NULL; rule = __xml_next_element(rule)) {
51 ruleset_default = FALSE;
58 return ruleset_default;
83 gboolean empty = TRUE;
84 gboolean passed = TRUE;
85 gboolean do_and = TRUE;
86 const char *value = NULL;
96 for (expr = __xml_first_child(rule); expr != NULL; expr = __xml_next_element(expr)) {
100 if (test && do_and == FALSE) {
104 }
else if (test == FALSE && do_and) {
111 crm_err(
"Invalid Rule %s: rules must contain at least one expression",
ID(rule));
114 crm_trace(
"Rule %s %s",
ID(rule), passed ?
"passed" :
"failed");
138 gboolean accept = FALSE;
139 const char *
uname = NULL;
150 if (node_hash != NULL) {
163 #ifdef ENABLE_VERSIONED_ATTRS 166 g_hash_table_lookup_extended(node_hash,
"#ra-version", NULL, NULL)) {
180 uname = g_hash_table_lookup(node_hash,
"#uname");
184 ID(expr), accept ?
"passed" :
"failed", uname ? uname :
"all nodes");
191 const char *tag = NULL;
192 const char *attr = NULL;
195 tag = crm_element_name(expr);
212 #ifdef ENABLE_VERSIONED_ATTRS 224 gboolean accept = FALSE;
225 const char *op = NULL;
226 const char *value = NULL;
271 gboolean accept = FALSE;
272 gboolean attr_allocated = FALSE;
274 const char *h_val = NULL;
275 GHashTable *table = NULL;
277 const char *op = NULL;
278 const char *
type = NULL;
279 const char *attr = NULL;
280 const char *value = NULL;
281 const char *value_source = NULL;
289 if (attr == NULL || op == NULL) {
290 pe_err(
"Invalid attribute or operation in expression" 296 if (match_data->
re) {
300 attr = (
const char *) resolved_attr;
301 attr_allocated = TRUE;
306 table = match_data->
params;
308 table = match_data->
meta;
313 const char *param_name = value;
314 const char *param_value = NULL;
316 if (param_name && param_name[0]) {
317 if ((param_value = (
const char *)g_hash_table_lookup(table, param_name))) {
324 h_val = (
const char *)g_hash_table_lookup(hash, attr);
327 if (attr_allocated) {
332 if (value != NULL && h_val != NULL) {
343 crm_trace(
"Defaulting to %s based comparison for '%s' op", type, op);
347 cmp = strcasecmp(h_val, value);
353 if (h_val_f < value_f) {
355 }
else if (h_val_f > value_f) {
366 }
else if (value == NULL && h_val == NULL) {
368 }
else if (value == NULL) {
385 if ((h_val == value) || cmp == 0) {
390 if ((h_val == NULL && value != NULL)
391 || (h_val != NULL && value == NULL)
396 }
else if (value == NULL || h_val == NULL) {
449 goldn = (y % 19) + 1;
450 epact = (11 * goldn + 18) % 30;
451 if ((epact == 25 && goldn > 11) || epact == 24)
454 return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
458 decodeNVpair(
const char *srcstring,
char separator,
char **name,
char **value)
462 const char *temp = NULL;
468 crm_trace(
"Attempting to decode: [%s]", srcstring);
469 if (srcstring != NULL) {
470 len = strlen(srcstring);
472 if (srcstring[lpc] == separator) {
473 *name = calloc(1, lpc + 1);
477 memcpy(*name, srcstring, lpc);
489 *value = calloc(1, len + 1);
490 if (*value == NULL) {
493 temp = srcstring + lpc + 1;
494 memcpy(*value, temp, len);
495 (*value)[len] =
'\0';
513 #define cron_check(xml_field, time_field) \ 514 value = crm_element_value(cron_spec, xml_field); \ 515 if(value != NULL) { \ 516 gboolean pass = TRUE; \ 517 decodeNVpair(value, '-', &value_low, &value_high); \ 518 if(value_low == NULL) { \ 519 value_low = strdup(value); \ 521 value_low_i = crm_parse_int(value_low, "0"); \ 522 value_high_i = crm_parse_int(value_high, "-1"); \ 523 if(value_high_i < 0) { \ 524 if(value_low_i != time_field) { \ 527 } else if(value_low_i > time_field) { \ 529 } else if(value_high_i < time_field) { \ 534 if(pass == FALSE) { \ 535 crm_debug("Condition '%s' in %s: failed", value, xml_field); \ 538 crm_debug("Condition '%s' in %s: passed", value, xml_field); \ 544 const char *value = NULL;
545 char *value_low = NULL;
546 char *value_high = NULL;
549 int value_high_i = 0;
582 #define update_field(xml_field, time_fn) \ 583 value = crm_element_value(duration_spec, xml_field); \ 584 if(value != NULL) { \ 585 int value_i = crm_parse_int(value, "0"); \ 586 time_fn(end, value_i); \ 593 const char *value = NULL;
614 const char *value = NULL;
617 xmlNode *duration_spec = NULL;
618 xmlNode *date_spec = NULL;
620 gboolean passed = FALSE;
622 crm_trace(
"Testing expression: %s",
ID(time_expr));
636 if (start != NULL && end == NULL && duration_spec != NULL) {
672 typedef struct sorted_set_s {
675 const char *special_name;
680 sort_pairs(gconstpointer a, gconstpointer b)
685 if (a == NULL && b == NULL) {
687 }
else if (a == NULL) {
689 }
else if (b == NULL) {
693 if (
safe_str_eq(pair_a->name, pair_a->special_name)) {
696 }
else if (
safe_str_eq(pair_b->name, pair_a->special_name)) {
700 if (pair_a->score < pair_b->score) {
702 }
else if (pair_a->score > pair_b->score) {
709 populate_hash(xmlNode * nvpair_list, GHashTable * hash, gboolean overwrite, xmlNode * top)
711 const char *name = NULL;
712 const char *value = NULL;
713 const char *old_value = NULL;
714 xmlNode *list = nvpair_list;
715 xmlNode *an_attr = NULL;
717 name = crm_element_name(list->children);
719 list = list->children;
722 for (an_attr = __xml_first_child(list); an_attr != NULL; an_attr = __xml_next_element(an_attr)) {
731 crm_trace(
"Setting attribute: %s", name);
737 if (name == NULL || value == NULL) {
742 old_value = g_hash_table_lookup(hash, name);
746 crm_trace(
"Removing value for %s (%s)", name, value);
747 g_hash_table_remove(hash, name);
751 }
else if (old_value == NULL) {
752 g_hash_table_insert(hash, strdup(name), strdup(value));
754 }
else if (overwrite) {
755 crm_debug(
"Overwriting value of %s: %s -> %s", name, old_value, value);
756 g_hash_table_replace(hash, strdup(name), strdup(value));
762 #ifdef ENABLE_VERSIONED_ATTRS 764 get_versioned_rule(xmlNode * attr_set)
766 xmlNode * rule = NULL;
767 xmlNode * expr = NULL;
769 for (rule = __xml_first_child(attr_set); rule != NULL; rule = __xml_next_element(rule)) {
771 for (expr = __xml_first_child(rule); expr != NULL; expr = __xml_next_element(expr)) {
783 versioned_attr(xmlNode * versioned_attrs,
const char * name)
785 xmlNode *attrs = NULL;
786 xmlNode *attr = NULL;
792 for (attrs = __xml_first_child(versioned_attrs); attrs != NULL; attrs = __xml_next_element(attrs)) {
793 for (attr = __xml_first_child(attrs); attr != NULL; attr = __xml_next_element(attr)) {
804 add_versioned_attributes(xmlNode * attr_set, xmlNode * versioned_attrs)
806 xmlNode *attr_set_copy = NULL;
807 xmlNode *rule = NULL;
809 CRM_CHECK(versioned_attrs != NULL,
return);
813 rule = get_versioned_rule(attr_set_copy);
815 xmlNode *expr = __xml_first_child(rule);
817 while (expr != NULL) {
819 xmlNode *node = expr;
820 expr = __xml_next_element(expr);
823 expr = __xml_next_element(expr);
827 xmlNode *attr = __xml_first_child(attr_set_copy);
829 while (attr != NULL) {
834 xmlNode *node = attr;
835 attr = __xml_next_element(attr);
838 attr = __xml_next_element(attr);
852 typedef struct unpack_data_s {
854 GHashTable *node_hash;
861 unpack_attr_set(gpointer
data, gpointer user_data)
866 if (
test_ruleset(pair->attr_set, unpack_data->node_hash, unpack_data->now) == FALSE) {
870 #ifdef ENABLE_VERSIONED_ATTRS 871 if (get_versioned_rule(pair->attr_set) && !(unpack_data->node_hash &&
872 g_hash_table_lookup_extended(unpack_data->node_hash,
"#ra-version", NULL, NULL))) {
878 crm_trace(
"Adding attributes from %s", pair->name);
879 populate_hash(pair->attr_set, unpack_data->hash, unpack_data->overwrite, unpack_data->top);
882 #ifdef ENABLE_VERSIONED_ATTRS 884 unpack_versioned_attr_set(gpointer
data, gpointer user_data)
889 if (
test_ruleset(pair->attr_set, unpack_data->node_hash, unpack_data->now) == FALSE) {
893 add_versioned_attributes(pair->attr_set, unpack_data->hash);
898 make_pairs_and_populate_data(xmlNode * top, xmlNode * xml_obj,
const char *set_name,
899 GHashTable * node_hash,
void * hash,
const char *always_first,
903 const char *score = NULL;
905 xmlNode *attr_set = NULL;
907 if (xml_obj == NULL) {
913 for (attr_set = __xml_first_child(xml_obj); attr_set != NULL; attr_set = __xml_next_element(attr_set)) {
915 if (set_name == NULL ||
crm_str_eq((
const char *)attr_set->name, set_name, TRUE)) {
918 if (attr_set == NULL) {
923 pair->name =
ID(attr_set);
924 pair->special_name = always_first;
925 pair->attr_set = attr_set;
930 unsorted = g_list_prepend(unsorted, pair);
936 data->node_hash = node_hash;
938 data->overwrite = overwrite;
943 return g_list_sort(unsorted, sort_pairs);
951 GHashTable * node_hash, GHashTable * hash,
const char *always_first,
955 GListPtr pairs = make_pairs_and_populate_data(top, xml_obj, set_name, node_hash, hash,
956 always_first, overwrite, now, &data);
959 g_list_foreach(pairs, unpack_attr_set, &data);
960 g_list_free_full(pairs, free);
964 #ifdef ENABLE_VERSIONED_ATTRS 966 pe_unpack_versioned_attributes(xmlNode * top, xmlNode * xml_obj,
const char *set_name,
967 GHashTable * node_hash, xmlNode * hash,
crm_time_t * now)
970 GListPtr pairs = make_pairs_and_populate_data(top, xml_obj, set_name, node_hash, hash,
971 NULL, FALSE, now, &data);
974 g_list_foreach(pairs, unpack_versioned_attr_set, &data);
975 g_list_free_full(pairs, free);
985 const char *p, *last_match_index;
986 char *p_dst, *result = NULL;
988 if (!
string ||
string[0] ==
'\0' || !match_data) {
992 p = last_match_index = string;
995 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
997 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
998 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
999 len += p - last_match_index + (match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so);
1000 last_match_index = p + 2;
1006 len += p - last_match_index + 1;
1013 p_dst = result = calloc(1, len);
1017 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
1019 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
1020 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
1022 int match_len = match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so;
1023 memcpy(p_dst, match_data->
string + match_data->
pmatch[i].rm_so, match_len);
#define CRM_CHECK(expr, failure_action)
void crm_time_add_years(crm_time_t *dt, int value)
#define cron_check(xml_field, time_field)
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)
void crm_time_add_seconds(crm_time_t *dt, int value)
gboolean safe_str_neq(const char *a, const char *b)
#define XML_EXPR_ATTR_TYPE
struct crm_time_s crm_time_t
gboolean test_expression(xmlNode *expr, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now)
#define XML_RULE_ATTR_SCORE
int char2score(const char *score)
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)
#define XML_EXPR_ATTR_VALUE_SOURCE
int crm_parse_int(const char *text, const char *default_text)
int crm_time_get_ordinal(crm_time_t *dt, uint32_t *y, uint32_t *d)
#define XML_NVPAIR_ATTR_NAME
gboolean test_attr_expression(xmlNode *expr, GHashTable *hash, crm_time_t *now)
#define XML_CIB_TAG_NVPAIR
void crm_time_add_hours(crm_time_t *dt, int value)
gboolean cron_range_satisfied(crm_time_t *now, xmlNode *cron_spec)
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)
gboolean pe_test_attr_expression_full(xmlNode *expr, GHashTable *hash, crm_time_t *now, pe_match_data_t *match_data)
xmlNode * copy_xml(xmlNode *src_node)
#define crm_debug(fmt, args...)
void crm_time_add_weeks(crm_time_t *dt, int value)
#define XML_EXPR_ATTR_VALUE
void crm_time_add_months(crm_time_t *dt, int value)
#define crm_trace(fmt, args...)
xmlNode * expand_idref(xmlNode *input, xmlNode *top)
Wrappers for and extensions to libxml2.
char * pe_expand_re_matches(const char *string, pe_re_match_data_t *match_data)
const char * crm_element_value(xmlNode *data, const char *name)
gboolean test_date_expression(xmlNode *time_expr, crm_time_t *now)
void crm_time_add_minutes(crm_time_t *dt, int value)
#define XML_EXPR_ATTR_OPERATION
crm_time_t * parse_xml_duration(crm_time_t *start, xmlNode *duration_spec)
void free_xml(xmlNode *child)
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)
enum rsc_role_e text2role(const char *role)
gboolean xml_has_children(const xmlNode *root)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
#define XML_RULE_ATTR_BOOLEAN_OP
gboolean test_rule(xmlNode *rule, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now)
void populate_hash(xmlNode *nvpair_list, GHashTable *hash, const char **attrs, int attrs_length)
struct unpack_data_s unpack_data_t
CRM_TRACE_INIT_DATA(pe_rules)
struct sorted_set_s sorted_set_t
int crm_time_get_gregorian(crm_time_t *dt, uint32_t *y, uint32_t *m, uint32_t *d)
#define crm_err(fmt, args...)
int crm_time_get_timeofday(crm_time_t *dt, uint32_t *h, uint32_t *m, uint32_t *s)
gboolean test_ruleset(xmlNode *ruleset, GHashTable *node_hash, crm_time_t *now)
void crm_time_set(crm_time_t *target, crm_time_t *source)
crm_time_t * crm_time_new(const char *string)
int compare_version(const char *version1, const char *version2)
#define XML_NVPAIR_ATTR_VALUE
int crm_time_compare(crm_time_t *dt, crm_time_t *rhs)
gboolean test_role_expression(xmlNode *expr, enum rsc_role_e role, crm_time_t *now)
int add_node_nocopy(xmlNode *parent, const char *name, xmlNode *child)
xmlNode * first_named_child(xmlNode *parent, const char *name)
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)
#define safe_str_eq(a, b)
enum expression_type find_expression_type(xmlNode *expr)
int crm_time_get_isoweek(crm_time_t *dt, uint32_t *y, uint32_t *w, uint32_t *d)
#define XML_EXPR_ATTR_ATTRIBUTE
void crm_time_add_days(crm_time_t *dt, int value)
#define update_field(xml_field, time_fn)
enum crm_ais_msg_types type
void crm_time_free(crm_time_t *dt)