ipc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  543. } else {
  544. /*
  545. * Overload, tell library to retry
  546. */
  547. res_overlay.header.size =
  548. ais_service[service]->lib_service[header->id].response_size;
  549. res_overlay.header.id =
  550. ais_service[service]->lib_service[header->id].response_id;
  551. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  552. openais_conn_send_response (
  553. conn_info,
  554. &res_overlay,
  555. res_overlay.header.size);
  556. }
  557. }
  558. conn_info->inb_inuse -= header->size;
  559. } /* while */
  560. if (conn_info->inb_inuse == 0) {
  561. conn_info->inb_start = 0;
  562. } else
  563. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  564. if (conn_info->inb_start >= SIZEINB) {
  565. /*
  566. * If in buffer is full, move it back to start
  567. */
  568. memmove (conn_info->inb,
  569. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  570. sizeof (char) * conn_info->inb_inuse);
  571. conn_info->inb_start = conn_info->inb_inuse;
  572. }
  573. return (0);
  574. error_exit:
  575. return (res);
  576. }
  577. char delivery_data[MESSAGE_SIZE_MAX];
  578. static void deliver_fn (
  579. struct totem_ip_address *source_addr,
  580. struct iovec *iovec,
  581. int iov_len,
  582. int endian_conversion_required)
  583. {
  584. struct req_header *header;
  585. int pos = 0;
  586. int i;
  587. int service;
  588. int fn_id;
  589. /*
  590. * Build buffer without iovecs to make processing easier
  591. * This is only used for messages which are multicast with iovecs
  592. * and self-delivered. All other mechanisms avoid the copy.
  593. */
  594. if (iov_len > 1) {
  595. for (i = 0; i < iov_len; i++) {
  596. memcpy (&delivery_data[pos], iovec[i].iov_base, iovec[i].iov_len);
  597. pos += iovec[i].iov_len;
  598. assert (pos < MESSAGE_SIZE_MAX);
  599. }
  600. header = (struct req_header *)delivery_data;
  601. } else {
  602. header = (struct req_header *)iovec[0].iov_base;
  603. }
  604. if (endian_conversion_required) {
  605. header->id = swab32 (header->id);
  606. header->size = swab32 (header->size);
  607. }
  608. // assert(iovec->iov_len == header->size);
  609. /*
  610. * Call the proper executive handler
  611. */
  612. service = header->id >> 16;
  613. fn_id = header->id & 0xffff;
  614. if (endian_conversion_required) {
  615. ais_service[service]->exec_service[fn_id].exec_endian_convert_fn
  616. (header);
  617. }
  618. ais_service[service]->exec_service[fn_id].exec_handler_fn
  619. (header, source_addr);
  620. }
  621. static int poll_handler_libais_accept (
  622. poll_handle handle,
  623. int fd,
  624. int revent,
  625. void *data)
  626. {
  627. socklen_t addrlen;
  628. struct sockaddr_un un_addr;
  629. int new_fd;
  630. #ifdef OPENAIS_LINUX
  631. int on = 1;
  632. #endif
  633. int res;
  634. addrlen = sizeof (struct sockaddr_un);
  635. retry_accept:
  636. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  637. if (new_fd == -1 && errno == EINTR) {
  638. goto retry_accept;
  639. }
  640. if (new_fd == -1) {
  641. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  642. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  643. }
  644. totemip_nosigpipe(new_fd);
  645. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  646. if (res == -1) {
  647. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  648. close (new_fd);
  649. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  650. }
  651. /*
  652. * Valid accept
  653. */
  654. /*
  655. * Request credentials of sender provided by kernel
  656. */
  657. #ifdef OPENAIS_LINUX
  658. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  659. #endif
  660. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  661. res = conn_info_create (new_fd);
  662. if (res != 0) {
  663. close (new_fd);
  664. }
  665. return (0);
  666. }
  667. /*
  668. * Exported functions
  669. */
  670. int message_source_is_local(struct message_source *source)
  671. {
  672. int ret = 0;
  673. assert (source != NULL);
  674. if ((totemip_localhost_check(&source->addr)
  675. ||(totemip_equal(&source->addr, my_ip)))) {
  676. ret = 1;
  677. }
  678. return ret;
  679. }
  680. void message_source_set (
  681. struct message_source *source,
  682. void *conn)
  683. {
  684. assert ((source != NULL) && (conn != NULL));
  685. totemip_copy(&source->addr, this_ip);
  686. source->conn = conn;
  687. }
  688. void openais_ipc_init (
  689. poll_handle ais_poll_handle,
  690. unsigned int gid_valid,
  691. struct totem_ip_address *my_ip_in)
  692. {
  693. int libais_server_fd;
  694. struct sockaddr_un un_addr;
  695. int res;
  696. /*
  697. * Create socket for libais clients, name socket, listen for connections
  698. */
  699. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  700. if (libais_server_fd == -1) {
  701. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  702. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  703. };
  704. totemip_nosigpipe(libais_server_fd);
  705. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  706. if (res == -1) {
  707. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  708. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  709. }
  710. #if !defined(OPENAIS_LINUX)
  711. unlink(socketname);
  712. #endif
  713. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  714. un_addr.sun_family = AF_UNIX;
  715. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  716. un_addr.sun_len = sizeof(struct sockaddr_un);
  717. #endif
  718. #if defined(OPENAIS_LINUX)
  719. strcpy (un_addr.sun_path + 1, socketname);
  720. #else
  721. strcpy (un_addr.sun_path, socketname);
  722. #endif
  723. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  724. if (res) {
  725. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  726. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  727. }
  728. listen (libais_server_fd, SERVER_BACKLOG);
  729. /*
  730. * Setup libais connection dispatch routine
  731. */
  732. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  733. POLLIN, 0, poll_handler_libais_accept);
  734. g_gid_valid = gid_valid;
  735. my_ip = my_ip_in;
  736. signal (SIGUSR1, sigusr1_handler);
  737. }
  738. /*
  739. * Get the conn info private data
  740. */
  741. void *openais_conn_private_data_get (void *conn)
  742. {
  743. struct conn_info *conn_info = (struct conn_info *)conn;
  744. if (conn != NULL) {
  745. return ((void *)conn_info->private_data);
  746. } else {
  747. return NULL;
  748. }
  749. }
  750. /*
  751. * Get the conn info partner connection
  752. */
  753. void *openais_conn_partner_get (void *conn)
  754. {
  755. struct conn_info *conn_info = (struct conn_info *)conn;
  756. if (conn != NULL) {
  757. return ((void *)conn_info->conn_info_partner);
  758. } else {
  759. return NULL;
  760. }
  761. }
  762. int openais_conn_send_response (
  763. void *conn,
  764. void *msg,
  765. int mlen)
  766. {
  767. struct queue *outq;
  768. char *cmsg;
  769. int res = 0;
  770. int queue_empty;
  771. struct outq_item *queue_item;
  772. struct outq_item queue_item_out;
  773. struct msghdr msg_send;
  774. struct iovec iov_send;
  775. char *msg_addr;
  776. struct conn_info *conn_info = (struct conn_info *)conn;
  777. if (conn_info == NULL) {
  778. return -1;
  779. }
  780. if (!libais_connection_active (conn_info)) {
  781. return (-1);
  782. }
  783. pthread_mutex_lock (&conn_info->mutex);
  784. outq = &conn_info->outq;
  785. msg_send.msg_iov = &iov_send;
  786. msg_send.msg_name = 0;
  787. msg_send.msg_namelen = 0;
  788. msg_send.msg_iovlen = 1;
  789. msg_send.msg_control = 0;
  790. msg_send.msg_controllen = 0;
  791. msg_send.msg_flags = 0;
  792. if (queue_is_full (outq)) {
  793. /*
  794. * Start a disconnect if we have not already started one
  795. * and report that the outgoing queue is full
  796. */
  797. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  798. libais_disconnect_delayed (conn_info);
  799. pthread_mutex_unlock (&conn_info->mutex);
  800. return (-1);
  801. }
  802. while (!queue_is_empty (outq)) {
  803. queue_item = queue_item_get (outq);
  804. msg_addr = (char *)queue_item->msg;
  805. msg_addr = &msg_addr[conn_info->byte_start];
  806. iov_send.iov_base = msg_addr;
  807. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  808. retry_sendmsg:
  809. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  810. if (res == -1 && errno == EINTR) {
  811. goto retry_sendmsg;
  812. }
  813. if (res == -1 && errno == EAGAIN) {
  814. break; /* outgoing kernel queue full */
  815. }
  816. if (res == -1 && errno == EPIPE) {
  817. libais_disconnect_delayed (conn_info);
  818. return (0);
  819. }
  820. if (res == -1) {
  821. assert (0);
  822. break; /* some other error, stop trying to send message */
  823. }
  824. if (res + conn_info->byte_start != queue_item->mlen) {
  825. conn_info->byte_start += res;
  826. break;
  827. }
  828. /*
  829. * Message sent, try sending another message
  830. */
  831. queue_item_remove (outq);
  832. conn_info->byte_start = 0;
  833. free (queue_item->msg);
  834. } /* while queue not empty */
  835. res = -1;
  836. queue_empty = queue_is_empty (outq);
  837. /*
  838. * Send requested message
  839. */
  840. if (queue_empty) {
  841. iov_send.iov_base = msg;
  842. iov_send.iov_len = mlen;
  843. retry_sendmsg_two:
  844. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  845. if (res == -1 && errno == EINTR) {
  846. goto retry_sendmsg_two;
  847. }
  848. if (res == -1 && errno == EAGAIN) {
  849. conn_info->byte_start = 0;
  850. conn_info->events = POLLIN|POLLNVAL;
  851. }
  852. if (res != -1) {
  853. if (res + conn_info->byte_start != mlen) {
  854. conn_info->byte_start += res;
  855. res = -1;
  856. } else {
  857. conn_info->byte_start = 0;
  858. conn_info->events = POLLIN|POLLNVAL;
  859. }
  860. }
  861. }
  862. /*
  863. * If res == -1 , errrno == EAGAIN which means kernel queue full
  864. */
  865. if (res == -1) {
  866. cmsg = malloc (mlen);
  867. if (cmsg == 0) {
  868. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  869. libais_disconnect_delayed (conn_info);
  870. return (-1);
  871. }
  872. queue_item_out.msg = cmsg;
  873. queue_item_out.mlen = mlen;
  874. memcpy (cmsg, msg, mlen);
  875. queue_item_add (outq, &queue_item_out);
  876. /*
  877. * Send a pthread_kill to interrupt the poll syscall
  878. * and start a new poll operation in the thread
  879. */
  880. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  881. pthread_kill (conn_info->thread, SIGUSR1);
  882. }
  883. pthread_mutex_unlock (&conn_info->mutex);
  884. return (0);
  885. }
  886. pthread_mutex_t *openais_ipc_mutex_get (void)
  887. {
  888. return (&serialize_input);
  889. }