clm.c 13 KB

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