main.c 16 KB

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