testvotequorum2.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <inttypes.h>
  37. #include <stdio.h>
  38. #include <stdint.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <corosync/corotypes.h>
  43. #include <corosync/votequorum.h>
  44. static votequorum_handle_t handle;
  45. static votequorum_ring_id_t last_received_ring_id;
  46. static int print_info(int ok_to_fail)
  47. {
  48. struct votequorum_info info;
  49. int err;
  50. if ( (err=votequorum_getinfo(handle, VOTEQUORUM_QDEVICE_NODEID, &info)) != CS_OK) {
  51. fprintf(stderr, "votequorum_getinfo error %d: %s\n", err, ok_to_fail?"OK":"FAILED");
  52. return -1;
  53. }
  54. else {
  55. printf("name %s\n", info.qdevice_name);
  56. printf("qdevice votes %d\n", info.qdevice_votes);
  57. if (info.flags & VOTEQUORUM_INFO_QDEVICE_ALIVE) {
  58. printf("alive ");
  59. }
  60. if (info.flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE) {
  61. printf("cast-vote ");
  62. }
  63. if (info.flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS) {
  64. printf("master-wins");
  65. }
  66. printf("\n\n");
  67. }
  68. return 0;
  69. }
  70. static void votequorum_notification_fn(
  71. votequorum_handle_t vq_handle,
  72. uint64_t context,
  73. uint32_t quorate,
  74. votequorum_ring_id_t ring_id,
  75. uint32_t node_list_entries,
  76. votequorum_node_t node_list[])
  77. {
  78. printf("votequorum notification called \n");
  79. printf(" current ringid = (%u.%"PRIu64")\n", ring_id.nodeid, ring_id.seq);
  80. printf("\n");
  81. memcpy(&last_received_ring_id, &ring_id, sizeof(ring_id));
  82. }
  83. static void usage(const char *command)
  84. {
  85. printf("%s [-p <num>] [-t <time>] [-n <name>] [-c] [-m]\n", command);
  86. printf(" -p <num> Number of times to poll qdevice (default 0=infinte)\n");
  87. printf(" -t <secs> Time (in seconds) to wait between polls (default=1)\n");
  88. printf(" -n <name> Name of quorum device (default QDEVICE)\n");
  89. printf(" -c Cast vote (default yes)\n");
  90. printf(" -q Don't print device status every poll time (default=will print)\n");
  91. printf(" -m Master wins (default no)\n");
  92. printf(" -1 Print status once and exit\n");
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. int ret = 0;
  97. int cast_vote = 1, master_wins = 0;
  98. int pollcount=0, polltime=1, quiet=0, once=0;
  99. int err;
  100. int opt;
  101. votequorum_callbacks_t callbacks;
  102. const char *devicename = "QDEVICE";
  103. const char *options = "n:p:t:cmq1h";
  104. memset(&callbacks, 0, sizeof(callbacks));
  105. callbacks.votequorum_notify_fn = votequorum_notification_fn;
  106. while ((opt = getopt(argc, argv, options)) != -1) {
  107. switch (opt) {
  108. case 'm':
  109. master_wins = 1;
  110. break;
  111. case 'c':
  112. cast_vote = 1;
  113. break;
  114. case '1':
  115. once = 1;
  116. break;
  117. case 'q':
  118. quiet = 1;
  119. break;
  120. case 'p':
  121. pollcount = atoi(optarg)+1;
  122. break;
  123. case 'n':
  124. devicename = strdup(optarg);
  125. break;
  126. case 't':
  127. polltime = atoi(optarg);
  128. break;
  129. case 'h':
  130. usage(argv[0]);
  131. exit(0);
  132. }
  133. }
  134. if ( (err=votequorum_initialize(&handle, &callbacks)) != CS_OK) {
  135. fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
  136. return -1;
  137. }
  138. if (quiet && once) {
  139. fprintf(stderr, "setting both -q (quet) and -1 (once) makes no sense\n");
  140. usage(argv[0]);
  141. exit(1);
  142. }
  143. if (!quiet) {
  144. print_info(1);
  145. }
  146. if (once) {
  147. exit(0);
  148. }
  149. if (argc >= 2) {
  150. if ( (err = votequorum_trackstart(handle, handle, CS_TRACK_CHANGES)) != CS_OK) {
  151. fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err);
  152. ret = -1;
  153. goto out;
  154. }
  155. if ( (err=votequorum_qdevice_register(handle, devicename)) != CS_OK) {
  156. fprintf(stderr, "qdevice_register FAILED: %d\n", err);
  157. ret = -1;
  158. goto out;
  159. }
  160. if ( (err=votequorum_qdevice_master_wins(handle, devicename, master_wins)) != CS_OK) {
  161. fprintf(stderr, "qdevice_master_wins FAILED: %d\n", err);
  162. ret = -1;
  163. goto out;
  164. }
  165. while (--pollcount) {
  166. if (votequorum_dispatch(handle, CS_DISPATCH_ALL) != CS_OK) {
  167. fprintf(stderr, "votequorum_dispatch error\n");
  168. ret = -1;
  169. goto out;
  170. }
  171. if (!quiet) print_info(0);
  172. if ((err=votequorum_qdevice_poll(handle, devicename, cast_vote, last_received_ring_id)) != CS_OK &&
  173. err != CS_ERR_MESSAGE_ERROR) {
  174. fprintf(stderr, "qdevice poll FAILED: %d\n", err);
  175. ret = -1;
  176. goto out;
  177. }
  178. if (err == CS_ERR_MESSAGE_ERROR) {
  179. fprintf(stderr, "qdevice poll passed OLD ring_id\n");
  180. }
  181. if (!quiet) print_info(0);
  182. sleep(polltime);
  183. }
  184. if ((err= votequorum_qdevice_unregister(handle, devicename)) != CS_OK) {
  185. fprintf(stderr, "qdevice unregister FAILED: %d\n", err);
  186. ret = -1;
  187. goto out;
  188. }
  189. }
  190. if (!quiet) print_info(1);
  191. out:
  192. votequorum_finalize(handle);
  193. return ret;
  194. }