main.c 32 KB

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