ipc.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc..
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <pthread.h>
  36. #include <assert.h>
  37. #include <pwd.h>
  38. #include <grp.h>
  39. #include <sys/types.h>
  40. #include <sys/poll.h>
  41. #include <sys/uio.h>
  42. #include <sys/mman.h>
  43. #include <sys/socket.h>
  44. #include <sys/un.h>
  45. #include <sys/time.h>
  46. #include <sys/resource.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <signal.h>
  55. #include <sched.h>
  56. #include <time.h>
  57. #include "../include/saAis.h"
  58. #include "../include/list.h"
  59. #include "../include/queue.h"
  60. #include "../lcr/lcr_ifact.h"
  61. #include "poll.h"
  62. #include "totempg.h"
  63. #include "totemsrp.h"
  64. #include "mempool.h"
  65. #include "mainconfig.h"
  66. #include "totemconfig.h"
  67. #include "main.h"
  68. #include "service.h"
  69. #include "sync.h"
  70. #include "swab.h"
  71. #include "objdb.h"
  72. #include "config.h"
  73. #define LOG_SERVICE LOG_SERVICE_IPC
  74. #include "print.h"
  75. #include "util.h"
  76. #define SERVER_BACKLOG 5
  77. static pthread_mutex_t serialize_input = PTHREAD_MUTEX_INITIALIZER;
  78. static unsigned int g_gid_valid = 0;
  79. static struct totem_ip_address *my_ip;
  80. struct outq_item {
  81. void *msg;
  82. size_t mlen;
  83. };
  84. enum conn_state {
  85. CONN_STATE_CONNECTING,
  86. CONN_STATE_CONNECTED,
  87. CONN_STATE_DISCONNECTING,
  88. CONN_STATE_DISCONNECTING_DELAYED
  89. };
  90. enum disc_state {
  91. DISC_STATE_EXITING,
  92. DISC_STATE_EXITED
  93. };
  94. struct conn_info {
  95. int fd; /* File descriptor */
  96. unsigned int events; /* events polled for by file descriptor */
  97. enum conn_state state; /* State of this connection */
  98. pthread_t thread; /* thread identifier */
  99. pthread_attr_t thread_attr; /* thread attribute */
  100. char *inb; /* Input buffer for non-blocking reads */
  101. int inb_nextheader; /* Next message header starts here */
  102. int inb_start; /* Start location of input buffer */
  103. int inb_inuse; /* Bytes currently stored in input buffer */
  104. struct queue outq; /* Circular queue for outgoing requests */
  105. int byte_start; /* Byte to start sending from in head of queue */
  106. enum service_types service;/* Type of service so dispatch knows how to route message */
  107. int authenticated; /* Is this connection authenticated? */
  108. void *private_data; /* library connection private data */
  109. struct conn_info *conn_info_partner; /* partner connection dispatch<->response */
  110. enum disc_state disc; /* disconnect state */
  111. pthread_mutex_t mutex;
  112. };
  113. static void *prioritized_poll_thread (void *conn);
  114. static int conn_info_outq_flush (struct conn_info *conn_info);
  115. static int libais_deliver (struct conn_info *conn_info);
  116. /*
  117. * IPC Initializers
  118. */
  119. static int response_init_send_response (
  120. struct conn_info *conn_info,
  121. void *message);
  122. static int dispatch_init_send_response (
  123. struct conn_info *conn_info,
  124. void *message);
  125. static int (*ais_init_service[]) (struct conn_info *conn_info, void *message) = {
  126. response_init_send_response,
  127. dispatch_init_send_response
  128. };
  129. static int response_init_send_response (
  130. struct conn_info *conn_info,
  131. void *message)
  132. {
  133. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  134. struct req_lib_response_init *req_lib_response_init = (struct req_lib_response_init *)message;
  135. struct res_lib_response_init res_lib_response_init;
  136. if (conn_info->authenticated) {
  137. conn_info->service = req_lib_response_init->resdis_header.service;
  138. error = SA_AIS_OK;
  139. }
  140. res_lib_response_init.header.size = sizeof (struct res_lib_response_init);
  141. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  142. res_lib_response_init.header.error = error;
  143. res_lib_response_init.conn_info = (unsigned long)conn_info;
  144. openais_conn_send_response (
  145. conn_info,
  146. &res_lib_response_init,
  147. sizeof (res_lib_response_init));
  148. if (error == SA_AIS_ERR_ACCESS) {
  149. return (-1);
  150. }
  151. conn_info->disc = DISC_STATE_EXITING;
  152. return (0);
  153. }
  154. static int dispatch_init_send_response (
  155. struct conn_info *conn_info,
  156. void *message)
  157. {
  158. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  159. struct req_lib_dispatch_init *req_lib_dispatch_init = (struct req_lib_dispatch_init *)message;
  160. struct res_lib_dispatch_init res_lib_dispatch_init;
  161. struct conn_info *msg_conn_info;
  162. if (conn_info->authenticated) {
  163. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  164. if (!ais_service[req_lib_dispatch_init->resdis_header.service])
  165. error = SA_AIS_ERR_NOT_SUPPORTED;
  166. else
  167. error = SA_AIS_OK;
  168. conn_info->conn_info_partner = (struct conn_info *)req_lib_dispatch_init->conn_info;
  169. msg_conn_info = (struct conn_info *)req_lib_dispatch_init->conn_info;
  170. msg_conn_info->conn_info_partner = conn_info;
  171. if (error == SA_AIS_OK) {
  172. int private_data_size;
  173. private_data_size = ais_service[req_lib_dispatch_init->resdis_header.service]->private_data_size;
  174. if (private_data_size) {
  175. conn_info->private_data = malloc (private_data_size);
  176. conn_info->conn_info_partner->private_data = conn_info->private_data;
  177. if (conn_info->private_data == NULL) {
  178. error = SA_AIS_ERR_NO_MEMORY;
  179. } else {
  180. memset (conn_info->private_data, 0, private_data_size);
  181. }
  182. } else {
  183. conn_info->private_data = NULL;
  184. conn_info->conn_info_partner->private_data = NULL;
  185. }
  186. }
  187. res_lib_dispatch_init.header.size = sizeof (struct res_lib_dispatch_init);
  188. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  189. res_lib_dispatch_init.header.error = error;
  190. openais_conn_send_response (
  191. conn_info,
  192. &res_lib_dispatch_init,
  193. sizeof (res_lib_dispatch_init));
  194. if (error != SA_AIS_OK) {
  195. return (-1);
  196. }
  197. }
  198. conn_info->state = CONN_STATE_CONNECTED;
  199. conn_info->disc = DISC_STATE_EXITING;
  200. ais_service[req_lib_dispatch_init->resdis_header.service]->lib_init_fn (conn_info);
  201. return (0);
  202. }
  203. /*
  204. * pthread_kill interrupts poll so poll can reread its events descriptor
  205. * via sigusr1
  206. */
  207. static void sigusr1_handler (int num) {
  208. }
  209. /*
  210. * Create a connection data structure
  211. */
  212. static inline unsigned int conn_info_create (int fd) {
  213. struct conn_info *conn_info;
  214. int res;
  215. conn_info = malloc (sizeof (struct conn_info));
  216. if (conn_info == 0) {
  217. return (ENOMEM);
  218. }
  219. memset (conn_info, 0, sizeof (struct conn_info));
  220. res = queue_init (&conn_info->outq, SIZEQUEUE,
  221. sizeof (struct outq_item));
  222. if (res != 0) {
  223. free (conn_info);
  224. return (ENOMEM);
  225. }
  226. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  227. if (conn_info->inb == 0) {
  228. queue_free (&conn_info->outq);
  229. free (conn_info);
  230. return (ENOMEM);
  231. }
  232. conn_info->state = CONN_STATE_CONNECTING;
  233. conn_info->fd = fd;
  234. conn_info->events = POLLIN|POLLNVAL;
  235. conn_info->service = SOCKET_SERVICE_INIT;
  236. pthread_mutex_init (&conn_info->mutex, NULL);
  237. pthread_attr_init (&conn_info->thread_attr);
  238. pthread_attr_setstacksize (&conn_info->thread_attr, 100000);
  239. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_DETACHED);
  240. res = pthread_create (&conn_info->thread, &conn_info->thread_attr,
  241. prioritized_poll_thread, conn_info);
  242. return (res);
  243. }
  244. static void conn_info_destroy (struct conn_info *conn_info)
  245. {
  246. struct outq_item *outq_item;
  247. close (conn_info->fd);
  248. /*
  249. * Free the outq queued items
  250. */
  251. while (!queue_is_empty (&conn_info->outq)) {
  252. outq_item = queue_item_get (&conn_info->outq);
  253. free (outq_item->msg);
  254. queue_item_remove (&conn_info->outq);
  255. }
  256. queue_free (&conn_info->outq);
  257. free (conn_info->inb);
  258. }
  259. static int libais_connection_active (struct conn_info *conn_info)
  260. {
  261. return (conn_info->state == CONN_STATE_CONNECTED ||
  262. conn_info->state == CONN_STATE_CONNECTING);
  263. }
  264. static void libais_disconnect_delayed (struct conn_info *conn_info)
  265. {
  266. conn_info->state = CONN_STATE_DISCONNECTING_DELAYED;
  267. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTING_DELAYED;
  268. }
  269. static int libais_disconnect (struct conn_info *conn_info)
  270. {
  271. int res = 0;
  272. if (conn_info->disc == DISC_STATE_EXITING) {
  273. if (conn_info->service != SOCKET_SERVICE_INIT && ais_service[conn_info->service]->lib_exit_fn) {
  274. res = ais_service[conn_info->service]->lib_exit_fn (conn_info);
  275. if (res == 0) {
  276. conn_info->disc = DISC_STATE_EXITED;
  277. conn_info->conn_info_partner->disc = DISC_STATE_EXITED;
  278. }
  279. return (res);
  280. }
  281. conn_info->disc = DISC_STATE_EXITED;
  282. if (conn_info->conn_info_partner)
  283. conn_info->conn_info_partner->disc = DISC_STATE_EXITED;
  284. return (0);
  285. }
  286. conn_info_destroy (conn_info);
  287. conn_info_destroy (conn_info->conn_info_partner);
  288. if (conn_info->private_data) {
  289. free (conn_info->private_data);
  290. }
  291. free (conn_info->conn_info_partner);
  292. free (conn_info);
  293. return (0);
  294. }
  295. /*
  296. * This thread runs in a specific thread priority mode to handle
  297. * I/O requests from the library
  298. */
  299. static void *prioritized_poll_thread (void *conn)
  300. {
  301. struct conn_info *conn_info = (struct conn_info *)conn;
  302. struct pollfd ufd;
  303. int fds;
  304. struct sched_param sched_param;
  305. int res;
  306. int timeout = 1;
  307. sched_param.sched_priority = 1;
  308. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  309. ufd.fd = conn_info->fd;
  310. for (;;) {
  311. retry_poll:
  312. ufd.events = conn_info->events;
  313. ufd.revents = 0;
  314. fds = poll (&ufd, 1, timeout);
  315. if (fds == -1) {
  316. goto retry_poll;
  317. }
  318. timeout = -1;
  319. pthread_mutex_lock (&serialize_input);
  320. if (fds == 1 && ufd.revents) {
  321. if ((ufd.revents & (POLLERR|POLLHUP)) ||
  322. conn_info->state == CONN_STATE_DISCONNECTING_DELAYED) {
  323. res = libais_disconnect (conn_info);
  324. if (res != 0) {
  325. pthread_mutex_unlock (&serialize_input);
  326. continue;
  327. } else {
  328. break;
  329. }
  330. }
  331. if (ufd.revents & POLLOUT) {
  332. conn_info_outq_flush (conn_info);
  333. }
  334. if (conn_info->state == CONN_STATE_CONNECTED && conn_info->conn_info_partner == 0) {
  335. timeout = 10;
  336. pthread_mutex_unlock (&serialize_input);
  337. continue;
  338. }
  339. if ((ufd.revents & POLLIN) == POLLIN) {
  340. libais_deliver (conn_info);
  341. }
  342. }
  343. pthread_mutex_unlock (&serialize_input);
  344. }
  345. pthread_mutex_unlock (&serialize_input);
  346. pthread_exit (0);
  347. return (0);
  348. }
  349. #if defined(OPENAIS_LINUX)
  350. /* SUN_LEN is broken for abstract namespace
  351. */
  352. #define AIS_SUN_LEN(a) sizeof(*(a))
  353. char *socketname = "libais.socket";
  354. #else
  355. #define AIS_SUN_LEN(a) SUN_LEN(a)
  356. char *socketname = "/var/run/libais.socket";
  357. #endif
  358. static int conn_info_outq_flush (struct conn_info *conn_info) {
  359. struct queue *outq;
  360. int res = 0;
  361. struct outq_item *queue_item;
  362. struct msghdr msg_send;
  363. struct iovec iov_send;
  364. char *msg_addr;
  365. pthread_mutex_lock (&conn_info->mutex);
  366. if (!libais_connection_active (conn_info)) {
  367. pthread_mutex_unlock (&conn_info->mutex);
  368. return (-1);
  369. }
  370. outq = &conn_info->outq;
  371. msg_send.msg_iov = &iov_send;
  372. msg_send.msg_name = 0;
  373. msg_send.msg_namelen = 0;
  374. msg_send.msg_iovlen = 1;
  375. msg_send.msg_control = 0;
  376. msg_send.msg_controllen = 0;
  377. msg_send.msg_flags = 0;
  378. while (!queue_is_empty (outq)) {
  379. queue_item = queue_item_get (outq);
  380. msg_addr = (char *)queue_item->msg;
  381. msg_addr = &msg_addr[conn_info->byte_start];
  382. iov_send.iov_base = msg_addr;
  383. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  384. retry_sendmsg:
  385. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  386. if (res == -1 && errno == EINTR) {
  387. goto retry_sendmsg;
  388. }
  389. if (res == -1 && errno == EAGAIN) {
  390. pthread_mutex_unlock (&conn_info->mutex);
  391. return (0);
  392. }
  393. if (res == -1 && errno == EPIPE) {
  394. pthread_mutex_unlock (&conn_info->mutex);
  395. libais_disconnect_delayed (conn_info);
  396. return (0);
  397. }
  398. if (res == -1) {
  399. printf ("ERRNO is %d\n", errno);
  400. assert (0); /* some other unhandled error here */
  401. }
  402. if (res + conn_info->byte_start != queue_item->mlen) {
  403. conn_info->byte_start += res;
  404. pthread_mutex_unlock (&conn_info->mutex);
  405. return (0);
  406. }
  407. /*
  408. * Message sent, try sending another message
  409. */
  410. queue_item_remove (outq);
  411. conn_info->byte_start = 0;
  412. free (queue_item->msg);
  413. } /* while queue not empty */
  414. if (queue_is_empty (outq)) {
  415. conn_info->events = POLLIN|POLLNVAL;
  416. }
  417. pthread_mutex_unlock (&conn_info->mutex);
  418. return (0);
  419. }
  420. struct res_overlay {
  421. struct res_header header;
  422. char buf[4096];
  423. };
  424. static int libais_deliver (struct conn_info *conn_info)
  425. {
  426. int res;
  427. struct req_header *header;
  428. int service;
  429. struct msghdr msg_recv;
  430. struct iovec iov_recv;
  431. #ifdef OPENAIS_LINUX
  432. struct cmsghdr *cmsg;
  433. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  434. struct ucred *cred;
  435. int on = 0;
  436. #else
  437. uid_t euid;
  438. gid_t egid;
  439. #endif
  440. int send_ok = 0;
  441. int send_ok_joined = 0;
  442. struct iovec send_ok_joined_iovec;
  443. struct res_overlay res_overlay;
  444. msg_recv.msg_iov = &iov_recv;
  445. msg_recv.msg_iovlen = 1;
  446. msg_recv.msg_name = 0;
  447. msg_recv.msg_namelen = 0;
  448. msg_recv.msg_flags = 0;
  449. if (conn_info->authenticated) {
  450. msg_recv.msg_control = 0;
  451. msg_recv.msg_controllen = 0;
  452. } else {
  453. #ifdef OPENAIS_LINUX
  454. msg_recv.msg_control = (void *)cmsg_cred;
  455. msg_recv.msg_controllen = sizeof (cmsg_cred);
  456. #else
  457. euid = -1; egid = -1;
  458. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  459. (euid == 0 || egid == g_gid_valid)) {
  460. conn_info->authenticated = 1;
  461. }
  462. if (conn_info->authenticated == 0) {
  463. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  464. }
  465. #endif
  466. }
  467. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  468. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  469. assert (iov_recv.iov_len != 0);
  470. retry_recv:
  471. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  472. if (res == -1 && errno == EINTR) {
  473. goto retry_recv;
  474. } else
  475. if (res == -1 && errno != EAGAIN) {
  476. goto error_exit;
  477. } else
  478. if (res == 0) {
  479. res = -1;
  480. goto error_exit;
  481. }
  482. /*
  483. * Authenticate if this connection has not been authenticated
  484. */
  485. #ifdef OPENAIS_LINUX
  486. if (conn_info->authenticated == 0) {
  487. cmsg = CMSG_FIRSTHDR (&msg_recv);
  488. cred = (struct ucred *)CMSG_DATA (cmsg);
  489. if (cred) {
  490. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  491. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  492. conn_info->authenticated = 1;
  493. }
  494. }
  495. if (conn_info->authenticated == 0) {
  496. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  497. }
  498. }
  499. #endif
  500. /*
  501. * Dispatch all messages received in recvmsg that can be dispatched
  502. * sizeof (struct req_header) needed at minimum to do any processing
  503. */
  504. conn_info->inb_inuse += res;
  505. conn_info->inb_start += res;
  506. while (conn_info->inb_inuse >= sizeof (struct req_header) && res != -1) {
  507. header = (struct req_header *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  508. if (header->size > conn_info->inb_inuse) {
  509. break;
  510. }
  511. service = conn_info->service;
  512. /*
  513. * If this service is in init phase, initialize service
  514. * else handle message using service service
  515. */
  516. if (service == SOCKET_SERVICE_INIT) {
  517. res = ais_init_service[header->id] (conn_info, header);
  518. } else {
  519. /*
  520. * Not an init service, but a standard service
  521. */
  522. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  523. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  524. header->id, ais_service[service]->lib_service_count);
  525. res = -1;
  526. goto error_exit;
  527. }
  528. /*
  529. * If flow control is required of the library handle, determine that
  530. * openais is not in synchronization and that totempg has room available
  531. * to queue a message, otherwise tell the library we are busy and to
  532. * try again later
  533. */
  534. send_ok_joined_iovec.iov_base = header;
  535. send_ok_joined_iovec.iov_len = header->size;
  536. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  537. &send_ok_joined_iovec, 1);
  538. send_ok =
  539. (sync_primary_designated() == 1) && (
  540. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  541. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  542. (send_ok_joined) &&
  543. (sync_in_process() == 0)));
  544. if (send_ok) {
  545. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  546. } else {
  547. /*
  548. * Overload, tell library to retry
  549. */
  550. res_overlay.header.size =
  551. ais_service[service]->lib_service[header->id].response_size;
  552. res_overlay.header.id =
  553. ais_service[service]->lib_service[header->id].response_id;
  554. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  555. openais_conn_send_response (
  556. conn_info,
  557. &res_overlay,
  558. res_overlay.header.size);
  559. }
  560. }
  561. conn_info->inb_inuse -= header->size;
  562. } /* while */
  563. if (conn_info->inb_inuse == 0) {
  564. conn_info->inb_start = 0;
  565. } else
  566. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  567. if (conn_info->inb_start >= SIZEINB) {
  568. /*
  569. * If in buffer is full, move it back to start
  570. */
  571. memmove (conn_info->inb,
  572. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  573. sizeof (char) * conn_info->inb_inuse);
  574. conn_info->inb_start = conn_info->inb_inuse;
  575. }
  576. return (0);
  577. error_exit:
  578. return (res);
  579. }
  580. char delivery_data[MESSAGE_SIZE_MAX];
  581. static void deliver_fn (
  582. struct totem_ip_address *source_addr,
  583. struct iovec *iovec,
  584. int iov_len,
  585. int endian_conversion_required)
  586. {
  587. struct req_header *header;
  588. int pos = 0;
  589. int i;
  590. int service;
  591. int fn_id;
  592. /*
  593. * Build buffer without iovecs to make processing easier
  594. * This is only used for messages which are multicast with iovecs
  595. * and self-delivered. All other mechanisms avoid the copy.
  596. */
  597. if (iov_len > 1) {
  598. for (i = 0; i < iov_len; i++) {
  599. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  600. pos += iovec[i].iov_len;
  601. assert (pos < MESSAGE_SIZE_MAX);
  602. }
  603. header = (struct req_header *)delivery_data;
  604. } else {
  605. header = (struct req_header *)iovec[0].iov_base;
  606. }
  607. if (endian_conversion_required) {
  608. header->id = swab32 (header->id);
  609. header->size = swab32 (header->size);
  610. }
  611. // assert(iovec->iov_len == header->size);
  612. /*
  613. * Call the proper executive handler
  614. */
  615. service = header->id >> 16;
  616. fn_id = header->id & 0xffff;
  617. if (endian_conversion_required) {
  618. ais_service[service]->exec_service[fn_id].exec_endian_convert_fn
  619. (header);
  620. }
  621. ais_service[service]->exec_service[fn_id].exec_handler_fn
  622. (header, source_addr);
  623. }
  624. static int poll_handler_libais_accept (
  625. poll_handle handle,
  626. int fd,
  627. int revent,
  628. void *data)
  629. {
  630. socklen_t addrlen;
  631. struct sockaddr_un un_addr;
  632. int new_fd;
  633. #ifdef OPENAIS_LINUX
  634. int on = 1;
  635. #endif
  636. int res;
  637. addrlen = sizeof (struct sockaddr_un);
  638. retry_accept:
  639. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  640. if (new_fd == -1 && errno == EINTR) {
  641. goto retry_accept;
  642. }
  643. if (new_fd == -1) {
  644. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  645. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  646. }
  647. totemip_nosigpipe(new_fd);
  648. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  649. if (res == -1) {
  650. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  651. close (new_fd);
  652. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  653. }
  654. /*
  655. * Valid accept
  656. */
  657. /*
  658. * Request credentials of sender provided by kernel
  659. */
  660. #ifdef OPENAIS_LINUX
  661. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  662. #endif
  663. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  664. res = conn_info_create (new_fd);
  665. if (res != 0) {
  666. close (new_fd);
  667. }
  668. return (0);
  669. }
  670. /*
  671. * Exported functions
  672. */
  673. int message_source_is_local(struct message_source *source)
  674. {
  675. int ret = 0;
  676. assert (source != NULL);
  677. if ((totemip_localhost_check(&source->addr)
  678. ||(totemip_equal(&source->addr, my_ip)))) {
  679. ret = 1;
  680. }
  681. return ret;
  682. }
  683. void message_source_set (
  684. struct message_source *source,
  685. void *conn)
  686. {
  687. assert ((source != NULL) && (conn != NULL));
  688. totemip_copy(&source->addr, this_ip);
  689. source->conn = conn;
  690. }
  691. void openais_ipc_init (
  692. poll_handle ais_poll_handle,
  693. unsigned int gid_valid,
  694. struct totem_ip_address *my_ip_in)
  695. {
  696. int libais_server_fd;
  697. struct sockaddr_un un_addr;
  698. int res;
  699. log_init ("IPC");
  700. /*
  701. * Create socket for libais clients, name socket, listen for connections
  702. */
  703. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  704. if (libais_server_fd == -1) {
  705. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  706. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  707. };
  708. totemip_nosigpipe(libais_server_fd);
  709. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  710. if (res == -1) {
  711. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  712. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  713. }
  714. #if !defined(OPENAIS_LINUX)
  715. unlink(socketname);
  716. #endif
  717. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  718. un_addr.sun_family = AF_UNIX;
  719. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  720. un_addr.sun_len = sizeof(struct sockaddr_un);
  721. #endif
  722. #if defined(OPENAIS_LINUX)
  723. strcpy (un_addr.sun_path + 1, socketname);
  724. #else
  725. strcpy (un_addr.sun_path, socketname);
  726. #endif
  727. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  728. if (res) {
  729. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  730. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  731. }
  732. listen (libais_server_fd, SERVER_BACKLOG);
  733. /*
  734. * Setup libais connection dispatch routine
  735. */
  736. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  737. POLLIN, 0, poll_handler_libais_accept);
  738. g_gid_valid = gid_valid;
  739. my_ip = my_ip_in;
  740. signal (SIGUSR1, sigusr1_handler);
  741. }
  742. /*
  743. * Get the conn info private data
  744. */
  745. void *openais_conn_private_data_get (void *conn)
  746. {
  747. struct conn_info *conn_info = (struct conn_info *)conn;
  748. if (conn != NULL) {
  749. return ((void *)conn_info->private_data);
  750. } else {
  751. return NULL;
  752. }
  753. }
  754. /*
  755. * Get the conn info partner connection
  756. */
  757. void *openais_conn_partner_get (void *conn)
  758. {
  759. struct conn_info *conn_info = (struct conn_info *)conn;
  760. if (conn != NULL) {
  761. return ((void *)conn_info->conn_info_partner);
  762. } else {
  763. return NULL;
  764. }
  765. }
  766. int openais_conn_send_response (
  767. void *conn,
  768. void *msg,
  769. int mlen)
  770. {
  771. struct queue *outq;
  772. char *cmsg;
  773. int res = 0;
  774. int queue_empty;
  775. struct outq_item *queue_item;
  776. struct outq_item queue_item_out;
  777. struct msghdr msg_send;
  778. struct iovec iov_send;
  779. char *msg_addr;
  780. struct conn_info *conn_info = (struct conn_info *)conn;
  781. if (conn_info == NULL) {
  782. return -1;
  783. }
  784. if (!libais_connection_active (conn_info)) {
  785. return (-1);
  786. }
  787. pthread_mutex_lock (&conn_info->mutex);
  788. outq = &conn_info->outq;
  789. msg_send.msg_iov = &iov_send;
  790. msg_send.msg_name = 0;
  791. msg_send.msg_namelen = 0;
  792. msg_send.msg_iovlen = 1;
  793. msg_send.msg_control = 0;
  794. msg_send.msg_controllen = 0;
  795. msg_send.msg_flags = 0;
  796. if (queue_is_full (outq)) {
  797. /*
  798. * Start a disconnect if we have not already started one
  799. * and report that the outgoing queue is full
  800. */
  801. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  802. libais_disconnect_delayed (conn_info);
  803. pthread_mutex_unlock (&conn_info->mutex);
  804. return (-1);
  805. }
  806. while (!queue_is_empty (outq)) {
  807. queue_item = queue_item_get (outq);
  808. msg_addr = (char *)queue_item->msg;
  809. msg_addr = &msg_addr[conn_info->byte_start];
  810. iov_send.iov_base = msg_addr;
  811. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  812. retry_sendmsg:
  813. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  814. if (res == -1 && errno == EINTR) {
  815. goto retry_sendmsg;
  816. }
  817. if (res == -1 && errno == EAGAIN) {
  818. break; /* outgoing kernel queue full */
  819. }
  820. if (res == -1 && errno == EPIPE) {
  821. libais_disconnect_delayed (conn_info);
  822. pthread_mutex_unlock (&conn_info->mutex);
  823. return (0);
  824. }
  825. if (res == -1) {
  826. assert (0);
  827. break; /* some other error, stop trying to send message */
  828. }
  829. if (res + conn_info->byte_start != queue_item->mlen) {
  830. conn_info->byte_start += res;
  831. break;
  832. }
  833. /*
  834. * Message sent, try sending another message
  835. */
  836. queue_item_remove (outq);
  837. conn_info->byte_start = 0;
  838. free (queue_item->msg);
  839. } /* while queue not empty */
  840. res = -1;
  841. queue_empty = queue_is_empty (outq);
  842. /*
  843. * Send requested message
  844. */
  845. if (queue_empty) {
  846. iov_send.iov_base = msg;
  847. iov_send.iov_len = mlen;
  848. retry_sendmsg_two:
  849. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  850. if (res == -1 && errno == EINTR) {
  851. goto retry_sendmsg_two;
  852. }
  853. if (res == -1 && errno == EAGAIN) {
  854. conn_info->byte_start = 0;
  855. conn_info->events = POLLIN|POLLNVAL;
  856. }
  857. if (res != -1) {
  858. if (res + conn_info->byte_start != mlen) {
  859. conn_info->byte_start += res;
  860. res = -1;
  861. } else {
  862. conn_info->byte_start = 0;
  863. conn_info->events = POLLIN|POLLNVAL;
  864. }
  865. }
  866. }
  867. /*
  868. * If res == -1 , errrno == EAGAIN which means kernel queue full
  869. */
  870. if (res == -1) {
  871. cmsg = malloc (mlen);
  872. if (cmsg == 0) {
  873. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  874. libais_disconnect_delayed (conn_info);
  875. return (-1);
  876. }
  877. queue_item_out.msg = cmsg;
  878. queue_item_out.mlen = mlen;
  879. memcpy (cmsg, msg, mlen);
  880. queue_item_add (outq, &queue_item_out);
  881. /*
  882. * Send a pthread_kill to interrupt the poll syscall
  883. * and start a new poll operation in the thread
  884. */
  885. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  886. pthread_kill (conn_info->thread, SIGUSR1);
  887. }
  888. pthread_mutex_unlock (&conn_info->mutex);
  889. return (0);
  890. }
  891. pthread_mutex_t *openais_ipc_mutex_get (void)
  892. {
  893. return (&serialize_input);
  894. }