pacemaker  2.0.3-4b1f869f0f
Scalable High-Availability cluster resource manager
util.h
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2019 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/results.h>
35 
36 # define ONLINESTATUS "online" // Status of an online client
37 # define OFFLINESTATUS "offline" // Status of an offline client
38 
39 // public name/value pair functions (from nvpair.c)
40 int pcmk_scan_nvpair(const char *input, char **name, char **value);
41 char *pcmk_format_nvpair(const char *name, const char *value, const char *units);
42 char *pcmk_format_named_time(const char *name, time_t epoch_time);
43 
44 /* public Pacemaker Remote functions (from remote.c) */
45 int crm_default_remote_port(void);
46 
47 /* public string functions (from strings.c) */
48 char *crm_itoa_stack(int an_int, char *buf, size_t len);
49 gboolean crm_is_true(const char *s);
50 int crm_str_to_boolean(const char *s, int *ret);
51 long long crm_parse_ll(const char *text, const char *default_text);
52 int crm_parse_int(const char *text, const char *default_text);
53 char * crm_strip_trailing_newline(char *str);
54 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
55 gboolean safe_str_neq(const char *a, const char *b);
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 
61 # define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
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()
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()
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 long long crm_get_msec(const char *input);
116 guint crm_parse_interval_spec(const char *input);
117 int char2score(const char *score);
118 char *score2char(int score);
119 char *score2char_stack(int score, char *buf, size_t len);
120 
121 // deprecated
122 #define crm_get_interval crm_parse_interval_spec
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 *action,
128  int *transition_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 = 0x001, // Requires provider
146  pcmk_ra_cap_status = 0x002, // Supports status instead of monitor
147  pcmk_ra_cap_params = 0x004, // Supports parameters
148  pcmk_ra_cap_unique = 0x008, // Supports unique clones
149  pcmk_ra_cap_promotable = 0x010, // Supports promotable clones
150 };
151 
152 uint32_t pcmk_get_ra_caps(const char *standard);
153 char *crm_generate_ra_key(const char *standard, const char *provider,
154  const char *type);
155 int crm_parse_agent_spec(const char *spec, char **standard, char **provider,
156  char **type);
157 bool crm_provider_required(const char *standard); // deprecated
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 
203 #ifdef HAVE_GNUTLS_GNUTLS_H
204 void crm_gnutls_global_init(void);
205 #endif
206 
207 bool pcmk_acl_required(const char *user);
208 
209 char *pcmk_hostname(void);
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif
results.h
Function and executable result codes.
crm_str_eq
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:224
did_rsc_op_fail
gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc)
Definition: operations.c:343
crm_strcase_hash
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:380
crm_str_to_boolean
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:187
pcmk_ra_cap_promotable
Definition: util.h:149
pcmk_ra_cap_provider
Definition: util.h:145
compare_version
int compare_version(const char *version1, const char *version2)
Definition: utils.c:461
type
enum crm_ais_msg_types type
Definition: internal.h:83
crm_itoa_stack
char * crm_itoa_stack(int an_int, char *buf, size_t len)
Definition: strings.c:24
crm_str_hash
#define crm_str_hash
Definition: util.h:62
pcmk_ra_caps
pcmk_ra_caps
Definition: util.h:143
crm_str_table_dup
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:400
crm_is_true
gboolean crm_is_true(const char *s)
Definition: strings.c:176
crm_parse_ll
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
Definition: strings.c:91
crm_strcase_equal
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)
Definition: strings.c:374
decode_transition_magic
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:153
crm_abort
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: utils.c:630
pcmk_ra_cap_params
Definition: util.h:147
parse_op_key
gboolean parse_op_key(const char *key, char **rsc_id, char **op_type, guint *interval_ms)
Definition: operations.c:47
crm_md5sum
char * crm_md5sum(const char *buffer)
Definition: utils.c:1159
crm_is_daemon_name
bool crm_is_daemon_name(const char *name)
Check whether a string represents a cluster daemon name.
Definition: utils.c:1142
crm_generate_uuid
char * crm_generate_uuid(void)
Definition: utils.c:1084
__attribute__
enum crm_proc_flag __attribute__
pcmk_hostname
char * pcmk_hostname(void)
Get the local hostname.
Definition: utils.c:1201
crm_generate_ra_key
char * crm_generate_ra_key(const char *standard, const char *provider, const char *type)
Definition: agents.c:68
lrmd_event_data_s
Definition: lrmd.h:198
crm_strdup_printf
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
rsc_op_expected_rc
int rsc_op_expected_rc(lrmd_event_data_t *event)
Definition: operations.c:332
crm_provider_required
bool crm_provider_required(const char *standard)
Check whether a resource standard requires a provider to be specified.
Definition: agents.c:90
g_str_hash_traditional
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:361
score2char
char * score2char(int score)
Definition: utils.c:251
crm_strip_trailing_newline
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:208
pcmk_ra_cap_status
Definition: util.h:146
crm_op_needs_metadata
bool crm_op_needs_metadata(const char *rsc_class, const char *op)
Check whether an operation requires resource agent meta-data.
Definition: operations.c:407
lrmd.h
Resource agent executor.
crm_create_op_xml
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:380
crm_user_lookup
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:402
crm_default_remote_port
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
Definition: remote.c:1190
pcmk_format_nvpair
char * pcmk_format_nvpair(const char *name, const char *value, const char *units)
Definition: nvpair.c:279
crm_build_path
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
Definition: io.c:42
crm_meta_value
const char * crm_meta_value(GHashTable *hash, const char *field)
Definition: utils.c:761
crm_parse_int
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
Definition: strings.c:114
decode_transition_key
gboolean decode_transition_key(const char *key, char **uuid, int *action, int *transition_id, int *target_rc)
Parse a transition key into its constituent parts.
Definition: operations.c:215
safe_str_neq
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:161
crm_parse_interval_spec
guint crm_parse_interval_spec(const char *input)
Definition: utils.c:542
char2score
int char2score(const char *score)
Definition: utils.c:199
pcmk_scan_nvpair
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:218
crm_get_msec
long long crm_get_msec(const char *input)
Definition: utils.c:572
score2char_stack
char * score2char_stack(int score, char *buf, size_t len)
Definition: utils.c:237
op_status
op_status
Definition: services.h:118
crm_parse_agent_spec
int crm_parse_agent_spec(const char *spec, char **standard, char **provider, char **type)
Parse a "standard[:provider]:type" agent specification.
Definition: agents.c:110
pcmk_acl_required
bool pcmk_acl_required(const char *user)
Definition: acl.c:701
pcmk_get_ra_caps
uint32_t pcmk_get_ra_caps(const char *standard)
Get capabilities of a resource agent standard.
Definition: agents.c:29
pcmk_ra_cap_unique
Definition: util.h:148
crm_meta_name
char * crm_meta_name(const char *field)
Definition: utils.c:739
pcmk_ra_cap_none
Definition: util.h:144
pcmk_format_named_time
char * pcmk_format_named_time(const char *name, time_t epoch_time)
Definition: nvpair.c:295