ipc.c 28 KB

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