main.c 15 KB

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