main.c 22 KB

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