main.c 25 KB

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