main.c 23 KB

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