76 #include <sys/types.h>
81 #include <sys/socket.h>
84 #include <sys/resource.h>
86 #include <netinet/in.h>
87 #include <arpa/inet.h>
96 #include <semaphore.h>
99 #ifdef HAVE_LIBSYSTEMD
100 #include <systemd/sd-daemon.h>
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>
130 #ifdef HAVE_SMALL_MEMORY_FOOTPRINT
131 #define IPC_LOGSYS_SIZE 1024*64
133 #define IPC_LOGSYS_SIZE 8192*128
148 #define SERVER_BACKLOG 5
150 static int sched_priority = 0;
152 static unsigned int service_count = 32;
158 static int sync_in_process = 1;
160 static qb_loop_t *corosync_poll_handle;
166 static const char *corosync_lock_file =
LOCALSTATEDIR"/run/corosync.pid";
168 static char corosync_config_file[PATH_MAX + 1] =
COROSYSCONFDIR "/corosync.conf";
170 static int lockfile_fd = -1;
174 return (corosync_poll_handle);
182 int (*dispatch_fn) (
int fd,
186 return qb_loop_poll_add(handle, QB_LOOP_MED, fd, events, data,
192 return qb_loop_poll_del(handle, fd);
209 return (corosync_config_file);
212 static void corosync_blackbox_write_to_file (
void)
214 char fname[PATH_MAX];
215 char fdata_fname[PATH_MAX];
216 char time_str[PATH_MAX];
217 struct tm cur_time_tm;
221 cur_time_t = time(NULL);
222 localtime_r(&cur_time_t, &cur_time_tm);
224 strftime(time_str, PATH_MAX,
"%Y-%m-%dT%H:%M:%S", &cur_time_tm);
225 if (snprintf(fname, PATH_MAX,
"%s/fdata-%s-%lld",
228 (
long long int)getpid()) >= PATH_MAX) {
233 if ((res = qb_log_blackbox_write_to_file(fname)) < 0) {
237 snprintf(fdata_fname,
sizeof(fdata_fname),
"%s/fdata",
get_state_dir());
239 if (symlink(fname, fdata_fname) == -1) {
245 static void unlink_all_completed (
void)
248 qb_loop_stop (corosync_poll_handle);
257 static int32_t sig_diag_handler (
int num,
void *data)
263 static int32_t sig_exit_handler (
int num,
void *data)
270 static void sigsegv_handler (
int num)
272 (void)signal (num, SIG_DFL);
273 corosync_blackbox_write_to_file ();
278 #define LOCALHOST_IP inet_addr("127.0.0.1")
280 static void *corosync_group_handle;
287 static void serialize_lock (
void)
291 static void serialize_unlock (
void)
295 static void corosync_sync_completed (
void)
298 "Completed service synchronization, ready to provide service.");
308 #ifdef HAVE_LIBSYSTEMD
309 sd_notify (0,
"READY=1");
313 static int corosync_sync_callbacks_retrieve (
321 if (callbacks == NULL) {
336 static void member_object_joined (
unsigned int nodeid)
343 "runtime.members.%u.ip",
nodeid);
345 "runtime.members.%u.join_count",
nodeid);
347 "runtime.members.%u.status",
nodeid);
362 static void member_object_left (
unsigned int nodeid)
367 "runtime.members.%u.status",
nodeid);
374 static void confchg_fn (
376 const unsigned int *member_list,
size_t member_list_entries,
377 const unsigned int *left_list,
size_t left_list_entries,
378 const unsigned int *joined_list,
size_t joined_list_entries,
382 int abort_activate = 0;
384 if (sync_in_process == 1) {
391 for (i = 0; i < left_list_entries; i++) {
392 member_object_left (left_list[i]);
394 for (i = 0; i < joined_list_entries; i++) {
395 member_object_joined (joined_list[i]);
400 for (i = 0; i < service_count; i++) {
403 member_list, member_list_entries,
404 left_list, left_list_entries,
405 joined_list, joined_list_entries,
ring_id);
409 if (abort_activate) {
420 static void priv_drop (
void)
425 static void corosync_tty_detach (
void)
453 devnull = open(
"/dev/null", O_RDWR);
458 if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
459 || dup2(devnull, 2) < 0) {
466 static void corosync_mlockall (
void)
469 struct rlimit rlimit;
471 rlimit.rlim_cur = RLIM_INFINITY;
472 rlimit.rlim_max = RLIM_INFINITY;
474 #ifndef RLIMIT_MEMLOCK
475 #define RLIMIT_MEMLOCK RLIMIT_VMEM
481 "Could not increase RLIMIT_MEMLOCK, not locking memory");
485 res = mlockall (MCL_CURRENT | MCL_FUTURE);
488 "Could not lock memory of service to avoid page faults");
493 static void corosync_totem_stats_updater (
void *data)
496 uint32_t total_mtt_rx_token;
497 uint32_t total_backlog_calc;
498 uint32_t total_token_holdtime;
513 cstr =
"number of multicast sendmsg failures is above threshold";
517 cstr =
"totem is continuously in gather state";
521 "Totem is unable to form a cluster because of an "
522 "operating system or network fault (reason: %s). The most common "
523 "cause of this message is that the local firewall is "
524 "configured improperly.", cstr);
530 total_mtt_rx_token = 0;
531 total_token_holdtime = 0;
532 total_backlog_calc = 0;
564 corosync_totem_stats_updater,
565 &corosync_stats_timer_handle);
568 static void corosync_totem_stats_init (
void)
572 corosync_totem_stats_updater,
573 &corosync_stats_timer_handle);
576 static void deliver_fn (
579 unsigned int msg_len,
580 int endian_conversion_required)
582 const struct qb_ipc_request_header *
header;
588 if (endian_conversion_required) {
611 if (endian_conversion_required) {
612 assert(
corosync_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
622 const struct iovec *iovec,
623 unsigned int iov_len,
626 const struct qb_ipc_request_header *req = iovec->iov_base;
630 service = req->id >> 16;
631 fn_id = req->id & 0xffff;
640 static void corosync_ring_id_create_or_load (
646 char filename[PATH_MAX];
648 snprintf (filename,
sizeof(filename),
"%s/ringid_%u",
650 fd = open (filename, O_RDONLY);
661 if ((fd == -1) || (res !=
sizeof (uint64_t))) {
663 fd = creat (filename, 0600);
669 "Couldn't write ringid file '%s'", filename);
675 "Couldn't create ringid file '%s'", filename);
684 static void corosync_ring_id_store (
688 char filename[PATH_MAX];
692 snprintf (filename,
sizeof(filename),
"%s/ringid_%u",
695 fd = creat (filename, 0600);
716 static qb_loop_timer_handle recheck_the_q_level_timer;
732 unsigned int service,
735 void *sending_allowed_private_data)
739 struct iovec reserve_iovec;
740 struct qb_ipc_request_header *
header = (
struct qb_ipc_request_header *)msg;
743 reserve_iovec.iov_base = (
char *)
header;
744 reserve_iovec.iov_len =
header->size;
747 corosync_group_handle,
758 sending_allowed = QB_FALSE;
764 sending_allowed = QB_TRUE;
766 sending_allowed = QB_TRUE;
773 return -EHOSTUNREACH;
776 return (sending_allowed);
794 assert (source != NULL);
805 assert ((source != NULL) && (conn != NULL));
818 static void timer_function_scheduler_timeout (
void *data)
821 unsigned long long tv_current;
822 unsigned long long tv_diff;
823 uint64_t schedmiss_event_tstamp;
825 tv_current = qb_util_nano_current_get ();
827 if (timeout_data->
tv_prev == 0) {
831 timeout_data->
tv_prev = tv_current;
835 tv_diff = tv_current - timeout_data->
tv_prev;
836 timeout_data->
tv_prev = tv_current;
839 schedmiss_event_tstamp = qb_util_nano_from_epoch_get() / QB_TIME_NS_IN_MSEC;
842 "(threshold is %0.4f ms). Consider token timeout increase.",
843 schedmiss_event_tstamp,
844 (
float)tv_diff / QB_TIME_NS_IN_MSEC, (
float)timeout_data->
max_tv_diff / QB_TIME_NS_IN_MSEC);
853 qb_loop_timer_add (corosync_poll_handle,
857 timer_function_scheduler_timeout,
862 static int corosync_set_rr_scheduler (
void)
866 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
869 sched_priority = sched_get_priority_max (SCHED_RR);
870 if (sched_priority != -1) {
875 "Could not set SCHED_RR at priority %d",
879 #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
880 qb_log_thread_priority_set (SCHED_OTHER, 0);
888 #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
889 res = qb_log_thread_priority_set (SCHED_RR, sched_priority);
895 "Could not set logsys thread priority."
896 " Can't continue because of priority inversions.");
902 "Could not get maximum scheduler priority");
908 "The Platform is missing process priority setting features. Leaving at default.");
918 static const char *corosync_basename(
const char *file_name)
921 base = strrchr (file_name,
'/');
930 _logsys_log_printf(
int level,
int subsys,
931 const char *function_name,
932 const char *file_name,
938 _logsys_log_printf(
int level,
int subsys,
939 const
char *function_name,
940 const
char *file_name,
942 const
char *format, ...)
946 va_start(ap, format);
947 qb_log_from_external_source_va(function_name, corosync_basename(file_name),
948 format, level, file_line,
953 static void fplay_key_change_notify_fn (
955 const char *key_name,
960 if (strcmp(key_name,
"runtime.blackbox.dump_flight_data") == 0) {
961 fprintf(stderr,
"Writetofile\n");
962 corosync_blackbox_write_to_file ();
964 if (strcmp(key_name,
"runtime.blackbox.dump_state") == 0) {
965 fprintf(stderr,
"statefump\n");
970 static void corosync_fplay_control_init (
void)
979 fplay_key_change_notify_fn,
983 fplay_key_change_notify_fn,
987 static void force_gather_notify_fn(
989 const char *key_name,
1001 if (strcmp(key_name,
"runtime.force_gather") == 0) {
1010 static void corosync_force_gather_init (
void)
1018 force_gather_notify_fn,
1029 static void set_icmap_ro_keys_flag (
void)
1064 static void main_service_ready (
void)
1077 corosync_totem_stats_init ();
1078 corosync_fplay_control_init ();
1079 corosync_force_gather_init ();
1082 corosync_sync_callbacks_retrieve,
1083 corosync_sync_completed);
1086 static enum e_corosync_done corosync_flock (
const char *lockfile, pid_t pid)
1095 lockfile_fd = open (lockfile, O_WRONLY | O_CREAT, 0640);
1096 if (lockfile_fd == -1) {
1102 lock.l_type = F_WRLCK;
1104 lock.l_whence = SEEK_SET;
1106 if (fcntl (lockfile_fd, F_SETLK, &
lock) == -1) {
1126 if (ftruncate (lockfile_fd, 0) == -1) {
1130 goto error_close_unlink;
1133 memset (pid_s, 0,
sizeof (pid_s));
1134 snprintf (pid_s,
sizeof (pid_s) - 1,
"%u\n", pid);
1137 if (write (lockfile_fd, pid_s, strlen (pid_s)) != strlen (pid_s)) {
1138 if (errno == EINTR) {
1142 "Error was %s", strerror (errno));
1144 goto error_close_unlink;
1148 if ((fd_flag = fcntl (lockfile_fd, F_GETFD, 0)) == -1) {
1150 "Error was %s", strerror (errno));
1152 goto error_close_unlink;
1154 fd_flag |= FD_CLOEXEC;
1155 if (fcntl (lockfile_fd, F_SETFD, fd_flag) == -1) {
1157 "Error was %s", strerror (errno));
1159 goto error_close_unlink;
1167 close (lockfile_fd);
1172 static int corosync_move_to_root_cgroup(
void) {
1183 f = fopen(
"/sys/fs/cgroup/cpu/cpu.rt_runtime_us",
"rt");
1186 "system without cgroup or with disabled CONFIG_RT_GROUP_SCHED");
1193 f = fopen(
"/sys/fs/cgroup/cpu/tasks",
"w");
1200 if (fprintf(f,
"%jd\n", (intmax_t)getpid()) <= 0) {
1203 goto close_and_exit_res;
1207 if (fclose(f) != 0) {
1218 int main (
int argc,
char **argv,
char **envp)
1220 const char *error_string;
1223 int background, sched_rr, prio, testonly, move_to_root_cgroup;
1225 uint64_t totem_config_warnings;
1230 int log_subsys_id_totem;
1237 while ((ch = getopt (argc, argv,
"c:ftv")) != EOF) {
1241 res = snprintf(corosync_config_file,
sizeof(corosync_config_file),
"%s", optarg);
1242 if (res >=
sizeof(corosync_config_file)) {
1243 fprintf (stderr,
"Config file path too long.\n");
1247 return EXIT_FAILURE;
1257 printf (
"Corosync Cluster Engine, version '%s'\n",
VERSION);
1258 printf (
"Copyright (c) 2006-2018 Red Hat, Inc.\n");
1260 return EXIT_SUCCESS;
1266 " -c : Corosync config file path.\n"\
1267 " -f : Start application in foreground.\n"\
1268 " -t : Test configuration and exit.\n"\
1269 " -v : Display version and SVN revision of Corosync and exit.\n");
1271 return EXIT_FAILURE;
1279 (void)signal (SIGSEGV, sigsegv_handler);
1280 (void)signal (SIGABRT, sigsegv_handler);
1281 #if MSG_NOSIGNAL != 0
1282 (void)signal (SIGPIPE, SIG_IGN);
1286 fprintf (stderr,
"Corosync Executive couldn't initialize configuration component.\n");
1287 syslog (
LOGSYS_LEVEL_ERROR,
"Corosync Executive couldn't initialize configuration component.");
1290 set_icmap_ro_keys_flag();
1303 fprintf (stderr,
"%s\n", error_string);
1309 fprintf (stderr,
"Corosync Executive couldn't initialize statistics component.\n");
1310 syslog (
LOGSYS_LEVEL_ERROR,
"Corosync Executive couldn't initialize statistics component.");
1324 fprintf(stderr,
"%s", error_string);
1338 "totemip.c,totemconfig.c,totemcrypto.c,totemsrp.c,"
1339 "totempg.c,totemudp.c,totemudpu.c,totemnet.c,totemknet.c");
1367 "Nodelist one is going to be used.");
1370 if (totem_config_warnings != 0) {
1385 move_to_root_cgroup = 1;
1387 if (strcmp(tmp_str,
"yes") != 0) {
1388 move_to_root_cgroup = 0;
1397 if (move_to_root_cgroup) {
1398 (void)corosync_move_to_root_cgroup();
1403 if (strcmp(tmp_str,
"yes") != 0) {
1411 if (strcmp(tmp_str,
"max") == 0) {
1413 }
else if (strcmp(tmp_str,
"min") == 0) {
1418 tmpli = strtol(tmp_str, &ep, 10);
1419 if (errno != 0 || *ep !=
'\0' || tmpli > INT_MAX || tmpli < INT_MIN) {
1434 if (corosync_set_rr_scheduler () != 0) {
1442 if (setpriority(PRIO_PGRP, 0, prio) != 0) {
1444 "Could not set priority %d", prio);
1470 corosync_tty_detach ();
1481 corosync_mlockall ();
1483 corosync_poll_handle = qb_loop_create ();
1489 qb_loop_signal_add(corosync_poll_handle, QB_LOOP_LOW,
1490 SIGUSR2, NULL, sig_diag_handler, NULL);
1491 qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1492 SIGINT, NULL, sig_exit_handler, NULL);
1493 qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1494 SIGQUIT, NULL, sig_exit_handler, NULL);
1495 qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
1496 SIGTERM, NULL, sig_exit_handler, NULL);
1503 if ((flock_err = corosync_flock (corosync_lock_file, getpid ())) !=
COROSYNC_DONE_EXIT) {
1518 corosync_poll_handle,
1526 main_service_ready);
1529 &corosync_group_handle,
1534 corosync_group_handle,
1555 qb_loop_run (corosync_poll_handle);
1565 qb_loop_destroy (corosync_poll_handle);
1574 close (lockfile_fd);
1575 unlink (corosync_lock_file);
1579 return EXIT_SUCCESS;