main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc..
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <pthread.h>
  37. #include <assert.h>
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #include <sys/types.h>
  41. #include <sys/poll.h>
  42. #include <sys/uio.h>
  43. #include <sys/mman.h>
  44. #include <sys/socket.h>
  45. #include <sys/un.h>
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <signal.h>
  56. #include <sched.h>
  57. #include <time.h>
  58. #include "../include/saAis.h"
  59. #include "../include/list.h"
  60. #include "../include/queue.h"
  61. #include "../lcr/lcr_ifact.h"
  62. #include "poll.h"
  63. #include "totempg.h"
  64. #include "totemsrp.h"
  65. #include "mempool.h"
  66. #include "mainconfig.h"
  67. #include "totemconfig.h"
  68. #include "main.h"
  69. #include "service.h"
  70. #include "sync.h"
  71. #include "swab.h"
  72. #include "objdb.h"
  73. #include "config.h"
  74. #include "ipc.h"
  75. #include "timer.h"
  76. #include "print.h"
  77. #include "util.h"
  78. #include "flow.h"
  79. #include "version.h"
  80. #define SERVER_BACKLOG 5
  81. static int ais_uid = 0;
  82. static int gid_valid = 0;
  83. static unsigned int service_count = 32;
  84. static pthread_mutex_t serialize_mutex = PTHREAD_MUTEX_INITIALIZER;
  85. static struct totem_logging_configuration totem_logging_configuration;
  86. static char delivery_data[MESSAGE_SIZE_MAX];
  87. SaClmClusterNodeT *(*main_clm_get_by_nodeid) (unsigned int node_id);
  88. static void sigusr2_handler (int num)
  89. {
  90. int i;
  91. for (i = 0; ais_service[i]; i++) {
  92. if (ais_service[i]->exec_dump_fn) {
  93. ais_service[i]->exec_dump_fn ();
  94. }
  95. }
  96. }
  97. static void sigsegv_handler (int num)
  98. {
  99. log_atsegv ();
  100. }
  101. struct totem_ip_address *this_ip;
  102. struct totem_ip_address this_non_loopback_ip;
  103. #define LOCALHOST_IP inet_addr("127.0.0.1")
  104. totempg_groups_handle openais_group_handle;
  105. struct totempg_group openais_group = {
  106. .group = "a",
  107. .group_len = 1
  108. };
  109. void sigintr_handler (int signum)
  110. {
  111. #ifdef DEBUG_MEMPOOL
  112. int stats_inuse[MEMPOOL_GROUP_SIZE];
  113. int stats_avail[MEMPOOL_GROUP_SIZE];
  114. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  115. int i;
  116. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  117. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  118. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  119. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  120. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  121. }
  122. #endif
  123. totempg_finalize ();
  124. openais_exit_error (AIS_DONE_EXIT);
  125. }
  126. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  127. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  128. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  129. void serialize_mutex_lock (void)
  130. {
  131. pthread_mutex_lock (&serialize_mutex);
  132. }
  133. void serialize_mutex_unlock (void)
  134. {
  135. pthread_mutex_unlock (&serialize_mutex);
  136. }
  137. static void openais_sync_completed (void)
  138. {
  139. }
  140. static int openais_sync_callbacks_retrieve (int sync_id,
  141. struct sync_callbacks *callbacks)
  142. {
  143. if (ais_service[sync_id] == NULL) {
  144. memset (callbacks, 0, sizeof (struct sync_callbacks));
  145. return (-1);
  146. }
  147. callbacks->name = ais_service[sync_id]->name;
  148. callbacks->sync_init = ais_service[sync_id]->sync_init;
  149. callbacks->sync_process = ais_service[sync_id]->sync_process;
  150. callbacks->sync_activate = ais_service[sync_id]->sync_activate;
  151. callbacks->sync_abort = ais_service[sync_id]->sync_abort;
  152. return (0);
  153. }
  154. static struct memb_ring_id aisexec_ring_id;
  155. static void confchg_fn (
  156. enum totem_configuration_type configuration_type,
  157. unsigned int *member_list, int member_list_entries,
  158. unsigned int *left_list, int left_list_entries,
  159. unsigned int *joined_list, int joined_list_entries,
  160. struct memb_ring_id *ring_id)
  161. {
  162. int i;
  163. memcpy (&aisexec_ring_id, ring_id, sizeof (struct memb_ring_id));
  164. if (!totemip_localhost_check(this_ip)) {
  165. totemip_copy(&this_non_loopback_ip, this_ip);
  166. }
  167. /*
  168. * Call configuration change for all services
  169. */
  170. for (i = 0; i < service_count; i++) {
  171. if (ais_service[i] && ais_service[i]->confchg_fn) {
  172. ais_service[i]->confchg_fn (configuration_type,
  173. member_list, member_list_entries,
  174. left_list, left_list_entries,
  175. joined_list, joined_list_entries, ring_id);
  176. }
  177. }
  178. }
  179. static void aisexec_uid_determine (struct main_config *main_config)
  180. {
  181. struct passwd *passwd;
  182. passwd = getpwnam(main_config->user);
  183. if (passwd == 0) {
  184. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", main_config->user);
  185. openais_exit_error (AIS_DONE_UID_DETERMINE);
  186. }
  187. ais_uid = passwd->pw_uid;
  188. }
  189. static void aisexec_gid_determine (struct main_config *main_config)
  190. {
  191. struct group *group;
  192. group = getgrnam (main_config->group);
  193. if (group == 0) {
  194. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", group->gr_name);
  195. openais_exit_error (AIS_DONE_GID_DETERMINE);
  196. }
  197. gid_valid = group->gr_gid;
  198. }
  199. static void aisexec_priv_drop (void)
  200. {
  201. return;
  202. setuid (ais_uid);
  203. setegid (ais_uid);
  204. }
  205. static void aisexec_mempool_init (void)
  206. {
  207. int res;
  208. res = mempool_init (pool_sizes);
  209. if (res == ENOMEM) {
  210. log_printf (LOG_LEVEL_ERROR, "Couldn't allocate memory pools, not enough memory");
  211. openais_exit_error (AIS_DONE_MEMPOOL_INIT);
  212. }
  213. }
  214. static void aisexec_tty_detach (void)
  215. {
  216. /*
  217. * Disconnect from TTY if this is not a debug run
  218. */
  219. switch (fork ()) {
  220. case -1:
  221. openais_exit_error (AIS_DONE_FORK);
  222. break;
  223. case 0:
  224. /*
  225. * child which is disconnected, run this process
  226. */
  227. break;
  228. default:
  229. exit (0);
  230. break;
  231. }
  232. }
  233. static void aisexec_setscheduler (void)
  234. {
  235. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  236. struct sched_param sched_param;
  237. int res;
  238. res = sched_get_priority_max (SCHED_RR);
  239. if (res != -1) {
  240. sched_param.sched_priority = 1;//res;
  241. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  242. if (res == -1) {
  243. log_printf (LOG_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  244. sched_param.sched_priority, strerror (errno));
  245. }
  246. } else
  247. log_printf (LOG_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  248. #else
  249. log_printf(LOG_LEVEL_WARNING, "Scheduler priority left to default value (no OS support)\n");
  250. #endif
  251. }
  252. static void aisexec_mlockall (void)
  253. {
  254. #if !defined(OPENAIS_BSD)
  255. int res;
  256. #endif
  257. struct rlimit rlimit;
  258. rlimit.rlim_cur = RLIM_INFINITY;
  259. rlimit.rlim_max = RLIM_INFINITY;
  260. #ifndef OPENAIS_SOLARIS
  261. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  262. #else
  263. setrlimit (RLIMIT_VMEM, &rlimit);
  264. #endif
  265. #if defined(OPENAIS_BSD)
  266. /* under FreeBSD a process with locked page cannot call dlopen
  267. * code disabled until FreeBSD bug i386/93396 was solved
  268. */
  269. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults\n");
  270. #else
  271. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  272. if (res == -1) {
  273. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  274. };
  275. #endif
  276. }
  277. static void deliver_fn (
  278. unsigned int nodeid,
  279. struct iovec *iovec,
  280. int iov_len,
  281. int endian_conversion_required)
  282. {
  283. mar_req_header_t *header;
  284. int pos = 0;
  285. int i;
  286. int service;
  287. int fn_id;
  288. /*
  289. * Build buffer without iovecs to make processing easier
  290. * This is only used for messages which are multicast with iovecs
  291. * and self-delivered. All other mechanisms avoid the copy.
  292. */
  293. if (iov_len > 1) {
  294. for (i = 0; i < iov_len; i++) {
  295. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  296. pos += iovec[i].iov_len;
  297. assert (pos < MESSAGE_SIZE_MAX);
  298. }
  299. header = (mar_req_header_t *)delivery_data;
  300. } else {
  301. header = (mar_req_header_t *)iovec[0].iov_base;
  302. }
  303. if (endian_conversion_required) {
  304. header->id = swab32 (header->id);
  305. header->size = swab32 (header->size);
  306. }
  307. // assert(iovec->iov_len == header->size);
  308. /*
  309. * Call the proper executive handler
  310. */
  311. service = header->id >> 16;
  312. fn_id = header->id & 0xffff;
  313. if (endian_conversion_required) {
  314. assert(ais_service[service]->exec_service[fn_id].exec_endian_convert_fn != NULL);
  315. ais_service[service]->exec_service[fn_id].exec_endian_convert_fn
  316. (header);
  317. }
  318. ais_service[service]->exec_service[fn_id].exec_handler_fn
  319. (header, nodeid);
  320. }
  321. int main (int argc, char **argv)
  322. {
  323. char *error_string;
  324. struct main_config main_config;
  325. struct totem_config totem_config;
  326. unsigned int objdb_handle;
  327. unsigned int config_handle;
  328. unsigned int config_version = 0;
  329. struct objdb_iface_ver0 *objdb;
  330. void *objdb_p;
  331. struct config_iface_ver0 *config;
  332. void *config_p;
  333. char *config_iface;
  334. int res, ch;
  335. int background, setprio;
  336. int totem_log_service;
  337. log_init ("MAIN");
  338. /* default configuration
  339. */
  340. background = 1;
  341. setprio = 1;
  342. while ((ch = getopt (argc, argv, "fp")) != EOF) {
  343. switch (ch) {
  344. case 'f':
  345. background = 0;
  346. break;
  347. case 'p':
  348. setprio = 0;
  349. break;
  350. default:
  351. fprintf(stderr, \
  352. "usage:\n"\
  353. " -f : Start application in foreground.\n"\
  354. " -p : Do not set process priority. \n");
  355. return EXIT_FAILURE;
  356. }
  357. }
  358. if (background)
  359. aisexec_tty_detach ();
  360. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service RELEASE '%s'\n", RELEASE_VERSION);
  361. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2002-2006 MontaVista Software, Inc and contributors.\n");
  362. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2006 Red Hat, Inc.\n");
  363. memset(&this_non_loopback_ip, 0, sizeof(struct totem_ip_address));
  364. totemip_localhost(AF_INET, &this_non_loopback_ip);
  365. signal (SIGINT, sigintr_handler);
  366. signal (SIGUSR2, sigusr2_handler);
  367. signal (SIGSEGV, sigsegv_handler);
  368. openais_timer_init (
  369. serialize_mutex_lock,
  370. serialize_mutex_unlock);
  371. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: started and ready to provide service.\n");
  372. aisexec_poll_handle = poll_create (
  373. serialize_mutex_lock,
  374. serialize_mutex_unlock);
  375. /*
  376. * Load the object database interface
  377. */
  378. res = lcr_ifact_reference (
  379. &objdb_handle,
  380. "objdb",
  381. 0,
  382. &objdb_p,
  383. 0);
  384. if (res == -1) {
  385. log_printf (LOG_LEVEL_ERROR, "AIS Executive couldn't open configuration object database component.\n");
  386. openais_exit_error (AIS_DONE_OBJDB);
  387. }
  388. objdb = (struct objdb_iface_ver0 *)objdb_p;
  389. objdb->objdb_init ();
  390. /* User's bootstrap config service */
  391. config_iface = getenv("OPENAIS_DEFAULT_CONFIG_IFACE");
  392. if (!config_iface) {
  393. config_iface = "aisparser";
  394. }
  395. res = lcr_ifact_reference (
  396. &config_handle,
  397. config_iface,
  398. config_version,
  399. &config_p,
  400. 0);
  401. config = (struct config_iface_ver0 *)config_p;
  402. if (res == -1) {
  403. log_printf (LOG_LEVEL_ERROR, "AIS Executive couldn't open configuration component.\n");
  404. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  405. }
  406. res = config->config_readconfig(objdb, &error_string);
  407. if (res == -1) {
  408. log_printf (LOG_LEVEL_ERROR, error_string);
  409. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  410. }
  411. openais_service_default_objdb_set (objdb);
  412. res = openais_service_link_all (objdb);
  413. if (res == -1) {
  414. log_printf (LOG_LEVEL_ERROR, "Could not load services\n");
  415. openais_exit_error (AIS_DONE_DYNAMICLOAD);
  416. }
  417. res = openais_main_config_read (objdb, &error_string, &main_config);
  418. if (res == -1) {
  419. log_printf (LOG_LEVEL_ERROR, error_string);
  420. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  421. }
  422. res = totem_config_read (objdb, &totem_config, &error_string);
  423. if (res == -1) {
  424. log_printf (LOG_LEVEL_ERROR, error_string);
  425. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  426. }
  427. res = totem_config_keyread (objdb, &totem_config, &error_string);
  428. if (res == -1) {
  429. log_printf (LOG_LEVEL_ERROR, error_string);
  430. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  431. }
  432. res = totem_config_validate (&totem_config, &error_string);
  433. if (res == -1) {
  434. log_printf (LOG_LEVEL_ERROR, error_string);
  435. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  436. }
  437. res = log_setup (&error_string, &main_config);
  438. if (res == -1) {
  439. log_printf (LOG_LEVEL_ERROR, error_string);
  440. openais_exit_error (AIS_DONE_LOGSETUP);
  441. }
  442. aisexec_uid_determine (&main_config);
  443. aisexec_gid_determine (&main_config);
  444. /*
  445. * Set round robin realtime scheduling with priority 99
  446. * Lock all memory to avoid page faults which may interrupt
  447. * application healthchecking
  448. */
  449. if (setprio)
  450. aisexec_setscheduler ();
  451. aisexec_mlockall ();
  452. totem_config.totem_logging_configuration = totem_logging_configuration;
  453. totem_log_service = _log_init ("TOTEM");
  454. totem_config.totem_logging_configuration.log_level_security = mkpri (LOG_LEVEL_SECURITY, totem_log_service);
  455. totem_config.totem_logging_configuration.log_level_error = mkpri (LOG_LEVEL_ERROR, totem_log_service);
  456. totem_config.totem_logging_configuration.log_level_warning = mkpri (LOG_LEVEL_WARNING, totem_log_service);
  457. totem_config.totem_logging_configuration.log_level_notice = mkpri (LOG_LEVEL_NOTICE, totem_log_service);
  458. totem_config.totem_logging_configuration.log_level_debug = mkpri (LOG_LEVEL_DEBUG, totem_log_service);
  459. totem_config.totem_logging_configuration.log_printf = internal_log_printf;
  460. /*
  461. * Sleep for a while to let other nodes in the cluster
  462. * understand that this node has been away (if it was
  463. * an aisexec restart).
  464. */
  465. usleep(totem_config.token_timeout * 2000);
  466. /*
  467. * if totempg_initialize doesn't have root priveleges, it cannot
  468. * bind to a specific interface. This only matters if
  469. * there is more then one interface in a system, so
  470. * in this case, only a warning is printed
  471. */
  472. /*
  473. * Join multicast group and setup delivery
  474. * and configuration change functions
  475. */
  476. totempg_initialize (
  477. aisexec_poll_handle,
  478. &totem_config);
  479. totempg_groups_initialize (
  480. &openais_group_handle,
  481. deliver_fn,
  482. confchg_fn);
  483. totempg_groups_join (
  484. openais_group_handle,
  485. &openais_group,
  486. 1);
  487. /*
  488. * This must occur after totempg is initialized because "this_ip" must be set
  489. */
  490. this_ip = &totem_config.interfaces[0].boundto;
  491. res = openais_service_init_all (service_count, objdb);
  492. if (res == -1) {
  493. log_printf (LOG_LEVEL_ERROR, "Could not init services\n");
  494. openais_exit_error (AIS_DONE_INIT_SERVICES);
  495. }
  496. sync_register (openais_sync_callbacks_retrieve, openais_sync_completed,
  497. totem_config.vsf_type);
  498. res = openais_flow_control_initialize ();
  499. /*
  500. * Drop root privleges to user 'ais'
  501. * TODO: Don't really need full root capabilities;
  502. * needed capabilities are:
  503. * CAP_NET_RAW (bindtodevice)
  504. * CAP_SYS_NICE (setscheduler)
  505. * CAP_IPC_LOCK (mlockall)
  506. */
  507. aisexec_priv_drop ();
  508. aisexec_mempool_init ();
  509. openais_ipc_init (
  510. serialize_mutex_lock,
  511. serialize_mutex_unlock,
  512. gid_valid,
  513. &this_non_loopback_ip);
  514. /*
  515. * Start main processing loop
  516. */
  517. poll_run (aisexec_poll_handle);
  518. return EXIT_SUCCESS;
  519. }