pacemaker  2.0.2-debe490
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xml.h
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2019 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 CRM_COMMON_XML__H
11 # define CRM_COMMON_XML__H
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
23 # include <stdio.h>
24 # include <sys/types.h>
25 # include <unistd.h>
26 
27 # include <stdlib.h>
28 # include <errno.h>
29 # include <fcntl.h>
30 
31 # include <libxml/tree.h>
32 # include <libxml/xpath.h>
33 
34 # include <crm/crm.h>
35 # include <crm/common/nvpair.h>
36 
37 /* Define compression parameters for IPC messages
38  *
39  * Compression costs a LOT, so we don't want to do it unless we're hitting
40  * message limits. Currently, we use 128KB as the threshold, because higher
41  * values don't play well with the heartbeat stack. With an earlier limit of
42  * 10KB, compressing 184 of 1071 messages accounted for 23% of the total CPU
43  * used by the cib.
44  */
45 # define CRM_BZ2_BLOCKS 4
46 # define CRM_BZ2_WORK 20
47 # define CRM_BZ2_THRESHOLD 128 * 1024
48 
49 # define XML_PARANOIA_CHECKS 0
50 
51 typedef const xmlChar *pcmkXmlStr;
52 
53 gboolean add_message_xml(xmlNode * msg, const char *field, xmlNode * xml);
54 xmlNode *get_message_xml(xmlNode * msg, const char *field);
55 
56 xmlDoc *getDocPtr(xmlNode * node);
57 
58 /*
59  * Replacement function for xmlCopyPropList which at the very least,
60  * doesn't work the way *I* would expect it to.
61  *
62  * Copy all the attributes/properties from src into target.
63  *
64  * Not recursive, does not return anything.
65  *
66  */
67 void copy_in_properties(xmlNode * target, xmlNode * src);
68 void expand_plus_plus(xmlNode * target, const char *name, const char *value);
69 void fix_plus_plus_recursive(xmlNode * target);
70 
71 /*
72  * Create a node named "name" as a child of "parent"
73  * If parent is NULL, creates an unconnected node.
74  *
75  * Returns the created node
76  *
77  */
78 xmlNode *create_xml_node(xmlNode * parent, const char *name);
79 
80 /*
81  *
82  */
83 void purge_diff_markers(xmlNode * a_node);
84 
85 /*
86  * Returns a deep copy of src_node
87  *
88  */
89 xmlNode *copy_xml(xmlNode * src_node);
90 
91 /*
92  * Add a copy of xml_node to new_parent
93  */
94 xmlNode *add_node_copy(xmlNode * new_parent, xmlNode * xml_node);
95 
96 int add_node_nocopy(xmlNode * parent, const char *name, xmlNode * child);
97 
98 /*
99  * XML I/O Functions
100  *
101  * Whitespace between tags is discarded.
102  */
103 xmlNode *filename2xml(const char *filename);
104 
105 xmlNode *stdin2xml(void);
106 
107 xmlNode *string2xml(const char *input);
108 
109 int write_xml_fd(xmlNode * xml_node, const char *filename, int fd, gboolean compress);
110 int write_xml_file(xmlNode * xml_node, const char *filename, gboolean compress);
111 
112 char *dump_xml_formatted(xmlNode * msg);
113 /* Also dump the text node with xml_log_option_text enabled */
114 char *dump_xml_formatted_with_text(xmlNode * msg);
115 
116 char *dump_xml_unformatted(xmlNode * msg);
117 
118 /*
119  * Diff related Functions
120  */
121 xmlNode *diff_xml_object(xmlNode * left, xmlNode * right, gboolean suppress);
122 
123 xmlNode *subtract_xml_object(xmlNode * parent, xmlNode * left, xmlNode * right,
124  gboolean full, gboolean * changed, const char *marker);
125 
126 gboolean can_prune_leaf(xmlNode * xml_node);
127 
128 gboolean apply_xml_diff(xmlNode *old_xml, xmlNode *diff, xmlNode **new_xml);
129 
130 /*
131  * Searching & Modifying
132  */
133 xmlNode *find_xml_node(xmlNode * cib, const char *node_path, gboolean must_find);
134 
135 xmlNode *find_entity(xmlNode * parent, const char *node_name, const char *id);
136 
137 void xml_remove_prop(xmlNode * obj, const char *name);
138 
139 gboolean replace_xml_child(xmlNode * parent, xmlNode * child, xmlNode * update,
140  gboolean delete_only);
141 
142 gboolean update_xml_child(xmlNode * child, xmlNode * to_update);
143 
144 int find_xml_children(xmlNode ** children, xmlNode * root,
145  const char *tag, const char *field, const char *value,
146  gboolean search_matches);
147 
148 xmlNode *get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level);
149 xmlNode *get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level);
150 
151 static inline const char *
152 crm_element_name(const xmlNode *xml)
153 {
154  return xml? (const char *)(xml->name) : NULL;
155 }
156 
157 gboolean xml_has_children(const xmlNode * root);
158 
159 char *calculate_on_disk_digest(xmlNode * local_cib);
160 char *calculate_operation_digest(xmlNode * local_cib, const char *version);
161 char *calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
162  const char *version);
163 
164 /* schema-related functions (from schemas.c) */
165 gboolean validate_xml(xmlNode * xml_blob, const char *validation, gboolean to_logs);
166 gboolean validate_xml_verbose(xmlNode * xml_blob);
167 
205 int update_validation(xmlNode **xml_blob, int *best, int max,
206  gboolean transform, gboolean to_logs);
207 
208 int get_schema_version(const char *name);
209 const char *get_schema_name(int version);
210 const char *xml_latest_schema(void);
211 gboolean cli_config_update(xmlNode ** xml, int *best_version, gboolean to_logs);
212 
213 void crm_xml_init(void);
214 void crm_xml_cleanup(void);
215 
216 static inline xmlNode *
217 __xml_first_child(const xmlNode *parent)
218 {
219  xmlNode *child = parent? parent->children : NULL;
220 
221  while (child && (child->type == XML_TEXT_NODE)) {
222  child = child->next;
223  }
224  return child;
225 }
226 
227 static inline xmlNode *
228 __xml_next(const xmlNode *child)
229 {
230  xmlNode *next = child? child->next : NULL;
231 
232  while (next && (next->type == XML_TEXT_NODE)) {
233  next = next->next;
234  }
235  return next;
236 }
237 
238 static inline xmlNode *
239 __xml_first_child_element(const xmlNode *parent)
240 {
241  xmlNode *child = parent? parent->children : NULL;
242 
243  while (child && (child->type != XML_ELEMENT_NODE)) {
244  child = child->next;
245  }
246  return child;
247 }
248 
249 static inline xmlNode *
250 __xml_next_element(const xmlNode *child)
251 {
252  xmlNode *next = child? child->next : NULL;
253 
254  while (next && (next->type != XML_ELEMENT_NODE)) {
255  next = next->next;
256  }
257  return next;
258 }
259 
260 void free_xml(xmlNode * child);
261 
262 xmlNode *first_named_child(const xmlNode *parent, const char *name);
263 xmlNode *crm_next_same_xml(const xmlNode *sibling);
264 
265 xmlNode *sorted_xml(xmlNode * input, xmlNode * parent, gboolean recursive);
266 xmlXPathObjectPtr xpath_search(xmlNode * xml_top, const char *path);
267 void crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
268  void (*helper)(xmlNode*, void*), void *user_data);
269 xmlNode *expand_idref(xmlNode * input, xmlNode * top);
270 
271 void freeXpathObject(xmlXPathObjectPtr xpathObj);
272 xmlNode *getXpathResult(xmlXPathObjectPtr xpathObj, int index);
273 void dedupXpathResults(xmlXPathObjectPtr xpathObj);
274 
275 static inline int numXpathResults(xmlXPathObjectPtr xpathObj)
276 {
277  if(xpathObj == NULL || xpathObj->nodesetval == NULL) {
278  return 0;
279  }
280  return xpathObj->nodesetval->nodeNr;
281 }
282 
283 bool xml_acl_enabled(xmlNode *xml);
284 void xml_acl_disable(xmlNode *xml);
285 bool xml_acl_denied(xmlNode *xml); /* Part or all of a change was rejected */
286 bool xml_acl_filtered_copy(const char *user, xmlNode* acl_source, xmlNode *xml, xmlNode ** result);
287 
288 bool xml_tracking_changes(xmlNode * xml);
289 bool xml_document_dirty(xmlNode *xml);
290 void xml_track_changes(xmlNode * xml, const char *user, xmlNode *acl_source, bool enforce_acls);
291 void xml_calculate_changes(xmlNode *old_xml, xmlNode *new_xml);
292 void xml_calculate_significant_changes(xmlNode *old_xml, xmlNode *new_xml);
293 void xml_accept_changes(xmlNode * xml);
294 void xml_log_changes(uint8_t level, const char *function, xmlNode *xml);
295 void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml);
296 bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3]);
297 
298 xmlNode *xml_create_patchset(
299  int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version);
300 int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version);
301 
302 void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest);
303 
304 void save_xml_to_file(xmlNode * xml, const char *desc, const char *filename);
305 char *xml_get_path(xmlNode *xml);
306 
307 char * crm_xml_escape(const char *text);
308 void crm_xml_sanitize_id(char *id);
309 void crm_xml_set_id(xmlNode *xml, const char *format, ...)
310  __attribute__ ((__format__ (__printf__, 2, 3)));
311 
315 void crm_destroy_xml(gpointer data);
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 
321 #endif
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
Definition: xml.c:1678
xmlNode * get_xpath_object_relative(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:198
A dumping ground.
xmlNode * get_message_xml(xmlNode *msg, const char *field)
Definition: xml.c:2504
mainloop_io_t * source
Definition: ipcs.h:44
xmlNode * first_named_child(const xmlNode *parent, const char *name)
Definition: xml.c:4155
void crm_xml_cleanup(void)
Definition: xml.c:4217
xmlNode * find_entity(xmlNode *parent, const char *node_name, const char *id)
Definition: xml.c:1743
void xml_track_changes(xmlNode *xml, const char *user, xmlNode *acl_source, bool enforce_acls)
Definition: xml.c:299
bool xml_acl_denied(xmlNode *xml)
Definition: acl.c:522
void crm_xml_init(void)
Definition: xml.c:4195
int write_xml_file(xmlNode *xml_node, const char *filename, gboolean compress)
Write XML to a file.
Definition: xml.c:2491
int get_schema_version(const char *name)
Definition: schemas.c:1025
void copy_in_properties(xmlNode *target, xmlNode *src)
Definition: xml.c:1750
void xml_accept_changes(xmlNode *xml)
Definition: xml.c:1000
xmlNode * filename2xml(const char *filename)
Definition: xml.c:2237
void dedupXpathResults(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:107
void purge_diff_markers(xmlNode *a_node)
Definition: xml.c:3236
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:220
xmlNode * string2xml(const char *input)
Definition: xml.c:2058
void crm_xml_sanitize_id(char *id)
Sanitize a string so it is usable as an XML ID.
Definition: xml.c:2325
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
Definition: schemas.c:696
xmlDoc * getDocPtr(xmlNode *node)
Definition: xml.c:1852
bool xml_acl_enabled(xmlNode *xml)
Definition: acl.c:546
gboolean validate_xml_verbose(xmlNode *xml_blob)
Definition: schemas.c:668
void expand_plus_plus(xmlNode *target, const char *name, const char *value)
Definition: xml.c:1791
bool xml_tracking_changes(xmlNode *xml)
Definition: xml.c:314
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:2020
char * calculate_operation_digest(xmlNode *local_cib, const char *version)
Calculate and return digest of XML operation.
Definition: digest.c:176
xmlNode * stdin2xml(void)
Definition: xml.c:2123
int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version)
Definition: xml.c:1601
gboolean update_xml_child(xmlNode *child, xmlNode *to_update)
Definition: xml.c:3974
xmlNode * add_node_copy(xmlNode *new_parent, xmlNode *xml_node)
Definition: xml.c:1868
xmlNode * expand_idref(xmlNode *input, xmlNode *top)
Definition: xml.c:4227
gboolean apply_xml_diff(xmlNode *old_xml, xmlNode *diff, xmlNode **new_xml)
Definition: xml.c:3267
bool xml_document_dirty(xmlNode *xml)
Definition: xml.c:325
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:1890
char * dump_xml_formatted(xmlNode *msg)
Definition: xml.c:3186
void xml_acl_disable(xmlNode *xml)
Definition: acl.c:533
void void crm_destroy_xml(gpointer data)
xmlNode destructor which can be used in glib collections
Definition: xml.c:4260
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
Definition: xml.c:2512
void free_xml(xmlNode *child)
Definition: xml.c:2014
gboolean xml_has_children(const xmlNode *root)
Definition: xml.c:3206
char * calculate_on_disk_digest(xmlNode *local_cib)
Calculate and return digest of XML tree, suitable for storing on disk.
Definition: digest.c:157
xmlNode * sorted_xml(xmlNode *input, xmlNode *parent, gboolean recursive)
Definition: xml.c:4123
const char * xml_latest_schema(void)
Definition: schemas.c:115
void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest)
Definition: xml.c:781
const xmlChar * pcmkXmlStr
Definition: xml.h:51
Functionality for manipulating name/value pairs.
int find_xml_children(xmlNode **children, xmlNode *root, const char *tag, const char *field, const char *value, gboolean search_matches)
Definition: xml.c:4008
bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3])
Definition: xml.c:1192
void xml_log_changes(uint8_t level, const char *function, xmlNode *xml)
Definition: xml.c:969
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
Definition: xpath.c:145
void crm_xml_set_id(xmlNode *xml, const char *format,...) __attribute__((__format__(__printf__
void xml_remove_prop(xmlNode *obj, const char *name)
Definition: xml.c:3215
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
Definition: xpath.c:64
char * calculate_xml_versioned_digest(xmlNode *input, gboolean sort, gboolean do_filter, const char *version)
Calculate and return digest of XML tree.
Definition: digest.c:193
gboolean can_prune_leaf(xmlNode *xml_node)
Definition: xml.c:3575
char * xml_get_path(xmlNode *xml)
Definition: xml.c:1937
char * dump_xml_unformatted(xmlNode *msg)
Definition: xml.c:3196
bool xml_acl_filtered_copy(const char *user, xmlNode *acl_source, xmlNode *xml, xmlNode **result)
Definition: acl.c:395
char data[0]
Definition: internal.h:92
void save_xml_to_file(xmlNode *xml, const char *desc, const char *filename)
Definition: xml.c:3249
void crm_foreach_xpath_result(xmlNode *xml, const char *xpath, void(*helper)(xmlNode *, void *), void *user_data)
Run a supplied function for each result of an xpath search.
Definition: xpath.c:179
int update_validation(xmlNode **xml_blob, int *best, int max, gboolean transform, gboolean to_logs)
Update CIB XML to most recent schema version.
Definition: schemas.c:1042
xmlNode * diff_xml_object(xmlNode *left, xmlNode *right, gboolean suppress)
Definition: xml.c:3547
int add_node_nocopy(xmlNode *parent, const char *name, xmlNode *child)
Definition: xml.c:1882
xmlNode * subtract_xml_object(xmlNode *parent, xmlNode *left, xmlNode *right, gboolean full, gboolean *changed, const char *marker)
Definition: xml.c:3670
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
Definition: xml.c:814
void xml_calculate_significant_changes(xmlNode *old_xml, xmlNode *new_xml)
Definition: xml.c:3525
int write_xml_fd(xmlNode *xml_node, const char *filename, int fd, gboolean compress)
Write XML to a file descriptor.
Definition: xml.c:2469
char * crm_xml_escape(const char *text)
Definition: xml.c:2538
gboolean cli_config_update(xmlNode **xml, int *best_version, gboolean to_logs)
Definition: schemas.c:1191
void fix_plus_plus_recursive(xmlNode *target)
Definition: xml.c:1773
void freeXpathObject(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:45
xmlNode * xml_create_patchset(int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version)
Definition: xml.c:724
void xml_calculate_changes(xmlNode *old_xml, xmlNode *new_xml)
Definition: xml.c:3532
char * dump_xml_formatted_with_text(xmlNode *msg)
Definition: xml.c:3176
uint32_t version
Definition: remote.c:146
enum crm_proc_flag __attribute__
gboolean replace_xml_child(xmlNode *parent, xmlNode *child, xmlNode *update, gboolean delete_only)
Definition: xml.c:4040
const char * get_schema_name(int version)
Definition: schemas.c:1016
xmlNode * crm_next_same_xml(const xmlNode *sibling)
Get next instance of same XML tag.
Definition: xml.c:4180