main.c 24 KB

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