pacemaker  1.1.14-70404b0
Scalable High-Availability cluster resource manager
crm_internal.h
Go to the documentation of this file.
1 /* crm_internal.h */
2 
3 /*
4  * Copyright (C) 2006 - 2008
5  * Andrew Beekhof <andrew@beekhof.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef CRM_INTERNAL__H
23 # define CRM_INTERNAL__H
24 
25 # include <config.h>
26 # include <portability.h>
27 
28 # include <glib.h>
29 # include <stdbool.h>
30 # include <libxml/tree.h>
31 
32 # include <crm/lrmd.h>
33 # include <crm/common/logging.h>
34 # include <crm/common/io.h>
35 # include <crm/common/ipcs.h>
36 # include <crm/common/procfs.h>
37 
38 /* Dynamic loading of libraries */
39 void *find_library_function(void **handle, const char *lib, const char *fn, int fatal);
40 void *convert_const_pointer(const void *ptr);
41 
42 /* For ACLs */
43 char *uid2username(uid_t uid);
44 void determine_request_user(const char *user, xmlNode * request, const char *field);
45 const char *crm_acl_get_set_user(xmlNode * request, const char *field, const char *peer_user);
46 
47 # if ENABLE_ACL
48 # include <string.h>
49 static inline gboolean
50 is_privileged(const char *user)
51 {
52  if (user == NULL) {
53  return FALSE;
54  } else if (strcmp(user, CRM_DAEMON_USER) == 0) {
55  return TRUE;
56  } else if (strcmp(user, "root") == 0) {
57  return TRUE;
58  }
59  return FALSE;
60 }
61 # endif
62 
63 /* CLI option processing*/
64 # ifdef HAVE_GETOPT_H
65 # include <getopt.h>
66 # else
67 # define no_argument 0
68 # define required_argument 1
69 # endif
70 
71 # define pcmk_option_default 0x00000
72 # define pcmk_option_hidden 0x00001
73 # define pcmk_option_paragraph 0x00002
74 # define pcmk_option_example 0x00004
75 
76 struct crm_option {
77  /* Fields from 'struct option' in getopt.h */
78  /* name of long option */
79  const char *name;
80  /*
81  * one of no_argument, required_argument, and optional_argument:
82  * whether option takes an argument
83  */
84  int has_arg;
85  /* if not NULL, set *flag to val when option found */
86  int *flag;
87  /* if flag not NULL, value to set *flag to; else return value */
88  int val;
89 
90  /* Custom fields */
91  const char *desc;
92  long flags;
93 };
94 
95 void crm_set_options(const char *short_options, const char *usage, struct crm_option *long_options,
96  const char *app_desc);
97 int crm_get_option(int argc, char **argv, int *index);
98 int crm_get_option_long(int argc, char **argv, int *index, const char **longname);
99 int crm_help(char cmd, int exit_code);
100 
101 /* Cluster Option Processing */
102 typedef struct pe_cluster_option_s {
103  const char *name;
104  const char *alt_name;
105  const char *type;
106  const char *values;
107  const char *default_value;
108 
109  gboolean(*is_valid) (const char *);
110 
111  const char *description_short;
112  const char *description_long;
113 
115 
116 const char *cluster_option(GHashTable * options, gboolean(*validate) (const char *),
117  const char *name, const char *old_name, const char *def_value);
118 
119 const char *get_cluster_pref(GHashTable * options, pe_cluster_option * option_list, int len,
120  const char *name);
121 
122 void config_metadata(const char *name, const char *version, const char *desc_short,
123  const char *desc_long, pe_cluster_option * option_list, int len);
124 
125 void verify_all_options(GHashTable * options, pe_cluster_option * option_list, int len);
126 gboolean check_time(const char *value);
127 gboolean check_timer(const char *value);
128 gboolean check_boolean(const char *value);
129 gboolean check_number(const char *value);
130 gboolean check_quorum(const char *value);
131 gboolean check_script(const char *value);
132 gboolean check_utilization(const char *value);
133 
134 /* Shared PE/crmd functionality */
135 void filter_action_parameters(xmlNode * param_set, const char *version);
136 
137 /* Resource operation updates */
138 xmlNode *create_operation_update(xmlNode * parent, lrmd_event_data_t * event,
139  const char * caller_version, int target_rc, const char * node,
140  const char * origin, int level);
141 
142 /* char2score */
143 extern int node_score_red;
144 extern int node_score_green;
145 extern int node_score_yellow;
146 extern int node_score_infinity;
147 
148 /* Assorted convenience functions */
149 static inline int
150 crm_strlen_zero(const char *s)
151 {
152  return !s || *s == '\0';
153 }
154 
155 char *add_list_element(char *list, const char *value);
156 
157 
178 int crm_pid_active(long pid, const char *daemon);
179 void crm_make_daemon(const char *name, gboolean daemonize, const char *pidfile);
180 
181 char *generate_op_key(const char *rsc_id, const char *op_type, int interval);
182 char *generate_notify_key(const char *rsc_id, const char *notify_type, const char *op_type);
183 char *generate_transition_magic_v202(const char *transition_key, int op_status);
184 char *generate_transition_magic(const char *transition_key, int op_status, int op_rc);
185 char *generate_transition_key(int action, int transition_id, int target_rc, const char *node);
186 
187 static inline long long
188 crm_clear_bit(const char *function, const char *target, long long word, long long bit)
189 {
190  long long rc = (word & ~bit);
191 
192  if (rc == word) {
193  /* Unchanged */
194  } else if (target) {
195  crm_trace("Bit 0x%.8llx for %s cleared by %s", bit, target, function);
196  } else {
197  crm_trace("Bit 0x%.8llx cleared by %s", bit, function);
198  }
199 
200  return rc;
201 }
202 
203 static inline long long
204 crm_set_bit(const char *function, const char *target, long long word, long long bit)
205 {
206  long long rc = (word | bit);
207 
208  if (rc == word) {
209  /* Unchanged */
210  } else if (target) {
211  crm_trace("Bit 0x%.8llx for %s set by %s", bit, target, function);
212  } else {
213  crm_trace("Bit 0x%.8llx set by %s", bit, function);
214  }
215 
216  return rc;
217 }
218 
219 # define set_bit(word, bit) word = crm_set_bit(__FUNCTION__, NULL, word, bit)
220 # define clear_bit(word, bit) word = crm_clear_bit(__FUNCTION__, NULL, word, bit)
221 
222 void g_hash_destroy_str(gpointer data);
223 
224 long long crm_int_helper(const char *text, char **end_text);
225 char *crm_concat(const char *prefix, const char *suffix, char join);
226 char *generate_hash_key(const char *crm_msg_reference, const char *sys);
227 
228 bool crm_compress_string(const char *data, int length, int max, char **result,
229  unsigned int *result_len);
230 
232 typedef struct crm_remote_s crm_remote_t;
233 
234 int crm_remote_send(crm_remote_t * remote, xmlNode * msg);
235 int crm_remote_ready(crm_remote_t * remote, int total_timeout /*ms */ );
236 gboolean crm_remote_recv(crm_remote_t * remote, int total_timeout /*ms */ , int *disconnected);
237 xmlNode *crm_remote_parse_buffer(crm_remote_t * remote);
238 int crm_remote_tcp_connect(const char *host, int port);
239 int crm_remote_tcp_connect_async(const char *host, int port, int timeout, /*ms */
240  int *timer_id, void *userdata, void (*callback) (void *userdata, int sock));
241 
242 # ifdef HAVE_GNUTLS_GNUTLS_H
243 
251 int crm_initiate_client_tls_handshake(crm_remote_t * remote, int timeout_ms);
252 
263 void *crm_create_anon_tls_session(int sock, int type, void *credentials);
264 
275 void *create_psk_tls_session(int csock, int type, void *credentials);
276 # endif
277 
278 # define REMOTE_MSG_TERMINATOR "\r\n\r\n"
279 
280 const char *daemon_option(const char *option);
281 void set_daemon_option(const char *option, const char *value);
282 gboolean daemon_option_enabled(const char *daemon, const char *option);
283 void strip_text_nodes(xmlNode * xml);
284 void pcmk_panic(const char *origin);
285 void sysrq_init(void);
286 pid_t pcmk_locate_sbd(void);
287 int crm_pidfile_inuse(const char *filename, long mypid, const char *daemon);
288 int crm_read_pidfile(const char *filename);
289 
290 # define crm_config_err(fmt...) { crm_config_error = TRUE; crm_err(fmt); }
291 # define crm_config_warn(fmt...) { crm_config_warning = TRUE; crm_warn(fmt); }
292 
293 # define attrd_channel T_ATTRD
294 # define F_ATTRD_KEY "attr_key"
295 # define F_ATTRD_ATTRIBUTE "attr_name"
296 # define F_ATTRD_REGEX "attr_regex"
297 # define F_ATTRD_TASK "task"
298 # define F_ATTRD_VALUE "attr_value"
299 # define F_ATTRD_SET "attr_set"
300 # define F_ATTRD_IS_REMOTE "attr_is_remote"
301 # define F_ATTRD_IS_PRIVATE "attr_is_private"
302 # define F_ATTRD_SECTION "attr_section"
303 # define F_ATTRD_DAMPEN "attr_dampening"
304 # define F_ATTRD_IGNORE_LOCALLY "attr_ignore_locally"
305 # define F_ATTRD_HOST "attr_host"
306 # define F_ATTRD_HOST_ID "attr_host_id"
307 # define F_ATTRD_USER "attr_user"
308 # define F_ATTRD_WRITER "attr_writer"
309 # define F_ATTRD_VERSION "attr_version"
310 
311 /* attrd operations */
312 # define ATTRD_OP_PEER_REMOVE "peer-remove"
313 # define ATTRD_OP_UPDATE "update"
314 # define ATTRD_OP_QUERY "query"
315 # define ATTRD_OP_REFRESH "refresh"
316 # define ATTRD_OP_FLUSH "flush"
317 # define ATTRD_OP_SYNC "sync"
318 # define ATTRD_OP_SYNC_RESPONSE "sync-response"
319 
320 # if SUPPORT_COROSYNC
321 # if CS_USES_LIBQB
322 # include <qb/qbipc_common.h>
323 # include <corosync/corotypes.h>
324 typedef struct qb_ipc_request_header cs_ipc_header_request_t;
325 typedef struct qb_ipc_response_header cs_ipc_header_response_t;
326 # else
327 # include <corosync/corodefs.h>
328 # include <corosync/coroipcc.h>
329 # include <corosync/coroipc_types.h>
330 static inline int
331 qb_to_cs_error(int a)
332 {
333  return a;
334 }
335 
336 typedef coroipc_request_header_t cs_ipc_header_request_t;
337 typedef coroipc_response_header_t cs_ipc_header_response_t;
338 # endif
339 # else
340 typedef struct {
341  int size __attribute__ ((aligned(8)));
342  int id __attribute__ ((aligned(8)));
343 } __attribute__ ((aligned(8))) cs_ipc_header_request_t;
344 
345 typedef struct {
346  int size __attribute__ ((aligned(8)));
347  int id __attribute__ ((aligned(8)));
348  int error __attribute__ ((aligned(8)));
349 } __attribute__ ((aligned(8))) cs_ipc_header_response_t;
350 
351 # endif
352 
353 void
354 attrd_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb);
355 void
356 stonith_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb);
357 
358 qb_ipcs_service_t *
359 crmd_ipc_server_init(struct qb_ipcs_service_handlers *cb);
360 
361 void cib_ipc_servers_init(qb_ipcs_service_t **ipcs_ro,
362  qb_ipcs_service_t **ipcs_rw,
363  qb_ipcs_service_t **ipcs_shm,
364  struct qb_ipcs_service_handlers *ro_cb,
365  struct qb_ipcs_service_handlers *rw_cb);
366 
367 void cib_ipc_servers_destroy(qb_ipcs_service_t *ipcs_ro,
368  qb_ipcs_service_t *ipcs_rw,
369  qb_ipcs_service_t *ipcs_shm);
370 
371 static inline void *realloc_safe(void *ptr, size_t size)
372 {
373  void *ret = realloc(ptr, size);
374 
375  if(ret == NULL) {
376  abort();
377  }
378 
379  return ret;
380 }
381 
382 const char *crm_xml_add_last_written(xmlNode *xml_node);
383 void crm_xml_dump(xmlNode * data, int options, char **buffer, int *offset, int *max, int depth);
384 void crm_buffer_add_char(char **buffer, int *offset, int *max, char c);
385 
386 gboolean crm_digest_verify(xmlNode *input, const char *expected);
387 
388 /* cross-platform compatibility functions */
389 char *crm_compat_realpath(const char *path);
390 
391 /* IPC Proxy Backend Shared Functions */
392 typedef struct remote_proxy_s {
393  char *node_name;
394  char *session_id;
395 
396  gboolean is_local;
397 
401 
403 void remote_proxy_notify_destroy(lrmd_t *lrmd, const char *session_id);
404 void remote_proxy_relay_event(lrmd_t *lrmd, const char *session_id, xmlNode *msg);
405 void remote_proxy_relay_response(lrmd_t *lrmd, const char *session_id, xmlNode *msg, int msg_id);
406 void remote_proxy_end_session(const char *session);
407 void remote_proxy_free(gpointer data);
408 
409 #endif /* CRM_INTERNAL__H */
gboolean daemon_option_enabled(const char *daemon, const char *option)
Definition: logging.c:165
char * generate_transition_magic(const char *transition_key, int op_status, int op_rc)
Definition: utils.c:922
void remote_proxy_relay_response(lrmd_t *lrmd, const char *session_id, xmlNode *msg, int msg_id)
Definition: proxy_common.c:62
char * crm_compat_realpath(const char *path)
Definition: compat.c:40
void * find_library_function(void **handle, const char *lib, const char *fn, int fatal)
void remote_proxy_end_session(const char *session)
Definition: proxy_common.c:75
void remote_proxy_notify_destroy(lrmd_t *lrmd, const char *session_id)
Definition: proxy_common.c:38
int crm_pid_active(long pid, const char *daemon)
Definition: utils.c:1179
int crm_remote_ready(crm_remote_t *remote, int total_timeout)
Definition: remote.c:447
gboolean crm_remote_recv(crm_remote_t *remote, int total_timeout, int *disconnected)
Definition: remote.c:599
void crm_buffer_add_char(char **buffer, int *offset, int *max, char c)
Definition: xml.c:3961
struct pe_cluster_option_s pe_cluster_option
uint32_t size
Definition: internal.h:52
uint32_t last_request_id
Definition: crm_internal.h:400
internal procfs utilities
void crm_make_daemon(const char *name, gboolean daemonize, const char *pidfile)
Definition: utils.c:1365
char * generate_transition_magic_v202(const char *transition_key, int op_status)
Definition: utils.c:905
struct mainloop_io_s mainloop_io_t
Definition: mainloop.h:35
int crm_help(char cmd, int exit_code)
Definition: utils.c:1625
void stonith_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb)
Definition: utils.c:1745
Local Resource Manager.
char * add_list_element(char *list, const char *value)
Definition: utils.c:2297
gboolean crm_digest_verify(xmlNode *input, const char *expected)
Return whether calculated digest of XML tree matches expected digest.
Definition: digest.c:221
AIS_Host host
Definition: internal.h:52
void g_hash_destroy_str(gpointer data)
Definition: utils.c:578
char * generate_notify_key(const char *rsc_id, const char *notify_type, const char *op_type)
Definition: utils.c:883
void remote_proxy_relay_event(lrmd_t *lrmd, const char *session_id, xmlNode *msg)
Definition: proxy_common.c:49
uint32_t pid
Definition: internal.h:49
void crm_set_options(const char *short_options, const char *usage, struct crm_option *long_options, const char *app_desc)
Definition: utils.c:1533
gboolean check_number(const char *value)
Definition: utils.c:135
void filter_action_parameters(xmlNode *param_set, const char *version)
Definition: utils.c:1048
const char * name
Definition: crm_internal.h:103
internal I/O utilities
int crm_remote_tcp_connect(const char *host, int port)
Definition: remote.c:919
int crm_read_pidfile(const char *filename)
Definition: utils.c:1260
void * convert_const_pointer(const void *ptr)
Definition: utils.c:2316
Wrappers for and extensions to libqb logging.
char version[256]
Definition: plugin.c:84
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:1700
const char * type
Definition: crm_internal.h:105
char * generate_transition_key(int action, int transition_id, int target_rc, const char *node)
Definition: utils.c:965
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:346
op_status
Definition: services.h:120
int daemon(int nochdir, int noclose)
const char * default_value
Definition: crm_internal.h:107
void determine_request_user(const char *user, xmlNode *request, const char *field)
int crm_initiate_client_tls_handshake(crm_remote_t *remote, int timeout_ms)
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:61
int crm_pidfile_inuse(const char *filename, long mypid, const char *daemon)
Definition: utils.c:1288
xmlNode * crm_remote_parse_buffer(crm_remote_t *remote)
Definition: remote.c:377
gboolean check_quorum(const char *value)
Definition: utils.c:156
#define crm_trace(fmt, args...)
Definition: logging.h:254
const char * values
Definition: crm_internal.h:106
enum crm_proc_flag __attribute__
void pcmk_panic(const char *origin)
Definition: watchdog.c:184
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:1717
const char * desc
Definition: crm_internal.h:91
gboolean check_time(const char *value)
Definition: utils.c:106
struct qb_ipc_response_header cs_ipc_header_response_t
Definition: crm_internal.h:325
#define CRM_DAEMON_USER
Definition: config.h:47
gboolean check_boolean(const char *value)
Definition: utils.c:124
int crm_remote_tcp_connect_async(const char *host, int port, int timeout, int *timer_id, void *userdata, void(*callback)(void *userdata, int sock))
Definition: remote.c:830
const char * description_long
Definition: crm_internal.h:112
int crm_get_option_long(int argc, char **argv, int *index, const char **longname)
Definition: utils.c:1580
int * flag
Definition: crm_internal.h:86
crm_ipc_t * ipc
Definition: crm_internal.h:398
struct remote_proxy_s remote_proxy_t
const char * daemon_option(const char *option)
Definition: logging.c:117
void attrd_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb)
Definition: utils.c:1733
qb_ipcs_service_t * crmd_ipc_server_init(struct qb_ipcs_service_handlers *cb)
Definition: utils.c:1727
const char * cluster_option(GHashTable *options, gboolean(*validate)(const char *), const char *name, const char *old_name, const char *def_value)
Definition: utils.c:280
const char * description_short
Definition: crm_internal.h:111
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
Definition: utils.c:2371
gboolean check_script(const char *value)
Definition: utils.c:174
void remote_proxy_free(gpointer data)
Definition: proxy_common.c:90
void set_daemon_option(const char *option, const char *value)
Definition: logging.c:141
gboolean check_utilization(const char *value)
Definition: utils.c:201
int node_score_infinity
Definition: utils.c:70
mainloop_io_t * source
Definition: crm_internal.h:399
int node_score_red
Definition: utils.c:67
xmlNode * create_operation_update(xmlNode *parent, lrmd_event_data_t *event, const char *caller_version, int target_rc, const char *node, const char *origin, int level)
Definition: utils.c:1949
#define uint32_t
Definition: stdint.in.h:158
char data[0]
Definition: internal.h:58
void * create_psk_tls_session(int csock, int type, void *credentials)
void verify_all_options(GHashTable *options, pe_cluster_option *option_list, int len)
Definition: utils.c:381
Definition: lrmd.h:439
void strip_text_nodes(xmlNode *xml)
Definition: xml.c:3112
char * generate_hash_key(const char *crm_msg_reference, const char *sys)
Definition: utils.c:412
const char * crm_acl_get_set_user(xmlNode *request, const char *field, const char *peer_user)
pid_t pcmk_locate_sbd(void)
Definition: watchdog.c:214
gboolean check_timer(const char *value)
Definition: utils.c:115
int crm_get_option(int argc, char **argv, int *index)
Definition: utils.c:1574
char * crm_concat(const char *prefix, const char *suffix, char join)
Definition: utils.c:394
char * generate_op_key(const char *rsc_id, const char *op_type, int interval)
Definition: utils.c:794
void * crm_create_anon_tls_session(int sock, int type, void *credentials)
int node_score_green
Definition: utils.c:68
const char * get_cluster_pref(GHashTable *options, pe_cluster_option *option_list, int len, const char *name)
Definition: utils.c:326
gboolean is_local
Definition: crm_internal.h:396
const char * alt_name
Definition: crm_internal.h:104
int crm_remote_send(crm_remote_t *remote, xmlNode *msg)
Definition: remote.c:329
int node_score_yellow
Definition: utils.c:69
long long crm_int_helper(const char *text, char **end_text)
Definition: utils.c:588
const char * crm_xml_add_last_written(xmlNode *xml_node)
Definition: xml.c:3213
char * uid2username(uid_t uid)
const char * name
Definition: crm_internal.h:79
void sysrq_init(void)
Definition: watchdog.c:49
enum crm_ais_msg_types type
Definition: internal.h:51
struct qb_ipc_request_header cs_ipc_header_request_t
Definition: crm_internal.h:324
void crm_xml_dump(xmlNode *data, int options, char **buffer, int *offset, int *max, int depth)
Definition: xml.c:3858