main.c 36 KB

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