util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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/un.h>
  44. #include <net/if.h>
  45. #include <arpa/inet.h>
  46. #include <netinet/in.h>
  47. #include <assert.h>
  48. #include "../include/ais_types.h"
  49. #include "../include/ais_msg.h"
  50. #include "util.h"
  51. enum SA_HANDLE_STATE {
  52. SA_HANDLE_STATE_EMPTY,
  53. SA_HANDLE_STATE_PENDINGREMOVAL,
  54. SA_HANDLE_STATE_ACTIVE
  55. };
  56. struct saHandle {
  57. int state;
  58. void *instance;
  59. int refCount;
  60. };
  61. SaErrorT
  62. saServiceConnect (
  63. int *fdOut,
  64. enum req_init_types initType)
  65. {
  66. int fd;
  67. int result;
  68. struct sockaddr_un address;
  69. struct req_lib_init req_lib_init;
  70. struct res_lib_init res_lib_init;
  71. SaErrorT error;
  72. gid_t egid;
  73. /*
  74. * Allow set group id binaries to be authenticated
  75. */
  76. egid = getegid();
  77. setregid (egid, -1);
  78. memset (&address, 0, sizeof (struct sockaddr_un));
  79. address.sun_family = PF_UNIX;
  80. strcpy (address.sun_path + 1, "libais.socket");
  81. fd = socket (PF_UNIX, SOCK_STREAM, 0);
  82. if (fd == -1) {
  83. return (SA_ERR_SYSTEM);
  84. }
  85. result = connect (fd, (struct sockaddr *)&address, sizeof (address));
  86. if (result == -1) {
  87. return (SA_ERR_TRY_AGAIN);
  88. }
  89. req_lib_init.header.magic = MESSAGE_MAGIC;
  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.error != SA_OK) {
  106. error = res_lib_init.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 message_header header;
  147. char instance[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_overlay = (struct message_overlay *)msg;
  157. void *inq_msg;
  158. int match;
  159. SaErrorT error;
  160. do {
  161. error = saRecvRetry (s, &message_overlay->header, sizeof (struct message_header), MSG_WAITALL | MSG_NOSIGNAL);
  162. if (error != SA_OK) {
  163. goto error_exit;
  164. }
  165. if (message_overlay->header.size > sizeof (struct message_header)) {
  166. error = saRecvRetry (s, &message_overlay->instance, message_overlay->header.size - sizeof (struct message_header), MSG_WAITALL | MSG_NOSIGNAL);
  167. if (error != SA_OK) {
  168. goto error_exit;
  169. }
  170. }
  171. match = (message_overlay->header.id == findMessageId);
  172. if (match == 0 && queue) {
  173. inq_msg = (void *)malloc (message_overlay->header.size);
  174. if (inq_msg == 0) {
  175. error = SA_ERR_NO_MEMORY;
  176. goto error_exit;
  177. }
  178. memcpy (inq_msg, msg, message_overlay->header.size);
  179. error = saQueueItemAdd (queue, &inq_msg);
  180. if (error != SA_OK) {
  181. free (inq_msg);
  182. goto error_exit;
  183. }
  184. error = saActivatePoll (s);
  185. if (error != SA_OK) {
  186. goto error_exit;
  187. }
  188. }
  189. } while (match == 0);
  190. error_exit:
  191. return (error);
  192. }
  193. SaErrorT
  194. saActivatePoll (int s) {
  195. struct req_lib_activatepoll req_lib_activatepoll;
  196. SaErrorT error;
  197. /*
  198. * Send activate poll to tell nodeexec to activate poll
  199. * on this file descriptor
  200. */
  201. req_lib_activatepoll.header.magic = MESSAGE_MAGIC;
  202. req_lib_activatepoll.header.size = sizeof (req_lib_activatepoll);
  203. req_lib_activatepoll.header.id = MESSAGE_REQ_LIB_ACTIVATEPOLL;
  204. error = saSendRetry (s, &req_lib_activatepoll,
  205. sizeof (struct req_lib_activatepoll), MSG_NOSIGNAL);
  206. return (error);
  207. }
  208. SaErrorT
  209. saSendRetry (
  210. int s,
  211. const void *msg,
  212. size_t len,
  213. int flags)
  214. {
  215. SaErrorT error = SA_OK;
  216. int result;
  217. struct msghdr msg_send;
  218. struct iovec iov_send;
  219. iov_send.iov_base = (void *)msg;
  220. iov_send.iov_len = len;
  221. msg_send.msg_iov = &iov_send;
  222. msg_send.msg_iovlen = 1;
  223. msg_send.msg_name = 0;
  224. msg_send.msg_namelen = 0;
  225. msg_send.msg_control = 0;
  226. msg_send.msg_controllen = 0;
  227. msg_send.msg_flags = 0;
  228. retry_send:
  229. result = sendmsg (s, &msg_send, flags);
  230. if (result == -1 && errno == EINTR) {
  231. goto retry_send;
  232. }
  233. if (result == -1) {
  234. error = SA_ERR_SYSTEM;
  235. }
  236. return (error);
  237. }
  238. SaErrorT saSendMsgRetry (
  239. int s,
  240. struct iovec *iov,
  241. int iov_len)
  242. {
  243. SaErrorT error = SA_OK;
  244. int result;
  245. struct msghdr msg_send;
  246. msg_send.msg_iov = iov;
  247. msg_send.msg_iovlen = iov_len;
  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, MSG_NOSIGNAL);
  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
  264. saSelectRetry (
  265. int s,
  266. fd_set *readfds,
  267. fd_set *writefds,
  268. fd_set *exceptfds,
  269. struct timeval *timeout)
  270. {
  271. SaErrorT error = SA_OK;
  272. int result;
  273. retry_select:
  274. result = select (s, readfds, writefds, exceptfds, timeout);
  275. if (result == -1 && errno == EINTR) {
  276. goto retry_select;
  277. }
  278. if (result == -1) {
  279. error = SA_ERR_SYSTEM;
  280. }
  281. return (error);
  282. }
  283. SaErrorT
  284. saPollRetry (
  285. struct pollfd *ufds,
  286. unsigned int nfds,
  287. int timeout)
  288. {
  289. SaErrorT error = SA_OK;
  290. int result;
  291. retry_poll:
  292. result = poll (ufds, nfds, timeout);
  293. if (result == -1 && errno == EINTR) {
  294. goto retry_poll;
  295. }
  296. if (result == -1) {
  297. error = SA_ERR_SYSTEM;
  298. }
  299. return (error);
  300. }
  301. SaErrorT
  302. saHandleCreate (
  303. struct saHandleDatabase *handleDatabase,
  304. int instanceSize,
  305. unsigned int *handleOut)
  306. {
  307. int handle;
  308. void *newHandles;
  309. int found = 0;
  310. void *instance;
  311. pthread_mutex_lock (&handleDatabase->mutex);
  312. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  313. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  314. found = 1;
  315. break;
  316. }
  317. }
  318. if (found == 0) {
  319. handleDatabase->handleCount += 1;
  320. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  321. sizeof (struct saHandle) * handleDatabase->handleCount);
  322. if (newHandles == 0) {
  323. pthread_mutex_unlock (&handleDatabase->mutex);
  324. return (SA_ERR_NO_MEMORY);
  325. }
  326. handleDatabase->handles = newHandles;
  327. }
  328. instance = malloc (instanceSize);
  329. if (instance == 0) {
  330. return (SA_ERR_NO_MEMORY);
  331. }
  332. memset (instance, 0, instanceSize);
  333. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  334. handleDatabase->handles[handle].instance = instance;
  335. handleDatabase->handles[handle].refCount = 1;
  336. *handleOut = handle;
  337. pthread_mutex_unlock (&handleDatabase->mutex);
  338. return (SA_OK);
  339. }
  340. SaErrorT
  341. saHandleDestroy (
  342. struct saHandleDatabase *handleDatabase,
  343. unsigned int handle)
  344. {
  345. pthread_mutex_lock (&handleDatabase->mutex);
  346. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  347. pthread_mutex_unlock (&handleDatabase->mutex);
  348. saHandleInstancePut (handleDatabase, handle);
  349. return (SA_OK);
  350. }
  351. SaErrorT
  352. saHandleInstanceGet (
  353. struct saHandleDatabase *handleDatabase,
  354. unsigned int handle,
  355. void **instance)
  356. {
  357. pthread_mutex_lock (&handleDatabase->mutex);
  358. if (handle > handleDatabase->handleCount) {
  359. return (SA_ERR_BAD_HANDLE);
  360. }
  361. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  362. return (SA_ERR_BAD_HANDLE);
  363. }
  364. *instance = handleDatabase->handles[handle].instance;
  365. handleDatabase->handles[handle].refCount += 1;
  366. pthread_mutex_unlock (&handleDatabase->mutex);
  367. return (SA_OK);
  368. }
  369. SaErrorT
  370. saHandleInstancePut (
  371. struct saHandleDatabase *handleDatabase,
  372. unsigned int handle)
  373. {
  374. void *instance;
  375. pthread_mutex_lock (&handleDatabase->mutex);
  376. handleDatabase->handles[handle].refCount -= 1;
  377. assert (handleDatabase->handles[handle].refCount >= 0);
  378. if (handleDatabase->handles[handle].refCount == 0) {
  379. instance = (handleDatabase->handles[handle].instance);
  380. handleDatabase->handleInstanceDestructor (instance);
  381. free (instance);
  382. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  383. }
  384. pthread_mutex_unlock (&handleDatabase->mutex);
  385. return (SA_OK);
  386. }
  387. SaErrorT
  388. saVersionVerify (
  389. struct saVersionDatabase *versionDatabase,
  390. const SaVersionT *version)
  391. {
  392. int found = 0;
  393. int i;
  394. if (version == 0) {
  395. return (SA_ERR_VERSION);
  396. }
  397. for (i = 0; i < versionDatabase->versionCount; i++) {
  398. if (memcmp (&versionDatabase->versionsSupported[i], version, sizeof (SaVersionT)) == 0) {
  399. found = 1;
  400. break;
  401. }
  402. }
  403. return (found ? SA_OK : SA_ERR_VERSION);
  404. }
  405. SaErrorT
  406. saQueueInit (
  407. struct queue *queue,
  408. int queueItems,
  409. int bytesPerItem)
  410. {
  411. queue->head = 0;
  412. queue->tail = queueItems - 1;
  413. queue->used = 0;
  414. queue->usedhw = 0;
  415. queue->size = queueItems;
  416. queue->bytesPerItem = bytesPerItem;
  417. queue->items = (void *)malloc (queueItems * bytesPerItem);
  418. if (queue->items == 0) {
  419. return (SA_ERR_NO_MEMORY);
  420. }
  421. memset (queue->items, 0, queueItems * bytesPerItem);
  422. return (SA_OK);
  423. }
  424. SaErrorT
  425. saQueueIsFull (
  426. struct queue *queue,
  427. int *isFull)
  428. {
  429. *isFull = ((queue->size - 1) == queue->used);
  430. return (SA_OK);
  431. }
  432. SaErrorT
  433. saQueueIsEmpty (
  434. struct queue *queue,
  435. int *isEmpty)
  436. {
  437. *isEmpty = (queue->used == 0);
  438. return (SA_OK);
  439. }
  440. SaErrorT
  441. saQueueItemAdd (
  442. struct queue *queue,
  443. void *item)
  444. {
  445. char *queueItem;
  446. int queuePosition;
  447. queuePosition = queue->head;
  448. queueItem = queue->items;
  449. queueItem += queuePosition * queue->bytesPerItem;
  450. memcpy (queueItem, item, queue->bytesPerItem);
  451. if (queue->tail == queue->head) {
  452. return (SA_ERR_LIBRARY);
  453. }
  454. queue->head = (queue->head + 1) % queue->size;
  455. queue->used++;
  456. if (queue->used > queue->usedhw) {
  457. queue->usedhw = queue->used;
  458. }
  459. return (SA_OK);
  460. }
  461. SaErrorT
  462. saQueueItemGet (struct queue *queue, void **item)
  463. {
  464. char *queueItem;
  465. int queuePosition;
  466. queuePosition = (queue->tail + 1) % queue->size;
  467. queueItem = queue->items;
  468. queueItem += queuePosition * queue->bytesPerItem;
  469. *item = (void *)queueItem;
  470. return (SA_OK);
  471. }
  472. SaErrorT
  473. saQueueItemRemove (struct queue *queue)
  474. {
  475. queue->tail = (queue->tail + 1) % queue->size;
  476. if (queue->tail == queue->head) {
  477. return (SA_ERR_LIBRARY);
  478. }
  479. queue->used--;
  480. return (SA_OK);
  481. }