main.c 24 KB

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