coroipcc.c 27 KB

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