util.c 17 KB

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