main.c 45 KB

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