main.c 43 KB

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