main.c 36 KB

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