main.c 55 KB

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