main.c 24 KB

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