coroipcc.c 27 KB

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