testvotequorum2.c 6.9 KB

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