root/include/crm/common/util.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. crm_itoa
  2. crm_ftoa
  3. crm_ttoa
  4. crm_str_table_new
  5. crm_strcase_table_new
  6. pcmk_any_flags_set
  7. pcmk_all_flags_set
  8. crm_hash_table_size
  9. is_not_set
  10. is_set
  11. is_set_any

   1 /*
   2  * Copyright 2004-2020 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 #ifndef CRM_COMMON_UTIL__H
  11 #  define CRM_COMMON_UTIL__H
  12 
  13 #ifdef __cplusplus
  14 extern "C" {
  15 #endif
  16 
  17 /**
  18  * \file
  19  * \brief Utility functions
  20  * \ingroup core
  21  */
  22 
  23 #  include <sys/types.h>    // gid_t, mode_t, size_t, time_t, uid_t
  24 #  include <stdlib.h>
  25 #  include <stdbool.h>
  26 #  include <stdint.h>       // uint32_t
  27 #  include <limits.h>
  28 #  include <signal.h>
  29 #  include <glib.h>
  30 
  31 #  include <libxml/tree.h>
  32 
  33 #  include <crm/lrmd.h>
  34 #  include <crm/common/acl.h>
  35 #  include <crm/common/results.h>
  36 
  37 #  define ONLINESTATUS  "online"  // Status of an online client
  38 #  define OFFLINESTATUS "offline" // Status of an offline client
  39 
  40 // public name/value pair functions (from nvpair.c)
  41 int pcmk_scan_nvpair(const char *input, char **name, char **value);
  42 char *pcmk_format_nvpair(const char *name, const char *value, const char *units);
  43 char *pcmk_format_named_time(const char *name, time_t epoch_time);
  44 
  45 /* public Pacemaker Remote functions (from remote.c) */
  46 int crm_default_remote_port(void);
  47 
  48 /* public string functions (from strings.c) */
  49 char *crm_itoa_stack(int an_int, char *buf, size_t len);
  50 gboolean crm_is_true(const char *s);
  51 int crm_str_to_boolean(const char *s, int *ret);
  52 long long crm_parse_ll(const char *text, const char *default_text);
  53 int crm_parse_int(const char *text, const char *default_text);
  54 long long crm_get_msec(const char *input);
  55 char * crm_strip_trailing_newline(char *str);
  56 gboolean crm_strcase_equal(gconstpointer a, gconstpointer b);
  57 guint crm_strcase_hash(gconstpointer v);
  58 guint g_str_hash_traditional(gconstpointer v);
  59 char *crm_strdup_printf(char const *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
  60 int pcmk_numeric_strcasecmp(const char *s1, const char *s2);
  61 
  62 #  define crm_str_hash g_str_hash_traditional
  63 
  64 static inline char *
  65 crm_itoa(int an_int)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     return crm_strdup_printf("%d", an_int);
  68 }
  69 
  70 static inline char *
  71 crm_ftoa(double a_float)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73     return crm_strdup_printf("%f", a_float);
  74 }
  75 
  76 static inline char *
  77 crm_ttoa(time_t epoch_time)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79     return crm_strdup_printf("%lld", (long long) epoch_time);
  80 }
  81 
  82 /*!
  83  * \brief Create hash table with dynamically allocated string keys/values
  84  *
  85  * \return Newly allocated hash table
  86  * \note It is the caller's responsibility to free the result, using
  87  *       g_hash_table_destroy().
  88  */
  89 static inline GHashTable *
  90 crm_str_table_new(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92     return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
  93 }
  94 
  95 /*!
  96  * \brief Create hash table with case-insensitive dynamically allocated string keys/values
  97  *
  98  * \return Newly allocated hash table
  99  * \note It is the caller's responsibility to free the result, using
 100  *       g_hash_table_destroy().
 101  */
 102 static inline GHashTable *
 103 crm_strcase_table_new(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105     return g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, free, free);
 106 }
 107 
 108 GHashTable *crm_str_table_dup(GHashTable *old_table);
 109 
 110 #  define crm_atoi(text, default_text) crm_parse_int(text, default_text)
 111 
 112 /* public I/O functions (from io.c) */
 113 void crm_build_path(const char *path_c, mode_t mode);
 114 
 115 guint crm_parse_interval_spec(const char *input);
 116 int char2score(const char *score);
 117 char *score2char(int score);
 118 char *score2char_stack(int score, char *buf, size_t len);
 119 
 120 /* public operation functions (from operations.c) */
 121 gboolean parse_op_key(const char *key, char **rsc_id, char **op_type,
 122                       guint *interval_ms);
 123 gboolean decode_transition_key(const char *key, char **uuid, int *transition_id,
 124                                int *action_id, int *target_rc);
 125 gboolean decode_transition_magic(const char *magic, char **uuid,
 126                                  int *transition_id, int *action_id,
 127                                  int *op_status, int *op_rc, int *target_rc);
 128 int rsc_op_expected_rc(lrmd_event_data_t *event);
 129 gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc);
 130 bool crm_op_needs_metadata(const char *rsc_class, const char *op);
 131 xmlNode *crm_create_op_xml(xmlNode *parent, const char *prefix,
 132                            const char *task, const char *interval_spec,
 133                            const char *timeout);
 134 #define CRM_DEFAULT_OP_TIMEOUT_S "20s"
 135 
 136 // Public resource agent functions (from agents.c)
 137 
 138 // Capabilities supported by a resource agent standard
 139 enum pcmk_ra_caps {
 140     pcmk_ra_cap_none         = 0,
 141     pcmk_ra_cap_provider     = (1 << 0), // Requires provider
 142     pcmk_ra_cap_status       = (1 << 1), // Supports status instead of monitor
 143     pcmk_ra_cap_params       = (1 << 2), // Supports parameters
 144     pcmk_ra_cap_unique       = (1 << 3), // Supports unique clones
 145     pcmk_ra_cap_promotable   = (1 << 4), // Supports promotable clones
 146     pcmk_ra_cap_stdin        = (1 << 5), // Reads from standard input
 147     pcmk_ra_cap_fence_params = (1 << 6), // Supports pcmk_monitor_timeout, etc.
 148 };
 149 
 150 uint32_t pcmk_get_ra_caps(const char *standard);
 151 char *crm_generate_ra_key(const char *standard, const char *provider,
 152                           const char *type);
 153 int crm_parse_agent_spec(const char *spec, char **standard, char **provider,
 154                          char **type);
 155 
 156 
 157 int compare_version(const char *version1, const char *version2);
 158 
 159 /* coverity[+kill] */
 160 void crm_abort(const char *file, const char *function, int line,
 161                const char *condition, gboolean do_core, gboolean do_fork);
 162 
 163 /*!
 164  * \brief Check whether any of specified flags are set in a flag group
 165  *
 166  * \param[in] flag_group        The flag group being examined
 167  * \param[in] flags_to_check    Which flags in flag_group should be checked
 168  *
 169  * \return true if \p flags_to_check is nonzero and any of its flags are set in
 170  *         \p flag_group, or false otherwise
 171  */
 172 static inline bool
 173 pcmk_any_flags_set(uint64_t flag_group, uint64_t flags_to_check)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175     return (flag_group & flags_to_check) != 0;
 176 }
 177 
 178 /*!
 179  * \brief Check whether all of specified flags are set in a flag group
 180  *
 181  * \param[in] flag_group        The flag group being examined
 182  * \param[in] flags_to_check    Which flags in flag_group should be checked
 183  *
 184  * \return true if \p flags_to_check is zero or all of its flags are set in
 185  *         \p flag_group, or false otherwise
 186  */
 187 static inline bool
 188 pcmk_all_flags_set(uint64_t flag_group, uint64_t flags_to_check)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190     return (flag_group & flags_to_check) == flags_to_check;
 191 }
 192 
 193 /*!
 194  * \brief Convenience alias for pcmk_all_flags_set(), to check single flag
 195  */
 196 #define pcmk_is_set(g, f)   pcmk_all_flags_set((g), (f))
 197 
 198 static inline guint
 199 crm_hash_table_size(GHashTable * hashtable)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201     if (hashtable == NULL) {
 202         return 0;
 203     }
 204     return g_hash_table_size(hashtable);
 205 }
 206 
 207 char *crm_meta_name(const char *field);
 208 const char *crm_meta_value(GHashTable * hash, const char *field);
 209 
 210 char *crm_md5sum(const char *buffer);
 211 
 212 char *crm_generate_uuid(void);
 213 
 214 // This belongs in ipc.h but is here for backward compatibility
 215 bool crm_is_daemon_name(const char *name);
 216 
 217 int crm_user_lookup(const char *name, uid_t * uid, gid_t * gid);
 218 int pcmk_daemon_user(uid_t *uid, gid_t *gid);
 219 
 220 #ifdef HAVE_GNUTLS_GNUTLS_H
 221 void crm_gnutls_global_init(void);
 222 #endif
 223 
 224 char *pcmk_hostname(void);
 225 
 226 bool pcmk_str_is_infinity(const char *s);
 227 bool pcmk_str_is_minus_infinity(const char *s);
 228 
 229 #ifndef PCMK__NO_COMPAT
 230 /* Everything here is deprecated and kept only for public API backward
 231  * compatibility. It will be moved to compatibility.h in a future release.
 232  */
 233 
 234 //! \deprecated Use crm_parse_interval_spec() instead
 235 #define crm_get_interval crm_parse_interval_spec
 236 
 237 //! \deprecated Use !pcmk_is_set() or !pcmk_all_flags_set() instead
 238 static inline gboolean
 239 is_not_set(long long word, long long bit)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241     return ((word & bit) == 0);
 242 }
 243 
 244 //! \deprecated Use pcmk_is_set() or pcmk_all_flags_set() instead
 245 static inline gboolean
 246 is_set(long long word, long long bit)
     /* [previous][next][first][last][top][bottom][index][help] */
 247 {
 248     return ((word & bit) == bit);
 249 }
 250 
 251 //! \deprecated Use pcmk_any_flags_set() instead
 252 static inline gboolean
 253 is_set_any(long long word, long long bit)
     /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255     return ((word & bit) != 0);
 256 }
 257 
 258 //! \deprecated Use pcmk_get_ra_caps() instead
 259 bool crm_provider_required(const char *standard);
 260 
 261 //! \deprecated Use strcmp or strcasecmp instead
 262 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
 263 
 264 //! \deprecated Use strcmp instead
 265 gboolean safe_str_neq(const char *a, const char *b);
 266 
 267 //! \deprecated Use strcasecmp instead
 268 #define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
 269 
 270 #endif // PCMK__NO_COMPAT
 271 
 272 #ifdef __cplusplus
 273 }
 274 #endif
 275 
 276 #endif

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