main.c 27 KB

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