4
0

util.c 12 KB

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