clm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <assert.h>
  39. #include <errno.h>
  40. #include <pthread.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include "../include/ais_types.h"
  46. #include "../include/saClm.h"
  47. #include "../include/ais_msg.h"
  48. #include "../include/ipc_clm.h"
  49. #include "util.h"
  50. struct res_overlay {
  51. struct res_header header;
  52. char data[512000];
  53. };
  54. struct clmInstance {
  55. int response_fd;
  56. int dispatch_fd;
  57. SaClmCallbacksT callbacks;
  58. int finalize;
  59. SaClmClusterNotificationBufferT notificationBuffer;
  60. pthread_mutex_t response_mutex;
  61. pthread_mutex_t dispatch_mutex;
  62. };
  63. static void clmHandleInstanceDestructor (void *);
  64. static struct saHandleDatabase clmHandleDatabase = {
  65. .handleCount = 0,
  66. .handles = 0,
  67. .mutex = PTHREAD_MUTEX_INITIALIZER,
  68. .handleInstanceDestructor = clmHandleInstanceDestructor
  69. };
  70. /*
  71. * Versions supported
  72. */
  73. static SaVersionT clmVersionsSupported[] = {
  74. { 'B', 1, 1 },
  75. { 'b', 1, 1 }
  76. };
  77. static struct saVersionDatabase clmVersionDatabase = {
  78. sizeof (clmVersionsSupported) / sizeof (SaVersionT),
  79. clmVersionsSupported
  80. };
  81. void clmHandleInstanceDestructor (void *instance)
  82. {
  83. }
  84. SaAisErrorT
  85. saClmInitialize (
  86. SaClmHandleT *clmHandle,
  87. const SaClmCallbacksT *clmCallbacks,
  88. SaVersionT *version)
  89. {
  90. struct clmInstance *clmInstance;
  91. SaAisErrorT 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->response_fd = -1;
  107. clmInstance->dispatch_fd = -1;
  108. error = saServiceConnectTwo (&clmInstance->response_fd,
  109. &clmInstance->dispatch_fd, CLM_SERVICE);
  110. if (error != SA_OK) {
  111. goto error_put_destroy;
  112. }
  113. memcpy (&clmInstance->callbacks, clmCallbacks, sizeof (SaClmCallbacksT));
  114. pthread_mutex_init (&clmInstance->response_mutex, NULL);
  115. pthread_mutex_init (&clmInstance->dispatch_mutex, NULL);
  116. clmInstance->notificationBuffer.notification = 0;
  117. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  118. return (SA_OK);
  119. error_put_destroy:
  120. saHandleInstancePut (&clmHandleDatabase, *clmHandle);
  121. error_destroy:
  122. saHandleDestroy (&clmHandleDatabase, *clmHandle);
  123. error_no_destroy:
  124. return (error);
  125. }
  126. SaAisErrorT
  127. saClmSelectionObjectGet (
  128. SaClmHandleT clmHandle,
  129. SaSelectionObjectT *selectionObject)
  130. {
  131. struct clmInstance *clmInstance;
  132. SaAisErrorT error;
  133. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  134. (void *)&clmInstance);
  135. if (error != SA_OK) {
  136. return (error);
  137. }
  138. *selectionObject = clmInstance->dispatch_fd;
  139. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  140. return (SA_OK);
  141. }
  142. SaAisErrorT
  143. saClmDispatch (
  144. SaClmHandleT clmHandle,
  145. SaDispatchFlagsT dispatchFlags)
  146. {
  147. struct pollfd ufds;
  148. int timeout = -1;
  149. SaAisErrorT error;
  150. int cont = 1; /* always continue do loop except when set to 0 */
  151. int dispatch_avail;
  152. struct clmInstance *clmInstance;
  153. struct res_clm_trackcallback *res_clm_trackcallback;
  154. struct res_clm_nodegetcallback *res_clm_nodegetcallback;
  155. SaClmCallbacksT callbacks;
  156. struct res_overlay dispatch_data;
  157. SaClmClusterNotificationBufferT notificationBuffer;
  158. int copy_items;
  159. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  160. (void *)&clmInstance);
  161. if (error != SA_OK) {
  162. return (error);
  163. }
  164. /*
  165. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  166. * wait indefinately for SA_DISPATCH_BLOCKING
  167. */
  168. if (dispatchFlags == SA_DISPATCH_ALL) {
  169. timeout = 0;
  170. }
  171. do {
  172. ufds.fd = clmInstance->dispatch_fd;
  173. ufds.events = POLLIN;
  174. ufds.revents = 0;
  175. pthread_mutex_lock (&clmInstance->dispatch_mutex);
  176. error = saPollRetry (&ufds, 1, timeout);
  177. if (error != SA_OK) {
  178. goto error_unlock;
  179. }
  180. /*
  181. * Handle has been finalized in another thread
  182. */
  183. if (clmInstance->finalize == 1) {
  184. error = SA_OK;
  185. goto error_unlock;
  186. }
  187. dispatch_avail = ufds.revents & POLLIN;
  188. if (dispatch_avail == 0 && dispatchFlags == SA_DISPATCH_ALL) {
  189. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  190. break; /* exit do while cont is 1 loop */
  191. } else
  192. if (dispatch_avail == 0) {
  193. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  194. continue; /* next poll */
  195. }
  196. if (ufds.revents & POLLIN) {
  197. error = saRecvRetry (clmInstance->dispatch_fd, &dispatch_data.header,
  198. sizeof (struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  199. if (error != SA_OK) {
  200. goto error_unlock;
  201. }
  202. if (dispatch_data.header.size > sizeof (struct res_header)) {
  203. error = saRecvRetry (clmInstance->dispatch_fd, &dispatch_data.data,
  204. dispatch_data.header.size - sizeof (struct res_header),
  205. MSG_WAITALL | MSG_NOSIGNAL);
  206. if (error != SA_OK) {
  207. goto error_unlock;
  208. }
  209. }
  210. } else {
  211. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  212. continue;
  213. }
  214. /*
  215. * Make copy of callbacks, message data, unlock instance, and call callback
  216. * A risk of this dispatch method is that the callback routines may
  217. * operate at the same time that clmFinalize has been called in another thread.
  218. */
  219. memcpy (&callbacks, &clmInstance->callbacks, sizeof (SaClmCallbacksT));
  220. memcpy (&notificationBuffer, &clmInstance->notificationBuffer,
  221. sizeof (SaClmClusterNotificationBufferT));
  222. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  223. /*
  224. * Dispatch incoming message
  225. */
  226. switch (dispatch_data.header.id) {
  227. case MESSAGE_RES_CLM_TRACKCALLBACK:
  228. res_clm_trackcallback = (struct res_clm_trackcallback *)&dispatch_data;
  229. error = SA_AIS_OK;
  230. /*
  231. * If buffer is not specified, allocate one
  232. */
  233. if (notificationBuffer.notification == 0) {
  234. notificationBuffer.notification = malloc (
  235. res_clm_trackcallback->numberOfItems *
  236. sizeof (SaClmClusterNotificationT));
  237. if (notificationBuffer.notification) {
  238. notificationBuffer.numberOfItems =
  239. res_clm_trackcallback->numberOfItems;
  240. } else {
  241. error = SA_AIS_ERR_NO_MEMORY;
  242. }
  243. }
  244. copy_items = res_clm_trackcallback->numberOfItems;
  245. if (copy_items > notificationBuffer.numberOfItems) {
  246. copy_items = notificationBuffer.numberOfItems;
  247. error = SA_AIS_ERR_NO_SPACE;
  248. }
  249. memcpy (notificationBuffer.notification,
  250. &res_clm_trackcallback->notification,
  251. copy_items *
  252. sizeof (SaClmClusterNotificationT));
  253. callbacks.saClmClusterTrackCallback (
  254. (const SaClmClusterNotificationBufferT *)&notificationBuffer,
  255. res_clm_trackcallback->numberOfMembers, error);
  256. break;
  257. case MESSAGE_RES_CLM_NODEGETCALLBACK:
  258. res_clm_nodegetcallback = (struct res_clm_nodegetcallback *)&dispatch_data;
  259. callbacks.saClmClusterNodeGetCallback (
  260. res_clm_nodegetcallback->invocation,
  261. &res_clm_nodegetcallback->clusterNode,
  262. res_clm_nodegetcallback->header.error);
  263. break;
  264. default:
  265. error = SA_ERR_LIBRARY;
  266. goto error_put;
  267. break;
  268. }
  269. /*
  270. * Determine if more messages should be processed
  271. * */
  272. switch (dispatchFlags) {
  273. case SA_DISPATCH_ONE:
  274. cont = 0;
  275. break;
  276. case SA_DISPATCH_ALL:
  277. break;
  278. case SA_DISPATCH_BLOCKING:
  279. break;
  280. }
  281. } while (cont);
  282. goto error_put;
  283. error_unlock:
  284. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  285. error_put:
  286. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  287. return (error);
  288. }
  289. SaAisErrorT
  290. saClmFinalize (
  291. SaClmHandleT clmHandle)
  292. {
  293. struct clmInstance *clmInstance;
  294. SaAisErrorT error;
  295. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  296. (void *)&clmInstance);
  297. if (error != SA_OK) {
  298. return (error);
  299. }
  300. pthread_mutex_lock (&clmInstance->dispatch_mutex);
  301. pthread_mutex_lock (&clmInstance->response_mutex);
  302. /*
  303. * Another thread has already started finalizing
  304. */
  305. if (clmInstance->finalize) {
  306. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  307. pthread_mutex_unlock (&clmInstance->response_mutex);
  308. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  309. return (SA_ERR_BAD_HANDLE);
  310. }
  311. clmInstance->finalize = 1;
  312. pthread_mutex_unlock (&clmInstance->dispatch_mutex);
  313. pthread_mutex_unlock (&clmInstance->response_mutex);
  314. saHandleDestroy (&clmHandleDatabase, clmHandle);
  315. if (clmInstance->response_fd != -1) {
  316. shutdown (clmInstance->response_fd, 0);
  317. close (clmInstance->response_fd);
  318. }
  319. if (clmInstance->dispatch_fd != -1) {
  320. shutdown (clmInstance->dispatch_fd, 0);
  321. close (clmInstance->dispatch_fd);
  322. }
  323. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  324. return (error);
  325. }
  326. SaAisErrorT
  327. saClmClusterTrack (
  328. SaClmHandleT clmHandle,
  329. SaUint8T trackFlags,
  330. SaClmClusterNotificationBufferT *notificationBuffer)
  331. {
  332. struct req_clm_clustertrack req_clustertrack;
  333. struct clmInstance *clmInstance;
  334. SaAisErrorT error = SA_OK;
  335. /*
  336. * Parameter checking
  337. */
  338. if (notificationBuffer == 0) {
  339. return (SA_AIS_ERR_INVALID_PARAM);
  340. }
  341. if (notificationBuffer->notification &&
  342. notificationBuffer->numberOfItems == 0) {
  343. return (SA_AIS_ERR_INVALID_PARAM);
  344. }
  345. if ((trackFlags & SA_TRACK_CHANGES) && (trackFlags & SA_TRACK_CHANGES_ONLY)) {
  346. return (SA_AIS_ERR_BAD_FLAGS);
  347. }
  348. /*
  349. * Request service
  350. */
  351. req_clustertrack.header.size = sizeof (struct req_clm_clustertrack);
  352. req_clustertrack.header.id = MESSAGE_REQ_CLM_TRACKSTART;
  353. req_clustertrack.trackFlags = trackFlags;
  354. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  355. (void *)&clmInstance);
  356. if (error != SA_OK) {
  357. return (error);
  358. }
  359. pthread_mutex_lock (&clmInstance->response_mutex);
  360. if (clmInstance->callbacks.saClmClusterTrackCallback == 0) {
  361. error = SA_AIS_ERR_INIT;
  362. goto error_exit;
  363. }
  364. error = saSendRetry (clmInstance->response_fd, &req_clustertrack,
  365. sizeof (struct req_clm_clustertrack), MSG_NOSIGNAL);
  366. memcpy (&clmInstance->notificationBuffer, notificationBuffer,
  367. sizeof (SaClmClusterNotificationBufferT));
  368. // TODO get response packet with saRecvRetry, but need to implement that
  369. // in executive service
  370. error_exit:
  371. pthread_mutex_unlock (&clmInstance->response_mutex);
  372. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  373. return (error);
  374. }
  375. SaAisErrorT
  376. saClmClusterTrackStop (
  377. SaClmHandleT clmHandle)
  378. {
  379. struct clmInstance *clmInstance;
  380. struct req_clm_trackstop req_trackstop;
  381. SaAisErrorT error = SA_OK;
  382. req_trackstop.header.size = sizeof (struct req_clm_trackstop);
  383. req_trackstop.header.id = MESSAGE_REQ_CLM_TRACKSTOP;
  384. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  385. (void *)&clmInstance);
  386. if (error != SA_OK) {
  387. return (error);
  388. }
  389. pthread_mutex_lock (&clmInstance->response_mutex);
  390. error = saSendRetry (clmInstance->response_fd, &req_trackstop,
  391. sizeof (struct req_clm_trackstop), MSG_NOSIGNAL);
  392. clmInstance->notificationBuffer.notification = 0;
  393. pthread_mutex_unlock (&clmInstance->response_mutex);
  394. // TODO what about getting response from executive? The
  395. // executive should send a response
  396. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  397. return (error);
  398. }
  399. SaAisErrorT
  400. saClmClusterNodeGet (
  401. SaClmHandleT clmHandle,
  402. SaClmNodeIdT nodeId,
  403. SaTimeT timeout,
  404. SaClmClusterNodeT *clusterNode)
  405. {
  406. struct clmInstance *clmInstance;
  407. struct req_clm_nodeget req_clm_nodeget;
  408. struct res_clm_nodeget res_clm_nodeget;
  409. SaAisErrorT error = SA_OK;
  410. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  411. (void *)&clmInstance);
  412. if (error != SA_OK) {
  413. return (error);
  414. }
  415. pthread_mutex_lock (&clmInstance->response_mutex);
  416. /*
  417. * Send request message
  418. */
  419. req_clm_nodeget.header.size = sizeof (struct req_clm_nodeget);
  420. req_clm_nodeget.header.id = MESSAGE_REQ_CLM_NODEGET;
  421. req_clm_nodeget.nodeId = nodeId;
  422. error = saSendReceiveReply (clmInstance->response_fd, &req_clm_nodeget,
  423. sizeof (struct req_clm_nodeget), &res_clm_nodeget, sizeof (res_clm_nodeget));
  424. if (error != SA_OK) {
  425. goto error_exit;
  426. }
  427. error = res_clm_nodeget.header.error;
  428. memcpy (clusterNode, &res_clm_nodeget.clusterNode,
  429. sizeof (SaClmClusterNodeT));
  430. error_exit:
  431. pthread_mutex_unlock (&clmInstance->response_mutex);
  432. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  433. return (error);
  434. }
  435. SaAisErrorT
  436. saClmClusterNodeGetAsync (
  437. SaClmHandleT clmHandle,
  438. SaInvocationT invocation,
  439. SaClmNodeIdT nodeId)
  440. {
  441. struct clmInstance *clmInstance;
  442. struct req_clm_nodegetasync req_clm_nodegetasync;
  443. struct res_clm_nodegetasync res_clm_nodegetasync;
  444. SaAisErrorT error = SA_OK;
  445. req_clm_nodegetasync.header.size = sizeof (struct req_clm_nodegetasync);
  446. req_clm_nodegetasync.header.id = MESSAGE_REQ_CLM_NODEGETASYNC;
  447. memcpy (&req_clm_nodegetasync.invocation, &invocation,
  448. sizeof (SaInvocationT));
  449. memcpy (&req_clm_nodegetasync.nodeId, &nodeId, sizeof (SaClmNodeIdT));
  450. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  451. (void *)&clmInstance);
  452. if (error != SA_OK) {
  453. return (error);
  454. }
  455. pthread_mutex_lock (&clmInstance->response_mutex);
  456. error = saSendReceiveReply (clmInstance->response_fd, &req_clm_nodegetasync,
  457. sizeof (struct req_clm_nodegetasync),
  458. &res_clm_nodegetasync, sizeof (struct res_clm_nodegetasync));
  459. if (error != SA_OK) {
  460. goto error_exit;
  461. }
  462. error = res_clm_nodegetasync.header.error;
  463. error_exit:
  464. pthread_mutex_unlock (&clmInstance->response_mutex);
  465. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  466. return (error);
  467. }