4
0

ipc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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 (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. conn_info->conn_info_partner->disc = DISC_STATE_EXITED;
  283. return (0);
  284. }
  285. conn_info_destroy (conn_info);
  286. conn_info_destroy (conn_info->conn_info_partner);
  287. if (conn_info->private_data) {
  288. free (conn_info->private_data);
  289. }
  290. free (conn_info->conn_info_partner);
  291. free (conn_info);
  292. return (0);
  293. }
  294. /*
  295. * This thread runs in a specific thread priority mode to handle
  296. * I/O requests from the library
  297. */
  298. static void *prioritized_poll_thread (void *conn)
  299. {
  300. struct conn_info *conn_info = (struct conn_info *)conn;
  301. struct pollfd ufd;
  302. int fds;
  303. struct sched_param sched_param;
  304. int res;
  305. int timeout = 1;
  306. sched_param.sched_priority = 1;
  307. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  308. ufd.fd = conn_info->fd;
  309. for (;;) {
  310. retry_poll:
  311. ufd.events = conn_info->events;
  312. ufd.revents = 0;
  313. fds = poll (&ufd, 1, timeout);
  314. if (fds == -1) {
  315. goto retry_poll;
  316. }
  317. timeout = -1;
  318. pthread_mutex_lock (&serialize_input);
  319. if (fds == 1 && ufd.revents) {
  320. if ((ufd.revents & (POLLERR|POLLHUP)) ||
  321. conn_info->state == CONN_STATE_DISCONNECTING_DELAYED) {
  322. res = libais_disconnect (conn_info);
  323. if (res != 0) {
  324. pthread_mutex_unlock (&serialize_input);
  325. continue;
  326. } else {
  327. break;
  328. }
  329. }
  330. if (ufd.revents & POLLOUT) {
  331. conn_info_outq_flush (conn_info);
  332. }
  333. if (conn_info->state == CONN_STATE_CONNECTED && conn_info->conn_info_partner == 0) {
  334. timeout = 10;
  335. pthread_mutex_unlock (&serialize_input);
  336. continue;
  337. }
  338. if ((ufd.revents & POLLIN) == POLLIN) {
  339. libais_deliver (conn_info);
  340. }
  341. pthread_mutex_unlock (&serialize_input);
  342. }
  343. }
  344. pthread_mutex_unlock (&serialize_input);
  345. pthread_exit (0);
  346. return (0);
  347. }
  348. #if defined(OPENAIS_LINUX)
  349. /* SUN_LEN is broken for abstract namespace
  350. */
  351. #define AIS_SUN_LEN(a) sizeof(*(a))
  352. char *socketname = "libais.socket";
  353. #else
  354. #define AIS_SUN_LEN(a) SUN_LEN(a)
  355. char *socketname = "/var/run/libais.socket";
  356. #endif
  357. static int conn_info_outq_flush (struct conn_info *conn_info) {
  358. struct queue *outq;
  359. int res = 0;
  360. struct outq_item *queue_item;
  361. struct msghdr msg_send;
  362. struct iovec iov_send;
  363. char *msg_addr;
  364. pthread_mutex_lock (&conn_info->mutex);
  365. if (!libais_connection_active (conn_info)) {
  366. return (-1);
  367. }
  368. outq = &conn_info->outq;
  369. msg_send.msg_iov = &iov_send;
  370. msg_send.msg_name = 0;
  371. msg_send.msg_namelen = 0;
  372. msg_send.msg_iovlen = 1;
  373. msg_send.msg_control = 0;
  374. msg_send.msg_controllen = 0;
  375. msg_send.msg_flags = 0;
  376. while (!queue_is_empty (outq)) {
  377. queue_item = queue_item_get (outq);
  378. msg_addr = (char *)queue_item->msg;
  379. msg_addr = &msg_addr[conn_info->byte_start];
  380. iov_send.iov_base = msg_addr;
  381. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  382. retry_sendmsg:
  383. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  384. if (res == -1 && errno == EINTR) {
  385. goto retry_sendmsg;
  386. }
  387. if (res == -1 && errno == EAGAIN) {
  388. pthread_mutex_unlock (&conn_info->mutex);
  389. return (0);
  390. }
  391. if (res == -1 && errno == EPIPE) {
  392. libais_disconnect_delayed (conn_info);
  393. return (0);
  394. }
  395. if (res == -1) {
  396. printf ("ERRNO is %d\n", errno);
  397. assert (0); /* some other unhandled error here */
  398. }
  399. if (res + conn_info->byte_start != queue_item->mlen) {
  400. conn_info->byte_start += res;
  401. pthread_mutex_unlock (&conn_info->mutex);
  402. return (0);
  403. }
  404. /*
  405. * Message sent, try sending another message
  406. */
  407. queue_item_remove (outq);
  408. conn_info->byte_start = 0;
  409. free (queue_item->msg);
  410. } /* while queue not empty */
  411. if (queue_is_empty (outq)) {
  412. conn_info->events = POLLIN|POLLNVAL;
  413. }
  414. pthread_mutex_unlock (&conn_info->mutex);
  415. return (0);
  416. }
  417. struct res_overlay {
  418. struct res_header header;
  419. char buf[4096];
  420. };
  421. static int libais_deliver (struct conn_info *conn_info)
  422. {
  423. int res;
  424. struct req_header *header;
  425. int service;
  426. struct msghdr msg_recv;
  427. struct iovec iov_recv;
  428. #ifdef OPENAIS_LINUX
  429. struct cmsghdr *cmsg;
  430. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  431. struct ucred *cred;
  432. int on = 0;
  433. #else
  434. uid_t euid;
  435. gid_t egid;
  436. #endif
  437. int send_ok = 0;
  438. int send_ok_joined = 0;
  439. struct iovec send_ok_joined_iovec;
  440. struct res_overlay res_overlay;
  441. msg_recv.msg_iov = &iov_recv;
  442. msg_recv.msg_iovlen = 1;
  443. msg_recv.msg_name = 0;
  444. msg_recv.msg_namelen = 0;
  445. msg_recv.msg_flags = 0;
  446. if (conn_info->authenticated) {
  447. msg_recv.msg_control = 0;
  448. msg_recv.msg_controllen = 0;
  449. } else {
  450. #ifdef OPENAIS_LINUX
  451. msg_recv.msg_control = (void *)cmsg_cred;
  452. msg_recv.msg_controllen = sizeof (cmsg_cred);
  453. #else
  454. euid = -1; egid = -1;
  455. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  456. (euid == 0 || egid == g_gid_valid)) {
  457. conn_info->authenticated = 1;
  458. }
  459. if (conn_info->authenticated == 0) {
  460. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, gid_valid);
  461. }
  462. #endif
  463. }
  464. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  465. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  466. assert (iov_recv.iov_len != 0);
  467. retry_recv:
  468. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  469. if (res == -1 && errno == EINTR) {
  470. goto retry_recv;
  471. } else
  472. if (res == -1 && errno != EAGAIN) {
  473. goto error_exit;
  474. } else
  475. if (res == 0) {
  476. res = -1;
  477. goto error_exit;
  478. }
  479. /*
  480. * Authenticate if this connection has not been authenticated
  481. */
  482. #ifdef OPENAIS_LINUX
  483. if (conn_info->authenticated == 0) {
  484. cmsg = CMSG_FIRSTHDR (&msg_recv);
  485. cred = (struct ucred *)CMSG_DATA (cmsg);
  486. if (cred) {
  487. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  488. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  489. conn_info->authenticated = 1;
  490. }
  491. }
  492. if (conn_info->authenticated == 0) {
  493. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  494. }
  495. }
  496. #endif
  497. /*
  498. * Dispatch all messages received in recvmsg that can be dispatched
  499. * sizeof (struct req_header) needed at minimum to do any processing
  500. */
  501. conn_info->inb_inuse += res;
  502. conn_info->inb_start += res;
  503. while (conn_info->inb_inuse >= sizeof (struct req_header) && res != -1) {
  504. header = (struct req_header *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  505. if (header->size > conn_info->inb_inuse) {
  506. break;
  507. }
  508. service = conn_info->service;
  509. /*
  510. * If this service is in init phase, initialize service
  511. * else handle message using service service
  512. */
  513. if (service == SOCKET_SERVICE_INIT) {
  514. res = ais_init_service[header->id] (conn_info, header);
  515. } else {
  516. /*
  517. * Not an init service, but a standard service
  518. */
  519. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  520. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  521. header->id, ais_service[service]->lib_service_count);
  522. res = -1;
  523. goto error_exit;
  524. }
  525. /*
  526. * If flow control is required of the library handle, determine that
  527. * openais is not in synchronization and that totempg has room available
  528. * to queue a message, otherwise tell the library we are busy and to
  529. * try again later
  530. */
  531. send_ok_joined_iovec.iov_base = header;
  532. send_ok_joined_iovec.iov_len = header->size;
  533. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  534. &send_ok_joined_iovec, 1);
  535. send_ok =
  536. (sync_primary_designated() == 1) && (
  537. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  538. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  539. (send_ok_joined) &&
  540. (sync_in_process() == 0)));
  541. if (send_ok) {
  542. // *prio = 0;
  543. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  544. } else {
  545. // *prio = (*prio) + 1;
  546. /*
  547. * Overload, tell library to retry
  548. */
  549. res_overlay.header.size =
  550. ais_service[service]->lib_service[header->id].response_size;
  551. res_overlay.header.id =
  552. ais_service[service]->lib_service[header->id].response_id;
  553. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  554. openais_conn_send_response (
  555. conn_info,
  556. &res_overlay,
  557. res_overlay.header.size);
  558. }
  559. }
  560. conn_info->inb_inuse -= header->size;
  561. } /* while */
  562. if (conn_info->inb_inuse == 0) {
  563. conn_info->inb_start = 0;
  564. } else
  565. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  566. if (conn_info->inb_start >= SIZEINB) {
  567. /*
  568. * If in buffer is full, move it back to start
  569. */
  570. memmove (conn_info->inb,
  571. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  572. sizeof (char) * conn_info->inb_inuse);
  573. conn_info->inb_start = conn_info->inb_inuse;
  574. }
  575. return (0);
  576. error_exit:
  577. return (res);
  578. }
  579. char delivery_data[MESSAGE_SIZE_MAX];
  580. static void deliver_fn (
  581. struct totem_ip_address *source_addr,
  582. struct iovec *iovec,
  583. int iov_len,
  584. int endian_conversion_required)
  585. {
  586. struct req_header *header;
  587. int pos = 0;
  588. int i;
  589. int service;
  590. int fn_id;
  591. /*
  592. * Build buffer without iovecs to make processing easier
  593. * This is only used for messages which are multicast with iovecs
  594. * and self-delivered. All other mechanisms avoid the copy.
  595. */
  596. if (iov_len > 1) {
  597. for (i = 0; i < iov_len; i++) {
  598. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  599. pos += iovec[i].iov_len;
  600. assert (pos < MESSAGE_SIZE_MAX);
  601. }
  602. header = (struct req_header *)delivery_data;
  603. } else {
  604. header = (struct req_header *)iovec[0].iov_base;
  605. }
  606. if (endian_conversion_required) {
  607. header->id = swab32 (header->id);
  608. header->size = swab32 (header->size);
  609. }
  610. // assert(iovec->iov_len == header->size);
  611. /*
  612. * Call the proper executive handler
  613. */
  614. service = header->id >> 16;
  615. fn_id = header->id & 0xffff;
  616. if (endian_conversion_required) {
  617. ais_service[service]->exec_service[fn_id].exec_endian_convert_fn
  618. (header);
  619. }
  620. ais_service[service]->exec_service[fn_id].exec_handler_fn
  621. (header, source_addr);
  622. }
  623. static int poll_handler_libais_accept (
  624. poll_handle handle,
  625. int fd,
  626. int revent,
  627. void *data)
  628. {
  629. socklen_t addrlen;
  630. struct sockaddr_un un_addr;
  631. int new_fd;
  632. #ifdef OPENAIS_LINUX
  633. int on = 1;
  634. #endif
  635. int res;
  636. addrlen = sizeof (struct sockaddr_un);
  637. retry_accept:
  638. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  639. if (new_fd == -1 && errno == EINTR) {
  640. goto retry_accept;
  641. }
  642. if (new_fd == -1) {
  643. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  644. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  645. }
  646. totemip_nosigpipe(new_fd);
  647. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  648. if (res == -1) {
  649. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  650. close (new_fd);
  651. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  652. }
  653. /*
  654. * Valid accept
  655. */
  656. /*
  657. * Request credentials of sender provided by kernel
  658. */
  659. #ifdef OPENAIS_LINUX
  660. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  661. #endif
  662. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  663. res = conn_info_create (new_fd);
  664. if (res != 0) {
  665. close (new_fd);
  666. }
  667. return (0);
  668. }
  669. /*
  670. * Exported functions
  671. */
  672. int message_source_is_local(struct message_source *source)
  673. {
  674. int ret = 0;
  675. assert (source != NULL);
  676. if ((totemip_localhost_check(&source->addr)
  677. ||(totemip_equal(&source->addr, my_ip)))) {
  678. ret = 1;
  679. }
  680. return ret;
  681. }
  682. void message_source_set (
  683. struct message_source *source,
  684. void *conn)
  685. {
  686. assert ((source != NULL) && (conn != NULL));
  687. totemip_copy(&source->addr, this_ip);
  688. source->conn = conn;
  689. }
  690. void openais_ipc_init (
  691. poll_handle ais_poll_handle,
  692. unsigned int gid_valid,
  693. struct totem_ip_address *my_ip_in)
  694. {
  695. int libais_server_fd;
  696. struct sockaddr_un un_addr;
  697. int res;
  698. /*
  699. * Create socket for libais clients, name socket, listen for connections
  700. */
  701. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  702. if (libais_server_fd == -1) {
  703. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  704. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  705. };
  706. totemip_nosigpipe(libais_server_fd);
  707. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  708. if (res == -1) {
  709. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  710. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  711. }
  712. #if !defined(OPENAIS_LINUX)
  713. unlink(socketname);
  714. #endif
  715. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  716. un_addr.sun_family = AF_UNIX;
  717. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  718. un_addr.sun_len = sizeof(struct sockaddr_un);
  719. #endif
  720. #if defined(OPENAIS_LINUX)
  721. strcpy (un_addr.sun_path + 1, socketname);
  722. #else
  723. strcpy (un_addr.sun_path, socketname);
  724. #endif
  725. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  726. if (res) {
  727. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  728. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  729. }
  730. listen (libais_server_fd, SERVER_BACKLOG);
  731. /*
  732. * Setup libais connection dispatch routine
  733. */
  734. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  735. POLLIN, 0, poll_handler_libais_accept);
  736. g_gid_valid = gid_valid;
  737. my_ip = my_ip_in;
  738. signal (SIGUSR1, sigusr1_handler);
  739. }
  740. /*
  741. * Get the conn info private data
  742. */
  743. void *openais_conn_private_data_get (void *conn)
  744. {
  745. struct conn_info *conn_info = (struct conn_info *)conn;
  746. if (conn != NULL) {
  747. return ((void *)conn_info->private_data);
  748. } else {
  749. return NULL;
  750. }
  751. }
  752. /*
  753. * Get the conn info partner connection
  754. */
  755. void *openais_conn_partner_get (void *conn)
  756. {
  757. struct conn_info *conn_info = (struct conn_info *)conn;
  758. if (conn != NULL) {
  759. return ((void *)conn_info->conn_info_partner);
  760. } else {
  761. return NULL;
  762. }
  763. }
  764. int openais_conn_send_response (
  765. void *conn,
  766. void *msg,
  767. int mlen)
  768. {
  769. struct queue *outq;
  770. char *cmsg;
  771. int res = 0;
  772. int queue_empty;
  773. struct outq_item *queue_item;
  774. struct outq_item queue_item_out;
  775. struct msghdr msg_send;
  776. struct iovec iov_send;
  777. char *msg_addr;
  778. struct conn_info *conn_info = (struct conn_info *)conn;
  779. if (conn_info == NULL) {
  780. return -1;
  781. }
  782. if (!libais_connection_active (conn_info)) {
  783. return (-1);
  784. }
  785. pthread_mutex_lock (&conn_info->mutex);
  786. outq = &conn_info->outq;
  787. msg_send.msg_iov = &iov_send;
  788. msg_send.msg_name = 0;
  789. msg_send.msg_namelen = 0;
  790. msg_send.msg_iovlen = 1;
  791. msg_send.msg_control = 0;
  792. msg_send.msg_controllen = 0;
  793. msg_send.msg_flags = 0;
  794. if (queue_is_full (outq)) {
  795. /*
  796. * Start a disconnect if we have not already started one
  797. * and report that the outgoing queue is full
  798. */
  799. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  800. libais_disconnect_delayed (conn_info);
  801. pthread_mutex_unlock (&conn_info->mutex);
  802. return (-1);
  803. }
  804. while (!queue_is_empty (outq)) {
  805. queue_item = queue_item_get (outq);
  806. msg_addr = (char *)queue_item->msg;
  807. msg_addr = &msg_addr[conn_info->byte_start];
  808. iov_send.iov_base = msg_addr;
  809. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  810. retry_sendmsg:
  811. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  812. if (res == -1 && errno == EINTR) {
  813. goto retry_sendmsg;
  814. }
  815. if (res == -1 && errno == EAGAIN) {
  816. break; /* outgoing kernel queue full */
  817. }
  818. if (res == -1 && errno == EPIPE) {
  819. libais_disconnect_delayed (conn_info);
  820. return (0);
  821. }
  822. if (res == -1) {
  823. assert (0);
  824. break; /* some other error, stop trying to send message */
  825. }
  826. if (res + conn_info->byte_start != queue_item->mlen) {
  827. conn_info->byte_start += res;
  828. break;
  829. }
  830. /*
  831. * Message sent, try sending another message
  832. */
  833. queue_item_remove (outq);
  834. conn_info->byte_start = 0;
  835. free (queue_item->msg);
  836. } /* while queue not empty */
  837. res = -1;
  838. queue_empty = queue_is_empty (outq);
  839. /*
  840. * Send requested message
  841. */
  842. if (queue_empty) {
  843. iov_send.iov_base = msg;
  844. iov_send.iov_len = mlen;
  845. retry_sendmsg_two:
  846. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  847. if (res == -1 && errno == EINTR) {
  848. goto retry_sendmsg_two;
  849. }
  850. if (res == -1 && errno == EAGAIN) {
  851. conn_info->byte_start = 0;
  852. conn_info->events = POLLIN|POLLNVAL;
  853. }
  854. if (res != -1) {
  855. if (res + conn_info->byte_start != mlen) {
  856. conn_info->byte_start += res;
  857. res = -1;
  858. } else {
  859. conn_info->byte_start = 0;
  860. conn_info->events = POLLIN|POLLNVAL;
  861. }
  862. }
  863. }
  864. /*
  865. * If res == -1 , errrno == EAGAIN which means kernel queue full
  866. */
  867. if (res == -1) {
  868. cmsg = malloc (mlen);
  869. if (cmsg == 0) {
  870. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  871. libais_disconnect_delayed (conn_info);
  872. return (-1);
  873. }
  874. queue_item_out.msg = cmsg;
  875. queue_item_out.mlen = mlen;
  876. memcpy (cmsg, msg, mlen);
  877. queue_item_add (outq, &queue_item_out);
  878. /*
  879. * Send a pthread_kill to interrupt the poll syscall
  880. * and start a new poll operation in the thread
  881. */
  882. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  883. pthread_kill (conn_info->thread, SIGUSR1);
  884. }
  885. pthread_mutex_unlock (&conn_info->mutex);
  886. return (0);
  887. }
  888. pthread_mutex_t *openais_ipc_mutex_get (void)
  889. {
  890. return (&serialize_input);
  891. }