pacemaker  2.0.5-ba59be712
Scalable High-Availability cluster resource manager
util.h
Go to the documentation of this file.
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 
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)
66 {
67  return crm_strdup_printf("%d", an_int);
68 }
69 
70 static inline char *
71 crm_ftoa(double a_float)
72 {
73  return crm_strdup_printf("%f", a_float);
74 }
75 
76 static inline char *
77 crm_ttoa(time_t epoch_time)
78 {
79  return crm_strdup_printf("%lld", (long long) epoch_time);
80 }
81 
89 static inline GHashTable *
90 crm_str_table_new(void)
91 {
92  return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
93 }
94 
102 static inline GHashTable *
103 crm_strcase_table_new(void)
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);
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
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 
172 static inline bool
173 pcmk_any_flags_set(uint64_t flag_group, uint64_t flags_to_check)
174 {
175  return (flag_group & flags_to_check) != 0;
176 }
177 
187 static inline bool
188 pcmk_all_flags_set(uint64_t flag_group, uint64_t flags_to_check)
189 {
190  return (flag_group & flags_to_check) == flags_to_check;
191 }
192 
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)
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 
235 #define crm_get_interval crm_parse_interval_spec
236 
238 static inline gboolean
239 is_not_set(long long word, long long bit)
240 {
241  return ((word & bit) == 0);
242 }
243 
245 static inline gboolean
246 is_set(long long word, long long bit)
247 {
248  return ((word & bit) == bit);
249 }
250 
252 static inline gboolean
253 is_set_any(long long word, long long bit)
254 {
255  return ((word & bit) != 0);
256 }
257 
259 bool crm_provider_required(const char *standard);
260 
262 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
263 
265 gboolean safe_str_neq(const char *a, const char *b);
266 
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
gboolean parse_op_key(const char *key, char **rsc_id, char **op_type, guint *interval_ms)
Definition: operations.c:184
xmlNode * crm_create_op_xml(xmlNode *parent, const char *prefix, const char *task, const char *interval_spec, const char *timeout)
Create a CIB XML element for an operation.
Definition: operations.c:474
bool crm_is_daemon_name(const char *name)
Check whether string represents a client name used by cluster daemons.
Definition: ipc_server.c:923
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:1082
char * crm_generate_uuid(void)
Definition: utils.c:498
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:550
char int pcmk_numeric_strcasecmp(const char *s1, const char *s2)
Sort strings, with numeric portions sorted numerically.
Definition: strings.c:931
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
Definition: io.c:81
char * crm_generate_ra_key(const char *standard, const char *provider, const char *type)
Definition: agents.c:92
int rsc_op_expected_rc(lrmd_event_data_t *event)
Definition: operations.c:428
int char2score(const char *score)
Definition: utils.c:61
long long crm_get_msec(const char *input)
Parse a time+units string and return milliseconds equivalent.
Definition: strings.c:340
char * score2char_stack(int score, char *buf, size_t len)
Definition: utils.c:96
const char * crm_meta_value(GHashTable *hash, const char *field)
Definition: utils.c:479
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
Definition: strings.c:134
Resource agent executor.
bool crm_op_needs_metadata(const char *rsc_class, const char *op)
Check whether an operation requires resource agent meta-data.
Definition: operations.c:501
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:122
guint crm_parse_interval_spec(const char *input)
Parse milliseconds from a Pacemaker interval specification.
Definition: utils.c:307
int pcmk_daemon_user(uid_t *uid, gid_t *gid)
Get user and group IDs of pacemaker daemon user.
Definition: utils.c:162
int pcmk_scan_nvpair(const char *input, char **name, char **value)
Extract the name and value from an input string formatted as "name=value". If unable to extract them,...
Definition: nvpair.c:221
char * pcmk_format_nvpair(const char *name, const char *value, const char *units)
Definition: nvpair.c:282
char * crm_meta_name(const char *field)
Definition: utils.c:457
int crm_parse_agent_spec(const char *spec, char **standard, char **provider, char **type)
Parse a "standard[:provider]:type" agent specification.
Definition: agents.c:120
char * pcmk_format_named_time(const char *name, time_t epoch_time)
Definition: nvpair.c:298
op_status
Definition: services.h:89
Low-level API for XML Access Control Lists (ACLs)
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:569
uint32_t pcmk_get_ra_caps(const char *standard)
Get capabilities of a resource agent standard.
Definition: agents.c:31
bool pcmk_str_is_infinity(const char *s)
Definition: utils.c:531
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:1098
char * crm_itoa_stack(int an_int, char *buf, size_t len)
Definition: strings.c:28
Function and executable result codes.
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:403
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
Definition: strings.c:107
gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc)
Definition: operations.c:439
bool pcmk_str_is_minus_infinity(const char *s)
Definition: utils.c:536
gboolean decode_transition_key(const char *key, char **uuid, int *transition_id, int *action_id, int *target_rc)
Parse a transition key into its constituent parts.
Definition: operations.c:317
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:589
int compare_version(const char *version1, const char *version2)
Definition: utils.c:225
#define crm_str_hash
Definition: util.h:62
gboolean decode_transition_magic(const char *magic, char **uuid, int *transition_id, int *action_id, int *op_status, int *op_rc, int *target_rc)
Parse a transition magic string into its constituent parts.
Definition: operations.c:254
char * crm_md5sum(const char *buffer)
Definition: digest.c:271
bool crm_provider_required(const char *standard)
Check whether a resource standard requires a provider to be specified.
Definition: agents.c:170
gboolean crm_is_true(const char *s)
Definition: strings.c:392
char * pcmk_hostname(void)
Get the local hostname.
Definition: utils.c:523
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:424
pcmk_ra_caps
Definition: util.h:139
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: utils.c:337
char * name
Definition: pcmk_fence.c:31
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
unsigned int timeout
Definition: pcmk_fence.c:32
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
Definition: remote.c:1262
enum crm_ais_msg_types type
Definition: internal.h:83
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)
Definition: strings.c:563
char * score2char(int score)
Definition: utils.c:110