ipc.c 29 KB

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