main.c 36 KB

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