root/lib/cib/cib_native.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cib_native_perform_op_delegate
  2. cib_native_dispatch_internal
  3. cib_native_destroy
  4. cib_native_signoff
  5. cib_native_signon_raw
  6. cib_native_signon
  7. cib_native_free
  8. cib_native_register_notification
  9. cib_native_set_connection_dnotify
  10. cib_native_client_id
  11. cib_native_new

   1 /*
   2  * Copyright 2004 International Business Machines
   3  * Later changes copyright 2004-2023 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 
  33 typedef 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 } cib_native_opaque_t;
  39 
  40 static int
  41 cib_native_perform_op_delegate(cib_t *cib, const char *op, const char *host,
     /* [previous][next][first][last][top][bottom][index][help] */
  42                                const char *section, xmlNode *data,
  43                                xmlNode **output_data, int call_options,
  44                                const char *user_name)
  45 {
  46     int rc = pcmk_ok;
  47     int reply_id = 0;
  48     enum crm_ipc_flags ipc_flags = crm_ipc_flags_none;
  49 
  50     xmlNode *op_msg = NULL;
  51     xmlNode *op_reply = NULL;
  52 
  53     cib_native_opaque_t *native = cib->variant_opaque;
  54 
  55     if (cib->state == cib_disconnected) {
  56         return -ENOTCONN;
  57     }
  58 
  59     if (output_data != NULL) {
  60         *output_data = NULL;
  61     }
  62 
  63     if (op == NULL) {
  64         crm_err("No operation specified");
  65         return -EINVAL;
  66     }
  67 
  68     if (call_options & cib_sync_call) {
  69         pcmk__set_ipc_flags(ipc_flags, "client", crm_ipc_client_response);
  70     }
  71 
  72     rc = cib__create_op(cib, op, host, section, data, call_options, user_name,
  73                         NULL, &op_msg);
  74     if (rc != pcmk_ok) {
  75         return rc;
  76     }
  77 
  78     if (pcmk_is_set(call_options, cib_transaction)) {
  79         rc = cib__extend_transaction(cib, op_msg);
  80         goto done;
  81     }
  82 
  83     crm_trace("Sending %s message to the CIB manager (timeout=%ds)", op, cib->call_timeout);
  84     rc = crm_ipc_send(native->ipc, op_msg, ipc_flags, cib->call_timeout * 1000, &op_reply);
  85 
  86     if (rc < 0) {
  87         crm_err("Couldn't perform %s operation (timeout=%ds): %s (%d)", op,
  88                 cib->call_timeout, pcmk_strerror(rc), rc);
  89         rc = -ECOMM;
  90         goto done;
  91     }
  92 
  93     crm_log_xml_trace(op_reply, "Reply");
  94 
  95     if (!(call_options & cib_sync_call)) {
  96         crm_trace("Async call, returning %d", cib->call_id);
  97         CRM_CHECK(cib->call_id != 0, return -ENOMSG);
  98         free_xml(op_reply);
  99         return cib->call_id;
 100     }
 101 
 102     rc = pcmk_ok;
 103     crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
 104     if (reply_id == cib->call_id) {
 105         xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
 106 
 107         crm_trace("Synchronous reply %d received", reply_id);
 108         if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
 109             rc = -EPROTO;
 110         }
 111 
 112         if (output_data == NULL || (call_options & cib_discard_reply)) {
 113             crm_trace("Discarding reply");
 114 
 115         } else if (tmp != NULL) {
 116             *output_data = copy_xml(tmp);
 117         }
 118 
 119     } else if (reply_id <= 0) {
 120         crm_err("Received bad reply: No id set");
 121         crm_log_xml_err(op_reply, "Bad reply");
 122         rc = -ENOMSG;
 123         goto done;
 124 
 125     } else {
 126         crm_err("Received bad reply: %d (wanted %d)", reply_id, cib->call_id);
 127         crm_log_xml_err(op_reply, "Old reply");
 128         rc = -ENOMSG;
 129         goto done;
 130     }
 131 
 132     if (op_reply == NULL && cib->state == cib_disconnected) {
 133         rc = -ENOTCONN;
 134 
 135     } else if (rc == pcmk_ok && op_reply == NULL) {
 136         rc = -ETIME;
 137     }
 138 
 139     switch (rc) {
 140         case pcmk_ok:
 141         case -EPERM:
 142             break;
 143 
 144             /* This is an internal value that clients do not and should not care about */
 145         case -pcmk_err_diff_resync:
 146             rc = pcmk_ok;
 147             break;
 148 
 149             /* These indicate internal problems */
 150         case -EPROTO:
 151         case -ENOMSG:
 152             crm_err("Call failed: %s", pcmk_strerror(rc));
 153             if (op_reply) {
 154                 crm_log_xml_err(op_reply, "Invalid reply");
 155             }
 156             break;
 157 
 158         default:
 159             if (!pcmk__str_eq(op, PCMK__CIB_REQUEST_QUERY, pcmk__str_none)) {
 160                 crm_warn("Call failed: %s", pcmk_strerror(rc));
 161             }
 162     }
 163 
 164   done:
 165     if (!crm_ipc_connected(native->ipc)) {
 166         crm_err("The CIB manager disconnected");
 167         cib->state = cib_disconnected;
 168     }
 169 
 170     free_xml(op_msg);
 171     free_xml(op_reply);
 172     return rc;
 173 }
 174 
 175 static int
 176 cib_native_dispatch_internal(const char *buffer, ssize_t length,
     /* [previous][next][first][last][top][bottom][index][help] */
 177                              gpointer userdata)
 178 {
 179     const char *type = NULL;
 180     xmlNode *msg = NULL;
 181 
 182     cib_t *cib = userdata;
 183 
 184     crm_trace("dispatching %p", userdata);
 185 
 186     if (cib == NULL) {
 187         crm_err("No CIB!");
 188         return 0;
 189     }
 190 
 191     msg = string2xml(buffer);
 192 
 193     if (msg == NULL) {
 194         crm_warn("Received a NULL message from the CIB manager");
 195         return 0;
 196     }
 197 
 198     /* do callbacks */
 199     type = crm_element_value(msg, F_TYPE);
 200     crm_trace("Activating %s callbacks...", type);
 201     crm_log_xml_explicit(msg, "cib-reply");
 202 
 203     if (pcmk__str_eq(type, T_CIB, pcmk__str_casei)) {
 204         cib_native_callback(cib, msg, 0, 0);
 205 
 206     } else if (pcmk__str_eq(type, T_CIB_NOTIFY, pcmk__str_casei)) {
 207         g_list_foreach(cib->notify_list, cib_native_notify, msg);
 208 
 209     } else {
 210         crm_err("Unknown message type: %s", type);
 211     }
 212 
 213     free_xml(msg);
 214     return 0;
 215 }
 216 
 217 static void
 218 cib_native_destroy(void *userdata)
     /* [previous][next][first][last][top][bottom][index][help] */
 219 {
 220     cib_t *cib = userdata;
 221     cib_native_opaque_t *native = cib->variant_opaque;
 222 
 223     crm_trace("destroying %p", userdata);
 224     cib->state = cib_disconnected;
 225     native->source = NULL;
 226     native->ipc = NULL;
 227 
 228     if (native->dnotify_fn) {
 229         native->dnotify_fn(userdata);
 230     }
 231 }
 232 
 233 static int
 234 cib_native_signoff(cib_t *cib)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236     cib_native_opaque_t *native = cib->variant_opaque;
 237 
 238     crm_debug("Disconnecting from the CIB manager");
 239 
 240     cib_free_notify(cib);
 241     remove_cib_op_callback(0, TRUE);
 242 
 243     if (native->source != NULL) {
 244         /* Attached to mainloop */
 245         mainloop_del_ipc_client(native->source);
 246         native->source = NULL;
 247         native->ipc = NULL;
 248 
 249     } else if (native->ipc) {
 250         /* Not attached to mainloop */
 251         crm_ipc_t *ipc = native->ipc;
 252 
 253         native->ipc = NULL;
 254         crm_ipc_close(ipc);
 255         crm_ipc_destroy(ipc);
 256     }
 257 
 258     cib->cmds->end_transaction(cib, false, cib_none);
 259     cib->state = cib_disconnected;
 260     cib->type = cib_no_connection;
 261 
 262     return pcmk_ok;
 263 }
 264 
 265 static int
 266 cib_native_signon_raw(cib_t *cib, const char *name, enum cib_conn_type type,
     /* [previous][next][first][last][top][bottom][index][help] */
 267                       int *async_fd)
 268 {
 269     int rc = pcmk_ok;
 270     const char *channel = NULL;
 271     cib_native_opaque_t *native = cib->variant_opaque;
 272     xmlNode *hello = NULL;
 273 
 274     struct ipc_client_callbacks cib_callbacks = {
 275         .dispatch = cib_native_dispatch_internal,
 276         .destroy = cib_native_destroy
 277     };
 278 
 279     cib->call_timeout = PCMK__IPC_TIMEOUT;
 280 
 281     if (type == cib_command) {
 282         cib->state = cib_connected_command;
 283         channel = PCMK__SERVER_BASED_RW;
 284 
 285     } else if (type == cib_command_nonblocking) {
 286         cib->state = cib_connected_command;
 287         channel = PCMK__SERVER_BASED_SHM;
 288 
 289     } else if (type == cib_query) {
 290         cib->state = cib_connected_query;
 291         channel = PCMK__SERVER_BASED_RO;
 292 
 293     } else {
 294         return -ENOTCONN;
 295     }
 296 
 297     crm_trace("Connecting %s channel", channel);
 298 
 299     if (async_fd != NULL) {
 300         native->ipc = crm_ipc_new(channel, 0);
 301         if (native->ipc != NULL) {
 302             rc = pcmk__connect_generic_ipc(native->ipc);
 303             if (rc == pcmk_rc_ok) {
 304                 rc = pcmk__ipc_fd(native->ipc, async_fd);
 305                 if (rc != pcmk_rc_ok) {
 306                     crm_info("Couldn't get file descriptor for %s IPC",
 307                              channel);
 308                 }
 309             }
 310             rc = pcmk_rc2legacy(rc);
 311         }
 312 
 313     } else {
 314         native->source =
 315             mainloop_add_ipc_client(channel, G_PRIORITY_HIGH, 512 * 1024 /* 512k */ , cib,
 316                                     &cib_callbacks);
 317         native->ipc = mainloop_get_ipc_client(native->source);
 318     }
 319 
 320     if (rc != pcmk_ok || native->ipc == NULL || !crm_ipc_connected(native->ipc)) {
 321         crm_info("Could not connect to CIB manager for %s", name);
 322         rc = -ENOTCONN;
 323     }
 324 
 325     if (rc == pcmk_ok) {
 326         rc = cib__create_op(cib, CRM_OP_REGISTER, NULL, NULL, NULL,
 327                             cib_sync_call, NULL, name, &hello);
 328     }
 329 
 330     if (rc == pcmk_ok) {
 331         xmlNode *reply = NULL;
 332 
 333         if (crm_ipc_send(native->ipc, hello, crm_ipc_client_response, -1,
 334                          &reply) > 0) {
 335             const char *msg_type = crm_element_value(reply, F_CIB_OPERATION);
 336 
 337             crm_log_xml_trace(reply, "reg-reply");
 338 
 339             if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
 340                 crm_info("Reply to CIB registration message has unknown type "
 341                          "'%s'",
 342                          msg_type);
 343                 rc = -EPROTO;
 344 
 345             } else {
 346                 native->token = crm_element_value_copy(reply, F_CIB_CLIENTID);
 347                 if (native->token == NULL) {
 348                     rc = -EPROTO;
 349                 }
 350             }
 351             free_xml(reply);
 352 
 353         } else {
 354             rc = -ECOMM;
 355         }
 356         free_xml(hello);
 357     }
 358 
 359     if (rc == pcmk_ok) {
 360         crm_info("Successfully connected to CIB manager for %s", name);
 361         return pcmk_ok;
 362     }
 363 
 364     crm_info("Connection to CIB manager for %s failed: %s",
 365              name, pcmk_strerror(rc));
 366     cib_native_signoff(cib);
 367     return rc;
 368 }
 369 
 370 static int
 371 cib_native_signon(cib_t *cib, const char *name, enum cib_conn_type type)
     /* [previous][next][first][last][top][bottom][index][help] */
 372 {
 373     return cib_native_signon_raw(cib, name, type, NULL);
 374 }
 375 
 376 static int
 377 cib_native_free(cib_t *cib)
     /* [previous][next][first][last][top][bottom][index][help] */
 378 {
 379     int rc = pcmk_ok;
 380 
 381     if (cib->state != cib_disconnected) {
 382         rc = cib_native_signoff(cib);
 383     }
 384 
 385     if (cib->state == cib_disconnected) {
 386         cib_native_opaque_t *native = cib->variant_opaque;
 387 
 388         free(native->token);
 389         free(cib->variant_opaque);
 390         free(cib->cmds);
 391         free(cib->user);
 392         free(cib);
 393     }
 394 
 395     return rc;
 396 }
 397 
 398 static int
 399 cib_native_register_notification(cib_t *cib, const char *callback, int enabled)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401     int rc = pcmk_ok;
 402     xmlNode *notify_msg = create_xml_node(NULL, "cib-callback");
 403     cib_native_opaque_t *native = cib->variant_opaque;
 404 
 405     if (cib->state != cib_disconnected) {
 406         crm_xml_add(notify_msg, F_CIB_OPERATION, T_CIB_NOTIFY);
 407         crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
 408         crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
 409         rc = crm_ipc_send(native->ipc, notify_msg, crm_ipc_client_response,
 410                           1000 * cib->call_timeout, NULL);
 411         if (rc <= 0) {
 412             crm_trace("Notification not registered: %d", rc);
 413             rc = -ECOMM;
 414         }
 415     }
 416 
 417     free_xml(notify_msg);
 418     return rc;
 419 }
 420 
 421 static int
 422 cib_native_set_connection_dnotify(cib_t *cib,
     /* [previous][next][first][last][top][bottom][index][help] */
 423                                   void (*dnotify) (gpointer user_data))
 424 {
 425     cib_native_opaque_t *native = NULL;
 426 
 427     if (cib == NULL) {
 428         crm_err("No CIB!");
 429         return FALSE;
 430     }
 431 
 432     native = cib->variant_opaque;
 433     native->dnotify_fn = dnotify;
 434 
 435     return pcmk_ok;
 436 }
 437 
 438 /*!
 439  * \internal
 440  * \brief Get the given CIB connection's unique client identifier
 441  *
 442  * These can be used to check whether this client requested the action that
 443  * triggered a CIB notification.
 444  *
 445  * \param[in]  cib       CIB connection
 446  * \param[out] async_id  If not \p NULL, where to store asynchronous client ID
 447  * \param[out] sync_id   If not \p NULL, where to store synchronous client ID
 448  *
 449  * \return Legacy Pacemaker return code (specifically, \p pcmk_ok)
 450  *
 451  * \note This is the \p cib_native variant implementation of
 452  *       \p cib_api_operations_t:client_id().
 453  * \note For \p cib_native objects, \p async_id and \p sync_id are the same.
 454  * \note The client ID is assigned during CIB sign-on.
 455  */
 456 static int
 457 cib_native_client_id(const cib_t *cib, const char **async_id,
     /* [previous][next][first][last][top][bottom][index][help] */
 458                      const char **sync_id)
 459 {
 460     cib_native_opaque_t *native = cib->variant_opaque;
 461 
 462     if (async_id != NULL) {
 463         *async_id = native->token;
 464     }
 465     if (sync_id != NULL) {
 466         *sync_id = native->token;
 467     }
 468     return pcmk_ok;
 469 }
 470 
 471 cib_t *
 472 cib_native_new(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 473 {
 474     cib_native_opaque_t *native = NULL;
 475     cib_t *cib = cib_new_variant();
 476 
 477     if (cib == NULL) {
 478         return NULL;
 479     }
 480 
 481     native = calloc(1, sizeof(cib_native_opaque_t));
 482 
 483     if (native == NULL) {
 484         free(cib);
 485         return NULL;
 486     }
 487 
 488     cib->variant = cib_native;
 489     cib->variant_opaque = native;
 490 
 491     native->ipc = NULL;
 492     native->source = NULL;
 493     native->dnotify_fn = NULL;
 494 
 495     /* assign variant specific ops */
 496     cib->delegate_fn = cib_native_perform_op_delegate;
 497     cib->cmds->signon = cib_native_signon;
 498     cib->cmds->signon_raw = cib_native_signon_raw;
 499     cib->cmds->signoff = cib_native_signoff;
 500     cib->cmds->free = cib_native_free;
 501 
 502     cib->cmds->register_notification = cib_native_register_notification;
 503     cib->cmds->set_connection_dnotify = cib_native_set_connection_dnotify;
 504 
 505     cib->cmds->client_id = cib_native_client_id;
 506 
 507     return cib;
 508 }

/* [previous][next][first][last][top][bottom][index][help] */