root/lib/common/ipc_common.c

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

DEFINITIONS

This source file includes following definitions.
  1. pcmk__ipc_buffer_size
  2. crm_ipc_default_buffer_size
  3. pcmk__valid_ipc_header
  4. pcmk__client_type_str

   1 /*
   2  * Copyright 2004-2024 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <stdio.h>
  13 #include <stdint.h>         // uint64_t
  14 #include <sys/types.h>
  15 
  16 #include <crm/common/xml.h>
  17 #include "crmcommon_private.h"
  18 
  19 #define MIN_MSG_SIZE    12336    // sizeof(struct qb_ipc_connection_response)
  20 #define MAX_MSG_SIZE    128*1024 // 128k default
  21 
  22 /*!
  23  * \internal
  24  * \brief Choose an IPC buffer size in bytes
  25  *
  26  * \param[in] max  Use this value if environment/default is lower
  27  *
  28  * \return Maximum of max and value of PCMK_ipc_buffer (default 128KB)
  29  */
  30 unsigned int
  31 pcmk__ipc_buffer_size(unsigned int max)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33     static long long env_value = 0LL; // Will be bounded to unsigned int
  34 
  35     if (env_value == 0LL) {
  36         const char *env_value_s = pcmk__env_option(PCMK__ENV_IPC_BUFFER);
  37         int rc = pcmk__scan_ll(env_value_s, &env_value, MAX_MSG_SIZE);
  38 
  39         if (rc != pcmk_rc_ok) {
  40             env_value = MAX_MSG_SIZE;
  41             max = QB_MAX(max, env_value);
  42             crm_warn("Using %u as IPC buffer size because '%s' is not "
  43                      "a valid value for PCMK_" PCMK__ENV_IPC_BUFFER ": %s",
  44                      max, env_value_s, pcmk_rc_str(rc));
  45 
  46         } else if (env_value <= 0LL) {
  47             env_value = MAX_MSG_SIZE;
  48             max = QB_MAX(max, env_value);
  49             crm_warn("Using %u as IPC buffer size because PCMK_"
  50                      PCMK__ENV_IPC_BUFFER " (%s) is not a positive integer",
  51                      max, env_value_s);
  52 
  53         } else if (env_value < MIN_MSG_SIZE) {
  54             env_value = MIN_MSG_SIZE;
  55             max = QB_MAX(max, env_value);
  56             crm_debug("Using %u as IPC buffer size because PCMK_"
  57                       PCMK__ENV_IPC_BUFFER " (%s) is too small",
  58                       max, env_value_s);
  59 
  60         } else if (env_value > UINT_MAX) {
  61             env_value = UINT_MAX;
  62             max = UINT_MAX;
  63             crm_debug("Using %u as IPC buffer size because PCMK_"
  64                       PCMK__ENV_IPC_BUFFER " (%s) is too big",
  65                       max, env_value_s);
  66         }
  67     }
  68 
  69     if (env_value > max) {
  70         const char *source = "PCMK_" PCMK__ENV_IPC_BUFFER;
  71 
  72         if (env_value == MAX_MSG_SIZE) {
  73             source = "default";
  74         }
  75         crm_debug("Using IPC buffer size %lld from %s (not %u)",
  76                   env_value, source, max);
  77         max = env_value;
  78     }
  79     return max;
  80 }
  81 
  82 /*!
  83  * \brief Return pacemaker's default IPC buffer size
  84  *
  85  * \return IPC buffer size in bytes
  86  */
  87 unsigned int
  88 crm_ipc_default_buffer_size(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90     static unsigned int default_size = 0;
  91 
  92     if (default_size == 0) {
  93         default_size = pcmk__ipc_buffer_size(0);
  94     }
  95     return default_size;
  96 }
  97 
  98 /*!
  99  * \internal
 100  * \brief Check whether an IPC header is valid
 101  *
 102  * \param[in] header  IPC header to check
 103  *
 104  * \return true if IPC header has a supported version, false otherwise
 105  */
 106 bool
 107 pcmk__valid_ipc_header(const pcmk__ipc_header_t *header)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109     if (header == NULL) {
 110         crm_err("IPC message without header");
 111         return false;
 112 
 113     } else if (header->version > PCMK__IPC_VERSION) {
 114         crm_err("Filtering incompatible v%d IPC message (only versions <= %d supported)",
 115                 header->version, PCMK__IPC_VERSION);
 116         return false;
 117     }
 118     return true;
 119 }
 120 
 121 const char *
 122 pcmk__client_type_str(uint64_t client_type)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124     switch (client_type) {
 125         case pcmk__client_ipc:
 126             return "IPC";
 127         case pcmk__client_tcp:
 128             return "TCP";
 129 #ifdef HAVE_GNUTLS_GNUTLS_H
 130         case pcmk__client_tls:
 131             return "TLS";
 132 #endif
 133         default:
 134             return "unknown";
 135     }
 136 }

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