4
0

votequorum_test_agent.c 8.9 KB

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