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