main.c 32 KB

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