ipc.c 27 KB

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