coroipcc.c 19 KB

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