main.c 45 KB

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