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
  6. crm_provider_required

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

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