pacemaker  2.0.3-4b1f869f0f
Scalable High-Availability cluster resource manager
crm_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-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_INTERNAL__H
11 # define CRM_INTERNAL__H
12 
13 # include <config.h>
14 # include <portability.h>
15 
16 # include <glib.h>
17 # include <stdbool.h>
18 # include <libxml/tree.h>
19 
20 # include <crm/lrmd.h>
21 # include <crm/common/logging.h>
22 # include <crm/common/ipcs.h>
23 # include <crm/common/internal.h>
24 
25 /* Dynamic loading of libraries */
26 void *find_library_function(void **handle, const char *lib, const char *fn, int fatal);
27 
28 /* For ACLs */
29 char *uid2username(uid_t uid);
30 const char *crm_acl_get_set_user(xmlNode * request, const char *field, const char *peer_user);
31 
32 # if ENABLE_ACL
33 # include <string.h>
34 static inline gboolean
35 is_privileged(const char *user)
36 {
37  if (user == NULL) {
38  return FALSE;
39  } else if (strcmp(user, CRM_DAEMON_USER) == 0) {
40  return TRUE;
41  } else if (strcmp(user, "root") == 0) {
42  return TRUE;
43  }
44  return FALSE;
45 }
46 # endif
47 
48 /* CLI option processing*/
49 # ifdef HAVE_GETOPT_H
50 # include <getopt.h>
51 # else
52 # define no_argument 0
53 # define required_argument 1
54 # endif
55 
56 # define pcmk_option_default 0x00000
57 # define pcmk_option_hidden 0x00001
58 # define pcmk_option_paragraph 0x00002
59 # define pcmk_option_example 0x00004
60 
61 struct crm_option {
62  /* Fields from 'struct option' in getopt.h */
63  /* name of long option */
64  const char *name;
65  /*
66  * one of no_argument, required_argument, and optional_argument:
67  * whether option takes an argument
68  */
69  int has_arg;
70  /* if not NULL, set *flag to val when option found */
71  int *flag;
72  /* if flag not NULL, value to set *flag to; else return value */
73  int val;
74 
75  /* Custom fields */
76  const char *desc;
77  long flags;
78 };
79 
80 void crm_set_options(const char *short_options, const char *usage, struct crm_option *long_options,
81  const char *app_desc);
82 int crm_get_option(int argc, char **argv, int *index);
83 int crm_get_option_long(int argc, char **argv, int *index, const char **longname);
84 _Noreturn void crm_help(char cmd, crm_exit_t exit_code);
85 
86 /* Cluster Option Processing */
87 typedef struct pe_cluster_option_s {
88  const char *name;
89  const char *alt_name;
90  const char *type;
91  const char *values;
92  const char *default_value;
93 
94  gboolean(*is_valid) (const char *);
95 
96  const char *description_short;
97  const char *description_long;
98 
100 
101 const char *cluster_option(GHashTable * options, gboolean(*validate) (const char *),
102  const char *name, const char *old_name, const char *def_value);
103 
104 const char *get_cluster_pref(GHashTable * options, pe_cluster_option * option_list, int len,
105  const char *name);
106 
107 void config_metadata(const char *name, const char *version, const char *desc_short,
108  const char *desc_long, pe_cluster_option * option_list, int len);
109 
110 void verify_all_options(GHashTable * options, pe_cluster_option * option_list, int len);
111 gboolean check_time(const char *value);
112 gboolean check_timer(const char *value);
113 gboolean check_boolean(const char *value);
114 gboolean check_number(const char *value);
115 gboolean check_positive_number(const char *value);
116 gboolean check_quorum(const char *value);
117 gboolean check_script(const char *value);
118 gboolean check_utilization(const char *value);
119 long crm_get_sbd_timeout(void);
120 long crm_auto_watchdog_timeout(void);
121 gboolean check_sbd_timeout(const char *value);
122 void crm_args_fini(void);
123 
124 /* char2score */
125 extern int node_score_red;
126 extern int node_score_green;
127 extern int node_score_yellow;
128 
129 /* Assorted convenience functions */
130 void crm_make_daemon(const char *name, gboolean daemonize, const char *pidfile);
131 
132 // printf-style format to create operation ID from resource, action, interval
133 #define CRM_OP_FMT "%s_%s_%u"
134 
135 static inline long long
136 crm_clear_bit(const char *function, int line, const char *target, long long word, long long bit)
137 {
138  long long rc = (word & ~bit);
139 
140  if (rc == word) {
141  /* Unchanged */
142  } else if (target) {
143  crm_trace("Bit 0x%.8llx for %s cleared by %s:%d", bit, target, function, line);
144  } else {
145  crm_trace("Bit 0x%.8llx cleared by %s:%d", bit, function, line);
146  }
147 
148  return rc;
149 }
150 
151 static inline long long
152 crm_set_bit(const char *function, int line, const char *target, long long word, long long bit)
153 {
154  long long rc = (word | bit);
155 
156  if (rc == word) {
157  /* Unchanged */
158  } else if (target) {
159  crm_trace("Bit 0x%.8llx for %s set by %s:%d", bit, target, function, line);
160  } else {
161  crm_trace("Bit 0x%.8llx set by %s:%d", bit, function, line);
162  }
163 
164  return rc;
165 }
166 
167 # define set_bit(word, bit) word = crm_set_bit(__FUNCTION__, __LINE__, NULL, word, bit)
168 # define clear_bit(word, bit) word = crm_clear_bit(__FUNCTION__, __LINE__, NULL, word, bit)
169 
170 char *generate_hash_key(const char *crm_msg_reference, const char *sys);
171 
172 const char *daemon_option(const char *option);
173 void set_daemon_option(const char *option, const char *value);
174 gboolean daemon_option_enabled(const char *daemon, const char *option);
175 void strip_text_nodes(xmlNode * xml);
176 void pcmk_panic(const char *origin);
177 pid_t pcmk_locate_sbd(void);
178 
179 # define crm_config_err(fmt...) { crm_config_error = TRUE; crm_err(fmt); }
180 # define crm_config_warn(fmt...) { crm_config_warning = TRUE; crm_warn(fmt); }
181 
182 # define F_ATTRD_KEY "attr_key"
183 # define F_ATTRD_ATTRIBUTE "attr_name"
184 # define F_ATTRD_REGEX "attr_regex"
185 # define F_ATTRD_TASK "task"
186 # define F_ATTRD_VALUE "attr_value"
187 # define F_ATTRD_SET "attr_set"
188 # define F_ATTRD_IS_REMOTE "attr_is_remote"
189 # define F_ATTRD_IS_PRIVATE "attr_is_private"
190 # define F_ATTRD_SECTION "attr_section"
191 # define F_ATTRD_DAMPEN "attr_dampening"
192 # define F_ATTRD_HOST "attr_host"
193 # define F_ATTRD_HOST_ID "attr_host_id"
194 # define F_ATTRD_USER "attr_user"
195 # define F_ATTRD_WRITER "attr_writer"
196 # define F_ATTRD_VERSION "attr_version"
197 # define F_ATTRD_RESOURCE "attr_resource"
198 # define F_ATTRD_OPERATION "attr_clear_operation"
199 # define F_ATTRD_INTERVAL "attr_clear_interval"
200 # define F_ATTRD_IS_FORCE_WRITE "attrd_is_force_write"
201 
202 /* attrd operations */
203 # define ATTRD_OP_PEER_REMOVE "peer-remove"
204 # define ATTRD_OP_UPDATE "update"
205 # define ATTRD_OP_UPDATE_BOTH "update-both"
206 # define ATTRD_OP_UPDATE_DELAY "update-delay"
207 # define ATTRD_OP_QUERY "query"
208 # define ATTRD_OP_REFRESH "refresh"
209 # define ATTRD_OP_FLUSH "flush"
210 # define ATTRD_OP_SYNC "sync"
211 # define ATTRD_OP_SYNC_RESPONSE "sync-response"
212 # define ATTRD_OP_CLEAR_FAILURE "clear-failure"
213 
214 # define PCMK_ENV_PHYSICAL_HOST "physical_host"
215 
216 
217 # if SUPPORT_COROSYNC
218 # include <qb/qbipc_common.h>
219 # include <corosync/corotypes.h>
220 typedef struct qb_ipc_request_header cs_ipc_header_request_t;
221 typedef struct qb_ipc_response_header cs_ipc_header_response_t;
222 # else
223 typedef struct {
224  int size __attribute__ ((aligned(8)));
225  int id __attribute__ ((aligned(8)));
226 } __attribute__ ((aligned(8))) cs_ipc_header_request_t;
227 
228 typedef struct {
229  int size __attribute__ ((aligned(8)));
230  int id __attribute__ ((aligned(8)));
231  int error __attribute__ ((aligned(8)));
232 } __attribute__ ((aligned(8))) cs_ipc_header_response_t;
233 
234 # endif
235 
236 void
237 attrd_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb);
238 void
239 stonith_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb);
240 
241 qb_ipcs_service_t *
242 crmd_ipc_server_init(struct qb_ipcs_service_handlers *cb);
243 
244 void cib_ipc_servers_init(qb_ipcs_service_t **ipcs_ro,
245  qb_ipcs_service_t **ipcs_rw,
246  qb_ipcs_service_t **ipcs_shm,
247  struct qb_ipcs_service_handlers *ro_cb,
248  struct qb_ipcs_service_handlers *rw_cb);
249 
250 void cib_ipc_servers_destroy(qb_ipcs_service_t *ipcs_ro,
251  qb_ipcs_service_t *ipcs_rw,
252  qb_ipcs_service_t *ipcs_shm);
253 
254 static inline void *realloc_safe(void *ptr, size_t size)
255 {
256  void *ret = realloc(ptr, size);
257 
258  if (ret == NULL) {
259  free(ptr); /* make coverity happy */
260  abort();
261  }
262 
263  return ret;
264 }
265 
266 const char *crm_xml_add_last_written(xmlNode *xml_node);
267 void crm_xml_dump(xmlNode * data, int options, char **buffer, int *offset, int *max, int depth);
268 void crm_buffer_add_char(char **buffer, int *offset, int *max, char c);
269 
270 gboolean crm_digest_verify(xmlNode *input, const char *expected);
271 
272 /* cross-platform compatibility functions */
273 char *crm_compat_realpath(const char *path);
274 
275 /* IPC Proxy Backend Shared Functions */
276 typedef struct remote_proxy_s {
277  char *node_name;
278  char *session_id;
279 
280  gboolean is_local;
281 
284  uint32_t last_request_id;
286 
288 
290  lrmd_t *lrmd, struct ipc_client_callbacks *proxy_callbacks,
291  const char *node_name, const char *session_id, const char *channel);
292 
293 int remote_proxy_check(lrmd_t *lrmd, GHashTable *hash);
294 void remote_proxy_cb(lrmd_t *lrmd, const char *node_name, xmlNode *msg);
297 
298 int remote_proxy_dispatch(const char *buffer, ssize_t length, gpointer userdata);
299 void remote_proxy_disconnected(gpointer data);
300 void remote_proxy_free(gpointer data);
301 
302 void remote_proxy_relay_event(remote_proxy_t *proxy, xmlNode *msg);
303 void remote_proxy_relay_response(remote_proxy_t *proxy, xmlNode *msg, int msg_id);
304 
305 #endif /* CRM_INTERNAL__H */
uid2username
char * uid2username(uid_t uid)
crm_acl_get_set_user
const char * crm_acl_get_set_user(xmlNode *request, const char *field, const char *peer_user)
crm_get_option
int crm_get_option(int argc, char **argv, int *index)
Definition: utils.c:868
crm_buffer_add_char
void crm_buffer_add_char(char **buffer, int *offset, int *max, char c)
Definition: xml.c:3278
stonith_ipc_server_init
void stonith_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb)
Definition: utils.c:1040
crm_option::desc
const char * desc
Definition: crm_internal.h:76
ipcs.h
remote_proxy_dispatch
int remote_proxy_dispatch(const char *buffer, ssize_t length, gpointer userdata)
Definition: proxy_common.c:116
crm_option
Definition: crm_internal.h:61
portability.h
node_score_yellow
int node_score_yellow
Definition: utils.c:65
data
char data[0]
Definition: internal.h:12
remote_proxy_s
Definition: crm_internal.h:276
remote_proxy_relay_response
void remote_proxy_relay_response(remote_proxy_t *proxy, xmlNode *msg, int msg_id)
Definition: proxy_common.c:79
remote_proxy_relay_event
void remote_proxy_relay_event(remote_proxy_t *proxy, xmlNode *msg)
Definition: proxy_common.c:66
lrmd_s
Definition: lrmd.h:533
crm_compat_realpath
char * crm_compat_realpath(const char *path)
Definition: compat.c:40
crm_set_options
void crm_set_options(const char *short_options, const char *usage, struct crm_option *long_options, const char *app_desc)
Definition: utils.c:827
crm_args_fini
void crm_args_fini(void)
Definition: utils.c:195
check_sbd_timeout
gboolean check_sbd_timeout(const char *value)
Definition: watchdog.c:234
crm_trace
#define crm_trace(fmt, args...)
Definition: logging.h:247
crm_option::has_arg
int has_arg
Definition: crm_internal.h:69
remote_proxy_check
int remote_proxy_check(lrmd_t *lrmd, GHashTable *hash)
Definition: lrmd_client.c:874
crm_digest_verify
gboolean crm_digest_verify(xmlNode *input, const char *expected)
Definition: digest.c:226
crm_make_daemon
void crm_make_daemon(const char *name, gboolean daemonize, const char *pidfile)
Definition: utils.c:695
pe_cluster_option_s::name
const char * name
Definition: crm_internal.h:88
remote_proxy_s::last_request_id
uint32_t last_request_id
Definition: crm_internal.h:284
pe_cluster_option_s::default_value
const char * default_value
Definition: crm_internal.h:92
pcmk_panic
void pcmk_panic(const char *origin)
Definition: watchdog.c:144
remote_proxy_ack_shutdown
void remote_proxy_ack_shutdown(lrmd_t *lrmd)
Send an acknowledgment of a remote proxy shutdown request.
Definition: proxy_common.c:42
cib_ipc_servers_init
void cib_ipc_servers_init(qb_ipcs_service_t **ipcs_ro, qb_ipcs_service_t **ipcs_rw, qb_ipcs_service_t **ipcs_shm, struct qb_ipcs_service_handlers *ro_cb, struct qb_ipcs_service_handlers *rw_cb)
Definition: utils.c:995
remote_proxy_t
struct remote_proxy_s remote_proxy_t
pe_cluster_option_s::type
const char * type
Definition: crm_internal.h:90
remote_proxy_new
remote_proxy_t * remote_proxy_new(lrmd_t *lrmd, struct ipc_client_callbacks *proxy_callbacks, const char *node_name, const char *session_id, const char *channel)
Definition: proxy_common.c:163
pe_cluster_option_s::values
const char * values
Definition: crm_internal.h:91
__attribute__
enum crm_proc_flag __attribute__
crm_auto_watchdog_timeout
long crm_auto_watchdog_timeout(void)
Definition: watchdog.c:226
check_number
gboolean check_number(const char *value)
Definition: utils.c:102
pe_cluster_option_s::is_valid
gboolean(* is_valid)(const char *)
Definition: crm_internal.h:94
remote_proxy_disconnected
void remote_proxy_disconnected(gpointer data)
Definition: proxy_common.c:145
crmd_ipc_server_init
qb_ipcs_service_t * crmd_ipc_server_init(struct qb_ipcs_service_handlers *cb)
Definition: utils.c:1022
config_metadata
void config_metadata(const char *name, const char *version, const char *desc_short, const char *desc_long, pe_cluster_option *option_list, int len)
Definition: utils.c:347
ipc_client_callbacks
Definition: mainloop.h:74
remote_proxy_s::lrm
lrmd_t * lrm
Definition: crm_internal.h:285
lrmd.h
Resource agent executor.
cib_ipc_servers_destroy
void cib_ipc_servers_destroy(qb_ipcs_service_t *ipcs_ro, qb_ipcs_service_t *ipcs_rw, qb_ipcs_service_t *ipcs_shm)
Definition: utils.c:1012
remote_proxy_cb
void remote_proxy_cb(lrmd_t *lrmd, const char *node_name, xmlNode *msg)
Definition: proxy_common.c:203
check_time
gboolean check_time(const char *value)
Definition: utils.c:73
cs_ipc_header_response_t
struct qb_ipc_response_header cs_ipc_header_response_t
Definition: crm_internal.h:221
remote_proxy_free
void remote_proxy_free(gpointer data)
Definition: proxy_common.c:105
size
uint32_t size
Definition: internal.h:6
crm_option::flags
long flags
Definition: crm_internal.h:77
crm_option::flag
int * flag
Definition: crm_internal.h:71
check_quorum
gboolean check_quorum(const char *value)
Definition: utils.c:132
pe_cluster_option_s::description_long
const char * description_long
Definition: crm_internal.h:97
check_boolean
gboolean check_boolean(const char *value)
Definition: utils.c:91
pe_cluster_option_s
Definition: crm_internal.h:87
check_script
gboolean check_script(const char *value)
Definition: utils.c:153
check_positive_number
gboolean check_positive_number(const char *value)
Definition: utils.c:123
set_daemon_option
void set_daemon_option(const char *option, const char *value)
Definition: logging.c:138
check_utilization
gboolean check_utilization(const char *value)
Definition: utils.c:180
mainloop_io_t
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:32
remote_proxy_s::node_name
char * node_name
Definition: crm_internal.h:277
daemon
int daemon(int nochdir, int noclose)
crm_option::name
const char * name
Definition: crm_internal.h:64
daemon_option
const char * daemon_option(const char *option)
Definition: logging.c:114
crm_exit_t
enum crm_exit_e crm_exit_t
pe_cluster_option_s::description_short
const char * description_short
Definition: crm_internal.h:96
crm_get_option_long
int crm_get_option_long(int argc, char **argv, int *index, const char **longname)
Definition: utils.c:874
get_cluster_pref
const char * get_cluster_pref(GHashTable *options, pe_cluster_option *option_list, int len, const char *name)
Definition: utils.c:328
logging.h
Wrappers for and extensions to libqb logging.
pe_cluster_option
struct pe_cluster_option_s pe_cluster_option
remote_proxy_s::ipc
crm_ipc_t * ipc
Definition: crm_internal.h:282
config.h
remote_proxy_s::session_id
char * session_id
Definition: crm_internal.h:278
check_timer
gboolean check_timer(const char *value)
Definition: utils.c:82
internal.h
remote_proxy_nack_shutdown
void remote_proxy_nack_shutdown(lrmd_t *lrmd)
We're not going to shutdown as response to a remote proxy shutdown request.
Definition: proxy_common.c:57
attrd_ipc_server_init
void attrd_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb)
Definition: utils.c:1028
crm_xml_dump
void crm_xml_dump(xmlNode *data, int options, char **buffer, int *offset, int *max, int depth)
Definition: xml.c:3173
node_score_red
int node_score_red
Definition: utils.c:63
version
uint32_t version
Definition: remote.c:3
cluster_option
const char * cluster_option(GHashTable *options, gboolean(*validate)(const char *), const char *name, const char *old_name, const char *def_value)
Definition: utils.c:266
crm_help
_Noreturn void crm_help(char cmd, crm_exit_t exit_code)
Definition: utils.c:919
strip_text_nodes
void strip_text_nodes(xmlNode *xml)
Definition: xml.c:2320
crm_option::val
int val
Definition: crm_internal.h:73
crm_xml_add_last_written
const char * crm_xml_add_last_written(xmlNode *xml_node)
Definition: xml.c:2418
remote_proxy_s::is_local
gboolean is_local
Definition: crm_internal.h:280
crm_get_sbd_timeout
long crm_get_sbd_timeout(void)
Definition: watchdog.c:215
pe_cluster_option_s::alt_name
const char * alt_name
Definition: crm_internal.h:89
CRM_DAEMON_USER
#define CRM_DAEMON_USER
Definition: config.h:32
pcmk_locate_sbd
pid_t pcmk_locate_sbd(void)
Definition: watchdog.c:175
crm_ipc_t
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:58
_Noreturn
#define _Noreturn
Definition: config.h:659
remote_proxy_s::source
mainloop_io_t * source
Definition: crm_internal.h:283
cs_ipc_header_request_t
struct qb_ipc_request_header cs_ipc_header_request_t
Definition: crm_internal.h:220
node_score_green
int node_score_green
Definition: utils.c:64
verify_all_options
void verify_all_options(GHashTable *options, pe_cluster_option *option_list, int len)
Definition: utils.c:382
daemon_option_enabled
gboolean daemon_option_enabled(const char *daemon, const char *option)
Definition: logging.c:162
find_library_function
void * find_library_function(void **handle, const char *lib, const char *fn, int fatal)
generate_hash_key
char * generate_hash_key(const char *crm_msg_reference, const char *sys)
Definition: utils.c:395