main.c 24 KB

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