main.c 43 KB

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