corosync  3.0.2
main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2006 MontaVista Software, Inc.
3  * Copyright (c) 2006-2018 Red Hat, Inc.
4  *
5  * All rights reserved.
6  *
7  * Author: Steven Dake (sdake@redhat.com)
8  *
9  * This software licensed under BSD license, the text of which follows:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * - Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  * - Neither the name of the MontaVista Software, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
72 #include <config.h>
73 
74 #include <pthread.h>
75 #include <assert.h>
76 #include <sys/types.h>
77 #include <sys/file.h>
78 #include <sys/poll.h>
79 #include <sys/uio.h>
80 #include <sys/mman.h>
81 #include <sys/socket.h>
82 #include <sys/un.h>
83 #include <sys/time.h>
84 #include <sys/resource.h>
85 #include <sys/stat.h>
86 #include <netinet/in.h>
87 #include <arpa/inet.h>
88 #include <unistd.h>
89 #include <fcntl.h>
90 #include <stdlib.h>
91 #include <stdio.h>
92 #include <errno.h>
93 #include <signal.h>
94 #include <sched.h>
95 #include <time.h>
96 #include <semaphore.h>
97 #include <string.h>
98 
99 #ifdef HAVE_LIBSYSTEMD
100 #include <systemd/sd-daemon.h>
101 #endif
102 
103 #include <qb/qbdefs.h>
104 #include <qb/qblog.h>
105 #include <qb/qbloop.h>
106 #include <qb/qbutil.h>
107 #include <qb/qbipcs.h>
108 
109 #include <corosync/swab.h>
110 #include <corosync/corotypes.h>
111 #include <corosync/corodefs.h>
112 #include <corosync/totem/totempg.h>
113 #include <corosync/logsys.h>
114 #include <corosync/icmap.h>
115 
116 #include "quorum.h"
117 #include "totemsrp.h"
118 #include "logconfig.h"
119 #include "totemconfig.h"
120 #include "main.h"
121 #include "sync.h"
122 #include "timer.h"
123 #include "util.h"
124 #include "apidef.h"
125 #include "service.h"
126 #include "schedwrk.h"
127 #include "ipcs_stats.h"
128 #include "stats.h"
129 
130 #ifdef HAVE_SMALL_MEMORY_FOOTPRINT
131 #define IPC_LOGSYS_SIZE 1024*64
132 #else
133 #define IPC_LOGSYS_SIZE 8192*128
134 #endif
135 
136 /*
137  * LibQB adds default "*" syslog filter so we have to set syslog_priority as low
138  * as possible so filters applied later in _logsys_config_apply_per_file takes
139  * effect.
140  */
141 LOGSYS_DECLARE_SYSTEM ("corosync",
143  LOG_DAEMON,
144  LOG_EMERG);
145 
146 LOGSYS_DECLARE_SUBSYS ("MAIN");
147 
148 #define SERVER_BACKLOG 5
149 
150 static int sched_priority = 0;
151 
152 static unsigned int service_count = 32;
153 
155 
156 static struct corosync_api_v1 *api = NULL;
157 
158 static int sync_in_process = 1;
159 
160 static qb_loop_t *corosync_poll_handle;
161 
162 struct sched_param global_sched_param;
163 
164 static corosync_timer_handle_t corosync_stats_timer_handle;
165 
166 static const char *corosync_lock_file = LOCALSTATEDIR"/run/corosync.pid";
167 
168 static char corosync_config_file[PATH_MAX + 1] = COROSYSCONFDIR "/corosync.conf";
169 
170 qb_loop_t *cs_poll_handle_get (void)
171 {
172  return (corosync_poll_handle);
173 }
174 
175 int cs_poll_dispatch_add (qb_loop_t * handle,
176  int fd,
177  int events,
178  void *data,
179 
180  int (*dispatch_fn) (int fd,
181  int revents,
182  void *data))
183 {
184  return qb_loop_poll_add(handle, QB_LOOP_MED, fd, events, data,
185  dispatch_fn);
186 }
187 
188 int cs_poll_dispatch_delete(qb_loop_t * handle, int fd)
189 {
190  return qb_loop_poll_del(handle, fd);
191 }
192 
194 {
195  int i;
196 
197  for (i = 0; i < SERVICES_COUNT_MAX; i++) {
198  if (corosync_service[i] && corosync_service[i]->exec_dump_fn) {
200  }
201  }
202 }
203 
204 const char *corosync_get_config_file(void)
205 {
206 
207  return (corosync_config_file);
208 }
209 
210 static void corosync_blackbox_write_to_file (void)
211 {
212  char fname[PATH_MAX];
213  char fdata_fname[PATH_MAX];
214  char time_str[PATH_MAX];
215  struct tm cur_time_tm;
216  time_t cur_time_t;
217  ssize_t res;
218 
219  cur_time_t = time(NULL);
220  localtime_r(&cur_time_t, &cur_time_tm);
221 
222  strftime(time_str, PATH_MAX, "%Y-%m-%dT%H:%M:%S", &cur_time_tm);
223  if (snprintf(fname, PATH_MAX, "%s/fdata-%s-%lld",
224  get_state_dir(),
225  time_str,
226  (long long int)getpid()) >= PATH_MAX) {
227  log_printf(LOGSYS_LEVEL_ERROR, "Can't snprintf blackbox file name");
228  return ;
229  }
230 
231  if ((res = qb_log_blackbox_write_to_file(fname)) < 0) {
232  LOGSYS_PERROR(-res, LOGSYS_LEVEL_ERROR, "Can't store blackbox file");
233  return ;
234  }
235  snprintf(fdata_fname, sizeof(fdata_fname), "%s/fdata", get_state_dir());
236  unlink(fdata_fname);
237  if (symlink(fname, fdata_fname) == -1) {
238  log_printf(LOGSYS_LEVEL_ERROR, "Can't create symlink to '%s' for corosync blackbox file '%s'",
239  fname, fdata_fname);
240  }
241 }
242 
243 static void unlink_all_completed (void)
244 {
245  api->timer_delete (corosync_stats_timer_handle);
246  qb_loop_stop (corosync_poll_handle);
247  icmap_fini();
248 }
249 
251 {
252  corosync_service_unlink_all (api, unlink_all_completed);
253 }
254 
255 static int32_t sig_diag_handler (int num, void *data)
256 {
258  return 0;
259 }
260 
261 static int32_t sig_exit_handler (int num, void *data)
262 {
263  log_printf(LOGSYS_LEVEL_NOTICE, "Node was shut down by a signal");
264  corosync_service_unlink_all (api, unlink_all_completed);
265  return 0;
266 }
267 
268 static void sigsegv_handler (int num)
269 {
270  (void)signal (num, SIG_DFL);
271  corosync_blackbox_write_to_file ();
272  qb_log_fini();
273  raise (num);
274 }
275 
276 #define LOCALHOST_IP inet_addr("127.0.0.1")
277 
278 static void *corosync_group_handle;
279 
280 static struct totempg_group corosync_group = {
281  .group = "a",
282  .group_len = 1
283 };
284 
285 static void serialize_lock (void)
286 {
287 }
288 
289 static void serialize_unlock (void)
290 {
291 }
292 
293 static void corosync_sync_completed (void)
294 {
296  "Completed service synchronization, ready to provide service.");
297  sync_in_process = 0;
298 
299  cs_ipcs_sync_state_changed(sync_in_process);
301  /*
302  * Inform totem to start using new message queue again
303  */
305 
306 #ifdef HAVE_LIBSYSTEMD
307  sd_notify (0, "READY=1");
308 #endif
309 }
310 
311 static int corosync_sync_callbacks_retrieve (
312  int service_id,
313  struct sync_callbacks *callbacks)
314 {
315  if (corosync_service[service_id] == NULL) {
316  return (-1);
317  }
318 
319  if (callbacks == NULL) {
320  return (0);
321  }
322 
323  callbacks->name = corosync_service[service_id]->name;
324 
325  callbacks->sync_init = corosync_service[service_id]->sync_init;
326  callbacks->sync_process = corosync_service[service_id]->sync_process;
327  callbacks->sync_activate = corosync_service[service_id]->sync_activate;
328  callbacks->sync_abort = corosync_service[service_id]->sync_abort;
329  return (0);
330 }
331 
332 static struct memb_ring_id corosync_ring_id;
333 
334 static void member_object_joined (unsigned int nodeid)
335 {
336  char member_ip[ICMAP_KEYNAME_MAXLEN];
337  char member_join_count[ICMAP_KEYNAME_MAXLEN];
338  char member_status[ICMAP_KEYNAME_MAXLEN];
339 
340  snprintf(member_ip, ICMAP_KEYNAME_MAXLEN,
341  "runtime.members.%u.ip", nodeid);
342  snprintf(member_join_count, ICMAP_KEYNAME_MAXLEN,
343  "runtime.members.%u.join_count", nodeid);
344  snprintf(member_status, ICMAP_KEYNAME_MAXLEN,
345  "runtime.members.%u.status", nodeid);
346 
347  if (icmap_get(member_ip, NULL, NULL, NULL) == CS_OK) {
348  icmap_inc(member_join_count);
349  icmap_set_string(member_status, "joined");
350  } else {
351  icmap_set_string(member_ip, (char*)api->totem_ifaces_print (nodeid));
352  icmap_set_uint32(member_join_count, 1);
353  icmap_set_string(member_status, "joined");
354  }
355 
357  "Member joined: %s", api->totem_ifaces_print (nodeid));
358 }
359 
360 static void member_object_left (unsigned int nodeid)
361 {
362  char member_status[ICMAP_KEYNAME_MAXLEN];
363 
364  snprintf(member_status, ICMAP_KEYNAME_MAXLEN,
365  "runtime.members.%u.status", nodeid);
366  icmap_set_string(member_status, "left");
367 
369  "Member left: %s", api->totem_ifaces_print (nodeid));
370 }
371 
372 static void confchg_fn (
373  enum totem_configuration_type configuration_type,
374  const unsigned int *member_list, size_t member_list_entries,
375  const unsigned int *left_list, size_t left_list_entries,
376  const unsigned int *joined_list, size_t joined_list_entries,
377  const struct memb_ring_id *ring_id)
378 {
379  int i;
380  int abort_activate = 0;
381 
382  if (sync_in_process == 1) {
383  abort_activate = 1;
384  }
385  sync_in_process = 1;
386  cs_ipcs_sync_state_changed(sync_in_process);
387  memcpy (&corosync_ring_id, ring_id, sizeof (struct memb_ring_id));
388 
389  for (i = 0; i < left_list_entries; i++) {
390  member_object_left (left_list[i]);
391  }
392  for (i = 0; i < joined_list_entries; i++) {
393  member_object_joined (joined_list[i]);
394  }
395  /*
396  * Call configuration change for all services
397  */
398  for (i = 0; i < service_count; i++) {
399  if (corosync_service[i] && corosync_service[i]->confchg_fn) {
400  corosync_service[i]->confchg_fn (configuration_type,
401  member_list, member_list_entries,
402  left_list, left_list_entries,
403  joined_list, joined_list_entries, ring_id);
404  }
405  }
406 
407  if (abort_activate) {
408  sync_abort ();
409  }
410  if (configuration_type == TOTEM_CONFIGURATION_TRANSITIONAL) {
411  sync_save_transitional (member_list, member_list_entries, ring_id);
412  }
413  if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
414  sync_start (member_list, member_list_entries, ring_id);
415  }
416 }
417 
418 static void priv_drop (void)
419 {
420  return; /* TODO: we are still not dropping privs */
421 }
422 
423 static void corosync_tty_detach (void)
424 {
425  int devnull;
426 
427  /*
428  * Disconnect from TTY if this is not a debug run
429  */
430 
431  switch (fork ()) {
432  case -1:
434  break;
435  case 0:
436  /*
437  * child which is disconnected, run this process
438  */
439  break;
440  default:
441  exit (0);
442  break;
443  }
444 
445  /* Create new session */
446  (void)setsid();
447 
448  /*
449  * Map stdin/out/err to /dev/null.
450  */
451  devnull = open("/dev/null", O_RDWR);
452  if (devnull == -1) {
454  }
455 
456  if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
457  || dup2(devnull, 2) < 0) {
458  close(devnull);
460  }
461  close(devnull);
462 }
463 
464 static void corosync_mlockall (void)
465 {
466  int res;
467  struct rlimit rlimit;
468 
469  rlimit.rlim_cur = RLIM_INFINITY;
470  rlimit.rlim_max = RLIM_INFINITY;
471 
472 #ifndef RLIMIT_MEMLOCK
473 #define RLIMIT_MEMLOCK RLIMIT_VMEM
474 #endif
475 
476  setrlimit (RLIMIT_MEMLOCK, &rlimit);
477 
478  res = mlockall (MCL_CURRENT | MCL_FUTURE);
479  if (res == -1) {
481  "Could not lock memory of service to avoid page faults");
482  };
483 }
484 
485 
486 static void corosync_totem_stats_updater (void *data)
487 {
488  totempg_stats_t * stats;
489  uint32_t total_mtt_rx_token;
490  uint32_t total_backlog_calc;
491  uint32_t total_token_holdtime;
492  int t, prev;
493  int32_t token_count;
494  const char *cstr;
495 
496  stats = api->totem_get_stats();
497 
498 
500 
501  if (stats->srp->continuous_gather > MAX_NO_CONT_GATHER ||
503  cstr = "";
504 
506  cstr = "number of multicast sendmsg failures is above threshold";
507  }
508 
509  if (stats->srp->continuous_gather > MAX_NO_CONT_GATHER) {
510  cstr = "totem is continuously in gather state";
511  }
512 
514  "Totem is unable to form a cluster because of an "
515  "operating system or network fault (reason: %s). The most common "
516  "cause of this message is that the local firewall is "
517  "configured improperly.", cstr);
519  } else {
521  }
522 
523  total_mtt_rx_token = 0;
524  total_token_holdtime = 0;
525  total_backlog_calc = 0;
526  token_count = 0;
527  t = stats->srp->latest_token;
528  while (1) {
529  if (t == 0)
530  prev = TOTEM_TOKEN_STATS_MAX - 1;
531  else
532  prev = t - 1;
533  if (prev == stats->srp->earliest_token)
534  break;
535  /* if tx == 0, then dropped token (not ours) */
536  if (stats->srp->token[t].tx != 0 ||
537  (stats->srp->token[t].rx - stats->srp->token[prev].rx) > 0 ) {
538  total_mtt_rx_token += (stats->srp->token[t].rx - stats->srp->token[prev].rx);
539  total_token_holdtime += (stats->srp->token[t].tx - stats->srp->token[t].rx);
540  total_backlog_calc += stats->srp->token[t].backlog_calc;
541  token_count++;
542  }
543  t = prev;
544  }
545  if (token_count) {
546  stats->srp->mtt_rx_token = (total_mtt_rx_token / token_count);
547  stats->srp->avg_token_workload = (total_token_holdtime / token_count);
548  stats->srp->avg_backlog_calc = (total_backlog_calc / token_count);
549  }
550 
551  stats->srp->time_since_token_last_received = qb_util_nano_current_get () / QB_TIME_NS_IN_MSEC -
552  stats->srp->token[stats->srp->latest_token].rx;
553 
555 
556  api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
557  corosync_totem_stats_updater,
558  &corosync_stats_timer_handle);
559 }
560 
561 static void corosync_totem_stats_init (void)
562 {
563  /* start stats timer */
564  api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
565  corosync_totem_stats_updater,
566  &corosync_stats_timer_handle);
567 }
568 
569 static void deliver_fn (
570  unsigned int nodeid,
571  const void *msg,
572  unsigned int msg_len,
573  int endian_conversion_required)
574 {
575  const struct qb_ipc_request_header *header;
576  int32_t service;
577  int32_t fn_id;
578  uint32_t id;
579 
580  header = msg;
581  if (endian_conversion_required) {
582  id = swab32 (header->id);
583  } else {
584  id = header->id;
585  }
586 
587  /*
588  * Call the proper executive handler
589  */
590  service = id >> 16;
591  fn_id = id & 0xffff;
592 
593  if (!corosync_service[service]) {
594  return;
595  }
596  if (fn_id >= corosync_service[service]->exec_engine_count) {
597  log_printf(LOGSYS_LEVEL_WARNING, "discarded unknown message %d for service %d (max id %d)",
598  fn_id, service, corosync_service[service]->exec_engine_count);
599  return;
600  }
601 
602  icmap_fast_inc(service_stats_rx[service][fn_id]);
603 
604  if (endian_conversion_required) {
605  assert(corosync_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
607  ((void *)msg);
608  }
609 
611  (msg, nodeid);
612 }
613 
615  const struct iovec *iovec,
616  unsigned int iov_len,
617  unsigned int guarantee)
618 {
619  const struct qb_ipc_request_header *req = iovec->iov_base;
620  int32_t service;
621  int32_t fn_id;
622 
623  service = req->id >> 16;
624  fn_id = req->id & 0xffff;
625 
626  if (corosync_service[service]) {
627  icmap_fast_inc(service_stats_tx[service][fn_id]);
628  }
629 
630  return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
631 }
632 
633 static void corosync_ring_id_create_or_load (
634  struct memb_ring_id *memb_ring_id,
635  unsigned int nodeid)
636 {
637  int fd;
638  int res = 0;
639  char filename[PATH_MAX];
640 
641  snprintf (filename, sizeof(filename), "%s/ringid_%u",
642  get_state_dir(), nodeid);
643  fd = open (filename, O_RDONLY, 0700);
644  /*
645  * If file can be opened and read, read the ring id
646  */
647  if (fd != -1) {
648  res = read (fd, &memb_ring_id->seq, sizeof (uint64_t));
649  close (fd);
650  }
651  /*
652  * If file could not be opened or read, create a new ring id
653  */
654  if ((fd == -1) || (res != sizeof (uint64_t))) {
655  memb_ring_id->seq = 0;
656  umask(0);
657  fd = open (filename, O_CREAT|O_RDWR, 0700);
658  if (fd != -1) {
659  res = write (fd, &memb_ring_id->seq, sizeof (uint64_t));
660  close (fd);
661  if (res == -1) {
663  "Couldn't write ringid file '%s'", filename);
664 
666  }
667  } else {
669  "Couldn't create ringid file '%s'", filename);
670 
672  }
673  }
674 
675  memb_ring_id->rep = nodeid;
676 }
677 
678 static void corosync_ring_id_store (
679  const struct memb_ring_id *memb_ring_id,
680  unsigned int nodeid)
681 {
682  char filename[PATH_MAX];
683  int fd;
684  int res;
685 
686  snprintf (filename, sizeof(filename), "%s/ringid_%u",
687  get_state_dir(), nodeid);
688 
689  fd = open (filename, O_WRONLY, 0700);
690  if (fd == -1) {
691  fd = open (filename, O_CREAT|O_RDWR, 0700);
692  }
693  if (fd == -1) {
695  "Couldn't store new ring id " CS_PRI_RING_ID_SEQ " to stable storage",
696  memb_ring_id->seq);
697 
699  }
701  "Storing new sequence id for ring " CS_PRI_RING_ID_SEQ, memb_ring_id->seq);
702  res = write (fd, &memb_ring_id->seq, sizeof(memb_ring_id->seq));
703  close (fd);
704  if (res != sizeof(memb_ring_id->seq)) {
706  "Couldn't store new ring id " CS_PRI_RING_ID_SEQ " to stable storage",
707  memb_ring_id->seq);
708 
710  }
711 }
712 
713 static qb_loop_timer_handle recheck_the_q_level_timer;
715 {
716  totempg_check_q_level(corosync_group_handle);
718  qb_loop_timer_add(cs_poll_handle_get(), QB_LOOP_MED, 1*QB_TIME_NS_IN_MSEC,
719  NULL, corosync_recheck_the_q_level, &recheck_the_q_level_timer);
720  }
721 }
722 
725 };
726 
727 
729  unsigned int service,
730  unsigned int id,
731  const void *msg,
732  void *sending_allowed_private_data)
733 {
735  (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
736  struct iovec reserve_iovec;
737  struct qb_ipc_request_header *header = (struct qb_ipc_request_header *)msg;
738  int sending_allowed;
739 
740  reserve_iovec.iov_base = (char *)header;
741  reserve_iovec.iov_len = header->size;
742 
744  corosync_group_handle,
745  &reserve_iovec, 1);
746  if (pd->reserved_msgs == -1) {
747  return -EINVAL;
748  }
749 
750  /* Message ID out of range */
751  if (id >= corosync_service[service]->lib_engine_count) {
752  return -EINVAL;
753  }
754 
755  sending_allowed = QB_FALSE;
756  if (corosync_quorum_is_quorate() == 1 ||
757  corosync_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) {
758  // we are quorate
759  // now check flow control
760  if (corosync_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) {
761  sending_allowed = QB_TRUE;
762  } else if (pd->reserved_msgs && sync_in_process == 0) {
763  sending_allowed = QB_TRUE;
764  } else if (pd->reserved_msgs == 0) {
765  return -ENOBUFS;
766  } else /* (sync_in_process) */ {
767  return -EINPROGRESS;
768  }
769  } else {
770  return -EHOSTUNREACH;
771  }
772 
773  return (sending_allowed);
774 }
775 
776 void corosync_sending_allowed_release (void *sending_allowed_private_data)
777 {
779  (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
780 
781  if (pd->reserved_msgs == -1) {
782  return;
783  }
785 }
786 
788 {
789  int ret = 0;
790 
791  assert (source != NULL);
792  if (source->nodeid == totempg_my_nodeid_get ()) {
793  ret = 1;
794  }
795  return ret;
796 }
797 
799  mar_message_source_t *source,
800  void *conn)
801 {
802  assert ((source != NULL) && (conn != NULL));
803  memset (source, 0, sizeof (mar_message_source_t));
804  source->nodeid = totempg_my_nodeid_get ();
805  source->conn = conn;
806 }
807 
810  qb_loop_timer_handle handle;
811  unsigned long long tv_prev;
812  unsigned long long max_tv_diff;
813 };
814 
815 static void timer_function_scheduler_timeout (void *data)
816 {
817  struct scheduler_pause_timeout_data *timeout_data = (struct scheduler_pause_timeout_data *)data;
818  unsigned long long tv_current;
819  unsigned long long tv_diff;
820 
821  tv_current = qb_util_nano_current_get ();
822 
823  if (timeout_data->tv_prev == 0) {
824  /*
825  * Initial call -> just pretent everything is ok
826  */
827  timeout_data->tv_prev = tv_current;
828  timeout_data->max_tv_diff = 0;
829  }
830 
831  tv_diff = tv_current - timeout_data->tv_prev;
832  timeout_data->tv_prev = tv_current;
833 
834  if (tv_diff > timeout_data->max_tv_diff) {
835  log_printf (LOGSYS_LEVEL_WARNING, "Corosync main process was not scheduled for %0.4f ms "
836  "(threshold is %0.4f ms). Consider token timeout increase.",
837  (float)tv_diff / QB_TIME_NS_IN_MSEC, (float)timeout_data->max_tv_diff / QB_TIME_NS_IN_MSEC);
838  }
839 
840  /*
841  * Set next threshold, because token_timeout can change
842  */
843  timeout_data->max_tv_diff = timeout_data->totem_config->token_timeout * QB_TIME_NS_IN_MSEC * 0.8;
844  qb_loop_timer_add (corosync_poll_handle,
845  QB_LOOP_MED,
846  timeout_data->totem_config->token_timeout * QB_TIME_NS_IN_MSEC / 3,
847  timeout_data,
848  timer_function_scheduler_timeout,
849  &timeout_data->handle);
850 }
851 
852 
853 static int corosync_set_rr_scheduler (void)
854 {
855  int ret_val = 0;
856 
857 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
858  int res;
859 
860  sched_priority = sched_get_priority_max (SCHED_RR);
861  if (sched_priority != -1) {
862  global_sched_param.sched_priority = sched_priority;
863  res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
864  if (res == -1) {
866  "Could not set SCHED_RR at priority %d",
867  global_sched_param.sched_priority);
868 
869  global_sched_param.sched_priority = 0;
870 #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
871  qb_log_thread_priority_set (SCHED_OTHER, 0);
872 #endif
873  ret_val = -1;
874  } else {
875 
876  /*
877  * Turn on SCHED_RR in logsys system
878  */
879 #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
880  res = qb_log_thread_priority_set (SCHED_RR, sched_priority);
881 #else
882  res = -1;
883 #endif
884  if (res == -1) {
886  "Could not set logsys thread priority."
887  " Can't continue because of priority inversions.");
889  }
890  }
891  } else {
893  "Could not get maximum scheduler priority");
894  sched_priority = 0;
895  ret_val = -1;
896  }
897 #else
899  "The Platform is missing process priority setting features. Leaving at default.");
900  ret_val = -1;
901 #endif
902 
903  return (ret_val);
904 }
905 
906 
907 /* The basename man page contains scary warnings about
908  thread-safety and portability, hence this */
909 static const char *corosync_basename(const char *file_name)
910 {
911  char *base;
912  base = strrchr (file_name, '/');
913  if (base) {
914  return base + 1;
915  }
916 
917  return file_name;
918 }
919 
920 static void
921 _logsys_log_printf(int level, int subsys,
922  const char *function_name,
923  const char *file_name,
924  int file_line,
925  const char *format,
926  ...) __attribute__((format(printf, 6, 7)));
927 
928 static void
929 _logsys_log_printf(int level, int subsys,
930  const char *function_name,
931  const char *file_name,
932  int file_line,
933  const char *format, ...)
934 {
935  va_list ap;
936 
937  va_start(ap, format);
938  qb_log_from_external_source_va(function_name, corosync_basename(file_name),
939  format, level, file_line,
940  subsys, ap);
941  va_end(ap);
942 }
943 
944 static void fplay_key_change_notify_fn (
945  int32_t event,
946  const char *key_name,
947  struct icmap_notify_value new_val,
948  struct icmap_notify_value old_val,
949  void *user_data)
950 {
951  if (strcmp(key_name, "runtime.blackbox.dump_flight_data") == 0) {
952  fprintf(stderr,"Writetofile\n");
953  corosync_blackbox_write_to_file ();
954  }
955  if (strcmp(key_name, "runtime.blackbox.dump_state") == 0) {
956  fprintf(stderr,"statefump\n");
958  }
959 }
960 
961 static void corosync_fplay_control_init (void)
962 {
963  icmap_track_t track = NULL;
964 
965  icmap_set_string("runtime.blackbox.dump_flight_data", "no");
966  icmap_set_string("runtime.blackbox.dump_state", "no");
967 
968  icmap_track_add("runtime.blackbox.dump_flight_data",
970  fplay_key_change_notify_fn,
971  NULL, &track);
972  icmap_track_add("runtime.blackbox.dump_state",
974  fplay_key_change_notify_fn,
975  NULL, &track);
976 }
977 
978 static void force_gather_notify_fn(
979  int32_t event,
980  const char *key_name,
981  struct icmap_notify_value new_val,
982  struct icmap_notify_value old_val,
983  void *user_data)
984 {
985  char *key_val;
986 
987  if (icmap_get_string(key_name, &key_val) == CS_OK && strcmp(key_val, "no") == 0)
988  goto out;
989 
990  icmap_set_string("runtime.force_gather", "no");
991 
992  if (strcmp(key_name, "runtime.force_gather") == 0) {
993  log_printf(LOGSYS_LEVEL_ERROR, "Forcing into GATHER state\n");
995  }
996 
997 out:
998  free(key_val);
999 }
1000 
1001 static void corosync_force_gather_init (void)
1002 {
1003  icmap_track_t track = NULL;
1004 
1005  icmap_set_string("runtime.force_gather", "no");
1006 
1007  icmap_track_add("runtime.force_gather",
1009  force_gather_notify_fn,
1010  NULL, &track);
1011 }
1012 
1013 /*
1014  * Set RO flag for keys, which ether doesn't make sense to change by user (statistic)
1015  * or which when changed are not reflected by runtime (totem.crypto_cipher, ...).
1016  *
1017  * Also some RO keys cannot be determined in this stage, so they are set later in
1018  * other functions (like nodelist.local_node_pos, ...)
1019  */
1020 static void set_icmap_ro_keys_flag (void)
1021 {
1022  /*
1023  * Set RO flag for all keys of internal configuration and runtime statistics
1024  */
1025  icmap_set_ro_access("internal_configuration.", CS_TRUE, CS_TRUE);
1026  icmap_set_ro_access("runtime.services.", CS_TRUE, CS_TRUE);
1027  icmap_set_ro_access("runtime.config.", CS_TRUE, CS_TRUE);
1028  icmap_set_ro_access("runtime.totem.", CS_TRUE, CS_TRUE);
1029  icmap_set_ro_access("uidgid.config.", CS_TRUE, CS_TRUE);
1030  icmap_set_ro_access("system.", CS_TRUE, CS_TRUE);
1031  icmap_set_ro_access("nodelist.", CS_TRUE, CS_TRUE);
1032 
1033  /*
1034  * Set RO flag for constrete keys of configuration which can't be changed
1035  * during runtime
1036  */
1037  icmap_set_ro_access("totem.crypto_cipher", CS_FALSE, CS_TRUE);
1038  icmap_set_ro_access("totem.crypto_hash", CS_FALSE, CS_TRUE);
1039  icmap_set_ro_access("totem.keyfile", CS_FALSE, CS_TRUE);
1040  icmap_set_ro_access("totem.key", CS_FALSE, CS_TRUE);
1041  icmap_set_ro_access("totem.secauth", CS_FALSE, CS_TRUE);
1042  icmap_set_ro_access("totem.ip_version", CS_FALSE, CS_TRUE);
1043  icmap_set_ro_access("totem.rrp_mode", CS_FALSE, CS_TRUE);
1044  icmap_set_ro_access("totem.transport", CS_FALSE, CS_TRUE);
1045  icmap_set_ro_access("totem.cluster_name", CS_FALSE, CS_TRUE);
1046  icmap_set_ro_access("totem.netmtu", CS_FALSE, CS_TRUE);
1047  icmap_set_ro_access("totem.threads", CS_FALSE, CS_TRUE);
1048  icmap_set_ro_access("totem.version", CS_FALSE, CS_TRUE);
1049  icmap_set_ro_access("totem.nodeid", CS_FALSE, CS_TRUE);
1050  icmap_set_ro_access("totem.clear_node_high_bit", CS_FALSE, CS_TRUE);
1051  icmap_set_ro_access("config.reload_in_progress", CS_FALSE, CS_TRUE);
1052  icmap_set_ro_access("config.totemconfig_reload_in_progress", CS_FALSE, CS_TRUE);
1053 }
1054 
1055 static void main_service_ready (void)
1056 {
1057  int res;
1058 
1059  /*
1060  * This must occur after totempg is initialized because "this_ip" must be set
1061  */
1063  if (res == -1) {
1064  log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services");
1066  }
1067  cs_ipcs_init();
1068  corosync_totem_stats_init ();
1069  corosync_fplay_control_init ();
1070  corosync_force_gather_init ();
1071 
1072  sync_init (
1073  corosync_sync_callbacks_retrieve,
1074  corosync_sync_completed);
1075 }
1076 
1077 static enum e_corosync_done corosync_flock (const char *lockfile, pid_t pid)
1078 {
1079  struct flock lock;
1080  enum e_corosync_done err;
1081  char pid_s[17];
1082  int fd_flag;
1083  int lf;
1084 
1085  err = COROSYNC_DONE_EXIT;
1086 
1087  lf = open (lockfile, O_WRONLY | O_CREAT, 0640);
1088  if (lf == -1) {
1089  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create lock file.");
1090  return (COROSYNC_DONE_ACQUIRE_LOCK);
1091  }
1092 
1093 retry_fcntl:
1094  lock.l_type = F_WRLCK;
1095  lock.l_start = 0;
1096  lock.l_whence = SEEK_SET;
1097  lock.l_len = 0;
1098  if (fcntl (lf, F_SETLK, &lock) == -1) {
1099  switch (errno) {
1100  case EINTR:
1101  goto retry_fcntl;
1102  break;
1103  case EAGAIN:
1104  case EACCES:
1105  log_printf (LOGSYS_LEVEL_ERROR, "Another Corosync instance is already running.");
1107  goto error_close;
1108  break;
1109  default:
1110  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't acquire lock. Error was %s",
1111  strerror(errno));
1113  goto error_close;
1114  break;
1115  }
1116  }
1117 
1118  if (ftruncate (lf, 0) == -1) {
1119  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't truncate lock file. Error was %s",
1120  strerror (errno));
1122  goto error_close_unlink;
1123  }
1124 
1125  memset (pid_s, 0, sizeof (pid_s));
1126  snprintf (pid_s, sizeof (pid_s) - 1, "%u\n", pid);
1127 
1128 retry_write:
1129  if (write (lf, pid_s, strlen (pid_s)) != strlen (pid_s)) {
1130  if (errno == EINTR) {
1131  goto retry_write;
1132  } else {
1133  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't write pid to lock file. "
1134  "Error was %s", strerror (errno));
1136  goto error_close_unlink;
1137  }
1138  }
1139 
1140  if ((fd_flag = fcntl (lf, F_GETFD, 0)) == -1) {
1141  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't get close-on-exec flag from lock file. "
1142  "Error was %s", strerror (errno));
1144  goto error_close_unlink;
1145  }
1146  fd_flag |= FD_CLOEXEC;
1147  if (fcntl (lf, F_SETFD, fd_flag) == -1) {
1148  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't set close-on-exec flag to lock file. "
1149  "Error was %s", strerror (errno));
1151  goto error_close_unlink;
1152  }
1153 
1154  return (err);
1155 
1156 error_close_unlink:
1157  unlink (lockfile);
1158 error_close:
1159  close (lf);
1160 
1161  return (err);
1162 }
1163 
1164 static int corosync_move_to_root_cgroup(void) {
1165  FILE *f;
1166  int res = -1;
1167 
1168  /*
1169  * /sys/fs/cgroup is hardcoded, because most of Linux distributions are now
1170  * using systemd and systemd uses hardcoded path of cgroup mount point.
1171  *
1172  * This feature is expected to be removed as soon as systemd gets support
1173  * for managing RT configuration.
1174  */
1175  f = fopen("/sys/fs/cgroup/cpu/cpu.rt_runtime_us", "rt");
1176  if (f == NULL) {
1177  log_printf(LOGSYS_LEVEL_DEBUG, "cpu.rt_runtime_us doesn't exists -> "
1178  "system without cgroup or with disabled CONFIG_RT_GROUP_SCHED");
1179 
1180  res = 0;
1181  goto exit_res;
1182  }
1183  (void)fclose(f);
1184 
1185  f = fopen("/sys/fs/cgroup/cpu/tasks", "w");
1186  if (f == NULL) {
1187  log_printf(LOGSYS_LEVEL_WARNING, "Can't open cgroups tasks file for writing");
1188 
1189  goto exit_res;
1190  }
1191 
1192  if (fprintf(f, "%jd\n", (intmax_t)getpid()) <= 0) {
1193  log_printf(LOGSYS_LEVEL_WARNING, "Can't write corosync pid into cgroups tasks file");
1194 
1195  goto close_and_exit_res;
1196  }
1197 
1198 close_and_exit_res:
1199  if (fclose(f) != 0) {
1200  log_printf(LOGSYS_LEVEL_WARNING, "Can't close cgroups tasks file");
1201 
1202  goto exit_res;
1203  }
1204 
1205 exit_res:
1206  return (res);
1207 }
1208 
1209 
1210 int main (int argc, char **argv, char **envp)
1211 {
1212  const char *error_string;
1213  struct totem_config totem_config;
1214  int res, ch;
1215  int background, sched_rr, prio, testonly, move_to_root_cgroup;
1216  struct stat stat_out;
1217  enum e_corosync_done flock_err;
1218  uint64_t totem_config_warnings;
1219  struct scheduler_pause_timeout_data scheduler_pause_timeout_data;
1220  long int tmpli;
1221  char *ep;
1222  char *tmp_str;
1223  int log_subsys_id_totem;
1224 
1225  /* default configuration
1226  */
1227  background = 1;
1228  testonly = 0;
1229 
1230  while ((ch = getopt (argc, argv, "c:ftv")) != EOF) {
1231 
1232  switch (ch) {
1233  case 'c':
1234  res = snprintf(corosync_config_file, sizeof(corosync_config_file), "%s", optarg);
1235  if (res >= sizeof(corosync_config_file)) {
1236  fprintf (stderr, "Config file path too long.\n");
1237  syslog (LOGSYS_LEVEL_ERROR, "Config file path too long.");
1238 
1240  return EXIT_FAILURE;
1241  }
1242  break;
1243  case 'f':
1244  background = 0;
1245  break;
1246  case 't':
1247  testonly = 1;
1248  break;
1249  case 'v':
1250  printf ("Corosync Cluster Engine, version '%s'\n", VERSION);
1251  printf ("Copyright (c) 2006-2018 Red Hat, Inc.\n");
1253  return EXIT_SUCCESS;
1254 
1255  break;
1256  default:
1257  fprintf(stderr, \
1258  "usage:\n"\
1259  " -c : Corosync config file path.\n"\
1260  " -f : Start application in foreground.\n"\
1261  " -t : Test configuration and exit.\n"\
1262  " -v : Display version and SVN revision of Corosync and exit.\n");
1264  return EXIT_FAILURE;
1265  }
1266  }
1267 
1268 
1269  /*
1270  * Other signals are registered later via qb_loop_signal_add
1271  */
1272  (void)signal (SIGSEGV, sigsegv_handler);
1273  (void)signal (SIGABRT, sigsegv_handler);
1274 #if MSG_NOSIGNAL != 0
1275  (void)signal (SIGPIPE, SIG_IGN);
1276 #endif
1277 
1278  if (icmap_init() != CS_OK) {
1279  fprintf (stderr, "Corosync Executive couldn't initialize configuration component.\n");
1280  syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize configuration component.");
1282  }
1283  set_icmap_ro_keys_flag();
1284 
1285  /*
1286  * Initialize the corosync_api_v1 definition
1287  */
1288  api = apidef_get ();
1289 
1290  res = coroparse_configparse(icmap_get_global_map(), &error_string);
1291  if (res == -1) {
1292  /*
1293  * Logsys can't log properly at this early stage, and we need to get this message out
1294  *
1295  */
1296  fprintf (stderr, "%s\n", error_string);
1297  syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
1299  }
1300 
1301  if (stats_map_init(api) != CS_OK) {
1302  fprintf (stderr, "Corosync Executive couldn't initialize statistics component.\n");
1303  syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize statistics component.");
1305  }
1306 
1307  res = corosync_log_config_read (&error_string);
1308  if (res == -1) {
1309  /*
1310  * if we are here, we _must_ flush the logsys queue
1311  * and try to inform that we couldn't read the config.
1312  * this is a desperate attempt before certain death
1313  * and there is no guarantee that we can print to stderr
1314  * nor that logsys is sending the messages where we expect.
1315  */
1316  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1317  fprintf(stderr, "%s", error_string);
1318  syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
1320  }
1321 
1322  if (!testonly) {
1323  log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine %s starting up", VERSION);
1324  log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "");
1325  }
1326 
1327  /*
1328  * Create totem logsys subsys before totem_config_read so log functions can be used
1329  */
1330  log_subsys_id_totem = _logsys_subsys_create("TOTEM", "totem,"
1331  "totemip.c,totemconfig.c,totemcrypto.c,totemsrp.c,"
1332  "totempg.c,totemudp.c,totemudpu.c,totemnet.c,totemknet.c");
1333 
1334  /*
1335  * Make sure required directory is present
1336  */
1337  res = stat (get_state_dir(), &stat_out);
1338  if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
1339  log_printf (LOGSYS_LEVEL_ERROR, "State directory %s not present. Please create it.", get_state_dir());
1341  }
1342 
1343  res = chdir(get_state_dir());
1344  if (res == -1) {
1345  log_printf (LOGSYS_LEVEL_ERROR, "Cannot chdir to state directory %s. "
1346  "Please make sure it has correct context and rights.", get_state_dir());
1348  }
1349 
1350  res = totem_config_read (&totem_config, &error_string, &totem_config_warnings);
1351  if (res == -1) {
1352  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1354  }
1355 
1356  if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_IGNORED) {
1357  log_printf (LOGSYS_LEVEL_WARNING, "member section is used together with nodelist. Members ignored.");
1358  }
1359 
1360  if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED) {
1361  log_printf (LOGSYS_LEVEL_WARNING, "member section is deprecated.");
1362  }
1363 
1364  if (totem_config_warnings & TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED) {
1365  log_printf (LOGSYS_LEVEL_WARNING, "nodeid appears both in totem section and nodelist. Nodelist one is used.");
1366  }
1367 
1368  if (totem_config_warnings & TOTEM_CONFIG_BINDNETADDR_NODELIST_SET) {
1369  log_printf (LOGSYS_LEVEL_WARNING, "interface section bindnetaddr is used together with nodelist. "
1370  "Nodelist one is going to be used.");
1371  }
1372 
1373  if (totem_config_warnings != 0) {
1374  log_printf (LOGSYS_LEVEL_WARNING, "Please migrate config file to nodelist.");
1375  }
1376 
1377  res = totem_config_keyread (&totem_config, &error_string);
1378  if (res == -1) {
1379  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1381  }
1382 
1383  res = totem_config_validate (&totem_config, &error_string);
1384  if (res == -1) {
1385  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1387  }
1388 
1389  if (testonly) {
1391  }
1392 
1393 
1394  move_to_root_cgroup = 1;
1395  if (icmap_get_string("system.move_to_root_cgroup", &tmp_str) == CS_OK) {
1396  if (strcmp(tmp_str, "yes") != 0) {
1397  move_to_root_cgroup = 0;
1398  }
1399  free(tmp_str);
1400  }
1401 
1402  /*
1403  * Try to move corosync into root cpu cgroup. Failure is not fatal and
1404  * error is deliberately ignored.
1405  */
1406  if (move_to_root_cgroup) {
1407  (void)corosync_move_to_root_cgroup();
1408  }
1409 
1410  sched_rr = 1;
1411  if (icmap_get_string("system.sched_rr", &tmp_str) == CS_OK) {
1412  if (strcmp(tmp_str, "yes") != 0) {
1413  sched_rr = 0;
1414  }
1415  free(tmp_str);
1416  }
1417 
1418  prio = 0;
1419  if (icmap_get_string("system.priority", &tmp_str) == CS_OK) {
1420  if (strcmp(tmp_str, "max") == 0) {
1421  prio = INT_MIN;
1422  } else if (strcmp(tmp_str, "min") == 0) {
1423  prio = INT_MAX;
1424  } else {
1425  errno = 0;
1426 
1427  tmpli = strtol(tmp_str, &ep, 10);
1428  if (errno != 0 || *ep != '\0' || tmpli > INT_MAX || tmpli < INT_MIN) {
1429  log_printf (LOGSYS_LEVEL_ERROR, "Priority value %s is invalid", tmp_str);
1431  }
1432 
1433  prio = tmpli;
1434  }
1435 
1436  free(tmp_str);
1437  }
1438 
1439  /*
1440  * Set round robin realtime scheduling with priority 99
1441  */
1442  if (sched_rr) {
1443  if (corosync_set_rr_scheduler () != 0) {
1444  prio = INT_MIN;
1445  } else {
1446  prio = 0;
1447  }
1448  }
1449 
1450  if (prio != 0) {
1451  if (setpriority(PRIO_PGRP, 0, prio) != 0) {
1453  "Could not set priority %d", prio);
1454  }
1455  }
1456 
1457  totem_config.totem_memb_ring_id_create_or_load = corosync_ring_id_create_or_load;
1458  totem_config.totem_memb_ring_id_store = corosync_ring_id_store;
1459 
1461  totem_config.totem_logging_configuration.log_subsys_id = log_subsys_id_totem;
1462 
1469  totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
1470 
1472 
1473  /*
1474  * Now we are fully initialized.
1475  */
1476  if (background) {
1478 
1479  corosync_tty_detach ();
1480 
1482 
1483  log_printf (LOGSYS_LEVEL_DEBUG, "Corosync TTY detached");
1484  }
1485 
1486  /*
1487  * Lock all memory to avoid page faults which may interrupt
1488  * application healthchecking
1489  */
1490  corosync_mlockall ();
1491 
1492  corosync_poll_handle = qb_loop_create ();
1493 
1494  memset(&scheduler_pause_timeout_data, 0, sizeof(scheduler_pause_timeout_data));
1495  scheduler_pause_timeout_data.totem_config = &totem_config;
1496  timer_function_scheduler_timeout (&scheduler_pause_timeout_data);
1497 
1498  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_LOW,
1499  SIGUSR2, NULL, sig_diag_handler, NULL);
1500  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1501  SIGINT, NULL, sig_exit_handler, NULL);
1502  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1503  SIGQUIT, NULL, sig_exit_handler, NULL);
1504  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1505  SIGTERM, NULL, sig_exit_handler, NULL);
1506 
1507  if (logsys_thread_start() != 0) {
1508  log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize log thread");
1510  }
1511 
1512  if ((flock_err = corosync_flock (corosync_lock_file, getpid ())) != COROSYNC_DONE_EXIT) {
1513  corosync_exit_error (flock_err);
1514  }
1515 
1516  /*
1517  * if totempg_initialize doesn't have root priveleges, it cannot
1518  * bind to a specific interface. This only matters if
1519  * there is more then one interface in a system, so
1520  * in this case, only a warning is printed
1521  */
1522  /*
1523  * Join multicast group and setup delivery
1524  * and configuration change functions
1525  */
1526  if (totempg_initialize (
1527  corosync_poll_handle,
1528  &totem_config) != 0) {
1529 
1530  log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize TOTEM layer");
1532  }
1533 
1535  main_service_ready);
1536 
1538  &corosync_group_handle,
1539  deliver_fn,
1540  confchg_fn);
1541 
1543  corosync_group_handle,
1544  &corosync_group,
1545  1);
1546 
1547  /*
1548  * Drop root privleges to user 'corosync'
1549  * TODO: Don't really need full root capabilities;
1550  * needed capabilities are:
1551  * CAP_NET_RAW (bindtodevice)
1552  * CAP_SYS_NICE (setscheduler)
1553  * CAP_IPC_LOCK (mlockall)
1554  */
1555  priv_drop ();
1556 
1557  schedwrk_init (
1558  serialize_lock,
1559  serialize_unlock);
1560 
1561  /*
1562  * Start main processing loop
1563  */
1564  qb_loop_run (corosync_poll_handle);
1565 
1566  /*
1567  * Exit was requested
1568  */
1569  totempg_finalize ();
1570 
1571  /*
1572  * free the loop resources
1573  */
1574  qb_loop_destroy (corosync_poll_handle);
1575 
1576  /*
1577  * free up the icmap
1578  */
1579 
1580  /*
1581  * Remove pid lock file
1582  */
1583  unlink (corosync_lock_file);
1584 
1586 
1587  return EXIT_SUCCESS;
1588 }
#define CS_TRUE
Definition: corotypes.h:54
const char * name
Definition: coroapi.h:491
int32_t cs_ipcs_q_level_get(void)
Definition: ipc_glue.c:601
unsigned int corosync_service_defaults_link_and_init(struct corosync_api_v1 *corosync_api)
Load all of the default services.
Definition: service.c:339
int corosync_quorum_is_quorate(void)
Definition: exec/quorum.c:65
void(* timer_delete)(corosync_timer_handle_t timer_handle)
Definition: coroapi.h:241
void totempg_finalize(void)
Definition: totempg.c:855
Totem Single Ring Protocol.
int(* timer_add_duration)(unsigned long long nanoseconds_in_future, void *data, void(*timer_nf)(void *data), corosync_timer_handle_t *handle)
Definition: coroapi.h:229
#define LOGSYS_LEVEL_INFO
Definition: logsys.h:75
#define LOGSYS_LEVEL_TRACE
Definition: logsys.h:77
void totempg_trans_ack(void)
Definition: totempg.c:1577
struct totem_config * totem_config
Definition: main.c:809
void *(* totem_get_stats)(void)
Definition: coroapi.h:435
#define PACKAGE_FEATURES
Definition: config.h:376
void logsys_blackbox_postfork(void)
Definition: logsys.c:884
#define CS_FALSE
Definition: corotypes.h:53
void sync_start(const unsigned int *member_list, size_t member_list_entries, const struct memb_ring_id *ring_id)
Definition: sync.c:505
qb_loop_timer_handle handle
Definition: main.c:810
void sync_abort(void)
Definition: sync.c:528
void(* exec_dump_fn)(void)
Definition: coroapi.h:501
void(* sync_abort)(void)
Definition: sync.h:47
totemsrp_token_stats_t token[TOTEM_TOKEN_STATS_MAX]
Definition: totemstats.h:90
Totem Single Ring Protocol.
cs_error_t stats_map_init(const struct corosync_api_v1 *corosync_api)
Definition: stats.c:261
void(* sync_init)(const unsigned int *trans_list, size_t trans_list_entries, const unsigned int *member_list, size_t member_list_entries, const struct memb_ring_id *ring_id)
Definition: sync.h:39
int sync_init(int(*sync_callbacks_retrieve)(int service_id, struct sync_callbacks *callbacks), void(*synchronization_completed)(void))
Definition: sync.c:158
void(* sync_init)(const unsigned int *trans_list, size_t trans_list_entries, const unsigned int *member_list, size_t member_list_entries, const struct memb_ring_id *ring_id)
Definition: coroapi.h:515
int coroparse_configparse(icmap_map_t config_map, const char **error_string)
Definition: coroparse.c:258
qb_loop_t * cs_poll_handle_get(void)
Definition: main.c:170
void corosync_recheck_the_q_level(void *data)
Definition: main.c:714
#define corosync_exit_error(err)
Definition: exec/util.h:72
void stats_trigger_trackers()
Definition: stats.c:452
int totempg_groups_joined_release(int msg_count)
Definition: totempg.c:1344
#define VERSION
Definition: config.h:436
totem_configuration_type
The totem_configuration_type enum.
Definition: coroapi.h:132
void(* totem_memb_ring_id_create_or_load)(struct memb_ring_id *memb_ring_id, unsigned int nodeid)
Definition: totem.h:238
#define TOTEM_TOKEN_STATS_MAX
Definition: totemstats.h:89
uint32_t avg_token_workload
Definition: totemstats.h:84
#define LOCALSTATEDIR
Definition: config.h:361
LOGSYS_DECLARE_SUBSYS("MAIN")
int guarantee
Definition: totemsrp.c:266
#define COROSYSCONFDIR
Definition: config.h:11
void(* sync_activate)(void)
Definition: coroapi.h:522
void schedwrk_init(void(*serialize_lock_fn)(void), void(*serialize_unlock_fn)(void))
Definition: schedwrk.c:83
cs_error_t icmap_set_string(const char *key_name, const char *value)
Definition: icmap.c:625
void(* confchg_fn)(enum totem_configuration_type configuration_type, const unsigned int *member_list, size_t member_list_entries, const unsigned int *left_list, size_t left_list_entries, const unsigned int *joined_list, size_t joined_list_entries, const struct memb_ring_id *ring_id)
Definition: coroapi.h:509
int cs_poll_dispatch_add(qb_loop_t *handle, int fd, int events, void *data, int(*dispatch_fn)(int fd, int revents, void *data))
Definition: main.c:175
int totempg_groups_initialize(void **instance, void(*deliver_fn)(unsigned int nodeid, const void *msg, unsigned int msg_len, int endian_conversion_required), void(*confchg_fn)(enum totem_configuration_type configuration_type, const unsigned int *member_list, size_t member_list_entries, const unsigned int *left_list, size_t left_list_entries, const unsigned int *joined_list, size_t joined_list_entries, const struct memb_ring_id *ring_id))
Initialize a groups instance.
Definition: totempg.c:1132
#define CS_PRI_RING_ID_SEQ
Definition: corotypes.h:60
#define RLIMIT_MEMLOCK
icmap_map_t icmap_get_global_map(void)
Return global icmap.
Definition: icmap.c:262
unsigned long long max_tv_diff
Definition: main.c:812
void totempg_force_gather(void)
Definition: totempg.c:1582
struct corosync_service_engine * corosync_service[SERVICES_COUNT_MAX]
Definition: service.c:110
int _logsys_subsys_create(const char *subsys, const char *filename)
_logsys_subsys_create
Definition: logsys.c:433
#define LOGSYS_PERROR(err_num, level, fmt, args...)
The LOGSYS_PERROR macro.
Definition: logsys.h:317
cs_error_t icmap_inc(const char *key_name)
Increase stored value by one.
Definition: icmap.c:1041
#define log_printf(level, format, args...)
Definition: logsys.h:323
#define MAX_NO_CONT_SENDMSG_FAILURES
Definition: totem.h:79
void(* sync_abort)(void)
Definition: coroapi.h:523
void(* exec_handler_fn)(const void *msg, unsigned int nodeid)
Definition: coroapi.h:476
void cs_ipcs_sync_state_changed(int32_t sync_in_process)
Definition: ipc_glue.c:664
int corosync_sending_allowed(unsigned int service, unsigned int id, const void *msg, void *sending_allowed_private_data)
Definition: main.c:728
void logsys_blackbox_prefork(void)
Definition: logsys.c:878
void icmap_fini(void)
Finalize global icmap.
Definition: icmap.c:245
int main_mcast(const struct iovec *iovec, unsigned int iov_len, unsigned int guarantee)
Definition: main.c:614
void cs_ipc_allow_connections(int32_t allow)
Definition: ipc_glue.c:159
void corosync_service_unlink_all(struct corosync_api_v1 *api, void(*unlink_all_complete)(void))
Unlink and exit all corosync services.
Definition: service.c:394
void(* sync_activate)(void)
Definition: sync.h:46
LOGSYS_DECLARE_SYSTEM("corosync", LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_OUTPUT_SYSLOG, LOG_DAEMON, LOG_EMERG)
#define ICMAP_TRACK_DELETE
Definition: icmap.h:77
void(*) in log_level_security)
Definition: totem.h:106
void totempg_service_ready_register(void(*totem_service_ready)(void))
Definition: totempg.c:1532
#define ICMAP_KEYNAME_MAXLEN
Maximum length of key in icmap.
Definition: icmap.h:48
#define MAX_NO_CONT_GATHER
Maximum number of continuous gather states.
Definition: totem.h:75
int totem_config_keyread(struct totem_config *totem_config, const char **error_string)
Definition: totemconfig.c:2103
const char * name
Definition: sync.h:48
#define LOGSYS_LEVEL_WARNING
Definition: logsys.h:73
#define ICMAP_TRACK_MODIFY
Definition: icmap.h:78
struct corosync_exec_handler * exec_engine
Definition: coroapi.h:506
unsigned int totempg_my_nodeid_get(void)
Definition: totempg.c:1523
totemsrp_stats_t * srp
Definition: totemstats.h:96
cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
Definition: icmap.c:595
uint64_t time_since_token_last_received
Definition: totemstats.h:80
const char * corosync_get_config_file(void)
Definition: main.c:204
void * user_data
Definition: sam.c:127
int logsys_thread_start(void)
logsys_thread_start
Definition: logsys.c:842
int(* sync_process)(void)
Definition: coroapi.h:521
int totem_config_validate(struct totem_config *totem_config, const char **error_string)
Definition: totemconfig.c:1876
unsigned int rep
Definition: totem.h:148
#define TOTEM_CONFIG_BINDNETADDR_NODELIST_SET
Definition: totemconfig.h:48
#define LOGSYS_MODE_OUTPUT_SYSLOG
Definition: logsys.h:62
#define ICMAP_TRACK_ADD
Definition: icmap.h:76
int totempg_groups_joined_reserve(void *instance, const struct iovec *iovec, unsigned int iov_len)
Definition: totempg.c:1300
const void * group
Definition: totempg.h:56
#define LOGSYS_LEVEL_ERROR
Definition: logsys.h:72
void sync_save_transitional(const unsigned int *member_list, size_t member_list_entries, const struct memb_ring_id *ring_id)
Definition: sync.c:517
int totempg_groups_mcast_joined(void *instance, const struct iovec *iovec, unsigned int iov_len, int guarantee)
Definition: totempg.c:1230
const char * service_stats_tx[SERVICES_COUNT_MAX][SERVICE_HANDLER_MAXIMUM_COUNT]
Definition: service.c:113
cs_error_t icmap_get(const char *key_name, void *value, size_t *value_len, icmap_value_types_t *type)
Retrieve value of key key_name and store it in user preallocated value pointer.
Definition: icmap.c:723
int corosync_log_config_read(cmap_handle_t cmap_h, const char *default_logfile, const char **error_string)
Definition: logconfig.c:716
#define LOGSYS_LEVEL_DEBUG
Definition: logsys.h:76
uint8_t firewall_enabled_or_nic_failure
Definition: totemstats.h:82
void cs_ipcs_init(void)
Definition: ipc_glue.c:818
The corosync_api_v1 struct.
Definition: coroapi.h:225
typedef __attribute__
#define TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED
Definition: totemconfig.h:46
uint32_t avg_backlog_calc
Definition: totemstats.h:85
void(* log_printf)(int level, int subsys, const char *function_name, const char *file_name, int file_line, const char *format,...) __attribute__((format(printf
Definition: totem.h:99
#define TOTEM_CONFIG_WARNING_MEMBERS_IGNORED
Definition: totemconfig.h:45
struct totem_message_header header
Definition: totemsrp.c:260
#define swab32(x)
The swab32 macro.
Definition: swab.h:51
uint32_t continuous_gather
Definition: totemstats.h:78
cs_error_t icmap_fast_inc(const char *key_name)
Increase stored value by one.
Definition: icmap.c:1061
#define MILLI_2_NANO_SECONDS
Definition: coroapi.h:105
void(* totem_memb_ring_id_store)(const struct memb_ring_id *memb_ring_id, unsigned int nodeid)
Definition: totem.h:242
void message_source_set(mar_message_source_t *source, void *conn)
Definition: main.c:798
void logsys_config_apply(void)
logsys_config_apply
Definition: logsys.c:790
unsigned int token_timeout
Definition: totem.h:174
void totempg_check_q_level(void *instance)
Definition: totempg.c:1292
const char * service_stats_rx[SERVICES_COUNT_MAX][SERVICE_HANDLER_MAXIMUM_COUNT]
Definition: service.c:112
void(* exec_endian_convert_fn)(void *msg)
Definition: coroapi.h:477
The memb_ring_id struct.
Definition: coroapi.h:122
struct corosync_api_v1 * apidef_get(void)
Definition: apidef.c:146
void corosync_sending_allowed_release(void *sending_allowed_private_data)
Definition: main.c:776
#define SERVICES_COUNT_MAX
Definition: coroapi.h:462
unsigned long long tv_prev
Definition: main.c:811
const char *(* totem_ifaces_print)(unsigned int nodeid)
Definition: coroapi.h:290
const char * get_state_dir(void)
Definition: util.c:172
void corosync_state_dump(void)
Definition: main.c:193
qb_loop_timer_handle corosync_timer_handle_t
corosync_timer_handle_t
Definition: coroapi.h:74
int cs_poll_dispatch_delete(qb_loop_t *handle, int fd)
Definition: main.c:188
e_corosync_done
Definition: exec/util.h:47
cs_error_t icmap_get_string(const char *key_name, char **str)
Shortcut for icmap_get for string type.
Definition: icmap.c:880
int message_source_is_local(const mar_message_source_t *source)
Definition: main.c:787
struct totem_logging_configuration totem_logging_configuration
Definition: totem.h:200
int totempg_groups_join(void *instance, const struct totempg_group *groups, size_t group_cnt)
Definition: totempg.c:1180
#define LOGSYS_LEVEL_NOTICE
Definition: logsys.h:74
unsigned long long seq
Definition: coroapi.h:124
cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
Set read-only access for given key (key_name) or prefix, If prefix is set.
Definition: icmap.c:1217
int totempg_initialize(qb_loop_t *poll_handle, struct totem_config *totem_config)
Initialize the totem process groups abstraction.
Definition: totempg.c:802
void corosync_shutdown_request(void)
Definition: main.c:250
struct sched_param global_sched_param
Definition: main.c:162
unsigned int nodeid
Definition: coroapi.h:75
uint32_t mtt_rx_token
Definition: totemstats.h:83
uint32_t continuous_sendmsg_failures
Definition: totemstats.h:79
struct memb_ring_id ring_id
Definition: totemsrp.c:264
int main(int argc, char **argv, char **envp)
Definition: main.c:1210
cs_error_t icmap_init(void)
Initialize global icmap.
Definition: icmap.c:206
#define TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED
Definition: totemconfig.h:47
#define LOGSYS_MODE_OUTPUT_STDERR
Definition: logsys.h:61
Structure passed as new_value and old_value in change callback.
Definition: icmap.h:91
cs_error_t icmap_track_add(const char *key_name, int32_t track_type, icmap_notify_fn_t notify_fn, void *user_data, icmap_track_t *icmap_track)
Add tracking function for given key_name.
Definition: icmap.c:1151
int totem_config_read(struct totem_config *totem_config, const char **error_string, uint64_t *warnings)
Definition: totemconfig.c:1668
int(* sync_process)(void)
Definition: sync.h:45
The mar_message_source_t struct.
Definition: coroapi.h:50
void logsys_system_fini(void)
logsys_system_fini
Definition: logsys.c:286