util.c 18 KB

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