coroipcc.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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)
  83. /* SUN_LEN is broken for abstract namespace
  84. */
  85. #define AIS_SUN_LEN(a) sizeof(*(a))
  86. #else
  87. #define AIS_SUN_LEN(a) SUN_LEN(a)
  88. #endif
  89. #ifdef SO_NOSIGPIPE
  90. void socket_nosigpipe(int s)
  91. {
  92. int on = 1;
  93. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  94. }
  95. #endif
  96. #ifndef MSG_NOSIGNAL
  97. #define MSG_NOSIGNAL 0
  98. #endif
  99. static cs_error_t
  100. socket_send (
  101. int s,
  102. void *msg,
  103. size_t len)
  104. {
  105. cs_error_t res = CS_OK;
  106. int result;
  107. struct msghdr msg_send;
  108. struct iovec iov_send;
  109. char *rbuf = msg;
  110. int processed = 0;
  111. msg_send.msg_iov = &iov_send;
  112. msg_send.msg_iovlen = 1;
  113. msg_send.msg_name = 0;
  114. msg_send.msg_namelen = 0;
  115. msg_send.msg_control = 0;
  116. msg_send.msg_controllen = 0;
  117. msg_send.msg_flags = 0;
  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. msg_recv.msg_control = 0;
  160. msg_recv.msg_controllen = 0;
  161. msg_recv.msg_flags = 0;
  162. retry_recv:
  163. iov_recv.iov_base = (void *)&rbuf[processed];
  164. iov_recv.iov_len = len - processed;
  165. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL|MSG_WAITALL);
  166. if (result == -1) {
  167. switch (errno) {
  168. case EINTR:
  169. goto retry_recv;
  170. break;
  171. case EAGAIN:
  172. goto retry_recv;
  173. break;
  174. default:
  175. res = CS_ERR_LIBRARY;
  176. goto res_exit;
  177. }
  178. }
  179. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  180. /* On many OS poll never return POLLHUP or POLLERR.
  181. * EOF is detected when recvmsg return 0.
  182. */
  183. if (result == 0) {
  184. res = CS_ERR_LIBRARY;
  185. goto res_exit;
  186. }
  187. #endif
  188. processed += result;
  189. if (processed != len) {
  190. goto retry_recv;
  191. }
  192. assert (processed == len);
  193. res_exit:
  194. return (res);
  195. }
  196. static int
  197. priv_change_send (struct ipc_instance *ipc_instance)
  198. {
  199. char buf_req;
  200. mar_req_priv_change req_priv_change;
  201. unsigned int res;
  202. req_priv_change.euid = geteuid();
  203. /*
  204. * Don't resend request unless euid has changed
  205. */
  206. if (ipc_instance->euid == req_priv_change.euid) {
  207. return (0);
  208. }
  209. req_priv_change.egid = getegid();
  210. buf_req = MESSAGE_REQ_CHANGE_EUID;
  211. res = socket_send (ipc_instance->fd, &buf_req, 1);
  212. if (res == -1) {
  213. return (-1);
  214. }
  215. res = socket_send (ipc_instance->fd, &req_priv_change,
  216. sizeof (req_priv_change));
  217. if (res == -1) {
  218. return (-1);
  219. }
  220. ipc_instance->euid = req_priv_change.euid;
  221. return (0);
  222. }
  223. #if defined(_SEM_SEMUN_UNDEFINED)
  224. union semun {
  225. int val;
  226. struct semid_ds *buf;
  227. unsigned short int *array;
  228. struct seminfo *__buf;
  229. };
  230. #endif
  231. static int
  232. circular_memory_map (char *path, const char *file, void **buf, size_t bytes)
  233. {
  234. int fd;
  235. void *addr_orig;
  236. void *addr;
  237. int res;
  238. sprintf (path, "/dev/shm/%s", file);
  239. fd = mkstemp (path);
  240. if (fd == -1) {
  241. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  242. fd = mkstemp (path);
  243. if (fd == -1) {
  244. return (-1);
  245. }
  246. }
  247. res = ftruncate (fd, bytes);
  248. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  249. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  250. if (addr_orig == MAP_FAILED) {
  251. return (-1);
  252. }
  253. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  254. MAP_FIXED | MAP_SHARED, fd, 0);
  255. if (addr != addr_orig) {
  256. return (-1);
  257. }
  258. addr = mmap (((char *)addr_orig) + bytes,
  259. bytes, PROT_READ | PROT_WRITE,
  260. MAP_FIXED | MAP_SHARED, fd, 0);
  261. res = close (fd);
  262. if (res) {
  263. return (-1);
  264. }
  265. *buf = addr_orig;
  266. return (0);
  267. }
  268. static void
  269. memory_unmap (void *addr, size_t bytes)
  270. {
  271. int res;
  272. res = munmap (addr, bytes);
  273. }
  274. void ipc_hdb_destructor (void *context ) {
  275. struct ipc_instance *ipc_instance = (struct ipc_instance *)context;
  276. /*
  277. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  278. */
  279. memory_unmap (ipc_instance->control_buffer, ipc_instance->control_size);
  280. memory_unmap (ipc_instance->request_buffer, ipc_instance->request_size);
  281. memory_unmap (ipc_instance->response_buffer, ipc_instance->response_size);
  282. memory_unmap (ipc_instance->dispatch_buffer, (ipc_instance->dispatch_size) << 1);
  283. }
  284. static int
  285. memory_map (char *path, const char *file, void **buf, size_t bytes)
  286. {
  287. int fd;
  288. void *addr_orig;
  289. void *addr;
  290. int res;
  291. sprintf (path, "/dev/shm/%s", file);
  292. fd = mkstemp (path);
  293. if (fd == -1) {
  294. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  295. fd = mkstemp (path);
  296. if (fd == -1) {
  297. return (-1);
  298. }
  299. }
  300. res = ftruncate (fd, bytes);
  301. addr_orig = mmap (NULL, bytes, PROT_NONE,
  302. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  303. if (addr_orig == MAP_FAILED) {
  304. return (-1);
  305. }
  306. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  307. MAP_FIXED | MAP_SHARED, fd, 0);
  308. if (addr != addr_orig) {
  309. return (-1);
  310. }
  311. res = close (fd);
  312. if (res) {
  313. return (-1);
  314. }
  315. *buf = addr_orig;
  316. return (0);
  317. }
  318. static cs_error_t
  319. msg_send (
  320. struct ipc_instance *ipc_instance,
  321. const struct iovec *iov,
  322. unsigned int iov_len)
  323. {
  324. struct sembuf sop;
  325. int i;
  326. int res;
  327. int req_buffer_idx = 0;
  328. for (i = 0; i < iov_len; i++) {
  329. memcpy (&ipc_instance->request_buffer[req_buffer_idx],
  330. iov[i].iov_base,
  331. iov[i].iov_len);
  332. req_buffer_idx += iov[i].iov_len;
  333. }
  334. /*
  335. * Signal semaphore #0 indicting a new message from client
  336. * to server request queue
  337. */
  338. sop.sem_num = 0;
  339. sop.sem_op = 1;
  340. sop.sem_flg = 0;
  341. retry_semop:
  342. res = semop (ipc_instance->semid, &sop, 1);
  343. if (res == -1 && errno == EINTR) {
  344. goto retry_semop;
  345. } else
  346. if (res == -1 && errno == EACCES) {
  347. priv_change_send (ipc_instance);
  348. goto retry_semop;
  349. } else
  350. if (res == -1) {
  351. return (CS_ERR_LIBRARY);
  352. }
  353. return (CS_OK);
  354. }
  355. static cs_error_t
  356. reply_receive (
  357. struct ipc_instance *ipc_instance,
  358. void *res_msg,
  359. size_t res_len)
  360. {
  361. struct sembuf sop;
  362. coroipc_response_header_t *response_header;
  363. int res;
  364. /*
  365. * Wait for semaphore #1 indicating a new message from server
  366. * to client in the response queue
  367. */
  368. sop.sem_num = 1;
  369. sop.sem_op = -1;
  370. sop.sem_flg = 0;
  371. retry_semop:
  372. res = semop (ipc_instance->semid, &sop, 1);
  373. if (res == -1 && errno == EINTR) {
  374. goto retry_semop;
  375. } else
  376. if (res == -1 && errno == EACCES) {
  377. priv_change_send (ipc_instance);
  378. goto retry_semop;
  379. } else
  380. if (res == -1) {
  381. return (CS_ERR_LIBRARY);
  382. }
  383. response_header = (coroipc_response_header_t *)ipc_instance->response_buffer;
  384. if (response_header->error == CS_ERR_TRY_AGAIN) {
  385. return (CS_ERR_TRY_AGAIN);
  386. }
  387. memcpy (res_msg, ipc_instance->response_buffer, res_len);
  388. return (CS_OK);
  389. }
  390. static cs_error_t
  391. reply_receive_in_buf (
  392. struct ipc_instance *ipc_instance,
  393. void **res_msg)
  394. {
  395. struct sembuf sop;
  396. int res;
  397. /*
  398. * Wait for semaphore #1 indicating a new message from server
  399. * to client in the response queue
  400. */
  401. sop.sem_num = 1;
  402. sop.sem_op = -1;
  403. sop.sem_flg = 0;
  404. retry_semop:
  405. res = semop (ipc_instance->semid, &sop, 1);
  406. if (res == -1 && errno == EINTR) {
  407. goto retry_semop;
  408. } else
  409. if (res == -1 && errno == EACCES) {
  410. priv_change_send (ipc_instance);
  411. goto retry_semop;
  412. } else
  413. if (res == -1) {
  414. return (CS_ERR_LIBRARY);
  415. }
  416. *res_msg = (char *)ipc_instance->response_buffer;
  417. return (CS_OK);
  418. }
  419. /*
  420. * External API
  421. */
  422. cs_error_t
  423. coroipcc_service_connect (
  424. const char *socket_name,
  425. unsigned int service,
  426. size_t request_size,
  427. size_t response_size,
  428. size_t dispatch_size,
  429. hdb_handle_t *handle)
  430. {
  431. int request_fd;
  432. struct sockaddr_un address;
  433. cs_error_t res;
  434. struct ipc_instance *ipc_instance;
  435. key_t semkey = 0;
  436. int sys_res;
  437. mar_req_setup_t req_setup;
  438. mar_res_setup_t res_setup;
  439. union semun semun;
  440. char control_map_path[128];
  441. char request_map_path[128];
  442. char response_map_path[128];
  443. char dispatch_map_path[128];
  444. res = hdb_error_to_cs (hdb_handle_create (&ipc_hdb,
  445. sizeof (struct ipc_instance), handle));
  446. if (res != CS_OK) {
  447. return (res);
  448. }
  449. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, *handle, (void **)&ipc_instance));
  450. if (res != CS_OK) {
  451. return (res);
  452. }
  453. res_setup.error = CS_ERR_LIBRARY;
  454. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  455. if (request_fd == -1) {
  456. return (CS_ERR_LIBRARY);
  457. }
  458. memset (&address, 0, sizeof (struct sockaddr_un));
  459. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  460. address.sun_len = sizeof(struct sockaddr_un);
  461. #endif
  462. address.sun_family = PF_UNIX;
  463. #if defined(COROSYNC_LINUX)
  464. sprintf (address.sun_path + 1, "%s", socket_name);
  465. #else
  466. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  467. #endif
  468. sys_res = connect (request_fd, (struct sockaddr *)&address,
  469. AIS_SUN_LEN(&address));
  470. if (sys_res == -1) {
  471. close (request_fd);
  472. return (CS_ERR_TRY_AGAIN);
  473. }
  474. /*
  475. * Allocate a semaphore segment
  476. */
  477. while (1) {
  478. semkey = random();
  479. ipc_instance->euid = geteuid ();
  480. if ((ipc_instance->semid
  481. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  482. break;
  483. }
  484. if (errno != EEXIST) {
  485. goto res_exit;
  486. }
  487. }
  488. semun.val = 0;
  489. res = semctl (ipc_instance->semid, 0, SETVAL, semun);
  490. if (res != 0) {
  491. goto res_exit;
  492. }
  493. res = semctl (ipc_instance->semid, 1, SETVAL, semun);
  494. if (res != 0) {
  495. goto res_exit;
  496. }
  497. res = memory_map (
  498. control_map_path,
  499. "control_buffer-XXXXXX",
  500. (void *)&ipc_instance->control_buffer,
  501. 8192);
  502. res = memory_map (
  503. request_map_path,
  504. "request_buffer-XXXXXX",
  505. (void *)&ipc_instance->request_buffer,
  506. request_size);
  507. res = memory_map (
  508. response_map_path,
  509. "response_buffer-XXXXXX",
  510. (void *)&ipc_instance->response_buffer,
  511. response_size);
  512. res = circular_memory_map (
  513. dispatch_map_path,
  514. "dispatch_buffer-XXXXXX",
  515. (void *)&ipc_instance->dispatch_buffer,
  516. dispatch_size);
  517. /*
  518. * Initialize IPC setup message
  519. */
  520. req_setup.service = service;
  521. strcpy (req_setup.control_file, control_map_path);
  522. strcpy (req_setup.request_file, request_map_path);
  523. strcpy (req_setup.response_file, response_map_path);
  524. strcpy (req_setup.dispatch_file, dispatch_map_path);
  525. req_setup.control_size = 8192;
  526. req_setup.request_size = request_size;
  527. req_setup.response_size = response_size;
  528. req_setup.dispatch_size = dispatch_size;
  529. req_setup.semkey = semkey;
  530. res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  531. if (res != CS_OK) {
  532. goto res_exit;
  533. }
  534. res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  535. if (res != CS_OK) {
  536. goto res_exit;
  537. }
  538. ipc_instance->fd = request_fd;
  539. ipc_instance->flow_control_state = 0;
  540. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  541. goto res_exit;
  542. }
  543. ipc_instance->control_size = 8192;
  544. ipc_instance->request_size = request_size;
  545. ipc_instance->response_size = response_size;
  546. ipc_instance->dispatch_size = dispatch_size;
  547. pthread_mutex_init (&ipc_instance->mutex, NULL);
  548. hdb_handle_put (&ipc_hdb, *handle);
  549. return (res_setup.error);
  550. res_exit:
  551. close (request_fd);
  552. if (ipc_instance->semid > 0)
  553. semctl (ipc_instance->semid, 0, IPC_RMID);
  554. return (res_setup.error);
  555. }
  556. cs_error_t
  557. coroipcc_service_disconnect (
  558. hdb_handle_t handle)
  559. {
  560. cs_error_t res;
  561. struct ipc_instance *ipc_instance;
  562. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  563. if (res != CS_OK) {
  564. return (res);
  565. }
  566. shutdown (ipc_instance->fd, SHUT_RDWR);
  567. close (ipc_instance->fd);
  568. hdb_handle_destroy (&ipc_hdb, handle);
  569. hdb_handle_put (&ipc_hdb, handle);
  570. return (CS_OK);
  571. }
  572. cs_error_t
  573. coroipcc_dispatch_flow_control_get (
  574. hdb_handle_t handle,
  575. unsigned int *flow_control_state)
  576. {
  577. struct ipc_instance *ipc_instance;
  578. cs_error_t res;
  579. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  580. if (res != CS_OK) {
  581. return (res);
  582. }
  583. *flow_control_state = ipc_instance->flow_control_state;
  584. hdb_handle_put (&ipc_hdb, handle);
  585. return (res);
  586. }
  587. cs_error_t
  588. coroipcc_fd_get (
  589. hdb_handle_t handle,
  590. int *fd)
  591. {
  592. struct ipc_instance *ipc_instance;
  593. cs_error_t res;
  594. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  595. if (res != CS_OK) {
  596. return (res);
  597. }
  598. *fd = ipc_instance->fd;
  599. hdb_handle_put (&ipc_hdb, handle);
  600. return (res);
  601. }
  602. cs_error_t
  603. coroipcc_dispatch_get (
  604. hdb_handle_t handle,
  605. void **data,
  606. int timeout)
  607. {
  608. struct pollfd ufds;
  609. int poll_events;
  610. char buf;
  611. struct ipc_instance *ipc_instance;
  612. int res;
  613. char buf_two = 1;
  614. char *data_addr;
  615. cs_error_t error = CS_OK;
  616. error = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  617. if (error != CS_OK) {
  618. return (error);
  619. }
  620. *data = NULL;
  621. ufds.fd = ipc_instance->fd;
  622. ufds.events = POLLIN;
  623. ufds.revents = 0;
  624. retry_poll:
  625. poll_events = poll (&ufds, 1, timeout);
  626. if (poll_events == -1 && errno == EINTR) {
  627. goto retry_poll;
  628. } else
  629. if (poll_events == -1) {
  630. goto error_put;
  631. } else
  632. if (poll_events == 0) {
  633. goto error_put;
  634. }
  635. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  636. error = CS_ERR_LIBRARY;
  637. goto error_put;
  638. }
  639. retry_recv:
  640. res = recv (ipc_instance->fd, &buf, 1, 0);
  641. if (res == -1 && errno == EINTR) {
  642. goto retry_recv;
  643. } else
  644. if (res == -1) {
  645. goto error_put;
  646. }
  647. if (res == 0) {
  648. goto error_put;
  649. }
  650. ipc_instance->flow_control_state = 0;
  651. if (buf == 1 || buf == 2) {
  652. ipc_instance->flow_control_state = 1;
  653. }
  654. /*
  655. * Notify executive to flush any pending dispatch messages
  656. */
  657. if (ipc_instance->flow_control_state) {
  658. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  659. res = socket_send (ipc_instance->fd, &buf_two, 1);
  660. assert (res == CS_OK); /* TODO */
  661. }
  662. /*
  663. * This is just a notification of flow control starting at the addition
  664. * of a new pending message, not a message to dispatch
  665. */
  666. if (buf == 2) {
  667. goto error_put;
  668. }
  669. if (buf == 3) {
  670. goto error_put;
  671. }
  672. data_addr = ipc_instance->dispatch_buffer;
  673. data_addr = &data_addr[ipc_instance->control_buffer->read];
  674. *data = (void *)data_addr;
  675. return (CS_OK);
  676. error_put:
  677. hdb_handle_put (&ipc_hdb, handle);
  678. return (error);
  679. }
  680. cs_error_t
  681. coroipcc_dispatch_put (hdb_handle_t handle)
  682. {
  683. struct sembuf sop;
  684. coroipc_response_header_t *header;
  685. struct ipc_instance *ipc_instance;
  686. int res;
  687. char *addr;
  688. unsigned int read_idx;
  689. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  690. if (res != CS_OK) {
  691. return (res);
  692. }
  693. sop.sem_num = 2;
  694. sop.sem_op = -1;
  695. sop.sem_flg = 0;
  696. retry_semop:
  697. res = semop (ipc_instance->semid, &sop, 1);
  698. if (res == -1 && errno == EINTR) {
  699. goto retry_semop;
  700. } else
  701. if (res == -1 && errno == EACCES) {
  702. priv_change_send (ipc_instance);
  703. goto retry_semop;
  704. } else
  705. if (res == -1) {
  706. return (CS_ERR_LIBRARY);
  707. }
  708. addr = ipc_instance->dispatch_buffer;
  709. read_idx = ipc_instance->control_buffer->read;
  710. header = (coroipc_response_header_t *) &addr[read_idx];
  711. ipc_instance->control_buffer->read =
  712. (read_idx + header->size) % ipc_instance->dispatch_size;
  713. /*
  714. * Put from dispatch get and also from this call's get
  715. */
  716. hdb_handle_put (&ipc_hdb, handle);
  717. hdb_handle_put (&ipc_hdb, handle);
  718. return (CS_OK);
  719. }
  720. cs_error_t
  721. coroipcc_msg_send_reply_receive (
  722. hdb_handle_t handle,
  723. const struct iovec *iov,
  724. unsigned int iov_len,
  725. void *res_msg,
  726. size_t res_len)
  727. {
  728. cs_error_t res;
  729. struct ipc_instance *ipc_instance;
  730. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  731. if (res != CS_OK) {
  732. return (res);
  733. }
  734. pthread_mutex_lock (&ipc_instance->mutex);
  735. res = msg_send (ipc_instance, iov, iov_len);
  736. if (res != CS_OK) {
  737. goto error_exit;
  738. }
  739. res = reply_receive (ipc_instance, res_msg, res_len);
  740. error_exit:
  741. hdb_handle_put (&ipc_hdb, handle);
  742. pthread_mutex_unlock (&ipc_instance->mutex);
  743. return (res);
  744. }
  745. cs_error_t
  746. coroipcc_msg_send_reply_receive_in_buf_get (
  747. hdb_handle_t handle,
  748. const struct iovec *iov,
  749. unsigned int iov_len,
  750. void **res_msg)
  751. {
  752. unsigned int res;
  753. struct ipc_instance *ipc_instance;
  754. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  755. if (res != CS_OK) {
  756. return (res);
  757. }
  758. pthread_mutex_lock (&ipc_instance->mutex);
  759. res = msg_send (ipc_instance, iov, iov_len);
  760. if (res != CS_OK) {
  761. goto error_exit;
  762. }
  763. res = reply_receive_in_buf (ipc_instance, res_msg);
  764. error_exit:
  765. pthread_mutex_unlock (&ipc_instance->mutex);
  766. return (res);
  767. }
  768. cs_error_t
  769. coroipcc_msg_send_reply_receive_in_buf_put (
  770. hdb_handle_t handle)
  771. {
  772. unsigned int res;
  773. struct ipc_instance *ipc_instance;
  774. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  775. if (res != CS_OK) {
  776. return (res);
  777. }
  778. hdb_handle_put (&ipc_hdb, handle);
  779. hdb_handle_put (&ipc_hdb, handle);
  780. return (res);
  781. }
  782. cs_error_t
  783. coroipcc_zcb_alloc (
  784. hdb_handle_t handle,
  785. void **buffer,
  786. size_t size,
  787. size_t header_size)
  788. {
  789. struct ipc_instance *ipc_instance;
  790. void *buf = NULL;
  791. char path[128];
  792. unsigned int res;
  793. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  794. coroipc_response_header_t res_coroipcs_zc_alloc;
  795. size_t map_size;
  796. struct iovec iovec;
  797. struct coroipcs_zc_header *hdr;
  798. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  799. if (res != CS_OK) {
  800. return (res);
  801. }
  802. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  803. res = memory_map (path, "corosync_zerocopy-XXXXXX", &buf, size);
  804. assert (res != -1);
  805. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  806. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  807. req_coroipcc_zc_alloc.map_size = map_size;
  808. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  809. iovec.iov_base = &req_coroipcc_zc_alloc;
  810. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  811. res = coroipcc_msg_send_reply_receive (
  812. handle,
  813. &iovec,
  814. 1,
  815. &res_coroipcs_zc_alloc,
  816. sizeof (coroipc_response_header_t));
  817. hdr = (struct coroipcs_zc_header *)buf;
  818. hdr->map_size = map_size;
  819. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  820. hdb_handle_put (&ipc_hdb, handle);
  821. return (res);
  822. }
  823. cs_error_t
  824. coroipcc_zcb_free (
  825. hdb_handle_t handle,
  826. void *buffer)
  827. {
  828. struct ipc_instance *ipc_instance;
  829. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  830. coroipc_response_header_t res_coroipcs_zc_free;
  831. struct iovec iovec;
  832. unsigned int res;
  833. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  834. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  835. if (res != CS_OK) {
  836. return (res);
  837. }
  838. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  839. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  840. req_coroipcc_zc_free.map_size = header->map_size;
  841. req_coroipcc_zc_free.server_address = header->server_address;
  842. iovec.iov_base = &req_coroipcc_zc_free;
  843. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  844. res = coroipcc_msg_send_reply_receive (
  845. handle,
  846. &iovec,
  847. 1,
  848. &res_coroipcs_zc_free,
  849. sizeof (coroipc_response_header_t));
  850. munmap (header, header->map_size);
  851. hdb_handle_put (&ipc_hdb, handle);
  852. return (res);
  853. }
  854. cs_error_t
  855. coroipcc_zcb_msg_send_reply_receive (
  856. hdb_handle_t handle,
  857. void *msg,
  858. void *res_msg,
  859. size_t res_len)
  860. {
  861. struct ipc_instance *ipc_instance;
  862. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  863. struct coroipcs_zc_header *hdr;
  864. struct iovec iovec;
  865. cs_error_t res;
  866. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  867. if (res != CS_OK) {
  868. return (res);
  869. }
  870. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  871. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  872. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  873. req_coroipcc_zc_execute.server_address = hdr->server_address;
  874. iovec.iov_base = &req_coroipcc_zc_execute;
  875. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  876. res = coroipcc_msg_send_reply_receive (
  877. handle,
  878. &iovec,
  879. 1,
  880. res_msg,
  881. res_len);
  882. hdb_handle_put (&ipc_hdb, handle);
  883. return (res);
  884. }