coroipcc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 <corosync/corotypes.h>
  58. #include <corosync/ipc_gen.h>
  59. #include <corosync/coroipcc.h>
  60. enum SA_HANDLE_STATE {
  61. SA_HANDLE_STATE_EMPTY,
  62. SA_HANDLE_STATE_PENDINGREMOVAL,
  63. SA_HANDLE_STATE_ACTIVE
  64. };
  65. struct saHandle {
  66. int state;
  67. void *instance;
  68. int refCount;
  69. uint32_t check;
  70. };
  71. struct ipc_segment {
  72. int fd;
  73. int shmid;
  74. int semid;
  75. int flow_control_state;
  76. struct shared_memory *shared_memory;
  77. uid_t euid;
  78. };
  79. #if defined(COROSYNC_LINUX)
  80. /* SUN_LEN is broken for abstract namespace
  81. */
  82. #define AIS_SUN_LEN(a) sizeof(*(a))
  83. #else
  84. #define AIS_SUN_LEN(a) SUN_LEN(a)
  85. #endif
  86. #ifdef SO_NOSIGPIPE
  87. void socket_nosigpipe(int s)
  88. {
  89. int on = 1;
  90. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  91. }
  92. #endif
  93. #ifndef MSG_NOSIGNAL
  94. #define MSG_NOSIGNAL 0
  95. #endif
  96. static int
  97. coroipcc_send (
  98. int s,
  99. void *msg,
  100. size_t len)
  101. {
  102. int result;
  103. struct msghdr msg_send;
  104. struct iovec iov_send;
  105. char *rbuf = msg;
  106. int processed = 0;
  107. msg_send.msg_iov = &iov_send;
  108. msg_send.msg_iovlen = 1;
  109. msg_send.msg_name = 0;
  110. msg_send.msg_namelen = 0;
  111. msg_send.msg_control = 0;
  112. msg_send.msg_controllen = 0;
  113. msg_send.msg_flags = 0;
  114. retry_send:
  115. iov_send.iov_base = &rbuf[processed];
  116. iov_send.iov_len = len - processed;
  117. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  118. /*
  119. * return immediately on any kind of syscall error that maps to
  120. * CS_ERR if no part of message has been sent
  121. */
  122. if (result == -1 && processed == 0) {
  123. if (errno == EINTR) {
  124. goto error_exit;
  125. }
  126. if (errno == EAGAIN) {
  127. goto error_exit;
  128. }
  129. if (errno == EFAULT) {
  130. goto error_exit;
  131. }
  132. }
  133. /*
  134. * retry read operations that are already started except
  135. * for fault in that case, return ERR_LIBRARY
  136. */
  137. if (result == -1 && processed > 0) {
  138. if (errno == EINTR) {
  139. goto retry_send;
  140. }
  141. if (errno == EAGAIN) {
  142. goto retry_send;
  143. }
  144. if (errno == EFAULT) {
  145. goto error_exit;
  146. }
  147. }
  148. /*
  149. * return ERR_LIBRARY on any other syscall error
  150. */
  151. if (result == -1) {
  152. goto error_exit;
  153. }
  154. processed += result;
  155. if (processed != len) {
  156. goto retry_send;
  157. }
  158. return (0);
  159. error_exit:
  160. return (-1);
  161. }
  162. static int
  163. coroipcc_recv (
  164. int s,
  165. void *msg,
  166. size_t len)
  167. {
  168. int error = 0;
  169. int result;
  170. struct msghdr msg_recv;
  171. struct iovec iov_recv;
  172. char *rbuf = msg;
  173. int processed = 0;
  174. msg_recv.msg_iov = &iov_recv;
  175. msg_recv.msg_iovlen = 1;
  176. msg_recv.msg_name = 0;
  177. msg_recv.msg_namelen = 0;
  178. msg_recv.msg_control = 0;
  179. msg_recv.msg_controllen = 0;
  180. msg_recv.msg_flags = 0;
  181. retry_recv:
  182. iov_recv.iov_base = (void *)&rbuf[processed];
  183. iov_recv.iov_len = len - processed;
  184. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL|MSG_WAITALL);
  185. if (result == -1 && errno == EINTR) {
  186. goto retry_recv;
  187. }
  188. if (result == -1 && errno == EAGAIN) {
  189. goto retry_recv;
  190. }
  191. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  192. /* On many OS poll never return POLLHUP or POLLERR.
  193. * EOF is detected when recvmsg return 0.
  194. */
  195. if (result == 0) {
  196. error = -1;
  197. goto error_exit;
  198. }
  199. #endif
  200. if (result == -1 || result == 0) {
  201. error = -1;
  202. goto error_exit;
  203. }
  204. processed += result;
  205. if (processed != len) {
  206. goto retry_recv;
  207. }
  208. assert (processed == len);
  209. error_exit:
  210. return (0);
  211. }
  212. static int
  213. priv_change_send (struct ipc_segment *ipc_segment)
  214. {
  215. char buf_req;
  216. mar_req_priv_change req_priv_change;
  217. unsigned int res;
  218. req_priv_change.euid = geteuid();
  219. /*
  220. * Don't resend request unless euid has changed
  221. */
  222. if (ipc_segment->euid == req_priv_change.euid) {
  223. return (0);
  224. }
  225. req_priv_change.egid = getegid();
  226. buf_req = MESSAGE_REQ_CHANGE_EUID;
  227. res = coroipcc_send (ipc_segment->fd, &buf_req, 1);
  228. if (res == -1) {
  229. return (-1);
  230. }
  231. res = coroipcc_send (ipc_segment->fd, &req_priv_change,
  232. sizeof (req_priv_change));
  233. if (res == -1) {
  234. return (-1);
  235. }
  236. ipc_segment->euid = req_priv_change.euid;
  237. return (0);
  238. }
  239. #if defined(_SEM_SEMUN_UNDEFINED)
  240. union semun {
  241. int val;
  242. struct semid_ds *buf;
  243. unsigned short int *array;
  244. struct seminfo *__buf;
  245. };
  246. #endif
  247. cs_error_t
  248. coroipcc_service_connect (
  249. const char *socket_name,
  250. enum service_types service,
  251. void **shmseg)
  252. {
  253. int request_fd;
  254. struct sockaddr_un address;
  255. cs_error_t error;
  256. struct ipc_segment *ipc_segment;
  257. key_t shmkey = 0;
  258. key_t semkey = 0;
  259. int res;
  260. mar_req_setup_t req_setup;
  261. mar_res_setup_t res_setup;
  262. union semun semun;
  263. res_setup.error = CS_ERR_LIBRARY;
  264. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  265. if (request_fd == -1) {
  266. return (-1);
  267. }
  268. memset (&address, 0, sizeof (struct sockaddr_un));
  269. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  270. address.sun_len = sizeof(struct sockaddr_un);
  271. #endif
  272. address.sun_family = PF_UNIX;
  273. #if defined(COROSYNC_LINUX)
  274. sprintf (address.sun_path + 1, "%s", socket_name);
  275. #else
  276. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  277. #endif
  278. res = connect (request_fd, (struct sockaddr *)&address,
  279. AIS_SUN_LEN(&address));
  280. if (res == -1) {
  281. close (request_fd);
  282. return (CS_ERR_TRY_AGAIN);
  283. }
  284. ipc_segment = malloc (sizeof (struct ipc_segment));
  285. if (ipc_segment == NULL) {
  286. close (request_fd);
  287. return (-1);
  288. }
  289. bzero (ipc_segment, sizeof (struct ipc_segment));
  290. /*
  291. * Allocate a shared memory segment
  292. */
  293. while (1) {
  294. shmkey = random();
  295. if ((ipc_segment->shmid
  296. = shmget (shmkey, sizeof (struct shared_memory),
  297. IPC_CREAT|IPC_EXCL|0600)) != -1) {
  298. break;
  299. }
  300. if (errno != EEXIST) {
  301. goto error_exit;
  302. }
  303. }
  304. /*
  305. * Allocate a semaphore segment
  306. */
  307. while (1) {
  308. semkey = random();
  309. ipc_segment->euid = geteuid ();
  310. if ((ipc_segment->semid
  311. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  312. break;
  313. }
  314. if (errno != EEXIST) {
  315. goto error_exit;
  316. }
  317. }
  318. /*
  319. * Attach to shared memory segment
  320. */
  321. ipc_segment->shared_memory = shmat (ipc_segment->shmid, NULL, 0);
  322. if (ipc_segment->shared_memory == (void *)-1) {
  323. goto error_exit;
  324. }
  325. semun.val = 0;
  326. res = semctl (ipc_segment->semid, 0, SETVAL, semun);
  327. if (res != 0) {
  328. goto error_exit;
  329. }
  330. res = semctl (ipc_segment->semid, 1, SETVAL, semun);
  331. if (res != 0) {
  332. goto error_exit;
  333. }
  334. req_setup.shmkey = shmkey;
  335. req_setup.semkey = semkey;
  336. req_setup.service = service;
  337. error = coroipcc_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  338. if (error != 0) {
  339. goto error_exit;
  340. }
  341. error = coroipcc_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  342. if (error != 0) {
  343. goto error_exit;
  344. }
  345. ipc_segment->fd = request_fd;
  346. ipc_segment->flow_control_state = 0;
  347. *shmseg = ipc_segment;
  348. /*
  349. * Something go wrong with server
  350. * Cleanup all
  351. */
  352. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  353. goto error_exit;
  354. }
  355. return (res_setup.error);
  356. error_exit:
  357. close (request_fd);
  358. if (ipc_segment->shmid > 0)
  359. shmctl (ipc_segment->shmid, IPC_RMID, NULL);
  360. if (ipc_segment->semid > 0)
  361. semctl (ipc_segment->semid, 0, IPC_RMID);
  362. return (res_setup.error);
  363. }
  364. cs_error_t
  365. coroipcc_service_disconnect (
  366. void *ipc_context)
  367. {
  368. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  369. shutdown (ipc_segment->fd, SHUT_RDWR);
  370. close (ipc_segment->fd);
  371. shmdt (ipc_segment->shared_memory);
  372. free (ipc_segment);
  373. return (CS_OK);
  374. }
  375. int
  376. coroipcc_dispatch_flow_control_get (
  377. void *ipc_context)
  378. {
  379. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  380. return (ipc_segment->flow_control_state);
  381. }
  382. int
  383. coroipcc_fd_get (void *ipc_ctx)
  384. {
  385. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  386. return (ipc_segment->fd);
  387. }
  388. static void memcpy_swrap (void *dest, size_t dest_len,
  389. void *src, int len, unsigned int *read)
  390. {
  391. char *dest_chr = (char *)dest;
  392. char *src_chr = (char *)src;
  393. unsigned int first_read;
  394. unsigned int second_read;
  395. first_read = len;
  396. second_read = 0;
  397. if (len + *read >= DISPATCH_SIZE) {
  398. first_read = DISPATCH_SIZE - *read;
  399. second_read = (len + *read) % DISPATCH_SIZE;
  400. }
  401. memcpy (dest_chr, &src_chr[*read], first_read);
  402. if (second_read) {
  403. memcpy (&dest_chr[first_read], src_chr,
  404. second_read);
  405. }
  406. *read = (*read + len) % (DISPATCH_SIZE);
  407. }
  408. int original_flow = -1;
  409. int
  410. coroipcc_dispatch_recv (void *ipc_ctx, void *data, size_t buflen, int timeout)
  411. {
  412. struct pollfd ufds;
  413. struct sembuf sop;
  414. int poll_events;
  415. mar_res_header_t *header;
  416. char buf;
  417. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  418. int res;
  419. unsigned int my_read;
  420. char buf_two = 1;
  421. ufds.fd = ipc_segment->fd;
  422. ufds.events = POLLIN;
  423. ufds.revents = 0;
  424. retry_poll:
  425. poll_events = poll (&ufds, 1, timeout);
  426. if (poll_events == -1 && errno == EINTR) {
  427. goto retry_poll;
  428. } else
  429. if (poll_events == -1) {
  430. return (-1);
  431. } else
  432. if (poll_events == 0) {
  433. return (0);
  434. }
  435. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  436. return (-1);
  437. }
  438. retry_recv:
  439. res = recv (ipc_segment->fd, &buf, 1, 0);
  440. if (res == -1 && errno == EINTR) {
  441. goto retry_recv;
  442. } else
  443. if (res == -1) {
  444. return (-1);
  445. }
  446. if (res == 0) {
  447. return (-1);
  448. }
  449. ipc_segment->flow_control_state = 0;
  450. if (buf == 1 || buf == 2) {
  451. ipc_segment->flow_control_state = 1;
  452. }
  453. /*
  454. * Notify executive to flush any pending dispatch messages
  455. */
  456. if (ipc_segment->flow_control_state) {
  457. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  458. res = coroipcc_send (ipc_segment->fd, &buf_two, 1);
  459. assert (res == 0); //TODO
  460. }
  461. /*
  462. * This is just a notification of flow control starting at the addition
  463. * of a new pending message, not a message to dispatch
  464. */
  465. if (buf == 2) {
  466. return (0);
  467. }
  468. if (buf == 3) {
  469. return (0);
  470. }
  471. sop.sem_num = 2;
  472. sop.sem_op = -1;
  473. sop.sem_flg = 0;
  474. retry_semop:
  475. res = semop (ipc_segment->semid, &sop, 1);
  476. if (res == -1 && errno == EINTR) {
  477. goto retry_semop;
  478. } else
  479. if (res == -1 && errno == EACCES) {
  480. priv_change_send (ipc_segment);
  481. goto retry_semop;
  482. } else
  483. if (res == -1) {
  484. return (-1);
  485. }
  486. if (buflen < DISPATCH_SIZE) {
  487. return -1;
  488. }
  489. if (ipc_segment->shared_memory->read + sizeof (mar_res_header_t) >= DISPATCH_SIZE) {
  490. my_read = ipc_segment->shared_memory->read;
  491. memcpy_swrap (data, DISPATCH_SIZE,
  492. ipc_segment->shared_memory->dispatch_buffer,
  493. sizeof (mar_res_header_t),
  494. &ipc_segment->shared_memory->read);
  495. header = (mar_res_header_t *)data;
  496. memcpy_swrap (
  497. (void *)((char *)data + sizeof (mar_res_header_t)),
  498. DISPATCH_SIZE,
  499. ipc_segment->shared_memory->dispatch_buffer,
  500. header->size - sizeof (mar_res_header_t),
  501. &ipc_segment->shared_memory->read);
  502. } else {
  503. header = (mar_res_header_t *)&ipc_segment->shared_memory->dispatch_buffer[ipc_segment->shared_memory->read];
  504. memcpy_swrap (
  505. data, DISPATCH_SIZE,
  506. ipc_segment->shared_memory->dispatch_buffer,
  507. header->size,
  508. &ipc_segment->shared_memory->read);
  509. }
  510. return (1);
  511. }
  512. static cs_error_t
  513. coroipcc_msg_send (
  514. void *ipc_context,
  515. const struct iovec *iov,
  516. unsigned int iov_len)
  517. {
  518. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  519. struct sembuf sop;
  520. int i;
  521. int res;
  522. int req_buffer_idx = 0;
  523. for (i = 0; i < iov_len; i++) {
  524. memcpy (&ipc_segment->shared_memory->req_buffer[req_buffer_idx],
  525. iov[i].iov_base,
  526. iov[i].iov_len);
  527. req_buffer_idx += iov[i].iov_len;
  528. }
  529. /*
  530. * Signal semaphore #0 indicting a new message from client
  531. * to server request queue
  532. */
  533. sop.sem_num = 0;
  534. sop.sem_op = 1;
  535. sop.sem_flg = 0;
  536. retry_semop:
  537. res = semop (ipc_segment->semid, &sop, 1);
  538. if (res == -1 && errno == EINTR) {
  539. goto retry_semop;
  540. } else
  541. if (res == -1 && errno == EACCES) {
  542. priv_change_send (ipc_segment);
  543. goto retry_semop;
  544. } else
  545. if (res == -1) {
  546. return (CS_ERR_LIBRARY);
  547. }
  548. return (CS_OK);
  549. }
  550. static cs_error_t
  551. coroipcc_reply_receive (
  552. void *ipc_context,
  553. void *res_msg, size_t res_len)
  554. {
  555. struct sembuf sop;
  556. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  557. int res;
  558. /*
  559. * Wait for semaphore #1 indicating a new message from server
  560. * to client in the response queue
  561. */
  562. sop.sem_num = 1;
  563. sop.sem_op = -1;
  564. sop.sem_flg = 0;
  565. retry_semop:
  566. res = semop (ipc_segment->semid, &sop, 1);
  567. if (res == -1 && errno == EINTR) {
  568. goto retry_semop;
  569. } else
  570. if (res == -1 && errno == EACCES) {
  571. priv_change_send (ipc_segment);
  572. goto retry_semop;
  573. } else
  574. if (res == -1) {
  575. return (CS_ERR_LIBRARY);
  576. }
  577. memcpy (res_msg, ipc_segment->shared_memory->res_buffer, res_len);
  578. return (CS_OK);
  579. }
  580. static cs_error_t
  581. coroipcc_reply_receive_in_buf (
  582. void *ipc_context,
  583. void **res_msg)
  584. {
  585. struct sembuf sop;
  586. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  587. int res;
  588. /*
  589. * Wait for semaphore #1 indicating a new message from server
  590. * to client in the response queue
  591. */
  592. sop.sem_num = 1;
  593. sop.sem_op = -1;
  594. sop.sem_flg = 0;
  595. retry_semop:
  596. res = semop (ipc_segment->semid, &sop, 1);
  597. if (res == -1 && errno == EINTR) {
  598. goto retry_semop;
  599. } else
  600. if (res == -1 && errno == EACCES) {
  601. priv_change_send (ipc_segment);
  602. goto retry_semop;
  603. } else
  604. if (res == -1) {
  605. return (CS_ERR_LIBRARY);
  606. }
  607. *res_msg = (char *)ipc_segment->shared_memory->res_buffer;
  608. return (CS_OK);
  609. }
  610. cs_error_t
  611. coroipcc_msg_send_reply_receive (
  612. void *ipc_context,
  613. const struct iovec *iov,
  614. unsigned int iov_len,
  615. void *res_msg,
  616. size_t res_len)
  617. {
  618. cs_error_t res;
  619. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  620. if (res != CS_OK) {
  621. return (res);
  622. }
  623. res = coroipcc_reply_receive (ipc_context, res_msg, res_len);
  624. if (res != CS_OK) {
  625. return (res);
  626. }
  627. return (CS_OK);
  628. }
  629. cs_error_t
  630. coroipcc_msg_send_reply_receive_in_buf (
  631. void *ipc_context,
  632. const struct iovec *iov,
  633. unsigned int iov_len,
  634. void **res_msg)
  635. {
  636. unsigned int res;
  637. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  638. if (res != CS_OK) {
  639. return (res);
  640. }
  641. res = coroipcc_reply_receive_in_buf (ipc_context, res_msg);
  642. if (res != CS_OK) {
  643. return (res);
  644. }
  645. return (CS_OK);
  646. }
  647. cs_error_t
  648. saHandleCreate (
  649. struct saHandleDatabase *handleDatabase,
  650. int instanceSize,
  651. uint64_t *handleOut)
  652. {
  653. uint32_t handle;
  654. uint32_t check;
  655. void *newHandles = NULL;
  656. int found = 0;
  657. void *instance;
  658. int i;
  659. pthread_mutex_lock (&handleDatabase->mutex);
  660. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  661. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  662. found = 1;
  663. break;
  664. }
  665. }
  666. if (found == 0) {
  667. handleDatabase->handleCount += 1;
  668. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  669. sizeof (struct saHandle) * handleDatabase->handleCount);
  670. if (newHandles == NULL) {
  671. pthread_mutex_unlock (&handleDatabase->mutex);
  672. return (CS_ERR_NO_MEMORY);
  673. }
  674. handleDatabase->handles = newHandles;
  675. }
  676. instance = malloc (instanceSize);
  677. if (instance == 0) {
  678. free (newHandles);
  679. pthread_mutex_unlock (&handleDatabase->mutex);
  680. return (CS_ERR_NO_MEMORY);
  681. }
  682. /*
  683. * This code makes sure the random number isn't zero
  684. * We use 0 to specify an invalid handle out of the 1^64 address space
  685. * If we get 0 200 times in a row, the RNG may be broken
  686. */
  687. for (i = 0; i < 200; i++) {
  688. check = random();
  689. if (check != 0) {
  690. break;
  691. }
  692. }
  693. memset (instance, 0, instanceSize);
  694. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  695. handleDatabase->handles[handle].instance = instance;
  696. handleDatabase->handles[handle].refCount = 1;
  697. handleDatabase->handles[handle].check = check;
  698. *handleOut = (uint64_t)((uint64_t)check << 32 | handle);
  699. pthread_mutex_unlock (&handleDatabase->mutex);
  700. return (CS_OK);
  701. }
  702. cs_error_t
  703. saHandleDestroy (
  704. struct saHandleDatabase *handleDatabase,
  705. uint64_t inHandle)
  706. {
  707. cs_error_t error = CS_OK;
  708. uint32_t check = inHandle >> 32;
  709. uint32_t handle = inHandle & 0xffffffff;
  710. pthread_mutex_lock (&handleDatabase->mutex);
  711. if (check != handleDatabase->handles[handle].check) {
  712. pthread_mutex_unlock (&handleDatabase->mutex);
  713. error = CS_ERR_BAD_HANDLE;
  714. return (error);
  715. }
  716. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  717. pthread_mutex_unlock (&handleDatabase->mutex);
  718. saHandleInstancePut (handleDatabase, inHandle);
  719. return (error);
  720. }
  721. cs_error_t
  722. saHandleInstanceGet (
  723. struct saHandleDatabase *handleDatabase,
  724. uint64_t inHandle,
  725. void **instance)
  726. {
  727. uint32_t check = inHandle >> 32;
  728. uint32_t handle = inHandle & 0xffffffff;
  729. cs_error_t error = CS_OK;
  730. pthread_mutex_lock (&handleDatabase->mutex);
  731. if (handle >= (uint64_t)handleDatabase->handleCount) {
  732. error = CS_ERR_BAD_HANDLE;
  733. goto error_exit;
  734. }
  735. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  736. error = CS_ERR_BAD_HANDLE;
  737. goto error_exit;
  738. }
  739. if (check != handleDatabase->handles[handle].check) {
  740. error = CS_ERR_BAD_HANDLE;
  741. goto error_exit;
  742. }
  743. *instance = handleDatabase->handles[handle].instance;
  744. handleDatabase->handles[handle].refCount += 1;
  745. error_exit:
  746. pthread_mutex_unlock (&handleDatabase->mutex);
  747. return (error);
  748. }
  749. cs_error_t
  750. saHandleInstancePut (
  751. struct saHandleDatabase *handleDatabase,
  752. uint64_t inHandle)
  753. {
  754. void *instance;
  755. cs_error_t error = CS_OK;
  756. uint32_t check = inHandle >> 32;
  757. uint32_t handle = inHandle & 0xffffffff;
  758. pthread_mutex_lock (&handleDatabase->mutex);
  759. if (check != handleDatabase->handles[handle].check) {
  760. error = CS_ERR_BAD_HANDLE;
  761. goto error_exit;
  762. }
  763. handleDatabase->handles[handle].refCount -= 1;
  764. assert (handleDatabase->handles[handle].refCount >= 0);
  765. if (handleDatabase->handles[handle].refCount == 0) {
  766. instance = (handleDatabase->handles[handle].instance);
  767. handleDatabase->handleInstanceDestructor (instance);
  768. free (instance);
  769. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  770. }
  771. error_exit:
  772. pthread_mutex_unlock (&handleDatabase->mutex);
  773. return (error);
  774. }