coroipcc.c 27 KB

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