testclm2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2005 Ericsson AB
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Rabbe Fogelholm (rabbe.fogelholm@ericsson.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. /*
  35. * testclm2.c
  36. *
  37. * Simple program to test cluster membership on an SA Forum platform.
  38. * The program expects one command-line argument which is "query"
  39. * or "callback". "Query" means that a single saClmClusterTrack call
  40. * is to be made. "Callback" means that callbacks are wanted when
  41. * there are changes in cluster membership. At least a 2-node cluster
  42. * is required to test this program mode.
  43. *
  44. * Tested on platforms:
  45. * Gentoo Linux 2005-08 (build)
  46. * Fedora Core 4 (build and run)
  47. *
  48. * Change history:
  49. * 2005-08-28 Rabbe Fogelholm:
  50. * Initial version
  51. * 2005-08-30 Rabbe Fogelholm:
  52. * Added call to saClmClusterTrackStop()
  53. * Possible to test SA_TRACK_CHANGES_ONLY
  54. * Improved diagnostics
  55. */
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <signal.h>
  59. #include <stdlib.h>
  60. #include <unistd.h>
  61. #include <time.h>
  62. #include <saClm.h>
  63. #define PGM_NAME "testclm2"
  64. #define MODE_QUERY 1
  65. #define MODE_CALLBACK 2
  66. #define MODE_UNKNOWN -1
  67. SaClmHandleT handle;
  68. int mode = MODE_UNKNOWN;
  69. void interruptAction();
  70. void usage();
  71. void clusterTrack(const SaClmClusterNotificationBufferT *, SaUint32T, SaAisErrorT);
  72. int apiCall(char *, SaAisErrorT);
  73. char *decodeStatus(int);
  74. void printBoolean(SaBoolT);
  75. void printName(SaNameT *);
  76. void printAddress(SaClmNodeAddressT *);
  77. void printCluster(const SaClmClusterNotificationBufferT *);
  78. void printDate(SaTimeT);
  79. char *decodeClusterChange(int);
  80. int main(int argc, char *argv[])
  81. {
  82. struct sigaction act;
  83. act.sa_handler = interruptAction;
  84. int status;
  85. if ((status = sigaction(SIGINT, &act, NULL)) != 0)
  86. {
  87. printf("sigaction returned: %d\n", status);
  88. }
  89. if (argc != 2 && argc != 3)
  90. {
  91. usage();
  92. return 1;
  93. }
  94. mode =
  95. strcmp(argv[1], "query") == 0 ? MODE_QUERY :
  96. strcmp(argv[1], "callback") == 0 ? MODE_CALLBACK :
  97. MODE_UNKNOWN;
  98. if (mode == MODE_UNKNOWN)
  99. {
  100. usage();
  101. return 1;
  102. }
  103. if (mode == MODE_CALLBACK && argc != 3)
  104. {
  105. usage();
  106. return 1;
  107. }
  108. int trackingMode =
  109. mode == MODE_QUERY ? SA_TRACK_CURRENT :
  110. mode == MODE_CALLBACK && strcmp(argv[2], "full") == 0 ? SA_TRACK_CHANGES :
  111. mode == MODE_CALLBACK && strcmp(argv[2], "delta") == 0 ? SA_TRACK_CHANGES_ONLY :
  112. -1;
  113. if (trackingMode == -1)
  114. {
  115. usage();
  116. return 1;
  117. }
  118. SaClmCallbacksT callbacks;
  119. callbacks.saClmClusterNodeGetCallback = NULL;
  120. callbacks.saClmClusterTrackCallback = (SaClmClusterTrackCallbackT)clusterTrack;
  121. SaVersionT version;
  122. version.releaseCode = 'B';
  123. version.majorVersion = 1;
  124. version.minorVersion = 0;
  125. if (! apiCall("Initialize", saClmInitialize(&handle ,&callbacks, &version)))
  126. return 1;
  127. printf("AIS version supported: %c.%d.%d\n",
  128. version.releaseCode, version.majorVersion, version.minorVersion);
  129. if (mode == MODE_QUERY)
  130. {
  131. SaClmClusterNotificationBufferT buffer = {123456789, 123456789, NULL};
  132. apiCall("ClusterTrack", saClmClusterTrack(handle, trackingMode, &buffer));
  133. printCluster(&buffer);
  134. free(buffer.notification);
  135. }
  136. if (mode == MODE_CALLBACK)
  137. {
  138. printf("(type ctrl-C to finish)\n");
  139. apiCall("ClusterTrack", saClmClusterTrack(handle, trackingMode, NULL));
  140. while (1)
  141. {
  142. apiCall("Dispatch", saClmDispatch(handle, SA_DISPATCH_ONE));
  143. printf("sleep 1 sec\n");
  144. sleep(1);
  145. }
  146. }
  147. apiCall("Finalize", saClmFinalize(handle));
  148. return 0;
  149. }
  150. void interruptAction()
  151. {
  152. fprintf(stderr, "SIGINT signal caught\n");
  153. if (mode == MODE_CALLBACK)
  154. {
  155. apiCall("ClusterTrackStop", saClmClusterTrackStop(handle));
  156. }
  157. apiCall("Finalize", saClmFinalize(handle));
  158. exit(0);
  159. }
  160. void usage()
  161. {
  162. fprintf(stderr, "%s: usage is:\n", PGM_NAME);
  163. fprintf(stderr, " membership query Query for membership once\n");
  164. fprintf(stderr, " membership callback full Callback on membership change, full report\n");
  165. fprintf(stderr, " membership callback delta Callback on membership change, delta report\n");
  166. }
  167. void clusterTrack(
  168. const SaClmClusterNotificationBufferT *buffer,
  169. SaUint32T numberOfMembers,
  170. SaAisErrorT error)
  171. {
  172. apiCall("clusterTrack callback", error);
  173. printf("number of members: %d\n\n", numberOfMembers);
  174. printCluster(buffer);
  175. }
  176. int apiCall(char *call, SaAisErrorT code)
  177. {
  178. char *s = decodeStatus(code);
  179. printf("called: %s, status: %s", call, s);
  180. if (strcmp(s, "unknown error code") == 0)
  181. {
  182. printf(": %d\n\n", code);
  183. }
  184. else
  185. {
  186. printf("\n\n");
  187. }
  188. return code == SA_AIS_OK ? 1 : 0;
  189. }
  190. char *decodeStatus(int code)
  191. {
  192. return
  193. code == SA_AIS_OK ? "successful" :
  194. code == SA_AIS_ERR_LIBRARY ? "error in library, cannot be used anymore" :
  195. code == SA_AIS_ERR_VERSION ? "version incompatibility" :
  196. code == SA_AIS_ERR_INIT ? "callback function has not been supplied" :
  197. code == SA_AIS_ERR_TIMEOUT ? "timeout occurred, call may or may not have succeeded" :
  198. code == SA_AIS_ERR_TRY_AGAIN ? "service cannot be provided now, try later" :
  199. code == SA_AIS_ERR_INVALID_PARAM ? "a parameter is not set correctly" :
  200. code == SA_AIS_ERR_NO_MEMORY ? "out of memory" :
  201. code == SA_AIS_ERR_BAD_HANDLE ? "handle is invalid" :
  202. code == SA_AIS_ERR_BUSY ? "resource already in use" :
  203. code == SA_AIS_ERR_ACCESS ? "access denied" :
  204. code == SA_AIS_ERR_NOT_EXIST ? "entity does not exist" :
  205. code == SA_AIS_ERR_NAME_TOO_LONG ? "name too long" :
  206. code == SA_AIS_ERR_EXIST ? "entity already exists" :
  207. code == SA_AIS_ERR_NO_SPACE ? "buffer space is not sufficient" :
  208. code == SA_AIS_ERR_INTERRUPT ? "request canceled by timeout or other interrupt" :
  209. code == SA_AIS_ERR_NAME_NOT_FOUND ? "name not found" :
  210. code == SA_AIS_ERR_NOT_SUPPORTED ? "requested function is not supported" :
  211. code == SA_AIS_ERR_BAD_OPERATION ? "requested operation is not allowed" :
  212. code == SA_AIS_ERR_FAILED_OPERATION ? "healthcheck unsuccessful, error callback done" :
  213. code == SA_AIS_ERR_NO_RESOURCES ? "insufficient resources other than memory" :
  214. code == SA_AIS_ERR_MESSAGE_ERROR ? "a communication error occurred" :
  215. code == SA_AIS_ERR_QUEUE_FULL ? "destination queue is full" :
  216. code == SA_AIS_ERR_QUEUE_NOT_AVAILABLE ? "destination queue not available" :
  217. code == SA_AIS_ERR_BAD_FLAGS ? "flags are invalid" :
  218. code == SA_AIS_ERR_TOO_BIG ? "value larger than maximum permitted" :
  219. code == SA_AIS_ERR_NO_SECTIONS ? "no sections matching spec in SectionIteratorInitialize call" :
  220. "unknown error code";
  221. }
  222. void printBoolean(SaBoolT b)
  223. {
  224. printf("%s\n", b ? "true" : "false");
  225. }
  226. void printName(SaNameT *name)
  227. {
  228. int i;
  229. for (i=0; i<name->length; i++) printf("%c", name->value[i]);
  230. printf("\n");
  231. }
  232. void printAddress(SaClmNodeAddressT *nodeAddress)
  233. {
  234. if (nodeAddress->family == SA_CLM_AF_INET6)
  235. {
  236. printf("sorry, cannot decode IPv6 yet\n");
  237. }
  238. else if (nodeAddress->length == 4)
  239. {
  240. // we may get here due to defect 833, see
  241. // http://www.osdl.org/developer_bugzilla/show_bug.cgi?id=833 for details
  242. printf("%d.%d.%d.%d\n",
  243. nodeAddress->value[0],
  244. nodeAddress->value[1],
  245. nodeAddress->value[2],
  246. nodeAddress->value[3]);
  247. }
  248. else
  249. {
  250. int k;
  251. for (k = 0; k < nodeAddress->length; k++)
  252. {
  253. printf("%c", nodeAddress->value[k]);
  254. }
  255. printf("\n");
  256. }
  257. }
  258. void printCluster(const SaClmClusterNotificationBufferT *buffer)
  259. {
  260. printf(" view number: %llu\n", (unsigned long long)buffer->viewNumber);
  261. printf(" number of items: %u\n\n", buffer->numberOfItems);
  262. int j; for (j=0; j<buffer->numberOfItems; j++)
  263. {
  264. printf(" node index within sequence: %d\n", j);
  265. printf(" cluster node: %u\n", buffer->notification[j].clusterNode.nodeId);
  266. printf(" address: "); printAddress(& buffer->notification[j].clusterNode.nodeAddress);
  267. printf(" name: "); printName(&(buffer->notification[j].clusterNode.nodeName));
  268. printf(" member: "); printBoolean(buffer->notification[j].clusterNode.member);
  269. printf(" booted: "); printDate(buffer->notification[j].clusterNode.bootTimestamp);
  270. printf(" initial view number: %llu\n", (unsigned long long)buffer->notification[j].clusterNode.initialViewNumber);
  271. printf(" cluster change: %s\n", decodeClusterChange(buffer->notification[j].clusterChange));
  272. printf("\n");
  273. }
  274. }
  275. void printDate(SaTimeT nanoseconds)
  276. {
  277. time_t tt = nanoseconds/SA_TIME_ONE_SECOND;
  278. struct tm *decodedTime = localtime(&tt);
  279. printf("%d-%02d-%02d %02d:%02d:%02d\n",
  280. decodedTime->tm_year + 1900,
  281. decodedTime->tm_mon + 1,
  282. decodedTime->tm_mday,
  283. decodedTime->tm_hour,
  284. decodedTime->tm_min,
  285. decodedTime->tm_sec);
  286. }
  287. char *decodeClusterChange(int code)
  288. {
  289. return
  290. code == SA_CLM_NODE_NO_CHANGE ? "node has not changed" :
  291. code == SA_CLM_NODE_JOINED ? "node has joined the cluster" :
  292. code == SA_CLM_NODE_LEFT ? "node has left the cluster" :
  293. code == SA_CLM_NODE_RECONFIGURED ? "node has been reconfigured" :
  294. "unknown type of node change";
  295. }