blob: fffddc420c1aba415e767aa4b843a32c8bd8c94e [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 */
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010017
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010018/*
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010019 * 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 Hrubis591ddcb2014-02-26 18:06:19 +010023 */
robbiew0abc81f2003-01-02 16:45:29 +000024
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 Hrubisdfda9f82014-03-06 13:37:21 +010032#include <stdlib.h>
robbiew0abc81f2003-01-02 16:45:29 +000033#include "test.h"
yaberauneyaa2a05e32009-11-26 14:19:32 +000034#include "linux_syscall_numbers.h"
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010035#include "safe_macros.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010036#include "tst_fs_type.h"
robbiew0abc81f2003-01-02 16:45:29 +000037
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010038static void setup(void);
39static void cleanup(void);
40static int setup01(void);
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010041static void cleanup01(void);
robbiew0abc81f2003-01-02 16:45:29 +000042
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020043char *TCID = "swapoff02";
44int TST_TOTAL = 3;
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010045
46static uid_t nobody_uid;
robbiew0abc81f2003-01-02 16:45:29 +000047
robbiew0abc81f2003-01-02 16:45:29 +000048static struct test_case_t {
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010049 char *err_desc;
50 int exp_errno;
51 char *exp_errval;
52 char *path;
53 int (*setup)(void);
54 void (*cleanup)(void);
robbiew0abc81f2003-01-02 16:45:29 +000055} testcase[] = {
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010056 {"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}
robbiew0abc81f2003-01-02 16:45:29 +000059};
60
subrata_modak56207ce2009-03-23 13:35:39 +000061int main(int ac, char **av)
robbiew0abc81f2003-01-02 16:45:29 +000062{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020063 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020064 const char *msg;
robbiew0abc81f2003-01-02 16:45:29 +000065
Garrett Coopera9e49f12010-12-16 10:54:03 -080066 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080067 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew0abc81f2003-01-02 16:45:29 +000068
robbiewadc1c012003-04-29 17:06:59 +000069 setup();
robbiew0abc81f2003-01-02 16:45:29 +000070
robbiewadc1c012003-04-29 17:06:59 +000071 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew0abc81f2003-01-02 16:45:29 +000072
Caspar Zhangd59a6592013-03-07 14:59:12 +080073 tst_count = 0;
robbiew0abc81f2003-01-02 16:45:29 +000074
subrata_modak56207ce2009-03-23 13:35:39 +000075 for (i = 0; i < TST_TOTAL; i++) {
robbiew0abc81f2003-01-02 16:45:29 +000076
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010077 if (testcase[i].setup)
78 testcase[i].setup();
robbiew0abc81f2003-01-02 16:45:29 +000079
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010080 TEST(ltp_syscall(__NR_swapoff, testcase[i].path));
robbiew0abc81f2003-01-02 16:45:29 +000081
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010082 if (testcase[i].cleanup)
83 testcase[i].cleanup();
84
85 if (TEST_RETURN == -1
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 && (TEST_ERRNO == testcase[i].exp_errno)) {
87 tst_resm(TPASS,
88 "swapoff(2) expected failure;"
subrata_modak56207ce2009-03-23 13:35:39 +000089 " Got errno - %s : %s",
90 testcase[i].exp_errval,
91 testcase[i].err_desc);
92
robbiewadc1c012003-04-29 17:06:59 +000093 } else {
94 tst_resm(TFAIL, "swapoff(2) failed to produce"
subrata_modak56207ce2009-03-23 13:35:39 +000095 " expected error; %d, errno"
96 ": %s and got %d",
97 testcase[i].exp_errno,
98 testcase[i].exp_errval, TEST_ERRNO);
99
100 if ((TEST_RETURN == 0) && (i == 2)) {
Jan Stancek359980f2013-02-15 10:16:05 +0100101 if (ltp_syscall
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 (__NR_swapon, "./swapfile01",
103 0) != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000104 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000105 " Failed to turn on"
106 " swap file");
robbiewadc1c012003-04-29 17:06:59 +0000107 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000108 }
robbiewadc1c012003-04-29 17:06:59 +0000109 }
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100110 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800111 }
robbiew0abc81f2003-01-02 16:45:29 +0000112
robbiewadc1c012003-04-29 17:06:59 +0000113 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800114 tst_exit();
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100115}
robbiew0abc81f2003-01-02 16:45:29 +0000116
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100117static int setup01(void)
robbiew0abc81f2003-01-02 16:45:29 +0000118{
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100119 SAFE_SETEUID(cleanup, nobody_uid);
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100120 return 0;
robbiew0abc81f2003-01-02 16:45:29 +0000121}
122
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100123static void cleanup01(void)
robbiew0abc81f2003-01-02 16:45:29 +0000124{
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100125 SAFE_SETEUID(cleanup, 0);
robbiew0abc81f2003-01-02 16:45:29 +0000126}
127
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100128static void setup(void)
robbiew0abc81f2003-01-02 16:45:29 +0000129{
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100130 long type;
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100131 struct passwd *nobody;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100132
robbiewadc1c012003-04-29 17:06:59 +0000133 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew0abc81f2003-01-02 16:45:29 +0000134
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100135 tst_require_root(NULL);
robbiew0abc81f2003-01-02 16:45:29 +0000136
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100137 nobody = SAFE_GETPWNAM(NULL, "nobody");
138 nobody_uid = nobody->pw_uid;
139
robbiewadc1c012003-04-29 17:06:59 +0000140 TEST_PAUSE;
robbiew0abc81f2003-01-02 16:45:29 +0000141
robbiewadc1c012003-04-29 17:06:59 +0000142 tst_tmpdir();
robbiew0abc81f2003-01-02 16:45:29 +0000143
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100144 switch ((type = tst_fs_type(cleanup, "."))) {
145 case TST_NFS_MAGIC:
146 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000147 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100148 "Cannot do swapoff on a file on %s filesystem",
149 tst_fs_type_name(type));
150 break;
robbiew143090a2005-01-17 22:10:09 +0000151 }
152
Xiaoguang Wang46941672014-04-25 10:02:07 +0800153 if (!tst_fs_has_free(NULL, ".", 1, TST_KB)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tst_brkm(TBROK, cleanup,
155 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000156 }
157
Cyril Hrubis912e2642014-02-26 18:11:46 +0100158 if (tst_fill_file("./swapfile01", 0x00, 1024, 1))
159 tst_brkm(TBROK, cleanup, "Failed to create swapfile");
Garrett Cooper2c282152010-12-16 00:55:50 -0800160}
robbiew0abc81f2003-01-02 16:45:29 +0000161
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100162static void cleanup(void)
robbiew0abc81f2003-01-02 16:45:29 +0000163{
robbiewadc1c012003-04-29 17:06:59 +0000164 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700165}