pacemaker  2.1.8-3980678f03
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-2024 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/common/mainloop.h>
31 #include <crm/common/xml.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  rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
73  NULL, &op_msg);
74  if (rc != pcmk_ok) {
75  return rc;
76  }
77 
78  if (pcmk_is_set(call_options, cib_transaction)) {
79  rc = cib__extend_transaction(cib, op_msg);
80  goto done;
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 
86  if (rc < 0) {
87  crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
88  cib->call_timeout, pcmk_strerror(rc), rc);
89  rc = -ECOMM;
90  goto done;
91  }
92 
93  crm_log_xml_trace(op_reply, "Reply");
94 
95  if (!(call_options & cib_sync_call)) {
96  crm_trace("Async call, returning %d", cib->call_id);
97  CRM_CHECK(cib->call_id != 0,
98  rc = -ENOMSG; goto done);
99  rc = cib->call_id;
100  goto done;
101  }
102 
103  rc = pcmk_ok;
104  crm_element_value_int(op_reply, PCMK__XA_CIB_CALLID, &reply_id);
105  if (reply_id == cib->call_id) {
106  xmlNode *wrapper = pcmk__xe_first_child(op_reply, PCMK__XE_CIB_CALLDATA,
107  NULL, NULL);
108  xmlNode *tmp = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
109 
110  crm_trace("Synchronous reply %d received", reply_id);
111  if (crm_element_value_int(op_reply, PCMK__XA_CIB_RC, &rc) != 0) {
112  rc = -EPROTO;
113  }
114 
115  if (output_data == NULL || (call_options & cib_discard_reply)) {
116  crm_trace("Discarding reply");
117  } else {
118  *output_data = pcmk__xml_copy(NULL, tmp);
119  }
120 
121  } else if (reply_id <= 0) {
122  crm_err("Received bad reply: No id set");
123  crm_log_xml_err(op_reply, "Bad reply");
124  rc = -ENOMSG;
125  goto done;
126 
127  } else {
128  crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
129  crm_log_xml_err(op_reply, "Old reply");
130  rc = -ENOMSG;
131  goto done;
132  }
133 
134  if (op_reply == NULL && cib->state == cib_disconnected) {
135  rc = -ENOTCONN;
136 
137  } else if (rc == pcmk_ok && op_reply == NULL) {
138  rc = -ETIME;
139  }
140 
141  switch (rc) {
142  case pcmk_ok:
143  case -EPERM:
144  break;
145 
146  /* This is an internal value that clients do not and should not care about */
147  case -pcmk_err_diff_resync:
148  rc = pcmk_ok;
149  break;
150 
151  /* These indicate internal problems */
152  case -EPROTO:
153  case -ENOMSG:
154  crm_err("Call failed: %s", pcmk_strerror(rc));
155  if (op_reply) {
156  crm_log_xml_err(op_reply, "Invalid reply");
157  }
158  break;
159 
160  default:
161  if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
162  crm_warn("Call failed: %s", pcmk_strerror(rc));
163  }
164  }
165 
166  done:
167  if (!crm_ipc_connected(native->ipc)) {
168  crm_err("The CIB manager disconnected");
169  cib->state = cib_disconnected;
170  }
171 
172  free_xml(op_msg);
173  free_xml(op_reply);
174  return rc;
175 }
176 
177 static int
178 cib_native_dispatch_internal(const char *buffer, ssize_t length,
179  gpointer userdata)
180 {
181  const char *type = NULL;
182  xmlNode *msg = NULL;
183 
184  cib_t *cib = userdata;
185 
186  crm_trace("dispatching %p", userdata);
187 
188  if (cib == NULL) {
189  crm_err("No CIB!");
190  return 0;
191  }
192 
193  msg = pcmk__xml_parse(buffer);
194 
195  if (msg == NULL) {
196  crm_warn("Received a NULL message from the CIB manager");
197  return 0;
198  }
199 
200  /* do callbacks */
202  crm_trace("Activating %s callbacks...", type);
203  crm_log_xml_explicit(msg, "cib-reply");
204 
205  if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
206  cib_native_callback(cib, msg, 0, 0);
207 
208  } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
209  g_list_foreach(cib->notify_list, cib_native_notify, msg);
210 
211  } else {
212  crm_err("Unknown message type: %s", type);
213  }
214 
215  free_xml(msg);
216  return 0;
217 }
218 
219 static void
220 cib_native_destroy(void *userdata)
221 {
222  cib_t *cib = userdata;
223  cib_native_opaque_t *native = cib->variant_opaque;
224 
225  crm_trace("destroying %p", userdata);
226  cib->state = cib_disconnected;
227  native->source = NULL;
228  native->ipc = NULL;
229 
230  if (native->dnotify_fn) {
231  native->dnotify_fn(userdata);
232  }
233 }
234 
235 static int
236 cib_native_signoff(cib_t *cib)
237 {
238  cib_native_opaque_t *native = cib->variant_opaque;
239 
240  crm_debug("Disconnecting from the CIB manager");
241 
242  cib_free_notify(cib);
243  remove_cib_op_callback(0, TRUE);
244 
245  if (native->source != NULL) {
246  /* Attached to mainloop */
247  mainloop_del_ipc_client(native->source);
248  native->source = NULL;
249  native->ipc = NULL;
250 
251  } else if (native->ipc) {
252  /* Not attached to mainloop */
253  crm_ipc_t *ipc = native->ipc;
254 
255  native->ipc = NULL;
256  crm_ipc_close(ipc);
257  crm_ipc_destroy(ipc);
258  }
259 
260  cib->cmds->end_transaction(cib, false, cib_none);
261  cib->state = cib_disconnected;
262  cib->type = cib_no_connection;
263 
264  return pcmk_ok;
265 }
266 
267 static int
268 cib_native_signon_raw(cib_t *cib, const char *name, enum cib_conn_type type,
269  int *async_fd)
270 {
271  int rc = pcmk_ok;
272  const char *channel = NULL;
273  cib_native_opaque_t *native = cib->variant_opaque;
274  xmlNode *hello = NULL;
275 
276  struct ipc_client_callbacks cib_callbacks = {
277  .dispatch = cib_native_dispatch_internal,
278  .destroy = cib_native_destroy
279  };
280 
282 
283  if (type == cib_command) {
285  channel = PCMK__SERVER_BASED_RW;
286 
287  } else if (type == cib_command_nonblocking) {
289  channel = PCMK__SERVER_BASED_SHM;
290 
291  } else if (type == cib_query) {
292  cib->state = cib_connected_query;
293  channel = PCMK__SERVER_BASED_RO;
294 
295  } else {
296  return -ENOTCONN;
297  }
298 
299  crm_trace("Connecting %s channel", channel);
300 
301  if (async_fd != NULL) {
302  native->ipc = crm_ipc_new(channel, 0);
303  if (native->ipc != NULL) {
304  rc = pcmk__connect_generic_ipc(native->ipc);
305  if (rc == pcmk_rc_ok) {
306  rc = pcmk__ipc_fd(native->ipc, async_fd);
307  if (rc != pcmk_rc_ok) {
308  crm_info("Couldn't get file descriptor for %s IPC",
309  channel);
310  }
311  }
312  rc = pcmk_rc2legacy(rc);
313  }
314 
315  } else {
316  native->source =
317  mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 512 * 1024 /* 512k */ , cib,
318  &cib_callbacks);
319  native->ipc = mainloop_get_ipc_client(native->source);
320  }
321 
322  if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
323  crm_info("Could not connect to CIB manager for %s", name);
324  rc = -ENOTCONN;
325  }
326 
327  if (rc == pcmk_ok) {
328  rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
329  cib_sync_call, NULL, name, &hello);
330  }
331 
332  if (rc == pcmk_ok) {
333  xmlNode *reply = NULL;
334 
335  if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
336  &reply) > 0) {
337  const char *msg_type = crm_element_value(reply, PCMK__XA_CIB_OP);
338 
339  crm_log_xml_trace(reply, "reg-reply");
340 
341  if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
342  crm_info("Reply to CIB registration message has unknown type "
343  "'%s'",
344  msg_type);
345  rc = -EPROTO;
346 
347  } else {
348  native->token = crm_element_value_copy(reply,
350  if (native->token == NULL) {
351  rc = -EPROTO;
352  }
353  }
354  free_xml(reply);
355 
356  } else {
357  rc = -ECOMM;
358  }
359  free_xml(hello);
360  }
361 
362  if (rc == pcmk_ok) {
363  crm_info("Successfully connected to CIB manager for %s", name);
364  return pcmk_ok;
365  }
366 
367  crm_info("Connection to CIB manager for %s failed: %s",
368  name, pcmk_strerror(rc));
369  cib_native_signoff(cib);
370  return rc;
371 }
372 
373 static int
374 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
375 {
376  return cib_native_signon_raw(cib, name, type, NULL);
377 }
378 
379 static int
380 cib_native_free(cib_t *cib)
381 {
382  int rc = pcmk_ok;
383 
384  if (cib->state != cib_disconnected) {
385  rc = cib_native_signoff(cib);
386  }
387 
388  if (cib->state == cib_disconnected) {
389  cib_native_opaque_t *native = cib->variant_opaque;
390 
391  free(native->token);
392  free(cib->variant_opaque);
393  free(cib->cmds);
394  free(cib->user);
395  free(cib);
396  }
397 
398  return rc;
399 }
400 
401 static int
402 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
403 {
404  int rc = pcmk_ok;
405  xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
406  cib_native_opaque_t *native = cib->variant_opaque;
407 
408  if (cib->state != cib_disconnected) {
410  crm_xml_add(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
411  crm_xml_add_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
412  rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
413  1000 * cib->call_timeout, NULL);
414  if (rc <= 0) {
415  crm_trace("Notification not registered: %d", rc);
416  rc = -ECOMM;
417  }
418  }
419 
420  free_xml(notify_msg);
421  return rc;
422 }
423 
424 static int
425 cib_native_set_connection_dnotify(cib_t *cib,
426  void (*dnotify) (gpointer user_data))
427 {
428  cib_native_opaque_t *native = NULL;
429 
430  if (cib == NULL) {
431  crm_err("No CIB!");
432  return FALSE;
433  }
434 
435  native = cib->variant_opaque;
436  native->dnotify_fn = dnotify;
437 
438  return pcmk_ok;
439 }
440 
459 static int
460 cib_native_client_id(const cib_t *cib, const char **async_id,
461  const char **sync_id)
462 {
463  cib_native_opaque_t *native = cib->variant_opaque;
464 
465  if (async_id != NULL) {
466  *async_id = native->token;
467  }
468  if (sync_id != NULL) {
469  *sync_id = native->token;
470  }
471  return pcmk_ok;
472 }
473 
474 cib_t *
476 {
477  cib_native_opaque_t *native = NULL;
478  cib_t *cib = cib_new_variant();
479 
480  if (cib == NULL) {
481  return NULL;
482  }
483 
484  native = calloc(1, sizeof(cib_native_opaque_t));
485 
486  if (native == NULL) {
487  free(cib);
488  return NULL;
489  }
490 
491  cib->variant = cib_native;
492  cib->variant_opaque = native;
493 
494  native->ipc = NULL;
495  native->source = NULL;
496  native->dnotify_fn = NULL;
497 
498  /* assign variant specific ops */
499  cib->delegate_fn = cib_native_perform_op_delegate;
500  cib->cmds->signon = cib_native_signon;
501  cib->cmds->signon_raw = cib_native_signon_raw;
502  cib->cmds->signoff = cib_native_signoff;
503  cib->cmds->free = cib_native_free;
504 
505  cib->cmds->register_notification = cib_native_register_notification;
506  cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
507 
508  cib->cmds->client_id = cib_native_client_id;
509 
510  return cib;
511 }
pcmk__cpg_host_t host
Definition: cpg.c:52
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:245
#define PCMK__SERVER_BASED_RW
Definition: crm_internal.h:67
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
Definition: xml.c:883
A dumping ground.
const char * pcmk_strerror(int rc)
Definition: results.c:149
#define ETIME
Definition: portability.h:111
char data[0]
Definition: cpg.c:58
int call_timeout
Definition: cib_types.h:388
int pcmk_rc2legacy(int rc)
Definition: results.c:546
#define PCMK__XE_CIB_CALLBACK
const char * name
Definition: cib.c:26
#define PCMK__XA_CIB_CLIENTID
int(* signoff)(cib_t *cib)
Definition: cib_types.h:166
#define PCMK__CIB_REQUEST_QUERY
Definition: internal.h:23
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:348
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:35
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
Definition: cib_client.c:800
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:301
int(* signon_raw)(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition: cib_types.h:162
void cib_free_notify(cib_t *cib)
Definition: cib_client.c:756
enum crm_ais_msg_types type
Definition: cpg.c:51
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:482
#define PCMK__XA_CIB_NOTIFY_TYPE
Wrappers for and extensions to glib mainloop.
#define CRM_OP_REGISTER
Definition: crm.h:129
int pcmk__connect_generic_ipc(crm_ipc_t *ipc)
Definition: ipc_client.c:896
void cib_native_notify(gpointer data, gpointer user_data)
Definition: cib_utils.c:823
cib_t * cib_new_variant(void)
Definition: cib_client.c:676
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition: cib_types.h:185
#define crm_warn(fmt, args...)
Definition: logging.h:394
cib_api_operations_t * cmds
Definition: cib_types.h:399
#define crm_debug(fmt, args...)
Definition: logging.h:402
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:184
cib_conn_type
Definition: cib_types.h:50
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition: cib_types.h:159
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
Definition: nvpair.c:674
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:446
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
Definition: xml.c:440
#define crm_trace(fmt, args...)
Definition: logging.h:404
#define crm_log_xml_explicit(xml, text)
Definition: logging.h:414
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition: util.h:98
#define PCMK__XE_CIB_CALLDATA
#define PCMK__VALUE_CIB
Wrappers for and extensions to libxml2.
#define PCMK__IPC_TIMEOUT
Definition: ipc_internal.h:52
#define ECOMM
Definition: portability.h:86
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:943
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc_client.c:1006
void free_xml(xmlNode *child)
Definition: xml.c:867
#define PCMK__SERVER_BASED_RO
Definition: crm_internal.h:66
int cib__create_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name, const char *client_name, xmlNode **op_msg)
Definition: cib_utils.c:660
xmlNode * pcmk__xml_parse(const char *input)
Definition: xml_io.c:244
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition: cib_types.h:248
void * variant_opaque
Definition: cib_types.h:389
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc_client.c:1068
char * user
Definition: cib_types.h:403
#define pcmk_err_diff_resync
Definition: results.h:83
#define crm_log_xml_err(xml, text)
Definition: logging.h:407
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client&#39;s CIB transaction.
Definition: cib_types.h:362
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
Definition: ipc_internal.h:212
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:949
#define crm_err(fmt, args...)
Definition: logging.h:391
struct cib_native_opaque_s cib_native_opaque_t
#define PCMK__XA_CIB_RC
int cib__extend_transaction(cib_t *cib, xmlNode *request)
Definition: cib_utils.c:743
int pcmk__ipc_fd(crm_ipc_t *ipc, int *fd)
Definition: ipc_client.c:1041
#define PCMK__XA_CIB_OP
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:850
enum cib_variant variant
Definition: cib_types.h:385
#define pcmk_ok
Definition: results.h:69
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
Definition: cib_utils.c:771
int call_id
Definition: cib_types.h:387
#define crm_log_xml_trace(xml, text)
Definition: logging.h:412
#define PCMK__SERVER_BASED_SHM
Definition: crm_internal.h:68
#define PCMK__XA_T
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:918
enum cib_conn_type type
Definition: cib_types.h:384
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml.c:720
#define PCMK__XA_CIB_NOTIFY_ACTIVATE
enum cib_state state
Definition: cib_types.h:382
int crm_ipc_send(crm_ipc_t *client, const xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
Send an IPC XML message.
Definition: ipc_client.c:1308
GList * notify_list
Definition: cib_types.h:392
crm_ipc_flags
Definition: ipc.h:163
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc_client.c:993
int(* free)(cib_t *cib)
Definition: cib_types.h:168
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:299
#define PCMK__XA_CIB_CALLID
#define crm_info(fmt, args...)
Definition: logging.h:399
Process request when the client commits the active transaction.
Definition: cib_types.h:108
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
Dispatch function for an IPC connection used as mainloop source.
Definition: mainloop.h:94
cib_t * cib_native_new(void)
Definition: cib_native.c:475
void * delegate_fn
Definition: cib_types.h:390
#define PCMK__VALUE_CIB_NOTIFY