main.c 56 KB

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