votequorum_test_agent.c 8.9 KB

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