main.c 52 KB

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