pacemaker 3.0.1-16e74fc4da
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-2025 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#include <errno.h>
14#include <crm_internal.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <stdio.h>
18#include <stdarg.h>
19#include <string.h>
20
21#include <glib.h>
22
23#include <crm/crm.h>
24#include <crm/cib/internal.h>
25
26#include <crm/common/mainloop.h>
27#include <crm/common/xml.h>
28
29typedef struct cib_native_opaque_s {
30 char *token;
31 crm_ipc_t *ipc;
32 void (*dnotify_fn) (gpointer user_data);
33 mainloop_io_t *source;
35
36static int
37cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host,
38 const char *section, xmlNode *data,
39 xmlNode **output_data, int call_options,
40 const char *user_name)
41{
42 int rc = pcmk_ok;
43 int reply_id = 0;
44 enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
45
46 xmlNode *op_msg = NULL;
47 xmlNode *op_reply = NULL;
48
50
51 if (cib->state == cib_disconnected) {
52 return -ENOTCONN;
53 }
54
55 if (output_data != NULL) {
56 *output_data = NULL;
57 }
58
59 if (op == NULL) {
60 crm_err("No operation specified");
61 return -EINVAL;
62 }
63
64 if (call_options & cib_sync_call) {
65 pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
66 }
67
68 rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
69 NULL, &op_msg);
70 if (rc != pcmk_ok) {
71 return rc;
72 }
73
74 if (pcmk_is_set(call_options, cib_transaction)) {
75 rc = cib__extend_transaction(cib, op_msg);
76 goto done;
77 }
78
79 crm_trace("Sending %s message to the CIB manager (timeout=%ds)", op, cib->call_timeout);
80 rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
81
82 if (rc < 0) {
83 crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
84 cib->call_timeout, pcmk_strerror(rc), rc);
85 rc = -ECOMM;
86 goto done;
87 }
88
89 crm_log_xml_trace(op_reply, "Reply");
90
91 if (!(call_options & cib_sync_call)) {
92 crm_trace("Async call, returning %d", cib->call_id);
93 CRM_CHECK(cib->call_id != 0,
94 rc = -ENOMSG; goto done);
95 rc = cib->call_id;
96 goto done;
97 }
98
99 rc = pcmk_ok;
100 crm_element_value_int(op_reply, PCMK__XA_CIB_CALLID, &reply_id);
101 if (reply_id == cib->call_id) {
102 xmlNode *wrapper = pcmk__xe_first_child(op_reply, PCMK__XE_CIB_CALLDATA,
103 NULL, NULL);
104 xmlNode *tmp = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
105
106 crm_trace("Synchronous reply %d received", reply_id);
107 if (crm_element_value_int(op_reply, PCMK__XA_CIB_RC, &rc) != 0) {
108 rc = -EPROTO;
109 }
110
111 if (output_data == NULL || (call_options & cib_discard_reply)) {
112 crm_trace("Discarding reply");
113 } else {
114 *output_data = pcmk__xml_copy(NULL, tmp);
115 }
116
117 } else if (reply_id <= 0) {
118 crm_err("Received bad reply: No id set");
119 crm_log_xml_err(op_reply, "Bad reply");
120 rc = -ENOMSG;
121 goto done;
122
123 } else {
124 crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
125 crm_log_xml_err(op_reply, "Old reply");
126 rc = -ENOMSG;
127 goto done;
128 }
129
130 if (op_reply == NULL && cib->state == cib_disconnected) {
131 rc = -ENOTCONN;
132
133 } else if (rc == pcmk_ok && op_reply == NULL) {
134 rc = -ETIME;
135 }
136
137 switch (rc) {
138 case pcmk_ok:
139 case -EPERM:
140 break;
141
142 /* This is an internal value that clients do not and should not care about */
144 rc = pcmk_ok;
145 break;
146
147 /* These indicate internal problems */
148 case -EPROTO:
149 case -ENOMSG:
150 crm_err("Call failed: %s", pcmk_strerror(rc));
151 if (op_reply) {
152 crm_log_xml_err(op_reply, "Invalid reply");
153 }
154 break;
155
156 default:
157 if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
158 crm_warn("Call failed: %s", pcmk_strerror(rc));
159 }
160 }
161
162 done:
163 if (!crm_ipc_connected(native->ipc)) {
164 crm_err("The CIB manager disconnected");
165 cib->state = cib_disconnected;
166 }
167
168 pcmk__xml_free(op_msg);
169 pcmk__xml_free(op_reply);
170 return rc;
171}
172
173static int
174cib_native_dispatch_internal(const char *buffer, ssize_t length,
175 gpointer userdata)
176{
177 const char *type = NULL;
178 xmlNode *msg = NULL;
179
180 cib_t *cib = userdata;
181
182 crm_trace("dispatching %p", userdata);
183
184 if (cib == NULL) {
185 crm_err("No CIB!");
186 return 0;
187 }
188
189 msg = pcmk__xml_parse(buffer);
190
191 if (msg == NULL) {
192 crm_warn("Received a NULL message from the CIB manager");
193 return 0;
194 }
195
196 /* do callbacks */
198 crm_trace("Activating %s callbacks...", type);
199 crm_log_xml_explicit(msg, "cib-reply");
200
201 if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
202 cib_native_callback(cib, msg, 0, 0);
203
204 } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
205 g_list_foreach(cib->notify_list, cib_native_notify, msg);
206
207 } else {
208 crm_err("Unknown message type: %s", type);
209 }
210
211 pcmk__xml_free(msg);
212 return 0;
213}
214
215static void
216cib_native_destroy(void *userdata)
217{
218 cib_t *cib = userdata;
219 cib_native_opaque_t *native = cib->variant_opaque;
220
221 crm_trace("destroying %p", userdata);
222 cib->state = cib_disconnected;
223 native->source = NULL;
224 native->ipc = NULL;
225
226 if (native->dnotify_fn) {
227 native->dnotify_fn(userdata);
228 }
229}
230
231static int
232cib_native_signoff(cib_t *cib)
233{
234 cib_native_opaque_t *native = cib->variant_opaque;
235
236 crm_debug("Disconnecting from the CIB manager");
237
238 cib_free_notify(cib);
239 remove_cib_op_callback(0, TRUE);
240
241 if (native->source != NULL) {
242 /* Attached to mainloop */
243 mainloop_del_ipc_client(native->source);
244 native->source = NULL;
245 native->ipc = NULL;
246
247 } else if (native->ipc) {
248 /* Not attached to mainloop */
249 crm_ipc_t *ipc = native->ipc;
250
251 native->ipc = NULL;
252 crm_ipc_close(ipc);
253 crm_ipc_destroy(ipc);
254 }
255
256 cib->cmds->end_transaction(cib, false, cib_none);
257 cib->state = cib_disconnected;
258 cib->type = cib_no_connection;
259
260 return pcmk_ok;
261}
262
263static int
264cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
265{
266 int rc = pcmk_ok;
267 const char *channel = NULL;
268 cib_native_opaque_t *native = cib->variant_opaque;
269 xmlNode *hello = NULL;
270
271 struct ipc_client_callbacks cib_callbacks = {
272 .dispatch = cib_native_dispatch_internal,
273 .destroy = cib_native_destroy
274 };
275
276 if (name == NULL) {
277 name = pcmk__s(crm_system_name, "client");
278 }
279
281
282 if (type == cib_command) {
284 channel = PCMK__SERVER_BASED_RW;
285
286 } else if (type == cib_command_nonblocking) {
288 channel = PCMK__SERVER_BASED_SHM;
289
290 } else if (type == cib_query) {
292 channel = PCMK__SERVER_BASED_RO;
293
294 } else {
295 return -ENOTCONN;
296 }
297
298 crm_trace("Connecting %s channel", channel);
299
300 native->source = mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 0, cib,
301 &cib_callbacks);
302 native->ipc = mainloop_get_ipc_client(native->source);
303
304 if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
305 crm_info("Could not connect to CIB manager for %s", name);
306 rc = -ENOTCONN;
307 }
308
309 if (rc == pcmk_ok) {
310 rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
311 cib_sync_call, NULL, name, &hello);
312 }
313
314 if (rc == pcmk_ok) {
315 xmlNode *reply = NULL;
316
317 if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
318 &reply) > 0) {
319 const char *msg_type = crm_element_value(reply, PCMK__XA_CIB_OP);
320
321 crm_log_xml_trace(reply, "reg-reply");
322
323 if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
324 crm_info("Reply to CIB registration message has unknown type "
325 "'%s'",
326 msg_type);
327 rc = -EPROTO;
328
329 } else {
330 native->token = crm_element_value_copy(reply,
332 if (native->token == NULL) {
333 rc = -EPROTO;
334 }
335 }
336 pcmk__xml_free(reply);
337
338 } else {
339 rc = -ECOMM;
340 }
341 pcmk__xml_free(hello);
342 }
343
344 if (rc == pcmk_ok) {
345 crm_info("Successfully connected to CIB manager for %s", name);
346 return pcmk_ok;
347 }
348
349 crm_info("Connection to CIB manager for %s failed: %s",
350 name, pcmk_strerror(rc));
351 cib_native_signoff(cib);
352 return rc;
353}
354
355static int
356cib_native_free(cib_t *cib)
357{
358 int rc = pcmk_ok;
359
360 if (cib->state != cib_disconnected) {
361 rc = cib_native_signoff(cib);
362 }
363
364 if (cib->state == cib_disconnected) {
365 cib_native_opaque_t *native = cib->variant_opaque;
366
367 free(native->token);
368 free(cib->variant_opaque);
369 free(cib->cmds);
370 free(cib->user);
371 free(cib);
372 }
373
374 return rc;
375}
376
377static int
378cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
379{
380 int rc = pcmk_ok;
381 xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
382 cib_native_opaque_t *native = cib->variant_opaque;
383
384 if (cib->state != cib_disconnected) {
386 crm_xml_add(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
387 crm_xml_add_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
388 rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
389 1000 * cib->call_timeout, NULL);
390 if (rc <= 0) {
391 crm_trace("Notification not registered: %d", rc);
392 rc = -ECOMM;
393 }
394 }
395
396 pcmk__xml_free(notify_msg);
397 return rc;
398}
399
400static int
401cib_native_set_connection_dnotify(cib_t *cib,
402 void (*dnotify) (gpointer user_data))
403{
404 cib_native_opaque_t *native = NULL;
405
406 if (cib == NULL) {
407 crm_err("No CIB!");
408 return FALSE;
409 }
410
411 native = cib->variant_opaque;
412 native->dnotify_fn = dnotify;
413
414 return pcmk_ok;
415}
416
435static int
436cib_native_client_id(const cib_t *cib, const char **async_id,
437 const char **sync_id)
438{
439 cib_native_opaque_t *native = cib->variant_opaque;
440
441 if (async_id != NULL) {
442 *async_id = native->token;
443 }
444 if (sync_id != NULL) {
445 *sync_id = native->token;
446 }
447 return pcmk_ok;
448}
449
450cib_t *
452{
453 cib_native_opaque_t *native = NULL;
454 cib_t *cib = cib_new_variant();
455
456 if (cib == NULL) {
457 return NULL;
458 }
459
460 native = calloc(1, sizeof(cib_native_opaque_t));
461
462 if (native == NULL) {
463 free(cib);
464 return NULL;
465 }
466
467 cib->variant = cib_native;
468 cib->variant_opaque = native;
469
470 native->ipc = NULL;
471 native->source = NULL;
472 native->dnotify_fn = NULL;
473
474 /* assign variant specific ops */
475 cib->delegate_fn = cib_native_perform_op_delegate;
476 cib->cmds->signon = cib_native_signon;
477 cib->cmds->signoff = cib_native_signoff;
478 cib->cmds->free = cib_native_free;
479
480 cib->cmds->register_notification = cib_native_register_notification;
481 cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
482
483 cib->cmds->client_id = cib_native_client_id;
484
485 return cib;
486}
int cib__extend_transaction(cib_t *cib, xmlNode *request)
Definition cib_utils.c:637
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
Definition cib_utils.c:665
cib_t * cib_new_variant(void)
Definition cib_client.c:622
#define PCMK__CIB_REQUEST_QUERY
Definition internal.h:30
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:558
void cib_native_notify(gpointer data, gpointer user_data)
Definition cib_utils.c:713
const char * name
Definition cib.c:26
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
Definition cib_client.c:730
void cib_free_notify(cib_t *cib)
Definition cib_client.c:686
cib_t * cib_native_new(void)
Definition cib_native.c:451
struct cib_native_opaque_s cib_native_opaque_t
cib_conn_type
Definition cib_types.h:45
@ cib_query
Definition cib_types.h:49
@ cib_no_connection
Definition cib_types.h:51
@ cib_command
Definition cib_types.h:46
@ cib_command_nonblocking
Definition cib_types.h:52
@ cib_none
Definition cib_types.h:56
@ cib_transaction
Process request when the client commits the active transaction.
Definition cib_types.h:87
@ cib_sync_call
Definition cib_types.h:112
@ cib_discard_reply
Definition cib_types.h:61
@ cib_native
Definition cib_types.h:30
@ cib_connected_command
Definition cib_types.h:37
@ cib_connected_query
Definition cib_types.h:40
@ cib_disconnected
Definition cib_types.h:42
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
Definition util.h:80
pcmk__cpg_host_t host
Definition cpg.c:4
char data[0]
Definition cpg.c:10
enum pcmk_ipc_server type
Definition cpg.c:3
A dumping ground.
#define CRM_OP_REGISTER
Definition crm.h:122
char * crm_system_name
Definition utils.c:45
#define PCMK__SERVER_BASED_RO
#define PCMK__SERVER_BASED_RW
#define PCMK__SERVER_BASED_SHM
void crm_ipc_destroy(crm_ipc_t *client)
Definition ipc_client.c:976
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:135
@ crm_ipc_flags_none
Definition ipc.h:136
@ crm_ipc_client_response
A response is expected in reply.
Definition ipc.h:142
bool crm_ipc_connected(crm_ipc_t *client)
void crm_ipc_close(crm_ipc_t *client)
Definition ipc_client.c:963
struct crm_ipc_s crm_ipc_t
Definition ipc.h:159
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
#define PCMK__IPC_TIMEOUT
#define crm_info(fmt, args...)
Definition logging.h:365
#define crm_warn(fmt, args...)
Definition logging.h:360
#define crm_log_xml_explicit(xml, text)
Definition logging.h:380
#define crm_log_xml_err(xml, text)
Definition logging.h:373
#define CRM_CHECK(expr, failure_action)
Definition logging.h:213
#define crm_debug(fmt, args...)
Definition logging.h:368
#define crm_err(fmt, args...)
Definition logging.h:357
#define crm_log_xml_trace(xml, text)
Definition logging.h:378
#define crm_trace(fmt, args...)
Definition logging.h:370
Wrappers for and extensions to glib mainloop.
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition mainloop.c:953
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:922
struct mainloop_io_s mainloop_io_t
Definition mainloop.h:41
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition mainloop.c:947
#define PCMK__VALUE_CIB
#define PCMK__VALUE_CIB_NOTIFY
#define ETIME
Definition portability.h:66
#define ECOMM
Definition portability.h:41
const char * pcmk_strerror(int rc)
Definition results.c:257
#define pcmk_ok
Definition results.h:65
#define pcmk_err_diff_resync
Definition results.h:79
@ pcmk__str_none
@ pcmk__str_casei
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition cib_types.h:141
int(* signoff)(cib_t *cib)
Definition cib_types.h:127
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
Definition cib_types.h:292
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition cib_types.h:124
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:229
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition cib_types.h:178
int(* free)(cib_t *cib)
Definition cib_types.h:129
enum cib_conn_type type
Definition cib_types.h:314
enum cib_state state
Definition cib_types.h:312
GList * notify_list
Definition cib_types.h:322
void * variant_opaque
Definition cib_types.h:319
void * delegate_fn
Definition cib_types.h:320
cib_api_operations_t * cmds
Definition cib_types.h:325
int call_timeout
Definition cib_types.h:318
enum cib_variant variant
Definition cib_types.h:315
char * user
Definition cib_types.h:329
int call_id
Definition cib_types.h:317
Wrappers for and extensions to libxml2.
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
Definition xml_element.c:43
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
Definition xml.c:832
void pcmk__xml_free(xmlNode *xml)
Definition xml.c:816
xmlNode * pcmk__xml_parse(const char *input)
Definition xml_io.c:167
#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