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

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