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