main.c 46 KB

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