coroipcc.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. addr = mmap (((char *)addr_orig) + bytes,
  267. bytes, PROT_READ | PROT_WRITE,
  268. MAP_FIXED | MAP_SHARED, fd, 0);
  269. res = close (fd);
  270. if (res) {
  271. return (-1);
  272. }
  273. *buf = addr_orig;
  274. return (0);
  275. }
  276. static void
  277. memory_unmap (void *addr, size_t bytes)
  278. {
  279. int res;
  280. res = munmap (addr, bytes);
  281. }
  282. void ipc_hdb_destructor (void *context ) {
  283. struct ipc_instance *ipc_instance = (struct ipc_instance *)context;
  284. /*
  285. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  286. */
  287. memory_unmap (ipc_instance->control_buffer, ipc_instance->control_size);
  288. memory_unmap (ipc_instance->request_buffer, ipc_instance->request_size);
  289. memory_unmap (ipc_instance->response_buffer, ipc_instance->response_size);
  290. memory_unmap (ipc_instance->dispatch_buffer, (ipc_instance->dispatch_size) << 1);
  291. }
  292. static int
  293. memory_map (char *path, const char *file, void **buf, size_t bytes)
  294. {
  295. int fd;
  296. void *addr_orig;
  297. void *addr;
  298. int res;
  299. sprintf (path, "/dev/shm/%s", file);
  300. fd = mkstemp (path);
  301. if (fd == -1) {
  302. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  303. fd = mkstemp (path);
  304. if (fd == -1) {
  305. return (-1);
  306. }
  307. }
  308. res = ftruncate (fd, bytes);
  309. addr_orig = mmap (NULL, bytes, PROT_NONE,
  310. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  311. if (addr_orig == MAP_FAILED) {
  312. return (-1);
  313. }
  314. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  315. MAP_FIXED | MAP_SHARED, fd, 0);
  316. if (addr != addr_orig) {
  317. return (-1);
  318. }
  319. res = close (fd);
  320. if (res) {
  321. return (-1);
  322. }
  323. *buf = addr_orig;
  324. return (0);
  325. }
  326. static cs_error_t
  327. msg_send (
  328. struct ipc_instance *ipc_instance,
  329. const struct iovec *iov,
  330. unsigned int iov_len)
  331. {
  332. struct sembuf sop;
  333. int i;
  334. int res;
  335. int req_buffer_idx = 0;
  336. for (i = 0; i < iov_len; i++) {
  337. memcpy (&ipc_instance->request_buffer[req_buffer_idx],
  338. iov[i].iov_base,
  339. iov[i].iov_len);
  340. req_buffer_idx += iov[i].iov_len;
  341. }
  342. /*
  343. * Signal semaphore #0 indicting a new message from client
  344. * to server request queue
  345. */
  346. sop.sem_num = 0;
  347. sop.sem_op = 1;
  348. sop.sem_flg = 0;
  349. retry_semop:
  350. res = semop (ipc_instance->semid, &sop, 1);
  351. if (res == -1 && errno == EINTR) {
  352. goto retry_semop;
  353. } else
  354. if (res == -1 && errno == EACCES) {
  355. priv_change_send (ipc_instance);
  356. goto retry_semop;
  357. } else
  358. if (res == -1) {
  359. return (CS_ERR_LIBRARY);
  360. }
  361. return (CS_OK);
  362. }
  363. static cs_error_t
  364. reply_receive (
  365. struct ipc_instance *ipc_instance,
  366. void *res_msg,
  367. size_t res_len)
  368. {
  369. struct sembuf sop;
  370. coroipc_response_header_t *response_header;
  371. int res;
  372. /*
  373. * Wait for semaphore #1 indicating a new message from server
  374. * to client in the response queue
  375. */
  376. sop.sem_num = 1;
  377. sop.sem_op = -1;
  378. sop.sem_flg = 0;
  379. retry_semop:
  380. res = semop (ipc_instance->semid, &sop, 1);
  381. if (res == -1 && errno == EINTR) {
  382. goto retry_semop;
  383. } else
  384. if (res == -1 && errno == EACCES) {
  385. priv_change_send (ipc_instance);
  386. goto retry_semop;
  387. } else
  388. if (res == -1) {
  389. return (CS_ERR_LIBRARY);
  390. }
  391. response_header = (coroipc_response_header_t *)ipc_instance->response_buffer;
  392. if (response_header->error == CS_ERR_TRY_AGAIN) {
  393. return (CS_ERR_TRY_AGAIN);
  394. }
  395. memcpy (res_msg, ipc_instance->response_buffer, res_len);
  396. return (CS_OK);
  397. }
  398. static cs_error_t
  399. reply_receive_in_buf (
  400. struct ipc_instance *ipc_instance,
  401. void **res_msg)
  402. {
  403. struct sembuf sop;
  404. int res;
  405. /*
  406. * Wait for semaphore #1 indicating a new message from server
  407. * to client in the response queue
  408. */
  409. sop.sem_num = 1;
  410. sop.sem_op = -1;
  411. sop.sem_flg = 0;
  412. retry_semop:
  413. res = semop (ipc_instance->semid, &sop, 1);
  414. if (res == -1 && errno == EINTR) {
  415. goto retry_semop;
  416. } else
  417. if (res == -1 && errno == EACCES) {
  418. priv_change_send (ipc_instance);
  419. goto retry_semop;
  420. } else
  421. if (res == -1) {
  422. return (CS_ERR_LIBRARY);
  423. }
  424. *res_msg = (char *)ipc_instance->response_buffer;
  425. return (CS_OK);
  426. }
  427. /*
  428. * External API
  429. */
  430. cs_error_t
  431. coroipcc_service_connect (
  432. const char *socket_name,
  433. unsigned int service,
  434. size_t request_size,
  435. size_t response_size,
  436. size_t dispatch_size,
  437. hdb_handle_t *handle)
  438. {
  439. int request_fd;
  440. struct sockaddr_un address;
  441. cs_error_t res;
  442. struct ipc_instance *ipc_instance;
  443. key_t semkey = 0;
  444. int sys_res;
  445. mar_req_setup_t req_setup;
  446. mar_res_setup_t res_setup;
  447. union semun semun;
  448. char control_map_path[128];
  449. char request_map_path[128];
  450. char response_map_path[128];
  451. char dispatch_map_path[128];
  452. res = hdb_error_to_cs (hdb_handle_create (&ipc_hdb,
  453. sizeof (struct ipc_instance), handle));
  454. if (res != CS_OK) {
  455. return (res);
  456. }
  457. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, *handle, (void **)&ipc_instance));
  458. if (res != CS_OK) {
  459. return (res);
  460. }
  461. res_setup.error = CS_ERR_LIBRARY;
  462. #if defined(COROSYNC_SOLARIS)
  463. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  464. #else
  465. request_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  466. #endif
  467. if (request_fd == -1) {
  468. return (CS_ERR_LIBRARY);
  469. }
  470. memset (&address, 0, sizeof (struct sockaddr_un));
  471. address.sun_family = AF_UNIX;
  472. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  473. address.sun_len = SUN_LEN(&address);
  474. #endif
  475. #if defined(COROSYNC_LINUX)
  476. sprintf (address.sun_path + 1, "%s", socket_name);
  477. #else
  478. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  479. #endif
  480. sys_res = connect (request_fd, (struct sockaddr *)&address,
  481. COROSYNC_SUN_LEN(&address));
  482. if (sys_res == -1) {
  483. #ifdef DEBUG
  484. fprintf(stderr, "Coroipcc: Can't connect: %d : %s\n", errno, strerror(errno));
  485. #endif
  486. close (request_fd);
  487. return (CS_ERR_TRY_AGAIN);
  488. }
  489. /*
  490. * Allocate a semaphore segment
  491. */
  492. while (1) {
  493. semkey = random();
  494. ipc_instance->euid = geteuid ();
  495. if ((ipc_instance->semid
  496. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  497. break;
  498. }
  499. if (errno != EEXIST) {
  500. goto res_exit;
  501. }
  502. }
  503. semun.val = 0;
  504. res = semctl (ipc_instance->semid, 0, SETVAL, semun);
  505. if (res != 0) {
  506. goto res_exit;
  507. }
  508. res = semctl (ipc_instance->semid, 1, SETVAL, semun);
  509. if (res != 0) {
  510. goto res_exit;
  511. }
  512. res = memory_map (
  513. control_map_path,
  514. "control_buffer-XXXXXX",
  515. (void *)&ipc_instance->control_buffer,
  516. 8192);
  517. res = memory_map (
  518. request_map_path,
  519. "request_buffer-XXXXXX",
  520. (void *)&ipc_instance->request_buffer,
  521. request_size);
  522. res = memory_map (
  523. response_map_path,
  524. "response_buffer-XXXXXX",
  525. (void *)&ipc_instance->response_buffer,
  526. response_size);
  527. res = circular_memory_map (
  528. dispatch_map_path,
  529. "dispatch_buffer-XXXXXX",
  530. (void *)&ipc_instance->dispatch_buffer,
  531. dispatch_size);
  532. /*
  533. * Initialize IPC setup message
  534. */
  535. req_setup.service = service;
  536. strcpy (req_setup.control_file, control_map_path);
  537. strcpy (req_setup.request_file, request_map_path);
  538. strcpy (req_setup.response_file, response_map_path);
  539. strcpy (req_setup.dispatch_file, dispatch_map_path);
  540. req_setup.control_size = 8192;
  541. req_setup.request_size = request_size;
  542. req_setup.response_size = response_size;
  543. req_setup.dispatch_size = dispatch_size;
  544. req_setup.semkey = semkey;
  545. res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  546. if (res != CS_OK) {
  547. goto res_exit;
  548. }
  549. res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  550. if (res != CS_OK) {
  551. goto res_exit;
  552. }
  553. ipc_instance->fd = request_fd;
  554. ipc_instance->flow_control_state = 0;
  555. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  556. goto res_exit;
  557. }
  558. ipc_instance->control_size = 8192;
  559. ipc_instance->request_size = request_size;
  560. ipc_instance->response_size = response_size;
  561. ipc_instance->dispatch_size = dispatch_size;
  562. pthread_mutex_init (&ipc_instance->mutex, NULL);
  563. hdb_handle_put (&ipc_hdb, *handle);
  564. return (res_setup.error);
  565. res_exit:
  566. close (request_fd);
  567. if (ipc_instance->semid > 0)
  568. semctl (ipc_instance->semid, 0, IPC_RMID);
  569. return (res_setup.error);
  570. }
  571. cs_error_t
  572. coroipcc_service_disconnect (
  573. hdb_handle_t handle)
  574. {
  575. cs_error_t res;
  576. struct ipc_instance *ipc_instance;
  577. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  578. if (res != CS_OK) {
  579. return (res);
  580. }
  581. shutdown (ipc_instance->fd, SHUT_RDWR);
  582. close (ipc_instance->fd);
  583. hdb_handle_destroy (&ipc_hdb, handle);
  584. hdb_handle_put (&ipc_hdb, handle);
  585. return (CS_OK);
  586. }
  587. cs_error_t
  588. coroipcc_dispatch_flow_control_get (
  589. hdb_handle_t handle,
  590. unsigned int *flow_control_state)
  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. *flow_control_state = ipc_instance->flow_control_state;
  599. hdb_handle_put (&ipc_hdb, handle);
  600. return (res);
  601. }
  602. cs_error_t
  603. coroipcc_fd_get (
  604. hdb_handle_t handle,
  605. int *fd)
  606. {
  607. struct ipc_instance *ipc_instance;
  608. cs_error_t res;
  609. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  610. if (res != CS_OK) {
  611. return (res);
  612. }
  613. *fd = ipc_instance->fd;
  614. hdb_handle_put (&ipc_hdb, handle);
  615. return (res);
  616. }
  617. cs_error_t
  618. coroipcc_dispatch_get (
  619. hdb_handle_t handle,
  620. void **data,
  621. int timeout)
  622. {
  623. struct pollfd ufds;
  624. int poll_events;
  625. char buf;
  626. struct ipc_instance *ipc_instance;
  627. int res;
  628. char buf_two = 1;
  629. char *data_addr;
  630. cs_error_t error = CS_OK;
  631. error = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  632. if (error != CS_OK) {
  633. return (error);
  634. }
  635. *data = NULL;
  636. ufds.fd = ipc_instance->fd;
  637. ufds.events = POLLIN;
  638. ufds.revents = 0;
  639. retry_poll:
  640. poll_events = poll (&ufds, 1, timeout);
  641. if (poll_events == -1 && errno == EINTR) {
  642. goto retry_poll;
  643. } else
  644. if (poll_events == -1) {
  645. goto error_put;
  646. } else
  647. if (poll_events == 0) {
  648. goto error_put;
  649. }
  650. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  651. error = CS_ERR_LIBRARY;
  652. goto error_put;
  653. }
  654. retry_recv:
  655. res = recv (ipc_instance->fd, &buf, 1, 0);
  656. if (res == -1 && errno == EINTR) {
  657. goto retry_recv;
  658. } else
  659. if (res == -1) {
  660. goto error_put;
  661. }
  662. if (res == 0) {
  663. goto error_put;
  664. }
  665. ipc_instance->flow_control_state = 0;
  666. if (buf == 1 || buf == 2) {
  667. ipc_instance->flow_control_state = 1;
  668. }
  669. /*
  670. * Notify executive to flush any pending dispatch messages
  671. */
  672. if (ipc_instance->flow_control_state) {
  673. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  674. res = socket_send (ipc_instance->fd, &buf_two, 1);
  675. assert (res == CS_OK); /* TODO */
  676. }
  677. /*
  678. * This is just a notification of flow control starting at the addition
  679. * of a new pending message, not a message to dispatch
  680. */
  681. if (buf == 2) {
  682. goto error_put;
  683. }
  684. if (buf == 3) {
  685. goto error_put;
  686. }
  687. data_addr = ipc_instance->dispatch_buffer;
  688. data_addr = &data_addr[ipc_instance->control_buffer->read];
  689. *data = (void *)data_addr;
  690. return (CS_OK);
  691. error_put:
  692. hdb_handle_put (&ipc_hdb, handle);
  693. return (error);
  694. }
  695. cs_error_t
  696. coroipcc_dispatch_put (hdb_handle_t handle)
  697. {
  698. struct sembuf sop;
  699. coroipc_response_header_t *header;
  700. struct ipc_instance *ipc_instance;
  701. int res;
  702. char *addr;
  703. unsigned int read_idx;
  704. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  705. if (res != CS_OK) {
  706. return (res);
  707. }
  708. sop.sem_num = 2;
  709. sop.sem_op = -1;
  710. sop.sem_flg = 0;
  711. retry_semop:
  712. res = semop (ipc_instance->semid, &sop, 1);
  713. if (res == -1 && errno == EINTR) {
  714. goto retry_semop;
  715. } else
  716. if (res == -1 && errno == EACCES) {
  717. priv_change_send (ipc_instance);
  718. goto retry_semop;
  719. } else
  720. if (res == -1) {
  721. return (CS_ERR_LIBRARY);
  722. }
  723. addr = ipc_instance->dispatch_buffer;
  724. read_idx = ipc_instance->control_buffer->read;
  725. header = (coroipc_response_header_t *) &addr[read_idx];
  726. ipc_instance->control_buffer->read =
  727. (read_idx + header->size) % ipc_instance->dispatch_size;
  728. /*
  729. * Put from dispatch get and also from this call's get
  730. */
  731. hdb_handle_put (&ipc_hdb, handle);
  732. hdb_handle_put (&ipc_hdb, handle);
  733. return (CS_OK);
  734. }
  735. cs_error_t
  736. coroipcc_msg_send_reply_receive (
  737. hdb_handle_t handle,
  738. const struct iovec *iov,
  739. unsigned int iov_len,
  740. void *res_msg,
  741. size_t res_len)
  742. {
  743. cs_error_t res;
  744. struct ipc_instance *ipc_instance;
  745. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  746. if (res != CS_OK) {
  747. return (res);
  748. }
  749. pthread_mutex_lock (&ipc_instance->mutex);
  750. res = msg_send (ipc_instance, iov, iov_len);
  751. if (res != CS_OK) {
  752. goto error_exit;
  753. }
  754. res = reply_receive (ipc_instance, res_msg, res_len);
  755. error_exit:
  756. hdb_handle_put (&ipc_hdb, handle);
  757. pthread_mutex_unlock (&ipc_instance->mutex);
  758. return (res);
  759. }
  760. cs_error_t
  761. coroipcc_msg_send_reply_receive_in_buf_get (
  762. hdb_handle_t handle,
  763. const struct iovec *iov,
  764. unsigned int iov_len,
  765. void **res_msg)
  766. {
  767. unsigned int res;
  768. struct ipc_instance *ipc_instance;
  769. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  770. if (res != CS_OK) {
  771. return (res);
  772. }
  773. pthread_mutex_lock (&ipc_instance->mutex);
  774. res = msg_send (ipc_instance, iov, iov_len);
  775. if (res != CS_OK) {
  776. goto error_exit;
  777. }
  778. res = reply_receive_in_buf (ipc_instance, res_msg);
  779. error_exit:
  780. pthread_mutex_unlock (&ipc_instance->mutex);
  781. return (res);
  782. }
  783. cs_error_t
  784. coroipcc_msg_send_reply_receive_in_buf_put (
  785. hdb_handle_t handle)
  786. {
  787. unsigned int res;
  788. struct ipc_instance *ipc_instance;
  789. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  790. if (res != CS_OK) {
  791. return (res);
  792. }
  793. hdb_handle_put (&ipc_hdb, handle);
  794. hdb_handle_put (&ipc_hdb, handle);
  795. return (res);
  796. }
  797. cs_error_t
  798. coroipcc_zcb_alloc (
  799. hdb_handle_t handle,
  800. void **buffer,
  801. size_t size,
  802. size_t header_size)
  803. {
  804. struct ipc_instance *ipc_instance;
  805. void *buf = NULL;
  806. char path[128];
  807. unsigned int res;
  808. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  809. coroipc_response_header_t res_coroipcs_zc_alloc;
  810. size_t map_size;
  811. struct iovec iovec;
  812. struct coroipcs_zc_header *hdr;
  813. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  814. if (res != CS_OK) {
  815. return (res);
  816. }
  817. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  818. res = memory_map (path, "corosync_zerocopy-XXXXXX", &buf, size);
  819. assert (res != -1);
  820. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  821. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  822. req_coroipcc_zc_alloc.map_size = map_size;
  823. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  824. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  825. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  826. res = coroipcc_msg_send_reply_receive (
  827. handle,
  828. &iovec,
  829. 1,
  830. &res_coroipcs_zc_alloc,
  831. sizeof (coroipc_response_header_t));
  832. hdr = (struct coroipcs_zc_header *)buf;
  833. hdr->map_size = map_size;
  834. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  835. hdb_handle_put (&ipc_hdb, handle);
  836. return (res);
  837. }
  838. cs_error_t
  839. coroipcc_zcb_free (
  840. hdb_handle_t handle,
  841. void *buffer)
  842. {
  843. struct ipc_instance *ipc_instance;
  844. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  845. coroipc_response_header_t res_coroipcs_zc_free;
  846. struct iovec iovec;
  847. unsigned int res;
  848. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  849. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  850. if (res != CS_OK) {
  851. return (res);
  852. }
  853. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  854. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  855. req_coroipcc_zc_free.map_size = header->map_size;
  856. req_coroipcc_zc_free.server_address = header->server_address;
  857. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  858. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  859. res = coroipcc_msg_send_reply_receive (
  860. handle,
  861. &iovec,
  862. 1,
  863. &res_coroipcs_zc_free,
  864. sizeof (coroipc_response_header_t));
  865. munmap ((void *)header, header->map_size);
  866. hdb_handle_put (&ipc_hdb, handle);
  867. return (res);
  868. }
  869. cs_error_t
  870. coroipcc_zcb_msg_send_reply_receive (
  871. hdb_handle_t handle,
  872. void *msg,
  873. void *res_msg,
  874. size_t res_len)
  875. {
  876. struct ipc_instance *ipc_instance;
  877. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  878. struct coroipcs_zc_header *hdr;
  879. struct iovec iovec;
  880. cs_error_t res;
  881. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  882. if (res != CS_OK) {
  883. return (res);
  884. }
  885. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  886. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  887. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  888. req_coroipcc_zc_execute.server_address = hdr->server_address;
  889. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  890. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  891. res = coroipcc_msg_send_reply_receive (
  892. handle,
  893. &iovec,
  894. 1,
  895. res_msg,
  896. res_len);
  897. hdb_handle_put (&ipc_hdb, handle);
  898. return (res);
  899. }