main.c 46 KB

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