coroipcc.c 25 KB

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