main.c 36 KB

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