main.c 19 KB

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