clm.c 15 KB

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