main.c 55 KB

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