util.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  5. * Copyright (c) 2006 Sun Microsystems, Inc.
  6. *
  7. * All rights reserved.
  8. *
  9. * Author: Steven Dake (sdake@mvista.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 "../include/saAis.h"
  55. #include "../include/ipc_gen.h"
  56. #include "util.h"
  57. enum SA_HANDLE_STATE {
  58. SA_HANDLE_STATE_EMPTY,
  59. SA_HANDLE_STATE_PENDINGREMOVAL,
  60. SA_HANDLE_STATE_ACTIVE
  61. };
  62. struct saHandle {
  63. int state;
  64. void *instance;
  65. int refCount;
  66. uint32_t check;
  67. };
  68. #ifdef OPENAIS_SOLARIS
  69. #define MSG_NOSIGNAL 0
  70. #endif
  71. #if defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS)
  72. /* SUN_LEN is broken for abstract namespace
  73. */
  74. #define AIS_SUN_LEN(a) sizeof(*(a))
  75. #else
  76. #define AIS_SUN_LEN(a) SUN_LEN(a)
  77. #endif
  78. #ifdef OPENAIS_LINUX
  79. static char *socketname = "libais.socket";
  80. #else
  81. static char *socketname = "/var/run/libais.socket";
  82. #endif
  83. #ifdef SO_NOSIGPIPE
  84. void socket_nosigpipe(int s)
  85. {
  86. int on = 1;
  87. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  88. }
  89. #endif
  90. SaAisErrorT
  91. saServiceConnect (
  92. int *responseOut,
  93. int *callbackOut,
  94. enum service_types service)
  95. {
  96. int responseFD;
  97. int callbackFD;
  98. int result;
  99. struct sockaddr_un address;
  100. mar_req_lib_response_init_t req_lib_response_init;
  101. mar_res_lib_response_init_t res_lib_response_init;
  102. mar_req_lib_dispatch_init_t req_lib_dispatch_init;
  103. mar_res_lib_dispatch_init_t res_lib_dispatch_init;
  104. SaAisErrorT error;
  105. gid_t egid;
  106. /*
  107. * Allow set group id binaries to be authenticated
  108. */
  109. egid = getegid();
  110. setregid (egid, -1);
  111. memset (&address, 0, sizeof (struct sockaddr_un));
  112. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  113. address.sun_len = sizeof(struct sockaddr_un);
  114. #endif
  115. address.sun_family = PF_UNIX;
  116. #if defined(OPENAIS_LINUX)
  117. strcpy (address.sun_path + 1, socketname);
  118. #else
  119. strcpy (address.sun_path, socketname);
  120. #endif
  121. responseFD = socket (PF_UNIX, SOCK_STREAM, 0);
  122. if (responseFD == -1) {
  123. return (SA_AIS_ERR_NO_RESOURCES);
  124. }
  125. socket_nosigpipe (responseFD);
  126. result = connect (responseFD, (struct sockaddr *)&address, AIS_SUN_LEN(&address));
  127. if (result == -1) {
  128. close (responseFD);
  129. return (SA_AIS_ERR_TRY_AGAIN);
  130. }
  131. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  132. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  133. req_lib_response_init.resdis_header.service = service;
  134. error = saSendRetry (responseFD, &req_lib_response_init,
  135. sizeof (mar_req_lib_response_init_t));
  136. if (error != SA_AIS_OK) {
  137. goto error_exit;
  138. }
  139. error = saRecvRetry (responseFD, &res_lib_response_init,
  140. sizeof (mar_res_lib_response_init_t));
  141. if (error != SA_AIS_OK) {
  142. goto error_exit;
  143. }
  144. /*
  145. * Check for security errors
  146. */
  147. if (res_lib_response_init.header.error != SA_AIS_OK) {
  148. error = res_lib_response_init.header.error;
  149. goto error_exit;
  150. }
  151. *responseOut = responseFD;
  152. /* if I comment out the 4 lines below the executive crashes */
  153. callbackFD = socket (PF_UNIX, SOCK_STREAM, 0);
  154. if (callbackFD == -1) {
  155. return (SA_AIS_ERR_NO_RESOURCES);
  156. }
  157. socket_nosigpipe (callbackFD);
  158. result = connect (callbackFD, (struct sockaddr *)&address, AIS_SUN_LEN(&address));
  159. if (result == -1) {
  160. close (callbackFD);
  161. close (responseFD);
  162. return (SA_AIS_ERR_TRY_AGAIN);
  163. }
  164. req_lib_dispatch_init.resdis_header.size = sizeof (req_lib_dispatch_init);
  165. req_lib_dispatch_init.resdis_header.id = MESSAGE_REQ_DISPATCH_INIT;
  166. req_lib_dispatch_init.resdis_header.service = service;
  167. req_lib_dispatch_init.conn_info = res_lib_response_init.conn_info;
  168. error = saSendRetry (callbackFD, &req_lib_dispatch_init,
  169. sizeof (mar_req_lib_dispatch_init_t));
  170. if (error != SA_AIS_OK) {
  171. goto error_exit_two;
  172. }
  173. error = saRecvRetry (callbackFD, &res_lib_dispatch_init,
  174. sizeof (mar_res_lib_dispatch_init_t));
  175. if (error != SA_AIS_OK) {
  176. goto error_exit_two;
  177. }
  178. /*
  179. * Check for security errors
  180. */
  181. if (res_lib_dispatch_init.header.error != SA_AIS_OK) {
  182. error = res_lib_dispatch_init.header.error;
  183. goto error_exit;
  184. }
  185. *callbackOut = callbackFD;
  186. return (SA_AIS_OK);
  187. error_exit_two:
  188. close (callbackFD);
  189. error_exit:
  190. close (responseFD);
  191. return (error);
  192. }
  193. SaAisErrorT
  194. saRecvRetry (
  195. int s,
  196. void *msg,
  197. size_t len)
  198. {
  199. SaAisErrorT error = SA_AIS_OK;
  200. int result;
  201. struct msghdr msg_recv;
  202. struct iovec iov_recv;
  203. char *rbuf = (char *)msg;
  204. int processed = 0;
  205. msg_recv.msg_iov = &iov_recv;
  206. msg_recv.msg_iovlen = 1;
  207. msg_recv.msg_name = 0;
  208. msg_recv.msg_namelen = 0;
  209. #ifndef OPENAIS_SOLARIS
  210. msg_recv.msg_control = 0;
  211. msg_recv.msg_controllen = 0;
  212. msg_recv.msg_flags = 0;
  213. #else
  214. msg_recv.msg_accrights = NULL;
  215. msg_recv.msg_accrightslen = 0;
  216. #endif
  217. retry_recv:
  218. iov_recv.iov_base = (void *)&rbuf[processed];
  219. iov_recv.iov_len = len - processed;
  220. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL);
  221. if (result == -1 && errno == EINTR) {
  222. goto retry_recv;
  223. }
  224. if (result == -1 && errno == EAGAIN) {
  225. goto retry_recv;
  226. }
  227. #if defined(OPENAIS_SOLARIS) || defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  228. /* On many OS poll never return POLLHUP or POLLERR.
  229. * EOF is detected when recvmsg return 0.
  230. */
  231. if (result == 0) {
  232. error = SA_AIS_ERR_BAD_HANDLE;
  233. goto error_exit;
  234. }
  235. #endif
  236. if (result == -1 || result == 0) {
  237. error = SA_AIS_ERR_LIBRARY;
  238. goto error_exit;
  239. }
  240. processed += result;
  241. if (processed != len) {
  242. goto retry_recv;
  243. }
  244. assert (processed == len);
  245. error_exit:
  246. return (error);
  247. }
  248. SaAisErrorT
  249. saSendRetry (
  250. int s,
  251. const void *msg,
  252. size_t len)
  253. {
  254. SaAisErrorT error = SA_AIS_OK;
  255. int result;
  256. struct msghdr msg_send;
  257. struct iovec iov_send;
  258. char *rbuf = (char *)msg;
  259. int processed = 0;
  260. msg_send.msg_iov = &iov_send;
  261. msg_send.msg_iovlen = 1;
  262. msg_send.msg_name = 0;
  263. msg_send.msg_namelen = 0;
  264. #ifndef OPENAIS_SOLARIS
  265. msg_send.msg_control = 0;
  266. msg_send.msg_controllen = 0;
  267. msg_send.msg_flags = 0;
  268. #else
  269. msg_send.msg_accrights = NULL;
  270. msg_send.msg_accrightslen = 0;
  271. #endif
  272. retry_send:
  273. iov_send.iov_base = (void *)&rbuf[processed];
  274. iov_send.iov_len = len - processed;
  275. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  276. /*
  277. * return immediately on any kind of syscall error that maps to
  278. * SA_AIS_ERR if no part of message has been sent
  279. */
  280. if (result == -1 && processed == 0) {
  281. if (errno == EINTR) {
  282. error = SA_AIS_ERR_TRY_AGAIN;
  283. goto error_exit;
  284. }
  285. if (errno == EAGAIN) {
  286. error = SA_AIS_ERR_TRY_AGAIN;
  287. goto error_exit;
  288. }
  289. if (errno == EFAULT) {
  290. error = SA_AIS_ERR_INVALID_PARAM;
  291. goto error_exit;
  292. }
  293. }
  294. /*
  295. * retry read operations that are already started except
  296. * for fault in that case, return ERR_LIBRARY
  297. */
  298. if (result == -1 && processed > 0) {
  299. if (errno == EINTR) {
  300. goto retry_send;
  301. }
  302. if (errno == EAGAIN) {
  303. goto retry_send;
  304. }
  305. if (errno == EFAULT) {
  306. error = SA_AIS_ERR_LIBRARY;
  307. goto error_exit;
  308. }
  309. }
  310. /*
  311. * return ERR_LIBRARY on any other syscall error
  312. */
  313. if (result == -1) {
  314. error = SA_AIS_ERR_LIBRARY;
  315. goto error_exit;
  316. }
  317. processed += result;
  318. if (processed != len) {
  319. goto retry_send;
  320. }
  321. error_exit:
  322. return (error);
  323. }
  324. SaAisErrorT saSendMsgRetry (
  325. int s,
  326. struct iovec *iov,
  327. int iov_len)
  328. {
  329. SaAisErrorT error = SA_AIS_OK;
  330. int result;
  331. int total_size = 0;
  332. int i;
  333. int csize;
  334. int csize_cntr;
  335. int total_sent = 0;
  336. int iov_len_sendmsg = iov_len;
  337. struct iovec *iov_sendmsg = iov;
  338. struct iovec iovec_save;
  339. int iovec_saved_position = -1;
  340. struct msghdr msg_send;
  341. for (i = 0; i < iov_len; i++) {
  342. total_size += iov[i].iov_len;
  343. }
  344. msg_send.msg_iov = iov_sendmsg;
  345. msg_send.msg_iovlen = iov_len_sendmsg;
  346. msg_send.msg_name = 0;
  347. msg_send.msg_namelen = 0;
  348. #ifndef OPENAIS_SOLARIS
  349. msg_send.msg_control = 0;
  350. msg_send.msg_controllen = 0;
  351. msg_send.msg_flags = 0;
  352. #else
  353. msg_send.msg_accrights = NULL;
  354. msg_send.msg_accrightslen = 0;
  355. #endif
  356. retry_sendmsg:
  357. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  358. /*
  359. * Can't send now, and message not committed, so don't retry send
  360. */
  361. if (result == -1 && iovec_saved_position == -1) {
  362. if (errno == EINTR) {
  363. error = SA_AIS_ERR_TRY_AGAIN;
  364. goto error_exit;
  365. }
  366. if (errno == EAGAIN) {
  367. error = SA_AIS_ERR_TRY_AGAIN;
  368. goto error_exit;
  369. }
  370. if (errno == EFAULT) {
  371. error = SA_AIS_ERR_INVALID_PARAM;
  372. goto error_exit;
  373. }
  374. }
  375. /*
  376. * Retry (and block) if portion of message has already been written
  377. */
  378. if (result == -1 && iovec_saved_position != -1) {
  379. if (errno == EINTR) {
  380. goto retry_sendmsg;
  381. }
  382. if (errno == EAGAIN) {
  383. goto retry_sendmsg;
  384. }
  385. if (errno == EFAULT) {
  386. error = SA_AIS_ERR_LIBRARY;
  387. goto error_exit;
  388. }
  389. }
  390. /*
  391. * ERR_LIBRARY for any other syscall error
  392. */
  393. if (result == -1) {
  394. error = SA_AIS_ERR_LIBRARY;
  395. goto error_exit;
  396. }
  397. if (iovec_saved_position != -1) {
  398. memcpy (&iov[iovec_saved_position], &iovec_save, sizeof (struct iovec));
  399. }
  400. total_sent += result;
  401. if (total_sent != total_size) {
  402. for (i = 0, csize = 0, csize_cntr = 0; i < iov_len; i++) {
  403. csize += iov[i].iov_len;
  404. if (csize > total_sent) {
  405. break;
  406. }
  407. csize_cntr += iov[i].iov_len;
  408. }
  409. memcpy (&iovec_save, &iov[i], sizeof (struct iovec));
  410. iovec_saved_position = i;
  411. iov[i].iov_base = ((char *)(iov[i].iov_base)) +
  412. (total_sent - csize_cntr);
  413. iov[i].iov_len = total_size - total_sent;
  414. msg_send.msg_iov = &iov[i];
  415. msg_send.msg_iovlen = iov_len - i;
  416. goto retry_sendmsg;
  417. }
  418. error_exit:
  419. return (error);
  420. }
  421. SaAisErrorT saSendMsgReceiveReply (
  422. int s,
  423. struct iovec *iov,
  424. int iov_len,
  425. void *responseMessage,
  426. int responseLen)
  427. {
  428. SaAisErrorT error = SA_AIS_OK;
  429. error = saSendMsgRetry (s, iov, iov_len);
  430. if (error != SA_AIS_OK) {
  431. goto error_exit;
  432. }
  433. error = saRecvRetry (s, responseMessage, responseLen);
  434. if (error != SA_AIS_OK) {
  435. goto error_exit;
  436. }
  437. error_exit:
  438. return (error);
  439. }
  440. SaAisErrorT saSendReceiveReply (
  441. int s,
  442. void *requestMessage,
  443. int requestLen,
  444. void *responseMessage,
  445. int responseLen)
  446. {
  447. SaAisErrorT error = SA_AIS_OK;
  448. error = saSendRetry (s, requestMessage, requestLen);
  449. if (error != SA_AIS_OK) {
  450. goto error_exit;
  451. }
  452. error = saRecvRetry (s, responseMessage, responseLen);
  453. if (error != SA_AIS_OK) {
  454. goto error_exit;
  455. }
  456. error_exit:
  457. return (error);
  458. }
  459. SaAisErrorT
  460. saPollRetry (
  461. struct pollfd *ufds,
  462. unsigned int nfds,
  463. int timeout)
  464. {
  465. SaAisErrorT error = SA_AIS_OK;
  466. int result;
  467. retry_poll:
  468. result = poll (ufds, nfds, timeout);
  469. if (result == -1 && errno == EINTR) {
  470. goto retry_poll;
  471. }
  472. if (result == -1) {
  473. error = SA_AIS_ERR_LIBRARY;
  474. }
  475. return (error);
  476. }
  477. SaAisErrorT
  478. saHandleCreate (
  479. struct saHandleDatabase *handleDatabase,
  480. int instanceSize,
  481. SaUint64T *handleOut)
  482. {
  483. uint32_t handle;
  484. uint32_t check;
  485. void *newHandles;
  486. int found = 0;
  487. void *instance;
  488. int i;
  489. pthread_mutex_lock (&handleDatabase->mutex);
  490. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  491. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  492. found = 1;
  493. break;
  494. }
  495. }
  496. if (found == 0) {
  497. handleDatabase->handleCount += 1;
  498. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  499. sizeof (struct saHandle) * handleDatabase->handleCount);
  500. if (newHandles == NULL) {
  501. pthread_mutex_unlock (&handleDatabase->mutex);
  502. return (SA_AIS_ERR_NO_MEMORY);
  503. }
  504. handleDatabase->handles = newHandles;
  505. }
  506. instance = malloc (instanceSize);
  507. if (instance == 0) {
  508. return (SA_AIS_ERR_NO_MEMORY);
  509. }
  510. /*
  511. * This code makes sure the random number isn't zero
  512. * We use 0 to specify an invalid handle out of the 1^64 address space
  513. * If we get 0 200 times in a row, the RNG may be broken
  514. */
  515. for (i = 0; i < 200; i++) {
  516. check = random();
  517. if (check != 0) {
  518. break;
  519. }
  520. }
  521. memset (instance, 0, instanceSize);
  522. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  523. handleDatabase->handles[handle].instance = instance;
  524. handleDatabase->handles[handle].refCount = 1;
  525. handleDatabase->handles[handle].check = check;
  526. *handleOut = (SaUint64T)((uint64_t)check << 32 | handle);
  527. pthread_mutex_unlock (&handleDatabase->mutex);
  528. return (SA_AIS_OK);
  529. }
  530. SaAisErrorT
  531. saHandleDestroy (
  532. struct saHandleDatabase *handleDatabase,
  533. SaUint64T inHandle)
  534. {
  535. SaAisErrorT error = SA_AIS_OK;
  536. uint32_t check = inHandle >> 32;
  537. uint32_t handle = inHandle & 0xffffffff;
  538. pthread_mutex_lock (&handleDatabase->mutex);
  539. if (check != handleDatabase->handles[handle].check) {
  540. pthread_mutex_unlock (&handleDatabase->mutex);
  541. error = SA_AIS_ERR_BAD_HANDLE;
  542. return (error);
  543. }
  544. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  545. pthread_mutex_unlock (&handleDatabase->mutex);
  546. saHandleInstancePut (handleDatabase, inHandle);
  547. return (error);
  548. }
  549. SaAisErrorT
  550. saHandleInstanceGet (
  551. struct saHandleDatabase *handleDatabase,
  552. SaUint64T inHandle,
  553. void **instance)
  554. {
  555. uint32_t check = inHandle >> 32;
  556. uint32_t handle = inHandle & 0xffffffff;
  557. SaAisErrorT error = SA_AIS_OK;
  558. pthread_mutex_lock (&handleDatabase->mutex);
  559. if (handle >= (SaUint64T)handleDatabase->handleCount) {
  560. error = SA_AIS_ERR_BAD_HANDLE;
  561. goto error_exit;
  562. }
  563. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  564. error = SA_AIS_ERR_BAD_HANDLE;
  565. goto error_exit;
  566. }
  567. if (check != handleDatabase->handles[handle].check) {
  568. error = SA_AIS_ERR_BAD_HANDLE;
  569. goto error_exit;
  570. }
  571. *instance = handleDatabase->handles[handle].instance;
  572. handleDatabase->handles[handle].refCount += 1;
  573. error_exit:
  574. pthread_mutex_unlock (&handleDatabase->mutex);
  575. return (error);
  576. }
  577. SaAisErrorT
  578. saHandleInstancePut (
  579. struct saHandleDatabase *handleDatabase,
  580. SaUint64T inHandle)
  581. {
  582. void *instance;
  583. SaAisErrorT error = SA_AIS_OK;
  584. uint32_t check = inHandle >> 32;
  585. uint32_t handle = inHandle & 0xffffffff;
  586. pthread_mutex_lock (&handleDatabase->mutex);
  587. if (check != handleDatabase->handles[handle].check) {
  588. error = SA_AIS_ERR_BAD_HANDLE;
  589. goto error_exit;
  590. }
  591. handleDatabase->handles[handle].refCount -= 1;
  592. assert (handleDatabase->handles[handle].refCount >= 0);
  593. if (handleDatabase->handles[handle].refCount == 0) {
  594. instance = (handleDatabase->handles[handle].instance);
  595. handleDatabase->handleInstanceDestructor (instance);
  596. free (instance);
  597. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  598. }
  599. error_exit:
  600. pthread_mutex_unlock (&handleDatabase->mutex);
  601. return (error);
  602. }
  603. SaAisErrorT
  604. saVersionVerify (
  605. struct saVersionDatabase *versionDatabase,
  606. SaVersionT *version)
  607. {
  608. int i;
  609. SaAisErrorT error = SA_AIS_ERR_VERSION;
  610. if (version == 0) {
  611. return (SA_AIS_ERR_INVALID_PARAM);
  612. }
  613. /*
  614. * Look for a release code that we support. If we find it then
  615. * make sure that the supported major version is >= to the required one.
  616. * In any case we return what we support in the version structure.
  617. */
  618. for (i = 0; i < versionDatabase->versionCount; i++) {
  619. /*
  620. * Check if the caller requires and old release code that we don't support.
  621. */
  622. if (version->releaseCode < versionDatabase->versionsSupported[i].releaseCode) {
  623. break;
  624. }
  625. /*
  626. * Check if we can support this release code.
  627. */
  628. if (version->releaseCode == versionDatabase->versionsSupported[i].releaseCode) {
  629. /*
  630. * Check if we can support the major version requested.
  631. */
  632. if (versionDatabase->versionsSupported[i].majorVersion >= version->majorVersion) {
  633. error = SA_AIS_OK;
  634. break;
  635. }
  636. /*
  637. * We support the release code, but not the major version.
  638. */
  639. break;
  640. }
  641. }
  642. /*
  643. * If we fall out of the if loop, the caller requires a release code
  644. * beyond what we support.
  645. */
  646. if (i == versionDatabase->versionCount) {
  647. i = versionDatabase->versionCount - 1;
  648. }
  649. /*
  650. * Tell the caller what we support
  651. */
  652. memcpy(version, &versionDatabase->versionsSupported[i], sizeof(*version));
  653. return (error);
  654. }
  655. /*
  656. * Get the time of day and convert to nanoseconds
  657. */
  658. SaTimeT clustTimeNow(void)
  659. {
  660. struct timeval tv;
  661. SaTimeT time_now;
  662. if (gettimeofday(&tv, 0)) {
  663. return 0ULL;
  664. }
  665. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  666. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  667. return time_now;
  668. }