pacemaker  1.1.16-94ff4df
Scalable High-Availability cluster resource manager
xml.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This software is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CRM_COMMON_XML__H
19 # define CRM_COMMON_XML__H
20 
27 # include <stdio.h>
28 # include <sys/types.h>
29 # include <unistd.h>
30 
31 # include <stdlib.h>
32 # include <errno.h>
33 # include <fcntl.h>
34 
35 # include <crm/crm.h>
36 
37 # include <libxml/tree.h>
38 # include <libxml/xpath.h>
39 
40 /* Compression costs a LOT, don't do it unless we're hitting message limits
41  *
42  * For now, use 256k as the lower size, which means we can have 4 big data fields
43  * before we hit heartbeat's message limit
44  *
45  * The previous limit was 10k, compressing 184 of 1071 messages accounted for 23%
46  * of the total CPU used by the cib
47  */
48 # define CRM_BZ2_BLOCKS 4
49 # define CRM_BZ2_WORK 20
50 # define CRM_BZ2_THRESHOLD 128 * 1024
51 
52 # define XML_PARANOIA_CHECKS 0
53 
54 gboolean add_message_xml(xmlNode * msg, const char *field, xmlNode * xml);
55 xmlNode *get_message_xml(xmlNode * msg, const char *field);
56 GHashTable *xml2list(xmlNode * parent);
57 
58 void hash2nvpair(gpointer key, gpointer value, gpointer user_data);
59 void hash2field(gpointer key, gpointer value, gpointer user_data);
60 void hash2metafield(gpointer key, gpointer value, gpointer user_data);
61 void hash2smartfield(gpointer key, gpointer value, gpointer user_data);
62 
63 xmlDoc *getDocPtr(xmlNode * node);
64 
65 /*
66  * Replacement function for xmlCopyPropList which at the very least,
67  * doesn't work the way *I* would expect it to.
68  *
69  * Copy all the attributes/properties from src into target.
70  *
71  * Not recursive, does not return anything.
72  *
73  */
74 void copy_in_properties(xmlNode * target, xmlNode * src);
75 void expand_plus_plus(xmlNode * target, const char *name, const char *value);
76 void fix_plus_plus_recursive(xmlNode * target);
77 
78 /*
79  * Create a node named "name" as a child of "parent"
80  * If parent is NULL, creates an unconnected node.
81  *
82  * Returns the created node
83  *
84  */
85 xmlNode *create_xml_node(xmlNode * parent, const char *name);
86 
87 /*
88  * Make a copy of name and value and use the copied memory to create
89  * an attribute for node.
90  *
91  * If node, name or value are NULL, nothing is done.
92  *
93  * If name or value are an empty string, nothing is done.
94  *
95  * Returns FALSE on failure and TRUE on success.
96  *
97  */
98 const char *crm_xml_add(xmlNode * node, const char *name, const char *value);
99 
100 const char *crm_xml_replace(xmlNode * node, const char *name, const char *value);
101 
102 const char *crm_xml_add_int(xmlNode * node, const char *name, int value);
103 
116 static inline const char *
117 crm_xml_add_boolean(xmlNode *node, const char *name, gboolean value)
118 {
119  return crm_xml_add(node, name, (value? "true" : "false"));
120 }
121 
122 /*
123  * Unlink the node and set its doc pointer to NULL so free_xml()
124  * will act appropriately
125  */
126 void unlink_xml_node(xmlNode * node);
127 
128 /*
129  *
130  */
131 void purge_diff_markers(xmlNode * a_node);
132 
133 /*
134  * Returns a deep copy of src_node
135  *
136  */
137 xmlNode *copy_xml(xmlNode * src_node);
138 
139 /*
140  * Add a copy of xml_node to new_parent
141  */
142 xmlNode *add_node_copy(xmlNode * new_parent, xmlNode * xml_node);
143 
144 int add_node_nocopy(xmlNode * parent, const char *name, xmlNode * child);
145 
146 /*
147  * XML I/O Functions
148  *
149  * Whitespace between tags is discarded.
150  */
151 xmlNode *filename2xml(const char *filename);
152 
153 xmlNode *stdin2xml(void);
154 
155 xmlNode *string2xml(const char *input);
156 
157 int write_xml_fd(xmlNode * xml_node, const char *filename, int fd, gboolean compress);
158 int write_xml_file(xmlNode * xml_node, const char *filename, gboolean compress);
159 
160 char *dump_xml_formatted(xmlNode * msg);
161 /* Also dump the text node with xml_log_option_text enabled */
162 char *dump_xml_formatted_with_text(xmlNode * msg);
163 
164 char *dump_xml_unformatted(xmlNode * msg);
165 
166 /*
167  * Diff related Functions
168  */
169 xmlNode *diff_xml_object(xmlNode * left, xmlNode * right, gboolean suppress);
170 
171 xmlNode *subtract_xml_object(xmlNode * parent, xmlNode * left, xmlNode * right,
172  gboolean full, gboolean * changed, const char *marker);
173 
174 gboolean can_prune_leaf(xmlNode * xml_node);
175 
176 void print_xml_diff(FILE * where, xmlNode * diff);
177 
178 gboolean apply_xml_diff(xmlNode * old, xmlNode * diff, xmlNode ** new);
179 
180 /*
181  * Searching & Modifying
182  */
183 xmlNode *find_xml_node(xmlNode * cib, const char *node_path, gboolean must_find);
184 
185 xmlNode *find_entity(xmlNode * parent, const char *node_name, const char *id);
186 
187 void xml_remove_prop(xmlNode * obj, const char *name);
188 
189 gboolean replace_xml_child(xmlNode * parent, xmlNode * child, xmlNode * update,
190  gboolean delete_only);
191 
192 gboolean update_xml_child(xmlNode * child, xmlNode * to_update);
193 
194 int find_xml_children(xmlNode ** children, xmlNode * root,
195  const char *tag, const char *field, const char *value,
196  gboolean search_matches);
197 
198 int crm_element_value_int(xmlNode * data, const char *name, int *dest);
199 char *crm_element_value_copy(xmlNode * data, const char *name);
200 int crm_element_value_const_int(const xmlNode * data, const char *name, int *dest);
201 const char *crm_element_value_const(const xmlNode * data, const char *name);
202 xmlNode *get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level);
203 xmlNode *get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level);
204 
205 static inline const char *
206 crm_element_name(xmlNode *xml)
207 {
208  return xml? (const char *)(xml->name) : NULL;
209 }
210 
211 const char *crm_element_value(xmlNode * data, const char *name);
212 
213 void xml_validate(const xmlNode * root);
214 
215 gboolean xml_has_children(const xmlNode * root);
216 
217 char *calculate_on_disk_digest(xmlNode * local_cib);
218 char *calculate_operation_digest(xmlNode * local_cib, const char *version);
219 char *calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
220  const char *version);
221 
222 /* schema-related functions (from schemas.c) */
223 gboolean validate_xml(xmlNode * xml_blob, const char *validation, gboolean to_logs);
224 gboolean validate_xml_verbose(xmlNode * xml_blob);
225 
263 int update_validation(xmlNode **xml_blob, int *best, int max,
264  gboolean transform, gboolean to_logs);
265 
266 int get_schema_version(const char *name);
267 const char *get_schema_name(int version);
268 const char *xml_latest_schema(void);
269 gboolean cli_config_update(xmlNode ** xml, int *best_version, gboolean to_logs);
270 
271 void crm_xml_init(void);
272 void crm_xml_cleanup(void);
273 
274 static inline xmlNode *
275 __xml_first_child(xmlNode * parent)
276 {
277  xmlNode *child = NULL;
278 
279  if (parent) {
280  child = parent->children;
281  while (child && child->type == XML_TEXT_NODE) {
282  child = child->next;
283  }
284  }
285  return child;
286 }
287 
288 static inline xmlNode *
289 __xml_next(xmlNode * child)
290 {
291  if (child) {
292  child = child->next;
293  while (child && child->type == XML_TEXT_NODE) {
294  child = child->next;
295  }
296  }
297  return child;
298 }
299 
300 static inline xmlNode *
301 __xml_next_element(xmlNode * child)
302 {
303  if (child) {
304  child = child->next;
305  while (child && child->type != XML_ELEMENT_NODE) {
306  child = child->next;
307  }
308  }
309  return child;
310 }
311 
312 void free_xml(xmlNode * child);
313 
314 xmlNode *first_named_child(xmlNode * parent, const char *name);
315 
316 xmlNode *sorted_xml(xmlNode * input, xmlNode * parent, gboolean recursive);
317 xmlXPathObjectPtr xpath_search(xmlNode * xml_top, const char *path);
318 void crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
319  void (*helper)(xmlNode*, void*), void *user_data);
320 xmlNode *expand_idref(xmlNode * input, xmlNode * top);
321 
322 void freeXpathObject(xmlXPathObjectPtr xpathObj);
323 xmlNode *getXpathResult(xmlXPathObjectPtr xpathObj, int index);
324 void dedupXpathResults(xmlXPathObjectPtr xpathObj);
325 
326 static inline int numXpathResults(xmlXPathObjectPtr xpathObj)
327 {
328  if(xpathObj == NULL || xpathObj->nodesetval == NULL) {
329  return 0;
330  }
331  return xpathObj->nodesetval->nodeNr;
332 }
333 
334 bool xml_acl_enabled(xmlNode *xml);
335 void xml_acl_disable(xmlNode *xml);
336 bool xml_acl_denied(xmlNode *xml); /* Part or all of a change was rejected */
337 bool xml_acl_filtered_copy(const char *user, xmlNode* acl_source, xmlNode *xml, xmlNode ** result);
338 
339 bool xml_tracking_changes(xmlNode * xml);
340 bool xml_document_dirty(xmlNode *xml);
341 void xml_track_changes(xmlNode * xml, const char *user, xmlNode *acl_source, bool enforce_acls);
342 void xml_calculate_changes(xmlNode * old, xmlNode * new); /* For comparing two documents after the fact */
343 void xml_accept_changes(xmlNode * xml);
344 void xml_log_changes(uint8_t level, const char *function, xmlNode *xml);
345 void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml);
346 bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3]);
347 
348 xmlNode *xml_create_patchset(
349  int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version);
350 int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version);
351 
352 void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest);
353 
354 void save_xml_to_file(xmlNode * xml, const char *desc, const char *filename);
355 char *xml_get_path(xmlNode *xml);
356 
357 char * crm_xml_escape(const char *text);
358 #endif
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
Definition: xml.c:2179
xmlNode * get_xpath_object_relative(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:198
void unlink_xml_node(xmlNode *node)
A dumping ground.
xmlNode * get_message_xml(xmlNode *msg, const char *field)
Definition: xml.c:3064
GHashTable * xml2list(xmlNode *parent)
Definition: xml.c:4776
void xml_validate(const xmlNode *root)
int crm_element_value_const_int(const xmlNode *data, const char *name, int *dest)
Definition: xml.c:3758
void xml_calculate_changes(xmlNode *old, xmlNode *new)
Definition: xml.c:4087
void crm_xml_cleanup(void)
Definition: xml.c:4939
xmlNode * find_entity(xmlNode *parent, const char *node_name, const char *id)
Definition: xml.c:2212
void xml_track_changes(xmlNode *xml, const char *user, xmlNode *acl_source, bool enforce_acls)
Definition: xml.c:838
bool xml_acl_denied(xmlNode *xml)
Definition: xml.c:803
void crm_xml_init(void)
Definition: xml.c:4917
int write_xml_file(xmlNode *xml_node, const char *filename, gboolean compress)
Definition: xml.c:3054
char * crm_element_value_copy(xmlNode *data, const char *name)
Definition: xml.c:3770
int get_schema_version(const char *name)
Definition: schemas.c:693
void print_xml_diff(FILE *where, xmlNode *diff)
void copy_in_properties(xmlNode *target, xmlNode *src)
Definition: xml.c:2231
void xml_accept_changes(xmlNode *xml)
Definition: xml.c:1533
xmlNode * filename2xml(const char *filename)
Definition: xml.c:2880
void dedupXpathResults(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:107
void purge_diff_markers(xmlNode *a_node)
Definition: xml.c:3803
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:224
xmlNode * string2xml(const char *input)
Definition: xml.c:2696
char version[256]
Definition: plugin.c:84
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
Definition: schemas.c:601
xmlDoc * getDocPtr(xmlNode *node)
Definition: xml.c:2333
bool xml_acl_enabled(xmlNode *xml)
Definition: xml.c:827
const char * crm_xml_replace(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2480
gboolean validate_xml_verbose(xmlNode *xml_blob)
Definition: schemas.c:575
void expand_plus_plus(xmlNode *target, const char *name, const char *value)
Definition: xml.c:2272
bool xml_tracking_changes(xmlNode *xml)
Definition: xml.c:853
void hash2smartfield(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4719
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:2633
void hash2field(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4742
char * calculate_operation_digest(xmlNode *local_cib, const char *version)
Calculate and return digest of XML operation.
Definition: digest.c:175
xmlNode * stdin2xml(void)
Definition: xml.c:2761
int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version)
Definition: xml.c:2102
gboolean update_xml_child(xmlNode *child, xmlNode *to_update)
Definition: xml.c:4554
xmlNode * add_node_copy(xmlNode *new_parent, xmlNode *xml_node)
Definition: xml.c:2349
const char * crm_element_value_const(const xmlNode *data, const char *name)
Definition: xml.c:3764
xmlNode * expand_idref(xmlNode *input, xmlNode *top)
Definition: xml.c:4947
bool xml_document_dirty(xmlNode *xml)
Definition: xml.c:864
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:2532
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
Definition: xml.c:3745
char * dump_xml_formatted(xmlNode *msg)
Definition: xml.c:3716
const char * crm_element_value(xmlNode *data, const char *name)
Definition: xml.c:4987
void xml_acl_disable(xmlNode *xml)
Definition: xml.c:814
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
Definition: xml.c:3072
void free_xml(xmlNode *child)
Definition: xml.c:2587
gboolean xml_has_children(const xmlNode *root)
Definition: xml.c:3736
char * calculate_on_disk_digest(xmlNode *local_cib)
Calculate and return digest of XML tree, suitable for storing on disk.
Definition: digest.c:156
xmlNode * sorted_xml(xmlNode *input, xmlNode *parent, gboolean recursive)
Definition: xml.c:4855
const char * xml_latest_schema(void)
Definition: schemas.c:104
void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest)
Definition: xml.c:1331
void hash2metafield(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4758
int find_xml_children(xmlNode **children, xmlNode *root, const char *tag, const char *field, const char *value, gboolean search_matches)
Definition: xml.c:4588
bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3])
Definition: xml.c:1725
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2434
void xml_log_changes(uint8_t level, const char *function, xmlNode *xml)
Definition: xml.c:1511
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Definition: xml.c:2522
gboolean apply_xml_diff(xmlNode *old, xmlNode *diff, xmlNode **new)
Definition: xml.c:3834
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
Definition: xpath.c:145
void xml_remove_prop(xmlNode *obj, const char *name)
Definition: xml.c:3782
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:192
gboolean can_prune_leaf(xmlNode *xml_node)
Definition: xml.c:4128
char * xml_get_path(xmlNode *xml)
Definition: xml.c:2575
char * dump_xml_unformatted(xmlNode *msg)
Definition: xml.c:3726
bool xml_acl_filtered_copy(const char *user, xmlNode *acl_source, xmlNode *xml, xmlNode **result)
Definition: xml.c:682
char data[0]
Definition: internal.h:58
#define uint8_t
Definition: stdint.in.h:144
void save_xml_to_file(xmlNode *xml, const char *desc, const char *filename)
Definition: xml.c:3816
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)
Try update CIB XML to the highest pacemaker&#39;s standard if feasible.
Definition: schemas.c:710
xmlNode * diff_xml_object(xmlNode *left, xmlNode *right, gboolean suppress)
Definition: xml.c:4100
int add_node_nocopy(xmlNode *parent, const char *name, xmlNode *child)
Definition: xml.c:2363
xmlNode * first_named_child(xmlNode *parent, const char *name)
Definition: xml.c:4899
xmlNode * subtract_xml_object(xmlNode *parent, xmlNode *left, xmlNode *right, gboolean full, gboolean *changed, const char *marker)
Definition: xml.c:4268
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
Definition: xml.c:1364
int write_xml_fd(xmlNode *xml_node, const char *filename, int fd, gboolean compress)
Definition: xml.c:3044
char * crm_xml_escape(const char *text)
Definition: xml.c:3098
gboolean cli_config_update(xmlNode **xml, int *best_version, gboolean to_logs)
Definition: schemas.c:853
void hash2nvpair(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4703
void fix_plus_plus_recursive(xmlNode *target)
Definition: xml.c:2254
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:1271
char * dump_xml_formatted_with_text(xmlNode *msg)
Definition: xml.c:3706
gboolean replace_xml_child(xmlNode *parent, xmlNode *child, xmlNode *update, gboolean delete_only)
Definition: xml.c:4620
const char * get_schema_name(int version)
Definition: schemas.c:684