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
165 if (node_hash && g_hash_table_lookup_extended(node_hash,
185 ID(expr), accept ?
"passed" :
"failed", uname ? uname :
"all nodes");
192 const char *tag = NULL;
193 const char *attr = NULL;
196 tag = crm_element_name(expr);
215 #ifdef ENABLE_VERSIONED_ATTRS
227 gboolean accept = FALSE;
228 const char *op = NULL;
229 const char *value = NULL;
274 gboolean accept = FALSE;
275 gboolean attr_allocated = FALSE;
277 const char *h_val = NULL;
278 GHashTable *table = NULL;
280 const char *op = NULL;
281 const char *
type = NULL;
282 const char *attr = NULL;
283 const char *value = NULL;
284 const char *value_source = NULL;
292 if (attr == NULL || op == NULL) {
293 pe_err(
"Invalid attribute or operation in expression"
299 if (match_data->
re) {
303 attr = (
const char *) resolved_attr;
304 attr_allocated = TRUE;
309 table = match_data->
params;
311 table = match_data->
meta;
316 const char *param_name = value;
317 const char *param_value = NULL;
319 if (param_name && param_name[0]) {
320 if ((param_value = (
const char *)g_hash_table_lookup(table, param_name))) {
327 h_val = (
const char *)g_hash_table_lookup(hash, attr);
330 if (attr_allocated) {
335 if (value != NULL && h_val != NULL) {
346 crm_trace(
"Defaulting to %s based comparison for '%s' op", type, op);
350 cmp = strcasecmp(h_val, value);
356 if (h_val_f < value_f) {
358 }
else if (h_val_f > value_f) {
369 }
else if (value == NULL && h_val == NULL) {
371 }
else if (value == NULL) {
388 if ((h_val == value) || cmp == 0) {
393 if ((h_val == NULL && value != NULL)
394 || (h_val != NULL && value == NULL)
399 }
else if (value == NULL || h_val == NULL) {
452 goldn = (y % 19) + 1;
453 epact = (11 * goldn + 18) % 30;
454 if ((epact == 25 && goldn > 11) || epact == 24)
457 return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
461 decodeNVpair(
const char *srcstring,
char separator,
char **name,
char **value)
465 const char *temp = NULL;
471 crm_trace(
"Attempting to decode: [%s]", srcstring);
472 if (srcstring != NULL) {
473 len = strlen(srcstring);
475 if (srcstring[lpc] == separator) {
476 *name = calloc(1, lpc + 1);
480 memcpy(*name, srcstring, lpc);
492 *value = calloc(1, len + 1);
493 if (*value == NULL) {
496 temp = srcstring + lpc + 1;
497 memcpy(*value, temp, len);
498 (*value)[len] =
'\0';
516 #define cron_check(xml_field, time_field) \
517 value = crm_element_value(cron_spec, xml_field); \
518 if(value != NULL) { \
519 gboolean pass = TRUE; \
520 decodeNVpair(value, '-', &value_low, &value_high); \
521 if(value_low == NULL) { \
522 value_low = strdup(value); \
524 value_low_i = crm_parse_int(value_low, "0"); \
525 value_high_i = crm_parse_int(value_high, "-1"); \
526 if(value_high_i < 0) { \
527 if(value_low_i != time_field) { \
530 } else if(value_low_i > time_field) { \
532 } else if(value_high_i < time_field) { \
537 if(pass == FALSE) { \
538 crm_debug("Condition '%s' in %s: failed", value, xml_field); \
541 crm_debug("Condition '%s' in %s: passed", value, xml_field); \
547 const char *value = NULL;
548 char *value_low = NULL;
549 char *value_high = NULL;
552 int value_high_i = 0;
585 #define update_field(xml_field, time_fn) \
586 value = crm_element_value(duration_spec, xml_field); \
587 if(value != NULL) { \
588 int value_i = crm_parse_int(value, "0"); \
589 time_fn(end, value_i); \
596 const char *value = NULL;
617 const char *value = NULL;
620 xmlNode *duration_spec = NULL;
621 xmlNode *date_spec = NULL;
623 gboolean passed = FALSE;
625 crm_trace(
"Testing expression: %s",
ID(time_expr));
639 if (start != NULL && end == NULL && duration_spec != NULL) {
675 typedef struct sorted_set_s {
678 const char *special_name;
683 sort_pairs(gconstpointer a, gconstpointer b)
688 if (a == NULL && b == NULL) {
690 }
else if (a == NULL) {
692 }
else if (b == NULL) {
696 if (
safe_str_eq(pair_a->name, pair_a->special_name)) {
699 }
else if (
safe_str_eq(pair_b->name, pair_a->special_name)) {
703 if (pair_a->score < pair_b->score) {
705 }
else if (pair_a->score > pair_b->score) {
712 populate_hash(xmlNode * nvpair_list, GHashTable * hash, gboolean overwrite, xmlNode * top)
714 const char *name = NULL;
715 const char *value = NULL;
716 const char *old_value = NULL;
717 xmlNode *list = nvpair_list;
718 xmlNode *an_attr = NULL;
720 name = crm_element_name(list->children);
722 list = list->children;
725 for (an_attr = __xml_first_child(list); an_attr != NULL; an_attr = __xml_next_element(an_attr)) {
734 crm_trace(
"Setting attribute: %s", name);
740 if (name == NULL || value == NULL) {
745 old_value = g_hash_table_lookup(hash, name);
749 crm_trace(
"Removing value for %s (%s)", name, value);
750 g_hash_table_remove(hash, name);
754 }
else if (old_value == NULL) {
755 g_hash_table_insert(hash, strdup(name), strdup(value));
757 }
else if (overwrite) {
758 crm_debug(
"Overwriting value of %s: %s -> %s", name, old_value, value);
759 g_hash_table_replace(hash, strdup(name), strdup(value));
765 #ifdef ENABLE_VERSIONED_ATTRS
767 get_versioned_rule(xmlNode * attr_set)
769 xmlNode * rule = NULL;
770 xmlNode * expr = NULL;
772 for (rule = __xml_first_child(attr_set); rule != NULL; rule = __xml_next_element(rule)) {
774 for (expr = __xml_first_child(rule); expr != NULL; expr = __xml_next_element(expr)) {
786 add_versioned_attributes(xmlNode * attr_set, xmlNode * versioned_attrs)
788 xmlNode *attr_set_copy = NULL;
789 xmlNode *rule = NULL;
790 xmlNode *expr = NULL;
792 if (!attr_set || !versioned_attrs) {
798 rule = get_versioned_rule(attr_set_copy);
804 expr = __xml_first_child(rule);
805 while (expr != NULL) {
807 xmlNode *node = expr;
809 expr = __xml_next_element(expr);
812 expr = __xml_next_element(expr);
820 typedef struct unpack_data_s {
822 GHashTable *node_hash;
829 unpack_attr_set(gpointer
data, gpointer user_data)
834 if (
test_ruleset(pair->attr_set, unpack_data->node_hash, unpack_data->now) == FALSE) {
838 #ifdef ENABLE_VERSIONED_ATTRS
839 if (get_versioned_rule(pair->attr_set) && !(unpack_data->node_hash &&
840 g_hash_table_lookup_extended(unpack_data->node_hash,
847 crm_trace(
"Adding attributes from %s", pair->name);
848 populate_hash(pair->attr_set, unpack_data->hash, unpack_data->overwrite, unpack_data->top);
851 #ifdef ENABLE_VERSIONED_ATTRS
853 unpack_versioned_attr_set(gpointer data, gpointer user_data)
858 if (
test_ruleset(pair->attr_set, unpack_data->node_hash, unpack_data->now) == FALSE) {
862 add_versioned_attributes(pair->attr_set, unpack_data->hash);
867 make_pairs_and_populate_data(xmlNode * top, xmlNode * xml_obj,
const char *set_name,
868 GHashTable * node_hash,
void * hash,
const char *always_first,
872 const char *score = NULL;
874 xmlNode *attr_set = NULL;
876 if (xml_obj == NULL) {
882 for (attr_set = __xml_first_child(xml_obj); attr_set != NULL; attr_set = __xml_next_element(attr_set)) {
884 if (set_name == NULL ||
crm_str_eq((
const char *)attr_set->name, set_name, TRUE)) {
887 if (attr_set == NULL) {
892 pair->name =
ID(attr_set);
893 pair->special_name = always_first;
894 pair->attr_set = attr_set;
899 unsorted = g_list_prepend(unsorted, pair);
905 data->node_hash = node_hash;
907 data->overwrite = overwrite;
912 return g_list_sort(unsorted, sort_pairs);
920 GHashTable * node_hash, GHashTable * hash,
const char *always_first,
924 GListPtr pairs = make_pairs_and_populate_data(top, xml_obj, set_name, node_hash, hash,
925 always_first, overwrite, now, &data);
928 g_list_foreach(pairs, unpack_attr_set, &data);
929 g_list_free_full(pairs, free);
933 #ifdef ENABLE_VERSIONED_ATTRS
935 pe_unpack_versioned_attributes(xmlNode * top, xmlNode * xml_obj,
const char *set_name,
936 GHashTable * node_hash, xmlNode * hash,
crm_time_t * now)
939 GListPtr pairs = make_pairs_and_populate_data(top, xml_obj, set_name, node_hash, hash,
940 NULL, FALSE, now, &data);
943 g_list_foreach(pairs, unpack_versioned_attr_set, &data);
944 g_list_free_full(pairs, free);
954 const char *p, *last_match_index;
955 char *p_dst, *result = NULL;
957 if (!
string ||
string[0] ==
'\0' || !match_data) {
961 p = last_match_index = string;
964 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
966 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
967 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
968 len += p - last_match_index + (match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so);
969 last_match_index = p + 2;
975 len += p - last_match_index + 1;
982 p_dst = result = calloc(1, len);
986 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
988 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
989 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
991 int match_len = match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so;
992 memcpy(p_dst, match_data->
string + match_data->
pmatch[i].rm_so, match_len);
1006 #ifdef ENABLE_VERSIONED_ATTRS
1008 pe_unpack_versioned_parameters(xmlNode *versioned_params,
const char *ra_version)
1010 GHashTable *hash = crm_str_table_new();
1012 if (versioned_params && ra_version) {
1013 GHashTable *node_hash = crm_str_table_new();
1014 xmlNode *attr_set = __xml_first_child(versioned_params);
1018 strdup(ra_version));
1020 node_hash, hash, NULL, FALSE, NULL);
1023 g_hash_table_destroy(node_hash);
#define CRM_CHECK(expr, failure_action)
void crm_time_add_years(crm_time_t *dt, int value)
#define cron_check(xml_field, time_field)
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
#define XML_RULE_ATTR_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)
int char2score(const char *score)
#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 CRM_ATTR_RA_VERSION
#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)
enum expression_type find_expression_type(xmlNode *expr)
#define CRM_TRACE_INIT_DATA(name)
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)
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)
#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.
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)
char * pe_expand_re_matches(const char *string, pe_re_match_data_t *match_data)
void free_xml(xmlNode *child)
enum rsc_role_e text2role(const char *role)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
#define XML_RULE_ATTR_BOOLEAN_OP
void populate_hash(xmlNode *nvpair_list, GHashTable *hash, const char **attrs, int attrs_length)
struct unpack_data_s unpack_data_t
struct sorted_set_s sorted_set_t
gboolean test_ruleset(xmlNode *ruleset, GHashTable *node_hash, crm_time_t *now)
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 pe_test_expression_full(xmlNode *expr, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now, pe_match_data_t *match_data)
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 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)
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)
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)
xmlNode * first_named_child(xmlNode *parent, const char *name)
gboolean test_rule(xmlNode *rule, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now)
#define safe_str_eq(a, b)
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)
gboolean test_expression(xmlNode *expr, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now)