pacemaker  2.1.6-802a72226b
Scalable High-Availability cluster resource manager
cib_native.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004 International Business Machines
3  * Later changes copyright 2004-2023 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;
39 
40 static int
41 cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host,
42  const char *section, xmlNode *data,
43  xmlNode **output_data, int call_options,
44  const char *user_name)
45 {
46  int rc = pcmk_ok;
47  int reply_id = 0;
48  enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
49 
50  xmlNode *op_msg = NULL;
51  xmlNode *op_reply = NULL;
52 
53  cib_native_opaque_t *native = cib->variant_opaque;
54 
55  if (cib->state == cib_disconnected) {
56  return -ENOTCONN;
57  }
58 
59  if (output_data != NULL) {
60  *output_data = NULL;
61  }
62 
63  if (op == NULL) {
64  crm_err("No operation specified");
65  return -EINVAL;
66  }
67 
68  if (call_options & cib_sync_call) {
69  pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
70  }
71 
72  cib->call_id++;
73  if (cib->call_id < 1) {
74  cib->call_id = 1;
75  }
76 
77  op_msg = cib_create_op(cib->call_id, op, host, section, data, call_options,
78  user_name);
79  if (op_msg == NULL) {
80  return -EPROTO;
81  }
82 
83  crm_trace("Sending %s message to the CIB manager (timeout=%ds)", op, cib->call_timeout);
84  rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
85  free_xml(op_msg);
86 
87  if (rc < 0) {
88  crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
89  cib->call_timeout, pcmk_strerror(rc), rc);
90  rc = -ECOMM;
91  goto done;
92  }
93 
94  crm_log_xml_trace(op_reply, "Reply");
95 
96  if (!(call_options & cib_sync_call)) {
97  crm_trace("Async call, returning %d", cib->call_id);
98  CRM_CHECK(cib->call_id != 0, return -ENOMSG);
99  free_xml(op_reply);
100  return cib->call_id;
101  }
102 
103  rc = pcmk_ok;
104  crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
105  if (reply_id == cib->call_id) {
106  xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
107 
108  crm_trace("Synchronous reply %d received", reply_id);
109  if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
110  rc = -EPROTO;
111  }
112 
113  if (output_data == NULL || (call_options & cib_discard_reply)) {
114  crm_trace("Discarding reply");
115 
116  } else if (tmp != NULL) {
117  *output_data = copy_xml(tmp);
118  }
119 
120  } else if (reply_id <= 0) {
121  crm_err("Received bad reply: No id set");
122  crm_log_xml_err(op_reply, "Bad reply");
123  rc = -ENOMSG;
124  goto done;
125 
126  } else {
127  crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
128  crm_log_xml_err(op_reply, "Old reply");
129  rc = -ENOMSG;
130  goto done;
131  }
132 
133  if (op_reply == NULL && cib->state == cib_disconnected) {
134  rc = -ENOTCONN;
135 
136  } else if (rc == pcmk_ok && op_reply == NULL) {
137  rc = -ETIME;
138  }
139 
140  switch (rc) {
141  case pcmk_ok:
142  case -EPERM:
143  break;
144 
145  /* This is an internal value that clients do not and should not care about */
146  case -pcmk_err_diff_resync:
147  rc = pcmk_ok;
148  break;
149 
150  /* These indicate internal problems */
151  case -EPROTO:
152  case -ENOMSG:
153  crm_err("Call failed: %s", pcmk_strerror(rc));
154  if (op_reply) {
155  crm_log_xml_err(op_reply, "Invalid reply");
156  }
157  break;
158 
159  default:
160  if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
161  crm_warn("Call failed: %s", pcmk_strerror(rc));
162  }
163  }
164 
165  done:
166  if (!crm_ipc_connected(native->ipc)) {
167  crm_err("The CIB manager disconnected");
168  cib->state = cib_disconnected;
169  }
170 
171  free_xml(op_reply);
172  return rc;
173 }
174 
175 static int
176 cib_native_dispatch_internal(const char *buffer, ssize_t length,
177  gpointer userdata)
178 {
179  const char *type = NULL;
180  xmlNode *msg = NULL;
181 
182  cib_t *cib = userdata;
183 
184  crm_trace("dispatching %p", userdata);
185 
186  if (cib == NULL) {
187  crm_err("No CIB!");
188  return 0;
189  }
190 
191  msg = string2xml(buffer);
192 
193  if (msg == NULL) {
194  crm_warn("Received a NULL message from the CIB manager");
195  return 0;
196  }
197 
198  /* do callbacks */
199  type = crm_element_value(msg, F_TYPE);
200  crm_trace("Activating %s callbacks...", type);
201  crm_log_xml_explicit(msg, "cib-reply");
202 
203  if (pcmk__str_eq(type, T_CIB, pcmk__str_casei)) {
204  cib_native_callback(cib, msg, 0, 0);
205 
206  } else if (pcmk__str_eq(type, T_CIB_NOTIFY, pcmk__str_casei)) {
207  g_list_foreach(cib->notify_list, cib_native_notify, msg);
208 
209  } else {
210  crm_err("Unknown message type: %s", type);
211  }
212 
213  free_xml(msg);
214  return 0;
215 }
216 
217 static void
218 cib_native_destroy(void *userdata)
219 {
220  cib_t *cib = userdata;
221  cib_native_opaque_t *native = cib->variant_opaque;
222 
223  crm_trace("destroying %p", userdata);
224  cib->state = cib_disconnected;
225  native->source = NULL;
226  native->ipc = NULL;
227 
228  if (native->dnotify_fn) {
229  native->dnotify_fn(userdata);
230  }
231 }
232 
233 static int
234 cib_native_signoff(cib_t *cib)
235 {
236  cib_native_opaque_t *native = cib->variant_opaque;
237 
238  crm_debug("Disconnecting from the CIB manager");
239 
240  cib_free_notify(cib);
241  remove_cib_op_callback(0, TRUE);
242 
243  if (native->source != NULL) {
244  /* Attached to mainloop */
245  mainloop_del_ipc_client(native->source);
246  native->source = NULL;
247  native->ipc = NULL;
248 
249  } else if (native->ipc) {
250  /* Not attached to mainloop */
251  crm_ipc_t *ipc = native->ipc;
252 
253  native->ipc = NULL;
254  crm_ipc_close(ipc);
255  crm_ipc_destroy(ipc);
256  }
257 
258  cib->state = cib_disconnected;
259  cib->type = cib_no_connection;
260 
261  return pcmk_ok;
262 }
263 
264 static int
265 cib_native_signon_raw(cib_t *cib, const char *name, enum cib_conn_type type,
266  int *async_fd)
267 {
268  int rc = pcmk_ok;
269  const char *channel = NULL;
270  cib_native_opaque_t *native = cib->variant_opaque;
271 
272  struct ipc_client_callbacks cib_callbacks = {
273  .dispatch = cib_native_dispatch_internal,
274  .destroy = cib_native_destroy
275  };
276 
278 
279  if (type == cib_command) {
281  channel = PCMK__SERVER_BASED_RW;
282 
283  } else if (type == cib_command_nonblocking) {
285  channel = PCMK__SERVER_BASED_SHM;
286 
287  } else if (type == cib_query) {
288  cib->state = cib_connected_query;
289  channel = PCMK__SERVER_BASED_RO;
290 
291  } else {
292  return -ENOTCONN;
293  }
294 
295  crm_trace("Connecting %s channel", channel);
296 
297  if (async_fd != NULL) {
298  native->ipc = crm_ipc_new(channel, 0);
299 
300  if (native->ipc && crm_ipc_connect(native->ipc)) {
301  *async_fd = crm_ipc_get_fd(native->ipc);
302 
303  } else if (native->ipc) {
304  rc = -ENOTCONN;
305  }
306 
307  } else {
308  native->source =
309  mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 512 * 1024 /* 512k */ , cib,
310  &cib_callbacks);
311  native->ipc = mainloop_get_ipc_client(native->source);
312  }
313 
314  if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
315  crm_info("Could not connect to CIB manager for %s", name);
316  rc = -ENOTCONN;
317  }
318 
319  if (rc == pcmk_ok) {
320  xmlNode *reply = NULL;
321  xmlNode *hello = create_xml_node(NULL, "cib_command");
322 
323  crm_xml_add(hello, F_TYPE, T_CIB);
327 
328  if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1, &reply) > 0) {
329  const char *msg_type = crm_element_value(reply, F_CIB_OPERATION);
330 
331  rc = pcmk_ok;
332  crm_log_xml_trace(reply, "reg-reply");
333 
334  if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
335  crm_info("Reply to CIB registration message has "
336  "unknown type '%s'", msg_type);
337  rc = -EPROTO;
338 
339  } else {
340  native->token = crm_element_value_copy(reply, F_CIB_CLIENTID);
341  if (native->token == NULL) {
342  rc = -EPROTO;
343  }
344  }
345  free_xml(reply);
346 
347  } else {
348  rc = -ECOMM;
349  }
350 
351  free_xml(hello);
352  }
353 
354  if (rc == pcmk_ok) {
355  crm_info("Successfully connected to CIB manager for %s", name);
356  return pcmk_ok;
357  }
358 
359  crm_info("Connection to CIB manager for %s failed: %s",
360  name, pcmk_strerror(rc));
361  cib_native_signoff(cib);
362  return rc;
363 }
364 
365 static int
366 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
367 {
368  return cib_native_signon_raw(cib, name, type, NULL);
369 }
370 
371 static int
372 cib_native_free(cib_t *cib)
373 {
374  int rc = pcmk_ok;
375 
376  if (cib->state != cib_disconnected) {
377  rc = cib_native_signoff(cib);
378  }
379 
380  if (cib->state == cib_disconnected) {
381  cib_native_opaque_t *native = cib->variant_opaque;
382 
383  free(native->token);
384  free(cib->variant_opaque);
385  free(cib->cmds);
386  free(cib);
387  }
388 
389  return rc;
390 }
391 
392 static int
393 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
394 {
395  int rc = pcmk_ok;
396  xmlNode *notify_msg = create_xml_node(NULL, "cib-callback");
397  cib_native_opaque_t *native = cib->variant_opaque;
398 
399  if (cib->state != cib_disconnected) {
401  crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
402  crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
403  rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
404  1000 * cib->call_timeout, NULL);
405  if (rc <= 0) {
406  crm_trace("Notification not registered: %d", rc);
407  rc = -ECOMM;
408  }
409  }
410 
411  free_xml(notify_msg);
412  return rc;
413 }
414 
415 static int
416 cib_native_set_connection_dnotify(cib_t *cib,
417  void (*dnotify) (gpointer user_data))
418 {
419  cib_native_opaque_t *native = NULL;
420 
421  if (cib == NULL) {
422  crm_err("No CIB!");
423  return FALSE;
424  }
425 
426  native = cib->variant_opaque;
427  native->dnotify_fn = dnotify;
428 
429  return pcmk_ok;
430 }
431 
450 static int
451 cib_native_client_id(const cib_t *cib, const char **async_id,
452  const char **sync_id)
453 {
454  cib_native_opaque_t *native = cib->variant_opaque;
455 
456  if (async_id != NULL) {
457  *async_id = native->token;
458  }
459  if (sync_id != NULL) {
460  *sync_id = native->token;
461  }
462  return pcmk_ok;
463 }
464 
465 cib_t *
467 {
468  cib_native_opaque_t *native = NULL;
469  cib_t *cib = cib_new_variant();
470 
471  if (cib == NULL) {
472  return NULL;
473  }
474 
475  native = calloc(1, sizeof(cib_native_opaque_t));
476 
477  if (native == NULL) {
478  free(cib);
479  return NULL;
480  }
481 
482  cib->variant = cib_native;
483  cib->variant_opaque = native;
484 
485  native->ipc = NULL;
486  native->source = NULL;
487  native->dnotify_fn = NULL;
488 
489  /* assign variant specific ops */
490  cib->delegate_fn = cib_native_perform_op_delegate;
491  cib->cmds->signon = cib_native_signon;
492  cib->cmds->signon_raw = cib_native_signon_raw;
493  cib->cmds->signoff = cib_native_signoff;
494  cib->cmds->free = cib_native_free;
495 
496  cib->cmds->register_notification = cib_native_register_notification;
497  cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
498 
499  cib->cmds->client_id = cib_native_client_id;
500 
501  return cib;
502 }
pcmk__cpg_host_t host
Definition: cpg.c:49
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:235
#define PCMK__SERVER_BASED_RW
Definition: crm_internal.h:97
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
Definition: ipc_client.c:867
A dumping ground.
#define F_TYPE
Definition: msg_xml.h:82
const char * pcmk_strerror(int rc)
Definition: results.c:148
#define ETIME
Definition: portability.h:150
char data[0]
Definition: cpg.c:55
int call_timeout
Definition: cib_types.h:209
const char * name
Definition: cib.c:24
int crm_ipc_get_fd(crm_ipc_t *client)
Definition: ipc_client.c:981
int(* signoff)(cib_t *cib)
Definition: cib_types.h:87
#define PCMK__CIB_REQUEST_QUERY
Definition: internal.h:24
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:398
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:33
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
Definition: cib_client.c:709
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:302
int(* signon_raw)(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition: cib_types.h:85
void cib_free_notify(cib_t *cib)
Definition: cib_client.c:665
enum crm_ais_msg_types type
Definition: cpg.c:48
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:532
Wrappers for and extensions to glib mainloop.
#define CRM_OP_REGISTER
Definition: crm.h:145
xmlNode * string2xml(const char *input)
Definition: xml.c:831
#define F_CIB_NOTIFY_ACTIVATE
Definition: internal.h:57
void cib_native_notify(gpointer data, gpointer user_data)
Definition: cib_utils.c:549
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:819
cib_t * cib_new_variant(void)
Definition: cib_client.c:595
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition: cib_types.h:98
#define crm_warn(fmt, args...)
Definition: logging.h:378
cib_api_operations_t * cmds
Definition: cib_types.h:216
#define crm_debug(fmt, args...)
Definition: logging.h:382
#define F_CIB_RC
Definition: internal.h:44
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:165
cib_conn_type
Definition: cib_types.h:46
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition: cib_types.h:84
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
Definition: nvpair.c:693
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:496
#define F_CIB_OPERATION
Definition: internal.h:40
#define F_CIB_CLIENTNAME
Definition: internal.h:55
#define crm_trace(fmt, args...)
Definition: logging.h:383
#define crm_log_xml_explicit(xml, text)
Definition: logging.h:393
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:677
#define PCMK__IPC_TIMEOUT
Definition: ipc_internal.h:52
#define ECOMM
Definition: portability.h:125
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:940
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc_client.c:955
void free_xml(xmlNode *child)
Definition: xml.c:813
#define PCMK__SERVER_BASED_RO
Definition: crm_internal.h:96
xmlNode * get_message_xml(const xmlNode *msg, const char *field)
Definition: messages.c:154
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition: cib_types.h:145
#define T_CIB
Definition: internal.h:65
void * variant_opaque
Definition: cib_types.h:210
#define F_CIB_NOTIFY_TYPE
Definition: internal.h:56
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc_client.c:995
#define T_CIB_NOTIFY
Definition: internal.h:66
xmlNode * cib_create_op(int call_id, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
Definition: cib_utils.c:468
#define F_CIB_CALLOPTS
Definition: internal.h:37
#define pcmk_err_diff_resync
Definition: results.h:77
#define crm_log_xml_err(xml, text)
Definition: logging.h:386
#define F_CIB_CALLDATA
Definition: internal.h:39
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
Definition: ipc_internal.h:207
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:946
#define crm_err(fmt, args...)
Definition: logging.h:377
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_client.c:1234
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
Create a new (legacy) object for using Pacemaker daemon IPC.
Definition: ipc_client.c:820
enum cib_variant variant
Definition: cib_types.h:206
#define pcmk_ok
Definition: results.h:68
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
Definition: cib_utils.c:500
int call_id
Definition: cib_types.h:208
#define F_CIB_CLIENTID
Definition: internal.h:36
#define crm_log_xml_trace(xml, text)
Definition: logging.h:391
#define F_CIB_CALLID
Definition: internal.h:38
#define PCMK__SERVER_BASED_SHM
Definition: crm_internal.h:98
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:915
enum cib_conn_type type
Definition: cib_types.h:205
enum cib_state state
Definition: cib_types.h:204
GList * notify_list
Definition: cib_types.h:213
crm_ipc_flags
Definition: ipc.h:144
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc_client.c:942
int(* free)(cib_t *cib)
Definition: cib_types.h:88
int(* client_id)(const cib_t *cib, const char **async_id, const char **sync_id)
Get the given CIB connection&#39;s unique client identifier(s)
Definition: cib_types.h:199
#define crm_info(fmt, args...)
Definition: logging.h:380
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
Dispatch function for an IPC connection used as mainloop source.
Definition: mainloop.h:84
cib_t * cib_native_new(void)
Definition: cib_native.c:466
void * delegate_fn
Definition: cib_types.h:211