22 #include <sys/types.h> 63 .match_data = match_data,
96 .match_data = match_data,
107 const char *tag = NULL;
108 const char *attr = NULL;
111 tag = crm_element_name(expr);
158 uint32_t epact, diy, goldn;
163 goldn = (y % 19) + 1;
164 epact = (11 * goldn + 18) % 30;
165 if ((epact == 25 && goldn > 11) || epact == 24)
168 return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
172 check_one(
const xmlNode *cron_spec,
const char *xml_field, uint32_t time_field)
185 }
else if (low == high) {
187 if (time_field < low) {
189 }
else if (time_field > high) {
194 }
else if (low != -1 && high != -1) {
196 if (time_field < low) {
198 }
else if (time_field > high) {
203 }
else if (low == -1) {
206 }
else if (high == -1) {
213 crm_debug(
"Condition '%s' in %s: passed", value, xml_field);
215 crm_debug(
"Condition '%s' in %s: failed", value, xml_field);
222 check_passes(
int rc) {
231 #define CHECK_ONE(spec, name, var) do { \ 232 int subpart_rc = check_one(spec, name, var); \ 233 if (check_passes(subpart_rc) == FALSE) { \ 241 uint32_t h, m, s, y, d, w;
263 CHECK_ONE(cron_spec,
"moon", phase_of_the_moon(now));
266 "(such as %s) is deprecated and will be removed " 267 "in a future release of Pacemaker",
ID(cron_spec));
278 update_field(
crm_time_t *t,
const xmlNode *xml,
const char *attr,
284 && (value != 0LL) && (value >= INT_MIN) && (value <= INT_MAX)) {
285 time_fn(t, (
int) value);
290 parse_xml_duration(
const crm_time_t *start,
const xmlNode *duration_spec)
309 if ((next_change != NULL) && (t != NULL)) {
318 typedef struct sorted_set_s {
321 const char *special_name;
326 sort_pairs(gconstpointer a, gconstpointer b)
331 if (a == NULL && b == NULL) {
333 }
else if (a == NULL) {
335 }
else if (b == NULL) {
339 if (pcmk__str_eq(pair_a->name, pair_a->special_name,
pcmk__str_casei)) {
342 }
else if (pcmk__str_eq(pair_b->name, pair_a->special_name,
pcmk__str_casei)) {
346 if (pair_a->score < pair_b->score) {
348 }
else if (pair_a->score > pair_b->score) {
355 populate_hash(xmlNode * nvpair_list, GHashTable * hash, gboolean overwrite, xmlNode * top)
357 const char *
name = NULL;
358 const char *value = NULL;
359 const char *old_value = NULL;
360 xmlNode *list = nvpair_list;
361 xmlNode *an_attr = NULL;
363 name = crm_element_name(list->children);
365 list = list->children;
368 for (an_attr = pcmk__xe_first_child(list); an_attr != NULL;
369 an_attr = pcmk__xe_next(an_attr)) {
384 if (
name == NULL || value == NULL) {
388 old_value = g_hash_table_lookup(hash,
name);
392 crm_trace(
"Letting %s default (removing explicit value \"%s\")",
394 g_hash_table_remove(hash,
name);
398 }
else if (old_value == NULL) {
400 g_hash_table_insert(hash, strdup(
name), strdup(value));
402 }
else if (overwrite) {
403 crm_trace(
"Setting %s=\"%s\" (overwriting old value \"%s\")",
404 name, value, old_value);
405 g_hash_table_replace(hash, strdup(
name), strdup(value));
411 typedef struct unpack_data_s {
420 unpack_attr_set(gpointer
data, gpointer user_data)
426 unpack_data->next_change)) {
430 crm_trace(
"Adding attributes from %s (score %d) %s overwrite",
431 pair->name, pair->score,
432 (unpack_data->overwrite?
"with" :
"without"));
433 populate_hash(pair->attr_set, unpack_data->hash, unpack_data->overwrite, unpack_data->top);
448 make_pairs(xmlNode *top,
const xmlNode *xml_obj,
const char *set_name,
449 const char *always_first)
451 GList *unsorted = NULL;
453 if (xml_obj == NULL) {
456 for (xmlNode *attr_set = pcmk__xe_first_child(xml_obj); attr_set != NULL;
457 attr_set = pcmk__xe_next(attr_set)) {
459 if (pcmk__str_eq(set_name, (
const char *) attr_set->name,
461 const char *score = NULL;
463 xmlNode *expanded_attr_set =
expand_idref(attr_set, top);
465 if (expanded_attr_set == NULL) {
471 pair->name =
ID(expanded_attr_set);
472 pair->special_name = always_first;
473 pair->attr_set = expanded_attr_set;
478 unsorted = g_list_prepend(unsorted, pair);
481 return g_list_sort(unsorted, sort_pairs);
499 const char *always_first, gboolean overwrite,
502 GList *pairs = make_pairs(top, xml_obj, set_name, always_first);
507 .overwrite = overwrite,
508 .next_change = next_change,
510 .rule_data = rule_data
513 g_list_foreach(pairs, unpack_attr_set, &
data);
514 g_list_free_full(pairs, free);
533 GHashTable *node_hash, GHashTable *hash,
534 const char *always_first, gboolean overwrite,
547 always_first, overwrite, next_change);
564 const char *p, *last_match_index;
565 char *p_dst, *
result = NULL;
567 if (pcmk__str_empty(
string) || !match_data) {
571 p = last_match_index = string;
574 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
576 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
577 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
578 len += p - last_match_index + (match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so);
579 last_match_index = p + 2;
585 len += p - last_match_index + 1;
592 p_dst =
result = calloc(1, len);
596 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
598 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
599 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
601 int match_len = match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so;
602 memcpy(p_dst, match_data->
string + match_data->
pmatch[i].rm_so, match_len);
630 gboolean ruleset_default = TRUE;
635 ruleset_default = FALSE;
647 return ruleset_default;
663 xmlNode *expr = NULL;
664 gboolean test = TRUE;
665 gboolean empty = TRUE;
666 gboolean passed = TRUE;
667 gboolean do_and = TRUE;
668 const char *value = NULL;
678 for (expr = pcmk__xe_first_child(rule); expr != NULL;
679 expr = pcmk__xe_next(expr)) {
684 if (test && do_and == FALSE) {
688 }
else if (test == FALSE && do_and) {
695 crm_err(
"Invalid Rule %s: rules must contain at least one expression",
ID(rule));
698 crm_trace(
"Rule %s %s",
ID(rule), passed ?
"passed" :
"failed");
715 gboolean accept = FALSE;
716 const char *
uname = NULL;
766 ID(expr), accept ?
"passed" :
"failed",
uname ?
uname :
"all nodes");
786 compare_attr_expr_vals(
const char *l_val,
const char *r_val,
const char *
type,
791 if (l_val != NULL && r_val != NULL) {
803 crm_trace(
"Defaulting to %s based comparison for '%s' op",
type, op);
807 cmp = strcasecmp(l_val, r_val);
817 if (l_val_num < r_val_num) {
819 }
else if (l_val_num > r_val_num) {
826 crm_debug(
"Integer parse error. Comparing %s and %s as strings",
828 cmp = compare_attr_expr_vals(l_val, r_val,
"string", op);
839 if (l_val_num < r_val_num) {
841 }
else if (l_val_num > r_val_num) {
848 crm_debug(
"Floating-point parse error. Comparing %s and %s as " 849 "strings", l_val, r_val);
850 cmp = compare_attr_expr_vals(l_val, r_val,
"string", op);
858 }
else if (l_val == NULL && r_val == NULL) {
860 }
else if (r_val == NULL) {
884 accept_attr_expr(
const char *l_val,
const char *r_val,
const char *
type,
890 return (l_val != NULL);
893 return (l_val == NULL);
897 cmp = compare_attr_expr_vals(l_val, r_val,
type, op);
905 }
else if (l_val == NULL || r_val == NULL) {
934 expand_value_source(
const char *value,
const char *value_source,
937 GHashTable *table = NULL;
939 if (pcmk__str_empty(value)) {
943 table = match_data->
params;
946 table = match_data->
meta;
955 return (
const char *) g_hash_table_lookup(table, value);
971 gboolean attr_allocated = FALSE;
972 const char *h_val = NULL;
974 const char *op = NULL;
975 const char *
type = NULL;
976 const char *attr = NULL;
977 const char *value = NULL;
978 const char *value_source = NULL;
988 " not specified", pcmk__s(
ID(expr),
"without ID"));
990 }
else if (op == NULL) {
992 " not specified", pcmk__s(
ID(expr),
"without ID"));
1000 if (resolved_attr != NULL) {
1001 attr = (
const char *) resolved_attr;
1002 attr_allocated = TRUE;
1007 value = expand_value_source(value, value_source, rule_data->
match_data);
1011 h_val = (
const char *)g_hash_table_lookup(rule_data->
node_hash, attr);
1014 if (attr_allocated) {
1019 return accept_attr_expr(h_val, value,
type, op);
1038 const char *value = NULL;
1041 xmlNode *duration_spec = NULL;
1042 xmlNode *date_spec = NULL;
1053 if (value != NULL) {
1057 if (value != NULL) {
1061 if (start != NULL && end == NULL && duration_spec != NULL) {
1062 end = parse_xml_duration(start, duration_spec);
1066 if ((start == NULL) && (end == NULL)) {
1070 crm_time_set_if_earlier(next_change, start);
1075 if (end && next_change) {
1078 crm_time_set_if_earlier(next_change, end);
1087 if (start == NULL) {
1096 crm_time_set_if_earlier(next_change, start);
1104 crm_time_set_if_earlier(next_change, end);
1122 crm_trace(
"Testing op_defaults expression: %s",
ID(expr));
1124 if (rule_data->
op_data == NULL) {
1125 crm_trace(
"No operations data provided");
1130 if (interval == 0 && errno != 0) {
1131 crm_trace(
"Could not parse interval: %s", interval_s);
1160 gboolean accept = FALSE;
1161 const char *op = NULL;
1162 const char *value = NULL;
1207 crm_trace(
"Testing rsc_defaults expression: %s",
ID(expr));
1214 if (
class != NULL &&
1250 return pe_test_rule(rule, node_hash, role, now, NULL, NULL);
1257 .
re = re_match_data,
1261 return pe_test_rule(rule, node_hash, role, now, NULL, &match_data);
1268 return pe_test_rule(rule, node_hash, role, now, NULL, match_data);
1281 .
re = re_match_data,
1298 GHashTable *node_hash, GHashTable *hash,
1299 const char *always_first, gboolean overwrite,
1311 pe_eval_nvpairs(top, xml_obj, set_name, &rule_data, hash, always_first,
#define CRM_CHECK(expr, failure_action)
int pe__eval_date_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data, crm_time_t *next_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)
Extract nvpair blocks contained by an XML element into a hash table.
void crm_time_add_years(crm_time_t *dt, int value)
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)
int crm_time_get_isoweek(const crm_time_t *dt, uint32_t *y, uint32_t *w, uint32_t *d)
Deprecated Pacemaker rule API.
void crm_time_add_seconds(crm_time_t *dt, int value)
Add a given number of seconds to a date/time or duration.
bool pcmk__char_in_any_str(int ch,...) G_GNUC_NULL_TERMINATED
#define XML_EXPR_ATTR_TYPE
bool crm_time_is_defined(const crm_time_t *t)
Check whether a time object has been initialized yet.
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
struct crm_time_s crm_time_t
#define pcmk__config_warn(fmt...)
gboolean test_expression(xmlNode *expr, GHashTable *node_hash, enum rsc_role_e role, crm_time_t *now)
#define XML_RULE_ATTR_SCORE
xmlNode * first_named_child(const xmlNode *parent, const char *name)
int char2score(const char *score)
Get the integer value of a score string.
crm_time_t * pcmk_copy_time(const crm_time_t *source)
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_LRM_ATTR_INTERVAL
#define XML_EXPR_ATTR_VALUE_SOURCE
#define XML_NVPAIR_ATTR_NAME
#define CHECK_ONE(spec, name, var)
enum crm_ais_msg_types type
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)
#define XML_CIB_TAG_NVPAIR
int crm_time_get_ordinal(const crm_time_t *dt, uint32_t *y, uint32_t *d)
void crm_time_add_hours(crm_time_t *dt, int value)
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)
pe_match_data_t * match_data
int pcmk__scan_ll(const char *text, long long *result, long long default_value)
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)
Evaluate one rule subelement (pass/fail)
pe_op_eval_data_t * op_data
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)
Extract nvpair blocks contained by an XML element into a hash table.
#define crm_debug(fmt, args...)
void crm_time_set(crm_time_t *target, const crm_time_t *source)
void crm_time_add_weeks(crm_time_t *dt, int value)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
#define XML_EXPR_ATTR_VALUE
void crm_time_add_months(crm_time_t *dt, int value)
#define crm_trace(fmt, args...)
gboolean pe__eval_attr_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data)
gboolean pe_eval_subexpr(xmlNode *expr, const pe_rule_eval_data_t *rule_data, crm_time_t *next_change)
Evaluate a single rule expression, including any subexpressions.
xmlNode * expand_idref(xmlNode *input, xmlNode *top)
gboolean pe_eval_expr(xmlNode *rule, const pe_rule_eval_data_t *rule_data, crm_time_t *next_change)
Evaluate all of a rule's expressions.
#define XML_AGENT_ATTR_PROVIDER
Wrappers for and extensions to libxml2.
int pe_cron_range_satisfied(const crm_time_t *now, const xmlNode *cron_spec)
void crm_time_add_minutes(crm_time_t *dt, int value)
#define XML_EXPR_ATTR_OPERATION
gboolean pe_evaluate_rules(xmlNode *ruleset, GHashTable *node_hash, crm_time_t *now, crm_time_t *next_change)
Evaluate any rules contained by given XML element.
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)
int crm_time_get_gregorian(const crm_time_t *dt, uint32_t *y, uint32_t *m, uint32_t *d)
enum rsc_role_e text2role(const char *role)
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
int pcmk__scan_double(const char *text, double *result, const char *default_text, char **end_text)
pe_rsc_eval_data_t * rsc_data
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
#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
pcmk__action_result_t result
#define PCMK_XE_DATE_EXPRESSION
#define crm_err(fmt, args...)
char guint crm_parse_interval_spec(const char *input)
Parse milliseconds from a Pacemaker interval specification.
gboolean test_ruleset(xmlNode *ruleset, GHashTable *node_hash, crm_time_t *now)
gboolean pe__eval_op_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data)
crm_time_t * crm_time_new(const char *string)
int compare_version(const char *version1, const char *version2)
#define XML_NVPAIR_ATTR_VALUE
rsc_role_e
Possible roles that a resource can be in.
gboolean pe_eval_rules(xmlNode *ruleset, const pe_rule_eval_data_t *rule_data, crm_time_t *next_change)
Evaluate rules.
int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
gboolean pe__eval_role_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data)
#define PCMK_XE_OP_EXPRESSION
#define XML_TAG_EXPRESSION
gboolean pe__eval_rsc_expr(const xmlNode *expr, const pe_rule_eval_data_t *rule_data)
#define PCMK_XE_RSC_EXPRESSION
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)
enum expression_type find_expression_type(xmlNode *expr)
int crm_time_get_timeofday(const crm_time_t *dt, uint32_t *h, uint32_t *m, uint32_t *s)
char * pe_expand_re_matches(const char *string, const pe_re_match_data_t *match_data)
Expand any regular expression submatches (%0-%9) in a string.
#define XML_EXPR_ATTR_ATTRIBUTE
void crm_time_add_days(crm_time_t *dt, int value)
#define XML_AGENT_ATTR_CLASS
xmlNode * crm_next_same_xml(const xmlNode *sibling)
Get next instance of same XML tag.
void crm_time_free(crm_time_t *dt)