main.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <assert.h>
  37. #include <pwd.h>
  38. #include <grp.h>
  39. #include <sys/types.h>
  40. #include <sys/poll.h>
  41. #include <sys/uio.h>
  42. #include <sys/mman.h>
  43. #include <sys/socket.h>
  44. #include <sys/un.h>
  45. #include <sys/sysinfo.h>
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <signal.h>
  56. #include <sched.h>
  57. #include <time.h>
  58. #include "../include/ais_types.h"
  59. #include "../include/ais_msg.h"
  60. #include "../include/list.h"
  61. #include "../include/queue.h"
  62. #include "poll.h"
  63. #include "totempg.h"
  64. #include "totemsrp.h"
  65. #include "mempool.h"
  66. #include "parse.h"
  67. #include "main.h"
  68. #include "handlers.h"
  69. #include "sync.h"
  70. #include "evs.h"
  71. #include "clm.h"
  72. #include "amf.h"
  73. #include "ckpt.h"
  74. #include "evt.h"
  75. #include "swab.h"
  76. #define LOG_SERVICE LOG_SERVICE_MAIN
  77. #include "print.h"
  78. #define SERVER_BACKLOG 5
  79. int ais_uid = 0;
  80. int gid_valid = 0;
  81. /*
  82. * All service handlers in the AIS
  83. */
  84. struct service_handler *ais_service_handlers[] = {
  85. &evs_service_handler,
  86. &clm_service_handler,
  87. &amf_service_handler,
  88. &ckpt_service_handler,
  89. &evt_service_handler
  90. };
  91. struct sync_callbacks sync_callbacks[5];
  92. int sync_callback_count;
  93. #define AIS_SERVICE_HANDLERS_COUNT 5
  94. #define AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX 40
  95. /*
  96. * IPC Initializers
  97. */
  98. static int dispatch_init_send_response (struct conn_info *conn_info, void *message);
  99. static int response_init_send_response (struct conn_info *conn_info, void *message);
  100. static int (*ais_init_handlers[]) (struct conn_info *conn_info, void *message) = {
  101. response_init_send_response,
  102. dispatch_init_send_response
  103. };
  104. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio);
  105. enum e_ais_done {
  106. AIS_DONE_EXIT = -1,
  107. AIS_DONE_UID_DETERMINE = -2,
  108. AIS_DONE_GID_DETERMINE = -3,
  109. AIS_DONE_MEMPOOL_INIT = -4,
  110. AIS_DONE_FORK = -5,
  111. AIS_DONE_LIBAIS_SOCKET = -6,
  112. AIS_DONE_LIBAIS_BIND = -7,
  113. AIS_DONE_READKEY = -8,
  114. AIS_DONE_MAINCONFIGREAD = -9,
  115. AIS_DONE_LOGSETUP = -10,
  116. AIS_DONE_AMFCONFIGREAD = -11,
  117. };
  118. static inline void ais_done (enum e_ais_done err)
  119. {
  120. log_printf (LOG_LEVEL_ERROR, "AIS Executive exiting.\n");
  121. poll_destroy (aisexec_poll_handle);
  122. exit (1);
  123. }
  124. static inline struct conn_info *conn_info_create (int fd) {
  125. struct conn_info *conn_info;
  126. int res;
  127. conn_info = malloc (sizeof (struct conn_info));
  128. if (conn_info == 0) {
  129. return (0);
  130. }
  131. memset (conn_info, 0, sizeof (struct conn_info));
  132. res = queue_init (&conn_info->outq, SIZEQUEUE,
  133. sizeof (struct outq_item));
  134. if (res != 0) {
  135. free (conn_info);
  136. return (0);
  137. }
  138. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  139. if (conn_info->inb == 0) {
  140. queue_free (&conn_info->outq);
  141. free (conn_info);
  142. return (0);
  143. }
  144. conn_info->state = CONN_STATE_ACTIVE;
  145. conn_info->fd = fd;
  146. conn_info->service = SOCKET_SERVICE_INIT;
  147. return (conn_info);
  148. }
  149. static void sigusr2_handler (int num)
  150. {
  151. int i;
  152. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  153. if (ais_service_handlers[i]->exec_dump_fn) {
  154. ais_service_handlers[i]->exec_dump_fn ();
  155. }
  156. }
  157. signal (SIGUSR2 ,sigusr2_handler);
  158. return;
  159. }
  160. struct sockaddr_in *this_ip;
  161. struct sockaddr_in this_non_loopback_ip;
  162. struct sockaddr_in config_mcast_addr;
  163. #define LOCALHOST_IP inet_addr("127.0.0.1")
  164. char *socketname = "libais.socket";
  165. static int libais_connection_active (struct conn_info *conn_info)
  166. {
  167. return (conn_info->state == CONN_STATE_ACTIVE);
  168. }
  169. static void libais_disconnect_delayed (struct conn_info *conn_info)
  170. {
  171. conn_info->state = CONN_STATE_DISCONNECTING_DELAYED;
  172. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTING_DELAYED;
  173. }
  174. static int libais_disconnect (struct conn_info *conn_info)
  175. {
  176. int res = 0;
  177. struct outq_item *outq_item;
  178. if (conn_info->should_exit_fn &&
  179. ais_service_handlers[conn_info->service]->libais_exit_fn) {
  180. res = ais_service_handlers[conn_info->service]->libais_exit_fn (conn_info);
  181. }
  182. if (conn_info->conn_info_partner &&
  183. conn_info->conn_info_partner->should_exit_fn &&
  184. ais_service_handlers[conn_info->conn_info_partner->service]->libais_exit_fn) {
  185. res = ais_service_handlers[conn_info->conn_info_partner->service]->libais_exit_fn (conn_info->conn_info_partner);
  186. }
  187. /*
  188. * Close the library connection and free its
  189. * data if it hasn't already been freed
  190. */
  191. if (conn_info->state != CONN_STATE_DISCONNECTING) {
  192. conn_info->state = CONN_STATE_DISCONNECTING;
  193. close (conn_info->fd);
  194. /*
  195. * Free the outq queued items
  196. */
  197. while (!queue_is_empty (&conn_info->outq)) {
  198. outq_item = queue_item_get (&conn_info->outq);
  199. free (outq_item->msg);
  200. queue_item_remove (&conn_info->outq);
  201. }
  202. queue_free (&conn_info->outq);
  203. free (conn_info->inb);
  204. }
  205. /*
  206. * Close the library connection and free its
  207. * data if it hasn't already been freed
  208. */
  209. if (conn_info->conn_info_partner &&
  210. conn_info->conn_info_partner->state != CONN_STATE_DISCONNECTING) {
  211. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTING;
  212. close (conn_info->conn_info_partner->fd);
  213. /*
  214. * Free the outq queued items
  215. */
  216. while (!queue_is_empty (&conn_info->conn_info_partner->outq)) {
  217. outq_item = queue_item_get (&conn_info->conn_info_partner->outq);
  218. free (outq_item->msg);
  219. queue_item_remove (&conn_info->conn_info_partner->outq);
  220. }
  221. queue_free (&conn_info->conn_info_partner->outq);
  222. if (conn_info->conn_info_partner->inb) {
  223. free (conn_info->conn_info_partner->inb);
  224. }
  225. }
  226. /*
  227. * If exit_fn didn't request a retry,
  228. * free the conn_info structure
  229. */
  230. if (res != -1) {
  231. if (conn_info->conn_info_partner) {
  232. poll_dispatch_delete (aisexec_poll_handle,
  233. conn_info->conn_info_partner->fd);
  234. }
  235. poll_dispatch_delete (aisexec_poll_handle, conn_info->fd);
  236. free (conn_info->conn_info_partner);
  237. free (conn_info);
  238. }
  239. /*
  240. * Inverse res from libais exit fn handler
  241. */
  242. return (res != -1 ? -1 : 0);
  243. }
  244. static int cleanup_send_response (struct conn_info *conn_info) {
  245. struct queue *outq;
  246. int res = 0;
  247. struct outq_item *queue_item;
  248. struct msghdr msg_send;
  249. struct iovec iov_send;
  250. char *msg_addr;
  251. if (!libais_connection_active (conn_info)) {
  252. return (-1);
  253. }
  254. outq = &conn_info->outq;
  255. msg_send.msg_iov = &iov_send;
  256. msg_send.msg_name = 0;
  257. msg_send.msg_namelen = 0;
  258. msg_send.msg_iovlen = 1;
  259. msg_send.msg_control = 0;
  260. msg_send.msg_controllen = 0;
  261. msg_send.msg_flags = 0;
  262. while (!queue_is_empty (outq)) {
  263. queue_item = queue_item_get (outq);
  264. msg_addr = (char *)queue_item->msg;
  265. msg_addr = &msg_addr[conn_info->byte_start];
  266. iov_send.iov_base = msg_addr;
  267. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  268. retry_sendmsg:
  269. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  270. if (res == -1 && errno == EINTR) {
  271. goto retry_sendmsg;
  272. }
  273. if (res == -1 && errno == EAGAIN) {
  274. break; /* outgoing kernel queue full */
  275. }
  276. if (res == -1) {
  277. return (-1); /* message couldn't be sent */
  278. }
  279. if (res + conn_info->byte_start != queue_item->mlen) {
  280. conn_info->byte_start += res;
  281. break;
  282. }
  283. /*
  284. * Message sent, try sending another message
  285. */
  286. queue_item_remove (outq);
  287. conn_info->byte_start = 0;
  288. free (queue_item->msg);
  289. } /* while queue not empty */
  290. if (queue_is_empty (outq)) {
  291. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  292. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  293. }
  294. return (0);
  295. }
  296. extern int libais_send_response (struct conn_info *conn_info,
  297. void *msg, int mlen)
  298. {
  299. struct queue *outq;
  300. char *cmsg;
  301. int res = 0;
  302. int queue_empty;
  303. struct outq_item *queue_item;
  304. struct outq_item queue_item_out;
  305. struct msghdr msg_send;
  306. struct iovec iov_send;
  307. char *msg_addr;
  308. if (!libais_connection_active (conn_info)) {
  309. return (-1);
  310. }
  311. outq = &conn_info->outq;
  312. msg_send.msg_iov = &iov_send;
  313. msg_send.msg_name = 0;
  314. msg_send.msg_namelen = 0;
  315. msg_send.msg_iovlen = 1;
  316. msg_send.msg_control = 0;
  317. msg_send.msg_controllen = 0;
  318. msg_send.msg_flags = 0;
  319. if (queue_is_full (outq)) {
  320. /*
  321. * Start a disconnect if we have not already started one
  322. * and report that the outgoing queue is full
  323. */
  324. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  325. libais_disconnect_delayed (conn_info);
  326. return (-1);
  327. }
  328. while (!queue_is_empty (outq)) {
  329. queue_item = queue_item_get (outq);
  330. msg_addr = (char *)queue_item->msg;
  331. msg_addr = &msg_addr[conn_info->byte_start];
  332. iov_send.iov_base = msg_addr;
  333. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  334. retry_sendmsg:
  335. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  336. if (res == -1 && errno == EINTR) {
  337. goto retry_sendmsg;
  338. }
  339. if (res == -1 && errno == EAGAIN) {
  340. break; /* outgoing kernel queue full */
  341. }
  342. if (res == -1) {
  343. break; /* some other error, stop trying to send message */
  344. }
  345. if (res + conn_info->byte_start != queue_item->mlen) {
  346. conn_info->byte_start += res;
  347. break;
  348. }
  349. /*
  350. * Message sent, try sending another message
  351. */
  352. queue_item_remove (outq);
  353. conn_info->byte_start = 0;
  354. free (queue_item->msg);
  355. } /* while queue not empty */
  356. res = -1;
  357. queue_empty = queue_is_empty (outq);
  358. /*
  359. * Send requested message
  360. */
  361. if (queue_empty) {
  362. iov_send.iov_base = msg;
  363. iov_send.iov_len = mlen;
  364. retry_sendmsg_two:
  365. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  366. if (res == -1 && errno == EINTR) {
  367. goto retry_sendmsg_two;
  368. }
  369. if (res == -1 && errno == EAGAIN) {
  370. conn_info->byte_start = 0;
  371. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  372. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  373. }
  374. if (res != -1) {
  375. if (res + conn_info->byte_start != mlen) {
  376. conn_info->byte_start += res;
  377. res = -1;
  378. } else {
  379. conn_info->byte_start = 0;
  380. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  381. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  382. }
  383. }
  384. }
  385. /*
  386. * If res == -1 , errrno == EAGAIN which means kernel queue full
  387. */
  388. if (res == -1) {
  389. cmsg = malloc (mlen);
  390. if (cmsg == 0) {
  391. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  392. libais_disconnect_delayed (conn_info);
  393. return (-1);
  394. }
  395. queue_item_out.msg = cmsg;
  396. queue_item_out.mlen = mlen;
  397. memcpy (cmsg, msg, mlen);
  398. queue_item_add (outq, &queue_item_out);
  399. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  400. POLLOUT|POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  401. }
  402. return (0);
  403. }
  404. static int poll_handler_libais_accept (
  405. poll_handle handle,
  406. int fd,
  407. int revent,
  408. void *data,
  409. unsigned int *prio)
  410. {
  411. socklen_t addrlen;
  412. struct conn_info *conn_info;
  413. struct sockaddr_un un_addr;
  414. int new_fd;
  415. int on = 1;
  416. addrlen = sizeof (struct sockaddr_un);
  417. retry_accept:
  418. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  419. if (new_fd == -1 && errno == EINTR) {
  420. goto retry_accept;
  421. }
  422. if (new_fd == -1) {
  423. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  424. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  425. }
  426. /*
  427. * Valid accept
  428. */
  429. /*
  430. * Request credentials of sender provided by kernel
  431. */
  432. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  433. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  434. conn_info = conn_info_create (new_fd);
  435. if (conn_info == 0) {
  436. close (new_fd);
  437. return (0); /* This is an error, but -1 would indicate disconnect from poll */
  438. }
  439. poll_dispatch_add (aisexec_poll_handle, new_fd, POLLIN|POLLNVAL, conn_info,
  440. poll_handler_libais_deliver, 0);
  441. // TODO is this needed, or shouldn't it be in conn_info_create ?
  442. memcpy (&conn_info->ais_ci.un_addr, &un_addr, sizeof (struct sockaddr_un));
  443. return (0);
  444. }
  445. static int dispatch_init_send_response (struct conn_info *conn_info, void *message)
  446. {
  447. SaErrorT error = SA_ERR_ACCESS;
  448. struct req_lib_dispatch_init *req_lib_dispatch_init = (struct req_lib_dispatch_init *)message;
  449. struct res_lib_dispatch_init res_lib_dispatch_init;
  450. struct conn_info *msg_conn_info;
  451. if (conn_info->authenticated) {
  452. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  453. error = SA_OK;
  454. conn_info->conn_info_partner = (struct conn_info *)req_lib_dispatch_init->conn_info;
  455. msg_conn_info = (struct conn_info *)req_lib_dispatch_init->conn_info;
  456. msg_conn_info->conn_info_partner = conn_info;
  457. }
  458. res_lib_dispatch_init.header.size = sizeof (struct res_lib_dispatch_init);
  459. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  460. res_lib_dispatch_init.header.error = error;
  461. libais_send_response (conn_info, &res_lib_dispatch_init,
  462. sizeof (res_lib_dispatch_init));
  463. if (error == SA_ERR_ACCESS) {
  464. return (-1);
  465. }
  466. conn_info->should_exit_fn = 1;
  467. ais_service_handlers[req_lib_dispatch_init->resdis_header.service]->libais_init_two_fn (conn_info);
  468. return (0);
  469. }
  470. static int response_init_send_response (struct conn_info *conn_info, void *message)
  471. {
  472. SaErrorT error = SA_ERR_ACCESS;
  473. struct req_lib_response_init *req_lib_response_init = (struct req_lib_response_init *)message;
  474. struct res_lib_response_init res_lib_response_init;
  475. if (conn_info->authenticated) {
  476. conn_info->service = req_lib_response_init->resdis_header.service;
  477. error = SA_OK;
  478. }
  479. res_lib_response_init.header.size = sizeof (struct res_lib_response_init);
  480. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  481. res_lib_response_init.header.error = error;
  482. res_lib_response_init.conn_info = (unsigned long)conn_info;
  483. libais_send_response (conn_info, &res_lib_response_init,
  484. sizeof (res_lib_response_init));
  485. if (error == SA_ERR_ACCESS) {
  486. return (-1);
  487. }
  488. conn_info->should_exit_fn = 0;
  489. return (0);
  490. }
  491. struct res_overlay {
  492. struct res_header header;
  493. char buf[4096];
  494. };
  495. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio)
  496. {
  497. int res;
  498. struct conn_info *conn_info = (struct conn_info *)data;
  499. struct req_header *header;
  500. int service;
  501. struct msghdr msg_recv;
  502. struct iovec iov_recv;
  503. struct cmsghdr *cmsg;
  504. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  505. struct ucred *cred;
  506. int on = 0;
  507. int send_ok = 0;
  508. struct res_overlay res_overlay;
  509. if (revent & (POLLERR|POLLHUP)) {
  510. res = libais_disconnect (conn_info);
  511. return (res);
  512. }
  513. /*
  514. * Handle delayed disconnections
  515. */
  516. if (conn_info->state == CONN_STATE_DISCONNECTING_DELAYED) {
  517. res = libais_disconnect (conn_info);
  518. return (res);
  519. }
  520. if (conn_info->state == CONN_STATE_DISCONNECTING) {
  521. return (0);
  522. }
  523. if (revent & POLLOUT) {
  524. cleanup_send_response (conn_info);
  525. }
  526. if ((revent & POLLIN) == 0) {
  527. return (0);
  528. }
  529. msg_recv.msg_iov = &iov_recv;
  530. msg_recv.msg_iovlen = 1;
  531. msg_recv.msg_name = 0;
  532. msg_recv.msg_namelen = 0;
  533. msg_recv.msg_flags = 0;
  534. if (conn_info->authenticated) {
  535. msg_recv.msg_control = 0;
  536. msg_recv.msg_controllen = 0;
  537. } else {
  538. msg_recv.msg_control = (void *)cmsg_cred;
  539. msg_recv.msg_controllen = sizeof (cmsg_cred);
  540. }
  541. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  542. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  543. assert (iov_recv.iov_len != 0);
  544. retry_recv:
  545. res = recvmsg (fd, &msg_recv, MSG_DONTWAIT | MSG_NOSIGNAL);
  546. if (res == -1 && errno == EINTR) {
  547. goto retry_recv;
  548. } else
  549. if (res == -1 && errno != EAGAIN) {
  550. goto error_disconnect;
  551. } else
  552. if (res == 0) {
  553. goto error_disconnect;
  554. return (-1);
  555. }
  556. /*
  557. * Authenticate if this connection has not been authenticated
  558. */
  559. if (conn_info->authenticated == 0) {
  560. cmsg = CMSG_FIRSTHDR (&msg_recv);
  561. cred = (struct ucred *)CMSG_DATA (cmsg);
  562. if (cred) {
  563. if (cred->uid == 0 || cred->gid == gid_valid) {
  564. setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  565. conn_info->authenticated = 1;
  566. }
  567. }
  568. if (conn_info->authenticated == 0) {
  569. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, gid_valid);
  570. }
  571. }
  572. /*
  573. * Dispatch all messages received in recvmsg that can be dispatched
  574. * sizeof (struct req_header) needed at minimum to do any processing
  575. */
  576. conn_info->inb_inuse += res;
  577. conn_info->inb_start += res;
  578. while (conn_info->inb_inuse >= sizeof (struct req_header) && res != -1) {
  579. header = (struct req_header *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  580. if (header->size > conn_info->inb_inuse) {
  581. break;
  582. }
  583. service = conn_info->service;
  584. /*
  585. * If this service is in init phase, initialize service
  586. * else handle message using service handlers
  587. */
  588. if (service == SOCKET_SERVICE_INIT) {
  589. res = ais_init_handlers[header->id] (conn_info, header);
  590. // TODO error in init_two_fn needs to be handled
  591. } else {
  592. /*
  593. * Not an init service, but a standard service
  594. */
  595. if (header->id < 0 || header->id > ais_service_handlers[service]->libais_handlers_count) {
  596. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  597. header->id, ais_service_handlers[service]->libais_handlers_count);
  598. res = -1;
  599. goto error_disconnect;
  600. }
  601. /*
  602. * If flow control is required of the library handle, determine that
  603. * openais is not in synchronization and that totempg has room available
  604. * to queue a message, otherwise tell the library we are busy and to
  605. * try again later
  606. */
  607. send_ok =
  608. (ais_service_handlers[service]->libais_handlers[header->id].flow_control == FLOW_CONTROL_NOT_REQUIRED) ||
  609. ((ais_service_handlers[service]->libais_handlers[header->id].flow_control == FLOW_CONTROL_REQUIRED) &&
  610. (totempg_send_ok (1000 + header->size)) &&
  611. (sync_in_process() == 0));
  612. if (send_ok) {
  613. // *prio = 0;
  614. res = ais_service_handlers[service]->libais_handlers[header->id].libais_handler_fn(conn_info, header);
  615. } else {
  616. // *prio = (*prio) + 1;
  617. /*
  618. * Overload, tell library to retry
  619. */
  620. res_overlay.header.size =
  621. ais_service_handlers[service]->libais_handlers[header->id].response_size;
  622. res_overlay.header.id =
  623. ais_service_handlers[service]->libais_handlers[header->id].response_id;
  624. res_overlay.header.error = SA_ERR_TRY_AGAIN;
  625. libais_send_response (conn_info, &res_overlay,
  626. res_overlay.header.size);
  627. }
  628. }
  629. conn_info->inb_inuse -= header->size;
  630. } /* while */
  631. if (conn_info->inb_inuse == 0) {
  632. conn_info->inb_start = 0;
  633. } else
  634. // BUG if (connections[fd].inb_start + connections[fd].inb_inuse >= SIZEINB) {
  635. if (conn_info->inb_start >= SIZEINB) {
  636. /*
  637. * If in buffer is full, move it back to start
  638. */
  639. memmove (conn_info->inb,
  640. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  641. sizeof (char) * conn_info->inb_inuse);
  642. conn_info->inb_start = conn_info->inb_inuse;
  643. }
  644. return (res);
  645. error_disconnect:
  646. res = libais_disconnect (conn_info);
  647. return (res);
  648. }
  649. extern void print_stats (void);
  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. print_stats ();
  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 in_addr 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 in_addr 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 in_addr *member_list, int member_list_entries,
  751. struct in_addr *left_list, int left_list_entries,
  752. struct in_addr *joined_list, int joined_list_entries,
  753. struct memb_ring_id *ring_id)
  754. {
  755. int i;
  756. if (this_ip->sin_addr.s_addr != LOCALHOST_IP) {
  757. memcpy(&this_non_loopback_ip, this_ip, sizeof(struct sockaddr_in));
  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 (void)
  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 ();
  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. void aisexec_keyread (unsigned char *key)
  897. {
  898. int fd;
  899. int res;
  900. fd = open ("/etc/ais/authkey", O_RDONLY);
  901. if (fd == -1) {
  902. log_printf (LOG_LEVEL_ERROR, "Could not open /etc/ais/authkey: %s\n", strerror (errno));
  903. ais_done (AIS_DONE_READKEY);
  904. }
  905. res = read (fd, key, 128);
  906. if (res == -1) {
  907. log_printf (LOG_LEVEL_ERROR, "Could not read /etc/ais/authkey: %s\n", strerror (errno));
  908. ais_done (AIS_DONE_READKEY);
  909. }
  910. if (res != 128) {
  911. log_printf (LOG_LEVEL_ERROR, "Could only read %d bits of 1024 bits from /etc/ais/authkey.\n", res * 8);
  912. ais_done (AIS_DONE_READKEY);
  913. }
  914. close (fd);
  915. }
  916. int message_source_is_local(struct message_source *source)
  917. {
  918. int ret = 0;
  919. if ((source->in_addr.s_addr == LOCALHOST_IP)
  920. ||(source->in_addr.s_addr == this_non_loopback_ip.sin_addr.s_addr)) {
  921. ret = 1;
  922. }
  923. return ret;
  924. }
  925. void message_source_set (struct message_source *source, struct conn_info *conn_info)
  926. {
  927. source->in_addr.s_addr = this_ip->sin_addr.s_addr;
  928. source->conn_info = conn_info;
  929. }
  930. int main (int argc, char **argv)
  931. {
  932. int libais_server_fd;
  933. int res;
  934. unsigned char private_key[128];
  935. char *error_string;
  936. struct openais_config openais_config;
  937. memset(&this_non_loopback_ip, 0, sizeof(struct sockaddr_in));
  938. this_non_loopback_ip.sin_addr.s_addr = LOCALHOST_IP;
  939. aisexec_uid_determine ();
  940. aisexec_gid_determine ();
  941. aisexec_poll_handle = poll_create ();
  942. signal (SIGUSR2, sigusr2_handler);
  943. /*
  944. * if totempg_initialize doesn't have root priveleges, it cannot
  945. * bind to a specific interface. This only matters if
  946. * there is more then one interface in a system, so
  947. * in this case, only a warning is printed
  948. */
  949. /*
  950. * Initialize group messaging interface with multicast address
  951. */
  952. res = openais_main_config_read (&error_string, &openais_config, 1);
  953. if (res == -1) {
  954. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2004 MontaVista Software, Inc and contributors.\n");
  955. log_printf (LOG_LEVEL_ERROR, error_string);
  956. ais_done (AIS_DONE_MAINCONFIGREAD);
  957. }
  958. memcpy (&config_mcast_addr, &openais_config.mcast_addr, sizeof (struct sockaddr_in));
  959. res = log_setup (&error_string, openais_config.logmode, openais_config.logfile);
  960. if (res == -1) {
  961. log_printf (LOG_LEVEL_ERROR, error_string);
  962. ais_done (AIS_DONE_LOGSETUP);
  963. }
  964. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2004 MontaVista Software, Inc. and contributors.\n");
  965. /*
  966. * Set round robin realtime scheduling with priority 99
  967. * Lock all memory to avoid page faults which may interrupt
  968. * application healthchecking
  969. */
  970. aisexec_setscheduler ();
  971. aisexec_mlockall ();
  972. aisexec_keyread (private_key);
  973. totempg_log_printf_init (internal_log_printf,
  974. mklog (LOG_LEVEL_SECURITY, LOG_SERVICE_GMI),
  975. mklog (LOG_LEVEL_ERROR, LOG_SERVICE_GMI),
  976. mklog (LOG_LEVEL_WARNING, LOG_SERVICE_GMI),
  977. mklog (LOG_LEVEL_NOTICE, LOG_SERVICE_GMI),
  978. mklog (LOG_LEVEL_DEBUG, LOG_SERVICE_GMI));
  979. totempg_initialize (&openais_config.mcast_addr, openais_config.interfaces, 1,
  980. &aisexec_poll_handle,
  981. private_key,
  982. sizeof (private_key),
  983. 0,
  984. 0,
  985. deliver_fn, confchg_fn,
  986. openais_config.timeouts);
  987. this_ip = &openais_config.interfaces[0].boundto;
  988. /*
  989. * Drop root privleges to user 'ais'
  990. * TODO: Don't really need full root capabilities;
  991. * needed capabilities are:
  992. * CAP_NET_RAW (bindtodevice)
  993. * CAP_SYS_NICE (setscheduler)
  994. * CAP_IPC_LOCK (mlockall)
  995. */
  996. aisexec_priv_drop ();
  997. aisexec_handler_fns_build ();
  998. aisexec_sync_fns_build ();
  999. aisexec_mempool_init ();
  1000. res = openais_amf_config_read (&error_string);
  1001. if (res == -1) {
  1002. log_printf (LOG_LEVEL_ERROR, error_string);
  1003. ais_done (AIS_DONE_AMFCONFIGREAD);
  1004. }
  1005. aisexec_tty_detach ();
  1006. signal (SIGINT, sigintr_handler);
  1007. aisexec_service_handlers_init ();
  1008. aisexec_libais_bind (&libais_server_fd);
  1009. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: started and ready to receive connections.\n");
  1010. /*
  1011. * Setup libais connection dispatch routine
  1012. */
  1013. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  1014. POLLIN, 0, poll_handler_libais_accept, 0);
  1015. /*
  1016. * Join multicast group and setup delivery
  1017. * and configuration change functions
  1018. */
  1019. /*
  1020. * Start main processing loop
  1021. */
  1022. poll_run (aisexec_poll_handle);
  1023. return (0);
  1024. }