main.c 45 KB

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