util.c 12 KB

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