clm.c 15 KB

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