main.c 52 KB

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