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