main.c 45 KB

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