main.c 15 KB

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