main.c 27 KB

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