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 | */ |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 17 | |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 18 | /* |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 19 | * This test case checks whether swapoff(2) system call returns |
| 20 | * 1. EINVAL when the path does not exist |
| 21 | * 2. ENOENT when the path exists but is invalid |
| 22 | * 3. EPERM when user is not a superuser |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 23 | */ |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 24 | |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <pwd.h> |
| 31 | #include <string.h> |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 33 | #include "test.h" |
yaberauneya | a2a05e3 | 2009-11-26 14:19:32 +0000 | [diff] [blame] | 34 | #include "linux_syscall_numbers.h" |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 35 | #include "safe_macros.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 36 | #include "tst_fs_type.h" |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 37 | |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 38 | static void setup(void); |
| 39 | static void cleanup(void); |
| 40 | static int setup01(void); |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 41 | static void cleanup01(void); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 43 | char *TCID = "swapoff02"; |
| 44 | int TST_TOTAL = 3; |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 45 | |
| 46 | static uid_t nobody_uid; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 47 | |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 48 | static struct test_case_t { |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 49 | char *err_desc; |
| 50 | int exp_errno; |
| 51 | char *exp_errval; |
| 52 | char *path; |
| 53 | int (*setup)(void); |
| 54 | void (*cleanup)(void); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 55 | } testcase[] = { |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 56 | {"path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL}, |
| 57 | {"Invalid file", EINVAL, "EINVAL", "./swapfile01", NULL, NULL}, |
| 58 | {"Permission denied", EPERM, "EPERM", "./swapfile01", setup01, cleanup01} |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | int main(int ac, char **av) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 62 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 63 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 64 | const char *msg; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 65 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 66 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 67 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 68 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 69 | setup(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 70 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 72 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 73 | tst_count = 0; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 74 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | for (i = 0; i < TST_TOTAL; i++) { |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 76 | |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 77 | if (testcase[i].setup) |
| 78 | testcase[i].setup(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 79 | |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 80 | TEST(ltp_syscall(__NR_swapoff, testcase[i].path)); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 81 | |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 82 | if (testcase[i].cleanup) |
| 83 | testcase[i].cleanup(); |
| 84 | |
| 85 | if (TEST_RETURN == -1 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | && (TEST_ERRNO == testcase[i].exp_errno)) { |
| 87 | tst_resm(TPASS, |
| 88 | "swapoff(2) expected failure;" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | " Got errno - %s : %s", |
| 90 | testcase[i].exp_errval, |
| 91 | testcase[i].err_desc); |
| 92 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 93 | } else { |
| 94 | tst_resm(TFAIL, "swapoff(2) failed to produce" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | " expected error; %d, errno" |
| 96 | ": %s and got %d", |
| 97 | testcase[i].exp_errno, |
| 98 | testcase[i].exp_errval, TEST_ERRNO); |
| 99 | |
| 100 | if ((TEST_RETURN == 0) && (i == 2)) { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 101 | if (ltp_syscall |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | (__NR_swapon, "./swapfile01", |
| 103 | 0) != 0) { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 104 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | " Failed to turn on" |
| 106 | " swap file"); |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 107 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 108 | } |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 109 | } |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 110 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 111 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 112 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 113 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 114 | tst_exit(); |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 115 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 116 | |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 117 | static int setup01(void) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 118 | { |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 119 | SAFE_SETEUID(cleanup, nobody_uid); |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 120 | return 0; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 123 | static void cleanup01(void) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 124 | { |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 125 | SAFE_SETEUID(cleanup, 0); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 128 | static void setup(void) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 129 | { |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 130 | long type; |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 131 | struct passwd *nobody; |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 132 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 133 | tst_sig(FORK, DEF_HANDLER, cleanup); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 134 | |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 135 | tst_require_root(NULL); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | dfda9f8 | 2014-03-06 13:37:21 +0100 | [diff] [blame] | 137 | nobody = SAFE_GETPWNAM(NULL, "nobody"); |
| 138 | nobody_uid = nobody->pw_uid; |
| 139 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 140 | TEST_PAUSE; |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 141 | |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 142 | tst_tmpdir(); |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 143 | |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 144 | switch ((type = tst_fs_type(cleanup, "."))) { |
| 145 | case TST_NFS_MAGIC: |
| 146 | case TST_TMPFS_MAGIC: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | tst_brkm(TCONF, cleanup, |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 148 | "Cannot do swapoff on a file on %s filesystem", |
| 149 | tst_fs_type_name(type)); |
| 150 | break; |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Xiaoguang Wang | 4694167 | 2014-04-25 10:02:07 +0800 | [diff] [blame] | 153 | if (!tst_fs_has_free(NULL, ".", 1, TST_KB)) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | tst_brkm(TBROK, cleanup, |
| 155 | "Insufficient disk space to create swap file"); |
robbiew | 143090a | 2005-01-17 22:10:09 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Cyril Hrubis | 912e264 | 2014-02-26 18:11:46 +0100 | [diff] [blame] | 158 | if (tst_fill_file("./swapfile01", 0x00, 1024, 1)) |
| 159 | tst_brkm(TBROK, cleanup, "Failed to create swapfile"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 160 | } |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 161 | |
Cyril Hrubis | 591ddcb | 2014-02-26 18:06:19 +0100 | [diff] [blame] | 162 | static void cleanup(void) |
robbiew | 0abc81f | 2003-01-02 16:45:29 +0000 | [diff] [blame] | 163 | { |
robbiew | adc1c01 | 2003-04-29 17:06:59 +0000 | [diff] [blame] | 164 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 165 | } |