ipc.c 26 KB

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