main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. sending_allowed =
  487. (corosync_quorum_is_quorate() == 1 ||
  488. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  489. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  490. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  491. (pd->reserved_msgs) &&
  492. (sync_in_process == 0)));
  493. return (sending_allowed);
  494. }
  495. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  496. {
  497. struct sending_allowed_private_data_struct *pd =
  498. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  499. totempg_groups_joined_release (pd->reserved_msgs);
  500. }
  501. static int ipc_subsys_id = -1;
  502. static void ipc_log_printf (const char *format, ...) __attribute__((format(printf, 1, 2)));
  503. static void ipc_log_printf (const char *format, ...) {
  504. va_list ap;
  505. va_start (ap, format);
  506. _logsys_log_vprintf (
  507. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  508. ipc_subsys_id,
  509. LOGSYS_RECID_LOG),
  510. __FUNCTION__, __FILE__, __LINE__,
  511. format, ap);
  512. va_end (ap);
  513. }
  514. static void ipc_fatal_error(const char *error_msg) {
  515. _logsys_log_printf (
  516. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  517. ipc_subsys_id,
  518. LOGSYS_RECID_LOG),
  519. __FUNCTION__, __FILE__, __LINE__,
  520. "%s", error_msg);
  521. exit(EXIT_FAILURE);
  522. }
  523. static int corosync_poll_handler_accept (
  524. hdb_handle_t handle,
  525. int fd,
  526. int revent,
  527. void *context)
  528. {
  529. return (coroipcs_handler_accept (fd, revent, context));
  530. }
  531. static int corosync_poll_handler_dispatch (
  532. hdb_handle_t handle,
  533. int fd,
  534. int revent,
  535. void *context)
  536. {
  537. return (coroipcs_handler_dispatch (fd, revent, context));
  538. }
  539. static void corosync_poll_accept_add (
  540. int fd)
  541. {
  542. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  543. corosync_poll_handler_accept);
  544. }
  545. static void corosync_poll_dispatch_add (
  546. int fd,
  547. void *context)
  548. {
  549. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  550. corosync_poll_handler_dispatch);
  551. }
  552. static void corosync_poll_dispatch_modify (
  553. int fd,
  554. int events)
  555. {
  556. poll_dispatch_modify (corosync_poll_handle, fd, events,
  557. corosync_poll_handler_dispatch);
  558. }
  559. static struct coroipcs_init_state ipc_init_state = {
  560. .socket_name = COROSYNC_SOCKET_NAME,
  561. .sched_policy = SCHED_OTHER,
  562. .sched_param = &global_sched_param,
  563. .malloc = malloc,
  564. .free = free,
  565. .log_printf = ipc_log_printf,
  566. .fatal_error = ipc_fatal_error,
  567. .security_valid = corosync_security_valid,
  568. .service_available = corosync_service_available,
  569. .private_data_size_get = corosync_private_data_size_get,
  570. .serialize_lock = serialize_lock,
  571. .serialize_unlock = serialize_unlock,
  572. .sending_allowed = corosync_sending_allowed,
  573. .sending_allowed_release = corosync_sending_allowed_release,
  574. .poll_accept_add = corosync_poll_accept_add,
  575. .poll_dispatch_add = corosync_poll_dispatch_add,
  576. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  577. .init_fn_get = corosync_init_fn_get,
  578. .exit_fn_get = corosync_exit_fn_get,
  579. .handler_fn_get = corosync_handler_fn_get
  580. };
  581. static void corosync_setscheduler (void)
  582. {
  583. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  584. int res;
  585. sched_priority = sched_get_priority_max (SCHED_RR);
  586. if (sched_priority != -1) {
  587. global_sched_param.sched_priority = sched_priority;
  588. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  589. if (res == -1) {
  590. global_sched_param.sched_priority = 0;
  591. log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  592. global_sched_param.sched_priority, strerror (errno));
  593. logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  594. } else {
  595. /*
  596. * Turn on SCHED_RR in ipc system
  597. */
  598. ipc_init_state.sched_policy = SCHED_RR;
  599. /*
  600. * Turn on SCHED_RR in logsys system
  601. */
  602. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  603. if (res == -1) {
  604. log_printf (LOGSYS_LEVEL_ERROR,
  605. "Could not set logsys thread priority."
  606. " Can't continue because of priority inversions.");
  607. corosync_exit_error (AIS_DONE_LOGSETUP);
  608. }
  609. }
  610. } else {
  611. log_printf (LOGSYS_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  612. sched_priority = 0;
  613. }
  614. #else
  615. log_printf(LOGSYS_LEVEL_WARNING,
  616. "The Platform is missing process priority setting features. Leaving at default.");
  617. #endif
  618. }
  619. void main_service_ready (void)
  620. {
  621. int res;
  622. /*
  623. * This must occur after totempg is initialized because "this_ip" must be set
  624. */
  625. res = corosync_service_defaults_link_and_init (api);
  626. if (res == -1) {
  627. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  628. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  629. }
  630. evil_init (api);
  631. }
  632. int main (int argc, char **argv)
  633. {
  634. const char *error_string;
  635. struct totem_config totem_config;
  636. hdb_handle_t objdb_handle;
  637. hdb_handle_t config_handle;
  638. unsigned int config_version = 0;
  639. void *objdb_p;
  640. struct config_iface_ver0 *config;
  641. void *config_p;
  642. const char *config_iface_init;
  643. char *config_iface;
  644. char *iface;
  645. int res, ch;
  646. int background, setprio;
  647. struct stat stat_out;
  648. char corosync_lib_dir[PATH_MAX];
  649. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  650. pthread_spin_init (&serialize_spin, 0);
  651. #endif
  652. /* default configuration
  653. */
  654. background = 1;
  655. setprio = 1;
  656. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  657. switch (ch) {
  658. case 'f':
  659. background = 0;
  660. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  661. break;
  662. case 'p':
  663. setprio = 0;
  664. break;
  665. case 'v':
  666. printf ("Corosync Cluster Engine, version '%s' SVN revision '%s'\n", VERSION, SVN_REVISION);
  667. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  668. return EXIT_SUCCESS;
  669. break;
  670. default:
  671. fprintf(stderr, \
  672. "usage:\n"\
  673. " -f : Start application in foreground.\n"\
  674. " -p : Do not set process priority. \n"\
  675. " -v : Display version and SVN revision of Corosync and exit.\n");
  676. return EXIT_FAILURE;
  677. }
  678. }
  679. if (background)
  680. corosync_tty_detach ();
  681. /*
  682. * Set round robin realtime scheduling with priority 99
  683. * Lock all memory to avoid page faults which may interrupt
  684. * application healthchecking
  685. */
  686. if (setprio) {
  687. corosync_setscheduler ();
  688. }
  689. corosync_mlockall ();
  690. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  691. (void)signal (SIGINT, sigintr_handler);
  692. (void)signal (SIGUSR2, sigusr2_handler);
  693. (void)signal (SIGSEGV, sigsegv_handler);
  694. (void)signal (SIGABRT, sigabrt_handler);
  695. (void)signal (SIGQUIT, sigquit_handler);
  696. #if MSG_NOSIGNAL == 0
  697. (void)signal (SIGPIPE, SIG_IGN);
  698. #endif
  699. corosync_timer_init (
  700. serialize_lock,
  701. serialize_unlock,
  702. sched_priority);
  703. corosync_poll_handle = poll_create ();
  704. /*
  705. * Load the object database interface
  706. */
  707. res = lcr_ifact_reference (
  708. &objdb_handle,
  709. "objdb",
  710. 0,
  711. &objdb_p,
  712. 0);
  713. if (res == -1) {
  714. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  715. corosync_exit_error (AIS_DONE_OBJDB);
  716. }
  717. objdb = (struct objdb_iface_ver0 *)objdb_p;
  718. objdb->objdb_init ();
  719. /*
  720. * Initialize the corosync_api_v1 definition
  721. */
  722. apidef_init (objdb);
  723. api = apidef_get ();
  724. num_config_modules = 0;
  725. /*
  726. * Bootstrap in the default configuration parser or use
  727. * the corosync default built in parser if the configuration parser
  728. * isn't overridden
  729. */
  730. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  731. if (!config_iface_init) {
  732. config_iface_init = "corosync_parser";
  733. }
  734. /* Make a copy so we can deface it with strtok */
  735. if ((config_iface = strdup(config_iface_init)) == NULL) {
  736. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  737. corosync_exit_error (AIS_DONE_OBJDB);
  738. }
  739. iface = strtok(config_iface, ":");
  740. while (iface)
  741. {
  742. res = lcr_ifact_reference (
  743. &config_handle,
  744. iface,
  745. config_version,
  746. &config_p,
  747. 0);
  748. config = (struct config_iface_ver0 *)config_p;
  749. if (res == -1) {
  750. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  751. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  752. }
  753. res = config->config_readconfig(objdb, &error_string);
  754. if (res == -1) {
  755. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  756. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  757. }
  758. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  759. config_modules[num_config_modules++] = config;
  760. iface = strtok(NULL, ":");
  761. }
  762. free(config_iface);
  763. res = corosync_main_config_read (objdb, &error_string);
  764. if (res == -1) {
  765. /*
  766. * if we are here, we _must_ flush the logsys queue
  767. * and try to inform that we couldn't read the config.
  768. * this is a desperate attempt before certain death
  769. * and there is no guarantee that we can print to stderr
  770. * nor that logsys is sending the messages where we expect.
  771. */
  772. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  773. fprintf(stderr, "%s", error_string);
  774. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  775. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  776. }
  777. logsys_fork_completed();
  778. /*
  779. * Make sure required directory is present
  780. */
  781. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  782. res = stat (corosync_lib_dir, &stat_out);
  783. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  784. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  785. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  786. }
  787. res = totem_config_read (objdb, &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. res = totem_config_keyread (objdb, &totem_config, &error_string);
  793. if (res == -1) {
  794. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  795. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  796. }
  797. res = totem_config_validate (&totem_config, &error_string);
  798. if (res == -1) {
  799. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  800. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  801. }
  802. totem_config.totem_logging_configuration = totem_logging_configuration;
  803. totem_config.totem_logging_configuration.log_subsys_id =
  804. _logsys_subsys_create ("TOTEM");
  805. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  806. log_printf (LOGSYS_LEVEL_ERROR,
  807. "Unable to initialize TOTEM logging subsystem\n");
  808. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  809. }
  810. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  811. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  812. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  813. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  814. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  815. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  816. res = corosync_main_config_compatibility_read (objdb,
  817. &minimum_sync_mode,
  818. &error_string);
  819. if (res == -1) {
  820. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  821. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  822. }
  823. res = corosync_main_config_compatibility_read (objdb,
  824. &minimum_sync_mode,
  825. &error_string);
  826. if (res == -1) {
  827. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  828. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  829. }
  830. /*
  831. * Sleep for a while to let other nodes in the cluster
  832. * understand that this node has been away (if it was
  833. * an corosync restart).
  834. */
  835. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  836. /*
  837. * if totempg_initialize doesn't have root priveleges, it cannot
  838. * bind to a specific interface. This only matters if
  839. * there is more then one interface in a system, so
  840. * in this case, only a warning is printed
  841. */
  842. /*
  843. * Join multicast group and setup delivery
  844. * and configuration change functions
  845. */
  846. totempg_initialize (
  847. corosync_poll_handle,
  848. &totem_config);
  849. totempg_service_ready_register (
  850. main_service_ready);
  851. totempg_groups_initialize (
  852. &corosync_group_handle,
  853. deliver_fn,
  854. confchg_fn);
  855. totempg_groups_join (
  856. corosync_group_handle,
  857. &corosync_group,
  858. 1);
  859. if (minimum_sync_mode == CS_SYNC_V2) {
  860. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  861. sync_v2_init (
  862. corosync_sync_v2_callbacks_retrieve,
  863. corosync_sync_completed);
  864. } else
  865. if (minimum_sync_mode == CS_SYNC_V1) {
  866. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  867. sync_register (
  868. corosync_sync_callbacks_retrieve,
  869. sync_v2_memb_list_determine,
  870. sync_v2_memb_list_abort,
  871. sync_v2_start);
  872. sync_v2_init (
  873. corosync_sync_v2_callbacks_retrieve,
  874. corosync_sync_completed);
  875. }
  876. /*
  877. * Drop root privleges to user 'ais'
  878. * TODO: Don't really need full root capabilities;
  879. * needed capabilities are:
  880. * CAP_NET_RAW (bindtodevice)
  881. * CAP_SYS_NICE (setscheduler)
  882. * CAP_IPC_LOCK (mlockall)
  883. */
  884. priv_drop ();
  885. schedwrk_init (
  886. serialize_lock,
  887. serialize_unlock);
  888. ipc_subsys_id = _logsys_subsys_create ("IPC");
  889. if (ipc_subsys_id < 0) {
  890. log_printf (LOGSYS_LEVEL_ERROR,
  891. "Could not initialize IPC logging subsystem\n");
  892. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  893. }
  894. coroipcs_ipc_init (&ipc_init_state);
  895. /*
  896. * Start main processing loop
  897. */
  898. poll_run (corosync_poll_handle);
  899. return EXIT_SUCCESS;
  900. }