robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : swapoff02 |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 20 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 22 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 24 | * TEST TITLE : Test checking for basic error conditions |
| 25 | * for swapoff(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
| 27 | * TEST CASE TOTAL : 3 |
| 28 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 29 | * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 31 | * SIGNALS |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 32 | * Uses SIGUSR1 to pause before test if option set. |
| 33 | * (See the parse_opts(3) man page). |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 34 | * |
| 35 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 36 | * This test case checks whether swapoff(2) system call returns |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 37 | * 1. EINVAL when the path does not exist |
| 38 | * 2. ENOENT when the path exists but is invalid |
| 39 | * 3. EPERM when user is not a superuser |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 40 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 41 | * Setup: |
| 42 | * Setup signal handling. |
| 43 | * Pause for SIGUSR1 if option specified. |
| 44 | * 1. For testing error on invalid user, change the effective uid |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * $ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 46 | * Test: |
| 47 | * Loop if the proper options are given. |
| 48 | * Execute system call. |
| 49 | * Check return code, if system call fails with errno == expected errno |
| 50 | * Issue syscall passed with expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 51 | * Otherwise, |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 52 | * Issue syscall failed to produce expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 53 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 54 | * Cleanup: |
| 55 | * Do cleanup for the test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 56 | * $ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 57 | * USAGE: <for command-line> |
| 58 | * swapoff02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p] |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | * where |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 60 | * -c n : Run n copies simultaneously |
| 61 | * -e : Turn on errno logging. |
| 62 | * -i n : Execute test n times. |
| 63 | * -I x : Execute test for x seconds. |
| 64 | * -p : Pause for SIGUSR1 before starting |
| 65 | * -P x : Pause for x seconds between iterations. |
| 66 | * -t : Turn on syscall timing. |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 67 | * |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 68 | *RESTRICTIONS: |
| 69 | *Incompatible with kernel versions below 2.1.35. |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 70 | * |
| 71 | *CHANGES: |
| 72 | * 2005/01/01 Add extra check to stop test if insufficient disk space in dir |
| 73 | * -Ricky Ng-Adam (rngadam@yahoo.com) |
| 74 | * 2005/01/01 Add extra check to stop test if swap file is on tmpfs |
| 75 | * -Ricky Ng-Adam (rngadam@yahoo.com) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 76 | *****************************************************************************/ |
| 77 | |
| 78 | #include <unistd.h> |
| 79 | #include <errno.h> |
| 80 | #include <sys/types.h> |
| 81 | #include <sys/stat.h> |
| 82 | #include <fcntl.h> |
| 83 | #include <pwd.h> |
| 84 | #include <string.h> |
| 85 | #include "test.h" |
| 86 | #include "usctest.h" |
| 87 | #include <stdlib.h> |
yaberauneya | 77772ef | 2009-10-17 06:53:58 +0000 | [diff] [blame] | 88 | #include "config.h" |
yaberauneya | a2a05e3 | 2009-11-26 14:19:32 +0000 | [diff] [blame] | 89 | #include "linux_syscall_numbers.h" |
| 90 | #include "swaponoff.h" |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 91 | |
| 92 | static void setup(); |
| 93 | static void cleanup(); |
| 94 | static int setup01(); |
| 95 | static int cleanup01(); |
| 96 | static int setup02(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | char *TCID = "swapoff02"; /* Test program identifier. */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 99 | int TST_TOTAL = 3; /* Total number of test cases. */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 100 | char nobody_uid[] = "nobody"; |
| 101 | struct passwd *ltpuser; |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 102 | int need_swapfile_cleanup = 0; /* attempt to swapoff in cleanup */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | static int exp_enos[] = { EPERM, EINVAL, ENOENT, 0 }; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 105 | |
| 106 | static struct test_case_t { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 107 | char *err_desc; /* error description */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | int exp_errno; /* expected error number */ |
| 109 | char *exp_errval; /* Expected errorvalue string */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 110 | char *path; /* path for swapon */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | int (*setupfunc) (); /* Test setup function */ |
| 112 | int (*cleanfunc) (); /* Test cleanup function */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 113 | } testcase[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | { |
| 115 | "path does not exist", ENOENT, "ENOENT", "./abcd", NULL, NULL}, { |
| 116 | "Invalid path", EINVAL, "EINVAL ", "./nofile", setup02, NULL}, { |
| 117 | "Permission denied", EPERM, "EPERM ", "./swapfile01", |
| 118 | setup01, cleanup01} |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | int main(int ac, char **av) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 122 | { |
| 123 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 124 | int lc, i; |
| 125 | char *msg; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 126 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 127 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 128 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 129 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 130 | setup(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 131 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 132 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 133 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 134 | Tst_count = 0; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 135 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | for (i = 0; i < TST_TOTAL; i++) { |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 137 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | if (testcase[i].setupfunc && |
| 139 | testcase[i].setupfunc() == -1) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 140 | tst_resm(TWARN, "Failed to setup test %d." |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | " Skipping test", i); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 142 | continue; |
| 143 | } else { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 144 | TEST(ltp_syscall(__NR_swapoff, |
| 145 | testcase[i].path)); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 146 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 147 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | if (testcase[i].cleanfunc && |
| 149 | testcase[i].cleanfunc() == -1) { |
| 150 | tst_brkm(TBROK, cleanup, "cleanup failed," |
| 151 | " quitting the test"); |
| 152 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 153 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 154 | /* check return code */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | if ((TEST_RETURN == -1) |
| 156 | && (TEST_ERRNO == testcase[i].exp_errno)) { |
| 157 | tst_resm(TPASS, |
| 158 | "swapoff(2) expected failure;" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | " Got errno - %s : %s", |
| 160 | testcase[i].exp_errval, |
| 161 | testcase[i].err_desc); |
| 162 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 163 | } else { |
| 164 | tst_resm(TFAIL, "swapoff(2) failed to produce" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | " expected error; %d, errno" |
| 166 | ": %s and got %d", |
| 167 | testcase[i].exp_errno, |
| 168 | testcase[i].exp_errval, TEST_ERRNO); |
| 169 | |
| 170 | if ((TEST_RETURN == 0) && (i == 2)) { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 171 | if (ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | (__NR_swapon, "./swapfile01", |
| 173 | 0) != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 174 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | " Failed to turn on" |
| 176 | " swap file"); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 177 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 178 | } |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 179 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 180 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 181 | TEST_ERROR_LOG(TEST_ERRNO); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | } /*End of TEST LOOPS */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 183 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 184 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | /*Clean up and exit */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 186 | cleanup(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 187 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 188 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | } /*End of main */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * setup01() - This function sets the user as nobody |
| 193 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | int setup01() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 195 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 197 | tst_resm(TWARN, "\"nobody\" user not present. skipping test"); |
| 198 | return -1; |
| 199 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 200 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 201 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 202 | tst_resm(TWARN, "seteuid failed to " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | "to set the effective uid to %d", ltpuser->pw_uid); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 204 | perror("seteuid"); |
| 205 | return -1; |
| 206 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 207 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 208 | return 0; /* user switched to nobody */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * cleanup01() - switch back to user root |
| 213 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 214 | int cleanup01() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 215 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | if (seteuid(0) == -1) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 217 | tst_brkm(TBROK, cleanup, "seteuid failed to set uid to root"); |
| 218 | perror("seteuid"); |
| 219 | return -1; |
| 220 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 221 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 222 | return 0; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 223 | } |
| 224 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | int setup02() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 226 | { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 227 | int fd; |
| 228 | fd = creat("nofile", S_IRWXU); |
| 229 | if (fd == -1) |
| 230 | tst_resm(TWARN, "Failed to create temporary file"); |
| 231 | return 0; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* setup() - performs all ONE TIME setup for this test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | void setup() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 236 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 237 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 238 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 239 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 240 | /* set the expected errnos... */ |
| 241 | TEST_EXP_ENOS(exp_enos); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 242 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 243 | /* Check whether we are root */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 244 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 245 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 246 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 247 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 248 | TEST_PAUSE; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 249 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 250 | tst_tmpdir(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 251 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | if (tst_is_cwd_tmpfs()) { |
| 253 | tst_brkm(TCONF, cleanup, |
| 254 | "Cannot do swapon on a file located on a tmpfs filesystem"); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 255 | } |
| 256 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | if (tst_is_cwd_nfs()) { |
| 258 | tst_brkm(TCONF, cleanup, |
| 259 | "Cannot do swapon on a file located on a nfs filesystem"); |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 260 | } |
| 261 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 262 | if (!tst_cwd_has_free(65536)) { |
| 263 | tst_brkm(TBROK, cleanup, |
| 264 | "Insufficient disk space to create swap file"); |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 265 | } |
| 266 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 267 | /*create file */ |
| 268 | if (system |
| 269 | ("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 > tmpfile" |
| 270 | " 2>&1") != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 271 | tst_brkm(TBROK, cleanup, "Failed to create file for swap"); |
| 272 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 273 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 274 | /* make above file a swap file */ |
| 275 | if (system("mkswap ./swapfile01 > tmpfile 2>&1") != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 276 | tst_brkm(TBROK, cleanup, "Failed to make swapfile"); |
| 277 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 278 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 279 | if (ltp_syscall(__NR_swapon, "./swapfile01", 0) != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 280 | tst_brkm(TBROK, cleanup, "Failed to turn on the swap file." |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | " skipping the test iteration"); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | need_swapfile_cleanup = 1; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 285 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 286 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * cleanup() - Performs one time cleanup for this test at |
| 290 | * completion or premature exit |
| 291 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 292 | void cleanup() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 293 | { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 294 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 295 | * print timing stats if that option was specified. |
| 296 | * print errno log if that option was specified. |
| 297 | */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 298 | TEST_CLEANUP; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 299 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 300 | if (need_swapfile_cleanup |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 301 | && (ltp_syscall(__NR_swapoff, "./swapfile01") != 0)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 302 | tst_resm(TWARN, |
| 303 | " Failed to turn off swap file. System reboot" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 304 | " after execution of LTP test suite is" |
| 305 | " recommended."); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 306 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 307 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 308 | tst_rmdir(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 309 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 310 | } |