coroipcc.c 26 KB

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