main.c 55 KB

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