root/lib/common/agents.c

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

DEFINITIONS

This source file includes following definitions.
  1. pcmk_get_ra_caps
  2. pcmk__effective_rc
  3. crm_generate_ra_key
  4. crm_parse_agent_spec
  5. pcmk_stonith_param

   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 <string.h>
  14 #include <strings.h>
  15 
  16 #include <crm/crm.h>
  17 #include <crm/common/util.h>
  18 
  19 /*!
  20  * \brief Get capabilities of a resource agent standard
  21  *
  22  * \param[in] standard  Standard name
  23  *
  24  * \return Bitmask of enum pcmk_ra_caps values
  25  */
  26 uint32_t
  27 pcmk_get_ra_caps(const char *standard)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29     /* @COMPAT This should probably be case-sensitive, but isn't,
  30      * for backward compatibility.
  31      */
  32     if (standard == NULL) {
  33         return pcmk_ra_cap_none;
  34 
  35     } else if (!strcasecmp(standard, PCMK_RESOURCE_CLASS_OCF)) {
  36         return pcmk_ra_cap_provider | pcmk_ra_cap_params
  37                | pcmk_ra_cap_unique | pcmk_ra_cap_promotable
  38                | pcmk_ra_cap_cli_exec;
  39 
  40     } else if (!strcasecmp(standard, PCMK_RESOURCE_CLASS_STONITH)) {
  41         /* @COMPAT Stonith resources can't really be unique clones, but we've
  42          * allowed it in the past and have it in some scheduler regression tests
  43          * (which were likely never used as real configurations).
  44          *
  45          * @TODO Remove pcmk_ra_cap_unique at the next major schema version
  46          * bump, with a transform to remove PCMK_META_GLOBALLY_UNIQUE from the
  47          * config.
  48          */
  49         return pcmk_ra_cap_params | pcmk_ra_cap_unique | pcmk_ra_cap_stdin
  50                | pcmk_ra_cap_fence_params;
  51 
  52     } else if (!strcasecmp(standard, PCMK_RESOURCE_CLASS_LSB)) {
  53         return pcmk_ra_cap_status | pcmk_ra_cap_cli_exec;
  54 
  55     } else if (!strcasecmp(standard, PCMK_RESOURCE_CLASS_SYSTEMD)
  56                || !strcasecmp(standard, PCMK_RESOURCE_CLASS_SERVICE)) {
  57         return pcmk_ra_cap_status;
  58     }
  59     return pcmk_ra_cap_none;
  60 }
  61 
  62 int
  63 pcmk__effective_rc(int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65     int remapped_rc = rc;
  66 
  67     switch (rc) {
  68         case PCMK_OCF_DEGRADED:
  69             remapped_rc = PCMK_OCF_OK;
  70             break;
  71 
  72         case PCMK_OCF_DEGRADED_PROMOTED:
  73             remapped_rc = PCMK_OCF_RUNNING_PROMOTED;
  74             break;
  75 
  76         default:
  77             break;
  78     }
  79 
  80     return remapped_rc;
  81 }
  82 
  83 char *
  84 crm_generate_ra_key(const char *standard, const char *provider,
     /* [previous][next][first][last][top][bottom][index][help] */
  85                     const char *type)
  86 {
  87     bool std_empty = pcmk__str_empty(standard);
  88     bool prov_empty = pcmk__str_empty(provider);
  89     bool ty_empty = pcmk__str_empty(type);
  90 
  91     if (std_empty || ty_empty) {
  92         return NULL;
  93     }
  94 
  95     return crm_strdup_printf("%s%s%s:%s",
  96                              standard,
  97                              (prov_empty ? "" : ":"), (prov_empty ? "" : provider),
  98                              type);
  99 }
 100 
 101 /*!
 102  * \brief Parse a "standard[:provider]:type" agent specification
 103  *
 104  * \param[in]  spec      Agent specification
 105  * \param[out] standard  Newly allocated memory containing agent standard (or NULL)
 106  * \param[out] provider  Newly allocated memory containing agent provider (or NULL)
 107  * \param[put] type      Newly allocated memory containing agent type (or NULL)
 108  *
 109  * \return pcmk_ok if the string could be parsed, -EINVAL otherwise
 110  *
 111  * \note It is acceptable for the type to contain a ':' if the standard supports
 112  *       that. For example, systemd supports the form "systemd:UNIT@A:B".
 113  * \note It is the caller's responsibility to free the returned values.
 114  */
 115 int
 116 crm_parse_agent_spec(const char *spec, char **standard, char **provider,
     /* [previous][next][first][last][top][bottom][index][help] */
 117                      char **type)
 118 {
 119     char *colon;
 120 
 121     CRM_CHECK(spec && standard && provider && type, return -EINVAL);
 122     *standard = NULL;
 123     *provider = NULL;
 124     *type = NULL;
 125 
 126     colon = strchr(spec, ':');
 127     if ((colon == NULL) || (colon == spec)) {
 128         return -EINVAL;
 129     }
 130 
 131     *standard = strndup(spec, colon - spec);
 132     spec = colon + 1;
 133 
 134     if (pcmk_is_set(pcmk_get_ra_caps(*standard), pcmk_ra_cap_provider)) {
 135         colon = strchr(spec, ':');
 136         if ((colon == NULL) || (colon == spec)) {
 137             free(*standard);
 138             return -EINVAL;
 139         }
 140         *provider = strndup(spec, colon - spec);
 141         spec = colon + 1;
 142     }
 143 
 144     if (*spec == '\0') {
 145         free(*standard);
 146         free(*provider);
 147         return -EINVAL;
 148     }
 149 
 150     *type = strdup(spec);
 151     return pcmk_ok;
 152 }
 153 
 154 /*!
 155  * \brief Check whether a given stonith parameter is handled by Pacemaker
 156  *
 157  * Return true if a given string is the name of one of the special resource
 158  * instance attributes interpreted directly by Pacemaker for stonith-class
 159  * resources.
 160  *
 161  * \param[in] param  Parameter name to check
 162  *
 163  * \return true if \p param is a special fencing parameter
 164  */
 165 bool
 166 pcmk_stonith_param(const char *param)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168     if (param == NULL) {
 169         return false;
 170     }
 171     if (pcmk__str_any_of(param, PCMK_STONITH_PROVIDES,
 172                          PCMK_STONITH_STONITH_TIMEOUT, NULL)) {
 173         return true;
 174     }
 175     if (!pcmk__starts_with(param, "pcmk_")) { // Short-circuit common case
 176         return false;
 177     }
 178     if (pcmk__str_any_of(param,
 179                          PCMK_STONITH_ACTION_LIMIT,
 180                          PCMK_STONITH_DELAY_BASE,
 181                          PCMK_STONITH_DELAY_MAX,
 182                          PCMK_STONITH_HOST_ARGUMENT,
 183                          PCMK_STONITH_HOST_CHECK,
 184                          PCMK_STONITH_HOST_LIST,
 185                          PCMK_STONITH_HOST_MAP,
 186                          NULL)) {
 187         return true;
 188     }
 189     param = strchr(param + 5, '_'); // Skip past "pcmk_ACTION"
 190     return pcmk__str_any_of(param, "_action", "_timeout", "_retries", NULL);
 191 }

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