coroipcc.c 27 KB

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