votequorum_test_agent.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2010 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@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 CONTRIBUTORS "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 <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <netdb.h>
  45. #include <sys/un.h>
  46. #include <syslog.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/votequorum.h>
  49. #include <corosync/quorum.h>
  50. #include "common_test_agent.h"
  51. static void getinfo (int sock)
  52. {
  53. votequorum_callbacks_t callbacks;
  54. int ret;
  55. struct votequorum_info info;
  56. char response[100];
  57. votequorum_handle_t g_handle;
  58. callbacks.votequorum_notify_fn = NULL;
  59. callbacks.votequorum_expectedvotes_notify_fn = NULL;
  60. ret = votequorum_initialize(&g_handle, &callbacks);
  61. if (ret != CS_OK) {
  62. snprintf (response, 100, "%s", FAIL_STR);
  63. syslog (LOG_ERR, "votequorum_initialize FAILED: %d\n", ret);
  64. goto send_response;
  65. }
  66. ret = votequorum_getinfo(g_handle, 0, &info);
  67. if (ret != CS_OK) {
  68. snprintf (response, 100, "%s", FAIL_STR);
  69. syslog (LOG_ERR, "votequorum_getinfo FAILED: %d\n", ret);
  70. goto send_response;
  71. }
  72. snprintf (response, 100, "%d:%d:%d:%d:%d",
  73. info.node_votes,
  74. info.node_expected_votes,
  75. info.highest_expected,
  76. info.total_votes,
  77. info.quorum);
  78. send_response:
  79. votequorum_finalize (g_handle);
  80. send (sock, response, strlen (response), 0);
  81. }
  82. static void setexpected (int sock, char *arg)
  83. {
  84. votequorum_callbacks_t callbacks;
  85. int ret;
  86. char response[100];
  87. votequorum_handle_t g_handle;
  88. callbacks.votequorum_notify_fn = NULL;
  89. callbacks.votequorum_expectedvotes_notify_fn = NULL;
  90. ret = votequorum_initialize(&g_handle, &callbacks);
  91. if (ret != CS_OK) {
  92. snprintf (response, 100, "%s", FAIL_STR);
  93. syslog (LOG_ERR, "votequorum_initialize FAILED: %d\n", ret);
  94. goto send_response;
  95. }
  96. ret = votequorum_setexpected (g_handle, atoi(arg));
  97. if (ret != CS_OK) {
  98. snprintf (response, 100, "%s", FAIL_STR);
  99. syslog (LOG_ERR, "set expected votes FAILED: %d\n", ret);
  100. goto send_response;
  101. }
  102. snprintf (response, 100, "%s", OK_STR);
  103. send_response:
  104. votequorum_finalize (g_handle);
  105. send (sock, response, strlen (response) + 1, 0);
  106. }
  107. static void setvotes (int sock, char *arg)
  108. {
  109. votequorum_callbacks_t callbacks;
  110. int ret;
  111. char response[100];
  112. votequorum_handle_t g_handle;
  113. callbacks.votequorum_notify_fn = NULL;
  114. callbacks.votequorum_expectedvotes_notify_fn = NULL;
  115. ret = votequorum_initialize(&g_handle, &callbacks);
  116. if (ret != CS_OK) {
  117. snprintf (response, 100, "%s", FAIL_STR);
  118. syslog (LOG_ERR, "votequorum_initialize FAILED: %d\n", ret);
  119. goto send_response;
  120. }
  121. ret = votequorum_setvotes (g_handle, 0, atoi(arg));
  122. if (ret != CS_OK) {
  123. snprintf (response, 100, "%s", FAIL_STR);
  124. syslog (LOG_ERR, "set votes FAILED: %d\n", ret);
  125. goto send_response;
  126. }
  127. snprintf (response, 100, "%s", OK_STR);
  128. send_response:
  129. votequorum_finalize (g_handle);
  130. send (sock, response, strlen (response), 0);
  131. }
  132. static void getquorate (int sock)
  133. {
  134. int ret;
  135. int quorate;
  136. char response[100];
  137. quorum_handle_t handle;
  138. ret = quorum_initialize (&handle, NULL);
  139. if (ret != CS_OK) {
  140. snprintf (response, 100, "%s", FAIL_STR);
  141. syslog (LOG_ERR, "quorum_initialize FAILED: %d\n", ret);
  142. goto send_response;
  143. }
  144. ret = quorum_getquorate (handle, &quorate);
  145. if (ret != CS_OK) {
  146. snprintf (response, 100, "%s", FAIL_STR);
  147. syslog (LOG_ERR, "getquorate FAILED: %d\n", ret);
  148. goto send_response;
  149. }
  150. snprintf (response, 100, "%d", quorate);
  151. send_response:
  152. quorum_finalize (handle);
  153. send (sock, response, strlen (response), 0);
  154. }
  155. static void do_command (int sock, char* func, char*args[], int num_args)
  156. {
  157. char response[100];
  158. if (parse_debug)
  159. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  160. if (strcmp ("votequorum_getinfo", func) == 0) {
  161. getinfo (sock);
  162. } else if (strcmp ("votequorum_setvotes", func) == 0) {
  163. setvotes (sock, args[0]);
  164. } else if (strcmp ("votequorum_setexpected", func) == 0) {
  165. setexpected (sock, args[0]);
  166. } else if (strcmp ("quorum_getquorate", func) == 0) {
  167. getquorate (sock);
  168. } else {
  169. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  170. snprintf (response, 100, "%s", NOT_SUPPORTED_STR);
  171. send (sock, response, strlen (response), 0);
  172. }
  173. }
  174. int main (int argc, char *argv[])
  175. {
  176. int ret;
  177. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  178. syslog (LOG_ERR, "votequorum_test_agent STARTING");
  179. parse_debug = 1;
  180. ret = test_agent_run (9037, do_command);
  181. syslog (LOG_ERR, "votequorum_test_agent EXITING");
  182. return ret;
  183. }