corosync  3.1.0
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 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  res = setrlimit (RLIMIT_MEMLOCK, &rlimit);
477  if (res == -1) {
479  "Could not increase RLIMIT_MEMLOCK, not locking memory");
480  return;
481  }
482 
483  res = mlockall (MCL_CURRENT | MCL_FUTURE);
484  if (res == -1) {
486  "Could not lock memory of service to avoid page faults");
487  };
488 }
489 
490 
491 static void corosync_totem_stats_updater (void *data)
492 {
493  totempg_stats_t * stats;
494  uint32_t total_mtt_rx_token;
495  uint32_t total_backlog_calc;
496  uint32_t total_token_holdtime;
497  int t, prev;
498  int32_t token_count;
499  const char *cstr;
500 
501  stats = api->totem_get_stats();
502 
503 
505 
506  if (stats->srp->continuous_gather > MAX_NO_CONT_GATHER ||
508  cstr = "";
509 
511  cstr = "number of multicast sendmsg failures is above threshold";
512  }
513 
514  if (stats->srp->continuous_gather > MAX_NO_CONT_GATHER) {
515  cstr = "totem is continuously in gather state";
516  }
517 
519  "Totem is unable to form a cluster because of an "
520  "operating system or network fault (reason: %s). The most common "
521  "cause of this message is that the local firewall is "
522  "configured improperly.", cstr);
524  } else {
526  }
527 
528  total_mtt_rx_token = 0;
529  total_token_holdtime = 0;
530  total_backlog_calc = 0;
531  token_count = 0;
532  t = stats->srp->latest_token;
533  while (1) {
534  if (t == 0)
535  prev = TOTEM_TOKEN_STATS_MAX - 1;
536  else
537  prev = t - 1;
538  if (prev == stats->srp->earliest_token)
539  break;
540  /* if tx == 0, then dropped token (not ours) */
541  if (stats->srp->token[t].tx != 0 ||
542  (stats->srp->token[t].rx - stats->srp->token[prev].rx) > 0 ) {
543  total_mtt_rx_token += (stats->srp->token[t].rx - stats->srp->token[prev].rx);
544  total_token_holdtime += (stats->srp->token[t].tx - stats->srp->token[t].rx);
545  total_backlog_calc += stats->srp->token[t].backlog_calc;
546  token_count++;
547  }
548  t = prev;
549  }
550  if (token_count) {
551  stats->srp->mtt_rx_token = (total_mtt_rx_token / token_count);
552  stats->srp->avg_token_workload = (total_token_holdtime / token_count);
553  stats->srp->avg_backlog_calc = (total_backlog_calc / token_count);
554  }
555 
556  stats->srp->time_since_token_last_received = qb_util_nano_current_get () / QB_TIME_NS_IN_MSEC -
557  stats->srp->token[stats->srp->latest_token].rx;
558 
560 
561  api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
562  corosync_totem_stats_updater,
563  &corosync_stats_timer_handle);
564 }
565 
566 static void corosync_totem_stats_init (void)
567 {
568  /* start stats timer */
569  api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
570  corosync_totem_stats_updater,
571  &corosync_stats_timer_handle);
572 }
573 
574 static void deliver_fn (
575  unsigned int nodeid,
576  const void *msg,
577  unsigned int msg_len,
578  int endian_conversion_required)
579 {
580  const struct qb_ipc_request_header *header;
581  int32_t service;
582  int32_t fn_id;
583  uint32_t id;
584 
585  header = msg;
586  if (endian_conversion_required) {
587  id = swab32 (header->id);
588  } else {
589  id = header->id;
590  }
591 
592  /*
593  * Call the proper executive handler
594  */
595  service = id >> 16;
596  fn_id = id & 0xffff;
597 
598  if (!corosync_service[service]) {
599  return;
600  }
601  if (fn_id >= corosync_service[service]->exec_engine_count) {
602  log_printf(LOGSYS_LEVEL_WARNING, "discarded unknown message %d for service %d (max id %d)",
603  fn_id, service, corosync_service[service]->exec_engine_count);
604  return;
605  }
606 
607  icmap_fast_inc(service_stats_rx[service][fn_id]);
608 
609  if (endian_conversion_required) {
610  assert(corosync_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
612  ((void *)msg);
613  }
614 
616  (msg, nodeid);
617 }
618 
620  const struct iovec *iovec,
621  unsigned int iov_len,
622  unsigned int guarantee)
623 {
624  const struct qb_ipc_request_header *req = iovec->iov_base;
625  int32_t service;
626  int32_t fn_id;
627 
628  service = req->id >> 16;
629  fn_id = req->id & 0xffff;
630 
631  if (corosync_service[service]) {
632  icmap_fast_inc(service_stats_tx[service][fn_id]);
633  }
634 
635  return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
636 }
637 
638 static void corosync_ring_id_create_or_load (
639  struct memb_ring_id *memb_ring_id,
640  unsigned int nodeid)
641 {
642  int fd;
643  int res = 0;
644  char filename[PATH_MAX];
645 
646  snprintf (filename, sizeof(filename), "%s/ringid_%u",
647  get_state_dir(), nodeid);
648  fd = open (filename, O_RDONLY, 0700);
649  /*
650  * If file can be opened and read, read the ring id
651  */
652  if (fd != -1) {
653  res = read (fd, &memb_ring_id->seq, sizeof (uint64_t));
654  close (fd);
655  }
656  /*
657  * If file could not be opened or read, create a new ring id
658  */
659  if ((fd == -1) || (res != sizeof (uint64_t))) {
660  memb_ring_id->seq = 0;
661  umask(0);
662  fd = open (filename, O_CREAT|O_RDWR, 0700);
663  if (fd != -1) {
664  res = write (fd, &memb_ring_id->seq, sizeof (uint64_t));
665  close (fd);
666  if (res == -1) {
668  "Couldn't write ringid file '%s'", filename);
669 
671  }
672  } else {
674  "Couldn't create ringid file '%s'", filename);
675 
677  }
678  }
679 
681 }
682 
683 static void corosync_ring_id_store (
684  const struct memb_ring_id *memb_ring_id,
685  unsigned int nodeid)
686 {
687  char filename[PATH_MAX];
688  int fd;
689  int res;
690 
691  snprintf (filename, sizeof(filename), "%s/ringid_%u",
692  get_state_dir(), nodeid);
693 
694  fd = open (filename, O_WRONLY, 0700);
695  if (fd == -1) {
696  fd = open (filename, O_CREAT|O_RDWR, 0700);
697  }
698  if (fd == -1) {
700  "Couldn't store new ring id " CS_PRI_RING_ID_SEQ " to stable storage",
701  memb_ring_id->seq);
702 
704  }
706  "Storing new sequence id for ring " CS_PRI_RING_ID_SEQ, memb_ring_id->seq);
707  res = write (fd, &memb_ring_id->seq, sizeof(memb_ring_id->seq));
708  close (fd);
709  if (res != sizeof(memb_ring_id->seq)) {
711  "Couldn't store new ring id " CS_PRI_RING_ID_SEQ " to stable storage",
712  memb_ring_id->seq);
713 
715  }
716 }
717 
718 static qb_loop_timer_handle recheck_the_q_level_timer;
720 {
721  totempg_check_q_level(corosync_group_handle);
723  qb_loop_timer_add(cs_poll_handle_get(), QB_LOOP_MED, 1*QB_TIME_NS_IN_MSEC,
724  NULL, corosync_recheck_the_q_level, &recheck_the_q_level_timer);
725  }
726 }
727 
730 };
731 
732 
734  unsigned int service,
735  unsigned int id,
736  const void *msg,
737  void *sending_allowed_private_data)
738 {
740  (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
741  struct iovec reserve_iovec;
742  struct qb_ipc_request_header *header = (struct qb_ipc_request_header *)msg;
743  int sending_allowed;
744 
745  reserve_iovec.iov_base = (char *)header;
746  reserve_iovec.iov_len = header->size;
747 
749  corosync_group_handle,
750  &reserve_iovec, 1);
751  if (pd->reserved_msgs == -1) {
752  return -EINVAL;
753  }
754 
755  /* Message ID out of range */
756  if (id >= corosync_service[service]->lib_engine_count) {
757  return -EINVAL;
758  }
759 
760  sending_allowed = QB_FALSE;
761  if (corosync_quorum_is_quorate() == 1 ||
762  corosync_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) {
763  // we are quorate
764  // now check flow control
765  if (corosync_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) {
766  sending_allowed = QB_TRUE;
767  } else if (pd->reserved_msgs && sync_in_process == 0) {
768  sending_allowed = QB_TRUE;
769  } else if (pd->reserved_msgs == 0) {
770  return -ENOBUFS;
771  } else /* (sync_in_process) */ {
772  return -EINPROGRESS;
773  }
774  } else {
775  return -EHOSTUNREACH;
776  }
777 
778  return (sending_allowed);
779 }
780 
781 void corosync_sending_allowed_release (void *sending_allowed_private_data)
782 {
784  (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
785 
786  if (pd->reserved_msgs == -1) {
787  return;
788  }
790 }
791 
793 {
794  int ret = 0;
795 
796  assert (source != NULL);
797  if (source->nodeid == totempg_my_nodeid_get ()) {
798  ret = 1;
799  }
800  return ret;
801 }
802 
804  mar_message_source_t *source,
805  void *conn)
806 {
807  assert ((source != NULL) && (conn != NULL));
808  memset (source, 0, sizeof (mar_message_source_t));
809  source->nodeid = totempg_my_nodeid_get ();
810  source->conn = conn;
811 }
812 
815  qb_loop_timer_handle handle;
816  unsigned long long tv_prev;
817  unsigned long long max_tv_diff;
818 };
819 
820 static void timer_function_scheduler_timeout (void *data)
821 {
822  struct scheduler_pause_timeout_data *timeout_data = (struct scheduler_pause_timeout_data *)data;
823  unsigned long long tv_current;
824  unsigned long long tv_diff;
825  uint64_t schedmiss_event_tstamp;
826 
827  tv_current = qb_util_nano_current_get ();
828 
829  if (timeout_data->tv_prev == 0) {
830  /*
831  * Initial call -> just pretent everything is ok
832  */
833  timeout_data->tv_prev = tv_current;
834  timeout_data->max_tv_diff = 0;
835  }
836 
837  tv_diff = tv_current - timeout_data->tv_prev;
838  timeout_data->tv_prev = tv_current;
839 
840  if (tv_diff > timeout_data->max_tv_diff) {
841  schedmiss_event_tstamp = qb_util_nano_from_epoch_get() / QB_TIME_NS_IN_MSEC;
842 
843  log_printf (LOGSYS_LEVEL_WARNING, "Corosync main process was not scheduled (@%" PRIu64 ") for %0.4f ms "
844  "(threshold is %0.4f ms). Consider token timeout increase.",
845  schedmiss_event_tstamp,
846  (float)tv_diff / QB_TIME_NS_IN_MSEC, (float)timeout_data->max_tv_diff / QB_TIME_NS_IN_MSEC);
847 
848  stats_add_schedmiss_event(schedmiss_event_tstamp, (float)tv_diff / QB_TIME_NS_IN_MSEC);
849  }
850 
851  /*
852  * Set next threshold, because token_timeout can change
853  */
854  timeout_data->max_tv_diff = timeout_data->totem_config->token_timeout * QB_TIME_NS_IN_MSEC * 0.8;
855  qb_loop_timer_add (corosync_poll_handle,
856  QB_LOOP_MED,
857  timeout_data->totem_config->token_timeout * QB_TIME_NS_IN_MSEC / 3,
858  timeout_data,
859  timer_function_scheduler_timeout,
860  &timeout_data->handle);
861 }
862 
863 
864 static int corosync_set_rr_scheduler (void)
865 {
866  int ret_val = 0;
867 
868 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
869  int res;
870 
871  sched_priority = sched_get_priority_max (SCHED_RR);
872  if (sched_priority != -1) {
873  global_sched_param.sched_priority = sched_priority;
874  res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
875  if (res == -1) {
877  "Could not set SCHED_RR at priority %d",
878  global_sched_param.sched_priority);
879 
880  global_sched_param.sched_priority = 0;
881 #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
882  qb_log_thread_priority_set (SCHED_OTHER, 0);
883 #endif
884  ret_val = -1;
885  } else {
886 
887  /*
888  * Turn on SCHED_RR in logsys system
889  */
890 #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
891  res = qb_log_thread_priority_set (SCHED_RR, sched_priority);
892 #else
893  res = -1;
894 #endif
895  if (res == -1) {
897  "Could not set logsys thread priority."
898  " Can't continue because of priority inversions.");
900  }
901  }
902  } else {
904  "Could not get maximum scheduler priority");
905  sched_priority = 0;
906  ret_val = -1;
907  }
908 #else
910  "The Platform is missing process priority setting features. Leaving at default.");
911  ret_val = -1;
912 #endif
913 
914  return (ret_val);
915 }
916 
917 
918 /* The basename man page contains scary warnings about
919  thread-safety and portability, hence this */
920 static const char *corosync_basename(const char *file_name)
921 {
922  char *base;
923  base = strrchr (file_name, '/');
924  if (base) {
925  return base + 1;
926  }
927 
928  return file_name;
929 }
930 
931 static void
932 _logsys_log_printf(int level, int subsys,
933  const char *function_name,
934  const char *file_name,
935  int file_line,
936  const char *format,
937  ...) __attribute__((format(printf, 6, 7)));
938 
939 static void
940 _logsys_log_printf(int level, int subsys,
941  const char *function_name,
942  const char *file_name,
943  int file_line,
944  const char *format, ...)
945 {
946  va_list ap;
947 
948  va_start(ap, format);
949  qb_log_from_external_source_va(function_name, corosync_basename(file_name),
950  format, level, file_line,
951  subsys, ap);
952  va_end(ap);
953 }
954 
955 static void fplay_key_change_notify_fn (
956  int32_t event,
957  const char *key_name,
958  struct icmap_notify_value new_val,
959  struct icmap_notify_value old_val,
960  void *user_data)
961 {
962  if (strcmp(key_name, "runtime.blackbox.dump_flight_data") == 0) {
963  fprintf(stderr,"Writetofile\n");
964  corosync_blackbox_write_to_file ();
965  }
966  if (strcmp(key_name, "runtime.blackbox.dump_state") == 0) {
967  fprintf(stderr,"statefump\n");
969  }
970 }
971 
972 static void corosync_fplay_control_init (void)
973 {
974  icmap_track_t track = NULL;
975 
976  icmap_set_string("runtime.blackbox.dump_flight_data", "no");
977  icmap_set_string("runtime.blackbox.dump_state", "no");
978 
979  icmap_track_add("runtime.blackbox.dump_flight_data",
981  fplay_key_change_notify_fn,
982  NULL, &track);
983  icmap_track_add("runtime.blackbox.dump_state",
985  fplay_key_change_notify_fn,
986  NULL, &track);
987 }
988 
989 static void force_gather_notify_fn(
990  int32_t event,
991  const char *key_name,
992  struct icmap_notify_value new_val,
993  struct icmap_notify_value old_val,
994  void *user_data)
995 {
996  char *key_val;
997 
998  if (icmap_get_string(key_name, &key_val) == CS_OK && strcmp(key_val, "no") == 0)
999  goto out;
1000 
1001  icmap_set_string("runtime.force_gather", "no");
1002 
1003  if (strcmp(key_name, "runtime.force_gather") == 0) {
1004  log_printf(LOGSYS_LEVEL_ERROR, "Forcing into GATHER state\n");
1006  }
1007 
1008 out:
1009  free(key_val);
1010 }
1011 
1012 static void corosync_force_gather_init (void)
1013 {
1014  icmap_track_t track = NULL;
1015 
1016  icmap_set_string("runtime.force_gather", "no");
1017 
1018  icmap_track_add("runtime.force_gather",
1020  force_gather_notify_fn,
1021  NULL, &track);
1022 }
1023 
1024 /*
1025  * Set RO flag for keys, which ether doesn't make sense to change by user (statistic)
1026  * or which when changed are not reflected by runtime (totem.crypto_cipher, ...).
1027  *
1028  * Also some RO keys cannot be determined in this stage, so they are set later in
1029  * other functions (like nodelist.local_node_pos, ...)
1030  */
1031 static void set_icmap_ro_keys_flag (void)
1032 {
1033  /*
1034  * Set RO flag for all keys of internal configuration and runtime statistics
1035  */
1036  icmap_set_ro_access("internal_configuration.", CS_TRUE, CS_TRUE);
1037  icmap_set_ro_access("runtime.services.", CS_TRUE, CS_TRUE);
1038  icmap_set_ro_access("runtime.config.", CS_TRUE, CS_TRUE);
1039  icmap_set_ro_access("runtime.totem.", CS_TRUE, CS_TRUE);
1040  icmap_set_ro_access("uidgid.config.", CS_TRUE, CS_TRUE);
1041  icmap_set_ro_access("system.", CS_TRUE, CS_TRUE);
1042  icmap_set_ro_access("nodelist.", CS_TRUE, CS_TRUE);
1043 
1044  /*
1045  * Set RO flag for constrete keys of configuration which can't be changed
1046  * during runtime
1047  */
1048  icmap_set_ro_access("totem.crypto_cipher", CS_FALSE, CS_TRUE);
1049  icmap_set_ro_access("totem.crypto_hash", CS_FALSE, CS_TRUE);
1050  icmap_set_ro_access("totem.keyfile", CS_FALSE, CS_TRUE);
1051  icmap_set_ro_access("totem.key", CS_FALSE, CS_TRUE);
1052  icmap_set_ro_access("totem.secauth", CS_FALSE, CS_TRUE);
1053  icmap_set_ro_access("totem.ip_version", CS_FALSE, CS_TRUE);
1054  icmap_set_ro_access("totem.rrp_mode", CS_FALSE, CS_TRUE);
1055  icmap_set_ro_access("totem.transport", CS_FALSE, CS_TRUE);
1056  icmap_set_ro_access("totem.cluster_name", CS_FALSE, CS_TRUE);
1057  icmap_set_ro_access("totem.netmtu", CS_FALSE, CS_TRUE);
1058  icmap_set_ro_access("totem.threads", CS_FALSE, CS_TRUE);
1059  icmap_set_ro_access("totem.version", CS_FALSE, CS_TRUE);
1060  icmap_set_ro_access("totem.nodeid", CS_FALSE, CS_TRUE);
1061  icmap_set_ro_access("totem.clear_node_high_bit", CS_FALSE, CS_TRUE);
1062  icmap_set_ro_access("config.reload_in_progress", CS_FALSE, CS_TRUE);
1063  icmap_set_ro_access("config.totemconfig_reload_in_progress", CS_FALSE, CS_TRUE);
1064 }
1065 
1066 static void main_service_ready (void)
1067 {
1068  int res;
1069 
1070  /*
1071  * This must occur after totempg is initialized because "this_ip" must be set
1072  */
1074  if (res == -1) {
1075  log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services");
1077  }
1078  cs_ipcs_init();
1079  corosync_totem_stats_init ();
1080  corosync_fplay_control_init ();
1081  corosync_force_gather_init ();
1082 
1083  sync_init (
1084  corosync_sync_callbacks_retrieve,
1085  corosync_sync_completed);
1086 }
1087 
1088 static enum e_corosync_done corosync_flock (const char *lockfile, pid_t pid)
1089 {
1090  struct flock lock;
1091  enum e_corosync_done err;
1092  char pid_s[17];
1093  int fd_flag;
1094  int lf;
1095 
1096  err = COROSYNC_DONE_EXIT;
1097 
1098  lf = open (lockfile, O_WRONLY | O_CREAT, 0640);
1099  if (lf == -1) {
1100  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create lock file.");
1101  return (COROSYNC_DONE_ACQUIRE_LOCK);
1102  }
1103 
1104 retry_fcntl:
1105  lock.l_type = F_WRLCK;
1106  lock.l_start = 0;
1107  lock.l_whence = SEEK_SET;
1108  lock.l_len = 0;
1109  if (fcntl (lf, F_SETLK, &lock) == -1) {
1110  switch (errno) {
1111  case EINTR:
1112  goto retry_fcntl;
1113  break;
1114  case EAGAIN:
1115  case EACCES:
1116  log_printf (LOGSYS_LEVEL_ERROR, "Another Corosync instance is already running.");
1118  goto error_close;
1119  break;
1120  default:
1121  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't acquire lock. Error was %s",
1122  strerror(errno));
1124  goto error_close;
1125  break;
1126  }
1127  }
1128 
1129  if (ftruncate (lf, 0) == -1) {
1130  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't truncate lock file. Error was %s",
1131  strerror (errno));
1133  goto error_close_unlink;
1134  }
1135 
1136  memset (pid_s, 0, sizeof (pid_s));
1137  snprintf (pid_s, sizeof (pid_s) - 1, "%u\n", pid);
1138 
1139 retry_write:
1140  if (write (lf, pid_s, strlen (pid_s)) != strlen (pid_s)) {
1141  if (errno == EINTR) {
1142  goto retry_write;
1143  } else {
1144  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't write pid to lock file. "
1145  "Error was %s", strerror (errno));
1147  goto error_close_unlink;
1148  }
1149  }
1150 
1151  if ((fd_flag = fcntl (lf, F_GETFD, 0)) == -1) {
1152  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't get close-on-exec flag from lock file. "
1153  "Error was %s", strerror (errno));
1155  goto error_close_unlink;
1156  }
1157  fd_flag |= FD_CLOEXEC;
1158  if (fcntl (lf, F_SETFD, fd_flag) == -1) {
1159  log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't set close-on-exec flag to lock file. "
1160  "Error was %s", strerror (errno));
1162  goto error_close_unlink;
1163  }
1164 
1165  return (err);
1166 
1167 error_close_unlink:
1168  unlink (lockfile);
1169 error_close:
1170  close (lf);
1171 
1172  return (err);
1173 }
1174 
1175 static int corosync_move_to_root_cgroup(void) {
1176  FILE *f;
1177  int res = -1;
1178 
1179  /*
1180  * /sys/fs/cgroup is hardcoded, because most of Linux distributions are now
1181  * using systemd and systemd uses hardcoded path of cgroup mount point.
1182  *
1183  * This feature is expected to be removed as soon as systemd gets support
1184  * for managing RT configuration.
1185  */
1186  f = fopen("/sys/fs/cgroup/cpu/cpu.rt_runtime_us", "rt");
1187  if (f == NULL) {
1188  log_printf(LOGSYS_LEVEL_DEBUG, "cpu.rt_runtime_us doesn't exists -> "
1189  "system without cgroup or with disabled CONFIG_RT_GROUP_SCHED");
1190 
1191  res = 0;
1192  goto exit_res;
1193  }
1194  (void)fclose(f);
1195 
1196  f = fopen("/sys/fs/cgroup/cpu/tasks", "w");
1197  if (f == NULL) {
1198  log_printf(LOGSYS_LEVEL_WARNING, "Can't open cgroups tasks file for writing");
1199 
1200  goto exit_res;
1201  }
1202 
1203  if (fprintf(f, "%jd\n", (intmax_t)getpid()) <= 0) {
1204  log_printf(LOGSYS_LEVEL_WARNING, "Can't write corosync pid into cgroups tasks file");
1205 
1206  goto close_and_exit_res;
1207  }
1208 
1209 close_and_exit_res:
1210  if (fclose(f) != 0) {
1211  log_printf(LOGSYS_LEVEL_WARNING, "Can't close cgroups tasks file");
1212 
1213  goto exit_res;
1214  }
1215 
1216 exit_res:
1217  return (res);
1218 }
1219 
1220 
1221 int main (int argc, char **argv, char **envp)
1222 {
1223  const char *error_string;
1224  struct totem_config totem_config;
1225  int res, ch;
1226  int background, sched_rr, prio, testonly, move_to_root_cgroup;
1227  struct stat stat_out;
1228  enum e_corosync_done flock_err;
1229  uint64_t totem_config_warnings;
1231  long int tmpli;
1232  char *ep;
1233  char *tmp_str;
1234  int log_subsys_id_totem;
1235 
1236  /* default configuration
1237  */
1238  background = 1;
1239  testonly = 0;
1240 
1241  while ((ch = getopt (argc, argv, "c:ftv")) != EOF) {
1242 
1243  switch (ch) {
1244  case 'c':
1245  res = snprintf(corosync_config_file, sizeof(corosync_config_file), "%s", optarg);
1246  if (res >= sizeof(corosync_config_file)) {
1247  fprintf (stderr, "Config file path too long.\n");
1248  syslog (LOGSYS_LEVEL_ERROR, "Config file path too long.");
1249 
1251  return EXIT_FAILURE;
1252  }
1253  break;
1254  case 'f':
1255  background = 0;
1256  break;
1257  case 't':
1258  testonly = 1;
1259  break;
1260  case 'v':
1261  printf ("Corosync Cluster Engine, version '%s'\n", VERSION);
1262  printf ("Copyright (c) 2006-2018 Red Hat, Inc.\n");
1264  return EXIT_SUCCESS;
1265 
1266  break;
1267  default:
1268  fprintf(stderr, \
1269  "usage:\n"\
1270  " -c : Corosync config file path.\n"\
1271  " -f : Start application in foreground.\n"\
1272  " -t : Test configuration and exit.\n"\
1273  " -v : Display version and SVN revision of Corosync and exit.\n");
1275  return EXIT_FAILURE;
1276  }
1277  }
1278 
1279 
1280  /*
1281  * Other signals are registered later via qb_loop_signal_add
1282  */
1283  (void)signal (SIGSEGV, sigsegv_handler);
1284  (void)signal (SIGABRT, sigsegv_handler);
1285 #if MSG_NOSIGNAL != 0
1286  (void)signal (SIGPIPE, SIG_IGN);
1287 #endif
1288 
1289  if (icmap_init() != CS_OK) {
1290  fprintf (stderr, "Corosync Executive couldn't initialize configuration component.\n");
1291  syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize configuration component.");
1293  }
1294  set_icmap_ro_keys_flag();
1295 
1296  /*
1297  * Initialize the corosync_api_v1 definition
1298  */
1299  api = apidef_get ();
1300 
1301  res = coroparse_configparse(icmap_get_global_map(), &error_string);
1302  if (res == -1) {
1303  /*
1304  * Logsys can't log properly at this early stage, and we need to get this message out
1305  *
1306  */
1307  fprintf (stderr, "%s\n", error_string);
1308  syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
1310  }
1311 
1312  if (stats_map_init(api) != CS_OK) {
1313  fprintf (stderr, "Corosync Executive couldn't initialize statistics component.\n");
1314  syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize statistics component.");
1316  }
1317 
1318  res = corosync_log_config_read (&error_string);
1319  if (res == -1) {
1320  /*
1321  * if we are here, we _must_ flush the logsys queue
1322  * and try to inform that we couldn't read the config.
1323  * this is a desperate attempt before certain death
1324  * and there is no guarantee that we can print to stderr
1325  * nor that logsys is sending the messages where we expect.
1326  */
1327  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1328  fprintf(stderr, "%s", error_string);
1329  syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
1331  }
1332 
1333  if (!testonly) {
1334  log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine %s starting up", VERSION);
1335  log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "");
1336  }
1337 
1338  /*
1339  * Create totem logsys subsys before totem_config_read so log functions can be used
1340  */
1341  log_subsys_id_totem = _logsys_subsys_create("TOTEM", "totem,"
1342  "totemip.c,totemconfig.c,totemcrypto.c,totemsrp.c,"
1343  "totempg.c,totemudp.c,totemudpu.c,totemnet.c,totemknet.c");
1344 
1345  /*
1346  * Make sure required directory is present
1347  */
1348  res = stat (get_state_dir(), &stat_out);
1349  if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
1350  log_printf (LOGSYS_LEVEL_ERROR, "State directory %s not present. Please create it.", get_state_dir());
1352  }
1353 
1354  res = chdir(get_state_dir());
1355  if (res == -1) {
1356  log_printf (LOGSYS_LEVEL_ERROR, "Cannot chdir to state directory %s. "
1357  "Please make sure it has correct context and rights.", get_state_dir());
1359  }
1360 
1361  res = totem_config_read (&totem_config, &error_string, &totem_config_warnings);
1362  if (res == -1) {
1363  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1365  }
1366 
1367  if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_IGNORED) {
1368  log_printf (LOGSYS_LEVEL_WARNING, "member section is used together with nodelist. Members ignored.");
1369  }
1370 
1371  if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED) {
1372  log_printf (LOGSYS_LEVEL_WARNING, "member section is deprecated.");
1373  }
1374 
1375  if (totem_config_warnings & TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED) {
1376  log_printf (LOGSYS_LEVEL_WARNING, "nodeid appears both in totem section and nodelist. Nodelist one is used.");
1377  }
1378 
1379  if (totem_config_warnings & TOTEM_CONFIG_BINDNETADDR_NODELIST_SET) {
1380  log_printf (LOGSYS_LEVEL_WARNING, "interface section bindnetaddr is used together with nodelist. "
1381  "Nodelist one is going to be used.");
1382  }
1383 
1384  if (totem_config_warnings != 0) {
1385  log_printf (LOGSYS_LEVEL_WARNING, "Please migrate config file to nodelist.");
1386  }
1387 
1388  res = totem_config_validate (&totem_config, &error_string);
1389  if (res == -1) {
1390  log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
1392  }
1393 
1394  if (testonly) {
1396  }
1397 
1398 
1399  move_to_root_cgroup = 1;
1400  if (icmap_get_string("system.move_to_root_cgroup", &tmp_str) == CS_OK) {
1401  if (strcmp(tmp_str, "yes") != 0) {
1402  move_to_root_cgroup = 0;
1403  }
1404  free(tmp_str);
1405  }
1406 
1407  /*
1408  * Try to move corosync into root cpu cgroup. Failure is not fatal and
1409  * error is deliberately ignored.
1410  */
1411  if (move_to_root_cgroup) {
1412  (void)corosync_move_to_root_cgroup();
1413  }
1414 
1415  sched_rr = 1;
1416  if (icmap_get_string("system.sched_rr", &tmp_str) == CS_OK) {
1417  if (strcmp(tmp_str, "yes") != 0) {
1418  sched_rr = 0;
1419  }
1420  free(tmp_str);
1421  }
1422 
1423  prio = 0;
1424  if (icmap_get_string("system.priority", &tmp_str) == CS_OK) {
1425  if (strcmp(tmp_str, "max") == 0) {
1426  prio = INT_MIN;
1427  } else if (strcmp(tmp_str, "min") == 0) {
1428  prio = INT_MAX;
1429  } else {
1430  errno = 0;
1431 
1432  tmpli = strtol(tmp_str, &ep, 10);
1433  if (errno != 0 || *ep != '\0' || tmpli > INT_MAX || tmpli < INT_MIN) {
1434  log_printf (LOGSYS_LEVEL_ERROR, "Priority value %s is invalid", tmp_str);
1436  }
1437 
1438  prio = tmpli;
1439  }
1440 
1441  free(tmp_str);
1442  }
1443 
1444  /*
1445  * Set round robin realtime scheduling with priority 99
1446  */
1447  if (sched_rr) {
1448  if (corosync_set_rr_scheduler () != 0) {
1449  prio = INT_MIN;
1450  } else {
1451  prio = 0;
1452  }
1453  }
1454 
1455  if (prio != 0) {
1456  if (setpriority(PRIO_PGRP, 0, prio) != 0) {
1458  "Could not set priority %d", prio);
1459  }
1460  }
1461 
1462  totem_config.totem_memb_ring_id_create_or_load = corosync_ring_id_create_or_load;
1463  totem_config.totem_memb_ring_id_store = corosync_ring_id_store;
1464 
1467 
1475 
1477 
1478  /*
1479  * Now we are fully initialized.
1480  */
1481  if (background) {
1483 
1484  corosync_tty_detach ();
1485 
1487 
1488  log_printf (LOGSYS_LEVEL_DEBUG, "Corosync TTY detached");
1489  }
1490 
1491  /*
1492  * Lock all memory to avoid page faults which may interrupt
1493  * application healthchecking
1494  */
1495  corosync_mlockall ();
1496 
1497  corosync_poll_handle = qb_loop_create ();
1498 
1501  timer_function_scheduler_timeout (&scheduler_pause_timeout_data);
1502 
1503  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_LOW,
1504  SIGUSR2, NULL, sig_diag_handler, NULL);
1505  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1506  SIGINT, NULL, sig_exit_handler, NULL);
1507  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1508  SIGQUIT, NULL, sig_exit_handler, NULL);
1509  qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1510  SIGTERM, NULL, sig_exit_handler, NULL);
1511 
1512  if (logsys_thread_start() != 0) {
1513  log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize log thread");
1515  }
1516 
1517  if ((flock_err = corosync_flock (corosync_lock_file, getpid ())) != COROSYNC_DONE_EXIT) {
1518  corosync_exit_error (flock_err);
1519  }
1520 
1521  /*
1522  * if totempg_initialize doesn't have root priveleges, it cannot
1523  * bind to a specific interface. This only matters if
1524  * there is more then one interface in a system, so
1525  * in this case, only a warning is printed
1526  */
1527  /*
1528  * Join multicast group and setup delivery
1529  * and configuration change functions
1530  */
1531  if (totempg_initialize (
1532  corosync_poll_handle,
1533  &totem_config) != 0) {
1534 
1535  log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize TOTEM layer");
1537  }
1538 
1540  main_service_ready);
1541 
1543  &corosync_group_handle,
1544  deliver_fn,
1545  confchg_fn);
1546 
1548  corosync_group_handle,
1549  &corosync_group,
1550  1);
1551 
1552  /*
1553  * Drop root privleges to user 'corosync'
1554  * TODO: Don't really need full root capabilities;
1555  * needed capabilities are:
1556  * CAP_NET_RAW (bindtodevice)
1557  * CAP_SYS_NICE (setscheduler)
1558  * CAP_IPC_LOCK (mlockall)
1559  */
1560  priv_drop ();
1561 
1562  schedwrk_init (
1563  serialize_lock,
1564  serialize_unlock);
1565 
1566  /*
1567  * Start main processing loop
1568  */
1569  qb_loop_run (corosync_poll_handle);
1570 
1571  /*
1572  * Exit was requested
1573  */
1574  totempg_finalize ();
1575 
1576  /*
1577  * free the loop resources
1578  */
1579  qb_loop_destroy (corosync_poll_handle);
1580 
1581  /*
1582  * free up the icmap
1583  */
1584 
1585  /*
1586  * Remove pid lock file
1587  */
1588  unlink (corosync_lock_file);
1589 
1591 
1592  return EXIT_SUCCESS;
1593 }
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:11
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:781
LOGSYS_DECLARE_SYSTEM("corosync", LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_OUTPUT_SYSLOG, LOG_DAEMON, LOG_EMERG)
void corosync_state_dump(void)
Definition: main.c:193
const char * corosync_get_config_file(void)
Definition: main.c:204
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
#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:803
int main(int argc, char **argv, char **envp)
Definition: main.c:1221
int main_mcast(const struct iovec *iovec, unsigned int iov_len, unsigned int guarantee)
Definition: main.c:619
void corosync_recheck_the_q_level(void *data)
Definition: main.c:719
int message_source_is_local(const mar_message_source_t *source)
Definition: main.c:792
qb_loop_t * cs_poll_handle_get(void)
Definition: main.c:170
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:733
int cs_poll_dispatch_delete(qb_loop_t *handle, int fd)
Definition: main.c:188
void corosync_shutdown_request(void)
Definition: main.c:250
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:815
unsigned long long max_tv_diff
Definition: main.c:817
struct totem_config * totem_config
Definition: main.c:814
unsigned long long tv_prev
Definition: main.c:816
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:1733
int totem_config_validate(struct totem_config *totem_config, const char **error_string)
Definition: totemconfig.c:1948
#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:1525
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:1584
void totempg_force_gather(void)
Definition: totempg.c:1589
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:1534
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:181
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