main.c 48 KB

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