main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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_DONTWAIT | 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_DONTWAIT | 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_DONTWAIT | 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. addrlen = sizeof (struct sockaddr_un);
  445. retry_accept:
  446. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  447. if (new_fd == -1 && errno == EINTR) {
  448. goto retry_accept;
  449. }
  450. if (new_fd == -1) {
  451. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  452. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  453. }
  454. /*
  455. * Valid accept
  456. */
  457. /*
  458. * Request credentials of sender provided by kernel
  459. */
  460. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  461. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  462. conn_info = conn_info_create (new_fd);
  463. if (conn_info == 0) {
  464. close (new_fd);
  465. return (0); /* This is an error, but -1 would indicate disconnect from poll */
  466. }
  467. poll_dispatch_add (aisexec_poll_handle, new_fd, POLLIN|POLLNVAL, conn_info,
  468. poll_handler_libais_deliver, 0);
  469. return (0);
  470. }
  471. static int dispatch_init_send_response (struct conn_info *conn_info, void *message)
  472. {
  473. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  474. struct req_lib_dispatch_init *req_lib_dispatch_init = (struct req_lib_dispatch_init *)message;
  475. struct res_lib_dispatch_init res_lib_dispatch_init;
  476. struct conn_info *msg_conn_info;
  477. if (conn_info->authenticated) {
  478. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  479. error = SA_AIS_OK;
  480. conn_info->conn_info_partner = (struct conn_info *)req_lib_dispatch_init->conn_info;
  481. msg_conn_info = (struct conn_info *)req_lib_dispatch_init->conn_info;
  482. msg_conn_info->conn_info_partner = conn_info;
  483. if (error == SA_AIS_OK) {
  484. int private_data_size;
  485. private_data_size = ais_service_handlers[req_lib_dispatch_init->resdis_header.service]->private_data_size;
  486. if (private_data_size) {
  487. conn_info->private_data = malloc (private_data_size);
  488. conn_info->conn_info_partner->private_data = conn_info->private_data;
  489. if (conn_info->private_data == NULL) {
  490. error = SA_AIS_ERR_NO_MEMORY;
  491. } else {
  492. memset (conn_info->private_data, 0, private_data_size);
  493. }
  494. } else {
  495. conn_info->private_data = NULL;
  496. conn_info->conn_info_partner->private_data = NULL;
  497. }
  498. }
  499. res_lib_dispatch_init.header.size = sizeof (struct res_lib_dispatch_init);
  500. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  501. res_lib_dispatch_init.header.error = error;
  502. openais_conn_send_response (
  503. conn_info,
  504. &res_lib_dispatch_init,
  505. sizeof (res_lib_dispatch_init));
  506. if (error != SA_AIS_OK) {
  507. return (-1);
  508. }
  509. }
  510. conn_info->should_exit_fn = 1;
  511. ais_service_handlers[req_lib_dispatch_init->resdis_header.service]->lib_init_fn (conn_info);
  512. return (0);
  513. }
  514. void *openais_conn_partner_get (void *conn)
  515. {
  516. struct conn_info *conn_info = (struct conn_info *)conn;
  517. return ((void *)conn_info->conn_info_partner);
  518. }
  519. void *openais_conn_private_data_get (void *conn)
  520. {
  521. struct conn_info *conn_info = (struct conn_info *)conn;
  522. return ((void *)conn_info->private_data);
  523. }
  524. static int response_init_send_response (struct conn_info *conn_info, void *message)
  525. {
  526. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  527. struct req_lib_response_init *req_lib_response_init = (struct req_lib_response_init *)message;
  528. struct res_lib_response_init res_lib_response_init;
  529. if (conn_info->authenticated) {
  530. conn_info->service = req_lib_response_init->resdis_header.service;
  531. error = SA_AIS_OK;
  532. }
  533. res_lib_response_init.header.size = sizeof (struct res_lib_response_init);
  534. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  535. res_lib_response_init.header.error = error;
  536. res_lib_response_init.conn_info = (unsigned long)conn_info;
  537. openais_conn_send_response (
  538. conn_info,
  539. &res_lib_response_init,
  540. sizeof (res_lib_response_init));
  541. if (error == SA_AIS_ERR_ACCESS) {
  542. return (-1);
  543. }
  544. conn_info->should_exit_fn = 0;
  545. return (0);
  546. }
  547. struct res_overlay {
  548. struct res_header header;
  549. char buf[4096];
  550. };
  551. static int poll_handler_libais_deliver (poll_handle handle, int fd, int revent, void *data, unsigned int *prio)
  552. {
  553. int res;
  554. struct conn_info *conn_info = (struct conn_info *)data;
  555. struct req_header *header;
  556. int service;
  557. struct msghdr msg_recv;
  558. struct iovec iov_recv;
  559. struct cmsghdr *cmsg;
  560. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  561. struct ucred *cred;
  562. int on = 0;
  563. int send_ok = 0;
  564. int send_ok_joined = 0;
  565. struct iovec send_ok_joined_iovec;
  566. struct res_overlay res_overlay;
  567. if (revent & (POLLERR|POLLHUP)) {
  568. res = libais_disconnect (conn_info);
  569. return (res);
  570. }
  571. /*
  572. * Handle delayed disconnections
  573. */
  574. if (conn_info->state == CONN_STATE_DISCONNECTING_DELAYED) {
  575. res = libais_disconnect (conn_info);
  576. return (res);
  577. }
  578. if (conn_info->state == CONN_STATE_DISCONNECTING) {
  579. return (0);
  580. }
  581. if (revent & POLLOUT) {
  582. cleanup_send_response (conn_info);
  583. }
  584. if ((revent & POLLIN) == 0) {
  585. return (0);
  586. }
  587. msg_recv.msg_iov = &iov_recv;
  588. msg_recv.msg_iovlen = 1;
  589. msg_recv.msg_name = 0;
  590. msg_recv.msg_namelen = 0;
  591. msg_recv.msg_flags = 0;
  592. if (conn_info->authenticated) {
  593. msg_recv.msg_control = 0;
  594. msg_recv.msg_controllen = 0;
  595. } else {
  596. msg_recv.msg_control = (void *)cmsg_cred;
  597. msg_recv.msg_controllen = sizeof (cmsg_cred);
  598. }
  599. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  600. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  601. assert (iov_recv.iov_len != 0);
  602. retry_recv:
  603. res = recvmsg (fd, &msg_recv, MSG_DONTWAIT | MSG_NOSIGNAL);
  604. if (res == -1 && errno == EINTR) {
  605. goto retry_recv;
  606. } else
  607. if (res == -1 && errno != EAGAIN) {
  608. goto error_disconnect;
  609. } else
  610. if (res == 0) {
  611. goto error_disconnect;
  612. return (-1);
  613. }
  614. /*
  615. * Authenticate if this connection has not been authenticated
  616. */
  617. if (conn_info->authenticated == 0) {
  618. cmsg = CMSG_FIRSTHDR (&msg_recv);
  619. cred = (struct ucred *)CMSG_DATA (cmsg);
  620. if (cred) {
  621. if (cred->uid == 0 || cred->gid == gid_valid) {
  622. setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  623. conn_info->authenticated = 1;
  624. }
  625. }
  626. if (conn_info->authenticated == 0) {
  627. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, gid_valid);
  628. }
  629. }
  630. /*
  631. * Dispatch all messages received in recvmsg that can be dispatched
  632. * sizeof (struct req_header) needed at minimum to do any processing
  633. */
  634. conn_info->inb_inuse += res;
  635. conn_info->inb_start += res;
  636. while (conn_info->inb_inuse >= sizeof (struct req_header) && res != -1) {
  637. header = (struct req_header *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  638. if (header->size > conn_info->inb_inuse) {
  639. break;
  640. }
  641. service = conn_info->service;
  642. /*
  643. * If this service is in init phase, initialize service
  644. * else handle message using service handlers
  645. */
  646. if (service == SOCKET_SERVICE_INIT) {
  647. res = ais_init_handlers[header->id] (conn_info, header);
  648. // TODO error in init_two_fn needs to be handled
  649. } else {
  650. /*
  651. * Not an init service, but a standard service
  652. */
  653. if (header->id < 0 || header->id > ais_service_handlers[service]->lib_handlers_count) {
  654. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  655. header->id, ais_service_handlers[service]->lib_handlers_count);
  656. res = -1;
  657. goto error_disconnect;
  658. }
  659. /*
  660. * If flow control is required of the library handle, determine that
  661. * openais is not in synchronization and that totempg has room available
  662. * to queue a message, otherwise tell the library we are busy and to
  663. * try again later
  664. */
  665. send_ok_joined_iovec.iov_base = header;
  666. send_ok_joined_iovec.iov_len = header->size;
  667. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  668. &send_ok_joined_iovec, 1);
  669. send_ok =
  670. (ykd_primary() == 1) && (
  671. (ais_service_handlers[service]->lib_handlers[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  672. ((ais_service_handlers[service]->lib_handlers[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  673. (send_ok_joined) &&
  674. (sync_in_process() == 0)));
  675. if (send_ok) {
  676. // *prio = 0;
  677. ais_service_handlers[service]->lib_handlers[header->id].lib_handler_fn(conn_info, header);
  678. } else {
  679. // *prio = (*prio) + 1;
  680. /*
  681. * Overload, tell library to retry
  682. */
  683. res_overlay.header.size =
  684. ais_service_handlers[service]->lib_handlers[header->id].response_size;
  685. res_overlay.header.id =
  686. ais_service_handlers[service]->lib_handlers[header->id].response_id;
  687. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  688. openais_conn_send_response (
  689. conn_info,
  690. &res_overlay,
  691. res_overlay.header.size);
  692. }
  693. }
  694. conn_info->inb_inuse -= header->size;
  695. } /* while */
  696. if (conn_info->inb_inuse == 0) {
  697. conn_info->inb_start = 0;
  698. } else
  699. // BUG if (connections[fd].inb_start + connections[fd].inb_inuse >= SIZEINB) {
  700. if (conn_info->inb_start >= SIZEINB) {
  701. /*
  702. * If in buffer is full, move it back to start
  703. */
  704. memmove (conn_info->inb,
  705. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  706. sizeof (char) * conn_info->inb_inuse);
  707. conn_info->inb_start = conn_info->inb_inuse;
  708. }
  709. return (0);
  710. error_disconnect:
  711. res = libais_disconnect (conn_info);
  712. return (res);
  713. }
  714. void sigintr_handler (int signum)
  715. {
  716. #ifdef DEBUG_MEMPOOL
  717. int stats_inuse[MEMPOOL_GROUP_SIZE];
  718. int stats_avail[MEMPOOL_GROUP_SIZE];
  719. int stats_memoryused[MEMPOOL_GROUP_SIZE];
  720. int i;
  721. mempool_getstats (stats_inuse, stats_avail, stats_memoryused);
  722. log_printf (LOG_LEVEL_DEBUG, "Memory pools:\n");
  723. for (i = 0; i < MEMPOOL_GROUP_SIZE; i++) {
  724. log_printf (LOG_LEVEL_DEBUG, "order %d size %d inuse %d avail %d memory used %d\n",
  725. i, 1<<i, stats_inuse[i], stats_avail[i], stats_memoryused[i]);
  726. }
  727. #endif
  728. totempg_finalize ();
  729. ais_done (AIS_DONE_EXIT);
  730. }
  731. static struct sched_param sched_param = {
  732. sched_priority: 99
  733. };
  734. static int pool_sizes[] = { 0, 0, 0, 0, 0, 4096, 0, 1, 0, /* 256 */
  735. 1024, 0, 1, 4096, 0, 0, 0, 0, /* 65536 */
  736. 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  737. static void openais_sync_completed (void)
  738. {
  739. }
  740. static int openais_sync_callbacks_retrieve (int sync_id,
  741. struct sync_callbacks *callbacks)
  742. {
  743. if (ais_service_handlers[sync_id] == NULL) {
  744. memset (callbacks, 0, sizeof (struct sync_callbacks));
  745. return (-1);
  746. }
  747. callbacks->sync_init = ais_service_handlers[sync_id]->sync_init;
  748. callbacks->sync_process = ais_service_handlers[sync_id]->sync_process;
  749. callbacks->sync_activate = ais_service_handlers[sync_id]->sync_activate;
  750. callbacks->sync_abort = ais_service_handlers[sync_id]->sync_abort;
  751. return (0);
  752. }
  753. char delivery_data[MESSAGE_SIZE_MAX];
  754. static void deliver_fn (
  755. struct totem_ip_address *source_addr,
  756. struct iovec *iovec,
  757. int iov_len,
  758. int endian_conversion_required)
  759. {
  760. struct req_header *header;
  761. int pos = 0;
  762. int i;
  763. int service;
  764. int fn_id;
  765. /*
  766. * Build buffer without iovecs to make processing easier
  767. * This is only used for messages which are multicast with iovecs
  768. * and self-delivered. All other mechanisms avoid the copy.
  769. */
  770. if (iov_len > 1) {
  771. for (i = 0; i < iov_len; i++) {
  772. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  773. pos += iovec[i].iov_len;
  774. assert (pos < MESSAGE_SIZE_MAX);
  775. }
  776. header = (struct req_header *)delivery_data;
  777. } else {
  778. header = (struct req_header *)iovec[0].iov_base;
  779. }
  780. if (endian_conversion_required) {
  781. header->id = swab32 (header->id);
  782. header->size = swab32 (header->size);
  783. }
  784. // assert(iovec->iov_len == header->size);
  785. /*
  786. * Call the proper executive handler
  787. */
  788. service = header->id >> 16;
  789. fn_id = header->id & 0xffff;
  790. if (endian_conversion_required) {
  791. ais_service_handlers[service]->exec_handlers[fn_id].exec_endian_convert_fn
  792. (header);
  793. }
  794. ais_service_handlers[service]->exec_handlers[fn_id].exec_handler_fn
  795. (header, source_addr);
  796. }
  797. static struct memb_ring_id aisexec_ring_id;
  798. static void confchg_fn (
  799. enum totem_configuration_type configuration_type,
  800. struct totem_ip_address *member_list, int member_list_entries,
  801. struct totem_ip_address *left_list, int left_list_entries,
  802. struct totem_ip_address *joined_list, int joined_list_entries,
  803. struct memb_ring_id *ring_id)
  804. {
  805. int i;
  806. memcpy (&aisexec_ring_id, ring_id, sizeof (struct memb_ring_id));
  807. if (!totemip_localhost_check(this_ip)) {
  808. totemip_copy(&this_non_loopback_ip, this_ip);
  809. }
  810. /*
  811. * Call configuration change for all services
  812. */
  813. for (i = 0; i < service_handlers_count; i++) {
  814. if (ais_service_handlers[i] && ais_service_handlers[i]->confchg_fn) {
  815. ais_service_handlers[i]->confchg_fn (configuration_type,
  816. member_list, member_list_entries,
  817. left_list, left_list_entries,
  818. joined_list, joined_list_entries, ring_id);
  819. }
  820. }
  821. }
  822. static void aisexec_uid_determine (void)
  823. {
  824. struct passwd *passwd;
  825. passwd = getpwnam("ais");
  826. if (passwd == 0) {
  827. log_printf (LOG_LEVEL_ERROR, "ERROR: The 'ais' user is not found in /etc/passwd, please read the documentation.\n");
  828. ais_done (AIS_DONE_UID_DETERMINE);
  829. }
  830. ais_uid = passwd->pw_uid;
  831. }
  832. static void aisexec_gid_determine (void)
  833. {
  834. struct group *group;
  835. group = getgrnam ("ais");
  836. if (group == 0) {
  837. log_printf (LOG_LEVEL_ERROR, "ERROR: The 'ais' group is not found in /etc/group, please read the documentation.\n");
  838. ais_done (AIS_DONE_GID_DETERMINE);
  839. }
  840. gid_valid = group->gr_gid;
  841. }
  842. static void aisexec_priv_drop (void)
  843. {
  844. return;
  845. setuid (ais_uid);
  846. setegid (ais_uid);
  847. }
  848. static void aisexec_mempool_init (void)
  849. {
  850. int res;
  851. res = mempool_init (pool_sizes);
  852. if (res == ENOMEM) {
  853. log_printf (LOG_LEVEL_ERROR, "Couldn't allocate memory pools, not enough memory");
  854. ais_done (AIS_DONE_MEMPOOL_INIT);
  855. }
  856. }
  857. static void aisexec_tty_detach (void)
  858. {
  859. #define DEBUG
  860. #ifndef DEBUG
  861. /*
  862. * Disconnect from TTY if this is not a debug run
  863. */
  864. switch (fork ()) {
  865. case -1:
  866. ais_done (AIS_DONE_FORK);
  867. break;
  868. case 0:
  869. /*
  870. * child which is disconnected, run this process
  871. */
  872. break;
  873. default:
  874. exit (0);
  875. break;
  876. }
  877. #endif
  878. #undef DEBUG
  879. }
  880. static void aisexec_libais_bind (int *server_fd)
  881. {
  882. int libais_server_fd;
  883. struct sockaddr_un un_addr;
  884. int res;
  885. /*
  886. * Create socket for libais clients, name socket, listen for connections
  887. */
  888. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  889. if (libais_server_fd == -1) {
  890. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  891. ais_done (AIS_DONE_LIBAIS_SOCKET);
  892. };
  893. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  894. un_addr.sun_family = AF_UNIX;
  895. strcpy (un_addr.sun_path + 1, socketname);
  896. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, sizeof (struct sockaddr_un));
  897. if (res) {
  898. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  899. ais_done (AIS_DONE_LIBAIS_BIND);
  900. }
  901. listen (libais_server_fd, SERVER_BACKLOG);
  902. *server_fd = libais_server_fd;
  903. }
  904. static void aisexec_setscheduler (void)
  905. {
  906. int res;
  907. return;
  908. res = sched_setscheduler (0, SCHED_RR, &sched_param);
  909. if (res == -1) {
  910. log_printf (LOG_LEVEL_WARNING, "Could not set SCHED_RR at priority 99: %s\n", strerror (errno));
  911. }
  912. }
  913. static void aisexec_mlockall (void)
  914. {
  915. int res;
  916. struct rlimit rlimit;
  917. rlimit.rlim_cur = RLIM_INFINITY;
  918. rlimit.rlim_max = RLIM_INFINITY;
  919. setrlimit (RLIMIT_MEMLOCK, &rlimit);
  920. res = mlockall (MCL_CURRENT | MCL_FUTURE);
  921. if (res == -1) {
  922. log_printf (LOG_LEVEL_WARNING, "Could not lock memory of service to avoid page faults: %s\n", strerror (errno));
  923. };
  924. }
  925. int message_source_is_local(struct message_source *source)
  926. {
  927. int ret = 0;
  928. if ((totemip_localhost_check(&source->addr)
  929. ||(totemip_equal(&source->addr, &this_non_loopback_ip)))) {
  930. ret = 1;
  931. }
  932. return ret;
  933. }
  934. void message_source_set (
  935. struct message_source *source,
  936. void *conn)
  937. {
  938. totemip_copy(&source->addr, this_ip);
  939. source->conn = conn;
  940. }
  941. struct totem_logging_configuration totem_logging_configuration;
  942. int service_handler_register (
  943. struct openais_service_handler *handler,
  944. struct openais_config *config)
  945. {
  946. int res = 0;
  947. assert (ais_service_handlers[handler->id] == NULL);
  948. log_printf (LOG_LEVEL_NOTICE, "Registering service handler '%s'\n", handler->name);
  949. ais_service_handlers[handler->id] = handler;
  950. if (ais_service_handlers[handler->id]->config_init_fn) {
  951. res = ais_service_handlers[handler->id]->config_init_fn (config);
  952. }
  953. return (res);
  954. }
  955. int service_handler_init (
  956. struct openais_service_handler *handler,
  957. struct openais_config *config)
  958. {
  959. int res = 0;
  960. assert (ais_service_handlers[handler->id] != NULL);
  961. log_printf (LOG_LEVEL_NOTICE, "Initializing service handler '%s'\n", handler->name);
  962. if (ais_service_handlers[handler->id]->exec_init_fn) {
  963. res = ais_service_handlers[handler->id]->exec_init_fn (config);
  964. }
  965. return (res);
  966. }
  967. void default_services_register (struct openais_config *openais_config)
  968. {
  969. int i;
  970. for (i = 0; i < openais_config->num_dynamic_services; i++) {
  971. lcr_ifact_reference (
  972. &openais_config->dynamic_services[i].handle,
  973. openais_config->dynamic_services[i].name,
  974. openais_config->dynamic_services[i].ver,
  975. (void **)&openais_config->dynamic_services[i].iface_ver0,
  976. (void *)0);
  977. if (!openais_config->dynamic_services[i].iface_ver0) {
  978. log_printf(LOG_LEVEL_ERROR, "AIS Component %s did not load.\n", openais_config->dynamic_services[i].name);
  979. ais_done(AIS_DONE_DYNAMICLOAD);
  980. } else {
  981. log_printf(LOG_LEVEL_ERROR, "AIS Component %s loaded.\n", openais_config->dynamic_services[i].name);
  982. }
  983. service_handler_register (
  984. openais_config->dynamic_services[i].iface_ver0->openais_get_service_handler_ver0(),
  985. openais_config);
  986. }
  987. }
  988. void default_services_init (struct openais_config *openais_config)
  989. {
  990. int i;
  991. for (i = 0; i < openais_config->num_dynamic_services; i++) {
  992. service_handler_init (openais_config->dynamic_services[i].iface_ver0->openais_get_service_handler_ver0(),
  993. openais_config);
  994. }
  995. }
  996. int main (int argc, char **argv)
  997. {
  998. int libais_server_fd;
  999. int res;
  1000. char *error_string;
  1001. struct openais_config openais_config;
  1002. memset(&this_non_loopback_ip, 0, sizeof(struct totem_ip_address));
  1003. totemip_localhost(AF_INET, &this_non_loopback_ip);
  1004. aisexec_uid_determine ();
  1005. aisexec_gid_determine ();
  1006. aisexec_poll_handle = poll_create ();
  1007. //TODO signal (SIGUSR2, sigusr2_handler);
  1008. /*
  1009. * if totempg_initialize doesn't have root priveleges, it cannot
  1010. * bind to a specific interface. This only matters if
  1011. * there is more then one interface in a system, so
  1012. * in this case, only a warning is printed
  1013. */
  1014. res = openais_main_config_read (&error_string, &openais_config, 1);
  1015. if (res == -1) {
  1016. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2006 MontaVista Software, Inc and contributors.\n");
  1017. log_printf (LOG_LEVEL_ERROR, error_string);
  1018. ais_done (AIS_DONE_MAINCONFIGREAD);
  1019. }
  1020. res = openais_amf_config_read (&error_string);
  1021. if (res == -1) {
  1022. log_printf (LOG_LEVEL_ERROR, error_string);
  1023. ais_done (AIS_DONE_AMFCONFIGREAD);
  1024. }
  1025. if (!openais_config.totem_config.interface_count) {
  1026. res = totem_config_read (&openais_config.totem_config, &error_string, 1);
  1027. if (res == -1) {
  1028. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2006 MontaVista Software, Inc and contributors.\n");
  1029. log_printf (LOG_LEVEL_ERROR, error_string);
  1030. ais_done (AIS_DONE_MAINCONFIGREAD);
  1031. }
  1032. }
  1033. res = totem_config_keyread ("/etc/ais/authkey", &openais_config.totem_config, &error_string);
  1034. if (res == -1) {
  1035. log_printf (LOG_LEVEL_ERROR, error_string);
  1036. ais_done (AIS_DONE_MAINCONFIGREAD);
  1037. }
  1038. res = totem_config_validate (&openais_config.totem_config, &error_string);
  1039. if (res == -1) {
  1040. log_printf (LOG_LEVEL_ERROR, error_string);
  1041. ais_done (AIS_DONE_MAINCONFIGREAD);
  1042. }
  1043. res = log_setup (&error_string, openais_config.logmode, openais_config.logfile);
  1044. if (res == -1) {
  1045. log_printf (LOG_LEVEL_ERROR, error_string);
  1046. ais_done (AIS_DONE_LOGSETUP);
  1047. }
  1048. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: Copyright (C) 2002-2006 MontaVista Software, Inc. and contributors.\n");
  1049. /*
  1050. * Set round robin realtime scheduling with priority 99
  1051. * Lock all memory to avoid page faults which may interrupt
  1052. * application healthchecking
  1053. */
  1054. aisexec_setscheduler ();
  1055. aisexec_mlockall ();
  1056. openais_config.totem_config.totem_logging_configuration = totem_logging_configuration;
  1057. openais_config.totem_config.totem_logging_configuration.log_level_security = mklog (LOG_LEVEL_SECURITY, LOG_SERVICE_GMI);
  1058. openais_config.totem_config.totem_logging_configuration.log_level_error = mklog (LOG_LEVEL_ERROR, LOG_SERVICE_GMI);
  1059. openais_config.totem_config.totem_logging_configuration.log_level_warning = mklog (LOG_LEVEL_WARNING, LOG_SERVICE_GMI);
  1060. openais_config.totem_config.totem_logging_configuration.log_level_notice = mklog (LOG_LEVEL_NOTICE, LOG_SERVICE_GMI);
  1061. openais_config.totem_config.totem_logging_configuration.log_level_debug = mklog (LOG_LEVEL_DEBUG, LOG_SERVICE_GMI);
  1062. openais_config.totem_config.totem_logging_configuration.log_printf = internal_log_printf;
  1063. default_services_register(&openais_config);
  1064. totempg_initialize (
  1065. aisexec_poll_handle,
  1066. &openais_config.totem_config);
  1067. totempg_groups_initialize (
  1068. &openais_group_handle,
  1069. deliver_fn,
  1070. confchg_fn);
  1071. totempg_groups_join (
  1072. openais_group_handle,
  1073. &openais_group,
  1074. 1);
  1075. /*
  1076. * This must occur after totempg is initialized because "this_ip" must be set
  1077. */
  1078. this_ip = &openais_config.totem_config.interfaces[0].boundto;
  1079. default_services_init(&openais_config);
  1080. sync_register (openais_sync_callbacks_retrieve, openais_sync_completed);
  1081. /*
  1082. * Drop root privleges to user 'ais'
  1083. * TODO: Don't really need full root capabilities;
  1084. * needed capabilities are:
  1085. * CAP_NET_RAW (bindtodevice)
  1086. * CAP_SYS_NICE (setscheduler)
  1087. * CAP_IPC_LOCK (mlockall)
  1088. */
  1089. aisexec_priv_drop ();
  1090. aisexec_mempool_init ();
  1091. signal (SIGINT, sigintr_handler);
  1092. aisexec_libais_bind (&libais_server_fd);
  1093. aisexec_tty_detach ();
  1094. log_printf (LOG_LEVEL_NOTICE, "AIS Executive Service: started and ready to receive connections.\n");
  1095. /*
  1096. * Setup libais connection dispatch routine
  1097. */
  1098. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  1099. POLLIN, 0, poll_handler_libais_accept, 0);
  1100. /*
  1101. * Join multicast group and setup delivery
  1102. * and configuration change functions
  1103. */
  1104. /*
  1105. * Start main processing loop
  1106. */
  1107. poll_run (aisexec_poll_handle);
  1108. return (0);
  1109. }