main.c 26 KB

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