main.c 45 KB

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