main.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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->mrp->srp->hdr.handle,
  475. "orf_token_tx", strlen("orf_token_tx"),
  476. &stats->mrp->srp->orf_token_tx, sizeof (stats->mrp->srp->orf_token_tx));
  477. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  478. "orf_token_rx", strlen("orf_token_rx"),
  479. &stats->mrp->srp->orf_token_rx, sizeof (stats->mrp->srp->orf_token_rx));
  480. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  481. "memb_merge_detect_tx", strlen("memb_merge_detect_tx"),
  482. &stats->mrp->srp->memb_merge_detect_tx, sizeof (stats->mrp->srp->memb_merge_detect_tx));
  483. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  484. "memb_merge_detect_rx", strlen("memb_merge_detect_rx"),
  485. &stats->mrp->srp->memb_merge_detect_rx, sizeof (stats->mrp->srp->memb_merge_detect_rx));
  486. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  487. "memb_join_tx", strlen("memb_join_tx"),
  488. &stats->mrp->srp->memb_join_tx, sizeof (stats->mrp->srp->memb_join_tx));
  489. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  490. "memb_join_rx", strlen("memb_join_rx"),
  491. &stats->mrp->srp->memb_join_rx, sizeof (stats->mrp->srp->memb_join_rx));
  492. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  493. "mcast_tx", strlen("mcast_tx"),
  494. &stats->mrp->srp->mcast_tx, sizeof (stats->mrp->srp->mcast_tx));
  495. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  496. "mcast_retx", strlen("mcast_retx"),
  497. &stats->mrp->srp->mcast_retx, sizeof (stats->mrp->srp->mcast_retx));
  498. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  499. "mcast_rx", strlen("mcast_rx"),
  500. &stats->mrp->srp->mcast_rx, sizeof (stats->mrp->srp->mcast_rx));
  501. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  502. "memb_commit_token_tx", strlen("memb_commit_token_tx"),
  503. &stats->mrp->srp->memb_commit_token_tx, sizeof (stats->mrp->srp->memb_commit_token_tx));
  504. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  505. "memb_commit_token_rx", strlen("memb_commit_token_rx"),
  506. &stats->mrp->srp->memb_commit_token_rx, sizeof (stats->mrp->srp->memb_commit_token_rx));
  507. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  508. "token_hold_cancel_tx", strlen("token_hold_cancel_tx"),
  509. &stats->mrp->srp->token_hold_cancel_tx, sizeof (stats->mrp->srp->token_hold_cancel_tx));
  510. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  511. "token_hold_cancel_rx", strlen("token_hold_cancel_rx"),
  512. &stats->mrp->srp->token_hold_cancel_rx, sizeof (stats->mrp->srp->token_hold_cancel_rx));
  513. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  514. "operational_entered", strlen("operational_entered"),
  515. &stats->mrp->srp->operational_entered, sizeof (stats->mrp->srp->operational_entered));
  516. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  517. "operational_token_lost", strlen("operational_token_lost"),
  518. &stats->mrp->srp->operational_token_lost, sizeof (stats->mrp->srp->operational_token_lost));
  519. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  520. "gather_entered", strlen("gather_entered"),
  521. &stats->mrp->srp->gather_entered, sizeof (stats->mrp->srp->gather_entered));
  522. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  523. "gather_token_lost", strlen("gather_token_lost"),
  524. &stats->mrp->srp->gather_token_lost, sizeof (stats->mrp->srp->gather_token_lost));
  525. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  526. "commit_entered", strlen("commit_entered"),
  527. &stats->mrp->srp->commit_entered, sizeof (stats->mrp->srp->commit_entered));
  528. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  529. "commit_token_lost", strlen("commit_token_lost"),
  530. &stats->mrp->srp->commit_token_lost, sizeof (stats->mrp->srp->commit_token_lost));
  531. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  532. "recovery_entered", strlen("recovery_entered"),
  533. &stats->mrp->srp->recovery_entered, sizeof (stats->mrp->srp->recovery_entered));
  534. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  535. "recovery_token_lost", strlen("recovery_token_lost"),
  536. &stats->mrp->srp->recovery_token_lost, sizeof (stats->mrp->srp->recovery_token_lost));
  537. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  538. "consensus_timeouts", strlen("consensus_timeouts"),
  539. &stats->mrp->srp->consensus_timeouts, sizeof (stats->mrp->srp->consensus_timeouts));
  540. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  541. "rx_msg_dropped", strlen("rx_msg_dropped"),
  542. &stats->mrp->srp->rx_msg_dropped, sizeof (stats->mrp->srp->rx_msg_dropped));
  543. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  544. "continuous_gather", strlen("continuous_gather"),
  545. &stats->mrp->srp->continuous_gather, sizeof (stats->mrp->srp->continuous_gather));
  546. firewall_enabled_or_nic_failure = (stats->mrp->srp->continuous_gather > MAX_NO_CONT_GATHER ? 1 : 0);
  547. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  548. "firewall_enabled_or_nic_failure", strlen("firewall_enabled_or_nic_failure"),
  549. &firewall_enabled_or_nic_failure, sizeof (firewall_enabled_or_nic_failure));
  550. total_mtt_rx_token = 0;
  551. total_token_holdtime = 0;
  552. total_backlog_calc = 0;
  553. token_count = 0;
  554. t = stats->mrp->srp->latest_token;
  555. while (1) {
  556. if (t == 0)
  557. prev = TOTEM_TOKEN_STATS_MAX - 1;
  558. else
  559. prev = t - 1;
  560. if (prev == stats->mrp->srp->earliest_token)
  561. break;
  562. /* if tx == 0, then dropped token (not ours) */
  563. if (stats->mrp->srp->token[t].tx != 0 ||
  564. (stats->mrp->srp->token[t].rx - stats->mrp->srp->token[prev].rx) > 0 ) {
  565. total_mtt_rx_token += (stats->mrp->srp->token[t].rx - stats->mrp->srp->token[prev].rx);
  566. total_token_holdtime += (stats->mrp->srp->token[t].tx - stats->mrp->srp->token[t].rx);
  567. total_backlog_calc += stats->mrp->srp->token[t].backlog_calc;
  568. token_count++;
  569. }
  570. t = prev;
  571. }
  572. if (token_count) {
  573. mtt_rx_token = (total_mtt_rx_token / token_count);
  574. avg_backlog_calc = (total_backlog_calc / token_count);
  575. avg_token_holdtime = (total_token_holdtime / token_count);
  576. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  577. "mtt_rx_token", strlen("mtt_rx_token"),
  578. &mtt_rx_token, sizeof (mtt_rx_token));
  579. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  580. "avg_token_workload", strlen("avg_token_workload"),
  581. &avg_token_holdtime, sizeof (avg_token_holdtime));
  582. objdb->object_key_replace (stats->mrp->srp->hdr.handle,
  583. "avg_backlog_calc", strlen("avg_backlog_calc"),
  584. &avg_backlog_calc, sizeof (avg_backlog_calc));
  585. }
  586. api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
  587. corosync_totem_stats_updater,
  588. &corosync_stats_timer_handle);
  589. }
  590. static void corosync_totem_stats_init (void)
  591. {
  592. totempg_stats_t * stats;
  593. hdb_handle_t object_find_handle;
  594. hdb_handle_t object_runtime_handle;
  595. hdb_handle_t object_totem_handle;
  596. uint32_t zero_32 = 0;
  597. uint64_t zero_64 = 0;
  598. stats = api->totem_get_stats();
  599. objdb->object_find_create (
  600. OBJECT_PARENT_HANDLE,
  601. "runtime",
  602. strlen ("runtime"),
  603. &object_find_handle);
  604. if (objdb->object_find_next (object_find_handle,
  605. &object_runtime_handle) == 0) {
  606. objdb->object_create (object_runtime_handle,
  607. &object_totem_handle,
  608. "totem", strlen ("totem"));
  609. objdb->object_create (object_totem_handle,
  610. &stats->hdr.handle,
  611. "pg", strlen ("pg"));
  612. objdb->object_create (stats->hdr.handle,
  613. &stats->mrp->hdr.handle,
  614. "mrp", strlen ("mrp"));
  615. objdb->object_create (stats->mrp->hdr.handle,
  616. &stats->mrp->srp->hdr.handle,
  617. "srp", strlen ("srp"));
  618. /* Members object */
  619. objdb->object_create (stats->mrp->srp->hdr.handle,
  620. &object_memb_handle,
  621. "members", strlen ("members"));
  622. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  623. "orf_token_tx", &stats->mrp->srp->orf_token_tx,
  624. sizeof (stats->mrp->srp->orf_token_tx), OBJDB_VALUETYPE_UINT64);
  625. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  626. "orf_token_rx", &stats->mrp->srp->orf_token_rx,
  627. sizeof (stats->mrp->srp->orf_token_rx), OBJDB_VALUETYPE_UINT64);
  628. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  629. "memb_merge_detect_tx", &stats->mrp->srp->memb_merge_detect_tx,
  630. sizeof (stats->mrp->srp->memb_merge_detect_tx), OBJDB_VALUETYPE_UINT64);
  631. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  632. "memb_merge_detect_rx", &stats->mrp->srp->memb_merge_detect_rx,
  633. sizeof (stats->mrp->srp->memb_merge_detect_rx), OBJDB_VALUETYPE_UINT64);
  634. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  635. "memb_join_tx", &stats->mrp->srp->memb_join_tx,
  636. sizeof (stats->mrp->srp->memb_join_tx), OBJDB_VALUETYPE_UINT64);
  637. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  638. "memb_join_rx", &stats->mrp->srp->memb_join_rx,
  639. sizeof (stats->mrp->srp->memb_join_rx), OBJDB_VALUETYPE_UINT64);
  640. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  641. "mcast_tx", &stats->mrp->srp->mcast_tx,
  642. sizeof (stats->mrp->srp->mcast_tx), OBJDB_VALUETYPE_UINT64);
  643. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  644. "mcast_retx", &stats->mrp->srp->mcast_retx,
  645. sizeof (stats->mrp->srp->mcast_retx), OBJDB_VALUETYPE_UINT64);
  646. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  647. "mcast_rx", &stats->mrp->srp->mcast_rx,
  648. sizeof (stats->mrp->srp->mcast_rx), OBJDB_VALUETYPE_UINT64);
  649. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  650. "memb_commit_token_tx", &stats->mrp->srp->memb_commit_token_tx,
  651. sizeof (stats->mrp->srp->memb_commit_token_tx), OBJDB_VALUETYPE_UINT64);
  652. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  653. "memb_commit_token_rx", &stats->mrp->srp->memb_commit_token_rx,
  654. sizeof (stats->mrp->srp->memb_commit_token_rx), OBJDB_VALUETYPE_UINT64);
  655. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  656. "token_hold_cancel_tx", &stats->mrp->srp->token_hold_cancel_tx,
  657. sizeof (stats->mrp->srp->token_hold_cancel_tx), OBJDB_VALUETYPE_UINT64);
  658. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  659. "token_hold_cancel_rx", &stats->mrp->srp->token_hold_cancel_rx,
  660. sizeof (stats->mrp->srp->token_hold_cancel_rx), OBJDB_VALUETYPE_UINT64);
  661. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  662. "operational_entered", &stats->mrp->srp->operational_entered,
  663. sizeof (stats->mrp->srp->operational_entered), OBJDB_VALUETYPE_UINT64);
  664. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  665. "operational_token_lost", &stats->mrp->srp->operational_token_lost,
  666. sizeof (stats->mrp->srp->operational_token_lost), OBJDB_VALUETYPE_UINT64);
  667. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  668. "gather_entered", &stats->mrp->srp->gather_entered,
  669. sizeof (stats->mrp->srp->gather_entered), OBJDB_VALUETYPE_UINT64);
  670. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  671. "gather_token_lost", &stats->mrp->srp->gather_token_lost,
  672. sizeof (stats->mrp->srp->gather_token_lost), OBJDB_VALUETYPE_UINT64);
  673. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  674. "commit_entered", &stats->mrp->srp->commit_entered,
  675. sizeof (stats->mrp->srp->commit_entered), OBJDB_VALUETYPE_UINT64);
  676. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  677. "commit_token_lost", &stats->mrp->srp->commit_token_lost,
  678. sizeof (stats->mrp->srp->commit_token_lost), OBJDB_VALUETYPE_UINT64);
  679. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  680. "recovery_entered", &stats->mrp->srp->recovery_entered,
  681. sizeof (stats->mrp->srp->recovery_entered), OBJDB_VALUETYPE_UINT64);
  682. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  683. "recovery_token_lost", &stats->mrp->srp->recovery_token_lost,
  684. sizeof (stats->mrp->srp->recovery_token_lost), OBJDB_VALUETYPE_UINT64);
  685. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  686. "consensus_timeouts", &stats->mrp->srp->consensus_timeouts,
  687. sizeof (stats->mrp->srp->consensus_timeouts), OBJDB_VALUETYPE_UINT64);
  688. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  689. "mtt_rx_token", &zero_32,
  690. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  691. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  692. "avg_token_workload", &zero_32,
  693. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  694. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  695. "avg_backlog_calc", &zero_32,
  696. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  697. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  698. "rx_msg_dropped", &zero_64,
  699. sizeof (zero_64), OBJDB_VALUETYPE_UINT64);
  700. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  701. "continuous_gather", &zero_32,
  702. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  703. objdb->object_key_create_typed (stats->mrp->srp->hdr.handle,
  704. "firewall_enabled_or_nic_failure", &zero_32,
  705. sizeof (zero_32), OBJDB_VALUETYPE_UINT32);
  706. }
  707. /* start stats timer */
  708. api->timer_add_duration (1500 * MILLI_2_NANO_SECONDS, NULL,
  709. corosync_totem_stats_updater,
  710. &corosync_stats_timer_handle);
  711. }
  712. static void deliver_fn (
  713. unsigned int nodeid,
  714. const void *msg,
  715. unsigned int msg_len,
  716. int endian_conversion_required)
  717. {
  718. const coroipc_request_header_t *header;
  719. int service;
  720. int fn_id;
  721. unsigned int id;
  722. unsigned int size;
  723. unsigned int key_incr_dummy;
  724. header = msg;
  725. if (endian_conversion_required) {
  726. id = swab32 (header->id);
  727. size = swab32 (header->size);
  728. } else {
  729. id = header->id;
  730. size = header->size;
  731. }
  732. /*
  733. * Call the proper executive handler
  734. */
  735. service = id >> 16;
  736. fn_id = id & 0xffff;
  737. serialize_lock();
  738. if (ais_service[service] == NULL && service == EVT_SERVICE) {
  739. evil_deliver_fn (nodeid, service, fn_id, msg,
  740. endian_conversion_required);
  741. }
  742. if (!ais_service[service]) {
  743. serialize_unlock();
  744. return;
  745. }
  746. if (fn_id >= ais_service[service]->exec_engine_count) {
  747. log_printf(LOGSYS_LEVEL_WARNING, "discarded unknown message %d for service %d (max id %d)",
  748. fn_id, service, ais_service[service]->exec_engine_count);
  749. serialize_unlock();
  750. return;
  751. }
  752. objdb->object_key_increment (service_stats_handle[service][fn_id],
  753. "rx", strlen("rx"),
  754. &key_incr_dummy);
  755. if (endian_conversion_required) {
  756. assert(ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  757. ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  758. ((void *)msg);
  759. }
  760. ais_service[service]->exec_engine[fn_id].exec_handler_fn
  761. (msg, nodeid);
  762. serialize_unlock();
  763. }
  764. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  765. {
  766. *modules = config_modules;
  767. *num = num_config_modules;
  768. }
  769. int main_mcast (
  770. const struct iovec *iovec,
  771. unsigned int iov_len,
  772. unsigned int guarantee)
  773. {
  774. const coroipc_request_header_t *req = iovec->iov_base;
  775. int service;
  776. int fn_id;
  777. unsigned int key_incr_dummy;
  778. service = req->id >> 16;
  779. fn_id = req->id & 0xffff;
  780. if (ais_service[service]) {
  781. objdb->object_key_increment (service_stats_handle[service][fn_id],
  782. "tx", strlen("tx"), &key_incr_dummy);
  783. }
  784. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  785. }
  786. int message_source_is_local (const mar_message_source_t *source)
  787. {
  788. int ret = 0;
  789. assert (source != NULL);
  790. if (source->nodeid == totempg_my_nodeid_get ()) {
  791. ret = 1;
  792. }
  793. return ret;
  794. }
  795. void message_source_set (
  796. mar_message_source_t *source,
  797. void *conn)
  798. {
  799. assert ((source != NULL) && (conn != NULL));
  800. memset (source, 0, sizeof (mar_message_source_t));
  801. source->nodeid = totempg_my_nodeid_get ();
  802. source->conn = conn;
  803. }
  804. /*
  805. * Provides the glue from corosync to the IPC Service
  806. */
  807. static int corosync_private_data_size_get (unsigned int service)
  808. {
  809. return (ais_service[service]->private_data_size);
  810. }
  811. static coroipcs_init_fn_lvalue corosync_init_fn_get (unsigned int service)
  812. {
  813. return (ais_service[service]->lib_init_fn);
  814. }
  815. static coroipcs_exit_fn_lvalue corosync_exit_fn_get (unsigned int service)
  816. {
  817. return (ais_service[service]->lib_exit_fn);
  818. }
  819. static coroipcs_handler_fn_lvalue corosync_handler_fn_get (unsigned int service, unsigned int id)
  820. {
  821. return (ais_service[service]->lib_engine[id].lib_handler_fn);
  822. }
  823. static int corosync_security_valid (int euid, int egid)
  824. {
  825. struct list_head *iter;
  826. if (corosync_not_enough_fds_left) {
  827. errno = EMFILE;
  828. return 0;
  829. }
  830. if (euid == 0 || egid == 0) {
  831. return (1);
  832. }
  833. for (iter = uidgid_list_head.next; iter != &uidgid_list_head;
  834. iter = iter->next) {
  835. struct uidgid_item *ugi = list_entry (iter, struct uidgid_item,
  836. list);
  837. if (euid == ugi->uid || egid == ugi->gid)
  838. return (1);
  839. }
  840. errno = EACCES;
  841. return (0);
  842. }
  843. static int corosync_service_available (unsigned int service)
  844. {
  845. return (ais_service[service] != NULL && !ais_service_exiting[service]);
  846. }
  847. struct sending_allowed_private_data_struct {
  848. int reserved_msgs;
  849. };
  850. static int corosync_sending_allowed (
  851. unsigned int service,
  852. unsigned int id,
  853. const void *msg,
  854. void *sending_allowed_private_data)
  855. {
  856. struct sending_allowed_private_data_struct *pd =
  857. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  858. struct iovec reserve_iovec;
  859. coroipc_request_header_t *header = (coroipc_request_header_t *)msg;
  860. int sending_allowed;
  861. reserve_iovec.iov_base = (char *)header;
  862. reserve_iovec.iov_len = header->size;
  863. pd->reserved_msgs = totempg_groups_joined_reserve (
  864. corosync_group_handle,
  865. &reserve_iovec, 1);
  866. if (pd->reserved_msgs == -1) {
  867. return (-1);
  868. }
  869. sending_allowed =
  870. (corosync_quorum_is_quorate() == 1 ||
  871. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  872. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  873. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  874. (pd->reserved_msgs) &&
  875. (sync_in_process == 0)));
  876. return (sending_allowed);
  877. }
  878. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  879. {
  880. struct sending_allowed_private_data_struct *pd =
  881. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  882. if (pd->reserved_msgs == -1) {
  883. return;
  884. }
  885. totempg_groups_joined_release (pd->reserved_msgs);
  886. }
  887. static int ipc_subsys_id = -1;
  888. static void ipc_fatal_error(const char *error_msg) __attribute__((noreturn));
  889. static void ipc_fatal_error(const char *error_msg) {
  890. _logsys_log_printf (
  891. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  892. ipc_subsys_id,
  893. LOGSYS_RECID_LOG),
  894. __FUNCTION__, __FILE__, __LINE__,
  895. "%s", error_msg);
  896. exit(EXIT_FAILURE);
  897. }
  898. static int corosync_poll_handler_accept (
  899. hdb_handle_t handle,
  900. int fd,
  901. int revent,
  902. void *context)
  903. {
  904. return (coroipcs_handler_accept (fd, revent, context));
  905. }
  906. static int corosync_poll_handler_dispatch (
  907. hdb_handle_t handle,
  908. int fd,
  909. int revent,
  910. void *context)
  911. {
  912. return (coroipcs_handler_dispatch (fd, revent, context));
  913. }
  914. static void corosync_poll_accept_add (
  915. int fd)
  916. {
  917. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  918. corosync_poll_handler_accept);
  919. }
  920. static void corosync_poll_dispatch_add (
  921. int fd,
  922. void *context)
  923. {
  924. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  925. corosync_poll_handler_dispatch);
  926. }
  927. static void corosync_poll_dispatch_modify (
  928. int fd,
  929. int events)
  930. {
  931. poll_dispatch_modify (corosync_poll_handle, fd, events,
  932. corosync_poll_handler_dispatch);
  933. }
  934. static void corosync_poll_dispatch_destroy (
  935. int fd,
  936. void *context)
  937. {
  938. poll_dispatch_delete (corosync_poll_handle, fd);
  939. }
  940. static hdb_handle_t corosync_stats_create_connection (const char* name,
  941. const pid_t pid, const int fd)
  942. {
  943. uint32_t zero_32 = 0;
  944. uint64_t zero_64 = 0;
  945. unsigned int key_incr_dummy;
  946. hdb_handle_t object_handle;
  947. objdb->object_key_increment (object_connection_handle,
  948. "active", strlen("active"),
  949. &key_incr_dummy);
  950. objdb->object_create (object_connection_handle,
  951. &object_handle,
  952. name,
  953. strlen (name));
  954. objdb->object_key_create_typed (object_handle,
  955. "service_id",
  956. &zero_32, sizeof (zero_32),
  957. OBJDB_VALUETYPE_UINT32);
  958. objdb->object_key_create_typed (object_handle,
  959. "client_pid",
  960. &pid, sizeof (pid),
  961. OBJDB_VALUETYPE_INT32);
  962. objdb->object_key_create_typed (object_handle,
  963. "responses",
  964. &zero_64, sizeof (zero_64),
  965. OBJDB_VALUETYPE_UINT64);
  966. objdb->object_key_create_typed (object_handle,
  967. "dispatched",
  968. &zero_64, sizeof (zero_64),
  969. OBJDB_VALUETYPE_UINT64);
  970. objdb->object_key_create_typed (object_handle,
  971. "requests",
  972. &zero_64, sizeof (zero_64),
  973. OBJDB_VALUETYPE_INT64);
  974. objdb->object_key_create_typed (object_handle,
  975. "sem_retry_count",
  976. &zero_64, sizeof (zero_64),
  977. OBJDB_VALUETYPE_UINT64);
  978. objdb->object_key_create_typed (object_handle,
  979. "send_retry_count",
  980. &zero_64, sizeof (zero_64),
  981. OBJDB_VALUETYPE_UINT64);
  982. objdb->object_key_create_typed (object_handle,
  983. "recv_retry_count",
  984. &zero_64, sizeof (zero_64),
  985. OBJDB_VALUETYPE_UINT64);
  986. objdb->object_key_create_typed (object_handle,
  987. "flow_control",
  988. &zero_32, sizeof (zero_32),
  989. OBJDB_VALUETYPE_UINT32);
  990. objdb->object_key_create_typed (object_handle,
  991. "flow_control_count",
  992. &zero_64, sizeof (zero_64),
  993. OBJDB_VALUETYPE_UINT64);
  994. objdb->object_key_create_typed (object_handle,
  995. "queue_size",
  996. &zero_64, sizeof (zero_64),
  997. OBJDB_VALUETYPE_UINT64);
  998. return object_handle;
  999. }
  1000. static void corosync_stats_destroy_connection (hdb_handle_t handle)
  1001. {
  1002. unsigned int key_incr_dummy;
  1003. objdb->object_destroy (handle);
  1004. objdb->object_key_increment (object_connection_handle,
  1005. "closed", strlen("closed"),
  1006. &key_incr_dummy);
  1007. objdb->object_key_decrement (object_connection_handle,
  1008. "active", strlen("active"),
  1009. &key_incr_dummy);
  1010. }
  1011. static void corosync_stats_update_value (hdb_handle_t handle,
  1012. const char *name, const void *value,
  1013. size_t value_len)
  1014. {
  1015. objdb->object_key_replace (handle,
  1016. name, strlen(name),
  1017. value, value_len);
  1018. }
  1019. static void corosync_stats_increment_value (hdb_handle_t handle,
  1020. const char* name)
  1021. {
  1022. unsigned int key_incr_dummy;
  1023. objdb->object_key_increment (handle,
  1024. name, strlen(name),
  1025. &key_incr_dummy);
  1026. }
  1027. static void corosync_stats_decrement_value (hdb_handle_t handle,
  1028. const char* name)
  1029. {
  1030. unsigned int key_incr_dummy;
  1031. objdb->object_key_decrement (handle,
  1032. name, strlen(name),
  1033. &key_incr_dummy);
  1034. }
  1035. static struct coroipcs_init_state_v2 ipc_init_state_v2 = {
  1036. .socket_name = COROSYNC_SOCKET_NAME,
  1037. .sched_policy = SCHED_OTHER,
  1038. .sched_param = &global_sched_param,
  1039. .malloc = malloc,
  1040. .free = free,
  1041. .log_printf = _logsys_log_printf,
  1042. .fatal_error = ipc_fatal_error,
  1043. .security_valid = corosync_security_valid,
  1044. .service_available = corosync_service_available,
  1045. .private_data_size_get = corosync_private_data_size_get,
  1046. .serialize_lock = serialize_lock,
  1047. .serialize_unlock = serialize_unlock,
  1048. .sending_allowed = corosync_sending_allowed,
  1049. .sending_allowed_release = corosync_sending_allowed_release,
  1050. .poll_accept_add = corosync_poll_accept_add,
  1051. .poll_dispatch_add = corosync_poll_dispatch_add,
  1052. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  1053. .poll_dispatch_destroy = corosync_poll_dispatch_destroy,
  1054. .init_fn_get = corosync_init_fn_get,
  1055. .exit_fn_get = corosync_exit_fn_get,
  1056. .handler_fn_get = corosync_handler_fn_get,
  1057. .stats_create_connection = corosync_stats_create_connection,
  1058. .stats_destroy_connection = corosync_stats_destroy_connection,
  1059. .stats_update_value = corosync_stats_update_value,
  1060. .stats_increment_value = corosync_stats_increment_value,
  1061. .stats_decrement_value = corosync_stats_decrement_value,
  1062. };
  1063. static void corosync_setscheduler (void)
  1064. {
  1065. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  1066. int res;
  1067. sched_priority = sched_get_priority_max (SCHED_RR);
  1068. if (sched_priority != -1) {
  1069. global_sched_param.sched_priority = sched_priority;
  1070. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  1071. if (res == -1) {
  1072. LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING,
  1073. "Could not set SCHED_RR at priority %d",
  1074. global_sched_param.sched_priority);
  1075. global_sched_param.sched_priority = 0;
  1076. logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  1077. } else {
  1078. /*
  1079. * Turn on SCHED_RR in ipc system
  1080. */
  1081. ipc_init_state_v2.sched_policy = SCHED_RR;
  1082. /*
  1083. * Turn on SCHED_RR in logsys system
  1084. */
  1085. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  1086. if (res == -1) {
  1087. log_printf (LOGSYS_LEVEL_ERROR,
  1088. "Could not set logsys thread priority."
  1089. " Can't continue because of priority inversions.");
  1090. corosync_exit_error (AIS_DONE_LOGSETUP);
  1091. }
  1092. }
  1093. } else {
  1094. LOGSYS_PERROR (errno, LOGSYS_LEVEL_WARNING,
  1095. "Could not get maximum scheduler priority");
  1096. sched_priority = 0;
  1097. }
  1098. #else
  1099. log_printf(LOGSYS_LEVEL_WARNING,
  1100. "The Platform is missing process priority setting features. Leaving at default.");
  1101. #endif
  1102. }
  1103. static void fplay_key_change_notify_fn (
  1104. object_change_type_t change_type,
  1105. hdb_handle_t parent_object_handle,
  1106. hdb_handle_t object_handle,
  1107. const void *object_name_pt, size_t object_name_len,
  1108. const void *key_name_pt, size_t key_len,
  1109. const void *key_value_pt, size_t key_value_len,
  1110. void *priv_data_pt)
  1111. {
  1112. if (key_len == strlen ("dump_flight_data") &&
  1113. memcmp ("dump_flight_data", key_name_pt, key_len) == 0) {
  1114. logsys_log_rec_store (LOCALSTATEDIR "/lib/corosync/fdata");
  1115. }
  1116. if (key_len == strlen ("dump_state") &&
  1117. memcmp ("dump_state", key_name_pt, key_len) == 0) {
  1118. corosync_state_dump ();
  1119. }
  1120. }
  1121. static void corosync_fplay_control_init (void)
  1122. {
  1123. hdb_handle_t object_find_handle;
  1124. hdb_handle_t object_runtime_handle;
  1125. hdb_handle_t object_blackbox_handle;
  1126. objdb->object_find_create (OBJECT_PARENT_HANDLE,
  1127. "runtime", strlen ("runtime"),
  1128. &object_find_handle);
  1129. if (objdb->object_find_next (object_find_handle,
  1130. &object_runtime_handle) != 0) {
  1131. return;
  1132. }
  1133. objdb->object_create (object_runtime_handle,
  1134. &object_blackbox_handle,
  1135. "blackbox", strlen ("blackbox"));
  1136. objdb->object_key_create_typed (object_blackbox_handle,
  1137. "dump_flight_data", "no", strlen("no"),
  1138. OBJDB_VALUETYPE_STRING);
  1139. objdb->object_key_create_typed (object_blackbox_handle,
  1140. "dump_state", "no", strlen("no"),
  1141. OBJDB_VALUETYPE_STRING);
  1142. objdb->object_track_start (object_blackbox_handle,
  1143. OBJECT_TRACK_DEPTH_RECURSIVE,
  1144. fplay_key_change_notify_fn,
  1145. NULL, NULL, NULL, NULL);
  1146. }
  1147. static void corosync_stats_init (void)
  1148. {
  1149. hdb_handle_t object_find_handle;
  1150. hdb_handle_t object_runtime_handle;
  1151. uint64_t zero_64 = 0;
  1152. objdb->object_find_create (OBJECT_PARENT_HANDLE,
  1153. "runtime", strlen ("runtime"),
  1154. &object_find_handle);
  1155. if (objdb->object_find_next (object_find_handle,
  1156. &object_runtime_handle) != 0) {
  1157. return;
  1158. }
  1159. /* Connection objects */
  1160. objdb->object_create (object_runtime_handle,
  1161. &object_connection_handle,
  1162. "connections", strlen ("connections"));
  1163. objdb->object_key_create_typed (object_connection_handle,
  1164. "active", &zero_64, sizeof (zero_64),
  1165. OBJDB_VALUETYPE_UINT64);
  1166. objdb->object_key_create_typed (object_connection_handle,
  1167. "closed", &zero_64, sizeof (zero_64),
  1168. OBJDB_VALUETYPE_UINT64);
  1169. }
  1170. static void main_low_fds_event(int32_t not_enough, int32_t fds_available)
  1171. {
  1172. corosync_not_enough_fds_left = not_enough;
  1173. if (not_enough) {
  1174. log_printf(LOGSYS_LEVEL_WARNING, "refusing new connections (fds_available:%d)\n",
  1175. fds_available);
  1176. } else {
  1177. log_printf(LOGSYS_LEVEL_NOTICE, "allowing new connections (fds_available:%d)\n",
  1178. fds_available);
  1179. }
  1180. }
  1181. static void main_service_ready (void)
  1182. {
  1183. int res;
  1184. /*
  1185. * This must occur after totempg is initialized because "this_ip" must be set
  1186. */
  1187. res = corosync_service_defaults_link_and_init (api);
  1188. if (res == -1) {
  1189. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  1190. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1191. }
  1192. evil_init (api);
  1193. corosync_stats_init ();
  1194. corosync_totem_stats_init ();
  1195. corosync_fplay_control_init ();
  1196. if (minimum_sync_mode == CS_SYNC_V2) {
  1197. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  1198. sync_v2_init (
  1199. corosync_sync_v2_callbacks_retrieve,
  1200. corosync_sync_completed);
  1201. } else
  1202. if (minimum_sync_mode == CS_SYNC_V1) {
  1203. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  1204. sync_register (
  1205. corosync_sync_callbacks_retrieve,
  1206. sync_v2_memb_list_determine,
  1207. sync_v2_memb_list_abort,
  1208. sync_v2_start);
  1209. sync_v2_init (
  1210. corosync_sync_v2_callbacks_retrieve,
  1211. corosync_sync_completed);
  1212. }
  1213. }
  1214. static enum e_ais_done corosync_flock (const char *lockfile, pid_t pid)
  1215. {
  1216. struct flock lock;
  1217. enum e_ais_done err;
  1218. char pid_s[17];
  1219. int fd_flag;
  1220. int lf;
  1221. err = AIS_DONE_EXIT;
  1222. lf = open (lockfile, O_WRONLY | O_CREAT, 0640);
  1223. if (lf == -1) {
  1224. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create lock file.\n");
  1225. return (AIS_DONE_AQUIRE_LOCK);
  1226. }
  1227. retry_fcntl:
  1228. lock.l_type = F_WRLCK;
  1229. lock.l_start = 0;
  1230. lock.l_whence = SEEK_SET;
  1231. lock.l_len = 0;
  1232. if (fcntl (lf, F_SETLK, &lock) == -1) {
  1233. switch (errno) {
  1234. case EINTR:
  1235. goto retry_fcntl;
  1236. break;
  1237. case EAGAIN:
  1238. case EACCES:
  1239. log_printf (LOGSYS_LEVEL_ERROR, "Another Corosync instance is already running.\n");
  1240. err = AIS_DONE_ALREADY_RUNNING;
  1241. goto error_close;
  1242. break;
  1243. default:
  1244. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't aquire lock. Error was %s\n",
  1245. strerror(errno));
  1246. err = AIS_DONE_AQUIRE_LOCK;
  1247. goto error_close;
  1248. break;
  1249. }
  1250. }
  1251. if (ftruncate (lf, 0) == -1) {
  1252. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't truncate lock file. Error was %s\n",
  1253. strerror (errno));
  1254. err = AIS_DONE_AQUIRE_LOCK;
  1255. goto error_close_unlink;
  1256. }
  1257. memset (pid_s, 0, sizeof (pid_s));
  1258. snprintf (pid_s, sizeof (pid_s) - 1, "%u\n", pid);
  1259. retry_write:
  1260. if (write (lf, pid_s, strlen (pid_s)) != strlen (pid_s)) {
  1261. if (errno == EINTR) {
  1262. goto retry_write;
  1263. } else {
  1264. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't write pid to lock file. "
  1265. "Error was %s\n", strerror (errno));
  1266. err = AIS_DONE_AQUIRE_LOCK;
  1267. goto error_close_unlink;
  1268. }
  1269. }
  1270. if ((fd_flag = fcntl (lf, F_GETFD, 0)) == -1) {
  1271. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't get close-on-exec flag from lock file. "
  1272. "Error was %s\n", strerror (errno));
  1273. err = AIS_DONE_AQUIRE_LOCK;
  1274. goto error_close_unlink;
  1275. }
  1276. fd_flag |= FD_CLOEXEC;
  1277. if (fcntl (lf, F_SETFD, fd_flag) == -1) {
  1278. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't set close-on-exec flag to lock file. "
  1279. "Error was %s\n", strerror (errno));
  1280. err = AIS_DONE_AQUIRE_LOCK;
  1281. goto error_close_unlink;
  1282. }
  1283. return (err);
  1284. error_close_unlink:
  1285. unlink (lockfile);
  1286. error_close:
  1287. close (lf);
  1288. return (err);
  1289. }
  1290. int main (int argc, char **argv, char **envp)
  1291. {
  1292. const char *error_string;
  1293. struct totem_config totem_config;
  1294. hdb_handle_t objdb_handle;
  1295. hdb_handle_t config_handle;
  1296. unsigned int config_version = 0;
  1297. void *objdb_p;
  1298. struct config_iface_ver0 *config;
  1299. void *config_p;
  1300. const char *config_iface_init;
  1301. char *config_iface;
  1302. char *iface;
  1303. char *strtok_save_pt;
  1304. int res, ch;
  1305. int background, setprio;
  1306. struct stat stat_out;
  1307. char corosync_lib_dir[PATH_MAX];
  1308. hdb_handle_t object_runtime_handle;
  1309. enum e_ais_done flock_err;
  1310. /* default configuration
  1311. */
  1312. background = 1;
  1313. setprio = 1;
  1314. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  1315. switch (ch) {
  1316. case 'f':
  1317. background = 0;
  1318. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  1319. break;
  1320. case 'p':
  1321. setprio = 0;
  1322. break;
  1323. case 'v':
  1324. printf ("Corosync Cluster Engine, version '%s'\n", VERSION);
  1325. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  1326. return EXIT_SUCCESS;
  1327. break;
  1328. default:
  1329. fprintf(stderr, \
  1330. "usage:\n"\
  1331. " -f : Start application in foreground.\n"\
  1332. " -p : Do not set process priority. \n"\
  1333. " -v : Display version and SVN revision of Corosync and exit.\n");
  1334. return EXIT_FAILURE;
  1335. }
  1336. }
  1337. /*
  1338. * Set round robin realtime scheduling with priority 99
  1339. * Lock all memory to avoid page faults which may interrupt
  1340. * application healthchecking
  1341. */
  1342. if (setprio) {
  1343. corosync_setscheduler ();
  1344. }
  1345. corosync_mlockall ();
  1346. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  1347. log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "\n");
  1348. (void)signal (SIGINT, sigintr_handler);
  1349. (void)signal (SIGUSR2, sigusr2_handler);
  1350. (void)signal (SIGSEGV, sigsegv_handler);
  1351. (void)signal (SIGABRT, sigabrt_handler);
  1352. (void)signal (SIGQUIT, sigquit_handler);
  1353. (void)signal (SIGTERM, sigterm_handler);
  1354. #if MSG_NOSIGNAL != 0
  1355. (void)signal (SIGPIPE, SIG_IGN);
  1356. #endif
  1357. /*
  1358. * Load the object database interface
  1359. */
  1360. res = lcr_ifact_reference (
  1361. &objdb_handle,
  1362. "objdb",
  1363. 0,
  1364. &objdb_p,
  1365. 0);
  1366. if (res == -1) {
  1367. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  1368. corosync_exit_error (AIS_DONE_OBJDB);
  1369. }
  1370. objdb = (struct objdb_iface_ver0 *)objdb_p;
  1371. objdb->objdb_init ();
  1372. /*
  1373. * Initialize the corosync_api_v1 definition
  1374. */
  1375. apidef_init (objdb);
  1376. api = apidef_get ();
  1377. num_config_modules = 0;
  1378. /*
  1379. * Bootstrap in the default configuration parser or use
  1380. * the corosync default built in parser if the configuration parser
  1381. * isn't overridden
  1382. */
  1383. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  1384. if (!config_iface_init) {
  1385. config_iface_init = "corosync_parser";
  1386. }
  1387. /* Make a copy so we can deface it with strtok */
  1388. if ((config_iface = strdup(config_iface_init)) == NULL) {
  1389. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  1390. corosync_exit_error (AIS_DONE_OBJDB);
  1391. }
  1392. iface = strtok_r(config_iface, ":", &strtok_save_pt);
  1393. while (iface)
  1394. {
  1395. res = lcr_ifact_reference (
  1396. &config_handle,
  1397. iface,
  1398. config_version,
  1399. &config_p,
  1400. 0);
  1401. config = (struct config_iface_ver0 *)config_p;
  1402. if (res == -1) {
  1403. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  1404. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1405. }
  1406. res = config->config_readconfig(objdb, &error_string);
  1407. if (res == -1) {
  1408. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1409. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1410. }
  1411. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  1412. config_modules[num_config_modules++] = config;
  1413. iface = strtok_r(NULL, ":", &strtok_save_pt);
  1414. }
  1415. free(config_iface);
  1416. res = corosync_main_config_read (objdb, &error_string);
  1417. if (res == -1) {
  1418. /*
  1419. * if we are here, we _must_ flush the logsys queue
  1420. * and try to inform that we couldn't read the config.
  1421. * this is a desperate attempt before certain death
  1422. * and there is no guarantee that we can print to stderr
  1423. * nor that logsys is sending the messages where we expect.
  1424. */
  1425. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1426. fprintf(stderr, "%s", error_string);
  1427. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1428. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1429. }
  1430. /*
  1431. * Make sure required directory is present
  1432. */
  1433. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  1434. res = stat (corosync_lib_dir, &stat_out);
  1435. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  1436. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  1437. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  1438. }
  1439. res = totem_config_read (objdb, &totem_config, &error_string);
  1440. if (res == -1) {
  1441. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1442. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1443. }
  1444. res = totem_config_keyread (objdb, &totem_config, &error_string);
  1445. if (res == -1) {
  1446. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1447. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1448. }
  1449. res = totem_config_validate (&totem_config, &error_string);
  1450. if (res == -1) {
  1451. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1452. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1453. }
  1454. totem_config.totem_logging_configuration = totem_logging_configuration;
  1455. totem_config.totem_logging_configuration.log_subsys_id =
  1456. _logsys_subsys_create ("TOTEM");
  1457. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  1458. log_printf (LOGSYS_LEVEL_ERROR,
  1459. "Unable to initialize TOTEM logging subsystem\n");
  1460. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1461. }
  1462. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  1463. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  1464. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  1465. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  1466. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  1467. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  1468. res = corosync_main_config_compatibility_read (objdb,
  1469. &minimum_sync_mode,
  1470. &error_string);
  1471. if (res == -1) {
  1472. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1473. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1474. }
  1475. res = corosync_main_config_compatibility_read (objdb,
  1476. &minimum_sync_mode,
  1477. &error_string);
  1478. if (res == -1) {
  1479. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  1480. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  1481. }
  1482. /* create the main runtime object */
  1483. objdb->object_create (OBJECT_PARENT_HANDLE,
  1484. &object_runtime_handle,
  1485. "runtime", strlen ("runtime"));
  1486. /*
  1487. * Now we are fully initialized.
  1488. */
  1489. if (background) {
  1490. corosync_tty_detach ();
  1491. }
  1492. logsys_fork_completed();
  1493. if ((flock_err = corosync_flock (corosync_lock_file, getpid ())) != AIS_DONE_EXIT) {
  1494. corosync_exit_error (flock_err);
  1495. }
  1496. corosync_timer_init (
  1497. serialize_lock,
  1498. serialize_unlock,
  1499. sched_priority);
  1500. corosync_poll_handle = poll_create ();
  1501. poll_low_fds_event_set(corosync_poll_handle, main_low_fds_event);
  1502. /*
  1503. * Sleep for a while to let other nodes in the cluster
  1504. * understand that this node has been away (if it was
  1505. * an corosync restart).
  1506. */
  1507. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  1508. /*
  1509. * Create semaphore and start "exit" thread
  1510. */
  1511. res = sem_init (&corosync_exit_sem, 0, 0);
  1512. if (res != 0) {
  1513. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create exit thread.\n");
  1514. corosync_exit_error (AIS_DONE_FATAL_ERR);
  1515. }
  1516. res = pthread_create (&corosync_exit_thread, NULL, corosync_exit_thread_handler, NULL);
  1517. if (res != 0) {
  1518. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create exit thread.\n");
  1519. corosync_exit_error (AIS_DONE_FATAL_ERR);
  1520. }
  1521. /*
  1522. * if totempg_initialize doesn't have root priveleges, it cannot
  1523. * bind to a specific interface. This only matters if
  1524. * there is more then one interface in a system, so
  1525. * in this case, only a warning is printed
  1526. */
  1527. /*
  1528. * Join multicast group and setup delivery
  1529. * and configuration change functions
  1530. */
  1531. totempg_initialize (
  1532. corosync_poll_handle,
  1533. &totem_config);
  1534. totempg_service_ready_register (
  1535. main_service_ready);
  1536. totempg_groups_initialize (
  1537. &corosync_group_handle,
  1538. deliver_fn,
  1539. confchg_fn);
  1540. totempg_groups_join (
  1541. corosync_group_handle,
  1542. &corosync_group,
  1543. 1);
  1544. /*
  1545. * Drop root privleges to user 'ais'
  1546. * TODO: Don't really need full root capabilities;
  1547. * needed capabilities are:
  1548. * CAP_NET_RAW (bindtodevice)
  1549. * CAP_SYS_NICE (setscheduler)
  1550. * CAP_IPC_LOCK (mlockall)
  1551. */
  1552. priv_drop ();
  1553. schedwrk_init (
  1554. serialize_lock,
  1555. serialize_unlock);
  1556. ipc_subsys_id = _logsys_subsys_create ("IPC");
  1557. if (ipc_subsys_id < 0) {
  1558. log_printf (LOGSYS_LEVEL_ERROR,
  1559. "Could not initialize IPC logging subsystem\n");
  1560. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1561. }
  1562. ipc_init_state_v2.log_subsys_id = ipc_subsys_id;
  1563. coroipcs_ipc_init_v2 (&ipc_init_state_v2);
  1564. /*
  1565. * Start main processing loop
  1566. */
  1567. poll_run (corosync_poll_handle);
  1568. return EXIT_SUCCESS;
  1569. }