main.c 32 KB

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