pacemaker  1.1.24-3850484742
Scalable High-Availability cluster resource manager
util.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This software is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CRM_COMMON_UTIL__H
19 # define CRM_COMMON_UTIL__H
20 
27 # include <sys/types.h>
28 # include <stdlib.h>
29 # include <stdbool.h>
30 # include <stdint.h> // uint32_t
31 # include <limits.h>
32 # include <signal.h>
33 # include <sysexits.h>
34 # include <glib.h>
35 
36 # include <libxml/tree.h>
37 
38 # include <crm/lrmd.h>
39 
40 # if SUPPORT_HEARTBEAT
41 # include <heartbeat.h>
42 # else
43 # define NORMALNODE "normal"
44 # define ACTIVESTATUS "active"/* fully functional, and all links are up */
45 # define DEADSTATUS "dead"
46  /* Status of non-working link or machine */
47 # define PINGSTATUS "ping"
48  /* Status of a working ping node */
49 # define JOINSTATUS "join"
50  /* Status when an api client joins */
51 # define LEAVESTATUS "leave"
52  /* Status when an api client leaves */
53 # define ONLINESTATUS "online"/* Status of an online client */
54 # define OFFLINESTATUS "offline"
55  /* Status of an offline client */
56 # endif
57 
58 /* public Pacemaker Remote functions (from remote.c) */
59 int crm_default_remote_port(void);
60 
61 /* public string functions (from strings.c) */
62 char *crm_itoa_stack(int an_int, char *buf, size_t len);
63 char *crm_itoa(int an_int);
64 gboolean crm_is_true(const char *s);
65 int crm_str_to_boolean(const char *s, int *ret);
66 long long crm_parse_ll(const char *text, const char *default_text);
67 int crm_parse_int(const char *text, const char *default_text);
68 char * crm_strip_trailing_newline(char *str);
69 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
70 gboolean safe_str_neq(const char *a, const char *b);
71 guint crm_strcase_hash(gconstpointer v);
72 guint g_str_hash_traditional(gconstpointer v);
73 
74 # define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
75 # define crm_str_hash g_str_hash_traditional
76 
77 /* used with hash tables where case does not matter */
78 static inline gboolean
79 crm_strcase_equal(gconstpointer a, gconstpointer b)
80 {
81  return crm_str_eq((const char *) a, (const char *) b, FALSE);
82 }
83 
91 static inline GHashTable *
92 crm_str_table_new()
93 {
94  return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
95 }
96 
104 static inline GHashTable *
105 crm_strcase_table_new()
106 {
107  return g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, free, free);
108 }
109 
110 GHashTable *crm_str_table_dup(GHashTable *old_table);
111 
112 # define crm_atoi(text, default_text) crm_parse_int(text, default_text)
113 
114 /* public I/O functions (from io.c) */
115 void crm_build_path(const char *path_c, mode_t mode);
116 
117 long long crm_get_msec(const char *input);
118 unsigned long long crm_get_interval(const char *input);
119 int char2score(const char *score);
120 char *score2char(int score);
121 char *score2char_stack(int score, char *buf, size_t len);
122 
123 /* public operation functions (from operations.c) */
124 gboolean parse_op_key(const char *key, char **rsc_id, char **op_type,
125  int *interval);
126 gboolean decode_transition_key(const char *key, char **uuid, int *action,
127  int *transition_id, int *target_rc);
128 gboolean decode_transition_magic(const char *magic, char **uuid,
129  int *transition_id, int *action_id,
130  int *op_status, int *op_rc, int *target_rc);
132 gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc);
133 bool crm_op_needs_metadata(const char *rsc_class, const char *op);
134 xmlNode *crm_create_op_xml(xmlNode *parent, const char *prefix,
135  const char *task, const char *interval,
136  const char *timeout);
137 #define CRM_DEFAULT_OP_TIMEOUT_S "20s"
138 
139 // Public resource agent functions (from agents.c)
140 
141 // Capabilities supported by a resource agent standard
144  pcmk_ra_cap_provider = 0x001, // Requires provider
145  pcmk_ra_cap_status = 0x002, // Supports status instead of monitor
146  pcmk_ra_cap_params = 0x004, // Supports parameters
147  pcmk_ra_cap_unique = 0x008, // Supports unique clones
148  pcmk_ra_cap_promotable = 0x010, // Supports promotable clones
149 };
150 
151 uint32_t pcmk_get_ra_caps(const char *standard);
152 char *crm_generate_ra_key(const char *standard, const char *provider,
153  const char *type);
154 int crm_parse_agent_spec(const char *spec, char **standard, char **provider,
155  char **type);
156 bool crm_provider_required(const char *standard); // deprecated
157 
158 
159 int compare_version(const char *version1, const char *version2);
160 
161 /* coverity[+kill] */
162 void crm_abort(const char *file, const char *function, int line,
163  const char *condition, gboolean do_core, gboolean do_fork);
164 
165 static inline gboolean
166 is_not_set(long long word, long long bit)
167 {
168  return ((word & bit) == 0);
169 }
170 
171 static inline gboolean
172 is_set(long long word, long long bit)
173 {
174  return ((word & bit) == bit);
175 }
176 
177 static inline gboolean
178 is_set_any(long long word, long long bit)
179 {
180  return ((word & bit) != 0);
181 }
182 
183 static inline guint
184 crm_hash_table_size(GHashTable * hashtable)
185 {
186  if (hashtable == NULL) {
187  return 0;
188  }
189  return g_hash_table_size(hashtable);
190 }
191 
192 char *crm_meta_name(const char *field);
193 const char *crm_meta_value(GHashTable * hash, const char *field);
194 
195 char *crm_md5sum(const char *buffer);
196 
197 char *crm_generate_uuid(void);
198 bool crm_is_daemon_name(const char *name);
199 
200 int crm_user_lookup(const char *name, uid_t * uid, gid_t * gid);
201 
202 #ifdef HAVE_GNUTLS_GNUTLS_H
203 void crm_gnutls_global_init(void);
204 #endif
205 
206 int crm_exit(int rc);
207 bool pcmk_acl_required(const char *user);
208 
209 #endif
bool crm_is_daemon_name(const char *name)
Check whether a string represents a cluster daemon name.
Definition: utils.c:1297
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:182
char * crm_generate_uuid(void)
Definition: utils.c:1279
guint g_str_hash_traditional(gconstpointer v)
Definition: strings.c:382
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
Definition: io.c:40
char * crm_generate_ra_key(const char *standard, const char *provider, const char *type)
Definition: agents.c:74
int rsc_op_expected_rc(lrmd_event_data_t *event)
Definition: operations.c:405
int char2score(const char *score)
Definition: utils.c:221
long long crm_get_msec(const char *input)
Definition: utils.c:589
char * score2char_stack(int score, char *buf, size_t len)
Definition: utils.c:259
const char * crm_meta_value(GHashTable *hash, const char *field)
Definition: utils.c:957
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
Definition: strings.c:157
Local Resource Manager.
bool crm_op_needs_metadata(const char *rsc_class, const char *op)
Check whether an operation requires resource agent meta-data.
Definition: operations.c:657
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:424
char * crm_meta_name(const char *field)
Definition: utils.c:935
int crm_parse_agent_spec(const char *spec, char **standard, char **provider, char **type)
Parse a "standard[:provider]:type" agent specification.
Definition: agents.c:116
gboolean decode_transition_key(const char *key, char **uuid, int *action, int *transition_id, int *target_rc)
Definition: operations.c:233
op_status
Definition: services.h:133
bool pcmk_acl_required(const char *user)
Definition: acl.c:656
unsigned long long crm_get_interval(const char *input)
Definition: utils.c:564
guint crm_strcase_hash(gconstpointer v)
Definition: strings.c:394
xmlNode * crm_create_op_xml(xmlNode *parent, const char *prefix, const char *task, const char *interval, const char *timeout)
Create a CIB XML element for an operation.
Definition: operations.c:455
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:245
char * crm_itoa_stack(int an_int, char *buf, size_t len)
Definition: strings.c:51
int crm_str_to_boolean(const char *s, int *ret)
Definition: strings.c:208
gboolean parse_op_key(const char *key, char **rsc_id, char **op_type, int *interval)
Definition: operations.c:46
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
Definition: strings.c:134
gboolean did_rsc_op_fail(lrmd_event_data_t *event, int target_rc)
Definition: operations.c:420
GHashTable * crm_str_table_dup(GHashTable *old_table)
Definition: strings.c:414
int compare_version(const char *version1, const char *version2)
Definition: utils.c:477
#define crm_str_hash
Definition: util.h:75
#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:190
int crm_exit(int rc)
Definition: utils.c:74
char * crm_md5sum(const char *buffer)
Definition: utils.c:1313
bool crm_provider_required(const char *standard)
Check whether a resource standard requires a provider to be specified.
Definition: agents.c:96
gboolean crm_is_true(const char *s)
Definition: strings.c:197
char * crm_strip_trailing_newline(char *str)
Definition: strings.c:229
pcmk_ra_caps
Definition: util.h:142
char * crm_itoa(int an_int)
Definition: strings.c:61
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
Definition: utils.c:647
int crm_default_remote_port(void)
Get the default remote connection TCP port on this host.
Definition: remote.c:1220
enum crm_ais_msg_types type
Definition: internal.h:79
char * score2char(int score)
Definition: utils.c:273