main.c 41 KB

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