clm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #include <pthread.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/select.h>
  43. #include <sys/un.h>
  44. #include "../include/ais_types.h"
  45. #include "../include/ais_clm.h"
  46. #include "../include/ais_msg.h"
  47. #include "util.h"
  48. struct res_overlay {
  49. struct res_header header;
  50. char data[4096];
  51. };
  52. struct clmInstance {
  53. int fd;
  54. struct queue inq;
  55. SaClmCallbacksT callbacks;
  56. int finalize;
  57. pthread_mutex_t mutex;
  58. };
  59. static void clmHandleInstanceDestructor (void *);
  60. static struct saHandleDatabase clmHandleDatabase = {
  61. .handleCount = 0,
  62. .handles = 0,
  63. .mutex = PTHREAD_MUTEX_INITIALIZER,
  64. .handleInstanceDestructor = clmHandleInstanceDestructor
  65. };
  66. /*
  67. * Versions supported
  68. */
  69. static SaVersionT clmVersionsSupported[] = {
  70. { 'A', 1, 1 },
  71. { 'a', 1, 1 }
  72. };
  73. static struct saVersionDatabase clmVersionDatabase = {
  74. sizeof (clmVersionsSupported) / sizeof (SaVersionT),
  75. clmVersionsSupported
  76. };
  77. void clmHandleInstanceDestructor (void *instance)
  78. {
  79. struct clmInstance *clmInstance = (struct clmInstance *)instance;
  80. if (clmInstance->fd != -1) {
  81. shutdown (clmInstance->fd, 0);
  82. close (clmInstance->fd);
  83. }
  84. }
  85. SaErrorT
  86. saClmInitialize (
  87. SaClmHandleT *clmHandle,
  88. const SaClmCallbacksT *clmCallbacks,
  89. const SaVersionT *version)
  90. {
  91. struct clmInstance *clmInstance;
  92. SaErrorT error = SA_OK;
  93. error = saVersionVerify (&clmVersionDatabase, version);
  94. if (error != SA_OK) {
  95. goto error_no_destroy;
  96. }
  97. error = saHandleCreate (&clmHandleDatabase, sizeof (struct clmInstance),
  98. clmHandle);
  99. if (error != SA_OK) {
  100. goto error_no_destroy;
  101. }
  102. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle,
  103. (void *)&clmInstance);
  104. if (error != SA_OK) {
  105. goto error_destroy;
  106. }
  107. clmInstance->fd = -1;
  108. /*
  109. * An inq is needed to store async messages while waiting for a
  110. * sync response
  111. */
  112. error = saQueueInit (&clmInstance->inq, 512, sizeof (void *));
  113. if (error != SA_OK) {
  114. goto error_put_destroy;
  115. }
  116. error = saServiceConnect (&clmInstance->fd, MESSAGE_REQ_CLM_INIT);
  117. if (error != SA_OK) {
  118. goto error_put_destroy;
  119. }
  120. memcpy (&clmInstance->callbacks, clmCallbacks, sizeof (SaClmCallbacksT));
  121. pthread_mutex_init (&clmInstance->mutex, NULL);
  122. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  123. return (SA_OK);
  124. error_put_destroy:
  125. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  126. error_destroy:
  127. saHandleDestroy (&clmHandleDatabase, *clmHandle);
  128. error_no_destroy:
  129. return (error);
  130. }
  131. SaErrorT
  132. saClmSelectionObjectGet (
  133. const SaClmHandleT *clmHandle,
  134. SaSelectionObjectT *selectionObject)
  135. {
  136. struct clmInstance *clmInstance;
  137. SaErrorT error;
  138. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle, (void *)&clmInstance);
  139. if (error != SA_OK) {
  140. return (error);
  141. }
  142. *selectionObject = clmInstance->fd;
  143. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  144. return (SA_OK);
  145. }
  146. SaErrorT
  147. saClmDispatch (
  148. const SaClmHandleT *clmHandle,
  149. SaDispatchFlagsT dispatchFlags)
  150. {
  151. struct pollfd ufds;
  152. int timeout = -1;
  153. SaErrorT error;
  154. int cont = 1; /* always continue do loop except when set to 0 */
  155. int dispatch_avail;
  156. int poll_fd;
  157. struct clmInstance *clmInstance;
  158. struct res_clm_trackcallback *res_clm_trackcallback;
  159. struct res_clm_nodegetcallback *res_clm_nodegetcallback;
  160. SaClmCallbacksT callbacks;
  161. struct res_overlay dispatch_data;
  162. int empty;
  163. struct res_header **queue_msg;
  164. struct res_header *msg;
  165. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle, (void *)&clmInstance);
  166. if (error != SA_OK) {
  167. return (error);
  168. }
  169. /*
  170. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  171. * wait indefinately for SA_DISPATCH_BLOCKING
  172. */
  173. if (dispatchFlags == SA_DISPATCH_ALL) {
  174. timeout = 0;
  175. }
  176. do {
  177. poll_fd = clmInstance->fd;
  178. ufds.fd = poll_fd;
  179. ufds.events = POLLIN;
  180. ufds.revents = 0;
  181. error = saPollRetry (&ufds, 1, timeout);
  182. if (error != SA_OK) {
  183. goto error_nounlock;
  184. }
  185. pthread_mutex_lock (&clmInstance->mutex);
  186. /*
  187. * Handle has been finalized in another thread
  188. */
  189. if (clmInstance->finalize == 1) {
  190. error = SA_OK;
  191. pthread_mutex_unlock (&clmInstance->mutex);
  192. goto error_unlock;
  193. }
  194. dispatch_avail = ufds.revents & POLLIN;
  195. if (dispatch_avail == 0 && dispatchFlags == SA_DISPATCH_ALL) {
  196. pthread_mutex_unlock (&clmInstance->mutex);
  197. break; /* exit do while cont is 1 loop */
  198. } else
  199. if (dispatch_avail == 0) {
  200. pthread_mutex_unlock (&clmInstance->mutex);
  201. continue; /* next poll */
  202. }
  203. saQueueIsEmpty(&clmInstance->inq, &empty);
  204. if (empty == 0) {
  205. /*
  206. * Queue is not empty, read data from queue
  207. */
  208. saQueueItemGet (&clmInstance->inq, (void *)&queue_msg);
  209. msg = *queue_msg;
  210. memcpy (&dispatch_data, msg, msg->size);
  211. saQueueItemRemove (&clmInstance->inq);
  212. free (msg);
  213. } else {
  214. /*
  215. * Queue empty, read response from socket
  216. */
  217. error = saRecvRetry (clmInstance->fd, &dispatch_data.header,
  218. sizeof (struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  219. if (error != SA_OK) {
  220. goto error_unlock;
  221. }
  222. if (dispatch_data.header.size > sizeof (struct res_header)) {
  223. error = saRecvRetry (clmInstance->fd, &dispatch_data.data,
  224. dispatch_data.header.size - sizeof (struct res_header),
  225. MSG_WAITALL | MSG_NOSIGNAL);
  226. if (error != SA_OK) {
  227. goto error_unlock;
  228. }
  229. }
  230. }
  231. /*
  232. * Make copy of callbacks, message data, unlock instance, and call callback
  233. * A risk of this dispatch method is that the callback routines may
  234. * operate at the same time that clmFinalize has been called in another thread.
  235. */
  236. memcpy (&callbacks, &clmInstance->callbacks, sizeof (SaClmCallbacksT));
  237. pthread_mutex_unlock (&clmInstance->mutex);
  238. /*
  239. * Dispatch incoming message
  240. */
  241. switch (dispatch_data.header.id) {
  242. case MESSAGE_RES_CLM_TRACKCALLBACK:
  243. res_clm_trackcallback = (struct res_clm_trackcallback *)&dispatch_data;
  244. memcpy (res_clm_trackcallback->notificationBufferAddress,
  245. &res_clm_trackcallback->notificationBuffer,
  246. res_clm_trackcallback->numberOfItems * sizeof (SaClmClusterNotificationT));
  247. callbacks.saClmClusterTrackCallback (
  248. res_clm_trackcallback->notificationBufferAddress,
  249. res_clm_trackcallback->numberOfItems, res_clm_trackcallback->numberOfMembers,
  250. res_clm_trackcallback->viewNumber, SA_OK);
  251. break;
  252. case MESSAGE_RES_CLM_NODEGETCALLBACK:
  253. res_clm_nodegetcallback = (struct res_clm_nodegetcallback *)&dispatch_data;
  254. memcpy (res_clm_nodegetcallback->clusterNodeAddress,
  255. &res_clm_nodegetcallback->clusterNode,
  256. sizeof (SaClmClusterNodeT));
  257. callbacks.saClmClusterNodeGetCallback (
  258. res_clm_nodegetcallback->invocation,
  259. &res_clm_nodegetcallback->clusterNode, SA_OK);
  260. break;
  261. default:
  262. error = SA_ERR_LIBRARY;
  263. goto error_nounlock;
  264. break;
  265. }
  266. /*
  267. * Determine if more messages should be processed
  268. * */
  269. switch (dispatchFlags) {
  270. case SA_DISPATCH_ONE:
  271. cont = 0;
  272. break;
  273. case SA_DISPATCH_ALL:
  274. break;
  275. case SA_DISPATCH_BLOCKING:
  276. break;
  277. }
  278. } while (cont);
  279. error_unlock:
  280. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  281. error_nounlock:
  282. return (error);
  283. }
  284. SaErrorT
  285. saClmFinalize (
  286. SaClmHandleT *clmHandle)
  287. {
  288. struct clmInstance *clmInstance;
  289. SaErrorT error;
  290. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle, (void *)&clmInstance);
  291. if (error != SA_OK) {
  292. return (error);
  293. }
  294. pthread_mutex_lock (&clmInstance->mutex);
  295. /*
  296. * Another thread has already started finalizing
  297. */
  298. if (clmInstance->finalize) {
  299. pthread_mutex_unlock (&clmInstance->mutex);
  300. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  301. return (SA_ERR_BAD_HANDLE);
  302. }
  303. clmInstance->finalize = 1;
  304. saActivatePoll (clmInstance->fd);
  305. pthread_mutex_unlock (&clmInstance->mutex);
  306. saHandleDestroy (&clmHandleDatabase, *clmHandle);
  307. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  308. return (error);
  309. }
  310. SaErrorT
  311. saClmClusterTrackStart (
  312. const SaClmHandleT *clmHandle,
  313. SaUint8T trackFlags,
  314. SaClmClusterNotificationT *notificationBuffer,
  315. SaUint32T numberOfItems)
  316. {
  317. struct req_clm_trackstart req_trackstart;
  318. struct clmInstance *clmInstance;
  319. SaErrorT error = SA_OK;
  320. req_trackstart.header.size = sizeof (struct req_clm_trackstart);
  321. req_trackstart.header.id = MESSAGE_REQ_CLM_TRACKSTART;
  322. req_trackstart.trackFlags = trackFlags;
  323. req_trackstart.notificationBufferAddress = notificationBuffer;
  324. req_trackstart.numberOfItems = numberOfItems;
  325. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle, (void *)&clmInstance);
  326. if (error != SA_OK) {
  327. return (error);
  328. }
  329. pthread_mutex_lock (&clmInstance->mutex);
  330. error = saSendRetry (clmInstance->fd, &req_trackstart, sizeof (struct req_clm_trackstart), MSG_NOSIGNAL);
  331. pthread_mutex_unlock (&clmInstance->mutex);
  332. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  333. return (error);
  334. }
  335. SaErrorT
  336. saClmClusterTrackStop (
  337. const SaClmHandleT *clmHandle)
  338. {
  339. struct clmInstance *clmInstance;
  340. struct req_clm_trackstop req_trackstop;
  341. SaErrorT error = SA_OK;
  342. req_trackstop.header.size = sizeof (struct req_clm_trackstop);
  343. req_trackstop.header.id = MESSAGE_REQ_CLM_TRACKSTOP;
  344. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle, (void *)&clmInstance);
  345. if (error != SA_OK) {
  346. return (error);
  347. }
  348. pthread_mutex_lock (&clmInstance->mutex);
  349. error = saSendRetry (clmInstance->fd, &req_trackstop, sizeof (struct req_clm_trackstop), MSG_NOSIGNAL);
  350. pthread_mutex_unlock (&clmInstance->mutex);
  351. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  352. return (error);
  353. }
  354. SaErrorT
  355. saClmClusterNodeGet (
  356. SaClmNodeIdT nodeId,
  357. SaTimeT timeout,
  358. SaClmClusterNodeT *clusterNode)
  359. {
  360. int fd;
  361. struct req_clm_nodeget req_clm_nodeget;
  362. struct res_clm_nodeget res_clm_nodeget;
  363. SaErrorT error = SA_OK;
  364. struct timeval select_timeout;
  365. fd_set read_fds;
  366. select_timeout.tv_usec = 0;
  367. select_timeout.tv_sec = 5;
  368. error = saServiceConnect (&fd, MESSAGE_REQ_CLM_INIT);
  369. if (error != SA_OK) {
  370. goto error_noclose;
  371. }
  372. /*
  373. * Send request message
  374. */
  375. req_clm_nodeget.header.size = sizeof (struct req_clm_nodeget);
  376. req_clm_nodeget.header.id = MESSAGE_REQ_CLM_NODEGET;
  377. req_clm_nodeget.nodeId = nodeId;
  378. error = saSendRetry (fd, &req_clm_nodeget, sizeof (struct req_clm_nodeget), MSG_NOSIGNAL);
  379. if (error != SA_OK) {
  380. goto error_close;
  381. }
  382. FD_ZERO (&read_fds);
  383. FD_SET (fd, &read_fds);
  384. /*
  385. * Wait for timeout interval
  386. */
  387. error = saSelectRetry (fd + 1, &read_fds, 0, 0, &select_timeout);
  388. if (error != SA_OK) {
  389. goto error_close;
  390. }
  391. /*
  392. * Was there a timeout in receiving the information?
  393. */
  394. if (FD_ISSET (fd, &read_fds) == 0) {
  395. error = SA_ERR_TIMEOUT;
  396. goto error_close;
  397. }
  398. error = saRecvRetry (fd, &res_clm_nodeget, sizeof (struct res_clm_nodeget),
  399. MSG_WAITALL | MSG_NOSIGNAL);
  400. if (error != SA_OK) {
  401. goto error_close;
  402. }
  403. error = res_clm_nodeget.header.error;
  404. memcpy (clusterNode, &res_clm_nodeget.clusterNode, sizeof (SaClmClusterNodeT));
  405. error_noclose:
  406. return (error);
  407. error_close:
  408. close (fd);
  409. return (error);
  410. }
  411. SaErrorT
  412. saClmClusterNodeGetAsync (
  413. const SaClmHandleT *clmHandle,
  414. SaInvocationT invocation,
  415. SaClmNodeIdT nodeId,
  416. SaClmClusterNodeT *clusterNode)
  417. {
  418. struct clmInstance *clmInstance;
  419. struct req_clm_nodegetasync req_clm_nodegetasync;
  420. struct res_clm_nodegetasync res_clm_nodegetasync;
  421. SaErrorT error = SA_OK;
  422. req_clm_nodegetasync.header.size = sizeof (struct req_clm_nodeget);
  423. req_clm_nodegetasync.header.id = MESSAGE_REQ_CLM_NODEGETASYNC;
  424. memcpy (&req_clm_nodegetasync.invocation, &invocation, sizeof (SaInvocationT));
  425. memcpy (&req_clm_nodegetasync.nodeId, &nodeId, sizeof (SaClmNodeIdT));
  426. req_clm_nodegetasync.clusterNodeAddress = clusterNode;
  427. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle, (void *)&clmInstance);
  428. if (error != SA_OK) {
  429. return (error);
  430. }
  431. pthread_mutex_lock (&clmInstance->mutex);
  432. error = saSendRetry (clmInstance->fd, &req_clm_nodegetasync,
  433. sizeof (struct req_clm_nodegetasync), MSG_NOSIGNAL);
  434. pthread_mutex_unlock (&clmInstance->mutex);
  435. error = saRecvQueue (clmInstance->fd, &res_clm_nodegetasync,
  436. &clmInstance->inq, MESSAGE_RES_CLM_NODEGETASYNC);
  437. if (error != SA_OK) {
  438. goto error_exit;
  439. }
  440. error = res_clm_nodegetasync.header.error;
  441. error_exit:
  442. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  443. return (error);
  444. }