main.c 36 KB

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