main.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. unsigned int key_incr_dummy;
  379. header = msg;
  380. if (endian_conversion_required) {
  381. id = swab32 (header->id);
  382. size = swab32 (header->size);
  383. } else {
  384. id = header->id;
  385. size = header->size;
  386. }
  387. /*
  388. * Call the proper executive handler
  389. */
  390. service = id >> 16;
  391. fn_id = id & 0xffff;
  392. serialize_lock();
  393. if (ais_service[service] == NULL && service == EVT_SERVICE) {
  394. evil_deliver_fn (nodeid, service, fn_id, msg,
  395. endian_conversion_required);
  396. }
  397. if (!ais_service[service]) {
  398. serialize_unlock();
  399. return;
  400. }
  401. objdb->object_key_increment (service_stats_handle[service][fn_id],
  402. "rx", strlen("rx"),
  403. &key_incr_dummy);
  404. if (endian_conversion_required) {
  405. assert(ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn != NULL);
  406. ais_service[service]->exec_engine[fn_id].exec_endian_convert_fn
  407. ((void *)msg);
  408. }
  409. ais_service[service]->exec_engine[fn_id].exec_handler_fn
  410. (msg, nodeid);
  411. serialize_unlock();
  412. }
  413. void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
  414. {
  415. *modules = config_modules;
  416. *num = num_config_modules;
  417. }
  418. int main_mcast (
  419. const struct iovec *iovec,
  420. unsigned int iov_len,
  421. unsigned int guarantee)
  422. {
  423. const coroipc_request_header_t *req = iovec->iov_base;
  424. int service;
  425. int fn_id;
  426. unsigned int key_incr_dummy;
  427. service = req->id >> 16;
  428. fn_id = req->id & 0xffff;
  429. if (ais_service[service]) {
  430. objdb->object_key_increment (service_stats_handle[service][fn_id],
  431. "tx", strlen("tx"),
  432. &key_incr_dummy);
  433. }
  434. return (totempg_groups_mcast_joined (corosync_group_handle, iovec, iov_len, guarantee));
  435. }
  436. int message_source_is_local (const mar_message_source_t *source)
  437. {
  438. int ret = 0;
  439. assert (source != NULL);
  440. if (source->nodeid == totempg_my_nodeid_get ()) {
  441. ret = 1;
  442. }
  443. return ret;
  444. }
  445. void message_source_set (
  446. mar_message_source_t *source,
  447. void *conn)
  448. {
  449. assert ((source != NULL) && (conn != NULL));
  450. memset (source, 0, sizeof (mar_message_source_t));
  451. source->nodeid = totempg_my_nodeid_get ();
  452. source->conn = conn;
  453. }
  454. /*
  455. * Provides the glue from corosync to the IPC Service
  456. */
  457. static int corosync_private_data_size_get (unsigned int service)
  458. {
  459. return (ais_service[service]->private_data_size);
  460. }
  461. static coroipcs_init_fn_lvalue corosync_init_fn_get (unsigned int service)
  462. {
  463. return (ais_service[service]->lib_init_fn);
  464. }
  465. static coroipcs_exit_fn_lvalue corosync_exit_fn_get (unsigned int service)
  466. {
  467. return (ais_service[service]->lib_exit_fn);
  468. }
  469. static coroipcs_handler_fn_lvalue corosync_handler_fn_get (unsigned int service, unsigned int id)
  470. {
  471. return (ais_service[service]->lib_engine[id].lib_handler_fn);
  472. }
  473. static int corosync_security_valid (int euid, int egid)
  474. {
  475. struct list_head *iter;
  476. if (euid == 0 || egid == 0) {
  477. return (1);
  478. }
  479. for (iter = uidgid_list_head.next; iter != &uidgid_list_head;
  480. iter = iter->next) {
  481. struct uidgid_item *ugi = list_entry (iter, struct uidgid_item,
  482. list);
  483. if (euid == ugi->uid || egid == ugi->gid)
  484. return (1);
  485. }
  486. return (0);
  487. }
  488. static int corosync_service_available (unsigned int service)
  489. {
  490. return (ais_service[service] != NULL);
  491. }
  492. struct sending_allowed_private_data_struct {
  493. int reserved_msgs;
  494. };
  495. static int corosync_sending_allowed (
  496. unsigned int service,
  497. unsigned int id,
  498. const void *msg,
  499. void *sending_allowed_private_data)
  500. {
  501. struct sending_allowed_private_data_struct *pd =
  502. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  503. struct iovec reserve_iovec;
  504. coroipc_request_header_t *header = (coroipc_request_header_t *)msg;
  505. int sending_allowed;
  506. reserve_iovec.iov_base = (char *)header;
  507. reserve_iovec.iov_len = header->size;
  508. pd->reserved_msgs = totempg_groups_joined_reserve (
  509. corosync_group_handle,
  510. &reserve_iovec, 1);
  511. if (pd->reserved_msgs == -1) {
  512. return (-1);
  513. }
  514. sending_allowed =
  515. (corosync_quorum_is_quorate() == 1 ||
  516. ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) &&
  517. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  518. ((ais_service[service]->lib_engine[id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  519. (pd->reserved_msgs) &&
  520. (sync_in_process == 0)));
  521. return (sending_allowed);
  522. }
  523. static void corosync_sending_allowed_release (void *sending_allowed_private_data)
  524. {
  525. struct sending_allowed_private_data_struct *pd =
  526. (struct sending_allowed_private_data_struct *)sending_allowed_private_data;
  527. if (pd->reserved_msgs == -1) {
  528. return;
  529. }
  530. totempg_groups_joined_release (pd->reserved_msgs);
  531. }
  532. static int ipc_subsys_id = -1;
  533. static void ipc_log_printf (const char *format, ...) __attribute__((format(printf, 1, 2)));
  534. static void ipc_log_printf (const char *format, ...) {
  535. va_list ap;
  536. va_start (ap, format);
  537. _logsys_log_vprintf (
  538. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  539. ipc_subsys_id,
  540. LOGSYS_RECID_LOG),
  541. __FUNCTION__, __FILE__, __LINE__,
  542. format, ap);
  543. va_end (ap);
  544. }
  545. static void ipc_fatal_error(const char *error_msg) {
  546. _logsys_log_printf (
  547. LOGSYS_ENCODE_RECID(LOGSYS_LEVEL_ERROR,
  548. ipc_subsys_id,
  549. LOGSYS_RECID_LOG),
  550. __FUNCTION__, __FILE__, __LINE__,
  551. "%s", error_msg);
  552. exit(EXIT_FAILURE);
  553. }
  554. static int corosync_poll_handler_accept (
  555. hdb_handle_t handle,
  556. int fd,
  557. int revent,
  558. void *context)
  559. {
  560. return (coroipcs_handler_accept (fd, revent, context));
  561. }
  562. static int corosync_poll_handler_dispatch (
  563. hdb_handle_t handle,
  564. int fd,
  565. int revent,
  566. void *context)
  567. {
  568. return (coroipcs_handler_dispatch (fd, revent, context));
  569. }
  570. static void corosync_poll_accept_add (
  571. int fd)
  572. {
  573. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, 0,
  574. corosync_poll_handler_accept);
  575. }
  576. static void corosync_poll_dispatch_add (
  577. int fd,
  578. void *context)
  579. {
  580. poll_dispatch_add (corosync_poll_handle, fd, POLLIN|POLLNVAL, context,
  581. corosync_poll_handler_dispatch);
  582. }
  583. static void corosync_poll_dispatch_modify (
  584. int fd,
  585. int events)
  586. {
  587. poll_dispatch_modify (corosync_poll_handle, fd, events,
  588. corosync_poll_handler_dispatch);
  589. }
  590. static hdb_handle_t corosync_stats_create_connection (const char* name,
  591. const pid_t pid, const int fd)
  592. {
  593. uint32_t zero_32 = 0;
  594. uint64_t zero_64 = 0;
  595. unsigned int key_incr_dummy;
  596. hdb_handle_t object_handle;
  597. objdb->object_key_increment (object_connection_handle,
  598. "active", strlen("active"),
  599. &key_incr_dummy);
  600. objdb->object_create (object_connection_handle,
  601. &object_handle,
  602. name,
  603. strlen (name));
  604. objdb->object_key_create_typed (object_handle,
  605. "service_id",
  606. &zero_32, sizeof (zero_32),
  607. OBJDB_VALUETYPE_UINT32);
  608. objdb->object_key_create_typed (object_handle,
  609. "client_pid",
  610. &pid, sizeof (pid),
  611. OBJDB_VALUETYPE_INT32);
  612. objdb->object_key_create_typed (object_handle,
  613. "responses",
  614. &zero_64, sizeof (zero_64),
  615. OBJDB_VALUETYPE_UINT64);
  616. objdb->object_key_create_typed (object_handle,
  617. "dispatched",
  618. &zero_64, sizeof (zero_64),
  619. OBJDB_VALUETYPE_UINT64);
  620. objdb->object_key_create_typed (object_handle,
  621. "requests",
  622. &zero_64, sizeof (zero_64),
  623. OBJDB_VALUETYPE_INT64);
  624. objdb->object_key_create_typed (object_handle,
  625. "sem_retry_count",
  626. &zero_64, sizeof (zero_64),
  627. OBJDB_VALUETYPE_UINT64);
  628. objdb->object_key_create_typed (object_handle,
  629. "send_retry_count",
  630. &zero_64, sizeof (zero_64),
  631. OBJDB_VALUETYPE_UINT64);
  632. objdb->object_key_create_typed (object_handle,
  633. "recv_retry_count",
  634. &zero_64, sizeof (zero_64),
  635. OBJDB_VALUETYPE_UINT64);
  636. return object_handle;
  637. }
  638. static void corosync_stats_destroy_connection (hdb_handle_t handle)
  639. {
  640. unsigned int key_incr_dummy;
  641. objdb->object_destroy (handle);
  642. objdb->object_key_increment (object_connection_handle,
  643. "closed", strlen("closed"),
  644. &key_incr_dummy);
  645. objdb->object_key_decrement (object_connection_handle,
  646. "active", strlen("active"),
  647. &key_incr_dummy);
  648. }
  649. static void corosync_stats_update_value (hdb_handle_t handle,
  650. const char *name, const void *value,
  651. const size_t value_len)
  652. {
  653. objdb->object_key_replace (handle,
  654. name, strlen(name),
  655. value, value_len);
  656. }
  657. static void corosync_stats_increment_value (hdb_handle_t handle,
  658. const char* name)
  659. {
  660. unsigned int key_incr_dummy;
  661. objdb->object_key_increment (handle,
  662. name, strlen(name),
  663. &key_incr_dummy);
  664. }
  665. static struct coroipcs_init_state ipc_init_state = {
  666. .socket_name = COROSYNC_SOCKET_NAME,
  667. .sched_policy = SCHED_OTHER,
  668. .sched_param = &global_sched_param,
  669. .malloc = malloc,
  670. .free = free,
  671. .log_printf = ipc_log_printf,
  672. .fatal_error = ipc_fatal_error,
  673. .security_valid = corosync_security_valid,
  674. .service_available = corosync_service_available,
  675. .private_data_size_get = corosync_private_data_size_get,
  676. .serialize_lock = serialize_lock,
  677. .serialize_unlock = serialize_unlock,
  678. .sending_allowed = corosync_sending_allowed,
  679. .sending_allowed_release = corosync_sending_allowed_release,
  680. .poll_accept_add = corosync_poll_accept_add,
  681. .poll_dispatch_add = corosync_poll_dispatch_add,
  682. .poll_dispatch_modify = corosync_poll_dispatch_modify,
  683. .init_fn_get = corosync_init_fn_get,
  684. .exit_fn_get = corosync_exit_fn_get,
  685. .handler_fn_get = corosync_handler_fn_get
  686. };
  687. static struct coroipcs_init_stats_state ipc_init_stats_state = {
  688. .stats_create_connection = corosync_stats_create_connection,
  689. .stats_destroy_connection = corosync_stats_destroy_connection,
  690. .stats_update_value = corosync_stats_update_value,
  691. .stats_increment_value = corosync_stats_increment_value
  692. };
  693. static void corosync_setscheduler (void)
  694. {
  695. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX) && defined(HAVE_SCHED_SETSCHEDULER)
  696. int res;
  697. sched_priority = sched_get_priority_max (SCHED_RR);
  698. if (sched_priority != -1) {
  699. global_sched_param.sched_priority = sched_priority;
  700. res = sched_setscheduler (0, SCHED_RR, &global_sched_param);
  701. if (res == -1) {
  702. global_sched_param.sched_priority = 0;
  703. log_printf (LOGSYS_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  704. global_sched_param.sched_priority, strerror (errno));
  705. logsys_thread_priority_set (SCHED_OTHER, NULL, 1);
  706. } else {
  707. /*
  708. * Turn on SCHED_RR in ipc system
  709. */
  710. ipc_init_state.sched_policy = SCHED_RR;
  711. /*
  712. * Turn on SCHED_RR in logsys system
  713. */
  714. res = logsys_thread_priority_set (SCHED_RR, &global_sched_param, 10);
  715. if (res == -1) {
  716. log_printf (LOGSYS_LEVEL_ERROR,
  717. "Could not set logsys thread priority."
  718. " Can't continue because of priority inversions.");
  719. corosync_exit_error (AIS_DONE_LOGSETUP);
  720. }
  721. }
  722. } else {
  723. log_printf (LOGSYS_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  724. sched_priority = 0;
  725. }
  726. #else
  727. log_printf(LOGSYS_LEVEL_WARNING,
  728. "The Platform is missing process priority setting features. Leaving at default.");
  729. #endif
  730. }
  731. static void corosync_stats_init (void)
  732. {
  733. hdb_handle_t object_find_handle;
  734. hdb_handle_t object_runtime_handle;
  735. uint64_t zero_64 = 0;
  736. objdb->object_find_create (OBJECT_PARENT_HANDLE,
  737. "runtime", strlen ("runtime"),
  738. &object_find_handle);
  739. if (objdb->object_find_next (object_find_handle,
  740. &object_runtime_handle) != 0) {
  741. return;
  742. }
  743. /* Connection objects */
  744. objdb->object_create (object_runtime_handle,
  745. &object_connection_handle,
  746. "connections", strlen ("connections"));
  747. objdb->object_key_create_typed (object_connection_handle,
  748. "active", &zero_64, sizeof (zero_64),
  749. OBJDB_VALUETYPE_UINT64);
  750. objdb->object_key_create_typed (object_connection_handle,
  751. "closed", &zero_64, sizeof (zero_64),
  752. OBJDB_VALUETYPE_UINT64);
  753. }
  754. static void main_service_ready (void)
  755. {
  756. int res;
  757. /*
  758. * This must occur after totempg is initialized because "this_ip" must be set
  759. */
  760. res = corosync_service_defaults_link_and_init (api);
  761. if (res == -1) {
  762. log_printf (LOGSYS_LEVEL_ERROR, "Could not initialize default services\n");
  763. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  764. }
  765. evil_init (api);
  766. corosync_stats_init ();
  767. }
  768. int main (int argc, char **argv)
  769. {
  770. const char *error_string;
  771. struct totem_config totem_config;
  772. hdb_handle_t objdb_handle;
  773. hdb_handle_t config_handle;
  774. unsigned int config_version = 0;
  775. void *objdb_p;
  776. struct config_iface_ver0 *config;
  777. void *config_p;
  778. const char *config_iface_init;
  779. char *config_iface;
  780. char *iface;
  781. int res, ch;
  782. int background, setprio;
  783. struct stat stat_out;
  784. char corosync_lib_dir[PATH_MAX];
  785. hdb_handle_t object_runtime_handle;
  786. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  787. pthread_spin_init (&serialize_spin, 0);
  788. #endif
  789. /* default configuration
  790. */
  791. background = 1;
  792. setprio = 1;
  793. while ((ch = getopt (argc, argv, "fpv")) != EOF) {
  794. switch (ch) {
  795. case 'f':
  796. background = 0;
  797. logsys_config_mode_set (NULL, LOGSYS_MODE_OUTPUT_STDERR|LOGSYS_MODE_THREADED|LOGSYS_MODE_FORK);
  798. break;
  799. case 'p':
  800. setprio = 0;
  801. break;
  802. case 'v':
  803. printf ("Corosync Cluster Engine, version '%s' SVN revision '%s'\n", VERSION, SVN_REVISION);
  804. printf ("Copyright (c) 2006-2009 Red Hat, Inc.\n");
  805. return EXIT_SUCCESS;
  806. break;
  807. default:
  808. fprintf(stderr, \
  809. "usage:\n"\
  810. " -f : Start application in foreground.\n"\
  811. " -p : Do not set process priority. \n"\
  812. " -v : Display version and SVN revision of Corosync and exit.\n");
  813. return EXIT_FAILURE;
  814. }
  815. }
  816. /*
  817. * Set round robin realtime scheduling with priority 99
  818. * Lock all memory to avoid page faults which may interrupt
  819. * application healthchecking
  820. */
  821. if (setprio) {
  822. corosync_setscheduler ();
  823. }
  824. corosync_mlockall ();
  825. log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
  826. log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "\n");
  827. (void)signal (SIGINT, sigintr_handler);
  828. (void)signal (SIGUSR2, sigusr2_handler);
  829. (void)signal (SIGSEGV, sigsegv_handler);
  830. (void)signal (SIGABRT, sigabrt_handler);
  831. (void)signal (SIGQUIT, sigquit_handler);
  832. (void)signal (SIGTERM, sigterm_handler);
  833. #if MSG_NOSIGNAL != 0
  834. (void)signal (SIGPIPE, SIG_IGN);
  835. #endif
  836. corosync_timer_init (
  837. serialize_lock,
  838. serialize_unlock,
  839. sched_priority);
  840. corosync_poll_handle = poll_create ();
  841. /*
  842. * Load the object database interface
  843. */
  844. res = lcr_ifact_reference (
  845. &objdb_handle,
  846. "objdb",
  847. 0,
  848. &objdb_p,
  849. 0);
  850. if (res == -1) {
  851. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration object database component.\n");
  852. corosync_exit_error (AIS_DONE_OBJDB);
  853. }
  854. objdb = (struct objdb_iface_ver0 *)objdb_p;
  855. objdb->objdb_init ();
  856. /*
  857. * Initialize the corosync_api_v1 definition
  858. */
  859. apidef_init (objdb);
  860. api = apidef_get ();
  861. num_config_modules = 0;
  862. /*
  863. * Bootstrap in the default configuration parser or use
  864. * the corosync default built in parser if the configuration parser
  865. * isn't overridden
  866. */
  867. config_iface_init = getenv("COROSYNC_DEFAULT_CONFIG_IFACE");
  868. if (!config_iface_init) {
  869. config_iface_init = "corosync_parser";
  870. }
  871. /* Make a copy so we can deface it with strtok */
  872. if ((config_iface = strdup(config_iface_init)) == NULL) {
  873. log_printf (LOGSYS_LEVEL_ERROR, "exhausted virtual memory");
  874. corosync_exit_error (AIS_DONE_OBJDB);
  875. }
  876. iface = strtok(config_iface, ":");
  877. while (iface)
  878. {
  879. res = lcr_ifact_reference (
  880. &config_handle,
  881. iface,
  882. config_version,
  883. &config_p,
  884. 0);
  885. config = (struct config_iface_ver0 *)config_p;
  886. if (res == -1) {
  887. log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't open configuration component '%s'\n", iface);
  888. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  889. }
  890. res = config->config_readconfig(objdb, &error_string);
  891. if (res == -1) {
  892. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  893. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  894. }
  895. log_printf (LOGSYS_LEVEL_NOTICE, "%s", error_string);
  896. config_modules[num_config_modules++] = config;
  897. iface = strtok(NULL, ":");
  898. }
  899. free(config_iface);
  900. res = corosync_main_config_read (objdb, &error_string);
  901. if (res == -1) {
  902. /*
  903. * if we are here, we _must_ flush the logsys queue
  904. * and try to inform that we couldn't read the config.
  905. * this is a desperate attempt before certain death
  906. * and there is no guarantee that we can print to stderr
  907. * nor that logsys is sending the messages where we expect.
  908. */
  909. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  910. fprintf(stderr, "%s", error_string);
  911. syslog (LOGSYS_LEVEL_ERROR, "%s", error_string);
  912. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  913. }
  914. /*
  915. * Make sure required directory is present
  916. */
  917. sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
  918. res = stat (corosync_lib_dir, &stat_out);
  919. if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  920. log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.\n", corosync_lib_dir);
  921. corosync_exit_error (AIS_DONE_DIR_NOT_PRESENT);
  922. }
  923. res = totem_config_read (objdb, &totem_config, &error_string);
  924. if (res == -1) {
  925. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  926. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  927. }
  928. res = totem_config_keyread (objdb, &totem_config, &error_string);
  929. if (res == -1) {
  930. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  931. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  932. }
  933. res = totem_config_validate (&totem_config, &error_string);
  934. if (res == -1) {
  935. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  936. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  937. }
  938. totem_config.totem_logging_configuration = totem_logging_configuration;
  939. totem_config.totem_logging_configuration.log_subsys_id =
  940. _logsys_subsys_create ("TOTEM");
  941. if (totem_config.totem_logging_configuration.log_subsys_id < 0) {
  942. log_printf (LOGSYS_LEVEL_ERROR,
  943. "Unable to initialize TOTEM logging subsystem\n");
  944. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  945. }
  946. totem_config.totem_logging_configuration.log_level_security = LOGSYS_LEVEL_WARNING;
  947. totem_config.totem_logging_configuration.log_level_error = LOGSYS_LEVEL_ERROR;
  948. totem_config.totem_logging_configuration.log_level_warning = LOGSYS_LEVEL_WARNING;
  949. totem_config.totem_logging_configuration.log_level_notice = LOGSYS_LEVEL_NOTICE;
  950. totem_config.totem_logging_configuration.log_level_debug = LOGSYS_LEVEL_DEBUG;
  951. totem_config.totem_logging_configuration.log_printf = _logsys_log_printf;
  952. res = corosync_main_config_compatibility_read (objdb,
  953. &minimum_sync_mode,
  954. &error_string);
  955. if (res == -1) {
  956. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  957. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  958. }
  959. res = corosync_main_config_compatibility_read (objdb,
  960. &minimum_sync_mode,
  961. &error_string);
  962. if (res == -1) {
  963. log_printf (LOGSYS_LEVEL_ERROR, "%s", error_string);
  964. corosync_exit_error (AIS_DONE_MAINCONFIGREAD);
  965. }
  966. /* create the main runtime object */
  967. objdb->object_create (OBJECT_PARENT_HANDLE,
  968. &object_runtime_handle,
  969. "runtime", strlen ("runtime"));
  970. /*
  971. * Now we are fully initialized.
  972. */
  973. if (background) {
  974. corosync_tty_detach ();
  975. }
  976. logsys_fork_completed();
  977. /*
  978. * Sleep for a while to let other nodes in the cluster
  979. * understand that this node has been away (if it was
  980. * an corosync restart).
  981. */
  982. // TODO what is this hack for? usleep(totem_config.token_timeout * 2000);
  983. /*
  984. * if totempg_initialize doesn't have root priveleges, it cannot
  985. * bind to a specific interface. This only matters if
  986. * there is more then one interface in a system, so
  987. * in this case, only a warning is printed
  988. */
  989. /*
  990. * Join multicast group and setup delivery
  991. * and configuration change functions
  992. */
  993. totempg_initialize (
  994. corosync_poll_handle,
  995. &totem_config);
  996. totempg_service_ready_register (
  997. main_service_ready);
  998. totempg_groups_initialize (
  999. &corosync_group_handle,
  1000. deliver_fn,
  1001. confchg_fn);
  1002. totempg_groups_join (
  1003. corosync_group_handle,
  1004. &corosync_group,
  1005. 1);
  1006. if (minimum_sync_mode == CS_SYNC_V2) {
  1007. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to none. Using V2 of the synchronization engine.\n");
  1008. sync_v2_init (
  1009. corosync_sync_v2_callbacks_retrieve,
  1010. corosync_sync_completed);
  1011. } else
  1012. if (minimum_sync_mode == CS_SYNC_V1) {
  1013. log_printf (LOGSYS_LEVEL_NOTICE, "Compatibility mode set to whitetank. Using V1 and V2 of the synchronization engine.\n");
  1014. sync_register (
  1015. corosync_sync_callbacks_retrieve,
  1016. sync_v2_memb_list_determine,
  1017. sync_v2_memb_list_abort,
  1018. sync_v2_start);
  1019. sync_v2_init (
  1020. corosync_sync_v2_callbacks_retrieve,
  1021. corosync_sync_completed);
  1022. }
  1023. /*
  1024. * Drop root privleges to user 'ais'
  1025. * TODO: Don't really need full root capabilities;
  1026. * needed capabilities are:
  1027. * CAP_NET_RAW (bindtodevice)
  1028. * CAP_SYS_NICE (setscheduler)
  1029. * CAP_IPC_LOCK (mlockall)
  1030. */
  1031. priv_drop ();
  1032. schedwrk_init (
  1033. serialize_lock,
  1034. serialize_unlock);
  1035. ipc_subsys_id = _logsys_subsys_create ("IPC");
  1036. if (ipc_subsys_id < 0) {
  1037. log_printf (LOGSYS_LEVEL_ERROR,
  1038. "Could not initialize IPC logging subsystem\n");
  1039. corosync_exit_error (AIS_DONE_INIT_SERVICES);
  1040. }
  1041. coroipcs_ipc_init (&ipc_init_state);
  1042. coroipcs_ipc_stats_init (&ipc_init_stats_state);
  1043. /*
  1044. * Start main processing loop
  1045. */
  1046. poll_run (corosync_poll_handle);
  1047. return EXIT_SUCCESS;
  1048. }