subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 3 | * Copyright (c) International Business Machines Corp., 2007 |
| 4 | * Created by <rsalveti@linux.vnet.ibm.com> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 5 | * |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 10 | * |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 19 | * |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 20 | ******************************************************************************/ |
| 21 | |
| 22 | /* |
| 23 | * This test case checks whether swapon(2) system call returns: |
| 24 | * - EPERM when there are more than MAX_SWAPFILES already in use. |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 25 | * |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 26 | */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 27 | |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
| 31 | #include <stdlib.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 32 | #include <sys/wait.h> |
| 33 | #include <sys/stat.h> |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 34 | #include <sys/utsname.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <pwd.h> |
| 37 | #include <string.h> |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 38 | #include <signal.h> |
| 39 | #include "test.h" |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 40 | #include "linux_syscall_numbers.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 41 | #include "tst_fs_type.h" |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 42 | #include "swaponoff.h" |
Stanislav Kholmanskikh | 4bdda04 | 2013-08-14 10:01:47 +0400 | [diff] [blame] | 43 | #include "libswapon.h" |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 44 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 45 | static void setup(void); |
| 46 | static void cleanup(void); |
| 47 | static int setup_swap(void); |
| 48 | static int clean_swap(void); |
| 49 | static int check_and_swapoff(const char *filename); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 50 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 51 | char *TCID = "swapon03"; |
| 52 | int TST_TOTAL = 1; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 54 | static int swapfiles; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame] | 56 | static long fs_type; |
| 57 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 58 | int testfiles = 3; |
| 59 | static struct swap_testfile_t { |
| 60 | char *filename; |
| 61 | } swap_testfiles[] = { |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 62 | {"firstswapfile"}, |
| 63 | {"secondswapfile"}, |
| 64 | {"thirdswapfile"} |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 65 | }; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 66 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 67 | int expected_errno = EPERM; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 68 | |
| 69 | int main(int ac, char **av) |
| 70 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 73 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 74 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 75 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 76 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | setup(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 78 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 79 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 80 | tst_count = 0; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 81 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 82 | if (setup_swap() < 0) { |
| 83 | clean_swap(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | tst_brkm(TBROK, cleanup, |
| 85 | "Setup failed, quitting the test"); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 88 | TEST(ltp_syscall(__NR_swapon, swap_testfiles[0].filename, 0)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 89 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 90 | if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) { |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 91 | tst_resm(TPASS, "swapon(2) got expected failure (%d),", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | expected_errno); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 93 | } else if (TEST_RETURN < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | tst_resm(TFAIL | TTERRNO, |
| 95 | "swapon(2) failed to produce expected error " |
| 96 | "(%d). System reboot recommended.", |
| 97 | expected_errno); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 98 | } else { |
| 99 | /* Probably the system supports MAX_SWAPFILES > 30, |
| 100 | * let's try with MAX_SWAPFILES == 32 */ |
| 101 | |
| 102 | /* Call swapon sys call once again for 32 |
| 103 | * now we can't receive an error */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 104 | TEST(ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | (__NR_swapon, swap_testfiles[1].filename, 0)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 106 | |
| 107 | /* Check return code (now we're expecting success) */ |
| 108 | if (TEST_RETURN < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | tst_resm(TFAIL | TTERRNO, |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 110 | "swapon(2) got an unexpected failure"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | } else { |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 112 | /* Call swapon sys call once again for 33 |
| 113 | * now we have to receive an error */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 114 | TEST(ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 115 | (__NR_swapon, swap_testfiles[2].filename, |
| 116 | 0)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 117 | |
| 118 | /* Check return code (should be an error) */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | if ((TEST_RETURN == -1) |
| 120 | && (TEST_ERRNO == expected_errno)) { |
| 121 | tst_resm(TPASS, |
| 122 | "swapon(2) got expected failure;" |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 123 | " Got errno = %d, probably your" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | " MAX_SWAPFILES is 32", |
| 125 | expected_errno); |
| 126 | } else { |
| 127 | tst_resm(TFAIL, |
| 128 | "swapon(2) failed to produce" |
| 129 | " expected error: %d, got %s." |
| 130 | " System reboot after execution of LTP" |
| 131 | " test suite is recommended.", |
| 132 | expected_errno, |
| 133 | strerror(TEST_ERRNO)); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } |
| 137 | } |
| 138 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 139 | if (clean_swap() < 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | tst_brkm(TBROK, cleanup, |
| 141 | "Cleanup failed, quitting the test"); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 142 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 144 | |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 145 | cleanup(); |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 146 | tst_exit(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 147 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 149 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 150 | /* |
| 151 | * Create 33 and activate 30 swapfiles. |
| 152 | */ |
| 153 | static int setup_swap(void) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 154 | { |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 155 | pid_t pid; |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 156 | int j, fd; |
| 157 | int status; |
Stanislav Kholmanskikh | 4bdda04 | 2013-08-14 10:01:47 +0400 | [diff] [blame] | 158 | int res = 0; |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 159 | char filename[15]; |
| 160 | char buf[BUFSIZ + 1]; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 161 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | /* Find out how many swapfiles (1 line per entry) already exist */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 163 | swapfiles = 0; |
| 164 | |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 165 | if (seteuid(0) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | tst_brkm(TFAIL | TERRNO, cleanup, "Failed to call seteuid"); |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 167 | } |
| 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | /* This includes the first (header) line */ |
| 170 | if ((fd = open("/proc/swaps", O_RDONLY)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 171 | tst_brkm(TFAIL | TERRNO, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | "Failed to find out existing number of swap files"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | } |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 174 | do { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | char *p = buf; |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 176 | res = read(fd, buf, BUFSIZ); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | if (res < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | tst_brkm(TFAIL | TERRNO, cleanup, |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 179 | "Failed to find out existing number of swap " |
| 180 | "files"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | } |
| 182 | buf[res] = '\0'; |
| 183 | while ((p = strchr(p, '\n'))) { |
| 184 | p++; |
| 185 | swapfiles++; |
| 186 | } |
yaberauneya | 89911a1 | 2009-11-26 14:11:35 +0000 | [diff] [blame] | 187 | } while (BUFSIZ <= res); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 188 | close(fd); |
| 189 | if (swapfiles) |
| 190 | swapfiles--; /* don't count the /proc/swaps header */ |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | if (swapfiles < 0) { |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 193 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | "Failed to find existing number of swapfiles"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 196 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | /* Determine how many more files are to be created */ |
yaberauneya | 96db7a4 | 2009-10-25 06:33:33 +0000 | [diff] [blame] | 198 | swapfiles = MAX_SWAPFILES - swapfiles; |
| 199 | if (swapfiles > MAX_SWAPFILES) { |
| 200 | swapfiles = MAX_SWAPFILES; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 202 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | pid = FORK_OR_VFORK(); |
| 204 | if (pid == 0) { |
| 205 | /*create and turn on remaining swapfiles */ |
| 206 | for (j = 0; j < swapfiles; j++) { |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 207 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 208 | /* prepare filename for the iteration */ |
| 209 | if (sprintf(filename, "swapfile%02d", j + 2) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 210 | printf("sprintf() failed to create " |
| 211 | "filename"); |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 212 | exit(1); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 213 | } |
| 214 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | /* Create the swapfile */ |
Stanislav Kholmanskikh | 4bdda04 | 2013-08-14 10:01:47 +0400 | [diff] [blame] | 216 | make_swapfile(cleanup, filename); |
subrata_modak | 05fd0c7 | 2009-02-12 15:06:05 +0000 | [diff] [blame] | 217 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | /* turn on the swap file */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 219 | res = ltp_syscall(__NR_swapon, filename, 0); |
| 220 | if (res != 0) { |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame] | 221 | if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) |
| 222 | exit(2); |
| 223 | |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 224 | if (errno == EPERM) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | printf("Successfully created %d " |
| 226 | "swapfiles\n", j); |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 227 | break; |
| 228 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 229 | printf("Failed to create " |
| 230 | "swapfile: %s\n", filename); |
Garrett Cooper | be5c788 | 2010-08-02 22:01:02 -0700 | [diff] [blame] | 231 | exit(1); |
| 232 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 235 | exit(0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 236 | } else |
| 237 | waitpid(pid, &status, 0); |
| 238 | |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame] | 239 | switch (WEXITSTATUS(status)) { |
| 240 | case 0: |
| 241 | break; |
| 242 | case 2: |
| 243 | tst_brkm(TCONF, cleanup, "Swapfile on BTRFS not implemeted"); |
| 244 | break; |
| 245 | default: |
Garrett Cooper | 1e27c53 | 2010-11-22 14:33:28 -0800 | [diff] [blame] | 246 | tst_brkm(TFAIL, cleanup, "Failed to setup swaps"); |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame] | 247 | break; |
subrata_modak | 05fd0c7 | 2009-02-12 15:06:05 +0000 | [diff] [blame] | 248 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 249 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 250 | /* Create all needed extra swapfiles for testing */ |
Stanislav Kholmanskikh | 4bdda04 | 2013-08-14 10:01:47 +0400 | [diff] [blame] | 251 | for (j = 0; j < testfiles; j++) |
| 252 | make_swapfile(cleanup, swap_testfiles[j].filename); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | |
| 256 | } |
| 257 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 258 | /* |
| 259 | * Turn off all swapfiles previously turned on |
| 260 | */ |
| 261 | static int clean_swap(void) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 262 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 263 | int j; |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 264 | char filename[FILENAME_MAX]; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 265 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 266 | for (j = 0; j < swapfiles; j++) { |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 267 | if (snprintf(filename, sizeof(filename), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 268 | "swapfile%02d", j + 2) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | tst_resm(TWARN, "sprintf() failed to create filename"); |
| 270 | tst_resm(TWARN, "Failed to turn off swap files. System" |
| 271 | " reboot after execution of LTP test" |
| 272 | " suite is recommended"); |
| 273 | return -1; |
| 274 | } |
| 275 | if (check_and_swapoff(filename) != 0) { |
| 276 | tst_resm(TWARN, "Failed to turn off swap file %s.", |
| 277 | filename); |
| 278 | return -1; |
| 279 | } |
| 280 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 281 | |
| 282 | for (j = 0; j < testfiles; j++) { |
| 283 | if (check_and_swapoff(swap_testfiles[j].filename) != 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 284 | tst_resm(TWARN, "Failed to turn off swap file %s.", |
| 285 | swap_testfiles[j].filename); |
| 286 | return -1; |
| 287 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 288 | } |
| 289 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 290 | return 0; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 293 | /* |
| 294 | * Check if the file is at /proc/swaps and remove it giving swapoff |
| 295 | */ |
| 296 | static int check_and_swapoff(const char *filename) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 297 | { |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 298 | char cmd_buffer[256]; |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 299 | int rc = -1; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 300 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 301 | if (snprintf(cmd_buffer, sizeof(cmd_buffer), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 302 | "grep -q '%s.*file' /proc/swaps", filename) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 303 | tst_resm(TWARN, |
| 304 | "sprintf() failed to create the command string"); |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 305 | } else { |
| 306 | |
| 307 | rc = 0; |
| 308 | |
| 309 | if (system(cmd_buffer) == 0) { |
| 310 | |
| 311 | /* now we need to swapoff the file */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 312 | if (ltp_syscall(__NR_swapoff, filename) != 0) { |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 313 | |
| 314 | tst_resm(TWARN, "Failed to turn off swap " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 315 | "file. system reboot after " |
| 316 | "execution of LTP test suite " |
| 317 | "is recommended"); |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 318 | rc = -1; |
| 319 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 320 | } |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 321 | |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 322 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 323 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 324 | |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 325 | return rc; |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 328 | static void setup(void) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 329 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 330 | tst_sig(FORK, DEF_HANDLER, cleanup); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 331 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 332 | tst_require_root(NULL); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 333 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 334 | tst_tmpdir(); |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 335 | |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame] | 336 | switch ((fs_type = tst_fs_type(cleanup, "."))) { |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 337 | case TST_NFS_MAGIC: |
| 338 | case TST_TMPFS_MAGIC: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 339 | tst_brkm(TCONF, cleanup, |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 340 | "Cannot do swapon on a file on %s filesystem", |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame] | 341 | tst_fs_type_name(fs_type)); |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 342 | break; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 343 | } |
| 344 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 345 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 346 | } |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 347 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 348 | static void cleanup(void) |
subrata_modak | 413ec14 | 2007-08-28 07:17:29 +0000 | [diff] [blame] | 349 | { |
Garrett Cooper | a83e7dc | 2010-02-24 17:05:14 -0800 | [diff] [blame] | 350 | clean_swap(); |
| 351 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 352 | tst_rmdir(); |
Cyril Hrubis | d9235f8 | 2012-06-29 14:08:41 +0200 | [diff] [blame] | 353 | } |