util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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/ais_types.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. };
  64. SaErrorT
  65. saServiceConnect (
  66. int *fdOut,
  67. enum service_types service)
  68. {
  69. int fd;
  70. int result;
  71. struct sockaddr_un address;
  72. struct req_lib_response_init req_lib_response_init;
  73. struct res_lib_response_init res_lib_response_init;
  74. SaErrorT error;
  75. gid_t egid;
  76. /*
  77. * Allow set group id binaries to be authenticated
  78. */
  79. egid = getegid();
  80. setregid (egid, -1);
  81. memset (&address, 0, sizeof (struct sockaddr_un));
  82. address.sun_family = PF_UNIX;
  83. strcpy (address.sun_path + 1, "libais.socket");
  84. fd = socket (PF_UNIX, SOCK_STREAM, 0);
  85. if (fd == -1) {
  86. return (SA_AIS_ERR_NO_RESOURCES);
  87. }
  88. result = connect (fd, (struct sockaddr *)&address, sizeof (address));
  89. if (result == -1) {
  90. return (SA_AIS_ERR_TRY_AGAIN);
  91. }
  92. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  93. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  94. req_lib_response_init.resdis_header.service = service;
  95. error = saSendRetry (fd, &req_lib_response_init,
  96. sizeof (struct req_lib_response_init), MSG_NOSIGNAL);
  97. if (error != SA_AIS_OK) {
  98. goto error_exit;
  99. }
  100. error = saRecvRetry (fd, &res_lib_response_init,
  101. sizeof (struct res_lib_response_init), MSG_WAITALL | MSG_NOSIGNAL);
  102. if (error != SA_AIS_OK) {
  103. goto error_exit;
  104. }
  105. /*
  106. * Check for security errors
  107. */
  108. if (res_lib_response_init.header.error != SA_AIS_OK) {
  109. error = res_lib_response_init.header.error;
  110. goto error_exit;
  111. }
  112. *fdOut = fd;
  113. return (SA_AIS_OK);
  114. error_exit:
  115. close (fd);
  116. return (error);
  117. }
  118. SaErrorT
  119. saServiceConnectTwo (
  120. int *responseOut,
  121. int *callbackOut,
  122. enum service_types service)
  123. {
  124. int responseFD;
  125. int callbackFD;
  126. int result;
  127. struct sockaddr_un address;
  128. struct req_lib_response_init req_lib_response_init;
  129. struct res_lib_response_init res_lib_response_init;
  130. struct req_lib_dispatch_init req_lib_dispatch_init;
  131. struct res_lib_dispatch_init res_lib_dispatch_init;
  132. SaErrorT error;
  133. gid_t egid;
  134. /*
  135. * Allow set group id binaries to be authenticated
  136. */
  137. egid = getegid();
  138. setregid (egid, -1);
  139. memset (&address, 0, sizeof (struct sockaddr_un));
  140. address.sun_family = PF_UNIX;
  141. strcpy (address.sun_path + 1, "libais.socket");
  142. responseFD = socket (PF_UNIX, SOCK_STREAM, 0);
  143. if (responseFD == -1) {
  144. return (SA_AIS_ERR_NO_RESOURCES);
  145. }
  146. result = connect (responseFD, (struct sockaddr *)&address, sizeof (address));
  147. if (result == -1) {
  148. return (SA_AIS_ERR_TRY_AGAIN);
  149. }
  150. req_lib_response_init.resdis_header.size = sizeof (req_lib_response_init);
  151. req_lib_response_init.resdis_header.id = MESSAGE_REQ_RESPONSE_INIT;
  152. req_lib_response_init.resdis_header.service = service;
  153. error = saSendRetry (responseFD, &req_lib_response_init,
  154. sizeof (struct req_lib_response_init),
  155. MSG_NOSIGNAL);
  156. if (error != SA_AIS_OK) {
  157. goto error_exit;
  158. }
  159. error = saRecvRetry (responseFD, &res_lib_response_init,
  160. sizeof (struct res_lib_response_init),
  161. MSG_WAITALL | MSG_NOSIGNAL);
  162. if (error != SA_AIS_OK) {
  163. goto error_exit;
  164. }
  165. /*
  166. * Check for security errors
  167. */
  168. if (res_lib_response_init.header.error != SA_AIS_OK) {
  169. error = res_lib_response_init.header.error;
  170. goto error_exit;
  171. }
  172. *responseOut = responseFD;
  173. /* if I comment out the 4 lines below the executive crashes */
  174. callbackFD = socket (PF_UNIX, SOCK_STREAM, 0);
  175. if (callbackFD == -1) {
  176. return (SA_AIS_ERR_NO_RESOURCES);
  177. }
  178. result = connect (callbackFD, (struct sockaddr *)&address, sizeof (address));
  179. if (result == -1) {
  180. return (SA_AIS_ERR_TRY_AGAIN);
  181. }
  182. req_lib_dispatch_init.resdis_header.size = sizeof (req_lib_dispatch_init);
  183. req_lib_dispatch_init.resdis_header.id = MESSAGE_REQ_DISPATCH_INIT;
  184. req_lib_dispatch_init.resdis_header.service = service;
  185. req_lib_dispatch_init.conn_info = res_lib_response_init.conn_info;
  186. error = saSendRetry (callbackFD, &req_lib_dispatch_init,
  187. sizeof (struct req_lib_dispatch_init),
  188. MSG_NOSIGNAL);
  189. if (error != SA_AIS_OK) {
  190. goto error_exit_two;
  191. }
  192. error = saRecvRetry (callbackFD, &res_lib_dispatch_init,
  193. sizeof (struct res_lib_dispatch_init),
  194. MSG_WAITALL | MSG_NOSIGNAL);
  195. if (error != SA_AIS_OK) {
  196. goto error_exit_two;
  197. }
  198. /*
  199. * Check for security errors
  200. */
  201. if (res_lib_dispatch_init.header.error != SA_AIS_OK) {
  202. error = res_lib_dispatch_init.header.error;
  203. goto error_exit;
  204. }
  205. *callbackOut = callbackFD;
  206. return (SA_AIS_OK);
  207. error_exit_two:
  208. close (callbackFD);
  209. error_exit:
  210. close (responseFD);
  211. return (error);
  212. }
  213. SaErrorT
  214. saRecvRetry (
  215. int s,
  216. void *msg,
  217. size_t len,
  218. int flags)
  219. {
  220. SaErrorT error = SA_AIS_OK;
  221. int result;
  222. struct msghdr msg_recv;
  223. struct iovec iov_recv;
  224. char *rbuf = (char *)msg;
  225. int processed = 0;
  226. msg_recv.msg_iov = &iov_recv;
  227. msg_recv.msg_iovlen = 1;
  228. msg_recv.msg_name = 0;
  229. msg_recv.msg_namelen = 0;
  230. msg_recv.msg_control = 0;
  231. msg_recv.msg_controllen = 0;
  232. msg_recv.msg_flags = 0;
  233. retry_recv:
  234. iov_recv.iov_base = (void *)&rbuf[processed];
  235. iov_recv.iov_len = len - processed;
  236. result = recvmsg (s, &msg_recv, flags);
  237. if (result == -1 && errno == EINTR) {
  238. goto retry_recv;
  239. }
  240. if (result == -1 || result == 0) {
  241. error = SA_AIS_ERR_LIBRARY;
  242. goto error_exit;
  243. }
  244. processed += result;
  245. if (processed != len) {
  246. goto retry_recv;
  247. }
  248. assert (processed == len);
  249. error_exit:
  250. return (error);
  251. }
  252. struct res_overlay {
  253. struct res_header header;
  254. char payload[0];
  255. };
  256. SaErrorT
  257. saSendRetry (
  258. int s,
  259. const void *msg,
  260. size_t len,
  261. int flags)
  262. {
  263. SaErrorT error = SA_AIS_OK;
  264. int result;
  265. struct msghdr msg_send;
  266. struct iovec iov_send;
  267. iov_send.iov_base = (void *)msg;
  268. iov_send.iov_len = len;
  269. msg_send.msg_iov = &iov_send;
  270. msg_send.msg_iovlen = 1;
  271. msg_send.msg_name = 0;
  272. msg_send.msg_namelen = 0;
  273. msg_send.msg_control = 0;
  274. msg_send.msg_controllen = 0;
  275. msg_send.msg_flags = 0;
  276. retry_send:
  277. result = sendmsg (s, &msg_send, flags);
  278. if (result == -1 && errno == EINTR) {
  279. goto retry_send;
  280. }
  281. if (result == -1) {
  282. error = SA_AIS_ERR_LIBRARY;
  283. }
  284. return (error);
  285. }
  286. SaErrorT saSendMsgRetry (
  287. int s,
  288. struct iovec *iov,
  289. int iov_len)
  290. {
  291. SaErrorT error = SA_AIS_OK;
  292. int result;
  293. struct msghdr msg_send;
  294. msg_send.msg_iov = iov;
  295. msg_send.msg_iovlen = iov_len;
  296. msg_send.msg_name = 0;
  297. msg_send.msg_namelen = 0;
  298. msg_send.msg_control = 0;
  299. msg_send.msg_controllen = 0;
  300. msg_send.msg_flags = 0;
  301. retry_send:
  302. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  303. if (result == -1 && errno == EINTR) {
  304. goto retry_send;
  305. }
  306. if (result == -1) {
  307. error = SA_AIS_ERR_LIBRARY;
  308. }
  309. return (error);
  310. }
  311. SaErrorT saSendMsgReceiveReply (
  312. int s,
  313. struct iovec *iov,
  314. int iov_len,
  315. void *responseMessage,
  316. int responseLen)
  317. {
  318. SaErrorT error = SA_AIS_OK;
  319. error = saSendMsgRetry (s, iov, iov_len);
  320. if (error != SA_AIS_OK) {
  321. goto error_exit;
  322. }
  323. error = saRecvRetry (s, responseMessage, responseLen,
  324. MSG_WAITALL | MSG_NOSIGNAL);
  325. if (error != SA_AIS_OK) {
  326. goto error_exit;
  327. }
  328. error_exit:
  329. return (error);
  330. }
  331. SaErrorT saSendReceiveReply (
  332. int s,
  333. void *requestMessage,
  334. int requestLen,
  335. void *responseMessage,
  336. int responseLen)
  337. {
  338. SaErrorT error = SA_AIS_OK;
  339. error = saSendRetry (s, requestMessage, requestLen,
  340. MSG_NOSIGNAL);
  341. if (error != SA_AIS_OK) {
  342. goto error_exit;
  343. }
  344. error = saRecvRetry (s, responseMessage, responseLen,
  345. MSG_WAITALL | MSG_NOSIGNAL);
  346. if (error != SA_AIS_OK) {
  347. goto error_exit;
  348. }
  349. error_exit:
  350. return (error);
  351. }
  352. SaErrorT
  353. saPollRetry (
  354. struct pollfd *ufds,
  355. unsigned int nfds,
  356. int timeout)
  357. {
  358. SaErrorT error = SA_AIS_OK;
  359. int result;
  360. retry_poll:
  361. result = poll (ufds, nfds, timeout);
  362. if (result == -1 && errno == EINTR) {
  363. goto retry_poll;
  364. }
  365. if (result == -1) {
  366. error = SA_AIS_ERR_LIBRARY;
  367. }
  368. return (error);
  369. }
  370. SaErrorT
  371. saHandleCreate (
  372. struct saHandleDatabase *handleDatabase,
  373. int instanceSize,
  374. SaUint64T *handleOut)
  375. {
  376. int handle;
  377. void *newHandles;
  378. int found = 0;
  379. void *instance;
  380. pthread_mutex_lock (&handleDatabase->mutex);
  381. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  382. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  383. found = 1;
  384. break;
  385. }
  386. }
  387. if (found == 0) {
  388. handleDatabase->handleCount += 1;
  389. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  390. sizeof (struct saHandle) * handleDatabase->handleCount);
  391. if (newHandles == 0) {
  392. pthread_mutex_unlock (&handleDatabase->mutex);
  393. return (SA_AIS_ERR_NO_MEMORY);
  394. }
  395. handleDatabase->handles = newHandles;
  396. }
  397. instance = malloc (instanceSize);
  398. if (instance == 0) {
  399. return (SA_AIS_ERR_NO_MEMORY);
  400. }
  401. memset (instance, 0, instanceSize);
  402. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  403. handleDatabase->handles[handle].instance = instance;
  404. handleDatabase->handles[handle].refCount = 1;
  405. *handleOut = handle;
  406. pthread_mutex_unlock (&handleDatabase->mutex);
  407. return (SA_AIS_OK);
  408. }
  409. SaErrorT
  410. saHandleDestroy (
  411. struct saHandleDatabase *handleDatabase,
  412. SaUint64T handle)
  413. {
  414. pthread_mutex_lock (&handleDatabase->mutex);
  415. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  416. pthread_mutex_unlock (&handleDatabase->mutex);
  417. saHandleInstancePut (handleDatabase, handle);
  418. return (SA_AIS_OK);
  419. }
  420. SaErrorT
  421. saHandleInstanceGet (
  422. struct saHandleDatabase *handleDatabase,
  423. SaUint64T handle,
  424. void **instance)
  425. {
  426. SaErrorT error = SA_AIS_OK;
  427. pthread_mutex_lock (&handleDatabase->mutex);
  428. if (handle >= (SaUint64T)handleDatabase->handleCount) {
  429. error = SA_AIS_ERR_BAD_HANDLE;
  430. goto error_exit;
  431. }
  432. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  433. error = SA_AIS_ERR_BAD_HANDLE;
  434. goto error_exit;
  435. }
  436. *instance = handleDatabase->handles[handle].instance;
  437. handleDatabase->handles[handle].refCount += 1;
  438. error_exit:
  439. pthread_mutex_unlock (&handleDatabase->mutex);
  440. return (error);
  441. }
  442. SaErrorT
  443. saHandleInstancePut (
  444. struct saHandleDatabase *handleDatabase,
  445. SaUint64T handle)
  446. {
  447. void *instance;
  448. pthread_mutex_lock (&handleDatabase->mutex);
  449. handleDatabase->handles[handle].refCount -= 1;
  450. assert (handleDatabase->handles[handle].refCount >= 0);
  451. if (handleDatabase->handles[handle].refCount == 0) {
  452. instance = (handleDatabase->handles[handle].instance);
  453. handleDatabase->handleInstanceDestructor (instance);
  454. free (instance);
  455. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  456. }
  457. pthread_mutex_unlock (&handleDatabase->mutex);
  458. return (SA_AIS_OK);
  459. }
  460. SaErrorT
  461. saVersionVerify (
  462. struct saVersionDatabase *versionDatabase,
  463. SaVersionT *version)
  464. {
  465. int i;
  466. SaErrorT error = SA_AIS_ERR_VERSION;
  467. if (version == 0) {
  468. return (SA_AIS_ERR_INVALID_PARAM);
  469. }
  470. /*
  471. * Look for a release code that we support. If we find it then
  472. * make sure that the supported major version is >= to the required one.
  473. * In any case we return what we support in the version structure.
  474. */
  475. for (i = 0; i < versionDatabase->versionCount; i++) {
  476. /*
  477. * Check if the caller requires and old release code that we don't support.
  478. */
  479. if (version->releaseCode < versionDatabase->versionsSupported[i].releaseCode) {
  480. break;
  481. }
  482. /*
  483. * Check if we can support this release code.
  484. */
  485. if (version->releaseCode == versionDatabase->versionsSupported[i].releaseCode) {
  486. /*
  487. * Check if we can support the major version requested.
  488. */
  489. if (versionDatabase->versionsSupported[i].major >= version->major) {
  490. error = SA_AIS_OK;
  491. break;
  492. }
  493. /*
  494. * We support the release code, but not the major version.
  495. */
  496. break;
  497. }
  498. }
  499. /*
  500. * If we fall out of the if loop, the caller requires a release code
  501. * beyond what we support.
  502. */
  503. if (i == versionDatabase->versionCount) {
  504. i = versionDatabase->versionCount - 1;
  505. }
  506. /*
  507. * Tell the caller what we support
  508. */
  509. memcpy(version, &versionDatabase->versionsSupported[i], sizeof(*version));
  510. return (error);
  511. }
  512. /*
  513. * Get the time of day and convert to nanoseconds
  514. */
  515. SaTimeT clustTimeNow(void)
  516. {
  517. struct timeval tv;
  518. SaTimeT time_now;
  519. if (gettimeofday(&tv, 0)) {
  520. return 0ULL;
  521. }
  522. time_now = (SaTimeT)(tv.tv_sec) * 1000000000ULL;
  523. time_now += (SaTimeT)(tv.tv_usec) * 1000ULL;
  524. return time_now;
  525. }