main.c 52 KB

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