clm.c 15 KB

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