main.c 23 KB

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