main.c 53 KB

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