coroipcc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. request_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  463. if (request_fd == -1) {
  464. return (CS_ERR_LIBRARY);
  465. }
  466. memset (&address, 0, sizeof (struct sockaddr_un));
  467. address.sun_family = AF_UNIX;
  468. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  469. address.sun_len = SUN_LEN(&address);
  470. #endif
  471. #if defined(COROSYNC_LINUX)
  472. sprintf (address.sun_path + 1, "%s", socket_name);
  473. #else
  474. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  475. #endif
  476. sys_res = connect (request_fd, (struct sockaddr *)&address,
  477. COROSYNC_SUN_LEN(&address));
  478. if (sys_res == -1) {
  479. #ifdef DEBUG
  480. fprintf(stderr, "Coroipcc: Can't connect: %d : %s\n", errno, strerror(errno));
  481. #endif
  482. close (request_fd);
  483. return (CS_ERR_TRY_AGAIN);
  484. }
  485. /*
  486. * Allocate a semaphore segment
  487. */
  488. while (1) {
  489. semkey = random();
  490. ipc_instance->euid = geteuid ();
  491. if ((ipc_instance->semid
  492. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  493. break;
  494. }
  495. if (errno != EEXIST) {
  496. goto res_exit;
  497. }
  498. }
  499. semun.val = 0;
  500. res = semctl (ipc_instance->semid, 0, SETVAL, semun);
  501. if (res != 0) {
  502. goto res_exit;
  503. }
  504. res = semctl (ipc_instance->semid, 1, SETVAL, semun);
  505. if (res != 0) {
  506. goto res_exit;
  507. }
  508. res = memory_map (
  509. control_map_path,
  510. "control_buffer-XXXXXX",
  511. (void *)&ipc_instance->control_buffer,
  512. 8192);
  513. res = memory_map (
  514. request_map_path,
  515. "request_buffer-XXXXXX",
  516. (void *)&ipc_instance->request_buffer,
  517. request_size);
  518. res = memory_map (
  519. response_map_path,
  520. "response_buffer-XXXXXX",
  521. (void *)&ipc_instance->response_buffer,
  522. response_size);
  523. res = circular_memory_map (
  524. dispatch_map_path,
  525. "dispatch_buffer-XXXXXX",
  526. (void *)&ipc_instance->dispatch_buffer,
  527. dispatch_size);
  528. /*
  529. * Initialize IPC setup message
  530. */
  531. req_setup.service = service;
  532. strcpy (req_setup.control_file, control_map_path);
  533. strcpy (req_setup.request_file, request_map_path);
  534. strcpy (req_setup.response_file, response_map_path);
  535. strcpy (req_setup.dispatch_file, dispatch_map_path);
  536. req_setup.control_size = 8192;
  537. req_setup.request_size = request_size;
  538. req_setup.response_size = response_size;
  539. req_setup.dispatch_size = dispatch_size;
  540. req_setup.semkey = semkey;
  541. res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  542. if (res != CS_OK) {
  543. goto res_exit;
  544. }
  545. res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  546. if (res != CS_OK) {
  547. goto res_exit;
  548. }
  549. ipc_instance->fd = request_fd;
  550. ipc_instance->flow_control_state = 0;
  551. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  552. goto res_exit;
  553. }
  554. ipc_instance->control_size = 8192;
  555. ipc_instance->request_size = request_size;
  556. ipc_instance->response_size = response_size;
  557. ipc_instance->dispatch_size = dispatch_size;
  558. pthread_mutex_init (&ipc_instance->mutex, NULL);
  559. hdb_handle_put (&ipc_hdb, *handle);
  560. return (res_setup.error);
  561. res_exit:
  562. close (request_fd);
  563. if (ipc_instance->semid > 0)
  564. semctl (ipc_instance->semid, 0, IPC_RMID);
  565. return (res_setup.error);
  566. }
  567. cs_error_t
  568. coroipcc_service_disconnect (
  569. hdb_handle_t handle)
  570. {
  571. cs_error_t res;
  572. struct ipc_instance *ipc_instance;
  573. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  574. if (res != CS_OK) {
  575. return (res);
  576. }
  577. shutdown (ipc_instance->fd, SHUT_RDWR);
  578. close (ipc_instance->fd);
  579. hdb_handle_destroy (&ipc_hdb, handle);
  580. hdb_handle_put (&ipc_hdb, handle);
  581. return (CS_OK);
  582. }
  583. cs_error_t
  584. coroipcc_dispatch_flow_control_get (
  585. hdb_handle_t handle,
  586. unsigned int *flow_control_state)
  587. {
  588. struct ipc_instance *ipc_instance;
  589. cs_error_t res;
  590. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  591. if (res != CS_OK) {
  592. return (res);
  593. }
  594. *flow_control_state = ipc_instance->flow_control_state;
  595. hdb_handle_put (&ipc_hdb, handle);
  596. return (res);
  597. }
  598. cs_error_t
  599. coroipcc_fd_get (
  600. hdb_handle_t handle,
  601. int *fd)
  602. {
  603. struct ipc_instance *ipc_instance;
  604. cs_error_t res;
  605. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  606. if (res != CS_OK) {
  607. return (res);
  608. }
  609. *fd = ipc_instance->fd;
  610. hdb_handle_put (&ipc_hdb, handle);
  611. return (res);
  612. }
  613. cs_error_t
  614. coroipcc_dispatch_get (
  615. hdb_handle_t handle,
  616. void **data,
  617. int timeout)
  618. {
  619. struct pollfd ufds;
  620. int poll_events;
  621. char buf;
  622. struct ipc_instance *ipc_instance;
  623. int res;
  624. char buf_two = 1;
  625. char *data_addr;
  626. cs_error_t error = CS_OK;
  627. error = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  628. if (error != CS_OK) {
  629. return (error);
  630. }
  631. *data = NULL;
  632. ufds.fd = ipc_instance->fd;
  633. ufds.events = POLLIN;
  634. ufds.revents = 0;
  635. retry_poll:
  636. poll_events = poll (&ufds, 1, timeout);
  637. if (poll_events == -1 && errno == EINTR) {
  638. goto retry_poll;
  639. } else
  640. if (poll_events == -1) {
  641. goto error_put;
  642. } else
  643. if (poll_events == 0) {
  644. goto error_put;
  645. }
  646. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  647. error = CS_ERR_LIBRARY;
  648. goto error_put;
  649. }
  650. retry_recv:
  651. res = recv (ipc_instance->fd, &buf, 1, 0);
  652. if (res == -1 && errno == EINTR) {
  653. goto retry_recv;
  654. } else
  655. if (res == -1) {
  656. goto error_put;
  657. }
  658. if (res == 0) {
  659. goto error_put;
  660. }
  661. ipc_instance->flow_control_state = 0;
  662. if (buf == 1 || buf == 2) {
  663. ipc_instance->flow_control_state = 1;
  664. }
  665. /*
  666. * Notify executive to flush any pending dispatch messages
  667. */
  668. if (ipc_instance->flow_control_state) {
  669. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  670. res = socket_send (ipc_instance->fd, &buf_two, 1);
  671. assert (res == CS_OK); /* TODO */
  672. }
  673. /*
  674. * This is just a notification of flow control starting at the addition
  675. * of a new pending message, not a message to dispatch
  676. */
  677. if (buf == 2) {
  678. goto error_put;
  679. }
  680. if (buf == 3) {
  681. goto error_put;
  682. }
  683. data_addr = ipc_instance->dispatch_buffer;
  684. data_addr = &data_addr[ipc_instance->control_buffer->read];
  685. *data = (void *)data_addr;
  686. return (CS_OK);
  687. error_put:
  688. hdb_handle_put (&ipc_hdb, handle);
  689. return (error);
  690. }
  691. cs_error_t
  692. coroipcc_dispatch_put (hdb_handle_t handle)
  693. {
  694. struct sembuf sop;
  695. coroipc_response_header_t *header;
  696. struct ipc_instance *ipc_instance;
  697. int res;
  698. char *addr;
  699. unsigned int read_idx;
  700. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  701. if (res != CS_OK) {
  702. return (res);
  703. }
  704. sop.sem_num = 2;
  705. sop.sem_op = -1;
  706. sop.sem_flg = 0;
  707. retry_semop:
  708. res = semop (ipc_instance->semid, &sop, 1);
  709. if (res == -1 && errno == EINTR) {
  710. goto retry_semop;
  711. } else
  712. if (res == -1 && errno == EACCES) {
  713. priv_change_send (ipc_instance);
  714. goto retry_semop;
  715. } else
  716. if (res == -1) {
  717. return (CS_ERR_LIBRARY);
  718. }
  719. addr = ipc_instance->dispatch_buffer;
  720. read_idx = ipc_instance->control_buffer->read;
  721. header = (coroipc_response_header_t *) &addr[read_idx];
  722. ipc_instance->control_buffer->read =
  723. (read_idx + header->size) % ipc_instance->dispatch_size;
  724. /*
  725. * Put from dispatch get and also from this call's get
  726. */
  727. hdb_handle_put (&ipc_hdb, handle);
  728. hdb_handle_put (&ipc_hdb, handle);
  729. return (CS_OK);
  730. }
  731. cs_error_t
  732. coroipcc_msg_send_reply_receive (
  733. hdb_handle_t handle,
  734. const struct iovec *iov,
  735. unsigned int iov_len,
  736. void *res_msg,
  737. size_t res_len)
  738. {
  739. cs_error_t res;
  740. struct ipc_instance *ipc_instance;
  741. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  742. if (res != CS_OK) {
  743. return (res);
  744. }
  745. pthread_mutex_lock (&ipc_instance->mutex);
  746. res = msg_send (ipc_instance, iov, iov_len);
  747. if (res != CS_OK) {
  748. goto error_exit;
  749. }
  750. res = reply_receive (ipc_instance, res_msg, res_len);
  751. error_exit:
  752. hdb_handle_put (&ipc_hdb, handle);
  753. pthread_mutex_unlock (&ipc_instance->mutex);
  754. return (res);
  755. }
  756. cs_error_t
  757. coroipcc_msg_send_reply_receive_in_buf_get (
  758. hdb_handle_t handle,
  759. const struct iovec *iov,
  760. unsigned int iov_len,
  761. void **res_msg)
  762. {
  763. unsigned int res;
  764. struct ipc_instance *ipc_instance;
  765. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  766. if (res != CS_OK) {
  767. return (res);
  768. }
  769. pthread_mutex_lock (&ipc_instance->mutex);
  770. res = msg_send (ipc_instance, iov, iov_len);
  771. if (res != CS_OK) {
  772. goto error_exit;
  773. }
  774. res = reply_receive_in_buf (ipc_instance, res_msg);
  775. error_exit:
  776. pthread_mutex_unlock (&ipc_instance->mutex);
  777. return (res);
  778. }
  779. cs_error_t
  780. coroipcc_msg_send_reply_receive_in_buf_put (
  781. hdb_handle_t handle)
  782. {
  783. unsigned int res;
  784. struct ipc_instance *ipc_instance;
  785. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  786. if (res != CS_OK) {
  787. return (res);
  788. }
  789. hdb_handle_put (&ipc_hdb, handle);
  790. hdb_handle_put (&ipc_hdb, handle);
  791. return (res);
  792. }
  793. cs_error_t
  794. coroipcc_zcb_alloc (
  795. hdb_handle_t handle,
  796. void **buffer,
  797. size_t size,
  798. size_t header_size)
  799. {
  800. struct ipc_instance *ipc_instance;
  801. void *buf = NULL;
  802. char path[128];
  803. unsigned int res;
  804. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  805. coroipc_response_header_t res_coroipcs_zc_alloc;
  806. size_t map_size;
  807. struct iovec iovec;
  808. struct coroipcs_zc_header *hdr;
  809. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  810. if (res != CS_OK) {
  811. return (res);
  812. }
  813. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  814. res = memory_map (path, "corosync_zerocopy-XXXXXX", &buf, size);
  815. assert (res != -1);
  816. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  817. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  818. req_coroipcc_zc_alloc.map_size = map_size;
  819. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  820. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  821. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  822. res = coroipcc_msg_send_reply_receive (
  823. handle,
  824. &iovec,
  825. 1,
  826. &res_coroipcs_zc_alloc,
  827. sizeof (coroipc_response_header_t));
  828. hdr = (struct coroipcs_zc_header *)buf;
  829. hdr->map_size = map_size;
  830. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  831. hdb_handle_put (&ipc_hdb, handle);
  832. return (res);
  833. }
  834. cs_error_t
  835. coroipcc_zcb_free (
  836. hdb_handle_t handle,
  837. void *buffer)
  838. {
  839. struct ipc_instance *ipc_instance;
  840. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  841. coroipc_response_header_t res_coroipcs_zc_free;
  842. struct iovec iovec;
  843. unsigned int res;
  844. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  845. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  846. if (res != CS_OK) {
  847. return (res);
  848. }
  849. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  850. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  851. req_coroipcc_zc_free.map_size = header->map_size;
  852. req_coroipcc_zc_free.server_address = header->server_address;
  853. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  854. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  855. res = coroipcc_msg_send_reply_receive (
  856. handle,
  857. &iovec,
  858. 1,
  859. &res_coroipcs_zc_free,
  860. sizeof (coroipc_response_header_t));
  861. munmap ((void *)header, header->map_size);
  862. hdb_handle_put (&ipc_hdb, handle);
  863. return (res);
  864. }
  865. cs_error_t
  866. coroipcc_zcb_msg_send_reply_receive (
  867. hdb_handle_t handle,
  868. void *msg,
  869. void *res_msg,
  870. size_t res_len)
  871. {
  872. struct ipc_instance *ipc_instance;
  873. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  874. struct coroipcs_zc_header *hdr;
  875. struct iovec iovec;
  876. cs_error_t res;
  877. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  878. if (res != CS_OK) {
  879. return (res);
  880. }
  881. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  882. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  883. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  884. req_coroipcc_zc_execute.server_address = hdr->server_address;
  885. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  886. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  887. res = coroipcc_msg_send_reply_receive (
  888. handle,
  889. &iovec,
  890. 1,
  891. res_msg,
  892. res_len);
  893. hdb_handle_put (&ipc_hdb, handle);
  894. return (res);
  895. }