ipc.c 27 KB

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