coroipcc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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/sem.h>
  57. #include <sys/mman.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/ipc_gen.h>
  60. #include <corosync/coroipcc.h>
  61. enum SA_HANDLE_STATE {
  62. SA_HANDLE_STATE_EMPTY,
  63. SA_HANDLE_STATE_PENDINGREMOVAL,
  64. SA_HANDLE_STATE_ACTIVE
  65. };
  66. struct saHandle {
  67. int state;
  68. void *instance;
  69. int refCount;
  70. uint32_t check;
  71. };
  72. struct ipc_segment {
  73. int fd;
  74. int shmid;
  75. int semid;
  76. int flow_control_state;
  77. struct shared_memory *shared_memory;
  78. void *dispatch_buffer;
  79. uid_t euid;
  80. };
  81. #if defined(COROSYNC_LINUX)
  82. /* SUN_LEN is broken for abstract namespace
  83. */
  84. #define AIS_SUN_LEN(a) sizeof(*(a))
  85. #else
  86. #define AIS_SUN_LEN(a) SUN_LEN(a)
  87. #endif
  88. #ifdef SO_NOSIGPIPE
  89. void socket_nosigpipe(int s)
  90. {
  91. int on = 1;
  92. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  93. }
  94. #endif
  95. #ifndef MSG_NOSIGNAL
  96. #define MSG_NOSIGNAL 0
  97. #endif
  98. static int
  99. coroipcc_send (
  100. int s,
  101. void *msg,
  102. size_t len)
  103. {
  104. int result;
  105. struct msghdr msg_send;
  106. struct iovec iov_send;
  107. char *rbuf = msg;
  108. int processed = 0;
  109. msg_send.msg_iov = &iov_send;
  110. msg_send.msg_iovlen = 1;
  111. msg_send.msg_name = 0;
  112. msg_send.msg_namelen = 0;
  113. msg_send.msg_control = 0;
  114. msg_send.msg_controllen = 0;
  115. msg_send.msg_flags = 0;
  116. retry_send:
  117. iov_send.iov_base = &rbuf[processed];
  118. iov_send.iov_len = len - processed;
  119. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  120. /*
  121. * return immediately on any kind of syscall error that maps to
  122. * CS_ERR if no part of message has been sent
  123. */
  124. if (result == -1 && processed == 0) {
  125. if (errno == EINTR) {
  126. goto error_exit;
  127. }
  128. if (errno == EAGAIN) {
  129. goto error_exit;
  130. }
  131. if (errno == EFAULT) {
  132. goto error_exit;
  133. }
  134. }
  135. /*
  136. * retry read operations that are already started except
  137. * for fault in that case, return ERR_LIBRARY
  138. */
  139. if (result == -1 && processed > 0) {
  140. if (errno == EINTR) {
  141. goto retry_send;
  142. }
  143. if (errno == EAGAIN) {
  144. goto retry_send;
  145. }
  146. if (errno == EFAULT) {
  147. goto error_exit;
  148. }
  149. }
  150. /*
  151. * return ERR_LIBRARY on any other syscall error
  152. */
  153. if (result == -1) {
  154. goto error_exit;
  155. }
  156. processed += result;
  157. if (processed != len) {
  158. goto retry_send;
  159. }
  160. return (0);
  161. error_exit:
  162. return (-1);
  163. }
  164. static int
  165. coroipcc_recv (
  166. int s,
  167. void *msg,
  168. size_t len)
  169. {
  170. int error = 0;
  171. int result;
  172. struct msghdr msg_recv;
  173. struct iovec iov_recv;
  174. char *rbuf = msg;
  175. int processed = 0;
  176. msg_recv.msg_iov = &iov_recv;
  177. msg_recv.msg_iovlen = 1;
  178. msg_recv.msg_name = 0;
  179. msg_recv.msg_namelen = 0;
  180. msg_recv.msg_control = 0;
  181. msg_recv.msg_controllen = 0;
  182. msg_recv.msg_flags = 0;
  183. retry_recv:
  184. iov_recv.iov_base = (void *)&rbuf[processed];
  185. iov_recv.iov_len = len - processed;
  186. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL|MSG_WAITALL);
  187. if (result == -1 && errno == EINTR) {
  188. goto retry_recv;
  189. }
  190. if (result == -1 && errno == EAGAIN) {
  191. goto retry_recv;
  192. }
  193. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  194. /* On many OS poll never return POLLHUP or POLLERR.
  195. * EOF is detected when recvmsg return 0.
  196. */
  197. if (result == 0) {
  198. error = -1;
  199. goto error_exit;
  200. }
  201. #endif
  202. if (result == -1 || result == 0) {
  203. error = -1;
  204. goto error_exit;
  205. }
  206. processed += result;
  207. if (processed != len) {
  208. goto retry_recv;
  209. }
  210. assert (processed == len);
  211. error_exit:
  212. return (0);
  213. }
  214. static int
  215. priv_change_send (struct ipc_segment *ipc_segment)
  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_segment->euid == req_priv_change.euid) {
  225. return (0);
  226. }
  227. req_priv_change.egid = getegid();
  228. buf_req = MESSAGE_REQ_CHANGE_EUID;
  229. res = coroipcc_send (ipc_segment->fd, &buf_req, 1);
  230. if (res == -1) {
  231. return (-1);
  232. }
  233. res = coroipcc_send (ipc_segment->fd, &req_priv_change,
  234. sizeof (req_priv_change));
  235. if (res == -1) {
  236. return (-1);
  237. }
  238. ipc_segment->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. static int
  250. circular_memory_map (char *path, const char *file, void **buf, size_t bytes)
  251. {
  252. int fd;
  253. void *addr_orig;
  254. void *addr;
  255. int res;
  256. sprintf (path, "/dev/shm/%s", file);
  257. fd = mkstemp (path);
  258. if (fd == -1) {
  259. sprintf (path, "/var/run/%s", file);
  260. fd = mkstemp (path);
  261. if (fd == -1) {
  262. return (-1);
  263. }
  264. }
  265. res = ftruncate (fd, bytes);
  266. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  267. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  268. if (addr_orig == MAP_FAILED) {
  269. return (-1);
  270. }
  271. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  272. MAP_FIXED | MAP_SHARED, fd, 0);
  273. if (addr != addr_orig) {
  274. return (-1);
  275. }
  276. addr = mmap (((char *)addr_orig) + bytes,
  277. bytes, PROT_READ | PROT_WRITE,
  278. MAP_FIXED | MAP_SHARED, fd, 0);
  279. res = close (fd);
  280. if (res) {
  281. return (-1);
  282. }
  283. *buf = addr_orig;
  284. return (0);
  285. }
  286. static void
  287. memory_unmap (void *addr, size_t bytes)
  288. {
  289. int res;
  290. res = munmap (addr, bytes);
  291. }
  292. static int
  293. memory_map (char *path, const char *file, void **buf, size_t bytes)
  294. {
  295. int fd;
  296. void *addr_orig;
  297. void *addr;
  298. int res;
  299. sprintf (path, "/dev/shm/%s", file);
  300. fd = mkstemp (path);
  301. if (fd == -1) {
  302. sprintf (path, "/var/run/%s", file);
  303. fd = mkstemp (path);
  304. if (fd == -1) {
  305. return (-1);
  306. }
  307. }
  308. res = ftruncate (fd, bytes);
  309. addr_orig = mmap (NULL, bytes, PROT_NONE,
  310. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  311. if (addr_orig == MAP_FAILED) {
  312. return (-1);
  313. }
  314. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  315. MAP_FIXED | MAP_SHARED, fd, 0);
  316. if (addr != addr_orig) {
  317. return (-1);
  318. }
  319. res = close (fd);
  320. if (res) {
  321. return (-1);
  322. }
  323. *buf = addr_orig;
  324. return (0);
  325. }
  326. cs_error_t
  327. coroipcc_service_connect (
  328. const char *socket_name,
  329. enum service_types service,
  330. void **shmseg)
  331. {
  332. int request_fd;
  333. struct sockaddr_un address;
  334. cs_error_t error;
  335. struct ipc_segment *ipc_segment;
  336. key_t shmkey = 0;
  337. key_t semkey = 0;
  338. int res;
  339. mar_req_setup_t req_setup;
  340. mar_res_setup_t res_setup;
  341. union semun semun;
  342. char dispatch_map_path[128];
  343. res_setup.error = CS_ERR_LIBRARY;
  344. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  345. if (request_fd == -1) {
  346. return (-1);
  347. }
  348. memset (&address, 0, sizeof (struct sockaddr_un));
  349. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  350. address.sun_len = sizeof(struct sockaddr_un);
  351. #endif
  352. address.sun_family = PF_UNIX;
  353. #if defined(COROSYNC_LINUX)
  354. sprintf (address.sun_path + 1, "%s", socket_name);
  355. #else
  356. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  357. #endif
  358. res = connect (request_fd, (struct sockaddr *)&address,
  359. AIS_SUN_LEN(&address));
  360. if (res == -1) {
  361. close (request_fd);
  362. return (CS_ERR_TRY_AGAIN);
  363. }
  364. ipc_segment = malloc (sizeof (struct ipc_segment));
  365. if (ipc_segment == NULL) {
  366. close (request_fd);
  367. return (-1);
  368. }
  369. bzero (ipc_segment, sizeof (struct ipc_segment));
  370. /*
  371. * Allocate a shared memory segment
  372. */
  373. while (1) {
  374. shmkey = random();
  375. if ((ipc_segment->shmid
  376. = shmget (shmkey, sizeof (struct shared_memory),
  377. IPC_CREAT|IPC_EXCL|0600)) != -1) {
  378. break;
  379. }
  380. if (errno != EEXIST) {
  381. goto error_exit;
  382. }
  383. }
  384. /*
  385. * Allocate a semaphore segment
  386. */
  387. while (1) {
  388. semkey = random();
  389. ipc_segment->euid = geteuid ();
  390. if ((ipc_segment->semid
  391. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  392. break;
  393. }
  394. if (errno != EEXIST) {
  395. goto error_exit;
  396. }
  397. }
  398. /*
  399. * Attach to shared memory segment
  400. */
  401. ipc_segment->shared_memory = shmat (ipc_segment->shmid, NULL, 0);
  402. if (ipc_segment->shared_memory == (void *)-1) {
  403. goto error_exit;
  404. }
  405. semun.val = 0;
  406. res = semctl (ipc_segment->semid, 0, SETVAL, semun);
  407. if (res != 0) {
  408. goto error_exit;
  409. }
  410. res = semctl (ipc_segment->semid, 1, SETVAL, semun);
  411. if (res != 0) {
  412. goto error_exit;
  413. }
  414. res = circular_memory_map (dispatch_map_path,
  415. "dispatch_bufer-XXXXXX",
  416. &ipc_segment->dispatch_buffer, DISPATCH_SIZE);
  417. strcpy (req_setup.dispatch_file, dispatch_map_path);
  418. req_setup.shmkey = shmkey;
  419. req_setup.semkey = semkey;
  420. req_setup.service = service;
  421. error = coroipcc_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  422. if (error != 0) {
  423. goto error_exit;
  424. }
  425. error = coroipcc_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  426. if (error != 0) {
  427. goto error_exit;
  428. }
  429. ipc_segment->fd = request_fd;
  430. ipc_segment->flow_control_state = 0;
  431. *shmseg = ipc_segment;
  432. /*
  433. * Something go wrong with server
  434. * Cleanup all
  435. */
  436. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  437. goto error_exit;
  438. }
  439. return (res_setup.error);
  440. error_exit:
  441. close (request_fd);
  442. if (ipc_segment->shmid > 0)
  443. shmctl (ipc_segment->shmid, IPC_RMID, NULL);
  444. if (ipc_segment->semid > 0)
  445. semctl (ipc_segment->semid, 0, IPC_RMID);
  446. return (res_setup.error);
  447. }
  448. cs_error_t
  449. coroipcc_service_disconnect (
  450. void *ipc_context)
  451. {
  452. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  453. shutdown (ipc_segment->fd, SHUT_RDWR);
  454. close (ipc_segment->fd);
  455. shmdt (ipc_segment->shared_memory);
  456. /*
  457. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  458. */
  459. memory_unmap (ipc_segment->dispatch_buffer, (DISPATCH_SIZE) << 1);
  460. free (ipc_segment);
  461. return (CS_OK);
  462. }
  463. int
  464. coroipcc_dispatch_flow_control_get (
  465. void *ipc_context)
  466. {
  467. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  468. return (ipc_segment->flow_control_state);
  469. }
  470. int
  471. coroipcc_fd_get (void *ipc_ctx)
  472. {
  473. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  474. return (ipc_segment->fd);
  475. }
  476. int
  477. coroipcc_dispatch_get (void *ipc_ctx, void **data, int timeout)
  478. {
  479. struct pollfd ufds;
  480. int poll_events;
  481. char buf;
  482. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  483. int res;
  484. char buf_two = 1;
  485. char *data_addr;
  486. ufds.fd = ipc_segment->fd;
  487. ufds.events = POLLIN;
  488. ufds.revents = 0;
  489. retry_poll:
  490. poll_events = poll (&ufds, 1, timeout);
  491. if (poll_events == -1 && errno == EINTR) {
  492. goto retry_poll;
  493. } else
  494. if (poll_events == -1) {
  495. return (-1);
  496. } else
  497. if (poll_events == 0) {
  498. return (0);
  499. }
  500. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  501. return (-1);
  502. }
  503. retry_recv:
  504. res = recv (ipc_segment->fd, &buf, 1, 0);
  505. if (res == -1 && errno == EINTR) {
  506. goto retry_recv;
  507. } else
  508. if (res == -1) {
  509. return (-1);
  510. }
  511. if (res == 0) {
  512. return (-1);
  513. }
  514. ipc_segment->flow_control_state = 0;
  515. if (buf == 1 || buf == 2) {
  516. ipc_segment->flow_control_state = 1;
  517. }
  518. /*
  519. * Notify executive to flush any pending dispatch messages
  520. */
  521. if (ipc_segment->flow_control_state) {
  522. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  523. res = coroipcc_send (ipc_segment->fd, &buf_two, 1);
  524. assert (res == 0); //TODO
  525. }
  526. /*
  527. * This is just a notification of flow control starting at the addition
  528. * of a new pending message, not a message to dispatch
  529. */
  530. if (buf == 2) {
  531. return (0);
  532. }
  533. if (buf == 3) {
  534. return (0);
  535. }
  536. data_addr = ipc_segment->dispatch_buffer;
  537. data_addr = &data_addr[ipc_segment->shared_memory->read];
  538. *data = (void *)data_addr;
  539. return (1);
  540. }
  541. int
  542. coroipcc_dispatch_put (void *ipc_ctx)
  543. {
  544. struct sembuf sop;
  545. mar_res_header_t *header;
  546. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  547. int res;
  548. char *addr;
  549. unsigned int read_idx;
  550. sop.sem_num = 2;
  551. sop.sem_op = -1;
  552. sop.sem_flg = 0;
  553. retry_semop:
  554. res = semop (ipc_segment->semid, &sop, 1);
  555. if (res == -1 && errno == EINTR) {
  556. goto retry_semop;
  557. } else
  558. if (res == -1 && errno == EACCES) {
  559. priv_change_send (ipc_segment);
  560. goto retry_semop;
  561. } else
  562. if (res == -1) {
  563. return (-1);
  564. }
  565. addr = ipc_segment->dispatch_buffer;
  566. read_idx = ipc_segment->shared_memory->read;
  567. header = (mar_res_header_t *) &addr[read_idx];
  568. ipc_segment->shared_memory->read =
  569. (read_idx + header->size) % (DISPATCH_SIZE);
  570. return (0);
  571. }
  572. static cs_error_t
  573. coroipcc_msg_send (
  574. void *ipc_context,
  575. const struct iovec *iov,
  576. unsigned int iov_len)
  577. {
  578. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  579. struct sembuf sop;
  580. int i;
  581. int res;
  582. int req_buffer_idx = 0;
  583. for (i = 0; i < iov_len; i++) {
  584. memcpy (&ipc_segment->shared_memory->req_buffer[req_buffer_idx],
  585. iov[i].iov_base,
  586. iov[i].iov_len);
  587. req_buffer_idx += iov[i].iov_len;
  588. }
  589. /*
  590. * Signal semaphore #0 indicting a new message from client
  591. * to server request queue
  592. */
  593. sop.sem_num = 0;
  594. sop.sem_op = 1;
  595. sop.sem_flg = 0;
  596. retry_semop:
  597. res = semop (ipc_segment->semid, &sop, 1);
  598. if (res == -1 && errno == EINTR) {
  599. goto retry_semop;
  600. } else
  601. if (res == -1 && errno == EACCES) {
  602. priv_change_send (ipc_segment);
  603. goto retry_semop;
  604. } else
  605. if (res == -1) {
  606. return (CS_ERR_LIBRARY);
  607. }
  608. return (CS_OK);
  609. }
  610. static cs_error_t
  611. coroipcc_reply_receive (
  612. void *ipc_context,
  613. void *res_msg, size_t res_len)
  614. {
  615. struct sembuf sop;
  616. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  617. int res;
  618. /*
  619. * Wait for semaphore #1 indicating a new message from server
  620. * to client in the response queue
  621. */
  622. sop.sem_num = 1;
  623. sop.sem_op = -1;
  624. sop.sem_flg = 0;
  625. retry_semop:
  626. res = semop (ipc_segment->semid, &sop, 1);
  627. if (res == -1 && errno == EINTR) {
  628. goto retry_semop;
  629. } else
  630. if (res == -1 && errno == EACCES) {
  631. priv_change_send (ipc_segment);
  632. goto retry_semop;
  633. } else
  634. if (res == -1) {
  635. return (CS_ERR_LIBRARY);
  636. }
  637. memcpy (res_msg, ipc_segment->shared_memory->res_buffer, res_len);
  638. return (CS_OK);
  639. }
  640. static cs_error_t
  641. coroipcc_reply_receive_in_buf (
  642. void *ipc_context,
  643. void **res_msg)
  644. {
  645. struct sembuf sop;
  646. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  647. int res;
  648. /*
  649. * Wait for semaphore #1 indicating a new message from server
  650. * to client in the response queue
  651. */
  652. sop.sem_num = 1;
  653. sop.sem_op = -1;
  654. sop.sem_flg = 0;
  655. retry_semop:
  656. res = semop (ipc_segment->semid, &sop, 1);
  657. if (res == -1 && errno == EINTR) {
  658. goto retry_semop;
  659. } else
  660. if (res == -1 && errno == EACCES) {
  661. priv_change_send (ipc_segment);
  662. goto retry_semop;
  663. } else
  664. if (res == -1) {
  665. return (CS_ERR_LIBRARY);
  666. }
  667. *res_msg = (char *)ipc_segment->shared_memory->res_buffer;
  668. return (CS_OK);
  669. }
  670. cs_error_t
  671. coroipcc_msg_send_reply_receive (
  672. void *ipc_context,
  673. const struct iovec *iov,
  674. unsigned int iov_len,
  675. void *res_msg,
  676. size_t res_len)
  677. {
  678. cs_error_t res;
  679. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  680. if (res != CS_OK) {
  681. return (res);
  682. }
  683. res = coroipcc_reply_receive (ipc_context, res_msg, res_len);
  684. if (res != CS_OK) {
  685. return (res);
  686. }
  687. return (CS_OK);
  688. }
  689. cs_error_t
  690. coroipcc_msg_send_reply_receive_in_buf (
  691. void *ipc_context,
  692. const struct iovec *iov,
  693. unsigned int iov_len,
  694. void **res_msg)
  695. {
  696. unsigned int res;
  697. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  698. if (res != CS_OK) {
  699. return (res);
  700. }
  701. res = coroipcc_reply_receive_in_buf (ipc_context, res_msg);
  702. if (res != CS_OK) {
  703. return (res);
  704. }
  705. return (CS_OK);
  706. }
  707. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  708. static void hdb_lock (struct saHandleDatabase *hdb)
  709. {
  710. pthread_spin_lock (&hdb->lock);
  711. }
  712. static void hdb_unlock (struct saHandleDatabase *hdb)
  713. {
  714. pthread_spin_unlock (&hdb->lock);
  715. }
  716. void saHandleDatabaseLock_init (struct saHandleDatabase *hdb)
  717. {
  718. pthread_spin_init (&hdb->lock, 0);
  719. }
  720. #else
  721. static void hdb_lock (struct saHandleDatabase *hdb)
  722. {
  723. pthread_mutex_lock (&hdb->lock);
  724. }
  725. static void hdb_unlock (struct saHandleDatabase *hdb)
  726. {
  727. pthread_mutex_unlock (&hdb->lock);
  728. }
  729. void saHandleDatabaseLock_init (struct saHandleDatabase *hdb)
  730. {
  731. pthread_mutex_init (&hdb->lock, NULL);
  732. }
  733. #endif
  734. cs_error_t
  735. coroipcc_zcb_alloc (
  736. void *ipc_context,
  737. void **buffer,
  738. size_t size,
  739. size_t header_size)
  740. {
  741. void *buf = NULL;
  742. char path[128];
  743. unsigned int res;
  744. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  745. mar_res_header_t res_coroipcs_zc_alloc;
  746. size_t map_size;
  747. struct iovec iovec;
  748. struct coroipcs_zc_header *hdr;
  749. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  750. res = memory_map (path, "cpg_zc-XXXXXX", &buf, size);
  751. assert (res != -1);
  752. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  753. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  754. req_coroipcc_zc_alloc.map_size = map_size;
  755. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  756. iovec.iov_base = &req_coroipcc_zc_alloc;
  757. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  758. res = coroipcc_msg_send_reply_receive (
  759. ipc_context,
  760. &iovec,
  761. 1,
  762. &res_coroipcs_zc_alloc,
  763. sizeof (mar_res_header_t));
  764. hdr = (struct coroipcs_zc_header *)buf;
  765. hdr->map_size = map_size;
  766. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  767. return (CS_OK);
  768. }
  769. cs_error_t
  770. coroipcc_zcb_free (
  771. void *ipc_context,
  772. void *buffer)
  773. {
  774. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  775. mar_res_header_t res_coroipcs_zc_free;
  776. struct iovec iovec;
  777. unsigned int res;
  778. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  779. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  780. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  781. req_coroipcc_zc_free.map_size = header->map_size;
  782. req_coroipcc_zc_free.server_address = header->server_address;
  783. iovec.iov_base = &req_coroipcc_zc_free;
  784. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  785. res = coroipcc_msg_send_reply_receive (
  786. ipc_context,
  787. &iovec,
  788. 1,
  789. &res_coroipcs_zc_free,
  790. sizeof (mar_res_header_t));
  791. munmap (header, header->map_size);
  792. return (CS_OK);
  793. }
  794. cs_error_t
  795. coroipcc_zcb_msg_send_reply_receive (
  796. void *ipc_context,
  797. void *msg,
  798. void *res_msg,
  799. size_t res_len)
  800. {
  801. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  802. struct coroipcs_zc_header *hdr;
  803. struct iovec iovec;
  804. cs_error_t res;
  805. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  806. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  807. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  808. req_coroipcc_zc_execute.server_address = hdr->server_address;
  809. iovec.iov_base = &req_coroipcc_zc_execute;
  810. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  811. res = coroipcc_msg_send_reply_receive (
  812. ipc_context,
  813. &iovec,
  814. 1,
  815. res_msg,
  816. res_len);
  817. return (res);
  818. }
  819. cs_error_t
  820. saHandleCreate (
  821. struct saHandleDatabase *handleDatabase,
  822. int instanceSize,
  823. uint64_t *handleOut)
  824. {
  825. uint32_t handle;
  826. uint32_t check;
  827. void *newHandles = NULL;
  828. int found = 0;
  829. void *instance;
  830. int i;
  831. hdb_lock (handleDatabase);
  832. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  833. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  834. found = 1;
  835. break;
  836. }
  837. }
  838. if (found == 0) {
  839. handleDatabase->handleCount += 1;
  840. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  841. sizeof (struct saHandle) * handleDatabase->handleCount);
  842. if (newHandles == NULL) {
  843. hdb_unlock (handleDatabase);
  844. return (CS_ERR_NO_MEMORY);
  845. }
  846. handleDatabase->handles = newHandles;
  847. }
  848. instance = malloc (instanceSize);
  849. if (instance == 0) {
  850. free (newHandles);
  851. hdb_unlock (handleDatabase);
  852. return (CS_ERR_NO_MEMORY);
  853. }
  854. /*
  855. * This code makes sure the random number isn't zero
  856. * We use 0 to specify an invalid handle out of the 1^64 address space
  857. * If we get 0 200 times in a row, the RNG may be broken
  858. */
  859. for (i = 0; i < 200; i++) {
  860. check = random();
  861. if (check != 0) {
  862. break;
  863. }
  864. }
  865. memset (instance, 0, instanceSize);
  866. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  867. handleDatabase->handles[handle].instance = instance;
  868. handleDatabase->handles[handle].refCount = 1;
  869. handleDatabase->handles[handle].check = check;
  870. *handleOut = (uint64_t)((uint64_t)check << 32 | handle);
  871. hdb_unlock (handleDatabase);
  872. return (CS_OK);
  873. }
  874. cs_error_t
  875. saHandleDestroy (
  876. struct saHandleDatabase *handleDatabase,
  877. uint64_t inHandle)
  878. {
  879. cs_error_t error = CS_OK;
  880. uint32_t check = inHandle >> 32;
  881. uint32_t handle = inHandle & 0xffffffff;
  882. hdb_lock (handleDatabase);
  883. if (check != handleDatabase->handles[handle].check) {
  884. hdb_unlock (handleDatabase);
  885. error = CS_ERR_BAD_HANDLE;
  886. return (error);
  887. }
  888. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  889. hdb_unlock (handleDatabase);
  890. saHandleInstancePut (handleDatabase, inHandle);
  891. return (error);
  892. }
  893. cs_error_t
  894. saHandleInstanceGet (
  895. struct saHandleDatabase *handleDatabase,
  896. uint64_t inHandle,
  897. void **instance)
  898. {
  899. uint32_t check = inHandle >> 32;
  900. uint32_t handle = inHandle & 0xffffffff;
  901. cs_error_t error = CS_OK;
  902. hdb_lock (handleDatabase);
  903. if (handle >= (uint64_t)handleDatabase->handleCount) {
  904. error = CS_ERR_BAD_HANDLE;
  905. goto error_exit;
  906. }
  907. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  908. error = CS_ERR_BAD_HANDLE;
  909. goto error_exit;
  910. }
  911. if (check != handleDatabase->handles[handle].check) {
  912. error = CS_ERR_BAD_HANDLE;
  913. goto error_exit;
  914. }
  915. *instance = handleDatabase->handles[handle].instance;
  916. handleDatabase->handles[handle].refCount += 1;
  917. error_exit:
  918. hdb_unlock (handleDatabase);
  919. return (error);
  920. }
  921. cs_error_t
  922. saHandleInstancePut (
  923. struct saHandleDatabase *handleDatabase,
  924. uint64_t inHandle)
  925. {
  926. void *instance;
  927. cs_error_t error = CS_OK;
  928. uint32_t check = inHandle >> 32;
  929. uint32_t handle = inHandle & 0xffffffff;
  930. hdb_lock (handleDatabase);
  931. if (check != handleDatabase->handles[handle].check) {
  932. error = CS_ERR_BAD_HANDLE;
  933. goto error_exit;
  934. }
  935. handleDatabase->handles[handle].refCount -= 1;
  936. assert (handleDatabase->handles[handle].refCount >= 0);
  937. if (handleDatabase->handles[handle].refCount == 0) {
  938. instance = (handleDatabase->handles[handle].instance);
  939. handleDatabase->handleInstanceDestructor (instance);
  940. free (instance);
  941. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  942. }
  943. error_exit:
  944. hdb_unlock (handleDatabase);
  945. return (error);
  946. }