12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
20 #include <sys/utsname.h>
22 #include <qb/qbipcc.h>
23 #include <qb/qbutil.h>
25 #include <corosync/corodefs.h>
26 #include <corosync/corotypes.h>
27 #include <corosync/hdb.h>
28 #include <corosync/cpg.h>
36 static bool cpg_evicted = FALSE;
39 #define cs_repeat(counter, max, code) do { \
41 if(rc == CS_ERR_TRY_AGAIN || rc == CS_ERR_QUEUE_FULL) { \
43 crm_debug("Retrying operation after %ds", counter); \
48 } while(counter < max)
54 if (cluster->cpg_handle) {
56 cpg_leave(cluster->cpg_handle, &cluster->group);
57 cpg_finalize(cluster->cpg_handle);
58 cluster->cpg_handle = 0;
67 cs_error_t rc = CS_OK;
69 static uint32_t local_nodeid = 0;
70 cpg_handle_t local_handle = handle;
71 cpg_callbacks_t cb = { };
78 if(local_nodeid != 0) {
84 cs_repeat(retries, 5, rc = cpg_initialize(&local_handle, &cb));
86 crm_err(
"Could not connect to the CPG API: %s (%d)",
91 rc = cpg_fd_get(local_handle, &fd);
93 crm_err(
"Could not obtain the CPG API connection: %s (%d)",
100 &found_uid, &found_gid))) {
101 crm_err(
"CPG provider is not authentic:"
102 " process %lld (uid: %lld, gid: %lld)",
104 (
long long) found_uid, (
long long) found_gid);
107 crm_err(
"Could not verify authenticity of CPG provider: %s (%d)",
116 cs_repeat(retries, 5, rc = cpg_local_get(local_handle, &local_nodeid));
120 crm_err(
"Could not get local node id from the CPG API: %s (%d)", ais_error2text(rc), rc);
126 cpg_finalize(local_handle);
128 crm_debug(
"Local nodeid is %u", local_nodeid);
136 static ssize_t crm_cs_flush(gpointer
data);
139 crm_cs_flush_cb(gpointer
data)
146 #define CS_SEND_MAX 200
148 crm_cs_flush(gpointer
data)
153 static unsigned int last_sent = 0;
154 cpg_handle_t *handle = (cpg_handle_t *)data;
162 if ((queue_len % 1000) == 0 && queue_len > 1) {
163 crm_err(
"CPG queue has grown to %d", queue_len);
166 crm_warn(
"CPG queue has grown to %d", queue_len);
179 rc = cpg_mcast_joined(*handle, CPG_TYPE_AGREED, iov, 1);
188 (
unsigned long long) iov->iov_len);
197 crm_info(
"Sent %d CPG messages (%d remaining, last=%u): %s (%lld)",
198 sent, queue_len, last_sent, ais_error2text(rc),
201 crm_trace(
"Sent %d CPG messages (%d remaining, last=%u): %s (%lld)",
202 sent, queue_len, last_sent, ais_error2text(rc),
207 uint32_t delay_ms = 100;
210 delay_ms = QB_MIN(1000,
CS_SEND_MAX + (10 * queue_len));
221 static unsigned int queued = 0;
224 crm_trace(
"Queueing CPG message %u (%llu bytes)",
225 queued, (
unsigned long long) iov->iov_len);
232 pcmk_cpg_dispatch(gpointer user_data)
237 rc = cpg_dispatch(cluster->cpg_handle, CS_DISPATCH_ONE);
239 crm_err(
"Connection to the CPG API failed: %s (%d)", ais_error2text(rc), rc);
240 cluster->cpg_handle = 0;
243 }
else if(cpg_evicted) {
244 crm_err(
"Evicted from CPG membership");
252 uint32_t *kind,
const char **from)
263 crm_err(
"Nodeid mismatch from %d.%d: claimed nodeid=%u", nodeid, pid, msg->
sender.
id);
266 }
else if (msg->
host.
id != 0 && (local_nodeid != msg->
host.
id)) {
281 crm_err(
"Peer with nodeid=%u is unknown", nodeid);
283 }
else if (peer->
uname == NULL) {
284 crm_err(
"No uname for peer with nodeid=%u", nodeid);
287 crm_notice(
"Fixing uname for peer with nodeid=%u", nodeid);
295 crm_trace(
"Got new%s message (size=%d, %d, %d)",
300 *kind = msg->header.
id;
308 char *uncompressed = NULL;
309 unsigned int new_size = msg->
size + 1;
316 uncompressed = calloc(1, new_size);
317 rc = BZ2_bzBuffToBuffDecompress(uncompressed, &new_size, msg->
data, msg->
compressed_size, 1, 0);
335 char *pid_s = crm_getpid_s();
342 data = strdup(msg->
data);
352 crm_err(
"Invalid message (id=%d, dest=%s:%s, from=%s:%s.%d):"
353 " min=%d, total=%d, size=%d, bz2_size=%d",
363 static int cmp_member_list_nodeid(
const void *first,
366 const struct cpg_address *
const a = *((
const struct cpg_address **) first),
367 *
const b = *((
const struct cpg_address **) second);
368 if (a->nodeid < b->nodeid) {
370 }
else if (a->nodeid > b->nodeid) {
378 cpgreason2str(cpg_reason_t reason)
381 case CPG_REASON_JOIN:
return " via cpg_join";
382 case CPG_REASON_LEAVE:
return " via cpg_leave";
383 case CPG_REASON_NODEDOWN:
return " via cluster exit";
384 case CPG_REASON_NODEUP:
return " via cluster join";
385 case CPG_REASON_PROCDOWN:
return " for unknown reason";
391 static inline const char *
395 return "unknown node";
396 }
else if (peer->
uname == NULL) {
405 const struct cpg_name *groupName,
406 const struct cpg_address *member_list,
size_t member_list_entries,
407 const struct cpg_address *left_list,
size_t left_list_entries,
408 const struct cpg_address *joined_list,
size_t joined_list_entries)
411 gboolean found = FALSE;
412 static int counter = 0;
414 const struct cpg_address *key, **sorted;
416 sorted = malloc(member_list_entries *
sizeof(
const struct cpg_address *));
419 for (
size_t iter = 0; iter < member_list_entries; iter++) {
420 sorted[iter] = member_list + iter;
423 qsort(sorted, member_list_entries,
sizeof(
const struct cpg_address *),
424 cmp_member_list_nodeid);
426 for (i = 0; i < left_list_entries; i++) {
428 const struct cpg_address **rival = NULL;
447 rival = bsearch(&key, sorted, member_list_entries,
448 sizeof(
const struct cpg_address *),
449 cmp_member_list_nodeid);
453 crm_info(
"Group %s event %d: %s (node %u pid %u) left%s",
454 groupName->value, counter, peer_name(peer),
455 left_list[i].nodeid, left_list[i].
pid,
456 cpgreason2str(left_list[i].reason));
461 }
else if (left_list[i].nodeid == local_nodeid) {
462 crm_warn(
"Group %s event %d: duplicate local pid %u left%s",
463 groupName->value, counter,
464 left_list[i].pid, cpgreason2str(left_list[i].reason));
467 "%s (node %u) duplicate pid %u left%s (%u remains)",
468 groupName->value, counter, peer_name(peer),
469 left_list[i].nodeid, left_list[i].
pid,
470 cpgreason2str(left_list[i].reason), (*rival)->pid);
476 for (i = 0; i < joined_list_entries; i++) {
477 crm_info(
"Group %s event %d: node %u pid %u joined%s",
478 groupName->value, counter, joined_list[i].nodeid,
479 joined_list[i].pid, cpgreason2str(joined_list[i].reason));
482 for (i = 0; i < member_list_entries; i++) {
485 if (member_list[i].nodeid == local_nodeid
486 && member_list[i].
pid != getpid()) {
488 crm_warn(
"Group %s event %d: detected duplicate local pid %u",
489 groupName->value, counter, member_list[i].pid);
492 crm_info(
"Group %s event %d: %s (node %u pid %u) is member",
493 groupName->value, counter, peer_name(peer),
494 member_list[i].nodeid, member_list[i].
pid);
509 time_t now = time(NULL);
515 }
else if (now > (peer->
when_lost + 60)) {
517 crm_warn(
"Node %u is member of group %s but was believed offline",
518 member_list[i].nodeid, groupName->value);
523 if (local_nodeid == member_list[i].nodeid) {
529 crm_err(
"Local node was evicted from group %s", groupName->value);
544 cpg_handle_t handle = 0;
556 cpg_callbacks_t cpg_callbacks = {
557 .cpg_deliver_fn = cluster->cpg.cpg_deliver_fn,
558 .cpg_confchg_fn = cluster->cpg.cpg_confchg_fn,
564 cluster->group.length = 0;
565 cluster->group.value[0] = 0;
568 strncpy(cluster->group.value, message_name, 127);
569 cluster->group.value[127] = 0;
570 cluster->group.length = 1 + QB_MIN(127, strlen(cluster->group.value));
572 cs_repeat(retries, 30, rc = cpg_initialize(&handle, &cpg_callbacks));
574 crm_err(
"Could not connect to the CPG API: %s (%d)",
575 cs_strerror(rc), rc);
579 rc = cpg_fd_get(handle, &fd);
581 crm_err(
"Could not obtain the CPG API connection: %s (%d)",
582 cs_strerror(rc), rc);
588 &found_uid, &found_gid))) {
589 crm_err(
"CPG provider is not authentic:"
590 " process %lld (uid: %lld, gid: %lld)",
592 (
long long) found_uid, (
long long) found_gid);
596 crm_err(
"Could not verify authenticity of CPG provider: %s (%d)",
604 crm_err(
"Could not get local node id from the CPG API");
611 cs_repeat(retries, 30, rc = cpg_join(handle, &cluster->group));
613 crm_err(
"Could not join the CPG group '%s': %d", message_name, rc);
618 cluster->cpg_handle = handle;
623 cpg_finalize(handle);
648 static int msg_id = 0;
649 static int local_pid = 0;
650 static int local_name_len = 0;
651 static const char *local_name = NULL;
662 crm_err(
"Invalid message class: %d", msg_class);
668 if(local_name == NULL) {
671 if(local_name_len == 0 && local_name) {
672 local_name_len = strlen(local_name);
679 if (local_pid == 0) {
680 local_pid = getpid();
691 msg->header.
id = msg_class;
692 msg->header.error = CS_OK;
699 target = strdup(node->
uname);
708 target = strdup(
"all");
720 msg->
size = 1 + strlen(data);
724 msg = realloc_safe(msg, msg->header.
size);
728 char *compressed = NULL;
729 unsigned int new_size = 0;
730 char *uncompressed = strdup(data);
735 msg = realloc_safe(msg, msg->header.
size);
736 memcpy(msg->
data, compressed, new_size);
742 msg = realloc_safe(msg, msg->header.
size);
750 iov = calloc(1,
sizeof(
struct iovec));
752 iov->iov_len = msg->header.
size;
755 crm_trace(
"Queueing CPG message %u to %s (%llu bytes, %d bytes compressed payload): %.200s",
756 msg->
id, target, (
unsigned long long) iov->iov_len,
759 crm_trace(
"Queueing CPG message %u to %s (%llu bytes, %d bytes payload): %.200s",
760 msg->
id, target, (
unsigned long long) iov->iov_len,
801 int scan_rc = sscanf(text,
"%d", &type);
enum crm_ais_msg_types type
#define CRM_CHECK(expr, failure_action)
gboolean send_cpg_iov(struct iovec *iov)
gboolean(* pcmk_cpg_dispatch_fn)(int kind, const char *from, const char *data)
#define crm_notice(fmt, args...)
const char * bz2_strerror(int rc)
gboolean safe_str_neq(const char *a, const char *b)
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
const char * get_local_node_name(void)
void(* destroy)(gpointer)
#define PCMK__SPECIAL_PID_AS_0(p)
crm_node_t * crm_get_peer(unsigned int id, const char *uname)
int(* dispatch)(gpointer userdata)
char * strerror(int errnum)
gboolean send_cluster_text(enum crm_ais_msg_class msg_class, const char *data, gboolean local, crm_node_t *node, enum crm_ais_msg_types dest)
char * pcmk_message_common_cs(cpg_handle_t handle, uint32_t nodeid, uint32_t pid, void *content, uint32_t *kind, const char **from)
Wrappers for and extensions to glib mainloop.
void cluster_disconnect_cpg(crm_cluster_t *cluster)
#define crm_warn(fmt, args...)
enum crm_ais_msg_types text2msg_type(const char *text)
#define crm_debug(fmt, args...)
const char * pcmk_message_name(const char *name)
Get name to be used as identifier for cluster messages.
GListPtr cs_message_queue
#define crm_trace(fmt, args...)
crm_node_t * crm_update_peer_proc(const char *source, crm_node_t *peer, uint32_t flag, const char *status)
#define CRM_SYSTEM_PENGINE
gboolean check_message_sanity(const AIS_Message *msg, const char *data)
struct crm_ais_msg_s AIS_Message
cpg_handle_t pcmk_cpg_handle
#define ais_data_len(msg)
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
#define CRM_SYSTEM_STONITHD
crm_node_t * crm_update_peer_state(const char *source, crm_node_t *node, const char *state, int membership)
Update a node's state and membership information.
#define CRM_SYSTEM_TENGINE
uint32_t get_local_nodeid(cpg_handle_t handle)
#define crm_err(fmt, args...)
#define G_PRIORITY_MEDIUM
#define CRM_BZ2_THRESHOLD
char * dump_xml_unformatted(xmlNode *msg)
gboolean send_cluster_message_cs(xmlNode *msg, gboolean local, crm_node_t *node, enum crm_ais_msg_types dest)
Wrappers for and extensions to libqb IPC.
#define cs_repeat(counter, max, code)
#define safe_str_eq(a, b)
crm_node_t * crm_find_peer(unsigned int id, const char *uname)
void pcmk_cpg_membership(cpg_handle_t handle, const struct cpg_name *groupName, const struct cpg_address *member_list, size_t member_list_entries, const struct cpg_address *left_list, size_t left_list_entries, const struct cpg_address *joined_list, size_t joined_list_entries)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#define crm_info(fmt, args...)
gboolean cluster_connect_cpg(crm_cluster_t *cluster)
enum crm_ais_msg_types type
int crm_ipc_is_authentic_process(int sock, uid_t refuid, gid_t refgid, pid_t *gotpid, uid_t *gotuid, gid_t *gotgid)
Check the authenticity of the IPC socket peer process.