main.c 45 KB

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