4
0

main.c 52 KB

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