main.c 54 KB

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