main.c 53 KB

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