/*************************************************************** * cleanup() - performs all ONE TIME cleanup for this test at * completion or premature exit. ***************************************************************/ void cleanup() { /* * print timing stats if that option was specified. * print errno log if that option was specified. */ TEST_CLEANUP; /* Remove any remaining swap files */ clean_swap(); tst_rmdir(); }
int main(int ac, char **av) { int lc; tst_parse_opts(ac, av, NULL, NULL); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; if (setup_swap() < 0) { clean_swap(); tst_brkm(TBROK, cleanup, "Setup failed, quitting the test"); } TEST(ltp_syscall(__NR_swapon, swap_testfiles[0].filename, 0)); if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) { tst_resm(TPASS, "swapon(2) got expected failure (%d),", expected_errno); } else if (TEST_RETURN < 0) { tst_resm(TFAIL | TTERRNO, "swapon(2) failed to produce expected error " "(%d). System reboot recommended.", expected_errno); } else { /* Probably the system supports MAX_SWAPFILES > 30, * let's try with MAX_SWAPFILES == 32 */ /* Call swapon sys call once again for 32 * now we can't receive an error */ TEST(ltp_syscall (__NR_swapon, swap_testfiles[1].filename, 0)); /* Check return code (now we're expecting success) */ if (TEST_RETURN < 0) { tst_resm(TFAIL | TTERRNO, "swapon(2) got an unexpected failure"); } else { /* Call swapon sys call once again for 33 * now we have to receive an error */ TEST(ltp_syscall (__NR_swapon, swap_testfiles[2].filename, 0)); /* Check return code (should be an error) */ if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) { tst_resm(TPASS, "swapon(2) got expected failure;" " Got errno = %d, probably your" " MAX_SWAPFILES is 32", expected_errno); } else { tst_resm(TFAIL, "swapon(2) failed to produce" " expected error: %d, got %s." " System reboot after execution of LTP" " test suite is recommended.", expected_errno, strerror(TEST_ERRNO)); } } } if (clean_swap() < 0) tst_brkm(TBROK, cleanup, "Cleanup failed, quitting the test"); } cleanup(); tst_exit(); }
static void cleanup(void) { clean_swap(); tst_rmdir(); }
/*************************************************************** * setup_swap() - Create 33 and activate 30 swapfiles. ***************************************************************/ int setup_swap() { int j, fd; /*j is loop counter, fd is file descriptor */ int pid; /* used for fork */ int status; /* used for fork */ int res = 0, pagesize = getpagesize(); int bs, count; char filename[15]; /* array to store new filename */ char buf[BUFSIZ + 1]; /* temp buffer for reading /proc/swaps */ /* Find out how many swapfiles (1 line per entry) already exist */ swapfiles = 0; if (seteuid(0) < 0) { tst_brkm(TFAIL | TERRNO, cleanup, "Failed to call seteuid"); } /* This includes the first (header) line */ if ((fd = open("/proc/swaps", O_RDONLY)) == -1) { tst_brkm(TFAIL | TERRNO, cleanup, "Failed to find out existing number of swap files"); } do { char *p = buf; res = read(fd, buf, BUFSIZ); if (res < 0) { tst_brkm(TFAIL | TERRNO, cleanup, "Failed to find out existing number of swap files"); } buf[res] = '\0'; while ((p = strchr(p, '\n'))) { p++; swapfiles++; } } while (BUFSIZ <= res); close(fd); if (swapfiles) swapfiles--; /* don't count the /proc/swaps header */ if (swapfiles < 0) { tst_brkm(TFAIL, cleanup, "Failed to find existing number of swapfiles"); } /* Determine how many more files are to be created */ swapfiles = MAX_SWAPFILES - swapfiles; if (swapfiles > MAX_SWAPFILES) { swapfiles = MAX_SWAPFILES; } /* args for dd */ if ((strncmp(kmachine, "ia64", 4)) == 0) { bs = 1024; count = 1024; } else if (pagesize == 65536) { bs = 1048; count = 655; } else { bs = 1048; count = 40; } pid = FORK_OR_VFORK(); if (pid == 0) { /*create and turn on remaining swapfiles */ for (j = 0; j < swapfiles; j++) { /* prepare filename for the iteration */ if (sprintf(filename, "swapfile%02d", j + 2) < 0) { tst_brkm(TFAIL | TERRNO, cleanup, "sprintf() failed to create filename"); } /* Create the swapfile */ if (create_swapfile(filename, bs, count) < 0) { tst_brkm(TFAIL | TERRNO, cleanup, "Failed to create swapfile for the test"); } /* turn on the swap file */ if ((res = syscall(__NR_swapon, filename, 0)) != 0) { tst_brkm(TFAIL | TERRNO, cleanup, "Failed swapon for file %s", filename); /* must cleanup already swapon files */ clean_swap(); exit(1); } } tst_exit(); } else waitpid(pid, &status, 0); if (WEXITSTATUS(status)) { tst_resm(TFAIL, "Failed to setup swaps"); exit(1); } /* Create all needed extra swapfiles for testing */ for (j = 0; j < testfiles; j++) { if (create_swapfile(swap_testfiles[j].filename, bs, count) < 0) { tst_resm(TWARN, "Failed to create swapfiles for the test"); exit(1); } } return 0; }
int main(int ac, char **av) { int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ /*************************************************************** * parse standard options ***************************************************************/ if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); /*************************************************************** * perform global setup for test ***************************************************************/ uname(&uval); kmachine = uval.machine; setup(); /*************************************************************** * check looping state if -i option given ***************************************************************/ for (lc = 0; TEST_LOOPING(lc); lc++) { Tst_count = 0; /* do the test setup */ if (setup_swap() < 0) { clean_swap(); tst_brkm(TBROK, cleanup, "Setup failed, quitting the test"); } /* Call swapon sys call for the first time */ TEST(syscall(__NR_swapon,swap_testfiles[0].filename, 0)); /* Check return code */ if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) { tst_resm(TPASS, "swapon(2) got expected failure;" " Got errno - %d, probably your" " MAX_SWAPFILES is 30", expected_errno); } else if (TEST_RETURN < 0) { tst_resm(TFAIL, "swapon(2) failed to produce" " expected error: %d, got %d (%s)." " System reboot after execution of LTP" " test suite is recommended.", expected_errno, TEST_ERRNO, strerror(TEST_ERRNO)); } else { /* Probably the system supports MAX_SWAPFILES > 30, * let's try with MAX_SWAPFILES == 32 */ /* Call swapon sys call once again for 32 * now we can't receive an error */ TEST(syscall(__NR_swapon, swap_testfiles[1].filename, 0)); /* Check return code (now we're expecting success) */ if (TEST_RETURN < 0) { tst_resm(TFAIL, "swapon(2) got an unexpected failure;" " Got errno = %d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); } else { /* Call swapon sys call once again for 33 * now we have to receive an error */ TEST(syscall(__NR_swapon, swap_testfiles[2].filename, 0)); /* Check return code (should be an error) */ if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) { tst_resm(TPASS, "swapon(2) got expected failure;" " Got errno - %d, probably your" " MAX_SWAPFILES is 32", expected_errno); } else { tst_resm(TFAIL, "swapon(2) failed to produce" " expected error: %d, got %s." " System reboot after execution of LTP" " test suite is recommended.", expected_errno, strerror(TEST_ERRNO)); } } } /* do the clean */ if (clean_swap() < 0) tst_brkm(TBROK, cleanup, "Cleanup failed, quitting the test"); TEST_ERROR_LOG(TEST_ERRNO); } /* End of TEST LOOPING */ /*************************************************************** * cleanup and exit ***************************************************************/ cleanup(); return (0); } /* End of main */