pacemaker 2.1.8-2.1.8
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
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
33typedef 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
40static int
41cib_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
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 */
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
177static int
178cib_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
219static void
220cib_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
235static int
236cib_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
267static int
268cib_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) {
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
373static int
374cib_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
379static int
380cib_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
401static int
402cib_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
424static int
425cib_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
459static int
460cib_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
474cib_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}
int cib__extend_transaction(cib_t *cib, xmlNode *request)
Definition cib_utils.c:743
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
Definition cib_utils.c:771
cib_t * cib_new_variant(void)
Definition cib_client.c:676
#define PCMK__CIB_REQUEST_QUERY
Definition internal.h:23
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
void cib_native_notify(gpointer data, gpointer user_data)
Definition cib_utils.c:823
const char * name
Definition cib.c:26
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
Definition cib_client.c:800
void cib_free_notify(cib_t *cib)
Definition cib_client.c:756
cib_t * cib_native_new(void)
Definition cib_native.c:475
struct cib_native_opaque_s cib_native_opaque_t
cib_conn_type
Definition cib_types.h:50
@ cib_query
Definition cib_types.h:54
@ cib_no_connection
Definition cib_types.h:56
@ cib_command
Definition cib_types.h:51
@ cib_command_nonblocking
Definition cib_types.h:57
@ cib_none
Definition cib_types.h:61
@ cib_transaction
Process request when the client commits the active transaction.
Definition cib_types.h:108
@ cib_sync_call
Definition cib_types.h:133
@ cib_discard_reply
Definition cib_types.h:66
@ cib_native
Definition cib_types.h:30
@ cib_connected_command
Definition cib_types.h:42
@ cib_connected_query
Definition cib_types.h:45
@ cib_disconnected
Definition cib_types.h:47
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition util.h:98
pcmk__cpg_host_t host
Definition cpg.c:4
enum crm_ais_msg_types type
Definition cpg.c:3
char data[0]
Definition cpg.c:10
A dumping ground.
#define CRM_OP_REGISTER
Definition crm.h:129
#define PCMK__SERVER_BASED_RO
#define PCMK__SERVER_BASED_RW
#define PCMK__SERVER_BASED_SHM
void crm_ipc_destroy(crm_ipc_t *client)
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.
crm_ipc_flags
Definition ipc.h:164
@ crm_ipc_flags_none
Definition ipc.h:165
@ crm_ipc_client_response
Definition ipc.h:170
bool crm_ipc_connected(crm_ipc_t *client)
void crm_ipc_close(crm_ipc_t *client)
Definition ipc_client.c:993
struct crm_ipc_s crm_ipc_t
Definition ipc.h:184
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
int pcmk__connect_generic_ipc(crm_ipc_t *ipc)
Definition ipc_client.c:896
int pcmk__ipc_fd(crm_ipc_t *ipc, int *fd)
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
#define PCMK__IPC_TIMEOUT
#define crm_info(fmt, args...)
Definition logging.h:397
#define crm_warn(fmt, args...)
Definition logging.h:392
#define crm_log_xml_explicit(xml, text)
Definition logging.h:412
#define crm_log_xml_err(xml, text)
Definition logging.h:405
#define CRM_CHECK(expr, failure_action)
Definition logging.h:245
#define crm_debug(fmt, args...)
Definition logging.h:400
#define crm_err(fmt, args...)
Definition logging.h:389
#define crm_log_xml_trace(xml, text)
Definition logging.h:410
#define crm_trace(fmt, args...)
Definition logging.h:402
Wrappers for and extensions to glib mainloop.
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition mainloop.c:949
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
struct mainloop_io_s mainloop_io_t
Definition mainloop.h:35
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition mainloop.c:943
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition nvpair.c:446
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
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
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_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition nvpair.c:301
#define PCMK__VALUE_CIB
#define PCMK__VALUE_CIB_NOTIFY
#define ETIME
#define ECOMM
Definition portability.h:86
const char * pcmk_strerror(int rc)
Definition results.c:149
@ pcmk_rc_ok
Definition results.h:162
#define pcmk_ok
Definition results.h:69
int pcmk_rc2legacy(int rc)
Definition results.c:546
#define pcmk_err_diff_resync
Definition results.h:83
@ pcmk__str_none
@ pcmk__str_casei
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition cib_types.h:185
int(* signoff)(cib_t *cib)
Definition cib_types.h:166
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
Definition cib_types.h:362
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition cib_types.h:159
int(* client_id)(const cib_t *cib, const char **async_id, const char **sync_id)
Get the given CIB connection's unique client identifier(s)
Definition cib_types.h:299
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition cib_types.h:248
int(* signon_raw)(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition cib_types.h:162
int(* free)(cib_t *cib)
Definition cib_types.h:168
enum cib_conn_type type
Definition cib_types.h:384
enum cib_state state
Definition cib_types.h:382
GList * notify_list
Definition cib_types.h:392
void * variant_opaque
Definition cib_types.h:389
void * delegate_fn
Definition cib_types.h:390
cib_api_operations_t * cmds
Definition cib_types.h:399
int call_timeout
Definition cib_types.h:388
enum cib_variant variant
Definition cib_types.h:385
char * user
Definition cib_types.h:403
int call_id
Definition cib_types.h:387
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
Dispatch function for an IPC connection used as mainloop source.
Definition mainloop.h:94
Wrappers for and extensions to libxml2.
void free_xml(xmlNode *child)
Definition xml.c:867
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
Definition xml.c:883
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
Definition xml.c:440
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition xml.c:720
xmlNode * pcmk__xml_parse(const char *input)
Definition xml_io.c:244
#define PCMK__XA_CIB_NOTIFY_ACTIVATE
#define PCMK__XA_CIB_OP
#define PCMK__XA_CIB_NOTIFY_TYPE
#define PCMK__XA_CIB_CALLID
#define PCMK__XA_CIB_CLIENTID
#define PCMK__XA_CIB_RC
#define PCMK__XA_T
#define PCMK__XE_CIB_CALLDATA
#define PCMK__XE_CIB_CALLBACK