coroipcc.c 23 KB

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