coroipcc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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/mman.h>
  57. #include <corosync/corotypes.h>
  58. #include <corosync/coroipc_types.h>
  59. #include <corosync/coroipc_ipc.h>
  60. #include <corosync/coroipcc.h>
  61. #include <corosync/hdb.h>
  62. #if _POSIX_THREAD_PROCESS_SHARED > 0
  63. #include <semaphore.h>
  64. #else
  65. #include <sys/sem.h>
  66. #endif
  67. #include "util.h"
  68. /*
  69. * Define sem_wait timeout (real timeout will be (n-1;n) )
  70. */
  71. #define IPC_SEMWAIT_TIMEOUT 2
  72. struct ipc_instance {
  73. int fd;
  74. #if _POSIX_THREAD_PROCESS_SHARED < 1
  75. int semid;
  76. #endif
  77. int flow_control_state;
  78. struct control_buffer *control_buffer;
  79. char *request_buffer;
  80. char *response_buffer;
  81. char *dispatch_buffer;
  82. size_t control_size;
  83. size_t request_size;
  84. size_t response_size;
  85. size_t dispatch_size;
  86. uid_t euid;
  87. pthread_mutex_t mutex;
  88. };
  89. void ipc_hdb_destructor (void *context);
  90. DECLARE_HDB_DATABASE(ipc_hdb,ipc_hdb_destructor);
  91. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  92. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  93. #else
  94. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  95. #endif
  96. #ifdef SO_NOSIGPIPE
  97. static void socket_nosigpipe(int s)
  98. {
  99. int on = 1;
  100. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  101. }
  102. #endif
  103. #ifndef MSG_NOSIGNAL
  104. #define MSG_NOSIGNAL 0
  105. #endif
  106. static cs_error_t
  107. socket_send (
  108. int s,
  109. void *msg,
  110. size_t len)
  111. {
  112. cs_error_t res = CS_OK;
  113. int result;
  114. struct msghdr msg_send;
  115. struct iovec iov_send;
  116. char *rbuf = msg;
  117. int processed = 0;
  118. msg_send.msg_iov = &iov_send;
  119. msg_send.msg_iovlen = 1;
  120. msg_send.msg_name = 0;
  121. msg_send.msg_namelen = 0;
  122. #if !defined(COROSYNC_SOLARIS)
  123. msg_send.msg_control = 0;
  124. msg_send.msg_controllen = 0;
  125. msg_send.msg_flags = 0;
  126. #else
  127. msg_send.msg_accrights = NULL;
  128. msg_send.msg_accrightslen = 0;
  129. #endif
  130. retry_send:
  131. iov_send.iov_base = &rbuf[processed];
  132. iov_send.iov_len = len - processed;
  133. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  134. if (result == -1) {
  135. switch (errno) {
  136. case EINTR:
  137. res = CS_ERR_TRY_AGAIN;
  138. goto res_exit;
  139. case EAGAIN:
  140. goto retry_send;
  141. break;
  142. default:
  143. res = CS_ERR_LIBRARY;
  144. goto res_exit;
  145. }
  146. }
  147. processed += result;
  148. if (processed != len) {
  149. goto retry_send;
  150. }
  151. return (CS_OK);
  152. res_exit:
  153. return (res);
  154. }
  155. static cs_error_t
  156. socket_recv (
  157. int s,
  158. void *msg,
  159. size_t len)
  160. {
  161. cs_error_t res = CS_OK;
  162. int result;
  163. struct msghdr msg_recv;
  164. struct iovec iov_recv;
  165. char *rbuf = msg;
  166. int processed = 0;
  167. msg_recv.msg_iov = &iov_recv;
  168. msg_recv.msg_iovlen = 1;
  169. msg_recv.msg_name = 0;
  170. msg_recv.msg_namelen = 0;
  171. #if !defined (COROSYNC_SOLARIS)
  172. msg_recv.msg_control = 0;
  173. msg_recv.msg_controllen = 0;
  174. msg_recv.msg_flags = 0;
  175. #else
  176. msg_recv.msg_accrights = NULL;
  177. msg_recv.msg_accrightslen = 0;
  178. #endif
  179. retry_recv:
  180. iov_recv.iov_base = (void *)&rbuf[processed];
  181. iov_recv.iov_len = len - processed;
  182. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL|MSG_WAITALL);
  183. if (result == -1) {
  184. switch (errno) {
  185. case EINTR:
  186. res = CS_ERR_TRY_AGAIN;
  187. goto res_exit;
  188. case EAGAIN:
  189. goto retry_recv;
  190. break;
  191. default:
  192. res = CS_ERR_LIBRARY;
  193. goto res_exit;
  194. }
  195. }
  196. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  197. /* On many OS poll never return POLLHUP or POLLERR.
  198. * EOF is detected when recvmsg return 0.
  199. */
  200. if (result == 0) {
  201. res = CS_ERR_LIBRARY;
  202. goto res_exit;
  203. }
  204. #endif
  205. processed += result;
  206. if (processed != len) {
  207. goto retry_recv;
  208. }
  209. assert (processed == len);
  210. res_exit:
  211. return (res);
  212. }
  213. #if _POSIX_THREAD_PROCESS_SHARED < 1
  214. static int
  215. priv_change_send (struct ipc_instance *ipc_instance)
  216. {
  217. char buf_req;
  218. mar_req_priv_change req_priv_change;
  219. unsigned int res;
  220. req_priv_change.euid = geteuid();
  221. /*
  222. * Don't resend request unless euid has changed
  223. */
  224. if (ipc_instance->euid == req_priv_change.euid) {
  225. return (0);
  226. }
  227. req_priv_change.egid = getegid();
  228. buf_req = MESSAGE_REQ_CHANGE_EUID;
  229. res = socket_send (ipc_instance->fd, &buf_req, 1);
  230. if (res == -1) {
  231. return (-1);
  232. }
  233. res = socket_send (ipc_instance->fd, &req_priv_change,
  234. sizeof (req_priv_change));
  235. if (res == -1) {
  236. return (-1);
  237. }
  238. ipc_instance->euid = req_priv_change.euid;
  239. return (0);
  240. }
  241. #if defined(_SEM_SEMUN_UNDEFINED)
  242. union semun {
  243. int val;
  244. struct semid_ds *buf;
  245. unsigned short int *array;
  246. struct seminfo *__buf;
  247. };
  248. #endif
  249. #endif
  250. static int
  251. circular_memory_map (char *path, const char *file, void **buf, size_t bytes)
  252. {
  253. int fd;
  254. void *addr_orig;
  255. void *addr;
  256. int res;
  257. sprintf (path, "/dev/shm/%s", file);
  258. fd = mkstemp (path);
  259. if (fd == -1) {
  260. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  261. fd = mkstemp (path);
  262. if (fd == -1) {
  263. return (-1);
  264. }
  265. }
  266. res = ftruncate (fd, bytes);
  267. if (res == -1) {
  268. close (fd);
  269. return (-1);
  270. }
  271. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  272. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  273. if (addr_orig == MAP_FAILED) {
  274. close (fd);
  275. return (-1);
  276. }
  277. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  278. MAP_FIXED | MAP_SHARED, fd, 0);
  279. if (addr != addr_orig) {
  280. close (fd);
  281. return (-1);
  282. }
  283. #ifdef COROSYNC_BSD
  284. madvise(addr_orig, bytes, MADV_NOSYNC);
  285. #endif
  286. addr = mmap (((char *)addr_orig) + bytes,
  287. bytes, PROT_READ | PROT_WRITE,
  288. MAP_FIXED | MAP_SHARED, fd, 0);
  289. if (addr == MAP_FAILED) {
  290. close (fd);
  291. return (-1);
  292. }
  293. #ifdef COROSYNC_BSD
  294. madvise(((char *)addr_orig) + bytes, bytes, MADV_NOSYNC);
  295. #endif
  296. res = close (fd);
  297. if (res) {
  298. return (-1);
  299. }
  300. *buf = addr_orig;
  301. return (0);
  302. }
  303. static void
  304. memory_unmap (void *addr, size_t bytes)
  305. {
  306. int res;
  307. res = munmap (addr, bytes);
  308. }
  309. void ipc_hdb_destructor (void *context ) {
  310. struct ipc_instance *ipc_instance = (struct ipc_instance *)context;
  311. /*
  312. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  313. */
  314. memory_unmap (ipc_instance->control_buffer, ipc_instance->control_size);
  315. memory_unmap (ipc_instance->request_buffer, ipc_instance->request_size);
  316. memory_unmap (ipc_instance->response_buffer, ipc_instance->response_size);
  317. memory_unmap (ipc_instance->dispatch_buffer, (ipc_instance->dispatch_size) << 1);
  318. }
  319. static int
  320. memory_map (char *path, const char *file, void **buf, size_t bytes)
  321. {
  322. int fd;
  323. void *addr_orig;
  324. void *addr;
  325. int res;
  326. sprintf (path, "/dev/shm/%s", file);
  327. fd = mkstemp (path);
  328. if (fd == -1) {
  329. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  330. fd = mkstemp (path);
  331. if (fd == -1) {
  332. return (-1);
  333. }
  334. }
  335. res = ftruncate (fd, bytes);
  336. if (res == -1) {
  337. close (fd);
  338. return (-1);
  339. }
  340. addr_orig = mmap (NULL, bytes, PROT_NONE,
  341. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  342. if (addr_orig == MAP_FAILED) {
  343. close (fd);
  344. return (-1);
  345. }
  346. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  347. MAP_FIXED | MAP_SHARED, fd, 0);
  348. if (addr != addr_orig) {
  349. close (fd);
  350. return (-1);
  351. }
  352. #ifdef COROSYNC_BSD
  353. madvise(addr_orig, bytes, MADV_NOSYNC);
  354. #endif
  355. res = close (fd);
  356. if (res) {
  357. return (-1);
  358. }
  359. *buf = addr_orig;
  360. return (0);
  361. }
  362. static cs_error_t
  363. msg_send (
  364. struct ipc_instance *ipc_instance,
  365. const struct iovec *iov,
  366. unsigned int iov_len)
  367. {
  368. #if _POSIX_THREAD_PROCESS_SHARED < 1
  369. struct sembuf sop;
  370. #endif
  371. int i;
  372. int res;
  373. int req_buffer_idx = 0;
  374. for (i = 0; i < iov_len; i++) {
  375. if ((req_buffer_idx + iov[i].iov_len) >
  376. ipc_instance->request_size) {
  377. return (CS_ERR_INVALID_PARAM);
  378. }
  379. memcpy (&ipc_instance->request_buffer[req_buffer_idx],
  380. iov[i].iov_base,
  381. iov[i].iov_len);
  382. req_buffer_idx += iov[i].iov_len;
  383. }
  384. #if _POSIX_THREAD_PROCESS_SHARED > 0
  385. res = sem_post (&ipc_instance->control_buffer->sem0);
  386. if (res == -1) {
  387. return (CS_ERR_LIBRARY);
  388. }
  389. #else
  390. /*
  391. * Signal semaphore #0 indicting a new message from client
  392. * to server request queue
  393. */
  394. sop.sem_num = 0;
  395. sop.sem_op = 1;
  396. sop.sem_flg = 0;
  397. retry_semop:
  398. res = semop (ipc_instance->semid, &sop, 1);
  399. if (res == -1 && errno == EINTR) {
  400. return (CS_ERR_TRY_AGAIN);
  401. } else
  402. if (res == -1 && errno == EACCES) {
  403. priv_change_send (ipc_instance);
  404. goto retry_semop;
  405. } else
  406. if (res == -1) {
  407. return (CS_ERR_LIBRARY);
  408. }
  409. #endif
  410. return (CS_OK);
  411. }
  412. inline static cs_error_t
  413. ipc_sem_wait (
  414. struct ipc_instance *ipc_instance,
  415. int sem_num)
  416. {
  417. #if _POSIX_THREAD_PROCESS_SHARED < 1
  418. struct sembuf sop;
  419. #else
  420. struct timespec timeout;
  421. struct pollfd pfd;
  422. sem_t *sem;
  423. #endif
  424. int res;
  425. #if _POSIX_THREAD_PROCESS_SHARED > 0
  426. switch (sem_num) {
  427. case 0:
  428. sem = &ipc_instance->control_buffer->sem0;
  429. break;
  430. case 1:
  431. sem = &ipc_instance->control_buffer->sem1;
  432. break;
  433. case 2:
  434. sem = &ipc_instance->control_buffer->sem2;
  435. break;
  436. }
  437. retry_semwait:
  438. timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
  439. timeout.tv_nsec = 0;
  440. res = sem_timedwait (sem, &timeout);
  441. if (res == -1 && errno == ETIMEDOUT) {
  442. pfd.fd = ipc_instance->fd;
  443. pfd.events = 0;
  444. res = poll (&pfd, 1, 0);
  445. if (res == -1 && errno == EINTR) {
  446. return (CS_ERR_TRY_AGAIN);
  447. } else
  448. if (res == -1) {
  449. return (CS_ERR_LIBRARY);
  450. }
  451. if (res == 1) {
  452. if (pfd.revents == POLLERR || pfd.revents == POLLHUP || pfd.revents == POLLNVAL) {
  453. return (CS_ERR_LIBRARY);
  454. }
  455. }
  456. goto retry_semwait;
  457. } else
  458. if (res == -1 && errno == EINTR) {
  459. return (CS_ERR_TRY_AGAIN);
  460. } else
  461. if (res == -1) {
  462. return (CS_ERR_LIBRARY);
  463. }
  464. #else
  465. /*
  466. * Wait for semaphore indicating a new message from server
  467. * to client in queue
  468. */
  469. sop.sem_num = sem_num;
  470. sop.sem_op = -1;
  471. sop.sem_flg = 0;
  472. retry_semop:
  473. res = semop (ipc_instance->semid, &sop, 1);
  474. if (res == -1 && errno == EINTR) {
  475. return (CS_ERR_TRY_AGAIN);
  476. } else
  477. if (res == -1 && errno == EACCES) {
  478. priv_change_send (ipc_instance);
  479. goto retry_semop;
  480. } else
  481. if (res == -1) {
  482. return (CS_ERR_LIBRARY);
  483. }
  484. #endif
  485. return (CS_OK);
  486. }
  487. static cs_error_t
  488. reply_receive (
  489. struct ipc_instance *ipc_instance,
  490. void *res_msg,
  491. size_t res_len)
  492. {
  493. coroipc_response_header_t *response_header;
  494. cs_error_t err;
  495. if ((err = ipc_sem_wait (ipc_instance, 1)) != CS_OK) {
  496. return (err);
  497. }
  498. response_header = (coroipc_response_header_t *)ipc_instance->response_buffer;
  499. if (response_header->error == CS_ERR_TRY_AGAIN) {
  500. return (CS_ERR_TRY_AGAIN);
  501. }
  502. memcpy (res_msg, ipc_instance->response_buffer, res_len);
  503. return (CS_OK);
  504. }
  505. static cs_error_t
  506. reply_receive_in_buf (
  507. struct ipc_instance *ipc_instance,
  508. void **res_msg)
  509. {
  510. cs_error_t err;
  511. if ((err = ipc_sem_wait (ipc_instance, 1)) != CS_OK) {
  512. return (err);
  513. }
  514. *res_msg = (char *)ipc_instance->response_buffer;
  515. return (CS_OK);
  516. }
  517. /*
  518. * External API
  519. */
  520. cs_error_t
  521. coroipcc_service_connect (
  522. const char *socket_name,
  523. unsigned int service,
  524. size_t request_size,
  525. size_t response_size,
  526. size_t dispatch_size,
  527. hdb_handle_t *handle)
  528. {
  529. int request_fd;
  530. struct sockaddr_un address;
  531. cs_error_t res;
  532. struct ipc_instance *ipc_instance;
  533. #if _POSIX_THREAD_PROCESS_SHARED < 1
  534. key_t semkey = 0;
  535. union semun semun;
  536. #endif
  537. int sys_res;
  538. mar_req_setup_t req_setup;
  539. mar_res_setup_t res_setup;
  540. char control_map_path[128];
  541. char request_map_path[128];
  542. char response_map_path[128];
  543. char dispatch_map_path[128];
  544. res = hdb_error_to_cs (hdb_handle_create (&ipc_hdb,
  545. sizeof (struct ipc_instance), handle));
  546. if (res != CS_OK) {
  547. return (res);
  548. }
  549. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, *handle, (void **)&ipc_instance));
  550. if (res != CS_OK) {
  551. return (res);
  552. }
  553. res_setup.error = CS_ERR_LIBRARY;
  554. #if defined(COROSYNC_SOLARIS)
  555. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  556. #else
  557. request_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  558. #endif
  559. if (request_fd == -1) {
  560. return (CS_ERR_LIBRARY);
  561. }
  562. #ifdef SO_NOSIGPIPE
  563. socket_nosigpipe (request_fd);
  564. #endif
  565. memset (&address, 0, sizeof (struct sockaddr_un));
  566. address.sun_family = AF_UNIX;
  567. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  568. address.sun_len = SUN_LEN(&address);
  569. #endif
  570. #if defined(COROSYNC_LINUX)
  571. sprintf (address.sun_path + 1, "%s", socket_name);
  572. #else
  573. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  574. #endif
  575. sys_res = connect (request_fd, (struct sockaddr *)&address,
  576. COROSYNC_SUN_LEN(&address));
  577. if (sys_res == -1) {
  578. res = CS_ERR_TRY_AGAIN;
  579. goto error_connect;
  580. }
  581. sys_res = memory_map (
  582. control_map_path,
  583. "control_buffer-XXXXXX",
  584. (void *)&ipc_instance->control_buffer,
  585. 8192);
  586. if (sys_res == -1) {
  587. res = CS_ERR_LIBRARY;
  588. goto error_connect;
  589. }
  590. sys_res = memory_map (
  591. request_map_path,
  592. "request_buffer-XXXXXX",
  593. (void *)&ipc_instance->request_buffer,
  594. request_size);
  595. if (sys_res == -1) {
  596. res = CS_ERR_LIBRARY;
  597. goto error_request_buffer;
  598. }
  599. sys_res = memory_map (
  600. response_map_path,
  601. "response_buffer-XXXXXX",
  602. (void *)&ipc_instance->response_buffer,
  603. response_size);
  604. if (sys_res == -1) {
  605. res = CS_ERR_LIBRARY;
  606. goto error_response_buffer;
  607. }
  608. sys_res = circular_memory_map (
  609. dispatch_map_path,
  610. "dispatch_buffer-XXXXXX",
  611. (void *)&ipc_instance->dispatch_buffer,
  612. dispatch_size);
  613. if (sys_res == -1) {
  614. res = CS_ERR_LIBRARY;
  615. goto error_dispatch_buffer;
  616. }
  617. #if _POSIX_THREAD_PROCESS_SHARED > 0
  618. sem_init (&ipc_instance->control_buffer->sem0, 1, 0);
  619. sem_init (&ipc_instance->control_buffer->sem1, 1, 0);
  620. sem_init (&ipc_instance->control_buffer->sem2, 1, 0);
  621. #else
  622. /*
  623. * Allocate a semaphore segment
  624. */
  625. while (1) {
  626. semkey = random();
  627. ipc_instance->euid = geteuid ();
  628. if ((ipc_instance->semid
  629. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  630. break;
  631. }
  632. /*
  633. * EACCESS can be returned as non root user when opening a different
  634. * users semaphore.
  635. *
  636. * EEXIST can happen when we are a root or nonroot user opening
  637. * an existing shared memory segment for which we have access
  638. */
  639. if (errno != EEXIST && errno != EACCES) {
  640. res = CS_ERR_LIBRARY;
  641. goto error_exit;
  642. }
  643. }
  644. semun.val = 0;
  645. sys_res = semctl (ipc_instance->semid, 0, SETVAL, semun);
  646. if (sys_res != 0) {
  647. res = CS_ERR_LIBRARY;
  648. goto error_exit;
  649. }
  650. sys_res = semctl (ipc_instance->semid, 1, SETVAL, semun);
  651. if (sys_res != 0) {
  652. res = CS_ERR_LIBRARY;
  653. goto error_exit;
  654. }
  655. #endif
  656. /*
  657. * Initialize IPC setup message
  658. */
  659. req_setup.service = service;
  660. strcpy (req_setup.control_file, control_map_path);
  661. strcpy (req_setup.request_file, request_map_path);
  662. strcpy (req_setup.response_file, response_map_path);
  663. strcpy (req_setup.dispatch_file, dispatch_map_path);
  664. req_setup.control_size = 8192;
  665. req_setup.request_size = request_size;
  666. req_setup.response_size = response_size;
  667. req_setup.dispatch_size = dispatch_size;
  668. #if _POSIX_THREAD_PROCESS_SHARED < 1
  669. req_setup.semkey = semkey;
  670. #endif
  671. res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  672. if (res != CS_OK) {
  673. goto error_exit;
  674. }
  675. res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  676. if (res != CS_OK) {
  677. goto error_exit;
  678. }
  679. ipc_instance->fd = request_fd;
  680. ipc_instance->flow_control_state = 0;
  681. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  682. res = res_setup.error;
  683. goto error_exit;
  684. }
  685. ipc_instance->control_size = 8192;
  686. ipc_instance->request_size = request_size;
  687. ipc_instance->response_size = response_size;
  688. ipc_instance->dispatch_size = dispatch_size;
  689. pthread_mutex_init (&ipc_instance->mutex, NULL);
  690. hdb_handle_put (&ipc_hdb, *handle);
  691. return (res_setup.error);
  692. error_exit:
  693. #if _POSIX_THREAD_PROCESS_SHARED < 1
  694. if (ipc_instance->semid > 0)
  695. semctl (ipc_instance->semid, 0, IPC_RMID);
  696. #endif
  697. memory_unmap (ipc_instance->dispatch_buffer, dispatch_size);
  698. error_dispatch_buffer:
  699. memory_unmap (ipc_instance->response_buffer, response_size);
  700. error_response_buffer:
  701. memory_unmap (ipc_instance->request_buffer, request_size);
  702. error_request_buffer:
  703. memory_unmap (ipc_instance->control_buffer, 8192);
  704. error_connect:
  705. close (request_fd);
  706. hdb_handle_destroy (&ipc_hdb, *handle);
  707. hdb_handle_put (&ipc_hdb, *handle);
  708. return (res);
  709. }
  710. cs_error_t
  711. coroipcc_service_disconnect (
  712. hdb_handle_t handle)
  713. {
  714. cs_error_t res;
  715. struct ipc_instance *ipc_instance;
  716. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  717. if (res != CS_OK) {
  718. return (res);
  719. }
  720. shutdown (ipc_instance->fd, SHUT_RDWR);
  721. close (ipc_instance->fd);
  722. hdb_handle_destroy (&ipc_hdb, handle);
  723. hdb_handle_put (&ipc_hdb, handle);
  724. return (CS_OK);
  725. }
  726. cs_error_t
  727. coroipcc_dispatch_flow_control_get (
  728. hdb_handle_t handle,
  729. unsigned int *flow_control_state)
  730. {
  731. struct ipc_instance *ipc_instance;
  732. cs_error_t res;
  733. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  734. if (res != CS_OK) {
  735. return (res);
  736. }
  737. *flow_control_state = ipc_instance->flow_control_state;
  738. hdb_handle_put (&ipc_hdb, handle);
  739. return (res);
  740. }
  741. cs_error_t
  742. coroipcc_fd_get (
  743. hdb_handle_t handle,
  744. int *fd)
  745. {
  746. struct ipc_instance *ipc_instance;
  747. cs_error_t res;
  748. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  749. if (res != CS_OK) {
  750. return (res);
  751. }
  752. *fd = ipc_instance->fd;
  753. hdb_handle_put (&ipc_hdb, handle);
  754. return (res);
  755. }
  756. cs_error_t
  757. coroipcc_dispatch_get (
  758. hdb_handle_t handle,
  759. void **data,
  760. int timeout)
  761. {
  762. struct pollfd ufds;
  763. int poll_events;
  764. char buf;
  765. struct ipc_instance *ipc_instance;
  766. int res;
  767. char buf_two = 1;
  768. char *data_addr;
  769. cs_error_t error = CS_OK;
  770. error = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  771. if (error != CS_OK) {
  772. return (error);
  773. }
  774. *data = NULL;
  775. ufds.fd = ipc_instance->fd;
  776. ufds.events = POLLIN;
  777. ufds.revents = 0;
  778. poll_events = poll (&ufds, 1, timeout);
  779. if (poll_events == -1 && errno == EINTR) {
  780. error = CS_ERR_TRY_AGAIN;
  781. goto error_put;
  782. } else
  783. if (poll_events == -1) {
  784. error = CS_ERR_LIBRARY;
  785. goto error_put;
  786. } else
  787. if (poll_events == 0) {
  788. error = CS_ERR_TRY_AGAIN;
  789. goto error_put;
  790. }
  791. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  792. error = CS_ERR_LIBRARY;
  793. goto error_put;
  794. }
  795. res = recv (ipc_instance->fd, &buf, 1, 0);
  796. if (res == -1 && errno == EINTR) {
  797. error = CS_ERR_TRY_AGAIN;
  798. goto error_put;
  799. } else
  800. if (res == -1) {
  801. error = CS_ERR_LIBRARY;
  802. goto error_put;
  803. } else
  804. if (res == 0) {
  805. /* Means that the peer closed cleanly the socket. However, it should
  806. * happen only on BSD and Darwing systems since poll() returns a
  807. * POLLHUP event on other systems.
  808. */
  809. error = CS_ERR_LIBRARY;
  810. goto error_put;
  811. }
  812. ipc_instance->flow_control_state = 0;
  813. if (buf == MESSAGE_RES_OUTQ_NOT_EMPTY || buf == MESSAGE_RES_ENABLE_FLOWCONTROL) {
  814. ipc_instance->flow_control_state = 1;
  815. }
  816. /*
  817. * Notify executive to flush any pending dispatch messages
  818. */
  819. if (ipc_instance->flow_control_state) {
  820. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  821. res = socket_send (ipc_instance->fd, &buf_two, 1);
  822. assert (res == CS_OK); /* TODO */
  823. }
  824. /*
  825. * This is just a notification of flow control starting at the addition
  826. * of a new pending message, not a message to dispatch
  827. */
  828. if (buf == MESSAGE_RES_ENABLE_FLOWCONTROL) {
  829. error = CS_ERR_TRY_AGAIN;
  830. goto error_put;
  831. }
  832. if (buf == MESSAGE_RES_OUTQ_FLUSH_NR) {
  833. error = CS_ERR_TRY_AGAIN;
  834. goto error_put;
  835. }
  836. data_addr = ipc_instance->dispatch_buffer;
  837. data_addr = &data_addr[ipc_instance->control_buffer->read];
  838. *data = (void *)data_addr;
  839. return (CS_OK);
  840. error_put:
  841. hdb_handle_put (&ipc_hdb, handle);
  842. return (error);
  843. }
  844. cs_error_t
  845. coroipcc_dispatch_put (hdb_handle_t handle)
  846. {
  847. coroipc_response_header_t *header;
  848. struct ipc_instance *ipc_instance;
  849. cs_error_t res;
  850. char *addr;
  851. unsigned int read_idx;
  852. res = hdb_error_to_cs (hdb_handle_get_always (&ipc_hdb, handle, (void **)&ipc_instance));
  853. if (res != CS_OK) {
  854. return (res);
  855. }
  856. if ((res = ipc_sem_wait (ipc_instance, 2)) != CS_OK) {
  857. goto error_exit;
  858. }
  859. addr = ipc_instance->dispatch_buffer;
  860. read_idx = ipc_instance->control_buffer->read;
  861. header = (coroipc_response_header_t *) &addr[read_idx];
  862. ipc_instance->control_buffer->read =
  863. (read_idx + header->size) % ipc_instance->dispatch_size;
  864. /*
  865. * Put from dispatch get and also from this call's get
  866. */
  867. res = CS_OK;
  868. error_exit:
  869. hdb_handle_put (&ipc_hdb, handle);
  870. hdb_handle_put (&ipc_hdb, handle);
  871. return (res);
  872. }
  873. cs_error_t
  874. coroipcc_msg_send_reply_receive (
  875. hdb_handle_t handle,
  876. const struct iovec *iov,
  877. unsigned int iov_len,
  878. void *res_msg,
  879. size_t res_len)
  880. {
  881. cs_error_t res;
  882. struct ipc_instance *ipc_instance;
  883. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  884. if (res != CS_OK) {
  885. return (res);
  886. }
  887. pthread_mutex_lock (&ipc_instance->mutex);
  888. res = msg_send (ipc_instance, iov, iov_len);
  889. if (res != CS_OK) {
  890. goto error_exit;
  891. }
  892. res = reply_receive (ipc_instance, res_msg, res_len);
  893. error_exit:
  894. hdb_handle_put (&ipc_hdb, handle);
  895. pthread_mutex_unlock (&ipc_instance->mutex);
  896. return (res);
  897. }
  898. cs_error_t
  899. coroipcc_msg_send_reply_receive_in_buf_get (
  900. hdb_handle_t handle,
  901. const struct iovec *iov,
  902. unsigned int iov_len,
  903. void **res_msg)
  904. {
  905. unsigned int res;
  906. struct ipc_instance *ipc_instance;
  907. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  908. if (res != CS_OK) {
  909. return (res);
  910. }
  911. pthread_mutex_lock (&ipc_instance->mutex);
  912. res = msg_send (ipc_instance, iov, iov_len);
  913. if (res != CS_OK) {
  914. goto error_exit;
  915. }
  916. res = reply_receive_in_buf (ipc_instance, res_msg);
  917. error_exit:
  918. pthread_mutex_unlock (&ipc_instance->mutex);
  919. return (res);
  920. }
  921. cs_error_t
  922. coroipcc_msg_send_reply_receive_in_buf_put (
  923. hdb_handle_t handle)
  924. {
  925. unsigned int res;
  926. struct ipc_instance *ipc_instance;
  927. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  928. if (res != CS_OK) {
  929. return (res);
  930. }
  931. hdb_handle_put (&ipc_hdb, handle);
  932. hdb_handle_put (&ipc_hdb, handle);
  933. return (res);
  934. }
  935. cs_error_t
  936. coroipcc_zcb_alloc (
  937. hdb_handle_t handle,
  938. void **buffer,
  939. size_t size,
  940. size_t header_size)
  941. {
  942. struct ipc_instance *ipc_instance;
  943. void *buf = NULL;
  944. char path[128];
  945. unsigned int res;
  946. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  947. coroipc_response_header_t res_coroipcs_zc_alloc;
  948. size_t map_size;
  949. struct iovec iovec;
  950. struct coroipcs_zc_header *hdr;
  951. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  952. if (res != CS_OK) {
  953. return (res);
  954. }
  955. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  956. res = memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size);
  957. assert (res != -1);
  958. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  959. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  960. req_coroipcc_zc_alloc.map_size = map_size;
  961. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  962. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  963. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  964. res = coroipcc_msg_send_reply_receive (
  965. handle,
  966. &iovec,
  967. 1,
  968. &res_coroipcs_zc_alloc,
  969. sizeof (coroipc_response_header_t));
  970. hdr = (struct coroipcs_zc_header *)buf;
  971. hdr->map_size = map_size;
  972. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  973. hdb_handle_put (&ipc_hdb, handle);
  974. return (res);
  975. }
  976. cs_error_t
  977. coroipcc_zcb_free (
  978. hdb_handle_t handle,
  979. void *buffer)
  980. {
  981. struct ipc_instance *ipc_instance;
  982. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  983. coroipc_response_header_t res_coroipcs_zc_free;
  984. struct iovec iovec;
  985. unsigned int res;
  986. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  987. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  988. if (res != CS_OK) {
  989. return (res);
  990. }
  991. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  992. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  993. req_coroipcc_zc_free.map_size = header->map_size;
  994. req_coroipcc_zc_free.server_address = header->server_address;
  995. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  996. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  997. res = coroipcc_msg_send_reply_receive (
  998. handle,
  999. &iovec,
  1000. 1,
  1001. &res_coroipcs_zc_free,
  1002. sizeof (coroipc_response_header_t));
  1003. munmap ((void *)header, header->map_size);
  1004. hdb_handle_put (&ipc_hdb, handle);
  1005. return (res);
  1006. }
  1007. cs_error_t
  1008. coroipcc_zcb_msg_send_reply_receive (
  1009. hdb_handle_t handle,
  1010. void *msg,
  1011. void *res_msg,
  1012. size_t res_len)
  1013. {
  1014. struct ipc_instance *ipc_instance;
  1015. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  1016. struct coroipcs_zc_header *hdr;
  1017. struct iovec iovec;
  1018. cs_error_t res;
  1019. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  1020. if (res != CS_OK) {
  1021. return (res);
  1022. }
  1023. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  1024. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  1025. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  1026. req_coroipcc_zc_execute.server_address = hdr->server_address;
  1027. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  1028. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  1029. res = coroipcc_msg_send_reply_receive (
  1030. handle,
  1031. &iovec,
  1032. 1,
  1033. res_msg,
  1034. res_len);
  1035. hdb_handle_put (&ipc_hdb, handle);
  1036. return (res);
  1037. }