coroipcc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. if (res == -1) {
  268. close (fd);
  269. return (-1);
  270. }
  271. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  272. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  273. if (addr_orig == MAP_FAILED) {
  274. close (fd);
  275. return (-1);
  276. }
  277. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  278. MAP_FIXED | MAP_SHARED, fd, 0);
  279. if (addr != addr_orig) {
  280. close (fd);
  281. return (-1);
  282. }
  283. #ifdef COROSYNC_BSD
  284. madvise(addr_orig, bytes, MADV_NOSYNC);
  285. #endif
  286. addr = mmap (((char *)addr_orig) + bytes,
  287. bytes, PROT_READ | PROT_WRITE,
  288. MAP_FIXED | MAP_SHARED, fd, 0);
  289. if (addr == MAP_FAILED) {
  290. close (fd);
  291. return (-1);
  292. }
  293. #ifdef COROSYNC_BSD
  294. madvise(((char *)addr_orig) + bytes, bytes, MADV_NOSYNC);
  295. #endif
  296. res = close (fd);
  297. if (res) {
  298. return (-1);
  299. }
  300. *buf = addr_orig;
  301. return (0);
  302. }
  303. static void
  304. memory_unmap (void *addr, size_t bytes)
  305. {
  306. int res;
  307. res = munmap (addr, bytes);
  308. }
  309. void ipc_hdb_destructor (void *context ) {
  310. struct ipc_instance *ipc_instance = (struct ipc_instance *)context;
  311. /*
  312. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  313. */
  314. memory_unmap (ipc_instance->control_buffer, ipc_instance->control_size);
  315. memory_unmap (ipc_instance->request_buffer, ipc_instance->request_size);
  316. memory_unmap (ipc_instance->response_buffer, ipc_instance->response_size);
  317. memory_unmap (ipc_instance->dispatch_buffer, (ipc_instance->dispatch_size) << 1);
  318. }
  319. static int
  320. memory_map (char *path, const char *file, void **buf, size_t bytes)
  321. {
  322. int fd;
  323. void *addr_orig;
  324. void *addr;
  325. int res;
  326. sprintf (path, "/dev/shm/%s", file);
  327. fd = mkstemp (path);
  328. if (fd == -1) {
  329. sprintf (path, LOCALSTATEDIR "/run/%s", file);
  330. fd = mkstemp (path);
  331. if (fd == -1) {
  332. return (-1);
  333. }
  334. }
  335. res = ftruncate (fd, bytes);
  336. if (res == -1) {
  337. close (fd);
  338. return (-1);
  339. }
  340. addr_orig = mmap (NULL, bytes, PROT_NONE,
  341. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  342. if (addr_orig == MAP_FAILED) {
  343. close (fd);
  344. return (-1);
  345. }
  346. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  347. MAP_FIXED | MAP_SHARED, fd, 0);
  348. if (addr != addr_orig) {
  349. close (fd);
  350. return (-1);
  351. }
  352. #ifdef COROSYNC_BSD
  353. madvise(addr_orig, bytes, MADV_NOSYNC);
  354. #endif
  355. res = close (fd);
  356. if (res) {
  357. return (-1);
  358. }
  359. *buf = addr_orig;
  360. return (0);
  361. }
  362. static cs_error_t
  363. msg_send (
  364. struct ipc_instance *ipc_instance,
  365. const struct iovec *iov,
  366. unsigned int iov_len)
  367. {
  368. #if _POSIX_THREAD_PROCESS_SHARED < 1
  369. struct sembuf sop;
  370. #endif
  371. int i;
  372. int res;
  373. int req_buffer_idx = 0;
  374. for (i = 0; i < iov_len; i++) {
  375. if ((req_buffer_idx + iov[i].iov_len) >
  376. ipc_instance->request_size) {
  377. return (CS_ERR_INVALID_PARAM);
  378. }
  379. memcpy (&ipc_instance->request_buffer[req_buffer_idx],
  380. iov[i].iov_base,
  381. iov[i].iov_len);
  382. req_buffer_idx += iov[i].iov_len;
  383. }
  384. #if _POSIX_THREAD_PROCESS_SHARED > 0
  385. res = sem_post (&ipc_instance->control_buffer->sem0);
  386. if (res == -1) {
  387. return (CS_ERR_LIBRARY);
  388. }
  389. #else
  390. /*
  391. * Signal semaphore #0 indicting a new message from client
  392. * to server request queue
  393. */
  394. sop.sem_num = 0;
  395. sop.sem_op = 1;
  396. sop.sem_flg = 0;
  397. retry_semop:
  398. res = semop (ipc_instance->semid, &sop, 1);
  399. if (res == -1 && errno == EINTR) {
  400. return (CS_ERR_TRY_AGAIN);
  401. } else
  402. if (res == -1 && errno == EACCES) {
  403. priv_change_send (ipc_instance);
  404. goto retry_semop;
  405. } else
  406. if (res == -1) {
  407. return (CS_ERR_LIBRARY);
  408. }
  409. #endif
  410. return (CS_OK);
  411. }
  412. static cs_error_t
  413. reply_receive (
  414. struct ipc_instance *ipc_instance,
  415. void *res_msg,
  416. size_t res_len)
  417. {
  418. #if _POSIX_THREAD_PROCESS_SHARED < 1
  419. struct sembuf sop;
  420. #else
  421. struct timespec timeout;
  422. struct pollfd pfd;
  423. #endif
  424. coroipc_response_header_t *response_header;
  425. int res;
  426. #if _POSIX_THREAD_PROCESS_SHARED > 0
  427. retry_semwait:
  428. timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
  429. timeout.tv_nsec = 0;
  430. res = sem_timedwait (&ipc_instance->control_buffer->sem1, &timeout);
  431. if (res == -1 && errno == ETIMEDOUT) {
  432. pfd.fd = ipc_instance->fd;
  433. pfd.events = 0;
  434. res = poll (&pfd, 1, 0);
  435. if (res == -1 && errno != EINTR) {
  436. return (CS_ERR_LIBRARY);
  437. }
  438. if (res == 1) {
  439. if (pfd.revents == POLLERR || pfd.revents == POLLHUP || pfd.revents == POLLNVAL) {
  440. return (CS_ERR_LIBRARY);
  441. }
  442. }
  443. goto retry_semwait;
  444. }
  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. response_header = (coroipc_response_header_t *)ipc_instance->response_buffer;
  470. if (response_header->error == CS_ERR_TRY_AGAIN) {
  471. return (CS_ERR_TRY_AGAIN);
  472. }
  473. memcpy (res_msg, ipc_instance->response_buffer, res_len);
  474. return (CS_OK);
  475. }
  476. static cs_error_t
  477. reply_receive_in_buf (
  478. struct ipc_instance *ipc_instance,
  479. void **res_msg)
  480. {
  481. #if _POSIX_THREAD_PROCESS_SHARED < 1
  482. struct sembuf sop;
  483. #else
  484. struct timespec timeout;
  485. struct pollfd pfd;
  486. #endif
  487. int res;
  488. #if _POSIX_THREAD_PROCESS_SHARED > 0
  489. retry_semwait:
  490. timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
  491. timeout.tv_nsec = 0;
  492. res = sem_timedwait (&ipc_instance->control_buffer->sem1, &timeout);
  493. if (res == -1 && errno == ETIMEDOUT) {
  494. pfd.fd = ipc_instance->fd;
  495. pfd.events = 0;
  496. res = poll (&pfd, 1, 0);
  497. if (res == -1 && errno != EINTR) {
  498. return (CS_ERR_LIBRARY);
  499. }
  500. if (pfd.revents == POLLERR || pfd.revents == POLLHUP) {
  501. return (CS_ERR_LIBRARY);
  502. }
  503. goto retry_semwait;
  504. }
  505. if (res == -1 && errno == EINTR) {
  506. goto retry_semwait;
  507. }
  508. #else
  509. /*
  510. * Wait for semaphore #1 indicating a new message from server
  511. * to client in the response queue
  512. */
  513. sop.sem_num = 1;
  514. sop.sem_op = -1;
  515. sop.sem_flg = 0;
  516. retry_semop:
  517. res = semop (ipc_instance->semid, &sop, 1);
  518. if (res == -1 && errno == EINTR) {
  519. return (CS_ERR_TRY_AGAIN);
  520. } else
  521. if (res == -1 && errno == EACCES) {
  522. priv_change_send (ipc_instance);
  523. goto retry_semop;
  524. } else
  525. if (res == -1) {
  526. return (CS_ERR_LIBRARY);
  527. }
  528. #endif
  529. *res_msg = (char *)ipc_instance->response_buffer;
  530. return (CS_OK);
  531. }
  532. /*
  533. * External API
  534. */
  535. cs_error_t
  536. coroipcc_service_connect (
  537. const char *socket_name,
  538. unsigned int service,
  539. size_t request_size,
  540. size_t response_size,
  541. size_t dispatch_size,
  542. hdb_handle_t *handle)
  543. {
  544. int request_fd;
  545. struct sockaddr_un address;
  546. cs_error_t res;
  547. struct ipc_instance *ipc_instance;
  548. #if _POSIX_THREAD_PROCESS_SHARED < 1
  549. key_t semkey = 0;
  550. union semun semun;
  551. #endif
  552. int sys_res;
  553. mar_req_setup_t req_setup;
  554. mar_res_setup_t res_setup;
  555. char control_map_path[128];
  556. char request_map_path[128];
  557. char response_map_path[128];
  558. char dispatch_map_path[128];
  559. res = hdb_error_to_cs (hdb_handle_create (&ipc_hdb,
  560. sizeof (struct ipc_instance), handle));
  561. if (res != CS_OK) {
  562. return (res);
  563. }
  564. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, *handle, (void **)&ipc_instance));
  565. if (res != CS_OK) {
  566. return (res);
  567. }
  568. res_setup.error = CS_ERR_LIBRARY;
  569. #if defined(COROSYNC_SOLARIS)
  570. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  571. #else
  572. request_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  573. #endif
  574. if (request_fd == -1) {
  575. return (CS_ERR_LIBRARY);
  576. }
  577. #ifdef SO_NOSIGPIPE
  578. socket_nosigpipe (request_fd);
  579. #endif
  580. memset (&address, 0, sizeof (struct sockaddr_un));
  581. address.sun_family = AF_UNIX;
  582. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  583. address.sun_len = SUN_LEN(&address);
  584. #endif
  585. #if defined(COROSYNC_LINUX)
  586. sprintf (address.sun_path + 1, "%s", socket_name);
  587. #else
  588. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  589. #endif
  590. sys_res = connect (request_fd, (struct sockaddr *)&address,
  591. COROSYNC_SUN_LEN(&address));
  592. if (sys_res == -1) {
  593. res = CS_ERR_TRY_AGAIN;
  594. goto error_connect;
  595. }
  596. sys_res = memory_map (
  597. control_map_path,
  598. "control_buffer-XXXXXX",
  599. (void *)&ipc_instance->control_buffer,
  600. 8192);
  601. if (sys_res == -1) {
  602. res = CS_ERR_LIBRARY;
  603. goto error_connect;
  604. }
  605. sys_res = memory_map (
  606. request_map_path,
  607. "request_buffer-XXXXXX",
  608. (void *)&ipc_instance->request_buffer,
  609. request_size);
  610. if (sys_res == -1) {
  611. res = CS_ERR_LIBRARY;
  612. goto error_request_buffer;
  613. }
  614. sys_res = memory_map (
  615. response_map_path,
  616. "response_buffer-XXXXXX",
  617. (void *)&ipc_instance->response_buffer,
  618. response_size);
  619. if (sys_res == -1) {
  620. res = CS_ERR_LIBRARY;
  621. goto error_response_buffer;
  622. }
  623. sys_res = circular_memory_map (
  624. dispatch_map_path,
  625. "dispatch_buffer-XXXXXX",
  626. (void *)&ipc_instance->dispatch_buffer,
  627. dispatch_size);
  628. if (sys_res == -1) {
  629. res = CS_ERR_LIBRARY;
  630. goto error_dispatch_buffer;
  631. }
  632. #if _POSIX_THREAD_PROCESS_SHARED > 0
  633. sem_init (&ipc_instance->control_buffer->sem0, 1, 0);
  634. sem_init (&ipc_instance->control_buffer->sem1, 1, 0);
  635. sem_init (&ipc_instance->control_buffer->sem2, 1, 0);
  636. #else
  637. /*
  638. * Allocate a semaphore segment
  639. */
  640. while (1) {
  641. semkey = random();
  642. ipc_instance->euid = geteuid ();
  643. if ((ipc_instance->semid
  644. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  645. break;
  646. }
  647. /*
  648. * EACCESS can be returned as non root user when opening a different
  649. * users semaphore.
  650. *
  651. * EEXIST can happen when we are a root or nonroot user opening
  652. * an existing shared memory segment for which we have access
  653. */
  654. if (errno != EEXIST && errno != EACCES) {
  655. res = CS_ERR_LIBRARY;
  656. goto error_exit;
  657. }
  658. }
  659. semun.val = 0;
  660. sys_res = semctl (ipc_instance->semid, 0, SETVAL, semun);
  661. if (sys_res != 0) {
  662. res = CS_ERR_LIBRARY;
  663. goto error_exit;
  664. }
  665. sys_res = semctl (ipc_instance->semid, 1, SETVAL, semun);
  666. if (sys_res != 0) {
  667. res = CS_ERR_LIBRARY;
  668. goto error_exit;
  669. }
  670. #endif
  671. /*
  672. * Initialize IPC setup message
  673. */
  674. req_setup.service = service;
  675. strcpy (req_setup.control_file, control_map_path);
  676. strcpy (req_setup.request_file, request_map_path);
  677. strcpy (req_setup.response_file, response_map_path);
  678. strcpy (req_setup.dispatch_file, dispatch_map_path);
  679. req_setup.control_size = 8192;
  680. req_setup.request_size = request_size;
  681. req_setup.response_size = response_size;
  682. req_setup.dispatch_size = dispatch_size;
  683. #if _POSIX_THREAD_PROCESS_SHARED < 1
  684. req_setup.semkey = semkey;
  685. #endif
  686. res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  687. if (res != CS_OK) {
  688. goto error_exit;
  689. }
  690. res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  691. if (res != CS_OK) {
  692. goto error_exit;
  693. }
  694. ipc_instance->fd = request_fd;
  695. ipc_instance->flow_control_state = 0;
  696. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  697. res = res_setup.error;
  698. goto error_exit;
  699. }
  700. ipc_instance->control_size = 8192;
  701. ipc_instance->request_size = request_size;
  702. ipc_instance->response_size = response_size;
  703. ipc_instance->dispatch_size = dispatch_size;
  704. pthread_mutex_init (&ipc_instance->mutex, NULL);
  705. hdb_handle_put (&ipc_hdb, *handle);
  706. return (res_setup.error);
  707. error_exit:
  708. #if _POSIX_THREAD_PROCESS_SHARED < 1
  709. if (ipc_instance->semid > 0)
  710. semctl (ipc_instance->semid, 0, IPC_RMID);
  711. #endif
  712. memory_unmap (ipc_instance->dispatch_buffer, dispatch_size);
  713. error_dispatch_buffer:
  714. memory_unmap (ipc_instance->response_buffer, response_size);
  715. error_response_buffer:
  716. memory_unmap (ipc_instance->request_buffer, request_size);
  717. error_request_buffer:
  718. memory_unmap (ipc_instance->control_buffer, 8192);
  719. error_connect:
  720. close (request_fd);
  721. hdb_handle_destroy (&ipc_hdb, *handle);
  722. hdb_handle_put (&ipc_hdb, *handle);
  723. return (res);
  724. }
  725. cs_error_t
  726. coroipcc_service_disconnect (
  727. hdb_handle_t handle)
  728. {
  729. cs_error_t res;
  730. struct ipc_instance *ipc_instance;
  731. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  732. if (res != CS_OK) {
  733. return (res);
  734. }
  735. shutdown (ipc_instance->fd, SHUT_RDWR);
  736. close (ipc_instance->fd);
  737. hdb_handle_destroy (&ipc_hdb, handle);
  738. hdb_handle_put (&ipc_hdb, handle);
  739. return (CS_OK);
  740. }
  741. cs_error_t
  742. coroipcc_dispatch_flow_control_get (
  743. hdb_handle_t handle,
  744. unsigned int *flow_control_state)
  745. {
  746. struct ipc_instance *ipc_instance;
  747. cs_error_t res;
  748. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  749. if (res != CS_OK) {
  750. return (res);
  751. }
  752. *flow_control_state = ipc_instance->flow_control_state;
  753. hdb_handle_put (&ipc_hdb, handle);
  754. return (res);
  755. }
  756. cs_error_t
  757. coroipcc_fd_get (
  758. hdb_handle_t handle,
  759. int *fd)
  760. {
  761. struct ipc_instance *ipc_instance;
  762. cs_error_t res;
  763. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  764. if (res != CS_OK) {
  765. return (res);
  766. }
  767. *fd = ipc_instance->fd;
  768. hdb_handle_put (&ipc_hdb, handle);
  769. return (res);
  770. }
  771. cs_error_t
  772. coroipcc_dispatch_get (
  773. hdb_handle_t handle,
  774. void **data,
  775. int timeout)
  776. {
  777. struct pollfd ufds;
  778. int poll_events;
  779. char buf;
  780. struct ipc_instance *ipc_instance;
  781. int res;
  782. char buf_two = 1;
  783. char *data_addr;
  784. cs_error_t error = CS_OK;
  785. error = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  786. if (error != CS_OK) {
  787. return (error);
  788. }
  789. *data = NULL;
  790. ufds.fd = ipc_instance->fd;
  791. ufds.events = POLLIN;
  792. ufds.revents = 0;
  793. poll_events = poll (&ufds, 1, timeout);
  794. if (poll_events == -1 && errno == EINTR) {
  795. error = CS_ERR_TRY_AGAIN;
  796. goto error_put;
  797. } else
  798. if (poll_events == -1) {
  799. error = CS_ERR_LIBRARY;
  800. goto error_put;
  801. } else
  802. if (poll_events == 0) {
  803. error = CS_ERR_TRY_AGAIN;
  804. goto error_put;
  805. }
  806. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  807. error = CS_ERR_LIBRARY;
  808. goto error_put;
  809. }
  810. res = recv (ipc_instance->fd, &buf, 1, 0);
  811. if (res == -1 && errno == EINTR) {
  812. error = CS_ERR_TRY_AGAIN;
  813. goto error_put;
  814. } else
  815. if (res == -1) {
  816. error = CS_ERR_LIBRARY;
  817. goto error_put;
  818. } else
  819. if (res == 0) {
  820. /* Means that the peer closed cleanly the socket. However, it should
  821. * happen only on BSD and Darwing systems since poll() returns a
  822. * POLLHUP event on other systems.
  823. */
  824. error = CS_ERR_LIBRARY;
  825. goto error_put;
  826. }
  827. ipc_instance->flow_control_state = 0;
  828. if (buf == MESSAGE_RES_OUTQ_NOT_EMPTY || buf == MESSAGE_RES_ENABLE_FLOWCONTROL) {
  829. ipc_instance->flow_control_state = 1;
  830. }
  831. /*
  832. * Notify executive to flush any pending dispatch messages
  833. */
  834. if (ipc_instance->flow_control_state) {
  835. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  836. res = socket_send (ipc_instance->fd, &buf_two, 1);
  837. assert (res == CS_OK); /* TODO */
  838. }
  839. /*
  840. * This is just a notification of flow control starting at the addition
  841. * of a new pending message, not a message to dispatch
  842. */
  843. if (buf == MESSAGE_RES_ENABLE_FLOWCONTROL) {
  844. error = CS_ERR_TRY_AGAIN;
  845. goto error_put;
  846. }
  847. if (buf == MESSAGE_RES_OUTQ_FLUSH_NR) {
  848. error = CS_ERR_TRY_AGAIN;
  849. goto error_put;
  850. }
  851. data_addr = ipc_instance->dispatch_buffer;
  852. data_addr = &data_addr[ipc_instance->control_buffer->read];
  853. *data = (void *)data_addr;
  854. return (CS_OK);
  855. error_put:
  856. hdb_handle_put (&ipc_hdb, handle);
  857. return (error);
  858. }
  859. cs_error_t
  860. coroipcc_dispatch_put (hdb_handle_t handle)
  861. {
  862. #if _POSIX_THREAD_PROCESS_SHARED < 1
  863. struct sembuf sop;
  864. #endif
  865. coroipc_response_header_t *header;
  866. struct ipc_instance *ipc_instance;
  867. int res;
  868. char *addr;
  869. unsigned int read_idx;
  870. res = hdb_error_to_cs (hdb_handle_get_always (&ipc_hdb, handle, (void **)&ipc_instance));
  871. if (res != CS_OK) {
  872. return (res);
  873. }
  874. #if _POSIX_THREAD_PROCESS_SHARED > 0
  875. retry_semwait:
  876. res = sem_wait (&ipc_instance->control_buffer->sem2);
  877. if (res == -1 && errno == EINTR) {
  878. goto retry_semwait;
  879. }
  880. #else
  881. sop.sem_num = 2;
  882. sop.sem_op = -1;
  883. sop.sem_flg = 0;
  884. retry_semop:
  885. res = semop (ipc_instance->semid, &sop, 1);
  886. if (res == -1 && errno == EINTR) {
  887. res = CS_ERR_TRY_AGAIN;
  888. goto error_exit;
  889. } else
  890. if (res == -1 && errno == EACCES) {
  891. priv_change_send (ipc_instance);
  892. goto retry_semop;
  893. } else
  894. if (res == -1) {
  895. res = CS_ERR_LIBRARY;
  896. goto error_exit;
  897. }
  898. #endif
  899. addr = ipc_instance->dispatch_buffer;
  900. read_idx = ipc_instance->control_buffer->read;
  901. header = (coroipc_response_header_t *) &addr[read_idx];
  902. ipc_instance->control_buffer->read =
  903. (read_idx + header->size) % ipc_instance->dispatch_size;
  904. /*
  905. * Put from dispatch get and also from this call's get
  906. */
  907. res = CS_OK;
  908. #if _POSIX_THREAD_PROCESS_SHARED < 1
  909. error_exit:
  910. #endif
  911. hdb_handle_put (&ipc_hdb, handle);
  912. hdb_handle_put (&ipc_hdb, handle);
  913. return (res);
  914. }
  915. cs_error_t
  916. coroipcc_msg_send_reply_receive (
  917. hdb_handle_t handle,
  918. const struct iovec *iov,
  919. unsigned int iov_len,
  920. void *res_msg,
  921. size_t res_len)
  922. {
  923. cs_error_t res;
  924. struct ipc_instance *ipc_instance;
  925. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  926. if (res != CS_OK) {
  927. return (res);
  928. }
  929. pthread_mutex_lock (&ipc_instance->mutex);
  930. res = msg_send (ipc_instance, iov, iov_len);
  931. if (res != CS_OK) {
  932. goto error_exit;
  933. }
  934. res = reply_receive (ipc_instance, res_msg, res_len);
  935. error_exit:
  936. hdb_handle_put (&ipc_hdb, handle);
  937. pthread_mutex_unlock (&ipc_instance->mutex);
  938. return (res);
  939. }
  940. cs_error_t
  941. coroipcc_msg_send_reply_receive_in_buf_get (
  942. hdb_handle_t handle,
  943. const struct iovec *iov,
  944. unsigned int iov_len,
  945. void **res_msg)
  946. {
  947. unsigned int res;
  948. struct ipc_instance *ipc_instance;
  949. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  950. if (res != CS_OK) {
  951. return (res);
  952. }
  953. pthread_mutex_lock (&ipc_instance->mutex);
  954. res = msg_send (ipc_instance, iov, iov_len);
  955. if (res != CS_OK) {
  956. goto error_exit;
  957. }
  958. res = reply_receive_in_buf (ipc_instance, res_msg);
  959. error_exit:
  960. pthread_mutex_unlock (&ipc_instance->mutex);
  961. return (res);
  962. }
  963. cs_error_t
  964. coroipcc_msg_send_reply_receive_in_buf_put (
  965. hdb_handle_t handle)
  966. {
  967. unsigned int res;
  968. struct ipc_instance *ipc_instance;
  969. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  970. if (res != CS_OK) {
  971. return (res);
  972. }
  973. hdb_handle_put (&ipc_hdb, handle);
  974. hdb_handle_put (&ipc_hdb, handle);
  975. return (res);
  976. }
  977. cs_error_t
  978. coroipcc_zcb_alloc (
  979. hdb_handle_t handle,
  980. void **buffer,
  981. size_t size,
  982. size_t header_size)
  983. {
  984. struct ipc_instance *ipc_instance;
  985. void *buf = NULL;
  986. char path[128];
  987. unsigned int res;
  988. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  989. coroipc_response_header_t res_coroipcs_zc_alloc;
  990. size_t map_size;
  991. struct iovec iovec;
  992. struct coroipcs_zc_header *hdr;
  993. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  994. if (res != CS_OK) {
  995. return (res);
  996. }
  997. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  998. res = memory_map (path, "corosync_zerocopy-XXXXXX", &buf, map_size);
  999. assert (res != -1);
  1000. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  1001. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  1002. req_coroipcc_zc_alloc.map_size = map_size;
  1003. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  1004. iovec.iov_base = (void *)&req_coroipcc_zc_alloc;
  1005. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  1006. res = coroipcc_msg_send_reply_receive (
  1007. handle,
  1008. &iovec,
  1009. 1,
  1010. &res_coroipcs_zc_alloc,
  1011. sizeof (coroipc_response_header_t));
  1012. hdr = (struct coroipcs_zc_header *)buf;
  1013. hdr->map_size = map_size;
  1014. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  1015. hdb_handle_put (&ipc_hdb, handle);
  1016. return (res);
  1017. }
  1018. cs_error_t
  1019. coroipcc_zcb_free (
  1020. hdb_handle_t handle,
  1021. void *buffer)
  1022. {
  1023. struct ipc_instance *ipc_instance;
  1024. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  1025. coroipc_response_header_t res_coroipcs_zc_free;
  1026. struct iovec iovec;
  1027. unsigned int res;
  1028. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  1029. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  1030. if (res != CS_OK) {
  1031. return (res);
  1032. }
  1033. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  1034. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  1035. req_coroipcc_zc_free.map_size = header->map_size;
  1036. req_coroipcc_zc_free.server_address = header->server_address;
  1037. iovec.iov_base = (void *)&req_coroipcc_zc_free;
  1038. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  1039. res = coroipcc_msg_send_reply_receive (
  1040. handle,
  1041. &iovec,
  1042. 1,
  1043. &res_coroipcs_zc_free,
  1044. sizeof (coroipc_response_header_t));
  1045. munmap ((void *)header, header->map_size);
  1046. hdb_handle_put (&ipc_hdb, handle);
  1047. return (res);
  1048. }
  1049. cs_error_t
  1050. coroipcc_zcb_msg_send_reply_receive (
  1051. hdb_handle_t handle,
  1052. void *msg,
  1053. void *res_msg,
  1054. size_t res_len)
  1055. {
  1056. struct ipc_instance *ipc_instance;
  1057. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  1058. struct coroipcs_zc_header *hdr;
  1059. struct iovec iovec;
  1060. cs_error_t res;
  1061. res = hdb_error_to_cs (hdb_handle_get (&ipc_hdb, handle, (void **)&ipc_instance));
  1062. if (res != CS_OK) {
  1063. return (res);
  1064. }
  1065. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  1066. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  1067. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  1068. req_coroipcc_zc_execute.server_address = hdr->server_address;
  1069. iovec.iov_base = (void *)&req_coroipcc_zc_execute;
  1070. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  1071. res = coroipcc_msg_send_reply_receive (
  1072. handle,
  1073. &iovec,
  1074. 1,
  1075. res_msg,
  1076. res_len);
  1077. hdb_handle_put (&ipc_hdb, handle);
  1078. return (res);
  1079. }