coroipcc.c 26 KB

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