main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. /**
  36. * \mainpage Corosync
  37. *
  38. * This is the doxygen generated developer documentation for the Corosync
  39. * project. For more information about Corosync, please see the project
  40. * web site, <a href="http://www.corosync.org">corosync.org</a>.
  41. *
  42. * \section license License
  43. *
  44. * This software licensed under BSD license, the text of which follows:
  45. *
  46. * Redistribution and use in source and binary forms, with or without
  47. * modification, are permitted provided that the following conditions are met:
  48. *
  49. * - Redistributions of source code must retain the above copyright notice,
  50. * this list of conditions and the following disclaimer.
  51. * - Redistributions in binary form must reproduce the above copyright notice,
  52. * this list of conditions and the following disclaimer in the documentation
  53. * and/or other materials provided with the distribution.
  54. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  55. * contributors may be used to endorse or promote products derived from this
  56. * software without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  59. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  62. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  63. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  64. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  65. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  66. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  67. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  68. * THE POSSIBILITY OF SUCH DAMAGE.
  69. */
  70. #include <config.h>
  71. #include <pthread.h>
  72. #include <assert.h>
  73. #include <sys/types.h>
  74. #include <sys/file.h>
  75. #include <sys/poll.h>
  76. #include <sys/uio.h>
  77. #include <sys/mman.h>
  78. #include <sys/socket.h>
  79. #include <sys/un.h>
  80. #include <sys/time.h>
  81. #include <sys/resource.h>
  82. #include <sys/stat.h>
  83. #include <netinet/in.h>
  84. #include <arpa/inet.h>
  85. #include <unistd.h>
  86. #include <fcntl.h>
  87. #include <stdlib.h>
  88. #include <stdio.h>
  89. #include <errno.h>
  90. #include <signal.h>
  91. #include <sched.h>
  92. #include <time.h>
  93. #include <semaphore.h>
  94. #include <string.h>
  95. #ifdef HAVE_LIBSYSTEMD
  96. #include <systemd/sd-daemon.h>
  97. #endif
  98. #include <qb/qbdefs.h>
  99. #include <qb/qblog.h>
  100. #include <qb/qbloop.h>
  101. #include <qb/qbutil.h>
  102. #include <qb/qbipcs.h>
  103. #include <corosync/swab.h>
  104. #include <corosync/corotypes.h>
  105. #include <corosync/corodefs.h>
  106. #include <corosync/totem/totempg.h>
  107. #include <corosync/logsys.h>
  108. #include <corosync/icmap.h>
  109. #include "quorum.h"
  110. #include "totemsrp.h"
  111. #include "logconfig.h"
  112. #include "totemconfig.h"
  113. #include "main.h"
  114. #include "sync.h"
  115. #include "timer.h"
  116. #include "util.h"
  117. #include "apidef.h"
  118. #include "service.h"
  119. #include "schedwrk.h"
  120. #include "ipcs_stats.h"
  121. #include "stats.h"
  122. #ifdef HAVE_SMALL_MEMORY_FOOTPRINT
  123. #define IPC_LOGSYS_SIZE 1024*64
  124. #else
  125. #define IPC_LOGSYS_SIZE 8192*128
  126. #endif
  127. /*
  128. * LibQB adds default "*" syslog filter so we have to set syslog_priority as low
  129. * as possible so filters applied later in _logsys_config_apply_per_file takes
  130. * effect.
  131. */
  132. LOGSYS_DECLARE_SYSTEM ("corosync",
  133. LOGSYS_MODE_OUTPUT_STDERR | LOGSYS_MODE_OUTPUT_SYSLOG,
  134. LOG_DAEMON,
  135. LOG_EMERG);
  136. LOGSYS_DECLARE_SUBSYS ("MAIN");
  137. #define SERVER_BACKLOG 5
  138. static int sched_priority = 0;
  139. static unsigned int service_count = 32;
  140. static struct totem_logging_configuration totem_logging_configuration;
  141. static struct corosync_api_v1 *api = NULL;
  142. static int sync_in_process = 1;
  143. static qb_loop_t *corosync_poll_handle;
  144. struct sched_param global_sched_param;
  145. static corosync_timer_handle_t corosync_stats_timer_handle;
  146. static const char *corosync_lock_file = LOCALSTATEDIR"/run/corosync.pid";
  147. static int ip_version = AF_INET;
  148. static char corosync_config_file[PATH_MAX + 1] = COROSYSCONFDIR "/corosync.conf";
  149. qb_loop_t *cs_poll_handle_get (void)
  150. {
  151. return (corosync_poll_handle);
  152. }
  153. int cs_poll_dispatch_add (qb_loop_t * handle,
  154. int fd,
  155. int events,
  156. void *data,
  157. int (*dispatch_fn) (int fd,
  158. int revents,
  159. void *data))
  160. {
  161. return qb_loop_poll_add(handle, QB_LOOP_MED, fd, events, data,
  162. dispatch_fn);
  163. }
  164. int cs_poll_dispatch_delete(qb_loop_t * handle, int fd)
  165. {
  166. return qb_loop_poll_del(handle, fd);
  167. }
  168. void corosync_state_dump (void)
  169. {
  170. int i;
  171. for (i = 0; i < SERVICES_COUNT_MAX; i++) {
  172. if (corosync_service[i] && corosync_service[i]->exec_dump_fn) {
  173. corosync_service[i]->exec_dump_fn ();
  174. }
  175. }
  176. }
  177. const char *corosync_get_config_file(void)
  178. {
  179. return (corosync_config_file);
  180. }
  181. static void corosync_blackbox_write_to_file (void)
  182. {
  183. char fname[PATH_MAX];
  184. char fdata_fname[PATH_MAX];
  185. char time_str[PATH_MAX];
  186. struct tm cur_time_tm;
  187. time_t cur_time_t;
  188. ssize_t res;
  189. cur_time_t = time(NULL);
  190. localtime_r(&cur_time_t, &cur_time_tm);
  191. strftime(time_str, PATH_MAX, "%Y-%m-%dT%H:%M:%S", &cur_time_tm);
  192. if (snprintf(fname, PATH_MAX, "%s/fdata-%s-%lld",
  193. get_run_dir(),
  194. time_str,
  195. (long long int)getpid()) >= PATH_MAX) {
  196. log_printf(LOGSYS_LEVEL_ERROR, "Can't snprintf blackbox file name");
  197. return ;
  198. }
  199. if ((res = qb_log_blackbox_write_to_file(fname)) < 0) {
  200. LOGSYS_PERROR(-res, LOGSYS_LEVEL_ERROR, "Can't store blackbox file");
  201. return ;
  202. }
  203. snprintf(fdata_fname, sizeof(fdata_fname), "%s/fdata", get_run_dir());
  204. unlink(fdata_fname);
  205. if (symlink(fname, fdata_fname) == -1) {
  206. log_printf(LOGSYS_LEVEL_ERROR, "Can't create symlink to '%s' for corosync blackbox file '%s'",
  207. fname, fdata_fname);
  208. }
  209. }
  210. static void unlink_all_completed (void)
  211. {
  212. api->timer_delete (corosync_stats_timer_handle);
  213. qb_loop_stop (corosync_poll_handle);
  214. icmap_fini();
  215. }
  216. void corosync_shutdown_request (void)
  217. {
  218. corosync_service_unlink_all (api, unlink_all_completed);
  219. }
  220. static int32_t sig_diag_handler (int num, void *data)
  221. {
  222. corosync_state_dump ();
  223. return 0;
  224. }
  225. static int32_t sig_exit_handler (int num, void *data)
  226. {
  227. log_printf(LOGSYS_LEVEL_NOTICE, "Node was shut down by a signal");
  228. corosync_service_unlink_all (api, unlink_all_completed);
  229. return 0;
  230. }
  231. static void sigsegv_handler (int num)
  232. {
  233. (void)signal (num, SIG_DFL);
  234. corosync_blackbox_write_to_file ();
  235. qb_log_fini();
  236. raise (num);
  237. }
  238. #define LOCALHOST_IP inet_addr("127.0.0.1")
  239. static void *corosync_group_handle;
  240. static struct totempg_group corosync_group = {
  241. .group = "a",
  242. .group_len = 1
  243. };
  244. static void serialize_lock (void)
  245. {
  246. }
  247. static void serialize_unlock (void)
  248. {
  249. }
  250. static void corosync_sync_completed (void)
  251. {
  252. log_printf (LOGSYS_LEVEL_NOTICE,
  253. "Completed service synchronization, ready to provide service.");
  254. sync_in_process = 0;
  255. cs_ipcs_sync_state_changed(sync_in_process);
  256. cs_ipc_allow_connections(1);
  257. /*
  258. * Inform totem to start using new message queue again
  259. */
  260. totempg_trans_ack();
  261. #ifdef HAVE_LIBSYSTEMD
  262. sd_notify (0, "READY=1");
  263. #endif
  264. }
  265. static int corosync_sync_callbacks_retrieve (
  266. int service_id,
  267. struct sync_callbacks *callbacks)
  268. {
  269. if (corosync_service[service_id] == NULL) {
  270. return (-1);
  271. }
  272. if (callbacks == NULL) {
  273. return (0);
  274. }
  275. callbacks->name = corosync_service[service_id]->name;
  276. callbacks->sync_init = corosync_service[service_id]->sync_init;
  277. callbacks->sync_process = corosync_service[service_id]->sync_process;
  278. callbacks->sync_activate = corosync_service[service_id]->sync_activate;
  279. callbacks->sync_abort = corosync_service[service_id]->sync_abort;
  280. return (0);
  281. }
  282. static struct memb_ring_id corosync_ring_id;
  283. static void member_object_joined (unsigned int nodeid)
  284. {
  285. char member_ip[ICMAP_KEYNAME_MAXLEN];
  286. char member_join_count[ICMAP_KEYNAME_MAXLEN];
  287. char member_status[ICMAP_KEYNAME_MAXLEN];
  288. snprintf(member_ip, ICMAP_KEYNAME_MAXLEN,
  289. "runtime.members.%u.ip", nodeid);
  290. snprintf(member_join_count, ICMAP_KEYNAME_MAXLEN,
  291. "runtime.members.%u.join_count", nodeid);
  292. snprintf(member_status, ICMAP_KEYNAME_MAXLEN,
  293. "runtime.members.%u.status", nodeid);
  294. if (icmap_get(member_ip, NULL, NULL, NULL) == CS_OK) {
  295. icmap_inc(member_join_count);
  296. icmap_set_string(member_status, "joined");
  297. } else {
  298. icmap_set_string(member_ip, (char*)api->totem_ifaces_print (nodeid));
  299. icmap_set_uint32(member_join_count, 1);
  300. icmap_set_string(member_status, "joined");
  301. }
  302. log_printf (LOGSYS_LEVEL_DEBUG,
  303. "Member joined: %s", api->totem_ifaces_print (nodeid));
  304. }
  305. static void member_object_left (unsigned int nodeid)
  306. {
  307. char member_status[ICMAP_KEYNAME_MAXLEN];
  308. snprintf(member_status, ICMAP_KEYNAME_MAXLEN,
  309. "runtime.members.%u.status", nodeid);
  310. icmap_set_string(member_status, "left");
  311. log_printf (LOGSYS_LEVEL_DEBUG,
  312. "Member left: %s", api->totem_ifaces_print (nodeid));
  313. }
  314. static void confchg_fn (
  315. enum totem_configuration_type configuration_type,
  316. const unsigned int *member_list, size_t member_list_entries,
  317. const unsigned int *left_list, size_t left_list_entries,
  318. const unsigned int *joined_list, size_t joined_list_entries,
  319. const struct memb_ring_id *ring_id)
  320. {
  321. int i;
  322. int abort_activate = 0;
  323. if (sync_in_process == 1) {
  324. abort_activate = 1;
  325. }
  326. sync_in_process = 1;
  327. cs_ipcs_sync_state_changed(sync_in_process);
  328. memcpy (&corosync_ring_id, ring_id, sizeof (struct memb_ring_id));
  329. for (i = 0; i < left_list_entries; i++) {
  330. member_object_left (left_list[i]);
  331. }
  332. for (i = 0; i < joined_list_entries; i++) {
  333. member_object_joined (joined_list[i]);
  334. }
  335. /*
  336. * Call configuration change for all services
  337. */
  338. for (i = 0; i < service_count; i++) {
  339. if (corosync_service[i] && corosync_service[i]->confchg_fn) {
  340. corosync_service[i]->confchg_fn (configuration_type,
  341. member_list, member_list_entries,
  342. left_list, left_list_entries,
  343. joined_list, joined_list_entries, ring_id);
  344. }
  345. }
  346. if (abort_activate) {
  347. sync_abort ();
  348. }
  349. if (configuration_type == TOTEM_CONFIGURATION_TRANSITIONAL) {
  350. sync_save_transitional (member_list, member_list_entries, ring_id);
  351. }
  352. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  353. sync_start (member_list, member_list_entries, ring_id);
  354. }
  355. }
  356. static void priv_drop (void)
  357. {
  358. return; /* TODO: we are still not dropping privs */
  359. }
  360. static void corosync_tty_detach (void)
  361. {
  362. int devnull;
  363. /*
  364. * Disconnect from TTY if this is not a debug run
  365. */
  366. switch (fork ()) {
  367. case -1:
  368. corosync_exit_error (COROSYNC_DONE_FORK);
  369. break;
  370. case 0:
  371. /*
  372. * child which is disconnected, run this process
  373. */
  374. break;
  375. default:
  376. exit (0);
  377. break;
  378. }
  379. /* Create new session */
  380. (void)setsid();
  381. /*
  382. * Map stdin/out/err to /dev/null.
  383. */
  384. devnull = open("/dev/null", O_RDWR);
  385. if (devnull == -1) {
  386. corosync_exit_error (COROSYNC_DONE_STD_TO_NULL_REDIR);
  387. }
  388. if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
  389. || dup2(devnull, 2) < 0) {
  390. close(devnull);
  391. corosync_exit_error (COROSYNC_DONE_STD_TO_NULL_REDIR);
  392. }
  393. close(devnull);
  394. }
  395. static void corosync_mlockall (void)
  396. {
  397. int res;
  398. struct rlimit rlimit;
  399. rlimit.rlim_cur = RLIM_INFINITY;
  400. rlimit.rlim_max = RLIM_INFINITY;
  401. #ifndef RLIMIT_MEMLOCK
  402. #define RLIMIT_MEMLOCK RLIMIT_VMEM
  403. #endif
  404. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  405. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  406. if (res == -1) {
  407. LOGSYS_PERROR (errno, LOGSYS_LEVEL_WARNING,
  408. "Could not lock memory of service to avoid page faults");
  409. };
  410. }
  411. static void corosync_totem_stats_updater (void *data)
  412. {
  413. totempg_stats_t * stats;
  414. uint32_t total_mtt_rx_token;
  415. uint32_t total_backlog_calc;
  416. uint32_t total_token_holdtime;
  417. int t, prev;
  418. int32_t token_count;
  419. const char *cstr;
  420. stats = api->totem_get_stats();
  421. stats->srp->firewall_enabled_or_nic_failure = stats->srp->continuous_gather > MAX_NO_CONT_GATHER ? 1 : 0;
  422. if (stats->srp->continuous_gather > MAX_NO_CONT_GATHER ||
  423. stats->srp->continuous_sendmsg_failures > MAX_NO_CONT_SENDMSG_FAILURES) {
  424. cstr = "";
  425. if (stats->srp->continuous_sendmsg_failures > MAX_NO_CONT_SENDMSG_FAILURES) {
  426. cstr = "number of multicast sendmsg failures is above threshold";
  427. }
  428. if (stats->srp->continuous_gather > MAX_NO_CONT_GATHER) {
  429. cstr = "totem is continuously in gather state";
  430. }
  431. log_printf (LOGSYS_LEVEL_WARNING,
  432. "Totem is unable to form a cluster because of an "
  433. "operating system or network fault (reason: %s). The most common "
  434. "cause of this message is that the local firewall is "
  435. "configured improperly.", cstr);
  436. stats->srp->firewall_enabled_or_nic_failure = 1;
  437. } else {
  438. stats->srp->firewall_enabled_or_nic_failure = 0;
  439. }
  440. total_mtt_rx_token = 0;
  441. total_token_holdtime = 0;
  442. total_backlog_calc = 0;
  443. token_count = 0;
  444. t = stats->srp->latest_token;
  445. while (1) {
  446. if (t == 0)
  447. prev = TOTEM_TOKEN_STATS_MAX - 1;
  448. else
  449. prev = t - 1;
  450. if (prev == stats->srp->earliest_token)
  451. break;
  452. /* if tx == 0, then dropped token (not ours) */
  453. if (stats->srp->token[t].tx != 0 ||
  454. (stats->srp->token[t].rx - stats->srp->token[prev].rx) > 0 ) {
  455. total_mtt_rx_token += (stats->srp->token[t].rx - stats->srp->token[prev].rx);
  456. total_token_holdtime += (stats->srp->token[t].tx - stats->srp->token[t].rx);
  457. total_backlog_calc += stats->srp->token[t].backlog_calc;
  458. token_count++;
  459. }
  460. t = prev;
  461. }
  462. if (token_count) {
  463. stats->srp->mtt_rx_token = (total_mtt_rx_token / token_count);
  464. stats->srp->avg_token_workload = (total_token_holdtime / token_count);
  465. stats->srp->avg_backlog_calc = (total_backlog_calc / token_count);
  466. }
  467. stats->srp->time_since_token_last_received = qb_util_nano_current_get () / QB_TIME_NS_IN_MSEC -
  468. stats->srp->token[stats->srp->latest_token].rx;
  469. stats_trigger_trackers();
  470. api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
  471. corosync_totem_stats_updater,
  472. &corosync_stats_timer_handle);
  473. }
  474. static void corosync_totem_stats_init (void)
  475. {
  476. /* start stats timer */
  477. api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
  478. corosync_totem_stats_updater,
  479. &corosync_stats_timer_handle);
  480. }
  481. static void deliver_fn (
  482. unsigned int nodeid,
  483. const void *msg,
  484. unsigned int msg_len,
  485. int endian_conversion_required)
  486. {
  487. const struct qb_ipc_request_header *header;
  488. int32_t service;
  489. int32_t fn_id;
  490. uint32_t id;
  491. header = msg;
  492. if (endian_conversion_required) {
  493. id = swab32 (header->id);
  494. } else {
  495. id = header->id;
  496. }
  497. /*
  498. * Call the proper executive handler
  499. */
  500. service = id >> 16;
  501. fn_id = id & 0xffff;
  502. if (!corosync_service[service]) {
  503. return;
  504. }
  505. if (fn_id >= corosync_service[service]->exec_engine_count) {
  506. log_printf(LOGSYS_LEVEL_WARNING, "discarded unknown message %d for service %d (max id %d)",
  507. fn_id, service, corosync_service[service]->exec_engine_count);
  508. return;
  509. }
  510. icmap_fast_inc(service_stats_rx[service][fn_id]);
  511. if (endian_conversion_required) {
  512. assert(corosync_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  513. corosync_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  514. ((void *)msg);
  515. }
  516. corosync_service[service]->exec_engine[fn_id].exec_handler_fn
  517. (msg, nodeid);
  518. }
  519. int main_mcast (
  520. const struct iovec *iovec,
  521. unsigned int iov_len,
  522. unsigned int guarantee)
  523. {
  524. const struct qb_ipc_request_header *req = iovec->iov_base;
  525. int32_t service;
  526. int32_t fn_id;
  527. service = req->id >> 16;
  528. fn_id = req->id & 0xffff;
  529. if (corosync_service[service]) {
  530. icmap_fast_inc(service_stats_tx[service][fn_id]);
  531. }
  532. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  533. }
  534. static void corosync_ring_id_create_or_load (
  535. struct memb_ring_id *memb_ring_id,
  536. unsigned int nodeid)
  537. {
  538. int fd;
  539. int res = 0;
  540. char filename[PATH_MAX];
  541. snprintf (filename, sizeof(filename), "%s/ringid_%u",
  542. get_run_dir(), nodeid);
  543. fd = open (filename, O_RDONLY, 0700);
  544. /*
  545. * If file can be opened and read, read the ring id
  546. */
  547. if (fd != -1) {
  548. res = read (fd, &memb_ring_id->seq, sizeof (uint64_t));
  549. close (fd);
  550. }
  551. /*
  552. * If file could not be opened or read, create a new ring id
  553. */
  554. if ((fd == -1) || (res != sizeof (uint64_t))) {
  555. memb_ring_id->seq = 0;
  556. umask(0);
  557. fd = open (filename, O_CREAT|O_RDWR, 0700);
  558. if (fd != -1) {
  559. res = write (fd, &memb_ring_id->seq, sizeof (uint64_t));
  560. close (fd);
  561. if (res == -1) {
  562. LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
  563. "Couldn't write ringid file '%s'", filename);
  564. corosync_exit_error (COROSYNC_DONE_STORE_RINGID);
  565. }
  566. } else {
  567. LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
  568. "Couldn't create ringid file '%s'", filename);
  569. corosync_exit_error (COROSYNC_DONE_STORE_RINGID);
  570. }
  571. }
  572. memb_ring_id->rep = nodeid;
  573. }
  574. static void corosync_ring_id_store (
  575. const struct memb_ring_id *memb_ring_id,
  576. unsigned int nodeid)
  577. {
  578. char filename[PATH_MAX];
  579. int fd;
  580. int res;
  581. snprintf (filename, sizeof(filename), "%s/ringid_%u",
  582. get_run_dir(), nodeid);
  583. fd = open (filename, O_WRONLY, 0700);
  584. if (fd == -1) {
  585. fd = open (filename, O_CREAT|O_RDWR, 0700);
  586. }
  587. if (fd == -1) {
  588. LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR,
  589. "Couldn't store new ring id %llx to stable storage",
  590. memb_ring_id->seq);
  591. corosync_exit_error (COROSYNC_DONE_STORE_RINGID);
  592. }
  593. log_printf (LOGSYS_LEVEL_DEBUG,
  594. "Storing new sequence id for ring %llx", memb_ring_id->seq);
  595. res = write (fd, &memb_ring_id->seq, sizeof(memb_ring_id->seq));
  596. close (fd);
  597. if (res != sizeof(memb_ring_id->seq)) {
  598. LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR,
  599. "Couldn't store new ring id %llx to stable storage",
  600. memb_ring_id->seq);
  601. corosync_exit_error (COROSYNC_DONE_STORE_RINGID);
  602. }
  603. }
  604. static qb_loop_timer_handle recheck_the_q_level_timer;
  605. void corosync_recheck_the_q_level(void *data)
  606. {
  607. totempg_check_q_level(corosync_group_handle);
  608. if (cs_ipcs_q_level_get() == TOTEM_Q_LEVEL_CRITICAL) {
  609. qb_loop_timer_add(cs_poll_handle_get(), QB_LOOP_MED, 1*QB_TIME_NS_IN_MSEC,
  610. NULL, corosync_recheck_the_q_level, &recheck_the_q_level_timer);
  611. }
  612. }
  613. struct sending_allowed_private_data_struct {
  614. int reserved_msgs;
  615. };
  616. int corosync_sending_allowed (
  617. unsigned int service,
  618. unsigned int id,
  619. const void *msg,
  620. void *sending_allowed_private_data)
  621. {
  622. struct sending_allowed_private_data_struct *pd =
  623. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  624. struct iovec reserve_iovec;
  625. struct qb_ipc_request_header *header = (struct qb_ipc_request_header *)msg;
  626. int sending_allowed;
  627. reserve_iovec.iov_base = (char *)header;
  628. reserve_iovec.iov_len = header->size;
  629. pd->reserved_msgs = totempg_groups_joined_reserve (
  630. corosync_group_handle,
  631. &reserve_iovec, 1);
  632. if (pd->reserved_msgs == -1) {
  633. return -EINVAL;
  634. }
  635. /* Message ID out of range */
  636. if (id >= corosync_service[service]->lib_engine_count) {
  637. return -EINVAL;
  638. }
  639. sending_allowed = QB_FALSE;
  640. if (corosync_quorum_is_quorate() == 1 ||
  641. corosync_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) {
  642. // we are quorate
  643. // now check flow control
  644. if (corosync_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) {
  645. sending_allowed = QB_TRUE;
  646. } else if (pd->reserved_msgs && sync_in_process == 0) {
  647. sending_allowed = QB_TRUE;
  648. } else if (pd->reserved_msgs == 0) {
  649. return -ENOBUFS;
  650. } else /* (sync_in_process) */ {
  651. return -EINPROGRESS;
  652. }
  653. } else {
  654. return -EHOSTUNREACH;
  655. }
  656. return (sending_allowed);
  657. }
  658. void corosync_sending_allowed_release (void *sending_allowed_private_data)
  659. {
  660. struct sending_allowed_private_data_struct *pd =
  661. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  662. if (pd->reserved_msgs == -1) {
  663. return;
  664. }
  665. totempg_groups_joined_release (pd->reserved_msgs);
  666. }
  667. int message_source_is_local (const mar_message_source_t *source)
  668. {
  669. int ret = 0;
  670. assert (source != NULL);
  671. if (source->nodeid == totempg_my_nodeid_get ()) {
  672. ret = 1;
  673. }
  674. return ret;
  675. }
  676. void message_source_set (
  677. mar_message_source_t *source,
  678. void *conn)
  679. {
  680. assert ((source != NULL) && (conn != NULL));
  681. memset (source, 0, sizeof (mar_message_source_t));
  682. source->nodeid = totempg_my_nodeid_get ();
  683. source->conn = conn;
  684. }
  685. struct scheduler_pause_timeout_data {
  686. struct totem_config *totem_config;
  687. qb_loop_timer_handle handle;
  688. unsigned long long tv_prev;
  689. unsigned long long max_tv_diff;
  690. };
  691. static void timer_function_scheduler_timeout (void *data)
  692. {
  693. struct scheduler_pause_timeout_data *timeout_data = (struct scheduler_pause_timeout_data *)data;
  694. unsigned long long tv_current;
  695. unsigned long long tv_diff;
  696. tv_current = qb_util_nano_current_get ();
  697. if (timeout_data->tv_prev == 0) {
  698. /*
  699. * Initial call -> just pretent everything is ok
  700. */
  701. timeout_data->tv_prev = tv_current;
  702. timeout_data->max_tv_diff = 0;
  703. }
  704. tv_diff = tv_current - timeout_data->tv_prev;
  705. timeout_data->tv_prev = tv_current;
  706. if (tv_diff > timeout_data->max_tv_diff) {
  707. log_printf (LOGSYS_LEVEL_WARNING, "Corosync main process was not scheduled for %0.4f ms "
  708. "(threshold is %0.4f ms). Consider token timeout increase.",
  709. (float)tv_diff / QB_TIME_NS_IN_MSEC, (float)timeout_data->max_tv_diff / QB_TIME_NS_IN_MSEC);
  710. }
  711. /*
  712. * Set next threshold, because token_timeout can change
  713. */
  714. timeout_data->max_tv_diff = timeout_data->totem_config->token_timeout * QB_TIME_NS_IN_MSEC * 0.8;
  715. qb_loop_timer_add (corosync_poll_handle,
  716. QB_LOOP_MED,
  717. timeout_data->totem_config->token_timeout * QB_TIME_NS_IN_MSEC / 3,
  718. timeout_data,
  719. timer_function_scheduler_timeout,
  720. &timeout_data->handle);
  721. }
  722. static int corosync_set_rr_scheduler (void)
  723. {
  724. int ret_val = 0;
  725. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  726. int res;
  727. sched_priority = sched_get_priority_max (SCHED_RR);
  728. if (sched_priority != -1) {
  729. global_sched_param.sched_priority = sched_priority;
  730. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  731. if (res == -1) {
  732. LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING,
  733. "Could not set SCHED_RR at priority %d",
  734. global_sched_param.sched_priority);
  735. global_sched_param.sched_priority = 0;
  736. #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
  737. qb_log_thread_priority_set (SCHED_OTHER, 0);
  738. #endif
  739. ret_val = -1;
  740. } else {
  741. /*
  742. * Turn on SCHED_RR in logsys system
  743. */
  744. #ifdef HAVE_QB_LOG_THREAD_PRIORITY_SET
  745. res = qb_log_thread_priority_set (SCHED_RR, sched_priority);
  746. #else
  747. res = -1;
  748. #endif
  749. if (res == -1) {
  750. log_printf (LOGSYS_LEVEL_ERROR,
  751. "Could not set logsys thread priority."
  752. " Can't continue because of priority inversions.");
  753. corosync_exit_error (COROSYNC_DONE_LOGSETUP);
  754. }
  755. }
  756. } else {
  757. LOGSYS_PERROR (errno, LOGSYS_LEVEL_WARNING,
  758. "Could not get maximum scheduler priority");
  759. sched_priority = 0;
  760. ret_val = -1;
  761. }
  762. #else
  763. log_printf(LOGSYS_LEVEL_WARNING,
  764. "The Platform is missing process priority setting features. Leaving at default.");
  765. ret_val = -1;
  766. #endif
  767. return (ret_val);
  768. }
  769. /* The basename man page contains scary warnings about
  770. thread-safety and portability, hence this */
  771. static const char *corosync_basename(const char *file_name)
  772. {
  773. char *base;
  774. base = strrchr (file_name, '/');
  775. if (base) {
  776. return base + 1;
  777. }
  778. return file_name;
  779. }
  780. static void
  781. _logsys_log_printf(int level, int subsys,
  782. const char *function_name,
  783. const char *file_name,
  784. int file_line,
  785. const char *format,
  786. ...) __attribute__((format(printf, 6, 7)));
  787. static void
  788. _logsys_log_printf(int level, int subsys,
  789. const char *function_name,
  790. const char *file_name,
  791. int file_line,
  792. const char *format, ...)
  793. {
  794. va_list ap;
  795. va_start(ap, format);
  796. qb_log_from_external_source_va(function_name, corosync_basename(file_name),
  797. format, level, file_line,
  798. subsys, ap);
  799. va_end(ap);
  800. }
  801. static void fplay_key_change_notify_fn (
  802. int32_t event,
  803. const char *key_name,
  804. struct icmap_notify_value new_val,
  805. struct icmap_notify_value old_val,
  806. void *user_data)
  807. {
  808. if (strcmp(key_name, "runtime.blackbox.dump_flight_data") == 0) {
  809. fprintf(stderr,"Writetofile\n");
  810. corosync_blackbox_write_to_file ();
  811. }
  812. if (strcmp(key_name, "runtime.blackbox.dump_state") == 0) {
  813. fprintf(stderr,"statefump\n");
  814. corosync_state_dump ();
  815. }
  816. }
  817. static void corosync_fplay_control_init (void)
  818. {
  819. icmap_track_t track = NULL;
  820. icmap_set_string("runtime.blackbox.dump_flight_data", "no");
  821. icmap_set_string("runtime.blackbox.dump_state", "no");
  822. icmap_track_add("runtime.blackbox.dump_flight_data",
  823. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY,
  824. fplay_key_change_notify_fn,
  825. NULL, &track);
  826. icmap_track_add("runtime.blackbox.dump_state",
  827. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY,
  828. fplay_key_change_notify_fn,
  829. NULL, &track);
  830. }
  831. static void force_gather_notify_fn(
  832. int32_t event,
  833. const char *key_name,
  834. struct icmap_notify_value new_val,
  835. struct icmap_notify_value old_val,
  836. void *user_data)
  837. {
  838. char *key_val;
  839. if (icmap_get_string(key_name, &key_val) == CS_OK && strcmp(key_val, "no") == 0)
  840. goto out;
  841. icmap_set_string("runtime.force_gather", "no");
  842. if (strcmp(key_name, "runtime.force_gather") == 0) {
  843. log_printf(LOGSYS_LEVEL_ERROR, "Forcing into GATHER state\n");
  844. totempg_force_gather();
  845. }
  846. out:
  847. free(key_val);
  848. }
  849. static void corosync_force_gather_init (void)
  850. {
  851. icmap_track_t track = NULL;
  852. icmap_set_string("runtime.force_gather", "no");
  853. icmap_track_add("runtime.force_gather",
  854. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY,
  855. force_gather_notify_fn,
  856. NULL, &track);
  857. }
  858. /*
  859. * Set RO flag for keys, which ether doesn't make sense to change by user (statistic)
  860. * or which when changed are not reflected by runtime (totem.crypto_cipher, ...).
  861. *
  862. * Also some RO keys cannot be determined in this stage, so they are set later in
  863. * other functions (like nodelist.local_node_pos, ...)
  864. */
  865. static void set_icmap_ro_keys_flag (void)
  866. {
  867. /*
  868. * Set RO flag for all keys of internal configuration and runtime statistics
  869. */
  870. icmap_set_ro_access("internal_configuration.", CS_TRUE, CS_TRUE);
  871. icmap_set_ro_access("runtime.services.", CS_TRUE, CS_TRUE);
  872. icmap_set_ro_access("runtime.config.", CS_TRUE, CS_TRUE);
  873. icmap_set_ro_access("runtime.totem.", CS_TRUE, CS_TRUE);
  874. icmap_set_ro_access("uidgid.config.", CS_TRUE, CS_TRUE);
  875. icmap_set_ro_access("system.", CS_TRUE, CS_TRUE);
  876. icmap_set_ro_access("nodelist.", CS_TRUE, CS_TRUE);
  877. /*
  878. * Set RO flag for constrete keys of configuration which can't be changed
  879. * during runtime
  880. */
  881. icmap_set_ro_access("totem.crypto_cipher", CS_FALSE, CS_TRUE);
  882. icmap_set_ro_access("totem.crypto_hash", CS_FALSE, CS_TRUE);
  883. icmap_set_ro_access("totem.secauth", CS_FALSE, CS_TRUE);
  884. icmap_set_ro_access("totem.ip_version", CS_FALSE, CS_TRUE);
  885. icmap_set_ro_access("totem.rrp_mode", CS_FALSE, CS_TRUE);
  886. icmap_set_ro_access("totem.transport", CS_FALSE, CS_TRUE);
  887. icmap_set_ro_access("totem.cluster_name", CS_FALSE, CS_TRUE);
  888. icmap_set_ro_access("totem.netmtu", CS_FALSE, CS_TRUE);
  889. icmap_set_ro_access("totem.threads", CS_FALSE, CS_TRUE);
  890. icmap_set_ro_access("totem.version", CS_FALSE, CS_TRUE);
  891. icmap_set_ro_access("totem.nodeid", CS_FALSE, CS_TRUE);
  892. icmap_set_ro_access("totem.clear_node_high_bit", CS_FALSE, CS_TRUE);
  893. icmap_set_ro_access("config.reload_in_progress", CS_FALSE, CS_TRUE);
  894. icmap_set_ro_access("config.totemconfig_reload_in_progress", CS_FALSE, CS_TRUE);
  895. }
  896. static void main_service_ready (void)
  897. {
  898. int res;
  899. /*
  900. * This must occur after totempg is initialized because "this_ip" must be set
  901. */
  902. res = corosync_service_defaults_link_and_init (api);
  903. if (res == -1) {
  904. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services");
  905. corosync_exit_error (COROSYNC_DONE_INIT_SERVICES);
  906. }
  907. cs_ipcs_init();
  908. corosync_totem_stats_init ();
  909. corosync_fplay_control_init ();
  910. corosync_force_gather_init ();
  911. sync_init (
  912. corosync_sync_callbacks_retrieve,
  913. corosync_sync_completed);
  914. }
  915. static enum e_corosync_done corosync_flock (const char *lockfile, pid_t pid)
  916. {
  917. struct flock lock;
  918. enum e_corosync_done err;
  919. char pid_s[17];
  920. int fd_flag;
  921. int lf;
  922. err = COROSYNC_DONE_EXIT;
  923. lf = open (lockfile, O_WRONLY | O_CREAT, 0640);
  924. if (lf == -1) {
  925. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create lock file.");
  926. return (COROSYNC_DONE_ACQUIRE_LOCK);
  927. }
  928. retry_fcntl:
  929. lock.l_type = F_WRLCK;
  930. lock.l_start = 0;
  931. lock.l_whence = SEEK_SET;
  932. lock.l_len = 0;
  933. if (fcntl (lf, F_SETLK, &lock) == -1) {
  934. switch (errno) {
  935. case EINTR:
  936. goto retry_fcntl;
  937. break;
  938. case EAGAIN:
  939. case EACCES:
  940. log_printf (LOGSYS_LEVEL_ERROR, "Another Corosync instance is already running.");
  941. err = COROSYNC_DONE_ALREADY_RUNNING;
  942. goto error_close;
  943. break;
  944. default:
  945. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't acquire lock. Error was %s",
  946. strerror(errno));
  947. err = COROSYNC_DONE_ACQUIRE_LOCK;
  948. goto error_close;
  949. break;
  950. }
  951. }
  952. if (ftruncate (lf, 0) == -1) {
  953. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't truncate lock file. Error was %s",
  954. strerror (errno));
  955. err = COROSYNC_DONE_ACQUIRE_LOCK;
  956. goto error_close_unlink;
  957. }
  958. memset (pid_s, 0, sizeof (pid_s));
  959. snprintf (pid_s, sizeof (pid_s) - 1, "%u\n", pid);
  960. retry_write:
  961. if (write (lf, pid_s, strlen (pid_s)) != strlen (pid_s)) {
  962. if (errno == EINTR) {
  963. goto retry_write;
  964. } else {
  965. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't write pid to lock file. "
  966. "Error was %s", strerror (errno));
  967. err = COROSYNC_DONE_ACQUIRE_LOCK;
  968. goto error_close_unlink;
  969. }
  970. }
  971. if ((fd_flag = fcntl (lf, F_GETFD, 0)) == -1) {
  972. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't get close-on-exec flag from lock file. "
  973. "Error was %s", strerror (errno));
  974. err = COROSYNC_DONE_ACQUIRE_LOCK;
  975. goto error_close_unlink;
  976. }
  977. fd_flag |= FD_CLOEXEC;
  978. if (fcntl (lf, F_SETFD, fd_flag) == -1) {
  979. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't set close-on-exec flag to lock file. "
  980. "Error was %s", strerror (errno));
  981. err = COROSYNC_DONE_ACQUIRE_LOCK;
  982. goto error_close_unlink;
  983. }
  984. return (err);
  985. error_close_unlink:
  986. unlink (lockfile);
  987. error_close:
  988. close (lf);
  989. return (err);
  990. }
  991. static int corosync_move_to_root_cgroup(void) {
  992. FILE *f;
  993. int res = -1;
  994. /*
  995. * /sys/fs/cgroup is hardcoded, because most of Linux distributions are now
  996. * using systemd and systemd uses hardcoded path of cgroup mount point.
  997. *
  998. * This feature is expected to be removed as soon as systemd gets support
  999. * for managing RT configuration.
  1000. */
  1001. f = fopen("/sys/fs/cgroup/cpu/cpu.rt_runtime_us", "rt");
  1002. if (f == NULL) {
  1003. log_printf(LOGSYS_LEVEL_DEBUG, "cpu.rt_runtime_us doesn't exists -> "
  1004. "system without cgroup or with disabled CONFIG_RT_GROUP_SCHED");
  1005. res = 0;
  1006. goto exit_res;
  1007. }
  1008. (void)fclose(f);
  1009. f = fopen("/sys/fs/cgroup/cpu/tasks", "w");
  1010. if (f == NULL) {
  1011. log_printf(LOGSYS_LEVEL_WARNING, "Can't open cgroups tasks file for writing");
  1012. goto exit_res;
  1013. }
  1014. if (fprintf(f, "%jd\n", (intmax_t)getpid()) <= 0) {
  1015. log_printf(LOGSYS_LEVEL_WARNING, "Can't write corosync pid into cgroups tasks file");
  1016. goto close_and_exit_res;
  1017. }
  1018. close_and_exit_res:
  1019. if (fclose(f) != 0) {
  1020. log_printf(LOGSYS_LEVEL_WARNING, "Can't close cgroups tasks file");
  1021. goto exit_res;
  1022. }
  1023. exit_res:
  1024. return (res);
  1025. }
  1026. int main (int argc, char **argv, char **envp)
  1027. {
  1028. const char *error_string;
  1029. struct totem_config totem_config;
  1030. int res, ch;
  1031. int background, sched_rr, prio, testonly, move_to_root_cgroup;
  1032. struct stat stat_out;
  1033. enum e_corosync_done flock_err;
  1034. uint64_t totem_config_warnings;
  1035. struct scheduler_pause_timeout_data scheduler_pause_timeout_data;
  1036. long int tmpli;
  1037. char *ep;
  1038. char *tmp_str;
  1039. /* default configuration
  1040. */
  1041. background = 1;
  1042. testonly = 0;
  1043. while ((ch = getopt (argc, argv, "c:ftv")) != EOF) {
  1044. switch (ch) {
  1045. case 'c':
  1046. res = snprintf(corosync_config_file, sizeof(corosync_config_file), "%s", optarg);
  1047. if (res >= sizeof(corosync_config_file)) {
  1048. fprintf (stderr, "Config file path too long.\n");
  1049. syslog (LOGSYS_LEVEL_ERROR, "Config file path too long.");
  1050. logsys_system_fini();
  1051. return EXIT_FAILURE;
  1052. }
  1053. break;
  1054. case 'f':
  1055. background = 0;
  1056. break;
  1057. case 't':
  1058. testonly = 1;
  1059. break;
  1060. case 'v':
  1061. printf ("Corosync Cluster Engine, version '%s'\n", VERSION);
  1062. printf ("Copyright (c) 2006-2018 Red Hat, Inc.\n");
  1063. logsys_system_fini();
  1064. return EXIT_SUCCESS;
  1065. break;
  1066. default:
  1067. fprintf(stderr, \
  1068. "usage:\n"\
  1069. " -c : Corosync config file path.\n"\
  1070. " -f : Start application in foreground.\n"\
  1071. " -t : Test configuration and exit.\n"\
  1072. " -v : Display version and SVN revision of Corosync and exit.\n");
  1073. logsys_system_fini();
  1074. return EXIT_FAILURE;
  1075. }
  1076. }
  1077. /*
  1078. * Other signals are registered later via qb_loop_signal_add
  1079. */
  1080. (void)signal (SIGSEGV, sigsegv_handler);
  1081. (void)signal (SIGABRT, sigsegv_handler);
  1082. #if MSG_NOSIGNAL != 0
  1083. (void)signal (SIGPIPE, SIG_IGN);
  1084. #endif
  1085. if (icmap_init() != CS_OK) {
  1086. fprintf (stderr, "Corosync Executive couldn't initialize configuration component.\n");
  1087. syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize configuration component.");
  1088. corosync_exit_error (COROSYNC_DONE_ICMAP);
  1089. }
  1090. set_icmap_ro_keys_flag();
  1091. /*
  1092. * Initialize the corosync_api_v1 definition
  1093. */
  1094. api = apidef_get ();
  1095. res = coroparse_configparse(icmap_get_global_map(), &error_string);
  1096. if (res == -1) {
  1097. /*
  1098. * Logsys can't log properly at this early stage, and we need to get this message out
  1099. *
  1100. */
  1101. fprintf (stderr, "%s\n", error_string);
  1102. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1103. corosync_exit_error (COROSYNC_DONE_MAINCONFIGREAD);
  1104. }
  1105. if (stats_map_init(api) != CS_OK) {
  1106. fprintf (stderr, "Corosync Executive couldn't initialize statistics component.\n");
  1107. syslog (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't initialize statistics component.");
  1108. corosync_exit_error (COROSYNC_DONE_STATS);
  1109. }
  1110. res = corosync_log_config_read (&error_string);
  1111. if (res == -1) {
  1112. /*
  1113. * if we are here, we _must_ flush the logsys queue
  1114. * and try to inform that we couldn't read the config.
  1115. * this is a desperate attempt before certain death
  1116. * and there is no guarantee that we can print to stderr
  1117. * nor that logsys is sending the messages where we expect.
  1118. */
  1119. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1120. fprintf(stderr, "%s", error_string);
  1121. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1122. corosync_exit_error (COROSYNC_DONE_LOGCONFIGREAD);
  1123. }
  1124. if (!testonly) {
  1125. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.", VERSION);
  1126. log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "");
  1127. }
  1128. /*
  1129. * Make sure required directory is present
  1130. */
  1131. res = stat (get_run_dir(), &stat_out);
  1132. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  1133. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.", get_run_dir());
  1134. corosync_exit_error (COROSYNC_DONE_DIR_NOT_PRESENT);
  1135. }
  1136. res = chdir(get_run_dir());
  1137. if (res == -1) {
  1138. log_printf (LOGSYS_LEVEL_ERROR, "Cannot chdir to run directory %s. "
  1139. "Please make sure it has correct context and rights.", get_run_dir());
  1140. corosync_exit_error (COROSYNC_DONE_DIR_NOT_PRESENT);
  1141. }
  1142. res = totem_config_read (&totem_config, &error_string, &totem_config_warnings);
  1143. if (res == -1) {
  1144. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1145. corosync_exit_error (COROSYNC_DONE_MAINCONFIGREAD);
  1146. }
  1147. if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_IGNORED) {
  1148. log_printf (LOGSYS_LEVEL_WARNING, "member section is used together with nodelist. Members ignored.");
  1149. }
  1150. if (totem_config_warnings & TOTEM_CONFIG_WARNING_MEMBERS_DEPRECATED) {
  1151. log_printf (LOGSYS_LEVEL_WARNING, "member section is deprecated.");
  1152. }
  1153. if (totem_config_warnings & TOTEM_CONFIG_WARNING_TOTEM_NODEID_IGNORED) {
  1154. log_printf (LOGSYS_LEVEL_WARNING, "nodeid appears both in totem section and nodelist. Nodelist one is used.");
  1155. }
  1156. if (totem_config_warnings & TOTEM_CONFIG_BINDNETADDR_NODELIST_SET) {
  1157. log_printf (LOGSYS_LEVEL_WARNING, "interface section bindnetaddr is used together with nodelist. "
  1158. "Nodelist one is going to be used.");
  1159. }
  1160. if (totem_config_warnings != 0) {
  1161. log_printf (LOGSYS_LEVEL_WARNING, "Please migrate config file to nodelist.");
  1162. }
  1163. res = totem_config_keyread (&totem_config, &error_string);
  1164. if (res == -1) {
  1165. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1166. corosync_exit_error (COROSYNC_DONE_MAINCONFIGREAD);
  1167. }
  1168. res = totem_config_validate (&totem_config, &error_string);
  1169. if (res == -1) {
  1170. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1171. corosync_exit_error (COROSYNC_DONE_MAINCONFIGREAD);
  1172. }
  1173. if (testonly) {
  1174. corosync_exit_error (COROSYNC_DONE_EXIT);
  1175. }
  1176. move_to_root_cgroup = 1;
  1177. if (icmap_get_string("system.move_to_root_cgroup", &tmp_str) == CS_OK) {
  1178. if (strcmp(tmp_str, "yes") != 0) {
  1179. move_to_root_cgroup = 0;
  1180. }
  1181. free(tmp_str);
  1182. }
  1183. /*
  1184. * Try to move corosync into root cpu cgroup. Failure is not fatal and
  1185. * error is deliberately ignored.
  1186. */
  1187. if (move_to_root_cgroup) {
  1188. (void)corosync_move_to_root_cgroup();
  1189. }
  1190. sched_rr = 1;
  1191. if (icmap_get_string("system.sched_rr", &tmp_str) == CS_OK) {
  1192. if (strcmp(tmp_str, "yes") != 0) {
  1193. sched_rr = 0;
  1194. }
  1195. free(tmp_str);
  1196. }
  1197. prio = 0;
  1198. if (icmap_get_string("system.priority", &tmp_str) == CS_OK) {
  1199. if (strcmp(tmp_str, "max") == 0) {
  1200. prio = INT_MIN;
  1201. } else if (strcmp(tmp_str, "min") == 0) {
  1202. prio = INT_MAX;
  1203. } else {
  1204. errno = 0;
  1205. tmpli = strtol(tmp_str, &ep, 10);
  1206. if (errno != 0 || *ep != '\0' || tmpli > INT_MAX || tmpli < INT_MIN) {
  1207. log_printf (LOGSYS_LEVEL_ERROR, "Priority value %s is invalid", tmp_str);
  1208. corosync_exit_error (COROSYNC_DONE_MAINCONFIGREAD);
  1209. }
  1210. prio = tmpli;
  1211. }
  1212. free(tmp_str);
  1213. }
  1214. /*
  1215. * Set round robin realtime scheduling with priority 99
  1216. */
  1217. if (sched_rr) {
  1218. if (corosync_set_rr_scheduler () != 0) {
  1219. prio = INT_MIN;
  1220. } else {
  1221. prio = 0;
  1222. }
  1223. }
  1224. if (prio != 0) {
  1225. if (setpriority(PRIO_PGRP, 0, prio) != 0) {
  1226. LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING,
  1227. "Could not set priority %d", prio);
  1228. }
  1229. }
  1230. ip_version = totem_config.ip_version;
  1231. totem_config.totem_memb_ring_id_create_or_load = corosync_ring_id_create_or_load;
  1232. totem_config.totem_memb_ring_id_store = corosync_ring_id_store;
  1233. totem_config.totem_logging_configuration = totem_logging_configuration;
  1234. totem_config.totem_logging_configuration.log_subsys_id = _logsys_subsys_create("TOTEM", "totem,"
  1235. "totemip.c,totemconfig.c,totemcrypto.c,totemsrp.c,"
  1236. "totempg.c,totemudp.c,totemudpu.c,totemnet.c,totemknet.c");
  1237. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  1238. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  1239. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  1240. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  1241. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  1242. totem_config.totem_logging_configuration.log_level_trace = LOGSYS_LEVEL_TRACE;
  1243. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  1244. logsys_config_apply();
  1245. /*
  1246. * Now we are fully initialized.
  1247. */
  1248. if (background) {
  1249. logsys_blackbox_prefork();
  1250. corosync_tty_detach ();
  1251. logsys_blackbox_postfork();
  1252. log_printf (LOGSYS_LEVEL_DEBUG, "Corosync TTY detached");
  1253. }
  1254. /*
  1255. * Lock all memory to avoid page faults which may interrupt
  1256. * application healthchecking
  1257. */
  1258. corosync_mlockall ();
  1259. corosync_poll_handle = qb_loop_create ();
  1260. memset(&scheduler_pause_timeout_data, 0, sizeof(scheduler_pause_timeout_data));
  1261. scheduler_pause_timeout_data.totem_config = &totem_config;
  1262. timer_function_scheduler_timeout (&scheduler_pause_timeout_data);
  1263. qb_loop_signal_add(corosync_poll_handle, QB_LOOP_LOW,
  1264. SIGUSR2, NULL, sig_diag_handler, NULL);
  1265. qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
  1266. SIGINT, NULL, sig_exit_handler, NULL);
  1267. qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
  1268. SIGQUIT, NULL, sig_exit_handler, NULL);
  1269. qb_loop_signal_add(corosync_poll_handle, QB_LOOP_HIGH,
  1270. SIGTERM, NULL, sig_exit_handler, NULL);
  1271. if (logsys_thread_start() != 0) {
  1272. log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize log thread");
  1273. corosync_exit_error (COROSYNC_DONE_LOGCONFIGREAD);
  1274. }
  1275. if ((flock_err = corosync_flock (corosync_lock_file, getpid ())) != COROSYNC_DONE_EXIT) {
  1276. corosync_exit_error (flock_err);
  1277. }
  1278. /*
  1279. * if totempg_initialize doesn't have root priveleges, it cannot
  1280. * bind to a specific interface. This only matters if
  1281. * there is more then one interface in a system, so
  1282. * in this case, only a warning is printed
  1283. */
  1284. /*
  1285. * Join multicast group and setup delivery
  1286. * and configuration change functions
  1287. */
  1288. if (totempg_initialize (
  1289. corosync_poll_handle,
  1290. &totem_config) != 0) {
  1291. log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize TOTEM layer");
  1292. corosync_exit_error (COROSYNC_DONE_FATAL_ERR);
  1293. }
  1294. totempg_service_ready_register (
  1295. main_service_ready);
  1296. totempg_groups_initialize (
  1297. &corosync_group_handle,
  1298. deliver_fn,
  1299. confchg_fn);
  1300. totempg_groups_join (
  1301. corosync_group_handle,
  1302. &corosync_group,
  1303. 1);
  1304. /*
  1305. * Drop root privleges to user 'corosync'
  1306. * TODO: Don't really need full root capabilities;
  1307. * needed capabilities are:
  1308. * CAP_NET_RAW (bindtodevice)
  1309. * CAP_SYS_NICE (setscheduler)
  1310. * CAP_IPC_LOCK (mlockall)
  1311. */
  1312. priv_drop ();
  1313. schedwrk_init (
  1314. serialize_lock,
  1315. serialize_unlock);
  1316. /*
  1317. * Start main processing loop
  1318. */
  1319. qb_loop_run (corosync_poll_handle);
  1320. /*
  1321. * Exit was requested
  1322. */
  1323. totempg_finalize ();
  1324. /*
  1325. * free the loop resources
  1326. */
  1327. qb_loop_destroy (corosync_poll_handle);
  1328. /*
  1329. * free up the icmap
  1330. */
  1331. /*
  1332. * Remove pid lock file
  1333. */
  1334. unlink (corosync_lock_file);
  1335. corosync_exit_error (COROSYNC_DONE_EXIT);
  1336. return EXIT_SUCCESS;
  1337. }