main.c 45 KB

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