clm.c 13 KB

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