main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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,
  265. poll_handler_libais_deliver);
  266. connections[new_fd].service = SOCKET_SERVICE_INIT;
  267. memcpy (&connections[new_fd].ais_ci.un_addr, &un_addr, sizeof (struct sockaddr_un));
  268. return (0);
  269. }
  270. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data)
  271. {
  272. int res;
  273. struct message_header *header;
  274. int service;
  275. struct msghdr msg_recv;
  276. struct iovec iov_recv;
  277. struct cmsghdr *cmsg;
  278. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  279. struct ucred *cred;
  280. int on = 0;
  281. msg_recv.msg_iov = &iov_recv;
  282. msg_recv.msg_iovlen = 1;
  283. msg_recv.msg_name = 0;
  284. msg_recv.msg_namelen = 0;
  285. msg_recv.msg_flags = 0;
  286. if (connections[fd].authenticated) {
  287. msg_recv.msg_control = 0;
  288. msg_recv.msg_controllen = 0;
  289. } else {
  290. msg_recv.msg_control = (void *)cmsg_cred;
  291. msg_recv.msg_controllen = sizeof (cmsg_cred);
  292. }
  293. iov_recv.iov_base = &connections[fd].inb[connections[fd].inb_start];
  294. iov_recv.iov_len = (SIZEINB) - connections[fd].inb_start;
  295. assert (iov_recv.iov_len != 0);
  296. //printf ("inb start inb inuse %d %d\n", connections[fd].inb_start, connections[fd].inb_inuse);
  297. retry_recv:
  298. res = recvmsg (fd, &msg_recv, MSG_DONTWAIT | MSG_NOSIGNAL);
  299. //printf ("received %d bytes\n", res);
  300. if (res == -1 && errno == EINTR) {
  301. goto retry_recv;
  302. } else
  303. if (res == -1) {
  304. goto error_exit;
  305. } else
  306. if (res == 0) {
  307. goto error_exit;
  308. return (-1);
  309. }
  310. /*
  311. * Authenticate if this connection has not been authenticated
  312. */
  313. if (connections[fd].authenticated == 0) {
  314. cmsg = CMSG_FIRSTHDR (&msg_recv);
  315. cred = (struct ucred *)CMSG_DATA (cmsg);
  316. if (cred) {
  317. if (cred->uid == 0 || cred->gid == gid_valid) {
  318. setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  319. connections[fd].authenticated = 1;
  320. }
  321. }
  322. if (connections[fd].authenticated == 0) {
  323. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, gid_valid);
  324. }
  325. }
  326. /*
  327. * Dispatch all messages received in recvmsg that can be dispatched
  328. * sizeof (struct message_header) needed at minimum to do any processing
  329. */
  330. connections[fd].inb_inuse += res;
  331. connections[fd].inb_start += res;
  332. while (connections[fd].inb_inuse >= sizeof (struct message_header) && res != -1) {
  333. header = (struct message_header *)&connections[fd].inb[connections[fd].inb_start - connections[fd].inb_inuse];
  334. if (header->magic != MESSAGE_MAGIC) {
  335. log_printf (LOG_LEVEL_SECURITY, "Invalid magic is %x should be %x\n", header->magic, MESSAGE_MAGIC);
  336. res = -1;
  337. goto error_exit;
  338. }
  339. if (header->size > connections[fd].inb_inuse) {
  340. break;
  341. }
  342. service = connections[fd].service;
  343. /*
  344. * If this service is in init phase, initialize service
  345. * else handle message using service handlers
  346. */
  347. if (service == SOCKET_SERVICE_INIT) {
  348. /*
  349. * Initializing service
  350. */
  351. res = ais_service_handlers[header->id]->libais_init_fn (fd, header);
  352. } else {
  353. /*
  354. * Not an init service, but a standard service
  355. */
  356. if (header->id < 0 || header->id > ais_service_handlers[service - 1]->libais_handler_fns_count) {
  357. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  358. header->id, ais_service_handlers[service - 1]->libais_handler_fns_count);
  359. res = -1;
  360. goto error_exit;
  361. }
  362. res = ais_service_handlers[service - 1]->libais_handler_fns[header->id](fd, header);
  363. }
  364. connections[fd].inb_inuse -= header->size;
  365. } /* while */
  366. if (connections[fd].inb_inuse == 0) {
  367. connections[fd].inb_start = 0;
  368. } else
  369. // BUG if (connections[fd].inb_start + connections[fd].inb_inuse >= SIZEINB) {
  370. if (connections[fd].inb_start >= SIZEINB) {
  371. /*
  372. * If in buffer is full, move it back to start
  373. */
  374. memmove (connections[fd].inb,
  375. &connections[fd].inb[connections[fd].inb_start -
  376. connections[fd].inb_inuse],
  377. sizeof (char) * connections[fd].inb_inuse);
  378. connections[fd].inb_start = connections[fd].inb_inuse;
  379. }
  380. return (res);
  381. error_exit:
  382. libais_disconnect (fd);
  383. return (-1); /* remove entry from poll list */
  384. }
  385. extern void print_stats (void);
  386. void sigintr_handler (int signum)
  387. {
  388. #ifdef DEBUG_MEMPOOL
  389. int stats_inuse[MEMPOOL_GROUP_SIZE];
  390. int stats_avail[MEMPOOL_GROUP_SIZE];
  391. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  392. int i;
  393. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  394. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  395. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  396. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  397. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  398. }
  399. #endif
  400. print_stats ();
  401. ais_done (0);
  402. }
  403. static struct sched_param sched_param = {
  404. sched_priority: 99
  405. };
  406. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  407. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  408. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  409. static int (*aisexec_handler_fns[AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX]) (void *msg);
  410. static int aisexec_handler_fns_count = 0;
  411. /*
  412. * Builds the handler table as an optimization
  413. */
  414. static void aisexec_handler_fns_build (void)
  415. {
  416. int i, j;
  417. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  418. for (j = 0; j < ais_service_handlers[i]->aisexec_handler_fns_count; j++) {
  419. aisexec_handler_fns[aisexec_handler_fns_count++] =
  420. ais_service_handlers[i]->aisexec_handler_fns[j];
  421. }
  422. }
  423. log_printf (LOG_LEVEL_DEBUG, "built %d handler functions\n", aisexec_handler_fns_count);
  424. }
  425. char delivery_data[MESSAGE_SIZE_MAX];
  426. static void deliver_fn (
  427. struct gmi_groupname *groupname,
  428. struct iovec *iovec,
  429. int iov_len)
  430. {
  431. struct message_header *header;
  432. int res;
  433. int pos = 0;
  434. int i;
  435. /*
  436. * Build buffer without iovecs to make processing easier
  437. * This is only used for messages which are multicast with iovecs
  438. * and self-delivered. All other mechanisms avoid the copy.
  439. */
  440. if (iov_len > 1) {
  441. for (i = 0; i < iov_len; i++) {
  442. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  443. pos += iovec[i].iov_len;
  444. assert (pos < MESSAGE_SIZE_MAX);
  445. }
  446. header = (struct message_header *)delivery_data;
  447. } else {
  448. header = iovec[0].iov_base;
  449. }
  450. res = aisexec_handler_fns[header->id](header);
  451. }
  452. static void confchg_fn (
  453. struct sockaddr_in *member_list, int member_list_entries,
  454. struct sockaddr_in *left_list, int left_list_entries,
  455. struct sockaddr_in *joined_list, int joined_list_entries)
  456. {
  457. int i;
  458. /*
  459. * Call configure change for all APIs
  460. */
  461. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  462. if (ais_service_handlers[i]->confchg_fn) {
  463. ais_service_handlers[i]->confchg_fn (member_list, member_list_entries,
  464. left_list, left_list_entries, joined_list, joined_list_entries);
  465. }
  466. }
  467. }
  468. static void aisexec_uid_determine (void)
  469. {
  470. struct passwd *passwd;
  471. passwd = getpwnam("ais");
  472. if (passwd == 0) {
  473. log_printf (LOG_LEVEL_ERROR, "ERROR: The 'ais' user is not found in /etc/passwd, please read the documentation.\n");
  474. ais_done (-1);
  475. }
  476. ais_uid = passwd->pw_uid;
  477. }
  478. static void aisexec_gid_determine (void)
  479. {
  480. struct group *group;
  481. group = getgrnam ("ais");
  482. if (group == 0) {
  483. log_printf (LOG_LEVEL_ERROR, "ERROR: The 'ais' group is not found in /etc/group, please read the documentation.\n");
  484. ais_done (-1);
  485. }
  486. gid_valid = group->gr_gid;
  487. }
  488. static void aisexec_priv_drop (void)
  489. {
  490. setuid (ais_uid);
  491. setegid (ais_uid);
  492. }
  493. static void aisexec_mempool_init (void)
  494. {
  495. int res;
  496. res = mempool_init (pool_sizes);
  497. if (res == ENOMEM) {
  498. log_printf (LOG_LEVEL_ERROR, "Couldn't allocate memory pools, not enough memory");
  499. ais_done (1);
  500. }
  501. }
  502. static void aisexec_tty_detach (void)
  503. {
  504. #define DEBUG
  505. #ifndef DEBUG
  506. /*
  507. * Disconnect from TTY if this is not a debug run
  508. */
  509. switch (fork ()) {
  510. case -1:
  511. ais_done (1);
  512. break;
  513. case 0:
  514. /*
  515. * child which is disconnected, run this process
  516. */
  517. break;
  518. default:
  519. exit (0);
  520. break;
  521. }
  522. #endif
  523. #undef DEBUG
  524. }
  525. static void aisexec_service_handlers_init (void)
  526. {
  527. int i;
  528. /*
  529. * Initialize all services
  530. */
  531. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  532. if (ais_service_handlers[i]->aisexec_init_fn) {
  533. ais_service_handlers[i]->aisexec_init_fn ();
  534. }
  535. }
  536. }
  537. static void aisexec_libais_bind (int *server_fd)
  538. {
  539. int libais_server_fd;
  540. struct sockaddr_un un_addr;
  541. int res;
  542. /*
  543. * Create socket for libais clients, name socket, listen for connections
  544. */
  545. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  546. if (libais_server_fd == -1) {
  547. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  548. ais_done (1);
  549. };
  550. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  551. un_addr.sun_family = AF_UNIX;
  552. strcpy (un_addr.sun_path + 1, socketname);
  553. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, sizeof (struct sockaddr_un));
  554. if (res) {
  555. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  556. ais_done (1);
  557. }
  558. listen (libais_server_fd, SERVER_BACKLOG);
  559. *server_fd = libais_server_fd;
  560. }
  561. static void aisexec_setscheduler (void)
  562. {
  563. int res;
  564. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  565. if (res == -1) {
  566. log_printf (LOG_LEVEL_WARNING, "Could not set SCHED_RR at priority 99: %s\n", strerror (errno));
  567. }
  568. }
  569. static void aisexec_mlockall (void)
  570. {
  571. int res;
  572. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  573. if (res == -1) {
  574. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  575. };
  576. }
  577. int main (int argc, char **argv)
  578. {
  579. int libais_server_fd;
  580. int res;
  581. struct sockaddr_in sockaddr_in_mcast;
  582. struct sockaddr_in sockaddr_in_bindnet;
  583. gmi_join_handle handle;
  584. char *error_string;
  585. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2004 MontaVista Software, Inc.\n");
  586. aisexec_uid_determine ();
  587. aisexec_gid_determine ();
  588. aisexec_poll_handle = poll_create ();
  589. /*
  590. * if gmi_init doesn't have root priveleges, it cannot
  591. * bind to a specific interface. This only matters if
  592. * there is more then one interface in a system, so
  593. * in this case, only a warning is printed
  594. */
  595. /*
  596. * Initialize group messaging interface with multicast address
  597. */
  598. res = amfReadNetwork (&error_string, &sockaddr_in_mcast, &sockaddr_in_bindnet);
  599. if (res == -1) {
  600. log_printf (LOG_LEVEL_ERROR, error_string);
  601. ais_done (1);
  602. }
  603. /*
  604. * Set round robin realtime scheduling with priority 99
  605. * Lock all memory to avoid page faults which may interrupt
  606. * application healthchecking
  607. */
  608. aisexec_setscheduler ();
  609. aisexec_mlockall ();
  610. gmi_log_printf_init (internal_log_printf_checkdebug,
  611. LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, LOG_LEVEL_NOTICE, LOG_LEVEL_DEBUG);
  612. gmi_init (&sockaddr_in_mcast, &sockaddr_in_bindnet,
  613. &aisexec_poll_handle, &this_ip);
  614. /*
  615. * Drop root privleges to user 'ais'
  616. * TODO: Don't really need full root capabilities;
  617. * needed capabilities are:
  618. * CAP_NET_RAW (bindtodevice)
  619. * CAP_SYS_NICE (setscheduler)
  620. * CAP_IPC_LOCK (mlockall)
  621. */
  622. aisexec_priv_drop ();
  623. aisexec_handler_fns_build ();
  624. aisexec_mempool_init ();
  625. res = amfReadGroups(&error_string);
  626. if (res == -1) {
  627. log_printf (LOG_LEVEL_ERROR, error_string);
  628. ais_done (1);
  629. }
  630. aisexec_tty_detach ();
  631. signal (SIGINT, sigintr_handler);
  632. aisexec_service_handlers_init ();
  633. aisexec_libais_bind (&libais_server_fd);
  634. res = grow_connections_table (libais_server_fd);
  635. if (res == -1) {
  636. log_printf (LOG_LEVEL_ERROR, "Could not allocate memory for listening socket.\n");
  637. ais_done (1);
  638. }
  639. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: started and ready to receive connections.\n");
  640. /*
  641. * Setup libais connection dispatch routine
  642. */
  643. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  644. POLLIN, 0, poll_handler_libais_accept);
  645. /*
  646. * Join multicast group and setup delivery
  647. * and configuration change functions
  648. */
  649. gmi_join (0, deliver_fn, confchg_fn, &handle);
  650. /*
  651. * Start main processing loop
  652. */
  653. poll_run (aisexec_poll_handle);
  654. return (0);
  655. }