main.c 52 KB

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