coroipcc.c 25 KB

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