corosync  3.0.1
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 %llx to stable storage",
696  memb_ring_id->seq);
697 
699  }
701  "Storing new sequence id for ring %llx", 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 %llx 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.secauth", CS_FALSE, CS_TRUE);
1040  icmap_set_ro_access("totem.ip_version", CS_FALSE, CS_TRUE);
1041  icmap_set_ro_access("totem.rrp_mode", CS_FALSE, CS_TRUE);
1042  icmap_set_ro_access("totem.transport", CS_FALSE, CS_TRUE);
1043  icmap_set_ro_access("totem.cluster_name", CS_FALSE, CS_TRUE);
1044  icmap_set_ro_access("totem.netmtu", CS_FALSE, CS_TRUE);
1045  icmap_set_ro_access("totem.threads", CS_FALSE, CS_TRUE);
1046  icmap_set_ro_access("totem.version", CS_FALSE, CS_TRUE);
1047  icmap_set_ro_access("totem.nodeid", CS_FALSE, CS_TRUE);
1048  icmap_set_ro_access("totem.clear_node_high_bit", CS_FALSE, CS_TRUE);
1049  icmap_set_ro_access("config.reload_in_progress", CS_FALSE, CS_TRUE);
1050  icmap_set_ro_access("config.totemconfig_reload_in_progress", CS_FALSE, CS_TRUE);
1051 }
1052 
1053 static void main_service_ready (void)
1054 {
1055  int res;
1056 
1057  /*
1058  * This must occur after totempg is initialized because "this_ip" must be set
1059  */
1061  if (res == -1) {
1062  log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services");
1064  }
1065  cs_ipcs_init();
1066  corosync_totem_stats_init ();
1067  corosync_fplay_control_init ();
1068  corosync_force_gather_init ();
1069 
1070  sync_init (
1071  corosync_sync_callbacks_retrieve,
1072  corosync_sync_completed);
1073 }
1074 
1075 static enum e_corosync_done corosync_flock (const char *lockfile, pid_t pid)
1076 {
1077  struct flock lock;
1078  enum e_corosync_done err;
1079  char pid_s[17];
1080  int fd_flag;
1081  int lf;
1082 
1083  err = COROSYNC_DONE_EXIT;
1084 
1085  lf = open (lockfile, O_WRONLY | O_CREAT, 0640);
1086  if (lf == -1) {
1087  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create lock file.");
1088  return (COROSYNC_DONE_ACQUIRE_LOCK);
1089  }
1090 
1091 retry_fcntl:
1092  lock.l_type = F_WRLCK;
1093  lock.l_start = 0;
1094  lock.l_whence = SEEK_SET;
1095  lock.l_len = 0;
1096  if (fcntl (lf, F_SETLK, &lock) == -1) {
1097  switch (errno) {
1098  case EINTR:
1099  goto retry_fcntl;
1100  break;
1101  case EAGAIN:
1102  case EACCES:
1103  log_printf (LOGSYS_LEVEL_ERROR, "Another Corosync instance is already running.");
1105  goto error_close;
1106  break;
1107  default:
1108  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't acquire lock. Error was %s",
1109  strerror(errno));
1111  goto error_close;
1112  break;
1113  }
1114  }
1115 
1116  if (ftruncate (lf, 0) == -1) {
1117  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't truncate lock file. Error was %s",
1118  strerror (errno));
1120  goto error_close_unlink;
1121  }
1122 
1123  memset (pid_s, 0, sizeof (pid_s));
1124  snprintf (pid_s, sizeof (pid_s) - 1, "%u\n", pid);
1125 
1126 retry_write:
1127  if (write (lf, pid_s, strlen (pid_s)) != strlen (pid_s)) {
1128  if (errno == EINTR) {
1129  goto retry_write;
1130  } else {
1131  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't write pid to lock file. "
1132  "Error was %s", strerror (errno));
1134  goto error_close_unlink;
1135  }
1136  }
1137 
1138  if ((fd_flag = fcntl (lf, F_GETFD, 0)) == -1) {
1139  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't get close-on-exec flag from lock file. "
1140  "Error was %s", strerror (errno));
1142  goto error_close_unlink;
1143  }
1144  fd_flag |= FD_CLOEXEC;
1145  if (fcntl (lf, F_SETFD, fd_flag) == -1) {
1146  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't set close-on-exec flag to lock file. "
1147  "Error was %s", strerror (errno));
1149  goto error_close_unlink;
1150  }
1151 
1152  return (err);
1153 
1154 error_close_unlink:
1155  unlink (lockfile);
1156 error_close:
1157  close (lf);
1158 
1159  return (err);
1160 }
1161 
1162 static int corosync_move_to_root_cgroup(void) {
1163  FILE *f;
1164  int res = -1;
1165 
1166  /*
1167  * /sys/fs/cgroup is hardcoded, because most of Linux distributions are now
1168  * using systemd and systemd uses hardcoded path of cgroup mount point.
1169  *
1170  * This feature is expected to be removed as soon as systemd gets support
1171  * for managing RT configuration.
1172  */
1173  f = fopen("/sys/fs/cgroup/cpu/cpu.rt_runtime_us", "rt");
1174  if (f == NULL) {
1175  log_printf(LOGSYS_LEVEL_DEBUG, "cpu.rt_runtime_us doesn't exists -> "
1176  "system without cgroup or with disabled CONFIG_RT_GROUP_SCHED");
1177 
1178  res = 0;
1179  goto exit_res;
1180  }
1181  (void)fclose(f);
1182 
1183  f = fopen("/sys/fs/cgroup/cpu/tasks", "w");
1184  if (f == NULL) {
1185  log_printf(LOGSYS_LEVEL_WARNING, "Can't open cgroups tasks file for writing");
1186 
1187  goto exit_res;
1188  }
1189 
1190  if (fprintf(f, "%jd\n", (intmax_t)getpid()) <= 0) {
1191  log_printf(LOGSYS_LEVEL_WARNING, "Can't write corosync pid into cgroups tasks file");
1192 
1193  goto close_and_exit_res;
1194  }
1195 
1196 close_and_exit_res:
1197  if (fclose(f) != 0) {
1198  log_printf(LOGSYS_LEVEL_WARNING, "Can't close cgroups tasks file");
1199 
1200  goto exit_res;
1201  }
1202 
1203 exit_res:
1204  return (res);
1205 }
1206 
1207 
1208 int main (int argc, char **argv, char **envp)
1209 {
1210  const char *error_string;
1211  struct totem_config totem_config;
1212  int res, ch;
1213  int background, sched_rr, prio, testonly, move_to_root_cgroup;
1214  struct stat stat_out;
1215  enum e_corosync_done flock_err;
1216  uint64_t totem_config_warnings;
1217  struct scheduler_pause_timeout_data scheduler_pause_timeout_data;
1218  long int tmpli;
1219  char *ep;
1220  char *tmp_str;
1221  int log_subsys_id_totem;
1222 
1223  /* default configuration
1224  */
1225  background = 1;
1226  testonly = 0;
1227 
1228  while ((ch = getopt (argc, argv, "c:ftv")) != EOF) {
1229 
1230  switch (ch) {
1231  case 'c':
1232  res = snprintf(corosync_config_file, sizeof(corosync_config_file), "%s", optarg);
1233  if (res >= sizeof(corosync_config_file)) {
1234  fprintf (stderr, "Config file path too long.\n");
1235  syslog (LOGSYS_LEVEL_ERROR, "Config file path too long.");
1236 
1238  return EXIT_FAILURE;
1239  }
1240  break;
1241  case 'f':
1242  background = 0;
1243  break;
1244  case 't':
1245  testonly = 1;
1246  break;
1247  case 'v':
1248  printf ("Corosync Cluster Engine, version '%s'\n", VERSION);
1249  printf ("Copyright (c) 2006-2018 Red Hat, Inc.\n");
1251  return EXIT_SUCCESS;
1252 
1253  break;
1254  default:
1255  fprintf(stderr, \
1256  "usage:\n"\
1257  " -c : Corosync config file path.\n"\
1258  " -f : Start application in foreground.\n"\
1259  " -t : Test configuration and exit.\n"\
1260  " -v : Display version and SVN revision of Corosync and exit.\n");
1262  return EXIT_FAILURE;
1263  }
1264  }
1265 
1266 
1267  /*
1268  * Other signals are registered later via qb_loop_signal_add
1269  */
1270  (void)signal (SIGSEGV, sigsegv_handler);
1271  (void)signal (SIGABRT, sigsegv_handler);
1272 #if MSG_NOSIGNAL != 0
1273  (void)signal (SIGPIPE, SIG_IGN);
1274 #endif
1275 
1276  if (icmap_init() != CS_OK) {
1277  fprintf (stderr, "Corosync Executive couldn't initialize configuration component.\n");
1278  syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize configuration component.");
1280  }
1281  set_icmap_ro_keys_flag();
1282 
1283  /*
1284  * Initialize the corosync_api_v1 definition
1285  */
1286  api = apidef_get ();
1287 
1288  res = coroparse_configparse(icmap_get_global_map(), &error_string);
1289  if (res == -1) {
1290  /*
1291  * Logsys can't log properly at this early stage, and we need to get this message out
1292  *
1293  */
1294  fprintf (stderr, "%s\n", error_string);
1295  syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
1297  }
1298 
1299  if (stats_map_init(api) != CS_OK) {
1300  fprintf (stderr, "Corosync Executive couldn't initialize statistics component.\n");
1301  syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize statistics component.");
1303  }
1304 
1305  res = corosync_log_config_read (&error_string);
1306  if (res == -1) {
1307  /*
1308  * if we are here, we _must_ flush the logsys queue
1309  * and try to inform that we couldn't read the config.
1310  * this is a desperate attempt before certain death
1311  * and there is no guarantee that we can print to stderr
1312  * nor that logsys is sending the messages where we expect.
1313  */
1314  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1315  fprintf(stderr, "%s", error_string);
1316  syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
1318  }
1319 
1320  if (!testonly) {
1321  log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine %s starting up", VERSION);
1322  log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "");
1323  }
1324 
1325  /*
1326  * Create totem logsys subsys before totem_config_read so log functions can be used
1327  */
1328  log_subsys_id_totem = _logsys_subsys_create("TOTEM", "totem,"
1329  "totemip.c,totemconfig.c,totemcrypto.c,totemsrp.c,"
1330  "totempg.c,totemudp.c,totemudpu.c,totemnet.c,totemknet.c");
1331 
1332  /*
1333  * Make sure required directory is present
1334  */
1335  res = stat (get_state_dir(), &stat_out);
1336  if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
1337  log_printf (LOGSYS_LEVEL_ERROR, "State directory %s not present. Please create it.", get_state_dir());
1339  }
1340 
1341  res = chdir(get_state_dir());
1342  if (res == -1) {
1343  log_printf (LOGSYS_LEVEL_ERROR, "Cannot chdir to state directory %s. "
1344  "Please make sure it has correct context and rights.", get_state_dir());
1346  }
1347 
1348  res = totem_config_read (&totem_config, &error_string, &totem_config_warnings);
1349  if (res == -1) {
1350  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1352  }
1353 
1354  if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_IGNORED) {
1355  log_printf (LOGSYS_LEVEL_WARNING, "member section is used together with nodelist. Members ignored.");
1356  }
1357 
1358  if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED) {
1359  log_printf (LOGSYS_LEVEL_WARNING, "member section is deprecated.");
1360  }
1361 
1362  if (totem_config_warnings & TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED) {
1363  log_printf (LOGSYS_LEVEL_WARNING, "nodeid appears both in totem section and nodelist. Nodelist one is used.");
1364  }
1365 
1366  if (totem_config_warnings & TOTEM_CONFIG_BINDNETADDR_NODELIST_SET) {
1367  log_printf (LOGSYS_LEVEL_WARNING, "interface section bindnetaddr is used together with nodelist. "
1368  "Nodelist one is going to be used.");
1369  }
1370 
1371  if (totem_config_warnings != 0) {
1372  log_printf (LOGSYS_LEVEL_WARNING, "Please migrate config file to nodelist.");
1373  }
1374 
1375  res = totem_config_keyread (&totem_config, &error_string);
1376  if (res == -1) {
1377  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1379  }
1380 
1381  res = totem_config_validate (&totem_config, &error_string);
1382  if (res == -1) {
1383  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1385  }
1386 
1387  if (testonly) {
1389  }
1390 
1391 
1392  move_to_root_cgroup = 1;
1393  if (icmap_get_string("system.move_to_root_cgroup", &tmp_str) == CS_OK) {
1394  if (strcmp(tmp_str, "yes") != 0) {
1395  move_to_root_cgroup = 0;
1396  }
1397  free(tmp_str);
1398  }
1399 
1400  /*
1401  * Try to move corosync into root cpu cgroup. Failure is not fatal and
1402  * error is deliberately ignored.
1403  */
1404  if (move_to_root_cgroup) {
1405  (void)corosync_move_to_root_cgroup();
1406  }
1407 
1408  sched_rr = 1;
1409  if (icmap_get_string("system.sched_rr", &tmp_str) == CS_OK) {
1410  if (strcmp(tmp_str, "yes") != 0) {
1411  sched_rr = 0;
1412  }
1413  free(tmp_str);
1414  }
1415 
1416  prio = 0;
1417  if (icmap_get_string("system.priority", &tmp_str) == CS_OK) {
1418  if (strcmp(tmp_str, "max") == 0) {
1419  prio = INT_MIN;
1420  } else if (strcmp(tmp_str, "min") == 0) {
1421  prio = INT_MAX;
1422  } else {
1423  errno = 0;
1424 
1425  tmpli = strtol(tmp_str, &ep, 10);
1426  if (errno != 0 || *ep != '\0' || tmpli > INT_MAX || tmpli < INT_MIN) {
1427  log_printf (LOGSYS_LEVEL_ERROR, "Priority value %s is invalid", tmp_str);
1429  }
1430 
1431  prio = tmpli;
1432  }
1433 
1434  free(tmp_str);
1435  }
1436 
1437  /*
1438  * Set round robin realtime scheduling with priority 99
1439  */
1440  if (sched_rr) {
1441  if (corosync_set_rr_scheduler () != 0) {
1442  prio = INT_MIN;
1443  } else {
1444  prio = 0;
1445  }
1446  }
1447 
1448  if (prio != 0) {
1449  if (setpriority(PRIO_PGRP, 0, prio) != 0) {
1451  "Could not set priority %d", prio);
1452  }
1453  }
1454 
1455  totem_config.totem_memb_ring_id_create_or_load = corosync_ring_id_create_or_load;
1456  totem_config.totem_memb_ring_id_store = corosync_ring_id_store;
1457 
1459  totem_config.totem_logging_configuration.log_subsys_id = log_subsys_id_totem;
1460 
1467  totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
1468 
1470 
1471  /*
1472  * Now we are fully initialized.
1473  */
1474  if (background) {
1476 
1477  corosync_tty_detach ();
1478 
1480 
1481  log_printf (LOGSYS_LEVEL_DEBUG, "Corosync TTY detached");
1482  }
1483 
1484  /*
1485  * Lock all memory to avoid page faults which may interrupt
1486  * application healthchecking
1487  */
1488  corosync_mlockall ();
1489 
1490  corosync_poll_handle = qb_loop_create ();
1491 
1492  memset(&scheduler_pause_timeout_data, 0, sizeof(scheduler_pause_timeout_data));
1493  scheduler_pause_timeout_data.totem_config = &totem_config;
1494  timer_function_scheduler_timeout (&scheduler_pause_timeout_data);
1495 
1496  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_LOW,
1497  SIGUSR2, NULL, sig_diag_handler, NULL);
1498  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1499  SIGINT, NULL, sig_exit_handler, NULL);
1500  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1501  SIGQUIT, NULL, sig_exit_handler, NULL);
1502  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1503  SIGTERM, NULL, sig_exit_handler, NULL);
1504 
1505  if (logsys_thread_start() != 0) {
1506  log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize log thread");
1508  }
1509 
1510  if ((flock_err = corosync_flock (corosync_lock_file, getpid ())) != COROSYNC_DONE_EXIT) {
1511  corosync_exit_error (flock_err);
1512  }
1513 
1514  /*
1515  * if totempg_initialize doesn't have root priveleges, it cannot
1516  * bind to a specific interface. This only matters if
1517  * there is more then one interface in a system, so
1518  * in this case, only a warning is printed
1519  */
1520  /*
1521  * Join multicast group and setup delivery
1522  * and configuration change functions
1523  */
1524  if (totempg_initialize (
1525  corosync_poll_handle,
1526  &totem_config) != 0) {
1527 
1528  log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize TOTEM layer");
1530  }
1531 
1533  main_service_ready);
1534 
1536  &corosync_group_handle,
1537  deliver_fn,
1538  confchg_fn);
1539 
1541  corosync_group_handle,
1542  &corosync_group,
1543  1);
1544 
1545  /*
1546  * Drop root privleges to user 'corosync'
1547  * TODO: Don't really need full root capabilities;
1548  * needed capabilities are:
1549  * CAP_NET_RAW (bindtodevice)
1550  * CAP_SYS_NICE (setscheduler)
1551  * CAP_IPC_LOCK (mlockall)
1552  */
1553  priv_drop ();
1554 
1555  schedwrk_init (
1556  serialize_lock,
1557  serialize_unlock);
1558 
1559  /*
1560  * Start main processing loop
1561  */
1562  qb_loop_run (corosync_poll_handle);
1563 
1564  /*
1565  * Exit was requested
1566  */
1567  totempg_finalize ();
1568 
1569  /*
1570  * free the loop resources
1571  */
1572  qb_loop_destroy (corosync_poll_handle);
1573 
1574  /*
1575  * free up the icmap
1576  */
1577 
1578  /*
1579  * Remove pid lock file
1580  */
1581  unlink (corosync_lock_file);
1582 
1584 
1585  return EXIT_SUCCESS;
1586 }
#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:827
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:1549
struct totem_config * totem_config
Definition: main.c:809
void *(* totem_get_stats)(void)
Definition: coroapi.h:435
#define PACKAGE_FEATURES
Definition: config.h:370
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:1316
#define VERSION
Definition: config.h:430
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:236
#define TOTEM_TOKEN_STATS_MAX
Definition: totemstats.h:89
uint32_t avg_token_workload
Definition: totemstats.h:84
#define LOCALSTATEDIR
Definition: config.h:355
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:1104
#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:1554
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:1504
#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:2004
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:1495
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:1777
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:1272
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:1202
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:240
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:1264
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:1152
#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:774
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:1208
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:1576
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