main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. sending_allowed =
  487. (corosync_quorum_is_quorate() == 1 ||
  488. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  489. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  490. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  491. (pd->reserved_msgs) &&
  492. (sync_in_process == 0)));
  493. return (sending_allowed);
  494. }
  495. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  496. {
  497. struct sending_allowed_private_data_struct *pd =
  498. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  499. totempg_groups_joined_release (pd->reserved_msgs);
  500. }
  501. static int ipc_subsys_id = -1;
  502. static void ipc_log_printf (const char *format, ...) __attribute__((format(printf, 1, 2)));
  503. static void ipc_log_printf (const char *format, ...) {
  504. va_list ap;
  505. va_start (ap, format);
  506. _logsys_log_vprintf (
  507. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  508. ipc_subsys_id,
  509. LOGSYS_RECID_LOG),
  510. __FUNCTION__, __FILE__, __LINE__,
  511. format, ap);
  512. va_end (ap);
  513. }
  514. static void ipc_fatal_error(const char *error_msg) {
  515. _logsys_log_printf (
  516. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  517. ipc_subsys_id,
  518. LOGSYS_RECID_LOG),
  519. __FUNCTION__, __FILE__, __LINE__,
  520. "%s", error_msg);
  521. exit(EXIT_FAILURE);
  522. }
  523. static int corosync_poll_handler_accept (
  524. hdb_handle_t handle,
  525. int fd,
  526. int revent,
  527. void *context)
  528. {
  529. return (coroipcs_handler_accept (fd, revent, context));
  530. }
  531. static int corosync_poll_handler_dispatch (
  532. hdb_handle_t handle,
  533. int fd,
  534. int revent,
  535. void *context)
  536. {
  537. return (coroipcs_handler_dispatch (fd, revent, context));
  538. }
  539. static void corosync_poll_accept_add (
  540. int fd)
  541. {
  542. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  543. corosync_poll_handler_accept);
  544. }
  545. static void corosync_poll_dispatch_add (
  546. int fd,
  547. void *context)
  548. {
  549. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  550. corosync_poll_handler_dispatch);
  551. }
  552. static void corosync_poll_dispatch_modify (
  553. int fd,
  554. int events)
  555. {
  556. poll_dispatch_modify (corosync_poll_handle, fd, events,
  557. corosync_poll_handler_dispatch);
  558. }
  559. static struct coroipcs_init_state ipc_init_state = {
  560. .socket_name = COROSYNC_SOCKET_NAME,
  561. .sched_policy = SCHED_OTHER,
  562. .sched_param = &global_sched_param,
  563. .malloc = malloc,
  564. .free = free,
  565. .log_printf = ipc_log_printf,
  566. .fatal_error = ipc_fatal_error,
  567. .security_valid = corosync_security_valid,
  568. .service_available = corosync_service_available,
  569. .private_data_size_get = corosync_private_data_size_get,
  570. .serialize_lock = serialize_lock,
  571. .serialize_unlock = serialize_unlock,
  572. .sending_allowed = corosync_sending_allowed,
  573. .sending_allowed_release = corosync_sending_allowed_release,
  574. .poll_accept_add = corosync_poll_accept_add,
  575. .poll_dispatch_add = corosync_poll_dispatch_add,
  576. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  577. .init_fn_get = corosync_init_fn_get,
  578. .exit_fn_get = corosync_exit_fn_get,
  579. .handler_fn_get = corosync_handler_fn_get
  580. };
  581. static void corosync_setscheduler (void)
  582. {
  583. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  584. int res;
  585. sched_priority = sched_get_priority_max (SCHED_RR);
  586. if (sched_priority != -1) {
  587. global_sched_param.sched_priority = sched_priority;
  588. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  589. if (res == -1) {
  590. global_sched_param.sched_priority = 0;
  591. log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  592. global_sched_param.sched_priority, strerror (errno));
  593. } else {
  594. /*
  595. * Turn on SCHED_RR in ipc system
  596. */
  597. ipc_init_state.sched_policy = SCHED_RR;
  598. }
  599. } else {
  600. log_printf (LOGSYS_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  601. sched_priority = 0;
  602. }
  603. #else
  604. log_printf(LOGSYS_LEVEL_WARNING,
  605. "The Platform is missing process priority setting features. Leaving at default.");
  606. #endif
  607. }
  608. int main (int argc, char **argv)
  609. {
  610. const char *error_string;
  611. struct totem_config totem_config;
  612. hdb_handle_t objdb_handle;
  613. hdb_handle_t config_handle;
  614. unsigned int config_version = 0;
  615. void *objdb_p;
  616. struct config_iface_ver0 *config;
  617. void *config_p;
  618. const char *config_iface_init;
  619. char *config_iface;
  620. char *iface;
  621. int res, ch;
  622. int background, setprio;
  623. struct stat stat_out;
  624. char corosync_lib_dir[PATH_MAX];
  625. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  626. pthread_spin_init (&serialize_spin, 0);
  627. #endif
  628. /* default configuration
  629. */
  630. background = 1;
  631. setprio = 1;
  632. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  633. switch (ch) {
  634. case 'f':
  635. background = 0;
  636. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  637. break;
  638. case 'p':
  639. setprio = 0;
  640. break;
  641. case 'v':
  642. printf ("Corosync Cluster Engine, version '%s' SVN revision '%s'\n", VERSION, SVN_REVISION);
  643. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  644. return EXIT_SUCCESS;
  645. break;
  646. default:
  647. fprintf(stderr, \
  648. "usage:\n"\
  649. " -f : Start application in foreground.\n"\
  650. " -p : Do not set process priority. \n"\
  651. " -v : Display version and SVN revision of Corosync and exit.\n");
  652. return EXIT_FAILURE;
  653. }
  654. }
  655. if (background)
  656. corosync_tty_detach ();
  657. /*
  658. * Set round robin realtime scheduling with priority 99
  659. * Lock all memory to avoid page faults which may interrupt
  660. * application healthchecking
  661. */
  662. if (setprio) {
  663. corosync_setscheduler ();
  664. }
  665. corosync_mlockall ();
  666. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  667. (void)signal (SIGINT, sigintr_handler);
  668. (void)signal (SIGUSR2, sigusr2_handler);
  669. (void)signal (SIGSEGV, sigsegv_handler);
  670. (void)signal (SIGABRT, sigabrt_handler);
  671. (void)signal (SIGQUIT, sigquit_handler);
  672. #if MSG_NOSIGNAL == 0
  673. (void)signal (SIGPIPE, SIG_IGN);
  674. #endif
  675. corosync_timer_init (
  676. serialize_lock,
  677. serialize_unlock,
  678. sched_priority);
  679. corosync_poll_handle = poll_create ();
  680. /*
  681. * Load the object database interface
  682. */
  683. res = lcr_ifact_reference (
  684. &objdb_handle,
  685. "objdb",
  686. 0,
  687. &objdb_p,
  688. 0);
  689. if (res == -1) {
  690. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  691. corosync_exit_error (AIS_DONE_OBJDB);
  692. }
  693. objdb = (struct objdb_iface_ver0 *)objdb_p;
  694. objdb->objdb_init ();
  695. /*
  696. * Initialize the corosync_api_v1 definition
  697. */
  698. apidef_init (objdb);
  699. api = apidef_get ();
  700. num_config_modules = 0;
  701. /*
  702. * Bootstrap in the default configuration parser or use
  703. * the corosync default built in parser if the configuration parser
  704. * isn't overridden
  705. */
  706. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  707. if (!config_iface_init) {
  708. config_iface_init = "corosync_parser";
  709. }
  710. /* Make a copy so we can deface it with strtok */
  711. if ((config_iface = strdup(config_iface_init)) == NULL) {
  712. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  713. corosync_exit_error (AIS_DONE_OBJDB);
  714. }
  715. iface = strtok(config_iface, ":");
  716. while (iface)
  717. {
  718. res = lcr_ifact_reference (
  719. &config_handle,
  720. iface,
  721. config_version,
  722. &config_p,
  723. 0);
  724. config = (struct config_iface_ver0 *)config_p;
  725. if (res == -1) {
  726. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  727. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  728. }
  729. res = config->config_readconfig(objdb, &error_string);
  730. if (res == -1) {
  731. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  732. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  733. }
  734. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  735. config_modules[num_config_modules++] = config;
  736. iface = strtok(NULL, ":");
  737. }
  738. free(config_iface);
  739. res = corosync_main_config_read (objdb, &error_string);
  740. if (res == -1) {
  741. /*
  742. * if we are here, we _must_ flush the logsys queue
  743. * and try to inform that we couldn't read the config.
  744. * this is a desperate attempt before certain death
  745. * and there is no guarantee that we can print to stderr
  746. * nor that logsys is sending the messages where we expect.
  747. */
  748. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  749. fprintf(stderr, "%s", error_string);
  750. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  751. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  752. }
  753. logsys_fork_completed();
  754. if (setprio) {
  755. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  756. if (res == -1) {
  757. log_printf (LOGSYS_LEVEL_ERROR,
  758. "Could not set logsys thread priority. Can't continue because of priority inversions.");
  759. corosync_exit_error (AIS_DONE_LOGSETUP);
  760. }
  761. } else {
  762. res = logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  763. }
  764. /*
  765. * Make sure required directory is present
  766. */
  767. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  768. res = stat (corosync_lib_dir, &stat_out);
  769. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  770. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  771. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  772. }
  773. res = totem_config_read (objdb, &totem_config, &error_string);
  774. if (res == -1) {
  775. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  776. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  777. }
  778. res = totem_config_keyread (objdb, &totem_config, &error_string);
  779. if (res == -1) {
  780. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  781. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  782. }
  783. res = totem_config_validate (&totem_config, &error_string);
  784. if (res == -1) {
  785. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  786. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  787. }
  788. totem_config.totem_logging_configuration = totem_logging_configuration;
  789. totem_config.totem_logging_configuration.log_subsys_id =
  790. _logsys_subsys_create ("TOTEM");
  791. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  792. log_printf (LOGSYS_LEVEL_ERROR,
  793. "Unable to initialize TOTEM logging subsystem\n");
  794. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  795. }
  796. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  797. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  798. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  799. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  800. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  801. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  802. res = corosync_main_config_compatibility_read (objdb,
  803. &minimum_sync_mode,
  804. &error_string);
  805. if (res == -1) {
  806. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  807. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  808. }
  809. res = corosync_main_config_compatibility_read (objdb,
  810. &minimum_sync_mode,
  811. &error_string);
  812. if (res == -1) {
  813. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  814. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  815. }
  816. /*
  817. * Sleep for a while to let other nodes in the cluster
  818. * understand that this node has been away (if it was
  819. * an corosync restart).
  820. */
  821. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  822. /*
  823. * if totempg_initialize doesn't have root priveleges, it cannot
  824. * bind to a specific interface. This only matters if
  825. * there is more then one interface in a system, so
  826. * in this case, only a warning is printed
  827. */
  828. /*
  829. * Join multicast group and setup delivery
  830. * and configuration change functions
  831. */
  832. totempg_initialize (
  833. corosync_poll_handle,
  834. &totem_config);
  835. totempg_groups_initialize (
  836. &corosync_group_handle,
  837. deliver_fn,
  838. confchg_fn);
  839. totempg_groups_join (
  840. corosync_group_handle,
  841. &corosync_group,
  842. 1);
  843. /*
  844. * This must occur after totempg is initialized because "this_ip" must be set
  845. */
  846. res = corosync_service_defaults_link_and_init (api);
  847. if (res == -1) {
  848. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  849. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  850. }
  851. evil_init (api);
  852. if (minimum_sync_mode == CS_SYNC_V2) {
  853. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  854. sync_v2_init (
  855. corosync_sync_v2_callbacks_retrieve,
  856. corosync_sync_completed);
  857. } else
  858. if (minimum_sync_mode == CS_SYNC_V1) {
  859. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  860. sync_register (
  861. corosync_sync_callbacks_retrieve,
  862. sync_v2_memb_list_determine,
  863. sync_v2_memb_list_abort,
  864. sync_v2_start);
  865. sync_v2_init (
  866. corosync_sync_v2_callbacks_retrieve,
  867. corosync_sync_completed);
  868. }
  869. /*
  870. * Drop root privleges to user 'ais'
  871. * TODO: Don't really need full root capabilities;
  872. * needed capabilities are:
  873. * CAP_NET_RAW (bindtodevice)
  874. * CAP_SYS_NICE (setscheduler)
  875. * CAP_IPC_LOCK (mlockall)
  876. */
  877. priv_drop ();
  878. schedwrk_init (
  879. serialize_lock,
  880. serialize_unlock);
  881. ipc_subsys_id = _logsys_subsys_create ("IPC");
  882. if (ipc_subsys_id < 0) {
  883. log_printf (LOGSYS_LEVEL_ERROR,
  884. "Could not initialize IPC logging subsystem\n");
  885. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  886. }
  887. coroipcs_ipc_init (&ipc_init_state);
  888. /*
  889. * Start main processing loop
  890. */
  891. poll_run (corosync_poll_handle);
  892. return EXIT_SUCCESS;
  893. }