coroipcc.c 26 KB

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