coroipcc.c 27 KB

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