robbiew | a3cc71f | 2003-02-18 20:53:50 +0000 | [diff] [blame] | 1 | /* Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify it |
| 4 | * under the terms of version 2 of the GNU General Public License as |
| 5 | * published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it would be useful, but |
| 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | * |
| 11 | * 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] | 12 | * with this program; if not, write the Free Software Foundation, Inc., |
| 13 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 14 | * |
| 15 | */ |
| 16 | /************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 17 | * |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 18 | * TEST IDENTIFIER : swapon02 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 19 | * |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 20 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 22 | * TEST TITLE : Test checking for basic error conditions |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 23 | * for swapon(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
| 25 | * TEST CASE TOTAL : 5 |
| 26 | * |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 27 | * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com> and |
| 28 | * Ricardo Salveti de Araujo <rsalveti@linux.vnet.ibm.com> for |
| 29 | * the EBUSY test case |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 31 | * SIGNALS |
robbiew | 6b01d68 | 2003-04-28 20:27:26 +0000 | [diff] [blame] | 32 | * Uses SIGUSR1 to pause before test if option set. |
| 33 | * (See the parse_opts(3) man page). |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 34 | * |
| 35 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 36 | * This test case checks whether swapon(2) system call returns |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 37 | * 1. ENOENT when the path does not exist |
| 38 | * 2. EINVAL when the path exists but is invalid |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 39 | * 3. EPERM when user is not a superuser |
| 40 | * 4. EBUSY when the specified path is already being used as a swap area |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | * $ |
robbiew | 6b01d68 | 2003-04-28 20:27:26 +0000 | [diff] [blame] | 42 | * Setup: |
| 43 | * Setup signal handling. |
| 44 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 45 | * 1. For testing error on invalid user, change the effective uid |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 46 | * |
robbiew | 6b01d68 | 2003-04-28 20:27:26 +0000 | [diff] [blame] | 47 | * Test: |
| 48 | * Loop if the proper options are given. |
| 49 | * Execute system call. |
| 50 | * Check return code, if system call fails with errno == expected errno |
| 51 | * Issue syscall passed with expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 52 | * Otherwise, |
robbiew | 6b01d68 | 2003-04-28 20:27:26 +0000 | [diff] [blame] | 53 | * Issue syscall failed to produce expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 54 | * |
robbiew | 6b01d68 | 2003-04-28 20:27:26 +0000 | [diff] [blame] | 55 | * Cleanup: |
| 56 | * Do cleanup for the test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | * $ |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 58 | * USAGE: <for command-line> |
| 59 | * swapon02 [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p] |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 60 | * where |
robbiew | 6b01d68 | 2003-04-28 20:27:26 +0000 | [diff] [blame] | 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 | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 68 | *RESTRICTIONS: |
| 69 | *Incompatible with kernel versions below 2.1.35. |
| 70 | *Incompatible if MAX_SWAPFILES definition in later kernels is changed |
| 71 | * -c option can't be used. |
| 72 | * |
| 73 | *CHANGES: |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 74 | * 2005/01/01 Add extra check to stop test if swap file is on tmpfs |
| 75 | * -Ricky Ng-Adam (rngadam@yahoo.com) |
robbiew | ab94156 | 2003-01-09 15:30:18 +0000 | [diff] [blame] | 76 | * 01/02/03 Added fork to handle SIGSEGV generated when the intentional EPERM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 77 | * error for hitting MAX_SWAPFILES is hit. |
robbiew | ab94156 | 2003-01-09 15:30:18 +0000 | [diff] [blame] | 78 | * -Robbie Williamson <robbiew@us.ibm.com> |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 79 | * 05/17/07 Fixing the test description and added the EBUSY test case |
| 80 | * - Ricardo Salveti de Araujo (rsalveti@linux.vnet.ibm.com) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 81 | * 08/16/07 Removed the the 'more than MAX_SWAPFILES swapfiles in use' test |
| 82 | * case as now we have a separated file only for this test. |
| 83 | * - Ricardo Salveti de Araujo (rsalveti@linux.vnet.ibm.com) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 84 | *****************************************************************************/ |
| 85 | |
| 86 | #include <unistd.h> |
| 87 | #include <errno.h> |
| 88 | #include <stdlib.h> |
| 89 | #include <sys/types.h> |
| 90 | #include <sys/wait.h> |
| 91 | #include <sys/stat.h> |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 92 | #include <fcntl.h> |
| 93 | #include <pwd.h> |
| 94 | #include <string.h> |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 95 | #include <sys/utsname.h> |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 96 | #include <signal.h> |
| 97 | #include "test.h" |
| 98 | #include "usctest.h" |
yaberauneya | 77772ef | 2009-10-17 06:53:58 +0000 | [diff] [blame] | 99 | #include "config.h" |
yaberauneya | a2a05e3 | 2009-11-26 14:19:32 +0000 | [diff] [blame] | 100 | #include "linux_syscall_numbers.h" |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 101 | #include "swaponoff.h" |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 102 | |
| 103 | static void setup(); |
| 104 | static void cleanup(); |
| 105 | static int setup01(); |
| 106 | static int cleanup01(); |
| 107 | static int setup02(); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 108 | static int setup03(); |
| 109 | static int cleanup03(); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 110 | void handler(int); |
| 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | char *TCID = "swapon02"; /* Test program identifier. */ |
| 113 | int TST_TOTAL = 4; /* Total number of test cases. */ |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 114 | char nobody_uid[] = "nobody"; |
| 115 | struct passwd *ltpuser; |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 116 | |
| 117 | struct utsname uval; |
| 118 | char *kmachine; |
| 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | static int exp_enos[] = { EPERM, EINVAL, ENOENT, EBUSY, 0 }; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 121 | |
| 122 | static struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | char *err_desc; /* error description */ |
| 124 | int exp_errno; /* expected error number */ |
| 125 | char *exp_errval; /* Expected errorvalue string */ |
| 126 | char *path; /* path to swapon */ |
| 127 | int (*setupfunc) (); /* Test setup function */ |
| 128 | int (*cleanfunc) (); /* Test cleanup function */ |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 129 | } testcase[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | { |
| 131 | "Path does not exist", ENOENT, "ENOENT", "./abcd", NULL, NULL}, { |
| 132 | "Invalid path", EINVAL, "EINVAL", "./nofile", setup02, NULL}, { |
| 133 | "Permission denied", EPERM, "EPERM", "./swapfile01", |
| 134 | setup01, cleanup01}, { |
| 135 | "The specified path is already being used as a swap area", |
| 136 | EBUSY, "EBUSY", "./alreadyused", setup03, cleanup03} |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | int main(int ac, char **av) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 140 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 141 | int lc, i; |
| 142 | char *msg; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 143 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 144 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 145 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 146 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | uname(&uval); |
| 148 | kmachine = uval.machine; |
| 149 | setup(); |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 150 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 152 | Tst_count = 0; |
| 153 | for (i = 0; i < TST_TOTAL; i++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | /* do the setup if the test have one */ |
| 156 | if (testcase[i].setupfunc |
| 157 | && testcase[i].setupfunc() == -1) { |
| 158 | tst_resm(TWARN, |
| 159 | "Failed to setup test %d." |
| 160 | " Skipping test", i); |
| 161 | continue; |
| 162 | } else { |
| 163 | /* run the test */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 164 | TEST(ltp_syscall(__NR_swapon, |
| 165 | testcase[i].path, 0)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | } |
| 167 | /* do the clean if the test have one */ |
| 168 | if (testcase[i].cleanfunc |
| 169 | && testcase[i].cleanfunc() == -1) { |
| 170 | tst_brkm(TBROK, cleanup, |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 171 | "Cleanup failed, quitting the test"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | } |
| 173 | /* check return code */ |
| 174 | if ((TEST_RETURN == -1) |
| 175 | && (TEST_ERRNO == testcase[i].exp_errno)) { |
| 176 | tst_resm(TPASS, |
| 177 | "swapon(2) expected failure;" |
| 178 | " Got errno - %s : %s", |
| 179 | testcase[i].exp_errval, |
| 180 | testcase[i].err_desc); |
| 181 | } else { |
| 182 | tst_resm(TFAIL, "swapon(2) failed to produce" |
| 183 | " expected error: %d, errno" |
| 184 | ": %s and got %d. " |
| 185 | " System reboot after" |
| 186 | " execution of LTP" |
| 187 | " test suite is" |
| 188 | " recommended.", |
| 189 | testcase[i].exp_errno, |
| 190 | testcase[i].exp_errval, TEST_ERRNO); |
| 191 | /*If swapfile is turned on, turn it off */ |
| 192 | if (TEST_RETURN == 0) { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 193 | if (ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | (__NR_swapoff, |
| 195 | testcase[i].path) != 0) { |
| 196 | tst_resm(TWARN, |
| 197 | "Failed to" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | " turn off swapfile" |
| 199 | " swapfile. System" |
| 200 | " reboot after" |
| 201 | " execution of LTP" |
| 202 | " test suite is" |
| 203 | " recommended."); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | TEST_ERROR_LOG(TEST_ERRNO); |
| 208 | } /*End of TEST LOOPS */ |
| 209 | } /*End of TEST LOOPING */ |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 210 | |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 211 | cleanup(); |
| 212 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | } /*End of main */ |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 214 | |
| 215 | /* |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 216 | * setup01() - This function creates the file and sets the user as nobody |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 217 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | int setup01() |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 219 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | int pagesize = getpagesize(); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 221 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | /*create file */ |
| 223 | if ((strncmp(kmachine, "ia64", 4)) == 0) { |
| 224 | if (system |
| 225 | ("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 > tmpfile" |
| 226 | " 2>&1") != 0) { |
| 227 | tst_brkm(TBROK, cleanup, |
| 228 | "Failed to create file for swap"); |
| 229 | } |
| 230 | } else if (pagesize == 65536) { |
| 231 | if (system |
| 232 | ("dd if=/dev/zero of=swapfile01 bs=1048 count=655 > tmpfile" |
| 233 | " 2>&1") != 0) { |
| 234 | tst_brkm(TBROK, cleanup, |
| 235 | "Failed to create file for swap"); |
| 236 | } |
| 237 | } else { |
| 238 | if (system |
| 239 | ("dd if=/dev/zero of=swapfile01 bs=1048 count=40 > tmpfile" |
| 240 | " 2>&1") != 0) { |
| 241 | tst_brkm(TBROK, cleanup, |
| 242 | "Failed to create file for swap"); |
| 243 | } |
| 244 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 245 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 246 | /* make above file a swap file */ |
| 247 | if (system("mkswap swapfile01 > tmpfile 2>&1") != 0) { |
| 248 | tst_brkm(TBROK, cleanup, "Failed to make swapfile"); |
| 249 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 250 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
| 252 | tst_resm(TWARN, "\"nobody\" user not present. skipping test"); |
| 253 | return -1; |
| 254 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 255 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 256 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 257 | tst_resm(TWARN, "seteuid failed to " |
| 258 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 259 | perror("seteuid"); |
| 260 | return -1; |
| 261 | } |
| 262 | return 0; /* user switched to nobody */ |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 266 | * cleanup01() - switch back to user root and gives swapoff to the swap file |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 267 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 268 | int cleanup01() |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 269 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 270 | if (seteuid(0) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 271 | tst_brkm(TBROK | TERRNO, cleanup, |
| 272 | "seteuid failed to set uid to root"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 273 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 274 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 275 | return 0; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 278 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 279 | * setup02() - create a normal file, to be used with swapon |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 280 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | int setup02() |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 282 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | int fd; |
| 284 | fd = creat("nofile", S_IRWXU); |
| 285 | if (fd == -1) |
| 286 | tst_resm(TWARN, "Failed to create temporary file"); |
| 287 | close(fd); |
| 288 | return 0; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 292 | * setup03() - This function creates the swap file and turn it on |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 293 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 294 | int setup03() |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 295 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 296 | int pagesize = getpagesize(); |
| 297 | int res = 0; |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 298 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 299 | /*create file */ |
| 300 | if ((strncmp(kmachine, "ia64", 4)) == 0) { |
| 301 | if (system |
| 302 | ("dd if=/dev/zero of=alreadyused bs=1024 count=65536 > tmpfile" |
| 303 | " 2>&1") != 0) { |
| 304 | tst_brkm(TBROK, cleanup, |
| 305 | "Failed to create file for swap"); |
| 306 | } |
| 307 | } else if (pagesize == 65536) { |
| 308 | if (system |
| 309 | ("dd if=/dev/zero of=alreadyused bs=1048 count=655 > tmpfile" |
| 310 | " 2>&1") != 0) { |
| 311 | tst_brkm(TBROK, cleanup, |
| 312 | "Failed to create file for swap"); |
| 313 | } |
| 314 | } else { |
| 315 | if (system |
| 316 | ("dd if=/dev/zero of=alreadyused bs=1048 count=40 > tmpfile" |
| 317 | " 2>&1") != 0) { |
| 318 | tst_brkm(TBROK, cleanup, |
| 319 | "Failed to create file for swap"); |
| 320 | } |
| 321 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 322 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 323 | /* make above file a swap file */ |
| 324 | if (system("mkswap alreadyused > tmpfile 2>&1") != 0) { |
| 325 | tst_brkm(TBROK, cleanup, "Failed to make swapfile"); |
| 326 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 327 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 328 | /* turn on the swap file */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 329 | res = ltp_syscall(__NR_swapon, "alreadyused", 0); |
| 330 | if (res != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 331 | tst_resm(TWARN, "Failed swapon for file alreadyused" |
| 332 | " returned %d", res); |
| 333 | return -1; |
| 334 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 335 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 336 | return 0; |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 340 | * cleanup03() - clearing the turned on swap file |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 341 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 342 | int cleanup03() |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 343 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 344 | /* give swapoff to the test swap file */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 345 | if (ltp_syscall(__NR_swapoff, "alreadyused") != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 346 | tst_resm(TWARN, "Failed to turn off swap files. system" |
| 347 | " reboot after execution of LTP test" |
| 348 | " suite is recommended"); |
| 349 | return -1; |
| 350 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 351 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 352 | return 0; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* setup() - performs all ONE TIME setup for this test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 356 | void setup() |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 357 | { |
mreed10 | ed3af43 | 2006-09-15 20:17:23 +0000 | [diff] [blame] | 358 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 359 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 360 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 361 | /* set the expected errnos... */ |
| 362 | TEST_EXP_ENOS(exp_enos); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 363 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 364 | /* Check whether we are root */ |
| 365 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 366 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 367 | } |
| 368 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 369 | tst_tmpdir(); |
robbiew | a3cc71f | 2003-02-18 20:53:50 +0000 | [diff] [blame] | 370 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 371 | if (tst_is_cwd_tmpfs()) { |
| 372 | tst_brkm(TCONF, cleanup, |
| 373 | "Cannot do swapon on a file located on a tmpfs filesystem"); |
| 374 | } |
| 375 | |
| 376 | if (tst_is_cwd_nfs()) { |
| 377 | tst_brkm(TCONF, cleanup, |
| 378 | "Cannot do swapon on a file located on a nfs filesystem"); |
| 379 | } |
| 380 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 381 | TEST_PAUSE; |
| 382 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 383 | } |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * cleanup() - Performs one time cleanup for this test at |
| 387 | * completion or premature exit |
| 388 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 389 | void cleanup() |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 390 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 391 | /* |
| 392 | * print timing stats if that option was specified. |
| 393 | * print errno log if that option was specified. |
| 394 | */ |
| 395 | TEST_CLEANUP; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 396 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 397 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 398 | } |