• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 
18 /*
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
23  */
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>
32 #include <stdlib.h>
33 #include "test.h"
34 #include "lapi/syscalls.h"
35 #include "safe_macros.h"
36 #include "../swapon/libswapon.h"
37 
38 static void setup(void);
39 static void cleanup(void);
40 static int setup01(void);
41 static void cleanup01(void);
42 
43 char *TCID = "swapoff02";
44 int TST_TOTAL = 3;
45 
46 static uid_t nobody_uid;
47 
48 static struct test_case_t {
49 	char *err_desc;
50 	int exp_errno;
51 	char *exp_errval;
52 	char *path;
53 	int (*setup)(void);
54 	void (*cleanup)(void);
55 } testcase[] = {
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}
59 };
60 
main(int ac,char ** av)61 int main(int ac, char **av)
62 {
63 	int lc, i;
64 
65 	tst_parse_opts(ac, av, NULL, NULL);
66 
67 	setup();
68 
69 	for (lc = 0; TEST_LOOPING(lc); lc++) {
70 
71 		tst_count = 0;
72 
73 		for (i = 0; i < TST_TOTAL; i++) {
74 
75 			if (testcase[i].setup)
76 				testcase[i].setup();
77 
78 			TEST(ltp_syscall(__NR_swapoff, testcase[i].path));
79 
80 			if (testcase[i].cleanup)
81 				testcase[i].cleanup();
82 
83 			if (TEST_RETURN == -1
84 			    && (TEST_ERRNO == testcase[i].exp_errno)) {
85 				tst_resm(TPASS,
86 					 "swapoff(2) expected failure;"
87 					 " Got errno - %s : %s",
88 					 testcase[i].exp_errval,
89 					 testcase[i].err_desc);
90 
91 			} else {
92 				tst_resm(TFAIL, "swapoff(2) failed to produce"
93 					 " expected error; %d, errno"
94 					 ": %s and got %d",
95 					 testcase[i].exp_errno,
96 					 testcase[i].exp_errval, TEST_ERRNO);
97 
98 				if ((TEST_RETURN == 0) && (i == 2)) {
99 					if (ltp_syscall
100 					    (__NR_swapon, "./swapfile01",
101 					     0) != 0) {
102 						tst_brkm(TBROK, cleanup,
103 							 " Failed to turn on"
104 							 " swap file");
105 					}
106 				}
107 			}
108 		}
109 	}
110 
111 	cleanup();
112 	tst_exit();
113 }
114 
setup01(void)115 static int setup01(void)
116 {
117 	SAFE_SETEUID(cleanup, nobody_uid);
118 	return 0;
119 }
120 
cleanup01(void)121 static void cleanup01(void)
122 {
123 	SAFE_SETEUID(cleanup, 0);
124 }
125 
setup(void)126 static void setup(void)
127 {
128 	struct passwd *nobody;
129 
130 	tst_sig(FORK, DEF_HANDLER, cleanup);
131 
132 	tst_require_root();
133 
134 	nobody = SAFE_GETPWNAM(NULL, "nobody");
135 	nobody_uid = nobody->pw_uid;
136 
137 	TEST_PAUSE;
138 
139 	tst_tmpdir();
140 
141 	is_swap_supported(cleanup, "./tstswap");
142 
143 	if (!tst_fs_has_free(NULL, ".", 1, TST_KB)) {
144 		tst_brkm(TBROK, cleanup,
145 			 "Insufficient disk space to create swap file");
146 	}
147 
148 	if (tst_fill_file("./swapfile01", 0x00, 1024, 1))
149 		tst_brkm(TBROK, cleanup, "Failed to create swapfile");
150 }
151 
cleanup(void)152 static void cleanup(void)
153 {
154 	tst_rmdir();
155 }
156