main.c 27 KB

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