coroipcc.c 23 KB

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