main.c 53 KB

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