util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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));
  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. if (error != SA_AIS_OK) {
  163. goto error_exit;
  164. }
  165. /*
  166. * Check for security errors
  167. */
  168. if (res_lib_response_init.header.error != SA_AIS_OK) {
  169. error = res_lib_response_init.header.error;
  170. goto error_exit;
  171. }
  172. *responseOut = responseFD;
  173. /* if I comment out the 4 lines below the executive crashes */
  174. callbackFD = socket (PF_UNIX, SOCK_STREAM, 0);
  175. if (callbackFD == -1) {
  176. return (SA_AIS_ERR_NO_RESOURCES);
  177. }
  178. result = connect (callbackFD, (struct sockaddr *)&address, sizeof (address));
  179. if (result == -1) {
  180. return (SA_AIS_ERR_TRY_AGAIN);
  181. }
  182. req_lib_dispatch_init.resdis_header.size = sizeof (req_lib_dispatch_init);
  183. req_lib_dispatch_init.resdis_header.id = MESSAGE_REQ_DISPATCH_INIT;
  184. req_lib_dispatch_init.resdis_header.service = service;
  185. req_lib_dispatch_init.conn_info = res_lib_response_init.conn_info;
  186. error = saSendRetry (callbackFD, &req_lib_dispatch_init,
  187. sizeof (struct req_lib_dispatch_init),
  188. MSG_NOSIGNAL);
  189. if (error != SA_AIS_OK) {
  190. goto error_exit_two;
  191. }
  192. error = saRecvRetry (callbackFD, &res_lib_dispatch_init,
  193. sizeof (struct res_lib_dispatch_init));
  194. if (error != SA_AIS_OK) {
  195. goto error_exit_two;
  196. }
  197. /*
  198. * Check for security errors
  199. */
  200. if (res_lib_dispatch_init.header.error != SA_AIS_OK) {
  201. error = res_lib_dispatch_init.header.error;
  202. goto error_exit;
  203. }
  204. *callbackOut = callbackFD;
  205. return (SA_AIS_OK);
  206. error_exit_two:
  207. close (callbackFD);
  208. error_exit:
  209. close (responseFD);
  210. return (error);
  211. }
  212. SaAisErrorT
  213. saRecvRetry (
  214. int s,
  215. void *msg,
  216. size_t len)
  217. {
  218. SaAisErrorT error = SA_AIS_OK;
  219. int result;
  220. struct msghdr msg_recv;
  221. struct iovec iov_recv;
  222. char *rbuf = (char *)msg;
  223. int processed = 0;
  224. msg_recv.msg_iov = &iov_recv;
  225. msg_recv.msg_iovlen = 1;
  226. msg_recv.msg_name = 0;
  227. msg_recv.msg_namelen = 0;
  228. msg_recv.msg_control = 0;
  229. msg_recv.msg_controllen = 0;
  230. msg_recv.msg_flags = 0;
  231. retry_recv:
  232. iov_recv.iov_base = (void *)&rbuf[processed];
  233. iov_recv.iov_len = len - processed;
  234. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL);
  235. if (result == -1 && errno == EINTR) {
  236. goto retry_recv;
  237. }
  238. if (result == -1 || result == 0) {
  239. error = SA_AIS_ERR_LIBRARY;
  240. goto error_exit;
  241. }
  242. processed += result;
  243. if (processed != len) {
  244. goto retry_recv;
  245. }
  246. assert (processed == len);
  247. error_exit:
  248. return (error);
  249. }
  250. struct res_overlay {
  251. struct res_header header;
  252. char payload[0];
  253. };
  254. SaAisErrorT
  255. saSendRetry (
  256. int s,
  257. const void *msg,
  258. size_t len,
  259. int flags)
  260. {
  261. SaAisErrorT error = SA_AIS_OK;
  262. int result;
  263. struct msghdr msg_send;
  264. struct iovec iov_send;
  265. char *rbuf = (char *)msg;
  266. int processed = 0;
  267. msg_send.msg_iov = &iov_send;
  268. msg_send.msg_iovlen = 1;
  269. msg_send.msg_name = 0;
  270. msg_send.msg_namelen = 0;
  271. msg_send.msg_control = 0;
  272. msg_send.msg_controllen = 0;
  273. msg_send.msg_flags = 0;
  274. retry_send:
  275. iov_send.iov_base = (void *)&rbuf[processed];
  276. iov_send.iov_len = len - processed;
  277. result = sendmsg (s, &msg_send, flags);
  278. /*
  279. * return immediately on any kind of syscall error that maps to
  280. * SA_AIS_ERR if no part of message has been sent
  281. */
  282. if (result == -1 && processed == 0) {
  283. if (errno == EINTR) {
  284. error = SA_AIS_ERR_TRY_AGAIN;
  285. goto error_exit;
  286. }
  287. if (errno == EAGAIN) {
  288. error = SA_AIS_ERR_TRY_AGAIN;
  289. goto error_exit;
  290. }
  291. if (errno == EFAULT) {
  292. error = SA_AIS_ERR_INVALID_PARAM;
  293. goto error_exit;
  294. }
  295. }
  296. /*
  297. * retry read operations that are already started except
  298. * for fault in that case, return ERR_LIBRARY
  299. */
  300. if (result == -1 && processed > 0) {
  301. if (errno == EINTR) {
  302. goto retry_send;
  303. }
  304. if (errno == EAGAIN) {
  305. goto retry_send;
  306. }
  307. if (errno == EFAULT) {
  308. error = SA_AIS_ERR_LIBRARY;
  309. goto error_exit;
  310. }
  311. }
  312. /*
  313. * return ERR_LIBRARY on any other syscall error
  314. */
  315. if (result == -1) {
  316. error = SA_AIS_ERR_LIBRARY;
  317. goto error_exit;
  318. }
  319. processed += result;
  320. if (processed != len) {
  321. goto retry_send;
  322. }
  323. error_exit:
  324. return (error);
  325. }
  326. SaAisErrorT saSendMsgRetry (
  327. int s,
  328. struct iovec *iov,
  329. int iov_len)
  330. {
  331. SaAisErrorT error = SA_AIS_OK;
  332. int result;
  333. int total_size = 0;
  334. int i;
  335. int csize;
  336. int csize_cntr;
  337. int total_sent = 0;
  338. int iov_len_sendmsg = iov_len;
  339. struct iovec *iov_sendmsg = iov;
  340. struct iovec iovec_save;
  341. int iovec_saved_position = -1;
  342. struct msghdr msg_send;
  343. for (i = 0; i < iov_len; i++) {
  344. total_size += iov[i].iov_len;
  345. }
  346. msg_send.msg_iov = iov_sendmsg;
  347. msg_send.msg_iovlen = iov_len_sendmsg;
  348. msg_send.msg_name = 0;
  349. msg_send.msg_namelen = 0;
  350. msg_send.msg_control = 0;
  351. msg_send.msg_controllen = 0;
  352. msg_send.msg_flags = 0;
  353. retry_sendmsg:
  354. result = sendmsg (s, &msg_send, MSG_NOSIGNAL | MSG_DONTWAIT);
  355. /*
  356. * Can't send now, and message not committed, so don't retry send
  357. */
  358. if (result == -1 && iovec_saved_position == -1) {
  359. if (errno == EINTR) {
  360. error = SA_AIS_ERR_TRY_AGAIN;
  361. goto error_exit;
  362. }
  363. if (errno == EAGAIN) {
  364. error = SA_AIS_ERR_TRY_AGAIN;
  365. goto error_exit;
  366. }
  367. if (errno == EFAULT) {
  368. error = SA_AIS_ERR_INVALID_PARAM;
  369. goto error_exit;
  370. }
  371. }
  372. /*
  373. * Retry (and block) if portion of message has already been written
  374. */
  375. if (result == -1 && iovec_saved_position != -1) {
  376. if (errno == EINTR) {
  377. goto retry_sendmsg;
  378. }
  379. if (errno == EAGAIN) {
  380. goto retry_sendmsg;
  381. }
  382. if (errno == EFAULT) {
  383. error = SA_AIS_ERR_LIBRARY;
  384. goto error_exit;
  385. }
  386. }
  387. /*
  388. * ERR_LIBRARY for any other syscall error
  389. */
  390. if (result == -1) {
  391. error = SA_AIS_ERR_LIBRARY;
  392. goto error_exit;
  393. }
  394. if (iovec_saved_position != -1) {
  395. memcpy (&iov[iovec_saved_position], &iovec_save, sizeof (struct iovec));
  396. }
  397. total_sent += result;
  398. if (total_sent != total_size) {
  399. for (i = 0, csize = 0, csize_cntr = 0; i < iov_len; i++) {
  400. csize += iov[i].iov_len;
  401. if (csize > total_sent) {
  402. break;
  403. }
  404. csize_cntr += iov[i].iov_len;
  405. }
  406. memcpy (&iovec_save, &iov[i], sizeof (struct iovec));
  407. iovec_saved_position = i;
  408. iov[i].iov_base = ((unsigned char *)(iov[i].iov_base)) +
  409. (total_sent - csize_cntr);
  410. iov[i].iov_len = total_size - total_sent;
  411. msg_send.msg_iov = &iov[i];
  412. msg_send.msg_iovlen = iov_len - i;
  413. goto retry_sendmsg;
  414. }
  415. error_exit:
  416. return (error);
  417. }
  418. SaAisErrorT saSendMsgReceiveReply (
  419. int s,
  420. struct iovec *iov,
  421. int iov_len,
  422. void *responseMessage,
  423. int responseLen)
  424. {
  425. SaAisErrorT error = SA_AIS_OK;
  426. error = saSendMsgRetry (s, iov, iov_len);
  427. if (error != SA_AIS_OK) {
  428. goto error_exit;
  429. }
  430. error = saRecvRetry (s, responseMessage, responseLen);
  431. if (error != SA_AIS_OK) {
  432. goto error_exit;
  433. }
  434. error_exit:
  435. return (error);
  436. }
  437. SaAisErrorT saSendReceiveReply (
  438. int s,
  439. void *requestMessage,
  440. int requestLen,
  441. void *responseMessage,
  442. int responseLen)
  443. {
  444. SaAisErrorT error = SA_AIS_OK;
  445. error = saSendRetry (s, requestMessage, requestLen,
  446. MSG_NOSIGNAL);
  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
  458. saPollRetry (
  459. struct pollfd *ufds,
  460. unsigned int nfds,
  461. int timeout)
  462. {
  463. SaAisErrorT error = SA_AIS_OK;
  464. int result;
  465. retry_poll:
  466. result = poll (ufds, nfds, timeout);
  467. if (result == -1 && errno == EINTR) {
  468. goto retry_poll;
  469. }
  470. if (result == -1) {
  471. error = SA_AIS_ERR_LIBRARY;
  472. }
  473. return (error);
  474. }
  475. SaAisErrorT
  476. saHandleCreate (
  477. struct saHandleDatabase *handleDatabase,
  478. int instanceSize,
  479. SaUint64T *handleOut)
  480. {
  481. uint32_t handle;
  482. uint32_t check;
  483. void *newHandles;
  484. int found = 0;
  485. void *instance;
  486. pthread_mutex_lock (&handleDatabase->mutex);
  487. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  488. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  489. found = 1;
  490. break;
  491. }
  492. }
  493. if (found == 0) {
  494. handleDatabase->handleCount += 1;
  495. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  496. sizeof (struct saHandle) * handleDatabase->handleCount);
  497. if (newHandles == 0) {
  498. pthread_mutex_unlock (&handleDatabase->mutex);
  499. return (SA_AIS_ERR_NO_MEMORY);
  500. }
  501. handleDatabase->handles = newHandles;
  502. }
  503. instance = malloc (instanceSize);
  504. if (instance == 0) {
  505. return (SA_AIS_ERR_NO_MEMORY);
  506. }
  507. check = random();
  508. memset (instance, 0, instanceSize);
  509. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  510. handleDatabase->handles[handle].instance = instance;
  511. handleDatabase->handles[handle].refCount = 1;
  512. handleDatabase->handles[handle].check = check;
  513. *handleOut = (SaUint64T)((uint64_t)check << 32 | handle);
  514. pthread_mutex_unlock (&handleDatabase->mutex);
  515. return (SA_AIS_OK);
  516. }
  517. SaAisErrorT
  518. saHandleDestroy (
  519. struct saHandleDatabase *handleDatabase,
  520. SaUint64T inHandle)
  521. {
  522. SaAisErrorT error = SA_AIS_OK;
  523. uint32_t check = inHandle >> 32;
  524. uint32_t handle = inHandle & 0xffffffff;
  525. pthread_mutex_lock (&handleDatabase->mutex);
  526. if (check != handleDatabase->handles[handle].check) {
  527. pthread_mutex_unlock (&handleDatabase->mutex);
  528. error = SA_AIS_ERR_BAD_HANDLE;
  529. return (error);
  530. }
  531. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  532. pthread_mutex_unlock (&handleDatabase->mutex);
  533. saHandleInstancePut (handleDatabase, inHandle);
  534. return (error);
  535. }
  536. SaAisErrorT
  537. saHandleInstanceGet (
  538. struct saHandleDatabase *handleDatabase,
  539. SaUint64T inHandle,
  540. void **instance)
  541. {
  542. uint32_t check = inHandle >> 32;
  543. uint32_t handle = inHandle & 0xffffffff;
  544. SaAisErrorT error = SA_AIS_OK;
  545. pthread_mutex_lock (&handleDatabase->mutex);
  546. if (handle >= (SaUint64T)handleDatabase->handleCount) {
  547. error = SA_AIS_ERR_BAD_HANDLE;
  548. goto error_exit;
  549. }
  550. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  551. error = SA_AIS_ERR_BAD_HANDLE;
  552. goto error_exit;
  553. }
  554. if (check != handleDatabase->handles[handle].check) {
  555. error = SA_AIS_ERR_BAD_HANDLE;
  556. goto error_exit;
  557. }
  558. *instance = handleDatabase->handles[handle].instance;
  559. handleDatabase->handles[handle].refCount += 1;
  560. error_exit:
  561. pthread_mutex_unlock (&handleDatabase->mutex);
  562. return (error);
  563. }
  564. SaAisErrorT
  565. saHandleInstancePut (
  566. struct saHandleDatabase *handleDatabase,
  567. SaUint64T inHandle)
  568. {
  569. void *instance;
  570. SaAisErrorT error = SA_AIS_OK;
  571. uint32_t check = inHandle >> 32;
  572. uint32_t handle = inHandle & 0xffffffff;
  573. pthread_mutex_lock (&handleDatabase->mutex);
  574. if (check != handleDatabase->handles[handle].check) {
  575. error = SA_AIS_ERR_BAD_HANDLE;
  576. goto error_exit;
  577. }
  578. handleDatabase->handles[handle].refCount -= 1;
  579. assert (handleDatabase->handles[handle].refCount >= 0);
  580. if (handleDatabase->handles[handle].refCount == 0) {
  581. instance = (handleDatabase->handles[handle].instance);
  582. handleDatabase->handleInstanceDestructor (instance);
  583. free (instance);
  584. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  585. }
  586. error_exit:
  587. pthread_mutex_unlock (&handleDatabase->mutex);
  588. return (error);
  589. }
  590. SaAisErrorT
  591. saVersionVerify (
  592. struct saVersionDatabase *versionDatabase,
  593. SaVersionT *version)
  594. {
  595. int i;
  596. SaAisErrorT error = SA_AIS_ERR_VERSION;
  597. if (version == 0) {
  598. return (SA_AIS_ERR_INVALID_PARAM);
  599. }
  600. /*
  601. * Look for a release code that we support. If we find it then
  602. * make sure that the supported major version is >= to the required one.
  603. * In any case we return what we support in the version structure.
  604. */
  605. for (i = 0; i < versionDatabase->versionCount; i++) {
  606. /*
  607. * Check if the caller requires and old release code that we don't support.
  608. */
  609. if (version->releaseCode < versionDatabase->versionsSupported[i].releaseCode) {
  610. break;
  611. }
  612. /*
  613. * Check if we can support this release code.
  614. */
  615. if (version->releaseCode == versionDatabase->versionsSupported[i].releaseCode) {
  616. /*
  617. * Check if we can support the major version requested.
  618. */
  619. if (versionDatabase->versionsSupported[i].majorVersion >= version->majorVersion) {
  620. error = SA_AIS_OK;
  621. break;
  622. }
  623. /*
  624. * We support the release code, but not the major version.
  625. */
  626. break;
  627. }
  628. }
  629. /*
  630. * If we fall out of the if loop, the caller requires a release code
  631. * beyond what we support.
  632. */
  633. if (i == versionDatabase->versionCount) {
  634. i = versionDatabase->versionCount - 1;
  635. }
  636. /*
  637. * Tell the caller what we support
  638. */
  639. memcpy(version, &versionDatabase->versionsSupported[i], sizeof(*version));
  640. return (error);
  641. }
  642. /*
  643. * Get the time of day and convert to nanoseconds
  644. */
  645. SaTimeT clustTimeNow(void)
  646. {
  647. struct timeval tv;
  648. SaTimeT time_now;
  649. if (gettimeofday(&tv, 0)) {
  650. return 0ULL;
  651. }
  652. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  653. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  654. return time_now;
  655. }