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 | */ |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 16 | /* |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 17 | * This test case checks whether swapon(2) system call returns |
| 18 | * 1. ENOENT when the path does not exist |
| 19 | * 2. EINVAL when the path exists but is invalid |
| 20 | * 3. EPERM when user is not a superuser |
| 21 | * 4. EBUSY when the specified path is already being used as a swap area |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 22 | */ |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 23 | |
| 24 | #include <unistd.h> |
| 25 | #include <errno.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <sys/stat.h> |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <pwd.h> |
| 32 | #include <string.h> |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 33 | #include <sys/utsname.h> |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 34 | #include <signal.h> |
| 35 | #include "test.h" |
| 36 | #include "usctest.h" |
yaberauneya | a2a05e3 | 2009-11-26 14:19:32 +0000 | [diff] [blame] | 37 | #include "linux_syscall_numbers.h" |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 38 | #include "safe_macros.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 39 | #include "tst_fs_type.h" |
Stanislav Kholmanskikh | 4bdda04 | 2013-08-14 10:01:47 +0400 | [diff] [blame] | 40 | #include "libswapon.h" |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 42 | static void setup(void); |
| 43 | static void cleanup(void); |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 44 | static void setup01(void); |
| 45 | static void cleanup01(void); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 46 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 47 | char *TCID = "swapon02"; |
| 48 | int TST_TOTAL = 4; |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 50 | static uid_t nobody_uid; |
| 51 | static int do_swapoff; |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 52 | static long fs_type; |
robbiew | ef95751 | 2003-03-13 15:33:06 +0000 | [diff] [blame] | 53 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | static int exp_enos[] = { EPERM, EINVAL, ENOENT, EBUSY, 0 }; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 55 | |
| 56 | static struct test_case_t { |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 57 | char *err_desc; |
| 58 | int exp_errno; |
| 59 | char *exp_errval; |
| 60 | char *path; |
| 61 | void (*setup)(void); |
| 62 | void (*cleanup)(void); |
| 63 | } testcases[] = { |
| 64 | {"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL}, |
| 65 | {"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL}, |
| 66 | {"Permission denied", EPERM, "EPERM", "./swapfile01", |
| 67 | setup01, cleanup01}, |
| 68 | {"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL}, |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 71 | static void verify_swapon(struct test_case_t *test) |
| 72 | { |
| 73 | if (test->setup) |
| 74 | test->setup(); |
| 75 | |
| 76 | TEST(ltp_syscall(__NR_swapon, test->path, 0)); |
| 77 | |
| 78 | if (test->cleanup) |
| 79 | test->cleanup(); |
| 80 | |
| 81 | if (TEST_RETURN == -1 && TEST_ERRNO == test->exp_errno) { |
| 82 | tst_resm(TPASS, "swapon(2) expected failure;" |
| 83 | " Got errno - %s : %s", |
| 84 | test->exp_errval, test->err_desc); |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 85 | return; |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 86 | } |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 87 | |
| 88 | if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) { |
| 89 | tst_resm(TCONF, "Swapfile on BTRFS not implemeted"); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | tst_resm(TFAIL, "swapon(2) failed to produce expected error:" |
| 94 | " %d, errno: %s and got %d.", test->exp_errno, |
| 95 | test->exp_errval, TEST_ERRNO); |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 96 | } |
| 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | int main(int ac, char **av) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 99 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 100 | int lc, i; |
| 101 | char *msg; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 103 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 104 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | setup(); |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 107 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | tst_count = 0; |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 110 | for (i = 0; i < TST_TOTAL; i++) |
| 111 | verify_swapon(testcases + i); |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 112 | } |
subrata_modak | daeac7a | 2007-05-17 14:31:45 +0000 | [diff] [blame] | 113 | |
Garrett Cooper | 62383b8 | 2010-11-22 12:02:14 -0800 | [diff] [blame] | 114 | cleanup(); |
| 115 | tst_exit(); |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 116 | } |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 117 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 118 | static void setup01(void) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 119 | { |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 120 | SAFE_SETEUID(cleanup, nobody_uid); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 123 | static void cleanup01(void) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 124 | { |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 125 | SAFE_SETEUID(cleanup, 0); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 128 | static void setup(void) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 129 | { |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 130 | struct passwd *nobody; |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | TEST_EXP_ENOS(exp_enos); |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 135 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 136 | tst_require_root(NULL); |
subrata_modak | 04c009e | 2007-11-28 09:38:08 +0000 | [diff] [blame] | 137 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 138 | nobody = SAFE_GETPWNAM(cleanup, "nobody"); |
| 139 | nobody_uid = nobody->pw_uid; |
| 140 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | tst_tmpdir(); |
robbiew | a3cc71f | 2003-02-18 20:53:50 +0000 | [diff] [blame] | 142 | |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 143 | switch ((fs_type = tst_fs_type(cleanup, "."))) { |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 144 | case TST_NFS_MAGIC: |
| 145 | case TST_TMPFS_MAGIC: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | tst_brkm(TCONF, cleanup, |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 147 | "Cannot do swapon on a file on %s filesystem", |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 148 | tst_fs_type_name(fs_type)); |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 149 | break; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 152 | SAFE_TOUCH(cleanup, "notswap", 0777, NULL); |
| 153 | make_swapfile(cleanup, "swapfile01"); |
| 154 | make_swapfile(cleanup, "alreadyused"); |
| 155 | |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 156 | if (ltp_syscall(__NR_swapon, "alreadyused", 0)) { |
| 157 | if (fs_type != TST_BTRFS_MAGIC || errno != EINVAL) |
| 158 | tst_resm(TWARN | TERRNO, "swapon(alreadyused) failed"); |
| 159 | } else { |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 160 | do_swapoff = 1; |
Cyril Hrubis | b16efb8 | 2014-03-06 14:49:17 +0100 | [diff] [blame^] | 161 | } |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 162 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 164 | } |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 165 | |
Cyril Hrubis | f6f4b7f | 2014-02-26 17:45:31 +0100 | [diff] [blame] | 166 | void cleanup(void) |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 167 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | TEST_CLEANUP; |
robbiew | dcc848e | 2003-01-02 21:03:35 +0000 | [diff] [blame] | 169 | |
Cyril Hrubis | a432c4d | 2014-03-06 14:28:30 +0100 | [diff] [blame] | 170 | if (do_swapoff && ltp_syscall(__NR_swapoff, "alreadyused")) |
| 171 | tst_resm(TWARN | TERRNO, "swapoff(alreadyused) failed"); |
| 172 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 174 | } |