pacemaker  1.1.17-b36b869ca8
Scalable High-Availability cluster resource manager
unpack.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This library 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.1 of the License, or (at your option) any later version.
8  *
9  * This library 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  * Lesser 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 
19 #include <crm_internal.h>
20 
21 #include <sys/param.h>
22 #include <crm/crm.h>
23 #include <crm/msg_xml.h>
24 
25 #include <crm/common/xml.h>
26 #include <crm/transition.h>
27 #include <sys/stat.h>
28 
29 CRM_TRACE_INIT_DATA(transitioner);
30 
31 static crm_action_t *
32 unpack_action(synapse_t * parent, xmlNode * xml_action)
33 {
34  crm_action_t *action = NULL;
35  const char *value = crm_element_value(xml_action, XML_ATTR_ID);
36 
37  if (value == NULL) {
38  crm_err("Actions must have an id!");
39  crm_log_xml_trace(xml_action, "Action with missing id");
40  return NULL;
41  }
42 
43  action = calloc(1, sizeof(crm_action_t));
44  if (action == NULL) {
45  crm_perror(LOG_CRIT, "Cannot unpack action");
46  crm_log_xml_trace(xml_action, "Lost action");
47  return NULL;
48  }
49 
50  action->id = crm_parse_int(value, NULL);
51  action->type = action_type_rsc;
52  action->xml = copy_xml(xml_action);
53  action->synapse = parent;
54 
55  if (safe_str_eq(crm_element_name(action->xml), XML_GRAPH_TAG_RSC_OP)) {
56  action->type = action_type_rsc;
57 
58  } else if (safe_str_eq(crm_element_name(action->xml), XML_GRAPH_TAG_PSEUDO_EVENT)) {
59  action->type = action_type_pseudo;
60 
61  } else if (safe_str_eq(crm_element_name(action->xml), XML_GRAPH_TAG_CRM_EVENT)) {
62  action->type = action_type_crm;
63  }
64 
65  action->params = xml2list(action->xml);
66 
67  value = g_hash_table_lookup(action->params, "CRM_meta_timeout");
68  if (value != NULL) {
69  action->timeout = crm_parse_int(value, NULL);
70  }
71 
72  /* Take start-delay into account for the timeout of the action timer */
73  value = g_hash_table_lookup(action->params, "CRM_meta_start_delay");
74  if (value != NULL) {
75  action->timeout += crm_parse_int(value, NULL);
76  }
77 
78  value = g_hash_table_lookup(action->params, "CRM_meta_interval");
79  if (value != NULL) {
80  action->interval = crm_parse_int(value, NULL);
81  }
82 
83  value = g_hash_table_lookup(action->params, "CRM_meta_can_fail");
84  if (value != NULL) {
85  crm_str_to_boolean(value, &(action->can_fail));
86  }
87 
88  crm_trace("Action %d has timer set to %dms", action->id, action->timeout);
89 
90  return action;
91 }
92 
93 static synapse_t *
94 unpack_synapse(crm_graph_t * new_graph, xmlNode * xml_synapse)
95 {
96  const char *value = NULL;
97  xmlNode *inputs = NULL;
98  xmlNode *action_set = NULL;
99  synapse_t *new_synapse = NULL;
100 
101  CRM_CHECK(xml_synapse != NULL, return NULL);
102  crm_trace("looking in synapse %s", ID(xml_synapse));
103 
104  new_synapse = calloc(1, sizeof(synapse_t));
105  new_synapse->id = crm_parse_int(ID(xml_synapse), NULL);
106 
107  value = crm_element_value(xml_synapse, XML_CIB_ATTR_PRIORITY);
108  if (value != NULL) {
109  new_synapse->priority = crm_parse_int(value, NULL);
110  }
111 
112  new_graph->num_synapses++;
113  CRM_CHECK(new_synapse->id >= 0, free(new_synapse);
114  return NULL);
115 
116  crm_trace("look for actions in synapse %s", crm_element_value(xml_synapse, XML_ATTR_ID));
117 
118  for (action_set = __xml_first_child(xml_synapse); action_set != NULL;
119  action_set = __xml_next(action_set)) {
120  if (crm_str_eq((const char *)action_set->name, "action_set", TRUE)) {
121  xmlNode *action = NULL;
122 
123  for (action = __xml_first_child(action_set); action != NULL;
124  action = __xml_next(action)) {
125  crm_action_t *new_action = unpack_action(new_synapse, action);
126 
127  new_graph->num_actions++;
128 
129  if (new_action == NULL) {
130  continue;
131  }
132  crm_trace("Adding action %d to synapse %d", new_action->id, new_synapse->id);
133 
134  new_synapse->actions = g_list_append(new_synapse->actions, new_action);
135  }
136  }
137  }
138 
139  crm_trace("look for inputs in synapse %s", ID(xml_synapse));
140 
141  for (inputs = __xml_first_child(xml_synapse); inputs != NULL; inputs = __xml_next(inputs)) {
142  if (crm_str_eq((const char *)inputs->name, "inputs", TRUE)) {
143  xmlNode *trigger = NULL;
144 
145  for (trigger = __xml_first_child(inputs); trigger != NULL;
146  trigger = __xml_next(trigger)) {
147  xmlNode *input = NULL;
148 
149  for (input = __xml_first_child(trigger); input != NULL; input = __xml_next(input)) {
150  crm_action_t *new_input = unpack_action(new_synapse, input);
151 
152  if (new_input == NULL) {
153  continue;
154  }
155 
156  crm_trace("Adding input %d to synapse %d", new_input->id, new_synapse->id);
157 
158  new_synapse->inputs = g_list_append(new_synapse->inputs, new_input);
159  }
160  }
161  }
162  }
163 
164  return new_synapse;
165 }
166 
167 crm_graph_t *
168 unpack_graph(xmlNode * xml_graph, const char *reference)
169 {
170 /*
171  <transition_graph>
172  <synapse>
173  <action_set>
174  <rsc_op id="2"
175  ...
176  <inputs>
177  <rsc_op id="2"
178  ...
179 */
180  crm_graph_t *new_graph = NULL;
181  const char *t_id = NULL;
182  const char *time = NULL;
183  xmlNode *synapse = NULL;
184 
185  new_graph = calloc(1, sizeof(crm_graph_t));
186 
187  new_graph->id = -1;
188  new_graph->abort_priority = 0;
189  new_graph->network_delay = -1;
190  new_graph->transition_timeout = -1;
191  new_graph->stonith_timeout = -1;
192  new_graph->completion_action = tg_done;
193 
194  if (reference) {
195  new_graph->source = strdup(reference);
196  } else {
197  new_graph->source = strdup("unknown");
198  }
199 
200  if (xml_graph != NULL) {
201  t_id = crm_element_value(xml_graph, "transition_id");
202  CRM_CHECK(t_id != NULL, free(new_graph);
203  return NULL);
204  new_graph->id = crm_parse_int(t_id, "-1");
205 
206  time = crm_element_value(xml_graph, "cluster-delay");
207  CRM_CHECK(time != NULL, free(new_graph);
208  return NULL);
209  new_graph->network_delay = crm_get_msec(time);
210 
211  time = crm_element_value(xml_graph, "stonith-timeout");
212  if (time == NULL) {
213  new_graph->stonith_timeout = new_graph->network_delay;
214  } else {
215  new_graph->stonith_timeout = crm_get_msec(time);
216  }
217 
218  t_id = crm_element_value(xml_graph, "batch-limit");
219  new_graph->batch_limit = crm_parse_int(t_id, "0");
220 
221  t_id = crm_element_value(xml_graph, "migration-limit");
222  new_graph->migration_limit = crm_parse_int(t_id, "-1");
223  }
224 
225  for (synapse = __xml_first_child(xml_graph); synapse != NULL; synapse = __xml_next(synapse)) {
226  if (crm_str_eq((const char *)synapse->name, "synapse", TRUE)) {
227  synapse_t *new_synapse = unpack_synapse(new_graph, synapse);
228 
229  if (new_synapse != NULL) {
230  new_graph->synapses = g_list_append(new_graph->synapses, new_synapse);
231  }
232  }
233  }
234 
235  crm_debug("Unpacked transition %d: %d actions in %d synapses",
236  new_graph->id, new_graph->num_actions, new_graph->num_synapses);
237 
238  return new_graph;
239 }
240 
241 static void
242 destroy_action(crm_action_t * action)
243 {
244  if (action->timer && action->timer->source_id != 0) {
245  crm_warn("Cancelling timer for action %d (src=%d)", action->id, action->timer->source_id);
246  g_source_remove(action->timer->source_id);
247  }
248  if (action->params) {
249  g_hash_table_destroy(action->params);
250  }
251  free_xml(action->xml);
252  free(action->timer);
253  free(action);
254 }
255 
256 static void
257 destroy_synapse(synapse_t * synapse)
258 {
259  while (g_list_length(synapse->actions) > 0) {
260  crm_action_t *action = g_list_nth_data(synapse->actions, 0);
261 
262  synapse->actions = g_list_remove(synapse->actions, action);
263  destroy_action(action);
264  }
265 
266  while (g_list_length(synapse->inputs) > 0) {
267  crm_action_t *action = g_list_nth_data(synapse->inputs, 0);
268 
269  synapse->inputs = g_list_remove(synapse->inputs, action);
270  destroy_action(action);
271  }
272  free(synapse);
273 }
274 
275 void
277 {
278  if (graph == NULL) {
279  return;
280  }
281  while (g_list_length(graph->synapses) > 0) {
282  synapse_t *synapse = g_list_nth_data(graph->synapses, 0);
283 
284  graph->synapses = g_list_remove(graph->synapses, synapse);
285  destroy_synapse(synapse);
286  }
287 
288  free(graph->source);
289  free(graph);
290 }
291 
293 convert_graph_action(xmlNode * resource, crm_action_t * action, int status, int rc)
294 {
295  xmlNode *xop = NULL;
296  lrmd_event_data_t *op = NULL;
297  GHashTableIter iter;
298  const char *name = NULL;
299  const char *value = NULL;
300  xmlNode *action_resource = NULL;
301 
302  CRM_CHECK(action != NULL, return NULL);
303  CRM_CHECK(action->type == action_type_rsc, return NULL);
304 
305  action_resource = first_named_child(action->xml, XML_CIB_TAG_RESOURCE);
306  CRM_CHECK(action_resource != NULL, crm_log_xml_warn(action->xml, "Bad");
307  return NULL);
308 
309  op = calloc(1, sizeof(lrmd_event_data_t));
310 
311  op->rsc_id = strdup(ID(action_resource));
312  op->interval = action->interval;
313  op->op_type = strdup(crm_element_value(action->xml, XML_LRM_ATTR_TASK));
314 
315  op->rc = rc;
316  op->op_status = status;
317  op->t_run = time(NULL);
318  op->t_rcchange = op->t_run;
319 
320  op->params = g_hash_table_new_full(crm_str_hash, g_str_equal,
322 
323  g_hash_table_iter_init(&iter, action->params);
324  while (g_hash_table_iter_next(&iter, (void **)&name, (void **)&value)) {
325  g_hash_table_insert(op->params, strdup(name), strdup(value));
326  }
327 
328  for (xop = __xml_first_child(resource); xop != NULL; xop = __xml_next(xop)) {
329  int tmp = 0;
330 
332  crm_debug("Got call_id=%d for %s", tmp, ID(resource));
333  if (tmp > op->call_id) {
334  op->call_id = tmp;
335  }
336  }
337 
338  op->call_id++;
339  return op;
340 }
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:164
GListPtr actions
Definition: transition.h:42
A dumping ground.
GHashTable * xml2list(xmlNode *parent)
Definition: xml.c:4923
action_type_e type
Definition: transition.h:51
enum transition_action completion_action
Definition: transition.h:98
const char * rsc_id
Definition: lrmd.h:197
xmlNode * xml
Definition: transition.h:63
long long crm_get_msec(const char *input)
Definition: utils.c:586
gboolean can_fail
Definition: transition.h:61
#define XML_GRAPH_TAG_RSC_OP
Definition: msg_xml.h:301
int crm_parse_int(const char *text, const char *default_text)
Definition: strings.c:125
unsigned int t_rcchange
Definition: lrmd.h:223
#define XML_GRAPH_TAG_CRM_EVENT
Definition: msg_xml.h:303
int num_synapses
Definition: transition.h:101
enum ocf_exitcode rc
Definition: lrmd.h:215
GHashTable * params
Definition: transition.h:50
crm_action_timer_t * timer
Definition: transition.h:53
int transition_timeout
Definition: transition.h:106
#define XML_GRAPH_TAG_PSEUDO_EVENT
Definition: msg_xml.h:302
#define XML_CIB_ATTR_PRIORITY
Definition: msg_xml.h:252
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:2711
int priority
Definition: transition.h:35
#define XML_LRM_ATTR_TASK
Definition: msg_xml.h:272
void * params
Definition: lrmd.h:234
#define crm_warn(fmt, args...)
Definition: logging.h:249
#define crm_debug(fmt, args...)
Definition: logging.h:253
#define XML_ATTR_ID
Definition: msg_xml.h:101
#define XML_CIB_TAG_RESOURCE
Definition: msg_xml.h:188
#define crm_trace(fmt, args...)
Definition: logging.h:254
Wrappers for and extensions to libxml2.
#define crm_log_xml_warn(xml, text)
Definition: logging.h:258
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
Definition: xml.c:3868
const char * crm_element_value(xmlNode *data, const char *name)
Definition: xml.c:5134
GListPtr synapses
Definition: transition.h:114
crm_graph_t * unpack_graph(xmlNode *xml_graph, const char *reference)
Definition: unpack.c:168
void free_xml(xmlNode *child)
Definition: xml.c:2705
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:213
int stonith_timeout
Definition: transition.h:105
const char * op_type
Definition: lrmd.h:199
int batch_limit
Definition: transition.h:103
unsigned int t_run
Definition: lrmd.h:221
GListPtr inputs
Definition: transition.h:43
char * source
Definition: transition.h:93
int network_delay
Definition: transition.h:104
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:176
int num_actions
Definition: transition.h:100
#define crm_perror(level, fmt, args...)
Log a system error message.
Definition: logging.h:226
int migration_limit
Definition: transition.h:116
#define crm_err(fmt, args...)
Definition: logging.h:248
int abort_priority
Definition: transition.h:94
#define XML_LRM_ATTR_CALLID
Definition: msg_xml.h:284
int source_id
Definition: transition.h:77
xmlNode * first_named_child(xmlNode *parent, const char *name)
Definition: xml.c:5046
#define crm_log_xml_trace(xml, text)
Definition: logging.h:262
CRM_TRACE_INIT_DATA(pe_status)
#define ID(x)
Definition: msg_xml.h:434
#define safe_str_eq(a, b)
Definition: util.h:64
#define crm_str_hash
Definition: crm.h:208
void destroy_graph(crm_graph_t *graph)
Definition: unpack.c:276
void g_hash_destroy_str(gpointer data)
Definition: strings.c:74
synapse_t * synapse
Definition: transition.h:54
lrmd_event_data_t * convert_graph_action(xmlNode *resource, crm_action_t *action, int status, int rc)
Definition: unpack.c:293