main.c 15 KB

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