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