util.c 17 KB

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