main.c 43 KB

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