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