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 case cib_query:
304 /* @COMPAT cib_command_nonblocking and cib_query are deprecated
305 * since 3.0.2
306 */
307 cib->state = cib_connected_command;
308 channel = PCMK__SERVER_BASED_RW;
309 break;
310
311 default:
312 return -ENOTCONN;
313 }
314
315 pcmk__trace("Connecting %s channel", channel);
316
317 native->source = mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 0, cib,
318 &cib_callbacks);
319 native->ipc = mainloop_get_ipc_client(native->source);
320
321 if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
322 pcmk__info("Could not connect to CIB manager for %s", name);
323 rc = -ENOTCONN;
324 }
325
326 if (rc == pcmk_ok) {
327 rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
328 cib_sync_call, NULL, name, &hello);
329 rc = pcmk_rc2legacy(rc);
330 }
331
332 if (rc == pcmk_ok) {
333 xmlNode *reply = NULL;
334 const char *msg_type = NULL;
335
336 if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
337 &reply) <= 0) {
338 rc = -ECOMM;
339 goto done;
340 }
341
342 /* The only reason we can receive an ACK here is if dispatch_common ->
343 * pcmk__client_data2xml processed something that's not valid XML.
344 * dispatch_common does not return ACK, unlike other daemons.
345 */
346 if (pcmk__xe_is(reply, PCMK__XE_ACK) && ack_is_failure(reply)) {
347 rc = -EPROTO;
348 pcmk__xml_free(reply);
349 goto done;
350 }
351
352 msg_type = pcmk__xe_get(reply, PCMK__XA_CIB_OP);
353
354 pcmk__log_xml_trace(reply, "reg-reply");
355
356 if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
357 pcmk__info("Reply to CIB registration message has unknown type "
358 "'%s'",
359 msg_type);
360 rc = -EPROTO;
361
362 } else {
363 native->token = pcmk__xe_get_copy(reply, PCMK__XA_CIB_CLIENTID);
364 if (native->token == NULL) {
365 rc = -EPROTO;
366 }
367 }
368
369 pcmk__xml_free(reply);
370 }
371
372 done:
373 pcmk__xml_free(hello);
374
375 if (rc == pcmk_ok) {
376 pcmk__info("Successfully connected to CIB manager for %s", name);
377 return pcmk_ok;
378 }
379
380 pcmk__info("Connection to CIB manager for %s failed: %s", name,
381 pcmk_strerror(rc));
382 cib_native_signoff(cib);
383 return rc;
384 }
385
386 static int
387 cib_native_free(cib_t *cib)
388 {
389 int rc = pcmk_ok;
390
391 if (cib->state != cib_disconnected) {
392 rc = cib_native_signoff(cib);
393 }
394
395 if (cib->state == cib_disconnected) {
396 cib_native_opaque_t *native = cib->variant_opaque;
397
398 free(native->token);
399 free(cib->variant_opaque);
400 free(cib->cmds);
401 free(cib->user);
402 free(cib);
403 }
404
405 return rc;
406 }
407
408 static int
409 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
410 {
411 int rc = pcmk_ok;
412 xmlNode *notify_msg = pcmk__xe_create(NULL, PCMK__XE_CIB_CALLBACK);
413 cib_native_opaque_t *native = cib->variant_opaque;
414
415 if (cib->state != cib_disconnected) {
416 pcmk__xe_set(notify_msg, PCMK__XA_CIB_OP, PCMK__VALUE_CIB_NOTIFY);
417 pcmk__xe_set(notify_msg, PCMK__XA_CIB_NOTIFY_TYPE, callback);
418 pcmk__xe_set_int(notify_msg, PCMK__XA_CIB_NOTIFY_ACTIVATE, enabled);
419
420 /* We don't care about the reply here, so there's no need to check
421 * if we got an ACK in response.
422 */
423 rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
424 1000 * cib->call_timeout, NULL);
425 if (rc <= 0) {
426 pcmk__trace("Notification not registered: %d", rc);
427 rc = -ECOMM;
428 }
429 }
430
431 pcmk__xml_free(notify_msg);
432 return rc;
433 }
434
435 static int
436 cib_native_set_connection_dnotify(cib_t *cib, void (*dnotify)(void *user_data))
437 {
438 cib_native_opaque_t *native = NULL;
439
440 if (cib == NULL) {
441 pcmk__err("No CIB!");
442 return FALSE;
443 }
444
445 native = cib->variant_opaque;
446 native->dnotify_fn = dnotify;
447
448 return pcmk_ok;
449 }
450
451 /*!
452 * \internal
453 * \brief Get the given CIB connection's unique client identifier
454 *
455 * These can be used to check whether this client requested the action that
456 * triggered a CIB notification.
457 *
458 * \param[in] cib CIB connection
459 * \param[out] async_id If not \p NULL, where to store asynchronous client ID
460 * \param[out] sync_id If not \p NULL, where to store synchronous client ID
461 *
462 * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
463 *
464 * \note This is the \p cib_native variant implementation of
465 * \p cib_api_operations_t:client_id().
466 * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
467 * \note The client ID is assigned during CIB sign-on.
468 */
469 static int
470 cib_native_client_id(const cib_t *cib, const char **async_id,
471 const char **sync_id)
472 {
473 cib_native_opaque_t *native = cib->variant_opaque;
474
475 if (async_id != NULL) {
476 *async_id = native->token;
477 }
478 if (sync_id != NULL) {
479 *sync_id = native->token;
480 }
481 return pcmk_ok;
482 }
483
484 cib_t *
485 cib_native_new(void)
486 {
487 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] |
488 cib_t *cib = cib_new_variant();
489
|
(3) Event path: |
Condition "cib == NULL", taking false branch. |
490 if (cib == NULL) {
491 return NULL;
492 }
493
494 native = calloc(1, sizeof(cib_native_opaque_t));
495
|
(4) Event path: |
Condition "native == NULL", taking true branch. |
496 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] |
497 free(cib);
498 return NULL;
499 }
500
501 cib->variant = cib_native;
502 cib->variant_opaque = native;
503
504 native->ipc = NULL;
505 native->source = NULL;
506 native->dnotify_fn = NULL;
507
508 /* assign variant specific ops */
509 cib->delegate_fn = cib_native_perform_op_delegate;
510 cib->cmds->signon = cib_native_signon;
511 cib->cmds->signoff = cib_native_signoff;
512 cib->cmds->free = cib_native_free;
513
514 cib->cmds->register_notification = cib_native_register_notification;
515 cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
516
517 cib->cmds->client_id = cib_native_client_id;
518
519 return cib;
520 }
521