ipc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. unsigned int *prio)
  629. {
  630. socklen_t addrlen;
  631. struct sockaddr_un un_addr;
  632. int new_fd;
  633. #ifdef OPENAIS_LINUX
  634. int on = 1;
  635. #endif
  636. int res;
  637. addrlen = sizeof (struct sockaddr_un);
  638. retry_accept:
  639. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  640. if (new_fd == -1 && errno == EINTR) {
  641. goto retry_accept;
  642. }
  643. if (new_fd == -1) {
  644. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  645. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  646. }
  647. totemip_nosigpipe(new_fd);
  648. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  649. if (res == -1) {
  650. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  651. close (new_fd);
  652. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  653. }
  654. /*
  655. * Valid accept
  656. */
  657. /*
  658. * Request credentials of sender provided by kernel
  659. */
  660. #ifdef OPENAIS_LINUX
  661. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  662. #endif
  663. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  664. res = conn_info_create (new_fd);
  665. if (res != 0) {
  666. close (new_fd);
  667. }
  668. return (0);
  669. }
  670. /*
  671. * Exported functions
  672. */
  673. int message_source_is_local(struct message_source *source)
  674. {
  675. int ret = 0;
  676. assert (source != NULL);
  677. if ((totemip_localhost_check(&source->addr)
  678. ||(totemip_equal(&source->addr, my_ip)))) {
  679. ret = 1;
  680. }
  681. return ret;
  682. }
  683. void message_source_set (
  684. struct message_source *source,
  685. void *conn)
  686. {
  687. assert ((source != NULL) && (conn != NULL));
  688. totemip_copy(&source->addr, this_ip);
  689. source->conn = conn;
  690. }
  691. void openais_ipc_init (
  692. poll_handle ais_poll_handle,
  693. unsigned int gid_valid,
  694. struct totem_ip_address *my_ip_in)
  695. {
  696. int libais_server_fd;
  697. struct sockaddr_un un_addr;
  698. int res;
  699. /*
  700. * Create socket for libais clients, name socket, listen for connections
  701. */
  702. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  703. if (libais_server_fd == -1) {
  704. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  705. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  706. };
  707. totemip_nosigpipe(libais_server_fd);
  708. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  709. if (res == -1) {
  710. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  711. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  712. }
  713. #if !defined(OPENAIS_LINUX)
  714. unlink(socketname);
  715. #endif
  716. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  717. un_addr.sun_family = AF_UNIX;
  718. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  719. un_addr.sun_len = sizeof(struct sockaddr_un);
  720. #endif
  721. #if defined(OPENAIS_LINUX)
  722. strcpy (un_addr.sun_path + 1, socketname);
  723. #else
  724. strcpy (un_addr.sun_path, socketname);
  725. #endif
  726. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  727. if (res) {
  728. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  729. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  730. }
  731. listen (libais_server_fd, SERVER_BACKLOG);
  732. /*
  733. * Setup libais connection dispatch routine
  734. */
  735. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  736. POLLIN, 0, poll_handler_libais_accept, 0);
  737. g_gid_valid = gid_valid;
  738. my_ip = my_ip_in;
  739. signal (SIGUSR1, sigusr1_handler);
  740. }
  741. /*
  742. * Get the conn info private data
  743. */
  744. void *openais_conn_private_data_get (void *conn)
  745. {
  746. struct conn_info *conn_info = (struct conn_info *)conn;
  747. if (conn != NULL) {
  748. return ((void *)conn_info->private_data);
  749. } else {
  750. return NULL;
  751. }
  752. }
  753. /*
  754. * Get the conn info partner connection
  755. */
  756. void *openais_conn_partner_get (void *conn)
  757. {
  758. struct conn_info *conn_info = (struct conn_info *)conn;
  759. if (conn != NULL) {
  760. return ((void *)conn_info->conn_info_partner);
  761. } else {
  762. return NULL;
  763. }
  764. }
  765. int openais_conn_send_response (
  766. void *conn,
  767. void *msg,
  768. int mlen)
  769. {
  770. struct queue *outq;
  771. char *cmsg;
  772. int res = 0;
  773. int queue_empty;
  774. struct outq_item *queue_item;
  775. struct outq_item queue_item_out;
  776. struct msghdr msg_send;
  777. struct iovec iov_send;
  778. char *msg_addr;
  779. struct conn_info *conn_info = (struct conn_info *)conn;
  780. if (conn_info == NULL) {
  781. return -1;
  782. }
  783. if (!libais_connection_active (conn_info)) {
  784. return (-1);
  785. }
  786. pthread_mutex_lock (&conn_info->mutex);
  787. outq = &conn_info->outq;
  788. msg_send.msg_iov = &iov_send;
  789. msg_send.msg_name = 0;
  790. msg_send.msg_namelen = 0;
  791. msg_send.msg_iovlen = 1;
  792. msg_send.msg_control = 0;
  793. msg_send.msg_controllen = 0;
  794. msg_send.msg_flags = 0;
  795. if (queue_is_full (outq)) {
  796. /*
  797. * Start a disconnect if we have not already started one
  798. * and report that the outgoing queue is full
  799. */
  800. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  801. libais_disconnect_delayed (conn_info);
  802. pthread_mutex_unlock (&conn_info->mutex);
  803. return (-1);
  804. }
  805. while (!queue_is_empty (outq)) {
  806. queue_item = queue_item_get (outq);
  807. msg_addr = (char *)queue_item->msg;
  808. msg_addr = &msg_addr[conn_info->byte_start];
  809. iov_send.iov_base = msg_addr;
  810. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  811. retry_sendmsg:
  812. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  813. if (res == -1 && errno == EINTR) {
  814. goto retry_sendmsg;
  815. }
  816. if (res == -1 && errno == EAGAIN) {
  817. break; /* outgoing kernel queue full */
  818. }
  819. if (res == -1 && errno == EPIPE) {
  820. libais_disconnect_delayed (conn_info);
  821. return (0);
  822. }
  823. if (res == -1) {
  824. assert (0);
  825. break; /* some other error, stop trying to send message */
  826. }
  827. if (res + conn_info->byte_start != queue_item->mlen) {
  828. conn_info->byte_start += res;
  829. break;
  830. }
  831. /*
  832. * Message sent, try sending another message
  833. */
  834. queue_item_remove (outq);
  835. conn_info->byte_start = 0;
  836. free (queue_item->msg);
  837. } /* while queue not empty */
  838. res = -1;
  839. queue_empty = queue_is_empty (outq);
  840. /*
  841. * Send requested message
  842. */
  843. if (queue_empty) {
  844. iov_send.iov_base = msg;
  845. iov_send.iov_len = mlen;
  846. retry_sendmsg_two:
  847. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  848. if (res == -1 && errno == EINTR) {
  849. goto retry_sendmsg_two;
  850. }
  851. if (res == -1 && errno == EAGAIN) {
  852. conn_info->byte_start = 0;
  853. conn_info->events = POLLIN|POLLNVAL;
  854. }
  855. if (res != -1) {
  856. if (res + conn_info->byte_start != mlen) {
  857. conn_info->byte_start += res;
  858. res = -1;
  859. } else {
  860. conn_info->byte_start = 0;
  861. conn_info->events = POLLIN|POLLNVAL;
  862. }
  863. }
  864. }
  865. /*
  866. * If res == -1 , errrno == EAGAIN which means kernel queue full
  867. */
  868. if (res == -1) {
  869. cmsg = malloc (mlen);
  870. if (cmsg == 0) {
  871. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  872. libais_disconnect_delayed (conn_info);
  873. return (-1);
  874. }
  875. queue_item_out.msg = cmsg;
  876. queue_item_out.mlen = mlen;
  877. memcpy (cmsg, msg, mlen);
  878. queue_item_add (outq, &queue_item_out);
  879. /*
  880. * Send a pthread_kill to interrupt the poll syscall
  881. * and start a new poll operation in the thread
  882. */
  883. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  884. pthread_kill (conn_info->thread, SIGUSR1);
  885. }
  886. pthread_mutex_unlock (&conn_info->mutex);
  887. return (0);
  888. }
  889. pthread_mutex_t *openais_ipc_mutex_get (void)
  890. {
  891. return (&serialize_input);
  892. }