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 "totemsrp.h"
  63. #include "mempool.h"
  64. #include "parse.h"
  65. #include "main.h"
  66. #include "handlers.h"
  67. #include "evs.h"
  68. #include "clm.h"
  69. #include "amf.h"
  70. #include "ckpt.h"
  71. #include "evt.h"
  72. #include "swab.h"
  73. #define LOG_SERVICE LOG_SERVICE_MAIN
  74. #include "print.h"
  75. #define SERVER_BACKLOG 5
  76. int ais_uid = 0;
  77. int gid_valid = 0;
  78. /*
  79. * All service handlers in the AIS
  80. */
  81. struct service_handler *ais_service_handlers[] = {
  82. &evs_service_handler,
  83. &clm_service_handler,
  84. &amf_service_handler,
  85. &ckpt_service_handler,
  86. &evt_service_handler
  87. };
  88. #define AIS_SERVICE_HANDLERS_COUNT 5
  89. #define AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX 40
  90. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio);
  91. enum e_ais_done {
  92. AIS_DONE_EXIT = -1,
  93. AIS_DONE_UID_DETERMINE = -2,
  94. AIS_DONE_GID_DETERMINE = -3,
  95. AIS_DONE_MEMPOOL_INIT = -4,
  96. AIS_DONE_FORK = -5,
  97. AIS_DONE_LIBAIS_SOCKET = -6,
  98. AIS_DONE_LIBAIS_BIND = -7,
  99. AIS_DONE_READKEY = -8,
  100. AIS_DONE_MAINCONFIGREAD = -9,
  101. AIS_DONE_LOGSETUP = -10,
  102. AIS_DONE_AMFCONFIGREAD = -11,
  103. };
  104. static inline void ais_done (enum e_ais_done err)
  105. {
  106. log_printf (LOG_LEVEL_ERROR, "AIS Executive exiting.\n");
  107. poll_destroy (aisexec_poll_handle);
  108. exit (1);
  109. }
  110. static inline struct conn_info *conn_info_create (int fd) {
  111. struct conn_info *conn_info;
  112. int res;
  113. conn_info = malloc (sizeof (struct conn_info));
  114. if (conn_info == 0) {
  115. return (0);
  116. }
  117. memset (conn_info, 0, sizeof (struct conn_info));
  118. res = queue_init (&conn_info->outq, SIZEQUEUE,
  119. sizeof (struct outq_item));
  120. if (res != 0) {
  121. free (conn_info);
  122. return (0);
  123. }
  124. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  125. if (conn_info->inb == 0) {
  126. queue_free (&conn_info->outq);
  127. free (conn_info);
  128. return (0);
  129. }
  130. conn_info->state = CONN_STATE_ACTIVE;
  131. conn_info->fd = fd;
  132. conn_info->service = SOCKET_SERVICE_INIT;
  133. return (conn_info);
  134. }
  135. static void sigusr2_handler (int num)
  136. {
  137. int i;
  138. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  139. if (ais_service_handlers[i]->exec_dump_fn) {
  140. ais_service_handlers[i]->exec_dump_fn ();
  141. }
  142. }
  143. signal (SIGUSR2 ,sigusr2_handler);
  144. return;
  145. }
  146. struct sockaddr_in this_ip;
  147. #define LOCALHOST_IP inet_addr("127.0.0.1")
  148. char *socketname = "libais.socket";
  149. static int libais_connection_active (struct conn_info *conn_info)
  150. {
  151. return (conn_info->state == CONN_STATE_ACTIVE);
  152. }
  153. static void libais_disconnect_delayed (struct conn_info *conn_info)
  154. {
  155. conn_info->state = CONN_STATE_DISCONNECTING_DELAYED;
  156. }
  157. static int libais_disconnect (struct conn_info *conn_info)
  158. {
  159. int res = 0;
  160. struct outq_item *outq_item;
  161. if (ais_service_handlers[conn_info->service - 1]->libais_exit_fn) {
  162. res = ais_service_handlers[conn_info->service - 1]->libais_exit_fn (conn_info);
  163. }
  164. /*
  165. * Close the library connection and free its
  166. * data if it hasn't already been freed
  167. */
  168. if (conn_info->state != CONN_STATE_DISCONNECTING) {
  169. conn_info->state = CONN_STATE_DISCONNECTING;
  170. close (conn_info->fd);
  171. /*
  172. * Free the outq queued items
  173. */
  174. while (!queue_is_empty (&conn_info->outq)) {
  175. outq_item = queue_item_get (&conn_info->outq);
  176. free (outq_item->msg);
  177. queue_item_remove (&conn_info->outq);
  178. }
  179. queue_free (&conn_info->outq);
  180. free (conn_info->inb);
  181. }
  182. /*
  183. * If exit_fn didn't request a retry,
  184. * free the conn_info structure
  185. */
  186. if (res != -1) {
  187. free (conn_info);
  188. }
  189. /*
  190. * Inverse res from libais exit fn handler
  191. */
  192. return (res != -1 ? -1 : 0);
  193. }
  194. static int cleanup_send_response (struct conn_info *conn_info) {
  195. struct queue *outq;
  196. int res = 0;
  197. struct outq_item *queue_item;
  198. struct msghdr msg_send;
  199. struct iovec iov_send;
  200. char *msg_addr;
  201. if (!libais_connection_active (conn_info)) {
  202. return (-1);
  203. }
  204. outq = &conn_info->outq;
  205. msg_send.msg_iov = &iov_send;
  206. msg_send.msg_name = 0;
  207. msg_send.msg_namelen = 0;
  208. msg_send.msg_iovlen = 1;
  209. msg_send.msg_control = 0;
  210. msg_send.msg_controllen = 0;
  211. msg_send.msg_flags = 0;
  212. while (!queue_is_empty (outq)) {
  213. queue_item = queue_item_get (outq);
  214. msg_addr = (char *)queue_item->msg;
  215. msg_addr = &msg_addr[conn_info->byte_start];
  216. iov_send.iov_base = msg_addr;
  217. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  218. retry_sendmsg:
  219. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  220. if (res == -1 && errno == EINTR) {
  221. goto retry_sendmsg;
  222. }
  223. if (res == -1 && errno == EAGAIN) {
  224. break; /* outgoing kernel queue full */
  225. }
  226. if (res == -1) {
  227. return (-1); /* message couldn't be sent */
  228. }
  229. if (res + conn_info->byte_start != queue_item->mlen) {
  230. conn_info->byte_start += res;
  231. break;
  232. }
  233. /*
  234. * Message sent, try sending another message
  235. */
  236. queue_item_remove (outq);
  237. conn_info->byte_start = 0;
  238. free (queue_item->msg);
  239. } /* while queue not empty */
  240. if (queue_is_empty (outq)) {
  241. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  242. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  243. }
  244. return (0);
  245. }
  246. extern int libais_send_response (struct conn_info *conn_info,
  247. void *msg, int mlen)
  248. {
  249. struct queue *outq;
  250. char *cmsg;
  251. int res = 0;
  252. int queue_empty;
  253. struct outq_item *queue_item;
  254. struct outq_item queue_item_out;
  255. struct msghdr msg_send;
  256. struct iovec iov_send;
  257. char *msg_addr;
  258. if (!libais_connection_active (conn_info)) {
  259. return (-1);
  260. }
  261. outq = &conn_info->outq;
  262. msg_send.msg_iov = &iov_send;
  263. msg_send.msg_name = 0;
  264. msg_send.msg_namelen = 0;
  265. msg_send.msg_iovlen = 1;
  266. msg_send.msg_control = 0;
  267. msg_send.msg_controllen = 0;
  268. msg_send.msg_flags = 0;
  269. if (queue_is_full (outq)) {
  270. /*
  271. * Start a disconnect if we have not already started one
  272. * and report that the outgoing queue is full
  273. */
  274. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  275. libais_disconnect_delayed (conn_info);
  276. return (-1);
  277. }
  278. while (!queue_is_empty (outq)) {
  279. queue_item = queue_item_get (outq);
  280. msg_addr = (char *)queue_item->msg;
  281. msg_addr = &msg_addr[conn_info->byte_start];
  282. iov_send.iov_base = msg_addr;
  283. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  284. retry_sendmsg:
  285. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  286. if (res == -1 && errno == EINTR) {
  287. goto retry_sendmsg;
  288. }
  289. if (res == -1 && errno == EAGAIN) {
  290. break; /* outgoing kernel queue full */
  291. }
  292. if (res == -1) {
  293. break; /* some other error, stop trying to send message */
  294. }
  295. if (res + conn_info->byte_start != queue_item->mlen) {
  296. conn_info->byte_start += res;
  297. break;
  298. }
  299. /*
  300. * Message sent, try sending another message
  301. */
  302. queue_item_remove (outq);
  303. conn_info->byte_start = 0;
  304. free (queue_item->msg);
  305. } /* while queue not empty */
  306. res = -1;
  307. queue_empty = queue_is_empty (outq);
  308. /*
  309. * Send requested message
  310. */
  311. if (queue_empty) {
  312. iov_send.iov_base = msg;
  313. iov_send.iov_len = mlen;
  314. retry_sendmsg_two:
  315. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  316. if (res == -1 && errno == EINTR) {
  317. goto retry_sendmsg_two;
  318. }
  319. if (res == -1 && errno == EAGAIN) {
  320. conn_info->byte_start = 0;
  321. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  322. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  323. }
  324. if (res != -1) {
  325. if (res + conn_info->byte_start != mlen) {
  326. conn_info->byte_start += res;
  327. res = -1;
  328. } else {
  329. conn_info->byte_start = 0;
  330. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  331. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  332. }
  333. }
  334. }
  335. /*
  336. * If res == -1 , errrno == EAGAIN which means kernel queue full
  337. */
  338. if (res == -1) {
  339. cmsg = malloc (mlen);
  340. if (cmsg == 0) {
  341. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  342. libais_disconnect_delayed (conn_info);
  343. return (-1);
  344. }
  345. queue_item_out.msg = cmsg;
  346. queue_item_out.mlen = mlen;
  347. memcpy (cmsg, msg, mlen);
  348. queue_item_add (outq, &queue_item_out);
  349. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  350. POLLOUT|POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  351. }
  352. return (0);
  353. }
  354. static int poll_handler_libais_accept (
  355. poll_handle handle,
  356. int fd,
  357. int revent,
  358. void *data,
  359. unsigned int *prio)
  360. {
  361. socklen_t addrlen;
  362. struct conn_info *conn_info;
  363. struct sockaddr_un un_addr;
  364. int new_fd;
  365. int on = 1;
  366. addrlen = sizeof (struct sockaddr_un);
  367. retry_accept:
  368. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  369. if (new_fd == -1 && errno == EINTR) {
  370. goto retry_accept;
  371. }
  372. if (new_fd == -1) {
  373. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  374. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  375. }
  376. /*
  377. * Valid accept
  378. */
  379. /*
  380. * Request credentials of sender provided by kernel
  381. */
  382. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  383. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  384. conn_info = conn_info_create (new_fd);
  385. if (conn_info == 0) {
  386. close (new_fd);
  387. return (0); /* This is an error, but -1 would indicate disconnect from poll */
  388. }
  389. poll_dispatch_add (aisexec_poll_handle, new_fd, POLLIN|POLLNVAL, conn_info,
  390. poll_handler_libais_deliver, 0);
  391. // TODO is this needed, or shouldn't it be in conn_info_create ?
  392. memcpy (&conn_info->ais_ci.un_addr, &un_addr, sizeof (struct sockaddr_un));
  393. return (0);
  394. }
  395. struct message_overlay {
  396. struct res_header header;
  397. char buf[4096];
  398. };
  399. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio)
  400. {
  401. int res;
  402. struct conn_info *conn_info = (struct conn_info *)data;
  403. struct req_header *header;
  404. int service;
  405. struct msghdr msg_recv;
  406. struct iovec iov_recv;
  407. struct cmsghdr *cmsg;
  408. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  409. struct ucred *cred;
  410. int on = 0;
  411. int send_ok = 0;
  412. struct message_overlay msg_overlay;
  413. msg_recv.msg_iov = &iov_recv;
  414. msg_recv.msg_iovlen = 1;
  415. msg_recv.msg_name = 0;
  416. msg_recv.msg_namelen = 0;
  417. msg_recv.msg_flags = 0;
  418. if (revent & POLLOUT) {
  419. cleanup_send_response (conn_info);
  420. }
  421. if ((revent & POLLIN) == 0) {
  422. return (0);
  423. }
  424. /*
  425. * Handle delayed disconnections
  426. */
  427. if (conn_info->state != CONN_STATE_ACTIVE) {
  428. res = libais_disconnect (conn_info);
  429. return (res);
  430. }
  431. if (conn_info->authenticated) {
  432. msg_recv.msg_control = 0;
  433. msg_recv.msg_controllen = 0;
  434. } else {
  435. msg_recv.msg_control = (void *)cmsg_cred;
  436. msg_recv.msg_controllen = sizeof (cmsg_cred);
  437. }
  438. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  439. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  440. assert (iov_recv.iov_len != 0);
  441. retry_recv:
  442. res = recvmsg (fd, &msg_recv, MSG_DONTWAIT | MSG_NOSIGNAL);
  443. if (res == -1 && errno == EINTR) {
  444. goto retry_recv;
  445. } else
  446. if (res == -1 && errno != EAGAIN) {
  447. goto error_disconnect;
  448. } else
  449. if (res == 0) {
  450. goto error_disconnect;
  451. return (-1);
  452. }
  453. /*
  454. * Authenticate if this connection has not been authenticated
  455. */
  456. if (conn_info->authenticated == 0) {
  457. cmsg = CMSG_FIRSTHDR (&msg_recv);
  458. cred = (struct ucred *)CMSG_DATA (cmsg);
  459. if (cred) {
  460. if (cred->uid == 0 || cred->gid == gid_valid) {
  461. setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  462. conn_info->authenticated = 1;
  463. }
  464. }
  465. if (conn_info->authenticated == 0) {
  466. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, gid_valid);
  467. }
  468. }
  469. /*
  470. * Dispatch all messages received in recvmsg that can be dispatched
  471. * sizeof (struct req_header) needed at minimum to do any processing
  472. */
  473. conn_info->inb_inuse += res;
  474. conn_info->inb_start += res;
  475. while (conn_info->inb_inuse >= sizeof (struct req_header) && res != -1) {
  476. header = (struct req_header *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  477. if (header->size > conn_info->inb_inuse) {
  478. break;
  479. }
  480. service = conn_info->service;
  481. /*
  482. * If this service is in init phase, initialize service
  483. * else handle message using service handlers
  484. */
  485. if (service == SOCKET_SERVICE_INIT) {
  486. /*
  487. * Initializing service
  488. */
  489. res = ais_service_handlers[header->id]->libais_init_fn (conn_info, header);
  490. } else {
  491. /*
  492. * Not an init service, but a standard service
  493. */
  494. if (header->id < 0 || header->id > ais_service_handlers[service - 1]->libais_handlers_count) {
  495. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  496. header->id, ais_service_handlers[service - 1]->libais_handlers_count);
  497. res = -1;
  498. goto error_disconnect;
  499. }
  500. /*
  501. * Determine if a message can be queued with totempg and if so
  502. * deliver it, otherwise tell the library we are too busy
  503. */
  504. send_ok = totempg_send_ok (1000 + header->size);
  505. if (send_ok) {
  506. // *prio = 0;
  507. res = ais_service_handlers[service - 1]->libais_handlers[header->id].libais_handler_fn(conn_info, header);
  508. } else {
  509. // *prio = (*prio) + 1;
  510. /*
  511. * Overload, tell library to retry
  512. */
  513. msg_overlay.header.size =
  514. ais_service_handlers[service - 1]->libais_handlers[header->id].response_size;
  515. msg_overlay.header.id =
  516. ais_service_handlers[service - 1]->libais_handlers[header->id].response_id;
  517. msg_overlay.header.error = SA_ERR_TRY_AGAIN;
  518. libais_send_response (conn_info, &msg_overlay,
  519. msg_overlay.header.size);
  520. }
  521. }
  522. conn_info->inb_inuse -= header->size;
  523. } /* while */
  524. if (conn_info->inb_inuse == 0) {
  525. conn_info->inb_start = 0;
  526. } else
  527. // BUG if (connections[fd].inb_start + connections[fd].inb_inuse >= SIZEINB) {
  528. if (conn_info->inb_start >= SIZEINB) {
  529. /*
  530. * If in buffer is full, move it back to start
  531. */
  532. memmove (conn_info->inb,
  533. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  534. sizeof (char) * conn_info->inb_inuse);
  535. conn_info->inb_start = conn_info->inb_inuse;
  536. }
  537. return (res);
  538. error_disconnect:
  539. res = libais_disconnect (conn_info);
  540. return (res);
  541. }
  542. extern void print_stats (void);
  543. void sigintr_handler (int signum)
  544. {
  545. #ifdef DEBUG_MEMPOOL
  546. int stats_inuse[MEMPOOL_GROUP_SIZE];
  547. int stats_avail[MEMPOOL_GROUP_SIZE];
  548. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  549. int i;
  550. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  551. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  552. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  553. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  554. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  555. }
  556. #endif
  557. print_stats ();
  558. ais_done (AIS_DONE_EXIT);
  559. }
  560. static struct sched_param sched_param = {
  561. sched_priority: 99
  562. };
  563. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  564. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  565. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  566. static int (*aisexec_handler_fns[AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX]) (void *msg, struct in_addr source_addr, int endian_conversion_required);
  567. static int aisexec_handler_fns_count = 0;
  568. /*
  569. * Builds the handler table as an optimization
  570. */
  571. static void aisexec_handler_fns_build (void)
  572. {
  573. int i, j;
  574. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  575. for (j = 0; j < ais_service_handlers[i]->aisexec_handler_fns_count; j++) {
  576. aisexec_handler_fns[aisexec_handler_fns_count++] =
  577. ais_service_handlers[i]->aisexec_handler_fns[j];
  578. }
  579. }
  580. log_printf (LOG_LEVEL_DEBUG, "built %d handler functions\n", aisexec_handler_fns_count);
  581. }
  582. char delivery_data[MESSAGE_SIZE_MAX];
  583. static void deliver_fn (
  584. struct in_addr source_addr,
  585. struct iovec *iovec,
  586. int iov_len,
  587. int endian_conversion_required)
  588. {
  589. struct req_header *header;
  590. int res;
  591. int pos = 0;
  592. int i;
  593. /*
  594. * Build buffer without iovecs to make processing easier
  595. * This is only used for messages which are multicast with iovecs
  596. * and self-delivered. All other mechanisms avoid the copy.
  597. */
  598. if (iov_len > 1) {
  599. for (i = 0; i < iov_len; i++) {
  600. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  601. pos += iovec[i].iov_len;
  602. assert (pos < MESSAGE_SIZE_MAX);
  603. }
  604. header = (struct req_header *)delivery_data;
  605. } else {
  606. header = (struct req_header *)iovec[0].iov_base;
  607. }
  608. if (endian_conversion_required) {
  609. header->id = swab32 (header->id);
  610. header->size = swab32 (header->size);
  611. }
  612. res = aisexec_handler_fns[header->id](header, source_addr, endian_conversion_required);
  613. }
  614. static void confchg_fn (
  615. enum totempg_configuration_type configuration_type,
  616. struct in_addr *member_list, void *member_list_private,
  617. int member_list_entries,
  618. struct in_addr *left_list, void *left_list_private,
  619. int left_list_entries,
  620. struct in_addr *joined_list, void *joined_list_private,
  621. int joined_list_entries,
  622. struct memb_ring_id *ring_id)
  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, ring_id);
  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. }