util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <errno.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/types.h>
  42. #include <sys/uio.h>
  43. #include <sys/socket.h>
  44. #include <sys/select.h>
  45. #include <sys/time.h>
  46. #include <sys/un.h>
  47. #include <net/if.h>
  48. #include <arpa/inet.h>
  49. #include <netinet/in.h>
  50. #include <assert.h>
  51. #include "../include/saAis.h"
  52. #include "../include/ipc_gen.h"
  53. #include "util.h"
  54. enum SA_HANDLE_STATE {
  55. SA_HANDLE_STATE_EMPTY,
  56. SA_HANDLE_STATE_PENDINGREMOVAL,
  57. SA_HANDLE_STATE_ACTIVE
  58. };
  59. struct saHandle {
  60. int state;
  61. void *instance;
  62. int refCount;
  63. uint32_t check;
  64. };
  65. SaErrorT
  66. saServiceConnect (
  67. int *fdOut,
  68. enum service_types service)
  69. {
  70. int fd;
  71. int result;
  72. struct sockaddr_un address;
  73. struct req_lib_response_init req_lib_response_init;
  74. struct res_lib_response_init res_lib_response_init;
  75. SaErrorT error;
  76. gid_t egid;
  77. /*
  78. * Allow set group id binaries to be authenticated
  79. */
  80. egid = getegid();
  81. setregid (egid, -1);
  82. memset (&address, 0, sizeof (struct sockaddr_un));
  83. address.sun_family = PF_UNIX;
  84. strcpy (address.sun_path + 1, "libais.socket");
  85. fd = socket (PF_UNIX, SOCK_STREAM, 0);
  86. if (fd == -1) {
  87. return (SA_AIS_ERR_NO_RESOURCES);
  88. }
  89. result = connect (fd, (struct sockaddr *)&address, sizeof (address));
  90. if (result == -1) {
  91. return (SA_AIS_ERR_TRY_AGAIN);
  92. }
  93. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  94. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  95. req_lib_response_init.resdis_header.service = service;
  96. error = saSendRetry (fd, &req_lib_response_init,
  97. sizeof (struct req_lib_response_init), MSG_NOSIGNAL);
  98. if (error != SA_AIS_OK) {
  99. goto error_exit;
  100. }
  101. error = saRecvRetry (fd, &res_lib_response_init,
  102. sizeof (struct res_lib_response_init), MSG_WAITALL | MSG_NOSIGNAL);
  103. if (error != SA_AIS_OK) {
  104. goto error_exit;
  105. }
  106. /*
  107. * Check for security errors
  108. */
  109. if (res_lib_response_init.header.error != SA_AIS_OK) {
  110. error = res_lib_response_init.header.error;
  111. goto error_exit;
  112. }
  113. *fdOut = fd;
  114. return (SA_AIS_OK);
  115. error_exit:
  116. close (fd);
  117. return (error);
  118. }
  119. SaErrorT
  120. saServiceConnectTwo (
  121. int *responseOut,
  122. int *callbackOut,
  123. enum service_types service)
  124. {
  125. int responseFD;
  126. int callbackFD;
  127. int result;
  128. struct sockaddr_un address;
  129. struct req_lib_response_init req_lib_response_init;
  130. struct res_lib_response_init res_lib_response_init;
  131. struct req_lib_dispatch_init req_lib_dispatch_init;
  132. struct res_lib_dispatch_init res_lib_dispatch_init;
  133. SaErrorT error;
  134. gid_t egid;
  135. /*
  136. * Allow set group id binaries to be authenticated
  137. */
  138. egid = getegid();
  139. setregid (egid, -1);
  140. memset (&address, 0, sizeof (struct sockaddr_un));
  141. address.sun_family = PF_UNIX;
  142. strcpy (address.sun_path + 1, "libais.socket");
  143. responseFD = socket (PF_UNIX, SOCK_STREAM, 0);
  144. if (responseFD == -1) {
  145. return (SA_AIS_ERR_NO_RESOURCES);
  146. }
  147. result = connect (responseFD, (struct sockaddr *)&address, sizeof (address));
  148. if (result == -1) {
  149. return (SA_AIS_ERR_TRY_AGAIN);
  150. }
  151. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  152. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  153. req_lib_response_init.resdis_header.service = service;
  154. error = saSendRetry (responseFD, &req_lib_response_init,
  155. sizeof (struct req_lib_response_init),
  156. MSG_NOSIGNAL);
  157. if (error != SA_AIS_OK) {
  158. goto error_exit;
  159. }
  160. error = saRecvRetry (responseFD, &res_lib_response_init,
  161. sizeof (struct res_lib_response_init),
  162. MSG_WAITALL | MSG_NOSIGNAL);
  163. if (error != SA_AIS_OK) {
  164. goto error_exit;
  165. }
  166. /*
  167. * Check for security errors
  168. */
  169. if (res_lib_response_init.header.error != SA_AIS_OK) {
  170. error = res_lib_response_init.header.error;
  171. goto error_exit;
  172. }
  173. *responseOut = responseFD;
  174. /* if I comment out the 4 lines below the executive crashes */
  175. callbackFD = socket (PF_UNIX, SOCK_STREAM, 0);
  176. if (callbackFD == -1) {
  177. return (SA_AIS_ERR_NO_RESOURCES);
  178. }
  179. result = connect (callbackFD, (struct sockaddr *)&address, sizeof (address));
  180. if (result == -1) {
  181. return (SA_AIS_ERR_TRY_AGAIN);
  182. }
  183. req_lib_dispatch_init.resdis_header.size = sizeof (req_lib_dispatch_init);
  184. req_lib_dispatch_init.resdis_header.id = MESSAGE_REQ_DISPATCH_INIT;
  185. req_lib_dispatch_init.resdis_header.service = service;
  186. req_lib_dispatch_init.conn_info = res_lib_response_init.conn_info;
  187. error = saSendRetry (callbackFD, &req_lib_dispatch_init,
  188. sizeof (struct req_lib_dispatch_init),
  189. MSG_NOSIGNAL);
  190. if (error != SA_AIS_OK) {
  191. goto error_exit_two;
  192. }
  193. error = saRecvRetry (callbackFD, &res_lib_dispatch_init,
  194. sizeof (struct res_lib_dispatch_init),
  195. MSG_WAITALL | MSG_NOSIGNAL);
  196. if (error != SA_AIS_OK) {
  197. goto error_exit_two;
  198. }
  199. /*
  200. * Check for security errors
  201. */
  202. if (res_lib_dispatch_init.header.error != SA_AIS_OK) {
  203. error = res_lib_dispatch_init.header.error;
  204. goto error_exit;
  205. }
  206. *callbackOut = callbackFD;
  207. return (SA_AIS_OK);
  208. error_exit_two:
  209. close (callbackFD);
  210. error_exit:
  211. close (responseFD);
  212. return (error);
  213. }
  214. SaErrorT
  215. saRecvRetry (
  216. int s,
  217. void *msg,
  218. size_t len,
  219. int flags)
  220. {
  221. SaErrorT error = SA_AIS_OK;
  222. int result;
  223. struct msghdr msg_recv;
  224. struct iovec iov_recv;
  225. char *rbuf = (char *)msg;
  226. int processed = 0;
  227. msg_recv.msg_iov = &iov_recv;
  228. msg_recv.msg_iovlen = 1;
  229. msg_recv.msg_name = 0;
  230. msg_recv.msg_namelen = 0;
  231. msg_recv.msg_control = 0;
  232. msg_recv.msg_controllen = 0;
  233. msg_recv.msg_flags = 0;
  234. retry_recv:
  235. iov_recv.iov_base = (void *)&rbuf[processed];
  236. iov_recv.iov_len = len - processed;
  237. result = recvmsg (s, &msg_recv, flags);
  238. if (result == -1 && errno == EINTR) {
  239. goto retry_recv;
  240. }
  241. if (result == -1 || result == 0) {
  242. error = SA_AIS_ERR_LIBRARY;
  243. goto error_exit;
  244. }
  245. processed += result;
  246. if (processed != len) {
  247. goto retry_recv;
  248. }
  249. assert (processed == len);
  250. error_exit:
  251. return (error);
  252. }
  253. struct res_overlay {
  254. struct res_header header;
  255. char payload[0];
  256. };
  257. SaErrorT
  258. saSendRetry (
  259. int s,
  260. const void *msg,
  261. size_t len,
  262. int flags)
  263. {
  264. SaErrorT error = SA_AIS_OK;
  265. int result;
  266. struct msghdr msg_send;
  267. struct iovec iov_send;
  268. iov_send.iov_base = (void *)msg;
  269. iov_send.iov_len = len;
  270. msg_send.msg_iov = &iov_send;
  271. msg_send.msg_iovlen = 1;
  272. msg_send.msg_name = 0;
  273. msg_send.msg_namelen = 0;
  274. msg_send.msg_control = 0;
  275. msg_send.msg_controllen = 0;
  276. msg_send.msg_flags = 0;
  277. retry_send:
  278. result = sendmsg (s, &msg_send, flags);
  279. if (result == -1 && errno == EINTR) {
  280. goto retry_send;
  281. }
  282. if (result == -1) {
  283. error = SA_AIS_ERR_LIBRARY;
  284. }
  285. return (error);
  286. }
  287. SaErrorT saSendMsgRetry (
  288. int s,
  289. struct iovec *iov,
  290. int iov_len)
  291. {
  292. SaErrorT error = SA_AIS_OK;
  293. int result;
  294. struct msghdr msg_send;
  295. msg_send.msg_iov = iov;
  296. msg_send.msg_iovlen = iov_len;
  297. msg_send.msg_name = 0;
  298. msg_send.msg_namelen = 0;
  299. msg_send.msg_control = 0;
  300. msg_send.msg_controllen = 0;
  301. msg_send.msg_flags = 0;
  302. retry_send:
  303. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  304. if (result == -1 && errno == EINTR) {
  305. goto retry_send;
  306. }
  307. if (result == -1) {
  308. error = SA_AIS_ERR_LIBRARY;
  309. }
  310. return (error);
  311. }
  312. SaErrorT saSendMsgReceiveReply (
  313. int s,
  314. struct iovec *iov,
  315. int iov_len,
  316. void *responseMessage,
  317. int responseLen)
  318. {
  319. SaErrorT error = SA_AIS_OK;
  320. error = saSendMsgRetry (s, iov, iov_len);
  321. if (error != SA_AIS_OK) {
  322. goto error_exit;
  323. }
  324. error = saRecvRetry (s, responseMessage, responseLen,
  325. MSG_WAITALL | MSG_NOSIGNAL);
  326. if (error != SA_AIS_OK) {
  327. goto error_exit;
  328. }
  329. error_exit:
  330. return (error);
  331. }
  332. SaErrorT saSendReceiveReply (
  333. int s,
  334. void *requestMessage,
  335. int requestLen,
  336. void *responseMessage,
  337. int responseLen)
  338. {
  339. SaErrorT error = SA_AIS_OK;
  340. error = saSendRetry (s, requestMessage, requestLen,
  341. MSG_NOSIGNAL);
  342. if (error != SA_AIS_OK) {
  343. goto error_exit;
  344. }
  345. error = saRecvRetry (s, responseMessage, responseLen,
  346. MSG_WAITALL | MSG_NOSIGNAL);
  347. if (error != SA_AIS_OK) {
  348. goto error_exit;
  349. }
  350. error_exit:
  351. return (error);
  352. }
  353. SaErrorT
  354. saPollRetry (
  355. struct pollfd *ufds,
  356. unsigned int nfds,
  357. int timeout)
  358. {
  359. SaErrorT error = SA_AIS_OK;
  360. int result;
  361. retry_poll:
  362. result = poll (ufds, nfds, timeout);
  363. if (result == -1 && errno == EINTR) {
  364. goto retry_poll;
  365. }
  366. if (result == -1) {
  367. error = SA_AIS_ERR_LIBRARY;
  368. }
  369. return (error);
  370. }
  371. SaErrorT
  372. saHandleCreate (
  373. struct saHandleDatabase *handleDatabase,
  374. int instanceSize,
  375. SaUint64T *handleOut)
  376. {
  377. uint32_t handle;
  378. uint32_t check;
  379. void *newHandles;
  380. int found = 0;
  381. void *instance;
  382. pthread_mutex_lock (&handleDatabase->mutex);
  383. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  384. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  385. found = 1;
  386. break;
  387. }
  388. }
  389. if (found == 0) {
  390. handleDatabase->handleCount += 1;
  391. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  392. sizeof (struct saHandle) * handleDatabase->handleCount);
  393. if (newHandles == 0) {
  394. pthread_mutex_unlock (&handleDatabase->mutex);
  395. return (SA_AIS_ERR_NO_MEMORY);
  396. }
  397. handleDatabase->handles = newHandles;
  398. }
  399. instance = malloc (instanceSize);
  400. if (instance == 0) {
  401. return (SA_AIS_ERR_NO_MEMORY);
  402. }
  403. check = random();
  404. memset (instance, 0, instanceSize);
  405. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  406. handleDatabase->handles[handle].instance = instance;
  407. handleDatabase->handles[handle].refCount = 1;
  408. handleDatabase->handles[handle].check = check;
  409. *handleOut = (SaUint64T)((uint64_t)check << 32 | handle);
  410. pthread_mutex_unlock (&handleDatabase->mutex);
  411. return (SA_AIS_OK);
  412. }
  413. SaErrorT
  414. saHandleDestroy (
  415. struct saHandleDatabase *handleDatabase,
  416. SaUint64T inHandle)
  417. {
  418. SaAisErrorT error = SA_AIS_OK;
  419. uint32_t check = inHandle >> 32;
  420. uint32_t handle = inHandle & 0xffffffff;
  421. pthread_mutex_lock (&handleDatabase->mutex);
  422. if (check != handleDatabase->handles[handle].check) {
  423. error = SA_AIS_ERR_BAD_HANDLE;
  424. goto error_exit;
  425. }
  426. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  427. error_exit:
  428. pthread_mutex_unlock (&handleDatabase->mutex);
  429. saHandleInstancePut (handleDatabase, inHandle);
  430. return (error);
  431. }
  432. SaErrorT
  433. saHandleInstanceGet (
  434. struct saHandleDatabase *handleDatabase,
  435. SaUint64T inHandle,
  436. void **instance)
  437. {
  438. uint32_t check = inHandle >> 32;
  439. uint32_t handle = inHandle & 0xffffffff;
  440. SaErrorT error = SA_AIS_OK;
  441. pthread_mutex_lock (&handleDatabase->mutex);
  442. if (handle >= (SaUint64T)handleDatabase->handleCount) {
  443. error = SA_AIS_ERR_BAD_HANDLE;
  444. goto error_exit;
  445. }
  446. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  447. error = SA_AIS_ERR_BAD_HANDLE;
  448. goto error_exit;
  449. }
  450. if (check != handleDatabase->handles[handle].check) {
  451. error = SA_AIS_ERR_BAD_HANDLE;
  452. goto error_exit;
  453. }
  454. *instance = handleDatabase->handles[handle].instance;
  455. handleDatabase->handles[handle].refCount += 1;
  456. error_exit:
  457. pthread_mutex_unlock (&handleDatabase->mutex);
  458. return (error);
  459. }
  460. SaErrorT
  461. saHandleInstancePut (
  462. struct saHandleDatabase *handleDatabase,
  463. SaUint64T inHandle)
  464. {
  465. void *instance;
  466. SaAisErrorT error = SA_AIS_OK;
  467. uint32_t check = inHandle >> 32;
  468. uint32_t handle = inHandle & 0xffffffff;
  469. pthread_mutex_lock (&handleDatabase->mutex);
  470. if (check != handleDatabase->handles[handle].check) {
  471. error = SA_AIS_ERR_BAD_HANDLE;
  472. goto error_exit;
  473. }
  474. handleDatabase->handles[handle].refCount -= 1;
  475. assert (handleDatabase->handles[handle].refCount >= 0);
  476. if (handleDatabase->handles[handle].refCount == 0) {
  477. instance = (handleDatabase->handles[handle].instance);
  478. handleDatabase->handleInstanceDestructor (instance);
  479. free (instance);
  480. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  481. }
  482. error_exit:
  483. pthread_mutex_unlock (&handleDatabase->mutex);
  484. return (error);
  485. }
  486. SaErrorT
  487. saVersionVerify (
  488. struct saVersionDatabase *versionDatabase,
  489. SaVersionT *version)
  490. {
  491. int i;
  492. SaErrorT error = SA_AIS_ERR_VERSION;
  493. if (version == 0) {
  494. return (SA_AIS_ERR_INVALID_PARAM);
  495. }
  496. /*
  497. * Look for a release code that we support. If we find it then
  498. * make sure that the supported major version is >= to the required one.
  499. * In any case we return what we support in the version structure.
  500. */
  501. for (i = 0; i < versionDatabase->versionCount; i++) {
  502. /*
  503. * Check if the caller requires and old release code that we don't support.
  504. */
  505. if (version->releaseCode < versionDatabase->versionsSupported[i].releaseCode) {
  506. break;
  507. }
  508. /*
  509. * Check if we can support this release code.
  510. */
  511. if (version->releaseCode == versionDatabase->versionsSupported[i].releaseCode) {
  512. /*
  513. * Check if we can support the major version requested.
  514. */
  515. if (versionDatabase->versionsSupported[i].majorVersion >= version->majorVersion) {
  516. error = SA_AIS_OK;
  517. break;
  518. }
  519. /*
  520. * We support the release code, but not the major version.
  521. */
  522. break;
  523. }
  524. }
  525. /*
  526. * If we fall out of the if loop, the caller requires a release code
  527. * beyond what we support.
  528. */
  529. if (i == versionDatabase->versionCount) {
  530. i = versionDatabase->versionCount - 1;
  531. }
  532. /*
  533. * Tell the caller what we support
  534. */
  535. memcpy(version, &versionDatabase->versionsSupported[i], sizeof(*version));
  536. return (error);
  537. }
  538. /*
  539. * Get the time of day and convert to nanoseconds
  540. */
  541. SaTimeT clustTimeNow(void)
  542. {
  543. struct timeval tv;
  544. SaTimeT time_now;
  545. if (gettimeofday(&tv, 0)) {
  546. return 0ULL;
  547. }
  548. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  549. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  550. return time_now;
  551. }