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 *wrapper = pcmk__xe_first_child(op_reply, PCMK__XE_CIB_CALLDATA,
130 NULL, NULL);
131 xmlNode *tmp = pcmk__xe_first_child(wrapper, NULL, NULL, NULL);
132
133 pcmk__trace("Synchronous reply %d received", reply_id);
134 if (pcmk__xe_get_int(op_reply, PCMK__XA_CIB_RC, &rc) != pcmk_rc_ok) {
135 rc = -EPROTO;
136 }
137
138 if (output_data == NULL || (call_options & cib_discard_reply)) {
139 pcmk__trace("Discarding reply");
140 } else {
141 *output_data = pcmk__xml_copy(NULL, tmp);
142 }
143
144 } else if (reply_id <= 0) {
145 pcmk__err("Received bad reply: No id set");
146 pcmk__log_xml_err(op_reply, "Bad reply");
147 rc = -ENOMSG;
148 goto done;
149
150 } else {
151 pcmk__err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
152 pcmk__log_xml_err(op_reply, "Old reply");
153 rc = -ENOMSG;
154 goto done;
155 }
156
157 if (op_reply == NULL && cib->state == cib_disconnected) {
158 rc = -ENOTCONN;
159
160 } else if (rc == pcmk_ok && op_reply == NULL) {
161 rc = -ETIME;
162 }
163
164 switch (rc) {
165 case pcmk_ok:
166 case -EPERM:
167 break;
168
169 /* These indicate internal problems */
170 case -EPROTO:
171 case -ENOMSG:
172 pcmk__err("Call failed: %s", pcmk_strerror(rc));
173 if (op_reply) {
174 pcmk__log_xml_err(op_reply, "Invalid reply");
175 }
176 break;
177
178 default:
179 if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
180 pcmk__warn("Call failed: %s", pcmk_strerror(rc));
181 }
182 }
183
184 done:
185 if (!crm_ipc_connected(native->ipc)) {
186 pcmk__err("The CIB manager disconnected");
187 cib->state = cib_disconnected;
188 }
189
190 pcmk__xml_free(op_msg);
191 pcmk__xml_free(op_reply);
192 return rc;
193 }
194
195 static int
196 cib_native_dispatch_internal(const char *buffer, ssize_t length,
197 gpointer userdata)
198 {
199 const char *type = NULL;
200 xmlNode *msg = NULL;
201
202 cib_t *cib = userdata;
203
204 pcmk__trace("dispatching %p", userdata);
205
206 if (cib == NULL) {
207 pcmk__err("No CIB!");
208 return 0;
209 }
210
211 msg = pcmk__xml_parse(buffer);
212
213 if (msg == NULL) {
214 pcmk__warn("Received a NULL message from the CIB manager");
215 return 0;
216 }
217
218 /* do callbacks */
219 type = pcmk__xe_get(msg, PCMK__XA_T);
220 pcmk__trace("Activating %s callbacks...", type);
221 crm_log_xml_explicit(msg, "cib-reply");
222
223 if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
224 cib_native_callback(cib, msg, 0, 0);
225
226 } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
227 g_list_foreach(cib->notify_list, cib_native_notify, msg);
228
229 } else {
230 pcmk__err("Unknown message type: %s", type);
231 }
232
233 pcmk__xml_free(msg);
234 return 0;
235 }
236
237 static void
238 cib_native_destroy(void *userdata)
239 {
240 cib_t *cib = userdata;
241 cib_native_opaque_t *native = cib->variant_opaque;
242
243 pcmk__trace("destroying %p", userdata);
244 cib->state = cib_disconnected;
245 native->source = NULL;
246 native->ipc = NULL;
247
248 if (native->dnotify_fn) {
249 native->dnotify_fn(userdata);
250 }
251 }
252
253 static int
254 cib_native_signoff(cib_t *cib)
255 {
256 cib_native_opaque_t *native = cib->variant_opaque;
257
258 pcmk__debug("Disconnecting from the CIB manager");
259
260 cib_free_notify(cib);
261 remove_cib_op_callback(0, TRUE);
262
263 if (native->source != NULL) {
264 /* Attached to mainloop */
265 g_clear_pointer(&native->source, mainloop_del_ipc_client);
266 native->ipc = NULL;
267
268 } else if (native->ipc) {
269 /* Not attached to mainloop */
270 crm_ipc_t *ipc = native->ipc;
271
272 native->ipc = NULL;
273 crm_ipc_close(ipc);
274 crm_ipc_destroy(ipc);
275 }
276
277 cib->cmds->end_transaction(cib, false, cib_none);
278 cib->state = cib_disconnected;
279 cib->type = cib_no_connection;
280
281 return pcmk_ok;
282 }
283
284 static int
285 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
286 {
287 int rc = pcmk_ok;
288 const char *channel = NULL;
289 cib_native_opaque_t *native = cib->variant_opaque;
290 xmlNode *hello = NULL;
291
292 struct ipc_client_callbacks cib_callbacks = {
293 .dispatch = cib_native_dispatch_internal,
294 .destroy = cib_native_destroy
295 };
296
297 if (name == NULL) {
298 name = pcmk__s(crm_system_name, "client");
299 }
300
301 cib->call_timeout = PCMK__IPC_TIMEOUT;
302
303 if (type == cib_command) {
304 cib->state = cib_connected_command;
305 channel = PCMK__SERVER_BASED_RW;
306
307 } else if (type == cib_command_nonblocking) {
308 cib->state = cib_connected_command;
309 channel = PCMK__SERVER_BASED_SHM;
310
311 } else if (type == cib_query) {
312 cib->state = cib_connected_query;
313 channel = PCMK__SERVER_BASED_RO;
314
315 } else {
316 return -ENOTCONN;
317 }
318
319 pcmk__trace("Connecting %s channel", channel);
320
321 native->source = mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 0, cib,
322 &cib_callbacks);
323 native->ipc = mainloop_get_ipc_client(native->source);
324
325 if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
326 pcmk__info("Could not connect to CIB manager for %s", name);
327 rc = -ENOTCONN;
328 }
329
330 if (rc == pcmk_ok) {
331 rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
332 cib_sync_call, NULL, name, &hello);
333 rc = pcmk_rc2legacy(rc);
334 }
335
336 if (rc == pcmk_ok) {
337 xmlNode *reply = NULL;
338 const char *msg_type = NULL;
339
340 if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
341 &reply) <= 0) {
342 rc = -ECOMM;
343 goto done;
344 }
345
346 /* The only reason we can receive an ACK here is if dispatch_common ->
347 * pcmk__client_data2xml processed something that's not valid XML.
348 * dispatch_common does not return ACK, unlike other daemons.
349 */
350 if (pcmk__xe_is(reply, PCMK__XE_ACK) && ack_is_failure(reply)) {
351 rc = -EPROTO;
352 pcmk__xml_free(reply);
353 goto done;
354 }
355
356 msg_type = pcmk__xe_get(reply, PCMK__XA_CIB_OP);
357
358 pcmk__log_xml_trace(reply, "reg-reply");
359
360 if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
361 pcmk__info("Reply to CIB registration message has unknown type "
362 "'%s'",
363 msg_type);
364 rc = -EPROTO;
365
366 } else {
367 native->token = pcmk__xe_get_copy(reply, PCMK__XA_CIB_CLIENTID);
368 if (native->token == NULL) {
369 rc = -EPROTO;
370 }
371 }
372
373 pcmk__xml_free(reply);
374 }
375
376 done:
377 pcmk__xml_free(hello);
378
379 if (rc == pcmk_ok) {
380 pcmk__info("Successfully connected to CIB manager for %s", name);
381 return pcmk_ok;
382 }
383
384 pcmk__info("Connection to CIB manager for %s failed: %s", name,
385 pcmk_strerror(rc));
386 cib_native_signoff(cib);
387 return rc;
388 }
389
390 static int
391 cib_native_free(cib_t *cib)
392 {
393 int rc = pcmk_ok;
394
395 if (cib->state != cib_disconnected) {
396 rc = cib_native_signoff(cib);
397 }
398
399 if (cib->state == cib_disconnected) {
400 cib_native_opaque_t *native = cib->variant_opaque;
401
402 free(native->token);
403 free(cib->variant_opaque);
404 free(cib->cmds);
405 free(cib->user);
406 free(cib);
407 }
408
409 return rc;
410 }
411
412 static int
413 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
414 {
415 int rc = pcmk_ok;
416 xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
417 cib_native_opaque_t *native = cib->variant_opaque;
418
419 if (cib->state != cib_disconnected) {
420 pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
421 pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
422 pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
423
424 /* We don't care about the reply here, so there's no need to check
425 * if we got an ACK in response.
426 */
427 rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
428 1000 * cib->call_timeout, NULL);
429 if (rc <= 0) {
430 pcmk__trace("Notification not registered: %d", rc);
431 rc = -ECOMM;
432 }
433 }
434
435 pcmk__xml_free(notify_msg);
436 return rc;
437 }
438
439 static int
440 cib_native_set_connection_dnotify(cib_t *cib,
441 void (*dnotify) (gpointer user_data))
442 {
443 cib_native_opaque_t *native = NULL;
444
445 if (cib == NULL) {
446 pcmk__err("No CIB!");
447 return FALSE;
448 }
449
450 native = cib->variant_opaque;
451 native->dnotify_fn = dnotify;
452
453 return pcmk_ok;
454 }
455
456 /*!
457 * \internal
458 * \brief Get the given CIB connection's unique client identifier
459 *
460 * These can be used to check whether this client requested the action that
461 * triggered a CIB notification.
462 *
463 * \param[in] cib CIB connection
464 * \param[out] async_id If not \p NULL, where to store asynchronous client ID
465 * \param[out] sync_id If not \p NULL, where to store synchronous client ID
466 *
467 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
468 *
469 * \note This is the \p cib_native variant implementation of
470 * \p cib_api_operations_t:client_id().
471 * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
472 * \note The client ID is assigned during CIB sign-on.
473 */
474 static int
475 cib_native_client_id(const cib_t *cib, const char **async_id,
476 const char **sync_id)
477 {
478 cib_native_opaque_t *native = cib->variant_opaque;
479
480 if (async_id != NULL) {
481 *async_id = native->token;
482 }
483 if (sync_id != NULL) {
484 *sync_id = native->token;
485 }
486 return pcmk_ok;
487 }
488
489 cib_t *
490 cib_native_new(void)
491 {
492 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] |
493 cib_t *cib = cib_new_variant();
494
|
(3) Event path: |
Condition "cib == NULL", taking false branch. |
495 if (cib == NULL) {
496 return NULL;
497 }
498
499 native = calloc(1, sizeof(cib_native_opaque_t));
500
|
(4) Event path: |
Condition "native == NULL", taking true branch. |
501 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] |
502 free(cib);
503 return NULL;
504 }
505
506 cib->variant = cib_native;
507 cib->variant_opaque = native;
508
509 native->ipc = NULL;
510 native->source = NULL;
511 native->dnotify_fn = NULL;
512
513 /* assign variant specific ops */
514 cib->delegate_fn = cib_native_perform_op_delegate;
515 cib->cmds->signon = cib_native_signon;
516 cib->cmds->signoff = cib_native_signoff;
517 cib->cmds->free = cib_native_free;
518
519 cib->cmds->register_notification = cib_native_register_notification;
520 cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
521
522 cib->cmds->client_id = cib_native_client_id;
523
524 return cib;
525 }
526