main.c 25 KB

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