21 #include <sys/param.h>
24 #include <sys/types.h>
38 #define PCMK_IPC_VERSION 1
41 #define PCMK_IPC_DEFAULT_QUEUE_MAX 500
43 struct crm_ipc_response_header {
44 struct qb_ipc_response_header qb;
51 static int hdr_offset = 0;
52 static unsigned int ipc_buffer_max = 0;
53 static unsigned int pick_ipc_buffer(
unsigned int max);
58 if (hdr_offset == 0) {
59 hdr_offset =
sizeof(
struct crm_ipc_response_header);
61 if (ipc_buffer_max == 0) {
62 ipc_buffer_max = pick_ipc_buffer(0);
69 return pick_ipc_buffer(0);
73 generateReference(
const char *custom1,
const char *custom2)
75 static uint ref_counter = 0;
76 const char *local_cust1 = custom1;
77 const char *local_cust2 = custom2;
78 int reference_len = 4;
79 char *since_epoch = NULL;
84 if (local_cust1 == NULL) {
85 local_cust1 =
"_empty_";
87 reference_len += strlen(local_cust1);
89 if (local_cust2 == NULL) {
90 local_cust2 =
"_empty_";
92 reference_len += strlen(local_cust2);
94 since_epoch = calloc(1, reference_len);
96 if (since_epoch != NULL) {
97 sprintf(since_epoch,
"%s-%s-%lu-%u",
98 local_cust1, local_cust2, (
unsigned long)time(NULL), ref_counter++);
106 const char *host_to,
const char *sys_to,
107 const char *sys_from,
const char *uuid_from,
const char *origin)
109 char *true_from = NULL;
110 xmlNode *request = NULL;
111 char *reference = generateReference(task, sys_from);
113 if (uuid_from != NULL) {
115 }
else if (sys_from != NULL) {
116 true_from = strdup(sys_from);
118 crm_err(
"No sys from specified");
133 if (host_to != NULL && strlen(host_to) > 0) {
137 if (msg_data != NULL) {
150 create_reply_adv(xmlNode * original_request, xmlNode * xml_response_data,
const char *origin)
152 xmlNode *reply = NULL;
162 crm_err(
"Cannot create new_message, no message type in original message");
167 crm_err(
"Cannot create new_message, original message was not a request");
173 crm_err(
"Cannot create new_message, malloc failed");
189 if (host_from != NULL && strlen(host_from) > 0) {
193 if (xml_response_data != NULL) {
209 if (client_connections) {
210 return g_hash_table_lookup(client_connections, c);
224 if (client_connections &&
id) {
225 g_hash_table_iter_init(&iter, client_connections);
226 while (g_hash_table_iter_next(&iter, &key, (gpointer *) & client)) {
227 if (strcmp(client->
id,
id) == 0) {
233 crm_trace(
"No client found with id=%s",
id);
242 }
else if (c->
name == NULL && c->
id == NULL) {
244 }
else if (c->
name == NULL) {
254 if (client_connections == NULL) {
256 client_connections = g_hash_table_new(g_direct_hash, g_direct_equal);
263 if (client_connections != NULL) {
264 int active = g_hash_table_size(client_connections);
267 crm_err(
"Exiting with %d active connections", active);
269 g_hash_table_destroy(client_connections); client_connections = NULL;
276 qb_ipcs_connection_t *c = NULL;
278 if (service == NULL) {
282 c = qb_ipcs_connection_first_get(service);
285 qb_ipcs_connection_t *last = c;
287 c = qb_ipcs_connection_next_get(service, last);
291 qb_ipcs_disconnect(last);
292 qb_ipcs_connection_unref(last);
310 g_hash_table_insert(client_connections, (key? key : client->
id), client);
317 static gid_t uid_cluster = 0;
318 static gid_t gid_cluster = 0;
327 if (uid_cluster == 0) {
329 static bool have_error = FALSE;
330 if(have_error == FALSE) {
331 crm_warn(
"Could not find user and group IDs for user %s",
338 if (uid_client != 0) {
339 crm_trace(
"Giving access to group %u", gid_cluster);
341 qb_ipcs_connection_auth_set(c, -1, gid_cluster, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
352 if ((uid_client == 0) || (uid_client == uid_cluster)) {
357 crm_debug(
"Connecting %p for uid=%d gid=%d pid=%u id=%s", c, uid_client, gid_client, client->
pid, client->
id);
372 if (client_connections) {
374 crm_trace(
"Destroying %p/%p (%d remaining)",
375 c, c->
ipcs, crm_hash_table_size(client_connections) - 1);
376 g_hash_table_remove(client_connections, c->
ipcs);
379 crm_trace(
"Destroying remote connection %p (%d remaining)",
380 c, crm_hash_table_size(client_connections) - 1);
381 g_hash_table_remove(client_connections, c->
id);
394 free(event[0].iov_base);
395 free(event[1].iov_base);
426 if ((errno == 0) && (qmax_int > 0)) {
437 struct qb_ipcs_connection_stats stats;
439 stats.client_pid = 0;
440 qb_ipcs_connection_stats_get(c, &stats, 0);
441 return stats.client_pid;
448 char *uncompressed = NULL;
449 char *text = ((
char *)data) +
sizeof(
struct crm_ipc_response_header);
450 struct crm_ipc_response_header *header =
data;
453 *
id = ((
struct qb_ipc_response_header *)data)->id;
456 *flags = header->flags;
467 crm_err(
"Filtering incompatible v%d IPC message, we only support versions <= %d",
472 if (header->size_compressed) {
474 unsigned int size_u = 1 + header->size_uncompressed;
475 uncompressed = calloc(1, size_u);
477 crm_trace(
"Decompressing message data %u bytes into %u bytes",
478 header->size_compressed, size_u);
480 rc = BZ2_bzBuffToBuffDecompress(uncompressed, &size_u, text, header->size_compressed, 1, 0);
490 CRM_ASSERT(text[header->size_uncompressed - 1] == 0);
502 crm_ipcs_flush_events_cb(gpointer
data)
519 delay_next_flush(
crm_client_t *c,
unsigned int queue_len)
522 guint delay = (queue_len < 40)? (1000 + 100 * queue_len) : 5000;
524 c->
event_timer = g_timeout_add(delay, crm_ipcs_flush_events_cb, c);
531 unsigned int sent = 0;
532 unsigned int queue_len = 0;
545 struct crm_ipc_response_header *header = NULL;
548 rc = qb_ipcs_event_sendv(c->
ipcs, event, 2);
554 header =
event[0].iov_base;
555 if (header->size_compressed) {
556 crm_trace(
"Event %d to %p[%d] (%lld compressed bytes) sent",
557 header->qb.id, c->
ipcs, c->
pid, (
long long) rc);
559 crm_trace(
"Event %d to %p[%d] (%lld bytes) sent: %.120s",
560 header->qb.id, c->
ipcs, c->
pid, (
long long) rc,
561 (
char *) (event[1].iov_base));
565 free(event[0].iov_base);
566 free(event[1].iov_base);
571 if (sent > 0 || queue_len) {
572 crm_trace(
"Sent %d events (%d remaining) for %p[%d]: %s (%lld)",
573 sent, queue_len, c->
ipcs, c->
pid,
584 if ((c->
queue_backlog <= 1) || (queue_len < c->queue_backlog)) {
586 crm_warn(
"Client with process ID %u has a backlog of %u messages "
589 crm_err(
"Evicting client with process ID %u due to backlog of %u messages "
592 qb_ipcs_disconnect(c->
ipcs);
598 delay_next_flush(c, queue_len);
611 static unsigned int biggest = 0;
613 unsigned int total = 0;
614 char *compressed = NULL;
616 struct crm_ipc_response_header *header = calloc(1,
sizeof(
struct crm_ipc_response_header));
622 if (max_send_size == 0) {
623 max_send_size = ipc_buffer_max;
629 iov = calloc(2,
sizeof(
struct iovec));
632 iov[0].iov_len = hdr_offset;
633 iov[0].iov_base = header;
636 header->size_uncompressed = 1 + strlen(buffer);
637 total = iov[0].iov_len + header->size_uncompressed;
639 if (total < max_send_size) {
640 iov[1].iov_base = buffer;
641 iov[1].iov_len = header->size_uncompressed;
644 unsigned int new_size = 0;
647 (buffer, header->size_uncompressed, max_send_size, &compressed, &new_size)) {
650 header->size_compressed = new_size;
652 iov[1].iov_len = header->size_compressed;
653 iov[1].iov_base = compressed;
657 biggest = QB_MAX(header->size_compressed, biggest);
660 ssize_t rc = -EMSGSIZE;
663 biggest = QB_MAX(header->size_uncompressed, biggest);
666 (
"Could not compress the message (%u bytes) into less than the configured ipc limit (%u bytes). "
667 "Set PCMK_ipc_buffer to a higher value (%u bytes suggested)",
668 header->size_uncompressed, max_send_size, 4 * biggest);
679 header->qb.size = iov[0].iov_len + iov[1].iov_len;
680 header->qb.id = (
int32_t)request;
684 return header->qb.size;
692 struct crm_ipc_response_header *header = iov[0].iov_base;
704 header->flags |=
flags;
706 header->qb.id =
id++;
713 struct iovec *iov_copy = calloc(2,
sizeof(
struct iovec));
716 iov_copy[0].iov_len = iov[0].iov_len;
717 iov_copy[0].iov_base = malloc(iov[0].iov_len);
718 memcpy(iov_copy[0].iov_base, iov[0].iov_base, iov[0].iov_len);
720 iov_copy[1].iov_len = iov[1].iov_len;
721 iov_copy[1].iov_base = malloc(iov[1].iov_len);
722 memcpy(iov_copy[1].iov_base, iov[1].iov_base, iov[1].iov_len);
730 rc = qb_ipcs_response_sendv(c->
ipcs, iov, 2);
731 if (rc < header->qb.size) {
732 crm_notice(
"Response %d to %p[%d] (%u bytes) failed: %s (%d)",
736 crm_trace(
"Response %d sent, %lld bytes to %p[%d]",
737 header->qb.id, (
long long) rc, c->
ipcs, c->
pid);
741 free(iov[0].iov_base);
742 free(iov[1].iov_base);
747 if (flags & crm_ipc_server_event) {
753 if (rc == -EPIPE || rc == -ENOTCONN) {
764 struct iovec *iov = NULL;
768 return -EDESTADDRREQ;
778 crm_notice(
"Message to %p[%d] failed: %s (%d)",
803 #define MIN_MSG_SIZE 12336
804 #define MAX_MSG_SIZE 128*1024
810 unsigned int max_buf_size;
812 unsigned int buf_size;
819 qb_ipcc_connection_t *ipc;
824 pick_ipc_buffer(
unsigned int max)
826 static unsigned int global_max = 0;
828 if (global_max == 0) {
829 const char *env = getenv(
"PCMK_ipc_buffer");
841 return QB_MAX(max, global_max);
851 client->name = strdup(name);
852 client->buf_size = pick_ipc_buffer(max_size);
853 client->buffer = malloc(client->buf_size);
856 client->max_buf_size = client->buf_size;
859 client->pfd.events = POLLIN;
860 client->pfd.revents = 0;
875 client->need_reply = FALSE;
876 client->ipc = qb_ipcc_connect(client->name, client->buf_size);
878 if (client->ipc == NULL) {
884 if (client->pfd.fd < 0) {
885 crm_debug(
"Could not obtain file descriptor for %s connection: %s (%d)", client->name,
pcmk_strerror(errno), errno);
889 qb_ipcc_context_set(client->ipc, client);
891 #ifdef HAVE_IPCS_GET_BUFFER_SIZE
892 client->max_buf_size = qb_ipcc_get_buffer_size(client->ipc);
893 if (client->max_buf_size > client->buf_size) {
894 free(client->buffer);
895 client->buffer = calloc(1, client->max_buf_size);
896 client->buf_size = client->max_buf_size;
907 crm_trace(
"Disconnecting %s IPC connection %p (%p)", client->name, client, client->ipc);
910 qb_ipcc_connection_t *ipc = client->ipc;
913 qb_ipcc_disconnect(ipc);
922 if (client->ipc && qb_ipcc_is_connected(client->ipc)) {
923 crm_notice(
"Destroying an active IPC connection to %s", client->name);
934 crm_trace(
"Destroying IPC connection to %s: %p", client->name, client);
935 free(client->buffer);
946 if (client && client->ipc && (qb_ipcc_fd_get(client->ipc, &fd) == 0)) {
950 crm_perror(LOG_ERR,
"Could not obtain file IPC descriptor for %s",
951 (client? client->name :
"unspecified client"));
960 if (client == NULL) {
964 }
else if (client->ipc == NULL) {
968 }
else if (client->pfd.fd < 0) {
973 rc = qb_ipcc_is_connected(client->ipc);
975 client->pfd.fd = -EINVAL;
998 client->pfd.revents = 0;
999 rc = poll(&(client->pfd), 1, 0);
1000 return (rc < 0)? -errno : rc;
1006 struct crm_ipc_response_header *header = (
struct crm_ipc_response_header *)(
void*)client->buffer;
1008 if (header->size_compressed) {
1010 unsigned int size_u = 1 + header->size_uncompressed;
1012 unsigned int new_buf_size = QB_MAX((hdr_offset + size_u), client->max_buf_size);
1013 char *uncompressed = calloc(1, new_buf_size);
1015 crm_trace(
"Decompressing message data %u bytes into %u bytes",
1016 header->size_compressed, size_u);
1018 rc = BZ2_bzBuffToBuffDecompress(uncompressed + hdr_offset, &size_u,
1019 client->buffer + hdr_offset, header->size_compressed, 1, 0);
1035 CRM_ASSERT(size_u == header->size_uncompressed);
1037 memcpy(uncompressed, client->buffer, hdr_offset);
1038 header = (
struct crm_ipc_response_header *)(
void*)uncompressed;
1040 free(client->buffer);
1041 client->buf_size = new_buf_size;
1042 client->buffer = uncompressed;
1045 CRM_ASSERT(client->buffer[hdr_offset + header->size_uncompressed - 1] == 0);
1052 struct crm_ipc_response_header *header = NULL;
1060 client->buffer[0] = 0;
1061 client->msg_size = qb_ipcc_event_recv(client->ipc, client->buffer, client->buf_size - 1, 0);
1062 if (client->msg_size >= 0) {
1063 int rc = crm_ipc_decompress(client);
1069 header = (
struct crm_ipc_response_header *)(
void*)client->buffer;
1071 crm_err(
"Filtering incompatible v%d IPC message, we only support versions <= %d",
1076 crm_trace(
"Received %s event %d, size=%u, rc=%d, text: %.100s",
1077 client->name, header->qb.id, header->qb.size, client->msg_size,
1078 client->buffer + hdr_offset);
1085 crm_err(
"Connection to %s failed", client->name);
1090 return header->size_uncompressed;
1099 return client->buffer +
sizeof(
struct crm_ipc_response_header);
1105 struct crm_ipc_response_header *header = NULL;
1108 if (client->buffer == NULL) {
1112 header = (
struct crm_ipc_response_header *)(
void*)client->buffer;
1113 return header->flags;
1120 return client->name;
1124 internal_ipc_send_recv(
crm_ipc_t * client,
const void *iov)
1129 rc = qb_ipcc_sendv_recv(client->ipc, iov, 2, client->buffer, client->buf_size, -1);
1136 internal_ipc_send_request(
crm_ipc_t * client,
const void *iov,
int ms_timeout)
1139 time_t timeout = time(NULL) + 1 + (ms_timeout / 1000);
1142 rc = qb_ipcc_sendv(client->ipc, iov, 2);
1149 internal_ipc_get_reply(
crm_ipc_t * client,
int request_id,
int ms_timeout)
1151 time_t timeout = time(NULL) + 1 + (ms_timeout / 1000);
1157 crm_trace(
"client %s waiting on reply to msg id %d", client->name, request_id);
1160 rc = qb_ipcc_recv(client->ipc, client->buffer, client->buf_size, 1000);
1162 struct crm_ipc_response_header *hdr = NULL;
1164 int rc = crm_ipc_decompress(client);
1170 hdr = (
struct crm_ipc_response_header *)(
void*)client->buffer;
1171 if (hdr->qb.id == request_id) {
1174 }
else if (hdr->qb.id < request_id) {
1177 crm_err(
"Discarding old reply %d (need %d)", hdr->qb.id, request_id);
1183 crm_err(
"Discarding newer reply %d (need %d)", hdr->qb.id, request_id);
1188 crm_err(
"Server disconnected client %s while waiting for msg id %d", client->name,
1193 }
while (time(NULL) < timeout);
1205 static int factor = 8;
1206 struct crm_ipc_response_header *header;
1210 if (client == NULL) {
1216 crm_notice(
"Connection to %s closed", client->name);
1220 if (ms_timeout == 0) {
1224 if (client->need_reply) {
1225 crm_trace(
"Trying again to obtain pending reply from %s", client->name);
1226 rc = qb_ipcc_recv(client->ipc, client->buffer, client->buf_size, ms_timeout);
1228 crm_warn(
"Sending to %s (%p) is disabled until pending reply is received", client->name,
1233 crm_notice(
"Lost reply from %s (%p) finally arrived, sending re-enabled", client->name,
1235 client->need_reply = FALSE;
1246 header = iov[0].iov_base;
1247 header->flags |=
flags;
1254 if(header->size_compressed) {
1255 if(factor < 10 && (client->max_buf_size / 10) < (rc / factor)) {
1256 crm_notice(
"Compressed message exceeds %d0%% of the configured ipc limit (%u bytes), "
1257 "consider setting PCMK_ipc_buffer to %u or higher",
1258 factor, client->max_buf_size, 2 * client->max_buf_size);
1263 crm_trace(
"Sending from client: %s request id: %d bytes: %u timeout:%d msg...",
1264 client->name, header->qb.id, header->qb.size, ms_timeout);
1268 rc = internal_ipc_send_request(client, iov, ms_timeout);
1271 crm_trace(
"Failed to send from client %s request %d with %u bytes...",
1272 client->name, header->qb.id, header->qb.size);
1276 crm_trace(
"Message sent, not waiting for reply to %d from %s to %u bytes...",
1277 header->qb.id, client->name, header->qb.size);
1282 rc = internal_ipc_get_reply(client, header->qb.id, ms_timeout);
1291 client->need_reply = TRUE;
1295 rc = internal_ipc_send_recv(client, iov);
1299 struct crm_ipc_response_header *hdr = (
struct crm_ipc_response_header *)(
void*)client->buffer;
1301 crm_trace(
"Received response %d, size=%u, rc=%ld, text: %.200s", hdr->qb.id, hdr->qb.size,
1309 crm_trace(
"Response not received: rc=%ld, errno=%d", rc, errno);
1316 }
else if (rc == -ETIMEDOUT) {
1317 crm_warn(
"Request %d to %s (%p) failed: %s (%ld) after %dms",
1318 header->qb.id, client->name, client->ipc,
pcmk_strerror(rc), rc, ms_timeout);
1321 }
else if (rc <= 0) {
1322 crm_warn(
"Request %d to %s (%p) failed: %s (%ld)",
1323 header->qb.id, client->name, client->ipc,
pcmk_strerror(rc), rc);
1327 free(iov[1].iov_base);
1336 const char *client_name,
const char *major_version,
const char *minor_version)
1338 xmlNode *hello_node = NULL;
1339 xmlNode *hello = NULL;
1341 if (uuid == NULL || strlen(uuid) == 0
1342 || client_name == NULL || strlen(client_name) == 0
1343 || major_version == NULL || strlen(major_version) == 0
1344 || minor_version == NULL || strlen(minor_version) == 0) {
1345 crm_err(
"Missing fields, Hello message will not be valid.");
1350 crm_xml_add(hello_node,
"major_version", major_version);
1351 crm_xml_add(hello_node,
"minor_version", minor_version);
1352 crm_xml_add(hello_node,
"client_name", client_name);
void crm_client_destroy(crm_client_t *c)
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
#define crm_notice(fmt, args...)
char * crm_generate_uuid(void)
int crm_ipc_get_fd(crm_ipc_t *client)
crm_client_t * crm_client_alloc(void *key)
Allocate a new crm_client_t object and generate its ID.
const char * pcmk_strerror(int rc)
void crm_client_disconnect_all(qb_ipcs_service_t *s)
qb_ipcs_connection_t * ipcs
xmlNode * crm_ipcs_recv(crm_client_t *c, void *data, size_t size, uint32_t *id, uint32_t *flags)
long long crm_int_helper(const char *text, char **end_text)
xmlNode * create_reply_adv(xmlNode *request, xmlNode *xml_response_data, const char *origin)
int crm_parse_int(const char *text, const char *default_text)
struct crm_remote_s * remote
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
#define CRM_LOG_ASSERT(expr)
long crm_ipc_read(crm_ipc_t *client)
#define clear_bit(word, bit)
xmlNode * create_hello_message(const char *uuid, const char *client_name, const char *major_version, const char *minor_version)
uint32_t crm_ipc_buffer_flags(crm_ipc_t *client)
crm_client_t * crm_client_get(qb_ipcs_connection_t *c)
ssize_t crm_ipcs_flush_events(crm_client_t *c)
void crm_client_init(void)
xmlNode * string2xml(const char *input)
const char * crm_ipc_buffer(crm_ipc_t *client)
void crm_ipcs_send_ack(crm_client_t *c, uint32_t request, uint32_t flags, const char *tag, const char *function, int line)
#define crm_warn(fmt, args...)
#define set_bit(word, bit)
#define crm_debug(fmt, args...)
struct crm_ipc_s crm_ipc_t
#define crm_trace(fmt, args...)
xmlNode * create_xml_node(xmlNode *parent, const char *name)
const char * crm_element_value(xmlNode *data, const char *name)
ssize_t crm_ipcs_send(crm_client_t *c, uint32_t request, xmlNode *message, enum crm_ipc_flags flags)
unsigned int crm_ipc_default_buffer_size(void)
void crm_ipc_destroy(crm_ipc_t *client)
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
void free_xml(xmlNode *child)
unsigned int queue_backlog
bool crm_ipc_connected(crm_ipc_t *client)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
int crm_ipc_ready(crm_ipc_t *client)
Check whether an IPC connection is ready to be read.
#define PCMK_IPC_DEFAULT_QUEUE_MAX
const char * crm_ipc_name(crm_ipc_t *client)
#define crm_perror(level, fmt, args...)
Log a system error message.
#define crm_err(fmt, args...)
int crm_ipc_send(crm_ipc_t *client, xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
const char * bz2_strerror(int rc)
xmlNode * create_request_adv(const char *task, xmlNode *xml_data, const char *host_to, const char *sys_to, const char *sys_from, const char *uuid_from, const char *origin)
#define crm_log_xml_notice(xml, text)
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
crm_client_t * crm_client_get_by_id(const char *id)
char * dump_xml_unformatted(xmlNode *msg)
GHashTable * client_connections
#define XML_ATTR_RESPONSE
ssize_t crm_ipc_prepare(uint32_t request, xmlNode *message, struct iovec **result, uint32_t max_send_size)
void crm_client_cleanup(void)
bool crm_set_client_queue_max(crm_client_t *client, const char *qmax)
Raise IPC eviction threshold for a client, if allowed.
int crm_ipcs_client_pid(qb_ipcs_connection_t *c)
Wrappers for and extensions to libqb IPC.
char * generate_hash_key(const char *crm_msg_reference, const char *sys)
#define crm_log_xml_trace(xml, text)
const char * crm_client_name(crm_client_t *c)
crm_client_t * crm_client_new(qb_ipcs_connection_t *c, uid_t uid, gid_t gid)
void crm_ipc_close(crm_ipc_t *client)
#define create_request(task, xml_data, host_to, sys_to, sys_from, uuid_from)
ssize_t crm_ipcs_sendv(crm_client_t *c, struct iovec *iov, enum crm_ipc_flags flags)
char * uid2username(uid_t uid)
enum crm_ais_msg_types type