subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2007 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 18 | * |
| 19 | * NAME |
| 20 | * swapon03.c |
| 21 | * |
| 22 | * DESCRIPTION |
| 23 | * This test case checks whether swapon(2) system call returns: |
| 24 | * - EPERM when there are more than MAX_SWAPFILES already in use. |
| 25 | * |
| 26 | * Setup: |
| 27 | * Setup signal handling. |
| 28 | * Pause for SIGUSR1 if option specified. |
| 29 | * Create MAX_SWAPFILES - 2 (to support latest kernels) swapfiles |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 30 | * |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 31 | * Test: |
| 32 | * Loop if the proper options are given. |
| 33 | * Execute system call. |
| 34 | * Check return code, if system call fails with errno == expected errno |
| 35 | * Issue syscall passed with expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 36 | * Otherwise, |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 37 | * Issue syscall failed to produce expected errno |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 38 | * |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 39 | * Cleanup: |
| 40 | * Do cleanup for the test. |
| 41 | * |
| 42 | * USAGE: <for command-line> |
| 43 | * swapon03 [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p] |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 44 | * where |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 45 | * -e : Turn on errno logging. |
| 46 | * -i n : Execute test n times. |
| 47 | * -I x : Execute test for x seconds. |
| 48 | * -p : Pause for SIGUSR1 before starting |
| 49 | * -P x : Pause for x seconds between iterations. |
| 50 | * -t : Turn on syscall timing. |
| 51 | * |
| 52 | * Author |
| 53 | * Ricardo Salveti de Araujo <rsalveti@linux.vnet.ibm.com> based on |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 54 | * swapon02 created by Aniruddha Marathe |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 55 | * |
| 56 | * History |
| 57 | * 16/08/2007 Created <rsalveti@linux.vnet.ibm.com> |
| 58 | * |
| 59 | ******************************************************************************/ |
| 60 | |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 61 | #include <sys/types.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 62 | #include <unistd.h> |
| 63 | #include <errno.h> |
| 64 | #include <stdlib.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 65 | #include <sys/wait.h> |
| 66 | #include <sys/stat.h> |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 67 | #include <sys/utsname.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 68 | #include <fcntl.h> |
| 69 | #include <pwd.h> |
| 70 | #include <string.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 71 | #include <signal.h> |
| 72 | #include "test.h" |
| 73 | #include "usctest.h" |
yaberauneya | 77772ef | 2009-10-17 06:53:58 +0000 | [diff] [blame] | 74 | #include "config.h" |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 75 | #include "linux_syscall_numbers.h" |
| 76 | #include "swaponoff.h" |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 77 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 78 | void setup(); |
| 79 | void cleanup(); |
| 80 | int setup_swap(); |
| 81 | int clean_swap(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | int check_and_swapoff(char *filename); |
| 83 | int create_swapfile(char *swapfile, int bs, int count); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 84 | |
| 85 | char *TCID = "swapon03"; /* Test program identifier. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | int TST_TOTAL = 1; /* Total number of test cases. */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 87 | |
| 88 | static int exp_enos[] = { EPERM, 0 }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 90 | static int swapfiles; /* Number of swapfiles turned on */ |
| 91 | |
| 92 | struct utsname uval; |
| 93 | char *kmachine; |
| 94 | |
| 95 | /* Paths for files that we'll use to test */ |
| 96 | int testfiles = 3; |
| 97 | static struct swap_testfile_t { |
| 98 | char *filename; |
| 99 | } swap_testfiles[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | { |
| 101 | "firstswapfile"}, { |
| 102 | "secondswapfile"}, { |
| 103 | "thirdswapfile"} |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 104 | }; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | int expected_errno = EPERM; /* Expected errno when doing the test */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 107 | |
| 108 | int main(int ac, char **av) |
| 109 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 110 | int lc; |
| 111 | char *msg; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 112 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 113 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 114 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 115 | |
| 116 | /*************************************************************** |
| 117 | * perform global setup for test |
| 118 | ***************************************************************/ |
| 119 | uname(&uval); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | kmachine = uval.machine; |
| 121 | setup(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 122 | |
| 123 | /*************************************************************** |
| 124 | * check looping state if -i option given |
| 125 | ***************************************************************/ |
| 126 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 127 | Tst_count = 0; |
| 128 | |
| 129 | /* do the test setup */ |
| 130 | if (setup_swap() < 0) { |
| 131 | clean_swap(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | tst_brkm(TBROK, cleanup, |
| 133 | "Setup failed, quitting the test"); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Call swapon sys call for the first time */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 137 | TEST(ltp_syscall(__NR_swapon, swap_testfiles[0].filename, 0)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 138 | |
| 139 | /* Check return code */ |
| 140 | if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) { |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 141 | tst_resm(TPASS, "swapon(2) got expected failure (%d),", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | expected_errno); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 143 | } else if (TEST_RETURN < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | tst_resm(TFAIL | TTERRNO, |
| 145 | "swapon(2) failed to produce expected error " |
| 146 | "(%d). System reboot recommended.", |
| 147 | expected_errno); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 148 | } else { |
| 149 | /* Probably the system supports MAX_SWAPFILES > 30, |
| 150 | * let's try with MAX_SWAPFILES == 32 */ |
| 151 | |
| 152 | /* Call swapon sys call once again for 32 |
| 153 | * now we can't receive an error */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 154 | TEST(ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | (__NR_swapon, swap_testfiles[1].filename, 0)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 156 | |
| 157 | /* Check return code (now we're expecting success) */ |
| 158 | if (TEST_RETURN < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | tst_resm(TFAIL | TTERRNO, |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 160 | "swapon(2) got an unexpected failure"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | } else { |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 162 | /* Call swapon sys call once again for 33 |
| 163 | * now we have to receive an error */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 164 | TEST(ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | (__NR_swapon, swap_testfiles[2].filename, |
| 166 | 0)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 167 | |
| 168 | /* Check return code (should be an error) */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | if ((TEST_RETURN == -1) |
| 170 | && (TEST_ERRNO == expected_errno)) { |
| 171 | tst_resm(TPASS, |
| 172 | "swapon(2) got expected failure;" |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 173 | " Got errno = %d, probably your" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | " MAX_SWAPFILES is 32", |
| 175 | expected_errno); |
| 176 | } else { |
| 177 | tst_resm(TFAIL, |
| 178 | "swapon(2) failed to produce" |
| 179 | " expected error: %d, got %s." |
| 180 | " System reboot after execution of LTP" |
| 181 | " test suite is recommended.", |
| 182 | expected_errno, |
| 183 | strerror(TEST_ERRNO)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* do the clean */ |
| 190 | if (clean_swap() < 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | tst_brkm(TBROK, cleanup, |
| 192 | "Cleanup failed, quitting the test"); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 193 | |
| 194 | TEST_ERROR_LOG(TEST_ERRNO); |
| 195 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 196 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 197 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 198 | cleanup(); |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 199 | tst_exit(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 200 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 201 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 202 | |
| 203 | /*************************************************************** |
| 204 | * setup_swap() - Create 33 and activate 30 swapfiles. |
| 205 | ***************************************************************/ |
| 206 | int setup_swap() |
| 207 | { |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 208 | pid_t pid; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 209 | int j, fd; /*j is loop counter, fd is file descriptor */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | int status; /* used for fork */ |
| 211 | int res = 0, pagesize = getpagesize(); |
| 212 | int bs, count; |
| 213 | char filename[15]; /* array to store new filename */ |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 214 | char buf[BUFSIZ + 1]; /* temp buffer for reading /proc/swaps */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 215 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | /* Find out how many swapfiles (1 line per entry) already exist */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 217 | swapfiles = 0; |
| 218 | |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 219 | if (seteuid(0) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | tst_brkm(TFAIL | TERRNO, cleanup, "Failed to call seteuid"); |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 221 | } |
| 222 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 223 | /* This includes the first (header) line */ |
| 224 | if ((fd = open("/proc/swaps", O_RDONLY)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | tst_brkm(TFAIL | TERRNO, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | "Failed to find out existing number of swap files"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | } |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 228 | do { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | char *p = buf; |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 230 | res = read(fd, buf, BUFSIZ); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 231 | if (res < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 232 | tst_brkm(TFAIL | TERRNO, cleanup, |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 233 | "Failed to find out existing number of swap " |
| 234 | "files"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | } |
| 236 | buf[res] = '\0'; |
| 237 | while ((p = strchr(p, '\n'))) { |
| 238 | p++; |
| 239 | swapfiles++; |
| 240 | } |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 241 | } while (BUFSIZ <= res); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 242 | close(fd); |
| 243 | if (swapfiles) |
| 244 | swapfiles--; /* don't count the /proc/swaps header */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 245 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 246 | if (swapfiles < 0) { |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 247 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 248 | "Failed to find existing number of swapfiles"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 250 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | /* Determine how many more files are to be created */ |
yaberauneya | 96db7a4 | 2009-10-25 06:33:33 +0000 | [diff] [blame] | 252 | swapfiles = MAX_SWAPFILES - swapfiles; |
| 253 | if (swapfiles > MAX_SWAPFILES) { |
| 254 | swapfiles = MAX_SWAPFILES; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 255 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 256 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | /* args for dd */ |
| 258 | if ((strncmp(kmachine, "ia64", 4)) == 0) { |
| 259 | bs = 1024; |
| 260 | count = 1024; |
| 261 | } else if (pagesize == 65536) { |
| 262 | bs = 1048; |
| 263 | count = 655; |
| 264 | } else { |
| 265 | bs = 1048; |
| 266 | count = 40; |
| 267 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 268 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | pid = FORK_OR_VFORK(); |
| 270 | if (pid == 0) { |
| 271 | /*create and turn on remaining swapfiles */ |
| 272 | for (j = 0; j < swapfiles; j++) { |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 273 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 274 | /* prepare filename for the iteration */ |
| 275 | if (sprintf(filename, "swapfile%02d", j + 2) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 276 | printf("sprintf() failed to create " |
| 277 | "filename"); |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 278 | exit(1); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 279 | } |
| 280 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 281 | /* Create the swapfile */ |
| 282 | if (create_swapfile(filename, bs, count) < 0) { |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 283 | printf("Failed to create swapfile"); |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 284 | exit(1); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 285 | } |
subrata_modak | 05fd0c7 | 2009-02-12 15:06:05 +0000 | [diff] [blame] | 286 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 287 | /* turn on the swap file */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 288 | res = ltp_syscall(__NR_swapon, filename, 0); |
| 289 | if (res != 0) { |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 290 | if (errno == EPERM) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 291 | printf("Successfully created %d " |
| 292 | "swapfiles\n", j); |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 293 | break; |
| 294 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 295 | printf("Failed to create " |
| 296 | "swapfile: %s\n", filename); |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 297 | exit(1); |
| 298 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 301 | exit(0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 302 | } else |
| 303 | waitpid(pid, &status, 0); |
| 304 | |
| 305 | if (WEXITSTATUS(status)) { |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 306 | tst_brkm(TFAIL, cleanup, "Failed to setup swaps"); |
subrata_modak | 05fd0c7 | 2009-02-12 15:06:05 +0000 | [diff] [blame] | 307 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 308 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 309 | /* Create all needed extra swapfiles for testing */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 310 | for (j = 0; j < testfiles; j++) { |
| 311 | if (create_swapfile(swap_testfiles[j].filename, bs, count) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 312 | tst_resm(TWARN, |
| 313 | "Failed to create swapfiles for the test"); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 314 | exit(1); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | |
| 320 | } |
| 321 | |
| 322 | /*************************************************************** |
| 323 | * create_swapfile() - Create a swap file |
| 324 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 325 | int create_swapfile(char *swapfile, int bs, int count) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 326 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 327 | char cmd_buffer[256]; |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 328 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 329 | /* prepare the path string for dd command */ |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 330 | if (snprintf(cmd_buffer, sizeof(cmd_buffer), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 331 | "dd if=/dev/zero of=%s bs=%d " |
| 332 | "count=%d > tmpfile 2>&1", swapfile, bs, count) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 333 | tst_resm(TWARN, |
| 334 | "sprintf() failed to create the command string"); |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 335 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 336 | return -1; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 337 | } |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 338 | |
| 339 | if (system(cmd_buffer) != 0) { |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 340 | tst_resm(TWARN, "dd command failed to create file via " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 341 | "command: %s", cmd_buffer); |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 342 | return -1; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 343 | } |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 344 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 345 | /* make the file swapfile */ |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 346 | if (snprintf(cmd_buffer, sizeof(cmd_buffer), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 347 | "mkswap %s > tmpfile 2>&1", swapfile) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 348 | tst_resm(TWARN, |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 349 | "snprintf() failed to create mkswap command string"); |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 350 | return -1; |
| 351 | } |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 352 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 353 | if (system(cmd_buffer) != 0) { |
yaberauneya | 225c7f3 | 2010-01-31 07:29:25 +0000 | [diff] [blame] | 354 | tst_resm(TWARN, "failed to make swap file %s via command %s", |
| 355 | swapfile, cmd_buffer); |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 356 | return -1; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 359 | return 0; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /*************************************************************** |
| 363 | * clean_swap() - clearing all turned on swapfiles |
| 364 | ***************************************************************/ |
| 365 | int clean_swap() |
| 366 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 367 | int j; |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 368 | char filename[FILENAME_MAX]; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 369 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 370 | for (j = 0; j < swapfiles; j++) { |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 371 | if (snprintf(filename, sizeof(filename), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 372 | "swapfile%02d", j + 2) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 373 | tst_resm(TWARN, "sprintf() failed to create filename"); |
| 374 | tst_resm(TWARN, "Failed to turn off swap files. System" |
| 375 | " reboot after execution of LTP test" |
| 376 | " suite is recommended"); |
| 377 | return -1; |
| 378 | } |
| 379 | if (check_and_swapoff(filename) != 0) { |
| 380 | tst_resm(TWARN, "Failed to turn off swap file %s.", |
| 381 | filename); |
| 382 | return -1; |
| 383 | } |
| 384 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 385 | |
| 386 | for (j = 0; j < testfiles; j++) { |
| 387 | if (check_and_swapoff(swap_testfiles[j].filename) != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 388 | tst_resm(TWARN, "Failed to turn off swap file %s.", |
| 389 | swap_testfiles[j].filename); |
| 390 | return -1; |
| 391 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 392 | } |
| 393 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 394 | return 0; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /*************************************************************** |
| 398 | * check_and_swapoff() - check if the file is at /proc/swaps and |
| 399 | * remove it giving swapoff |
| 400 | ***************************************************************/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 401 | int check_and_swapoff(char *filename) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 402 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 403 | char cmd_buffer[256]; |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 404 | int rc = -1; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 405 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 406 | if (snprintf(cmd_buffer, sizeof(cmd_buffer), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 407 | "grep -q '%s.*file' /proc/swaps", filename) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 408 | tst_resm(TWARN, |
| 409 | "sprintf() failed to create the command string"); |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 410 | } else { |
| 411 | |
| 412 | rc = 0; |
| 413 | |
| 414 | if (system(cmd_buffer) == 0) { |
| 415 | |
| 416 | /* now we need to swapoff the file */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 417 | if (ltp_syscall(__NR_swapoff, filename) != 0) { |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 418 | |
| 419 | tst_resm(TWARN, "Failed to turn off swap " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 420 | "file. system reboot after " |
| 421 | "execution of LTP test suite " |
| 422 | "is recommended"); |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 423 | rc = -1; |
| 424 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 425 | } |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 426 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 427 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 428 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 429 | |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 430 | return rc; |
| 431 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /*************************************************************** |
| 435 | * setup() - performs all ONE TIME setup for this test |
| 436 | ***************************************************************/ |
| 437 | void setup() |
| 438 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 439 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 440 | tst_sig(FORK, DEF_HANDLER, cleanup); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 441 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 442 | /* set the expected errnos... */ |
| 443 | TEST_EXP_ENOS(exp_enos); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 444 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 445 | /* Check whether we are root */ |
| 446 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 447 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 448 | } |
| 449 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 450 | tst_tmpdir(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 451 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 452 | if (tst_is_cwd_tmpfs()) { |
| 453 | tst_brkm(TCONF, cleanup, |
| 454 | "Cannot do swapon on a file located on a tmpfs filesystem"); |
| 455 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 456 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 457 | if (tst_is_cwd_nfs()) { |
| 458 | tst_brkm(TCONF, cleanup, |
| 459 | "Cannot do swapon on a file located on a nfs filesystem"); |
| 460 | } |
| 461 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 462 | TEST_PAUSE; |
| 463 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 464 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 465 | |
| 466 | /*************************************************************** |
| 467 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 468 | * completion or premature exit. |
| 469 | ***************************************************************/ |
| 470 | void cleanup() |
| 471 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 472 | /* |
| 473 | * print timing stats if that option was specified. |
| 474 | * print errno log if that option was specified. |
| 475 | */ |
| 476 | TEST_CLEANUP; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 477 | |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 478 | /* Remove any remaining swap files */ |
| 479 | clean_swap(); |
| 480 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 481 | tst_rmdir(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 482 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 483 | } |