blob: 6d993d245985c81c080888e5cc886d54be487d26 [file] [log] [blame]
robbiew0abc81f2003-01-02 16:45:29 +00001/*
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 Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew0abc81f2003-01-02 16:45:29 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewadc1c012003-04-29 17:06:59 +000019 * TEST IDENTIFIER : swapoff02
robbiew0abc81f2003-01-02 16:45:29 +000020 *
subrata_modak4bb656a2009-02-26 12:02:09 +000021 *
robbiewadc1c012003-04-29 17:06:59 +000022 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000023 *
robbiewadc1c012003-04-29 17:06:59 +000024 * TEST TITLE : Test checking for basic error conditions
25 * for swapoff(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
27 * TEST CASE TOTAL : 3
28 *
robbiewadc1c012003-04-29 17:06:59 +000029 * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000030 *
robbiew0abc81f2003-01-02 16:45:29 +000031 * SIGNALS
robbiewadc1c012003-04-29 17:06:59 +000032 * Uses SIGUSR1 to pause before test if option set.
33 * (See the parse_opts(3) man page).
robbiew0abc81f2003-01-02 16:45:29 +000034 *
35 * DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000036 * This test case checks whether swapoff(2) system call returns
robbiewadc1c012003-04-29 17:06:59 +000037 * 1. EINVAL when the path does not exist
38 * 2. ENOENT when the path exists but is invalid
39 * 3. EPERM when user is not a superuser
subrata_modakbdbaec52009-02-26 12:14:51 +000040 *
robbiewadc1c012003-04-29 17:06:59 +000041 * Setup:
42 * Setup signal handling.
43 * Pause for SIGUSR1 if option specified.
44 * 1. For testing error on invalid user, change the effective uid
subrata_modak56207ce2009-03-23 13:35:39 +000045 * $
robbiewadc1c012003-04-29 17:06:59 +000046 * Test:
47 * Loop if the proper options are given.
48 * Execute system call.
49 * Check return code, if system call fails with errno == expected errno
50 * Issue syscall passed with expected errno
subrata_modak4bb656a2009-02-26 12:02:09 +000051 * Otherwise,
robbiewadc1c012003-04-29 17:06:59 +000052 * Issue syscall failed to produce expected errno
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
robbiewadc1c012003-04-29 17:06:59 +000054 * Cleanup:
55 * Do cleanup for the test.
subrata_modak56207ce2009-03-23 13:35:39 +000056 * $
robbiew0abc81f2003-01-02 16:45:29 +000057 * USAGE: <for command-line>
58 * swapoff02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p]
subrata_modak4bb656a2009-02-26 12:02:09 +000059 * where
robbiewadc1c012003-04-29 17:06:59 +000060 * -c n : Run n copies simultaneously
61 * -e : Turn on errno logging.
62 * -i n : Execute test n times.
63 * -I x : Execute test for x seconds.
64 * -p : Pause for SIGUSR1 before starting
65 * -P x : Pause for x seconds between iterations.
66 * -t : Turn on syscall timing.
Garrett Cooper2c282152010-12-16 00:55:50 -080067 *
robbiew0abc81f2003-01-02 16:45:29 +000068 *RESTRICTIONS:
69 *Incompatible with kernel versions below 2.1.35.
robbiew143090a2005-01-17 22:10:09 +000070 *
71 *CHANGES:
72 * 2005/01/01 Add extra check to stop test if insufficient disk space in dir
73 * -Ricky Ng-Adam (rngadam@yahoo.com)
74 * 2005/01/01 Add extra check to stop test if swap file is on tmpfs
75 * -Ricky Ng-Adam (rngadam@yahoo.com)
robbiew0abc81f2003-01-02 16:45:29 +000076 *****************************************************************************/
77
78#include <unistd.h>
79#include <errno.h>
80#include <sys/types.h>
81#include <sys/stat.h>
82#include <fcntl.h>
83#include <pwd.h>
84#include <string.h>
85#include "test.h"
86#include "usctest.h"
87#include <stdlib.h>
yaberauneya77772ef2009-10-17 06:53:58 +000088#include "config.h"
yaberauneyaa2a05e32009-11-26 14:19:32 +000089#include "linux_syscall_numbers.h"
90#include "swaponoff.h"
robbiew0abc81f2003-01-02 16:45:29 +000091
92static void setup();
93static void cleanup();
94static int setup01();
95static int cleanup01();
96static int setup02();
robbiew0abc81f2003-01-02 16:45:29 +000097
subrata_modak56207ce2009-03-23 13:35:39 +000098char *TCID = "swapoff02"; /* Test program identifier. */
robbiewadc1c012003-04-29 17:06:59 +000099int TST_TOTAL = 3; /* Total number of test cases. */
robbiew0abc81f2003-01-02 16:45:29 +0000100char nobody_uid[] = "nobody";
101struct passwd *ltpuser;
subrata_modak04c009e2007-11-28 09:38:08 +0000102int need_swapfile_cleanup = 0; /* attempt to swapoff in cleanup */
robbiew0abc81f2003-01-02 16:45:29 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104static int exp_enos[] = { EPERM, EINVAL, ENOENT, 0 };
robbiew0abc81f2003-01-02 16:45:29 +0000105
106static struct test_case_t {
robbiewadc1c012003-04-29 17:06:59 +0000107 char *err_desc; /* error description */
subrata_modak56207ce2009-03-23 13:35:39 +0000108 int exp_errno; /* expected error number */
109 char *exp_errval; /* Expected errorvalue string */
robbiewadc1c012003-04-29 17:06:59 +0000110 char *path; /* path for swapon */
subrata_modak56207ce2009-03-23 13:35:39 +0000111 int (*setupfunc) (); /* Test setup function */
112 int (*cleanfunc) (); /* Test cleanup function */
robbiew0abc81f2003-01-02 16:45:29 +0000113} testcase[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000114 {
115 "path does not exist", ENOENT, "ENOENT", "./abcd", NULL, NULL}, {
116 "Invalid path", EINVAL, "EINVAL ", "./nofile", setup02, NULL}, {
117 "Permission denied", EPERM, "EPERM ", "./swapfile01",
118 setup01, cleanup01}
robbiew0abc81f2003-01-02 16:45:29 +0000119};
120
subrata_modak56207ce2009-03-23 13:35:39 +0000121int main(int ac, char **av)
robbiew0abc81f2003-01-02 16:45:29 +0000122{
123
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200124 int lc, i;
125 char *msg;
robbiew0abc81f2003-01-02 16:45:29 +0000126
Garrett Coopera9e49f12010-12-16 10:54:03 -0800127 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800128 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew0abc81f2003-01-02 16:45:29 +0000129
robbiewadc1c012003-04-29 17:06:59 +0000130 setup();
robbiew0abc81f2003-01-02 16:45:29 +0000131
robbiewadc1c012003-04-29 17:06:59 +0000132 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew0abc81f2003-01-02 16:45:29 +0000133
robbiewadc1c012003-04-29 17:06:59 +0000134 Tst_count = 0;
robbiew0abc81f2003-01-02 16:45:29 +0000135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 for (i = 0; i < TST_TOTAL; i++) {
robbiew0abc81f2003-01-02 16:45:29 +0000137
subrata_modak56207ce2009-03-23 13:35:39 +0000138 if (testcase[i].setupfunc &&
139 testcase[i].setupfunc() == -1) {
robbiewadc1c012003-04-29 17:06:59 +0000140 tst_resm(TWARN, "Failed to setup test %d."
subrata_modak56207ce2009-03-23 13:35:39 +0000141 " Skipping test", i);
robbiewadc1c012003-04-29 17:06:59 +0000142 continue;
143 } else {
Jan Stancek359980f2013-02-15 10:16:05 +0100144 TEST(ltp_syscall(__NR_swapoff,
145 testcase[i].path));
robbiewadc1c012003-04-29 17:06:59 +0000146 }
robbiew0abc81f2003-01-02 16:45:29 +0000147
subrata_modak56207ce2009-03-23 13:35:39 +0000148 if (testcase[i].cleanfunc &&
149 testcase[i].cleanfunc() == -1) {
150 tst_brkm(TBROK, cleanup, "cleanup failed,"
151 " quitting the test");
152 }
robbiew0abc81f2003-01-02 16:45:29 +0000153
robbiewadc1c012003-04-29 17:06:59 +0000154 /* check return code */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 if ((TEST_RETURN == -1)
156 && (TEST_ERRNO == testcase[i].exp_errno)) {
157 tst_resm(TPASS,
158 "swapoff(2) expected failure;"
subrata_modak56207ce2009-03-23 13:35:39 +0000159 " Got errno - %s : %s",
160 testcase[i].exp_errval,
161 testcase[i].err_desc);
162
robbiewadc1c012003-04-29 17:06:59 +0000163 } else {
164 tst_resm(TFAIL, "swapoff(2) failed to produce"
subrata_modak56207ce2009-03-23 13:35:39 +0000165 " expected error; %d, errno"
166 ": %s and got %d",
167 testcase[i].exp_errno,
168 testcase[i].exp_errval, TEST_ERRNO);
169
170 if ((TEST_RETURN == 0) && (i == 2)) {
Jan Stancek359980f2013-02-15 10:16:05 +0100171 if (ltp_syscall
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 (__NR_swapon, "./swapfile01",
173 0) != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000174 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000175 " Failed to turn on"
176 " swap file");
robbiewadc1c012003-04-29 17:06:59 +0000177 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000178 }
robbiewadc1c012003-04-29 17:06:59 +0000179 }
robbiew0abc81f2003-01-02 16:45:29 +0000180
robbiewadc1c012003-04-29 17:06:59 +0000181 TEST_ERROR_LOG(TEST_ERRNO);
subrata_modak56207ce2009-03-23 13:35:39 +0000182 } /*End of TEST LOOPS */
Garrett Cooper2c282152010-12-16 00:55:50 -0800183 }
robbiew0abc81f2003-01-02 16:45:29 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 /*Clean up and exit */
robbiewadc1c012003-04-29 17:06:59 +0000186 cleanup();
robbiew0abc81f2003-01-02 16:45:29 +0000187
Garrett Cooper53740502010-12-16 00:04:01 -0800188 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000189} /*End of main */
robbiew0abc81f2003-01-02 16:45:29 +0000190
191/*
192 * setup01() - This function sets the user as nobody
193 */
subrata_modak56207ce2009-03-23 13:35:39 +0000194int setup01()
robbiew0abc81f2003-01-02 16:45:29 +0000195{
subrata_modak56207ce2009-03-23 13:35:39 +0000196 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
robbiewadc1c012003-04-29 17:06:59 +0000197 tst_resm(TWARN, "\"nobody\" user not present. skipping test");
198 return -1;
199 }
robbiew0abc81f2003-01-02 16:45:29 +0000200
robbiewadc1c012003-04-29 17:06:59 +0000201 if (seteuid(ltpuser->pw_uid) == -1) {
202 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000203 "to set the effective uid to %d", ltpuser->pw_uid);
robbiewadc1c012003-04-29 17:06:59 +0000204 perror("seteuid");
205 return -1;
206 }
robbiew0abc81f2003-01-02 16:45:29 +0000207
subrata_modak56207ce2009-03-23 13:35:39 +0000208 return 0; /* user switched to nobody */
robbiew0abc81f2003-01-02 16:45:29 +0000209}
210
211/*
212 * cleanup01() - switch back to user root
213 */
subrata_modak56207ce2009-03-23 13:35:39 +0000214int cleanup01()
robbiew0abc81f2003-01-02 16:45:29 +0000215{
subrata_modak56207ce2009-03-23 13:35:39 +0000216 if (seteuid(0) == -1) {
robbiewadc1c012003-04-29 17:06:59 +0000217 tst_brkm(TBROK, cleanup, "seteuid failed to set uid to root");
218 perror("seteuid");
219 return -1;
220 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000221
robbiewadc1c012003-04-29 17:06:59 +0000222 return 0;
robbiew0abc81f2003-01-02 16:45:29 +0000223}
224
subrata_modak56207ce2009-03-23 13:35:39 +0000225int setup02()
robbiew0abc81f2003-01-02 16:45:29 +0000226{
robbiewadc1c012003-04-29 17:06:59 +0000227 int fd;
228 fd = creat("nofile", S_IRWXU);
229 if (fd == -1)
230 tst_resm(TWARN, "Failed to create temporary file");
231 return 0;
robbiew0abc81f2003-01-02 16:45:29 +0000232}
233
234/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000235void setup()
robbiew0abc81f2003-01-02 16:45:29 +0000236{
Garrett Cooper2c282152010-12-16 00:55:50 -0800237
robbiewadc1c012003-04-29 17:06:59 +0000238 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew0abc81f2003-01-02 16:45:29 +0000239
robbiewadc1c012003-04-29 17:06:59 +0000240 /* set the expected errnos... */
241 TEST_EXP_ENOS(exp_enos);
robbiew0abc81f2003-01-02 16:45:29 +0000242
subrata_modak56207ce2009-03-23 13:35:39 +0000243 /* Check whether we are root */
robbiewadc1c012003-04-29 17:06:59 +0000244 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800245 tst_brkm(TBROK, NULL, "Test must be run as root");
robbiewadc1c012003-04-29 17:06:59 +0000246 }
robbiew0abc81f2003-01-02 16:45:29 +0000247
robbiewadc1c012003-04-29 17:06:59 +0000248 TEST_PAUSE;
robbiew0abc81f2003-01-02 16:45:29 +0000249
robbiewadc1c012003-04-29 17:06:59 +0000250 tst_tmpdir();
robbiew0abc81f2003-01-02 16:45:29 +0000251
subrata_modak56207ce2009-03-23 13:35:39 +0000252 if (tst_is_cwd_tmpfs()) {
253 tst_brkm(TCONF, cleanup,
254 "Cannot do swapon on a file located on a tmpfs filesystem");
subrata_modak04c009e2007-11-28 09:38:08 +0000255 }
256
subrata_modak56207ce2009-03-23 13:35:39 +0000257 if (tst_is_cwd_nfs()) {
258 tst_brkm(TCONF, cleanup,
259 "Cannot do swapon on a file located on a nfs filesystem");
robbiew143090a2005-01-17 22:10:09 +0000260 }
261
subrata_modak56207ce2009-03-23 13:35:39 +0000262 if (!tst_cwd_has_free(65536)) {
263 tst_brkm(TBROK, cleanup,
264 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000265 }
266
subrata_modak56207ce2009-03-23 13:35:39 +0000267 /*create file */
268 if (system
269 ("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 > tmpfile"
270 " 2>&1") != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000271 tst_brkm(TBROK, cleanup, "Failed to create file for swap");
272 }
robbiew0abc81f2003-01-02 16:45:29 +0000273
subrata_modak56207ce2009-03-23 13:35:39 +0000274 /* make above file a swap file */
275 if (system("mkswap ./swapfile01 > tmpfile 2>&1") != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000276 tst_brkm(TBROK, cleanup, "Failed to make swapfile");
277 }
robbiew0abc81f2003-01-02 16:45:29 +0000278
Jan Stancek359980f2013-02-15 10:16:05 +0100279 if (ltp_syscall(__NR_swapon, "./swapfile01", 0) != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000280 tst_brkm(TBROK, cleanup, "Failed to turn on the swap file."
subrata_modak56207ce2009-03-23 13:35:39 +0000281 " skipping the test iteration");
subrata_modak04c009e2007-11-28 09:38:08 +0000282 }
283
284 need_swapfile_cleanup = 1;
robbiew0abc81f2003-01-02 16:45:29 +0000285
Garrett Cooper2c282152010-12-16 00:55:50 -0800286}
robbiew0abc81f2003-01-02 16:45:29 +0000287
288/*
289* cleanup() - Performs one time cleanup for this test at
290* completion or premature exit
291*/
subrata_modak56207ce2009-03-23 13:35:39 +0000292void cleanup()
robbiew0abc81f2003-01-02 16:45:29 +0000293{
robbiewadc1c012003-04-29 17:06:59 +0000294 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000295 * print timing stats if that option was specified.
296 * print errno log if that option was specified.
297 */
robbiewadc1c012003-04-29 17:06:59 +0000298 TEST_CLEANUP;
robbiew0abc81f2003-01-02 16:45:29 +0000299
Wanlong Gao354ebb42012-12-07 10:10:04 +0800300 if (need_swapfile_cleanup
Jan Stancek359980f2013-02-15 10:16:05 +0100301 && (ltp_syscall(__NR_swapoff, "./swapfile01") != 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800302 tst_resm(TWARN,
303 " Failed to turn off swap file. System reboot"
subrata_modak56207ce2009-03-23 13:35:39 +0000304 " after execution of LTP test suite is"
305 " recommended.");
robbiewadc1c012003-04-29 17:06:59 +0000306 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800307
robbiewadc1c012003-04-29 17:06:59 +0000308 tst_rmdir();
robbiew0abc81f2003-01-02 16:45:29 +0000309
Chris Dearmanec6edca2012-10-17 19:54:01 -0700310}