ipc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. #ifdef OPENAIS_SOLARIS
  586. /* res == 0 means hang up on Solaris */
  587. libais_disconnect_request (conn_info);
  588. #endif
  589. return;
  590. }
  591. /*
  592. * Authenticate if this connection has not been authenticated
  593. */
  594. #ifdef OPENAIS_LINUX
  595. if (conn_info->authenticated == 0) {
  596. cmsg = CMSG_FIRSTHDR (&msg_recv);
  597. assert (cmsg);
  598. cred = (struct ucred *)CMSG_DATA (cmsg);
  599. if (cred) {
  600. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  601. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  602. conn_info->authenticated = 1;
  603. }
  604. }
  605. if (conn_info->authenticated == 0) {
  606. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  607. }
  608. }
  609. #elif defined(OPENAIS_SOLARIS)
  610. /* TODO Fix this. There is no authentication on Solaris yet. */
  611. conn_info->authenticated = 1;
  612. #endif
  613. /*
  614. * Dispatch all messages received in recvmsg that can be dispatched
  615. * sizeof (mar_req_header_t) needed at minimum to do any processing
  616. */
  617. conn_info->inb_inuse += res;
  618. conn_info->inb_start += res;
  619. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && res != -1) {
  620. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  621. if (header->size > conn_info->inb_inuse) {
  622. break;
  623. }
  624. service = conn_info->service;
  625. /*
  626. * If this service is in init phase, initialize service
  627. * else handle message using service service
  628. */
  629. if (service == SOCKET_SERVICE_INIT) {
  630. res = ais_init_service[header->id] (conn_info, header);
  631. } else {
  632. /*
  633. * Not an init service, but a standard service
  634. */
  635. if (header->id < 0 || header->id > ais_service[service]->lib_service_count) {
  636. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  637. header->id, ais_service[service]->lib_service_count);
  638. return ;
  639. }
  640. /*
  641. * If flow control is required of the library handle, determine that
  642. * openais is not in synchronization and that totempg has room available
  643. * to queue a message, otherwise tell the library we are busy and to
  644. * try again later
  645. */
  646. send_ok_joined_iovec.iov_base = (char *)header;
  647. send_ok_joined_iovec.iov_len = header->size;
  648. send_ok_joined = totempg_groups_send_ok_joined (openais_group_handle,
  649. &send_ok_joined_iovec, 1);
  650. send_ok =
  651. (sync_primary_designated() == 1) && (
  652. (ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_NOT_REQUIRED) ||
  653. ((ais_service[service]->lib_service[header->id].flow_control == OPENAIS_FLOW_CONTROL_REQUIRED) &&
  654. (send_ok_joined) &&
  655. (sync_in_process() == 0)));
  656. if (send_ok) {
  657. ais_service[service]->lib_service[header->id].lib_handler_fn(conn_info, header);
  658. } else {
  659. /*
  660. * Overload, tell library to retry
  661. */
  662. res_overlay.header.size =
  663. ais_service[service]->lib_service[header->id].response_size;
  664. res_overlay.header.id =
  665. ais_service[service]->lib_service[header->id].response_id;
  666. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  667. openais_conn_send_response (
  668. conn_info,
  669. &res_overlay,
  670. res_overlay.header.size);
  671. }
  672. }
  673. conn_info->inb_inuse -= header->size;
  674. } /* while */
  675. if (conn_info->inb_inuse == 0) {
  676. conn_info->inb_start = 0;
  677. } else
  678. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  679. if (conn_info->inb_start >= SIZEINB) {
  680. /*
  681. * If in buffer is full, move it back to start
  682. */
  683. memmove (conn_info->inb,
  684. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  685. sizeof (char) * conn_info->inb_inuse);
  686. conn_info->inb_start = conn_info->inb_inuse;
  687. }
  688. return;
  689. }
  690. static int poll_handler_libais_accept (
  691. poll_handle handle,
  692. int fd,
  693. int revent,
  694. void *data)
  695. {
  696. socklen_t addrlen;
  697. struct sockaddr_un un_addr;
  698. int new_fd;
  699. #ifdef OPENAIS_LINUX
  700. int on = 1;
  701. #endif
  702. int res;
  703. addrlen = sizeof (struct sockaddr_un);
  704. retry_accept:
  705. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  706. if (new_fd == -1 && errno == EINTR) {
  707. goto retry_accept;
  708. }
  709. if (new_fd == -1) {
  710. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  711. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  712. }
  713. totemip_nosigpipe(new_fd);
  714. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  715. if (res == -1) {
  716. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  717. close (new_fd);
  718. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  719. }
  720. /*
  721. * Valid accept
  722. */
  723. /*
  724. * Request credentials of sender provided by kernel
  725. */
  726. #ifdef OPENAIS_LINUX
  727. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  728. #endif
  729. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  730. res = conn_info_create (new_fd);
  731. if (res != 0) {
  732. close (new_fd);
  733. }
  734. return (0);
  735. }
  736. /*
  737. * Exported functions
  738. */
  739. int message_source_is_local(mar_message_source_t *source)
  740. {
  741. int ret = 0;
  742. assert (source != NULL);
  743. if (source->nodeid == my_ip->nodeid) {
  744. ret = 1;
  745. }
  746. return ret;
  747. }
  748. void message_source_set (
  749. mar_message_source_t *source,
  750. void *conn)
  751. {
  752. assert ((source != NULL) && (conn != NULL));
  753. source->nodeid = my_ip->nodeid;
  754. source->conn = conn;
  755. }
  756. void openais_ipc_init (
  757. void (*serialize_lock_fn) (void),
  758. void (*serialize_unlock_fn) (void),
  759. unsigned int gid_valid,
  760. struct totem_ip_address *my_ip_in)
  761. {
  762. int libais_server_fd;
  763. struct sockaddr_un un_addr;
  764. int res;
  765. log_init ("IPC");
  766. ipc_serialize_lock_fn = serialize_lock_fn;
  767. ipc_serialize_unlock_fn = serialize_unlock_fn;
  768. /*
  769. * Create socket for libais clients, name socket, listen for connections
  770. */
  771. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  772. if (libais_server_fd == -1) {
  773. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  774. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  775. };
  776. totemip_nosigpipe(libais_server_fd);
  777. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  778. if (res == -1) {
  779. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  780. openais_exit_error (AIS_DONE_LIBAIS_SOCKET);
  781. }
  782. #if !defined(OPENAIS_LINUX)
  783. unlink(socketname);
  784. #endif
  785. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  786. un_addr.sun_family = AF_UNIX;
  787. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  788. un_addr.sun_len = sizeof(struct sockaddr_un);
  789. #endif
  790. #if defined(OPENAIS_LINUX)
  791. strcpy (un_addr.sun_path + 1, socketname);
  792. #else
  793. strcpy (un_addr.sun_path, socketname);
  794. #endif
  795. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  796. if (res) {
  797. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  798. openais_exit_error (AIS_DONE_LIBAIS_BIND);
  799. }
  800. listen (libais_server_fd, SERVER_BACKLOG);
  801. /*
  802. * Setup libais connection dispatch routine
  803. */
  804. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  805. POLLIN, 0, poll_handler_libais_accept);
  806. g_gid_valid = gid_valid;
  807. my_ip = my_ip_in;
  808. }
  809. /*
  810. * Get the conn info private data
  811. */
  812. void *openais_conn_private_data_get (void *conn)
  813. {
  814. struct conn_info *conn_info = (struct conn_info *)conn;
  815. if (conn != NULL) {
  816. return ((void *)conn_info->private_data);
  817. } else {
  818. return NULL;
  819. }
  820. }
  821. /*
  822. * Get the conn info partner connection
  823. */
  824. void *openais_conn_partner_get (void *conn)
  825. {
  826. struct conn_info *conn_info = (struct conn_info *)conn;
  827. if (conn != NULL) {
  828. return ((void *)conn_info->conn_info_partner);
  829. } else {
  830. return NULL;
  831. }
  832. }
  833. int openais_conn_send_response (
  834. void *conn,
  835. void *msg,
  836. int mlen)
  837. {
  838. struct queue *outq;
  839. char *cmsg;
  840. int res = 0;
  841. int queue_empty;
  842. struct outq_item *queue_item;
  843. struct outq_item queue_item_out;
  844. struct msghdr msg_send;
  845. struct iovec iov_send;
  846. char *msg_addr;
  847. struct conn_info *conn_info = (struct conn_info *)conn;
  848. if (conn_info == NULL) {
  849. return -1;
  850. }
  851. if (!libais_connection_active (conn_info)) {
  852. return (-1);
  853. }
  854. outq = &conn_info->outq;
  855. msg_send.msg_iov = &iov_send;
  856. msg_send.msg_name = 0;
  857. msg_send.msg_namelen = 0;
  858. msg_send.msg_iovlen = 1;
  859. #ifndef OPENAIS_SOLARIS
  860. msg_send.msg_control = 0;
  861. msg_send.msg_controllen = 0;
  862. msg_send.msg_flags = 0;
  863. #else
  864. msg_send.msg_accrights = NULL;
  865. msg_send.msg_accrightslen = 0;
  866. #endif
  867. if (queue_is_full (outq)) {
  868. /*
  869. * Start a disconnect if we have not already started one
  870. * and report that the outgoing queue is full
  871. */
  872. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  873. libais_disconnect_request (conn_info);
  874. return (-1);
  875. }
  876. while (!queue_is_empty (outq)) {
  877. queue_item = queue_item_get (outq);
  878. msg_addr = (char *)queue_item->msg;
  879. msg_addr = &msg_addr[conn_info->byte_start];
  880. iov_send.iov_base = msg_addr;
  881. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  882. retry_sendmsg:
  883. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  884. if (res == -1 && errno == EINTR) {
  885. goto retry_sendmsg;
  886. }
  887. if (res == -1 && errno == EAGAIN) {
  888. break; /* outgoing kernel queue full */
  889. }
  890. if (res == -1 && errno == EPIPE) {
  891. libais_disconnect_request (conn_info);
  892. return (0);
  893. }
  894. if (res == -1) {
  895. assert (0);
  896. break; /* some other error, stop trying to send message */
  897. }
  898. if (res + conn_info->byte_start != queue_item->mlen) {
  899. conn_info->byte_start += res;
  900. break;
  901. }
  902. /*
  903. * Message sent, try sending another message
  904. */
  905. queue_item_remove (outq);
  906. conn_info->byte_start = 0;
  907. free (queue_item->msg);
  908. } /* while queue not empty */
  909. res = -1;
  910. queue_empty = queue_is_empty (outq);
  911. /*
  912. * Send request message
  913. */
  914. if (queue_empty) {
  915. iov_send.iov_base = msg;
  916. iov_send.iov_len = mlen;
  917. retry_sendmsg_two:
  918. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  919. if (res == -1 && errno == EINTR) {
  920. goto retry_sendmsg_two;
  921. }
  922. if (res == -1 && errno == EAGAIN) {
  923. conn_info->byte_start = 0;
  924. conn_info->events = POLLIN|POLLNVAL;
  925. }
  926. if (res != -1) {
  927. if (res + conn_info->byte_start != mlen) {
  928. conn_info->byte_start += res;
  929. res = -1;
  930. } else {
  931. conn_info->byte_start = 0;
  932. conn_info->events = POLLIN|POLLNVAL;
  933. }
  934. }
  935. }
  936. /*
  937. * If res == -1 , errrno == EAGAIN which means kernel queue full
  938. */
  939. if (res == -1) {
  940. cmsg = malloc (mlen);
  941. if (cmsg == 0) {
  942. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  943. libais_disconnect_request (conn_info);
  944. return (-1);
  945. }
  946. queue_item_out.msg = cmsg;
  947. queue_item_out.mlen = mlen;
  948. memcpy (cmsg, msg, mlen);
  949. queue_item_add (outq, &queue_item_out);
  950. /*
  951. * Send a pthread_kill to interrupt the poll syscall
  952. * and start a new poll operation in the thread
  953. */
  954. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  955. pthread_kill (conn_info->thread, SIGUSR1);
  956. }
  957. return (0);
  958. }
  959. int openais_ipc_timer_add (
  960. void *conn,
  961. void (*timer_fn) (void *data),
  962. void *data,
  963. unsigned int msec_in_future,
  964. timer_handle *handle)
  965. {
  966. struct conn_info *conn_info = (struct conn_info *)conn;
  967. int res;
  968. res = timerlist_add_future (
  969. &conn_info->timerlist,
  970. timer_fn,
  971. data,
  972. msec_in_future,
  973. handle);
  974. return (res);
  975. }
  976. void openais_ipc_timer_del (
  977. void *conn,
  978. timer_handle timer_handle)
  979. {
  980. struct conn_info *conn_info = (struct conn_info *)conn;
  981. timerlist_del (&conn_info->timerlist, timer_handle);
  982. }
  983. void openais_ipc_timer_del_data (
  984. void *conn,
  985. timer_handle timer_handle)
  986. {
  987. struct conn_info *conn_info = (struct conn_info *)conn;
  988. timerlist_del (&conn_info->timerlist, timer_handle);
  989. }