coroipc.c 19 KB

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