main.c 28 KB

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