coroipcc.c 27 KB

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