testsam.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 <corosync/corotypes.h>
  44. #include <corosync/sam.h>
  45. #include <signal.h>
  46. #include <string.h>
  47. #include <sys/wait.h>
  48. static int test2_sig_delivered = 0;
  49. static int test5_hc_cb_count = 0;
  50. static int test6_sig_delivered = 0;
  51. /*
  52. * First test will just register SAM, with policy restart. First instance will
  53. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  54. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  55. * Then start hc again and sleep 3 seconds. This should force restart again.
  56. * Last instance just calls initialize again. This should end with error.
  57. * Then call start, followed by stop and start again. Finally, we will call finalize
  58. * twice. One should succeed, second should fail. After this, we will call every function
  59. * (none should succeed).
  60. */
  61. static int test1 (void)
  62. {
  63. cs_error_t error;
  64. unsigned int instance_id;
  65. int i;
  66. printf ("%s: initialize\n", __FUNCTION__);
  67. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  68. if (error != CS_OK) {
  69. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  70. return 1;
  71. }
  72. printf ("%s: register\n", __FUNCTION__);
  73. error = sam_register (&instance_id);
  74. if (error != CS_OK) {
  75. fprintf (stderr, "Can't register. Error %d\n", error);
  76. return 1;
  77. }
  78. if (instance_id == 1 || instance_id == 2) {
  79. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  80. error = sam_start ();
  81. if (error != CS_OK) {
  82. fprintf (stderr, "Can't start hc. Error %d\n", error);
  83. return 1;
  84. }
  85. for (i = 0; i < 10; i++) {
  86. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  87. sleep (1);
  88. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  89. error = sam_hc_send ();
  90. if (error != CS_OK) {
  91. fprintf (stderr, "Can't send hc. Error %d\n", error);
  92. return 1;
  93. }
  94. }
  95. if (instance_id == 2) {
  96. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  97. error = sam_stop ();
  98. if (error != CS_OK) {
  99. fprintf (stderr, "Can't send hc. Error %d\n", error);
  100. return 1;
  101. }
  102. }
  103. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  104. sleep (3);
  105. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  106. error = sam_start ();
  107. if (error != CS_OK) {
  108. fprintf (stderr, "Can't start hc. Error %d\n", error);
  109. return 1;
  110. }
  111. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  112. sleep (3);
  113. return 0;
  114. }
  115. if (instance_id == 3) {
  116. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  117. if (error == CS_OK) {
  118. fprintf (stderr, "Can initialize SAM API after initialization");
  119. return 1;
  120. }
  121. error = sam_start ();
  122. if (error != CS_OK) {
  123. fprintf (stderr, "Can't start hc. Error %d\n", error);
  124. return 1;
  125. }
  126. error = sam_stop ();
  127. if (error != CS_OK) {
  128. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  129. return 1;
  130. }
  131. error = sam_finalize ();
  132. if (error != CS_OK) {
  133. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  134. return 1;
  135. }
  136. error = sam_finalize ();
  137. if (error == CS_OK) {
  138. fprintf (stderr, "Can finalize sam after finalization!\n");
  139. return 1;
  140. }
  141. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  142. sam_start () == CS_OK || sam_stop () == CS_OK ||
  143. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  144. sam_hc_callback_register (NULL) == CS_OK) {
  145. fprintf (stderr, "Can call one of function after finalization!\n");
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. static void test2_signal (int sig) {
  153. printf ("%s\n", __FUNCTION__);
  154. test2_sig_delivered = 1;
  155. }
  156. /*
  157. * This tests recovery policy quit and callback.
  158. */
  159. static int test2 (void) {
  160. cs_error_t error;
  161. unsigned int instance_id;
  162. printf ("%s: initialize\n", __FUNCTION__);
  163. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  164. if (error != CS_OK) {
  165. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  166. return 1;
  167. }
  168. printf ("%s: register\n", __FUNCTION__);
  169. error = sam_register (&instance_id);
  170. if (error != CS_OK) {
  171. fprintf (stderr, "Can't register. Error %d\n", error);
  172. return 1;
  173. }
  174. if (instance_id == 1) {
  175. signal (SIGTERM, test2_signal);
  176. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  177. error = sam_start ();
  178. if (error != CS_OK) {
  179. fprintf (stderr, "Can't start hc. Error %d\n", error);
  180. return 1;
  181. }
  182. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  183. sleep (1);
  184. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  185. error = sam_hc_send ();
  186. if (error != CS_OK) {
  187. fprintf (stderr, "Can't send hc. Error %d\n", error);
  188. return 1;
  189. }
  190. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  191. while (!test2_sig_delivered) {
  192. sleep (1);
  193. }
  194. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  195. sleep (3);
  196. }
  197. return 1;
  198. }
  199. /*
  200. * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
  201. * when it dies.
  202. */
  203. static int test3 (void) {
  204. cs_error_t error;
  205. unsigned int instance_id;
  206. int tmp1, tmp2, tmp3;
  207. printf ("%s: initialize\n", __FUNCTION__);
  208. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  209. if (error != CS_OK) {
  210. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  211. return 1;
  212. }
  213. printf ("%s: register\n", __FUNCTION__);
  214. error = sam_register (&instance_id);
  215. if (error != CS_OK) {
  216. fprintf (stderr, "Can't register. Error %d\n", error);
  217. return 1;
  218. }
  219. if (instance_id < 100) {
  220. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  221. error = sam_start ();
  222. if (error != CS_OK) {
  223. fprintf (stderr, "Can't start hc. Error %d\n", error);
  224. return 1;
  225. }
  226. printf ("%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
  227. tmp2 = rand ();
  228. tmp3 = 0;
  229. tmp1 = tmp2 / tmp3;
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. /*
  235. * Test sam_data_store, sam_data_restore and sam_data_getsize
  236. */
  237. static int test4 (void)
  238. {
  239. size_t size;
  240. cs_error_t err;
  241. int i;
  242. unsigned int instance_id;
  243. char saved_data[128];
  244. char saved_data2[128];
  245. printf ("%s: sam_data_getsize 1\n", __FUNCTION__);
  246. err = sam_data_getsize (&size);
  247. if (err != CS_ERR_BAD_HANDLE) {
  248. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  249. return 1;
  250. }
  251. printf ("%s: sam_data_getsize 2\n", __FUNCTION__);
  252. err = sam_data_getsize (NULL);
  253. if (err != CS_ERR_INVALID_PARAM) {
  254. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  255. return 1;
  256. }
  257. printf ("%s: sam_data_store 1\n", __FUNCTION__);
  258. err = sam_data_store (NULL, 0);
  259. if (err != CS_ERR_BAD_HANDLE) {
  260. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  261. return 1;
  262. }
  263. printf ("%s: sam_data_restore 1\n", __FUNCTION__);
  264. err = sam_data_restore (saved_data, sizeof (saved_data));
  265. if (err != CS_ERR_BAD_HANDLE) {
  266. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  267. return 1;
  268. }
  269. printf ("%s: sam_initialize\n", __FUNCTION__);
  270. err = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  271. if (err != CS_OK) {
  272. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  273. return 1;
  274. }
  275. printf ("%s: sam_data_getsize 3\n", __FUNCTION__);
  276. err = sam_data_getsize (&size);
  277. if (err != CS_OK) {
  278. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  279. return 1;
  280. }
  281. if (size != 0) {
  282. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  283. return 1;
  284. }
  285. printf ("%s: sam_data_restore 2\n", __FUNCTION__);
  286. err = sam_data_restore (NULL, sizeof (saved_data));
  287. if (err != CS_ERR_INVALID_PARAM) {
  288. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  289. return 1;
  290. }
  291. /*
  292. * Store some real data
  293. */
  294. for (i = 0; i < sizeof (saved_data); i++) {
  295. saved_data[i] = (char)(i + 5);
  296. }
  297. printf ("%s: sam_data_store 2\n", __FUNCTION__);
  298. err = sam_data_store (saved_data, sizeof (saved_data));
  299. if (err != CS_OK) {
  300. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  301. return 1;
  302. }
  303. printf ("%s: sam_data_getsize 4\n", __FUNCTION__);
  304. err = sam_data_getsize (&size);
  305. if (err != CS_OK) {
  306. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  307. return 1;
  308. }
  309. if (size != sizeof (saved_data)) {
  310. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  311. return 1;
  312. }
  313. printf ("%s: sam_data_restore 3\n", __FUNCTION__);
  314. err = sam_data_restore (saved_data2, sizeof (saved_data2) - 1);
  315. if (err != CS_ERR_INVALID_PARAM) {
  316. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  317. return 1;
  318. }
  319. printf ("%s: sam_data_restore 4\n", __FUNCTION__);
  320. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  321. if (err != CS_OK) {
  322. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  323. return 1;
  324. }
  325. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  326. fprintf (stderr, "Retored data are not same\n");
  327. return 1;
  328. }
  329. memset (saved_data2, 0, sizeof (saved_data2));
  330. printf ("%s: sam_data_store 3\n", __FUNCTION__);
  331. err = sam_data_store (NULL, 1);
  332. if (err != CS_OK) {
  333. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  334. return 1;
  335. }
  336. printf ("%s: sam_data_getsize 5\n", __FUNCTION__);
  337. err = sam_data_getsize (&size);
  338. if (err != CS_OK) {
  339. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  340. return 1;
  341. }
  342. if (size != 0) {
  343. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  344. return 1;
  345. }
  346. printf ("%s: sam_data_store 4\n", __FUNCTION__);
  347. err = sam_data_store (saved_data, sizeof (saved_data));
  348. if (err != CS_OK) {
  349. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  350. return 1;
  351. }
  352. printf ("%s: register\n", __FUNCTION__);
  353. err = sam_register (&instance_id);
  354. if (err != CS_OK) {
  355. fprintf (stderr, "Can't register. Error %d\n", err);
  356. return 1;
  357. }
  358. if (instance_id == 1) {
  359. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  360. err = sam_start ();
  361. if (err != CS_OK) {
  362. fprintf (stderr, "Can't start hc. Error %d\n", err);
  363. return 1;
  364. }
  365. printf ("%s iid %d: sam_data_getsize 6\n", __FUNCTION__, instance_id);
  366. err = sam_data_getsize (&size);
  367. if (err != CS_OK) {
  368. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  369. return 1;
  370. }
  371. if (size != sizeof (saved_data2)) {
  372. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  373. return 1;
  374. }
  375. printf ("%s iid %d: sam_data_restore 5\n", __FUNCTION__, instance_id);
  376. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  377. if (err != CS_OK) {
  378. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  379. return 1;
  380. }
  381. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  382. fprintf (stderr, "Retored data are not same\n");
  383. return 1;
  384. }
  385. for (i = 0; i < sizeof (saved_data); i++) {
  386. saved_data[i] = (char)(i - 5);
  387. }
  388. printf ("%s iid %d: sam_data_store 5\n", __FUNCTION__, instance_id);
  389. err = sam_data_store (saved_data, sizeof (saved_data) - 7);
  390. if (err != CS_OK) {
  391. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  392. return 1;
  393. }
  394. exit (1);
  395. }
  396. if (instance_id == 2) {
  397. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  398. err = sam_start ();
  399. if (err != CS_OK) {
  400. fprintf (stderr, "Can't start hc. Error %d\n", err);
  401. return 1;
  402. }
  403. printf ("%s iid %d: sam_data_getsize 7\n", __FUNCTION__, instance_id);
  404. err = sam_data_getsize (&size);
  405. if (err != CS_OK) {
  406. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  407. return 1;
  408. }
  409. if (size != sizeof (saved_data2) - 7) {
  410. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  411. return 1;
  412. }
  413. printf ("%s iid %d: sam_data_restore 6\n", __FUNCTION__, instance_id);
  414. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  415. if (err != CS_OK) {
  416. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  417. return 1;
  418. }
  419. for (i = 0; i < sizeof (saved_data); i++) {
  420. saved_data[i] = (char)(i - 5);
  421. }
  422. if (memcmp (saved_data, saved_data2, sizeof (saved_data2) - 7) != 0) {
  423. fprintf (stderr, "Retored data are not same\n");
  424. return 1;
  425. }
  426. printf ("%s iid %d: sam_data_store 6\n", __FUNCTION__, instance_id);
  427. err = sam_data_store (NULL, 0);
  428. if (err != CS_OK) {
  429. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  430. return 1;
  431. }
  432. exit (1);
  433. }
  434. if (instance_id == 3) {
  435. printf ("%s iid %d: sam_data_getsize 8\n", __FUNCTION__, instance_id);
  436. err = sam_data_getsize (&size);
  437. if (err != CS_OK) {
  438. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  439. return 1;
  440. }
  441. if (size != 0) {
  442. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  443. return 1;
  444. }
  445. }
  446. return (0);
  447. }
  448. static int test5_hc_cb (void)
  449. {
  450. printf ("%s %d\n", __FUNCTION__, ++test5_hc_cb_count);
  451. sam_data_store (&test5_hc_cb_count, sizeof (test5_hc_cb_count));
  452. if (test5_hc_cb_count > 10)
  453. return 1;
  454. return 0;
  455. }
  456. /*
  457. * Test event driven healtchecking.
  458. */
  459. static int test5 (void)
  460. {
  461. cs_error_t error;
  462. unsigned int instance_id;
  463. int hc_cb_count;
  464. printf ("%s: initialize\n", __FUNCTION__);
  465. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  466. if (error != CS_OK) {
  467. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  468. return 1;
  469. }
  470. printf ("%s: register\n", __FUNCTION__);
  471. error = sam_register (&instance_id);
  472. if (error != CS_OK) {
  473. fprintf (stderr, "Can't register. Error %d\n", error);
  474. return 1;
  475. }
  476. if (instance_id == 1) {
  477. printf ("%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  478. error = sam_hc_callback_register (test5_hc_cb);
  479. if (error != CS_OK) {
  480. fprintf (stderr, "Can't register hc cb. Error %d\n", error);
  481. return 1;
  482. }
  483. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  484. error = sam_start ();
  485. if (error != CS_OK) {
  486. fprintf (stderr, "Can't start hc. Error %d\n", error);
  487. return 1;
  488. }
  489. sleep (2);
  490. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  491. return 1;
  492. }
  493. if (instance_id == 2) {
  494. error = sam_data_restore (&hc_cb_count, sizeof (hc_cb_count));
  495. if (error != CS_OK) {
  496. fprintf (stderr, "sam_data_restore should return CS_OK. Error returned %d\n", error);
  497. return 1;
  498. }
  499. if (hc_cb_count != 11) {
  500. fprintf (stderr, "%s iid %d: Premature killed. hc_cb_count should be 11 and it is %d\n",
  501. __FUNCTION__, instance_id - 1, hc_cb_count);
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. return 1;
  507. }
  508. static void test6_signal (int sig) {
  509. cs_error_t error;
  510. printf ("%s\n", __FUNCTION__);
  511. test6_sig_delivered++;
  512. if ((error = sam_data_store (&test6_sig_delivered, sizeof (test6_sig_delivered))) != CS_OK) {
  513. fprintf (stderr, "Can't store data! Error : %d\n", error);
  514. }
  515. }
  516. /*
  517. * Test warn signal set.
  518. */
  519. static int test6 (void) {
  520. cs_error_t error;
  521. unsigned int instance_id;
  522. int test6_sig_del;
  523. printf ("%s: initialize\n", __FUNCTION__);
  524. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  525. if (error != CS_OK) {
  526. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  527. return 1;
  528. }
  529. printf ("%s: register\n", __FUNCTION__);
  530. error = sam_register (&instance_id);
  531. if (error != CS_OK) {
  532. fprintf (stderr, "Can't register. Error %d\n", error);
  533. return 1;
  534. }
  535. if (instance_id == 1) {
  536. error = sam_warn_signal_set (SIGUSR1);
  537. if (error != CS_OK) {
  538. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  539. return 1;
  540. }
  541. signal (SIGUSR1, test6_signal);
  542. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  543. error = sam_start ();
  544. if (error != CS_OK) {
  545. fprintf (stderr, "Can't start hc. Error %d\n", error);
  546. return 1;
  547. }
  548. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  549. sleep (1);
  550. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  551. error = sam_hc_send ();
  552. if (error != CS_OK) {
  553. fprintf (stderr, "Can't send hc. Error %d\n", error);
  554. return 1;
  555. }
  556. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  557. while (!test6_sig_delivered) {
  558. sleep (1);
  559. }
  560. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  561. sleep (3);
  562. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  563. return (1);
  564. }
  565. if (instance_id == 2) {
  566. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  567. if (error != CS_OK) {
  568. fprintf (stderr, "Can't restore data. Error %d\n", error);
  569. return 1;
  570. }
  571. if (test6_sig_del != 1) {
  572. fprintf (stderr, "Previous test failed. Signal was not delivered\n");
  573. return 1;
  574. }
  575. error = sam_warn_signal_set (SIGKILL);
  576. if (error != CS_OK) {
  577. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  578. return 1;
  579. }
  580. signal (SIGUSR1, test6_signal);
  581. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  582. error = sam_start ();
  583. if (error != CS_OK) {
  584. fprintf (stderr, "Can't start hc. Error %d\n", error);
  585. return 1;
  586. }
  587. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  588. sleep (1);
  589. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  590. error = sam_hc_send ();
  591. if (error != CS_OK) {
  592. fprintf (stderr, "Can't send hc. Error %d\n", error);
  593. return 1;
  594. }
  595. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  596. while (!test6_sig_delivered) {
  597. sleep (1);
  598. }
  599. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  600. return (1);
  601. }
  602. if (instance_id == 3) {
  603. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  604. if (error != CS_OK) {
  605. fprintf (stderr, "Can't restore data. Error %d\n", error);
  606. return 1;
  607. }
  608. if (test6_sig_del != 1) {
  609. fprintf (stderr, "Previous test failed. Signal WAS delivered\n");
  610. return 1;
  611. }
  612. return (0);
  613. }
  614. return 1;
  615. }
  616. int main(int argc, char *argv[])
  617. {
  618. pid_t pid;
  619. int err;
  620. int stat;
  621. int all_passed = 1;
  622. pid = fork ();
  623. if (pid == -1) {
  624. fprintf (stderr, "Can't fork\n");
  625. return 1;
  626. }
  627. if (pid == 0) {
  628. err = test1 ();
  629. sam_finalize ();
  630. return err;
  631. }
  632. waitpid (pid, &stat, 0);
  633. fprintf (stderr, "test1 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  634. if (WEXITSTATUS (stat) != 0)
  635. all_passed = 0;
  636. pid = fork ();
  637. if (pid == -1) {
  638. fprintf (stderr, "Can't fork\n");
  639. return 1;
  640. }
  641. if (pid == 0) {
  642. err = test2 ();
  643. sam_finalize ();
  644. return (err);
  645. }
  646. waitpid (pid, &stat, 0);
  647. fprintf (stderr, "test2 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  648. if (WEXITSTATUS (stat) != 0)
  649. all_passed = 0;
  650. pid = fork ();
  651. if (pid == -1) {
  652. fprintf (stderr, "Can't fork\n");
  653. return 1;
  654. }
  655. if (pid == 0) {
  656. err = test3 ();
  657. sam_finalize ();
  658. return (err);
  659. }
  660. waitpid (pid, &stat, 0);
  661. fprintf (stderr, "test3 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  662. if (WEXITSTATUS (stat) != 0)
  663. all_passed = 0;
  664. pid = fork ();
  665. if (pid == -1) {
  666. fprintf (stderr, "Can't fork\n");
  667. return 1;
  668. }
  669. if (pid == 0) {
  670. err = test4 ();
  671. sam_finalize ();
  672. return (err);
  673. }
  674. waitpid (pid, &stat, 0);
  675. fprintf (stderr, "test4 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  676. if (WEXITSTATUS (stat) != 0)
  677. all_passed = 0;
  678. pid = fork ();
  679. if (pid == -1) {
  680. fprintf (stderr, "Can't fork\n");
  681. return 1;
  682. }
  683. if (pid == 0) {
  684. err = test5 ();
  685. sam_finalize ();
  686. return (err);
  687. }
  688. waitpid (pid, &stat, 0);
  689. fprintf (stderr, "test5 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  690. if (WEXITSTATUS (stat) != 0)
  691. all_passed = 0;
  692. pid = fork ();
  693. if (pid == -1) {
  694. fprintf (stderr, "Can't fork\n");
  695. return 1;
  696. }
  697. if (pid == 0) {
  698. err = test6 ();
  699. sam_finalize ();
  700. return (err);
  701. }
  702. waitpid (pid, &stat, 0);
  703. fprintf (stderr, "test6 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  704. if (WEXITSTATUS (stat) != 0)
  705. all_passed = 0;
  706. if (all_passed)
  707. fprintf (stderr, "All tests passed\n");
  708. return (all_passed ? 0 : 1);
  709. }