12 #if defined(US_AUTH_PEERCRED_UCRED) || defined(US_AUTH_PEERCRED_SOCKPEERCRED) 13 # ifdef US_AUTH_PEERCRED_UCRED 18 # include <sys/socket.h> 19 #elif defined(US_AUTH_GETPEERUCRED) 24 #include <sys/types.h> 58 (*api)->server = server;
73 (*api)->ipc_size_max = 512 * 1024;
92 (*api)->ipc_size_max = 5 * 1024 * 1024;
95 if ((*api)->cmds == NULL) {
102 (*api)->ipc_size_max);
103 if ((*api)->ipc == NULL) {
110 if ((*api)->cmds->new_data != NULL) {
111 if ((*api)->cmds->new_data(*api) !=
pcmk_rc_ok) {
124 if ((api != NULL) && (api->
cmds != NULL)) {
147 if ((api != NULL) && (api->
cb != NULL)) {
148 api->
cb(api, event_type, status, event_data, api->
user_data);
161 ipc_post_disconnect(gpointer user_data)
188 free_daemon_specific_data(api);
189 crm_trace(
"Freeing IPC API object after disconnect");
202 bool free_on_disconnect =
false;
209 if (api->
ipc != NULL) {
224 if (!free_on_disconnect) {
225 free_daemon_specific_data(api);
244 return for_log?
"Pacemaker" : NULL;
248 return for_log?
"attribute manager" : NULL ;
251 return for_log?
"CIB manager" : NULL ;
257 return for_log?
"executor" : NULL ;
260 return for_log?
"fencer" : NULL ;
266 return for_log?
"scheduler" : NULL ;
269 return for_log?
"Pacemaker" : NULL;
318 dispatch_ipc_data(
const char *buffer, ssize_t length, gpointer user_data)
325 if (buffer == NULL) {
326 crm_warn(
"Empty message received from %s IPC",
333 crm_warn(
"Malformed message received from %s IPC",
337 call_api_dispatch(api, msg);
364 struct pollfd pollfd = { 0, };
370 pollfd.events = POLLIN;
371 rc = poll(&pollfd, 1, timeout_ms);
374 }
else if (
rc == 0) {
411 .destroy = ipc_post_disconnect,
419 crm_debug(
"Connected to %s IPC (attached to main loop)",
438 crm_debug(
"Connected to %s IPC (without main loop)",
457 crm_err(
"Cannot connect to uninitialized API object");
461 if (api->
ipc == NULL) {
464 if (api->
ipc == NULL) {
465 crm_err(
"Failed to re-create IPC API");
476 switch (dispatch_type) {
478 rc = connect_with_main_loop(api);
483 rc = connect_without_main_loop(api);
513 if ((api == NULL) || (api->
ipc == NULL)) {
542 ipc_post_disconnect(api);
587 xmlNode *reply = NULL;
590 if ((api == NULL) || (api->
ipc == NULL) || (request == NULL)) {
597 && (api->
cmds != NULL)
608 }
else if (
rc == 0) {
614 call_api_dispatch(api, reply);
643 create_purge_node_request(
pcmk_ipc_api_t *api,
const char *node_name,
646 xmlNode *request = NULL;
695 xmlNode *request = NULL;
700 if ((node_name == NULL) && (nodeid == 0)) {
704 request = create_purge_node_request(api, node_name, nodeid);
705 if (request == NULL) {
711 crm_debug(
"%s peer cache purge of node %s[%lu]: rc=%d",
722 unsigned int max_buf_size;
723 unsigned int buf_size;
728 qb_ipcc_connection_t *ipc;
750 if (client == NULL) {
755 client->name = strdup(
name);
756 if (client->name == NULL) {
762 client->buffer = malloc(client->buf_size);
763 if (client->buffer == NULL) {
771 client->max_buf_size = client->buf_size;
774 client->pfd.events = POLLIN;
775 client->pfd.revents = 0;
794 pid_t found_pid = 0; uid_t found_uid = 0; gid_t found_gid = 0;
797 client->need_reply = FALSE;
798 client->ipc = qb_ipcc_connect(client->name, client->buf_size);
800 if (client->ipc == NULL) {
806 if (client->pfd.fd < 0) {
823 &found_pid, &found_uid,
825 crm_err(
"Daemon (IPC %s) is not authentic:" 826 " process %lld (uid: %lld, gid: %lld)",
828 (
long long) found_uid, (
long long) found_gid);
830 errno = ECONNABORTED;
835 crm_perror(LOG_ERR,
"Could not verify authenticity of daemon (IPC %s)",
842 qb_ipcc_context_set(client->ipc, client);
844 #ifdef HAVE_IPCS_GET_BUFFER_SIZE 845 client->max_buf_size = qb_ipcc_get_buffer_size(client->ipc);
846 if (client->max_buf_size > client->buf_size) {
847 free(client->buffer);
848 client->buffer = calloc(1, client->max_buf_size);
849 client->buf_size = client->max_buf_size;
861 qb_ipcc_connection_t *ipc = client->ipc;
864 qb_ipcc_disconnect(ipc);
873 if (client->ipc && qb_ipcc_is_connected(client->ipc)) {
874 crm_notice(
"Destroying an active IPC connection to %s", client->name);
885 crm_trace(
"Destroying IPC connection to %s: %p", client->name, client);
886 free(client->buffer);
897 if (client && client->ipc && (qb_ipcc_fd_get(client->ipc, &fd) == 0)) {
901 crm_perror(LOG_ERR,
"Could not obtain file IPC descriptor for %s",
902 (client? client->name :
"unspecified client"));
911 if (client == NULL) {
915 }
else if (client->ipc == NULL) {
919 }
else if (client->pfd.fd < 0) {
924 rc = qb_ipcc_is_connected(client->ipc);
926 client->pfd.fd = -EINVAL;
949 client->pfd.revents = 0;
950 rc = poll(&(client->pfd), 1, 0);
951 return (
rc < 0)? -errno :
rc;
964 unsigned int new_buf_size = QB_MAX((
sizeof(
pcmk__ipc_header_t) + size_u), client->max_buf_size);
965 char *uncompressed = calloc(1, new_buf_size);
967 crm_trace(
"Decompressing message data %u bytes into %u bytes",
993 free(client->buffer);
994 client->buf_size = new_buf_size;
995 client->buffer = uncompressed;
1011 client->buffer[0] = 0;
1012 client->msg_size = qb_ipcc_event_recv(client->ipc, client->buffer,
1013 client->buf_size, 0);
1014 if (client->msg_size >= 0) {
1015 int rc = crm_ipc_decompress(client);
1026 crm_trace(
"Received %s event %d, size=%u, rc=%d, text: %.100s",
1027 client->name, header->
qb.id, header->
qb.size, client->msg_size,
1035 crm_err(
"Connection to %s failed", client->name);
1058 if (client->buffer == NULL) {
1063 return header->
flags;
1070 return client->name;
1075 internal_ipc_get_reply(
crm_ipc_t *client,
int request_id,
int ms_timeout,
1078 time_t
timeout = time(NULL) + 1 + (ms_timeout / 1000);
1082 crm_trace(
"client %s waiting on reply to msg id %d", client->name, request_id);
1085 *bytes = qb_ipcc_recv(client->ipc, client->buffer, client->buf_size, 1000);
1089 rc = crm_ipc_decompress(client);
1095 if (hdr->
qb.id == request_id) {
1098 }
else if (hdr->
qb.id < request_id) {
1101 crm_err(
"Discarding old reply %d (need %d)", hdr->
qb.id, request_id);
1107 crm_err(
"Discarding newer reply %d (need %d)", hdr->
qb.id, request_id);
1112 crm_err(
"Server disconnected client %s while waiting for msg id %d", client->name,
1117 }
while (time(NULL) <
timeout);
1146 static uint32_t
id = 0;
1147 static int factor = 8;
1150 if (client == NULL) {
1151 crm_notice(
"Can't send IPC request without connection (bug?): %.100s",
1157 crm_notice(
"Can't send IPC request to %s: Connection closed",
1162 if (ms_timeout == 0) {
1166 if (client->need_reply) {
1167 qb_rc = qb_ipcc_recv(client->ipc, client->buffer, client->buf_size, ms_timeout);
1169 crm_warn(
"Sending IPC to %s disabled until pending reply received",
1174 crm_notice(
"Sending IPC to %s re-enabled after pending reply received",
1176 client->need_reply = FALSE;
1184 crm_warn(
"Couldn't prepare IPC request to %s: %s " CRM_XS " rc=%d",
1189 header = iov[0].iov_base;
1198 if(factor < 10 && (client->max_buf_size / 10) < (bytes / factor)) {
1199 crm_notice(
"Compressed message exceeds %d0%% of configured IPC " 1200 "limit (%u bytes); consider setting PCMK_ipc_buffer to " 1202 factor, client->max_buf_size, 2 * client->max_buf_size);
1207 crm_trace(
"Sending %s IPC request %d of %u bytes using %dms timeout",
1208 client->name, header->
qb.id, header->
qb.size, ms_timeout);
1212 time_t
timeout = time(NULL) + 1 + (ms_timeout / 1000);
1222 qb_rc = qb_ipcc_sendv(client->ipc, iov, 2);
1223 }
while ((qb_rc == -EAGAIN) && (time(NULL) <
timeout));
1230 crm_trace(
"Not waiting for reply to %s IPC request %d",
1231 client->name, header->
qb.id);
1235 rc = internal_ipc_get_reply(client, header->
qb.id, ms_timeout, &bytes);
1243 client->need_reply = TRUE;
1250 qb_rc = qb_ipcc_sendv_recv(client->ipc, iov, 2, client->buffer,
1251 client->buf_size, -1);
1259 crm_trace(
"Received %d-byte reply %d to %s IPC %d: %.100s",
1260 rc, hdr->
qb.id, client->name, header->
qb.id,
1268 crm_trace(
"No reply to %s IPC %d: rc=%d",
1269 client->name, header->
qb.id,
rc);
1274 crm_notice(
"Couldn't send %s IPC request %d: Connection closed " 1275 CRM_XS " rc=%d", client->name, header->
qb.id,
rc);
1277 }
else if (
rc == -ETIMEDOUT) {
1278 crm_warn(
"%s IPC request %d failed: %s after %dms " CRM_XS " rc=%d",
1283 }
else if (
rc <= 0) {
1285 client->name, header->
qb.id,
1295 pid_t *gotpid, uid_t *gotuid, gid_t *gotgid) {
1297 pid_t found_pid = 0; uid_t found_uid = 0; gid_t found_gid = 0;
1298 #if defined(US_AUTH_PEERCRED_UCRED) 1300 socklen_t ucred_len =
sizeof(ucred);
1302 if (!getsockopt(sock, SOL_SOCKET, SO_PEERCRED,
1304 && ucred_len ==
sizeof(ucred)) {
1305 found_pid = ucred.pid; found_uid = ucred.uid; found_gid = ucred.gid;
1307 #elif defined(US_AUTH_PEERCRED_SOCKPEERCRED) 1308 struct sockpeercred sockpeercred;
1309 socklen_t sockpeercred_len =
sizeof(sockpeercred);
1311 if (!getsockopt(sock, SOL_SOCKET, SO_PEERCRED,
1312 &sockpeercred, &sockpeercred_len)
1313 && sockpeercred_len ==
sizeof(sockpeercred_len)) {
1314 found_pid = sockpeercred.pid;
1315 found_uid = sockpeercred.uid; found_gid = sockpeercred.gid;
1317 #elif defined(US_AUTH_GETPEEREID) 1318 if (!getpeereid(sock, &found_uid, &found_gid)) {
1321 #elif defined(US_AUTH_GETPEERUCRED) 1323 if (!getpeerucred(sock, &ucred)) {
1325 found_pid = ucred_getpid(ucred);
1326 found_uid = ucred_geteuid(ucred); found_gid = ucred_getegid(ucred);
1334 # error "No way to authenticate a Unix socket peer" 1338 if (gotpid != NULL) {
1339 *gotpid = found_pid;
1341 if (gotuid != NULL) {
1342 *gotuid = found_uid;
1344 if (gotgid != NULL) {
1345 *gotgid = found_gid;
1347 ret = (found_uid == 0 || found_uid == refuid || found_gid == refgid);
1357 gid_t refgid, pid_t *gotpid)
1359 static char last_asked_name[PATH_MAX / 2] =
"";
1364 pid_t found_pid = 0; uid_t found_uid = 0; gid_t found_gid = 0;
1365 qb_ipcc_connection_t *c;
1367 c = qb_ipcc_connect(
name, 0);
1374 qb_rc = qb_ipcc_fd_get(c, &fd);
1377 crm_err(
"Could not get fd from %s IPC: %s " CRM_XS " rc=%d",
1383 &found_uid, &found_gid);
1386 crm_err(
"Could not get peer credentials from %s IPC: %s " 1391 if (gotpid != NULL) {
1392 *gotpid = found_pid;
1396 crm_err(
"Daemon (IPC %s) effectively blocked with unauthorized" 1397 " process %lld (uid: %lld, gid: %lld)",
1399 (
long long) found_uid, (
long long) found_gid);
1405 if ((found_uid != refuid || found_gid != refgid)
1406 && strncmp(last_asked_name,
name,
sizeof(last_asked_name))) {
1407 if ((found_uid == 0) && (refuid != 0)) {
1408 crm_warn(
"Daemon (IPC %s) runs as root, whereas the expected" 1409 " credentials are %lld:%lld, hazard of violating" 1410 " the least privilege principle",
1411 name, (
long long) refuid, (
long long) refgid);
1413 crm_notice(
"Daemon (IPC %s) runs as %lld:%lld, whereas the" 1414 " expected credentials are %lld:%lld, which may" 1415 " mean a different set of privileges than expected",
1416 name, (
long long) found_uid, (
long long) found_gid,
1417 (
long long) refuid, (
long long) refgid);
1419 memccpy(last_asked_name,
name,
'\0',
sizeof(last_asked_name));
1424 qb_ipcc_disconnect(c);
#define CRM_CHECK(expr, failure_action)
int pcmk__ipc_prepare_iov(uint32_t request, xmlNode *message, uint32_t max_send_size, struct iovec **result, ssize_t *bytes)
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
#define crm_notice(fmt, args...)
const char * pcmk_strerror(int rc)
const char * bz2_strerror(int rc)
void(* dispatch)(pcmk_ipc_api_t *api, xmlNode *msg)
enum pcmk_ipc_dispatch dispatch_type
#define PCMK__ATTRD_CMD_PEER_REMOVE
int pcmk_rc2legacy(int rc)
int pcmk_poll_ipc(pcmk_ipc_api_t *api, int timeout_ms)
Check whether an IPC connection has data available (without main loop)
long crm_ipc_read(crm_ipc_t *client)
void pcmk_free_ipc_api(pcmk_ipc_api_t *api)
Free the contents of an IPC API object.
G_GNUC_INTERNAL pcmk__ipc_methods_t * pcmk__pacemakerd_api_methods(void)
const char * crm_ipc_buffer(crm_ipc_t *client)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
int pcmk_ipc_purge_node(pcmk_ipc_api_t *api, const char *node_name, uint32_t nodeid)
Ask a Pacemaker daemon to purge a node from its peer cache.
struct mainloop_io_s mainloop_io_t
#define PCMK__SPECIAL_PID_AS_0(p)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
#define PCMK__XA_ATTR_NODE_ID
int pcmk_new_ipc_api(pcmk_ipc_api_t **api, enum pcmk_ipc_server server)
Create a new object for using Pacemaker daemon IPC.
#define PCMK__SPECIAL_PID
enum crm_exit_e crm_exit_t
void crm_ipc_destroy(crm_ipc_t *client)
#define CRM_LOG_ASSERT(expr)
int pcmk__send_ipc_request(pcmk_ipc_api_t *api, xmlNode *request)
int pcmk_daemon_user(uid_t *uid, gid_t *gid)
Get user and group IDs of pacemaker daemon user.
struct pcmk__ipc_header_s pcmk__ipc_header_t
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
char * strerror(int errnum)
int crm_ipc_get_fd(crm_ipc_t *client)
xmlNode * string2xml(const char *input)
int pcmk__ipc_is_authentic_process_active(const char *name, uid_t refuid, gid_t refgid, pid_t *gotpid)
Caller will poll and dispatch IPC.
#define crm_warn(fmt, args...)
pcmk_ipc_server
Available IPC interfaces.
#define crm_debug(fmt, args...)
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
Create a new (legacy) object for using Pacemaker daemon IPC.
struct crm_ipc_s crm_ipc_t
void pcmk_register_ipc_callback(pcmk_ipc_api_t *api, pcmk_ipc_callback_t cb, void *user_data)
Register a callback for IPC API events.
const char * pcmk_ipc_name(pcmk_ipc_api_t *api, bool for_log)
Get the IPC name used with an IPC API connection.
#define crm_trace(fmt, args...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
void pcmk__call_ipc_callback(pcmk_ipc_api_t *api, enum pcmk_ipc_event event_type, crm_exit_t status, void *event_data)
uint32_t crm_ipc_buffer_flags(crm_ipc_t *client)
#define pcmk__clear_ipc_flags(ipc_flags, ipc_name, flags_to_clear)
void pcmk_disconnect_ipc(pcmk_ipc_api_t *api)
Disconnect an IPC API instance.
G_GNUC_INTERNAL bool pcmk__valid_ipc_header(const pcmk__ipc_header_t *header)
xmlNode * create_xml_node(xmlNode *parent, const char *name)
void crm_ipc_close(crm_ipc_t *client)
void mainloop_del_ipc_client(mainloop_io_t *client)
int crm_ipc_send(crm_ipc_t *client, xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
Send an IPC XML message.
int pcmk_legacy2rc(int legacy_rc)
void free_xml(xmlNode *child)
void pcmk_free_ipc_event(struct iovec *event)
Free an I/O vector created by pcmk__ipc_prepare_iov()
void(* pcmk_ipc_callback_t)(pcmk_ipc_api_t *api, enum pcmk_ipc_event event_type, crm_exit_t status, void *event_data, void *user_data)
Callback function type for Pacemaker daemon IPC APIs.
int(* post_connect)(pcmk_ipc_api_t *api)
G_GNUC_INTERNAL unsigned int pcmk__ipc_buffer_size(unsigned int max)
#define CRM_OP_RM_NODE_CACHE
pcmk_ipc_dispatch
How IPC replies should be dispatched.
pcmk_ipc_event
Possible event types that an IPC event callback can be called for.
bool crm_ipc_connected(crm_ipc_t *client)
#define PCMK__XA_ATTR_NODE_NAME
Attach IPC to GMainLoop for dispatch.
Termination of IPC connection.
mainloop_io_t * mainloop_io
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define pcmk__set_ipc_flags(ipc_flags, ipc_name, flags_to_set)
#define crm_err(fmt, args...)
Sending a command will wait for any reply.
void pcmk_dispatch_ipc(pcmk_ipc_api_t *api)
Dispatch available messages on an IPC connection (without main loop)
void crm_xml_set_id(xmlNode *xml, const char *format,...) __attribute__((__format__(__printf__
#define crm_log_xml_notice(xml, text)
int crm_ipc_ready(crm_ipc_t *client)
Check whether an IPC connection is ready to be read.
void(* free_data)(void *api_data)
bool(* reply_expected)(pcmk_ipc_api_t *api, xmlNode *request)
bool pcmk_ipc_is_connected(pcmk_ipc_api_t *api)
Check whether an IPC API connection is active.
IPC interface to Pacemaker daemons.
int pcmk_connect_ipc(pcmk_ipc_api_t *api, enum pcmk_ipc_dispatch dispatch_type)
Connect to a Pacemaker daemon via IPC.
#define crm_log_xml_trace(xml, text)
G_GNUC_INTERNAL pcmk__ipc_methods_t * pcmk__controld_api_methods(void)
pcmk__ipc_methods_t * cmds
void(* post_disconnect)(pcmk_ipc_api_t *api)
int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata, struct ipc_client_callbacks *callbacks, mainloop_io_t **source)
Connect to IPC and add it as a main loop source.
enum pcmk_ipc_server server
#define create_request(task, xml_data, host_to, sys_to, sys_from, uuid_from)
#define crm_info(fmt, args...)
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
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.
const char * crm_ipc_name(crm_ipc_t *client)