main.c 56 KB

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