main.c 26 KB

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