main.c 20 KB

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