main.c 45 KB

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