testvotequorum2.c 6.8 KB

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