main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <assert.h>
  35. #include <grp.h>
  36. #include <sys/poll.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/un.h>
  41. #include <sys/sysinfo.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <linux/if.h>
  45. #include <linux/sockios.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <signal.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include "../include/ais_types.h"
  55. #include "../include/ais_msg.h"
  56. #include "../include/list.h"
  57. #include "../include/queue.h"
  58. #include "poll.h"
  59. #include "gmi.h"
  60. #include "mempool.h"
  61. #include "parse.h"
  62. #include "main.h"
  63. #include "handlers.h"
  64. #include "clm.h"
  65. #include "amf.h"
  66. #include "ckpt.h"
  67. #include "print.h"
  68. #define SERVER_BACKLOG 5
  69. int connection_entries = 0;
  70. struct connection *connections = 0;
  71. int gid_valid = 20;
  72. struct gmi_groupname aisexec_groupname = { "0123" };
  73. /*
  74. * All service handlers in the AIS
  75. */
  76. struct service_handler *ais_service_handlers[] = {
  77. &clm_service_handler,
  78. &amf_service_handler,
  79. &ckpt_service_handler,
  80. &ckpt_checkpoint_service_handler,
  81. &ckpt_sectioniterator_service_handler
  82. };
  83. #define AIS_SERVICE_HANDLERS_COUNT 5
  84. #define AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX 40
  85. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data);
  86. static inline void ais_done (int err)
  87. {
  88. log_printf (LOG_LEVEL_ERROR, "AIS Executive exiting.\n");
  89. exit (1);
  90. }
  91. static inline int init_connection_entry (int fd)
  92. {
  93. int res;
  94. memset (&connections[fd], 0, sizeof (struct connection));
  95. connections[fd].active = 1;
  96. res = queue_init (&connections[fd].outq, SIZEQUEUE, sizeof (struct outq_item));
  97. if (res != 0) {
  98. goto error_exit;
  99. }
  100. connections[fd].inb = malloc (sizeof (char) * SIZEINB);
  101. if (connections[fd].inb == 0) {
  102. queue_free (&connections[fd].outq);
  103. goto error_exit;
  104. }
  105. return (0);
  106. error_exit:
  107. return (-1);
  108. }
  109. /*
  110. * Grows the connections table to fd + 1 in size clearing new entries
  111. */
  112. static inline int grow_connections_table (int fd)
  113. {
  114. struct connection *conn_temp;
  115. if (fd + 1 > connection_entries) {
  116. conn_temp = mempool_realloc (connections, (fd + 1) * sizeof (struct connection));
  117. if (conn_temp == 0) {
  118. return (-1);
  119. }
  120. connections = conn_temp;
  121. memset (&connections[connection_entries], 0,
  122. (fd - connection_entries + 1) * sizeof (struct connection));
  123. connection_entries = fd + 1;
  124. }
  125. return (0);
  126. }
  127. struct sockaddr_in this_ip;
  128. #define LOCALHOST_IP inet_addr("127.0.0.1")
  129. char *socketname = "libais.socket";
  130. static void libais_disconnect (int fd)
  131. {
  132. int i;
  133. close (fd);
  134. connections[fd].active = 0;
  135. queue_free (&connections[fd].outq);
  136. free (connections[fd].inb);
  137. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  138. if (ais_service_handlers[i]->libais_exit_fn) {
  139. ais_service_handlers[i]->libais_exit_fn (fd);
  140. }
  141. }
  142. poll_dispatch_delete (aisexec_poll_handle, fd);
  143. }
  144. extern int libais_send_response (int s, void *msg, int mlen)
  145. {
  146. struct queue *outq;
  147. char *cmsg;
  148. int res;
  149. int queue_empty;
  150. struct outq_item *queue_item;
  151. struct outq_item queue_item_out;
  152. struct msghdr msg_send;
  153. struct iovec iov_send;
  154. outq = &connections[s].outq;
  155. msg_send.msg_iov = &iov_send;
  156. msg_send.msg_name = 0;
  157. msg_send.msg_namelen = 0;
  158. msg_send.msg_iovlen = 1;
  159. msg_send.msg_control = 0;
  160. msg_send.msg_controllen = 0;
  161. msg_send.msg_flags = 0;
  162. if (queue_is_full (outq)) {
  163. log_printf (LOG_LEVEL_ERROR, "queue is full.\n");
  164. ais_done (1);
  165. }
  166. while (!queue_is_empty (outq)) {
  167. queue_item = queue_item_get (outq);
  168. iov_send.iov_base = (void *)connections[s].byte_start;
  169. iov_send.iov_len = queue_item->mlen;
  170. retry_sendmsg:
  171. res = sendmsg (s, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  172. if (res == -1 && errno == EINTR) {
  173. goto retry_sendmsg;
  174. }
  175. if (res == -1 && errno == EAGAIN) {
  176. break; /* outgoing kernel queue full, ais_done while not empty */
  177. }
  178. if (res == -1) {
  179. return (-1); /* message couldn't be sent */
  180. }
  181. /*
  182. * Message sent, try sending another message
  183. */
  184. queue_item_remove (outq);
  185. connections[s].byte_start = 0;
  186. mempool_free (queue_item->msg);
  187. } /* while queue not empty */
  188. res = 0;
  189. queue_empty = queue_is_empty (outq);
  190. /*
  191. * Send requested message
  192. */
  193. if (queue_empty) {
  194. iov_send.iov_base = msg;
  195. iov_send.iov_len = mlen;
  196. retry_sendmsg_two:
  197. res = sendmsg (s, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  198. if (res == -1 && errno == EINTR) {
  199. goto retry_sendmsg_two;
  200. }
  201. if (res == -1 && errno != EAGAIN) {
  202. return (-1);
  203. }
  204. }
  205. /*
  206. * If res == -1 , errrno == EAGAIN which means kernel queue full
  207. */
  208. if (res == -1) {
  209. cmsg = mempool_malloc (mlen);
  210. if (cmsg == 0) {
  211. ais_done (1);
  212. }
  213. queue_item_out.msg = cmsg;
  214. queue_item_out.mlen = mlen;
  215. memcpy (cmsg, msg, mlen);
  216. queue_item_add (outq, &queue_item_out);
  217. }
  218. return (0);
  219. }
  220. static int poll_handler_libais_accept (
  221. poll_handle handle,
  222. int fd,
  223. int revent,
  224. void *data)
  225. {
  226. int addrlen;
  227. struct sockaddr_un un_addr;
  228. int new_fd;
  229. int on = 1;
  230. int res;
  231. addrlen = sizeof (struct sockaddr_un);
  232. retry_accept:
  233. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  234. if (new_fd == -1 && errno == EINTR) {
  235. goto retry_accept;
  236. }
  237. if (new_fd == -1) {
  238. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  239. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  240. }
  241. /*
  242. * Valid accept
  243. */
  244. /*
  245. * Request credentials of sender provided by kernel
  246. */
  247. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  248. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  249. /*
  250. * Generate new connections array
  251. */
  252. res = grow_connections_table (new_fd);
  253. if (res == -1) {
  254. close (new_fd);
  255. return (0); /* This is an error, but -1 would indicate disconnect from poll */
  256. }
  257. res = init_connection_entry (new_fd);
  258. if (res == -1) {
  259. close (new_fd);
  260. return (0); /* This is an error, but -1 would indicate disconnect from poll */
  261. }
  262. poll_dispatch_add (aisexec_poll_handle, new_fd, POLLIN, 0, poll_handler_libais_deliver);
  263. connections[new_fd].service = SOCKET_SERVICE_INIT;
  264. memcpy (&connections[new_fd].ais_ci.un_addr, &un_addr, sizeof (struct sockaddr_un));
  265. return (0);
  266. }
  267. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data)
  268. {
  269. int res;
  270. struct message_header *header;
  271. int service;
  272. struct msghdr msg_recv;
  273. struct iovec iov_recv;
  274. struct cmsghdr *cmsg;
  275. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  276. struct ucred *cred;
  277. int on = 0;
  278. msg_recv.msg_iov = &iov_recv;
  279. msg_recv.msg_iovlen = 1;
  280. msg_recv.msg_name = 0;
  281. msg_recv.msg_namelen = 0;
  282. msg_recv.msg_flags = 0;
  283. if (connections[fd].authenticated) {
  284. msg_recv.msg_control = 0;
  285. msg_recv.msg_controllen = 0;
  286. } else {
  287. msg_recv.msg_control = (void *)cmsg_cred;
  288. msg_recv.msg_controllen = sizeof (cmsg_cred);
  289. }
  290. iov_recv.iov_base = &connections[fd].inb[connections[fd].inb_start];
  291. iov_recv.iov_len = (SIZEINB) - connections[fd].inb_start;
  292. assert (iov_recv.iov_len != 0);
  293. //printf ("inb start inb inuse %d %d\n", connections[fd].inb_start, connections[fd].inb_inuse);
  294. retry_recv:
  295. res = recvmsg (fd, &msg_recv, MSG_DONTWAIT | MSG_NOSIGNAL);
  296. //printf ("received %d bytes\n", res);
  297. if (res == -1 && errno == EINTR) {
  298. goto retry_recv;
  299. } else
  300. if (res == -1) {
  301. goto error_exit;
  302. } else
  303. if (res == 0) {
  304. goto error_exit;
  305. return (-1);
  306. }
  307. /*
  308. * Authenticate if this connection has not been authenticated
  309. */
  310. if (connections[fd].authenticated == 0) {
  311. cmsg = CMSG_FIRSTHDR (&msg_recv);
  312. cred = (struct ucred *)CMSG_DATA (cmsg);
  313. if (cred) {
  314. if (cred->uid == 0 || cred->gid == gid_valid) {
  315. setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  316. connections[fd].authenticated = 1;
  317. }
  318. }
  319. if (connections[fd].authenticated == 0) {
  320. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, gid_valid);
  321. }
  322. }
  323. /*
  324. * Dispatch all messages received in recvmsg that can be dispatched
  325. * sizeof (struct message_header) needed at minimum to do any processing
  326. */
  327. connections[fd].inb_inuse += res;
  328. connections[fd].inb_start += res;
  329. while (connections[fd].inb_inuse >= sizeof (struct message_header) && res != -1) {
  330. header = (struct message_header *)&connections[fd].inb[connections[fd].inb_start - connections[fd].inb_inuse];
  331. if (header->magic != MESSAGE_MAGIC) {
  332. log_printf (LOG_LEVEL_SECURITY, "Invalid magic is %x should be %x\n", header->magic, MESSAGE_MAGIC);
  333. res = -1;
  334. goto error_exit;
  335. }
  336. if (header->size > connections[fd].inb_inuse) {
  337. break;
  338. }
  339. service = connections[fd].service;
  340. /*
  341. * If this service is in init phase, initialize service
  342. * else handle message using service handlers
  343. */
  344. if (service == SOCKET_SERVICE_INIT) {
  345. /*
  346. * Initializing service
  347. */
  348. res = ais_service_handlers[header->id]->libais_init_fn (fd, header);
  349. } else {
  350. /*
  351. * Not an init service, but a standard service
  352. */
  353. if (header->id < 0 || header->id > ais_service_handlers[service - 1]->libais_handler_fns_count) {
  354. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  355. header->id, ais_service_handlers[service - 1]->libais_handler_fns_count);
  356. res = -1;
  357. goto error_exit;
  358. }
  359. res = ais_service_handlers[service - 1]->libais_handler_fns[header->id](fd, header);
  360. }
  361. connections[fd].inb_inuse -= header->size;
  362. } /* while */
  363. if (connections[fd].inb_inuse == 0) {
  364. connections[fd].inb_start = 0;
  365. } else
  366. // BUG if (connections[fd].inb_start + connections[fd].inb_inuse >= SIZEINB) {
  367. if (connections[fd].inb_start >= SIZEINB) {
  368. /*
  369. * If in buffer is full, move it back to start
  370. */
  371. memmove (connections[fd].inb,
  372. &connections[fd].inb[connections[fd].inb_start -
  373. connections[fd].inb_inuse],
  374. sizeof (char) * connections[fd].inb_inuse);
  375. connections[fd].inb_start = connections[fd].inb_inuse;
  376. }
  377. return (res);
  378. error_exit:
  379. libais_disconnect (fd);
  380. return (-1); /* remove entry from poll list */
  381. }
  382. extern void print_stats (void);
  383. void sigintr_handler (int signum)
  384. {
  385. #ifdef DEBUG_MEMPOOL
  386. int stats_inuse[MEMPOOL_GROUP_SIZE];
  387. int stats_avail[MEMPOOL_GROUP_SIZE];
  388. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  389. int i;
  390. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  391. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  392. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  393. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  394. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  395. }
  396. #endif
  397. print_stats ();
  398. ais_done (0);
  399. }
  400. static struct sched_param sched_param = {
  401. sched_priority: 99
  402. };
  403. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  404. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  405. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  406. static int (*aisexec_handler_fns[AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX]) (int fd, void *);
  407. static int aisexec_handler_fns_count = 0;
  408. /*
  409. * Builds the handler table as an optimization
  410. */
  411. static void aisexec_handler_fns_build (void)
  412. {
  413. int i, j;
  414. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  415. for (j = 0; j < ais_service_handlers[i]->aisexec_handler_fns_count; j++) {
  416. aisexec_handler_fns[aisexec_handler_fns_count++] =
  417. ais_service_handlers[i]->aisexec_handler_fns[j];
  418. }
  419. }
  420. log_printf (LOG_LEVEL_DEBUG, "built %d handler functions\n", aisexec_handler_fns_count);
  421. }
  422. char delivery_data[MESSAGE_SIZE_MAX];
  423. static void deliver_fn (
  424. struct gmi_groupname *groupname,
  425. struct iovec *iovec,
  426. int iov_len)
  427. {
  428. struct message_header *header;
  429. int res;
  430. int pos = 0;
  431. int i;
  432. /*
  433. * Build buffer without iovecs to make processing easier
  434. * This is only used for messages which are multicast with iovecs
  435. * and self-delivered. All other mechanisms avoid the copy.
  436. */
  437. if (iov_len > 1) {
  438. for (i = 0; i < iov_len; i++) {
  439. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  440. pos += iovec[i].iov_len;
  441. assert (pos < MESSAGE_SIZE_MAX);
  442. }
  443. header = (struct message_header *)delivery_data;
  444. } else {
  445. header = iovec[0].iov_base;
  446. }
  447. res = aisexec_handler_fns[header->id](0, header);
  448. }
  449. static void confchg_fn (
  450. struct sockaddr_in *member_list, int member_list_entries,
  451. struct sockaddr_in *left_list, int left_list_entries,
  452. struct sockaddr_in *joined_list, int joined_list_entries)
  453. {
  454. int i;
  455. /*
  456. * Call configure change for all APIs
  457. */
  458. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  459. if (ais_service_handlers[i]->confchg_fn) {
  460. ais_service_handlers[i]->confchg_fn (member_list, member_list_entries,
  461. left_list, left_list_entries, joined_list, joined_list_entries);
  462. }
  463. }
  464. }
  465. static void aisexec_group_determine (void) {
  466. struct group *group;
  467. group = getgrnam ("ais");
  468. if (group == 0) {
  469. log_printf (LOG_LEVEL_ERROR, "The 'ais' group is not found in /etc/group, please read the documentation.\n");
  470. ais_done (-1);
  471. }
  472. gid_valid = group->gr_gid;
  473. }
  474. static void aisexec_mempool_init (void)
  475. {
  476. int res;
  477. res = mempool_init (pool_sizes);
  478. if (res == ENOMEM) {
  479. log_printf (LOG_LEVEL_ERROR, "Couldn't allocate memory pools, not enough memory");
  480. ais_done (1);
  481. }
  482. }
  483. static void aisexec_tty_detach (void)
  484. {
  485. #define DEBUG
  486. #ifndef DEBUG
  487. /*
  488. * Disconnect from TTY if this is not a debug run
  489. */
  490. switch (fork ()) {
  491. case -1:
  492. ais_done (1);
  493. break;
  494. case 0:
  495. /*
  496. * child which is disconnected, run this process
  497. */
  498. break;
  499. default:
  500. exit (0);
  501. break;
  502. }
  503. #endif
  504. #undef DEBUG
  505. }
  506. static void aisexec_service_handlers_init (void)
  507. {
  508. int i;
  509. /*
  510. * Initialize all services
  511. */
  512. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  513. if (ais_service_handlers[i]->aisexec_init_fn) {
  514. ais_service_handlers[i]->aisexec_init_fn ();
  515. }
  516. }
  517. }
  518. static void aisexec_libais_bind (int *server_fd)
  519. {
  520. int libais_server_fd;
  521. struct sockaddr_un un_addr;
  522. int res;
  523. /*
  524. * Create socket for libais clients, name socket, listen for connections
  525. */
  526. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  527. if (libais_server_fd == -1) {
  528. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  529. ais_done (1);
  530. };
  531. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  532. un_addr.sun_family = AF_UNIX;
  533. strcpy (un_addr.sun_path + 1, socketname);
  534. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, sizeof (struct sockaddr_un));
  535. if (res) {
  536. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  537. ais_done (1);
  538. }
  539. listen (libais_server_fd, SERVER_BACKLOG);
  540. *server_fd = libais_server_fd;
  541. }
  542. static void aisexec_setscheduler (void)
  543. {
  544. int res;
  545. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  546. if (res == -1) {
  547. log_printf (LOG_LEVEL_ERROR, "WARNING: Could not set SCHED_RR at priority 99: %s\n", strerror (errno));
  548. }
  549. }
  550. static void aisexec_mlockall (void)
  551. {
  552. int res;
  553. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  554. if (res == -1) {
  555. log_printf (LOG_LEVEL_ERROR, "WARNING: Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  556. };
  557. }
  558. int main (int argc, char **argv)
  559. {
  560. int libais_server_fd;
  561. int res;
  562. struct sockaddr_in sockaddr_in_mcast;
  563. struct sockaddr_in sockaddr_in_bindnet;
  564. gmi_join_handle handle;
  565. char *error_string;
  566. aisexec_group_determine ();
  567. aisexec_handler_fns_build ();
  568. aisexec_poll_handle = poll_create ();
  569. res = amfReadNetwork (&error_string, &sockaddr_in_mcast, &sockaddr_in_bindnet);
  570. if (res == -1) {
  571. log_printf (LOG_LEVEL_ERROR, error_string);
  572. ais_done (1);
  573. }
  574. /*
  575. * Initialize group messaging interface with multicast address
  576. */
  577. gmi_init (&sockaddr_in_mcast, &sockaddr_in_bindnet,
  578. &aisexec_poll_handle, &this_ip);
  579. aisexec_mempool_init ();
  580. res = amfReadGroups(&error_string);
  581. if (res == -1) {
  582. log_printf (LOG_LEVEL_ERROR, error_string);
  583. ais_done (1);
  584. }
  585. aisexec_tty_detach ();
  586. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2004 MontaVista Software, Inc.\n");
  587. signal (SIGINT, sigintr_handler);
  588. aisexec_service_handlers_init ();
  589. aisexec_libais_bind (&libais_server_fd);
  590. res = grow_connections_table (libais_server_fd);
  591. if (res == -1) {
  592. log_printf (LOG_LEVEL_ERROR, "Could not allocate memory for listening socket.\n");
  593. ais_done (1);
  594. }
  595. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: started and ready to receive connections.\n");
  596. /*
  597. * Set round robin realtime scheduling with priority 99
  598. * Lock all memory to avoid page faults which may interrupt
  599. * application healthchecking
  600. */
  601. aisexec_setscheduler ();
  602. aisexec_mlockall ();
  603. /*
  604. * Setup libais connection dispatch routine
  605. */
  606. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  607. POLLIN, 0, poll_handler_libais_accept);
  608. /*
  609. * Join multicast group and setup delivery
  610. * and configuration change functions
  611. */
  612. gmi_join (0, deliver_fn, confchg_fn, &handle);
  613. /*
  614. * Start main processing loop
  615. */
  616. poll_run (aisexec_poll_handle);
  617. return (0);
  618. }