pacemaker 2.1.5-a3f44794f94
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-2022 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;
38
40
41int cib_native_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
42 xmlNode * data, xmlNode ** output_data, int call_options);
43
44int cib_native_perform_op_delegate(cib_t * cib, const char *op, const char *host,
45 const char *section, xmlNode * data, xmlNode ** output_data,
46 int call_options, const char *user_name);
47
48int cib_native_free(cib_t * cib);
49int cib_native_signoff(cib_t * cib);
50int cib_native_signon(cib_t * cib, const char *name, enum cib_conn_type type);
51int cib_native_signon_raw(cib_t * cib, const char *name, enum cib_conn_type type, int *event_fd);
52
53int cib_native_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data));
54
55cib_t *
57{
58 cib_native_opaque_t *native = NULL;
59 cib_t *cib = cib_new_variant();
60
61 if (cib == NULL) {
62 return NULL;
63 }
64
65 native = calloc(1, sizeof(cib_native_opaque_t));
66
67 if (native == NULL) {
68 free(cib);
69 return NULL;
70 }
71
72 cib->variant = cib_native;
73 cib->variant_opaque = native;
74
75 native->ipc = NULL;
76 native->source = NULL;
77 native->dnotify_fn = NULL;
78
79 /* assign variant specific ops */
85
88
89 return cib;
90}
91
92int
94{
95 return cib_native_signon_raw(cib, name, type, NULL);
96}
97
98static int
99cib_native_dispatch_internal(const char *buffer, ssize_t length, gpointer userdata)
100{
101 const char *type = NULL;
102 xmlNode *msg = NULL;
103
104 cib_t *cib = userdata;
105
106 crm_trace("dispatching %p", userdata);
107
108 if (cib == NULL) {
109 crm_err("No CIB!");
110 return 0;
111 }
112
113 msg = string2xml(buffer);
114
115 if (msg == NULL) {
116 crm_warn("Received a NULL message from the CIB manager");
117 return 0;
118 }
119
120 /* do callbacks */
122 crm_trace("Activating %s callbacks...", type);
123 crm_log_xml_explicit(msg, "cib-reply");
124
125 if (pcmk__str_eq(type, T_CIB, pcmk__str_casei)) {
126 cib_native_callback(cib, msg, 0, 0);
127
128 } else if (pcmk__str_eq(type, T_CIB_NOTIFY, pcmk__str_casei)) {
129 g_list_foreach(cib->notify_list, cib_native_notify, msg);
130
131 } else {
132 crm_err("Unknown message type: %s", type);
133 }
134
135 free_xml(msg);
136 return 0;
137}
138
139static void
140cib_native_destroy(void *userdata)
141{
142 cib_t *cib = userdata;
143 cib_native_opaque_t *native = cib->variant_opaque;
144
145 crm_trace("destroying %p", userdata);
146 cib->state = cib_disconnected;
147 native->source = NULL;
148 native->ipc = NULL;
149
150 if (native->dnotify_fn) {
151 native->dnotify_fn(userdata);
152 }
153}
154
155int
156cib_native_signon_raw(cib_t * cib, const char *name, enum cib_conn_type type, int *async_fd)
157{
158 int rc = pcmk_ok;
159 const char *channel = NULL;
160 cib_native_opaque_t *native = cib->variant_opaque;
161
162 struct ipc_client_callbacks cib_callbacks = {
163 .dispatch = cib_native_dispatch_internal,
164 .destroy = cib_native_destroy
165 };
166
168
169 if (type == cib_command) {
171 channel = PCMK__SERVER_BASED_RW;
172
173 } else if (type == cib_command_nonblocking) {
175 channel = PCMK__SERVER_BASED_SHM;
176
177 } else if (type == cib_query) {
179 channel = PCMK__SERVER_BASED_RO;
180
181 } else {
182 return -ENOTCONN;
183 }
184
185 crm_trace("Connecting %s channel", channel);
186
187 if (async_fd != NULL) {
188 native->ipc = crm_ipc_new(channel, 0);
189
190 if (native->ipc && crm_ipc_connect(native->ipc)) {
191 *async_fd = crm_ipc_get_fd(native->ipc);
192
193 } else if (native->ipc) {
194 rc = -ENOTCONN;
195 }
196
197 } else {
198 native->source =
199 mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 512 * 1024 /* 512k */ , cib,
200 &cib_callbacks);
201 native->ipc = mainloop_get_ipc_client(native->source);
202 }
203
204 if (rc != pcmk_ok || native->ipc == NULL || crm_ipc_connected(native->ipc) == FALSE) {
205 crm_info("Could not connect to CIB manager for %s", name);
206 rc = -ENOTCONN;
207 }
208
209 if (rc == pcmk_ok) {
210 xmlNode *reply = NULL;
211 xmlNode *hello = create_xml_node(NULL, "cib_command");
212
213 crm_xml_add(hello, F_TYPE, T_CIB);
217
218 if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1, &reply) > 0) {
219 const char *msg_type = crm_element_value(reply, F_CIB_OPERATION);
220
221 rc = pcmk_ok;
222 crm_log_xml_trace(reply, "reg-reply");
223
224 if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
225 crm_info("Reply to CIB registration message has "
226 "unknown type '%s'", msg_type);
227 rc = -EPROTO;
228
229 } else {
230 native->token = crm_element_value_copy(reply, F_CIB_CLIENTID);
231 if (native->token == NULL) {
232 rc = -EPROTO;
233 }
234 }
235 free_xml(reply);
236
237 } else {
238 rc = -ECOMM;
239 }
240
241 free_xml(hello);
242 }
243
244 if (rc == pcmk_ok) {
245 crm_info("Successfully connected to CIB manager for %s", name);
246 return pcmk_ok;
247 }
248
249 crm_info("Connection to CIB manager for %s failed: %s",
250 name, pcmk_strerror(rc));
252 return rc;
253}
254
255int
257{
258 cib_native_opaque_t *native = cib->variant_opaque;
259
260 crm_debug("Disconnecting from the CIB manager");
261
262 cib_free_notify(cib);
263 remove_cib_op_callback(0, TRUE);
264
265 if (native->source != NULL) {
266 /* Attached to mainloop */
267 mainloop_del_ipc_client(native->source);
268 native->source = NULL;
269 native->ipc = NULL;
270
271 } else if (native->ipc) {
272 /* Not attached to mainloop */
273 crm_ipc_t *ipc = native->ipc;
274
275 native->ipc = NULL;
276 crm_ipc_close(ipc);
277 crm_ipc_destroy(ipc);
278 }
279
280 cib->state = cib_disconnected;
281 cib->type = cib_no_connection;
282
283 return pcmk_ok;
284}
285
286int
288{
289 int rc = pcmk_ok;
290
291 if (cib->state != cib_disconnected) {
292 rc = cib_native_signoff(cib);
293 }
294
295 if (cib->state == cib_disconnected) {
296 cib_native_opaque_t *native = cib->variant_opaque;
297
298 free(native->token);
299 free(cib->variant_opaque);
300 free(cib->cmds);
301 free(cib);
302 }
303
304 return rc;
305}
306
307int
308cib_native_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
309 xmlNode * data, xmlNode ** output_data, int call_options)
310{
311 return cib_native_perform_op_delegate(cib, op, host, section,
312 data, output_data, call_options, NULL);
313}
314
315int
316cib_native_perform_op_delegate(cib_t * cib, const char *op, const char *host, const char *section,
317 xmlNode * data, xmlNode ** output_data, int call_options,
318 const char *user_name)
319{
320 int rc = pcmk_ok;
321 int reply_id = 0;
322 enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
323
324 xmlNode *op_msg = NULL;
325 xmlNode *op_reply = NULL;
326
327 cib_native_opaque_t *native = cib->variant_opaque;
328
329 if (cib->state == cib_disconnected) {
330 return -ENOTCONN;
331 }
332
333 if (output_data != NULL) {
334 *output_data = NULL;
335 }
336
337 if (op == NULL) {
338 crm_err("No operation specified");
339 return -EINVAL;
340 }
341
342 if (call_options & cib_sync_call) {
343 pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
344 }
345
346 cib->call_id++;
347 if (cib->call_id < 1) {
348 cib->call_id = 1;
349 }
350
351 CRM_CHECK(native->token != NULL,;
352 );
353 op_msg =
354 cib_create_op(cib->call_id, native->token, op, host, section, data, call_options,
355 user_name);
356 if (op_msg == NULL) {
357 return -EPROTO;
358 }
359
360 crm_trace("Sending %s message to the CIB manager (timeout=%ds)", op, cib->call_timeout);
361 rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
362 free_xml(op_msg);
363
364 if (rc < 0) {
365 crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
366 cib->call_timeout, pcmk_strerror(rc), rc);
367 rc = -ECOMM;
368 goto done;
369 }
370
371 crm_log_xml_trace(op_reply, "Reply");
372
373 if (!(call_options & cib_sync_call)) {
374 crm_trace("Async call, returning %d", cib->call_id);
375 CRM_CHECK(cib->call_id != 0, return -ENOMSG);
376 free_xml(op_reply);
377 return cib->call_id;
378 }
379
380 rc = pcmk_ok;
381 crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
382 if (reply_id == cib->call_id) {
383 xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
384
385 crm_trace("Synchronous reply %d received", reply_id);
386 if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
387 rc = -EPROTO;
388 }
389
390 if (output_data == NULL || (call_options & cib_discard_reply)) {
391 crm_trace("Discarding reply");
392
393 } else if (tmp != NULL) {
394 *output_data = copy_xml(tmp);
395 }
396
397 } else if (reply_id <= 0) {
398 crm_err("Received bad reply: No id set");
399 crm_log_xml_err(op_reply, "Bad reply");
400 rc = -ENOMSG;
401 goto done;
402
403 } else {
404 crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
405 crm_log_xml_err(op_reply, "Old reply");
406 rc = -ENOMSG;
407 goto done;
408 }
409
410 if (op_reply == NULL && cib->state == cib_disconnected) {
411 rc = -ENOTCONN;
412
413 } else if (rc == pcmk_ok && op_reply == NULL) {
414 rc = -ETIME;
415 }
416
417 switch (rc) {
418 case pcmk_ok:
419 case -EPERM:
420 break;
421
422 /* This is an internal value that clients do not and should not care about */
424 rc = pcmk_ok;
425 break;
426
427 /* These indicate internal problems */
428 case -EPROTO:
429 case -ENOMSG:
430 crm_err("Call failed: %s", pcmk_strerror(rc));
431 if (op_reply) {
432 crm_log_xml_err(op_reply, "Invalid reply");
433 }
434 break;
435
436 default:
437 if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
438 crm_warn("Call failed: %s", pcmk_strerror(rc));
439 }
440 }
441
442 done:
443 if (crm_ipc_connected(native->ipc) == FALSE) {
444 crm_err("The CIB manager disconnected");
445 cib->state = cib_disconnected;
446 }
447
448 free_xml(op_reply);
449 return rc;
450}
451
452int
453cib_native_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data))
454{
455 cib_native_opaque_t *native = NULL;
456
457 if (cib == NULL) {
458 crm_err("No CIB!");
459 return FALSE;
460 }
461
462 native = cib->variant_opaque;
463 native->dnotify_fn = dnotify;
464
465 return pcmk_ok;
466}
467
468int
469cib_native_register_notification(cib_t * cib, const char *callback, int enabled)
470{
471 int rc = pcmk_ok;
472 xmlNode *notify_msg = create_xml_node(NULL, "cib-callback");
473 cib_native_opaque_t *native = cib->variant_opaque;
474
475 if (cib->state != cib_disconnected) {
477 crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
478 crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
479 rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
480 1000 * cib->call_timeout, NULL);
481 if (rc <= 0) {
482 crm_trace("Notification not registered: %d", rc);
483 rc = -ECOMM;
484 }
485 }
486
487 free_xml(notify_msg);
488 return rc;
489}
#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:470
#define F_CIB_NOTIFY_TYPE
Definition: internal.h:57
#define F_CIB_OPERATION
Definition: internal.h:40
cib_t * cib_new_variant(void)
Definition: cib_client.c:365
#define T_CIB
Definition: internal.h:66
#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:56
#define F_CIB_CALLDATA
Definition: internal.h:39
#define T_CIB_NOTIFY
Definition: internal.h:67
#define F_CIB_NOTIFY_ACTIVATE
Definition: internal.h:58
xmlNode * cib_create_op(int call_id, const char *token, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
Definition: cib_utils.c:437
void cib_native_notify(gpointer data, gpointer user_data)
Definition: cib_utils.c:519
const char * name
Definition: cib.c:24
void remove_cib_op_callback(int call_id, gboolean all_callbacks)
Definition: cib_client.c:684
void cib_free_notify(cib_t *cib)
Definition: cib_client.c:435
int cib_native_perform_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options)
Definition: cib_native.c:308
int cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options, const char *user_name)
Definition: cib_native.c:316
int cib_native_signon_raw(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition: cib_native.c:156
int cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
Definition: cib_native.c:93
cib_t * cib_native_new(void)
Definition: cib_native.c:56
struct cib_native_opaque_s cib_native_opaque_t
int cib_native_signoff(cib_t *cib)
Definition: cib_native.c:256
int cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
Definition: cib_native.c:469
int cib_native_free(cib_t *cib)
Definition: cib_native.c:287
int cib_native_set_connection_dnotify(cib_t *cib, void(*dnotify)(gpointer user_data))
Definition: cib_native.c:453
cib_conn_type
Definition: cib_types.h:42
@ cib_query
Definition: cib_types.h:44
@ cib_no_connection
Definition: cib_types.h:45
@ cib_command
Definition: cib_types.h:43
@ cib_command_nonblocking
Definition: cib_types.h:46
@ cib_sync_call
Definition: cib_types.h:61
@ cib_discard_reply
Definition: cib_types.h:55
@ cib_native
Definition: cib_types.h:30
@ cib_connected_command
Definition: cib_types.h:37
@ cib_connected_query
Definition: cib_types.h:38
@ cib_disconnected
Definition: cib_types.h:39
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
Definition: crm_internal.h:91
#define PCMK__SERVER_BASED_RW
Definition: crm_internal.h:92
#define PCMK__SERVER_BASED_SHM
Definition: crm_internal.h:93
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc_client.c:930
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.
Definition: ipc_client.c:1209
int crm_ipc_get_fd(crm_ipc_t *client)
Definition: ipc_client.c:956
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:970
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
Definition: ipc_client.c:847
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc_client.c:917
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:800
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
Definition: ipc_internal.h:205
#define PCMK__IPC_TIMEOUT
Definition: ipc_internal.h:51
#define crm_info(fmt, args...)
Definition: logging.h:362
#define crm_warn(fmt, args...)
Definition: logging.h:360
#define crm_log_xml_explicit(xml, text)
Definition: logging.h:375
#define crm_log_xml_err(xml, text)
Definition: logging.h:368
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:227
#define crm_debug(fmt, args...)
Definition: logging.h:364
#define crm_err(fmt, args...)
Definition: logging.h:359
#define crm_log_xml_trace(xml, text)
Definition: logging.h:373
#define crm_trace(fmt, args...)
Definition: logging.h:365
Wrappers for and extensions to glib mainloop.
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:947
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:916
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:33
void mainloop_del_ipc_client(mainloop_io_t *client)
Definition: mainloop.c:941
#define F_TYPE
Definition: msg_xml.h:69
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: nvpair.c:517
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Definition: nvpair.c:553
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:419
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
Definition: nvpair.c:714
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:323
#define ETIME
Definition: portability.h:150
#define ECOMM
Definition: portability.h:125
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:87
int(* signoff)(cib_t *cib)
Definition: cib_types.h:76
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
Definition: cib_types.h:73
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
Definition: cib_types.h:131
int(* signon_raw)(cib_t *cib, const char *name, enum cib_conn_type type, int *event_fd)
Definition: cib_types.h:74
int(* free)(cib_t *cib)
Definition: cib_types.h:77
enum cib_conn_type type
Definition: cib_types.h:168
enum cib_state state
Definition: cib_types.h:167
GList * notify_list
Definition: cib_types.h:176
void * variant_opaque
Definition: cib_types.h:173
void * delegate_fn
Definition: cib_types.h:174
cib_api_operations_t * cmds
Definition: cib_types.h:179
int call_timeout
Definition: cib_types.h:172
enum cib_variant variant
Definition: cib_types.h:169
int call_id
Definition: cib_types.h:171
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:930
void free_xml(xmlNode *child)
Definition: xml.c:885
xmlNode * get_message_xml(const xmlNode *msg, const char *field)
Definition: messages.c:154
xmlNode * copy_xml(xmlNode *src_node)
Definition: xml.c:891
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:749