main.c 23 KB

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