main.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. if (fn_id >= ais_service[service]->exec_engine_count) {
  659. log_printf(LOGSYS_LEVEL_WARNING, "discarded unknown message %d for service %d (max id %d)",
  660. fn_id, service, ais_service[service]->exec_engine_count);
  661. serialize_unlock();
  662. return;
  663. }
  664. objdb->object_key_increment (service_stats_handle[service][fn_id],
  665. "rx", strlen("rx"),
  666. &key_incr_dummy);
  667. if (endian_conversion_required) {
  668. assert(ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  669. ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  670. ((void *)msg);
  671. }
  672. ais_service[service]->exec_engine[fn_id].exec_handler_fn
  673. (msg, nodeid);
  674. serialize_unlock();
  675. }
  676. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  677. {
  678. *modules = config_modules;
  679. *num = num_config_modules;
  680. }
  681. int main_mcast (
  682. const struct iovec *iovec,
  683. unsigned int iov_len,
  684. unsigned int guarantee)
  685. {
  686. const coroipc_request_header_t *req = iovec->iov_base;
  687. int service;
  688. int fn_id;
  689. unsigned int key_incr_dummy;
  690. service = req->id >> 16;
  691. fn_id = req->id & 0xffff;
  692. if (ais_service[service]) {
  693. objdb->object_key_increment (service_stats_handle[service][fn_id],
  694. "tx", strlen("tx"), &key_incr_dummy);
  695. }
  696. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  697. }
  698. int message_source_is_local (const mar_message_source_t *source)
  699. {
  700. int ret = 0;
  701. assert (source != NULL);
  702. if (source->nodeid == totempg_my_nodeid_get ()) {
  703. ret = 1;
  704. }
  705. return ret;
  706. }
  707. void message_source_set (
  708. mar_message_source_t *source,
  709. void *conn)
  710. {
  711. assert ((source != NULL) && (conn != NULL));
  712. memset (source, 0, sizeof (mar_message_source_t));
  713. source->nodeid = totempg_my_nodeid_get ();
  714. source->conn = conn;
  715. }
  716. /*
  717. * Provides the glue from corosync to the IPC Service
  718. */
  719. static int corosync_private_data_size_get (unsigned int service)
  720. {
  721. return (ais_service[service]->private_data_size);
  722. }
  723. static coroipcs_init_fn_lvalue corosync_init_fn_get (unsigned int service)
  724. {
  725. return (ais_service[service]->lib_init_fn);
  726. }
  727. static coroipcs_exit_fn_lvalue corosync_exit_fn_get (unsigned int service)
  728. {
  729. return (ais_service[service]->lib_exit_fn);
  730. }
  731. static coroipcs_handler_fn_lvalue corosync_handler_fn_get (unsigned int service, unsigned int id)
  732. {
  733. return (ais_service[service]->lib_engine[id].lib_handler_fn);
  734. }
  735. static int corosync_security_valid (int euid, int egid)
  736. {
  737. struct list_head *iter;
  738. if (euid == 0 || egid == 0) {
  739. return (1);
  740. }
  741. for (iter = uidgid_list_head.next; iter != &uidgid_list_head;
  742. iter = iter->next) {
  743. struct uidgid_item *ugi = list_entry (iter, struct uidgid_item,
  744. list);
  745. if (euid == ugi->uid || egid == ugi->gid)
  746. return (1);
  747. }
  748. return (0);
  749. }
  750. static int corosync_service_available (unsigned int service)
  751. {
  752. return (ais_service[service] != NULL && !ais_service_exiting[service]);
  753. }
  754. struct sending_allowed_private_data_struct {
  755. int reserved_msgs;
  756. };
  757. static int corosync_sending_allowed (
  758. unsigned int service,
  759. unsigned int id,
  760. const void *msg,
  761. void *sending_allowed_private_data)
  762. {
  763. struct sending_allowed_private_data_struct *pd =
  764. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  765. struct iovec reserve_iovec;
  766. coroipc_request_header_t *header = (coroipc_request_header_t *)msg;
  767. int sending_allowed;
  768. reserve_iovec.iov_base = (char *)header;
  769. reserve_iovec.iov_len = header->size;
  770. pd->reserved_msgs = totempg_groups_joined_reserve (
  771. corosync_group_handle,
  772. &reserve_iovec, 1);
  773. if (pd->reserved_msgs == -1) {
  774. return (-1);
  775. }
  776. sending_allowed =
  777. (corosync_quorum_is_quorate() == 1 ||
  778. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  779. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  780. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  781. (pd->reserved_msgs) &&
  782. (sync_in_process == 0)));
  783. return (sending_allowed);
  784. }
  785. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  786. {
  787. struct sending_allowed_private_data_struct *pd =
  788. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  789. if (pd->reserved_msgs == -1) {
  790. return;
  791. }
  792. totempg_groups_joined_release (pd->reserved_msgs);
  793. }
  794. static int ipc_subsys_id = -1;
  795. static void ipc_fatal_error(const char *error_msg) {
  796. _logsys_log_printf (
  797. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  798. ipc_subsys_id,
  799. LOGSYS_RECID_LOG),
  800. __FUNCTION__, __FILE__, __LINE__,
  801. "%s", error_msg);
  802. exit(EXIT_FAILURE);
  803. }
  804. static int corosync_poll_handler_accept (
  805. hdb_handle_t handle,
  806. int fd,
  807. int revent,
  808. void *context)
  809. {
  810. return (coroipcs_handler_accept (fd, revent, context));
  811. }
  812. static int corosync_poll_handler_dispatch (
  813. hdb_handle_t handle,
  814. int fd,
  815. int revent,
  816. void *context)
  817. {
  818. return (coroipcs_handler_dispatch (fd, revent, context));
  819. }
  820. static void corosync_poll_accept_add (
  821. int fd)
  822. {
  823. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  824. corosync_poll_handler_accept);
  825. }
  826. static void corosync_poll_dispatch_add (
  827. int fd,
  828. void *context)
  829. {
  830. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  831. corosync_poll_handler_dispatch);
  832. }
  833. static void corosync_poll_dispatch_modify (
  834. int fd,
  835. int events)
  836. {
  837. poll_dispatch_modify (corosync_poll_handle, fd, events,
  838. corosync_poll_handler_dispatch);
  839. }
  840. static void corosync_poll_dispatch_destroy (
  841. int fd,
  842. void *context)
  843. {
  844. poll_dispatch_delete (corosync_poll_handle, fd);
  845. }
  846. static hdb_handle_t corosync_stats_create_connection (const char* name,
  847. const pid_t pid, const int fd)
  848. {
  849. uint32_t zero_32 = 0;
  850. uint64_t zero_64 = 0;
  851. unsigned int key_incr_dummy;
  852. hdb_handle_t object_handle;
  853. objdb->object_key_increment (object_connection_handle,
  854. "active", strlen("active"),
  855. &key_incr_dummy);
  856. objdb->object_create (object_connection_handle,
  857. &object_handle,
  858. name,
  859. strlen (name));
  860. objdb->object_key_create_typed (object_handle,
  861. "service_id",
  862. &zero_32, sizeof (zero_32),
  863. OBJDB_VALUETYPE_UINT32);
  864. objdb->object_key_create_typed (object_handle,
  865. "client_pid",
  866. &pid, sizeof (pid),
  867. OBJDB_VALUETYPE_INT32);
  868. objdb->object_key_create_typed (object_handle,
  869. "responses",
  870. &zero_64, sizeof (zero_64),
  871. OBJDB_VALUETYPE_UINT64);
  872. objdb->object_key_create_typed (object_handle,
  873. "dispatched",
  874. &zero_64, sizeof (zero_64),
  875. OBJDB_VALUETYPE_UINT64);
  876. objdb->object_key_create_typed (object_handle,
  877. "requests",
  878. &zero_64, sizeof (zero_64),
  879. OBJDB_VALUETYPE_INT64);
  880. objdb->object_key_create_typed (object_handle,
  881. "sem_retry_count",
  882. &zero_64, sizeof (zero_64),
  883. OBJDB_VALUETYPE_UINT64);
  884. objdb->object_key_create_typed (object_handle,
  885. "send_retry_count",
  886. &zero_64, sizeof (zero_64),
  887. OBJDB_VALUETYPE_UINT64);
  888. objdb->object_key_create_typed (object_handle,
  889. "recv_retry_count",
  890. &zero_64, sizeof (zero_64),
  891. OBJDB_VALUETYPE_UINT64);
  892. objdb->object_key_create_typed (object_handle,
  893. "flow_control",
  894. &zero_32, sizeof (zero_32),
  895. OBJDB_VALUETYPE_UINT32);
  896. objdb->object_key_create_typed (object_handle,
  897. "flow_control_count",
  898. &zero_64, sizeof (zero_64),
  899. OBJDB_VALUETYPE_UINT64);
  900. objdb->object_key_create_typed (object_handle,
  901. "queue_size",
  902. &zero_64, sizeof (zero_64),
  903. OBJDB_VALUETYPE_UINT64);
  904. return object_handle;
  905. }
  906. static void corosync_stats_destroy_connection (hdb_handle_t handle)
  907. {
  908. unsigned int key_incr_dummy;
  909. objdb->object_destroy (handle);
  910. objdb->object_key_increment (object_connection_handle,
  911. "closed", strlen("closed"),
  912. &key_incr_dummy);
  913. objdb->object_key_decrement (object_connection_handle,
  914. "active", strlen("active"),
  915. &key_incr_dummy);
  916. }
  917. static void corosync_stats_update_value (hdb_handle_t handle,
  918. const char *name, const void *value,
  919. size_t value_len)
  920. {
  921. objdb->object_key_replace (handle,
  922. name, strlen(name),
  923. value, value_len);
  924. }
  925. static void corosync_stats_increment_value (hdb_handle_t handle,
  926. const char* name)
  927. {
  928. unsigned int key_incr_dummy;
  929. objdb->object_key_increment (handle,
  930. name, strlen(name),
  931. &key_incr_dummy);
  932. }
  933. static void corosync_stats_decrement_value (hdb_handle_t handle,
  934. const char* name)
  935. {
  936. unsigned int key_incr_dummy;
  937. objdb->object_key_decrement (handle,
  938. name, strlen(name),
  939. &key_incr_dummy);
  940. }
  941. static struct coroipcs_init_state_v2 ipc_init_state_v2 = {
  942. .socket_name = COROSYNC_SOCKET_NAME,
  943. .sched_policy = SCHED_OTHER,
  944. .sched_param = &global_sched_param,
  945. .malloc = malloc,
  946. .free = free,
  947. .log_printf = _logsys_log_printf,
  948. .fatal_error = ipc_fatal_error,
  949. .security_valid = corosync_security_valid,
  950. .service_available = corosync_service_available,
  951. .private_data_size_get = corosync_private_data_size_get,
  952. .serialize_lock = serialize_lock,
  953. .serialize_unlock = serialize_unlock,
  954. .sending_allowed = corosync_sending_allowed,
  955. .sending_allowed_release = corosync_sending_allowed_release,
  956. .poll_accept_add = corosync_poll_accept_add,
  957. .poll_dispatch_add = corosync_poll_dispatch_add,
  958. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  959. .poll_dispatch_destroy = corosync_poll_dispatch_destroy,
  960. .init_fn_get = corosync_init_fn_get,
  961. .exit_fn_get = corosync_exit_fn_get,
  962. .handler_fn_get = corosync_handler_fn_get,
  963. .stats_create_connection = corosync_stats_create_connection,
  964. .stats_destroy_connection = corosync_stats_destroy_connection,
  965. .stats_update_value = corosync_stats_update_value,
  966. .stats_increment_value = corosync_stats_increment_value,
  967. .stats_decrement_value = corosync_stats_decrement_value,
  968. };
  969. static void corosync_setscheduler (void)
  970. {
  971. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  972. int res;
  973. sched_priority = sched_get_priority_max (SCHED_RR);
  974. if (sched_priority != -1) {
  975. global_sched_param.sched_priority = sched_priority;
  976. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  977. if (res == -1) {
  978. char error_str[100];
  979. strerror_r (errno, error_str, 100);
  980. global_sched_param.sched_priority = 0;
  981. log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  982. global_sched_param.sched_priority, error_str);
  983. logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  984. } else {
  985. /*
  986. * Turn on SCHED_RR in ipc system
  987. */
  988. ipc_init_state_v2.sched_policy = SCHED_RR;
  989. /*
  990. * Turn on SCHED_RR in logsys system
  991. */
  992. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  993. if (res == -1) {
  994. log_printf (LOGSYS_LEVEL_ERROR,
  995. "Could not set logsys thread priority."
  996. " Can't continue because of priority inversions.");
  997. corosync_exit_error (AIS_DONE_LOGSETUP);
  998. }
  999. }
  1000. } else {
  1001. char error_str[100];
  1002. strerror_r (errno, error_str, 100);
  1003. log_printf (LOGSYS_LEVEL_WARNING,
  1004. "Could not get maximum scheduler priority: %s\n",
  1005. error_str);
  1006. sched_priority = 0;
  1007. }
  1008. #else
  1009. log_printf(LOGSYS_LEVEL_WARNING,
  1010. "The Platform is missing process priority setting features. Leaving at default.");
  1011. #endif
  1012. }
  1013. static void corosync_stats_init (void)
  1014. {
  1015. hdb_handle_t object_find_handle;
  1016. hdb_handle_t object_runtime_handle;
  1017. uint64_t zero_64 = 0;
  1018. objdb->object_find_create (OBJECT_PARENT_HANDLE,
  1019. "runtime", strlen ("runtime"),
  1020. &object_find_handle);
  1021. if (objdb->object_find_next (object_find_handle,
  1022. &object_runtime_handle) != 0) {
  1023. return;
  1024. }
  1025. /* Connection objects */
  1026. objdb->object_create (object_runtime_handle,
  1027. &object_connection_handle,
  1028. "connections", strlen ("connections"));
  1029. objdb->object_key_create_typed (object_connection_handle,
  1030. "active", &zero_64, sizeof (zero_64),
  1031. OBJDB_VALUETYPE_UINT64);
  1032. objdb->object_key_create_typed (object_connection_handle,
  1033. "closed", &zero_64, sizeof (zero_64),
  1034. OBJDB_VALUETYPE_UINT64);
  1035. }
  1036. static void main_service_ready (void)
  1037. {
  1038. int res;
  1039. /*
  1040. * This must occur after totempg is initialized because "this_ip" must be set
  1041. */
  1042. res = corosync_service_defaults_link_and_init (api);
  1043. if (res == -1) {
  1044. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  1045. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1046. }
  1047. evil_init (api);
  1048. corosync_stats_init ();
  1049. corosync_totem_stats_init ();
  1050. if (minimum_sync_mode == CS_SYNC_V2) {
  1051. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  1052. sync_v2_init (
  1053. corosync_sync_v2_callbacks_retrieve,
  1054. corosync_sync_completed);
  1055. } else
  1056. if (minimum_sync_mode == CS_SYNC_V1) {
  1057. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  1058. sync_register (
  1059. corosync_sync_callbacks_retrieve,
  1060. sync_v2_memb_list_determine,
  1061. sync_v2_memb_list_abort,
  1062. sync_v2_start);
  1063. sync_v2_init (
  1064. corosync_sync_v2_callbacks_retrieve,
  1065. corosync_sync_completed);
  1066. }
  1067. }
  1068. int main (int argc, char **argv)
  1069. {
  1070. const char *error_string;
  1071. struct totem_config totem_config;
  1072. hdb_handle_t objdb_handle;
  1073. hdb_handle_t config_handle;
  1074. unsigned int config_version = 0;
  1075. void *objdb_p;
  1076. struct config_iface_ver0 *config;
  1077. void *config_p;
  1078. const char *config_iface_init;
  1079. char *config_iface;
  1080. char *iface;
  1081. char *strtok_save_pt;
  1082. int res, ch;
  1083. int background, setprio;
  1084. struct stat stat_out;
  1085. char corosync_lib_dir[PATH_MAX];
  1086. hdb_handle_t object_runtime_handle;
  1087. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  1088. pthread_spin_init (&serialize_spin, 0);
  1089. #endif
  1090. /* default configuration
  1091. */
  1092. background = 1;
  1093. setprio = 1;
  1094. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  1095. switch (ch) {
  1096. case 'f':
  1097. background = 0;
  1098. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  1099. break;
  1100. case 'p':
  1101. setprio = 0;
  1102. break;
  1103. case 'v':
  1104. printf ("Corosync Cluster Engine, version '%s' SVN revision '%s'\n", VERSION, SVN_REVISION);
  1105. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  1106. return EXIT_SUCCESS;
  1107. break;
  1108. default:
  1109. fprintf(stderr, \
  1110. "usage:\n"\
  1111. " -f : Start application in foreground.\n"\
  1112. " -p : Do not set process priority. \n"\
  1113. " -v : Display version and SVN revision of Corosync and exit.\n");
  1114. return EXIT_FAILURE;
  1115. }
  1116. }
  1117. /*
  1118. * Set round robin realtime scheduling with priority 99
  1119. * Lock all memory to avoid page faults which may interrupt
  1120. * application healthchecking
  1121. */
  1122. if (setprio) {
  1123. corosync_setscheduler ();
  1124. }
  1125. corosync_mlockall ();
  1126. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  1127. log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "\n");
  1128. (void)signal (SIGINT, sigintr_handler);
  1129. (void)signal (SIGUSR2, sigusr2_handler);
  1130. (void)signal (SIGSEGV, sigsegv_handler);
  1131. (void)signal (SIGABRT, sigabrt_handler);
  1132. (void)signal (SIGQUIT, sigquit_handler);
  1133. (void)signal (SIGTERM, sigterm_handler);
  1134. #if MSG_NOSIGNAL != 0
  1135. (void)signal (SIGPIPE, SIG_IGN);
  1136. #endif
  1137. /*
  1138. * Load the object database interface
  1139. */
  1140. res = lcr_ifact_reference (
  1141. &objdb_handle,
  1142. "objdb",
  1143. 0,
  1144. &objdb_p,
  1145. 0);
  1146. if (res == -1) {
  1147. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  1148. corosync_exit_error (AIS_DONE_OBJDB);
  1149. }
  1150. objdb = (struct objdb_iface_ver0 *)objdb_p;
  1151. objdb->objdb_init ();
  1152. /*
  1153. * Initialize the corosync_api_v1 definition
  1154. */
  1155. apidef_init (objdb);
  1156. api = apidef_get ();
  1157. num_config_modules = 0;
  1158. /*
  1159. * Bootstrap in the default configuration parser or use
  1160. * the corosync default built in parser if the configuration parser
  1161. * isn't overridden
  1162. */
  1163. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  1164. if (!config_iface_init) {
  1165. config_iface_init = "corosync_parser";
  1166. }
  1167. /* Make a copy so we can deface it with strtok */
  1168. if ((config_iface = strdup(config_iface_init)) == NULL) {
  1169. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  1170. corosync_exit_error (AIS_DONE_OBJDB);
  1171. }
  1172. iface = strtok_r(config_iface, ":", &strtok_save_pt);
  1173. while (iface)
  1174. {
  1175. res = lcr_ifact_reference (
  1176. &config_handle,
  1177. iface,
  1178. config_version,
  1179. &config_p,
  1180. 0);
  1181. config = (struct config_iface_ver0 *)config_p;
  1182. if (res == -1) {
  1183. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  1184. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1185. }
  1186. res = config->config_readconfig(objdb, &error_string);
  1187. if (res == -1) {
  1188. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1189. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1190. }
  1191. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  1192. config_modules[num_config_modules++] = config;
  1193. iface = strtok_r(NULL, ":", &strtok_save_pt);
  1194. }
  1195. free(config_iface);
  1196. res = corosync_main_config_read (objdb, &error_string);
  1197. if (res == -1) {
  1198. /*
  1199. * if we are here, we _must_ flush the logsys queue
  1200. * and try to inform that we couldn't read the config.
  1201. * this is a desperate attempt before certain death
  1202. * and there is no guarantee that we can print to stderr
  1203. * nor that logsys is sending the messages where we expect.
  1204. */
  1205. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1206. fprintf(stderr, "%s", error_string);
  1207. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1208. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1209. }
  1210. /*
  1211. * Make sure required directory is present
  1212. */
  1213. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  1214. res = stat (corosync_lib_dir, &stat_out);
  1215. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  1216. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  1217. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  1218. }
  1219. res = totem_config_read (objdb, &totem_config, &error_string);
  1220. if (res == -1) {
  1221. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1222. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1223. }
  1224. res = totem_config_keyread (objdb, &totem_config, &error_string);
  1225. if (res == -1) {
  1226. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1227. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1228. }
  1229. res = totem_config_validate (&totem_config, &error_string);
  1230. if (res == -1) {
  1231. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1232. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1233. }
  1234. totem_config.totem_logging_configuration = totem_logging_configuration;
  1235. totem_config.totem_logging_configuration.log_subsys_id =
  1236. _logsys_subsys_create ("TOTEM");
  1237. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  1238. log_printf (LOGSYS_LEVEL_ERROR,
  1239. "Unable to initialize TOTEM logging subsystem\n");
  1240. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1241. }
  1242. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  1243. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  1244. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  1245. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  1246. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  1247. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  1248. res = corosync_main_config_compatibility_read (objdb,
  1249. &minimum_sync_mode,
  1250. &error_string);
  1251. if (res == -1) {
  1252. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1253. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1254. }
  1255. res = corosync_main_config_compatibility_read (objdb,
  1256. &minimum_sync_mode,
  1257. &error_string);
  1258. if (res == -1) {
  1259. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1260. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1261. }
  1262. /* create the main runtime object */
  1263. objdb->object_create (OBJECT_PARENT_HANDLE,
  1264. &object_runtime_handle,
  1265. "runtime", strlen ("runtime"));
  1266. /*
  1267. * Now we are fully initialized.
  1268. */
  1269. if (background) {
  1270. corosync_tty_detach ();
  1271. }
  1272. logsys_fork_completed();
  1273. corosync_timer_init (
  1274. serialize_lock,
  1275. serialize_unlock,
  1276. sched_priority);
  1277. corosync_poll_handle = poll_create ();
  1278. /*
  1279. * Sleep for a while to let other nodes in the cluster
  1280. * understand that this node has been away (if it was
  1281. * an corosync restart).
  1282. */
  1283. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  1284. /*
  1285. * Create semaphore and start "exit" thread
  1286. */
  1287. res = sem_init (&corosync_exit_sem, 0, 0);
  1288. if (res != 0) {
  1289. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create exit thread.\n");
  1290. corosync_exit_error (AIS_DONE_FATAL_ERR);
  1291. }
  1292. res = pthread_create (&corosync_exit_thread, NULL, corosync_exit_thread_handler, NULL);
  1293. if (res != 0) {
  1294. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create exit thread.\n");
  1295. corosync_exit_error (AIS_DONE_FATAL_ERR);
  1296. }
  1297. /*
  1298. * if totempg_initialize doesn't have root priveleges, it cannot
  1299. * bind to a specific interface. This only matters if
  1300. * there is more then one interface in a system, so
  1301. * in this case, only a warning is printed
  1302. */
  1303. /*
  1304. * Join multicast group and setup delivery
  1305. * and configuration change functions
  1306. */
  1307. totempg_initialize (
  1308. corosync_poll_handle,
  1309. &totem_config);
  1310. totempg_service_ready_register (
  1311. main_service_ready);
  1312. totempg_groups_initialize (
  1313. &corosync_group_handle,
  1314. deliver_fn,
  1315. confchg_fn);
  1316. totempg_groups_join (
  1317. corosync_group_handle,
  1318. &corosync_group,
  1319. 1);
  1320. /*
  1321. * Drop root privleges to user 'ais'
  1322. * TODO: Don't really need full root capabilities;
  1323. * needed capabilities are:
  1324. * CAP_NET_RAW (bindtodevice)
  1325. * CAP_SYS_NICE (setscheduler)
  1326. * CAP_IPC_LOCK (mlockall)
  1327. */
  1328. priv_drop ();
  1329. schedwrk_init (
  1330. serialize_lock,
  1331. serialize_unlock);
  1332. ipc_subsys_id = _logsys_subsys_create ("IPC");
  1333. if (ipc_subsys_id < 0) {
  1334. log_printf (LOGSYS_LEVEL_ERROR,
  1335. "Could not initialize IPC logging subsystem\n");
  1336. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1337. }
  1338. ipc_init_state_v2.log_subsys_id = ipc_subsys_id;
  1339. coroipcs_ipc_init_v2 (&ipc_init_state_v2);
  1340. /*
  1341. * Start main processing loop
  1342. */
  1343. poll_run (corosync_poll_handle);
  1344. return EXIT_SUCCESS;
  1345. }