main.c 32 KB

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