main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 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. #include <config.h>
  36. #include <pthread.h>
  37. #include <assert.h>
  38. #include <sys/types.h>
  39. #include <sys/poll.h>
  40. #include <sys/uio.h>
  41. #include <sys/mman.h>
  42. #include <sys/socket.h>
  43. #include <sys/un.h>
  44. #include <sys/time.h>
  45. #include <sys/resource.h>
  46. #include <sys/stat.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <signal.h>
  55. #include <sched.h>
  56. #include <time.h>
  57. #include <corosync/swab.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/coroipc_types.h>
  60. #include <corosync/corodefs.h>
  61. #include <corosync/list.h>
  62. #include <corosync/lcr/lcr_ifact.h>
  63. #include <corosync/totem/coropoll.h>
  64. #include <corosync/totem/totempg.h>
  65. #include <corosync/engine/objdb.h>
  66. #include <corosync/engine/config.h>
  67. #include <corosync/engine/logsys.h>
  68. #include <corosync/coroipcs.h>
  69. #include "quorum.h"
  70. #include "totemsrp.h"
  71. #include "mainconfig.h"
  72. #include "totemconfig.h"
  73. #include "main.h"
  74. #include "sync.h"
  75. #include "syncv2.h"
  76. #include "tlist.h"
  77. #include "timer.h"
  78. #include "util.h"
  79. #include "apidef.h"
  80. #include "service.h"
  81. #include "schedwrk.h"
  82. #include "evil.h"
  83. LOGSYS_DECLARE_SYSTEM ("corosync",
  84. LOGSYS_MODE_OUTPUT_STDERR | LOGSYS_MODE_THREADED | LOGSYS_MODE_FORK,
  85. 0,
  86. NULL,
  87. LOG_INFO,
  88. LOG_DAEMON,
  89. LOG_INFO,
  90. NULL,
  91. 1000000);
  92. LOGSYS_DECLARE_SUBSYS ("MAIN");
  93. #define SERVER_BACKLOG 5
  94. static int sched_priority = 0;
  95. static unsigned int service_count = 32;
  96. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  97. static pthread_spinlock_t serialize_spin;
  98. #else
  99. static pthread_mutex_t serialize_mutex = PTHREAD_MUTEX_INITIALIZER;
  100. #endif
  101. static struct totem_logging_configuration totem_logging_configuration;
  102. static int num_config_modules;
  103. static struct config_iface_ver0 *config_modules[MAX_DYNAMIC_SERVICES];
  104. static struct objdb_iface_ver0 *objdb = NULL;
  105. static struct corosync_api_v1 *api = NULL;
  106. static enum cs_sync_mode minimum_sync_mode;
  107. static enum cs_sync_mode minimum_sync_mode;
  108. static int sync_in_process = 1;
  109. static hdb_handle_t corosync_poll_handle;
  110. struct sched_param global_sched_param;
  111. static hdb_handle_t object_connection_handle;
  112. static corosync_timer_handle_t corosync_stats_timer_handle;
  113. hdb_handle_t corosync_poll_handle_get (void)
  114. {
  115. return (corosync_poll_handle);
  116. }
  117. void corosync_state_dump (void)
  118. {
  119. int i;
  120. for (i = 0; i < SERVICE_HANDLER_MAXIMUM_COUNT; i++) {
  121. if (ais_service[i] && ais_service[i]->exec_dump_fn) {
  122. ais_service[i]->exec_dump_fn ();
  123. }
  124. }
  125. }
  126. static void unlink_all_completed (void)
  127. {
  128. poll_stop (0);
  129. totempg_finalize ();
  130. coroipcs_ipc_exit ();
  131. corosync_exit_error (AIS_DONE_EXIT);
  132. }
  133. void corosync_shutdown_request (void)
  134. {
  135. static int called = 0;
  136. if (called) {
  137. return;
  138. }
  139. if (called == 0) {
  140. called = 1;
  141. }
  142. corosync_service_unlink_all (api, unlink_all_completed);
  143. }
  144. static void sigusr2_handler (int num)
  145. {
  146. /*
  147. * TODO remove this from sigusr2 handler and access via cfg service
  148. * engine api - corosync-cfgtool
  149. */
  150. corosync_state_dump ();
  151. }
  152. static void sigterm_handler (int num)
  153. {
  154. corosync_shutdown_request ();
  155. }
  156. static void sigquit_handler (int num)
  157. {
  158. corosync_shutdown_request ();
  159. }
  160. static void sigintr_handler (int num)
  161. {
  162. corosync_shutdown_request ();
  163. }
  164. static void sigsegv_handler (int num)
  165. {
  166. (void)signal (SIGSEGV, SIG_DFL);
  167. logsys_atexit();
  168. logsys_log_rec_store (LOCALSTATEDIR "/lib/corosync/fdata");
  169. raise (SIGSEGV);
  170. }
  171. static void sigabrt_handler (int num)
  172. {
  173. (void)signal (SIGABRT, SIG_DFL);
  174. logsys_atexit();
  175. logsys_log_rec_store (LOCALSTATEDIR "/lib/corosync/fdata");
  176. raise (SIGABRT);
  177. }
  178. #define LOCALHOST_IP inet_addr("127.0.0.1")
  179. static hdb_handle_t corosync_group_handle;
  180. static struct totempg_group corosync_group = {
  181. .group = "a",
  182. .group_len = 1
  183. };
  184. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  185. static void serialize_lock (void)
  186. {
  187. pthread_spin_lock (&serialize_spin);
  188. }
  189. static void serialize_unlock (void)
  190. {
  191. pthread_spin_unlock (&serialize_spin);
  192. }
  193. #else
  194. static void serialize_lock (void)
  195. {
  196. pthread_mutex_lock (&serialize_mutex);
  197. }
  198. static void serialize_unlock (void)
  199. {
  200. pthread_mutex_unlock (&serialize_mutex);
  201. }
  202. #endif
  203. static void corosync_sync_completed (void)
  204. {
  205. log_printf (LOGSYS_LEVEL_NOTICE,
  206. "Completed service synchronization, ready to provide service.\n");
  207. sync_in_process = 0;
  208. }
  209. static int corosync_sync_callbacks_retrieve (int sync_id,
  210. struct sync_callbacks *callbacks)
  211. {
  212. unsigned int ais_service_index;
  213. int res;
  214. for (ais_service_index = 0;
  215. ais_service_index < SERVICE_HANDLER_MAXIMUM_COUNT;
  216. ais_service_index++) {
  217. if (ais_service[ais_service_index] != NULL
  218. && ais_service[ais_service_index]->sync_mode == CS_SYNC_V1) {
  219. if (ais_service_index == sync_id) {
  220. break;
  221. }
  222. }
  223. }
  224. /*
  225. * Try to load backwards compat sync engines
  226. */
  227. if (ais_service_index == SERVICE_HANDLER_MAXIMUM_COUNT) {
  228. res = evil_callbacks_load (sync_id, callbacks);
  229. return (res);
  230. }
  231. callbacks->name = ais_service[ais_service_index]->name;
  232. callbacks->sync_init = ais_service[ais_service_index]->sync_init;
  233. callbacks->sync_process = ais_service[ais_service_index]->sync_process;
  234. callbacks->sync_activate = ais_service[ais_service_index]->sync_activate;
  235. callbacks->sync_abort = ais_service[ais_service_index]->sync_abort;
  236. return (0);
  237. }
  238. static int corosync_sync_v2_callbacks_retrieve (
  239. int service_id,
  240. struct sync_callbacks *callbacks)
  241. {
  242. int res;
  243. if (minimum_sync_mode == CS_SYNC_V2 && service_id == CLM_SERVICE && ais_service[CLM_SERVICE] == NULL) {
  244. res = evil_callbacks_load (service_id, callbacks);
  245. return (res);
  246. }
  247. if (minimum_sync_mode == CS_SYNC_V2 && service_id == EVT_SERVICE && ais_service[EVT_SERVICE] == NULL) {
  248. res = evil_callbacks_load (service_id, callbacks);
  249. return (res);
  250. }
  251. if (ais_service[service_id] == NULL) {
  252. return (-1);
  253. }
  254. if (minimum_sync_mode == CS_SYNC_V1 && ais_service[service_id]->sync_mode != CS_SYNC_V2) {
  255. return (-1);
  256. }
  257. callbacks->name = ais_service[service_id]->name;
  258. callbacks->sync_init = ais_service[service_id]->sync_init;
  259. callbacks->sync_process = ais_service[service_id]->sync_process;
  260. callbacks->sync_activate = ais_service[service_id]->sync_activate;
  261. callbacks->sync_abort = ais_service[service_id]->sync_abort;
  262. return (0);
  263. }
  264. static struct memb_ring_id corosync_ring_id;
  265. static void confchg_fn (
  266. enum totem_configuration_type configuration_type,
  267. const unsigned int *member_list, size_t member_list_entries,
  268. const unsigned int *left_list, size_t left_list_entries,
  269. const unsigned int *joined_list, size_t joined_list_entries,
  270. const struct memb_ring_id *ring_id)
  271. {
  272. int i;
  273. int abort_activate = 0;
  274. if (sync_in_process == 1) {
  275. abort_activate = 1;
  276. }
  277. sync_in_process = 1;
  278. serialize_lock ();
  279. memcpy (&corosync_ring_id, ring_id, sizeof (struct memb_ring_id));
  280. /*
  281. * Call configuration change for all services
  282. */
  283. for (i = 0; i < service_count; i++) {
  284. if (ais_service[i] && ais_service[i]->confchg_fn) {
  285. ais_service[i]->confchg_fn (configuration_type,
  286. member_list, member_list_entries,
  287. left_list, left_list_entries,
  288. joined_list, joined_list_entries, ring_id);
  289. }
  290. }
  291. serialize_unlock ();
  292. if (abort_activate) {
  293. sync_v2_abort ();
  294. }
  295. if (minimum_sync_mode == CS_SYNC_V2 && configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  296. sync_v2_start (member_list, member_list_entries, ring_id);
  297. }
  298. }
  299. static void priv_drop (void)
  300. {
  301. return; /* TODO: we are still not dropping privs */
  302. }
  303. static void corosync_tty_detach (void)
  304. {
  305. int fd;
  306. /*
  307. * Disconnect from TTY if this is not a debug run
  308. */
  309. switch (fork ()) {
  310. case -1:
  311. corosync_exit_error (AIS_DONE_FORK);
  312. break;
  313. case 0:
  314. /*
  315. * child which is disconnected, run this process
  316. */
  317. /* setset();
  318. close (0);
  319. close (1);
  320. close (2);
  321. */
  322. break;
  323. default:
  324. exit (0);
  325. break;
  326. }
  327. /* Create new session */
  328. (void)setsid();
  329. /*
  330. * Map stdin/out/err to /dev/null.
  331. */
  332. fd = open("/dev/null", O_RDWR);
  333. if (fd >= 0) {
  334. /* dup2 to 0 / 1 / 2 (stdin / stdout / stderr) */
  335. dup2(fd, STDIN_FILENO); /* 0 */
  336. dup2(fd, STDOUT_FILENO); /* 1 */
  337. dup2(fd, STDERR_FILENO); /* 2 */
  338. /* Should be 0, but just in case it isn't... */
  339. if (fd > 2)
  340. close(fd);
  341. }
  342. }
  343. static void corosync_mlockall (void)
  344. {
  345. #if !defined(COROSYNC_BSD)
  346. int res;
  347. #endif
  348. struct rlimit rlimit;
  349. rlimit.rlim_cur = RLIM_INFINITY;
  350. rlimit.rlim_max = RLIM_INFINITY;
  351. #ifndef COROSYNC_SOLARIS
  352. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  353. #else
  354. setrlimit (RLIMIT_VMEM, &rlimit);
  355. #endif
  356. #if defined(COROSYNC_BSD)
  357. /* under FreeBSD a process with locked page cannot call dlopen
  358. * code disabled until FreeBSD bug i386/93396 was solved
  359. */
  360. log_printf (LOGSYS_LEVEL_WARNING, "Could not lock memory of service to avoid page faults\n");
  361. #else
  362. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  363. if (res == -1) {
  364. log_printf (LOGSYS_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  365. };
  366. #endif
  367. }
  368. static void corosync_totem_stats_updater (void *data)
  369. {
  370. totempg_stats_t * stats;
  371. uint32_t mtt_rx_token;
  372. uint32_t total_mtt_rx_token;
  373. uint32_t avg_backlog_calc;
  374. uint32_t total_backlog_calc;
  375. uint32_t avg_token_holdtime;
  376. uint32_t total_token_holdtime;
  377. int t, prev;
  378. int32_t token_count;
  379. stats = api->totem_get_stats();
  380. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  381. "orf_token_tx", strlen("orf_token_tx"),
  382. &stats->mrp->srp->orf_token_tx, sizeof (stats->mrp->srp->orf_token_tx));
  383. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  384. "orf_token_rx", strlen("orf_token_rx"),
  385. &stats->mrp->srp->orf_token_rx, sizeof (stats->mrp->srp->orf_token_rx));
  386. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  387. "memb_merge_detect_tx", strlen("memb_merge_detect_tx"),
  388. &stats->mrp->srp->memb_merge_detect_tx, sizeof (stats->mrp->srp->memb_merge_detect_tx));
  389. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  390. "memb_merge_detect_rx", strlen("memb_merge_detect_rx"),
  391. &stats->mrp->srp->memb_merge_detect_rx, sizeof (stats->mrp->srp->memb_merge_detect_rx));
  392. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  393. "memb_join_tx", strlen("memb_join_tx"),
  394. &stats->mrp->srp->memb_join_tx, sizeof (stats->mrp->srp->memb_join_tx));
  395. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  396. "memb_join_rx", strlen("memb_join_rx"),
  397. &stats->mrp->srp->memb_join_rx, sizeof (stats->mrp->srp->memb_join_rx));
  398. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  399. "mcast_tx", strlen("mcast_tx"),
  400. &stats->mrp->srp->mcast_tx, sizeof (stats->mrp->srp->mcast_tx));
  401. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  402. "mcast_retx", strlen("mcast_retx"),
  403. &stats->mrp->srp->mcast_retx, sizeof (stats->mrp->srp->mcast_retx));
  404. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  405. "mcast_rx", strlen("mcast_rx"),
  406. &stats->mrp->srp->mcast_rx, sizeof (stats->mrp->srp->mcast_rx));
  407. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  408. "memb_commit_token_tx", strlen("memb_commit_token_tx"),
  409. &stats->mrp->srp->memb_commit_token_tx, sizeof (stats->mrp->srp->memb_commit_token_tx));
  410. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  411. "memb_commit_token_rx", strlen("memb_commit_token_rx"),
  412. &stats->mrp->srp->memb_commit_token_rx, sizeof (stats->mrp->srp->memb_commit_token_rx));
  413. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  414. "token_hold_cancel_tx", strlen("token_hold_cancel_tx"),
  415. &stats->mrp->srp->token_hold_cancel_tx, sizeof (stats->mrp->srp->token_hold_cancel_tx));
  416. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  417. "token_hold_cancel_rx", strlen("token_hold_cancel_rx"),
  418. &stats->mrp->srp->token_hold_cancel_rx, sizeof (stats->mrp->srp->token_hold_cancel_rx));
  419. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  420. "operational_entered", strlen("operational_entered"),
  421. &stats->mrp->srp->operational_entered, sizeof (stats->mrp->srp->operational_entered));
  422. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  423. "operational_token_lost", strlen("operational_token_lost"),
  424. &stats->mrp->srp->operational_token_lost, sizeof (stats->mrp->srp->operational_token_lost));
  425. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  426. "gather_entered", strlen("gather_entered"),
  427. &stats->mrp->srp->gather_entered, sizeof (stats->mrp->srp->gather_entered));
  428. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  429. "gather_token_lost", strlen("gather_token_lost"),
  430. &stats->mrp->srp->gather_token_lost, sizeof (stats->mrp->srp->gather_token_lost));
  431. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  432. "commit_entered", strlen("commit_entered"),
  433. &stats->mrp->srp->commit_entered, sizeof (stats->mrp->srp->commit_entered));
  434. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  435. "commit_token_lost", strlen("commit_token_lost"),
  436. &stats->mrp->srp->commit_token_lost, sizeof (stats->mrp->srp->commit_token_lost));
  437. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  438. "recovery_entered", strlen("recovery_entered"),
  439. &stats->mrp->srp->recovery_entered, sizeof (stats->mrp->srp->recovery_entered));
  440. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  441. "recovery_token_lost", strlen("recovery_token_lost"),
  442. &stats->mrp->srp->recovery_token_lost, sizeof (stats->mrp->srp->recovery_token_lost));
  443. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  444. "consensus_timeouts", strlen("consensus_timeouts"),
  445. &stats->mrp->srp->consensus_timeouts, sizeof (stats->mrp->srp->consensus_timeouts));
  446. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  447. "rx_msg_dropped", strlen("rx_msg_dropped"),
  448. &stats->mrp->srp->rx_msg_dropped, sizeof (stats->mrp->srp->rx_msg_dropped));
  449. total_mtt_rx_token = 0;
  450. total_token_holdtime = 0;
  451. total_backlog_calc = 0;
  452. token_count = 0;
  453. t = stats->mrp->srp->latest_token;
  454. while (1) {
  455. if (t == 0)
  456. prev = TOTEM_TOKEN_STATS_MAX - 1;
  457. else
  458. prev = t - 1;
  459. if (prev == stats->mrp->srp->earliest_token)
  460. break;
  461. /* if tx == 0, then dropped token (not ours) */
  462. if (stats->mrp->srp->token[t].tx != 0 ||
  463. (stats->mrp->srp->token[t].rx - stats->mrp->srp->token[prev].rx) > 0 ) {
  464. total_mtt_rx_token += (stats->mrp->srp->token[t].rx - stats->mrp->srp->token[prev].rx);
  465. total_token_holdtime += (stats->mrp->srp->token[t].tx - stats->mrp->srp->token[t].rx);
  466. total_backlog_calc += stats->mrp->srp->token[t].backlog_calc;
  467. token_count++;
  468. }
  469. t = prev;
  470. }
  471. if (token_count) {
  472. mtt_rx_token = (total_mtt_rx_token / token_count);
  473. avg_backlog_calc = (total_backlog_calc / token_count);
  474. avg_token_holdtime = (total_token_holdtime / token_count);
  475. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  476. "mtt_rx_token", strlen("mtt_rx_token"),
  477. &mtt_rx_token, sizeof (mtt_rx_token));
  478. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  479. "avg_token_workload", strlen("avg_token_workload"),
  480. &avg_token_holdtime, sizeof (avg_backlog_calc));
  481. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  482. "avg_backlog_calc", strlen("avg_backlog_calc"),
  483. &avg_backlog_calc, sizeof (avg_backlog_calc));
  484. }
  485. api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
  486. corosync_totem_stats_updater,
  487. &corosync_stats_timer_handle);
  488. }
  489. static void corosync_totem_stats_init (void)
  490. {
  491. totempg_stats_t * stats;
  492. hdb_handle_t object_find_handle;
  493. hdb_handle_t object_runtime_handle;
  494. hdb_handle_t object_totem_handle;
  495. uint32_t zero_32 = 0;
  496. uint64_t zero_64 = 0;
  497. stats = api->totem_get_stats();
  498. objdb->object_find_create (
  499. OBJECT_PARENT_HANDLE,
  500. "runtime",
  501. strlen ("runtime"),
  502. &object_find_handle);
  503. if (objdb->object_find_next (
  504. object_find_handle,
  505. &object_runtime_handle) == 0) {
  506. objdb->object_create (object_runtime_handle,
  507. &object_totem_handle,
  508. "totem", strlen ("totem"));
  509. objdb->object_create (object_totem_handle,
  510. &stats->hdr.handle,
  511. "pg", strlen ("pg"));
  512. objdb->object_create (stats->hdr.handle,
  513. &stats->mrp->hdr.handle,
  514. "mrp", strlen ("mrp"));
  515. objdb->object_create (stats->mrp->hdr.handle,
  516. &stats->mrp->srp->hdr.handle,
  517. "srp", strlen ("srp"));
  518. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  519. "orf_token_tx", &stats->mrp->srp->orf_token_tx,
  520. sizeof (stats->mrp->srp->orf_token_tx), OBJDB_VALUETYPE_UINT64);
  521. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  522. "orf_token_rx", &stats->mrp->srp->orf_token_rx,
  523. sizeof (stats->mrp->srp->orf_token_rx), OBJDB_VALUETYPE_UINT64);
  524. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  525. "memb_merge_detect_tx", &stats->mrp->srp->memb_merge_detect_tx,
  526. sizeof (stats->mrp->srp->memb_merge_detect_tx), OBJDB_VALUETYPE_UINT64);
  527. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  528. "memb_merge_detect_rx", &stats->mrp->srp->memb_merge_detect_rx,
  529. sizeof (stats->mrp->srp->memb_merge_detect_rx), OBJDB_VALUETYPE_UINT64);
  530. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  531. "memb_join_tx", &stats->mrp->srp->memb_join_tx,
  532. sizeof (stats->mrp->srp->memb_join_tx), OBJDB_VALUETYPE_UINT64);
  533. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  534. "memb_join_rx", &stats->mrp->srp->memb_join_rx,
  535. sizeof (stats->mrp->srp->memb_join_rx), OBJDB_VALUETYPE_UINT64);
  536. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  537. "mcast_tx", &stats->mrp->srp->mcast_tx,
  538. sizeof (stats->mrp->srp->mcast_tx), OBJDB_VALUETYPE_UINT64);
  539. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  540. "mcast_retx", &stats->mrp->srp->mcast_retx,
  541. sizeof (stats->mrp->srp->mcast_retx), OBJDB_VALUETYPE_UINT64);
  542. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  543. "mcast_rx", &stats->mrp->srp->mcast_rx,
  544. sizeof (stats->mrp->srp->mcast_rx), OBJDB_VALUETYPE_UINT64);
  545. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  546. "memb_commit_token_tx", &stats->mrp->srp->memb_commit_token_tx,
  547. sizeof (stats->mrp->srp->memb_commit_token_tx), OBJDB_VALUETYPE_UINT64);
  548. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  549. "memb_commit_token_rx", &stats->mrp->srp->memb_commit_token_rx,
  550. sizeof (stats->mrp->srp->memb_commit_token_rx), OBJDB_VALUETYPE_UINT64);
  551. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  552. "token_hold_cancel_tx", &stats->mrp->srp->token_hold_cancel_tx,
  553. sizeof (stats->mrp->srp->token_hold_cancel_tx), OBJDB_VALUETYPE_UINT64);
  554. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  555. "token_hold_cancel_rx", &stats->mrp->srp->token_hold_cancel_rx,
  556. sizeof (stats->mrp->srp->token_hold_cancel_rx), OBJDB_VALUETYPE_UINT64);
  557. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  558. "operational_entered", &stats->mrp->srp->operational_entered,
  559. sizeof (stats->mrp->srp->operational_entered), OBJDB_VALUETYPE_UINT64);
  560. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  561. "operational_token_lost", &stats->mrp->srp->operational_token_lost,
  562. sizeof (stats->mrp->srp->operational_token_lost), OBJDB_VALUETYPE_UINT64);
  563. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  564. "gather_entered", &stats->mrp->srp->gather_entered,
  565. sizeof (stats->mrp->srp->gather_entered), OBJDB_VALUETYPE_UINT64);
  566. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  567. "gather_token_lost", &stats->mrp->srp->gather_token_lost,
  568. sizeof (stats->mrp->srp->gather_token_lost), OBJDB_VALUETYPE_UINT64);
  569. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  570. "commit_entered", &stats->mrp->srp->commit_entered,
  571. sizeof (stats->mrp->srp->commit_entered), OBJDB_VALUETYPE_UINT64);
  572. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  573. "commit_token_lost", &stats->mrp->srp->commit_token_lost,
  574. sizeof (stats->mrp->srp->commit_token_lost), OBJDB_VALUETYPE_UINT64);
  575. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  576. "recovery_entered", &stats->mrp->srp->recovery_entered,
  577. sizeof (stats->mrp->srp->recovery_entered), OBJDB_VALUETYPE_UINT64);
  578. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  579. "recovery_token_lost", &stats->mrp->srp->recovery_token_lost,
  580. sizeof (stats->mrp->srp->recovery_token_lost), OBJDB_VALUETYPE_UINT64);
  581. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  582. "consensus_timeouts", &stats->mrp->srp->consensus_timeouts,
  583. sizeof (stats->mrp->srp->consensus_timeouts), OBJDB_VALUETYPE_UINT64);
  584. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  585. "mtt_rx_token", &zero_32,
  586. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  587. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  588. "avg_token_workload", &zero_32,
  589. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  590. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  591. "avg_backlog_calc", &zero_64,
  592. sizeof (zero_64), OBJDB_VALUETYPE_UINT64);
  593. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  594. "rx_msg_dropped", &zero_64,
  595. sizeof (zero_64), OBJDB_VALUETYPE_UINT64);
  596. }
  597. /* start stats timer */
  598. api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
  599. corosync_totem_stats_updater,
  600. &corosync_stats_timer_handle);
  601. }
  602. static void deliver_fn (
  603. unsigned int nodeid,
  604. const void *msg,
  605. unsigned int msg_len,
  606. int endian_conversion_required)
  607. {
  608. const coroipc_request_header_t *header;
  609. int service;
  610. int fn_id;
  611. unsigned int id;
  612. unsigned int size;
  613. unsigned int key_incr_dummy;
  614. header = msg;
  615. if (endian_conversion_required) {
  616. id = swab32 (header->id);
  617. size = swab32 (header->size);
  618. } else {
  619. id = header->id;
  620. size = header->size;
  621. }
  622. /*
  623. * Call the proper executive handler
  624. */
  625. service = id >> 16;
  626. fn_id = id & 0xffff;
  627. serialize_lock();
  628. if (ais_service[service] == NULL && service == EVT_SERVICE) {
  629. evil_deliver_fn (nodeid, service, fn_id, msg,
  630. endian_conversion_required);
  631. }
  632. if (!ais_service[service]) {
  633. serialize_unlock();
  634. return;
  635. }
  636. objdb->object_key_increment (service_stats_handle[service][fn_id],
  637. "rx", strlen("rx"),
  638. &key_incr_dummy);
  639. if (endian_conversion_required) {
  640. assert(ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  641. ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  642. ((void *)msg);
  643. }
  644. ais_service[service]->exec_engine[fn_id].exec_handler_fn
  645. (msg, nodeid);
  646. serialize_unlock();
  647. }
  648. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  649. {
  650. *modules = config_modules;
  651. *num = num_config_modules;
  652. }
  653. int main_mcast (
  654. const struct iovec *iovec,
  655. unsigned int iov_len,
  656. unsigned int guarantee)
  657. {
  658. const coroipc_request_header_t *req = iovec->iov_base;
  659. int service;
  660. int fn_id;
  661. unsigned int key_incr_dummy;
  662. service = req->id >> 16;
  663. fn_id = req->id & 0xffff;
  664. if (ais_service[service]) {
  665. objdb->object_key_increment (service_stats_handle[service][fn_id],
  666. "tx", strlen("tx"),
  667. &key_incr_dummy);
  668. }
  669. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  670. }
  671. int message_source_is_local (const mar_message_source_t *source)
  672. {
  673. int ret = 0;
  674. assert (source != NULL);
  675. if (source->nodeid == totempg_my_nodeid_get ()) {
  676. ret = 1;
  677. }
  678. return ret;
  679. }
  680. void message_source_set (
  681. mar_message_source_t *source,
  682. void *conn)
  683. {
  684. assert ((source != NULL) && (conn != NULL));
  685. memset (source, 0, sizeof (mar_message_source_t));
  686. source->nodeid = totempg_my_nodeid_get ();
  687. source->conn = conn;
  688. }
  689. /*
  690. * Provides the glue from corosync to the IPC Service
  691. */
  692. static int corosync_private_data_size_get (unsigned int service)
  693. {
  694. return (ais_service[service]->private_data_size);
  695. }
  696. static coroipcs_init_fn_lvalue corosync_init_fn_get (unsigned int service)
  697. {
  698. return (ais_service[service]->lib_init_fn);
  699. }
  700. static coroipcs_exit_fn_lvalue corosync_exit_fn_get (unsigned int service)
  701. {
  702. return (ais_service[service]->lib_exit_fn);
  703. }
  704. static coroipcs_handler_fn_lvalue corosync_handler_fn_get (unsigned int service, unsigned int id)
  705. {
  706. return (ais_service[service]->lib_engine[id].lib_handler_fn);
  707. }
  708. static int corosync_security_valid (int euid, int egid)
  709. {
  710. struct list_head *iter;
  711. if (euid == 0 || egid == 0) {
  712. return (1);
  713. }
  714. for (iter = uidgid_list_head.next; iter != &uidgid_list_head;
  715. iter = iter->next) {
  716. struct uidgid_item *ugi = list_entry (iter, struct uidgid_item,
  717. list);
  718. if (euid == ugi->uid || egid == ugi->gid)
  719. return (1);
  720. }
  721. return (0);
  722. }
  723. static int corosync_service_available (unsigned int service)
  724. {
  725. return (ais_service[service] != NULL);
  726. }
  727. struct sending_allowed_private_data_struct {
  728. int reserved_msgs;
  729. };
  730. static int corosync_sending_allowed (
  731. unsigned int service,
  732. unsigned int id,
  733. const void *msg,
  734. void *sending_allowed_private_data)
  735. {
  736. struct sending_allowed_private_data_struct *pd =
  737. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  738. struct iovec reserve_iovec;
  739. coroipc_request_header_t *header = (coroipc_request_header_t *)msg;
  740. int sending_allowed;
  741. reserve_iovec.iov_base = (char *)header;
  742. reserve_iovec.iov_len = header->size;
  743. pd->reserved_msgs = totempg_groups_joined_reserve (
  744. corosync_group_handle,
  745. &reserve_iovec, 1);
  746. if (pd->reserved_msgs == -1) {
  747. return (-1);
  748. }
  749. sending_allowed =
  750. (corosync_quorum_is_quorate() == 1 ||
  751. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  752. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  753. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  754. (pd->reserved_msgs) &&
  755. (sync_in_process == 0)));
  756. return (sending_allowed);
  757. }
  758. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  759. {
  760. struct sending_allowed_private_data_struct *pd =
  761. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  762. if (pd->reserved_msgs == -1) {
  763. return;
  764. }
  765. totempg_groups_joined_release (pd->reserved_msgs);
  766. }
  767. static int ipc_subsys_id = -1;
  768. static void ipc_fatal_error(const char *error_msg) {
  769. _logsys_log_printf (
  770. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  771. ipc_subsys_id,
  772. LOGSYS_RECID_LOG),
  773. __FUNCTION__, __FILE__, __LINE__,
  774. "%s", error_msg);
  775. exit(EXIT_FAILURE);
  776. }
  777. static int corosync_poll_handler_accept (
  778. hdb_handle_t handle,
  779. int fd,
  780. int revent,
  781. void *context)
  782. {
  783. return (coroipcs_handler_accept (fd, revent, context));
  784. }
  785. static int corosync_poll_handler_dispatch (
  786. hdb_handle_t handle,
  787. int fd,
  788. int revent,
  789. void *context)
  790. {
  791. return (coroipcs_handler_dispatch (fd, revent, context));
  792. }
  793. static void corosync_poll_accept_add (
  794. int fd)
  795. {
  796. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  797. corosync_poll_handler_accept);
  798. }
  799. static void corosync_poll_dispatch_add (
  800. int fd,
  801. void *context)
  802. {
  803. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  804. corosync_poll_handler_dispatch);
  805. }
  806. static void corosync_poll_dispatch_modify (
  807. int fd,
  808. int events)
  809. {
  810. poll_dispatch_modify (corosync_poll_handle, fd, events,
  811. corosync_poll_handler_dispatch);
  812. }
  813. static hdb_handle_t corosync_stats_create_connection (const char* name,
  814. const pid_t pid, const int fd)
  815. {
  816. uint32_t zero_32 = 0;
  817. uint64_t zero_64 = 0;
  818. unsigned int key_incr_dummy;
  819. hdb_handle_t object_handle;
  820. objdb->object_key_increment (object_connection_handle,
  821. "active", strlen("active"),
  822. &key_incr_dummy);
  823. objdb->object_create (object_connection_handle,
  824. &object_handle,
  825. name,
  826. strlen (name));
  827. objdb->object_key_create_typed (object_handle,
  828. "service_id",
  829. &zero_32, sizeof (zero_32),
  830. OBJDB_VALUETYPE_UINT32);
  831. objdb->object_key_create_typed (object_handle,
  832. "client_pid",
  833. &pid, sizeof (pid),
  834. OBJDB_VALUETYPE_INT32);
  835. objdb->object_key_create_typed (object_handle,
  836. "responses",
  837. &zero_64, sizeof (zero_64),
  838. OBJDB_VALUETYPE_UINT64);
  839. objdb->object_key_create_typed (object_handle,
  840. "dispatched",
  841. &zero_64, sizeof (zero_64),
  842. OBJDB_VALUETYPE_UINT64);
  843. objdb->object_key_create_typed (object_handle,
  844. "requests",
  845. &zero_64, sizeof (zero_64),
  846. OBJDB_VALUETYPE_INT64);
  847. objdb->object_key_create_typed (object_handle,
  848. "sem_retry_count",
  849. &zero_64, sizeof (zero_64),
  850. OBJDB_VALUETYPE_UINT64);
  851. objdb->object_key_create_typed (object_handle,
  852. "send_retry_count",
  853. &zero_64, sizeof (zero_64),
  854. OBJDB_VALUETYPE_UINT64);
  855. objdb->object_key_create_typed (object_handle,
  856. "recv_retry_count",
  857. &zero_64, sizeof (zero_64),
  858. OBJDB_VALUETYPE_UINT64);
  859. return object_handle;
  860. }
  861. static void corosync_stats_destroy_connection (hdb_handle_t handle)
  862. {
  863. unsigned int key_incr_dummy;
  864. objdb->object_destroy (handle);
  865. objdb->object_key_increment (object_connection_handle,
  866. "closed", strlen("closed"),
  867. &key_incr_dummy);
  868. objdb->object_key_decrement (object_connection_handle,
  869. "active", strlen("active"),
  870. &key_incr_dummy);
  871. }
  872. static void corosync_stats_update_value (hdb_handle_t handle,
  873. const char *name, const void *value,
  874. size_t value_len)
  875. {
  876. objdb->object_key_replace (handle,
  877. name, strlen(name),
  878. value, value_len);
  879. }
  880. static void corosync_stats_increment_value (hdb_handle_t handle,
  881. const char* name)
  882. {
  883. unsigned int key_incr_dummy;
  884. objdb->object_key_increment (handle,
  885. name, strlen(name),
  886. &key_incr_dummy);
  887. }
  888. static struct coroipcs_init_state_v2 ipc_init_state_v2 = {
  889. .socket_name = COROSYNC_SOCKET_NAME,
  890. .sched_policy = SCHED_OTHER,
  891. .sched_param = &global_sched_param,
  892. .malloc = malloc,
  893. .free = free,
  894. .log_printf = _logsys_log_printf,
  895. .fatal_error = ipc_fatal_error,
  896. .security_valid = corosync_security_valid,
  897. .service_available = corosync_service_available,
  898. .private_data_size_get = corosync_private_data_size_get,
  899. .serialize_lock = serialize_lock,
  900. .serialize_unlock = serialize_unlock,
  901. .sending_allowed = corosync_sending_allowed,
  902. .sending_allowed_release = corosync_sending_allowed_release,
  903. .poll_accept_add = corosync_poll_accept_add,
  904. .poll_dispatch_add = corosync_poll_dispatch_add,
  905. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  906. .init_fn_get = corosync_init_fn_get,
  907. .exit_fn_get = corosync_exit_fn_get,
  908. .handler_fn_get = corosync_handler_fn_get,
  909. .stats_create_connection = corosync_stats_create_connection,
  910. .stats_destroy_connection = corosync_stats_destroy_connection,
  911. .stats_update_value = corosync_stats_update_value,
  912. .stats_increment_value = corosync_stats_increment_value,
  913. };
  914. static void corosync_setscheduler (void)
  915. {
  916. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  917. int res;
  918. sched_priority = sched_get_priority_max (SCHED_RR);
  919. if (sched_priority != -1) {
  920. global_sched_param.sched_priority = sched_priority;
  921. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  922. if (res == -1) {
  923. global_sched_param.sched_priority = 0;
  924. log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  925. global_sched_param.sched_priority, strerror (errno));
  926. logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  927. } else {
  928. /*
  929. * Turn on SCHED_RR in ipc system
  930. */
  931. ipc_init_state_v2.sched_policy = SCHED_RR;
  932. /*
  933. * Turn on SCHED_RR in logsys system
  934. */
  935. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  936. if (res == -1) {
  937. log_printf (LOGSYS_LEVEL_ERROR,
  938. "Could not set logsys thread priority."
  939. " Can't continue because of priority inversions.");
  940. corosync_exit_error (AIS_DONE_LOGSETUP);
  941. }
  942. }
  943. } else {
  944. log_printf (LOGSYS_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  945. sched_priority = 0;
  946. }
  947. #else
  948. log_printf(LOGSYS_LEVEL_WARNING,
  949. "The Platform is missing process priority setting features. Leaving at default.");
  950. #endif
  951. }
  952. static void corosync_stats_init (void)
  953. {
  954. hdb_handle_t object_find_handle;
  955. hdb_handle_t object_runtime_handle;
  956. uint64_t zero_64 = 0;
  957. objdb->object_find_create (OBJECT_PARENT_HANDLE,
  958. "runtime", strlen ("runtime"),
  959. &object_find_handle);
  960. if (objdb->object_find_next (object_find_handle,
  961. &object_runtime_handle) != 0) {
  962. return;
  963. }
  964. /* Connection objects */
  965. objdb->object_create (object_runtime_handle,
  966. &object_connection_handle,
  967. "connections", strlen ("connections"));
  968. objdb->object_key_create_typed (object_connection_handle,
  969. "active", &zero_64, sizeof (zero_64),
  970. OBJDB_VALUETYPE_UINT64);
  971. objdb->object_key_create_typed (object_connection_handle,
  972. "closed", &zero_64, sizeof (zero_64),
  973. OBJDB_VALUETYPE_UINT64);
  974. }
  975. static void main_service_ready (void)
  976. {
  977. int res;
  978. /*
  979. * This must occur after totempg is initialized because "this_ip" must be set
  980. */
  981. res = corosync_service_defaults_link_and_init (api);
  982. if (res == -1) {
  983. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  984. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  985. }
  986. evil_init (api);
  987. corosync_stats_init ();
  988. corosync_totem_stats_init ();
  989. }
  990. int main (int argc, char **argv)
  991. {
  992. const char *error_string;
  993. struct totem_config totem_config;
  994. hdb_handle_t objdb_handle;
  995. hdb_handle_t config_handle;
  996. unsigned int config_version = 0;
  997. void *objdb_p;
  998. struct config_iface_ver0 *config;
  999. void *config_p;
  1000. const char *config_iface_init;
  1001. char *config_iface;
  1002. char *iface;
  1003. int res, ch;
  1004. int background, setprio;
  1005. struct stat stat_out;
  1006. char corosync_lib_dir[PATH_MAX];
  1007. hdb_handle_t object_runtime_handle;
  1008. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  1009. pthread_spin_init (&serialize_spin, 0);
  1010. #endif
  1011. /* default configuration
  1012. */
  1013. background = 1;
  1014. setprio = 1;
  1015. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  1016. switch (ch) {
  1017. case 'f':
  1018. background = 0;
  1019. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  1020. break;
  1021. case 'p':
  1022. setprio = 0;
  1023. break;
  1024. case 'v':
  1025. printf ("Corosync Cluster Engine, version '%s' SVN revision '%s'\n", VERSION, SVN_REVISION);
  1026. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  1027. return EXIT_SUCCESS;
  1028. break;
  1029. default:
  1030. fprintf(stderr, \
  1031. "usage:\n"\
  1032. " -f : Start application in foreground.\n"\
  1033. " -p : Do not set process priority. \n"\
  1034. " -v : Display version and SVN revision of Corosync and exit.\n");
  1035. return EXIT_FAILURE;
  1036. }
  1037. }
  1038. /*
  1039. * Set round robin realtime scheduling with priority 99
  1040. * Lock all memory to avoid page faults which may interrupt
  1041. * application healthchecking
  1042. */
  1043. if (setprio) {
  1044. corosync_setscheduler ();
  1045. }
  1046. corosync_mlockall ();
  1047. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  1048. log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "\n");
  1049. (void)signal (SIGINT, sigintr_handler);
  1050. (void)signal (SIGUSR2, sigusr2_handler);
  1051. (void)signal (SIGSEGV, sigsegv_handler);
  1052. (void)signal (SIGABRT, sigabrt_handler);
  1053. (void)signal (SIGQUIT, sigquit_handler);
  1054. (void)signal (SIGTERM, sigterm_handler);
  1055. #if MSG_NOSIGNAL != 0
  1056. (void)signal (SIGPIPE, SIG_IGN);
  1057. #endif
  1058. corosync_timer_init (
  1059. serialize_lock,
  1060. serialize_unlock,
  1061. sched_priority);
  1062. corosync_poll_handle = poll_create ();
  1063. /*
  1064. * Load the object database interface
  1065. */
  1066. res = lcr_ifact_reference (
  1067. &objdb_handle,
  1068. "objdb",
  1069. 0,
  1070. &objdb_p,
  1071. 0);
  1072. if (res == -1) {
  1073. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  1074. corosync_exit_error (AIS_DONE_OBJDB);
  1075. }
  1076. objdb = (struct objdb_iface_ver0 *)objdb_p;
  1077. objdb->objdb_init ();
  1078. /*
  1079. * Initialize the corosync_api_v1 definition
  1080. */
  1081. apidef_init (objdb);
  1082. api = apidef_get ();
  1083. num_config_modules = 0;
  1084. /*
  1085. * Bootstrap in the default configuration parser or use
  1086. * the corosync default built in parser if the configuration parser
  1087. * isn't overridden
  1088. */
  1089. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  1090. if (!config_iface_init) {
  1091. config_iface_init = "corosync_parser";
  1092. }
  1093. /* Make a copy so we can deface it with strtok */
  1094. if ((config_iface = strdup(config_iface_init)) == NULL) {
  1095. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  1096. corosync_exit_error (AIS_DONE_OBJDB);
  1097. }
  1098. iface = strtok(config_iface, ":");
  1099. while (iface)
  1100. {
  1101. res = lcr_ifact_reference (
  1102. &config_handle,
  1103. iface,
  1104. config_version,
  1105. &config_p,
  1106. 0);
  1107. config = (struct config_iface_ver0 *)config_p;
  1108. if (res == -1) {
  1109. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  1110. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1111. }
  1112. res = config->config_readconfig(objdb, &error_string);
  1113. if (res == -1) {
  1114. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1115. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1116. }
  1117. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  1118. config_modules[num_config_modules++] = config;
  1119. iface = strtok(NULL, ":");
  1120. }
  1121. free(config_iface);
  1122. res = corosync_main_config_read (objdb, &error_string);
  1123. if (res == -1) {
  1124. /*
  1125. * if we are here, we _must_ flush the logsys queue
  1126. * and try to inform that we couldn't read the config.
  1127. * this is a desperate attempt before certain death
  1128. * and there is no guarantee that we can print to stderr
  1129. * nor that logsys is sending the messages where we expect.
  1130. */
  1131. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1132. fprintf(stderr, "%s", error_string);
  1133. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1134. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1135. }
  1136. /*
  1137. * Make sure required directory is present
  1138. */
  1139. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  1140. res = stat (corosync_lib_dir, &stat_out);
  1141. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  1142. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  1143. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  1144. }
  1145. res = totem_config_read (objdb, &totem_config, &error_string);
  1146. if (res == -1) {
  1147. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1148. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1149. }
  1150. res = totem_config_keyread (objdb, &totem_config, &error_string);
  1151. if (res == -1) {
  1152. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1153. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1154. }
  1155. res = totem_config_validate (&totem_config, &error_string);
  1156. if (res == -1) {
  1157. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1158. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1159. }
  1160. totem_config.totem_logging_configuration = totem_logging_configuration;
  1161. totem_config.totem_logging_configuration.log_subsys_id =
  1162. _logsys_subsys_create ("TOTEM");
  1163. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  1164. log_printf (LOGSYS_LEVEL_ERROR,
  1165. "Unable to initialize TOTEM logging subsystem\n");
  1166. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1167. }
  1168. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  1169. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  1170. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  1171. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  1172. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  1173. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  1174. res = corosync_main_config_compatibility_read (objdb,
  1175. &minimum_sync_mode,
  1176. &error_string);
  1177. if (res == -1) {
  1178. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1179. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1180. }
  1181. res = corosync_main_config_compatibility_read (objdb,
  1182. &minimum_sync_mode,
  1183. &error_string);
  1184. if (res == -1) {
  1185. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1186. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1187. }
  1188. /* create the main runtime object */
  1189. objdb->object_create (OBJECT_PARENT_HANDLE,
  1190. &object_runtime_handle,
  1191. "runtime", strlen ("runtime"));
  1192. /*
  1193. * Now we are fully initialized.
  1194. */
  1195. if (background) {
  1196. corosync_tty_detach ();
  1197. }
  1198. logsys_fork_completed();
  1199. /*
  1200. * Sleep for a while to let other nodes in the cluster
  1201. * understand that this node has been away (if it was
  1202. * an corosync restart).
  1203. */
  1204. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  1205. /*
  1206. * if totempg_initialize doesn't have root priveleges, it cannot
  1207. * bind to a specific interface. This only matters if
  1208. * there is more then one interface in a system, so
  1209. * in this case, only a warning is printed
  1210. */
  1211. /*
  1212. * Join multicast group and setup delivery
  1213. * and configuration change functions
  1214. */
  1215. totempg_initialize (
  1216. corosync_poll_handle,
  1217. &totem_config);
  1218. totempg_service_ready_register (
  1219. main_service_ready);
  1220. totempg_groups_initialize (
  1221. &corosync_group_handle,
  1222. deliver_fn,
  1223. confchg_fn);
  1224. totempg_groups_join (
  1225. corosync_group_handle,
  1226. &corosync_group,
  1227. 1);
  1228. if (minimum_sync_mode == CS_SYNC_V2) {
  1229. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  1230. sync_v2_init (
  1231. corosync_sync_v2_callbacks_retrieve,
  1232. corosync_sync_completed);
  1233. } else
  1234. if (minimum_sync_mode == CS_SYNC_V1) {
  1235. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  1236. sync_register (
  1237. corosync_sync_callbacks_retrieve,
  1238. sync_v2_memb_list_determine,
  1239. sync_v2_memb_list_abort,
  1240. sync_v2_start);
  1241. sync_v2_init (
  1242. corosync_sync_v2_callbacks_retrieve,
  1243. corosync_sync_completed);
  1244. }
  1245. /*
  1246. * Drop root privleges to user 'ais'
  1247. * TODO: Don't really need full root capabilities;
  1248. * needed capabilities are:
  1249. * CAP_NET_RAW (bindtodevice)
  1250. * CAP_SYS_NICE (setscheduler)
  1251. * CAP_IPC_LOCK (mlockall)
  1252. */
  1253. priv_drop ();
  1254. schedwrk_init (
  1255. serialize_lock,
  1256. serialize_unlock);
  1257. ipc_subsys_id = _logsys_subsys_create ("IPC");
  1258. if (ipc_subsys_id < 0) {
  1259. log_printf (LOGSYS_LEVEL_ERROR,
  1260. "Could not initialize IPC logging subsystem\n");
  1261. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1262. }
  1263. ipc_init_state_v2.log_subsys_id = ipc_subsys_id;
  1264. coroipcs_ipc_init_v2 (&ipc_init_state_v2);
  1265. /*
  1266. * Start main processing loop
  1267. */
  1268. poll_run (corosync_poll_handle);
  1269. return EXIT_SUCCESS;
  1270. }