clm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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_put;
  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. goto error_unlock;
  200. }
  201. dispatch_avail = ufds.revents & POLLIN;
  202. if (dispatch_avail == 0 && dispatchFlags == SA_DISPATCH_ALL) {
  203. pthread_mutex_unlock (&clmInstance->mutex);
  204. break; /* exit do while cont is 1 loop */
  205. } else
  206. if (dispatch_avail == 0) {
  207. pthread_mutex_unlock (&clmInstance->mutex);
  208. continue; /* next poll */
  209. }
  210. saQueueIsEmpty(&clmInstance->inq, &empty);
  211. if (empty == 0) {
  212. /*
  213. * Queue is not empty, read data from queue
  214. */
  215. saQueueItemGet (&clmInstance->inq, (void *)&queue_msg);
  216. msg = *queue_msg;
  217. memcpy (&dispatch_data, msg, msg->size);
  218. saQueueItemRemove (&clmInstance->inq);
  219. free (msg);
  220. } else {
  221. /*
  222. * Queue empty, read response from socket
  223. */
  224. error = saRecvRetry (clmInstance->fd, &dispatch_data.header,
  225. sizeof (struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  226. if (error != SA_OK) {
  227. goto error_unlock;
  228. }
  229. if (dispatch_data.header.size > sizeof (struct res_header)) {
  230. error = saRecvRetry (clmInstance->fd, &dispatch_data.data,
  231. dispatch_data.header.size - sizeof (struct res_header),
  232. MSG_WAITALL | MSG_NOSIGNAL);
  233. if (error != SA_OK) {
  234. goto error_unlock;
  235. }
  236. }
  237. }
  238. /*
  239. * Make copy of callbacks, message data, unlock instance, and call callback
  240. * A risk of this dispatch method is that the callback routines may
  241. * operate at the same time that clmFinalize has been called in another thread.
  242. */
  243. memcpy (&callbacks, &clmInstance->callbacks, sizeof (SaClmCallbacksT));
  244. memcpy (&notificationBuffer, &clmInstance->notificationBuffer,
  245. sizeof (SaClmClusterNotificationBufferT));
  246. pthread_mutex_unlock (&clmInstance->mutex);
  247. /*
  248. * Dispatch incoming message
  249. */
  250. switch (dispatch_data.header.id) {
  251. case MESSAGE_RES_CLM_TRACKCALLBACK:
  252. res_clm_trackcallback = (struct res_clm_trackcallback *)&dispatch_data;
  253. error = SA_AIS_OK;
  254. /*
  255. * If buffer is not specified, allocate one
  256. */
  257. if (notificationBuffer.notification == 0) {
  258. notificationBuffer.notification = malloc (
  259. res_clm_trackcallback->numberOfItems *
  260. sizeof (SaClmClusterNotificationT));
  261. if (notificationBuffer.notification) {
  262. notificationBuffer.numberOfItems =
  263. res_clm_trackcallback->numberOfItems;
  264. } else {
  265. error = SA_AIS_ERR_NO_MEMORY;
  266. }
  267. }
  268. copy_items = res_clm_trackcallback->numberOfItems;
  269. if (copy_items > notificationBuffer.numberOfItems) {
  270. copy_items = notificationBuffer.numberOfItems;
  271. error = SA_AIS_ERR_NO_SPACE;
  272. }
  273. memcpy (notificationBuffer.notification,
  274. &res_clm_trackcallback->notification,
  275. copy_items *
  276. sizeof (SaClmClusterNotificationT));
  277. callbacks.saClmClusterTrackCallback (
  278. (const SaClmClusterNotificationBufferT *)&notificationBuffer,
  279. res_clm_trackcallback->numberOfMembers, error);
  280. break;
  281. case MESSAGE_RES_CLM_NODEGETCALLBACK:
  282. res_clm_nodegetcallback = (struct res_clm_nodegetcallback *)&dispatch_data;
  283. callbacks.saClmClusterNodeGetCallback (
  284. res_clm_nodegetcallback->invocation,
  285. &res_clm_nodegetcallback->clusterNode,
  286. res_clm_nodegetcallback->header.error);
  287. break;
  288. default:
  289. error = SA_ERR_LIBRARY;
  290. goto error_put;
  291. break;
  292. }
  293. /*
  294. * Determine if more messages should be processed
  295. * */
  296. switch (dispatchFlags) {
  297. case SA_DISPATCH_ONE:
  298. cont = 0;
  299. break;
  300. case SA_DISPATCH_ALL:
  301. break;
  302. case SA_DISPATCH_BLOCKING:
  303. break;
  304. }
  305. } while (cont);
  306. error_unlock:
  307. pthread_mutex_unlock (&clmInstance->mutex);
  308. error_put:
  309. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  310. error_nounlock:
  311. return (error);
  312. }
  313. SaAisErrorT
  314. saClmFinalize (
  315. SaClmHandleT clmHandle)
  316. {
  317. struct clmInstance *clmInstance;
  318. SaAisErrorT error;
  319. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  320. (void *)&clmInstance);
  321. if (error != SA_OK) {
  322. return (error);
  323. }
  324. pthread_mutex_lock (&clmInstance->mutex);
  325. /*
  326. * Another thread has already started finalizing
  327. */
  328. if (clmInstance->finalize) {
  329. pthread_mutex_unlock (&clmInstance->mutex);
  330. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  331. return (SA_ERR_BAD_HANDLE);
  332. }
  333. clmInstance->finalize = 1;
  334. saActivatePoll (clmInstance->fd);
  335. pthread_mutex_unlock (&clmInstance->mutex);
  336. saHandleDestroy (&clmHandleDatabase, clmHandle);
  337. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  338. return (error);
  339. }
  340. SaAisErrorT
  341. saClmClusterTrack (
  342. SaClmHandleT clmHandle,
  343. SaUint8T trackFlags,
  344. SaClmClusterNotificationBufferT *notificationBuffer)
  345. {
  346. struct req_clm_clustertrack req_clustertrack;
  347. struct clmInstance *clmInstance;
  348. SaAisErrorT error = SA_OK;
  349. /*
  350. * Parameter checking
  351. */
  352. if (notificationBuffer == 0) {
  353. return (SA_AIS_ERR_INVALID_PARAM);
  354. }
  355. if (notificationBuffer->notification &&
  356. notificationBuffer->numberOfItems == 0) {
  357. return (SA_AIS_ERR_INVALID_PARAM);
  358. }
  359. if ((trackFlags & SA_TRACK_CHANGES) && (trackFlags & SA_TRACK_CHANGES_ONLY)) {
  360. return (SA_AIS_ERR_BAD_FLAGS);
  361. }
  362. /*
  363. * Request service
  364. */
  365. req_clustertrack.header.size = sizeof (struct req_clm_clustertrack);
  366. req_clustertrack.header.id = MESSAGE_REQ_CLM_TRACKSTART;
  367. req_clustertrack.trackFlags = trackFlags;
  368. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  369. (void *)&clmInstance);
  370. if (error != SA_OK) {
  371. return (error);
  372. }
  373. pthread_mutex_lock (&clmInstance->mutex);
  374. if (clmInstance->callbacks.saClmClusterTrackCallback == 0) {
  375. error = SA_AIS_ERR_INIT;
  376. goto error_exit;
  377. }
  378. error = saSendRetry (clmInstance->fd, &req_clustertrack,
  379. sizeof (struct req_clm_clustertrack), MSG_NOSIGNAL);
  380. memcpy (&clmInstance->notificationBuffer, notificationBuffer,
  381. sizeof (SaClmClusterNotificationBufferT));
  382. error_exit:
  383. pthread_mutex_unlock (&clmInstance->mutex);
  384. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  385. return (error);
  386. }
  387. SaAisErrorT
  388. saClmClusterTrackStop (
  389. SaClmHandleT clmHandle)
  390. {
  391. struct clmInstance *clmInstance;
  392. struct req_clm_trackstop req_trackstop;
  393. SaAisErrorT error = SA_OK;
  394. req_trackstop.header.size = sizeof (struct req_clm_trackstop);
  395. req_trackstop.header.id = MESSAGE_REQ_CLM_TRACKSTOP;
  396. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  397. (void *)&clmInstance);
  398. if (error != SA_OK) {
  399. return (error);
  400. }
  401. pthread_mutex_lock (&clmInstance->mutex);
  402. error = saSendRetry (clmInstance->fd, &req_trackstop,
  403. sizeof (struct req_clm_trackstop), MSG_NOSIGNAL);
  404. clmInstance->notificationBuffer.notification = 0;
  405. pthread_mutex_unlock (&clmInstance->mutex);
  406. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  407. return (error);
  408. }
  409. SaAisErrorT
  410. saClmClusterNodeGet (
  411. SaClmHandleT clmHandle,
  412. SaClmNodeIdT nodeId,
  413. SaTimeT timeout,
  414. SaClmClusterNodeT *clusterNode)
  415. {
  416. struct clmInstance *clmInstance;
  417. struct req_clm_nodeget req_clm_nodeget;
  418. struct res_clm_nodeget res_clm_nodeget;
  419. SaAisErrorT error = SA_OK;
  420. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  421. (void *)&clmInstance);
  422. if (error != SA_OK) {
  423. return (error);
  424. }
  425. pthread_mutex_lock (&clmInstance->mutex);
  426. /*
  427. * Send request message
  428. */
  429. req_clm_nodeget.header.size = sizeof (struct req_clm_nodeget);
  430. req_clm_nodeget.header.id = MESSAGE_REQ_CLM_NODEGET;
  431. req_clm_nodeget.nodeId = nodeId;
  432. error = saSendRetry (clmInstance->fd, &req_clm_nodeget,
  433. sizeof (struct req_clm_nodeget), MSG_NOSIGNAL);
  434. if (error != SA_OK) {
  435. goto error_exit;
  436. }
  437. error = saRecvRetry (clmInstance->fd, &res_clm_nodeget,
  438. sizeof (struct res_clm_nodeget),
  439. MSG_WAITALL | MSG_NOSIGNAL);
  440. if (error != SA_OK) {
  441. goto error_exit;
  442. }
  443. error = res_clm_nodeget.header.error;
  444. memcpy (clusterNode, &res_clm_nodeget.clusterNode,
  445. sizeof (SaClmClusterNodeT));
  446. error_exit:
  447. pthread_mutex_unlock (&clmInstance->mutex);
  448. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  449. return (error);
  450. }
  451. SaAisErrorT
  452. saClmClusterNodeGetAsync (
  453. SaClmHandleT clmHandle,
  454. SaInvocationT invocation,
  455. SaClmNodeIdT nodeId)
  456. {
  457. struct clmInstance *clmInstance;
  458. struct req_clm_nodegetasync req_clm_nodegetasync;
  459. struct res_clm_nodegetasync res_clm_nodegetasync;
  460. SaAisErrorT error = SA_OK;
  461. req_clm_nodegetasync.header.size = sizeof (struct req_clm_nodeget);
  462. req_clm_nodegetasync.header.id = MESSAGE_REQ_CLM_NODEGETASYNC;
  463. memcpy (&req_clm_nodegetasync.invocation, &invocation,
  464. sizeof (SaInvocationT));
  465. memcpy (&req_clm_nodegetasync.nodeId, &nodeId, sizeof (SaClmNodeIdT));
  466. error = saHandleInstanceGet (&clmHandleDatabase, clmHandle,
  467. (void *)&clmInstance);
  468. if (error != SA_OK) {
  469. return (error);
  470. }
  471. pthread_mutex_lock (&clmInstance->mutex);
  472. error = saSendRetry (clmInstance->fd, &req_clm_nodegetasync,
  473. sizeof (struct req_clm_nodegetasync), MSG_NOSIGNAL);
  474. pthread_mutex_unlock (&clmInstance->mutex);
  475. error = saRecvQueue (clmInstance->fd, &res_clm_nodegetasync,
  476. &clmInstance->inq, MESSAGE_RES_CLM_NODEGETASYNC);
  477. if (error != SA_OK) {
  478. goto error_exit;
  479. }
  480. error = res_clm_nodegetasync.header.error;
  481. error_exit:
  482. saHandleInstancePut (&clmHandleDatabase, clmHandle);
  483. return (error);
  484. }