pacemaker  1.1.17-b36b869ca8
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 /* Define compression parameters for IPC messages
41  *
42  * Compression costs a LOT, so we don't want to do it unless we're hitting
43  * message limits. Currently, we use 128KB as the threshold, because higher
44  * values don't play well with the heartbeat stack. With an earlier limit of
45  * 10KB, compressing 184 of 1071 messages accounted for 23% of the total CPU
46  * 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 
222 static inline const char *
223 crm_copy_xml_element(xmlNode *obj1, xmlNode *obj2, const char *element)
224 {
225  const char *value = crm_element_value(obj1, element);
226 
227  crm_xml_add(obj2, element, value);
228  return value;
229 }
230 
231 void xml_validate(const xmlNode * root);
232 
233 gboolean xml_has_children(const xmlNode * root);
234 
235 char *calculate_on_disk_digest(xmlNode * local_cib);
236 char *calculate_operation_digest(xmlNode * local_cib, const char *version);
237 char *calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
238  const char *version);
239 
240 /* schema-related functions (from schemas.c) */
241 gboolean validate_xml(xmlNode * xml_blob, const char *validation, gboolean to_logs);
242 gboolean validate_xml_verbose(xmlNode * xml_blob);
243 
281 int update_validation(xmlNode **xml_blob, int *best, int max,
282  gboolean transform, gboolean to_logs);
283 
284 int get_schema_version(const char *name);
285 const char *get_schema_name(int version);
286 const char *xml_latest_schema(void);
287 gboolean cli_config_update(xmlNode ** xml, int *best_version, gboolean to_logs);
288 
289 void crm_xml_init(void);
290 void crm_xml_cleanup(void);
291 
292 static inline xmlNode *
293 __xml_first_child(xmlNode * parent)
294 {
295  xmlNode *child = NULL;
296 
297  if (parent) {
298  child = parent->children;
299  while (child && child->type == XML_TEXT_NODE) {
300  child = child->next;
301  }
302  }
303  return child;
304 }
305 
306 static inline xmlNode *
307 __xml_next(xmlNode * child)
308 {
309  if (child) {
310  child = child->next;
311  while (child && child->type == XML_TEXT_NODE) {
312  child = child->next;
313  }
314  }
315  return child;
316 }
317 
318 static inline xmlNode *
319 __xml_first_child_element(xmlNode * parent)
320 {
321  xmlNode *child = NULL;
322 
323  if (parent) {
324  child = parent->children;
325  }
326 
327  while (child) {
328  if(child->type == XML_ELEMENT_NODE) {
329  return child;
330  }
331  child = child->next;
332  }
333  return NULL;
334 }
335 
336 static inline xmlNode *
337 __xml_next_element(xmlNode * child)
338 {
339  while (child) {
340  child = child->next;
341  if(child && child->type == XML_ELEMENT_NODE) {
342  return child;
343  }
344  }
345  return NULL;
346 }
347 
348 void free_xml(xmlNode * child);
349 
350 xmlNode *first_named_child(xmlNode * parent, const char *name);
351 
352 xmlNode *sorted_xml(xmlNode * input, xmlNode * parent, gboolean recursive);
353 xmlXPathObjectPtr xpath_search(xmlNode * xml_top, const char *path);
354 void crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
355  void (*helper)(xmlNode*, void*), void *user_data);
356 xmlNode *expand_idref(xmlNode * input, xmlNode * top);
357 
358 void freeXpathObject(xmlXPathObjectPtr xpathObj);
359 xmlNode *getXpathResult(xmlXPathObjectPtr xpathObj, int index);
360 void dedupXpathResults(xmlXPathObjectPtr xpathObj);
361 
362 static inline int numXpathResults(xmlXPathObjectPtr xpathObj)
363 {
364  if(xpathObj == NULL || xpathObj->nodesetval == NULL) {
365  return 0;
366  }
367  return xpathObj->nodesetval->nodeNr;
368 }
369 
370 bool xml_acl_enabled(xmlNode *xml);
371 void xml_acl_disable(xmlNode *xml);
372 bool xml_acl_denied(xmlNode *xml); /* Part or all of a change was rejected */
373 bool xml_acl_filtered_copy(const char *user, xmlNode* acl_source, xmlNode *xml, xmlNode ** result);
374 
375 bool xml_tracking_changes(xmlNode * xml);
376 bool xml_document_dirty(xmlNode *xml);
377 void xml_track_changes(xmlNode * xml, const char *user, xmlNode *acl_source, bool enforce_acls);
378 void xml_calculate_changes(xmlNode * old, xmlNode * new); /* For comparing two documents after the fact */
379 void xml_accept_changes(xmlNode * xml);
380 void xml_log_changes(uint8_t level, const char *function, xmlNode *xml);
381 void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml);
382 bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3]);
383 
384 xmlNode *xml_create_patchset(
385  int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version);
386 int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version);
387 
388 void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest);
389 
390 void save_xml_to_file(xmlNode * xml, const char *desc, const char *filename);
391 char *xml_get_path(xmlNode *xml);
392 
393 char * crm_xml_escape(const char *text);
394 void crm_xml_sanitize_id(char *id);
395 void crm_xml_set_id(xmlNode *xml, const char *format, ...)
396  __attribute__ ((__format__ (__printf__, 2, 3)));
397 
398 #endif
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
Definition: xml.c:2234
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:3187
GHashTable * xml2list(xmlNode *parent)
Definition: xml.c:4923
void xml_validate(const xmlNode *root)
int crm_element_value_const_int(const xmlNode *data, const char *name, int *dest)
Definition: xml.c:3881
void xml_calculate_changes(xmlNode *old, xmlNode *new)
Definition: xml.c:4211
void crm_xml_cleanup(void)
Definition: xml.c:5086
xmlNode * find_entity(xmlNode *parent, const char *node_name, const char *id)
Definition: xml.c:2267
void xml_track_changes(xmlNode *xml, const char *user, xmlNode *acl_source, bool enforce_acls)
Definition: xml.c:854
bool xml_acl_denied(xmlNode *xml)
Definition: xml.c:819
void crm_xml_init(void)
Definition: xml.c:5064
int write_xml_file(xmlNode *xml_node, const char *filename, gboolean compress)
Definition: xml.c:3177
char * crm_element_value_copy(xmlNode *data, const char *name)
Definition: xml.c:3893
int get_schema_version(const char *name)
Definition: schemas.c:698
void print_xml_diff(FILE *where, xmlNode *diff)
void copy_in_properties(xmlNode *target, xmlNode *src)
Definition: xml.c:2286
void xml_accept_changes(xmlNode *xml)
Definition: xml.c:1570
xmlNode * filename2xml(const char *filename)
Definition: xml.c:2958
void dedupXpathResults(xmlXPathObjectPtr xpathObj)
Definition: xpath.c:107
void purge_diff_markers(xmlNode *a_node)
Definition: xml.c:3926
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:2774
void crm_xml_sanitize_id(char *id)
Sanitize a string so it is usable as an XML ID.
Definition: xml.c:3045
char version[256]
Definition: plugin.c:84
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
Definition: schemas.c:606
xmlDoc * getDocPtr(xmlNode *node)
Definition: xml.c:2388
bool xml_acl_enabled(xmlNode *xml)
Definition: xml.c:843
const char * crm_xml_replace(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2535
gboolean validate_xml_verbose(xmlNode *xml_blob)
Definition: schemas.c:578
void expand_plus_plus(xmlNode *target, const char *name, const char *value)
Definition: xml.c:2327
bool xml_tracking_changes(xmlNode *xml)
Definition: xml.c:869
void hash2smartfield(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4861
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:2711
void hash2field(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4884
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:2839
int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version)
Definition: xml.c:2157
gboolean update_xml_child(xmlNode *child, xmlNode *to_update)
Definition: xml.c:4696
enum crm_proc_flag __attribute__
xmlNode * add_node_copy(xmlNode *new_parent, xmlNode *xml_node)
Definition: xml.c:2404
const char * crm_element_value_const(const xmlNode *data, const char *name)
Definition: xml.c:3887
xmlNode * expand_idref(xmlNode *input, xmlNode *top)
Definition: xml.c:5094
bool xml_document_dirty(xmlNode *xml)
Definition: xml.c:880
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:2587
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
Definition: xml.c:3868
char * dump_xml_formatted(xmlNode *msg)
Definition: xml.c:3839
const char * crm_element_value(xmlNode *data, const char *name)
Definition: xml.c:5134
void xml_acl_disable(xmlNode *xml)
Definition: xml.c:830
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
Definition: xml.c:3195
void free_xml(xmlNode *child)
Definition: xml.c:2705
gboolean xml_has_children(const xmlNode *root)
Definition: xml.c:3859
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:5002
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:1351
void hash2metafield(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4900
int find_xml_children(xmlNode **children, xmlNode *root, const char *tag, const char *field, const char *value, gboolean search_matches)
Definition: xml.c:4730
bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3])
Definition: xml.c:1762
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2489
void xml_log_changes(uint8_t level, const char *function, xmlNode *xml)
Definition: xml.c:1539
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Definition: xml.c:2577
gboolean apply_xml_diff(xmlNode *old, xmlNode *diff, xmlNode **new)
Definition: xml.c:3957
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:3905
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:4252
char * xml_get_path(xmlNode *xml)
Definition: xml.c:2630
char * dump_xml_unformatted(xmlNode *msg)
Definition: xml.c:3849
bool xml_acl_filtered_copy(const char *user, xmlNode *acl_source, xmlNode *xml, xmlNode **result)
Definition: xml.c:698
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:3939
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:715
xmlNode * diff_xml_object(xmlNode *left, xmlNode *right, gboolean suppress)
Definition: xml.c:4224
int add_node_nocopy(xmlNode *parent, const char *name, xmlNode *child)
Definition: xml.c:2418
xmlNode * first_named_child(xmlNode *parent, const char *name)
Definition: xml.c:5046
xmlNode * subtract_xml_object(xmlNode *parent, xmlNode *left, xmlNode *right, gboolean full, gboolean *changed, const char *marker)
Definition: xml.c:4410
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
Definition: xml.c:1384
int write_xml_fd(xmlNode *xml_node, const char *filename, int fd, gboolean compress)
Definition: xml.c:3167
char * crm_xml_escape(const char *text)
Definition: xml.c:3221
gboolean cli_config_update(xmlNode **xml, int *best_version, gboolean to_logs)
Definition: schemas.c:858
void hash2nvpair(gpointer key, gpointer value, gpointer user_data)
Definition: xml.c:4845
void fix_plus_plus_recursive(xmlNode *target)
Definition: xml.c:2309
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:1291
char * dump_xml_formatted_with_text(xmlNode *msg)
Definition: xml.c:3829
gboolean replace_xml_child(xmlNode *parent, xmlNode *child, xmlNode *update, gboolean delete_only)
Definition: xml.c:4762
const char * get_schema_name(int version)
Definition: schemas.c:689