main.c 26 KB

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