main.c 25 KB

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