util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * Copyright (c) 2002-2003 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. return (error);
  144. }
  145. struct message_overlay {
  146. struct res_header header;
  147. char payload[4096];
  148. };
  149. SaErrorT
  150. saRecvQueue (
  151. int s,
  152. void *msg,
  153. struct queue *queue,
  154. int findMessageId)
  155. {
  156. struct message_overlay message;
  157. void *inq_msg;
  158. int match;
  159. SaErrorT error;
  160. do {
  161. error = saRecvRetry (s, &message.header, sizeof (struct res_header),
  162. MSG_WAITALL | MSG_NOSIGNAL);
  163. if (error != SA_OK) {
  164. goto error_exit;
  165. }
  166. if (message.header.size > sizeof (struct req_header)) {
  167. error = saRecvRetry (s, &message.payload,
  168. message.header.size - sizeof (struct res_header),
  169. MSG_WAITALL | MSG_NOSIGNAL);
  170. if (error != SA_OK) {
  171. goto error_exit;
  172. }
  173. }
  174. match = (message.header.id == findMessageId);
  175. if (match == 0 && queue) {
  176. inq_msg = (void *)malloc (message.header.size);
  177. if (inq_msg == 0) {
  178. error = SA_ERR_NO_MEMORY;
  179. goto error_exit;
  180. }
  181. memcpy (inq_msg, &message, message.header.size);
  182. error = saQueueItemAdd (queue, &inq_msg);
  183. if (error != SA_OK) {
  184. free (inq_msg);
  185. goto error_exit;
  186. }
  187. error = saActivatePoll (s);
  188. if (error != SA_OK) {
  189. goto error_exit;
  190. }
  191. } else {
  192. memcpy (msg, &message, message.header.size);
  193. }
  194. } while (match == 0);
  195. error_exit:
  196. return (error);
  197. }
  198. SaErrorT
  199. saActivatePoll (int s) {
  200. struct req_lib_activatepoll req_lib_activatepoll;
  201. SaErrorT error;
  202. /*
  203. * Send activate poll to tell nodeexec to activate poll
  204. * on this file descriptor
  205. */
  206. req_lib_activatepoll.header.size = sizeof (req_lib_activatepoll);
  207. req_lib_activatepoll.header.id = MESSAGE_REQ_LIB_ACTIVATEPOLL;
  208. error = saSendRetry (s, &req_lib_activatepoll,
  209. sizeof (struct req_lib_activatepoll), MSG_NOSIGNAL);
  210. return (error);
  211. }
  212. SaErrorT
  213. saSendRetry (
  214. int s,
  215. const void *msg,
  216. size_t len,
  217. int flags)
  218. {
  219. SaErrorT error = SA_OK;
  220. int result;
  221. struct msghdr msg_send;
  222. struct iovec iov_send;
  223. iov_send.iov_base = (void *)msg;
  224. iov_send.iov_len = len;
  225. msg_send.msg_iov = &iov_send;
  226. msg_send.msg_iovlen = 1;
  227. msg_send.msg_name = 0;
  228. msg_send.msg_namelen = 0;
  229. msg_send.msg_control = 0;
  230. msg_send.msg_controllen = 0;
  231. msg_send.msg_flags = 0;
  232. retry_send:
  233. result = sendmsg (s, &msg_send, flags);
  234. if (result == -1 && errno == EINTR) {
  235. goto retry_send;
  236. }
  237. if (result == -1) {
  238. error = SA_ERR_SYSTEM;
  239. }
  240. return (error);
  241. }
  242. SaErrorT saSendMsgRetry (
  243. int s,
  244. struct iovec *iov,
  245. int iov_len)
  246. {
  247. SaErrorT error = SA_OK;
  248. int result;
  249. struct msghdr msg_send;
  250. msg_send.msg_iov = iov;
  251. msg_send.msg_iovlen = iov_len;
  252. msg_send.msg_name = 0;
  253. msg_send.msg_namelen = 0;
  254. msg_send.msg_control = 0;
  255. msg_send.msg_controllen = 0;
  256. msg_send.msg_flags = 0;
  257. retry_send:
  258. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  259. if (result == -1 && errno == EINTR) {
  260. goto retry_send;
  261. }
  262. if (result == -1) {
  263. error = SA_ERR_SYSTEM;
  264. }
  265. return (error);
  266. }
  267. SaErrorT
  268. saSelectRetry (
  269. int s,
  270. fd_set *readfds,
  271. fd_set *writefds,
  272. fd_set *exceptfds,
  273. struct timeval *timeout)
  274. {
  275. SaErrorT error = SA_OK;
  276. int result;
  277. retry_select:
  278. result = select (s, readfds, writefds, exceptfds, timeout);
  279. if (result == -1 && errno == EINTR) {
  280. goto retry_select;
  281. }
  282. if (result == -1) {
  283. error = SA_ERR_SYSTEM;
  284. }
  285. return (error);
  286. }
  287. SaErrorT
  288. saPollRetry (
  289. struct pollfd *ufds,
  290. unsigned int nfds,
  291. int timeout)
  292. {
  293. SaErrorT error = SA_OK;
  294. int result;
  295. retry_poll:
  296. result = poll (ufds, nfds, timeout);
  297. if (result == -1 && errno == EINTR) {
  298. goto retry_poll;
  299. }
  300. if (result == -1) {
  301. error = SA_ERR_SYSTEM;
  302. }
  303. return (error);
  304. }
  305. SaErrorT
  306. saHandleCreate (
  307. struct saHandleDatabase *handleDatabase,
  308. int instanceSize,
  309. unsigned int *handleOut)
  310. {
  311. int handle;
  312. void *newHandles;
  313. int found = 0;
  314. void *instance;
  315. pthread_mutex_lock (&handleDatabase->mutex);
  316. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  317. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  318. found = 1;
  319. break;
  320. }
  321. }
  322. if (found == 0) {
  323. handleDatabase->handleCount += 1;
  324. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  325. sizeof (struct saHandle) * handleDatabase->handleCount);
  326. if (newHandles == 0) {
  327. pthread_mutex_unlock (&handleDatabase->mutex);
  328. return (SA_ERR_NO_MEMORY);
  329. }
  330. handleDatabase->handles = newHandles;
  331. }
  332. instance = malloc (instanceSize);
  333. if (instance == 0) {
  334. return (SA_ERR_NO_MEMORY);
  335. }
  336. memset (instance, 0, instanceSize);
  337. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  338. handleDatabase->handles[handle].instance = instance;
  339. handleDatabase->handles[handle].refCount = 1;
  340. *handleOut = handle;
  341. pthread_mutex_unlock (&handleDatabase->mutex);
  342. return (SA_OK);
  343. }
  344. SaErrorT
  345. saHandleDestroy (
  346. struct saHandleDatabase *handleDatabase,
  347. unsigned int handle)
  348. {
  349. pthread_mutex_lock (&handleDatabase->mutex);
  350. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  351. pthread_mutex_unlock (&handleDatabase->mutex);
  352. saHandleInstancePut (handleDatabase, handle);
  353. return (SA_OK);
  354. }
  355. SaErrorT
  356. saHandleInstanceGet (
  357. struct saHandleDatabase *handleDatabase,
  358. unsigned int handle,
  359. void **instance)
  360. {
  361. pthread_mutex_lock (&handleDatabase->mutex);
  362. if (handle > handleDatabase->handleCount) {
  363. return (SA_ERR_BAD_HANDLE);
  364. }
  365. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  366. return (SA_ERR_BAD_HANDLE);
  367. }
  368. *instance = handleDatabase->handles[handle].instance;
  369. handleDatabase->handles[handle].refCount += 1;
  370. pthread_mutex_unlock (&handleDatabase->mutex);
  371. return (SA_OK);
  372. }
  373. SaErrorT
  374. saHandleInstancePut (
  375. struct saHandleDatabase *handleDatabase,
  376. unsigned int handle)
  377. {
  378. void *instance;
  379. pthread_mutex_lock (&handleDatabase->mutex);
  380. handleDatabase->handles[handle].refCount -= 1;
  381. assert (handleDatabase->handles[handle].refCount >= 0);
  382. if (handleDatabase->handles[handle].refCount == 0) {
  383. instance = (handleDatabase->handles[handle].instance);
  384. handleDatabase->handleInstanceDestructor (instance);
  385. free (instance);
  386. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  387. }
  388. pthread_mutex_unlock (&handleDatabase->mutex);
  389. return (SA_OK);
  390. }
  391. SaErrorT
  392. saVersionVerify (
  393. struct saVersionDatabase *versionDatabase,
  394. const SaVersionT *version)
  395. {
  396. int found = 0;
  397. int i;
  398. if (version == 0) {
  399. return (SA_ERR_VERSION);
  400. }
  401. for (i = 0; i < versionDatabase->versionCount; i++) {
  402. if (memcmp (&versionDatabase->versionsSupported[i], version, sizeof (SaVersionT)) == 0) {
  403. found = 1;
  404. break;
  405. }
  406. }
  407. return (found ? SA_OK : SA_ERR_VERSION);
  408. }
  409. SaErrorT
  410. saQueueInit (
  411. struct queue *queue,
  412. int queueItems,
  413. int bytesPerItem)
  414. {
  415. queue->head = 0;
  416. queue->tail = queueItems - 1;
  417. queue->used = 0;
  418. queue->usedhw = 0;
  419. queue->size = queueItems;
  420. queue->bytesPerItem = bytesPerItem;
  421. queue->items = (void *)malloc (queueItems * bytesPerItem);
  422. if (queue->items == 0) {
  423. return (SA_ERR_NO_MEMORY);
  424. }
  425. memset (queue->items, 0, queueItems * bytesPerItem);
  426. return (SA_OK);
  427. }
  428. SaErrorT
  429. saQueueIsFull (
  430. struct queue *queue,
  431. int *isFull)
  432. {
  433. *isFull = ((queue->size - 1) == queue->used);
  434. return (SA_OK);
  435. }
  436. SaErrorT
  437. saQueueIsEmpty (
  438. struct queue *queue,
  439. int *isEmpty)
  440. {
  441. *isEmpty = (queue->used == 0);
  442. return (SA_OK);
  443. }
  444. SaErrorT
  445. saQueueItemAdd (
  446. struct queue *queue,
  447. void *item)
  448. {
  449. char *queueItem;
  450. int queuePosition;
  451. queuePosition = queue->head;
  452. queueItem = queue->items;
  453. queueItem += queuePosition * queue->bytesPerItem;
  454. memcpy (queueItem, item, queue->bytesPerItem);
  455. if (queue->tail == queue->head) {
  456. return (SA_ERR_LIBRARY);
  457. }
  458. queue->head = (queue->head + 1) % queue->size;
  459. queue->used++;
  460. if (queue->used > queue->usedhw) {
  461. queue->usedhw = queue->used;
  462. }
  463. return (SA_OK);
  464. }
  465. SaErrorT
  466. saQueueItemGet (struct queue *queue, void **item)
  467. {
  468. char *queueItem;
  469. int queuePosition;
  470. queuePosition = (queue->tail + 1) % queue->size;
  471. queueItem = queue->items;
  472. queueItem += queuePosition * queue->bytesPerItem;
  473. *item = (void *)queueItem;
  474. return (SA_OK);
  475. }
  476. SaErrorT
  477. saQueueItemRemove (struct queue *queue)
  478. {
  479. queue->tail = (queue->tail + 1) % queue->size;
  480. if (queue->tail == queue->head) {
  481. return (SA_ERR_LIBRARY);
  482. }
  483. queue->used--;
  484. return (SA_OK);
  485. }
  486. /*
  487. * Get the time of day and convert to nanoseconds
  488. */
  489. SaTimeT clustTimeNow(void)
  490. {
  491. struct timeval tv;
  492. SaTimeT time_now;
  493. if (gettimeofday(&tv, 0)) {
  494. return 0ULL;
  495. }
  496. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  497. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  498. return time_now;
  499. }