main.c 52 KB

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