main.c 31 KB

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