main.c 55 KB

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