votequorum_test_agent.c 8.9 KB

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