main.c 27 KB

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