main.c 32 KB

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