util.c 17 KB

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