main.c 54 KB

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