• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
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
12  * with this program; if not, write the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  *
15  */
16 /*
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
22  */
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>
30 #include <fcntl.h>
31 #include <pwd.h>
32 #include <string.h>
33 #include <sys/utsname.h>
34 #include <signal.h>
35 #include "test.h"
36 #include "lapi/syscalls.h"
37 #include "safe_macros.h"
38 #include "libswapon.h"
39 
40 static void setup(void);
41 static void cleanup(void);
42 static void setup01(void);
43 static void cleanup01(void);
44 
45 char *TCID = "swapon02";
46 int TST_TOTAL = 4;
47 
48 static uid_t nobody_uid;
49 static int do_swapoff;
50 static long fs_type;
51 
52 static struct test_case_t {
53 	char *err_desc;
54 	int exp_errno;
55 	char *exp_errval;
56 	char *path;
57 	void (*setup)(void);
58 	void (*cleanup)(void);
59 } testcases[] = {
60 	{"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL},
61 	{"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL},
62 	{"Permission denied", EPERM, "EPERM", "./swapfile01",
63 	 setup01, cleanup01},
64 	{"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL},
65 };
66 
verify_swapon(struct test_case_t * test)67 static void verify_swapon(struct test_case_t *test)
68 {
69 	if (test->setup)
70 		test->setup();
71 
72 	TEST(ltp_syscall(__NR_swapon, test->path, 0));
73 
74 	if (test->cleanup)
75 		test->cleanup();
76 
77 	if (TEST_RETURN == -1 && TEST_ERRNO == test->exp_errno) {
78 		tst_resm(TPASS, "swapon(2) expected failure;"
79 			 " Got errno - %s : %s",
80 			 test->exp_errval, test->err_desc);
81 		return;
82 	}
83 
84 	if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) {
85 		tst_resm(TCONF, "Swapfile on BTRFS not implemeted");
86 			return;
87 	}
88 
89 	tst_resm(TFAIL, "swapon(2) failed to produce expected error:"
90 	         " %d, errno: %s and got %d.", test->exp_errno,
91 	         test->exp_errval, TEST_ERRNO);
92 }
93 
main(int ac,char ** av)94 int main(int ac, char **av)
95 {
96 	int lc, i;
97 
98 	tst_parse_opts(ac, av, NULL, NULL);
99 
100 	setup();
101 
102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
103 		tst_count = 0;
104 		for (i = 0; i < TST_TOTAL; i++)
105 			verify_swapon(testcases + i);
106 	}
107 
108 	cleanup();
109 	tst_exit();
110 }
111 
setup01(void)112 static void setup01(void)
113 {
114 	SAFE_SETEUID(cleanup, nobody_uid);
115 }
116 
cleanup01(void)117 static void cleanup01(void)
118 {
119 	SAFE_SETEUID(cleanup, 0);
120 }
121 
setup(void)122 static void setup(void)
123 {
124 	struct passwd *nobody;
125 
126 	tst_sig(FORK, DEF_HANDLER, cleanup);
127 
128 	tst_require_root();
129 
130 	nobody = SAFE_GETPWNAM(cleanup, "nobody");
131 	nobody_uid = nobody->pw_uid;
132 
133 	tst_tmpdir();
134 
135 	switch ((fs_type = tst_fs_type(cleanup, "."))) {
136 	case TST_NFS_MAGIC:
137 	case TST_TMPFS_MAGIC:
138 		tst_brkm(TCONF, cleanup,
139 			 "Cannot do swapon on a file on %s filesystem",
140 			 tst_fs_type_name(fs_type));
141 	break;
142 	}
143 
144 	SAFE_TOUCH(cleanup, "notswap", 0777, NULL);
145 	make_swapfile(cleanup, "swapfile01");
146 	make_swapfile(cleanup, "alreadyused");
147 
148 	if (ltp_syscall(__NR_swapon, "alreadyused", 0)) {
149 		if (fs_type != TST_BTRFS_MAGIC || errno != EINVAL)
150 			tst_resm(TWARN | TERRNO, "swapon(alreadyused) failed");
151 	} else {
152 		do_swapoff = 1;
153 	}
154 
155 	TEST_PAUSE;
156 }
157 
cleanup(void)158 void cleanup(void)
159 {
160 	if (do_swapoff && ltp_syscall(__NR_swapoff, "alreadyused"))
161 		tst_resm(TWARN | TERRNO, "swapoff(alreadyused) failed");
162 
163 	tst_rmdir();
164 }
165