util.c 12 KB

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