sam_test_agent.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 Red Hat, 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. /*
  35. * Provides test of SAM API
  36. */
  37. #include <config.h>
  38. #include <sys/types.h>
  39. #include <stdio.h>
  40. #include <stdint.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <signal.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <netdb.h>
  49. #include <sys/un.h>
  50. #include <sys/wait.h>
  51. #include <syslog.h>
  52. #include <corosync/corotypes.h>
  53. #include <corosync/sam.h>
  54. #include "common_test_agent.h"
  55. static int test2_sig_delivered = 0;
  56. static int test4_hc_cb_count = 0;
  57. /*
  58. * First test will just register SAM, with policy restart. First instance will
  59. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  60. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  61. * Then start hc again and sleep 3 seconds. This should force restart again.
  62. * Last instance just calls initialize again. This should end with error.
  63. * Then call start, followed by stop and start again. Finally, we will call finalize
  64. * twice. One should succeed, second should fail. After this, we will call every function
  65. * (none should succeed).
  66. */
  67. static int test1 (void)
  68. {
  69. cs_error_t error;
  70. unsigned int instance_id;
  71. int i;
  72. syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
  73. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  74. if (error != CS_OK) {
  75. syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
  76. return 1;
  77. }
  78. syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
  79. error = sam_register (&instance_id);
  80. if (error != CS_OK) {
  81. syslog (LOG_ERR, "Can't register. Error %d\n", error);
  82. return 1;
  83. }
  84. if (instance_id == 1 || instance_id == 2) {
  85. syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
  86. error = sam_start ();
  87. if (error != CS_OK) {
  88. syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
  89. return 1;
  90. }
  91. for (i = 0; i < 10; i++) {
  92. syslog (LOG_INFO,"%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  93. sleep (1);
  94. syslog (LOG_INFO,"%s iid %d: hc send\n", __FUNCTION__, instance_id);
  95. error = sam_hc_send ();
  96. if (error != CS_OK) {
  97. syslog (LOG_ERR, "Can't send hc. Error %d\n", error);
  98. return 1;
  99. }
  100. }
  101. if (instance_id == 2) {
  102. syslog (LOG_INFO,"%s iid %d: stop\n", __FUNCTION__, instance_id);
  103. error = sam_stop ();
  104. if (error != CS_OK) {
  105. syslog (LOG_ERR, "Can't send hc. Error %d\n", error);
  106. return 1;
  107. }
  108. }
  109. syslog (LOG_INFO,"%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  110. sleep (3);
  111. syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
  112. error = sam_start ();
  113. if (error != CS_OK) {
  114. syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
  115. return 1;
  116. }
  117. syslog (LOG_INFO,"%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  118. sleep (3);
  119. return 0;
  120. }
  121. if (instance_id == 3) {
  122. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  123. if (error == CS_OK) {
  124. syslog (LOG_ERR, "Can initialize SAM API after initialization");
  125. return 1;
  126. }
  127. error = sam_start ();
  128. if (error != CS_OK) {
  129. syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
  130. return 1;
  131. }
  132. error = sam_stop ();
  133. if (error != CS_OK) {
  134. syslog (LOG_ERR, "Can't stop hc. Error %d\n", error);
  135. return 1;
  136. }
  137. error = sam_finalize ();
  138. if (error != CS_OK) {
  139. syslog (LOG_ERR, "Can't finalize sam. Error %d\n", error);
  140. return 1;
  141. }
  142. error = sam_finalize ();
  143. if (error == CS_OK) {
  144. syslog (LOG_ERR, "Can finalize sam after finalization!\n");
  145. return 1;
  146. }
  147. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  148. sam_start () == CS_OK || sam_stop () == CS_OK ||
  149. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  150. sam_hc_callback_register (NULL) == CS_OK) {
  151. syslog (LOG_ERR, "Can call one of function after finalization!\n");
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. static void test2_signal (int sig) {
  159. syslog (LOG_INFO,"%s\n", __FUNCTION__);
  160. test2_sig_delivered = 1;
  161. }
  162. /*
  163. * This tests recovery policy quit and callback.
  164. */
  165. static int test2 (void) {
  166. cs_error_t error;
  167. unsigned int instance_id;
  168. syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
  169. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  170. if (error != CS_OK) {
  171. syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
  172. return 1;
  173. }
  174. syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
  175. error = sam_register (&instance_id);
  176. if (error != CS_OK) {
  177. syslog (LOG_ERR, "Can't register. Error %d\n", error);
  178. return 1;
  179. }
  180. if (instance_id == 1) {
  181. signal (SIGTERM, test2_signal);
  182. syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
  183. error = sam_start ();
  184. if (error != CS_OK) {
  185. syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
  186. return 1;
  187. }
  188. syslog (LOG_INFO,"%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  189. sleep (1);
  190. syslog (LOG_INFO,"%s iid %d: hc send\n", __FUNCTION__, instance_id);
  191. error = sam_hc_send ();
  192. if (error != CS_OK) {
  193. syslog (LOG_ERR, "Can't send hc. Error %d\n", error);
  194. return 1;
  195. }
  196. syslog (LOG_INFO,"%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  197. while (!test2_sig_delivered) {
  198. sleep (1);
  199. }
  200. syslog (LOG_INFO,"%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  201. sleep (3);
  202. }
  203. return 1;
  204. }
  205. /*
  206. * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
  207. * when it dies.
  208. */
  209. static int test3 (void) {
  210. cs_error_t error;
  211. unsigned int instance_id;
  212. int tmp1, tmp2, tmp3;
  213. syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
  214. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  215. if (error != CS_OK) {
  216. syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
  217. return 1;
  218. }
  219. syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
  220. error = sam_register (&instance_id);
  221. if (error != CS_OK) {
  222. syslog (LOG_ERR, "Can't register. Error %d\n", error);
  223. return 1;
  224. }
  225. if (instance_id < 100) {
  226. syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
  227. error = sam_start ();
  228. if (error != CS_OK) {
  229. syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
  230. return 1;
  231. }
  232. syslog (LOG_INFO,"%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
  233. tmp2 = rand ();
  234. tmp3 = 0;
  235. tmp1 = tmp2 / tmp3;
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int test4_hc_cb (void)
  241. {
  242. syslog (LOG_INFO,"%s %d\n", __FUNCTION__, ++test4_hc_cb_count);
  243. if (test4_hc_cb_count > 10)
  244. return 1;
  245. return 0;
  246. }
  247. /*
  248. * Test event driven healtchecking.
  249. */
  250. static int test4 (void)
  251. {
  252. cs_error_t error;
  253. unsigned int instance_id;
  254. syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
  255. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  256. if (error != CS_OK) {
  257. syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
  258. return 1;
  259. }
  260. syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
  261. error = sam_register (&instance_id);
  262. if (error != CS_OK) {
  263. syslog (LOG_ERR, "Can't register. Error %d\n", error);
  264. return 1;
  265. }
  266. if (instance_id == 1) {
  267. syslog (LOG_INFO,"%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  268. error = sam_hc_callback_register (test4_hc_cb);
  269. if (error != CS_OK) {
  270. syslog (LOG_ERR, "Can't register hc cb. Error %d\n", error);
  271. return 1;
  272. }
  273. syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
  274. error = sam_start ();
  275. if (error != CS_OK) {
  276. syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
  277. return 1;
  278. }
  279. sleep (2);
  280. syslog (LOG_INFO,"%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  281. return 1;
  282. }
  283. if (instance_id == 2) {
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. static void do_command (int sock, char* func, char*args[], int num_args)
  289. {
  290. char response[100];
  291. pid_t pid;
  292. int err;
  293. int stat;
  294. int please_wait = 1;
  295. snprintf (response, 100, "%s", FAIL_STR);
  296. if (parse_debug)
  297. syslog (LOG_INFO,"RPC:%s() called.", func);
  298. pid = fork ();
  299. if (pid == -1) {
  300. syslog (LOG_ERR, "Can't fork\n");
  301. send (sock, response, strlen (response) + 1, 0);
  302. return;
  303. }
  304. if (pid == 0) {
  305. if (strcmp ("test1", func) == 0) {
  306. err = test1 ();
  307. } else if (strcmp ("test2", func) == 0) {
  308. err = test2 ();
  309. } else if (strcmp ("test3", func) == 0) {
  310. err = test3 ();
  311. } else if (strcmp ("test4", func) == 0) {
  312. err = test4 ();
  313. } else {
  314. err = -1;
  315. please_wait = 0;
  316. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  317. snprintf (response, 100, "%s", NOT_SUPPORTED_STR);
  318. }
  319. }
  320. if (please_wait) {
  321. waitpid (pid, &stat, 0);
  322. if (WEXITSTATUS (stat) == 0) {
  323. snprintf (response, 100, "%s", OK_STR);
  324. } else {
  325. snprintf (response, 100, "%s", FAIL_STR);
  326. }
  327. }
  328. send (sock, response, strlen (response) + 1, 0);
  329. }
  330. int main (int argc, char *argv[])
  331. {
  332. int ret;
  333. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  334. syslog (LOG_ERR, "sam_test_agent STARTING");
  335. parse_debug = 1;
  336. ret = test_agent_run (9036, do_command);
  337. syslog (LOG_ERR, "sam_test_agent EXITING");
  338. return ret;
  339. }