ipc.c 28 KB

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