coroipcc.c 27 KB

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