main.c 19 KB

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