coroipcc.c 26 KB

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