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 | * |
| 19 | * TEST IDENTIFIER : swapoff01 |
| 20 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 21 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 23 | * TEST TITLE : Basic test for swapoff(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 1 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 27 | * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 29 | * SIGNALS |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 30 | * Uses SIGUSR1 to pause before test if option set. |
| 31 | * (See the parse_opts(3) man page). |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 32 | * |
| 33 | * DESCRIPTION |
| 34 | * This is a Phase I test for the swapoff(2) system call. |
| 35 | * It is intended to provide a limited exposure of the system call. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 36 | * $ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 37 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 38 | * Setup: |
| 39 | * Setup signal handling. |
| 40 | * Pause for SIGUSR1 if option specified. |
| 41 | * Create a temporary directory. |
| 42 | * Create file of size 40K ( minimum swapfile size). |
| 43 | * Make this file as swap file using mkswap(8) |
| 44 | * Turn on the swap file. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +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 failed (return=-1) |
| 50 | * Log the errno and Issue a FAIL message. |
| 51 | * Otherwise, Issue a PASS message. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 52 | * |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 53 | * Cleanup: |
| 54 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 55 | * |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 56 | * USAGE: <for command-line> |
| 57 | * swapoff01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] |
| 58 | * where: |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 59 | * -c n : Run n copies simultaneously. |
| 60 | * -e : Turn on errno logging. |
| 61 | * -i n : Execute test n times. |
| 62 | * -I x : Execute test for x seconds. |
| 63 | * -p : Pause for SIGUSR1 before starting |
| 64 | * -P x : Pause for x seconds between iterations. |
| 65 | * -t : Turn on syscall timing. |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 66 | * |
| 67 | *RESTRICTIONS: |
| 68 | * Not compatible with kernel versions below 1.3.2. |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 69 | * |
| 70 | *CHANGES: |
| 71 | * 2005/01/01 Add extra check to stop test if insufficient disk space in dir |
| 72 | * -Ricky Ng-Adam (rngadam@yahoo.com) |
| 73 | * 2005/01/01 Add extra check to stop test if swap file is on tmpfs |
| 74 | * -Ricky Ng-Adam (rngadam@yahoo.com) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 75 | *****************************************************************************/ |
| 76 | |
| 77 | #include <unistd.h> |
| 78 | #include "test.h" |
| 79 | #include "usctest.h" |
| 80 | #include <errno.h> |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 81 | #include <stdlib.h> |
yaberauneya | 77772ef | 2009-10-17 06:53:58 +0000 | [diff] [blame] | 82 | #include "config.h" |
yaberauneya | a2a05e3 | 2009-11-26 14:19:32 +0000 | [diff] [blame] | 83 | #include "linux_syscall_numbers.h" |
| 84 | #include "swaponoff.h" |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 85 | |
| 86 | static void setup(); |
| 87 | static void cleanup(); |
| 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | char *TCID = "swapoff01"; /* Test program identifier. */ |
| 90 | int TST_TOTAL = 1; /* Total number of test cases. */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | int main(int ac, char **av) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 93 | { |
| 94 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 95 | int lc; |
| 96 | char *msg; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 98 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 100 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 101 | setup(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 102 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 104 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 105 | Tst_count = 0; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 106 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 107 | if (ltp_syscall(__NR_swapon, "./swapfile01", 0) != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 108 | tst_resm(TWARN, "Failed to turn on the swap file" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | ", skipping test iteration"); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 110 | continue; |
| 111 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 112 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 113 | TEST(ltp_syscall(__NR_swapoff, "./swapfile01")); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 115 | /* check return code */ |
| 116 | if (TEST_RETURN == -1) { |
| 117 | TEST_ERROR_LOG(TEST_ERRNO); |
| 118 | tst_resm(TFAIL, "swapoff(2) Failed to turn off" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | " swapfile. System reboot after execution" |
| 120 | " of LTP test suite is recommended."); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 121 | tst_resm(TWARN, "It is recommended not to run" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | " swapon01 and swapon02"); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 123 | } else { |
| 124 | tst_resm(TPASS, "swapoff(2) passed and turned off" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | " swapfile."); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 126 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | } /*End for TEST_LOOPING */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | /*Clean up and exit */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 130 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 131 | tst_exit(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 132 | |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* setup() - performs all ONE TIME setup for this test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | void setup() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 137 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 138 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 139 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 140 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | /* Check whether we are root */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 142 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 143 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 144 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 145 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 146 | TEST_PAUSE; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 147 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 148 | tst_tmpdir(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 149 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | if (tst_is_cwd_tmpfs()) { |
| 151 | tst_brkm(TCONF, cleanup, |
| 152 | "Cannot do swapon on a file located on a tmpfs filesystem"); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 153 | } |
| 154 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | if (tst_is_cwd_nfs()) { |
| 156 | tst_brkm(TCONF, cleanup, |
| 157 | "Cannot do swapon on a file located on a nfs filesystem"); |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 158 | } |
| 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | if (!tst_cwd_has_free(65536)) { |
| 161 | tst_brkm(TBROK, cleanup, |
| 162 | "Insufficient disk space to create swap file"); |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 163 | } |
| 164 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | /*create file */ |
| 166 | if (system |
| 167 | ("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 > tmpfile" |
| 168 | " 2>&1 ") != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 169 | tst_brkm(TBROK, cleanup, "Failed to create file for swap"); |
| 170 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 171 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | /* make above file a swap file */ |
| 173 | if (system("mkswap swapfile01 > tmpfile 2>&1") != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 174 | tst_brkm(TBROK, cleanup, "Failed to make swapfile"); |
| 175 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 176 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 177 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * cleanup() - Performs one time cleanup for this test at |
| 181 | * completion or premature exit |
| 182 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | void cleanup() |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 184 | { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 185 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | * print timing stats if that option was specified. |
| 187 | * print errno log if that option was specified. |
| 188 | */ |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 189 | TEST_CLEANUP; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 190 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 191 | tst_rmdir(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 192 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 193 | } |