main.c 25 KB

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