ipc.c 26 KB

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