pacemaker  2.0.2-debe490
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-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>
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 
42 /* public Pacemaker Remote functions (from remote.c) */
43 int crm_default_remote_port(void);
44 
45 /* public string functions (from strings.c) */
46 char *crm_itoa_stack(int an_int, char *buf, size_t len);
47 gboolean crm_is_true(const char *s);
48 int crm_str_to_boolean(const char *s, int *ret);
49 long long crm_parse_ll(const char *text, const char *default_text);
50 int crm_parse_int(const char *text, const char *default_text);
51 char * crm_strip_trailing_newline(char *str);
52 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
53 gboolean safe_str_neq(const char *a, const char *b);
54 gboolean crm_strcase_equal(gconstpointer a, gconstpointer b);
55 guint crm_strcase_hash(gconstpointer v);
56 guint g_str_hash_traditional(gconstpointer v);
57 char *crm_strdup_printf(char const *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
58 
59 # define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
60 # define crm_str_hash g_str_hash_traditional
61 
62 static inline char *
63 crm_itoa(int an_int)
64 {
65  return crm_strdup_printf("%d", an_int);
66 }
67 
75 static inline GHashTable *
76 crm_str_table_new()
77 {
78  return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
79 }
80 
88 static inline GHashTable *
89 crm_strcase_table_new()
90 {
91  return g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, free, free);
92 }
93 
94 GHashTable *crm_str_table_dup(GHashTable *old_table);
95 
96 # define crm_atoi(text, default_text) crm_parse_int(text, default_text)
97 
98 /* public I/O functions (from io.c) */
99 void crm_build_path(const char *path_c, mode_t mode);
100 
101 long long crm_get_msec(const char *input);
102 guint crm_parse_interval_spec(const char *input);
103 int char2score(const char *score);
104 char *score2char(int score);
105 char *score2char_stack(int score, char *buf, size_t len);
106 
107 // deprecated
108 #define crm_get_interval crm_parse_interval_spec
109 
110 /* public operation functions (from operations.c) */
111 gboolean parse_op_key(const char *key, char **rsc_id, char **op_type,
112  guint *interval_ms);
113 gboolean decode_transition_key(const char *key, char **uuid, int *action,
114  int *transition_id, int *target_rc);
115 gboolean decode_transition_magic(const char *magic, char **uuid,
116  int *transition_id, int *action_id,
117  int *op_status, int *op_rc, int *target_rc);
119 gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc);
120 bool crm_op_needs_metadata(const char *rsc_class, const char *op);
121 xmlNode *crm_create_op_xml(xmlNode *parent, const char *prefix,
122  const char *task, const char *interval_spec,
123  const char *timeout);
124 #define CRM_DEFAULT_OP_TIMEOUT_S "20s"
125 
126 // Public resource agent functions (from agents.c)
127 
128 // Capabilities supported by a resource agent standard
131  pcmk_ra_cap_provider = 0x001, // Requires provider
132  pcmk_ra_cap_status = 0x002, // Supports status instead of monitor
133  pcmk_ra_cap_params = 0x004, // Supports parameters
134  pcmk_ra_cap_unique = 0x008, // Supports unique clones
135  pcmk_ra_cap_promotable = 0x010, // Supports promotable clones
136 };
137 
138 uint32_t pcmk_get_ra_caps(const char *standard);
139 char *crm_generate_ra_key(const char *standard, const char *provider,
140  const char *type);
141 int crm_parse_agent_spec(const char *spec, char **standard, char **provider,
142  char **type);
143 bool crm_provider_required(const char *standard); // deprecated
144 
145 
146 int compare_version(const char *version1, const char *version2);
147 
148 /* coverity[+kill] */
149 void crm_abort(const char *file, const char *function, int line,
150  const char *condition, gboolean do_core, gboolean do_fork);
151 
152 static inline gboolean
153 is_not_set(long long word, long long bit)
154 {
155  return ((word & bit) == 0);
156 }
157 
158 static inline gboolean
159 is_set(long long word, long long bit)
160 {
161  return ((word & bit) == bit);
162 }
163 
164 static inline gboolean
165 is_set_any(long long word, long long bit)
166 {
167  return ((word & bit) != 0);
168 }
169 
170 static inline guint
171 crm_hash_table_size(GHashTable * hashtable)
172 {
173  if (hashtable == NULL) {
174  return 0;
175  }
176  return g_hash_table_size(hashtable);
177 }
178 
179 char *crm_meta_name(const char *field);
180 const char *crm_meta_value(GHashTable * hash, const char *field);
181 
182 char *crm_md5sum(const char *buffer);
183 
184 char *crm_generate_uuid(void);
185 bool crm_is_daemon_name(const char *name);
186 
187 int crm_user_lookup(const char *name, uid_t * uid, gid_t * gid);
188 
189 #ifdef HAVE_GNUTLS_GNUTLS_H
190 void crm_gnutls_global_init(void);
191 #endif
192 
193 bool pcmk_acl_required(const char *user);
194 
195 char *pcmk_hostname(void);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif
gboolean parse_op_key(const char *key, char **rsc_id, char **op_type, guint *interval_ms)
Definition: operations.c:47
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:422
bool crm_is_daemon_name(const char *name)
Check whether a string represents a cluster daemon name.
Definition: utils.c:1136
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:157
char * crm_generate_uuid(void)
Definition: utils.c:1078
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:357
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:376
int char2score(const char *score)
Definition: utils.c:199
long long crm_get_msec(const char *input)
Definition: utils.c:567
char * score2char_stack(int score, char *buf, size_t len)
Definition: utils.c:237
const char * crm_meta_value(GHashTable *hash, const char *field)
Definition: utils.c:756
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
Definition: strings.c:110
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:607
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:402
guint crm_parse_interval_spec(const char *input)
Definition: utils.c:542
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:217
char * crm_meta_name(const char *field)
Definition: utils.c:734
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:110
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:222
op_status
Definition: services.h:118
bool pcmk_acl_required(const char *user)
Definition: acl.c:621
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:376
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:220
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:183
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
Definition: strings.c:87
gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc)
Definition: operations.c:387
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:396
int compare_version(const char *version1, const char *version2)
Definition: utils.c:461
#define crm_str_hash
Definition: util.h:60
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:160
char * crm_md5sum(const char *buffer)
Definition: utils.c:1153
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:172
char * pcmk_hostname(void)
Get the local hostname.
Definition: utils.c:1195
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:204
pcmk_ra_caps
Definition: util.h:129
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: utils.c:625
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:85
gboolean crm_strcase_equal(gconstpointer a, gconstpointer b)
Definition: strings.c:370
char * score2char(int score)
Definition: utils.c:251