ipc.c 26 KB

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