main.c 42 KB

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