main.c 15 KB

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