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