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)(void *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, void *userdata)
195 {
196 const char *type = NULL;
197 xmlNode *msg = NULL;
198
199 cib_t *cib = userdata;
200
201 pcmk__trace("dispatching %p", userdata);
202
203 if (cib == NULL) {
204 pcmk__err("No CIB!");
205 return 0;
206 }
207
208 msg = pcmk__xml_parse(buffer);
209
210 if (msg == NULL) {
211 pcmk__warn("Received a NULL message from the CIB manager");
212 return 0;
213 }
214
215 /* do callbacks */
216 type = pcmk__xe_get(msg, PCMK__XA_T);
217 pcmk__trace("Activating %s callbacks...", type);
218 crm_log_xml_explicit(msg, "cib-reply");
219
220 if (pcmk__str_eq(type, PCMK__VALUE_CIB, pcmk__str_none)) {
221 cib_native_callback(cib, msg, 0, 0);
222
223 } else if (pcmk__str_eq(type, PCMK__VALUE_CIB_NOTIFY, pcmk__str_none)) {
224 g_list_foreach(cib->notify_list, cib_native_notify, msg);
225
226 } else {
227 pcmk__err("Unknown message type: %s", type);
228 }
229
230 pcmk__xml_free(msg);
231 return 0;
232 }
233
234 static void
235 cib_native_destroy(void *userdata)
236 {
237 cib_t *cib = userdata;
238 cib_native_opaque_t *native = cib->variant_opaque;
239
240 pcmk__trace("destroying %p", userdata);
241 cib->state = cib_disconnected;
242 native->source = NULL;
243 native->ipc = NULL;
244
245 if (native->dnotify_fn) {
246 native->dnotify_fn(userdata);
247 }
248 }
249
250 static int
251 cib_native_signoff(cib_t *cib)
252 {
253 cib_native_opaque_t *native = cib->variant_opaque;
254
255 pcmk__debug("Disconnecting from the CIB manager");
256
257 cib_free_notify(cib);
258 remove_cib_op_callback(0, TRUE);
259
260 if (native->source != NULL) {
261 /* Attached to mainloop */
262 g_clear_pointer(&native->source, mainloop_del_ipc_client);
263 native->ipc = NULL;
264
265 } else if (native->ipc) {
266 /* Not attached to mainloop */
267 crm_ipc_t *ipc = native->ipc;
268
269 native->ipc = NULL;
270 crm_ipc_close(ipc);
271 crm_ipc_destroy(ipc);
272 }
273
274 cib->cmds->end_transaction(cib, false, cib_none);
275 cib->state = cib_disconnected;
276 cib->type = cib_no_connection;
277
278 return pcmk_ok;
279 }
280
281 static int
282 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
283 {
284 int rc = pcmk_ok;
285 const char *channel = NULL;
286 cib_native_opaque_t *native = cib->variant_opaque;
287 xmlNode *hello = NULL;
288
289 struct ipc_client_callbacks cib_callbacks = {
290 .dispatch = cib_native_dispatch_internal,
291 .destroy = cib_native_destroy
292 };
293
294 if (name == NULL) {
295 name = pcmk__s(crm_system_name, "client");
296 }
297
298 cib->call_timeout = PCMK__IPC_TIMEOUT;
299
300 switch (type) {
301 case cib_command:
302 case cib_command_nonblocking:
303 // @COMPAT cib_command_nonblocking is deprecated since 3.0.2
304 cib->state = cib_connected_command;
305 channel = PCMK__SERVER_BASED_RW;
306 break;
307
308 case cib_query:
309 cib->state = cib_connected_query;
310 channel = PCMK__SERVER_BASED_RO;
311 break;
312
313 default:
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, void (*dnotify)(void *user_data))
439 {
440 cib_native_opaque_t *native = NULL;
441
442 if (cib == NULL) {
443 pcmk__err("No CIB!");
444 return FALSE;
445 }
446
447 native = cib->variant_opaque;
448 native->dnotify_fn = dnotify;
449
450 return pcmk_ok;
451 }
452
453 /*!
454 * \internal
455 * \brief Get the given CIB connection's unique client identifier
456 *
457 * These can be used to check whether this client requested the action that
458 * triggered a CIB notification.
459 *
460 * \param[in] cib CIB connection
461 * \param[out] async_id If not \p NULL, where to store asynchronous client ID
462 * \param[out] sync_id If not \p NULL, where to store synchronous client ID
463 *
464 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
465 *
466 * \note This is the \p cib_native variant implementation of
467 * \p cib_api_operations_t:client_id().
468 * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
469 * \note The client ID is assigned during CIB sign-on.
470 */
471 static int
472 cib_native_client_id(const cib_t *cib, const char **async_id,
473 const char **sync_id)
474 {
475 cib_native_opaque_t *native = cib->variant_opaque;
476
477 if (async_id != NULL) {
478 *async_id = native->token;
479 }
480 if (sync_id != NULL) {
481 *sync_id = native->token;
482 }
483 return pcmk_ok;
484 }
485
486 cib_t *
487 cib_native_new(void)
488 {
489 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] |
490 cib_t *cib = cib_new_variant();
491
|
(3) Event path: |
Condition "cib == NULL", taking false branch. |
492 if (cib == NULL) {
493 return NULL;
494 }
495
496 native = calloc(1, sizeof(cib_native_opaque_t));
497
|
(4) Event path: |
Condition "native == NULL", taking true branch. |
498 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] |
499 free(cib);
500 return NULL;
501 }
502
503 cib->variant = cib_native;
504 cib->variant_opaque = native;
505
506 native->ipc = NULL;
507 native->source = NULL;
508 native->dnotify_fn = NULL;
509
510 /* assign variant specific ops */
511 cib->delegate_fn = cib_native_perform_op_delegate;
512 cib->cmds->signon = cib_native_signon;
513 cib->cmds->signoff = cib_native_signoff;
514 cib->cmds->free = cib_native_free;
515
516 cib->cmds->register_notification = cib_native_register_notification;
517 cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
518
519 cib->cmds->client_id = cib_native_client_id;
520
521 return cib;
522 }
523