util.c 16 KB

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