pacemaker  2.1.7-0f7f88312f
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  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, return -ENOMSG);
98  free_xml(op_reply);
99  return cib->call_id;
100  }
101 
102  rc = pcmk_ok;
103  crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
104  if (reply_id == cib->call_id) {
105  xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
106 
107  crm_trace("Synchronous reply %d received", reply_id);
108  if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
109  rc = -EPROTO;
110  }
111 
112  if (output_data == NULL || (call_options & cib_discard_reply)) {
113  crm_trace("Discarding reply");
114 
115  } else if (tmp != NULL) {
116  *output_data = copy_xml(tmp);
117  }
118 
119  } else if (reply_id <= 0) {
120  crm_err("Received bad reply: No id set");
121  crm_log_xml_err(op_reply, "Bad reply");
122  rc = -ENOMSG;
123  goto done;
124 
125  } else {
126  crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
127  crm_log_xml_err(op_reply, "Old reply");
128  rc = -ENOMSG;
129  goto done;
130  }
131 
132  if (op_reply == NULL && cib->state == cib_disconnected) {
133  rc = -ENOTCONN;
134 
135  } else if (rc == pcmk_ok && op_reply == NULL) {
136  rc = -ETIME;
137  }
138 
139  switch (rc) {
140  case pcmk_ok:
141  case -EPERM:
142  break;
143 
144  /* This is an internal value that clients do not and should not care about */
145  case -pcmk_err_diff_resync:
146  rc = pcmk_ok;
147  break;
148 
149  /* These indicate internal problems */
150  case -EPROTO:
151  case -ENOMSG:
152  crm_err("Call failed: %s", pcmk_strerror(rc));
153  if (op_reply) {
154  crm_log_xml_err(op_reply, "Invalid reply");
155  }
156  break;
157 
158  default:
159  if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
160  crm_warn("Call failed: %s", pcmk_strerror(rc));
161  }
162  }
163 
164  done:
165  if (!crm_ipc_connected(native->ipc)) {
166  crm_err("The CIB manager disconnected");
167  cib->state = cib_disconnected;
168  }
169 
170  free_xml(op_msg);
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->cmds->end_transaction(cib, false, cib_none);
259  cib->state = cib_disconnected;
260  cib->type = cib_no_connection;
261 
262  return pcmk_ok;
263 }
264 
265 static int
266 cib_native_signon_raw(cib_t *cib, const char *name, enum cib_conn_type type,
267  int *async_fd)
268 {
269  int rc = pcmk_ok;
270  const char *channel = NULL;
271  cib_native_opaque_t *native = cib->variant_opaque;
272  xmlNode *hello = NULL;
273 
274  struct ipc_client_callbacks cib_callbacks = {
275  .dispatch = cib_native_dispatch_internal,
276  .destroy = cib_native_destroy
277  };
278 
280 
281  if (type == cib_command) {
283  channel = PCMK__SERVER_BASED_RW;
284 
285  } else if (type == cib_command_nonblocking) {
287  channel = PCMK__SERVER_BASED_SHM;
288 
289  } else if (type == cib_query) {
290  cib->state = cib_connected_query;
291  channel = PCMK__SERVER_BASED_RO;
292 
293  } else {
294  return -ENOTCONN;
295  }
296 
297  crm_trace("Connecting %s channel", channel);
298 
299  if (async_fd != NULL) {
300  native->ipc = crm_ipc_new(channel, 0);
301  if (native->ipc != NULL) {
302  rc = pcmk__connect_generic_ipc(native->ipc);
303  if (rc == pcmk_rc_ok) {
304  rc = pcmk__ipc_fd(native->ipc, async_fd);
305  if (rc != pcmk_rc_ok) {
306  crm_info("Couldn't get file descriptor for %s IPC",
307  channel);
308  }
309  }
310  rc = pcmk_rc2legacy(rc);
311  }
312 
313  } else {
314  native->source =
315  mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 512 * 1024 /* 512k */ , cib,
316  &cib_callbacks);
317  native->ipc = mainloop_get_ipc_client(native->source);
318  }
319 
320  if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
321  crm_info("Could not connect to CIB manager for %s", name);
322  rc = -ENOTCONN;
323  }
324 
325  if (rc == pcmk_ok) {
326  rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
327  cib_sync_call, NULL, name, &hello);
328  }
329 
330  if (rc == pcmk_ok) {
331  xmlNode *reply = NULL;
332 
333  if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
334  &reply) > 0) {
335  const char *msg_type = crm_element_value(reply, F_CIB_OPERATION);
336 
337  crm_log_xml_trace(reply, "reg-reply");
338 
339  if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
340  crm_info("Reply to CIB registration message has unknown type "
341  "'%s'",
342  msg_type);
343  rc = -EPROTO;
344 
345  } else {
346  native->token = crm_element_value_copy(reply, F_CIB_CLIENTID);
347  if (native->token == NULL) {
348  rc = -EPROTO;
349  }
350  }
351  free_xml(reply);
352 
353  } else {
354  rc = -ECOMM;
355  }
356  free_xml(hello);
357  }
358 
359  if (rc == pcmk_ok) {
360  crm_info("Successfully connected to CIB manager for %s", name);
361  return pcmk_ok;
362  }
363 
364  crm_info("Connection to CIB manager for %s failed: %s",
365  name, pcmk_strerror(rc));
366  cib_native_signoff(cib);
367  return rc;
368 }
369 
370 static int
371 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
372 {
373  return cib_native_signon_raw(cib, name, type, NULL);
374 }
375 
376 static int
377 cib_native_free(cib_t *cib)
378 {
379  int rc = pcmk_ok;
380 
381  if (cib->state != cib_disconnected) {
382  rc = cib_native_signoff(cib);
383  }
384 
385  if (cib->state == cib_disconnected) {
386  cib_native_opaque_t *native = cib->variant_opaque;
387 
388  free(native->token);
389  free(cib->variant_opaque);
390  free(cib->cmds);
391  free(cib->user);
392  free(cib);
393  }
394 
395  return rc;
396 }
397 
398 static int
399 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
400 {
401  int rc = pcmk_ok;
402  xmlNode *notify_msg = create_xml_node(NULL, "cib-callback");
403  cib_native_opaque_t *native = cib->variant_opaque;
404 
405  if (cib->state != cib_disconnected) {
407  crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
408  crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
409  rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
410  1000 * cib->call_timeout, NULL);
411  if (rc <= 0) {
412  crm_trace("Notification not registered: %d", rc);
413  rc = -ECOMM;
414  }
415  }
416 
417  free_xml(notify_msg);
418  return rc;
419 }
420 
421 static int
422 cib_native_set_connection_dnotify(cib_t *cib,
423  void (*dnotify) (gpointer user_data))
424 {
425  cib_native_opaque_t *native = NULL;
426 
427  if (cib == NULL) {
428  crm_err("No CIB!");
429  return FALSE;
430  }
431 
432  native = cib->variant_opaque;
433  native->dnotify_fn = dnotify;
434 
435  return pcmk_ok;
436 }
437 
456 static int
457 cib_native_client_id(const cib_t *cib, const char **async_id,
458  const char **sync_id)
459 {
460  cib_native_opaque_t *native = cib->variant_opaque;
461 
462  if (async_id != NULL) {
463  *async_id = native->token;
464  }
465  if (sync_id != NULL) {
466  *sync_id = native->token;
467  }
468  return pcmk_ok;
469 }
470 
471 cib_t *
473 {
474  cib_native_opaque_t *native = NULL;
475  cib_t *cib = cib_new_variant();
476 
477  if (cib == NULL) {
478  return NULL;
479  }
480 
481  native = calloc(1, sizeof(cib_native_opaque_t));
482 
483  if (native == NULL) {
484  free(cib);
485  return NULL;
486  }
487 
488  cib->variant = cib_native;
489  cib->variant_opaque = native;
490 
491  native->ipc = NULL;
492  native->source = NULL;
493  native->dnotify_fn = NULL;
494 
495  /* assign variant specific ops */
496  cib->delegate_fn = cib_native_perform_op_delegate;
497  cib->cmds->signon = cib_native_signon;
498  cib->cmds->signon_raw = cib_native_signon_raw;
499  cib->cmds->signoff = cib_native_signoff;
500  cib->cmds->free = cib_native_free;
501 
502  cib->cmds->register_notification = cib_native_register_notification;
503  cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
504 
505  cib->cmds->client_id = cib_native_client_id;
506 
507  return cib;
508 }
pcmk__cpg_host_t host
Definition: cpg.c:49
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:238
#define PCMK__SERVER_BASED_RW
Definition: crm_internal.h:102
A dumping ground.
#define F_TYPE
Definition: msg_xml.h:91
const char * pcmk_strerror(int rc)
Definition: results.c:149
#define ETIME
Definition: portability.h:111
char data[0]
Definition: cpg.c:55
int call_timeout
Definition: cib_types.h:335
int pcmk_rc2legacy(int rc)
Definition: results.c:546
const char * name
Definition: cib.c:26
int(* signoff)(cib_t *cib)
Definition: cib_types.h:132
#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:349
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:787
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:129
void cib_free_notify(cib_t *cib)
Definition: cib_client.c:743
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:483
Wrappers for and extensions to glib mainloop.
#define CRM_OP_REGISTER
Definition: crm.h:144
xmlNode * string2xml(const char *input)
Definition: xml.c:800
#define F_CIB_NOTIFY_ACTIVATE
Definition: internal.h:57
int pcmk__connect_generic_ipc(crm_ipc_t *ipc)
Definition: ipc_client.c:895
void cib_native_notify(gpointer data, gpointer user_data)
Definition: cib_utils.c:835
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:789
cib_t * cib_new_variant(void)
Definition: cib_client.c:665
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition: cib_types.h:146
#define crm_warn(fmt, args...)
Definition: logging.h:382
cib_api_operations_t * cmds
Definition: cib_types.h:345
#define crm_debug(fmt, args...)
Definition: logging.h:386
#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:126
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
Definition: nvpair.c:644
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:447
#define F_CIB_OPERATION
Definition: internal.h:40
#define crm_trace(fmt, args...)
Definition: logging.h:387
#define crm_log_xml_explicit(xml, text)
Definition: logging.h:397
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition: util.h:99
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:638
#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:944
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc_client.c:1005
void free_xml(xmlNode *child)
Definition: xml.c:783
#define PCMK__SERVER_BASED_RO
Definition: crm_internal.h:101
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:673
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:200
#define T_CIB
Definition: internal.h:64
void * variant_opaque
Definition: cib_types.h:336
#define F_CIB_NOTIFY_TYPE
Definition: internal.h:56
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc_client.c:1067
#define T_CIB_NOTIFY
Definition: internal.h:66
char * user
Definition: cib_types.h:349
#define pcmk_err_diff_resync
Definition: results.h:77
#define crm_log_xml_err(xml, text)
Definition: logging.h:390
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:314
#define F_CIB_CALLDATA
Definition: internal.h:39
#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:950
#define crm_err(fmt, args...)
Definition: logging.h:381
struct cib_native_opaque_s cib_native_opaque_t
int cib__extend_transaction(cib_t *cib, xmlNode *request)
Definition: cib_utils.c:758
int pcmk__ipc_fd(crm_ipc_t *ipc, int *fd)
Definition: ipc_client.c:1040
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:849
enum cib_variant variant
Definition: cib_types.h:332
#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:786
int call_id
Definition: cib_types.h:334
#define F_CIB_CLIENTID
Definition: internal.h:36
#define crm_log_xml_trace(xml, text)
Definition: logging.h:395
#define F_CIB_CALLID
Definition: internal.h:38
#define PCMK__SERVER_BASED_SHM
Definition: crm_internal.h:103
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:919
enum cib_conn_type type
Definition: cib_types.h:331
enum cib_state state
Definition: cib_types.h:330
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:1307
GList * notify_list
Definition: cib_types.h:339
crm_ipc_flags
Definition: ipc.h:144
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc_client.c:992
int(* free)(cib_t *cib)
Definition: cib_types.h:133
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:251
#define crm_info(fmt, args...)
Definition: logging.h:384
Process request when the client commits the active transaction.
Definition: cib_types.h:100
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:472
void * delegate_fn
Definition: cib_types.h:337