main.c 48 KB

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