testvotequorum2.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2009-2014 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 <config.h>
  35. #include <sys/types.h>
  36. #include <stdio.h>
  37. #include <stdint.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <corosync/corotypes.h>
  42. #include <corosync/votequorum.h>
  43. static votequorum_handle_t handle;
  44. static int print_info(int ok_to_fail)
  45. {
  46. struct votequorum_info info;
  47. int err;
  48. if ( (err=votequorum_getinfo(handle, VOTEQUORUM_QDEVICE_NODEID, &info)) != CS_OK) {
  49. fprintf(stderr, "votequorum_getinfo error %d: %s\n", err, ok_to_fail?"OK":"FAILED");
  50. return -1;
  51. }
  52. else {
  53. printf("name %s\n", info.qdevice_name);
  54. printf("qdevice votes %d\n", info.qdevice_votes);
  55. if (info.flags & VOTEQUORUM_INFO_QDEVICE_ALIVE) {
  56. printf("alive ");
  57. }
  58. if (info.flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  59. printf("cast-vote ");
  60. }
  61. if (info.flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS) {
  62. printf("master-wins");
  63. }
  64. printf("\n\n");
  65. }
  66. return 0;
  67. }
  68. static void usage(const char *command)
  69. {
  70. printf("%s [-p <num>] [-t <time>] [-n <name>] [-c] [-m]\n", command);
  71. printf(" -p <num> Number of times to poll qdevice (default 0=infinte)\n");
  72. printf(" -t <secs> Time (in seconds) to wait between polls (default=1)\n");
  73. printf(" -n <name> Name of quorum device (default QDEVICE)\n");
  74. printf(" -c Cast vote (default yes)\n");
  75. printf(" -q Don't print device status every poll time (default=will print)\n");
  76. printf(" -m Master wins (default no)\n");
  77. printf(" -1 Print status once and exit\n");
  78. }
  79. int main(int argc, char *argv[])
  80. {
  81. int ret = 0;
  82. int cast_vote = 1, master_wins = 0;
  83. int pollcount=0, polltime=1, quiet=0, once=0;
  84. int err;
  85. int opt;
  86. const char *devicename = "QDEVICE";
  87. const char *options = "n:p:t:cmq1h";
  88. while ((opt = getopt(argc, argv, options)) != -1) {
  89. switch (opt) {
  90. case 'm':
  91. master_wins = 1;
  92. break;
  93. case 'c':
  94. cast_vote = 1;
  95. break;
  96. case '1':
  97. once = 1;
  98. break;
  99. case 'q':
  100. quiet = 1;
  101. break;
  102. case 'p':
  103. pollcount = atoi(optarg)+1;
  104. break;
  105. case 'n':
  106. devicename = strdup(optarg);
  107. break;
  108. case 't':
  109. polltime = atoi(optarg);
  110. break;
  111. case 'h':
  112. usage(argv[0]);
  113. exit(0);
  114. }
  115. }
  116. if ( (err=votequorum_initialize(&handle, NULL)) != CS_OK) {
  117. fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
  118. return -1;
  119. }
  120. if (quiet && once) {
  121. fprintf(stderr, "setting both -q (quet) and -1 (once) makes no sense\n");
  122. usage(argv[0]);
  123. exit(1);
  124. }
  125. if (!quiet) {
  126. print_info(1);
  127. }
  128. if (once) {
  129. exit(0);
  130. }
  131. if (argc >= 2) {
  132. if ( (err=votequorum_qdevice_register(handle, devicename)) != CS_OK) {
  133. fprintf(stderr, "qdevice_register FAILED: %d\n", err);
  134. ret = -1;
  135. goto out;
  136. }
  137. if ( (err=votequorum_qdevice_master_wins(handle, devicename, master_wins)) != CS_OK) {
  138. fprintf(stderr, "qdevice_master_wins FAILED: %d\n", err);
  139. ret = -1;
  140. goto out;
  141. }
  142. while (--pollcount) {
  143. if (!quiet) print_info(0);
  144. if ((err=votequorum_qdevice_poll(handle, devicename, cast_vote)) != CS_OK) {
  145. fprintf(stderr, "qdevice poll FAILED: %d\n", err);
  146. ret = -1;
  147. goto out;
  148. }
  149. if (!quiet) print_info(0);
  150. sleep(polltime);
  151. }
  152. if ((err= votequorum_qdevice_unregister(handle, devicename)) != CS_OK) {
  153. fprintf(stderr, "qdevice unregister FAILED: %d\n", err);
  154. ret = -1;
  155. goto out;
  156. }
  157. }
  158. if (!quiet) print_info(1);
  159. out:
  160. votequorum_finalize(handle);
  161. return ret;
  162. }