ipc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 "ipc.h"
  69. #include "service.h"
  70. #include "sync.h"
  71. #include "swab.h"
  72. #include "objdb.h"
  73. #include "config.h"
  74. #include "tlist.h"
  75. #define LOG_SERVICE LOG_SERVICE_IPC
  76. #include "print.h"
  77. #include "util.h"
  78. #define SERVER_BACKLOG 5
  79. static unsigned int g_gid_valid = 0;
  80. static struct totem_ip_address *my_ip;
  81. static void (*ipc_serialize_lock_fn) (void);
  82. static void (*ipc_serialize_unlock_fn) (void);
  83. struct outq_item {
  84. void *msg;
  85. size_t mlen;
  86. };
  87. enum conn_state {
  88. CONN_STATE_CONNECTING,
  89. CONN_STATE_CONNECTED,
  90. CONN_STATE_DISCONNECTING,
  91. CONN_STATE_DISCONNECTING_DELAYED
  92. };
  93. enum disc_state {
  94. DISC_STATE_EXITING,
  95. DISC_STATE_EXITED
  96. };
  97. struct conn_info {
  98. int fd; /* File descriptor */
  99. unsigned int events; /* events polled for by file descriptor */
  100. enum conn_state state; /* State of this connection */
  101. pthread_t thread; /* thread identifier */
  102. pthread_attr_t thread_attr; /* thread attribute */
  103. char *inb; /* Input buffer for non-blocking reads */
  104. int inb_nextheader; /* Next message header starts here */
  105. int inb_start; /* Start location of input buffer */
  106. int inb_inuse; /* Bytes currently stored in input buffer */
  107. struct queue outq; /* Circular queue for outgoing requests */
  108. int byte_start; /* Byte to start sending from in head of queue */
  109. enum service_types service;/* Type of service so dispatch knows how to route message */
  110. int authenticated; /* Is this connection authenticated? */
  111. void *private_data; /* library connection private data */
  112. struct conn_info *conn_info_partner; /* partner connection dispatch<->response */
  113. enum disc_state disc; /* disconnect state */
  114. struct timerlist timerlist;
  115. pthread_mutex_t mutex;
  116. };
  117. static void *prioritized_poll_thread (void *conn);
  118. static int conn_info_outq_flush (struct conn_info *conn_info);
  119. static int libais_deliver (struct conn_info *conn_info);
  120. /*
  121. * IPC Initializers
  122. */
  123. static int response_init_send_response (
  124. struct conn_info *conn_info,
  125. void *message);
  126. static int dispatch_init_send_response (
  127. struct conn_info *conn_info,
  128. void *message);
  129. static int (*ais_init_service[]) (struct conn_info *conn_info, void *message) = {
  130. response_init_send_response,
  131. dispatch_init_send_response
  132. };
  133. static int response_init_send_response (
  134. struct conn_info *conn_info,
  135. void *message)
  136. {
  137. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  138. mar_req_lib_response_init_t *req_lib_response_init = (mar_req_lib_response_init_t *)message;
  139. mar_res_lib_response_init_t res_lib_response_init;
  140. if (conn_info->authenticated) {
  141. conn_info->service = req_lib_response_init->resdis_header.service;
  142. error = SA_AIS_OK;
  143. }
  144. res_lib_response_init.header.size = sizeof (mar_res_lib_response_init_t);
  145. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  146. res_lib_response_init.header.error = error;
  147. res_lib_response_init.conn_info = conn_info;
  148. openais_conn_send_response (
  149. conn_info,
  150. &res_lib_response_init,
  151. sizeof (res_lib_response_init));
  152. if (error == SA_AIS_ERR_ACCESS) {
  153. return (-1);
  154. }
  155. conn_info->disc = DISC_STATE_EXITING;
  156. return (0);
  157. }
  158. static int dispatch_init_send_response (
  159. struct conn_info *conn_info,
  160. void *message)
  161. {
  162. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  163. mar_req_lib_dispatch_init_t *req_lib_dispatch_init = (mar_req_lib_dispatch_init_t *)message;
  164. mar_res_lib_dispatch_init_t res_lib_dispatch_init;
  165. struct conn_info *msg_conn_info;
  166. if (conn_info->authenticated) {
  167. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  168. if (!ais_service[req_lib_dispatch_init->resdis_header.service])
  169. error = SA_AIS_ERR_NOT_SUPPORTED;
  170. else
  171. error = SA_AIS_OK;
  172. conn_info->conn_info_partner = (struct conn_info *)req_lib_dispatch_init->conn_info;
  173. msg_conn_info = (struct conn_info *)req_lib_dispatch_init->conn_info;
  174. msg_conn_info->conn_info_partner = conn_info;
  175. if (error == SA_AIS_OK) {
  176. int private_data_size;
  177. private_data_size = ais_service[req_lib_dispatch_init->resdis_header.service]->private_data_size;
  178. if (private_data_size) {
  179. conn_info->private_data = malloc (private_data_size);
  180. conn_info->conn_info_partner->private_data = conn_info->private_data;
  181. if (conn_info->private_data == NULL) {
  182. error = SA_AIS_ERR_NO_MEMORY;
  183. } else {
  184. memset (conn_info->private_data, 0, private_data_size);
  185. }
  186. } else {
  187. conn_info->private_data = NULL;
  188. conn_info->conn_info_partner->private_data = NULL;
  189. }
  190. }
  191. res_lib_dispatch_init.header.size = sizeof (mar_res_lib_dispatch_init_t);
  192. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  193. res_lib_dispatch_init.header.error = error;
  194. openais_conn_send_response (
  195. conn_info,
  196. &res_lib_dispatch_init,
  197. sizeof (res_lib_dispatch_init));
  198. if (error != SA_AIS_OK) {
  199. return (-1);
  200. }
  201. }
  202. conn_info->state = CONN_STATE_CONNECTED;
  203. conn_info->disc = DISC_STATE_EXITING;
  204. ais_service[req_lib_dispatch_init->resdis_header.service]->lib_init_fn (conn_info);
  205. return (0);
  206. }
  207. /*
  208. * Create a connection data structure
  209. */
  210. static inline unsigned int conn_info_create (int fd) {
  211. struct conn_info *conn_info;
  212. int res;
  213. conn_info = malloc (sizeof (struct conn_info));
  214. if (conn_info == 0) {
  215. return (ENOMEM);
  216. }
  217. memset (conn_info, 0, sizeof (struct conn_info));
  218. res = queue_init (&conn_info->outq, SIZEQUEUE,
  219. sizeof (struct outq_item));
  220. if (res != 0) {
  221. free (conn_info);
  222. return (ENOMEM);
  223. }
  224. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  225. if (conn_info->inb == 0) {
  226. queue_free (&conn_info->outq);
  227. free (conn_info);
  228. return (ENOMEM);
  229. }
  230. conn_info->state = CONN_STATE_CONNECTING;
  231. conn_info->fd = fd;
  232. conn_info->events = POLLIN|POLLNVAL;
  233. conn_info->service = SOCKET_SERVICE_INIT;
  234. pthread_mutex_init (&conn_info->mutex, NULL);
  235. pthread_attr_init (&conn_info->thread_attr);
  236. pthread_attr_setstacksize (&conn_info->thread_attr, 100000);
  237. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_DETACHED);
  238. res = pthread_create (&conn_info->thread, &conn_info->thread_attr,
  239. prioritized_poll_thread, conn_info);
  240. return (res);
  241. }
  242. static void conn_info_destroy (struct conn_info *conn_info)
  243. {
  244. struct outq_item *outq_item;
  245. close (conn_info->fd);
  246. /*
  247. * Free the outq queued items
  248. */
  249. while (!queue_is_empty (&conn_info->outq)) {
  250. outq_item = queue_item_get (&conn_info->outq);
  251. free (outq_item->msg);
  252. queue_item_remove (&conn_info->outq);
  253. }
  254. queue_free (&conn_info->outq);
  255. free (conn_info->inb);
  256. }
  257. static int libais_connection_active (struct conn_info *conn_info)
  258. {
  259. return (conn_info->state == CONN_STATE_CONNECTED ||
  260. conn_info->state == CONN_STATE_CONNECTING);
  261. }
  262. static void libais_disconnect_delayed (struct conn_info *conn_info)
  263. {
  264. conn_info->state = CONN_STATE_DISCONNECTING_DELAYED;
  265. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTING_DELAYED;
  266. }
  267. static int libais_disconnect (struct conn_info *conn_info)
  268. {
  269. int res = 0;
  270. if (conn_info->disc == DISC_STATE_EXITING) {
  271. if (conn_info->service != SOCKET_SERVICE_INIT && ais_service[conn_info->service]->lib_exit_fn) {
  272. res = ais_service[conn_info->service]->lib_exit_fn (conn_info);
  273. if (res == 0) {
  274. conn_info->disc = DISC_STATE_EXITED;
  275. conn_info->conn_info_partner->disc = DISC_STATE_EXITED;
  276. }
  277. return (res);
  278. }
  279. conn_info->disc = DISC_STATE_EXITED;
  280. if (conn_info->conn_info_partner)
  281. conn_info->conn_info_partner->disc = DISC_STATE_EXITED;
  282. return (0);
  283. }
  284. conn_info_destroy (conn_info);
  285. conn_info_destroy (conn_info->conn_info_partner);
  286. if (conn_info->private_data) {
  287. free (conn_info->private_data);
  288. }
  289. free (conn_info->conn_info_partner);
  290. free (conn_info);
  291. return (0);
  292. }
  293. /*
  294. * This thread runs in a specific thread priority mode to handle
  295. * I/O requests from the library
  296. */
  297. static void *prioritized_poll_thread (void *conn)
  298. {
  299. struct conn_info *conn_info = (struct conn_info *)conn;
  300. struct pollfd ufd;
  301. int fds;
  302. struct sched_param sched_param;
  303. int res;
  304. int timeout = 1;
  305. sched_param.sched_priority = 1;
  306. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  307. ufd.fd = conn_info->fd;
  308. for (;;) {
  309. retry_poll:
  310. ufd.events = conn_info->events;
  311. ufd.revents = 0;
  312. fds = poll (&ufd, 1, timeout);
  313. if (fds == -1) {
  314. goto retry_poll;
  315. }
  316. timeout = -1;
  317. ipc_serialize_lock_fn ();
  318. if (fds == 1 && ufd.revents) {
  319. if ((ufd.revents & (POLLERR|POLLHUP)) ||
  320. conn_info->state == CONN_STATE_DISCONNECTING_DELAYED) {
  321. res = libais_disconnect (conn_info);
  322. if (res != 0) {
  323. ipc_serialize_unlock_fn ();
  324. continue;
  325. } else {
  326. break;
  327. }
  328. }
  329. if (ufd.revents & POLLOUT) {
  330. conn_info_outq_flush (conn_info);
  331. }
  332. if (conn_info->state == CONN_STATE_CONNECTED && conn_info->conn_info_partner == 0) {
  333. timeout = 10;
  334. ipc_serialize_unlock_fn ();
  335. continue;
  336. }
  337. if ((ufd.revents & POLLIN) == POLLIN) {
  338. libais_deliver (conn_info);
  339. }
  340. }
  341. ipc_serialize_unlock_fn ();
  342. }
  343. ipc_serialize_unlock_fn ();
  344. pthread_exit (0);
  345. return (0);
  346. }
  347. #if defined(OPENAIS_LINUX)
  348. /* SUN_LEN is broken for abstract namespace
  349. */
  350. #define AIS_SUN_LEN(a) sizeof(*(a))
  351. char *socketname = "libais.socket";
  352. #else
  353. #define AIS_SUN_LEN(a) SUN_LEN(a)
  354. char *socketname = "/var/run/libais.socket";
  355. #endif
  356. static int conn_info_outq_flush (struct conn_info *conn_info) {
  357. struct queue *outq;
  358. int res = 0;
  359. struct outq_item *queue_item;
  360. struct msghdr msg_send;
  361. struct iovec iov_send;
  362. char *msg_addr;
  363. pthread_mutex_lock (&conn_info->mutex);
  364. if (!libais_connection_active (conn_info)) {
  365. pthread_mutex_unlock (&conn_info->mutex);
  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. pthread_mutex_unlock (&conn_info->mutex);
  393. libais_disconnect_delayed (conn_info);
  394. return (0);
  395. }
  396. if (res == -1) {
  397. printf ("ERRNO is %d\n", errno);
  398. assert (0); /* some other unhandled error here */
  399. }
  400. if (res + conn_info->byte_start != queue_item->mlen) {
  401. conn_info->byte_start += res;
  402. pthread_mutex_unlock (&conn_info->mutex);
  403. return (0);
  404. }
  405. /*
  406. * Message sent, try sending another message
  407. */
  408. queue_item_remove (outq);
  409. conn_info->byte_start = 0;
  410. free (queue_item->msg);
  411. } /* while queue not empty */
  412. if (queue_is_empty (outq)) {
  413. conn_info->events = POLLIN|POLLNVAL;
  414. }
  415. pthread_mutex_unlock (&conn_info->mutex);
  416. return (0);
  417. }
  418. struct res_overlay {
  419. mar_res_header_t header __attribute((aligned(8)));
  420. char buf[4096];
  421. };
  422. static int libais_deliver (struct conn_info *conn_info)
  423. {
  424. int res;
  425. mar_req_header_t *header;
  426. int service;
  427. struct msghdr msg_recv;
  428. struct iovec iov_recv;
  429. #ifdef OPENAIS_LINUX
  430. struct cmsghdr *cmsg;
  431. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  432. struct ucred *cred;
  433. int on = 0;
  434. #else
  435. uid_t euid;
  436. gid_t egid;
  437. #endif
  438. int send_ok = 0;
  439. int send_ok_joined = 0;
  440. struct iovec send_ok_joined_iovec;
  441. struct res_overlay res_overlay;
  442. msg_recv.msg_iov = &iov_recv;
  443. msg_recv.msg_iovlen = 1;
  444. msg_recv.msg_name = 0;
  445. msg_recv.msg_namelen = 0;
  446. msg_recv.msg_flags = 0;
  447. if (conn_info->authenticated) {
  448. msg_recv.msg_control = 0;
  449. msg_recv.msg_controllen = 0;
  450. } else {
  451. #ifdef OPENAIS_LINUX
  452. msg_recv.msg_control = (void *)cmsg_cred;
  453. msg_recv.msg_controllen = sizeof (cmsg_cred);
  454. #else
  455. euid = -1; egid = -1;
  456. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  457. (euid == 0 || egid == g_gid_valid)) {
  458. conn_info->authenticated = 1;
  459. }
  460. if (conn_info->authenticated == 0) {
  461. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  462. }
  463. #endif
  464. }
  465. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  466. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  467. assert (iov_recv.iov_len != 0);
  468. retry_recv:
  469. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  470. if (res == -1 && errno == EINTR) {
  471. goto retry_recv;
  472. } else
  473. if (res == -1 && errno != EAGAIN) {
  474. goto error_exit;
  475. } else
  476. if (res == 0) {
  477. res = -1;
  478. goto error_exit;
  479. }
  480. /*
  481. * Authenticate if this connection has not been authenticated
  482. */
  483. #ifdef OPENAIS_LINUX
  484. if (conn_info->authenticated == 0) {
  485. cmsg = CMSG_FIRSTHDR (&msg_recv);
  486. cred = (struct ucred *)CMSG_DATA (cmsg);
  487. if (cred) {
  488. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  489. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  490. conn_info->authenticated = 1;
  491. }
  492. }
  493. if (conn_info->authenticated == 0) {
  494. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  495. }
  496. }
  497. #endif
  498. /*
  499. * Dispatch all messages received in recvmsg that can be dispatched
  500. * sizeof (mar_req_header_t) needed at minimum to do any processing
  501. */
  502. conn_info->inb_inuse += res;
  503. conn_info->inb_start += res;
  504. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && res != -1) {
  505. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  506. if (header->size > conn_info->inb_inuse) {
  507. break;
  508. }
  509. service = conn_info->service;
  510. /*
  511. * If this service is in init phase, initialize service
  512. * else handle message using service service
  513. */
  514. if (service == SOCKET_SERVICE_INIT) {
  515. res = ais_init_service[header->id] (conn_info, header);
  516. } else {
  517. /*
  518. * Not an init service, but a standard service
  519. */
  520. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  521. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  522. header->id, ais_service[service]->lib_service_count);
  523. res = -1;
  524. goto error_exit;
  525. }
  526. /*
  527. * If flow control is required of the library handle, determine that
  528. * openais is not in synchronization and that totempg has room available
  529. * to queue a message, otherwise tell the library we are busy and to
  530. * try again later
  531. */
  532. send_ok_joined_iovec.iov_base = header;
  533. send_ok_joined_iovec.iov_len = header->size;
  534. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  535. &send_ok_joined_iovec, 1);
  536. send_ok =
  537. (sync_primary_designated() == 1) && (
  538. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  539. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  540. (send_ok_joined) &&
  541. (sync_in_process() == 0)));
  542. if (send_ok) {
  543. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  544. } else {
  545. /*
  546. * Overload, tell library to retry
  547. */
  548. res_overlay.header.size =
  549. ais_service[service]->lib_service[header->id].response_size;
  550. res_overlay.header.id =
  551. ais_service[service]->lib_service[header->id].response_id;
  552. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  553. openais_conn_send_response (
  554. conn_info,
  555. &res_overlay,
  556. res_overlay.header.size);
  557. }
  558. }
  559. conn_info->inb_inuse -= header->size;
  560. } /* while */
  561. if (conn_info->inb_inuse == 0) {
  562. conn_info->inb_start = 0;
  563. } else
  564. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  565. if (conn_info->inb_start >= SIZEINB) {
  566. /*
  567. * If in buffer is full, move it back to start
  568. */
  569. memmove (conn_info->inb,
  570. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  571. sizeof (char) * conn_info->inb_inuse);
  572. conn_info->inb_start = conn_info->inb_inuse;
  573. }
  574. return (0);
  575. error_exit:
  576. return (res);
  577. }
  578. static int poll_handler_libais_accept (
  579. poll_handle handle,
  580. int fd,
  581. int revent,
  582. void *data)
  583. {
  584. socklen_t addrlen;
  585. struct sockaddr_un un_addr;
  586. int new_fd;
  587. #ifdef OPENAIS_LINUX
  588. int on = 1;
  589. #endif
  590. int res;
  591. addrlen = sizeof (struct sockaddr_un);
  592. retry_accept:
  593. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  594. if (new_fd == -1 && errno == EINTR) {
  595. goto retry_accept;
  596. }
  597. if (new_fd == -1) {
  598. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  599. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  600. }
  601. totemip_nosigpipe(new_fd);
  602. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  603. if (res == -1) {
  604. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  605. close (new_fd);
  606. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  607. }
  608. /*
  609. * Valid accept
  610. */
  611. /*
  612. * Request credentials of sender provided by kernel
  613. */
  614. #ifdef OPENAIS_LINUX
  615. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  616. #endif
  617. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  618. res = conn_info_create (new_fd);
  619. if (res != 0) {
  620. close (new_fd);
  621. }
  622. return (0);
  623. }
  624. /*
  625. * Exported functions
  626. */
  627. int message_source_is_local(mar_message_source_t *source)
  628. {
  629. int ret = 0;
  630. assert (source != NULL);
  631. if (source->nodeid == my_ip->nodeid) {
  632. ret = 1;
  633. }
  634. return ret;
  635. }
  636. void message_source_set (
  637. mar_message_source_t *source,
  638. void *conn)
  639. {
  640. assert ((source != NULL) && (conn != NULL));
  641. source->nodeid = my_ip->nodeid;
  642. source->conn = conn;
  643. }
  644. void openais_ipc_init (
  645. void (*serialize_lock_fn) (void),
  646. void (*serialize_unlock_fn) (void),
  647. unsigned int gid_valid,
  648. struct totem_ip_address *my_ip_in)
  649. {
  650. int libais_server_fd;
  651. struct sockaddr_un un_addr;
  652. int res;
  653. log_init ("IPC");
  654. ipc_serialize_lock_fn = serialize_lock_fn;
  655. ipc_serialize_unlock_fn = serialize_unlock_fn;
  656. /*
  657. * Create socket for libais clients, name socket, listen for connections
  658. */
  659. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  660. if (libais_server_fd == -1) {
  661. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  662. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  663. };
  664. totemip_nosigpipe(libais_server_fd);
  665. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  666. if (res == -1) {
  667. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  668. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  669. }
  670. #if !defined(OPENAIS_LINUX)
  671. unlink(socketname);
  672. #endif
  673. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  674. un_addr.sun_family = AF_UNIX;
  675. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  676. un_addr.sun_len = sizeof(struct sockaddr_un);
  677. #endif
  678. #if defined(OPENAIS_LINUX)
  679. strcpy (un_addr.sun_path + 1, socketname);
  680. #else
  681. strcpy (un_addr.sun_path, socketname);
  682. #endif
  683. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  684. if (res) {
  685. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  686. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  687. }
  688. listen (libais_server_fd, SERVER_BACKLOG);
  689. /*
  690. * Setup libais connection dispatch routine
  691. */
  692. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  693. POLLIN, 0, poll_handler_libais_accept);
  694. g_gid_valid = gid_valid;
  695. my_ip = my_ip_in;
  696. }
  697. /*
  698. * Get the conn info private data
  699. */
  700. void *openais_conn_private_data_get (void *conn)
  701. {
  702. struct conn_info *conn_info = (struct conn_info *)conn;
  703. if (conn != NULL) {
  704. return ((void *)conn_info->private_data);
  705. } else {
  706. return NULL;
  707. }
  708. }
  709. /*
  710. * Get the conn info partner connection
  711. */
  712. void *openais_conn_partner_get (void *conn)
  713. {
  714. struct conn_info *conn_info = (struct conn_info *)conn;
  715. if (conn != NULL) {
  716. return ((void *)conn_info->conn_info_partner);
  717. } else {
  718. return NULL;
  719. }
  720. }
  721. int openais_conn_send_response (
  722. void *conn,
  723. void *msg,
  724. int mlen)
  725. {
  726. struct queue *outq;
  727. char *cmsg;
  728. int res = 0;
  729. int queue_empty;
  730. struct outq_item *queue_item;
  731. struct outq_item queue_item_out;
  732. struct msghdr msg_send;
  733. struct iovec iov_send;
  734. char *msg_addr;
  735. struct conn_info *conn_info = (struct conn_info *)conn;
  736. if (conn_info == NULL) {
  737. return -1;
  738. }
  739. if (!libais_connection_active (conn_info)) {
  740. return (-1);
  741. }
  742. pthread_mutex_lock (&conn_info->mutex);
  743. outq = &conn_info->outq;
  744. msg_send.msg_iov = &iov_send;
  745. msg_send.msg_name = 0;
  746. msg_send.msg_namelen = 0;
  747. msg_send.msg_iovlen = 1;
  748. msg_send.msg_control = 0;
  749. msg_send.msg_controllen = 0;
  750. msg_send.msg_flags = 0;
  751. if (queue_is_full (outq)) {
  752. /*
  753. * Start a disconnect if we have not already started one
  754. * and report that the outgoing queue is full
  755. */
  756. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  757. libais_disconnect_delayed (conn_info);
  758. pthread_mutex_unlock (&conn_info->mutex);
  759. return (-1);
  760. }
  761. while (!queue_is_empty (outq)) {
  762. queue_item = queue_item_get (outq);
  763. msg_addr = (char *)queue_item->msg;
  764. msg_addr = &msg_addr[conn_info->byte_start];
  765. iov_send.iov_base = msg_addr;
  766. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  767. retry_sendmsg:
  768. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  769. if (res == -1 && errno == EINTR) {
  770. goto retry_sendmsg;
  771. }
  772. if (res == -1 && errno == EAGAIN) {
  773. break; /* outgoing kernel queue full */
  774. }
  775. if (res == -1 && errno == EPIPE) {
  776. libais_disconnect_delayed (conn_info);
  777. pthread_mutex_unlock (&conn_info->mutex);
  778. return (0);
  779. }
  780. if (res == -1) {
  781. assert (0);
  782. break; /* some other error, stop trying to send message */
  783. }
  784. if (res + conn_info->byte_start != queue_item->mlen) {
  785. conn_info->byte_start += res;
  786. break;
  787. }
  788. /*
  789. * Message sent, try sending another message
  790. */
  791. queue_item_remove (outq);
  792. conn_info->byte_start = 0;
  793. free (queue_item->msg);
  794. } /* while queue not empty */
  795. res = -1;
  796. queue_empty = queue_is_empty (outq);
  797. /*
  798. * Send requested message
  799. */
  800. if (queue_empty) {
  801. iov_send.iov_base = msg;
  802. iov_send.iov_len = mlen;
  803. retry_sendmsg_two:
  804. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  805. if (res == -1 && errno == EINTR) {
  806. goto retry_sendmsg_two;
  807. }
  808. if (res == -1 && errno == EAGAIN) {
  809. conn_info->byte_start = 0;
  810. conn_info->events = POLLIN|POLLNVAL;
  811. }
  812. if (res != -1) {
  813. if (res + conn_info->byte_start != mlen) {
  814. conn_info->byte_start += res;
  815. res = -1;
  816. } else {
  817. conn_info->byte_start = 0;
  818. conn_info->events = POLLIN|POLLNVAL;
  819. }
  820. }
  821. }
  822. /*
  823. * If res == -1 , errrno == EAGAIN which means kernel queue full
  824. */
  825. if (res == -1) {
  826. cmsg = malloc (mlen);
  827. if (cmsg == 0) {
  828. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  829. libais_disconnect_delayed (conn_info);
  830. return (-1);
  831. }
  832. queue_item_out.msg = cmsg;
  833. queue_item_out.mlen = mlen;
  834. memcpy (cmsg, msg, mlen);
  835. queue_item_add (outq, &queue_item_out);
  836. /*
  837. * Send a pthread_kill to interrupt the poll syscall
  838. * and start a new poll operation in the thread
  839. */
  840. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  841. pthread_kill (conn_info->thread, SIGUSR1);
  842. }
  843. pthread_mutex_unlock (&conn_info->mutex);
  844. return (0);
  845. }
  846. int openais_ipc_timer_add (
  847. void *conn,
  848. void (*timer_fn) (void *data),
  849. void *data,
  850. unsigned int msec_in_future,
  851. timer_handle *handle)
  852. {
  853. struct conn_info *conn_info = (struct conn_info *)conn;
  854. int res;
  855. res = timerlist_add_future (
  856. &conn_info->timerlist,
  857. timer_fn,
  858. data,
  859. msec_in_future,
  860. handle);
  861. return (res);
  862. }
  863. void openais_ipc_timer_del (
  864. void *conn,
  865. timer_handle timer_handle)
  866. {
  867. struct conn_info *conn_info = (struct conn_info *)conn;
  868. timerlist_del (&conn_info->timerlist, timer_handle);
  869. }
  870. void openais_ipc_timer_del_data (
  871. void *conn,
  872. timer_handle timer_handle)
  873. {
  874. struct conn_info *conn_info = (struct conn_info *)conn;
  875. timerlist_del (&conn_info->timerlist, timer_handle);
  876. }