1 /*
2 * Copyright 2004 International Business Machines
3 * Later changes copyright 2004-2026 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 <stdbool.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21
22 #include <glib.h>
23
24 #include <crm/crm.h>
25 #include <crm/cib/internal.h>
26
27 #include <crm/common/mainloop.h>
28 #include <crm/common/xml.h>
29
30 typedef struct {
31 char *token;
32 crm_ipc_t *ipc;
33 void (*dnotify_fn) (gpointer user_data);
34 mainloop_io_t *source;
35 } cib_native_opaque_t;
36
37 static bool
38 ack_is_failure(const xmlNode *reply)
39 {
40 int status = 0;
41
42 pcmk__xe_get_int(reply, PCMK_XA_STATUS, &status);
43 if (status != CRM_EX_OK) {
44 pcmk__err("Received error response from based: %s", crm_exit_str(status));
45 return true;
46 }
47
48 return false;
49 }
50
51 static int
52 cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host,
53 const char *section, xmlNode *data,
54 xmlNode **output_data, int call_options,
55 const char *user_name)
56 {
57 int rc = pcmk_ok;
58 int reply_id = 0;
59 enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
60
61 xmlNode *op_msg = NULL;
62 xmlNode *op_reply = NULL;
63
64 cib_native_opaque_t *native = cib->variant_opaque;
65
66 if (cib->state == cib_disconnected) {
67 return -ENOTCONN;
68 }
69
70 if (output_data != NULL) {
71 *output_data = NULL;
72 }
73
74 if (op == NULL) {
75 pcmk__err("No operation specified");
76 return -EINVAL;
77 }
78
79 if (call_options & cib_sync_call) {
80 pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
81 }
82
83 rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
84 NULL, &op_msg);
85 rc = pcmk_rc2legacy(rc);
86 if (rc != pcmk_ok) {
87 return rc;
88 }
89
90 if (pcmk__is_set(call_options, cib_transaction)) {
91 rc = cib__extend_transaction(cib, op_msg);
92 rc = pcmk_rc2legacy(rc);
93 goto done;
94 }
95
96 pcmk__trace("Sending %s message to the CIB manager (timeout=%ds)", op,
97 cib->call_timeout);
98 rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
99
100 if (rc < 0) {
101 pcmk__err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
102 cib->call_timeout, pcmk_strerror(rc), rc);
103 rc = -ECOMM;
104 goto done;
105 }
106
107 /* The only reason we can receive an ACK here is if dispatch_common ->
108 * pcmk__client_data2xml processed something that's not valid XML.
109 * dispatch_common does not return ACK, unlike other daemons.
110 */
111 if (pcmk__xe_is(op_reply, PCMK__XE_ACK) && ack_is_failure(op_reply)) {
112 rc = -EPROTO;
113 goto done;
114 }
115
116 pcmk__log_xml_trace(op_reply, "Reply");
117
118 if (!(call_options & cib_sync_call)) {
119 pcmk__trace("Async call, returning %d", cib->call_id);
120 CRM_CHECK(cib->call_id != 0,
121 rc = -ENOMSG; goto done);
122 rc = cib->call_id;
123 goto done;
124 }
125
126 rc = pcmk_ok;
127 pcmk__xe_get_int(op_reply, PCMK__XA_CIB_CALLID, &reply_id);
128 if (reply_id == cib->call_id) {
129 xmlNode *tmp = cib__get_calldata(op_reply);
130
131 pcmk__trace("Synchronous reply %d received", reply_id);
132 if (pcmk__xe_get_int(op_reply, PCMK__XA_CIB_RC, &rc) != pcmk_rc_ok) {
133 rc = -EPROTO;
134 }
135
136 if (output_data == NULL || (call_options & cib_discard_reply)) {
137 pcmk__trace("Discarding reply");
138 } else {
139 *output_data = pcmk__xml_copy(NULL, tmp);
140 }
141
142 } else if (reply_id <= 0) {
143 pcmk__err("Received bad reply: No id set");
144 pcmk__log_xml_err(op_reply, "Bad reply");
145 rc = -ENOMSG;
146 goto done;
147
148 } else {
149 pcmk__err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
150 pcmk__log_xml_err(op_reply, "Old reply");
151 rc = -ENOMSG;
152 goto done;
153 }
154
155 if (op_reply == NULL && cib->state == cib_disconnected) {
156 rc = -ENOTCONN;
157
158 } else if (rc == pcmk_ok && op_reply == NULL) {
159 rc = -ETIME;
160 }
161
162 switch (rc) {
163 case pcmk_ok:
164 case -EPERM:
165 break;
166
167 /* These indicate internal problems */
168 case -EPROTO:
169 case -ENOMSG:
170 pcmk__err("Call failed: %s", pcmk_strerror(rc));
171 if (op_reply) {
172 pcmk__log_xml_err(op_reply, "Invalid reply");
173 }
174 break;
175
176 default:
177 if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
178 pcmk__warn("Call failed: %s", pcmk_strerror(rc));
179 }
180 }
181
182 done:
183 if (!crm_ipc_connected(native->ipc)) {
184 pcmk__err("The CIB manager disconnected");
185 cib->state = cib_disconnected;
186 }
187
188 pcmk__xml_free(op_msg);
189 pcmk__xml_free(op_reply);
190 return rc;
191 }
192
193 static int
194 cib_native_dispatch_internal(const char *buffer, ssize_t length,
195 gpointer userdata)
196 {
197 const char *type = NULL;
198 xmlNode *msg = NULL;
199
200 cib_t *cib = userdata;
201
202 pcmk__trace("dispatching %p", userdata);
203
204 if (cib == NULL) {
205 pcmk__err("No CIB!");
206 return 0;
207 }
208
209 msg = pcmk__xml_parse(buffer);
210
211 if (msg == NULL) {
212 pcmk__warn("Received a NULL message from the CIB manager");
213 return 0;
214 }
215
216 /* do callbacks */
217 type = pcmk__xe_get(msg, PCMK__XA_T);
218 pcmk__trace("Activating %s callbacks...", type);
219 crm_log_xml_explicit(msg, "cib-reply");
220
221 if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
222 cib_native_callback(cib, msg, 0, 0);
223
224 } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
225 g_list_foreach(cib->notify_list, cib_native_notify, msg);
226
227 } else {
228 pcmk__err("Unknown message type: %s", type);
229 }
230
231 pcmk__xml_free(msg);
232 return 0;
233 }
234
235 static void
236 cib_native_destroy(void *userdata)
237 {
238 cib_t *cib = userdata;
239 cib_native_opaque_t *native = cib->variant_opaque;
240
241 pcmk__trace("destroying %p", userdata);
242 cib->state = cib_disconnected;
243 native->source = NULL;
244 native->ipc = NULL;
245
246 if (native->dnotify_fn) {
247 native->dnotify_fn(userdata);
248 }
249 }
250
251 static int
252 cib_native_signoff(cib_t *cib)
253 {
254 cib_native_opaque_t *native = cib->variant_opaque;
255
256 pcmk__debug("Disconnecting from the CIB manager");
257
258 cib_free_notify(cib);
259 remove_cib_op_callback(0, TRUE);
260
261 if (native->source != NULL) {
262 /* Attached to mainloop */
263 g_clear_pointer(&native->source, mainloop_del_ipc_client);
264 native->ipc = NULL;
265
266 } else if (native->ipc) {
267 /* Not attached to mainloop */
268 crm_ipc_t *ipc = native->ipc;
269
270 native->ipc = NULL;
271 crm_ipc_close(ipc);
272 crm_ipc_destroy(ipc);
273 }
274
275 cib->cmds->end_transaction(cib, false, cib_none);
276 cib->state = cib_disconnected;
277 cib->type = cib_no_connection;
278
279 return pcmk_ok;
280 }
281
282 static int
283 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
284 {
285 int rc = pcmk_ok;
286 const char *channel = NULL;
287 cib_native_opaque_t *native = cib->variant_opaque;
288 xmlNode *hello = NULL;
289
290 struct ipc_client_callbacks cib_callbacks = {
291 .dispatch = cib_native_dispatch_internal,
292 .destroy = cib_native_destroy
293 };
294
295 if (name == NULL) {
296 name = pcmk__s(crm_system_name, "client");
297 }
298
299 cib->call_timeout = PCMK__IPC_TIMEOUT;
300
301 if (type == cib_command) {
302 cib->state = cib_connected_command;
303 channel = PCMK__SERVER_BASED_RW;
304
305 } else if (type == cib_command_nonblocking) {
306 cib->state = cib_connected_command;
307 channel = PCMK__SERVER_BASED_SHM;
308
309 } else if (type == cib_query) {
310 cib->state = cib_connected_query;
311 channel = PCMK__SERVER_BASED_RO;
312
313 } else {
314 return -ENOTCONN;
315 }
316
317 pcmk__trace("Connecting %s channel", channel);
318
319 native->source = mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 0, cib,
320 &cib_callbacks);
321 native->ipc = mainloop_get_ipc_client(native->source);
322
323 if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
324 pcmk__info("Could not connect to CIB manager for %s", name);
325 rc = -ENOTCONN;
326 }
327
328 if (rc == pcmk_ok) {
329 rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
330 cib_sync_call, NULL, name, &hello);
331 rc = pcmk_rc2legacy(rc);
332 }
333
334 if (rc == pcmk_ok) {
335 xmlNode *reply = NULL;
336 const char *msg_type = NULL;
337
338 if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
339 &reply) <= 0) {
340 rc = -ECOMM;
341 goto done;
342 }
343
344 /* The only reason we can receive an ACK here is if dispatch_common ->
345 * pcmk__client_data2xml processed something that's not valid XML.
346 * dispatch_common does not return ACK, unlike other daemons.
347 */
348 if (pcmk__xe_is(reply, PCMK__XE_ACK) && ack_is_failure(reply)) {
349 rc = -EPROTO;
350 pcmk__xml_free(reply);
351 goto done;
352 }
353
354 msg_type = pcmk__xe_get(reply, PCMK__XA_CIB_OP);
355
356 pcmk__log_xml_trace(reply, "reg-reply");
357
358 if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
359 pcmk__info("Reply to CIB registration message has unknown type "
360 "'%s'",
361 msg_type);
362 rc = -EPROTO;
363
364 } else {
365 native->token = pcmk__xe_get_copy(reply, PCMK__XA_CIB_CLIENTID);
366 if (native->token == NULL) {
367 rc = -EPROTO;
368 }
369 }
370
371 pcmk__xml_free(reply);
372 }
373
374 done:
375 pcmk__xml_free(hello);
376
377 if (rc == pcmk_ok) {
378 pcmk__info("Successfully connected to CIB manager for %s", name);
379 return pcmk_ok;
380 }
381
382 pcmk__info("Connection to CIB manager for %s failed: %s", name,
383 pcmk_strerror(rc));
384 cib_native_signoff(cib);
385 return rc;
386 }
387
388 static int
389 cib_native_free(cib_t *cib)
390 {
391 int rc = pcmk_ok;
392
393 if (cib->state != cib_disconnected) {
394 rc = cib_native_signoff(cib);
395 }
396
397 if (cib->state == cib_disconnected) {
398 cib_native_opaque_t *native = cib->variant_opaque;
399
400 free(native->token);
401 free(cib->variant_opaque);
402 free(cib->cmds);
403 free(cib->user);
404 free(cib);
405 }
406
407 return rc;
408 }
409
410 static int
411 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
412 {
413 int rc = pcmk_ok;
414 xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
415 cib_native_opaque_t *native = cib->variant_opaque;
416
417 if (cib->state != cib_disconnected) {
418 pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
419 pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
420 pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
421
422 /* We don't care about the reply here, so there's no need to check
423 * if we got an ACK in response.
424 */
425 rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
426 1000 * cib->call_timeout, NULL);
427 if (rc <= 0) {
428 pcmk__trace("Notification not registered: %d", rc);
429 rc = -ECOMM;
430 }
431 }
432
433 pcmk__xml_free(notify_msg);
434 return rc;
435 }
436
437 static int
438 cib_native_set_connection_dnotify(cib_t *cib,
439 void (*dnotify) (gpointer user_data))
440 {
441 cib_native_opaque_t *native = NULL;
442
443 if (cib == NULL) {
444 pcmk__err("No CIB!");
445 return FALSE;
446 }
447
448 native = cib->variant_opaque;
449 native->dnotify_fn = dnotify;
450
451 return pcmk_ok;
452 }
453
454 /*!
455 * \internal
456 * \brief Get the given CIB connection's unique client identifier
457 *
458 * These can be used to check whether this client requested the action that
459 * triggered a CIB notification.
460 *
461 * \param[in] cib CIB connection
462 * \param[out] async_id If not \p NULL, where to store asynchronous client ID
463 * \param[out] sync_id If not \p NULL, where to store synchronous client ID
464 *
465 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
466 *
467 * \note This is the \p cib_native variant implementation of
468 * \p cib_api_operations_t:client_id().
469 * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
470 * \note The client ID is assigned during CIB sign-on.
471 */
472 static int
473 cib_native_client_id(const cib_t *cib, const char **async_id,
474 const char **sync_id)
475 {
476 cib_native_opaque_t *native = cib->variant_opaque;
477
478 if (async_id != NULL) {
479 *async_id = native->token;
480 }
481 if (sync_id != NULL) {
482 *sync_id = native->token;
483 }
484 return pcmk_ok;
485 }
486
487 cib_t *
488 cib_native_new(void)
489 {
490 cib_native_opaque_t *native = NULL;
|
(1) Event alloc_arg: |
"cib_new_variant" allocates memory that is stored into "cib_new_variant()->cmds". [details] |
|
(2) Event var_assign: |
Assigning: "cib->cmds" = "cib_new_variant()->cmds". |
| Also see events: |
[leaked_storage] |
491 cib_t *cib = cib_new_variant();
492
|
(3) Event path: |
Condition "cib == NULL", taking false branch. |
493 if (cib == NULL) {
494 return NULL;
495 }
496
497 native = calloc(1, sizeof(cib_native_opaque_t));
498
|
(4) Event path: |
Condition "native == NULL", taking true branch. |
499 if (native == NULL) {
|
CID (unavailable; MK=5145b60158a6c3f90f7309d42b6744d8) (#1 of 1): Resource leak (RESOURCE_LEAK): |
|
(5) Event leaked_storage: |
Freeing "cib" without freeing its pointer field "cmds" leaks the storage that "cmds" points to. |
| Also see events: |
[alloc_arg][var_assign] |
500 free(cib);
501 return NULL;
502 }
503
504 cib->variant = cib_native;
505 cib->variant_opaque = native;
506
507 native->ipc = NULL;
508 native->source = NULL;
509 native->dnotify_fn = NULL;
510
511 /* assign variant specific ops */
512 cib->delegate_fn = cib_native_perform_op_delegate;
513 cib->cmds->signon = cib_native_signon;
514 cib->cmds->signoff = cib_native_signoff;
515 cib->cmds->free = cib_native_free;
516
517 cib->cmds->register_notification = cib_native_register_notification;
518 cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
519
520 cib->cmds->client_id = cib_native_client_id;
521
522 return cib;
523 }
524