main.c 31 KB

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