coroipcc.c 25 KB

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