util.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <errno.h>
  40. #include <string.h>
  41. #include <fcntl.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/types.h>
  44. #include <sys/uio.h>
  45. #include <sys/socket.h>
  46. #include <sys/select.h>
  47. #include <sys/time.h>
  48. #include <sys/un.h>
  49. #include <net/if.h>
  50. #include <arpa/inet.h>
  51. #include <netinet/in.h>
  52. #include <assert.h>
  53. #include "../include/saAis.h"
  54. #include "../include/ipc_gen.h"
  55. #include "util.h"
  56. enum SA_HANDLE_STATE {
  57. SA_HANDLE_STATE_EMPTY,
  58. SA_HANDLE_STATE_PENDINGREMOVAL,
  59. SA_HANDLE_STATE_ACTIVE
  60. };
  61. struct saHandle {
  62. int state;
  63. void *instance;
  64. int refCount;
  65. uint32_t check;
  66. };
  67. #if defined(OPENAIS_LINUX)
  68. /* SUN_LEN is broken for abstract namespace
  69. */
  70. #define AIS_SUN_LEN(a) sizeof(*(a))
  71. static char *socketname = "libais.socket";
  72. #else
  73. #define AIS_SUN_LEN(a) SUN_LEN(a)
  74. static char *socketname = "/var/run/libais.socket";
  75. #endif
  76. #ifdef SO_NOSIGPIPE
  77. void socket_nosigpipe(int s)
  78. {
  79. int on = 1;
  80. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  81. }
  82. #endif
  83. SaAisErrorT
  84. saServiceConnect (
  85. int *fdOut,
  86. enum service_types service)
  87. {
  88. int fd;
  89. int result;
  90. struct sockaddr_un address;
  91. struct req_lib_response_init req_lib_response_init;
  92. struct res_lib_response_init res_lib_response_init;
  93. SaAisErrorT error;
  94. gid_t egid;
  95. /*
  96. * Allow set group id binaries to be authenticated
  97. */
  98. egid = getegid();
  99. setregid (egid, -1);
  100. memset (&address, 0, sizeof (struct sockaddr_un));
  101. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  102. address.sun_len = sizeof(struct sockaddr_un);
  103. #endif
  104. address.sun_family = PF_UNIX;
  105. #if defined(OPENAIS_LINUX)
  106. strcpy (address.sun_path + 1, socketname);
  107. #else
  108. strcpy (address.sun_path, socketname);
  109. #endif
  110. fd = socket (PF_UNIX, SOCK_STREAM, 0);
  111. if (fd == -1) {
  112. return (SA_AIS_ERR_NO_RESOURCES);
  113. }
  114. socket_nosigpipe (fd);
  115. result = connect (fd, (struct sockaddr *)&address, AIS_SUN_LEN(&address));
  116. if (result == -1) {
  117. return (SA_AIS_ERR_TRY_AGAIN);
  118. }
  119. result = fcntl (fd, F_SETFL, O_NONBLOCK);
  120. if (result == -1) {
  121. return (SA_AIS_ERR_TRY_AGAIN);
  122. }
  123. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  124. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  125. req_lib_response_init.resdis_header.service = service;
  126. error = saSendRetry (fd, &req_lib_response_init,
  127. sizeof (struct req_lib_response_init));
  128. if (error != SA_AIS_OK) {
  129. goto error_exit;
  130. }
  131. error = saRecvRetry (fd, &res_lib_response_init,
  132. sizeof (struct res_lib_response_init));
  133. if (error != SA_AIS_OK) {
  134. goto error_exit;
  135. }
  136. /*
  137. * Check for security errors
  138. */
  139. if (res_lib_response_init.header.error != SA_AIS_OK) {
  140. error = res_lib_response_init.header.error;
  141. goto error_exit;
  142. }
  143. *fdOut = fd;
  144. return (SA_AIS_OK);
  145. error_exit:
  146. close (fd);
  147. return (error);
  148. }
  149. SaAisErrorT
  150. saServiceConnectTwo (
  151. int *responseOut,
  152. int *callbackOut,
  153. enum service_types service)
  154. {
  155. int responseFD;
  156. int callbackFD;
  157. int result;
  158. struct sockaddr_un address;
  159. struct req_lib_response_init req_lib_response_init;
  160. struct res_lib_response_init res_lib_response_init;
  161. struct req_lib_dispatch_init req_lib_dispatch_init;
  162. struct res_lib_dispatch_init res_lib_dispatch_init;
  163. SaAisErrorT error;
  164. gid_t egid;
  165. /*
  166. * Allow set group id binaries to be authenticated
  167. */
  168. egid = getegid();
  169. setregid (egid, -1);
  170. memset (&address, 0, sizeof (struct sockaddr_un));
  171. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  172. address.sun_len = sizeof(struct sockaddr_un);
  173. #endif
  174. address.sun_family = PF_UNIX;
  175. #if defined(OPENAIS_LINUX)
  176. strcpy (address.sun_path + 1, socketname);
  177. #else
  178. strcpy (address.sun_path, socketname);
  179. #endif
  180. responseFD = socket (PF_UNIX, SOCK_STREAM, 0);
  181. if (responseFD == -1) {
  182. return (SA_AIS_ERR_NO_RESOURCES);
  183. }
  184. socket_nosigpipe (responseFD);
  185. result = connect (responseFD, (struct sockaddr *)&address, AIS_SUN_LEN(&address));
  186. if (result == -1) {
  187. close (responseFD);
  188. return (SA_AIS_ERR_TRY_AGAIN);
  189. }
  190. result = fcntl (responseFD, F_SETFL, O_NONBLOCK);
  191. if (result == -1) {
  192. close (responseFD);
  193. return (SA_AIS_ERR_TRY_AGAIN);
  194. }
  195. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  196. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  197. req_lib_response_init.resdis_header.service = service;
  198. error = saSendRetry (responseFD, &req_lib_response_init,
  199. sizeof (struct req_lib_response_init));
  200. if (error != SA_AIS_OK) {
  201. goto error_exit;
  202. }
  203. error = saRecvRetry (responseFD, &res_lib_response_init,
  204. sizeof (struct res_lib_response_init));
  205. if (error != SA_AIS_OK) {
  206. goto error_exit;
  207. }
  208. /*
  209. * Check for security errors
  210. */
  211. if (res_lib_response_init.header.error != SA_AIS_OK) {
  212. error = res_lib_response_init.header.error;
  213. goto error_exit;
  214. }
  215. *responseOut = responseFD;
  216. /* if I comment out the 4 lines below the executive crashes */
  217. callbackFD = socket (PF_UNIX, SOCK_STREAM, 0);
  218. if (callbackFD == -1) {
  219. return (SA_AIS_ERR_NO_RESOURCES);
  220. }
  221. socket_nosigpipe (callbackFD);
  222. result = connect (callbackFD, (struct sockaddr *)&address, AIS_SUN_LEN(&address));
  223. if (result == -1) {
  224. close (callbackFD);
  225. close (responseFD);
  226. return (SA_AIS_ERR_TRY_AGAIN);
  227. }
  228. result = fcntl (callbackFD, F_SETFL, O_NONBLOCK);
  229. if (result == -1) {
  230. close (callbackFD);
  231. close (responseFD);
  232. return (SA_AIS_ERR_TRY_AGAIN);
  233. }
  234. req_lib_dispatch_init.resdis_header.size = sizeof (req_lib_dispatch_init);
  235. req_lib_dispatch_init.resdis_header.id = MESSAGE_REQ_DISPATCH_INIT;
  236. req_lib_dispatch_init.resdis_header.service = service;
  237. req_lib_dispatch_init.conn_info = res_lib_response_init.conn_info;
  238. error = saSendRetry (callbackFD, &req_lib_dispatch_init,
  239. sizeof (struct req_lib_dispatch_init));
  240. if (error != SA_AIS_OK) {
  241. goto error_exit_two;
  242. }
  243. error = saRecvRetry (callbackFD, &res_lib_dispatch_init,
  244. sizeof (struct res_lib_dispatch_init));
  245. if (error != SA_AIS_OK) {
  246. goto error_exit_two;
  247. }
  248. /*
  249. * Check for security errors
  250. */
  251. if (res_lib_dispatch_init.header.error != SA_AIS_OK) {
  252. error = res_lib_dispatch_init.header.error;
  253. goto error_exit;
  254. }
  255. *callbackOut = callbackFD;
  256. return (SA_AIS_OK);
  257. error_exit_two:
  258. close (callbackFD);
  259. error_exit:
  260. close (responseFD);
  261. return (error);
  262. }
  263. SaAisErrorT
  264. saRecvRetry (
  265. int s,
  266. void *msg,
  267. size_t len)
  268. {
  269. SaAisErrorT error = SA_AIS_OK;
  270. int result;
  271. struct msghdr msg_recv;
  272. struct iovec iov_recv;
  273. char *rbuf = (char *)msg;
  274. int processed = 0;
  275. msg_recv.msg_iov = &iov_recv;
  276. msg_recv.msg_iovlen = 1;
  277. msg_recv.msg_name = 0;
  278. msg_recv.msg_namelen = 0;
  279. msg_recv.msg_control = 0;
  280. msg_recv.msg_controllen = 0;
  281. msg_recv.msg_flags = 0;
  282. retry_recv:
  283. iov_recv.iov_base = (void *)&rbuf[processed];
  284. iov_recv.iov_len = len - processed;
  285. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  286. if (result == -1 && errno == EINTR) {
  287. goto retry_recv;
  288. }
  289. if (result == -1 && errno == EAGAIN) {
  290. goto retry_recv;
  291. }
  292. if (result == -1 || result == 0) {
  293. error = SA_AIS_ERR_LIBRARY;
  294. goto error_exit;
  295. }
  296. processed += result;
  297. if (processed != len) {
  298. goto retry_recv;
  299. }
  300. assert (processed == len);
  301. error_exit:
  302. return (error);
  303. }
  304. struct res_overlay {
  305. struct res_header header;
  306. char payload[0];
  307. };
  308. SaAisErrorT
  309. saSendRetry (
  310. int s,
  311. const void *msg,
  312. size_t len)
  313. {
  314. SaAisErrorT error = SA_AIS_OK;
  315. int result;
  316. struct msghdr msg_send;
  317. struct iovec iov_send;
  318. char *rbuf = (char *)msg;
  319. int processed = 0;
  320. msg_send.msg_iov = &iov_send;
  321. msg_send.msg_iovlen = 1;
  322. msg_send.msg_name = 0;
  323. msg_send.msg_namelen = 0;
  324. msg_send.msg_control = 0;
  325. msg_send.msg_controllen = 0;
  326. msg_send.msg_flags = 0;
  327. retry_send:
  328. iov_send.iov_base = (void *)&rbuf[processed];
  329. iov_send.iov_len = len - processed;
  330. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  331. /*
  332. * return immediately on any kind of syscall error that maps to
  333. * SA_AIS_ERR if no part of message has been sent
  334. */
  335. if (result == -1 && processed == 0) {
  336. if (errno == EINTR) {
  337. error = SA_AIS_ERR_TRY_AGAIN;
  338. goto error_exit;
  339. }
  340. if (errno == EAGAIN) {
  341. error = SA_AIS_ERR_TRY_AGAIN;
  342. goto error_exit;
  343. }
  344. if (errno == EFAULT) {
  345. error = SA_AIS_ERR_INVALID_PARAM;
  346. goto error_exit;
  347. }
  348. }
  349. /*
  350. * retry read operations that are already started except
  351. * for fault in that case, return ERR_LIBRARY
  352. */
  353. if (result == -1 && processed > 0) {
  354. if (errno == EINTR) {
  355. goto retry_send;
  356. }
  357. if (errno == EAGAIN) {
  358. goto retry_send;
  359. }
  360. if (errno == EFAULT) {
  361. error = SA_AIS_ERR_LIBRARY;
  362. goto error_exit;
  363. }
  364. }
  365. /*
  366. * return ERR_LIBRARY on any other syscall error
  367. */
  368. if (result == -1) {
  369. error = SA_AIS_ERR_LIBRARY;
  370. goto error_exit;
  371. }
  372. processed += result;
  373. if (processed != len) {
  374. goto retry_send;
  375. }
  376. error_exit:
  377. return (error);
  378. }
  379. SaAisErrorT saSendMsgRetry (
  380. int s,
  381. struct iovec *iov,
  382. int iov_len)
  383. {
  384. SaAisErrorT error = SA_AIS_OK;
  385. int result;
  386. int total_size = 0;
  387. int i;
  388. int csize;
  389. int csize_cntr;
  390. int total_sent = 0;
  391. int iov_len_sendmsg = iov_len;
  392. struct iovec *iov_sendmsg = iov;
  393. struct iovec iovec_save;
  394. int iovec_saved_position = -1;
  395. struct msghdr msg_send;
  396. for (i = 0; i < iov_len; i++) {
  397. total_size += iov[i].iov_len;
  398. }
  399. msg_send.msg_iov = iov_sendmsg;
  400. msg_send.msg_iovlen = iov_len_sendmsg;
  401. msg_send.msg_name = 0;
  402. msg_send.msg_namelen = 0;
  403. msg_send.msg_control = 0;
  404. msg_send.msg_controllen = 0;
  405. msg_send.msg_flags = 0;
  406. retry_sendmsg:
  407. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  408. /*
  409. * Can't send now, and message not committed, so don't retry send
  410. */
  411. if (result == -1 && iovec_saved_position == -1) {
  412. if (errno == EINTR) {
  413. error = SA_AIS_ERR_TRY_AGAIN;
  414. goto error_exit;
  415. }
  416. if (errno == EAGAIN) {
  417. error = SA_AIS_ERR_TRY_AGAIN;
  418. goto error_exit;
  419. }
  420. if (errno == EFAULT) {
  421. error = SA_AIS_ERR_INVALID_PARAM;
  422. goto error_exit;
  423. }
  424. }
  425. /*
  426. * Retry (and block) if portion of message has already been written
  427. */
  428. if (result == -1 && iovec_saved_position != -1) {
  429. if (errno == EINTR) {
  430. goto retry_sendmsg;
  431. }
  432. if (errno == EAGAIN) {
  433. goto retry_sendmsg;
  434. }
  435. if (errno == EFAULT) {
  436. error = SA_AIS_ERR_LIBRARY;
  437. goto error_exit;
  438. }
  439. }
  440. /*
  441. * ERR_LIBRARY for any other syscall error
  442. */
  443. if (result == -1) {
  444. error = SA_AIS_ERR_LIBRARY;
  445. goto error_exit;
  446. }
  447. if (iovec_saved_position != -1) {
  448. memcpy (&iov[iovec_saved_position], &iovec_save, sizeof (struct iovec));
  449. }
  450. total_sent += result;
  451. if (total_sent != total_size) {
  452. for (i = 0, csize = 0, csize_cntr = 0; i < iov_len; i++) {
  453. csize += iov[i].iov_len;
  454. if (csize > total_sent) {
  455. break;
  456. }
  457. csize_cntr += iov[i].iov_len;
  458. }
  459. memcpy (&iovec_save, &iov[i], sizeof (struct iovec));
  460. iovec_saved_position = i;
  461. iov[i].iov_base = ((unsigned char *)(iov[i].iov_base)) +
  462. (total_sent - csize_cntr);
  463. iov[i].iov_len = total_size - total_sent;
  464. msg_send.msg_iov = &iov[i];
  465. msg_send.msg_iovlen = iov_len - i;
  466. goto retry_sendmsg;
  467. }
  468. error_exit:
  469. return (error);
  470. }
  471. SaAisErrorT saSendMsgReceiveReply (
  472. int s,
  473. struct iovec *iov,
  474. int iov_len,
  475. void *responseMessage,
  476. int responseLen)
  477. {
  478. SaAisErrorT error = SA_AIS_OK;
  479. error = saSendMsgRetry (s, iov, iov_len);
  480. if (error != SA_AIS_OK) {
  481. goto error_exit;
  482. }
  483. error = saRecvRetry (s, responseMessage, responseLen);
  484. if (error != SA_AIS_OK) {
  485. goto error_exit;
  486. }
  487. error_exit:
  488. return (error);
  489. }
  490. SaAisErrorT saSendReceiveReply (
  491. int s,
  492. void *requestMessage,
  493. int requestLen,
  494. void *responseMessage,
  495. int responseLen)
  496. {
  497. SaAisErrorT error = SA_AIS_OK;
  498. error = saSendRetry (s, requestMessage, requestLen);
  499. if (error != SA_AIS_OK) {
  500. goto error_exit;
  501. }
  502. error = saRecvRetry (s, responseMessage, responseLen);
  503. if (error != SA_AIS_OK) {
  504. goto error_exit;
  505. }
  506. error_exit:
  507. return (error);
  508. }
  509. SaAisErrorT
  510. saPollRetry (
  511. struct pollfd *ufds,
  512. unsigned int nfds,
  513. int timeout)
  514. {
  515. SaAisErrorT error = SA_AIS_OK;
  516. int result;
  517. retry_poll:
  518. result = poll (ufds, nfds, timeout);
  519. if (result == -1 && errno == EINTR) {
  520. goto retry_poll;
  521. }
  522. if (result == -1) {
  523. error = SA_AIS_ERR_LIBRARY;
  524. }
  525. return (error);
  526. }
  527. SaAisErrorT
  528. saHandleCreate (
  529. struct saHandleDatabase *handleDatabase,
  530. int instanceSize,
  531. SaUint64T *handleOut)
  532. {
  533. uint32_t handle;
  534. uint32_t check;
  535. void *newHandles;
  536. int found = 0;
  537. void *instance;
  538. pthread_mutex_lock (&handleDatabase->mutex);
  539. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  540. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  541. found = 1;
  542. break;
  543. }
  544. }
  545. if (found == 0) {
  546. handleDatabase->handleCount += 1;
  547. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  548. sizeof (struct saHandle) * handleDatabase->handleCount);
  549. if (newHandles == 0) {
  550. pthread_mutex_unlock (&handleDatabase->mutex);
  551. return (SA_AIS_ERR_NO_MEMORY);
  552. }
  553. handleDatabase->handles = newHandles;
  554. }
  555. instance = malloc (instanceSize);
  556. if (instance == 0) {
  557. return (SA_AIS_ERR_NO_MEMORY);
  558. }
  559. check = random();
  560. memset (instance, 0, instanceSize);
  561. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  562. handleDatabase->handles[handle].instance = instance;
  563. handleDatabase->handles[handle].refCount = 1;
  564. handleDatabase->handles[handle].check = check;
  565. *handleOut = (SaUint64T)((uint64_t)check << 32 | handle);
  566. pthread_mutex_unlock (&handleDatabase->mutex);
  567. return (SA_AIS_OK);
  568. }
  569. SaAisErrorT
  570. saHandleDestroy (
  571. struct saHandleDatabase *handleDatabase,
  572. SaUint64T inHandle)
  573. {
  574. SaAisErrorT error = SA_AIS_OK;
  575. uint32_t check = inHandle >> 32;
  576. uint32_t handle = inHandle & 0xffffffff;
  577. pthread_mutex_lock (&handleDatabase->mutex);
  578. if (check != handleDatabase->handles[handle].check) {
  579. pthread_mutex_unlock (&handleDatabase->mutex);
  580. error = SA_AIS_ERR_BAD_HANDLE;
  581. return (error);
  582. }
  583. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  584. pthread_mutex_unlock (&handleDatabase->mutex);
  585. saHandleInstancePut (handleDatabase, inHandle);
  586. return (error);
  587. }
  588. SaAisErrorT
  589. saHandleInstanceGet (
  590. struct saHandleDatabase *handleDatabase,
  591. SaUint64T inHandle,
  592. void **instance)
  593. {
  594. uint32_t check = inHandle >> 32;
  595. uint32_t handle = inHandle & 0xffffffff;
  596. SaAisErrorT error = SA_AIS_OK;
  597. pthread_mutex_lock (&handleDatabase->mutex);
  598. if (handle >= (SaUint64T)handleDatabase->handleCount) {
  599. error = SA_AIS_ERR_BAD_HANDLE;
  600. goto error_exit;
  601. }
  602. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  603. error = SA_AIS_ERR_BAD_HANDLE;
  604. goto error_exit;
  605. }
  606. if (check != handleDatabase->handles[handle].check) {
  607. error = SA_AIS_ERR_BAD_HANDLE;
  608. goto error_exit;
  609. }
  610. *instance = handleDatabase->handles[handle].instance;
  611. handleDatabase->handles[handle].refCount += 1;
  612. error_exit:
  613. pthread_mutex_unlock (&handleDatabase->mutex);
  614. return (error);
  615. }
  616. SaAisErrorT
  617. saHandleInstancePut (
  618. struct saHandleDatabase *handleDatabase,
  619. SaUint64T inHandle)
  620. {
  621. void *instance;
  622. SaAisErrorT error = SA_AIS_OK;
  623. uint32_t check = inHandle >> 32;
  624. uint32_t handle = inHandle & 0xffffffff;
  625. pthread_mutex_lock (&handleDatabase->mutex);
  626. if (check != handleDatabase->handles[handle].check) {
  627. error = SA_AIS_ERR_BAD_HANDLE;
  628. goto error_exit;
  629. }
  630. handleDatabase->handles[handle].refCount -= 1;
  631. assert (handleDatabase->handles[handle].refCount >= 0);
  632. if (handleDatabase->handles[handle].refCount == 0) {
  633. instance = (handleDatabase->handles[handle].instance);
  634. handleDatabase->handleInstanceDestructor (instance);
  635. free (instance);
  636. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  637. }
  638. error_exit:
  639. pthread_mutex_unlock (&handleDatabase->mutex);
  640. return (error);
  641. }
  642. SaAisErrorT
  643. saVersionVerify (
  644. struct saVersionDatabase *versionDatabase,
  645. SaVersionT *version)
  646. {
  647. int i;
  648. SaAisErrorT error = SA_AIS_ERR_VERSION;
  649. if (version == 0) {
  650. return (SA_AIS_ERR_INVALID_PARAM);
  651. }
  652. /*
  653. * Look for a release code that we support. If we find it then
  654. * make sure that the supported major version is >= to the required one.
  655. * In any case we return what we support in the version structure.
  656. */
  657. for (i = 0; i < versionDatabase->versionCount; i++) {
  658. /*
  659. * Check if the caller requires and old release code that we don't support.
  660. */
  661. if (version->releaseCode < versionDatabase->versionsSupported[i].releaseCode) {
  662. break;
  663. }
  664. /*
  665. * Check if we can support this release code.
  666. */
  667. if (version->releaseCode == versionDatabase->versionsSupported[i].releaseCode) {
  668. /*
  669. * Check if we can support the major version requested.
  670. */
  671. if (versionDatabase->versionsSupported[i].majorVersion >= version->majorVersion) {
  672. error = SA_AIS_OK;
  673. break;
  674. }
  675. /*
  676. * We support the release code, but not the major version.
  677. */
  678. break;
  679. }
  680. }
  681. /*
  682. * If we fall out of the if loop, the caller requires a release code
  683. * beyond what we support.
  684. */
  685. if (i == versionDatabase->versionCount) {
  686. i = versionDatabase->versionCount - 1;
  687. }
  688. /*
  689. * Tell the caller what we support
  690. */
  691. memcpy(version, &versionDatabase->versionsSupported[i], sizeof(*version));
  692. return (error);
  693. }
  694. /*
  695. * Get the time of day and convert to nanoseconds
  696. */
  697. SaTimeT clustTimeNow(void)
  698. {
  699. struct timeval tv;
  700. SaTimeT time_now;
  701. if (gettimeofday(&tv, 0)) {
  702. return 0ULL;
  703. }
  704. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  705. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  706. return time_now;
  707. }