main.c 32 KB

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