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
37 static void setup(void);
38 static void cleanup(void);
39 static int setup01(void);
40 static void cleanup01(void);
41
42 char *TCID = "swapoff02";
43 int TST_TOTAL = 3;
44
45 static uid_t nobody_uid;
46
47 static struct test_case_t {
48 char *err_desc;
49 int exp_errno;
50 char *exp_errval;
51 char *path;
52 int (*setup)(void);
53 void (*cleanup)(void);
54 } testcase[] = {
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}
58 };
59
main(int ac,char ** av)60 int main(int ac, char **av)
61 {
62 int lc, i;
63
64 tst_parse_opts(ac, av, NULL, NULL);
65
66 setup();
67
68 for (lc = 0; TEST_LOOPING(lc); lc++) {
69
70 tst_count = 0;
71
72 for (i = 0; i < TST_TOTAL; i++) {
73
74 if (testcase[i].setup)
75 testcase[i].setup();
76
77 TEST(ltp_syscall(__NR_swapoff, testcase[i].path));
78
79 if (testcase[i].cleanup)
80 testcase[i].cleanup();
81
82 if (TEST_RETURN == -1
83 && (TEST_ERRNO == testcase[i].exp_errno)) {
84 tst_resm(TPASS,
85 "swapoff(2) expected failure;"
86 " Got errno - %s : %s",
87 testcase[i].exp_errval,
88 testcase[i].err_desc);
89
90 } else {
91 tst_resm(TFAIL, "swapoff(2) failed to produce"
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)) {
98 if (ltp_syscall
99 (__NR_swapon, "./swapfile01",
100 0) != 0) {
101 tst_brkm(TBROK, cleanup,
102 " Failed to turn on"
103 " swap file");
104 }
105 }
106 }
107 }
108 }
109
110 cleanup();
111 tst_exit();
112 }
113
setup01(void)114 static int setup01(void)
115 {
116 SAFE_SETEUID(cleanup, nobody_uid);
117 return 0;
118 }
119
cleanup01(void)120 static void cleanup01(void)
121 {
122 SAFE_SETEUID(cleanup, 0);
123 }
124
setup(void)125 static void setup(void)
126 {
127 long type;
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 switch ((type = tst_fs_type(cleanup, "."))) {
142 case TST_NFS_MAGIC:
143 case TST_TMPFS_MAGIC:
144 tst_brkm(TCONF, cleanup,
145 "Cannot do swapoff on a file on %s filesystem",
146 tst_fs_type_name(type));
147 break;
148 }
149
150 if (!tst_fs_has_free(NULL, ".", 1, TST_KB)) {
151 tst_brkm(TBROK, cleanup,
152 "Insufficient disk space to create swap file");
153 }
154
155 if (tst_fill_file("./swapfile01", 0x00, 1024, 1))
156 tst_brkm(TBROK, cleanup, "Failed to create swapfile");
157 }
158
cleanup(void)159 static void cleanup(void)
160 {
161 tst_rmdir();
162 }
163