main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <signal.h>
  54. #include <sched.h>
  55. #include <time.h>
  56. #include <corosync/swab.h>
  57. #include <corosync/corotypes.h>
  58. #include <corosync/list.h>
  59. #include <corosync/queue.h>
  60. #include <corosync/lcr/lcr_ifact.h>
  61. #include <corosync/totem/coropoll.h>
  62. #include <corosync/totem/totempg.h>
  63. #include <corosync/engine/objdb.h>
  64. #include <corosync/engine/config.h>
  65. #include <corosync/engine/logsys.h>
  66. #include "quorum.h"
  67. #include "totemsrp.h"
  68. #include "mempool.h"
  69. #include "mainconfig.h"
  70. #include "totemconfig.h"
  71. #include "main.h"
  72. #include "sync.h"
  73. #include "tlist.h"
  74. #include "coroipcs.h"
  75. #include "timer.h"
  76. #include "util.h"
  77. #include "apidef.h"
  78. #include "service.h"
  79. #include "version.h"
  80. LOGSYS_DECLARE_SYSTEM ("corosync",
  81. LOG_MODE_OUTPUT_STDERR | LOG_MODE_THREADED | LOG_MODE_FORK,
  82. NULL,
  83. LOG_DAEMON,
  84. NULL,
  85. 1000000);
  86. LOGSYS_DECLARE_SUBSYS ("MAIN", LOG_INFO);
  87. #define SERVER_BACKLOG 5
  88. static int sched_priority = 0;
  89. static unsigned int service_count = 32;
  90. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  91. static pthread_spinlock_t serialize_spin;
  92. #else
  93. static pthread_mutex_t serialize_mutex = PTHREAD_MUTEX_INITIALIZER;
  94. #endif
  95. static struct totem_logging_configuration totem_logging_configuration;
  96. static char delivery_data[MESSAGE_SIZE_MAX];
  97. static int num_config_modules;
  98. static struct config_iface_ver0 *config_modules[MAX_DYNAMIC_SERVICES];
  99. static struct objdb_iface_ver0 *objdb = NULL;
  100. static struct corosync_api_v1 *api = NULL;
  101. static struct main_config main_config;
  102. unsigned long long *(*main_clm_get_by_nodeid) (unsigned int node_id);
  103. hdb_handle_t corosync_poll_handle;
  104. static void sigusr2_handler (int num)
  105. {
  106. int i;
  107. for (i = 0; ais_service[i]; i++) {
  108. if (ais_service[i]->exec_dump_fn) {
  109. ais_service[i]->exec_dump_fn ();
  110. }
  111. }
  112. }
  113. static void *corosync_exit (void *arg) __attribute__((__noreturn__));
  114. static void *corosync_exit (void *arg)
  115. {
  116. if (api) {
  117. corosync_service_unlink_all (api);
  118. }
  119. #ifdef DEBUG_MEMPOOL
  120. int stats_inuse[MEMPOOL_GROUP_SIZE];
  121. int stats_avail[MEMPOOL_GROUP_SIZE];
  122. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  123. int i;
  124. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  125. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  126. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  127. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  128. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  129. }
  130. #endif
  131. poll_stop (0);
  132. totempg_finalize ();
  133. coroipcs_ipc_exit ();
  134. corosync_exit_error (AIS_DONE_EXIT);
  135. }
  136. pthread_t corosync_exit_thread;
  137. static void init_shutdown(void *data)
  138. {
  139. pthread_create (&corosync_exit_thread, NULL, corosync_exit, NULL);
  140. }
  141. static poll_timer_handle shutdown_handle;
  142. static void sigquit_handler (int num)
  143. {
  144. /* avoid creating threads from within the interrupt context */
  145. poll_timer_add (corosync_poll_handle, 500, NULL, init_shutdown, &shutdown_handle);
  146. }
  147. static void sigsegv_handler (int num)
  148. {
  149. (void)signal (SIGSEGV, SIG_DFL);
  150. logsys_atsegv();
  151. logsys_log_rec_store (LOCALSTATEDIR "/lib/corosync/fdata");
  152. raise (SIGSEGV);
  153. }
  154. static void sigabrt_handler (int num)
  155. {
  156. (void)signal (SIGABRT, SIG_DFL);
  157. logsys_atsegv();
  158. logsys_log_rec_store (LOCALSTATEDIR "/lib/corosync/fdata");
  159. raise (SIGABRT);
  160. }
  161. #define LOCALHOST_IP inet_addr("127.0.0.1")
  162. hdb_handle_t corosync_group_handle;
  163. struct totempg_group corosync_group = {
  164. .group = "a",
  165. .group_len = 1
  166. };
  167. static void sigintr_handler (int signum)
  168. {
  169. poll_timer_add (corosync_poll_handle, 500, NULL,
  170. init_shutdown, &shutdown_handle);
  171. }
  172. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  173. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  174. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  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. }
  197. static int corosync_sync_callbacks_retrieve (int sync_id,
  198. struct sync_callbacks *callbacks)
  199. {
  200. unsigned int ais_service_index;
  201. unsigned int ais_services_found = 0;
  202. for (ais_service_index = 0;
  203. ais_service_index < SERVICE_HANDLER_MAXIMUM_COUNT;
  204. ais_service_index++) {
  205. if (ais_service[ais_service_index] != NULL) {
  206. if (ais_services_found == sync_id) {
  207. break;
  208. }
  209. ais_services_found += 1;
  210. }
  211. }
  212. if (ais_service_index == SERVICE_HANDLER_MAXIMUM_COUNT) {
  213. memset (callbacks, 0, sizeof (struct sync_callbacks));
  214. return (-1);
  215. }
  216. callbacks->name = ais_service[ais_service_index]->name;
  217. callbacks->sync_init = ais_service[ais_service_index]->sync_init;
  218. callbacks->sync_process = ais_service[ais_service_index]->sync_process;
  219. callbacks->sync_activate = ais_service[ais_service_index]->sync_activate;
  220. callbacks->sync_abort = ais_service[ais_service_index]->sync_abort;
  221. return (0);
  222. }
  223. static struct memb_ring_id corosync_ring_id;
  224. static void confchg_fn (
  225. enum totem_configuration_type configuration_type,
  226. unsigned int *member_list, int member_list_entries,
  227. unsigned int *left_list, int left_list_entries,
  228. unsigned int *joined_list, int joined_list_entries,
  229. struct memb_ring_id *ring_id)
  230. {
  231. int i;
  232. serialize_lock ();
  233. memcpy (&corosync_ring_id, ring_id, sizeof (struct memb_ring_id));
  234. /*
  235. * Call configuration change for all services
  236. */
  237. for (i = 0; i < service_count; i++) {
  238. if (ais_service[i] && ais_service[i]->confchg_fn) {
  239. ais_service[i]->confchg_fn (configuration_type,
  240. member_list, member_list_entries,
  241. left_list, left_list_entries,
  242. joined_list, joined_list_entries, ring_id);
  243. }
  244. }
  245. serialize_unlock ();
  246. }
  247. static void priv_drop (void)
  248. {
  249. return; /* TODO: we are still not dropping privs */
  250. setuid (main_config.uid);
  251. setegid (main_config.gid);
  252. }
  253. static void corosync_mempool_init (void)
  254. {
  255. int res;
  256. res = mempool_init (pool_sizes);
  257. if (res == ENOMEM) {
  258. log_printf (LOG_LEVEL_ERROR, "Couldn't allocate memory pools, not enough memory");
  259. corosync_exit_error (AIS_DONE_MEMPOOL_INIT);
  260. }
  261. }
  262. static void corosync_tty_detach (void)
  263. {
  264. int fd;
  265. /*
  266. * Disconnect from TTY if this is not a debug run
  267. */
  268. switch (fork ()) {
  269. case -1:
  270. corosync_exit_error (AIS_DONE_FORK);
  271. break;
  272. case 0:
  273. /*
  274. * child which is disconnected, run this process
  275. */
  276. /* setset();
  277. close (0);
  278. close (1);
  279. close (2);
  280. */
  281. break;
  282. default:
  283. exit (0);
  284. break;
  285. }
  286. /* Create new session */
  287. (void)setsid();
  288. /*
  289. * Map stdin/out/err to /dev/null.
  290. */
  291. fd = open("/dev/null", O_RDWR);
  292. if (fd >= 0) {
  293. /* dup2 to 0 / 1 / 2 (stdin / stdout / stderr) */
  294. dup2(fd, STDIN_FILENO); /* 0 */
  295. dup2(fd, STDOUT_FILENO); /* 1 */
  296. dup2(fd, STDERR_FILENO); /* 2 */
  297. /* Should be 0, but just in case it isn't... */
  298. if (fd > 2)
  299. close(fd);
  300. }
  301. }
  302. static void corosync_setscheduler (void)
  303. {
  304. #if ! defined(TS_CLASS) && (defined(COROSYNC_BSD) || defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS))
  305. struct sched_param sched_param;
  306. int res;
  307. sched_priority = sched_get_priority_max (SCHED_RR);
  308. if (sched_priority != -1) {
  309. sched_param.sched_priority = sched_priority;
  310. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  311. if (res == -1) {
  312. log_printf (LOG_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  313. sched_param.sched_priority, strerror (errno));
  314. }
  315. } else {
  316. log_printf (LOG_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  317. sched_priority = 0;
  318. }
  319. #else
  320. log_printf(LOG_LEVEL_WARNING, "Scheduler priority left to default value (no OS support)\n");
  321. #endif
  322. }
  323. static void corosync_mlockall (void)
  324. {
  325. #if !defined(COROSYNC_BSD)
  326. int res;
  327. #endif
  328. struct rlimit rlimit;
  329. rlimit.rlim_cur = RLIM_INFINITY;
  330. rlimit.rlim_max = RLIM_INFINITY;
  331. #ifndef COROSYNC_SOLARIS
  332. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  333. #else
  334. setrlimit (RLIMIT_VMEM, &rlimit);
  335. #endif
  336. #if defined(COROSYNC_BSD)
  337. /* under FreeBSD a process with locked page cannot call dlopen
  338. * code disabled until FreeBSD bug i386/93396 was solved
  339. */
  340. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults\n");
  341. #else
  342. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  343. if (res == -1) {
  344. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  345. };
  346. #endif
  347. }
  348. static void deliver_fn (
  349. unsigned int nodeid,
  350. struct iovec *iovec,
  351. int iov_len,
  352. int endian_conversion_required)
  353. {
  354. mar_req_header_t *header;
  355. int pos = 0;
  356. int i;
  357. int service;
  358. int fn_id;
  359. /*
  360. * Build buffer without iovecs to make processing easier
  361. * This is only used for messages which are multicast with iovecs
  362. * and self-delivered. All other mechanisms avoid the copy.
  363. */
  364. if (iov_len > 1) {
  365. for (i = 0; i < iov_len; i++) {
  366. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  367. pos += iovec[i].iov_len;
  368. assert (pos < MESSAGE_SIZE_MAX);
  369. }
  370. header = (mar_req_header_t *)delivery_data;
  371. } else {
  372. header = (mar_req_header_t *)iovec[0].iov_base;
  373. }
  374. if (endian_conversion_required) {
  375. header->id = swab32 (header->id);
  376. header->size = swab32 (header->size);
  377. }
  378. // assert(iovec->iov_len == header->size);
  379. /*
  380. * Call the proper executive handler
  381. */
  382. service = header->id >> 16;
  383. fn_id = header->id & 0xffff;
  384. if (!ais_service[service])
  385. return;
  386. serialize_lock();
  387. if (endian_conversion_required) {
  388. assert(ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  389. ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  390. (header);
  391. }
  392. ais_service[service]->exec_engine[fn_id].exec_handler_fn
  393. (header, nodeid);
  394. serialize_unlock();
  395. }
  396. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  397. {
  398. *modules = config_modules;
  399. *num = num_config_modules;
  400. }
  401. int main_mcast (
  402. struct iovec *iovec,
  403. int iov_len,
  404. unsigned int guarantee)
  405. {
  406. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  407. }
  408. int message_source_is_local (const mar_message_source_t *source)
  409. {
  410. int ret = 0;
  411. assert (source != NULL);
  412. if (source->nodeid == totempg_my_nodeid_get ()) {
  413. ret = 1;
  414. }
  415. return ret;
  416. }
  417. void message_source_set (
  418. mar_message_source_t *source,
  419. void *conn)
  420. {
  421. assert ((source != NULL) && (conn != NULL));
  422. memset (source, 0, sizeof (mar_message_source_t));
  423. source->nodeid = totempg_my_nodeid_get ();
  424. source->conn = conn;
  425. }
  426. /*
  427. * Provides the glue from corosync to the IPC Service
  428. */
  429. static int corosync_private_data_size_get (unsigned int service)
  430. {
  431. return (ais_service[service]->private_data_size);
  432. }
  433. static coroipcs_init_fn_lvalue corosync_init_fn_get (unsigned int service)
  434. {
  435. return (ais_service[service]->lib_init_fn);
  436. }
  437. static coroipcs_exit_fn_lvalue corosync_exit_fn_get (unsigned int service)
  438. {
  439. return (ais_service[service]->lib_exit_fn);
  440. }
  441. static coroipcs_handler_fn_lvalue corosync_handler_fn_get (unsigned int service, unsigned int id)
  442. {
  443. return (ais_service[service]->lib_engine[id].lib_handler_fn);
  444. }
  445. static int corosync_security_valid (int euid, int egid)
  446. {
  447. if (euid == 0 || egid == 0) {
  448. return (1);
  449. }
  450. if (euid == main_config.uid || egid == main_config.gid) {
  451. return (1);
  452. }
  453. return (0);
  454. }
  455. static int corosync_service_available (unsigned int service)
  456. {
  457. return (ais_service[service]);
  458. }
  459. static int corosync_response_size_get (unsigned int service, unsigned int id)
  460. {
  461. return (ais_service[service]->lib_engine[id].response_size);
  462. }
  463. static int corosync_response_id_get (unsigned int service, unsigned int id)
  464. {
  465. return (ais_service[service]->lib_engine[id].response_id);
  466. }
  467. struct sending_allowed_private_data_struct {
  468. int reserved_msgs;
  469. };
  470. static int corosync_sending_allowed (
  471. unsigned int service,
  472. unsigned int id,
  473. void *msg,
  474. void *sending_allowed_private_data)
  475. {
  476. struct sending_allowed_private_data_struct *pd =
  477. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  478. struct iovec reserve_iovec;
  479. mar_req_header_t *header = (mar_req_header_t *)msg;
  480. int sending_allowed;
  481. reserve_iovec.iov_base = (char *)header;
  482. reserve_iovec.iov_len = header->size;
  483. pd->reserved_msgs = totempg_groups_joined_reserve (
  484. corosync_group_handle,
  485. &reserve_iovec, 1);
  486. sending_allowed =
  487. (corosync_quorum_is_quorate() == 1 ||
  488. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  489. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  490. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  491. (pd->reserved_msgs) &&
  492. (sync_in_process() == 0)));
  493. return (sending_allowed);
  494. }
  495. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  496. {
  497. struct sending_allowed_private_data_struct *pd =
  498. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  499. totempg_groups_joined_release (pd->reserved_msgs);
  500. }
  501. static int ipc_subsys_id = -1;
  502. static void ipc_log_printf (const char *format, ...) {
  503. va_list ap;
  504. va_start (ap, format);
  505. _logsys_log_printf (ipc_subsys_id, __FUNCTION__,
  506. __FILE__, __LINE__, LOG_LEVEL_ERROR, format, ap);
  507. va_end (ap);
  508. }
  509. static int corosync_poll_handler_accept (
  510. hdb_handle_t handle,
  511. int fd,
  512. int revent,
  513. void *context)
  514. {
  515. return (coroipcs_handler_accept (fd, revent, context));
  516. }
  517. static int corosync_poll_handler_dispatch (
  518. hdb_handle_t handle,
  519. int fd,
  520. int revent,
  521. void *context)
  522. {
  523. return (coroipcs_handler_dispatch (fd, revent, context));
  524. }
  525. static void corosync_poll_accept_add (
  526. int fd)
  527. {
  528. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  529. corosync_poll_handler_accept);
  530. }
  531. static void corosync_poll_dispatch_add (
  532. int fd,
  533. void *context)
  534. {
  535. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  536. corosync_poll_handler_dispatch);
  537. }
  538. static void corosync_poll_dispatch_modify (
  539. int fd,
  540. int events)
  541. {
  542. poll_dispatch_modify (corosync_poll_handle, fd, events,
  543. corosync_poll_handler_dispatch);
  544. }
  545. struct coroipcs_init_state ipc_init_state = {
  546. .socket_name = IPC_SOCKET_NAME,
  547. .malloc = malloc,
  548. .free = free,
  549. .log_printf = ipc_log_printf,
  550. .security_valid = corosync_security_valid,
  551. .service_available = corosync_service_available,
  552. .private_data_size_get = corosync_private_data_size_get,
  553. .serialize_lock = serialize_lock,
  554. .serialize_unlock = serialize_unlock,
  555. .sending_allowed = corosync_sending_allowed,
  556. .sending_allowed_release = corosync_sending_allowed_release,
  557. .response_size_get = corosync_response_size_get,
  558. .response_id_get = corosync_response_id_get,
  559. .poll_accept_add = corosync_poll_accept_add,
  560. .poll_dispatch_add = corosync_poll_dispatch_add,
  561. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  562. .init_fn_get = corosync_init_fn_get,
  563. .exit_fn_get = corosync_exit_fn_get,
  564. .handler_fn_get = corosync_handler_fn_get
  565. };
  566. int main (int argc, char **argv)
  567. {
  568. const char *error_string;
  569. struct totem_config totem_config;
  570. hdb_handle_t objdb_handle;
  571. hdb_handle_t config_handle;
  572. unsigned int config_version = 0;
  573. void *objdb_p;
  574. struct config_iface_ver0 *config;
  575. void *config_p;
  576. const char *config_iface_init;
  577. char *config_iface;
  578. char *iface;
  579. int res, ch;
  580. int background, setprio;
  581. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  582. pthread_spin_init (&serialize_spin, 0);
  583. #endif
  584. /* default configuration
  585. */
  586. background = 1;
  587. setprio = 1;
  588. while ((ch = getopt (argc, argv, "fp")) != EOF) {
  589. switch (ch) {
  590. case 'f':
  591. background = 0;
  592. logsys_config_mode_set (LOG_MODE_OUTPUT_STDERR|LOG_MODE_THREADED|LOG_MODE_FORK);
  593. break;
  594. case 'p':
  595. setprio = 0;
  596. break;
  597. default:
  598. fprintf(stderr, \
  599. "usage:\n"\
  600. " -f : Start application in foreground.\n"\
  601. " -p : Do not set process priority. \n");
  602. return EXIT_FAILURE;
  603. }
  604. }
  605. if (background)
  606. corosync_tty_detach ();
  607. log_printf (LOG_LEVEL_NOTICE, "Corosync Executive Service RELEASE '%s'\n", RELEASE_VERSION);
  608. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2002-2006 MontaVista Software, Inc and contributors.\n");
  609. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2006-2008 Red Hat, Inc.\n");
  610. (void)signal (SIGINT, sigintr_handler);
  611. (void)signal (SIGUSR2, sigusr2_handler);
  612. (void)signal (SIGSEGV, sigsegv_handler);
  613. (void)signal (SIGABRT, sigabrt_handler);
  614. (void)signal (SIGQUIT, sigquit_handler);
  615. #if MSG_NOSIGNAL == 0
  616. (void)signal (SIGPIPE, SIG_IGN);
  617. #endif
  618. corosync_timer_init (
  619. serialize_lock,
  620. serialize_unlock,
  621. sched_priority);
  622. log_printf (LOG_LEVEL_NOTICE, "Corosync Executive Service: started and ready to provide service.\n");
  623. corosync_poll_handle = poll_create ();
  624. /*
  625. * Load the object database interface
  626. */
  627. res = lcr_ifact_reference (
  628. &objdb_handle,
  629. "objdb",
  630. 0,
  631. &objdb_p,
  632. 0);
  633. if (res == -1) {
  634. log_printf (LOG_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  635. corosync_exit_error (AIS_DONE_OBJDB);
  636. }
  637. objdb = (struct objdb_iface_ver0 *)objdb_p;
  638. objdb->objdb_init ();
  639. /*
  640. * Initialize the corosync_api_v1 definition
  641. */
  642. apidef_init (objdb);
  643. api = apidef_get ();
  644. num_config_modules = 0;
  645. /*
  646. * Bootstrap in the default configuration parser or use
  647. * the corosync default built in parser if the configuration parser
  648. * isn't overridden
  649. */
  650. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  651. if (!config_iface_init) {
  652. config_iface_init = "corosync_parser";
  653. }
  654. /* Make a copy so we can deface it with strtok */
  655. config_iface = strdup(config_iface_init);
  656. iface = strtok(config_iface, ":");
  657. while (iface)
  658. {
  659. res = lcr_ifact_reference (
  660. &config_handle,
  661. iface,
  662. config_version,
  663. &config_p,
  664. 0);
  665. config = (struct config_iface_ver0 *)config_p;
  666. if (res == -1) {
  667. log_printf (LOG_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  668. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  669. }
  670. res = config->config_readconfig(objdb, &error_string);
  671. if (res == -1) {
  672. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  673. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  674. }
  675. log_printf (LOG_LEVEL_NOTICE, "%s", error_string);
  676. config_modules[num_config_modules++] = config;
  677. iface = strtok(NULL, ":");
  678. }
  679. if (config_iface)
  680. free(config_iface);
  681. res = corosync_main_config_read (objdb, &error_string, &main_config);
  682. if (res == -1) {
  683. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  684. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  685. }
  686. res = totem_config_read (objdb, &totem_config, &error_string);
  687. if (res == -1) {
  688. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  689. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  690. }
  691. res = totem_config_keyread (objdb, &totem_config, &error_string);
  692. if (res == -1) {
  693. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  694. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  695. }
  696. res = totem_config_validate (&totem_config, &error_string);
  697. if (res == -1) {
  698. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  699. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  700. }
  701. /*
  702. * Set round robin realtime scheduling with priority 99
  703. * Lock all memory to avoid page faults which may interrupt
  704. * application healthchecking
  705. */
  706. if (setprio)
  707. corosync_setscheduler ();
  708. corosync_mlockall ();
  709. totem_config.totem_logging_configuration = totem_logging_configuration;
  710. totem_config.totem_logging_configuration.log_subsys_id =
  711. _logsys_subsys_create ("TOTEM", LOG_INFO);
  712. totem_config.totem_logging_configuration.log_level_security = LOG_LEVEL_SECURITY;
  713. totem_config.totem_logging_configuration.log_level_error = LOG_LEVEL_ERROR;
  714. totem_config.totem_logging_configuration.log_level_warning = LOG_LEVEL_WARNING;
  715. totem_config.totem_logging_configuration.log_level_notice = LOG_LEVEL_NOTICE;
  716. totem_config.totem_logging_configuration.log_level_debug = LOG_LEVEL_DEBUG;
  717. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  718. /*
  719. * Sleep for a while to let other nodes in the cluster
  720. * understand that this node has been away (if it was
  721. * an corosync restart).
  722. */
  723. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  724. /*
  725. * if totempg_initialize doesn't have root priveleges, it cannot
  726. * bind to a specific interface. This only matters if
  727. * there is more then one interface in a system, so
  728. * in this case, only a warning is printed
  729. */
  730. /*
  731. * Join multicast group and setup delivery
  732. * and configuration change functions
  733. */
  734. totempg_initialize (
  735. corosync_poll_handle,
  736. &totem_config);
  737. totempg_groups_initialize (
  738. &corosync_group_handle,
  739. deliver_fn,
  740. confchg_fn);
  741. totempg_groups_join (
  742. corosync_group_handle,
  743. &corosync_group,
  744. 1);
  745. /*
  746. * This must occur after totempg is initialized because "this_ip" must be set
  747. */
  748. res = corosync_service_defaults_link_and_init (api);
  749. if (res == -1) {
  750. log_printf (LOG_LEVEL_ERROR, "Could not initialize default services\n");
  751. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  752. }
  753. sync_register (corosync_sync_callbacks_retrieve, corosync_sync_completed);
  754. /*
  755. * Drop root privleges to user 'ais'
  756. * TODO: Don't really need full root capabilities;
  757. * needed capabilities are:
  758. * CAP_NET_RAW (bindtodevice)
  759. * CAP_SYS_NICE (setscheduler)
  760. * CAP_IPC_LOCK (mlockall)
  761. */
  762. priv_drop ();
  763. corosync_mempool_init ();
  764. ipc_subsys_id = _logsys_subsys_create ("IPC", LOG_INFO);
  765. ipc_init_state.sched_priority = sched_priority;
  766. coroipcs_ipc_init (&ipc_init_state);
  767. /*
  768. * Start main processing loop
  769. */
  770. poll_run (corosync_poll_handle);
  771. return EXIT_SUCCESS;
  772. }