main.c 45 KB

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