main.c 55 KB

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