main.c 46 KB

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