main.c 19 KB

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