testsam.c 18 KB

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