main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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_log_printf (const char *format, ...) __attribute__((format(printf, 1, 2)));
  769. static void ipc_log_printf (const char *format, ...) {
  770. va_list ap;
  771. va_start (ap, format);
  772. _logsys_log_vprintf (
  773. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  774. ipc_subsys_id,
  775. LOGSYS_RECID_LOG),
  776. __FUNCTION__, __FILE__, __LINE__,
  777. format, ap);
  778. va_end (ap);
  779. }
  780. static void ipc_fatal_error(const char *error_msg) {
  781. _logsys_log_printf (
  782. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  783. ipc_subsys_id,
  784. LOGSYS_RECID_LOG),
  785. __FUNCTION__, __FILE__, __LINE__,
  786. "%s", error_msg);
  787. exit(EXIT_FAILURE);
  788. }
  789. static int corosync_poll_handler_accept (
  790. hdb_handle_t handle,
  791. int fd,
  792. int revent,
  793. void *context)
  794. {
  795. return (coroipcs_handler_accept (fd, revent, context));
  796. }
  797. static int corosync_poll_handler_dispatch (
  798. hdb_handle_t handle,
  799. int fd,
  800. int revent,
  801. void *context)
  802. {
  803. return (coroipcs_handler_dispatch (fd, revent, context));
  804. }
  805. static void corosync_poll_accept_add (
  806. int fd)
  807. {
  808. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  809. corosync_poll_handler_accept);
  810. }
  811. static void corosync_poll_dispatch_add (
  812. int fd,
  813. void *context)
  814. {
  815. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  816. corosync_poll_handler_dispatch);
  817. }
  818. static void corosync_poll_dispatch_modify (
  819. int fd,
  820. int events)
  821. {
  822. poll_dispatch_modify (corosync_poll_handle, fd, events,
  823. corosync_poll_handler_dispatch);
  824. }
  825. static hdb_handle_t corosync_stats_create_connection (const char* name,
  826. const pid_t pid, const int fd)
  827. {
  828. uint32_t zero_32 = 0;
  829. uint64_t zero_64 = 0;
  830. unsigned int key_incr_dummy;
  831. hdb_handle_t object_handle;
  832. objdb->object_key_increment (object_connection_handle,
  833. "active", strlen("active"),
  834. &key_incr_dummy);
  835. objdb->object_create (object_connection_handle,
  836. &object_handle,
  837. name,
  838. strlen (name));
  839. objdb->object_key_create_typed (object_handle,
  840. "service_id",
  841. &zero_32, sizeof (zero_32),
  842. OBJDB_VALUETYPE_UINT32);
  843. objdb->object_key_create_typed (object_handle,
  844. "client_pid",
  845. &pid, sizeof (pid),
  846. OBJDB_VALUETYPE_INT32);
  847. objdb->object_key_create_typed (object_handle,
  848. "responses",
  849. &zero_64, sizeof (zero_64),
  850. OBJDB_VALUETYPE_UINT64);
  851. objdb->object_key_create_typed (object_handle,
  852. "dispatched",
  853. &zero_64, sizeof (zero_64),
  854. OBJDB_VALUETYPE_UINT64);
  855. objdb->object_key_create_typed (object_handle,
  856. "requests",
  857. &zero_64, sizeof (zero_64),
  858. OBJDB_VALUETYPE_INT64);
  859. objdb->object_key_create_typed (object_handle,
  860. "sem_retry_count",
  861. &zero_64, sizeof (zero_64),
  862. OBJDB_VALUETYPE_UINT64);
  863. objdb->object_key_create_typed (object_handle,
  864. "send_retry_count",
  865. &zero_64, sizeof (zero_64),
  866. OBJDB_VALUETYPE_UINT64);
  867. objdb->object_key_create_typed (object_handle,
  868. "recv_retry_count",
  869. &zero_64, sizeof (zero_64),
  870. OBJDB_VALUETYPE_UINT64);
  871. return object_handle;
  872. }
  873. static void corosync_stats_destroy_connection (hdb_handle_t handle)
  874. {
  875. unsigned int key_incr_dummy;
  876. objdb->object_destroy (handle);
  877. objdb->object_key_increment (object_connection_handle,
  878. "closed", strlen("closed"),
  879. &key_incr_dummy);
  880. objdb->object_key_decrement (object_connection_handle,
  881. "active", strlen("active"),
  882. &key_incr_dummy);
  883. }
  884. static void corosync_stats_update_value (hdb_handle_t handle,
  885. const char *name, const void *value,
  886. size_t value_len)
  887. {
  888. objdb->object_key_replace (handle,
  889. name, strlen(name),
  890. value, value_len);
  891. }
  892. static void corosync_stats_increment_value (hdb_handle_t handle,
  893. const char* name)
  894. {
  895. unsigned int key_incr_dummy;
  896. objdb->object_key_increment (handle,
  897. name, strlen(name),
  898. &key_incr_dummy);
  899. }
  900. static struct coroipcs_init_state ipc_init_state = {
  901. .socket_name = COROSYNC_SOCKET_NAME,
  902. .sched_policy = SCHED_OTHER,
  903. .sched_param = &global_sched_param,
  904. .malloc = malloc,
  905. .free = free,
  906. .log_printf = ipc_log_printf,
  907. .fatal_error = ipc_fatal_error,
  908. .security_valid = corosync_security_valid,
  909. .service_available = corosync_service_available,
  910. .private_data_size_get = corosync_private_data_size_get,
  911. .serialize_lock = serialize_lock,
  912. .serialize_unlock = serialize_unlock,
  913. .sending_allowed = corosync_sending_allowed,
  914. .sending_allowed_release = corosync_sending_allowed_release,
  915. .poll_accept_add = corosync_poll_accept_add,
  916. .poll_dispatch_add = corosync_poll_dispatch_add,
  917. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  918. .init_fn_get = corosync_init_fn_get,
  919. .exit_fn_get = corosync_exit_fn_get,
  920. .handler_fn_get = corosync_handler_fn_get
  921. };
  922. static struct coroipcs_init_stats_state ipc_init_stats_state = {
  923. .stats_create_connection = corosync_stats_create_connection,
  924. .stats_destroy_connection = corosync_stats_destroy_connection,
  925. .stats_update_value = corosync_stats_update_value,
  926. .stats_increment_value = corosync_stats_increment_value
  927. };
  928. static void corosync_setscheduler (void)
  929. {
  930. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  931. int res;
  932. sched_priority = sched_get_priority_max (SCHED_RR);
  933. if (sched_priority != -1) {
  934. global_sched_param.sched_priority = sched_priority;
  935. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  936. if (res == -1) {
  937. global_sched_param.sched_priority = 0;
  938. log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  939. global_sched_param.sched_priority, strerror (errno));
  940. logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  941. } else {
  942. /*
  943. * Turn on SCHED_RR in ipc system
  944. */
  945. ipc_init_state.sched_policy = SCHED_RR;
  946. /*
  947. * Turn on SCHED_RR in logsys system
  948. */
  949. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  950. if (res == -1) {
  951. log_printf (LOGSYS_LEVEL_ERROR,
  952. "Could not set logsys thread priority."
  953. " Can't continue because of priority inversions.");
  954. corosync_exit_error (AIS_DONE_LOGSETUP);
  955. }
  956. }
  957. } else {
  958. log_printf (LOGSYS_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  959. sched_priority = 0;
  960. }
  961. #else
  962. log_printf(LOGSYS_LEVEL_WARNING,
  963. "The Platform is missing process priority setting features. Leaving at default.");
  964. #endif
  965. }
  966. static void corosync_stats_init (void)
  967. {
  968. hdb_handle_t object_find_handle;
  969. hdb_handle_t object_runtime_handle;
  970. uint64_t zero_64 = 0;
  971. objdb->object_find_create (OBJECT_PARENT_HANDLE,
  972. "runtime", strlen ("runtime"),
  973. &object_find_handle);
  974. if (objdb->object_find_next (object_find_handle,
  975. &object_runtime_handle) != 0) {
  976. return;
  977. }
  978. /* Connection objects */
  979. objdb->object_create (object_runtime_handle,
  980. &object_connection_handle,
  981. "connections", strlen ("connections"));
  982. objdb->object_key_create_typed (object_connection_handle,
  983. "active", &zero_64, sizeof (zero_64),
  984. OBJDB_VALUETYPE_UINT64);
  985. objdb->object_key_create_typed (object_connection_handle,
  986. "closed", &zero_64, sizeof (zero_64),
  987. OBJDB_VALUETYPE_UINT64);
  988. }
  989. static void main_service_ready (void)
  990. {
  991. int res;
  992. /*
  993. * This must occur after totempg is initialized because "this_ip" must be set
  994. */
  995. res = corosync_service_defaults_link_and_init (api);
  996. if (res == -1) {
  997. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  998. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  999. }
  1000. evil_init (api);
  1001. corosync_stats_init ();
  1002. corosync_totem_stats_init ();
  1003. }
  1004. int main (int argc, char **argv)
  1005. {
  1006. const char *error_string;
  1007. struct totem_config totem_config;
  1008. hdb_handle_t objdb_handle;
  1009. hdb_handle_t config_handle;
  1010. unsigned int config_version = 0;
  1011. void *objdb_p;
  1012. struct config_iface_ver0 *config;
  1013. void *config_p;
  1014. const char *config_iface_init;
  1015. char *config_iface;
  1016. char *iface;
  1017. int res, ch;
  1018. int background, setprio;
  1019. struct stat stat_out;
  1020. char corosync_lib_dir[PATH_MAX];
  1021. hdb_handle_t object_runtime_handle;
  1022. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  1023. pthread_spin_init (&serialize_spin, 0);
  1024. #endif
  1025. /* default configuration
  1026. */
  1027. background = 1;
  1028. setprio = 1;
  1029. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  1030. switch (ch) {
  1031. case 'f':
  1032. background = 0;
  1033. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  1034. break;
  1035. case 'p':
  1036. setprio = 0;
  1037. break;
  1038. case 'v':
  1039. printf ("Corosync Cluster Engine, version '%s' SVN revision '%s'\n", VERSION, SVN_REVISION);
  1040. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  1041. return EXIT_SUCCESS;
  1042. break;
  1043. default:
  1044. fprintf(stderr, \
  1045. "usage:\n"\
  1046. " -f : Start application in foreground.\n"\
  1047. " -p : Do not set process priority. \n"\
  1048. " -v : Display version and SVN revision of Corosync and exit.\n");
  1049. return EXIT_FAILURE;
  1050. }
  1051. }
  1052. /*
  1053. * Set round robin realtime scheduling with priority 99
  1054. * Lock all memory to avoid page faults which may interrupt
  1055. * application healthchecking
  1056. */
  1057. if (setprio) {
  1058. corosync_setscheduler ();
  1059. }
  1060. corosync_mlockall ();
  1061. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  1062. log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "\n");
  1063. (void)signal (SIGINT, sigintr_handler);
  1064. (void)signal (SIGUSR2, sigusr2_handler);
  1065. (void)signal (SIGSEGV, sigsegv_handler);
  1066. (void)signal (SIGABRT, sigabrt_handler);
  1067. (void)signal (SIGQUIT, sigquit_handler);
  1068. (void)signal (SIGTERM, sigterm_handler);
  1069. #if MSG_NOSIGNAL != 0
  1070. (void)signal (SIGPIPE, SIG_IGN);
  1071. #endif
  1072. corosync_timer_init (
  1073. serialize_lock,
  1074. serialize_unlock,
  1075. sched_priority);
  1076. corosync_poll_handle = poll_create ();
  1077. /*
  1078. * Load the object database interface
  1079. */
  1080. res = lcr_ifact_reference (
  1081. &objdb_handle,
  1082. "objdb",
  1083. 0,
  1084. &objdb_p,
  1085. 0);
  1086. if (res == -1) {
  1087. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  1088. corosync_exit_error (AIS_DONE_OBJDB);
  1089. }
  1090. objdb = (struct objdb_iface_ver0 *)objdb_p;
  1091. objdb->objdb_init ();
  1092. /*
  1093. * Initialize the corosync_api_v1 definition
  1094. */
  1095. apidef_init (objdb);
  1096. api = apidef_get ();
  1097. num_config_modules = 0;
  1098. /*
  1099. * Bootstrap in the default configuration parser or use
  1100. * the corosync default built in parser if the configuration parser
  1101. * isn't overridden
  1102. */
  1103. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  1104. if (!config_iface_init) {
  1105. config_iface_init = "corosync_parser";
  1106. }
  1107. /* Make a copy so we can deface it with strtok */
  1108. if ((config_iface = strdup(config_iface_init)) == NULL) {
  1109. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  1110. corosync_exit_error (AIS_DONE_OBJDB);
  1111. }
  1112. iface = strtok(config_iface, ":");
  1113. while (iface)
  1114. {
  1115. res = lcr_ifact_reference (
  1116. &config_handle,
  1117. iface,
  1118. config_version,
  1119. &config_p,
  1120. 0);
  1121. config = (struct config_iface_ver0 *)config_p;
  1122. if (res == -1) {
  1123. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  1124. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1125. }
  1126. res = config->config_readconfig(objdb, &error_string);
  1127. if (res == -1) {
  1128. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1129. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1130. }
  1131. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  1132. config_modules[num_config_modules++] = config;
  1133. iface = strtok(NULL, ":");
  1134. }
  1135. free(config_iface);
  1136. res = corosync_main_config_read (objdb, &error_string);
  1137. if (res == -1) {
  1138. /*
  1139. * if we are here, we _must_ flush the logsys queue
  1140. * and try to inform that we couldn't read the config.
  1141. * this is a desperate attempt before certain death
  1142. * and there is no guarantee that we can print to stderr
  1143. * nor that logsys is sending the messages where we expect.
  1144. */
  1145. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1146. fprintf(stderr, "%s", error_string);
  1147. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1148. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1149. }
  1150. /*
  1151. * Make sure required directory is present
  1152. */
  1153. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  1154. res = stat (corosync_lib_dir, &stat_out);
  1155. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  1156. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  1157. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  1158. }
  1159. res = totem_config_read (objdb, &totem_config, &error_string);
  1160. if (res == -1) {
  1161. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1162. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1163. }
  1164. res = totem_config_keyread (objdb, &totem_config, &error_string);
  1165. if (res == -1) {
  1166. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1167. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1168. }
  1169. res = totem_config_validate (&totem_config, &error_string);
  1170. if (res == -1) {
  1171. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1172. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1173. }
  1174. totem_config.totem_logging_configuration = totem_logging_configuration;
  1175. totem_config.totem_logging_configuration.log_subsys_id =
  1176. _logsys_subsys_create ("TOTEM");
  1177. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  1178. log_printf (LOGSYS_LEVEL_ERROR,
  1179. "Unable to initialize TOTEM logging subsystem\n");
  1180. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1181. }
  1182. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  1183. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  1184. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  1185. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  1186. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  1187. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  1188. res = corosync_main_config_compatibility_read (objdb,
  1189. &minimum_sync_mode,
  1190. &error_string);
  1191. if (res == -1) {
  1192. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1193. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1194. }
  1195. res = corosync_main_config_compatibility_read (objdb,
  1196. &minimum_sync_mode,
  1197. &error_string);
  1198. if (res == -1) {
  1199. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1200. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1201. }
  1202. /* create the main runtime object */
  1203. objdb->object_create (OBJECT_PARENT_HANDLE,
  1204. &object_runtime_handle,
  1205. "runtime", strlen ("runtime"));
  1206. /*
  1207. * Now we are fully initialized.
  1208. */
  1209. if (background) {
  1210. corosync_tty_detach ();
  1211. }
  1212. logsys_fork_completed();
  1213. /*
  1214. * Sleep for a while to let other nodes in the cluster
  1215. * understand that this node has been away (if it was
  1216. * an corosync restart).
  1217. */
  1218. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  1219. /*
  1220. * if totempg_initialize doesn't have root priveleges, it cannot
  1221. * bind to a specific interface. This only matters if
  1222. * there is more then one interface in a system, so
  1223. * in this case, only a warning is printed
  1224. */
  1225. /*
  1226. * Join multicast group and setup delivery
  1227. * and configuration change functions
  1228. */
  1229. totempg_initialize (
  1230. corosync_poll_handle,
  1231. &totem_config);
  1232. totempg_service_ready_register (
  1233. main_service_ready);
  1234. totempg_groups_initialize (
  1235. &corosync_group_handle,
  1236. deliver_fn,
  1237. confchg_fn);
  1238. totempg_groups_join (
  1239. corosync_group_handle,
  1240. &corosync_group,
  1241. 1);
  1242. if (minimum_sync_mode == CS_SYNC_V2) {
  1243. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  1244. sync_v2_init (
  1245. corosync_sync_v2_callbacks_retrieve,
  1246. corosync_sync_completed);
  1247. } else
  1248. if (minimum_sync_mode == CS_SYNC_V1) {
  1249. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  1250. sync_register (
  1251. corosync_sync_callbacks_retrieve,
  1252. sync_v2_memb_list_determine,
  1253. sync_v2_memb_list_abort,
  1254. sync_v2_start);
  1255. sync_v2_init (
  1256. corosync_sync_v2_callbacks_retrieve,
  1257. corosync_sync_completed);
  1258. }
  1259. /*
  1260. * Drop root privleges to user 'ais'
  1261. * TODO: Don't really need full root capabilities;
  1262. * needed capabilities are:
  1263. * CAP_NET_RAW (bindtodevice)
  1264. * CAP_SYS_NICE (setscheduler)
  1265. * CAP_IPC_LOCK (mlockall)
  1266. */
  1267. priv_drop ();
  1268. schedwrk_init (
  1269. serialize_lock,
  1270. serialize_unlock);
  1271. ipc_subsys_id = _logsys_subsys_create ("IPC");
  1272. if (ipc_subsys_id < 0) {
  1273. log_printf (LOGSYS_LEVEL_ERROR,
  1274. "Could not initialize IPC logging subsystem\n");
  1275. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1276. }
  1277. coroipcs_ipc_init (&ipc_init_state);
  1278. coroipcs_ipc_stats_init (&ipc_init_stats_state);
  1279. /*
  1280. * Start main processing loop
  1281. */
  1282. poll_run (corosync_poll_handle);
  1283. return EXIT_SUCCESS;
  1284. }