pacemaker  2.0.2-debe490
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cib_native.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004 International Business Machines
3  *
4  * This source code is licensed under the GNU Lesser General Public License
5  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
6  */
7 
8 #include <crm_internal.h>
9 
10 #ifndef _GNU_SOURCE
11 # define _GNU_SOURCE
12 #endif
13 
14 #include <bzlib.h>
15 #include <errno.h>
16 #include <crm_internal.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <string.h>
22 
23 #include <glib.h>
24 
25 #include <crm/crm.h>
26 #include <crm/cib/internal.h>
27 
28 #include <crm/msg_xml.h>
29 #include <crm/common/mainloop.h>
30 
31 typedef struct cib_native_opaque_s {
32  char *token;
33  crm_ipc_t *ipc;
34  void (*dnotify_fn) (gpointer user_data);
35  mainloop_io_t *source;
36 
38 
39 int cib_native_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
40  xmlNode * data, xmlNode ** output_data, int call_options);
41 
42 int cib_native_perform_op_delegate(cib_t * cib, const char *op, const char *host,
43  const char *section, xmlNode * data, xmlNode ** output_data,
44  int call_options, const char *user_name);
45 
46 int cib_native_free(cib_t * cib);
47 int cib_native_signoff(cib_t * cib);
48 int cib_native_signon(cib_t * cib, const char *name, enum cib_conn_type type);
49 int cib_native_signon_raw(cib_t * cib, const char *name, enum cib_conn_type type, int *event_fd);
50 
51 bool cib_native_dispatch(cib_t * cib);
52 
53 int cib_native_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data));
54 
55 cib_t *
57 {
58  cib_native_opaque_t *native = NULL;
59  cib_t *cib = cib_new_variant();
60 
61  native = calloc(1, sizeof(cib_native_opaque_t));
62 
63  cib->variant = cib_native;
64  cib->variant_opaque = native;
65 
66  native->ipc = NULL;
67  native->source = NULL;
68  native->dnotify_fn = NULL;
69 
70  /* assign variant specific ops */
75  cib->cmds->free = cib_native_free;
76 
79 
80  return cib;
81 }
82 
83 int
84 cib_native_signon(cib_t * cib, const char *name, enum cib_conn_type type)
85 {
86  return cib_native_signon_raw(cib, name, type, NULL);
87 }
88 
89 static int
90 cib_native_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata)
91 {
92  const char *type = NULL;
93  xmlNode *msg = NULL;
94 
95  cib_t *cib = userdata;
96 
97  crm_trace("dispatching %p", userdata);
98 
99  if (cib == NULL) {
100  crm_err("No CIB!");
101  return 0;
102  }
103 
104  msg = string2xml(buffer);
105 
106  if (msg == NULL) {
107  crm_warn("Received a NULL message from the CIB manager");
108  return 0;
109  }
110 
111  /* do callbacks */
112  type = crm_element_value(msg, F_TYPE);
113  crm_trace("Activating %s callbacks...", type);
114  crm_log_xml_explicit(msg, "cib-reply");
115 
116  if (safe_str_eq(type, T_CIB)) {
117  cib_native_callback(cib, msg, 0, 0);
118 
119  } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
120  g_list_foreach(cib->notify_list, cib_native_notify, msg);
121 
122  } else {
123  crm_err("Unknown message type: %s", type);
124  }
125 
126  free_xml(msg);
127  return 0;
128 }
129 
130 bool
132 {
133  gboolean stay_connected = TRUE;
134  cib_native_opaque_t *native;
135 
136  if (cib == NULL) {
137  crm_err("No CIB!");
138  return FALSE;
139  }
140 
141  crm_trace("dispatching %p", cib);
142  native = cib->variant_opaque;
143  while (crm_ipc_ready(native->ipc)) {
144 
145  if (crm_ipc_read(native->ipc) > 0) {
146  const char *msg = crm_ipc_buffer(native->ipc);
147 
148  cib_native_dispatch_internal(msg, strlen(msg), cib);
149  }
150 
151  if (crm_ipc_connected(native->ipc) == FALSE) {
152  crm_err("Connection closed");
153  stay_connected = FALSE;
154  }
155  }
156 
157  return stay_connected;
158 }
159 
160 static void
161 cib_native_destroy(void *userdata)
162 {
163  cib_t *cib = userdata;
164  cib_native_opaque_t *native = cib->variant_opaque;
165 
166  crm_trace("destroying %p", userdata);
167  cib->state = cib_disconnected;
168  native->source = NULL;
169  native->ipc = NULL;
170 
171  if (native->dnotify_fn) {
172  native->dnotify_fn(userdata);
173  }
174 }
175 
176 int
177 cib_native_signon_raw(cib_t * cib, const char *name, enum cib_conn_type type, int *async_fd)
178 {
179  int rc = pcmk_ok;
180  const char *channel = NULL;
181  cib_native_opaque_t *native = cib->variant_opaque;
182 
183  static struct ipc_client_callbacks cib_callbacks = {
184  .dispatch = cib_native_dispatch_internal,
185  .destroy = cib_native_destroy
186  };
187 
189 
190  if (type == cib_command) {
192  channel = CIB_CHANNEL_RW;
193 
194  } else if (type == cib_command_nonblocking) {
196  channel = CIB_CHANNEL_SHM;
197 
198  } else if (type == cib_query) {
199  cib->state = cib_connected_query;
200  channel = CIB_CHANNEL_RO;
201 
202  } else {
203  return -ENOTCONN;
204  }
205 
206  crm_trace("Connecting %s channel", channel);
207 
208  if (async_fd != NULL) {
209  native->ipc = crm_ipc_new(channel, 0);
210 
211  if (native->ipc && crm_ipc_connect(native->ipc)) {
212  *async_fd = crm_ipc_get_fd(native->ipc);
213 
214  } else if (native->ipc) {
215  crm_perror(LOG_ERR, "Connection to cluster information base failed");
216  rc = -ENOTCONN;
217  }
218 
219  } else {
220  native->source =
221  mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 512 * 1024 /* 512k */ , cib,
222  &cib_callbacks);
223  native->ipc = mainloop_get_ipc_client(native->source);
224  }
225 
226  if (rc != pcmk_ok || native->ipc == NULL || crm_ipc_connected(native->ipc) == FALSE) {
227  crm_debug("Connection unsuccessful (%d %p)", rc, native->ipc);
228  rc = -ENOTCONN;
229  }
230 
231  if (rc == pcmk_ok) {
232  xmlNode *reply = NULL;
233  xmlNode *hello = create_xml_node(NULL, "cib_command");
234 
235  crm_xml_add(hello, F_TYPE, T_CIB);
237  crm_xml_add(hello, F_CIB_CLIENTNAME, name);
239 
240  if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1, &reply) > 0) {
241  const char *msg_type = crm_element_value(reply, F_CIB_OPERATION);
242 
243  rc = pcmk_ok;
244  crm_log_xml_trace(reply, "reg-reply");
245 
246  if (safe_str_neq(msg_type, CRM_OP_REGISTER)) {
247  crm_err("Invalid registration message: %s", msg_type);
248  rc = -EPROTO;
249 
250  } else {
251  native->token = crm_element_value_copy(reply, F_CIB_CLIENTID);
252  if (native->token == NULL) {
253  rc = -EPROTO;
254  }
255  }
256  free_xml(reply);
257 
258  } else {
259  rc = -ECOMM;
260  }
261 
262  free_xml(hello);
263  }
264 
265  if (rc == pcmk_ok) {
266  crm_debug("Connection to CIB manager successful");
267  return pcmk_ok;
268  }
269 
270  crm_debug("Connection to CIB manager failed: %s", pcmk_strerror(rc));
271  cib_native_signoff(cib);
272  return rc;
273 }
274 
275 int
277 {
278  cib_native_opaque_t *native = cib->variant_opaque;
279 
280  crm_debug("Disconnecting from the CIB manager");
281 
282  if (native->source != NULL) {
283  /* Attached to mainloop */
284  mainloop_del_ipc_client(native->source);
285  native->source = NULL;
286  native->ipc = NULL;
287 
288  } else if (native->ipc) {
289  /* Not attached to mainloop */
290  crm_ipc_t *ipc = native->ipc;
291 
292  native->ipc = NULL;
293  crm_ipc_close(ipc);
294  crm_ipc_destroy(ipc);
295  }
296 
297  cib->state = cib_disconnected;
298  cib->type = cib_no_connection;
299 
300  return pcmk_ok;
301 }
302 
303 int
305 {
306  int rc = pcmk_ok;
307 
308  if (cib->state != cib_disconnected) {
309  rc = cib_native_signoff(cib);
310  }
311 
312  if (cib->state == cib_disconnected) {
313  cib_native_opaque_t *native = cib->variant_opaque;
314 
315  free(native->token);
316  free(cib->variant_opaque);
317  free(cib->cmds);
318  free(cib);
319  }
320 
321  return rc;
322 }
323 
324 int
325 cib_native_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
326  xmlNode * data, xmlNode ** output_data, int call_options)
327 {
328  return cib_native_perform_op_delegate(cib, op, host, section,
329  data, output_data, call_options, NULL);
330 }
331 
332 int
333 cib_native_perform_op_delegate(cib_t * cib, const char *op, const char *host, const char *section,
334  xmlNode * data, xmlNode ** output_data, int call_options,
335  const char *user_name)
336 {
337  int rc = pcmk_ok;
338  int reply_id = 0;
339  enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
340 
341  xmlNode *op_msg = NULL;
342  xmlNode *op_reply = NULL;
343 
344  cib_native_opaque_t *native = cib->variant_opaque;
345 
346  if (cib->state == cib_disconnected) {
347  return -ENOTCONN;
348  }
349 
350  if (output_data != NULL) {
351  *output_data = NULL;
352  }
353 
354  if (op == NULL) {
355  crm_err("No operation specified");
356  return -EINVAL;
357  }
358 
359  if (call_options & cib_sync_call) {
360  ipc_flags |= crm_ipc_client_response;
361  }
362 
363  cib->call_id++;
364  /* prevent call_id from being negative (or zero) and conflicting
365  * with the cib_errors enum
366  * use 2 because we use it as (cib->call_id - 1) below
367  */
368  if (cib->call_id < 1) {
369  cib->call_id = 1;
370  }
371 
372  CRM_CHECK(native->token != NULL,;
373  );
374  op_msg =
375  cib_create_op(cib->call_id, native->token, op, host, section, data, call_options,
376  user_name);
377  if (op_msg == NULL) {
378  return -EPROTO;
379  }
380 
381  crm_trace("Sending %s message to the CIB manager (timeout=%ds)", op, cib->call_timeout);
382  rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
383  free_xml(op_msg);
384 
385  if (rc < 0) {
386  crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
387  cib->call_timeout, pcmk_strerror(rc), rc);
388  rc = -ECOMM;
389  goto done;
390  }
391 
392  crm_log_xml_trace(op_reply, "Reply");
393 
394  if (!(call_options & cib_sync_call)) {
395  crm_trace("Async call, returning %d", cib->call_id);
396  CRM_CHECK(cib->call_id != 0, return -ENOMSG);
397  free_xml(op_reply);
398  return cib->call_id;
399  }
400 
401  rc = pcmk_ok;
402  crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
403  if (reply_id == cib->call_id) {
404  xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
405 
406  crm_trace("Synchronous reply %d received", reply_id);
407  if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
408  rc = -EPROTO;
409  }
410 
411  if (output_data == NULL || (call_options & cib_discard_reply)) {
412  crm_trace("Discarding reply");
413 
414  } else if (tmp != NULL) {
415  *output_data = copy_xml(tmp);
416  }
417 
418  } else if (reply_id <= 0) {
419  crm_err("Received bad reply: No id set");
420  crm_log_xml_err(op_reply, "Bad reply");
421  rc = -ENOMSG;
422  goto done;
423 
424  } else {
425  crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
426  crm_log_xml_err(op_reply, "Old reply");
427  rc = -ENOMSG;
428  goto done;
429  }
430 
431  if (op_reply == NULL && cib->state == cib_disconnected) {
432  rc = -ENOTCONN;
433 
434  } else if (rc == pcmk_ok && op_reply == NULL) {
435  rc = -ETIME;
436  }
437 
438  switch (rc) {
439  case pcmk_ok:
440  case -EPERM:
441  break;
442 
443  /* This is an internal value that clients do not and should not care about */
444  case -pcmk_err_diff_resync:
445  rc = pcmk_ok;
446  break;
447 
448  /* These indicate internal problems */
449  case -EPROTO:
450  case -ENOMSG:
451  crm_err("Call failed: %s", pcmk_strerror(rc));
452  if (op_reply) {
453  crm_log_xml_err(op_reply, "Invalid reply");
454  }
455  break;
456 
457  default:
458  if (safe_str_neq(op, CIB_OP_QUERY)) {
459  crm_warn("Call failed: %s", pcmk_strerror(rc));
460  }
461  }
462 
463  done:
464  if (crm_ipc_connected(native->ipc) == FALSE) {
465  crm_err("The CIB manager disconnected");
466  cib->state = cib_disconnected;
467  }
468 
469  free_xml(op_reply);
470  return rc;
471 }
472 
473 int
474 cib_native_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data))
475 {
476  cib_native_opaque_t *native = NULL;
477 
478  if (cib == NULL) {
479  crm_err("No CIB!");
480  return FALSE;
481  }
482 
483  native = cib->variant_opaque;
484  native->dnotify_fn = dnotify;
485 
486  return pcmk_ok;
487 }
488 
489 int
490 cib_native_register_notification(cib_t * cib, const char *callback, int enabled)
491 {
492  int rc = pcmk_ok;
493  xmlNode *notify_msg = create_xml_node(NULL, "cib-callback");
494  cib_native_opaque_t *native = cib->variant_opaque;
495 
496  if (cib->state != cib_disconnected) {
498  crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
499  crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
500  rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
501  1000 * cib->call_timeout, NULL);
502  if (rc <= 0) {
503  crm_trace("Notification not registered: %d", rc);
504  rc = -ECOMM;
505  }
506  }
507 
508  free_xml(notify_msg);
509  return rc;
510 }
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:156
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
Definition: ipc.c:955
A dumping ground.
#define F_TYPE
Definition: msg_xml.h:30
xmlNode * get_message_xml(xmlNode *msg, const char *field)
Definition: xml.c:2504
const char * pcmk_strerror(int rc)
Definition: results.c:188
#define ETIME
Definition: portability.h:162
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:157
int call_timeout
Definition: cib.h:150
#define CIB_CHANNEL_SHM
Definition: internal.h:71
#define CIB_CHANNEL_RO
Definition: internal.h:69
int crm_ipc_get_fd(crm_ipc_t *client)
Definition: ipc.c:1061
int(* free)(cib_t *cib)
Definition: cib.h:85
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
Definition: nvpair.c:383
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:32
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:275
#define MAX_IPC_DELAY
Definition: crm.h:56
AIS_Host host
Definition: internal.h:86
int(* signon_raw)(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition: cib.h:83
long crm_ipc_read(crm_ipc_t *client)
Definition: ipc.c:1170
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition: cib.h:96
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:459
Wrappers for and extensions to glib mainloop.
#define CRM_OP_REGISTER
Definition: crm.h:120
xmlNode * string2xml(const char *input)
Definition: xml.c:2058
const char * crm_ipc_buffer(crm_ipc_t *client)
Definition: ipc.c:1217
#define F_CIB_NOTIFY_ACTIVATE
Definition: internal.h:54
xmlNode * cib_create_op(int call_id, const char *token, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
Definition: cib_utils.c:512
void cib_native_notify(gpointer data, gpointer user_data)
Definition: cib_utils.c:594
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:2020
int cib_native_set_connection_dnotify(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition: cib_native.c:474
cib_t * cib_new_variant(void)
Definition: cib_client.c:343
#define crm_warn(fmt, args...)
Definition: logging.h:241
Definition: cib.h:48
cib_api_operations_t * cmds
Definition: cib.h:157
#define crm_debug(fmt, args...)
Definition: logging.h:245
#define F_CIB_RC
Definition: internal.h:40
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:58
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
Definition: nvpair.c:556
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:423
#define F_CIB_OPERATION
Definition: internal.h:36
#define F_CIB_CLIENTNAME
Definition: internal.h:52
int cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
Definition: cib_native.c:490
#define CIB_OP_QUERY
Definition: internal.h:22
int cib_native_signoff(cib_t *cib)
Definition: cib_native.c:276
#define crm_trace(fmt, args...)
Definition: logging.h:246
#define crm_log_xml_explicit(xml, text)
Definition: logging.h:256
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition: cib.h:129
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:1890
#define ECOMM
Definition: portability.h:138
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:849
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc.c:1038
cib_t * cib_native_new(void)
Definition: cib_native.c:56
void free_xml(xmlNode *child)
Definition: xml.c:2014
#define CIB_CHANNEL_RW
Definition: internal.h:70
int cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
Definition: cib_native.c:84
#define T_CIB
Definition: internal.h:61
void * variant_opaque
Definition: cib.h:151
#define F_CIB_NOTIFY_TYPE
Definition: internal.h:53
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc.c:1075
int cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options, const char *user_name)
Definition: cib_native.c:333
#define T_CIB_NOTIFY
Definition: internal.h:62
int cib_native_free(cib_t *cib)
Definition: cib_native.c:304
int crm_ipc_ready(crm_ipc_t *client)
Check whether an IPC connection is ready to be read.
Definition: ipc.c:1107
cib_conn_type
Definition: cib.h:46
#define pcmk_err_diff_resync
Definition: results.h:66
#define F_CIB_CALLOPTS
Definition: internal.h:33
#define crm_log_xml_err(xml, text)
Definition: logging.h:249
#define crm_perror(level, fmt, args...)
Log a system error message.
Definition: logging.h:218
#define F_CIB_CALLDATA
Definition: internal.h:35
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:855
#define crm_err(fmt, args...)
Definition: logging.h:240
int cib_native_perform_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options)
Definition: cib_native.c:325
struct cib_native_opaque_s cib_native_opaque_t
int crm_ipc_send(crm_ipc_t *client, xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
Definition: ipc.c:1320
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
Definition: ipc.c:925
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition: cib.h:82
Definition: cib.h:34
char data[0]
Definition: internal.h:92
enum cib_variant variant
Definition: cib.h:147
#define pcmk_ok
Definition: results.h:57
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
Definition: cib_utils.c:547
int call_id
Definition: cib.h:149
#define F_CIB_CLIENTID
Definition: internal.h:32
#define crm_log_xml_trace(xml, text)
Definition: logging.h:254
#define F_CIB_CALLID
Definition: internal.h:34
int(* signoff)(cib_t *cib)
Definition: cib.h:84
mainloop_io_t * mainloop_add_ipc_client(const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
Definition: mainloop.c:821
bool cib_native_dispatch(cib_t *cib)
Definition: cib_native.c:131
#define safe_str_eq(a, b)
Definition: util.h:59
enum cib_conn_type type
Definition: cib.h:146
int cib_native_signon_raw(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition: cib_native.c:177
enum cib_state state
Definition: cib.h:145
GList * notify_list
Definition: cib.h:154
crm_ipc_flags
Definition: ipc.h:39
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc.c:1023
Definition: cib.h:144
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
Definition: mainloop.h:75
void * delegate_fn
Definition: cib.h:152
enum crm_ais_msg_types type
Definition: internal.h:85