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