4
0

clm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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[128000];
  53. };
  54. struct clmInstance {
  55. int fd;
  56. struct queue inq;
  57. SaClmCallbacksT callbacks;
  58. int finalize;
  59. SaClmClusterNotificationBufferT notificationBuffer;
  60. pthread_mutex_t mutex;
  61. };
  62. static void clmHandleInstanceDestructor (void *);
  63. static struct saHandleDatabase clmHandleDatabase = {
  64. .handleCount = 0,
  65. .handles = 0,
  66. .mutex = PTHREAD_MUTEX_INITIALIZER,
  67. .handleInstanceDestructor = clmHandleInstanceDestructor
  68. };
  69. /*
  70. * Versions supported
  71. */
  72. static SaVersionT clmVersionsSupported[] = {
  73. { 'B', 1, 1 },
  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. struct clmInstance *clmInstance = (struct clmInstance *)instance;
  83. if (clmInstance->fd != -1) {
  84. shutdown (clmInstance->fd, 0);
  85. close (clmInstance->fd);
  86. }
  87. }
  88. SaAisErrorT
  89. saClmInitialize (
  90. SaClmHandleT *clmHandle,
  91. const SaClmCallbacksT *clmCallbacks,
  92. SaVersionT *version)
  93. {
  94. struct clmInstance *clmInstance;
  95. SaAisErrorT error = SA_OK;
  96. error = saVersionVerify (&clmVersionDatabase, version);
  97. if (error != SA_OK) {
  98. goto error_no_destroy;
  99. }
  100. error = saHandleCreate (&clmHandleDatabase, sizeof (struct clmInstance),
  101. clmHandle);
  102. if (error != SA_OK) {
  103. goto error_no_destroy;
  104. }
  105. error = saHandleInstanceGet (&clmHandleDatabase, *clmHandle,
  106. (void *)&clmInstance);
  107. if (error != SA_OK) {
  108. goto error_destroy;
  109. }
  110. clmInstance->fd = -1;
  111. /*
  112. * An inq is needed to store async messages while waiting for a
  113. * sync response
  114. */
  115. error = saQueueInit (&clmInstance->inq, 512, sizeof (void *));
  116. if (error != SA_OK) {
  117. goto error_put_destroy;
  118. }
  119. error = saServiceConnect (&clmInstance->fd, MESSAGE_REQ_CLM_INIT);
  120. if (error != SA_OK) {
  121. goto error_put_destroy;
  122. }
  123. memcpy (&clmInstance->callbacks, clmCallbacks, sizeof (SaClmCallbacksT));
  124. pthread_mutex_init (&clmInstance->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->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. int poll_fd;
  162. struct clmInstance *clmInstance;
  163. struct res_clm_trackcallback *res_clm_trackcallback;
  164. struct res_clm_nodegetcallback *res_clm_nodegetcallback;
  165. SaClmCallbacksT callbacks;
  166. struct res_overlay dispatch_data;
  167. int empty;
  168. struct res_header **queue_msg;
  169. struct res_header *msg;
  170. SaClmClusterNotificationBufferT notificationBuffer;
  171. int copy_items;
  172. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  173. (void *)&clmInstance);
  174. if (error != SA_OK) {
  175. return (error);
  176. }
  177. /*
  178. * Timeout instantly for SA_DISPATCH_ONE or SA_DISPATCH_ALL and
  179. * wait indefinately for SA_DISPATCH_BLOCKING
  180. */
  181. if (dispatchFlags == SA_DISPATCH_ALL) {
  182. timeout = 0;
  183. }
  184. do {
  185. poll_fd = clmInstance->fd;
  186. ufds.fd = poll_fd;
  187. ufds.events = POLLIN;
  188. ufds.revents = 0;
  189. error = saPollRetry (&ufds, 1, timeout);
  190. if (error != SA_OK) {
  191. goto error_nounlock;
  192. }
  193. pthread_mutex_lock (&clmInstance->mutex);
  194. /*
  195. * Handle has been finalized in another thread
  196. */
  197. if (clmInstance->finalize == 1) {
  198. error = SA_OK;
  199. pthread_mutex_unlock (&clmInstance->mutex);
  200. goto error_unlock;
  201. }
  202. dispatch_avail = ufds.revents & POLLIN;
  203. if (dispatch_avail == 0 && dispatchFlags == SA_DISPATCH_ALL) {
  204. pthread_mutex_unlock (&clmInstance->mutex);
  205. break; /* exit do while cont is 1 loop */
  206. } else
  207. if (dispatch_avail == 0) {
  208. pthread_mutex_unlock (&clmInstance->mutex);
  209. continue; /* next poll */
  210. }
  211. saQueueIsEmpty(&clmInstance->inq, &empty);
  212. if (empty == 0) {
  213. /*
  214. * Queue is not empty, read data from queue
  215. */
  216. saQueueItemGet (&clmInstance->inq, (void *)&queue_msg);
  217. msg = *queue_msg;
  218. memcpy (&dispatch_data, msg, msg->size);
  219. saQueueItemRemove (&clmInstance->inq);
  220. free (msg);
  221. } else {
  222. /*
  223. * Queue empty, read response from socket
  224. */
  225. error = saRecvRetry (clmInstance->fd, &dispatch_data.header,
  226. sizeof (struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  227. if (error != SA_OK) {
  228. goto error_unlock;
  229. }
  230. if (dispatch_data.header.size > sizeof (struct res_header)) {
  231. error = saRecvRetry (clmInstance->fd, &dispatch_data.data,
  232. dispatch_data.header.size - sizeof (struct res_header),
  233. MSG_WAITALL | MSG_NOSIGNAL);
  234. if (error != SA_OK) {
  235. goto error_unlock;
  236. }
  237. }
  238. }
  239. /*
  240. * Make copy of callbacks, message data, unlock instance, and call callback
  241. * A risk of this dispatch method is that the callback routines may
  242. * operate at the same time that clmFinalize has been called in another thread.
  243. */
  244. memcpy (&callbacks, &clmInstance->callbacks, sizeof (SaClmCallbacksT));
  245. memcpy (&notificationBuffer, &clmInstance->notificationBuffer,
  246. sizeof (SaClmClusterNotificationBufferT));
  247. pthread_mutex_unlock (&clmInstance->mutex);
  248. /*
  249. * Dispatch incoming message
  250. */
  251. switch (dispatch_data.header.id) {
  252. case MESSAGE_RES_CLM_TRACKCALLBACK:
  253. res_clm_trackcallback = (struct res_clm_trackcallback *)&dispatch_data;
  254. error = SA_AIS_OK;
  255. /*
  256. * If buffer is not specified, allocate one
  257. */
  258. if (notificationBuffer.notification == 0) {
  259. notificationBuffer.notification = malloc (
  260. res_clm_trackcallback->numberOfItems *
  261. sizeof (SaClmClusterNotificationT));
  262. if (notificationBuffer.notification) {
  263. notificationBuffer.numberOfItems =
  264. res_clm_trackcallback->numberOfItems;
  265. } else {
  266. error = SA_AIS_ERR_NO_MEMORY;
  267. }
  268. }
  269. copy_items = res_clm_trackcallback->numberOfItems;
  270. if (copy_items > notificationBuffer.numberOfItems) {
  271. copy_items = notificationBuffer.numberOfItems;
  272. error = SA_AIS_ERR_NO_SPACE;
  273. }
  274. memcpy (notificationBuffer.notification,
  275. &res_clm_trackcallback->notification,
  276. copy_items *
  277. sizeof (SaClmClusterNotificationT));
  278. callbacks.saClmClusterTrackCallback (
  279. (const SaClmClusterNotificationBufferT *)&notificationBuffer,
  280. res_clm_trackcallback->numberOfMembers, error);
  281. break;
  282. case MESSAGE_RES_CLM_NODEGETCALLBACK:
  283. res_clm_nodegetcallback = (struct res_clm_nodegetcallback *)&dispatch_data;
  284. callbacks.saClmClusterNodeGetCallback (
  285. res_clm_nodegetcallback->invocation,
  286. &res_clm_nodegetcallback->clusterNode,
  287. res_clm_nodegetcallback->header.error);
  288. break;
  289. default:
  290. error = SA_ERR_LIBRARY;
  291. goto error_nounlock;
  292. break;
  293. }
  294. /*
  295. * Determine if more messages should be processed
  296. * */
  297. switch (dispatchFlags) {
  298. case SA_DISPATCH_ONE:
  299. cont = 0;
  300. break;
  301. case SA_DISPATCH_ALL:
  302. break;
  303. case SA_DISPATCH_BLOCKING:
  304. break;
  305. }
  306. } while (cont);
  307. error_unlock:
  308. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  309. error_nounlock:
  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->mutex);
  324. /*
  325. * Another thread has already started finalizing
  326. */
  327. if (clmInstance->finalize) {
  328. pthread_mutex_unlock (&clmInstance->mutex);
  329. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  330. return (SA_ERR_BAD_HANDLE);
  331. }
  332. clmInstance->finalize = 1;
  333. saActivatePoll (clmInstance->fd);
  334. pthread_mutex_unlock (&clmInstance->mutex);
  335. saHandleDestroy (&clmHandleDatabase, clmHandle);
  336. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  337. return (error);
  338. }
  339. SaAisErrorT
  340. saClmClusterTrack (
  341. SaClmHandleT clmHandle,
  342. SaUint8T trackFlags,
  343. SaClmClusterNotificationBufferT *notificationBuffer)
  344. {
  345. struct req_clm_clustertrack req_clustertrack;
  346. struct clmInstance *clmInstance;
  347. SaAisErrorT error = SA_OK;
  348. /*
  349. * Parameter checking
  350. */
  351. if (notificationBuffer == 0) {
  352. return (SA_AIS_ERR_INVALID_PARAM);
  353. }
  354. if (notificationBuffer->notification &&
  355. notificationBuffer->numberOfItems == 0) {
  356. return (SA_AIS_ERR_INVALID_PARAM);
  357. }
  358. if ((trackFlags & SA_TRACK_CHANGES) && (trackFlags & SA_TRACK_CHANGES_ONLY)) {
  359. return (SA_AIS_ERR_BAD_FLAGS);
  360. }
  361. /*
  362. * Request service
  363. */
  364. req_clustertrack.header.size = sizeof (struct req_clm_clustertrack);
  365. req_clustertrack.header.id = MESSAGE_REQ_CLM_TRACKSTART;
  366. req_clustertrack.trackFlags = trackFlags;
  367. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  368. (void *)&clmInstance);
  369. if (error != SA_OK) {
  370. return (error);
  371. }
  372. pthread_mutex_lock (&clmInstance->mutex);
  373. if (clmInstance->callbacks.saClmClusterTrackCallback == 0) {
  374. error = SA_AIS_ERR_INIT;
  375. goto error_exit;
  376. }
  377. error = saSendRetry (clmInstance->fd, &req_clustertrack,
  378. sizeof (struct req_clm_clustertrack), MSG_NOSIGNAL);
  379. memcpy (&clmInstance->notificationBuffer, notificationBuffer,
  380. sizeof (SaClmClusterNotificationBufferT));
  381. error_exit:
  382. pthread_mutex_unlock (&clmInstance->mutex);
  383. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  384. return (error);
  385. }
  386. SaAisErrorT
  387. saClmClusterTrackStop (
  388. SaClmHandleT clmHandle)
  389. {
  390. struct clmInstance *clmInstance;
  391. struct req_clm_trackstop req_trackstop;
  392. SaAisErrorT error = SA_OK;
  393. req_trackstop.header.size = sizeof (struct req_clm_trackstop);
  394. req_trackstop.header.id = MESSAGE_REQ_CLM_TRACKSTOP;
  395. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  396. (void *)&clmInstance);
  397. if (error != SA_OK) {
  398. return (error);
  399. }
  400. pthread_mutex_lock (&clmInstance->mutex);
  401. error = saSendRetry (clmInstance->fd, &req_trackstop,
  402. sizeof (struct req_clm_trackstop), MSG_NOSIGNAL);
  403. clmInstance->notificationBuffer.notification = 0;
  404. pthread_mutex_unlock (&clmInstance->mutex);
  405. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  406. return (error);
  407. }
  408. SaAisErrorT
  409. saClmClusterNodeGet (
  410. SaClmHandleT clmHandle,
  411. SaClmNodeIdT nodeId,
  412. SaTimeT timeout,
  413. SaClmClusterNodeT *clusterNode)
  414. {
  415. struct clmInstance *clmInstance;
  416. struct req_clm_nodeget req_clm_nodeget;
  417. struct res_clm_nodeget res_clm_nodeget;
  418. SaAisErrorT error = SA_OK;
  419. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  420. (void *)&clmInstance);
  421. if (error != SA_OK) {
  422. return (error);
  423. }
  424. pthread_mutex_lock (&clmInstance->mutex);
  425. /*
  426. * Send request message
  427. */
  428. req_clm_nodeget.header.size = sizeof (struct req_clm_nodeget);
  429. req_clm_nodeget.header.id = MESSAGE_REQ_CLM_NODEGET;
  430. req_clm_nodeget.nodeId = nodeId;
  431. error = saSendRetry (clmInstance->fd, &req_clm_nodeget,
  432. sizeof (struct req_clm_nodeget), MSG_NOSIGNAL);
  433. if (error != SA_OK) {
  434. goto error_exit;
  435. }
  436. error = saRecvRetry (clmInstance->fd, &res_clm_nodeget,
  437. sizeof (struct res_clm_nodeget),
  438. MSG_WAITALL | MSG_NOSIGNAL);
  439. if (error != SA_OK) {
  440. goto error_exit;
  441. }
  442. error = res_clm_nodeget.header.error;
  443. memcpy (clusterNode, &res_clm_nodeget.clusterNode,
  444. sizeof (SaClmClusterNodeT));
  445. error_exit:
  446. pthread_mutex_unlock (&clmInstance->mutex);
  447. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  448. return (error);
  449. }
  450. SaAisErrorT
  451. saClmClusterNodeGetAsync (
  452. SaClmHandleT clmHandle,
  453. SaInvocationT invocation,
  454. SaClmNodeIdT nodeId)
  455. {
  456. struct clmInstance *clmInstance;
  457. struct req_clm_nodegetasync req_clm_nodegetasync;
  458. struct res_clm_nodegetasync res_clm_nodegetasync;
  459. SaAisErrorT error = SA_OK;
  460. req_clm_nodegetasync.header.size = sizeof (struct req_clm_nodeget);
  461. req_clm_nodegetasync.header.id = MESSAGE_REQ_CLM_NODEGETASYNC;
  462. memcpy (&req_clm_nodegetasync.invocation, &invocation,
  463. sizeof (SaInvocationT));
  464. memcpy (&req_clm_nodegetasync.nodeId, &nodeId, sizeof (SaClmNodeIdT));
  465. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  466. (void *)&clmInstance);
  467. if (error != SA_OK) {
  468. return (error);
  469. }
  470. pthread_mutex_lock (&clmInstance->mutex);
  471. error = saSendRetry (clmInstance->fd, &req_clm_nodegetasync,
  472. sizeof (struct req_clm_nodegetasync), MSG_NOSIGNAL);
  473. pthread_mutex_unlock (&clmInstance->mutex);
  474. error = saRecvQueue (clmInstance->fd, &res_clm_nodegetasync,
  475. &clmInstance->inq, MESSAGE_RES_CLM_NODEGETASYNC);
  476. if (error != SA_OK) {
  477. goto error_exit;
  478. }
  479. error = res_clm_nodegetasync.header.error;
  480. error_exit:
  481. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  482. return (error);
  483. }