coroipcc.c 25 KB

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