pacemaker  2.0.4-2deceaa
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_str_eq(const char *a, const char *b, gboolean use_case);
57 gboolean safe_str_neq(const char *a, const char *b);
58 gboolean crm_strcase_equal(gconstpointer a, gconstpointer b);
59 guint crm_strcase_hash(gconstpointer v);
60 guint g_str_hash_traditional(gconstpointer v);
61 char *crm_strdup_printf(char const *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
62 int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end);
63 gboolean pcmk__str_in_list(GList *lst, const gchar *s);
64 
65 # define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
66 # define crm_str_hash g_str_hash_traditional
67 
68 static inline char *
69 crm_itoa(int an_int)
70 {
71  return crm_strdup_printf("%d", an_int);
72 }
73 
74 static inline char *
75 crm_ftoa(double a_float)
76 {
77  return crm_strdup_printf("%f", a_float);
78 }
79 
80 static inline char *
81 crm_ttoa(time_t epoch_time)
82 {
83  return crm_strdup_printf("%lld", (long long) epoch_time);
84 }
85 
93 static inline GHashTable *
94 crm_str_table_new(void)
95 {
96  return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
97 }
98 
106 static inline GHashTable *
107 crm_strcase_table_new(void)
108 {
109  return g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, free, free);
110 }
111 
112 GHashTable *crm_str_table_dup(GHashTable *old_table);
113 
114 # define crm_atoi(text, default_text) crm_parse_int(text, default_text)
115 
116 /* public I/O functions (from io.c) */
117 void crm_build_path(const char *path_c, mode_t mode);
118 
119 guint crm_parse_interval_spec(const char *input);
120 int char2score(const char *score);
121 char *score2char(int score);
122 char *score2char_stack(int score, char *buf, size_t len);
123 
124 /* public operation functions (from operations.c) */
125 gboolean parse_op_key(const char *key, char **rsc_id, char **op_type,
126  guint *interval_ms);
127 gboolean decode_transition_key(const char *key, char **uuid, int *transition_id,
128  int *action_id, int *target_rc);
129 gboolean decode_transition_magic(const char *magic, char **uuid,
130  int *transition_id, int *action_id,
131  int *op_status, int *op_rc, int *target_rc);
133 gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc);
134 bool crm_op_needs_metadata(const char *rsc_class, const char *op);
135 xmlNode *crm_create_op_xml(xmlNode *parent, const char *prefix,
136  const char *task, const char *interval_spec,
137  const char *timeout);
138 #define CRM_DEFAULT_OP_TIMEOUT_S "20s"
139 
140 // Public resource agent functions (from agents.c)
141 
142 // Capabilities supported by a resource agent standard
145  pcmk_ra_cap_provider = (1 << 0), // Requires provider
146  pcmk_ra_cap_status = (1 << 1), // Supports status instead of monitor
147  pcmk_ra_cap_params = (1 << 2), // Supports parameters
148  pcmk_ra_cap_unique = (1 << 3), // Supports unique clones
149  pcmk_ra_cap_promotable = (1 << 4), // Supports promotable clones
150  pcmk_ra_cap_stdin = (1 << 5), // Reads from standard input
151 };
152 
153 uint32_t pcmk_get_ra_caps(const char *standard);
154 char *crm_generate_ra_key(const char *standard, const char *provider,
155  const char *type);
156 int crm_parse_agent_spec(const char *spec, char **standard, char **provider,
157  char **type);
158 
159 
160 int compare_version(const char *version1, const char *version2);
161 
162 /* coverity[+kill] */
163 void crm_abort(const char *file, const char *function, int line,
164  const char *condition, gboolean do_core, gboolean do_fork);
165 
166 static inline gboolean
167 is_not_set(long long word, long long bit)
168 {
169  return ((word & bit) == 0);
170 }
171 
172 static inline gboolean
173 is_set(long long word, long long bit)
174 {
175  return ((word & bit) == bit);
176 }
177 
178 static inline gboolean
179 is_set_any(long long word, long long bit)
180 {
181  return ((word & bit) != 0);
182 }
183 
184 static inline guint
185 crm_hash_table_size(GHashTable * hashtable)
186 {
187  if (hashtable == NULL) {
188  return 0;
189  }
190  return g_hash_table_size(hashtable);
191 }
192 
193 char *crm_meta_name(const char *field);
194 const char *crm_meta_value(GHashTable * hash, const char *field);
195 
196 char *crm_md5sum(const char *buffer);
197 
198 char *crm_generate_uuid(void);
199 bool crm_is_daemon_name(const char *name);
200 
201 int crm_user_lookup(const char *name, uid_t * uid, gid_t * gid);
202 int pcmk_daemon_user(uid_t *uid, gid_t *gid);
203 
204 #ifdef HAVE_GNUTLS_GNUTLS_H
205 void crm_gnutls_global_init(void);
206 #endif
207 
208 char *pcmk_hostname(void);
209 
210 bool pcmk_str_is_infinity(const char *s);
211 bool pcmk_str_is_minus_infinity(const char *s);
212 
213 #ifndef PCMK__NO_COMPAT
214 /* Everything here is deprecated and kept only for public API backward
215  * compatibility. It will be moved to compatibility.h when 2.1.0 is released.
216  */
217 
219 #define crm_get_interval crm_parse_interval_spec
220 
222 bool crm_provider_required(const char *standard);
223 
224 #endif // PCMK__NO_COMPAT
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif
gboolean parse_op_key(const char *key, char **rsc_id, char **op_type, guint *interval_ms)
Definition: operations.c:48
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:377
bool crm_is_daemon_name(const char *name)
Check whether a string represents a cluster daemon name.
Definition: utils.c:553
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:263
char * crm_generate_uuid(void)
Definition: utils.c:495
gboolean pcmk__str_in_list(GList *lst, const gchar *s)
Definition: strings.c:660
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:456
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:70
int rsc_op_expected_rc(lrmd_event_data_t *event)
Definition: operations.c:329
int char2score(const char *score)
Definition: utils.c:59
long long crm_get_msec(const char *input)
Parse a time+units string and return milliseconds equivalent.
Definition: strings.c:211
char * score2char_stack(int score, char *buf, size_t len)
Definition: utils.c:94
const char * crm_meta_value(GHashTable *hash, const char *field)
Definition: utils.c:476
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
Definition: strings.c:126
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:404
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:131
guint crm_parse_interval_spec(const char *input)
Parse milliseconds from a Pacemaker interval specification.
Definition: utils.c:309
int pcmk_daemon_user(uid_t *uid, gid_t *gid)
Get user and group IDs of pacemaker daemon user.
Definition: utils.c:171
int pcmk_scan_nvpair(const char *input, char **name, char **value)
Extract the name and value from an input string formatted as &quot;name=value&quot;. If unable to extract them...
Definition: nvpair.c:220
char * pcmk_format_nvpair(const char *name, const char *value, const char *units)
Definition: nvpair.c:281
char * crm_meta_name(const char *field)
Definition: utils.c:454
int crm_parse_agent_spec(const char *spec, char **standard, char **provider, char **type)
Parse a &quot;standard[:provider]:type&quot; agent specification.
Definition: agents.c:98
char * pcmk_format_named_time(const char *name, time_t epoch_time)
Definition: nvpair.c:297
op_status
Definition: services.h:118
Low-level API for XML Access Control Lists (ACLs)
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:475
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:620
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:326
char * crm_itoa_stack(int an_int, char *buf, size_t len)
Definition: strings.c:24
Function and executable result codes.
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:289
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
Definition: strings.c:100
gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc)
Definition: operations.c:340
bool pcmk_str_is_minus_infinity(const char *s)
Definition: utils.c:625
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:218
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:495
int compare_version(const char *version1, const char *version2)
Definition: utils.c:227
#define crm_str_hash
Definition: util.h:66
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:155
char * crm_md5sum(const char *buffer)
Definition: utils.c:570
bool crm_provider_required(const char *standard)
Check whether a resource standard requires a provider to be specified.
Definition: agents.c:148
gboolean crm_is_true(const char *s)
Definition: strings.c:278
char * pcmk_hostname(void)
Get the local hostname.
Definition: utils.c:612
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:310
pcmk_ra_caps
Definition: util.h:143
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: utils.c:341
char * name
Definition: pcmk_fence.c:30
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
unsigned int timeout
Definition: pcmk_fence.c:31
char int pcmk__parse_ll_range(const char *srcstring, long long *start, long long *end)
Definition: strings.c:605
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
Definition: remote.c:1262
enum crm_proc_flag __attribute__
enum crm_ais_msg_types type
Definition: internal.h:83
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)
Definition: strings.c:469
char * score2char(int score)
Definition: utils.c:108