pacemaker  3.0.0-d8340737c4
Scalable High-Availability cluster resource manager
crmcommon_private.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-2024 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #ifndef PCMK__COMMON_CRMCOMMON_PRIVATE__H
11 #define PCMK__COMMON_CRMCOMMON_PRIVATE__H
12 
13 /* This header is for the sole use of libcrmcommon, so that functions can be
14  * declared with G_GNUC_INTERNAL for efficiency.
15  */
16 
17 #include <stdint.h> // uint8_t, uint32_t
18 #include <stdbool.h> // bool
19 #include <sys/types.h> // size_t
20 
21 #include <glib.h> // G_GNUC_INTERNAL, G_GNUC_PRINTF, gchar, etc.
22 #include <libxml/tree.h> // xmlNode, xmlAttr
23 #include <qb/qbipcc.h> // struct qb_ipc_response_header
24 
25 #include <crm/common/ipc.h> // pcmk_ipc_api_t, crm_ipc_t, etc.
26 #include <crm/common/iso8601.h> // crm_time_t
27 #include <crm/common/logging.h> // LOG_NEVER
28 #include <crm/common/mainloop.h> // mainloop_io_t
29 #include <crm/common/output_internal.h> // pcmk__output_t
30 #include <crm/common/results.h> // crm_exit_t
31 #include <crm/common/rules.h> // pcmk_rule_input_t
32 #include <crm/common/xml.h> // pcmkXmlStr
33 #include <crm/common/xml_internal.h> // enum xml_private_flags
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 // Decent chunk size for processing large amounts of data
40 #define PCMK__BUFFER_SIZE 4096
41 
42 #if defined(PCMK__UNIT_TESTING)
43 #undef G_GNUC_INTERNAL
44 #define G_GNUC_INTERNAL
45 #endif
46 
47 /* When deleting portions of an XML tree, we keep a record so we can know later
48  * (e.g. when checking differences) that something was deleted.
49  */
50 typedef struct pcmk__deleted_xml_s {
51  gchar *path;
52  int position;
54 
55 typedef struct xml_node_private_s {
56  uint32_t check;
57  uint32_t flags;
59 
60 typedef struct xml_doc_private_s {
61  uint32_t check;
62  uint32_t flags;
63  char *user;
64  GList *acls;
65  GList *deleted_objs; // List of pcmk__deleted_xml_t
67 
68 // XML private data magic numbers
69 #define PCMK__XML_DOC_PRIVATE_MAGIC 0x81726354UL
70 #define PCMK__XML_NODE_PRIVATE_MAGIC 0x54637281UL
71 
72 // XML entity references
73 #define PCMK__XML_ENTITY_AMP "&amp;"
74 #define PCMK__XML_ENTITY_GT "&gt;"
75 #define PCMK__XML_ENTITY_LT "&lt;"
76 #define PCMK__XML_ENTITY_QUOT "&quot;"
77 
78 #define pcmk__set_xml_flags(xml_priv, flags_to_set) do { \
79  (xml_priv)->flags = pcmk__set_flags_as(__func__, __LINE__, \
80  LOG_NEVER, "XML", "XML node", (xml_priv)->flags, \
81  (flags_to_set), #flags_to_set); \
82  } while (0)
83 
84 #define pcmk__clear_xml_flags(xml_priv, flags_to_clear) do { \
85  (xml_priv)->flags = pcmk__clear_flags_as(__func__, __LINE__, \
86  LOG_NEVER, "XML", "XML node", (xml_priv)->flags, \
87  (flags_to_clear), #flags_to_clear); \
88  } while (0)
89 
90 G_GNUC_INTERNAL
91 bool pcmk__xml_reset_node_flags(xmlNode *xml, void *user_data);
92 
93 G_GNUC_INTERNAL
94 void pcmk__xml_set_parent_flags(xmlNode *xml, uint64_t flags);
95 
96 G_GNUC_INTERNAL
97 void pcmk__xml_new_private_data(xmlNode *xml);
98 
99 G_GNUC_INTERNAL
100 void pcmk__xml_free_private_data(xmlNode *xml);
101 
102 G_GNUC_INTERNAL
103 void pcmk__xml_free_node(xmlNode *xml);
104 
105 G_GNUC_INTERNAL
106 xmlDoc *pcmk__xml_new_doc(void);
107 
108 G_GNUC_INTERNAL
109 bool pcmk__tracking_xml_changes(xmlNode *xml, bool lazy);
110 
111 G_GNUC_INTERNAL
112 int pcmk__xml_position(const xmlNode *xml,
113  enum xml_private_flags ignore_if_set);
114 
115 G_GNUC_INTERNAL
116 xmlNode *pcmk__xml_match(const xmlNode *haystack, const xmlNode *needle,
117  bool exact);
118 
119 G_GNUC_INTERNAL
120 xmlNode *pcmk__xc_match(const xmlNode *root, const xmlNode *search_comment,
121  bool exact);
122 
123 G_GNUC_INTERNAL
124 void pcmk__xc_update(xmlNode *parent, xmlNode *target, xmlNode *update);
125 
126 G_GNUC_INTERNAL
127 void pcmk__free_acls(GList *acls);
128 
129 G_GNUC_INTERNAL
130 void pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user);
131 
132 G_GNUC_INTERNAL
133 bool pcmk__is_user_in_group(const char *user, const char *group);
134 
135 G_GNUC_INTERNAL
136 void pcmk__apply_acl(xmlNode *xml);
137 
138 G_GNUC_INTERNAL
139 void pcmk__apply_creation_acl(xmlNode *xml, bool check_top);
140 
141 G_GNUC_INTERNAL
142 int pcmk__xa_remove(xmlAttr *attr, bool force);
143 
144 G_GNUC_INTERNAL
145 void pcmk__mark_xml_attr_dirty(xmlAttr *a);
146 
147 G_GNUC_INTERNAL
148 bool pcmk__xa_filterable(const char *name);
149 
150 G_GNUC_INTERNAL
151 void pcmk__log_xmllib_err(void *ctx, const char *fmt, ...)
152 G_GNUC_PRINTF(2, 3);
153 
154 G_GNUC_INTERNAL
155 void pcmk__mark_xml_node_dirty(xmlNode *xml);
156 
157 G_GNUC_INTERNAL
158 bool pcmk__marked_as_deleted(xmlAttrPtr a, void *user_data);
159 
160 G_GNUC_INTERNAL
161 void pcmk__dump_xml_attr(const xmlAttr *attr, GString *buffer);
162 
163 G_GNUC_INTERNAL
164 int pcmk__xe_set_score(xmlNode *target, const char *name, const char *value);
165 
166 G_GNUC_INTERNAL
167 bool pcmk__xml_is_name_start_char(const char *utf8, int *len);
168 
169 G_GNUC_INTERNAL
170 bool pcmk__xml_is_name_char(const char *utf8, int *len);
171 
172 /*
173  * Date/times
174  */
175 
176 // For use with pcmk__add_time_from_xml()
186 };
187 
188 G_GNUC_INTERNAL
189 const char *pcmk__time_component_attr(enum pcmk__time_component component);
190 
191 G_GNUC_INTERNAL
193  const xmlNode *xml);
194 
195 G_GNUC_INTERNAL
197 
198 
199 /*
200  * IPC
201  */
202 
203 #define PCMK__IPC_VERSION 1
204 
205 #define PCMK__CONTROLD_API_MAJOR "1"
206 #define PCMK__CONTROLD_API_MINOR "0"
207 
208 // IPC behavior that varies by daemon
209 typedef struct pcmk__ipc_methods_s {
218  int (*new_data)(pcmk_ipc_api_t *api);
219 
226  void (*free_data)(void *api_data);
227 
243 
253  bool (*reply_expected)(pcmk_ipc_api_t *api, const xmlNode *request);
254 
264  bool (*dispatch)(pcmk_ipc_api_t *api, xmlNode *msg);
265 
274 
275 // Implementation of pcmk_ipc_api_t
277  enum pcmk_ipc_server server; // Daemon this IPC API instance is for
278  enum pcmk_ipc_dispatch dispatch_type; // How replies should be dispatched
279  size_t ipc_size_max; // maximum IPC buffer size
280  crm_ipc_t *ipc; // IPC connection
281  mainloop_io_t *mainloop_io; // If using mainloop, I/O source for IPC
282  bool free_on_disconnect; // Whether disconnect should free object
283  pcmk_ipc_callback_t cb; // Caller-registered callback (if any)
284  void *user_data; // Caller-registered data (if any)
285  void *api_data; // For daemon-specific use
286  pcmk__ipc_methods_t *cmds; // Behavior that varies by daemon
287 };
288 
289 typedef struct pcmk__ipc_header_s {
290  struct qb_ipc_response_header qb;
292  uint32_t size_compressed;
293  uint32_t flags;
294  uint8_t version;
296 
297 G_GNUC_INTERNAL
298 int pcmk__send_ipc_request(pcmk_ipc_api_t *api, const xmlNode *request);
299 
300 G_GNUC_INTERNAL
302  enum pcmk_ipc_event event_type,
303  crm_exit_t status, void *event_data);
304 
305 G_GNUC_INTERNAL
306 unsigned int pcmk__ipc_buffer_size(unsigned int max);
307 
308 G_GNUC_INTERNAL
309 bool pcmk__valid_ipc_header(const pcmk__ipc_header_t *header);
310 
311 G_GNUC_INTERNAL
313 
314 G_GNUC_INTERNAL
316 
317 G_GNUC_INTERNAL
319 
320 G_GNUC_INTERNAL
322 
323 
324 /*
325  * Logging
326  */
327 
329 #define PCMK__XML_PREFIX_CREATED "++"
330 
332 #define PCMK__XML_PREFIX_DELETED "--"
333 
335 #define PCMK__XML_PREFIX_MODIFIED "+ "
336 
338 #define PCMK__XML_PREFIX_MOVED "+~"
339 
340 /*
341  * Output
342  */
343 G_GNUC_INTERNAL
344 int pcmk__bare_output_new(pcmk__output_t **out, const char *fmt_name,
345  const char *filename, char **argv);
346 
347 G_GNUC_INTERNAL
349 
350 G_GNUC_INTERNAL
352 
353 G_GNUC_INTERNAL
355 
356 /*
357  * Rules
358  */
359 
360 // How node attribute values may be compared in rules
371 };
372 
373 // How node attribute values may be parsed in rules
380 };
381 
382 // Where to obtain reference value for a node attribute comparison
388 };
389 
390 G_GNUC_INTERNAL
391 enum pcmk__comparison pcmk__parse_comparison(const char *op);
392 
393 G_GNUC_INTERNAL
394 enum pcmk__type pcmk__parse_type(const char *type, enum pcmk__comparison op,
395  const char *value1, const char *value2);
396 
397 G_GNUC_INTERNAL
398 enum pcmk__reference_source pcmk__parse_source(const char *source);
399 
400 G_GNUC_INTERNAL
401 int pcmk__cmp_by_type(const char *value1, const char *value2,
402  enum pcmk__type type);
403 
404 G_GNUC_INTERNAL
405 int pcmk__unpack_duration(const xmlNode *duration, const crm_time_t *start,
406  crm_time_t **end);
407 
408 G_GNUC_INTERNAL
409 int pcmk__evaluate_date_spec(const xmlNode *date_spec, const crm_time_t *now);
410 
411 G_GNUC_INTERNAL
412 int pcmk__evaluate_attr_expression(const xmlNode *expression,
413  const pcmk_rule_input_t *rule_input);
414 
415 G_GNUC_INTERNAL
416 int pcmk__evaluate_rsc_expression(const xmlNode *expr,
417  const pcmk_rule_input_t *rule_input);
418 
419 G_GNUC_INTERNAL
420 int pcmk__evaluate_op_expression(const xmlNode *expr,
421  const pcmk_rule_input_t *rule_input);
422 
423 
424 /*
425  * Utils
426  */
427 #define PCMK__PW_BUFFER_LEN 500
428 
429 
430 /*
431  * Schemas
432  */
433 typedef struct {
434  unsigned char v[2];
436 
440 };
441 
442 typedef struct {
444  char *name;
445 
450  GList *transforms;
451 
452  void *cache;
453  enum pcmk__schema_validator validator;
456 
457 G_GNUC_INTERNAL
458 GList *pcmk__find_x_0_schema(void);
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #endif // PCMK__COMMON_CRMCOMMON_PRIVATE__H
G_GNUC_INTERNAL enum pcmk__type pcmk__parse_type(const char *type, enum pcmk__comparison op, const char *value1, const char *value2)
Definition: rules.c:693
G_GNUC_INTERNAL int pcmk__evaluate_date_spec(const xmlNode *date_spec, const crm_time_t *now)
Definition: rules.c:154
G_GNUC_INTERNAL bool pcmk__xml_reset_node_flags(xmlNode *xml, void *user_data)
Definition: xml.c:125
Data used to evaluate a rule (any NULL items are ignored)
Definition: rules.h:57
enum pcmk_ipc_dispatch dispatch_type
pcmk__comparison
G_GNUC_INTERNAL bool pcmk__xml_is_name_char(const char *utf8, int *len)
Definition: xml.c:591
const char * name
Definition: cib.c:26
enum pcmk_ipc_server type
Definition: cpg.c:51
G_GNUC_INTERNAL int pcmk__evaluate_attr_expression(const xmlNode *expression, const pcmk_rule_input_t *rule_input)
Definition: rules.c:957
struct crm_time_s crm_time_t
Definition: iso8601.h:32
G_GNUC_INTERNAL pcmk__ipc_methods_t * pcmk__schedulerd_api_methods(void)
G_GNUC_INTERNAL pcmk__ipc_methods_t * pcmk__pacemakerd_api_methods(void)
pcmk__time_component
G_GNUC_INTERNAL void pcmk__mark_xml_attr_dirty(xmlAttr *a)
Definition: xml_attr.c:75
pcmk__reference_source
G_GNUC_INTERNAL void pcmk__log_xmllib_err(void *ctx, const char *fmt,...) G_GNUC_PRINTF(2
G_GNUC_INTERNAL int pcmk__xml_position(const xmlNode *xml, enum xml_private_flags ignore_if_set)
Definition: xml.c:385
G_GNUC_INTERNAL int pcmk__evaluate_op_expression(const xmlNode *expr, const pcmk_rule_input_t *rule_input)
Definition: rules.c:1178
G_GNUC_INTERNAL void pcmk__register_option_messages(pcmk__output_t *out)
G_GNUC_INTERNAL void pcmk__xml_set_parent_flags(xmlNode *xml, uint64_t flags)
Definition: xml.c:84
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:41
pcmk__type
G_GNUC_INTERNAL bool pcmk__is_user_in_group(const char *user, const char *group)
Definition: utils.c:67
enum crm_exit_e crm_exit_t
G_GNUC_INTERNAL const char * pcmk__time_component_attr(enum pcmk__time_component component)
Definition: iso8601.c:1534
struct pcmk__ipc_header_s pcmk__ipc_header_t
G_GNUC_INTERNAL void pcmk__call_ipc_callback(pcmk_ipc_api_t *api, enum pcmk_ipc_event event_type, crm_exit_t status, void *event_data)
Definition: ipc_client.c:150
Wrappers for and extensions to glib mainloop.
G_GNUC_INTERNAL int pcmk__cmp_by_type(const char *value1, const char *value2, enum pcmk__type type)
Definition: rules.c:742
G_GNUC_INTERNAL bool pcmk__output_text_get_fancy(pcmk__output_t *out)
Definition: output_text.c:360
Wrappers for and extensions to libqb logging.
G_GNUC_INTERNAL bool pcmk__marked_as_deleted(xmlAttrPtr a, void *user_data)
Definition: xml_attr.c:87
G_GNUC_INTERNAL int pcmk__xe_set_score(xmlNode *target, const char *name, const char *value)
Definition: xml_element.c:174
G_GNUC_INTERNAL bool pcmk__xml_is_name_start_char(const char *utf8, int *len)
Definition: xml.c:520
struct xml_doc_private_s xml_doc_private_t
G_GNUC_INTERNAL xmlDoc * pcmk__xml_new_doc(void)
Definition: xml.c:479
struct qb_ipc_response_header qb
G_GNUC_INTERNAL int pcmk__bare_output_new(pcmk__output_t **out, const char *fmt_name, const char *filename, char **argv)
Definition: output.c:63
struct pcmk__deleted_xml_s pcmk__deleted_xml_t
G_GNUC_INTERNAL GList * pcmk__find_x_0_schema(void)
Definition: schemas.c:111
Formatted output for pacemaker tools.
pcmk_ipc_server
Available IPC interfaces.
Definition: ipc.h:48
G_GNUC_INTERNAL void pcmk__xc_update(xmlNode *parent, xmlNode *target, xmlNode *update)
Definition: xml_comment.c:99
G_GNUC_INTERNAL void pcmk__xml_new_private_data(xmlNode *xml)
Definition: xml.c:328
G_GNUC_INTERNAL void pcmk__apply_creation_acl(xmlNode *xml, bool check_top)
Definition: acl.c:554
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:151
pcmk_ipc_callback_t cb
G_GNUC_INTERNAL pcmk__ipc_methods_t * pcmk__attrd_api_methods(void)
Definition: ipc_attrd.c:114
G_GNUC_INTERNAL void pcmk__xml_free_node(xmlNode *xml)
Definition: xml.c:696
G_GNUC_INTERNAL int pcmk__add_time_from_xml(crm_time_t *t, enum pcmk__time_component component, const xmlNode *xml)
Definition: iso8601.c:1615
G_GNUC_INTERNAL int pcmk__send_ipc_request(pcmk_ipc_api_t *api, const xmlNode *request)
Definition: ipc_client.c:658
G_GNUC_INTERNAL void pcmk__xml_free_private_data(xmlNode *xml)
Definition: xml.c:340
G_GNUC_INTERNAL enum pcmk__reference_source pcmk__parse_source(const char *source)
Definition: rules.c:804
Wrappers for and extensions to libxml2.
G_GNUC_INTERNAL bool pcmk__valid_ipc_header(const pcmk__ipc_header_t *header)
Definition: ipc_common.c:107
ISO_8601 Date handling.
G_GNUC_INTERNAL int pcmk__unpack_duration(const xmlNode *duration, const crm_time_t *start, crm_time_t **end)
Definition: rules.c:245
struct pcmk__ipc_methods_s pcmk__ipc_methods_t
void(* pcmk_ipc_callback_t)(pcmk_ipc_api_t *api, enum pcmk_ipc_event event_type, crm_exit_t status, void *event_data, void *user_data)
Callback function type for Pacemaker daemon IPC APIs.
Definition: ipc.h:98
int(* post_connect)(pcmk_ipc_api_t *api)
Function and executable result codes.
G_GNUC_INTERNAL xmlNode * pcmk__xml_match(const xmlNode *haystack, const xmlNode *needle, bool exact)
Definition: xml.c:428
G_GNUC_INTERNAL void pcmk__apply_acl(xmlNode *xml)
Definition: acl.c:214
G_GNUC_INTERNAL unsigned int pcmk__ipc_buffer_size(unsigned int max)
Definition: ipc_common.c:31
G_GNUC_INTERNAL bool pcmk__xa_filterable(const char *name)
Definition: digest.c:232
xml_private_flags
Definition: xml_internal.h:404
const char * target
Definition: pcmk_fence.c:31
struct xml_node_private_s xml_node_private_t
int(* new_data)(pcmk_ipc_api_t *api)
pcmk_ipc_dispatch
How IPC replies should be dispatched.
Definition: ipc.h:74
pcmk_ipc_event
Possible event types that an IPC event callback can be called for.
Definition: ipc.h:61
pcmk__schema_validator
mainloop_io_t * mainloop_io
bool(* dispatch)(pcmk_ipc_api_t *api, xmlNode *msg)
bool(* reply_expected)(pcmk_ipc_api_t *api, const xmlNode *request)
G_GNUC_INTERNAL void pcmk__set_time_if_earlier(crm_time_t *target, const crm_time_t *source)
Definition: iso8601.c:1461
G_GNUC_INTERNAL void pcmk__dump_xml_attr(const xmlAttr *attr, GString *buffer)
Definition: xml_attr.c:106
pcmk__schema_version_t version
This structure contains everything that makes up a single output formatter.
void(* free_data)(void *api_data)
G_GNUC_INTERNAL void pcmk__free_acls(GList *acls)
Definition: acl.c:43
G_GNUC_INTERNAL void pcmk__unpack_acl(xmlNode *source, xmlNode *target, const char *user)
Definition: acl.c:272
IPC interface to Pacemaker daemons.
G_GNUC_INTERNAL pcmk__ipc_methods_t * pcmk__controld_api_methods(void)
Definition: ipc_controld.c:299
pcmk__ipc_methods_t * cmds
G_GNUC_INTERNAL enum pcmk__comparison pcmk__parse_comparison(const char *op)
Definition: rules.c:651
G_GNUC_INTERNAL void G_GNUC_INTERNAL void pcmk__mark_xml_node_dirty(xmlNode *xml)
Definition: xml.c:107
G_GNUC_INTERNAL bool pcmk__tracking_xml_changes(xmlNode *xml, bool lazy)
Definition: xml.c:69
void(* post_disconnect)(pcmk_ipc_api_t *api)
const char * parent
Definition: cib.c:27
Scheduler API for rules.
G_GNUC_INTERNAL xmlNode * pcmk__xc_match(const xmlNode *root, const xmlNode *search_comment, bool exact)
Definition: xml_comment.c:50
enum pcmk_ipc_server server
G_GNUC_INTERNAL int pcmk__xa_remove(xmlAttr *attr, bool force)
Definition: xml_attr.c:45
uint64_t flags
Definition: remote.c:211
G_GNUC_INTERNAL void pcmk__register_patchset_messages(pcmk__output_t *out)
G_GNUC_INTERNAL int pcmk__evaluate_rsc_expression(const xmlNode *expr, const pcmk_rule_input_t *rule_input)
Definition: rules.c:1109