main.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <assert.h>
  35. #include <pwd.h>
  36. #include <grp.h>
  37. #include <sys/types.h>
  38. #include <sys/poll.h>
  39. #include <sys/uio.h>
  40. #include <sys/mman.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sys/sysinfo.h>
  44. #include <sys/time.h>
  45. #include <sys/resource.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <signal.h>
  54. #include <sched.h>
  55. #include <time.h>
  56. #include "../include/ais_types.h"
  57. #include "../include/ais_msg.h"
  58. #include "../include/list.h"
  59. #include "../include/queue.h"
  60. #include "poll.h"
  61. #include "totempg.h"
  62. #include "totemsrp.h"
  63. #include "mempool.h"
  64. #include "parse.h"
  65. #include "main.h"
  66. #include "handlers.h"
  67. #include "sync.h"
  68. #include "evs.h"
  69. #include "clm.h"
  70. #include "amf.h"
  71. #include "ckpt.h"
  72. #include "evt.h"
  73. #include "swab.h"
  74. #define LOG_SERVICE LOG_SERVICE_MAIN
  75. #include "print.h"
  76. #define SERVER_BACKLOG 5
  77. int ais_uid = 0;
  78. int gid_valid = 0;
  79. /*
  80. * All service handlers in the AIS
  81. */
  82. struct service_handler *ais_service_handlers[] = {
  83. &evs_service_handler,
  84. &clm_service_handler,
  85. &amf_service_handler,
  86. &ckpt_service_handler,
  87. &evt_service_handler
  88. };
  89. struct sync_callbacks sync_callbacks[5];
  90. int sync_callback_count;
  91. #define AIS_SERVICE_HANDLERS_COUNT 5
  92. #define AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX 40
  93. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio);
  94. enum e_ais_done {
  95. AIS_DONE_EXIT = -1,
  96. AIS_DONE_UID_DETERMINE = -2,
  97. AIS_DONE_GID_DETERMINE = -3,
  98. AIS_DONE_MEMPOOL_INIT = -4,
  99. AIS_DONE_FORK = -5,
  100. AIS_DONE_LIBAIS_SOCKET = -6,
  101. AIS_DONE_LIBAIS_BIND = -7,
  102. AIS_DONE_READKEY = -8,
  103. AIS_DONE_MAINCONFIGREAD = -9,
  104. AIS_DONE_LOGSETUP = -10,
  105. AIS_DONE_AMFCONFIGREAD = -11,
  106. };
  107. static inline void ais_done (enum e_ais_done err)
  108. {
  109. log_printf (LOG_LEVEL_ERROR, "AIS Executive exiting.\n");
  110. poll_destroy (aisexec_poll_handle);
  111. exit (1);
  112. }
  113. static inline struct conn_info *conn_info_create (int fd) {
  114. struct conn_info *conn_info;
  115. int res;
  116. conn_info = malloc (sizeof (struct conn_info));
  117. if (conn_info == 0) {
  118. return (0);
  119. }
  120. memset (conn_info, 0, sizeof (struct conn_info));
  121. res = queue_init (&conn_info->outq, SIZEQUEUE,
  122. sizeof (struct outq_item));
  123. if (res != 0) {
  124. free (conn_info);
  125. return (0);
  126. }
  127. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  128. if (conn_info->inb == 0) {
  129. queue_free (&conn_info->outq);
  130. free (conn_info);
  131. return (0);
  132. }
  133. conn_info->state = CONN_STATE_ACTIVE;
  134. conn_info->fd = fd;
  135. conn_info->service = SOCKET_SERVICE_INIT;
  136. return (conn_info);
  137. }
  138. static void sigusr2_handler (int num)
  139. {
  140. int i;
  141. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  142. if (ais_service_handlers[i]->exec_dump_fn) {
  143. ais_service_handlers[i]->exec_dump_fn ();
  144. }
  145. }
  146. signal (SIGUSR2 ,sigusr2_handler);
  147. return;
  148. }
  149. struct sockaddr_in *this_ip;
  150. struct sockaddr_in this_non_loopback_ip;
  151. struct sockaddr_in config_mcast_addr;
  152. #define LOCALHOST_IP inet_addr("127.0.0.1")
  153. char *socketname = "libais.socket";
  154. static int libais_connection_active (struct conn_info *conn_info)
  155. {
  156. return (conn_info->state == CONN_STATE_ACTIVE);
  157. }
  158. static void libais_disconnect_delayed (struct conn_info *conn_info)
  159. {
  160. conn_info->state = CONN_STATE_DISCONNECTING_DELAYED;
  161. }
  162. static int libais_disconnect (struct conn_info *conn_info)
  163. {
  164. int res = 0;
  165. struct outq_item *outq_item;
  166. if (ais_service_handlers[conn_info->service - 1]->libais_exit_fn) {
  167. res = ais_service_handlers[conn_info->service - 1]->libais_exit_fn (conn_info);
  168. }
  169. /*
  170. * Close the library connection and free its
  171. * data if it hasn't already been freed
  172. */
  173. if (conn_info->state != CONN_STATE_DISCONNECTING) {
  174. conn_info->state = CONN_STATE_DISCONNECTING;
  175. close (conn_info->fd);
  176. /*
  177. * Free the outq queued items
  178. */
  179. while (!queue_is_empty (&conn_info->outq)) {
  180. outq_item = queue_item_get (&conn_info->outq);
  181. free (outq_item->msg);
  182. queue_item_remove (&conn_info->outq);
  183. }
  184. queue_free (&conn_info->outq);
  185. free (conn_info->inb);
  186. }
  187. /*
  188. * If exit_fn didn't request a retry,
  189. * free the conn_info structure
  190. */
  191. if (res != -1) {
  192. free (conn_info);
  193. }
  194. /*
  195. * Inverse res from libais exit fn handler
  196. */
  197. return (res != -1 ? -1 : 0);
  198. }
  199. static int cleanup_send_response (struct conn_info *conn_info) {
  200. struct queue *outq;
  201. int res = 0;
  202. struct outq_item *queue_item;
  203. struct msghdr msg_send;
  204. struct iovec iov_send;
  205. char *msg_addr;
  206. if (!libais_connection_active (conn_info)) {
  207. return (-1);
  208. }
  209. outq = &conn_info->outq;
  210. msg_send.msg_iov = &iov_send;
  211. msg_send.msg_name = 0;
  212. msg_send.msg_namelen = 0;
  213. msg_send.msg_iovlen = 1;
  214. msg_send.msg_control = 0;
  215. msg_send.msg_controllen = 0;
  216. msg_send.msg_flags = 0;
  217. while (!queue_is_empty (outq)) {
  218. queue_item = queue_item_get (outq);
  219. msg_addr = (char *)queue_item->msg;
  220. msg_addr = &msg_addr[conn_info->byte_start];
  221. iov_send.iov_base = msg_addr;
  222. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  223. retry_sendmsg:
  224. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  225. if (res == -1 && errno == EINTR) {
  226. goto retry_sendmsg;
  227. }
  228. if (res == -1 && errno == EAGAIN) {
  229. break; /* outgoing kernel queue full */
  230. }
  231. if (res == -1) {
  232. return (-1); /* message couldn't be sent */
  233. }
  234. if (res + conn_info->byte_start != queue_item->mlen) {
  235. conn_info->byte_start += res;
  236. break;
  237. }
  238. /*
  239. * Message sent, try sending another message
  240. */
  241. queue_item_remove (outq);
  242. conn_info->byte_start = 0;
  243. free (queue_item->msg);
  244. } /* while queue not empty */
  245. if (queue_is_empty (outq)) {
  246. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  247. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  248. }
  249. return (0);
  250. }
  251. extern int libais_send_response (struct conn_info *conn_info,
  252. void *msg, int mlen)
  253. {
  254. struct queue *outq;
  255. char *cmsg;
  256. int res = 0;
  257. int queue_empty;
  258. struct outq_item *queue_item;
  259. struct outq_item queue_item_out;
  260. struct msghdr msg_send;
  261. struct iovec iov_send;
  262. char *msg_addr;
  263. if (!libais_connection_active (conn_info)) {
  264. return (-1);
  265. }
  266. outq = &conn_info->outq;
  267. msg_send.msg_iov = &iov_send;
  268. msg_send.msg_name = 0;
  269. msg_send.msg_namelen = 0;
  270. msg_send.msg_iovlen = 1;
  271. msg_send.msg_control = 0;
  272. msg_send.msg_controllen = 0;
  273. msg_send.msg_flags = 0;
  274. if (queue_is_full (outq)) {
  275. /*
  276. * Start a disconnect if we have not already started one
  277. * and report that the outgoing queue is full
  278. */
  279. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  280. libais_disconnect_delayed (conn_info);
  281. return (-1);
  282. }
  283. while (!queue_is_empty (outq)) {
  284. queue_item = queue_item_get (outq);
  285. msg_addr = (char *)queue_item->msg;
  286. msg_addr = &msg_addr[conn_info->byte_start];
  287. iov_send.iov_base = msg_addr;
  288. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  289. retry_sendmsg:
  290. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  291. if (res == -1 && errno == EINTR) {
  292. goto retry_sendmsg;
  293. }
  294. if (res == -1 && errno == EAGAIN) {
  295. break; /* outgoing kernel queue full */
  296. }
  297. if (res == -1) {
  298. break; /* some other error, stop trying to send message */
  299. }
  300. if (res + conn_info->byte_start != queue_item->mlen) {
  301. conn_info->byte_start += res;
  302. break;
  303. }
  304. /*
  305. * Message sent, try sending another message
  306. */
  307. queue_item_remove (outq);
  308. conn_info->byte_start = 0;
  309. free (queue_item->msg);
  310. } /* while queue not empty */
  311. res = -1;
  312. queue_empty = queue_is_empty (outq);
  313. /*
  314. * Send requested message
  315. */
  316. if (queue_empty) {
  317. iov_send.iov_base = msg;
  318. iov_send.iov_len = mlen;
  319. retry_sendmsg_two:
  320. res = sendmsg (conn_info->fd, &msg_send, MSG_DONTWAIT | MSG_NOSIGNAL);
  321. if (res == -1 && errno == EINTR) {
  322. goto retry_sendmsg_two;
  323. }
  324. if (res == -1 && errno == EAGAIN) {
  325. conn_info->byte_start = 0;
  326. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  327. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  328. }
  329. if (res != -1) {
  330. if (res + conn_info->byte_start != mlen) {
  331. conn_info->byte_start += res;
  332. res = -1;
  333. } else {
  334. conn_info->byte_start = 0;
  335. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  336. POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  337. }
  338. }
  339. }
  340. /*
  341. * If res == -1 , errrno == EAGAIN which means kernel queue full
  342. */
  343. if (res == -1) {
  344. cmsg = malloc (mlen);
  345. if (cmsg == 0) {
  346. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  347. libais_disconnect_delayed (conn_info);
  348. return (-1);
  349. }
  350. queue_item_out.msg = cmsg;
  351. queue_item_out.mlen = mlen;
  352. memcpy (cmsg, msg, mlen);
  353. queue_item_add (outq, &queue_item_out);
  354. poll_dispatch_modify (aisexec_poll_handle, conn_info->fd,
  355. POLLOUT|POLLIN|POLLNVAL, poll_handler_libais_deliver, 0);
  356. }
  357. return (0);
  358. }
  359. static int poll_handler_libais_accept (
  360. poll_handle handle,
  361. int fd,
  362. int revent,
  363. void *data,
  364. unsigned int *prio)
  365. {
  366. socklen_t addrlen;
  367. struct conn_info *conn_info;
  368. struct sockaddr_un un_addr;
  369. int new_fd;
  370. int on = 1;
  371. addrlen = sizeof (struct sockaddr_un);
  372. retry_accept:
  373. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  374. if (new_fd == -1 && errno == EINTR) {
  375. goto retry_accept;
  376. }
  377. if (new_fd == -1) {
  378. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  379. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  380. }
  381. /*
  382. * Valid accept
  383. */
  384. /*
  385. * Request credentials of sender provided by kernel
  386. */
  387. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  388. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  389. conn_info = conn_info_create (new_fd);
  390. if (conn_info == 0) {
  391. close (new_fd);
  392. return (0); /* This is an error, but -1 would indicate disconnect from poll */
  393. }
  394. poll_dispatch_add (aisexec_poll_handle, new_fd, POLLIN|POLLNVAL, conn_info,
  395. poll_handler_libais_deliver, 0);
  396. // TODO is this needed, or shouldn't it be in conn_info_create ?
  397. memcpy (&conn_info->ais_ci.un_addr, &un_addr, sizeof (struct sockaddr_un));
  398. return (0);
  399. }
  400. struct message_overlay {
  401. struct res_header header;
  402. char buf[4096];
  403. };
  404. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio)
  405. {
  406. int res;
  407. struct conn_info *conn_info = (struct conn_info *)data;
  408. struct req_header *header;
  409. int service;
  410. struct msghdr msg_recv;
  411. struct iovec iov_recv;
  412. struct cmsghdr *cmsg;
  413. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  414. struct ucred *cred;
  415. int on = 0;
  416. int send_ok = 0;
  417. struct message_overlay msg_overlay;
  418. msg_recv.msg_iov = &iov_recv;
  419. msg_recv.msg_iovlen = 1;
  420. msg_recv.msg_name = 0;
  421. msg_recv.msg_namelen = 0;
  422. msg_recv.msg_flags = 0;
  423. if (revent & POLLOUT) {
  424. cleanup_send_response (conn_info);
  425. }
  426. if ((revent & POLLIN) == 0) {
  427. return (0);
  428. }
  429. /*
  430. * Handle delayed disconnections
  431. */
  432. if (conn_info->state != CONN_STATE_ACTIVE) {
  433. res = libais_disconnect (conn_info);
  434. return (res);
  435. }
  436. if (conn_info->authenticated) {
  437. msg_recv.msg_control = 0;
  438. msg_recv.msg_controllen = 0;
  439. } else {
  440. msg_recv.msg_control = (void *)cmsg_cred;
  441. msg_recv.msg_controllen = sizeof (cmsg_cred);
  442. }
  443. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  444. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  445. assert (iov_recv.iov_len != 0);
  446. retry_recv:
  447. res = recvmsg (fd, &msg_recv, MSG_DONTWAIT | MSG_NOSIGNAL);
  448. if (res == -1 && errno == EINTR) {
  449. goto retry_recv;
  450. } else
  451. if (res == -1 && errno != EAGAIN) {
  452. goto error_disconnect;
  453. } else
  454. if (res == 0) {
  455. goto error_disconnect;
  456. return (-1);
  457. }
  458. /*
  459. * Authenticate if this connection has not been authenticated
  460. */
  461. if (conn_info->authenticated == 0) {
  462. cmsg = CMSG_FIRSTHDR (&msg_recv);
  463. cred = (struct ucred *)CMSG_DATA (cmsg);
  464. if (cred) {
  465. if (cred->uid == 0 || cred->gid == gid_valid) {
  466. setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  467. conn_info->authenticated = 1;
  468. }
  469. }
  470. if (conn_info->authenticated == 0) {
  471. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, gid_valid);
  472. }
  473. }
  474. /*
  475. * Dispatch all messages received in recvmsg that can be dispatched
  476. * sizeof (struct req_header) needed at minimum to do any processing
  477. */
  478. conn_info->inb_inuse += res;
  479. conn_info->inb_start += res;
  480. while (conn_info->inb_inuse >= sizeof (struct req_header) && res != -1) {
  481. header = (struct req_header *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  482. if (header->size > conn_info->inb_inuse) {
  483. break;
  484. }
  485. service = conn_info->service;
  486. /*
  487. * If this service is in init phase, initialize service
  488. * else handle message using service handlers
  489. */
  490. if (service == SOCKET_SERVICE_INIT) {
  491. /*
  492. * Initializing service
  493. */
  494. res = ais_service_handlers[header->id]->libais_init_fn (conn_info, header);
  495. } else {
  496. /*
  497. * Not an init service, but a standard service
  498. */
  499. if (header->id < 0 || header->id > ais_service_handlers[service - 1]->libais_handlers_count) {
  500. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  501. header->id, ais_service_handlers[service - 1]->libais_handlers_count);
  502. res = -1;
  503. goto error_disconnect;
  504. }
  505. /*
  506. * If flow control is required of the library handle, determine that
  507. * openais is not in synchronization and that totempg has room available
  508. * to queue a message, otherwise tell the library we are busy and to
  509. * try again later
  510. */
  511. send_ok =
  512. (ais_service_handlers[service - 1]->libais_handlers[header->id].flow_control == FLOW_CONTROL_NOT_REQUIRED) ||
  513. ((ais_service_handlers[service - 1]->libais_handlers[header->id].flow_control == FLOW_CONTROL_REQUIRED) &&
  514. (totempg_send_ok (1000 + header->size)) &&
  515. (sync_in_process() == 0));
  516. if (send_ok) {
  517. // *prio = 0;
  518. res = ais_service_handlers[service - 1]->libais_handlers[header->id].libais_handler_fn(conn_info, header);
  519. } else {
  520. // *prio = (*prio) + 1;
  521. /*
  522. * Overload, tell library to retry
  523. */
  524. msg_overlay.header.size =
  525. ais_service_handlers[service - 1]->libais_handlers[header->id].response_size;
  526. msg_overlay.header.id =
  527. ais_service_handlers[service - 1]->libais_handlers[header->id].response_id;
  528. msg_overlay.header.error = SA_ERR_TRY_AGAIN;
  529. libais_send_response (conn_info, &msg_overlay,
  530. msg_overlay.header.size);
  531. }
  532. }
  533. conn_info->inb_inuse -= header->size;
  534. } /* while */
  535. if (conn_info->inb_inuse == 0) {
  536. conn_info->inb_start = 0;
  537. } else
  538. // BUG if (connections[fd].inb_start + connections[fd].inb_inuse >= SIZEINB) {
  539. if (conn_info->inb_start >= SIZEINB) {
  540. /*
  541. * If in buffer is full, move it back to start
  542. */
  543. memmove (conn_info->inb,
  544. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  545. sizeof (char) * conn_info->inb_inuse);
  546. conn_info->inb_start = conn_info->inb_inuse;
  547. }
  548. return (res);
  549. error_disconnect:
  550. res = libais_disconnect (conn_info);
  551. return (res);
  552. }
  553. extern void print_stats (void);
  554. void sigintr_handler (int signum)
  555. {
  556. #ifdef DEBUG_MEMPOOL
  557. int stats_inuse[MEMPOOL_GROUP_SIZE];
  558. int stats_avail[MEMPOOL_GROUP_SIZE];
  559. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  560. int i;
  561. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  562. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  563. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  564. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  565. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  566. }
  567. #endif
  568. print_stats ();
  569. ais_done (AIS_DONE_EXIT);
  570. }
  571. static struct sched_param sched_param = {
  572. sched_priority: 99
  573. };
  574. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  575. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  576. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  577. static int (*aisexec_handler_fns[AIS_SERVICE_HANDLER_AISEXEC_FUNCTIONS_MAX]) (void *msg, struct in_addr source_addr, int endian_conversion_required);
  578. static int aisexec_handler_fns_count = 1;
  579. /*
  580. * Builds the handler table as an optimization
  581. */
  582. static void aisexec_handler_fns_build (void)
  583. {
  584. int i, j;
  585. /*
  586. * Install sync handler function
  587. */
  588. aisexec_handler_fns[0] = sync_deliver_fn;
  589. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  590. for (j = 0; j < ais_service_handlers[i]->aisexec_handler_fns_count; j++) {
  591. aisexec_handler_fns[aisexec_handler_fns_count++] =
  592. ais_service_handlers[i]->aisexec_handler_fns[j];
  593. }
  594. }
  595. log_printf (LOG_LEVEL_DEBUG, "built %d handler functions\n", aisexec_handler_fns_count);
  596. }
  597. void sync_completed (void)
  598. {
  599. }
  600. void aisexec_sync_fns_build (void)
  601. {
  602. int i;
  603. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  604. if (ais_service_handlers[i]->sync_init) {
  605. sync_callbacks[sync_callback_count].sync_init =
  606. ais_service_handlers[i]->sync_init;
  607. sync_callbacks[sync_callback_count].sync_process =
  608. ais_service_handlers[i]->sync_process;
  609. sync_callbacks[sync_callback_count].sync_activate =
  610. ais_service_handlers[i]->sync_activate;
  611. sync_callbacks[sync_callback_count].sync_abort =
  612. ais_service_handlers[i]->sync_abort;
  613. sync_callback_count++;
  614. }
  615. }
  616. sync_register (sync_callbacks, sync_callback_count, sync_completed);
  617. }
  618. char delivery_data[MESSAGE_SIZE_MAX];
  619. static void deliver_fn (
  620. struct in_addr source_addr,
  621. struct iovec *iovec,
  622. int iov_len,
  623. int endian_conversion_required)
  624. {
  625. struct req_header *header;
  626. int res;
  627. int pos = 0;
  628. int i;
  629. /*
  630. * Build buffer without iovecs to make processing easier
  631. * This is only used for messages which are multicast with iovecs
  632. * and self-delivered. All other mechanisms avoid the copy.
  633. */
  634. if (iov_len > 1) {
  635. for (i = 0; i < iov_len; i++) {
  636. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  637. pos += iovec[i].iov_len;
  638. assert (pos < MESSAGE_SIZE_MAX);
  639. }
  640. header = (struct req_header *)delivery_data;
  641. } else {
  642. header = (struct req_header *)iovec[0].iov_base;
  643. }
  644. if (endian_conversion_required) {
  645. header->id = swab32 (header->id);
  646. header->size = swab32 (header->size);
  647. }
  648. res = aisexec_handler_fns[header->id](header, source_addr,
  649. endian_conversion_required);
  650. }
  651. static void confchg_fn (
  652. enum totem_configuration_type configuration_type,
  653. struct in_addr *member_list, void *member_list_private,
  654. int member_list_entries,
  655. struct in_addr *left_list, void *left_list_private,
  656. int left_list_entries,
  657. struct in_addr *joined_list, void *joined_list_private,
  658. int joined_list_entries,
  659. struct memb_ring_id *ring_id)
  660. {
  661. int i;
  662. if (this_ip->sin_addr.s_addr != LOCALHOST_IP) {
  663. memcpy(&this_non_loopback_ip, this_ip, sizeof(struct sockaddr_in));
  664. }
  665. /*
  666. * Execute configuration change for synchronization service
  667. */
  668. sync_confchg_fn (configuration_type,
  669. member_list, member_list_private, member_list_entries,
  670. left_list, left_list_private, left_list_entries,
  671. joined_list, joined_list_private, joined_list_entries, ring_id);
  672. /*
  673. * Call configuration change for all services
  674. */
  675. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  676. if (ais_service_handlers[i]->confchg_fn) {
  677. ais_service_handlers[i]->confchg_fn (configuration_type,
  678. member_list, member_list_private, member_list_entries,
  679. left_list, left_list_private, left_list_entries,
  680. joined_list, joined_list_private, joined_list_entries, ring_id);
  681. }
  682. }
  683. }
  684. static void aisexec_uid_determine (void)
  685. {
  686. struct passwd *passwd;
  687. passwd = getpwnam("ais");
  688. if (passwd == 0) {
  689. log_printf (LOG_LEVEL_ERROR, "ERROR: The 'ais' user is not found in /etc/passwd, please read the documentation.\n");
  690. ais_done (AIS_DONE_UID_DETERMINE);
  691. }
  692. ais_uid = passwd->pw_uid;
  693. }
  694. static void aisexec_gid_determine (void)
  695. {
  696. struct group *group;
  697. group = getgrnam ("ais");
  698. if (group == 0) {
  699. log_printf (LOG_LEVEL_ERROR, "ERROR: The 'ais' group is not found in /etc/group, please read the documentation.\n");
  700. ais_done (AIS_DONE_GID_DETERMINE);
  701. }
  702. gid_valid = group->gr_gid;
  703. }
  704. static void aisexec_priv_drop (void)
  705. {
  706. return;
  707. setuid (ais_uid);
  708. setegid (ais_uid);
  709. }
  710. static void aisexec_mempool_init (void)
  711. {
  712. int res;
  713. res = mempool_init (pool_sizes);
  714. if (res == ENOMEM) {
  715. log_printf (LOG_LEVEL_ERROR, "Couldn't allocate memory pools, not enough memory");
  716. ais_done (AIS_DONE_MEMPOOL_INIT);
  717. }
  718. }
  719. static void aisexec_tty_detach (void)
  720. {
  721. #define DEBUG
  722. #ifndef DEBUG
  723. /*
  724. * Disconnect from TTY if this is not a debug run
  725. */
  726. switch (fork ()) {
  727. case -1:
  728. ais_done (AIS_DONE_FORK);
  729. break;
  730. case 0:
  731. /*
  732. * child which is disconnected, run this process
  733. */
  734. break;
  735. default:
  736. exit (0);
  737. break;
  738. }
  739. #endif
  740. #undef DEBUG
  741. }
  742. static void aisexec_service_handlers_init (void)
  743. {
  744. int i;
  745. /*
  746. * Initialize all services
  747. */
  748. for (i = 0; i < AIS_SERVICE_HANDLERS_COUNT; i++) {
  749. if (ais_service_handlers[i]->exec_init_fn) {
  750. if (!ais_service_handlers[i]->exec_init_fn) {
  751. continue;
  752. }
  753. ais_service_handlers[i]->exec_init_fn ();
  754. }
  755. }
  756. }
  757. static void aisexec_libais_bind (int *server_fd)
  758. {
  759. int libais_server_fd;
  760. struct sockaddr_un un_addr;
  761. int res;
  762. /*
  763. * Create socket for libais clients, name socket, listen for connections
  764. */
  765. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  766. if (libais_server_fd == -1) {
  767. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  768. ais_done (AIS_DONE_LIBAIS_SOCKET);
  769. };
  770. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  771. un_addr.sun_family = AF_UNIX;
  772. strcpy (un_addr.sun_path + 1, socketname);
  773. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, sizeof (struct sockaddr_un));
  774. if (res) {
  775. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  776. ais_done (AIS_DONE_LIBAIS_BIND);
  777. }
  778. listen (libais_server_fd, SERVER_BACKLOG);
  779. *server_fd = libais_server_fd;
  780. }
  781. static void aisexec_setscheduler (void)
  782. {
  783. int res;
  784. return;
  785. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  786. if (res == -1) {
  787. log_printf (LOG_LEVEL_WARNING, "Could not set SCHED_RR at priority 99: %s\n", strerror (errno));
  788. }
  789. }
  790. static void aisexec_mlockall (void)
  791. {
  792. int res;
  793. struct rlimit rlimit;
  794. rlimit.rlim_cur = RLIM_INFINITY;
  795. rlimit.rlim_max = RLIM_INFINITY;
  796. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  797. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  798. if (res == -1) {
  799. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  800. };
  801. }
  802. void aisexec_keyread (unsigned char *key)
  803. {
  804. int fd;
  805. int res;
  806. fd = open ("/etc/ais/authkey", O_RDONLY);
  807. if (fd == -1) {
  808. log_printf (LOG_LEVEL_ERROR, "Could not open /etc/ais/authkey: %s\n", strerror (errno));
  809. ais_done (AIS_DONE_READKEY);
  810. }
  811. res = read (fd, key, 128);
  812. if (res == -1) {
  813. log_printf (LOG_LEVEL_ERROR, "Could not read /etc/ais/authkey: %s\n", strerror (errno));
  814. ais_done (AIS_DONE_READKEY);
  815. }
  816. if (res != 128) {
  817. log_printf (LOG_LEVEL_ERROR, "Could only read %d bits of 1024 bits from /etc/ais/authkey.\n", res * 8);
  818. ais_done (AIS_DONE_READKEY);
  819. }
  820. close (fd);
  821. }
  822. int message_source_is_local(struct message_source *source)
  823. {
  824. int ret = 0;
  825. if ((source->in_addr.s_addr == LOCALHOST_IP)
  826. ||(source->in_addr.s_addr == this_non_loopback_ip.sin_addr.s_addr)) {
  827. ret = 1;
  828. }
  829. return ret;
  830. }
  831. void message_source_set (struct message_source *source, struct conn_info *conn_info)
  832. {
  833. source->in_addr.s_addr = this_ip->sin_addr.s_addr;
  834. source->conn_info = conn_info;
  835. }
  836. int main (int argc, char **argv)
  837. {
  838. int libais_server_fd;
  839. int res;
  840. unsigned char private_key[128];
  841. char *error_string;
  842. struct openais_config openais_config;
  843. memset(&this_non_loopback_ip, 0, sizeof(struct sockaddr_in));
  844. aisexec_uid_determine ();
  845. aisexec_gid_determine ();
  846. aisexec_poll_handle = poll_create ();
  847. signal (SIGUSR2, sigusr2_handler);
  848. /*
  849. * if totempg_initialize doesn't have root priveleges, it cannot
  850. * bind to a specific interface. This only matters if
  851. * there is more then one interface in a system, so
  852. * in this case, only a warning is printed
  853. */
  854. /*
  855. * Initialize group messaging interface with multicast address
  856. */
  857. res = openais_main_config_read (&error_string, &openais_config, 1);
  858. if (res == -1) {
  859. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2004 MontaVista Software, Inc and contributors.\n");
  860. log_printf (LOG_LEVEL_ERROR, error_string);
  861. ais_done (AIS_DONE_MAINCONFIGREAD);
  862. }
  863. memcpy (&config_mcast_addr, &openais_config.mcast_addr, sizeof (struct sockaddr_in));
  864. res = log_setup (&error_string, openais_config.logmode, openais_config.logfile);
  865. if (res == -1) {
  866. log_printf (LOG_LEVEL_ERROR, error_string);
  867. ais_done (AIS_DONE_LOGSETUP);
  868. }
  869. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2004 MontaVista Software, Inc. and contributors.\n");
  870. /*
  871. * Set round robin realtime scheduling with priority 99
  872. * Lock all memory to avoid page faults which may interrupt
  873. * application healthchecking
  874. */
  875. aisexec_setscheduler ();
  876. aisexec_mlockall ();
  877. aisexec_keyread (private_key);
  878. totempg_log_printf_init (internal_log_printf,
  879. mklog (LOG_LEVEL_SECURITY, LOG_SERVICE_GMI),
  880. mklog (LOG_LEVEL_ERROR, LOG_SERVICE_GMI),
  881. mklog (LOG_LEVEL_WARNING, LOG_SERVICE_GMI),
  882. mklog (LOG_LEVEL_NOTICE, LOG_SERVICE_GMI),
  883. mklog (LOG_LEVEL_DEBUG, LOG_SERVICE_GMI));
  884. totempg_initialize (&openais_config.mcast_addr, openais_config.interfaces, 1,
  885. &aisexec_poll_handle,
  886. private_key,
  887. sizeof (private_key),
  888. 0,
  889. 0,
  890. deliver_fn, confchg_fn);
  891. this_ip = &openais_config.interfaces[0].boundto;
  892. /*
  893. * Drop root privleges to user 'ais'
  894. * TODO: Don't really need full root capabilities;
  895. * needed capabilities are:
  896. * CAP_NET_RAW (bindtodevice)
  897. * CAP_SYS_NICE (setscheduler)
  898. * CAP_IPC_LOCK (mlockall)
  899. */
  900. aisexec_priv_drop ();
  901. aisexec_handler_fns_build ();
  902. aisexec_sync_fns_build ();
  903. aisexec_mempool_init ();
  904. res = openais_amf_config_read (&error_string);
  905. if (res == -1) {
  906. log_printf (LOG_LEVEL_ERROR, error_string);
  907. ais_done (AIS_DONE_AMFCONFIGREAD);
  908. }
  909. aisexec_tty_detach ();
  910. signal (SIGINT, sigintr_handler);
  911. aisexec_service_handlers_init ();
  912. aisexec_libais_bind (&libais_server_fd);
  913. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: started and ready to receive connections.\n");
  914. /*
  915. * Setup libais connection dispatch routine
  916. */
  917. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  918. POLLIN, 0, poll_handler_libais_accept, 0);
  919. /*
  920. * Join multicast group and setup delivery
  921. * and configuration change functions
  922. */
  923. /*
  924. * Start main processing loop
  925. */
  926. poll_run (aisexec_poll_handle);
  927. return (0);
  928. }