main.c 49 KB

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