coroipcc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. coroipcc_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. coroipcc_memory_unmap (void *addr, size_t bytes)
  288. {
  289. int res;
  290. res = munmap (addr, bytes);
  291. }
  292. cs_error_t
  293. coroipcc_service_connect (
  294. const char *socket_name,
  295. enum service_types service,
  296. void **shmseg)
  297. {
  298. int request_fd;
  299. struct sockaddr_un address;
  300. cs_error_t error;
  301. struct ipc_segment *ipc_segment;
  302. key_t shmkey = 0;
  303. key_t semkey = 0;
  304. int res;
  305. mar_req_setup_t req_setup;
  306. mar_res_setup_t res_setup;
  307. union semun semun;
  308. char dispatch_map_path[128];
  309. res_setup.error = CS_ERR_LIBRARY;
  310. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  311. if (request_fd == -1) {
  312. return (-1);
  313. }
  314. memset (&address, 0, sizeof (struct sockaddr_un));
  315. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  316. address.sun_len = sizeof(struct sockaddr_un);
  317. #endif
  318. address.sun_family = PF_UNIX;
  319. #if defined(COROSYNC_LINUX)
  320. sprintf (address.sun_path + 1, "%s", socket_name);
  321. #else
  322. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  323. #endif
  324. res = connect (request_fd, (struct sockaddr *)&address,
  325. AIS_SUN_LEN(&address));
  326. if (res == -1) {
  327. close (request_fd);
  328. return (CS_ERR_TRY_AGAIN);
  329. }
  330. ipc_segment = malloc (sizeof (struct ipc_segment));
  331. if (ipc_segment == NULL) {
  332. close (request_fd);
  333. return (-1);
  334. }
  335. bzero (ipc_segment, sizeof (struct ipc_segment));
  336. /*
  337. * Allocate a shared memory segment
  338. */
  339. while (1) {
  340. shmkey = random();
  341. if ((ipc_segment->shmid
  342. = shmget (shmkey, sizeof (struct shared_memory),
  343. IPC_CREAT|IPC_EXCL|0600)) != -1) {
  344. break;
  345. }
  346. if (errno != EEXIST) {
  347. goto error_exit;
  348. }
  349. }
  350. /*
  351. * Allocate a semaphore segment
  352. */
  353. while (1) {
  354. semkey = random();
  355. ipc_segment->euid = geteuid ();
  356. if ((ipc_segment->semid
  357. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  358. break;
  359. }
  360. if (errno != EEXIST) {
  361. goto error_exit;
  362. }
  363. }
  364. /*
  365. * Attach to shared memory segment
  366. */
  367. ipc_segment->shared_memory = shmat (ipc_segment->shmid, NULL, 0);
  368. if (ipc_segment->shared_memory == (void *)-1) {
  369. goto error_exit;
  370. }
  371. semun.val = 0;
  372. res = semctl (ipc_segment->semid, 0, SETVAL, semun);
  373. if (res != 0) {
  374. goto error_exit;
  375. }
  376. res = semctl (ipc_segment->semid, 1, SETVAL, semun);
  377. if (res != 0) {
  378. goto error_exit;
  379. }
  380. res = coroipcc_memory_map (dispatch_map_path,
  381. "dispatch_bufer-XXXXXX",
  382. &ipc_segment->dispatch_buffer, DISPATCH_SIZE);
  383. strcpy (req_setup.dispatch_file, dispatch_map_path);
  384. req_setup.shmkey = shmkey;
  385. req_setup.semkey = semkey;
  386. req_setup.service = service;
  387. error = coroipcc_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  388. if (error != 0) {
  389. goto error_exit;
  390. }
  391. error = coroipcc_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  392. if (error != 0) {
  393. goto error_exit;
  394. }
  395. ipc_segment->fd = request_fd;
  396. ipc_segment->flow_control_state = 0;
  397. *shmseg = ipc_segment;
  398. /*
  399. * Something go wrong with server
  400. * Cleanup all
  401. */
  402. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  403. goto error_exit;
  404. }
  405. return (res_setup.error);
  406. error_exit:
  407. close (request_fd);
  408. if (ipc_segment->shmid > 0)
  409. shmctl (ipc_segment->shmid, IPC_RMID, NULL);
  410. if (ipc_segment->semid > 0)
  411. semctl (ipc_segment->semid, 0, IPC_RMID);
  412. return (res_setup.error);
  413. }
  414. cs_error_t
  415. coroipcc_service_disconnect (
  416. void *ipc_context)
  417. {
  418. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  419. shutdown (ipc_segment->fd, SHUT_RDWR);
  420. close (ipc_segment->fd);
  421. shmdt (ipc_segment->shared_memory);
  422. coroipcc_memory_unmap (ipc_segment->dispatch_buffer, (DISPATCH_SIZE));
  423. free (ipc_segment);
  424. return (CS_OK);
  425. }
  426. int
  427. coroipcc_dispatch_flow_control_get (
  428. void *ipc_context)
  429. {
  430. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  431. return (ipc_segment->flow_control_state);
  432. }
  433. int
  434. coroipcc_fd_get (void *ipc_ctx)
  435. {
  436. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  437. return (ipc_segment->fd);
  438. }
  439. int
  440. coroipcc_dispatch_get (void *ipc_ctx, void **data, int timeout)
  441. {
  442. struct pollfd ufds;
  443. int poll_events;
  444. char buf;
  445. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  446. int res;
  447. char buf_two = 1;
  448. char *data_addr;
  449. ufds.fd = ipc_segment->fd;
  450. ufds.events = POLLIN;
  451. ufds.revents = 0;
  452. retry_poll:
  453. poll_events = poll (&ufds, 1, timeout);
  454. if (poll_events == -1 && errno == EINTR) {
  455. goto retry_poll;
  456. } else
  457. if (poll_events == -1) {
  458. return (-1);
  459. } else
  460. if (poll_events == 0) {
  461. return (0);
  462. }
  463. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  464. return (-1);
  465. }
  466. retry_recv:
  467. res = recv (ipc_segment->fd, &buf, 1, 0);
  468. if (res == -1 && errno == EINTR) {
  469. goto retry_recv;
  470. } else
  471. if (res == -1) {
  472. return (-1);
  473. }
  474. if (res == 0) {
  475. return (-1);
  476. }
  477. ipc_segment->flow_control_state = 0;
  478. if (buf == 1 || buf == 2) {
  479. ipc_segment->flow_control_state = 1;
  480. }
  481. /*
  482. * Notify executive to flush any pending dispatch messages
  483. */
  484. if (ipc_segment->flow_control_state) {
  485. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  486. res = coroipcc_send (ipc_segment->fd, &buf_two, 1);
  487. assert (res == 0); //TODO
  488. }
  489. /*
  490. * This is just a notification of flow control starting at the addition
  491. * of a new pending message, not a message to dispatch
  492. */
  493. if (buf == 2) {
  494. return (0);
  495. }
  496. if (buf == 3) {
  497. return (0);
  498. }
  499. data_addr = ipc_segment->dispatch_buffer;
  500. data_addr = &data_addr[ipc_segment->shared_memory->read];
  501. *data = (void *)data_addr;
  502. return (1);
  503. }
  504. int
  505. coroipcc_dispatch_put (void *ipc_ctx)
  506. {
  507. struct sembuf sop;
  508. mar_res_header_t *header;
  509. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  510. int res;
  511. char *addr;
  512. unsigned int read_idx;
  513. sop.sem_num = 2;
  514. sop.sem_op = -1;
  515. sop.sem_flg = 0;
  516. retry_semop:
  517. res = semop (ipc_segment->semid, &sop, 1);
  518. if (res == -1 && errno == EINTR) {
  519. goto retry_semop;
  520. } else
  521. if (res == -1 && errno == EACCES) {
  522. priv_change_send (ipc_segment);
  523. goto retry_semop;
  524. } else
  525. if (res == -1) {
  526. return (-1);
  527. }
  528. addr = ipc_segment->dispatch_buffer;
  529. read_idx = ipc_segment->shared_memory->read;
  530. header = (mar_res_header_t *) &addr[read_idx];
  531. ipc_segment->shared_memory->read =
  532. (read_idx + header->size) % (DISPATCH_SIZE);
  533. return (0);
  534. }
  535. static cs_error_t
  536. coroipcc_msg_send (
  537. void *ipc_context,
  538. const struct iovec *iov,
  539. unsigned int iov_len)
  540. {
  541. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  542. struct sembuf sop;
  543. int i;
  544. int res;
  545. int req_buffer_idx = 0;
  546. for (i = 0; i < iov_len; i++) {
  547. memcpy (&ipc_segment->shared_memory->req_buffer[req_buffer_idx],
  548. iov[i].iov_base,
  549. iov[i].iov_len);
  550. req_buffer_idx += iov[i].iov_len;
  551. }
  552. /*
  553. * Signal semaphore #0 indicting a new message from client
  554. * to server request queue
  555. */
  556. sop.sem_num = 0;
  557. sop.sem_op = 1;
  558. sop.sem_flg = 0;
  559. retry_semop:
  560. res = semop (ipc_segment->semid, &sop, 1);
  561. if (res == -1 && errno == EINTR) {
  562. goto retry_semop;
  563. } else
  564. if (res == -1 && errno == EACCES) {
  565. priv_change_send (ipc_segment);
  566. goto retry_semop;
  567. } else
  568. if (res == -1) {
  569. return (CS_ERR_LIBRARY);
  570. }
  571. return (CS_OK);
  572. }
  573. static cs_error_t
  574. coroipcc_reply_receive (
  575. void *ipc_context,
  576. void *res_msg, size_t res_len)
  577. {
  578. struct sembuf sop;
  579. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  580. int res;
  581. /*
  582. * Wait for semaphore #1 indicating a new message from server
  583. * to client in the response queue
  584. */
  585. sop.sem_num = 1;
  586. sop.sem_op = -1;
  587. sop.sem_flg = 0;
  588. retry_semop:
  589. res = semop (ipc_segment->semid, &sop, 1);
  590. if (res == -1 && errno == EINTR) {
  591. goto retry_semop;
  592. } else
  593. if (res == -1 && errno == EACCES) {
  594. priv_change_send (ipc_segment);
  595. goto retry_semop;
  596. } else
  597. if (res == -1) {
  598. return (CS_ERR_LIBRARY);
  599. }
  600. memcpy (res_msg, ipc_segment->shared_memory->res_buffer, res_len);
  601. return (CS_OK);
  602. }
  603. static cs_error_t
  604. coroipcc_reply_receive_in_buf (
  605. void *ipc_context,
  606. void **res_msg)
  607. {
  608. struct sembuf sop;
  609. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  610. int res;
  611. /*
  612. * Wait for semaphore #1 indicating a new message from server
  613. * to client in the response queue
  614. */
  615. sop.sem_num = 1;
  616. sop.sem_op = -1;
  617. sop.sem_flg = 0;
  618. retry_semop:
  619. res = semop (ipc_segment->semid, &sop, 1);
  620. if (res == -1 && errno == EINTR) {
  621. goto retry_semop;
  622. } else
  623. if (res == -1 && errno == EACCES) {
  624. priv_change_send (ipc_segment);
  625. goto retry_semop;
  626. } else
  627. if (res == -1) {
  628. return (CS_ERR_LIBRARY);
  629. }
  630. *res_msg = (char *)ipc_segment->shared_memory->res_buffer;
  631. return (CS_OK);
  632. }
  633. cs_error_t
  634. coroipcc_msg_send_reply_receive (
  635. void *ipc_context,
  636. const struct iovec *iov,
  637. unsigned int iov_len,
  638. void *res_msg,
  639. size_t res_len)
  640. {
  641. cs_error_t res;
  642. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  643. if (res != CS_OK) {
  644. return (res);
  645. }
  646. res = coroipcc_reply_receive (ipc_context, res_msg, res_len);
  647. if (res != CS_OK) {
  648. return (res);
  649. }
  650. return (CS_OK);
  651. }
  652. cs_error_t
  653. coroipcc_msg_send_reply_receive_in_buf (
  654. void *ipc_context,
  655. const struct iovec *iov,
  656. unsigned int iov_len,
  657. void **res_msg)
  658. {
  659. unsigned int res;
  660. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  661. if (res != CS_OK) {
  662. return (res);
  663. }
  664. res = coroipcc_reply_receive_in_buf (ipc_context, res_msg);
  665. if (res != CS_OK) {
  666. return (res);
  667. }
  668. return (CS_OK);
  669. }
  670. cs_error_t
  671. saHandleCreate (
  672. struct saHandleDatabase *handleDatabase,
  673. int instanceSize,
  674. uint64_t *handleOut)
  675. {
  676. uint32_t handle;
  677. uint32_t check;
  678. void *newHandles = NULL;
  679. int found = 0;
  680. void *instance;
  681. int i;
  682. pthread_mutex_lock (&handleDatabase->mutex);
  683. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  684. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  685. found = 1;
  686. break;
  687. }
  688. }
  689. if (found == 0) {
  690. handleDatabase->handleCount += 1;
  691. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  692. sizeof (struct saHandle) * handleDatabase->handleCount);
  693. if (newHandles == NULL) {
  694. pthread_mutex_unlock (&handleDatabase->mutex);
  695. return (CS_ERR_NO_MEMORY);
  696. }
  697. handleDatabase->handles = newHandles;
  698. }
  699. instance = malloc (instanceSize);
  700. if (instance == 0) {
  701. free (newHandles);
  702. pthread_mutex_unlock (&handleDatabase->mutex);
  703. return (CS_ERR_NO_MEMORY);
  704. }
  705. /*
  706. * This code makes sure the random number isn't zero
  707. * We use 0 to specify an invalid handle out of the 1^64 address space
  708. * If we get 0 200 times in a row, the RNG may be broken
  709. */
  710. for (i = 0; i < 200; i++) {
  711. check = random();
  712. if (check != 0) {
  713. break;
  714. }
  715. }
  716. memset (instance, 0, instanceSize);
  717. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  718. handleDatabase->handles[handle].instance = instance;
  719. handleDatabase->handles[handle].refCount = 1;
  720. handleDatabase->handles[handle].check = check;
  721. *handleOut = (uint64_t)((uint64_t)check << 32 | handle);
  722. pthread_mutex_unlock (&handleDatabase->mutex);
  723. return (CS_OK);
  724. }
  725. cs_error_t
  726. saHandleDestroy (
  727. struct saHandleDatabase *handleDatabase,
  728. uint64_t inHandle)
  729. {
  730. cs_error_t error = CS_OK;
  731. uint32_t check = inHandle >> 32;
  732. uint32_t handle = inHandle & 0xffffffff;
  733. pthread_mutex_lock (&handleDatabase->mutex);
  734. if (check != handleDatabase->handles[handle].check) {
  735. pthread_mutex_unlock (&handleDatabase->mutex);
  736. error = CS_ERR_BAD_HANDLE;
  737. return (error);
  738. }
  739. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  740. pthread_mutex_unlock (&handleDatabase->mutex);
  741. saHandleInstancePut (handleDatabase, inHandle);
  742. return (error);
  743. }
  744. cs_error_t
  745. saHandleInstanceGet (
  746. struct saHandleDatabase *handleDatabase,
  747. uint64_t inHandle,
  748. void **instance)
  749. {
  750. uint32_t check = inHandle >> 32;
  751. uint32_t handle = inHandle & 0xffffffff;
  752. cs_error_t error = CS_OK;
  753. pthread_mutex_lock (&handleDatabase->mutex);
  754. if (handle >= (uint64_t)handleDatabase->handleCount) {
  755. error = CS_ERR_BAD_HANDLE;
  756. goto error_exit;
  757. }
  758. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  759. error = CS_ERR_BAD_HANDLE;
  760. goto error_exit;
  761. }
  762. if (check != handleDatabase->handles[handle].check) {
  763. error = CS_ERR_BAD_HANDLE;
  764. goto error_exit;
  765. }
  766. *instance = handleDatabase->handles[handle].instance;
  767. handleDatabase->handles[handle].refCount += 1;
  768. error_exit:
  769. pthread_mutex_unlock (&handleDatabase->mutex);
  770. return (error);
  771. }
  772. cs_error_t
  773. saHandleInstancePut (
  774. struct saHandleDatabase *handleDatabase,
  775. uint64_t inHandle)
  776. {
  777. void *instance;
  778. cs_error_t error = CS_OK;
  779. uint32_t check = inHandle >> 32;
  780. uint32_t handle = inHandle & 0xffffffff;
  781. pthread_mutex_lock (&handleDatabase->mutex);
  782. if (check != handleDatabase->handles[handle].check) {
  783. error = CS_ERR_BAD_HANDLE;
  784. goto error_exit;
  785. }
  786. handleDatabase->handles[handle].refCount -= 1;
  787. assert (handleDatabase->handles[handle].refCount >= 0);
  788. if (handleDatabase->handles[handle].refCount == 0) {
  789. instance = (handleDatabase->handles[handle].instance);
  790. handleDatabase->handleInstanceDestructor (instance);
  791. free (instance);
  792. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  793. }
  794. error_exit:
  795. pthread_mutex_unlock (&handleDatabase->mutex);
  796. return (error);
  797. }