4
0

coroipcc.c 27 KB

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