main.c 31 KB

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