22 #include <sys/types.h> 63 .match_data = match_data,
96 .match_data = match_data,
107 const char *attr = NULL;
156 uint32_t epact, diy, goldn;
161 goldn = (y % 19) + 1;
162 epact = (11 * goldn + 18) % 30;
163 if ((epact == 25 && goldn > 11) || epact == 24)
166 return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
170 check_one(
const xmlNode *cron_spec,
const char *xml_field, uint32_t time_field)
183 }
else if (low == high) {
185 if (time_field < low) {
187 }
else if (time_field > high) {
192 }
else if (low != -1 && high != -1) {
194 if (time_field < low) {
196 }
else if (time_field > high) {
201 }
else if (low == -1) {
204 }
else if (high == -1) {
211 crm_debug(
"Condition '%s' in %s: passed", value, xml_field);
213 crm_debug(
"Condition '%s' in %s: failed", value, xml_field);
220 check_passes(
int rc) {
229 #define CHECK_ONE(spec, name, var) do { \ 230 int subpart_rc = check_one(spec, name, var); \ 231 if (check_passes(subpart_rc) == FALSE) { \ 239 uint32_t h, m, s, y, d, w;
261 CHECK_ONE(cron_spec,
"moon", phase_of_the_moon(now));
264 "(such as %s) is deprecated and will be removed " 265 "in a future release of Pacemaker",
ID(cron_spec));
276 update_field(
crm_time_t *t,
const xmlNode *xml,
const char *attr,
282 && (value != 0LL) && (value >= INT_MIN) && (value <= INT_MAX)) {
283 time_fn(t, (
int) value);
288 parse_xml_duration(
const crm_time_t *start,
const xmlNode *duration_spec)
307 if ((next_change != NULL) && (t != NULL)) {
316 typedef struct sorted_set_s {
319 const char *special_name;
325 sort_pairs(gconstpointer a, gconstpointer b)
330 if (a == NULL && b == NULL) {
332 }
else if (a == NULL) {
334 }
else if (b == NULL) {
338 if (pcmk__str_eq(pair_a->name, pair_a->special_name,
pcmk__str_casei)) {
341 }
else if (pcmk__str_eq(pair_b->name, pair_a->special_name,
pcmk__str_casei)) {
349 if (pair_a->score < pair_b->score) {
350 return pair_a->overwrite? -1 : 1;
351 }
else if (pair_a->score > pair_b->score) {
352 return pair_a->overwrite? 1 : -1;
358 populate_hash(xmlNode * nvpair_list, GHashTable * hash, gboolean overwrite, xmlNode * top)
360 const char *
name = NULL;
361 const char *value = NULL;
362 const char *old_value = NULL;
363 xmlNode *list = nvpair_list;
364 xmlNode *an_attr = NULL;
367 list = list->children;
370 for (an_attr = pcmk__xe_first_child(list); an_attr != NULL;
371 an_attr = pcmk__xe_next(an_attr)) {
386 if (
name == NULL || value == NULL) {
390 old_value = g_hash_table_lookup(hash,
name);
394 crm_trace(
"Letting %s default (removing explicit value \"%s\")",
396 g_hash_table_remove(hash,
name);
400 }
else if (old_value == NULL) {
402 g_hash_table_insert(hash, strdup(
name), strdup(value));
404 }
else if (overwrite) {
405 crm_trace(
"Setting %s=\"%s\" (overwriting old value \"%s\")",
406 name, value, old_value);
407 g_hash_table_replace(hash, strdup(
name), strdup(value));
413 typedef struct unpack_data_s {
422 unpack_attr_set(gpointer
data, gpointer user_data)
428 unpack_data->next_change)) {
432 crm_trace(
"Adding attributes from %s (score %d) %s overwrite",
433 pair->name, pair->score,
434 (unpack_data->overwrite?
"with" :
"without"));
435 populate_hash(pair->attr_set, unpack_data->hash, unpack_data->overwrite, unpack_data->top);
450 make_pairs(xmlNode *top,
const xmlNode *xml_obj,
const char *set_name,
451 const char *always_first, gboolean overwrite)
453 GList *unsorted = NULL;
455 if (xml_obj == NULL) {
458 for (xmlNode *attr_set = pcmk__xe_first_child(xml_obj); attr_set != NULL;
459 attr_set = pcmk__xe_next(attr_set)) {
461 if (pcmk__str_eq(set_name, (
const char *) attr_set->name,
463 const char *score = NULL;
465 xmlNode *expanded_attr_set =
expand_idref(attr_set, top);
467 if (expanded_attr_set == NULL) {
473 pair->name =
ID(expanded_attr_set);
474 pair->special_name = always_first;
475 pair->attr_set = expanded_attr_set;
476 pair->overwrite = overwrite;
481 unsorted = g_list_prepend(unsorted, pair);
484 return g_list_sort(unsorted, sort_pairs);
502 const char *always_first, gboolean overwrite,
505 GList *pairs = make_pairs(top, xml_obj, set_name, always_first, overwrite);
510 .overwrite = overwrite,
511 .next_change = next_change,
513 .rule_data = rule_data
516 g_list_foreach(pairs, unpack_attr_set, &
data);
517 g_list_free_full(pairs, free);
536 GHashTable *node_hash, GHashTable *hash,
537 const char *always_first, gboolean overwrite,
550 always_first, overwrite, next_change);
567 const char *p, *last_match_index;
568 char *p_dst, *
result = NULL;
570 if (pcmk__str_empty(
string) || !match_data) {
574 p = last_match_index = string;
577 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
579 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
580 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
581 len += p - last_match_index + (match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so);
582 last_match_index = p + 2;
588 len += p - last_match_index + 1;
595 p_dst =
result = calloc(1, len);
599 if (*p ==
'%' && *(p + 1) && isdigit(*(p + 1))) {
601 if (match_data->
nregs >= i && match_data->
pmatch[i].rm_so != -1 &&
602 match_data->
pmatch[i].rm_eo > match_data->
pmatch[i].rm_so) {
604 int match_len = match_data->
pmatch[i].rm_eo - match_data->
pmatch[i].rm_so;
605 memcpy(p_dst, match_data->
string + match_data->
pmatch[i].rm_so, match_len);
633 gboolean ruleset_default = TRUE;
638 ruleset_default = FALSE;
650 return ruleset_default;
666 xmlNode *expr = NULL;
667 gboolean test = TRUE;
668 gboolean empty = TRUE;
669 gboolean passed = TRUE;
670 gboolean do_and = TRUE;
671 const char *value = NULL;
681 for (expr = pcmk__xe_first_child(rule); expr != NULL;
682 expr = pcmk__xe_next(expr)) {
687 if (test && do_and == FALSE) {
691 }
else if (test == FALSE && do_and) {
698 crm_err(
"Invalid Rule %s: rules must contain at least one expression",
ID(rule));
701 crm_trace(
"Rule %s %s",
ID(rule), passed ?
"passed" :
"failed");
718 gboolean accept = FALSE;
719 const char *
uname = NULL;
769 ID(expr), accept ?
"passed" :
"failed",
uname ?
uname :
"all nodes");
789 compare_attr_expr_vals(
const char *l_val,
const char *r_val,
const char *
type,
794 if (l_val != NULL && r_val != NULL) {
806 crm_trace(
"Defaulting to %s based comparison for '%s' op",
type, op);
810 cmp = strcasecmp(l_val, r_val);
820 if (l_val_num < r_val_num) {
822 }
else if (l_val_num > r_val_num) {
829 crm_debug(
"Integer parse error. Comparing %s and %s as strings",
831 cmp = compare_attr_expr_vals(l_val, r_val,
"string", op);
842 if (l_val_num < r_val_num) {
844 }
else if (l_val_num > r_val_num) {
851 crm_debug(
"Floating-point parse error. Comparing %s and %s as " 852 "strings", l_val, r_val);
853 cmp = compare_attr_expr_vals(l_val, r_val,
"string", op);
861 }
else if (l_val == NULL && r_val == NULL) {
863 }
else if (r_val == NULL) {
887 accept_attr_expr(
const char *l_val,
const char *r_val,
const char *
type,
893 return (l_val != NULL);
896 return (l_val == NULL);
900 cmp = compare_attr_expr_vals(l_val, r_val,
type, op);
908 }
else if (l_val == NULL || r_val == NULL) {
937 expand_value_source(
const char *value,
const char *value_source,
940 GHashTable *table = NULL;
942 if (pcmk__str_empty(value)) {
946 table = match_data->
params;
949 table = match_data->
meta;
958 return (
const char *) g_hash_table_lookup(table, value);
974 gboolean attr_allocated = FALSE;
975 const char *h_val = NULL;
977 const char *op = NULL;
978 const char *
type = NULL;
979 const char *attr = NULL;
980 const char *value = NULL;
981 const char *value_source = NULL;
991 " not specified", pcmk__s(
ID(expr),
"without ID"));
993 }
else if (op == NULL) {
995 " not specified", pcmk__s(
ID(expr),
"without ID"));
1003 if (resolved_attr != NULL) {
1004 attr = (
const char *) resolved_attr;
1005 attr_allocated = TRUE;
1010 value = expand_value_source(value, value_source, rule_data->
match_data);
1014 h_val = (
const char *)g_hash_table_lookup(rule_data->
node_hash, attr);
1017 if (attr_allocated) {
1022 return accept_attr_expr(h_val, value,
type, op);
1041 const char *value = NULL;
1044 xmlNode *duration_spec = NULL;
1045 xmlNode *date_spec = NULL;
1056 if (value != NULL) {
1060 if (value != NULL) {
1064 if (start != NULL && end == NULL && duration_spec != NULL) {
1065 end = parse_xml_duration(start, duration_spec);
1069 if ((start == NULL) && (end == NULL)) {
1073 crm_time_set_if_earlier(next_change, start);
1078 if (end && next_change) {
1081 crm_time_set_if_earlier(next_change, end);
1090 if (start == NULL) {
1099 crm_time_set_if_earlier(next_change, start);
1107 crm_time_set_if_earlier(next_change, end);
1125 crm_trace(
"Testing op_defaults expression: %s",
ID(expr));
1127 if (rule_data->
op_data == NULL) {
1128 crm_trace(
"No operations data provided");
1133 if (interval == 0 && errno != 0) {
1134 crm_trace(
"Could not parse interval: %s", interval_s);
1163 gboolean accept = FALSE;
1164 const char *op = NULL;
1165 const char *value = NULL;
1210 crm_trace(
"Testing rsc_defaults expression: %s",
ID(expr));
1217 if (
class != NULL &&
1253 return pe_test_rule(rule, node_hash, role, now, NULL, NULL);
1260 .
re = re_match_data,
1264 return pe_test_rule(rule, node_hash, role, now, NULL, &match_data);
1271 return pe_test_rule(rule, node_hash, role, now, NULL, match_data);
1284 .
re = re_match_data,
1301 GHashTable *node_hash, GHashTable *hash,
1302 const char *always_first, gboolean overwrite,
1314 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
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
Resource role is unknown.
#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)