util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 <sys/ioctl.h>
  41. #include <sys/types.h>
  42. #include <sys/uio.h>
  43. #include <sys/socket.h>
  44. #include <sys/select.h>
  45. #include <sys/time.h>
  46. #include <sys/un.h>
  47. #include <net/if.h>
  48. #include <arpa/inet.h>
  49. #include <netinet/in.h>
  50. #include <assert.h>
  51. #include "../include/saAis.h"
  52. #include "../include/ipc_gen.h"
  53. #include "util.h"
  54. enum SA_HANDLE_STATE {
  55. SA_HANDLE_STATE_EMPTY,
  56. SA_HANDLE_STATE_PENDINGREMOVAL,
  57. SA_HANDLE_STATE_ACTIVE
  58. };
  59. struct saHandle {
  60. int state;
  61. void *instance;
  62. int refCount;
  63. uint32_t check;
  64. };
  65. SaAisErrorT
  66. saServiceConnect (
  67. int *fdOut,
  68. enum service_types service)
  69. {
  70. int fd;
  71. int result;
  72. struct sockaddr_un address;
  73. struct req_lib_response_init req_lib_response_init;
  74. struct res_lib_response_init res_lib_response_init;
  75. SaAisErrorT error;
  76. gid_t egid;
  77. /*
  78. * Allow set group id binaries to be authenticated
  79. */
  80. egid = getegid();
  81. setregid (egid, -1);
  82. memset (&address, 0, sizeof (struct sockaddr_un));
  83. address.sun_family = PF_UNIX;
  84. strcpy (address.sun_path + 1, "libais.socket");
  85. fd = socket (PF_UNIX, SOCK_STREAM, 0);
  86. if (fd == -1) {
  87. return (SA_AIS_ERR_NO_RESOURCES);
  88. }
  89. result = connect (fd, (struct sockaddr *)&address, sizeof (address));
  90. if (result == -1) {
  91. return (SA_AIS_ERR_TRY_AGAIN);
  92. }
  93. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  94. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  95. req_lib_response_init.resdis_header.service = service;
  96. error = saSendRetry (fd, &req_lib_response_init,
  97. sizeof (struct req_lib_response_init), MSG_NOSIGNAL);
  98. if (error != SA_AIS_OK) {
  99. goto error_exit;
  100. }
  101. error = saRecvRetry (fd, &res_lib_response_init,
  102. sizeof (struct res_lib_response_init), MSG_WAITALL | MSG_NOSIGNAL);
  103. if (error != SA_AIS_OK) {
  104. goto error_exit;
  105. }
  106. /*
  107. * Check for security errors
  108. */
  109. if (res_lib_response_init.header.error != SA_AIS_OK) {
  110. error = res_lib_response_init.header.error;
  111. goto error_exit;
  112. }
  113. *fdOut = fd;
  114. return (SA_AIS_OK);
  115. error_exit:
  116. close (fd);
  117. return (error);
  118. }
  119. SaAisErrorT
  120. saServiceConnectTwo (
  121. int *responseOut,
  122. int *callbackOut,
  123. enum service_types service)
  124. {
  125. int responseFD;
  126. int callbackFD;
  127. int result;
  128. struct sockaddr_un address;
  129. struct req_lib_response_init req_lib_response_init;
  130. struct res_lib_response_init res_lib_response_init;
  131. struct req_lib_dispatch_init req_lib_dispatch_init;
  132. struct res_lib_dispatch_init res_lib_dispatch_init;
  133. SaAisErrorT error;
  134. gid_t egid;
  135. /*
  136. * Allow set group id binaries to be authenticated
  137. */
  138. egid = getegid();
  139. setregid (egid, -1);
  140. memset (&address, 0, sizeof (struct sockaddr_un));
  141. address.sun_family = PF_UNIX;
  142. strcpy (address.sun_path + 1, "libais.socket");
  143. responseFD = socket (PF_UNIX, SOCK_STREAM, 0);
  144. if (responseFD == -1) {
  145. return (SA_AIS_ERR_NO_RESOURCES);
  146. }
  147. result = connect (responseFD, (struct sockaddr *)&address, sizeof (address));
  148. if (result == -1) {
  149. return (SA_AIS_ERR_TRY_AGAIN);
  150. }
  151. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  152. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  153. req_lib_response_init.resdis_header.service = service;
  154. error = saSendRetry (responseFD, &req_lib_response_init,
  155. sizeof (struct req_lib_response_init),
  156. MSG_NOSIGNAL);
  157. if (error != SA_AIS_OK) {
  158. goto error_exit;
  159. }
  160. error = saRecvRetry (responseFD, &res_lib_response_init,
  161. sizeof (struct res_lib_response_init),
  162. MSG_WAITALL | MSG_NOSIGNAL);
  163. if (error != SA_AIS_OK) {
  164. goto error_exit;
  165. }
  166. /*
  167. * Check for security errors
  168. */
  169. if (res_lib_response_init.header.error != SA_AIS_OK) {
  170. error = res_lib_response_init.header.error;
  171. goto error_exit;
  172. }
  173. *responseOut = responseFD;
  174. /* if I comment out the 4 lines below the executive crashes */
  175. callbackFD = socket (PF_UNIX, SOCK_STREAM, 0);
  176. if (callbackFD == -1) {
  177. return (SA_AIS_ERR_NO_RESOURCES);
  178. }
  179. result = connect (callbackFD, (struct sockaddr *)&address, sizeof (address));
  180. if (result == -1) {
  181. return (SA_AIS_ERR_TRY_AGAIN);
  182. }
  183. req_lib_dispatch_init.resdis_header.size = sizeof (req_lib_dispatch_init);
  184. req_lib_dispatch_init.resdis_header.id = MESSAGE_REQ_DISPATCH_INIT;
  185. req_lib_dispatch_init.resdis_header.service = service;
  186. req_lib_dispatch_init.conn_info = res_lib_response_init.conn_info;
  187. error = saSendRetry (callbackFD, &req_lib_dispatch_init,
  188. sizeof (struct req_lib_dispatch_init),
  189. MSG_NOSIGNAL);
  190. if (error != SA_AIS_OK) {
  191. goto error_exit_two;
  192. }
  193. error = saRecvRetry (callbackFD, &res_lib_dispatch_init,
  194. sizeof (struct res_lib_dispatch_init),
  195. MSG_WAITALL | MSG_NOSIGNAL);
  196. if (error != SA_AIS_OK) {
  197. goto error_exit_two;
  198. }
  199. /*
  200. * Check for security errors
  201. */
  202. if (res_lib_dispatch_init.header.error != SA_AIS_OK) {
  203. error = res_lib_dispatch_init.header.error;
  204. goto error_exit;
  205. }
  206. *callbackOut = callbackFD;
  207. return (SA_AIS_OK);
  208. error_exit_two:
  209. close (callbackFD);
  210. error_exit:
  211. close (responseFD);
  212. return (error);
  213. }
  214. SaAisErrorT
  215. saRecvRetry (
  216. int s,
  217. void *msg,
  218. size_t len,
  219. int flags)
  220. {
  221. SaAisErrorT error = SA_AIS_OK;
  222. int result;
  223. struct msghdr msg_recv;
  224. struct iovec iov_recv;
  225. char *rbuf = (char *)msg;
  226. int processed = 0;
  227. msg_recv.msg_iov = &iov_recv;
  228. msg_recv.msg_iovlen = 1;
  229. msg_recv.msg_name = 0;
  230. msg_recv.msg_namelen = 0;
  231. msg_recv.msg_control = 0;
  232. msg_recv.msg_controllen = 0;
  233. msg_recv.msg_flags = 0;
  234. retry_recv:
  235. iov_recv.iov_base = (void *)&rbuf[processed];
  236. iov_recv.iov_len = len - processed;
  237. result = recvmsg (s, &msg_recv, flags);
  238. if (result == -1 && errno == EINTR) {
  239. goto retry_recv;
  240. }
  241. if (result == -1 || result == 0) {
  242. error = SA_AIS_ERR_LIBRARY;
  243. goto error_exit;
  244. }
  245. processed += result;
  246. if (processed != len) {
  247. goto retry_recv;
  248. }
  249. assert (processed == len);
  250. error_exit:
  251. return (error);
  252. }
  253. struct res_overlay {
  254. struct res_header header;
  255. char payload[0];
  256. };
  257. SaAisErrorT
  258. saSendRetry (
  259. int s,
  260. const void *msg,
  261. size_t len,
  262. int flags)
  263. {
  264. SaAisErrorT error = SA_AIS_OK;
  265. int result;
  266. struct msghdr msg_send;
  267. struct iovec iov_send;
  268. char *rbuf = (char *)msg;
  269. int processed = 0;
  270. msg_send.msg_iov = &iov_send;
  271. msg_send.msg_iovlen = 1;
  272. msg_send.msg_name = 0;
  273. msg_send.msg_namelen = 0;
  274. msg_send.msg_control = 0;
  275. msg_send.msg_controllen = 0;
  276. msg_send.msg_flags = 0;
  277. retry_send:
  278. iov_send.iov_base = (void *)&rbuf[processed];
  279. iov_send.iov_len = len - processed;
  280. result = sendmsg (s, &msg_send, flags);
  281. /*
  282. * return immediately on any kind of syscall error that maps to
  283. * SA_AIS_ERR if no part of message has been sent
  284. */
  285. if (result == -1 && processed == 0) {
  286. if (errno == EINTR) {
  287. error = SA_AIS_ERR_TRY_AGAIN;
  288. goto error_exit;
  289. }
  290. if (errno == EAGAIN) {
  291. error = SA_AIS_ERR_TRY_AGAIN;
  292. goto error_exit;
  293. }
  294. if (errno == EFAULT) {
  295. error = SA_AIS_ERR_INVALID_PARAM;
  296. goto error_exit;
  297. }
  298. }
  299. /*
  300. * retry read operations that are already started except
  301. * for fault in that case, return ERR_LIBRARY
  302. */
  303. if (result == -1 && processed > 0) {
  304. if (errno == EINTR) {
  305. goto retry_send;
  306. }
  307. if (errno == EAGAIN) {
  308. goto retry_send;
  309. }
  310. if (errno == EFAULT) {
  311. error = SA_AIS_ERR_LIBRARY;
  312. goto error_exit;
  313. }
  314. }
  315. /*
  316. * return ERR_LIBRARY on any other syscall error
  317. */
  318. if (result == -1) {
  319. error = SA_AIS_ERR_LIBRARY;
  320. goto error_exit;
  321. }
  322. processed += result;
  323. if (processed != len) {
  324. goto retry_send;
  325. }
  326. error_exit:
  327. return (error);
  328. }
  329. SaAisErrorT saSendMsgRetry (
  330. int s,
  331. struct iovec *iov,
  332. int iov_len)
  333. {
  334. SaAisErrorT error = SA_AIS_OK;
  335. int result;
  336. int total_size = 0;
  337. int i;
  338. int csize;
  339. int csize_cntr;
  340. int total_sent = 0;
  341. int iov_len_sendmsg = iov_len;
  342. struct iovec *iov_sendmsg = iov;
  343. struct iovec iovec_save;
  344. int iovec_saved_position = -1;
  345. struct msghdr msg_send;
  346. for (i = 0; i < iov_len; i++) {
  347. total_size += iov[i].iov_len;
  348. }
  349. msg_send.msg_iov = iov_sendmsg;
  350. msg_send.msg_iovlen = iov_len_sendmsg;
  351. msg_send.msg_name = 0;
  352. msg_send.msg_namelen = 0;
  353. msg_send.msg_control = 0;
  354. msg_send.msg_controllen = 0;
  355. msg_send.msg_flags = 0;
  356. retry_sendmsg:
  357. result = sendmsg (s, &msg_send, MSG_NOSIGNAL | MSG_DONTWAIT);
  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 = ((unsigned 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. MSG_WAITALL | MSG_NOSIGNAL);
  435. if (error != SA_AIS_OK) {
  436. goto error_exit;
  437. }
  438. error_exit:
  439. return (error);
  440. }
  441. SaAisErrorT saSendReceiveReply (
  442. int s,
  443. void *requestMessage,
  444. int requestLen,
  445. void *responseMessage,
  446. int responseLen)
  447. {
  448. SaAisErrorT error = SA_AIS_OK;
  449. error = saSendRetry (s, requestMessage, requestLen,
  450. MSG_NOSIGNAL);
  451. if (error != SA_AIS_OK) {
  452. goto error_exit;
  453. }
  454. error = saRecvRetry (s, responseMessage, responseLen,
  455. MSG_WAITALL | MSG_NOSIGNAL);
  456. if (error != SA_AIS_OK) {
  457. goto error_exit;
  458. }
  459. error_exit:
  460. return (error);
  461. }
  462. SaAisErrorT
  463. saPollRetry (
  464. struct pollfd *ufds,
  465. unsigned int nfds,
  466. int timeout)
  467. {
  468. SaAisErrorT error = SA_AIS_OK;
  469. int result;
  470. retry_poll:
  471. result = poll (ufds, nfds, timeout);
  472. if (result == -1 && errno == EINTR) {
  473. goto retry_poll;
  474. }
  475. if (result == -1) {
  476. error = SA_AIS_ERR_LIBRARY;
  477. }
  478. return (error);
  479. }
  480. SaAisErrorT
  481. saHandleCreate (
  482. struct saHandleDatabase *handleDatabase,
  483. int instanceSize,
  484. SaUint64T *handleOut)
  485. {
  486. uint32_t handle;
  487. uint32_t check;
  488. void *newHandles;
  489. int found = 0;
  490. void *instance;
  491. pthread_mutex_lock (&handleDatabase->mutex);
  492. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  493. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  494. found = 1;
  495. break;
  496. }
  497. }
  498. if (found == 0) {
  499. handleDatabase->handleCount += 1;
  500. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  501. sizeof (struct saHandle) * handleDatabase->handleCount);
  502. if (newHandles == 0) {
  503. pthread_mutex_unlock (&handleDatabase->mutex);
  504. return (SA_AIS_ERR_NO_MEMORY);
  505. }
  506. handleDatabase->handles = newHandles;
  507. }
  508. instance = malloc (instanceSize);
  509. if (instance == 0) {
  510. return (SA_AIS_ERR_NO_MEMORY);
  511. }
  512. check = random();
  513. memset (instance, 0, instanceSize);
  514. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  515. handleDatabase->handles[handle].instance = instance;
  516. handleDatabase->handles[handle].refCount = 1;
  517. handleDatabase->handles[handle].check = check;
  518. *handleOut = (SaUint64T)((uint64_t)check << 32 | handle);
  519. pthread_mutex_unlock (&handleDatabase->mutex);
  520. return (SA_AIS_OK);
  521. }
  522. SaAisErrorT
  523. saHandleDestroy (
  524. struct saHandleDatabase *handleDatabase,
  525. SaUint64T inHandle)
  526. {
  527. SaAisErrorT error = SA_AIS_OK;
  528. uint32_t check = inHandle >> 32;
  529. uint32_t handle = inHandle & 0xffffffff;
  530. pthread_mutex_lock (&handleDatabase->mutex);
  531. if (check != handleDatabase->handles[handle].check) {
  532. error = SA_AIS_ERR_BAD_HANDLE;
  533. goto error_exit;
  534. }
  535. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  536. error_exit:
  537. pthread_mutex_unlock (&handleDatabase->mutex);
  538. saHandleInstancePut (handleDatabase, inHandle);
  539. return (error);
  540. }
  541. SaAisErrorT
  542. saHandleInstanceGet (
  543. struct saHandleDatabase *handleDatabase,
  544. SaUint64T inHandle,
  545. void **instance)
  546. {
  547. uint32_t check = inHandle >> 32;
  548. uint32_t handle = inHandle & 0xffffffff;
  549. SaAisErrorT error = SA_AIS_OK;
  550. pthread_mutex_lock (&handleDatabase->mutex);
  551. if (handle >= (SaUint64T)handleDatabase->handleCount) {
  552. error = SA_AIS_ERR_BAD_HANDLE;
  553. goto error_exit;
  554. }
  555. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  556. error = SA_AIS_ERR_BAD_HANDLE;
  557. goto error_exit;
  558. }
  559. if (check != handleDatabase->handles[handle].check) {
  560. error = SA_AIS_ERR_BAD_HANDLE;
  561. goto error_exit;
  562. }
  563. *instance = handleDatabase->handles[handle].instance;
  564. handleDatabase->handles[handle].refCount += 1;
  565. error_exit:
  566. pthread_mutex_unlock (&handleDatabase->mutex);
  567. return (error);
  568. }
  569. SaAisErrorT
  570. saHandleInstancePut (
  571. struct saHandleDatabase *handleDatabase,
  572. SaUint64T inHandle)
  573. {
  574. void *instance;
  575. SaAisErrorT error = SA_AIS_OK;
  576. uint32_t check = inHandle >> 32;
  577. uint32_t handle = inHandle & 0xffffffff;
  578. pthread_mutex_lock (&handleDatabase->mutex);
  579. if (check != handleDatabase->handles[handle].check) {
  580. error = SA_AIS_ERR_BAD_HANDLE;
  581. goto error_exit;
  582. }
  583. handleDatabase->handles[handle].refCount -= 1;
  584. assert (handleDatabase->handles[handle].refCount >= 0);
  585. if (handleDatabase->handles[handle].refCount == 0) {
  586. instance = (handleDatabase->handles[handle].instance);
  587. handleDatabase->handleInstanceDestructor (instance);
  588. free (instance);
  589. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  590. }
  591. error_exit:
  592. pthread_mutex_unlock (&handleDatabase->mutex);
  593. return (error);
  594. }
  595. SaAisErrorT
  596. saVersionVerify (
  597. struct saVersionDatabase *versionDatabase,
  598. SaVersionT *version)
  599. {
  600. int i;
  601. SaAisErrorT error = SA_AIS_ERR_VERSION;
  602. if (version == 0) {
  603. return (SA_AIS_ERR_INVALID_PARAM);
  604. }
  605. /*
  606. * Look for a release code that we support. If we find it then
  607. * make sure that the supported major version is >= to the required one.
  608. * In any case we return what we support in the version structure.
  609. */
  610. for (i = 0; i < versionDatabase->versionCount; i++) {
  611. /*
  612. * Check if the caller requires and old release code that we don't support.
  613. */
  614. if (version->releaseCode < versionDatabase->versionsSupported[i].releaseCode) {
  615. break;
  616. }
  617. /*
  618. * Check if we can support this release code.
  619. */
  620. if (version->releaseCode == versionDatabase->versionsSupported[i].releaseCode) {
  621. /*
  622. * Check if we can support the major version requested.
  623. */
  624. if (versionDatabase->versionsSupported[i].majorVersion >= version->majorVersion) {
  625. error = SA_AIS_OK;
  626. break;
  627. }
  628. /*
  629. * We support the release code, but not the major version.
  630. */
  631. break;
  632. }
  633. }
  634. /*
  635. * If we fall out of the if loop, the caller requires a release code
  636. * beyond what we support.
  637. */
  638. if (i == versionDatabase->versionCount) {
  639. i = versionDatabase->versionCount - 1;
  640. }
  641. /*
  642. * Tell the caller what we support
  643. */
  644. memcpy(version, &versionDatabase->versionsSupported[i], sizeof(*version));
  645. return (error);
  646. }
  647. /*
  648. * Get the time of day and convert to nanoseconds
  649. */
  650. SaTimeT clustTimeNow(void)
  651. {
  652. struct timeval tv;
  653. SaTimeT time_now;
  654. if (gettimeofday(&tv, 0)) {
  655. return 0ULL;
  656. }
  657. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  658. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  659. return time_now;
  660. }