pacemaker  2.0.1-57cc9c14bf
Scalable High-Availability cluster resource manager
util.h
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2018 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This source code is licensed under the GNU Lesser General Public License
5  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
6  */
7 
8 #ifndef CRM_COMMON_UTIL__H
9 # define CRM_COMMON_UTIL__H
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
21 # include <sys/types.h>
22 # include <stdlib.h>
23 # include <stdbool.h>
24 # include <stdint.h> // uint32_t
25 # include <limits.h>
26 # include <signal.h>
27 # include <glib.h>
28 
29 # include <libxml/tree.h>
30 
31 # include <crm/lrmd.h>
32 # include <crm/common/results.h>
33 
34 # define ONLINESTATUS "online" // Status of an online client
35 # define OFFLINESTATUS "offline" // Status of an offline client
36 
37 /* public Pacemaker Remote functions (from remote.c) */
38 int crm_default_remote_port(void);
39 
40 /* public string functions (from strings.c) */
41 char *crm_itoa_stack(int an_int, char *buf, size_t len);
42 gboolean crm_is_true(const char *s);
43 int crm_str_to_boolean(const char *s, int *ret);
44 long long crm_parse_ll(const char *text, const char *default_text);
45 int crm_parse_int(const char *text, const char *default_text);
46 char * crm_strip_trailing_newline(char *str);
47 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
48 gboolean safe_str_neq(const char *a, const char *b);
49 gboolean crm_strcase_equal(gconstpointer a, gconstpointer b);
50 guint crm_strcase_hash(gconstpointer v);
51 guint g_str_hash_traditional(gconstpointer v);
52 char *crm_strdup_printf(char const *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
53 
54 # define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
55 # define crm_str_hash g_str_hash_traditional
56 
57 static inline char *
58 crm_itoa(int an_int)
59 {
60  return crm_strdup_printf("%d", an_int);
61 }
62 
70 static inline GHashTable *
71 crm_str_table_new()
72 {
73  return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
74 }
75 
83 static inline GHashTable *
84 crm_strcase_table_new()
85 {
86  return g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, free, free);
87 }
88 
89 GHashTable *crm_str_table_dup(GHashTable *old_table);
90 
91 # define crm_atoi(text, default_text) crm_parse_int(text, default_text)
92 
93 /* public I/O functions (from io.c) */
94 void crm_build_path(const char *path_c, mode_t mode);
95 
96 long long crm_get_msec(const char *input);
97 guint crm_parse_interval_spec(const char *input);
98 int char2score(const char *score);
99 char *score2char(int score);
100 char *score2char_stack(int score, char *buf, size_t len);
101 
102 // deprecated
103 #define crm_get_interval crm_parse_interval_spec
104 
105 /* public operation functions (from operations.c) */
106 gboolean parse_op_key(const char *key, char **rsc_id, char **op_type,
107  guint *interval_ms);
108 gboolean decode_transition_key(const char *key, char **uuid, int *action,
109  int *transition_id, int *target_rc);
110 gboolean decode_transition_magic(const char *magic, char **uuid,
111  int *transition_id, int *action_id,
112  int *op_status, int *op_rc, int *target_rc);
114 gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc);
115 bool crm_op_needs_metadata(const char *rsc_class, const char *op);
116 xmlNode *crm_create_op_xml(xmlNode *parent, const char *prefix,
117  const char *task, const char *interval_spec,
118  const char *timeout);
119 #define CRM_DEFAULT_OP_TIMEOUT_S "20s"
120 
121 // Public resource agent functions (from agents.c)
122 
123 // Capabilities supported by a resource agent standard
126  pcmk_ra_cap_provider = 0x001, // Requires provider
127  pcmk_ra_cap_status = 0x002, // Supports status instead of monitor
128  pcmk_ra_cap_params = 0x004, // Supports parameters
129  pcmk_ra_cap_unique = 0x008, // Supports unique clones
130  pcmk_ra_cap_promotable = 0x010, // Supports promotable clones
131 };
132 
133 uint32_t pcmk_get_ra_caps(const char *standard);
134 char *crm_generate_ra_key(const char *standard, const char *provider,
135  const char *type);
136 int crm_parse_agent_spec(const char *spec, char **standard, char **provider,
137  char **type);
138 bool crm_provider_required(const char *standard); // deprecated
139 
140 
141 int compare_version(const char *version1, const char *version2);
142 
143 /* coverity[+kill] */
144 void crm_abort(const char *file, const char *function, int line,
145  const char *condition, gboolean do_core, gboolean do_fork);
146 
147 static inline gboolean
148 is_not_set(long long word, long long bit)
149 {
150  return ((word & bit) == 0);
151 }
152 
153 static inline gboolean
154 is_set(long long word, long long bit)
155 {
156  return ((word & bit) == bit);
157 }
158 
159 static inline gboolean
160 is_set_any(long long word, long long bit)
161 {
162  return ((word & bit) != 0);
163 }
164 
165 static inline guint
166 crm_hash_table_size(GHashTable * hashtable)
167 {
168  if (hashtable == NULL) {
169  return 0;
170  }
171  return g_hash_table_size(hashtable);
172 }
173 
174 char *crm_meta_name(const char *field);
175 const char *crm_meta_value(GHashTable * hash, const char *field);
176 
177 char *crm_md5sum(const char *buffer);
178 
179 char *crm_generate_uuid(void);
180 bool crm_is_daemon_name(const char *name);
181 
182 int crm_user_lookup(const char *name, uid_t * uid, gid_t * gid);
183 
184 #ifdef HAVE_GNUTLS_GNUTLS_H
185 void crm_gnutls_global_init(void);
186 #endif
187 
188 bool pcmk_acl_required(const char *user);
189 
190 char *pcmk_hostname(void);
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 #endif
gboolean parse_op_key(const char *key, char **rsc_id, char **op_type, guint *interval_ms)
Definition: operations.c:45
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:359
bool crm_is_daemon_name(const char *name)
Check whether a string represents a cluster daemon name.
Definition: utils.c:1133
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:141
char * crm_generate_uuid(void)
Definition: utils.c:1075
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:341
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
Definition: io.c:39
char * crm_generate_ra_key(const char *standard, const char *provider, const char *type)
Definition: agents.c:68
int rsc_op_expected_rc(lrmd_event_data_t *event)
Definition: operations.c:309
int char2score(const char *score)
Definition: utils.c:197
long long crm_get_msec(const char *input)
Definition: utils.c:565
char * score2char_stack(int score, char *buf, size_t len)
Definition: utils.c:235
const char * crm_meta_value(GHashTable *hash, const char *field)
Definition: utils.c:754
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
Definition: strings.c:107
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:556
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:400
guint crm_parse_interval_spec(const char *input)
Definition: utils.c:540
char * crm_meta_name(const char *field)
Definition: utils.c:732
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
gboolean decode_transition_key(const char *key, char **uuid, int *action, int *transition_id, int *target_rc)
Definition: operations.c:177
op_status
Definition: services.h:116
bool pcmk_acl_required(const char *user)
Definition: acl.c:621
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:360
uint32_t pcmk_get_ra_caps(const char *standard)
Get capabilities of a resource agent standard.
Definition: agents.c:29
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
Definition: strings.c:204
char * crm_itoa_stack(int an_int, char *buf, size_t len)
Definition: strings.c:22
Function and executable result codes.
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:167
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
Definition: strings.c:85
gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc)
Definition: operations.c:324
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:380
int compare_version(const char *version1, const char *version2)
Definition: utils.c:453
#define crm_str_hash
Definition: util.h:55
#define uint32_t
Definition: stdint.in.h:158
gboolean decode_transition_magic(const char *magic, char **uuid, int *transition_id, int *action_id, int *op_status, int *op_rc, int *target_rc)
Definition: operations.c:143
char * crm_md5sum(const char *buffer)
Definition: utils.c:1150
bool crm_provider_required(const char *standard)
Check whether a resource standard requires a provider to be specified.
Definition: agents.c:90
gboolean crm_is_true(const char *s)
Definition: strings.c:156
char * pcmk_hostname(void)
Get the local hostname.
Definition: utils.c:1192
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:188
pcmk_ra_caps
Definition: util.h:124
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: utils.c:623
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
Definition: remote.c:1180
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:354
char * score2char(int score)
Definition: utils.c:249