clm.c 13 KB

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