votequorum_test_agent.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <poll.h>
  48. #include <corosync/totem/coropoll.h>
  49. #include <corosync/corotypes.h>
  50. #include <corosync/votequorum.h>
  51. #include <corosync/quorum.h>
  52. #include "common_test_agent.h"
  53. static quorum_handle_t q_handle = 0;
  54. static votequorum_handle_t vq_handle = 0;
  55. static void votequorum_notification_fn(
  56. votequorum_handle_t handle,
  57. uint64_t context,
  58. uint32_t quorate,
  59. uint32_t node_list_entries,
  60. votequorum_node_t node_list[])
  61. {
  62. syslog (LOG_INFO, "VQ notification quorate: %d", quorate);
  63. }
  64. static void quorum_notification_fn(
  65. quorum_handle_t handle,
  66. uint32_t quorate,
  67. uint64_t ring_id,
  68. uint32_t view_list_entries,
  69. uint32_t *view_list)
  70. {
  71. syslog (LOG_INFO, "NQ notification quorate: %d", quorate);
  72. }
  73. static int vq_dispatch_wrapper_fn (hdb_handle_t handle,
  74. int fd,
  75. int revents,
  76. void *data)
  77. {
  78. cs_error_t error = votequorum_dispatch (vq_handle, CS_DISPATCH_ALL);
  79. if (error == CS_ERR_LIBRARY) {
  80. syslog (LOG_ERR, "%s() got LIB error disconnecting from corosync.", __func__);
  81. poll_dispatch_delete (ta_poll_handle_get(), fd);
  82. close (fd);
  83. }
  84. return 0;
  85. }
  86. static int q_dispatch_wrapper_fn (hdb_handle_t handle,
  87. int fd,
  88. int revents,
  89. void *data)
  90. {
  91. cs_error_t error = quorum_dispatch (q_handle, CS_DISPATCH_ALL);
  92. if (error == CS_ERR_LIBRARY) {
  93. syslog (LOG_ERR, "%s() got LIB error disconnecting from corosync.", __func__);
  94. poll_dispatch_delete (ta_poll_handle_get(), fd);
  95. close (fd);
  96. }
  97. return 0;
  98. }
  99. static int q_lib_init(void)
  100. {
  101. votequorum_callbacks_t vq_callbacks;
  102. quorum_callbacks_t q_callbacks;
  103. int ret = 0;
  104. int retry = 3;
  105. int fd;
  106. if (vq_handle == 0) {
  107. syslog (LOG_INFO, "votequorum_initialize");
  108. vq_callbacks.votequorum_notify_fn = votequorum_notification_fn;
  109. vq_callbacks.votequorum_expectedvotes_notify_fn = NULL;
  110. ret = CS_ERR_NOT_EXIST;
  111. while (ret == CS_ERR_NOT_EXIST && retry > 0) {
  112. ret = votequorum_initialize (&vq_handle, &vq_callbacks);
  113. if (ret == CS_ERR_NOT_EXIST) {
  114. sleep (1);
  115. retry--;
  116. }
  117. }
  118. if (ret != CS_OK) {
  119. syslog (LOG_ERR, "votequorum_initialize FAILED: %d\n", ret);
  120. vq_handle = 0;
  121. }
  122. else {
  123. ret = votequorum_trackstart (vq_handle, vq_handle, CS_TRACK_CHANGES);
  124. if (ret != CS_OK) {
  125. syslog (LOG_ERR, "votequorum_trackstart FAILED: %d\n", ret);
  126. }
  127. votequorum_fd_get (vq_handle, &fd);
  128. poll_dispatch_add (ta_poll_handle_get(), fd,
  129. POLLIN|POLLNVAL, NULL, vq_dispatch_wrapper_fn);
  130. }
  131. }
  132. if (q_handle == 0) {
  133. syslog (LOG_INFO, "quorum_initialize");
  134. q_callbacks.quorum_notify_fn = quorum_notification_fn;
  135. ret = quorum_initialize (&q_handle, &q_callbacks);
  136. if (ret != CS_OK) {
  137. syslog (LOG_ERR, "quorum_initialize FAILED: %d\n", ret);
  138. q_handle = 0;
  139. }
  140. else {
  141. ret = quorum_trackstart (q_handle, CS_TRACK_CHANGES);
  142. if (ret != CS_OK) {
  143. syslog (LOG_ERR, "quorum_trackstart FAILED: %d\n", ret);
  144. }
  145. quorum_fd_get (q_handle, &fd);
  146. poll_dispatch_add (ta_poll_handle_get(), fd,
  147. POLLIN|POLLNVAL, NULL, q_dispatch_wrapper_fn);
  148. }
  149. }
  150. return ret;
  151. }
  152. static void lib_init (int sock)
  153. {
  154. int ret;
  155. char response[100];
  156. snprintf (response, 100, "%s", OK_STR);
  157. ret = q_lib_init ();
  158. if (ret != CS_OK) {
  159. snprintf (response, 100, "%s", FAIL_STR);
  160. syslog (LOG_ERR, "q_lib_init FAILED: %d\n", ret);
  161. }
  162. send (sock, response, strlen (response), 0);
  163. }
  164. static void getinfo (int sock)
  165. {
  166. int ret;
  167. struct votequorum_info info;
  168. char response[100];
  169. q_lib_init ();
  170. ret = votequorum_getinfo(vq_handle, 0, &info);
  171. if (ret != CS_OK) {
  172. snprintf (response, 100, "%s", FAIL_STR);
  173. syslog (LOG_ERR, "votequorum_getinfo FAILED: %d\n", ret);
  174. goto send_response;
  175. }
  176. snprintf (response, 100, "%d:%d:%d:%d:%d",
  177. info.node_votes,
  178. info.node_expected_votes,
  179. info.highest_expected,
  180. info.total_votes,
  181. info.quorum);
  182. send_response:
  183. send (sock, response, strlen (response), 0);
  184. }
  185. static void setexpected (int sock, char *arg)
  186. {
  187. int ret;
  188. char response[100];
  189. q_lib_init ();
  190. ret = votequorum_setexpected (vq_handle, atoi(arg));
  191. if (ret != CS_OK) {
  192. snprintf (response, 100, "%s", FAIL_STR);
  193. syslog (LOG_ERR, "set expected votes FAILED: %d\n", ret);
  194. goto send_response;
  195. }
  196. snprintf (response, 100, "%s", OK_STR);
  197. send_response:
  198. votequorum_finalize (vq_handle);
  199. send (sock, response, strlen (response) + 1, 0);
  200. }
  201. static void setvotes (int sock, char *arg)
  202. {
  203. int ret;
  204. char response[100];
  205. q_lib_init ();
  206. ret = votequorum_setvotes (vq_handle, 0, atoi(arg));
  207. if (ret != CS_OK) {
  208. snprintf (response, 100, "%s", FAIL_STR);
  209. syslog (LOG_ERR, "set votes FAILED: %d\n", ret);
  210. goto send_response;
  211. }
  212. snprintf (response, 100, "%s", OK_STR);
  213. send_response:
  214. votequorum_finalize (vq_handle);
  215. send (sock, response, strlen (response), 0);
  216. }
  217. static void getquorate (int sock)
  218. {
  219. int ret;
  220. int quorate;
  221. char response[100];
  222. q_lib_init ();
  223. ret = quorum_getquorate (q_handle, &quorate);
  224. if (ret != CS_OK) {
  225. snprintf (response, 100, "%s", FAIL_STR);
  226. syslog (LOG_ERR, "getquorate FAILED: %d\n", ret);
  227. goto send_response;
  228. }
  229. snprintf (response, 100, "%d", quorate);
  230. send_response:
  231. send (sock, response, strlen (response), 0);
  232. }
  233. static void context_test (int sock)
  234. {
  235. char response[100];
  236. char *cmp;
  237. snprintf (response, 100, "%s", OK_STR);
  238. votequorum_context_set (vq_handle, response);
  239. votequorum_context_get (vq_handle, (void**)&cmp);
  240. if (response != cmp) {
  241. snprintf (response, 100, "%s", FAIL_STR);
  242. syslog (LOG_ERR, "votequorum context not the same");
  243. }
  244. quorum_context_set (q_handle, response);
  245. quorum_context_get (q_handle, (const void**)&cmp);
  246. if (response != cmp) {
  247. snprintf (response, 100, "%s", FAIL_STR);
  248. syslog (LOG_ERR, "quorum context not the same");
  249. }
  250. send (sock, response, strlen (response) + 1, 0);
  251. }
  252. static void do_command (int sock, char* func, char*args[], int num_args)
  253. {
  254. char response[100];
  255. if (parse_debug)
  256. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  257. if (strcmp ("votequorum_getinfo", func) == 0) {
  258. getinfo (sock);
  259. } else if (strcmp ("votequorum_setvotes", func) == 0) {
  260. setvotes (sock, args[0]);
  261. } else if (strcmp ("votequorum_setexpected", func) == 0) {
  262. setexpected (sock, args[0]);
  263. } else if (strcmp ("quorum_getquorate", func) == 0) {
  264. getquorate (sock);
  265. } else if (strcmp ("init", func) == 0) {
  266. lib_init (sock);
  267. } else if (strcmp ("context_test", func) == 0) {
  268. context_test (sock);
  269. } else if (strcmp ("are_you_ok_dude", func) == 0) {
  270. snprintf (response, 100, "%s", OK_STR);
  271. send (sock, response, strlen (response) + 1, 0);
  272. } else {
  273. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  274. snprintf (response, 100, "%s", NOT_SUPPORTED_STR);
  275. send (sock, response, strlen (response), 0);
  276. }
  277. }
  278. int main (int argc, char *argv[])
  279. {
  280. int ret;
  281. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  282. syslog (LOG_ERR, "votequorum_test_agent STARTING");
  283. parse_debug = 1;
  284. ret = test_agent_run (9037, do_command);
  285. syslog (LOG_ERR, "votequorum_test_agent EXITING");
  286. return ret;
  287. }