main.c 15 KB

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