main.c 36 KB

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