coroipcc.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  5. * Copyright (c) 2006-2009 Red Hat, Inc.
  6. *
  7. * All rights reserved.
  8. *
  9. * Author: Steven Dake (sdake@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <unistd.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/types.h>
  46. #include <sys/uio.h>
  47. #include <sys/socket.h>
  48. #include <sys/select.h>
  49. #include <sys/time.h>
  50. #include <sys/un.h>
  51. #include <net/if.h>
  52. #include <arpa/inet.h>
  53. #include <netinet/in.h>
  54. #include <assert.h>
  55. #include <sys/shm.h>
  56. #include <sys/sem.h>
  57. #include <sys/mman.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/coroipc_types.h>
  60. #include <corosync/coroipc_ipc.h>
  61. #include <corosync/coroipcc.h>
  62. #include <corosync/hdb.h>
  63. #include "util.h"
  64. struct ipc_instance {
  65. int fd;
  66. int shmid;
  67. int semid;
  68. int flow_control_state;
  69. struct control_buffer *control_buffer;
  70. char *request_buffer;
  71. char *response_buffer;
  72. char *dispatch_buffer;
  73. size_t control_size;
  74. size_t request_size;
  75. size_t response_size;
  76. size_t dispatch_size;
  77. uid_t euid;
  78. pthread_mutex_t mutex;
  79. };
  80. void ipc_hdb_destructor (void *context);
  81. DECLARE_HDB_DATABASE(ipc_hdb,ipc_hdb_destructor);
  82. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  83. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  84. #else
  85. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  86. #endif
  87. #ifdef SO_NOSIGPIPE
  88. void socket_nosigpipe(int s)
  89. {
  90. int on = 1;
  91. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  92. }
  93. #endif
  94. #ifndef MSG_NOSIGNAL
  95. #define MSG_NOSIGNAL 0
  96. #endif
  97. static cs_error_t
  98. socket_send (
  99. int s,
  100. void *msg,
  101. size_t len)
  102. {
  103. cs_error_t res = CS_OK;
  104. int result;
  105. struct msghdr msg_send;
  106. struct iovec iov_send;
  107. char *rbuf = msg;
  108. int processed = 0;
  109. msg_send.msg_iov = &iov_send;
  110. msg_send.msg_iovlen = 1;
  111. msg_send.msg_name = 0;
  112. msg_send.msg_namelen = 0;
  113. #if !defined(COROSYNC_SOLARIS)
  114. msg_send.msg_control = 0;
  115. msg_send.msg_controllen = 0;
  116. msg_send.msg_flags = 0;
  117. #endif
  118. retry_send:
  119. iov_send.iov_base = &rbuf[processed];
  120. iov_send.iov_len = len - processed;
  121. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  122. if (result == -1) {
  123. switch (errno) {
  124. case EINTR:
  125. goto retry_send;
  126. break;
  127. case EAGAIN:
  128. goto retry_send;
  129. break;
  130. default:
  131. res = CS_ERR_LIBRARY;
  132. goto res_exit;
  133. }
  134. }
  135. processed += result;
  136. if (processed != len) {
  137. goto retry_send;
  138. }
  139. return (CS_OK);
  140. res_exit:
  141. return (res);
  142. }
  143. static cs_error_t
  144. socket_recv (
  145. int s,
  146. void *msg,
  147. size_t len)
  148. {
  149. cs_error_t res = CS_OK;
  150. int result;
  151. struct msghdr msg_recv;
  152. struct iovec iov_recv;
  153. char *rbuf = msg;
  154. int processed = 0;
  155. msg_recv.msg_iov = &iov_recv;
  156. msg_recv.msg_iovlen = 1;
  157. msg_recv.msg_name = 0;
  158. msg_recv.msg_namelen = 0;
  159. #if !defined (COROSYNC_SOLARIS)
  160. msg_recv.msg_control = 0;
  161. msg_recv.msg_controllen = 0;
  162. msg_recv.msg_flags = 0;
  163. #endif
  164. retry_recv:
  165. iov_recv.iov_base = (void *)&rbuf[processed];
  166. iov_recv.iov_len = len - processed;
  167. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL|MSG_WAITALL);
  168. if (result == -1) {
  169. switch (errno) {
  170. case EINTR:
  171. goto retry_recv;
  172. break;
  173. case EAGAIN:
  174. goto retry_recv;
  175. break;
  176. default:
  177. res = CS_ERR_LIBRARY;
  178. goto res_exit;
  179. }
  180. }
  181. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  182. /* On many OS poll never return POLLHUP or POLLERR.
  183. * EOF is detected when recvmsg return 0.
  184. */
  185. if (result == 0) {
  186. res = CS_ERR_LIBRARY;
  187. goto res_exit;
  188. }
  189. #endif
  190. processed += result;
  191. if (processed != len) {
  192. goto retry_recv;
  193. }
  194. assert (processed == len);
  195. res_exit:
  196. return (res);
  197. }
  198. static int
  199. priv_change_send (struct ipc_instance *ipc_instance)
  200. {
  201. char buf_req;
  202. mar_req_priv_change req_priv_change;
  203. unsigned int res;
  204. req_priv_change.euid = geteuid();
  205. /*
  206. * Don't resend request unless euid has changed
  207. */
  208. if (ipc_instance->euid == req_priv_change.euid) {
  209. return (0);
  210. }
  211. req_priv_change.egid = getegid();
  212. buf_req = MESSAGE_REQ_CHANGE_EUID;
  213. res = socket_send (ipc_instance->fd, &buf_req, 1);
  214. if (res == -1) {
  215. return (-1);
  216. }
  217. res = socket_send (ipc_instance->fd, &req_priv_change,
  218. sizeof (req_priv_change));
  219. if (res == -1) {
  220. return (-1);
  221. }
  222. ipc_instance->euid = req_priv_change.euid;
  223. return (0);
  224. }
  225. #if !defined(semun)
  226. union semun {
  227. int val;
  228. struct semid_ds *buf;
  229. unsigned short int *array;
  230. struct seminfo *__buf;
  231. };
  232. #endif
  233. static int
  234. circular_memory_map (char *path, const char *file, void **buf, size_t bytes)
  235. {
  236. int fd;
  237. void *addr_orig;
  238. void *addr;
  239. int res;
  240. sprintf (path, "/dev/shm/%s", file);
  241. fd = mkstemp (path);
  242. if (fd == -1) {
  243. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  244. fd = mkstemp (path);
  245. if (fd == -1) {
  246. return (-1);
  247. }
  248. }
  249. res = ftruncate (fd, bytes);
  250. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  251. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  252. if (addr_orig == MAP_FAILED) {
  253. return (-1);
  254. }
  255. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  256. MAP_FIXED | MAP_SHARED, fd, 0);
  257. if (addr != addr_orig) {
  258. return (-1);
  259. }
  260. addr = mmap (((char *)addr_orig) + bytes,
  261. bytes, PROT_READ | PROT_WRITE,
  262. MAP_FIXED | MAP_SHARED, fd, 0);
  263. res = close (fd);
  264. if (res) {
  265. return (-1);
  266. }
  267. *buf = addr_orig;
  268. return (0);
  269. }
  270. static void
  271. memory_unmap (void *addr, size_t bytes)
  272. {
  273. int res;
  274. res = munmap (addr, bytes);
  275. }
  276. void ipc_hdb_destructor (void *context ) {
  277. struct ipc_instance *ipc_instance = (struct ipc_instance *)context;
  278. /*
  279. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  280. */
  281. memory_unmap (ipc_instance->control_buffer, ipc_instance->control_size);
  282. memory_unmap (ipc_instance->request_buffer, ipc_instance->request_size);
  283. memory_unmap (ipc_instance->response_buffer, ipc_instance->response_size);
  284. memory_unmap (ipc_instance->dispatch_buffer, (ipc_instance->dispatch_size) << 1);
  285. }
  286. static int
  287. memory_map (char *path, const char *file, void **buf, size_t bytes)
  288. {
  289. int fd;
  290. void *addr_orig;
  291. void *addr;
  292. int res;
  293. sprintf (path, "/dev/shm/%s", file);
  294. fd = mkstemp (path);
  295. if (fd == -1) {
  296. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  297. fd = mkstemp (path);
  298. if (fd == -1) {
  299. return (-1);
  300. }
  301. }
  302. res = ftruncate (fd, bytes);
  303. addr_orig = mmap (NULL, bytes, PROT_NONE,
  304. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  305. if (addr_orig == MAP_FAILED) {
  306. return (-1);
  307. }
  308. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  309. MAP_FIXED | MAP_SHARED, fd, 0);
  310. if (addr != addr_orig) {
  311. return (-1);
  312. }
  313. res = close (fd);
  314. if (res) {
  315. return (-1);
  316. }
  317. *buf = addr_orig;
  318. return (0);
  319. }
  320. static cs_error_t
  321. msg_send (
  322. struct ipc_instance *ipc_instance,
  323. const struct iovec *iov,
  324. unsigned int iov_len)
  325. {
  326. struct sembuf sop;
  327. int i;
  328. int res;
  329. int req_buffer_idx = 0;
  330. for (i = 0; i < iov_len; i++) {
  331. memcpy (&ipc_instance->request_buffer[req_buffer_idx],
  332. iov[i].iov_base,
  333. iov[i].iov_len);
  334. req_buffer_idx += iov[i].iov_len;
  335. }
  336. /*
  337. * Signal semaphore #0 indicting a new message from client
  338. * to server request queue
  339. */
  340. sop.sem_num = 0;
  341. sop.sem_op = 1;
  342. sop.sem_flg = 0;
  343. retry_semop:
  344. res = semop (ipc_instance->semid, &sop, 1);
  345. if (res == -1 && errno == EINTR) {
  346. goto retry_semop;
  347. } else
  348. if (res == -1 && errno == EACCES) {
  349. priv_change_send (ipc_instance);
  350. goto retry_semop;
  351. } else
  352. if (res == -1) {
  353. return (CS_ERR_LIBRARY);
  354. }
  355. return (CS_OK);
  356. }
  357. static cs_error_t
  358. reply_receive (
  359. struct ipc_instance *ipc_instance,
  360. void *res_msg,
  361. size_t res_len)
  362. {
  363. struct sembuf sop;
  364. coroipc_response_header_t *response_header;
  365. int res;
  366. /*
  367. * Wait for semaphore #1 indicating a new message from server
  368. * to client in the response queue
  369. */
  370. sop.sem_num = 1;
  371. sop.sem_op = -1;
  372. sop.sem_flg = 0;
  373. retry_semop:
  374. res = semop (ipc_instance->semid, &sop, 1);
  375. if (res == -1 && errno == EINTR) {
  376. goto retry_semop;
  377. } else
  378. if (res == -1 && errno == EACCES) {
  379. priv_change_send (ipc_instance);
  380. goto retry_semop;
  381. } else
  382. if (res == -1) {
  383. return (CS_ERR_LIBRARY);
  384. }
  385. response_header = (coroipc_response_header_t *)ipc_instance->response_buffer;
  386. if (response_header->error == CS_ERR_TRY_AGAIN) {
  387. return (CS_ERR_TRY_AGAIN);
  388. }
  389. memcpy (res_msg, ipc_instance->response_buffer, res_len);
  390. return (CS_OK);
  391. }
  392. static cs_error_t
  393. reply_receive_in_buf (
  394. struct ipc_instance *ipc_instance,
  395. void **res_msg)
  396. {
  397. struct sembuf sop;
  398. int res;
  399. /*
  400. * Wait for semaphore #1 indicating a new message from server
  401. * to client in the response queue
  402. */
  403. sop.sem_num = 1;
  404. sop.sem_op = -1;
  405. sop.sem_flg = 0;
  406. retry_semop:
  407. res = semop (ipc_instance->semid, &sop, 1);
  408. if (res == -1 && errno == EINTR) {
  409. goto retry_semop;
  410. } else
  411. if (res == -1 && errno == EACCES) {
  412. priv_change_send (ipc_instance);
  413. goto retry_semop;
  414. } else
  415. if (res == -1) {
  416. return (CS_ERR_LIBRARY);
  417. }
  418. *res_msg = (char *)ipc_instance->response_buffer;
  419. return (CS_OK);
  420. }
  421. /*
  422. * External API
  423. */
  424. cs_error_t
  425. coroipcc_service_connect (
  426. const char *socket_name,
  427. unsigned int service,
  428. size_t request_size,
  429. size_t response_size,
  430. size_t dispatch_size,
  431. hdb_handle_t *handle)
  432. {
  433. int request_fd;
  434. struct sockaddr_un address;
  435. cs_error_t res;
  436. struct ipc_instance *ipc_instance;
  437. key_t semkey = 0;
  438. int sys_res;
  439. mar_req_setup_t req_setup;
  440. mar_res_setup_t res_setup;
  441. union semun semun;
  442. char control_map_path[128];
  443. char request_map_path[128];
  444. char response_map_path[128];
  445. char dispatch_map_path[128];
  446. res = hdb_error_to_cs (hdb_handle_create (&ipc_hdb,
  447. sizeof (struct ipc_instance), handle));
  448. if (res != CS_OK) {
  449. return (res);
  450. }
  451. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, *handle, (void **)&ipc_instance));
  452. if (res != CS_OK) {
  453. return (res);
  454. }
  455. res_setup.error = CS_ERR_LIBRARY;
  456. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  457. if (request_fd == -1) {
  458. return (CS_ERR_LIBRARY);
  459. }
  460. memset (&address, 0, sizeof (struct sockaddr_un));
  461. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  462. address.sun_len = sizeof(struct sockaddr_un);
  463. #endif
  464. address.sun_family = PF_UNIX;
  465. #if defined(COROSYNC_LINUX)
  466. sprintf (address.sun_path + 1, "%s", socket_name);
  467. #else
  468. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  469. #endif
  470. sys_res = connect (request_fd, (struct sockaddr *)&address,
  471. COROSYNC_SUN_LEN(&address));
  472. if (sys_res == -1) {
  473. close (request_fd);
  474. return (CS_ERR_TRY_AGAIN);
  475. }
  476. /*
  477. * Allocate a semaphore segment
  478. */
  479. while (1) {
  480. semkey = random();
  481. ipc_instance->euid = geteuid ();
  482. if ((ipc_instance->semid
  483. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  484. break;
  485. }
  486. if (errno != EEXIST) {
  487. goto res_exit;
  488. }
  489. }
  490. semun.val = 0;
  491. res = semctl (ipc_instance->semid, 0, SETVAL, semun);
  492. if (res != 0) {
  493. goto res_exit;
  494. }
  495. res = semctl (ipc_instance->semid, 1, SETVAL, semun);
  496. if (res != 0) {
  497. goto res_exit;
  498. }
  499. res = memory_map (
  500. control_map_path,
  501. "control_buffer-XXXXXX",
  502. (void *)&ipc_instance->control_buffer,
  503. 8192);
  504. res = memory_map (
  505. request_map_path,
  506. "request_buffer-XXXXXX",
  507. (void *)&ipc_instance->request_buffer,
  508. request_size);
  509. res = memory_map (
  510. response_map_path,
  511. "response_buffer-XXXXXX",
  512. (void *)&ipc_instance->response_buffer,
  513. response_size);
  514. res = circular_memory_map (
  515. dispatch_map_path,
  516. "dispatch_buffer-XXXXXX",
  517. (void *)&ipc_instance->dispatch_buffer,
  518. dispatch_size);
  519. /*
  520. * Initialize IPC setup message
  521. */
  522. req_setup.service = service;
  523. strcpy (req_setup.control_file, control_map_path);
  524. strcpy (req_setup.request_file, request_map_path);
  525. strcpy (req_setup.response_file, response_map_path);
  526. strcpy (req_setup.dispatch_file, dispatch_map_path);
  527. req_setup.control_size = 8192;
  528. req_setup.request_size = request_size;
  529. req_setup.response_size = response_size;
  530. req_setup.dispatch_size = dispatch_size;
  531. req_setup.semkey = semkey;
  532. res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  533. if (res != CS_OK) {
  534. goto res_exit;
  535. }
  536. res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  537. if (res != CS_OK) {
  538. goto res_exit;
  539. }
  540. ipc_instance->fd = request_fd;
  541. ipc_instance->flow_control_state = 0;
  542. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  543. goto res_exit;
  544. }
  545. ipc_instance->control_size = 8192;
  546. ipc_instance->request_size = request_size;
  547. ipc_instance->response_size = response_size;
  548. ipc_instance->dispatch_size = dispatch_size;
  549. pthread_mutex_init (&ipc_instance->mutex, NULL);
  550. hdb_handle_put (&ipc_hdb, *handle);
  551. return (res_setup.error);
  552. res_exit:
  553. close (request_fd);
  554. if (ipc_instance->semid > 0)
  555. semctl (ipc_instance->semid, 0, IPC_RMID);
  556. return (res_setup.error);
  557. }
  558. cs_error_t
  559. coroipcc_service_disconnect (
  560. hdb_handle_t handle)
  561. {
  562. cs_error_t res;
  563. struct ipc_instance *ipc_instance;
  564. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  565. if (res != CS_OK) {
  566. return (res);
  567. }
  568. shutdown (ipc_instance->fd, SHUT_RDWR);
  569. close (ipc_instance->fd);
  570. hdb_handle_destroy (&ipc_hdb, handle);
  571. hdb_handle_put (&ipc_hdb, handle);
  572. return (CS_OK);
  573. }
  574. cs_error_t
  575. coroipcc_dispatch_flow_control_get (
  576. hdb_handle_t handle,
  577. unsigned int *flow_control_state)
  578. {
  579. struct ipc_instance *ipc_instance;
  580. cs_error_t res;
  581. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  582. if (res != CS_OK) {
  583. return (res);
  584. }
  585. *flow_control_state = ipc_instance->flow_control_state;
  586. hdb_handle_put (&ipc_hdb, handle);
  587. return (res);
  588. }
  589. cs_error_t
  590. coroipcc_fd_get (
  591. hdb_handle_t handle,
  592. int *fd)
  593. {
  594. struct ipc_instance *ipc_instance;
  595. cs_error_t res;
  596. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  597. if (res != CS_OK) {
  598. return (res);
  599. }
  600. *fd = ipc_instance->fd;
  601. hdb_handle_put (&ipc_hdb, handle);
  602. return (res);
  603. }
  604. cs_error_t
  605. coroipcc_dispatch_get (
  606. hdb_handle_t handle,
  607. void **data,
  608. int timeout)
  609. {
  610. struct pollfd ufds;
  611. int poll_events;
  612. char buf;
  613. struct ipc_instance *ipc_instance;
  614. int res;
  615. char buf_two = 1;
  616. char *data_addr;
  617. cs_error_t error = CS_OK;
  618. error = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  619. if (error != CS_OK) {
  620. return (error);
  621. }
  622. *data = NULL;
  623. ufds.fd = ipc_instance->fd;
  624. ufds.events = POLLIN;
  625. ufds.revents = 0;
  626. retry_poll:
  627. poll_events = poll (&ufds, 1, timeout);
  628. if (poll_events == -1 && errno == EINTR) {
  629. goto retry_poll;
  630. } else
  631. if (poll_events == -1) {
  632. goto error_put;
  633. } else
  634. if (poll_events == 0) {
  635. goto error_put;
  636. }
  637. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  638. error = CS_ERR_LIBRARY;
  639. goto error_put;
  640. }
  641. retry_recv:
  642. res = recv (ipc_instance->fd, &buf, 1, 0);
  643. if (res == -1 && errno == EINTR) {
  644. goto retry_recv;
  645. } else
  646. if (res == -1) {
  647. goto error_put;
  648. }
  649. if (res == 0) {
  650. goto error_put;
  651. }
  652. ipc_instance->flow_control_state = 0;
  653. if (buf == 1 || buf == 2) {
  654. ipc_instance->flow_control_state = 1;
  655. }
  656. /*
  657. * Notify executive to flush any pending dispatch messages
  658. */
  659. if (ipc_instance->flow_control_state) {
  660. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  661. res = socket_send (ipc_instance->fd, &buf_two, 1);
  662. assert (res == CS_OK); /* TODO */
  663. }
  664. /*
  665. * This is just a notification of flow control starting at the addition
  666. * of a new pending message, not a message to dispatch
  667. */
  668. if (buf == 2) {
  669. goto error_put;
  670. }
  671. if (buf == 3) {
  672. goto error_put;
  673. }
  674. data_addr = ipc_instance->dispatch_buffer;
  675. data_addr = &data_addr[ipc_instance->control_buffer->read];
  676. *data = (void *)data_addr;
  677. return (CS_OK);
  678. error_put:
  679. hdb_handle_put (&ipc_hdb, handle);
  680. return (error);
  681. }
  682. cs_error_t
  683. coroipcc_dispatch_put (hdb_handle_t handle)
  684. {
  685. struct sembuf sop;
  686. coroipc_response_header_t *header;
  687. struct ipc_instance *ipc_instance;
  688. int res;
  689. char *addr;
  690. unsigned int read_idx;
  691. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  692. if (res != CS_OK) {
  693. return (res);
  694. }
  695. sop.sem_num = 2;
  696. sop.sem_op = -1;
  697. sop.sem_flg = 0;
  698. retry_semop:
  699. res = semop (ipc_instance->semid, &sop, 1);
  700. if (res == -1 && errno == EINTR) {
  701. goto retry_semop;
  702. } else
  703. if (res == -1 && errno == EACCES) {
  704. priv_change_send (ipc_instance);
  705. goto retry_semop;
  706. } else
  707. if (res == -1) {
  708. return (CS_ERR_LIBRARY);
  709. }
  710. addr = ipc_instance->dispatch_buffer;
  711. read_idx = ipc_instance->control_buffer->read;
  712. header = (coroipc_response_header_t *) &addr[read_idx];
  713. ipc_instance->control_buffer->read =
  714. (read_idx + header->size) % ipc_instance->dispatch_size;
  715. /*
  716. * Put from dispatch get and also from this call's get
  717. */
  718. hdb_handle_put (&ipc_hdb, handle);
  719. hdb_handle_put (&ipc_hdb, handle);
  720. return (CS_OK);
  721. }
  722. cs_error_t
  723. coroipcc_msg_send_reply_receive (
  724. hdb_handle_t handle,
  725. const struct iovec *iov,
  726. unsigned int iov_len,
  727. void *res_msg,
  728. size_t res_len)
  729. {
  730. cs_error_t res;
  731. struct ipc_instance *ipc_instance;
  732. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  733. if (res != CS_OK) {
  734. return (res);
  735. }
  736. pthread_mutex_lock (&ipc_instance->mutex);
  737. res = msg_send (ipc_instance, iov, iov_len);
  738. if (res != CS_OK) {
  739. goto error_exit;
  740. }
  741. res = reply_receive (ipc_instance, res_msg, res_len);
  742. error_exit:
  743. hdb_handle_put (&ipc_hdb, handle);
  744. pthread_mutex_unlock (&ipc_instance->mutex);
  745. return (res);
  746. }
  747. cs_error_t
  748. coroipcc_msg_send_reply_receive_in_buf_get (
  749. hdb_handle_t handle,
  750. const struct iovec *iov,
  751. unsigned int iov_len,
  752. void **res_msg)
  753. {
  754. unsigned int res;
  755. struct ipc_instance *ipc_instance;
  756. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  757. if (res != CS_OK) {
  758. return (res);
  759. }
  760. pthread_mutex_lock (&ipc_instance->mutex);
  761. res = msg_send (ipc_instance, iov, iov_len);
  762. if (res != CS_OK) {
  763. goto error_exit;
  764. }
  765. res = reply_receive_in_buf (ipc_instance, res_msg);
  766. error_exit:
  767. pthread_mutex_unlock (&ipc_instance->mutex);
  768. return (res);
  769. }
  770. cs_error_t
  771. coroipcc_msg_send_reply_receive_in_buf_put (
  772. hdb_handle_t handle)
  773. {
  774. unsigned int res;
  775. struct ipc_instance *ipc_instance;
  776. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  777. if (res != CS_OK) {
  778. return (res);
  779. }
  780. hdb_handle_put (&ipc_hdb, handle);
  781. hdb_handle_put (&ipc_hdb, handle);
  782. return (res);
  783. }
  784. cs_error_t
  785. coroipcc_zcb_alloc (
  786. hdb_handle_t handle,
  787. void **buffer,
  788. size_t size,
  789. size_t header_size)
  790. {
  791. struct ipc_instance *ipc_instance;
  792. void *buf = NULL;
  793. char path[128];
  794. unsigned int res;
  795. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  796. coroipc_response_header_t res_coroipcs_zc_alloc;
  797. size_t map_size;
  798. struct iovec iovec;
  799. struct coroipcs_zc_header *hdr;
  800. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  801. if (res != CS_OK) {
  802. return (res);
  803. }
  804. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  805. res = memory_map (path, "corosync_zerocopy-XXXXXX", &buf, size);
  806. assert (res != -1);
  807. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  808. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  809. req_coroipcc_zc_alloc.map_size = map_size;
  810. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  811. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  812. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  813. res = coroipcc_msg_send_reply_receive (
  814. handle,
  815. &iovec,
  816. 1,
  817. &res_coroipcs_zc_alloc,
  818. sizeof (coroipc_response_header_t));
  819. hdr = (struct coroipcs_zc_header *)buf;
  820. hdr->map_size = map_size;
  821. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  822. hdb_handle_put (&ipc_hdb, handle);
  823. return (res);
  824. }
  825. cs_error_t
  826. coroipcc_zcb_free (
  827. hdb_handle_t handle,
  828. void *buffer)
  829. {
  830. struct ipc_instance *ipc_instance;
  831. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  832. coroipc_response_header_t res_coroipcs_zc_free;
  833. struct iovec iovec;
  834. unsigned int res;
  835. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  836. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  837. if (res != CS_OK) {
  838. return (res);
  839. }
  840. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  841. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  842. req_coroipcc_zc_free.map_size = header->map_size;
  843. req_coroipcc_zc_free.server_address = header->server_address;
  844. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  845. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  846. res = coroipcc_msg_send_reply_receive (
  847. handle,
  848. &iovec,
  849. 1,
  850. &res_coroipcs_zc_free,
  851. sizeof (coroipc_response_header_t));
  852. munmap ((void *)header, header->map_size);
  853. hdb_handle_put (&ipc_hdb, handle);
  854. return (res);
  855. }
  856. cs_error_t
  857. coroipcc_zcb_msg_send_reply_receive (
  858. hdb_handle_t handle,
  859. void *msg,
  860. void *res_msg,
  861. size_t res_len)
  862. {
  863. struct ipc_instance *ipc_instance;
  864. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  865. struct coroipcs_zc_header *hdr;
  866. struct iovec iovec;
  867. cs_error_t res;
  868. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  869. if (res != CS_OK) {
  870. return (res);
  871. }
  872. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  873. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  874. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  875. req_coroipcc_zc_execute.server_address = hdr->server_address;
  876. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  877. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  878. res = coroipcc_msg_send_reply_receive (
  879. handle,
  880. &iovec,
  881. 1,
  882. res_msg,
  883. res_len);
  884. hdb_handle_put (&ipc_hdb, handle);
  885. return (res);
  886. }