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