pacemaker  1.1.14-70404b0
Scalable High-Availability cluster resource manager
ipc.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-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 #include <crm_internal.h>
11 
12 #if defined(US_AUTH_PEERCRED_UCRED) || defined(US_AUTH_PEERCRED_SOCKPEERCRED)
13 # ifdef US_AUTH_PEERCRED_UCRED
14 # ifndef _GNU_SOURCE
15 # define _GNU_SOURCE
16 # endif
17 # endif
18 # include <sys/socket.h>
19 #elif defined(US_AUTH_GETPEERUCRED)
20 # include <ucred.h>
21 #endif
22 
23 #include <sys/param.h>
24 
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <grp.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <bzlib.h>
34 
35 #include <crm/crm.h> /* indirectly: pcmk_err_generic */
36 #include <crm/msg_xml.h>
37 #include <crm/common/ipc.h>
38 #include <crm/common/ipcs.h>
39 
40 #include <crm/common/ipc_internal.h> /* PCMK__SPECIAL_PID* */
41 
42 #define PCMK_IPC_VERSION 1
43 
44 struct crm_ipc_response_header {
45  struct qb_ipc_response_header qb;
46  uint32_t size_uncompressed;
47  uint32_t size_compressed;
49  uint8_t version; /* Protect against version changes for anyone that might bother to statically link us */
50 };
51 
52 static int hdr_offset = 0;
53 static unsigned int ipc_buffer_max = 0;
54 static unsigned int pick_ipc_buffer(unsigned int max);
55 
56 static inline void
57 crm_ipc_init(void)
58 {
59  if (hdr_offset == 0) {
60  hdr_offset = sizeof(struct crm_ipc_response_header);
61  }
62  if (ipc_buffer_max == 0) {
63  ipc_buffer_max = pick_ipc_buffer(0);
64  }
65 }
66 
67 unsigned int
69 {
70  return pick_ipc_buffer(0);
71 }
72 
73 static char *
74 generateReference(const char *custom1, const char *custom2)
75 {
76  static uint ref_counter = 0;
77  const char *local_cust1 = custom1;
78  const char *local_cust2 = custom2;
79  int reference_len = 4;
80  char *since_epoch = NULL;
81 
82  reference_len += 20; /* too big */
83  reference_len += 40; /* too big */
84 
85  if (local_cust1 == NULL) {
86  local_cust1 = "_empty_";
87  }
88  reference_len += strlen(local_cust1);
89 
90  if (local_cust2 == NULL) {
91  local_cust2 = "_empty_";
92  }
93  reference_len += strlen(local_cust2);
94 
95  since_epoch = calloc(1, reference_len);
96 
97  if (since_epoch != NULL) {
98  sprintf(since_epoch, "%s-%s-%lu-%u",
99  local_cust1, local_cust2, (unsigned long)time(NULL), ref_counter++);
100  }
101 
102  return since_epoch;
103 }
104 
105 xmlNode *
106 create_request_adv(const char *task, xmlNode * msg_data,
107  const char *host_to, const char *sys_to,
108  const char *sys_from, const char *uuid_from, const char *origin)
109 {
110  char *true_from = NULL;
111  xmlNode *request = NULL;
112  char *reference = generateReference(task, sys_from);
113 
114  if (uuid_from != NULL) {
115  true_from = generate_hash_key(sys_from, uuid_from);
116  } else if (sys_from != NULL) {
117  true_from = strdup(sys_from);
118  } else {
119  crm_err("No sys from specified");
120  }
121 
122  /* host_from will get set for us if necessary by CRMd when routed */
123  request = create_xml_node(NULL, __FUNCTION__);
124  crm_xml_add(request, F_CRM_ORIGIN, origin);
125  crm_xml_add(request, F_TYPE, T_CRM);
128  crm_xml_add(request, F_CRM_REFERENCE, reference);
129  crm_xml_add(request, F_CRM_TASK, task);
130  crm_xml_add(request, F_CRM_SYS_TO, sys_to);
131  crm_xml_add(request, F_CRM_SYS_FROM, true_from);
132 
133  /* HOSTTO will be ignored if it is to the DC anyway. */
134  if (host_to != NULL && strlen(host_to) > 0) {
135  crm_xml_add(request, F_CRM_HOST_TO, host_to);
136  }
137 
138  if (msg_data != NULL) {
139  add_message_xml(request, F_CRM_DATA, msg_data);
140  }
141  free(reference);
142  free(true_from);
143 
144  return request;
145 }
146 
147 /*
148  * This method adds a copy of xml_response_data
149  */
150 xmlNode *
151 create_reply_adv(xmlNode * original_request, xmlNode * xml_response_data, const char *origin)
152 {
153  xmlNode *reply = NULL;
154 
155  const char *host_from = crm_element_value(original_request, F_CRM_HOST_FROM);
156  const char *sys_from = crm_element_value(original_request, F_CRM_SYS_FROM);
157  const char *sys_to = crm_element_value(original_request, F_CRM_SYS_TO);
158  const char *type = crm_element_value(original_request, F_CRM_MSG_TYPE);
159  const char *operation = crm_element_value(original_request, F_CRM_TASK);
160  const char *crm_msg_reference = crm_element_value(original_request, F_CRM_REFERENCE);
161 
162  if (type == NULL) {
163  crm_err("Cannot create new_message, no message type in original message");
164  CRM_ASSERT(type != NULL);
165  return NULL;
166 #if 0
167  } else if (strcasecmp(XML_ATTR_REQUEST, type) != 0) {
168  crm_err("Cannot create new_message, original message was not a request");
169  return NULL;
170 #endif
171  }
172  reply = create_xml_node(NULL, __FUNCTION__);
173  if (reply == NULL) {
174  crm_err("Cannot create new_message, malloc failed");
175  return NULL;
176  }
177 
178  crm_xml_add(reply, F_CRM_ORIGIN, origin);
179  crm_xml_add(reply, F_TYPE, T_CRM);
182  crm_xml_add(reply, F_CRM_REFERENCE, crm_msg_reference);
183  crm_xml_add(reply, F_CRM_TASK, operation);
184 
185  /* since this is a reply, we reverse the from and to */
186  crm_xml_add(reply, F_CRM_SYS_TO, sys_from);
187  crm_xml_add(reply, F_CRM_SYS_FROM, sys_to);
188 
189  /* HOSTTO will be ignored if it is to the DC anyway. */
190  if (host_from != NULL && strlen(host_from) > 0) {
191  crm_xml_add(reply, F_CRM_HOST_TO, host_from);
192  }
193 
194  if (xml_response_data != NULL) {
195  add_message_xml(reply, F_CRM_DATA, xml_response_data);
196  }
197 
198  return reply;
199 }
200 
201 /* Libqb based IPC */
202 
203 /* Server... */
204 
205 GHashTable *client_connections = NULL;
206 
207 crm_client_t *
208 crm_client_get(qb_ipcs_connection_t * c)
209 {
210  if (client_connections) {
211  return g_hash_table_lookup(client_connections, c);
212  }
213 
214  crm_trace("No client found for %p", c);
215  return NULL;
216 }
217 
218 crm_client_t *
219 crm_client_get_by_id(const char *id)
220 {
221  gpointer key;
222  crm_client_t *client;
223  GHashTableIter iter;
224 
225  if (client_connections && id) {
226  g_hash_table_iter_init(&iter, client_connections);
227  while (g_hash_table_iter_next(&iter, &key, (gpointer *) & client)) {
228  if (strcmp(client->id, id) == 0) {
229  return client;
230  }
231  }
232  }
233 
234  crm_trace("No client found with id=%s", id);
235  return NULL;
236 }
237 
238 const char *
240 {
241  if (c == NULL) {
242  return "null";
243  } else if (c->name == NULL && c->id == NULL) {
244  return "unknown";
245  } else if (c->name == NULL) {
246  return c->id;
247  } else {
248  return c->name;
249  }
250 }
251 
252 void
254 {
255  if (client_connections == NULL) {
256  crm_trace("Creating client hash table");
257  client_connections = g_hash_table_new(g_direct_hash, g_direct_equal);
258  }
259 }
260 
261 void
263 {
264  if (client_connections != NULL) {
265  int active = g_hash_table_size(client_connections);
266 
267  if (active) {
268  crm_err("Exiting with %d active connections", active);
269  }
270  g_hash_table_destroy(client_connections); client_connections = NULL;
271  }
272 }
273 
274 void
275 crm_client_disconnect_all(qb_ipcs_service_t *service)
276 {
277  qb_ipcs_connection_t *c = qb_ipcs_connection_first_get(service);
278 
279  while (c != NULL) {
280  qb_ipcs_connection_t *last = c;
281 
282  c = qb_ipcs_connection_next_get(service, last);
283 
284  /* There really shouldn't be anyone connected at this point */
285  crm_notice("Disconnecting client %p, pid=%d...", last, crm_ipcs_client_pid(last));
286  qb_ipcs_disconnect(last);
287  qb_ipcs_connection_unref(last);
288  }
289 }
290 
291 crm_client_t *
292 crm_client_new(qb_ipcs_connection_t * c, uid_t uid_client, gid_t gid_client)
293 {
294  static gid_t gid_cluster = 0;
295 
296  crm_client_t *client = NULL;
297 
298  CRM_LOG_ASSERT(c);
299  if (c == NULL) {
300  return NULL;
301  }
302 
303  if (gid_cluster == 0) {
304  if(crm_user_lookup(CRM_DAEMON_USER, NULL, &gid_cluster) < 0) {
305  static bool have_error = FALSE;
306  if(have_error == FALSE) {
307  crm_warn("Could not find group for user %s", CRM_DAEMON_USER);
308  have_error = TRUE;
309  }
310  }
311  }
312 
313  if (uid_client != 0) {
314  crm_trace("Giving access to group %u", gid_cluster);
315  /* Passing -1 to chown(2) means don't change */
316  qb_ipcs_connection_auth_set(c, -1, gid_cluster, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
317  }
318 
319  crm_client_init();
320 
321  /* TODO: Do our own auth checking, return NULL if unauthorized */
322  client = calloc(1, sizeof(crm_client_t));
323 
324  client->ipcs = c;
325  client->kind = CRM_CLIENT_IPC;
326  client->pid = crm_ipcs_client_pid(c);
327 
328  client->id = crm_generate_uuid();
329 
330  crm_debug("Connecting %p for uid=%d gid=%d pid=%u id=%s", c, uid_client, gid_client, client->pid, client->id);
331 
332 #if ENABLE_ACL
333  client->user = uid2username(uid_client);
334 #endif
335 
336  g_hash_table_insert(client_connections, c, client);
337  return client;
338 }
339 
340 void
342 {
343  if (c == NULL) {
344  return;
345  }
346 
347  if (client_connections) {
348  if (c->ipcs) {
349  crm_trace("Destroying %p/%p (%d remaining)",
350  c, c->ipcs, crm_hash_table_size(client_connections) - 1);
351  g_hash_table_remove(client_connections, c->ipcs);
352 
353  } else {
354  crm_trace("Destroying remote connection %p (%d remaining)",
355  c, crm_hash_table_size(client_connections) - 1);
356  g_hash_table_remove(client_connections, c->id);
357  }
358  }
359 
360  if (c->event_timer) {
361  g_source_remove(c->event_timer);
362  }
363 
364  crm_debug("Destroying %d events", g_list_length(c->event_queue));
365  while (c->event_queue) {
366  struct iovec *event = c->event_queue->data;
367 
368  c->event_queue = g_list_remove(c->event_queue, event);
369  free(event[0].iov_base);
370  free(event[1].iov_base);
371  free(event);
372  }
373 
374  free(c->id);
375  free(c->name);
376  free(c->user);
377  if (c->remote) {
378  if (c->remote->auth_timeout) {
379  g_source_remove(c->remote->auth_timeout);
380  }
381  free(c->remote->buffer);
382  free(c->remote);
383  }
384  free(c);
385 }
386 
387 int
388 crm_ipcs_client_pid(qb_ipcs_connection_t * c)
389 {
390  struct qb_ipcs_connection_stats stats;
391 
392  stats.client_pid = 0;
393  qb_ipcs_connection_stats_get(c, &stats, 0);
394  return stats.client_pid;
395 }
396 
397 xmlNode *
399 {
400  xmlNode *xml = NULL;
401  char *uncompressed = NULL;
402  char *text = ((char *)data) + sizeof(struct crm_ipc_response_header);
403  struct crm_ipc_response_header *header = data;
404 
405  if (id) {
406  *id = ((struct qb_ipc_response_header *)data)->id;
407  }
408  if (flags) {
409  *flags = header->flags;
410  }
411 
412  if (is_set(header->flags, crm_ipc_proxied)) {
413  /* mark this client as being the endpoint of a proxy connection.
414  * Proxy connections responses are sent on the event channel to avoid
415  * blocking the proxy daemon (crmd) */
417  }
418 
419  if(header->version > PCMK_IPC_VERSION) {
420  crm_err("Filtering incompatible v%d IPC message, we only support versions <= %d",
421  header->version, PCMK_IPC_VERSION);
422  return NULL;
423  }
424 
425  if (header->size_compressed) {
426  int rc = 0;
427  unsigned int size_u = 1 + header->size_uncompressed;
428  uncompressed = calloc(1, size_u);
429 
430  crm_trace("Decompressing message data %u bytes into %u bytes",
431  header->size_compressed, size_u);
432 
433  rc = BZ2_bzBuffToBuffDecompress(uncompressed, &size_u, text, header->size_compressed, 1, 0);
434  text = uncompressed;
435 
436  if (rc != BZ_OK) {
437  crm_err("Decompression failed: %s (%d)", bz2_strerror(rc), rc);
438  free(uncompressed);
439  return NULL;
440  }
441  }
442 
443  CRM_ASSERT(text[header->size_uncompressed - 1] == 0);
444 
445  crm_trace("Received %.200s", text);
446  xml = string2xml(text);
447 
448  free(uncompressed);
449  return xml;
450 }
451 
453 
454 static gboolean
455 crm_ipcs_flush_events_cb(gpointer data)
456 {
457  crm_client_t *c = data;
458 
459  c->event_timer = 0;
461  return FALSE;
462 }
463 
464 ssize_t
466 {
467  int sent = 0;
468  ssize_t rc = 0;
469  int queue_len = 0;
470 
471  if (c == NULL) {
472  return pcmk_ok;
473 
474  } else if (c->event_timer) {
475  /* There is already a timer, wait until it goes off */
476  crm_trace("Timer active for %p - %d", c->ipcs, c->event_timer);
477  return pcmk_ok;
478  }
479 
480  queue_len = g_list_length(c->event_queue);
481  while (c->event_queue && sent < 100) {
482  struct crm_ipc_response_header *header = NULL;
483  struct iovec *event = c->event_queue->data;
484 
485  rc = qb_ipcs_event_sendv(c->ipcs, event, 2);
486  if (rc < 0) {
487  break;
488  }
489 
490  sent++;
491  header = event[0].iov_base;
492  if (header->size_compressed) {
493  crm_trace("Event %d to %p[%d] (%d compressed bytes) sent",
494  header->qb.id, c->ipcs, c->pid, rc);
495  } else {
496  crm_trace("Event %d to %p[%d] (%d bytes) sent: %.120s",
497  header->qb.id, c->ipcs, c->pid, rc, event[1].iov_base);
498  }
499 
500  c->event_queue = g_list_remove(c->event_queue, event);
501  free(event[0].iov_base);
502  free(event[1].iov_base);
503  free(event);
504  }
505 
506  queue_len -= sent;
507  if (sent > 0 || c->event_queue) {
508  crm_trace("Sent %d events (%d remaining) for %p[%d]: %s (%d)",
509  sent, queue_len, c->ipcs, c->pid, pcmk_strerror(rc < 0 ? rc : 0), rc);
510  }
511 
512  if (c->event_queue) {
513  if (queue_len % 100 == 0 && queue_len > 99) {
514  crm_warn("Event queue for %p[%d] has grown to %d", c->ipcs, c->pid, queue_len);
515 
516  } else if (queue_len > 500) {
517  crm_err("Evicting slow client %p[%d]: event queue reached %d entries",
518  c->ipcs, c->pid, queue_len);
519  qb_ipcs_disconnect(c->ipcs);
520  return rc;
521  }
522 
523  c->event_timer = g_timeout_add(1000 + 100 * queue_len, crm_ipcs_flush_events_cb, c);
524  }
525 
526  return rc;
527 }
528 
529 ssize_t
530 crm_ipc_prepare(uint32_t request, xmlNode * message, struct iovec ** result, uint32_t max_send_size)
531 {
532  static unsigned int biggest = 0;
533  struct iovec *iov;
534  unsigned int total = 0;
535  char *compressed = NULL;
536  char *buffer = dump_xml_unformatted(message);
537  struct crm_ipc_response_header *header = calloc(1, sizeof(struct crm_ipc_response_header));
538 
539  CRM_ASSERT(result != NULL);
540 
541  crm_ipc_init();
542 
543  if (max_send_size == 0) {
544  max_send_size = ipc_buffer_max;
545  }
546 
547  CRM_LOG_ASSERT(max_send_size != 0);
548 
549  *result = NULL;
550  iov = calloc(2, sizeof(struct iovec));
551 
552 
553  iov[0].iov_len = hdr_offset;
554  iov[0].iov_base = header;
555 
556  header->version = PCMK_IPC_VERSION;
557  header->size_uncompressed = 1 + strlen(buffer);
558  total = iov[0].iov_len + header->size_uncompressed;
559 
560  if (total < max_send_size) {
561  iov[1].iov_base = buffer;
562  iov[1].iov_len = header->size_uncompressed;
563 
564  } else {
565  unsigned int new_size = 0;
566 
568  (buffer, header->size_uncompressed, max_send_size, &compressed, &new_size)) {
569 
570  header->flags |= crm_ipc_compressed;
571  header->size_compressed = new_size;
572 
573  iov[1].iov_len = header->size_compressed;
574  iov[1].iov_base = compressed;
575 
576  free(buffer);
577 
578  biggest = QB_MAX(header->size_compressed, biggest);
579 
580  } else {
581  ssize_t rc = -EMSGSIZE;
582 
583  crm_log_xml_trace(message, "EMSGSIZE");
584  biggest = QB_MAX(header->size_uncompressed, biggest);
585 
586  crm_err
587  ("Could not compress the message (%u bytes) into less than the configured ipc limit (%u bytes). "
588  "Set PCMK_ipc_buffer to a higher value (%u bytes suggested)",
589  header->size_uncompressed, max_send_size, 4 * biggest);
590 
591  free(compressed);
592  free(buffer);
593  free(header);
594  free(iov);
595 
596  return rc;
597  }
598  }
599 
600  header->qb.size = iov[0].iov_len + iov[1].iov_len;
601  header->qb.id = (int32_t)request; /* Replying to a specific request */
602 
603  *result = iov;
604  CRM_ASSERT(header->qb.size > 0);
605  return header->qb.size;
606 }
607 
608 ssize_t
609 crm_ipcs_sendv(crm_client_t * c, struct iovec * iov, enum crm_ipc_flags flags)
610 {
611  ssize_t rc;
612  static uint32_t id = 1;
613  struct crm_ipc_response_header *header = iov[0].iov_base;
614 
616  /* _ALL_ replies to proxied connections need to be sent as events */
617  if (is_not_set(flags, crm_ipc_server_event)) {
618  flags |= crm_ipc_server_event;
619  /* this flag lets us know this was originally meant to be a response.
620  * even though we're sending it over the event channel. */
622  }
623  }
624 
625  header->flags |= flags;
626  if (flags & crm_ipc_server_event) {
627  header->qb.id = id++; /* We don't really use it, but doesn't hurt to set one */
628 
629  if (flags & crm_ipc_server_free) {
630  crm_trace("Sending the original to %p[%d]", c->ipcs, c->pid);
631  c->event_queue = g_list_append(c->event_queue, iov);
632 
633  } else {
634  struct iovec *iov_copy = calloc(2, sizeof(struct iovec));
635 
636  crm_trace("Sending a copy to %p[%d]", c->ipcs, c->pid);
637  iov_copy[0].iov_len = iov[0].iov_len;
638  iov_copy[0].iov_base = malloc(iov[0].iov_len);
639  memcpy(iov_copy[0].iov_base, iov[0].iov_base, iov[0].iov_len);
640 
641  iov_copy[1].iov_len = iov[1].iov_len;
642  iov_copy[1].iov_base = malloc(iov[1].iov_len);
643  memcpy(iov_copy[1].iov_base, iov[1].iov_base, iov[1].iov_len);
644 
645  c->event_queue = g_list_append(c->event_queue, iov_copy);
646  }
647 
648  } else {
649  CRM_LOG_ASSERT(header->qb.id != 0); /* Replying to a specific request */
650 
651  rc = qb_ipcs_response_sendv(c->ipcs, iov, 2);
652  if (rc < header->qb.size) {
653  crm_notice("Response %d to %p[%d] (%u bytes) failed: %s (%d)",
654  header->qb.id, c->ipcs, c->pid, header->qb.size, pcmk_strerror(rc), rc);
655 
656  } else {
657  crm_trace("Response %d sent, %d bytes to %p[%d]", header->qb.id, rc, c->ipcs, c->pid);
658  }
659 
660  if (flags & crm_ipc_server_free) {
661  free(iov[0].iov_base);
662  free(iov[1].iov_base);
663  free(iov);
664  }
665  }
666 
667  if (flags & crm_ipc_server_event) {
668  rc = crm_ipcs_flush_events(c);
669  } else {
671  }
672 
673  if (rc == -EPIPE || rc == -ENOTCONN) {
674  crm_trace("Client %p disconnected", c->ipcs);
675  }
676 
677  return rc;
678 }
679 
680 ssize_t
681 crm_ipcs_send(crm_client_t * c, uint32_t request, xmlNode * message,
682  enum crm_ipc_flags flags)
683 {
684  struct iovec *iov = NULL;
685  ssize_t rc = 0;
686 
687  if(c == NULL) {
688  return -EDESTADDRREQ;
689  }
690  crm_ipc_init();
691 
692  rc = crm_ipc_prepare(request, message, &iov, ipc_buffer_max);
693  if (rc > 0) {
694  rc = crm_ipcs_sendv(c, iov, flags | crm_ipc_server_free);
695 
696  } else {
697  free(iov);
698  crm_notice("Message to %p[%d] failed: %s (%d)",
699  c->ipcs, c->pid, pcmk_strerror(rc), rc);
700  }
701 
702  return rc;
703 }
704 
705 void
706 crm_ipcs_send_ack(crm_client_t * c, uint32_t request, uint32_t flags, const char *tag, const char *function,
707  int line)
708 {
709  if (flags & crm_ipc_client_response) {
710  xmlNode *ack = create_xml_node(NULL, tag);
711 
712  crm_trace("Ack'ing msg from %s (%p)", crm_client_name(c), c);
713  c->request_id = 0;
714  crm_xml_add(ack, "function", function);
715  crm_xml_add_int(ack, "line", line);
716  crm_ipcs_send(c, request, ack, flags);
717  free_xml(ack);
718  }
719 }
720 
721 /* Client... */
722 
723 #define MIN_MSG_SIZE 12336 /* sizeof(struct qb_ipc_connection_response) */
724 #define MAX_MSG_SIZE 128*1024 /* 128k default */
725 
726 struct crm_ipc_s {
727  struct pollfd pfd;
728 
729  /* the max size we can send/receive over ipc */
730  unsigned int max_buf_size;
731  /* Size of the allocated 'buffer' */
732  unsigned int buf_size;
733  int msg_size;
734  int need_reply;
735  char *buffer;
736  char *name;
737  uint32_t buffer_flags;
738 
739  qb_ipcc_connection_t *ipc;
740 
741 };
742 
743 static unsigned int
744 pick_ipc_buffer(unsigned int max)
745 {
746  static unsigned int global_max = 0;
747 
748  if(global_max == 0) {
749  const char *env = getenv("PCMK_ipc_buffer");
750 
751  if (env) {
752  int env_max = crm_parse_int(env, "0");
753 
754  global_max = (env_max > 0)? env_max : MAX_MSG_SIZE;
755 
756  } else {
757  global_max = MAX_MSG_SIZE;
758  }
759  }
760 
761  return QB_MAX(max, global_max);
762 }
763 
764 crm_ipc_t *
765 crm_ipc_new(const char *name, size_t max_size)
766 {
767  crm_ipc_t *client = NULL;
768 
769  client = calloc(1, sizeof(crm_ipc_t));
770 
771  client->name = strdup(name);
772  client->buf_size = pick_ipc_buffer(max_size);
773  client->buffer = malloc(client->buf_size);
774 
775  /* Clients initiating connection pick the max buf size */
776  client->max_buf_size = client->buf_size;
777 
778  client->pfd.fd = -1;
779  client->pfd.events = POLLIN;
780  client->pfd.revents = 0;
781 
782  return client;
783 }
784 
794 bool
796 {
797  static uid_t cl_uid = 0;
798  static gid_t cl_gid = 0;
799  pid_t found_pid = 0; uid_t found_uid = 0; gid_t found_gid = 0;
800  int rv;
801 
802  client->need_reply = FALSE;
803  client->ipc = qb_ipcc_connect(client->name, client->buf_size);
804 
805  if (client->ipc == NULL) {
806  crm_debug("Could not establish %s connection: %s (%d)", client->name, pcmk_strerror(errno), errno);
807  return FALSE;
808  }
809 
810  client->pfd.fd = crm_ipc_get_fd(client);
811  if (client->pfd.fd < 0) {
812  rv = errno;
813  /* message already omitted */
814  crm_ipc_close(client);
815  errno = rv;
816  return FALSE;
817  }
818 
819  if (!cl_uid && !cl_gid
820  && (rv = crm_user_lookup(CRM_DAEMON_USER, &cl_uid, &cl_gid)) < 0) {
821  errno = -rv;
822  /* message already omitted */
823  crm_ipc_close(client);
824  errno = -rv;
825  return FALSE;
826  }
827 
828  if (!(rv = crm_ipc_is_authentic_process(client->pfd.fd, cl_uid, cl_gid,
829  &found_pid, &found_uid,
830  &found_gid))) {
831  crm_err("Daemon (IPC %s) is not authentic:"
832  " process %lld (uid: %lld, gid: %lld)",
833  client->name, (long long) PCMK__SPECIAL_PID_AS_0(found_pid),
834  (long long) found_uid, (long long) found_gid);
835  crm_ipc_close(client);
836  errno = ECONNABORTED;
837  return FALSE;
838 
839  } else if (rv < 0) {
840  errno = -rv;
841  crm_perror(LOG_ERR, "Could not verify authenticity of daemon (IPC %s)",
842  client->name);
843  crm_ipc_close(client);
844  errno = -rv;
845  return FALSE;
846  }
847 
848  qb_ipcc_context_set(client->ipc, client);
849 
850 #ifdef HAVE_IPCS_GET_BUFFER_SIZE
851  client->max_buf_size = qb_ipcc_get_buffer_size(client->ipc);
852  if (client->max_buf_size > client->buf_size) {
853  free(client->buffer);
854  client->buffer = calloc(1, client->max_buf_size);
855  client->buf_size = client->max_buf_size;
856  }
857 #endif
858 
859  return TRUE;
860 }
861 
862 void
864 {
865  if (client) {
866  crm_trace("Disconnecting %s IPC connection %p (%p.%p)", client->name, client, client->ipc);
867 
868  if (client->ipc) {
869  qb_ipcc_connection_t *ipc = client->ipc;
870 
871  client->ipc = NULL;
872  qb_ipcc_disconnect(ipc);
873  }
874  }
875 }
876 
877 void
879 {
880  if (client) {
881  if (client->ipc && qb_ipcc_is_connected(client->ipc)) {
882  crm_notice("Destroying an active IPC connection to %s", client->name);
883  /* The next line is basically unsafe
884  *
885  * If this connection was attached to mainloop and mainloop is active,
886  * the 'disconnected' callback will end up back here and we'll end
887  * up free'ing the memory twice - something that can still happen
888  * even without this if we destroy a connection and it closes before
889  * we call exit
890  */
891  /* crm_ipc_close(client); */
892  }
893  crm_trace("Destroying IPC connection to %s: %p", client->name, client);
894  free(client->buffer);
895  free(client->name);
896  free(client);
897  }
898 }
899 
900 int
902 {
903  int fd = 0;
904 
905  if (client && client->ipc && (qb_ipcc_fd_get(client->ipc, &fd) == 0)) {
906  return fd;
907  }
908  errno = EINVAL;
909  crm_perror(LOG_ERR, "Could not obtain file IPC descriptor for %s",
910  (client? client->name : "unspecified client"));
911  return -errno;
912 }
913 
914 bool
916 {
917  bool rc = FALSE;
918 
919  if (client == NULL) {
920  crm_trace("No client");
921  return FALSE;
922 
923  } else if (client->ipc == NULL) {
924  crm_trace("No connection");
925  return FALSE;
926 
927  } else if (client->pfd.fd < 0) {
928  crm_trace("Bad descriptor");
929  return FALSE;
930  }
931 
932  rc = qb_ipcc_is_connected(client->ipc);
933  if (rc == FALSE) {
934  client->pfd.fd = -EINVAL;
935  }
936  return rc;
937 }
938 
939 int
941 {
942  CRM_ASSERT(client != NULL);
943 
944  if (crm_ipc_connected(client) == FALSE) {
945  return -ENOTCONN;
946  }
947 
948  client->pfd.revents = 0;
949  return poll(&(client->pfd), 1, 0);
950 }
951 
952 static int
953 crm_ipc_decompress(crm_ipc_t * client)
954 {
955  struct crm_ipc_response_header *header = (struct crm_ipc_response_header *)(void*)client->buffer;
956 
957  if (header->size_compressed) {
958  int rc = 0;
959  unsigned int size_u = 1 + header->size_uncompressed;
960  /* never let buf size fall below our max size required for ipc reads. */
961  unsigned int new_buf_size = QB_MAX((hdr_offset + size_u), client->max_buf_size);
962  char *uncompressed = calloc(1, new_buf_size);
963 
964  crm_trace("Decompressing message data %u bytes into %u bytes",
965  header->size_compressed, size_u);
966 
967  rc = BZ2_bzBuffToBuffDecompress(uncompressed + hdr_offset, &size_u,
968  client->buffer + hdr_offset, header->size_compressed, 1, 0);
969 
970  if (rc != BZ_OK) {
971  crm_err("Decompression failed: %s (%d)", bz2_strerror(rc), rc);
972  free(uncompressed);
973  return -EILSEQ;
974  }
975 
976  /*
977  * This assert no longer holds true. For an identical msg, some clients may
978  * require compression, and others may not. If that same msg (event) is sent
979  * to multiple clients, it could result in some clients receiving a compressed
980  * msg even though compression was not explicitly required for them.
981  *
982  * CRM_ASSERT((header->size_uncompressed + hdr_offset) >= ipc_buffer_max);
983  */
984  CRM_ASSERT(size_u == header->size_uncompressed);
985 
986  memcpy(uncompressed, client->buffer, hdr_offset); /* Preserve the header */
987  header = (struct crm_ipc_response_header *)(void*)uncompressed;
988 
989  free(client->buffer);
990  client->buf_size = new_buf_size;
991  client->buffer = uncompressed;
992  }
993 
994  CRM_ASSERT(client->buffer[hdr_offset + header->size_uncompressed - 1] == 0);
995  return pcmk_ok;
996 }
997 
998 long
1000 {
1001  struct crm_ipc_response_header *header = NULL;
1002 
1003  CRM_ASSERT(client != NULL);
1004  CRM_ASSERT(client->ipc != NULL);
1005  CRM_ASSERT(client->buffer != NULL);
1006 
1007  crm_ipc_init();
1008 
1009  client->buffer[0] = 0;
1010  client->msg_size = qb_ipcc_event_recv(client->ipc, client->buffer, client->buf_size - 1, 0);
1011  if (client->msg_size >= 0) {
1012  int rc = crm_ipc_decompress(client);
1013 
1014  if (rc != pcmk_ok) {
1015  return rc;
1016  }
1017 
1018  header = (struct crm_ipc_response_header *)(void*)client->buffer;
1019  if(header->version > PCMK_IPC_VERSION) {
1020  crm_err("Filtering incompatible v%d IPC message, we only support versions <= %d",
1021  header->version, PCMK_IPC_VERSION);
1022  return -EBADMSG;
1023  }
1024 
1025  crm_trace("Received %s event %d, size=%u, rc=%d, text: %.100s",
1026  client->name, header->qb.id, header->qb.size, client->msg_size,
1027  client->buffer + hdr_offset);
1028 
1029  } else {
1030  crm_trace("No message from %s received: %s", client->name, pcmk_strerror(client->msg_size));
1031  }
1032 
1033  if (crm_ipc_connected(client) == FALSE || client->msg_size == -ENOTCONN) {
1034  crm_err("Connection to %s failed", client->name);
1035  }
1036 
1037  if (header) {
1038  /* Data excluding the header */
1039  return header->size_uncompressed;
1040  }
1041  return -ENOMSG;
1042 }
1043 
1044 const char *
1046 {
1047  CRM_ASSERT(client != NULL);
1048  return client->buffer + sizeof(struct crm_ipc_response_header);
1049 }
1050 
1051 uint32_t
1053 {
1054  struct crm_ipc_response_header *header = NULL;
1055 
1056  CRM_ASSERT(client != NULL);
1057  if (client->buffer == NULL) {
1058  return 0;
1059  }
1060 
1061  header = (struct crm_ipc_response_header *)(void*)client->buffer;
1062  return header->flags;
1063 }
1064 
1065 const char *
1067 {
1068  CRM_ASSERT(client != NULL);
1069  return client->name;
1070 }
1071 
1072 static int
1073 internal_ipc_send_recv(crm_ipc_t * client, const void *iov)
1074 {
1075  int rc = 0;
1076 
1077  do {
1078  rc = qb_ipcc_sendv_recv(client->ipc, iov, 2, client->buffer, client->buf_size, -1);
1079  } while (rc == -EAGAIN && crm_ipc_connected(client));
1080 
1081  return rc;
1082 }
1083 
1084 static int
1085 internal_ipc_send_request(crm_ipc_t * client, const void *iov, int ms_timeout)
1086 {
1087  int rc = 0;
1088  time_t timeout = time(NULL) + 1 + (ms_timeout / 1000);
1089 
1090  do {
1091  rc = qb_ipcc_sendv(client->ipc, iov, 2);
1092  } while (rc == -EAGAIN && time(NULL) < timeout && crm_ipc_connected(client));
1093 
1094  return rc;
1095 }
1096 
1097 static int
1098 internal_ipc_get_reply(crm_ipc_t * client, int request_id, int ms_timeout)
1099 {
1100  time_t timeout = time(NULL) + 1 + (ms_timeout / 1000);
1101  int rc = 0;
1102 
1103  crm_ipc_init();
1104 
1105  /* get the reply */
1106  crm_trace("client %s waiting on reply to msg id %d", client->name, request_id);
1107  do {
1108 
1109  rc = qb_ipcc_recv(client->ipc, client->buffer, client->buf_size, 1000);
1110  if (rc > 0) {
1111  struct crm_ipc_response_header *hdr = NULL;
1112 
1113  int rc = crm_ipc_decompress(client);
1114 
1115  if (rc != pcmk_ok) {
1116  return rc;
1117  }
1118 
1119  hdr = (struct crm_ipc_response_header *)(void*)client->buffer;
1120  if (hdr->qb.id == request_id) {
1121  /* Got it */
1122  break;
1123  } else if (hdr->qb.id < request_id) {
1124  xmlNode *bad = string2xml(crm_ipc_buffer(client));
1125 
1126  crm_err("Discarding old reply %d (need %d)", hdr->qb.id, request_id);
1127  crm_log_xml_notice(bad, "OldIpcReply");
1128 
1129  } else {
1130  xmlNode *bad = string2xml(crm_ipc_buffer(client));
1131 
1132  crm_err("Discarding newer reply %d (need %d)", hdr->qb.id, request_id);
1133  crm_log_xml_notice(bad, "ImpossibleReply");
1134  CRM_ASSERT(hdr->qb.id <= request_id);
1135  }
1136  } else if (crm_ipc_connected(client) == FALSE) {
1137  crm_err("Server disconnected client %s while waiting for msg id %d", client->name,
1138  request_id);
1139  break;
1140  }
1141 
1142  } while (time(NULL) < timeout);
1143 
1144  return rc;
1145 }
1146 
1147 int
1148 crm_ipc_send(crm_ipc_t * client, xmlNode * message, enum crm_ipc_flags flags, int32_t ms_timeout,
1149  xmlNode ** reply)
1150 {
1151  long rc = 0;
1152  struct iovec *iov;
1153  static uint32_t id = 0;
1154  static int factor = 8;
1155  struct crm_ipc_response_header *header;
1156 
1157  crm_ipc_init();
1158 
1159  if (client == NULL) {
1160  crm_notice("Invalid connection");
1161  return -ENOTCONN;
1162 
1163  } else if (crm_ipc_connected(client) == FALSE) {
1164  /* Don't even bother */
1165  crm_notice("Connection to %s closed", client->name);
1166  return -ENOTCONN;
1167  }
1168 
1169  if (ms_timeout == 0) {
1170  ms_timeout = 5000;
1171  }
1172 
1173  if (client->need_reply) {
1174  crm_trace("Trying again to obtain pending reply from %s", client->name);
1175  rc = qb_ipcc_recv(client->ipc, client->buffer, client->buf_size, ms_timeout);
1176  if (rc < 0) {
1177  crm_warn("Sending to %s (%p) is disabled until pending reply is received", client->name,
1178  client->ipc);
1179  return -EALREADY;
1180 
1181  } else {
1182  crm_notice("Lost reply from %s (%p) finally arrived, sending re-enabled", client->name,
1183  client->ipc);
1184  client->need_reply = FALSE;
1185  }
1186  }
1187 
1188  id++;
1189  CRM_LOG_ASSERT(id != 0); /* Crude wrap-around detection */
1190  rc = crm_ipc_prepare(id, message, &iov, client->max_buf_size);
1191  if(rc < 0) {
1192  return rc;
1193  }
1194 
1195  header = iov[0].iov_base;
1196  header->flags |= flags;
1197 
1198  if(is_set(flags, crm_ipc_proxied)) {
1199  /* Don't look for a synchronous response */
1201  }
1202 
1203  if(header->size_compressed) {
1204  if(factor < 10 && (client->max_buf_size / 10) < (rc / factor)) {
1205  crm_notice("Compressed message exceeds %d0%% of the configured ipc limit (%u bytes), "
1206  "consider setting PCMK_ipc_buffer to %u or higher",
1207  factor, client->max_buf_size, 2 * client->max_buf_size);
1208  factor++;
1209  }
1210  }
1211 
1212  crm_trace("Sending from client: %s request id: %d bytes: %u timeout:%d msg...",
1213  client->name, header->qb.id, header->qb.size, ms_timeout);
1214 
1215  if (ms_timeout > 0 || is_not_set(flags, crm_ipc_client_response)) {
1216 
1217  rc = internal_ipc_send_request(client, iov, ms_timeout);
1218 
1219  if (rc <= 0) {
1220  crm_trace("Failed to send from client %s request %d with %u bytes...",
1221  client->name, header->qb.id, header->qb.size);
1222  goto send_cleanup;
1223 
1224  } else if (is_not_set(flags, crm_ipc_client_response)) {
1225  crm_trace("Message sent, not waiting for reply to %d from %s to %u bytes...",
1226  header->qb.id, client->name, header->qb.size);
1227 
1228  goto send_cleanup;
1229  }
1230 
1231  rc = internal_ipc_get_reply(client, header->qb.id, ms_timeout);
1232  if (rc < 0) {
1233  /* No reply, for now, disable sending
1234  *
1235  * The alternative is to close the connection since we don't know
1236  * how to detect and discard out-of-sequence replies
1237  *
1238  * TODO - implement the above
1239  */
1240  client->need_reply = TRUE;
1241  }
1242 
1243  } else {
1244  rc = internal_ipc_send_recv(client, iov);
1245  }
1246 
1247  if (rc > 0) {
1248  struct crm_ipc_response_header *hdr = (struct crm_ipc_response_header *)(void*)client->buffer;
1249 
1250  crm_trace("Received response %d, size=%u, rc=%ld, text: %.200s", hdr->qb.id, hdr->qb.size,
1251  rc, crm_ipc_buffer(client));
1252 
1253  if (reply) {
1254  *reply = string2xml(crm_ipc_buffer(client));
1255  }
1256 
1257  } else {
1258  crm_trace("Response not received: rc=%ld, errno=%d", rc, errno);
1259  }
1260 
1261  send_cleanup:
1262  if (crm_ipc_connected(client) == FALSE) {
1263  crm_notice("Connection to %s closed: %s (%ld)", client->name, pcmk_strerror(rc), rc);
1264 
1265  } else if (rc == -ETIMEDOUT) {
1266  crm_warn("Request %d to %s (%p) failed: %s (%ld) after %dms",
1267  header->qb.id, client->name, client->ipc, pcmk_strerror(rc), rc, ms_timeout);
1268  crm_write_blackbox(0, NULL);
1269 
1270  } else if (rc <= 0) {
1271  crm_warn("Request %d to %s (%p) failed: %s (%ld)",
1272  header->qb.id, client->name, client->ipc, pcmk_strerror(rc), rc);
1273  }
1274 
1275  free(header);
1276  free(iov[1].iov_base);
1277  free(iov);
1278  return rc;
1279 }
1280 
1281 int
1282 crm_ipc_is_authentic_process(int sock, uid_t refuid, gid_t refgid,
1283  pid_t *gotpid, uid_t *gotuid, gid_t *gotgid) {
1284  int ret = 0;
1285  pid_t found_pid = 0; uid_t found_uid = 0; gid_t found_gid = 0;
1286 #if defined(US_AUTH_PEERCRED_UCRED)
1287  struct ucred ucred;
1288  socklen_t ucred_len = sizeof(ucred);
1289 
1290  if (!getsockopt(sock, SOL_SOCKET, SO_PEERCRED,
1291  &ucred, &ucred_len)
1292  && ucred_len == sizeof(ucred)) {
1293  found_pid = ucred.pid; found_uid = ucred.uid; found_gid = ucred.gid;
1294 
1295 #elif defined(US_AUTH_PEERCRED_SOCKPEERCRED)
1296  struct sockpeercred sockpeercred;
1297  socklen_t sockpeercred_len = sizeof(sockpeercred);
1298 
1299  if (!getsockopt(sock, SOL_SOCKET, SO_PEERCRED,
1300  &sockpeercred, &sockpeercred_len)
1301  && sockpeercred_len == sizeof(sockpeercred_len)) {
1302  found_pid = sockpeercred.pid;
1303  found_uid = sockpeercred.uid; found_gid = sockpeercred.gid;
1304 
1305 #elif defined(US_AUTH_GETPEEREID)
1306  if (!getpeereid(sock, &found_uid, &found_gid)) {
1307  found_pid = PCMK__SPECIAL_PID; /* cannot obtain PID (FreeBSD) */
1308 
1309 #elif defined(US_AUTH_GETPEERUCRED)
1310  ucred_t *ucred;
1311  if (!getpeerucred(sock, &ucred)) {
1312  errno = 0;
1313  found_pid = ucred_getpid(ucred);
1314  found_uid = ucred_geteuid(ucred); found_gid = ucred_getegid(ucred);
1315  ret = -errno;
1316  ucred_free(ucred);
1317  if (ret) {
1318  return (ret < 0) ? ret : -pcmk_err_generic;
1319  }
1320 
1321 #else
1322 # error "No way to authenticate a Unix socket peer"
1323  errno = 0;
1324  if (0) {
1325 #endif
1326  if (gotpid != NULL) {
1327  *gotpid = found_pid;
1328  }
1329  if (gotuid != NULL) {
1330  *gotuid = found_uid;
1331  }
1332  if (gotgid != NULL) {
1333  *gotgid = found_gid;
1334  }
1335  ret = (found_uid == 0 || found_uid == refuid || found_gid == refgid);
1336  } else {
1337  ret = (errno > 0) ? -errno : -pcmk_err_generic;
1338  }
1339 
1340  return ret;
1341 }
1342 
1343 int
1344 pcmk__ipc_is_authentic_process_active(const char *name, uid_t refuid,
1345  gid_t refgid, pid_t *gotpid) {
1346  static char last_asked_name[PATH_MAX / 2] = ""; /* log spam prevention */
1347  int fd, ret = 0;
1348  pid_t found_pid = 0; uid_t found_uid = 0; gid_t found_gid = 0;
1349  qb_ipcc_connection_t *c;
1350 
1351  if ((c = qb_ipcc_connect(name, 0)) == NULL) {
1352  crm_info("Could not connect to %s IPC: %s", name, strerror(errno));
1353 
1354  } else if ((ret = qb_ipcc_fd_get(c, &fd))) {
1355  crm_err("Could not get fd from %s IPC: %s (%d)", name,
1356  strerror(-ret), -ret);
1357  ret = -1;
1358 
1359  } else if ((ret = crm_ipc_is_authentic_process(fd, refuid, refgid,
1360  &found_pid, &found_uid,
1361  &found_gid)) < 0) {
1362  if (ret == -pcmk_err_generic) {
1363  crm_err("Could not get peer credentials from %s IPC", name);
1364  } else {
1365  crm_err("Could not get peer credentials from %s IPC: %s (%d)",
1366  name, strerror(-ret), -ret);
1367  }
1368  ret = -1;
1369 
1370  } else {
1371  if (gotpid != NULL) {
1372  *gotpid = found_pid;
1373  }
1374 
1375  if (!ret) {
1376  crm_err("Daemon (IPC %s) effectively blocked with unauthorized"
1377  " process %lld (uid: %lld, gid: %lld)",
1378  name, (long long) PCMK__SPECIAL_PID_AS_0(found_pid),
1379  (long long) found_uid, (long long) found_gid);
1380  ret = -2;
1381  } else if ((found_uid != refuid || found_gid != refgid)
1382  && strncmp(last_asked_name, name, sizeof(last_asked_name))) {
1383  if (!found_uid && refuid) {
1384  crm_warn("Daemon (IPC %s) runs as root, whereas the expected"
1385  " credentials are %lld:%lld, hazard of violating"
1386  " the least privilege principle",
1387  name, (long long) refuid, (long long) refgid);
1388  } else {
1389  crm_notice("Daemon (IPC %s) runs as %lld:%lld, whereas the"
1390  " expected credentials are %lld:%lld, which may"
1391  " mean a different set of privileges than expected",
1392  name, (long long) found_uid, (long long) found_gid,
1393  (long long) refuid, (long long) refgid);
1394  }
1395  memccpy(last_asked_name, name, '\0', sizeof(last_asked_name));
1396  }
1397  }
1398 
1399  if (ret) { /* here, !ret only when we could not initially connect */
1400  qb_ipcc_disconnect(c);
1401  }
1402 
1403  return ret;
1404 }
1405 
1406 
1407 /* Utils */
1408 
1409 xmlNode *
1410 create_hello_message(const char *uuid,
1411  const char *client_name, const char *major_version, const char *minor_version)
1412 {
1413  xmlNode *hello_node = NULL;
1414  xmlNode *hello = NULL;
1415 
1416  if (uuid == NULL || strlen(uuid) == 0
1417  || client_name == NULL || strlen(client_name) == 0
1418  || major_version == NULL || strlen(major_version) == 0
1419  || minor_version == NULL || strlen(minor_version) == 0) {
1420  crm_err("Missing fields, Hello message will not be valid.");
1421  return NULL;
1422  }
1423 
1424  hello_node = create_xml_node(NULL, XML_TAG_OPTIONS);
1425  crm_xml_add(hello_node, "major_version", major_version);
1426  crm_xml_add(hello_node, "minor_version", minor_version);
1427  crm_xml_add(hello_node, "client_name", client_name);
1428  crm_xml_add(hello_node, "client_uuid", uuid);
1429 
1430  crm_trace("creating hello message");
1431  hello = create_request(CRM_OP_HELLO, hello_node, NULL, NULL, client_name, uuid);
1432  free_xml(hello_node);
1433 
1434  return hello;
1435 }
#define F_CRM_TASK
Definition: msg_xml.h:56
const char * crm_ipc_buffer(crm_ipc_t *client)
Definition: ipc.c:1045
#define F_CRM_REFERENCE
Definition: msg_xml.h:62
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
Definition: logging.c:407
A dumping ground.
#define F_TYPE
Definition: msg_xml.h:34
void crm_ipc_close(crm_ipc_t *client)
Definition: ipc.c:863
#define crm_notice(fmt, args...)
Definition: logging.h:250
char * crm_generate_uuid(void)
Definition: utils.c:2327
uint32_t crm_ipc_buffer_flags(crm_ipc_t *client)
Definition: ipc.c:1052
int crm_ipc_send(crm_ipc_t *client, xmlNode *message, enum crm_ipc_flags flags, int32_t ms_timeout, xmlNode **reply)
Definition: ipc.c:1148
#define F_CRM_HOST_TO
Definition: msg_xml.h:57
#define XML_TAG_OPTIONS
Definition: msg_xml.h:120
const char * pcmk_strerror(int rc)
Definition: logging.c:1113
crm_client_t * crm_client_get(qb_ipcs_connection_t *c)
Definition: ipc.c:208
uint32_t flags
Definition: ipcs.h:79
qb_ipcs_connection_t * ipcs
Definition: ipcs.h:90
#define F_CRM_MSG_TYPE
Definition: msg_xml.h:58
uint32_t size
Definition: internal.h:52
int request_id
Definition: ipcs.h:78
#define CRM_FEATURE_SET
Definition: crm.h:38
#define F_CRM_HOST_FROM
Definition: msg_xml.h:61
#define pcmk_ok
Definition: error.h:42
#define T_CRM
Definition: msg_xml.h:46
crm_client_t * crm_client_get_by_id(const char *id)
Definition: ipc.c:219
xmlNode * create_reply_adv(xmlNode *original_request, xmlNode *xml_response_data, const char *origin)
Definition: ipc.c:151
char * buffer
Definition: ipcs.h:43
#define PCMK__SPECIAL_PID_AS_0(p)
Definition: ipc_internal.h:34
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
Definition: ipc.c:795
int crm_parse_int(const char *text, const char *default_text)
Definition: utils.c:634
#define PCMK_IPC_VERSION
Definition: ipc.c:42
#define PCMK__SPECIAL_PID
Definition: ipc_internal.h:25
struct crm_remote_s * remote
Definition: ipcs.h:92
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
Definition: utils.c:446
#define CRM_LOG_ASSERT(expr)
Definition: logging.h:150
void crm_client_destroy(crm_client_t *c)
Definition: ipc.c:341
int crm_ipc_get_fd(crm_ipc_t *client)
Definition: ipc.c:901
void crm_client_init(void)
Definition: ipc.c:253
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.
Definition: ipc.c:1282
#define clear_bit(word, bit)
Definition: crm_internal.h:220
void crm_client_disconnect_all(qb_ipcs_service_t *service)
Definition: ipc.c:275
char * strerror(int errnum)
ssize_t crm_ipc_prepare(uint32_t request, xmlNode *message, struct iovec **result, uint32_t max_send_size)
Definition: ipc.c:530
char * user
Definition: ipcs.h:74
ssize_t crm_ipcs_flush_events(crm_client_t *c)
Definition: ipc.c:465
xmlNode * string2xml(const char *input)
Definition: xml.c:2957
char version[256]
Definition: plugin.c:84
#define MAX_MSG_SIZE
Definition: ipc.c:724
#define XML_ATTR_REQUEST
Definition: msg_xml.h:125
ssize_t crm_ipcs_sendv(crm_client_t *c, struct iovec *iov, enum crm_ipc_flags flags)
Definition: ipc.c:609
#define crm_warn(fmt, args...)
Definition: logging.h:249
crm_client_t * crm_client_new(qb_ipcs_connection_t *c, uid_t uid_client, gid_t gid_client)
Definition: ipc.c:292
uint64_t flags
Definition: remote.c:121
#define crm_debug(fmt, args...)
Definition: logging.h:253
#define F_CRM_SYS_TO
Definition: msg_xml.h:59
struct crm_ipc_s crm_ipc_t
Definition: ipc.h:61
const char * crm_ipc_name(crm_ipc_t *client)
Definition: ipc.c:1066
GList * event_queue
Definition: ipcs.h:83
GHashTable * client_connections
Definition: ipc.c:205
unsigned int crm_ipc_default_buffer_size(void)
Definition: ipc.c:68
#define crm_trace(fmt, args...)
Definition: logging.h:254
void crm_ipc_destroy(crm_ipc_t *client)
Definition: ipc.c:878
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:2793
const char * crm_element_value(xmlNode *data, const char *name)
Definition: xml.c:5839
#define pcmk_err_generic
Definition: error.h:45
#define CRM_DAEMON_USER
Definition: config.h:47
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
Definition: xml.c:3332
void free_xml(xmlNode *child)
Definition: xml.c:2848
void crm_ipcs_send_ack(crm_client_t *c, uint32_t request, uint32_t flags, const char *tag, const char *function, int line)
Definition: ipc.c:706
xmlNode * crm_ipcs_recv(crm_client_t *c, void *data, size_t size, uint32_t *id, uint32_t *flags)
Definition: ipc.c:398
int auth_timeout
Definition: ipcs.h:46
#define F_CRM_DATA
Definition: msg_xml.h:55
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2695
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Definition: xml.c:2783
ssize_t crm_ipcs_send(crm_client_t *c, uint32_t request, xmlNode *message, enum crm_ipc_flags flags)
Definition: ipc.c:681
int pcmk__ipc_is_authentic_process_active(const char *name, uid_t refuid, gid_t refgid, pid_t *gotpid)
Definition: ipc.c:1344
uint pid
Definition: ipcs.h:67
int event_timer
Definition: ipcs.h:82
#define crm_perror(level, fmt, args...)
Log a system error message.
Definition: logging.h:226
bool crm_compress_string(const char *data, int length, int max, char **result, unsigned int *result_len)
Definition: utils.c:2371
xmlNode * create_hello_message(const char *uuid, const char *client_name, const char *major_version, const char *minor_version)
Definition: ipc.c:1410
#define CRM_OP_HELLO
Definition: crm.h:106
#define crm_err(fmt, args...)
Definition: logging.h:248
const char * bz2_strerror(int rc)
Definition: logging.c:1176
#define F_CRM_SYS_FROM
Definition: msg_xml.h:60
#define crm_log_xml_notice(xml, text)
Definition: logging.h:259
char * dump_xml_unformatted(xmlNode *msg)
Definition: xml.c:3987
int crm_ipc_ready(crm_ipc_t *client)
Definition: ipc.c:940
#define uint32_t
Definition: stdint.in.h:158
#define XML_ATTR_RESPONSE
Definition: msg_xml.h:126
#define CRM_ASSERT(expr)
Definition: error.h:35
char data[0]
Definition: internal.h:58
long crm_ipc_read(crm_ipc_t *client)
Definition: ipc.c:999
char * id
Definition: ipcs.h:72
#define uint8_t
Definition: stdint.in.h:144
Wrappers for and extensions to libqb IPC.
char * generate_hash_key(const char *crm_msg_reference, const char *sys)
Definition: utils.c:412
#define F_CRM_ORIGIN
Definition: msg_xml.h:64
int crm_ipcs_client_pid(qb_ipcs_connection_t *c)
Definition: ipc.c:388
#define crm_log_xml_trace(xml, text)
Definition: logging.h:262
void crm_client_cleanup(void)
Definition: ipc.c:262
enum client_type kind
Definition: ipcs.h:88
const char * crm_client_name(crm_client_t *c)
Definition: ipc.c:239
bool crm_ipc_connected(crm_ipc_t *client)
Definition: ipc.c:915
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
Definition: ipc.c:765
char * name
Definition: ipcs.h:73
crm_ipc_flags
Definition: ipc.h:41
xmlNode * create_request_adv(const char *task, xmlNode *msg_data, const char *host_to, const char *sys_to, const char *sys_from, const char *uuid_from, const char *origin)
Definition: ipc.c:106
#define create_request(task, xml_data, host_to, sys_to, sys_from, uuid_from)
Definition: ipc.h:34
#define crm_info(fmt, args...)
Definition: logging.h:251
char * uid2username(uid_t uid)
#define F_CRM_VERSION
Definition: msg_xml.h:63
enum crm_ais_msg_types type
Definition: internal.h:51
#define int32_t
Definition: stdint.in.h:157