pacemaker 2.1.6-6fdc9deea29
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-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
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 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 */
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
175static int
176cib_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 */
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
217static void
218cib_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
233static int
234cib_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
264static int
265cib_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) {
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
365static int
366cib_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
371static int
372cib_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
392static int
393cib_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
415static int
416cib_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
450static int
451cib_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
465cib_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}
#define F_CIB_CALLID
Definition internal.h:38
#define F_CIB_CALLOPTS
Definition internal.h:37
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
Definition cib_utils.c:500
#define F_CIB_NOTIFY_TYPE
Definition internal.h:56
#define F_CIB_OPERATION
Definition internal.h:40
cib_t * cib_new_variant(void)
Definition cib_client.c:595
#define T_CIB
Definition internal.h:65
#define PCMK__CIB_REQUEST_QUERY
Definition internal.h:24
#define F_CIB_CLIENTID
Definition internal.h:36
#define F_CIB_RC
Definition internal.h:44
#define F_CIB_CLIENTNAME
Definition internal.h:55
#define F_CIB_CALLDATA
Definition internal.h:39
#define T_CIB_NOTIFY
Definition internal.h:66
#define F_CIB_NOTIFY_ACTIVATE
Definition internal.h:57
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
void cib_native_notify(gpointer data, gpointer user_data)
Definition cib_utils.c:549
const char * name
Definition cib.c:24
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
Definition cib_client.c:709
void cib_free_notify(cib_t *cib)
Definition cib_client.c:665
cib_t * cib_native_new(void)
Definition cib_native.c:466
struct cib_native_opaque_s cib_native_opaque_t
cib_conn_type
Definition cib_types.h:46
@ cib_query
Definition cib_types.h:48
@ cib_no_connection
Definition cib_types.h:49
@ cib_command
Definition cib_types.h:47
@ cib_command_nonblocking
Definition cib_types.h:50
@ cib_sync_call
Definition cib_types.h:65
@ cib_discard_reply
Definition cib_types.h:59
@ cib_native
Definition cib_types.h:30
@ cib_connected_command
Definition cib_types.h:41
@ cib_connected_query
Definition cib_types.h:42
@ cib_disconnected
Definition cib_types.h:43
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:145
#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:955
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.
int crm_ipc_get_fd(crm_ipc_t *client)
Definition ipc_client.c:981
crm_ipc_flags
Definition ipc.h:145
@ crm_ipc_flags_none
Definition ipc.h:146
@ crm_ipc_client_response
Definition ipc.h:151
bool crm_ipc_connected(crm_ipc_t *client)
Definition ipc_client.c:995
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
Definition ipc_client.c:867
void crm_ipc_close(crm_ipc_t *client)
Definition ipc_client.c:942
struct crm_ipc_s crm_ipc_t
Definition ipc.h:165
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
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
#define PCMK__IPC_TIMEOUT
#define crm_info(fmt, args...)
Definition logging.h:378
#define crm_warn(fmt, args...)
Definition logging.h:376
#define crm_log_xml_explicit(xml, text)
Definition logging.h:391
#define crm_log_xml_err(xml, text)
Definition logging.h:384
#define CRM_CHECK(expr, failure_action)
Definition logging.h:235
#define crm_debug(fmt, args...)
Definition logging.h:380
#define crm_err(fmt, args...)
Definition logging.h:375
#define crm_log_xml_trace(xml, text)
Definition logging.h:389
#define crm_trace(fmt, args...)
Definition logging.h:381
Wrappers for and extensions to glib mainloop.
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition mainloop.c:946
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
struct mainloop_io_s mainloop_io_t
Definition mainloop.h:33
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition mainloop.c:940
#define F_TYPE
Definition msg_xml.h:82
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition nvpair.c:496
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
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
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_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition nvpair.c:302
#define ETIME
#define ECOMM
const char * pcmk_strerror(int rc)
Definition results.c:148
#define pcmk_ok
Definition results.h:68
#define pcmk_err_diff_resync
Definition results.h:77
@ pcmk__str_none
@ pcmk__str_casei
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition cib_types.h:98
int(* signoff)(cib_t *cib)
Definition cib_types.h:87
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition cib_types.h:84
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:199
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition cib_types.h:145
int(* signon_raw)(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition cib_types.h:85
int(* free)(cib_t *cib)
Definition cib_types.h:88
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
void * variant_opaque
Definition cib_types.h:210
void * delegate_fn
Definition cib_types.h:211
cib_api_operations_t * cmds
Definition cib_types.h:216
int call_timeout
Definition cib_types.h:209
enum cib_variant variant
Definition cib_types.h:206
int call_id
Definition cib_types.h:208
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
Dispatch function for an IPC connection used as mainloop source.
Definition mainloop.h:84
xmlNode * string2xml(const char *input)
Definition xml.c:831
void free_xml(xmlNode *child)
Definition xml.c:813
xmlNode * get_message_xml(const xmlNode *msg, const char *field)
Definition messages.c:154
xmlNode * copy_xml(xmlNode *src_node)
Definition xml.c:819
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition xml.c:677