main.c 37 KB

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