coroipcc.c 27 KB

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