ipc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <pthread.h>
  37. #include <assert.h>
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #include <sys/types.h>
  41. #include <sys/poll.h>
  42. #include <sys/uio.h>
  43. #include <sys/mman.h>
  44. #include <sys/socket.h>
  45. #include <sys/un.h>
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <signal.h>
  56. #include <sched.h>
  57. #include <time.h>
  58. #include "../include/saAis.h"
  59. #include "../include/list.h"
  60. #include "../include/queue.h"
  61. #include "../lcr/lcr_ifact.h"
  62. #include "poll.h"
  63. #include "totempg.h"
  64. #include "totemsrp.h"
  65. #include "mempool.h"
  66. #include "mainconfig.h"
  67. #include "totemconfig.h"
  68. #include "main.h"
  69. #include "ipc.h"
  70. #include "service.h"
  71. #include "sync.h"
  72. #include "swab.h"
  73. #include "objdb.h"
  74. #include "config.h"
  75. #include "tlist.h"
  76. #define LOG_SERVICE LOG_SERVICE_IPC
  77. #include "print.h"
  78. #include "util.h"
  79. #ifdef OPENAIS_SOLARIS
  80. #define MSG_NOSIGNAL 0
  81. #endif
  82. #define SERVER_BACKLOG 5
  83. static unsigned int g_gid_valid = 0;
  84. static struct totem_ip_address *my_ip;
  85. static void (*ipc_serialize_lock_fn) (void);
  86. static void (*ipc_serialize_unlock_fn) (void);
  87. struct outq_item {
  88. void *msg;
  89. size_t mlen;
  90. };
  91. enum conn_state {
  92. CONN_STATE_ACTIVE,
  93. CONN_STATE_SECURITY,
  94. CONN_STATE_REQUESTED,
  95. CONN_STATE_CLOSED,
  96. CONN_STATE_DISCONNECTED
  97. };
  98. struct conn_info {
  99. int fd; /* File descriptor */
  100. unsigned int events; /* events polled for by file descriptor */
  101. enum conn_state state; /* State of this connection */
  102. pthread_t thread; /* thread identifier */
  103. pthread_attr_t thread_attr; /* thread attribute */
  104. char *inb; /* Input buffer for non-blocking reads */
  105. int inb_nextheader; /* Next message header starts here */
  106. int inb_start; /* Start location of input buffer */
  107. int inb_inuse; /* Bytes currently stored in input buffer */
  108. struct queue outq; /* Circular queue for outgoing requests */
  109. int byte_start; /* Byte to start sending from in head of queue */
  110. enum service_types service;/* Type of service so dispatch knows how to route message */
  111. int authenticated; /* Is this connection authenticated? */
  112. void *private_data; /* library connection private data */
  113. struct conn_info *conn_info_partner; /* partner connection dispatch<->response */
  114. int (*lib_exit_fn) (void *conn);
  115. struct timerlist timerlist;
  116. pthread_mutex_t mutex;
  117. pthread_mutex_t *shared_mutex;
  118. };
  119. static void *prioritized_poll_thread (void *conn);
  120. static int conn_info_outq_flush (struct conn_info *conn_info);
  121. static void libais_deliver (struct conn_info *conn_info);
  122. /*
  123. * IPC Initializers
  124. */
  125. static int response_init_send_response (
  126. struct conn_info *conn_info,
  127. void *message);
  128. static int dispatch_init_send_response (
  129. struct conn_info *conn_info,
  130. void *message);
  131. static int (*ais_init_service[]) (struct conn_info *conn_info, void *message) = {
  132. response_init_send_response,
  133. dispatch_init_send_response
  134. };
  135. static void libais_disconnect_security (struct conn_info *conn_info)
  136. {
  137. conn_info->state = CONN_STATE_SECURITY;
  138. close (conn_info->fd);
  139. }
  140. static int response_init_send_response (
  141. struct conn_info *conn_info,
  142. void *message)
  143. {
  144. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  145. size_t cinfo = (size_t)conn_info;
  146. mar_req_lib_response_init_t *req_lib_response_init = (mar_req_lib_response_init_t *)message;
  147. mar_res_lib_response_init_t res_lib_response_init;
  148. if (conn_info->authenticated) {
  149. conn_info->service = req_lib_response_init->resdis_header.service;
  150. error = SA_AIS_OK;
  151. }
  152. res_lib_response_init.header.size = sizeof (mar_res_lib_response_init_t);
  153. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  154. res_lib_response_init.header.error = error;
  155. res_lib_response_init.conn_info = (mar_uint64_t)cinfo;
  156. openais_conn_send_response (
  157. conn_info,
  158. &res_lib_response_init,
  159. sizeof (res_lib_response_init));
  160. if (error == SA_AIS_ERR_ACCESS) {
  161. libais_disconnect_security (conn_info);
  162. return (-1);
  163. }
  164. return (0);
  165. }
  166. static int dispatch_init_send_response (
  167. struct conn_info *conn_info,
  168. void *message)
  169. {
  170. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  171. size_t cinfo;
  172. mar_req_lib_dispatch_init_t *req_lib_dispatch_init = (mar_req_lib_dispatch_init_t *)message;
  173. mar_res_lib_dispatch_init_t res_lib_dispatch_init;
  174. struct conn_info *msg_conn_info;
  175. if (conn_info->authenticated) {
  176. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  177. if (!ais_service[req_lib_dispatch_init->resdis_header.service])
  178. error = SA_AIS_ERR_NOT_SUPPORTED;
  179. else
  180. error = SA_AIS_OK;
  181. cinfo = (size_t)req_lib_dispatch_init->conn_info;
  182. conn_info->conn_info_partner = (struct conn_info *)cinfo;
  183. conn_info->conn_info_partner->shared_mutex = conn_info->shared_mutex;
  184. msg_conn_info = (struct conn_info *)cinfo;
  185. msg_conn_info->conn_info_partner = conn_info;
  186. if (error == SA_AIS_OK) {
  187. int private_data_size;
  188. private_data_size = ais_service[req_lib_dispatch_init->resdis_header.service]->private_data_size;
  189. if (private_data_size) {
  190. conn_info->private_data = malloc (private_data_size);
  191. conn_info->conn_info_partner->private_data = conn_info->private_data;
  192. if (conn_info->private_data == NULL) {
  193. error = SA_AIS_ERR_NO_MEMORY;
  194. } else {
  195. memset (conn_info->private_data, 0, private_data_size);
  196. }
  197. } else {
  198. conn_info->private_data = NULL;
  199. conn_info->conn_info_partner->private_data = NULL;
  200. }
  201. }
  202. }
  203. res_lib_dispatch_init.header.size = sizeof (mar_res_lib_dispatch_init_t);
  204. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  205. res_lib_dispatch_init.header.error = error;
  206. openais_conn_send_response (
  207. conn_info,
  208. &res_lib_dispatch_init,
  209. sizeof (res_lib_dispatch_init));
  210. if (error == SA_AIS_ERR_ACCESS) {
  211. libais_disconnect_security (conn_info);
  212. return (-1);
  213. }
  214. if (error != SA_AIS_OK) {
  215. return (-1);
  216. }
  217. conn_info->state = CONN_STATE_ACTIVE;
  218. conn_info->conn_info_partner->state = CONN_STATE_ACTIVE;
  219. conn_info->lib_exit_fn = ais_service[conn_info->service]->lib_exit_fn;
  220. ais_service[conn_info->service]->lib_init_fn (conn_info);
  221. return (0);
  222. }
  223. /*
  224. * Create a connection data structure
  225. */
  226. static inline unsigned int conn_info_create (int fd) {
  227. struct conn_info *conn_info;
  228. int res;
  229. conn_info = malloc (sizeof (struct conn_info));
  230. if (conn_info == 0) {
  231. return (ENOMEM);
  232. }
  233. memset (conn_info, 0, sizeof (struct conn_info));
  234. res = queue_init (&conn_info->outq, SIZEQUEUE,
  235. sizeof (struct outq_item));
  236. if (res != 0) {
  237. free (conn_info);
  238. return (ENOMEM);
  239. }
  240. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  241. if (conn_info->inb == NULL) {
  242. queue_free (&conn_info->outq);
  243. free (conn_info);
  244. return (ENOMEM);
  245. }
  246. conn_info->shared_mutex = malloc (sizeof (pthread_mutex_t));
  247. if (conn_info->shared_mutex == NULL) {
  248. free (conn_info->inb);
  249. queue_free (&conn_info->outq);
  250. free (conn_info);
  251. return (ENOMEM);
  252. }
  253. pthread_mutex_init (&conn_info->mutex, NULL);
  254. pthread_mutex_init (conn_info->shared_mutex, NULL);
  255. conn_info->state = CONN_STATE_ACTIVE;
  256. conn_info->fd = fd;
  257. conn_info->events = POLLIN|POLLNVAL;
  258. conn_info->service = SOCKET_SERVICE_INIT;
  259. pthread_attr_init (&conn_info->thread_attr);
  260. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  261. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_DETACHED);
  262. res = pthread_create (&conn_info->thread, &conn_info->thread_attr,
  263. prioritized_poll_thread, conn_info);
  264. return (res);
  265. }
  266. static void conn_info_destroy (struct conn_info *conn_info)
  267. {
  268. struct outq_item *outq_item;
  269. /*
  270. * Free the outq queued items
  271. */
  272. while (!queue_is_empty (&conn_info->outq)) {
  273. outq_item = queue_item_get (&conn_info->outq);
  274. free (outq_item->msg);
  275. queue_item_remove (&conn_info->outq);
  276. }
  277. queue_free (&conn_info->outq);
  278. free (conn_info->inb);
  279. if (conn_info->conn_info_partner) {
  280. conn_info->conn_info_partner->conn_info_partner = NULL;
  281. }
  282. free (conn_info);
  283. }
  284. static int libais_connection_active (struct conn_info *conn_info)
  285. {
  286. return (conn_info->state == CONN_STATE_ACTIVE);
  287. }
  288. static void libais_disconnect_request (struct conn_info *conn_info)
  289. {
  290. if (conn_info->state == CONN_STATE_ACTIVE) {
  291. conn_info->state = CONN_STATE_REQUESTED;
  292. conn_info->conn_info_partner->state = CONN_STATE_REQUESTED;
  293. }
  294. }
  295. static int libais_disconnect (struct conn_info *conn_info)
  296. {
  297. int res = 0;
  298. assert (conn_info->state != CONN_STATE_ACTIVE);
  299. if (conn_info->state == CONN_STATE_DISCONNECTED) {
  300. assert (0);
  301. }
  302. /*
  303. * Close active connections
  304. */
  305. if (conn_info->state == CONN_STATE_ACTIVE || conn_info->state == CONN_STATE_REQUESTED) {
  306. close (conn_info->fd);
  307. conn_info->state = CONN_STATE_CLOSED;
  308. close (conn_info->conn_info_partner->fd);
  309. conn_info->conn_info_partner->state = CONN_STATE_CLOSED;
  310. }
  311. /*
  312. * Note we will only call the close operation once on the first time
  313. * one of the connections is closed
  314. */
  315. if (conn_info->state == CONN_STATE_CLOSED) {
  316. if (conn_info->lib_exit_fn) {
  317. res = conn_info->lib_exit_fn (conn_info);
  318. }
  319. if (res == -1) {
  320. return (-1);
  321. }
  322. if (conn_info->conn_info_partner->lib_exit_fn) {
  323. res = conn_info->conn_info_partner->lib_exit_fn (conn_info);
  324. }
  325. if (res == -1) {
  326. return (-1);
  327. }
  328. }
  329. conn_info->state = CONN_STATE_DISCONNECTED;
  330. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTED;
  331. return (0);
  332. }
  333. static inline void conn_info_mutex_lock (
  334. struct conn_info *conn_info,
  335. unsigned int service)
  336. {
  337. if (service == SOCKET_SERVICE_INIT) {
  338. pthread_mutex_lock (&conn_info->mutex);
  339. } else {
  340. pthread_mutex_lock (conn_info->shared_mutex);
  341. }
  342. }
  343. static inline void conn_info_mutex_unlock (
  344. struct conn_info *conn_info,
  345. unsigned int service)
  346. {
  347. if (service == SOCKET_SERVICE_INIT) {
  348. pthread_mutex_unlock (&conn_info->mutex);
  349. } else {
  350. pthread_mutex_unlock (conn_info->shared_mutex);
  351. }
  352. }
  353. /*
  354. * This thread runs in a specific thread priority mode to handle
  355. * I/O requests from the library
  356. */
  357. static void *prioritized_poll_thread (void *conn)
  358. {
  359. struct conn_info *conn_info = (struct conn_info *)conn;
  360. struct pollfd ufd;
  361. int fds;
  362. int res;
  363. pthread_mutex_t *rel_mutex;
  364. unsigned int service;
  365. struct conn_info *cinfo_partner;
  366. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  367. struct sched_param sched_param;
  368. sched_param.sched_priority = 1;
  369. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  370. #endif
  371. ufd.fd = conn_info->fd;
  372. for (;;) {
  373. retry_poll:
  374. service = conn_info->service;
  375. ufd.events = conn_info->events;
  376. ufd.revents = 0;
  377. fds = poll (&ufd, 1, -1);
  378. conn_info_mutex_lock (conn_info, service);
  379. switch (conn_info->state) {
  380. case CONN_STATE_SECURITY:
  381. conn_info_mutex_unlock (conn_info, service);
  382. free (conn_info->shared_mutex);
  383. conn_info_destroy (conn);
  384. pthread_exit (0);
  385. break;
  386. case CONN_STATE_REQUESTED:
  387. case CONN_STATE_CLOSED:
  388. res = libais_disconnect (conn);
  389. if (res != 0) {
  390. conn_info_mutex_unlock (conn_info, service);
  391. goto retry_poll;
  392. }
  393. break;
  394. case CONN_STATE_DISCONNECTED:
  395. rel_mutex = conn_info->shared_mutex;
  396. cinfo_partner = conn_info->conn_info_partner;
  397. conn_info_destroy (conn);
  398. if (service == SOCKET_SERVICE_INIT) {
  399. pthread_mutex_unlock (&conn_info->mutex);
  400. } else {
  401. pthread_mutex_unlock (rel_mutex);
  402. }
  403. if (cinfo_partner == NULL) {
  404. free (rel_mutex);
  405. }
  406. pthread_exit (0);
  407. /*
  408. * !! NOTE !! this is the exit point for this thread
  409. */
  410. break;
  411. default:
  412. break;
  413. }
  414. if (fds == -1) {
  415. conn_info_mutex_unlock (conn_info, service);
  416. goto retry_poll;
  417. }
  418. ipc_serialize_lock_fn ();
  419. if (fds == 1 && ufd.revents) {
  420. if (ufd.revents & (POLLERR|POLLHUP)) {
  421. libais_disconnect_request (conn_info);
  422. conn_info_mutex_unlock (conn_info, service);
  423. ipc_serialize_unlock_fn ();
  424. continue;
  425. }
  426. if (ufd.revents & POLLOUT) {
  427. conn_info_outq_flush (conn_info);
  428. }
  429. if ((ufd.revents & POLLIN) == POLLIN) {
  430. libais_deliver (conn_info);
  431. }
  432. }
  433. ipc_serialize_unlock_fn ();
  434. conn_info_mutex_unlock (conn_info, service);
  435. }
  436. /*
  437. * This code never reached
  438. */
  439. return (0);
  440. }
  441. #if defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS)
  442. /* SUN_LEN is broken for abstract namespace
  443. */
  444. #define AIS_SUN_LEN(a) sizeof(*(a))
  445. #else
  446. #define AIS_SUN_LEN(a) SUN_LEN(a)
  447. #endif
  448. #if defined(OPENAIS_LINUX)
  449. char *socketname = "libais.socket";
  450. #else
  451. char *socketname = "/var/run/libais.socket";
  452. #endif
  453. static int conn_info_outq_flush (struct conn_info *conn_info) {
  454. struct queue *outq;
  455. int res = 0;
  456. struct outq_item *queue_item;
  457. struct msghdr msg_send;
  458. struct iovec iov_send;
  459. char *msg_addr;
  460. if (!libais_connection_active (conn_info)) {
  461. return (-1);
  462. }
  463. outq = &conn_info->outq;
  464. msg_send.msg_iov = &iov_send;
  465. msg_send.msg_name = 0;
  466. msg_send.msg_namelen = 0;
  467. msg_send.msg_iovlen = 1;
  468. #ifndef OPENAIS_SOLARIS
  469. msg_send.msg_control = 0;
  470. msg_send.msg_controllen = 0;
  471. msg_send.msg_flags = 0;
  472. #else
  473. msg_send.msg_accrights = NULL;
  474. msg_send.msg_accrightslen = 0;
  475. #endif
  476. while (!queue_is_empty (outq)) {
  477. queue_item = queue_item_get (outq);
  478. msg_addr = (char *)queue_item->msg;
  479. msg_addr = &msg_addr[conn_info->byte_start];
  480. iov_send.iov_base = msg_addr;
  481. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  482. retry_sendmsg:
  483. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  484. if (res == -1 && errno == EINTR) {
  485. goto retry_sendmsg;
  486. }
  487. if (res == -1 && errno == EAGAIN) {
  488. return (0);
  489. }
  490. if (res == -1 && errno == EPIPE) {
  491. libais_disconnect_request (conn_info);
  492. return (0);
  493. }
  494. if (res == -1) {
  495. printf ("ERRNO is %d\n", errno);
  496. assert (0); /* some other unhandled error here */
  497. }
  498. if (res + conn_info->byte_start != queue_item->mlen) {
  499. conn_info->byte_start += res;
  500. return (0);
  501. }
  502. /*
  503. * Message sent, try sending another message
  504. */
  505. queue_item_remove (outq);
  506. conn_info->byte_start = 0;
  507. free (queue_item->msg);
  508. } /* while queue not empty */
  509. if (queue_is_empty (outq)) {
  510. conn_info->events = POLLIN|POLLNVAL;
  511. }
  512. return (0);
  513. }
  514. struct res_overlay {
  515. mar_res_header_t header __attribute((aligned(8)));
  516. char buf[4096];
  517. };
  518. static void libais_deliver (struct conn_info *conn_info)
  519. {
  520. int res;
  521. mar_req_header_t *header;
  522. int service;
  523. struct msghdr msg_recv;
  524. struct iovec iov_recv;
  525. #ifdef OPENAIS_LINUX
  526. struct cmsghdr *cmsg;
  527. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  528. struct ucred *cred;
  529. int on = 0;
  530. #elif defined(OPENAIS_SOLARIS)
  531. int fd;
  532. #else
  533. uid_t euid;
  534. gid_t egid;
  535. #endif
  536. int send_ok = 0;
  537. int send_ok_joined = 0;
  538. struct iovec send_ok_joined_iovec;
  539. struct res_overlay res_overlay;
  540. msg_recv.msg_iov = &iov_recv;
  541. msg_recv.msg_iovlen = 1;
  542. msg_recv.msg_name = 0;
  543. msg_recv.msg_namelen = 0;
  544. #ifndef OPENAIS_SOLARIS
  545. msg_recv.msg_flags = 0;
  546. #endif
  547. if (conn_info->authenticated) {
  548. #ifndef OPENAIS_SOLARIS
  549. msg_recv.msg_control = 0;
  550. msg_recv.msg_controllen = 0;
  551. #else
  552. msg_recv.msg_accrights = NULL;
  553. msg_recv.msg_accrightslen = 0;
  554. #endif
  555. } else {
  556. #ifdef OPENAIS_LINUX
  557. msg_recv.msg_control = (void *)cmsg_cred;
  558. msg_recv.msg_controllen = sizeof (cmsg_cred);
  559. #elif defined(OPENAIS_SOLARIS)
  560. msg_recv.msg_accrights = (char *)&fd;
  561. msg_recv.msg_accrightslen = sizeof (fd);
  562. #else
  563. euid = -1; egid = -1;
  564. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  565. (euid == 0 || egid == g_gid_valid)) {
  566. conn_info->authenticated = 1;
  567. }
  568. if (conn_info->authenticated == 0) {
  569. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  570. }
  571. #endif
  572. }
  573. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  574. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  575. assert (iov_recv.iov_len != 0);
  576. retry_recv:
  577. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  578. if (res == -1 && errno == EINTR) {
  579. goto retry_recv;
  580. } else
  581. if (res == -1 && errno != EAGAIN) {
  582. return;
  583. } else
  584. if (res == 0) {
  585. #if defined(OPENAIS_SOLARIS) || defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  586. /* On many OS poll never return POLLHUP or POLLERR.
  587. * EOF is detected when recvmsg return 0.
  588. */
  589. libais_disconnect_request (conn_info);
  590. #endif
  591. return;
  592. }
  593. /*
  594. * Authenticate if this connection has not been authenticated
  595. */
  596. #ifdef OPENAIS_LINUX
  597. if (conn_info->authenticated == 0) {
  598. cmsg = CMSG_FIRSTHDR (&msg_recv);
  599. assert (cmsg);
  600. cred = (struct ucred *)CMSG_DATA (cmsg);
  601. if (cred) {
  602. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  603. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  604. conn_info->authenticated = 1;
  605. }
  606. }
  607. if (conn_info->authenticated == 0) {
  608. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  609. }
  610. }
  611. #elif defined(OPENAIS_SOLARIS)
  612. /* TODO Fix this. There is no authentication on Solaris yet. */
  613. conn_info->authenticated = 1;
  614. #endif
  615. /*
  616. * Dispatch all messages received in recvmsg that can be dispatched
  617. * sizeof (mar_req_header_t) needed at minimum to do any processing
  618. */
  619. conn_info->inb_inuse += res;
  620. conn_info->inb_start += res;
  621. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && res != -1) {
  622. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  623. if (header->size > conn_info->inb_inuse) {
  624. break;
  625. }
  626. service = conn_info->service;
  627. /*
  628. * If this service is in init phase, initialize service
  629. * else handle message using service service
  630. */
  631. if (service == SOCKET_SERVICE_INIT) {
  632. res = ais_init_service[header->id] (conn_info, header);
  633. } else {
  634. /*
  635. * Not an init service, but a standard service
  636. */
  637. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  638. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  639. header->id, ais_service[service]->lib_service_count);
  640. return ;
  641. }
  642. /*
  643. * If flow control is required of the library handle, determine that
  644. * openais is not in synchronization and that totempg has room available
  645. * to queue a message, otherwise tell the library we are busy and to
  646. * try again later
  647. */
  648. send_ok_joined_iovec.iov_base = (char *)header;
  649. send_ok_joined_iovec.iov_len = header->size;
  650. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  651. &send_ok_joined_iovec, 1);
  652. send_ok =
  653. (sync_primary_designated() == 1) && (
  654. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  655. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  656. (send_ok_joined) &&
  657. (sync_in_process() == 0)));
  658. if (send_ok) {
  659. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  660. } else {
  661. /*
  662. * Overload, tell library to retry
  663. */
  664. res_overlay.header.size =
  665. ais_service[service]->lib_service[header->id].response_size;
  666. res_overlay.header.id =
  667. ais_service[service]->lib_service[header->id].response_id;
  668. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  669. openais_conn_send_response (
  670. conn_info,
  671. &res_overlay,
  672. res_overlay.header.size);
  673. }
  674. }
  675. conn_info->inb_inuse -= header->size;
  676. } /* while */
  677. if (conn_info->inb_inuse == 0) {
  678. conn_info->inb_start = 0;
  679. } else
  680. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  681. if (conn_info->inb_start >= SIZEINB) {
  682. /*
  683. * If in buffer is full, move it back to start
  684. */
  685. memmove (conn_info->inb,
  686. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  687. sizeof (char) * conn_info->inb_inuse);
  688. conn_info->inb_start = conn_info->inb_inuse;
  689. }
  690. return;
  691. }
  692. static int poll_handler_libais_accept (
  693. poll_handle handle,
  694. int fd,
  695. int revent,
  696. void *data)
  697. {
  698. socklen_t addrlen;
  699. struct sockaddr_un un_addr;
  700. int new_fd;
  701. #ifdef OPENAIS_LINUX
  702. int on = 1;
  703. #endif
  704. int res;
  705. addrlen = sizeof (struct sockaddr_un);
  706. retry_accept:
  707. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  708. if (new_fd == -1 && errno == EINTR) {
  709. goto retry_accept;
  710. }
  711. if (new_fd == -1) {
  712. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  713. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  714. }
  715. totemip_nosigpipe(new_fd);
  716. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  717. if (res == -1) {
  718. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  719. close (new_fd);
  720. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  721. }
  722. /*
  723. * Valid accept
  724. */
  725. /*
  726. * Request credentials of sender provided by kernel
  727. */
  728. #ifdef OPENAIS_LINUX
  729. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  730. #endif
  731. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  732. res = conn_info_create (new_fd);
  733. if (res != 0) {
  734. close (new_fd);
  735. }
  736. return (0);
  737. }
  738. /*
  739. * Exported functions
  740. */
  741. int message_source_is_local(mar_message_source_t *source)
  742. {
  743. int ret = 0;
  744. assert (source != NULL);
  745. if (source->nodeid == my_ip->nodeid) {
  746. ret = 1;
  747. }
  748. return ret;
  749. }
  750. void message_source_set (
  751. mar_message_source_t *source,
  752. void *conn)
  753. {
  754. assert ((source != NULL) && (conn != NULL));
  755. source->nodeid = my_ip->nodeid;
  756. source->conn = conn;
  757. }
  758. void openais_ipc_init (
  759. void (*serialize_lock_fn) (void),
  760. void (*serialize_unlock_fn) (void),
  761. unsigned int gid_valid,
  762. struct totem_ip_address *my_ip_in)
  763. {
  764. int libais_server_fd;
  765. struct sockaddr_un un_addr;
  766. int res;
  767. log_init ("IPC");
  768. ipc_serialize_lock_fn = serialize_lock_fn;
  769. ipc_serialize_unlock_fn = serialize_unlock_fn;
  770. /*
  771. * Create socket for libais clients, name socket, listen for connections
  772. */
  773. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  774. if (libais_server_fd == -1) {
  775. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  776. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  777. };
  778. totemip_nosigpipe(libais_server_fd);
  779. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  780. if (res == -1) {
  781. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  782. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  783. }
  784. #if !defined(OPENAIS_LINUX)
  785. unlink(socketname);
  786. #endif
  787. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  788. un_addr.sun_family = AF_UNIX;
  789. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  790. un_addr.sun_len = sizeof(struct sockaddr_un);
  791. #endif
  792. #if defined(OPENAIS_LINUX)
  793. strcpy (un_addr.sun_path + 1, socketname);
  794. #else
  795. strcpy (un_addr.sun_path, socketname);
  796. #endif
  797. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  798. if (res) {
  799. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  800. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  801. }
  802. listen (libais_server_fd, SERVER_BACKLOG);
  803. /*
  804. * Setup libais connection dispatch routine
  805. */
  806. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  807. POLLIN, 0, poll_handler_libais_accept);
  808. g_gid_valid = gid_valid;
  809. my_ip = my_ip_in;
  810. }
  811. /*
  812. * Get the conn info private data
  813. */
  814. void *openais_conn_private_data_get (void *conn)
  815. {
  816. struct conn_info *conn_info = (struct conn_info *)conn;
  817. if (conn != NULL) {
  818. return ((void *)conn_info->private_data);
  819. } else {
  820. return NULL;
  821. }
  822. }
  823. /*
  824. * Get the conn info partner connection
  825. */
  826. void *openais_conn_partner_get (void *conn)
  827. {
  828. struct conn_info *conn_info = (struct conn_info *)conn;
  829. if (conn != NULL) {
  830. return ((void *)conn_info->conn_info_partner);
  831. } else {
  832. return NULL;
  833. }
  834. }
  835. int openais_conn_send_response (
  836. void *conn,
  837. void *msg,
  838. int mlen)
  839. {
  840. struct queue *outq;
  841. char *cmsg;
  842. int res = 0;
  843. int queue_empty;
  844. struct outq_item *queue_item;
  845. struct outq_item queue_item_out;
  846. struct msghdr msg_send;
  847. struct iovec iov_send;
  848. char *msg_addr;
  849. struct conn_info *conn_info = (struct conn_info *)conn;
  850. if (conn_info == NULL) {
  851. return -1;
  852. }
  853. if (!libais_connection_active (conn_info)) {
  854. return (-1);
  855. }
  856. outq = &conn_info->outq;
  857. msg_send.msg_iov = &iov_send;
  858. msg_send.msg_name = 0;
  859. msg_send.msg_namelen = 0;
  860. msg_send.msg_iovlen = 1;
  861. #ifndef OPENAIS_SOLARIS
  862. msg_send.msg_control = 0;
  863. msg_send.msg_controllen = 0;
  864. msg_send.msg_flags = 0;
  865. #else
  866. msg_send.msg_accrights = NULL;
  867. msg_send.msg_accrightslen = 0;
  868. #endif
  869. if (queue_is_full (outq)) {
  870. /*
  871. * Start a disconnect if we have not already started one
  872. * and report that the outgoing queue is full
  873. */
  874. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  875. libais_disconnect_request (conn_info);
  876. return (-1);
  877. }
  878. while (!queue_is_empty (outq)) {
  879. queue_item = queue_item_get (outq);
  880. msg_addr = (char *)queue_item->msg;
  881. msg_addr = &msg_addr[conn_info->byte_start];
  882. iov_send.iov_base = msg_addr;
  883. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  884. retry_sendmsg:
  885. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  886. if (res == -1 && errno == EINTR) {
  887. goto retry_sendmsg;
  888. }
  889. if (res == -1 && errno == EAGAIN) {
  890. break; /* outgoing kernel queue full */
  891. }
  892. if (res == -1 && errno == EPIPE) {
  893. libais_disconnect_request (conn_info);
  894. return (0);
  895. }
  896. if (res == -1) {
  897. assert (0);
  898. break; /* some other error, stop trying to send message */
  899. }
  900. if (res + conn_info->byte_start != queue_item->mlen) {
  901. conn_info->byte_start += res;
  902. break;
  903. }
  904. /*
  905. * Message sent, try sending another message
  906. */
  907. queue_item_remove (outq);
  908. conn_info->byte_start = 0;
  909. free (queue_item->msg);
  910. } /* while queue not empty */
  911. res = -1;
  912. queue_empty = queue_is_empty (outq);
  913. /*
  914. * Send request message
  915. */
  916. if (queue_empty) {
  917. iov_send.iov_base = msg;
  918. iov_send.iov_len = mlen;
  919. retry_sendmsg_two:
  920. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  921. if (res == -1 && errno == EINTR) {
  922. goto retry_sendmsg_two;
  923. }
  924. if (res == -1 && errno == EAGAIN) {
  925. conn_info->byte_start = 0;
  926. conn_info->events = POLLIN|POLLNVAL;
  927. }
  928. if (res != -1) {
  929. if (res + conn_info->byte_start != mlen) {
  930. conn_info->byte_start += res;
  931. res = -1;
  932. } else {
  933. conn_info->byte_start = 0;
  934. conn_info->events = POLLIN|POLLNVAL;
  935. }
  936. }
  937. }
  938. /*
  939. * If res == -1 , errrno == EAGAIN which means kernel queue full
  940. */
  941. if (res == -1) {
  942. cmsg = malloc (mlen);
  943. if (cmsg == 0) {
  944. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  945. libais_disconnect_request (conn_info);
  946. return (-1);
  947. }
  948. queue_item_out.msg = cmsg;
  949. queue_item_out.mlen = mlen;
  950. memcpy (cmsg, msg, mlen);
  951. queue_item_add (outq, &queue_item_out);
  952. /*
  953. * Send a pthread_kill to interrupt the poll syscall
  954. * and start a new poll operation in the thread
  955. */
  956. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  957. pthread_kill (conn_info->thread, SIGUSR1);
  958. }
  959. return (0);
  960. }
  961. int openais_ipc_timer_add (
  962. void *conn,
  963. void (*timer_fn) (void *data),
  964. void *data,
  965. unsigned int msec_in_future,
  966. timer_handle *handle)
  967. {
  968. struct conn_info *conn_info = (struct conn_info *)conn;
  969. int res;
  970. res = timerlist_add_future (
  971. &conn_info->timerlist,
  972. timer_fn,
  973. data,
  974. msec_in_future,
  975. handle);
  976. return (res);
  977. }
  978. void openais_ipc_timer_del (
  979. void *conn,
  980. timer_handle timer_handle)
  981. {
  982. struct conn_info *conn_info = (struct conn_info *)conn;
  983. timerlist_del (&conn_info->timerlist, timer_handle);
  984. }
  985. void openais_ipc_timer_del_data (
  986. void *conn,
  987. timer_handle timer_handle)
  988. {
  989. struct conn_info *conn_info = (struct conn_info *)conn;
  990. timerlist_del (&conn_info->timerlist, timer_handle);
  991. }