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. #define DEBUG
  206. #ifndef DEBUG
  207. /*
  208. * Disconnect from TTY if this is not a debug run
  209. */
  210. switch (fork ()) {
  211. case -1:
  212. openais_exit_error (AIS_DONE_FORK);
  213. break;
  214. case 0:
  215. /*
  216. * child which is disconnected, run this process
  217. */
  218. break;
  219. default:
  220. exit (0);
  221. break;
  222. }
  223. #endif
  224. }
  225. static void aisexec_setscheduler (void)
  226. {
  227. #if defined(OPENAIS_BSD) || defined(OPENAIS_LINUX)
  228. struct sched_param sched_param;
  229. int res;
  230. res = sched_get_priority_max (SCHED_RR);
  231. if (res != -1) {
  232. sched_param.sched_priority = res;
  233. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  234. if (res == -1) {
  235. log_printf (LOG_LEVEL_WARNING, "Could not set SCHED_RR at priority %d: %s\n",
  236. sched_param.sched_priority, strerror (errno));
  237. }
  238. } else
  239. log_printf (LOG_LEVEL_WARNING, "Could not get maximum scheduler priority: %s\n", strerror (errno));
  240. #else
  241. log_printf(LOG_LEVEL_WARNING, "Scheduler priority left to default value (no OS support)\n");
  242. #endif
  243. }
  244. static void aisexec_mlockall (void)
  245. {
  246. #if !defined(OPENAIS_BSD)
  247. int res;
  248. #endif
  249. struct rlimit rlimit;
  250. rlimit.rlim_cur = RLIM_INFINITY;
  251. rlimit.rlim_max = RLIM_INFINITY;
  252. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  253. #if defined(OPENAIS_BSD)
  254. /* under FreeBSD a process with locked page cannot call dlopen
  255. * code disabled until FreeBSD bug i386/93396 was solved
  256. */
  257. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults\n");
  258. #else
  259. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  260. if (res == -1) {
  261. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  262. };
  263. #endif
  264. }
  265. static void deliver_fn (
  266. struct totem_ip_address *source_addr,
  267. struct iovec *iovec,
  268. int iov_len,
  269. int endian_conversion_required)
  270. {
  271. struct req_header *header;
  272. int pos = 0;
  273. int i;
  274. int service;
  275. int fn_id;
  276. /*
  277. * Build buffer without iovecs to make processing easier
  278. * This is only used for messages which are multicast with iovecs
  279. * and self-delivered. All other mechanisms avoid the copy.
  280. */
  281. if (iov_len > 1) {
  282. for (i = 0; i < iov_len; i++) {
  283. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  284. pos += iovec[i].iov_len;
  285. assert (pos < MESSAGE_SIZE_MAX);
  286. }
  287. header = (struct req_header *)delivery_data;
  288. } else {
  289. header = (struct req_header *)iovec[0].iov_base;
  290. }
  291. if (endian_conversion_required) {
  292. header->id = swab32 (header->id);
  293. header->size = swab32 (header->size);
  294. }
  295. // assert(iovec->iov_len == header->size);
  296. /*
  297. * Call the proper executive handler
  298. */
  299. service = header->id >> 16;
  300. fn_id = header->id & 0xffff;
  301. if (endian_conversion_required) {
  302. ais_service[service]->exec_service[fn_id].exec_endian_convert_fn
  303. (header);
  304. }
  305. ais_service[service]->exec_service[fn_id].exec_handler_fn
  306. (header, source_addr);
  307. }
  308. int main (int argc, char **argv)
  309. {
  310. char *error_string;
  311. struct main_config main_config;
  312. struct totem_config totem_config;
  313. unsigned int objdb_handle;
  314. unsigned int config_handle;
  315. unsigned int config_version = 0;
  316. struct objdb_iface_ver0 *objdb;
  317. void *objdb_p;
  318. struct config_iface_ver0 *config;
  319. void *config_p;
  320. char *config_iface;
  321. int res;
  322. int totem_log_service;
  323. log_init ("MAIN");
  324. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service RELEASE %s\n", release_name);
  325. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2002-2006 MontaVista Software, Inc and contributors.\n");
  326. log_printf (LOG_LEVEL_NOTICE, "Copyright (C) 2006 Red Hat, Inc.\n");
  327. memset(&this_non_loopback_ip, 0, sizeof(struct totem_ip_address));
  328. totemip_localhost(AF_INET, &this_non_loopback_ip);
  329. aisexec_poll_handle = poll_create (openais_ipc_mutex_get());
  330. /*
  331. * Load the object database interface
  332. */
  333. res = lcr_ifact_reference (
  334. &objdb_handle,
  335. "objdb",
  336. 0,
  337. &objdb_p,
  338. 0);
  339. if (res == -1) {
  340. log_printf (LOG_LEVEL_ERROR, "AIS Executive couldn't open configuration object database component.\n");
  341. openais_exit_error (AIS_DONE_OBJDB);
  342. }
  343. objdb = (struct objdb_iface_ver0 *)objdb_p;
  344. objdb->objdb_init ();
  345. /* User's bootstrap config service */
  346. config_iface = getenv("OPENAIS_DEFAULT_CONFIG_IFACE");
  347. if (!config_iface) {
  348. config_iface = "aisparser";
  349. }
  350. res = lcr_ifact_reference (
  351. &config_handle,
  352. config_iface,
  353. config_version,
  354. &config_p,
  355. 0);
  356. config = (struct config_iface_ver0 *)config_p;
  357. if (res == -1) {
  358. log_printf (LOG_LEVEL_ERROR, "AIS Executive couldn't open configuration component.\n");
  359. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  360. }
  361. res = config->config_readconfig(objdb, &error_string);
  362. if (res == -1) {
  363. log_printf (LOG_LEVEL_ERROR, error_string);
  364. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  365. }
  366. openais_service_default_objdb_set (objdb);
  367. res = openais_service_link_all (objdb);
  368. if (res == -1) {
  369. log_printf (LOG_LEVEL_ERROR, "Could not load services\n");
  370. openais_exit_error (AIS_DONE_DYNAMICLOAD);
  371. }
  372. res = openais_main_config_read (objdb, &error_string, &main_config);
  373. if (res == -1) {
  374. log_printf (LOG_LEVEL_ERROR, error_string);
  375. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  376. }
  377. res = totem_config_read (objdb, &totem_config, &error_string, 3);
  378. if (res == -1) {
  379. log_printf (LOG_LEVEL_ERROR, error_string);
  380. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  381. }
  382. res = totem_config_keyread (objdb, &totem_config, &error_string);
  383. if (res == -1) {
  384. log_printf (LOG_LEVEL_ERROR, error_string);
  385. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  386. }
  387. res = totem_config_validate (&totem_config, &error_string);
  388. if (res == -1) {
  389. log_printf (LOG_LEVEL_ERROR, error_string);
  390. openais_exit_error (AIS_DONE_MAINCONFIGREAD);
  391. }
  392. res = log_setup (&error_string, &main_config);
  393. if (res == -1) {
  394. log_printf (LOG_LEVEL_ERROR, error_string);
  395. openais_exit_error (AIS_DONE_LOGSETUP);
  396. }
  397. aisexec_uid_determine (&main_config);
  398. aisexec_gid_determine (&main_config);
  399. /*
  400. * Set round robin realtime scheduling with priority 99
  401. * Lock all memory to avoid page faults which may interrupt
  402. * application healthchecking
  403. */
  404. aisexec_setscheduler ();
  405. aisexec_mlockall ();
  406. totem_config.totem_logging_configuration = totem_logging_configuration;
  407. totem_log_service = _log_init ("TOTEM");
  408. totem_config.totem_logging_configuration.log_level_security = mkpri (LOG_LEVEL_SECURITY, totem_log_service);
  409. totem_config.totem_logging_configuration.log_level_error = mkpri (LOG_LEVEL_ERROR, totem_log_service);
  410. totem_config.totem_logging_configuration.log_level_warning = mkpri (LOG_LEVEL_WARNING, totem_log_service);
  411. totem_config.totem_logging_configuration.log_level_notice = mkpri (LOG_LEVEL_NOTICE, totem_log_service);
  412. totem_config.totem_logging_configuration.log_level_debug = mkpri (LOG_LEVEL_DEBUG, totem_log_service);
  413. totem_config.totem_logging_configuration.log_printf = internal_log_printf;
  414. /*
  415. * if totempg_initialize doesn't have root priveleges, it cannot
  416. * bind to a specific interface. This only matters if
  417. * there is more then one interface in a system, so
  418. * in this case, only a warning is printed
  419. */
  420. /*
  421. * Join multicast group and setup delivery
  422. * and configuration change functions
  423. */
  424. totempg_initialize (
  425. aisexec_poll_handle,
  426. &totem_config);
  427. totempg_groups_initialize (
  428. &openais_group_handle,
  429. deliver_fn,
  430. confchg_fn);
  431. totempg_groups_join (
  432. openais_group_handle,
  433. &openais_group,
  434. 1);
  435. /*
  436. * This must occur after totempg is initialized because "this_ip" must be set
  437. */
  438. this_ip = &totem_config.interfaces[0].boundto;
  439. res = openais_service_init_all (service_count, objdb);
  440. if (res == -1) {
  441. log_printf (LOG_LEVEL_ERROR, "Could not init services\n");
  442. openais_exit_error (AIS_DONE_INIT_SERVICES);
  443. }
  444. sync_register (openais_sync_callbacks_retrieve, openais_sync_completed);
  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. }