main.c 24 KB

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