util.c 13 KB

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