main.c 23 KB

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