main.c 24 KB

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