coroipcc.c 27 KB

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