util.c 12 KB

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