util.c 18 KB

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