main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. 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. sched_priority = sched_get_priority_max (SCHED_RR);
  307. if (sched_priority != -1) {
  308. sched_param.sched_priority = sched_priority;
  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. sched_priority = 0;
  317. }
  318. #else
  319. log_printf(LOG_LEVEL_WARNING, "Scheduler priority left to default value (no OS support)\n");
  320. #endif
  321. }
  322. static void corosync_mlockall (void)
  323. {
  324. #if !defined(COROSYNC_BSD)
  325. int res;
  326. #endif
  327. struct rlimit rlimit;
  328. rlimit.rlim_cur = RLIM_INFINITY;
  329. rlimit.rlim_max = RLIM_INFINITY;
  330. #ifndef COROSYNC_SOLARIS
  331. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  332. #else
  333. setrlimit (RLIMIT_VMEM, &rlimit);
  334. #endif
  335. #if defined(COROSYNC_BSD)
  336. /* under FreeBSD a process with locked page cannot call dlopen
  337. * code disabled until FreeBSD bug i386/93396 was solved
  338. */
  339. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults\n");
  340. #else
  341. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  342. if (res == -1) {
  343. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  344. };
  345. #endif
  346. }
  347. static void deliver_fn (
  348. unsigned int nodeid,
  349. struct iovec *iovec,
  350. int iov_len,
  351. int endian_conversion_required)
  352. {
  353. mar_req_header_t *header;
  354. int pos = 0;
  355. int i;
  356. int service;
  357. int fn_id;
  358. /*
  359. * Build buffer without iovecs to make processing easier
  360. * This is only used for messages which are multicast with iovecs
  361. * and self-delivered. All other mechanisms avoid the copy.
  362. */
  363. if (iov_len > 1) {
  364. for (i = 0; i < iov_len; i++) {
  365. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  366. pos += iovec[i].iov_len;
  367. assert (pos < MESSAGE_SIZE_MAX);
  368. }
  369. header = (mar_req_header_t *)delivery_data;
  370. } else {
  371. header = (mar_req_header_t *)iovec[0].iov_base;
  372. }
  373. if (endian_conversion_required) {
  374. header->id = swab32 (header->id);
  375. header->size = swab32 (header->size);
  376. }
  377. // assert(iovec->iov_len == header->size);
  378. /*
  379. * Call the proper executive handler
  380. */
  381. service = header->id >> 16;
  382. fn_id = header->id & 0xffff;
  383. if (!ais_service[service])
  384. return;
  385. serialize_lock();
  386. if (endian_conversion_required) {
  387. assert(ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  388. ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  389. (header);
  390. }
  391. ais_service[service]->exec_engine[fn_id].exec_handler_fn
  392. (header, nodeid);
  393. serialize_unlock();
  394. }
  395. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  396. {
  397. *modules = config_modules;
  398. *num = num_config_modules;
  399. }
  400. int main_mcast (
  401. struct iovec *iovec,
  402. int iov_len,
  403. unsigned int guarantee)
  404. {
  405. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  406. }
  407. int message_source_is_local (const mar_message_source_t *source)
  408. {
  409. int ret = 0;
  410. assert (source != NULL);
  411. if (source->nodeid == totempg_my_nodeid_get ()) {
  412. ret = 1;
  413. }
  414. return ret;
  415. }
  416. void message_source_set (
  417. mar_message_source_t *source,
  418. void *conn)
  419. {
  420. assert ((source != NULL) && (conn != NULL));
  421. memset (source, 0, sizeof (mar_message_source_t));
  422. source->nodeid = totempg_my_nodeid_get ();
  423. source->conn = conn;
  424. }
  425. /*
  426. * Provides the glue from corosync to the IPC Service
  427. */
  428. static int corosync_private_data_size_get (unsigned int service)
  429. {
  430. return (ais_service[service]->private_data_size);
  431. }
  432. static coroipcs_init_fn_lvalue corosync_init_fn_get (unsigned int service)
  433. {
  434. return (ais_service[service]->lib_init_fn);
  435. }
  436. static coroipcs_exit_fn_lvalue corosync_exit_fn_get (unsigned int service)
  437. {
  438. return (ais_service[service]->lib_exit_fn);
  439. }
  440. static coroipcs_handler_fn_lvalue corosync_handler_fn_get (unsigned int service, unsigned int id)
  441. {
  442. return (ais_service[service]->lib_engine[id].lib_handler_fn);
  443. }
  444. static int corosync_security_valid (int euid, int egid)
  445. {
  446. if (euid == 0 || egid == 0) {
  447. return (1);
  448. }
  449. if (euid == main_config.uid || egid == main_config.gid) {
  450. return (1);
  451. }
  452. return (0);
  453. }
  454. static int corosync_service_available (unsigned int service)
  455. {
  456. return (ais_service[service]);
  457. }
  458. static int corosync_response_size_get (unsigned int service, unsigned int id)
  459. {
  460. return (ais_service[service]->lib_engine[id].response_size);
  461. }
  462. static int corosync_response_id_get (unsigned int service, unsigned int id)
  463. {
  464. return (ais_service[service]->lib_engine[id].response_id);
  465. }
  466. struct sending_allowed_private_data_struct {
  467. int reserved_msgs;
  468. };
  469. static int corosync_sending_allowed (
  470. unsigned int service,
  471. unsigned int id,
  472. void *msg,
  473. void *sending_allowed_private_data)
  474. {
  475. struct sending_allowed_private_data_struct *pd =
  476. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  477. struct iovec reserve_iovec;
  478. mar_req_header_t *header = (mar_req_header_t *)msg;
  479. int sending_allowed;
  480. reserve_iovec.iov_base = (char *)header;
  481. reserve_iovec.iov_len = header->size;
  482. pd->reserved_msgs = totempg_groups_joined_reserve (
  483. corosync_group_handle,
  484. &reserve_iovec, 1);
  485. sending_allowed =
  486. (corosync_quorum_is_quorate() == 1 ||
  487. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  488. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  489. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  490. (pd->reserved_msgs) &&
  491. (sync_in_process() == 0)));
  492. return (sending_allowed);
  493. }
  494. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  495. {
  496. struct sending_allowed_private_data_struct *pd =
  497. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  498. totempg_groups_joined_release (pd->reserved_msgs);
  499. }
  500. static int ipc_subsys_id = -1;
  501. static void ipc_log_printf (const char *format, ...) {
  502. va_list ap;
  503. va_start (ap, format);
  504. _logsys_log_printf (ipc_subsys_id, __FUNCTION__,
  505. __FILE__, __LINE__, LOG_LEVEL_ERROR, format, ap);
  506. va_end (ap);
  507. }
  508. static int corosync_poll_handler_accept (
  509. hdb_handle_t handle,
  510. int fd,
  511. int revent,
  512. void *context)
  513. {
  514. return (coroipcs_handler_accept (fd, revent, context));
  515. }
  516. static int corosync_poll_handler_dispatch (
  517. hdb_handle_t handle,
  518. int fd,
  519. int revent,
  520. void *context)
  521. {
  522. return (coroipcs_handler_dispatch (fd, revent, context));
  523. }
  524. static void corosync_poll_accept_add (
  525. int fd)
  526. {
  527. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  528. corosync_poll_handler_accept);
  529. }
  530. static void corosync_poll_dispatch_add (
  531. int fd,
  532. void *context)
  533. {
  534. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  535. corosync_poll_handler_dispatch);
  536. }
  537. static void corosync_poll_dispatch_modify (
  538. int fd,
  539. int events)
  540. {
  541. poll_dispatch_modify (corosync_poll_handle, fd, events,
  542. corosync_poll_handler_dispatch);
  543. }
  544. struct coroipcs_init_state ipc_init_state = {
  545. .socket_name = IPC_SOCKET_NAME,
  546. .malloc = malloc,
  547. .free = free,
  548. .log_printf = ipc_log_printf,
  549. .security_valid = corosync_security_valid,
  550. .service_available = corosync_service_available,
  551. .private_data_size_get = corosync_private_data_size_get,
  552. .serialize_lock = serialize_lock,
  553. .serialize_unlock = serialize_unlock,
  554. .sending_allowed = corosync_sending_allowed,
  555. .sending_allowed_release = corosync_sending_allowed_release,
  556. .response_size_get = corosync_response_size_get,
  557. .response_id_get = corosync_response_id_get,
  558. .poll_accept_add = corosync_poll_accept_add,
  559. .poll_dispatch_add = corosync_poll_dispatch_add,
  560. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  561. .init_fn_get = corosync_init_fn_get,
  562. .exit_fn_get = corosync_exit_fn_get,
  563. .handler_fn_get = corosync_handler_fn_get
  564. };
  565. int main (int argc, char **argv)
  566. {
  567. const char *error_string;
  568. struct totem_config totem_config;
  569. hdb_handle_t objdb_handle;
  570. hdb_handle_t config_handle;
  571. unsigned int config_version = 0;
  572. void *objdb_p;
  573. struct config_iface_ver0 *config;
  574. void *config_p;
  575. const char *config_iface_init;
  576. char *config_iface;
  577. char *iface;
  578. int res, ch;
  579. int background, setprio;
  580. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  581. pthread_spin_init (&serialize_spin, 0);
  582. #endif
  583. /* default configuration
  584. */
  585. background = 1;
  586. setprio = 1;
  587. while ((ch = getopt (argc, argv, "fp")) != EOF) {
  588. switch (ch) {
  589. case 'f':
  590. background = 0;
  591. logsys_config_mode_set (LOG_MODE_OUTPUT_STDERR|LOG_MODE_THREADED|LOG_MODE_FORK);
  592. break;
  593. case 'p':
  594. setprio = 0;
  595. break;
  596. default:
  597. fprintf(stderr, \
  598. "usage:\n"\
  599. " -f : Start application in foreground.\n"\
  600. " -p : Do not set process priority. \n");
  601. return EXIT_FAILURE;
  602. }
  603. }
  604. if (background)
  605. corosync_tty_detach ();
  606. log_printf (LOG_LEVEL_NOTICE, "Corosync Executive Service RELEASE '%s'\n", RELEASE_VERSION);
  607. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2002-2006 MontaVista Software, Inc and contributors.\n");
  608. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2006-2008 Red Hat, Inc.\n");
  609. (void)signal (SIGINT, sigintr_handler);
  610. (void)signal (SIGUSR2, sigusr2_handler);
  611. (void)signal (SIGSEGV, sigsegv_handler);
  612. (void)signal (SIGABRT, sigabrt_handler);
  613. (void)signal (SIGQUIT, sigquit_handler);
  614. #if MSG_NOSIGNAL == 0
  615. (void)signal (SIGPIPE, SIG_IGN);
  616. #endif
  617. corosync_timer_init (
  618. serialize_lock,
  619. serialize_unlock,
  620. sched_priority);
  621. log_printf (LOG_LEVEL_NOTICE, "Corosync Executive Service: started and ready to provide service.\n");
  622. corosync_poll_handle = poll_create ();
  623. /*
  624. * Load the object database interface
  625. */
  626. res = lcr_ifact_reference (
  627. &objdb_handle,
  628. "objdb",
  629. 0,
  630. &objdb_p,
  631. 0);
  632. if (res == -1) {
  633. log_printf (LOG_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  634. corosync_exit_error (AIS_DONE_OBJDB);
  635. }
  636. objdb = (struct objdb_iface_ver0 *)objdb_p;
  637. objdb->objdb_init ();
  638. /*
  639. * Initialize the corosync_api_v1 definition
  640. */
  641. apidef_init (objdb);
  642. api = apidef_get ();
  643. num_config_modules = 0;
  644. /*
  645. * Bootstrap in the default configuration parser or use
  646. * the corosync default built in parser if the configuration parser
  647. * isn't overridden
  648. */
  649. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  650. if (!config_iface_init) {
  651. config_iface_init = "corosync_parser";
  652. }
  653. /* Make a copy so we can deface it with strtok */
  654. config_iface = strdup(config_iface_init);
  655. iface = strtok(config_iface, ":");
  656. while (iface)
  657. {
  658. res = lcr_ifact_reference (
  659. &config_handle,
  660. iface,
  661. config_version,
  662. &config_p,
  663. 0);
  664. config = (struct config_iface_ver0 *)config_p;
  665. if (res == -1) {
  666. log_printf (LOG_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  667. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  668. }
  669. res = config->config_readconfig(objdb, &error_string);
  670. if (res == -1) {
  671. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  672. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  673. }
  674. log_printf (LOG_LEVEL_NOTICE, "%s", error_string);
  675. config_modules[num_config_modules++] = config;
  676. iface = strtok(NULL, ":");
  677. }
  678. if (config_iface)
  679. free(config_iface);
  680. res = corosync_main_config_read (objdb, &error_string, &main_config);
  681. if (res == -1) {
  682. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  683. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  684. }
  685. res = totem_config_read (objdb, &totem_config, &error_string);
  686. if (res == -1) {
  687. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  688. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  689. }
  690. res = totem_config_keyread (objdb, &totem_config, &error_string);
  691. if (res == -1) {
  692. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  693. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  694. }
  695. res = totem_config_validate (&totem_config, &error_string);
  696. if (res == -1) {
  697. log_printf (LOG_LEVEL_ERROR, "%s", error_string);
  698. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  699. }
  700. /*
  701. * Set round robin realtime scheduling with priority 99
  702. * Lock all memory to avoid page faults which may interrupt
  703. * application healthchecking
  704. */
  705. if (setprio)
  706. corosync_setscheduler ();
  707. corosync_mlockall ();
  708. totem_config.totem_logging_configuration = totem_logging_configuration;
  709. totem_config.totem_logging_configuration.log_subsys_id =
  710. _logsys_subsys_create ("TOTEM", LOG_INFO);
  711. totem_config.totem_logging_configuration.log_level_security = LOG_LEVEL_SECURITY;
  712. totem_config.totem_logging_configuration.log_level_error = LOG_LEVEL_ERROR;
  713. totem_config.totem_logging_configuration.log_level_warning = LOG_LEVEL_WARNING;
  714. totem_config.totem_logging_configuration.log_level_notice = LOG_LEVEL_NOTICE;
  715. totem_config.totem_logging_configuration.log_level_debug = LOG_LEVEL_DEBUG;
  716. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  717. /*
  718. * Sleep for a while to let other nodes in the cluster
  719. * understand that this node has been away (if it was
  720. * an corosync restart).
  721. */
  722. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  723. /*
  724. * if totempg_initialize doesn't have root priveleges, it cannot
  725. * bind to a specific interface. This only matters if
  726. * there is more then one interface in a system, so
  727. * in this case, only a warning is printed
  728. */
  729. /*
  730. * Join multicast group and setup delivery
  731. * and configuration change functions
  732. */
  733. totempg_initialize (
  734. corosync_poll_handle,
  735. &totem_config);
  736. totempg_groups_initialize (
  737. &corosync_group_handle,
  738. deliver_fn,
  739. confchg_fn);
  740. totempg_groups_join (
  741. corosync_group_handle,
  742. &corosync_group,
  743. 1);
  744. /*
  745. * This must occur after totempg is initialized because "this_ip" must be set
  746. */
  747. res = corosync_service_defaults_link_and_init (api);
  748. if (res == -1) {
  749. log_printf (LOG_LEVEL_ERROR, "Could not initialize default services\n");
  750. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  751. }
  752. sync_register (corosync_sync_callbacks_retrieve, corosync_sync_completed);
  753. /*
  754. * Drop root privleges to user 'ais'
  755. * TODO: Don't really need full root capabilities;
  756. * needed capabilities are:
  757. * CAP_NET_RAW (bindtodevice)
  758. * CAP_SYS_NICE (setscheduler)
  759. * CAP_IPC_LOCK (mlockall)
  760. */
  761. priv_drop ();
  762. corosync_mempool_init ();
  763. ipc_subsys_id = _logsys_subsys_create ("IPC", LOG_INFO);
  764. ipc_init_state.sched_priority = sched_priority;
  765. coroipcs_ipc_init (&ipc_init_state);
  766. /*
  767. * Start main processing loop
  768. */
  769. poll_run (corosync_poll_handle);
  770. return EXIT_SUCCESS;
  771. }