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
21 /**
22 * \file
23 * \brief Utility functions
24 * \ingroup core
25 */
26
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
crm_strcase_equal(gconstpointer a,gconstpointer b)79 crm_strcase_equal(gconstpointer a, gconstpointer b)
80 {
81 return crm_str_eq((const char *) a, (const char *) b, FALSE);
82 }
83
84 /*!
85 * \brief Create hash table with dynamically allocated string keys/values
86 *
87 * \return Newly hash table
88 * \note It is the caller's responsibility to free the result, using
89 * g_hash_table_destroy().
90 */
91 static inline GHashTable *
crm_str_table_new()92 crm_str_table_new()
93 {
94 return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
95 }
96
97 /*!
98 * \brief Create hash table with case-insensitive dynamically allocated string keys/values
99 *
100 * \return Newly hash table
101 * \note It is the caller's responsibility to free the result, using
102 * g_hash_table_destroy().
103 */
104 static inline GHashTable *
crm_strcase_table_new()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);
131 int rsc_op_expected_rc(lrmd_event_data_t *event);
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
142 enum pcmk_ra_caps {
143 pcmk_ra_cap_none = 0x000,
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
is_not_set(long long word,long long bit)166 is_not_set(long long word, long long bit)
167 {
168 return ((word & bit) == 0);
169 }
170
171 static inline gboolean
is_set(long long word,long long bit)172 is_set(long long word, long long bit)
173 {
174 return ((word & bit) == bit);
175 }
176
177 static inline gboolean
is_set_any(long long word,long long bit)178 is_set_any(long long word, long long bit)
179 {
180 return ((word & bit) != 0);
181 }
182
183 static inline guint
crm_hash_table_size(GHashTable * hashtable)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
210