testvotequorum1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.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 CONTIBUTORS "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 <sys/types.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdint.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <corosync/corotypes.h>
  41. #include <corosync/votequorum.h>
  42. static votequorum_handle_t handle;
  43. static char *node_state(int state)
  44. {
  45. switch (state) {
  46. case NODESTATE_JOINING:
  47. return "Joining";
  48. break;
  49. case NODESTATE_MEMBER:
  50. return "Member";
  51. break;
  52. case NODESTATE_DEAD:
  53. return "Dead";
  54. break;
  55. case NODESTATE_LEAVING:
  56. return "Leaving";
  57. break;
  58. case NODESTATE_DISALLOWED:
  59. return "Disallowed";
  60. break;
  61. default:
  62. return "UNKNOWN";
  63. break;
  64. }
  65. }
  66. static void votequorum_notification_fn(
  67. votequorum_handle_t handle,
  68. uint64_t context,
  69. uint32_t quorate,
  70. uint32_t node_list_entries,
  71. votequorum_node_t node_list[]
  72. )
  73. {
  74. int i;
  75. printf("votequorum notification called \n");
  76. printf(" quorate = %d\n", quorate);
  77. printf(" number of nodes = %d\n", node_list_entries);
  78. for (i = 0; i< node_list_entries; i++) {
  79. printf(" %d: %s\n", node_list[i].nodeid, node_state(node_list[i].state));
  80. }
  81. printf("\n");
  82. }
  83. int main(int argc, char *argv[])
  84. {
  85. struct votequorum_info info;
  86. votequorum_callbacks_t callbacks;
  87. int err;
  88. if (argc > 1 && strcmp(argv[1], "-h")==0) {
  89. fprintf(stderr, "usage: %s [new-expected] [new-votes]\n", argv[0]);
  90. return 0;
  91. }
  92. callbacks.votequorum_notify_fn = votequorum_notification_fn;
  93. if ( (err=votequorum_initialize(&handle, &callbacks)) != CS_OK)
  94. fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
  95. if ( (err = votequorum_trackstart(handle, handle, CS_TRACK_CHANGES)) != CS_OK)
  96. fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err);
  97. if ( (err=votequorum_getinfo(handle, 0, &info)) != CS_OK)
  98. fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
  99. else {
  100. printf("node votes %d\n", info.node_votes);
  101. printf("expected votes %d\n", info.node_expected_votes);
  102. printf("highest expected %d\n", info.highest_expected);
  103. printf("total votes %d\n", info.total_votes);
  104. printf("quorum %d\n", info.quorum);
  105. printf("flags ");
  106. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  107. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("Disallowed ");
  108. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  109. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  110. printf("\n");
  111. }
  112. if (argc >= 2 && atoi(argv[1])) {
  113. if ( (err=votequorum_setexpected(handle, atoi(argv[1]))) != CS_OK)
  114. fprintf(stderr, "set expected votes FAILED: %d\n", err);
  115. }
  116. if (argc >= 3 && atoi(argv[2])) {
  117. if ( (err=votequorum_setvotes(handle, 0, atoi(argv[2]))) != CS_OK)
  118. fprintf(stderr, "set votes FAILED: %d\n", err);
  119. }
  120. if (argc >= 2) {
  121. if ( (err=votequorum_getinfo(handle, 0, &info)) != CS_OK)
  122. fprintf(stderr, "votequorum_getinfo2 FAILED: %d\n", err);
  123. else {
  124. printf("-------------------\n");
  125. printf("node votes %d\n", info.node_votes);
  126. printf("expected votes %d\n", info.node_expected_votes);
  127. printf("highest expected %d\n", info.highest_expected);
  128. printf("total votes %d\n", info.total_votes);
  129. printf("votequorum %d\n", info.quorum);
  130. printf("flags ");
  131. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  132. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("Disallowed ");
  133. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  134. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  135. printf("\n");
  136. }
  137. }
  138. printf("Waiting for votequorum events, press ^C to finish\n");
  139. printf("-------------------\n");
  140. while (1)
  141. votequorum_dispatch(handle, CS_DISPATCH_ALL);
  142. return 0;
  143. }