4
0

util.c 12 KB

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