main.c 23 KB

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