blob: aef9e36a67cf6336bc5d98279f973a183106b841 [file] [log] [blame]
robbiewa3cc71f2003-02-18 20:53:50 +00001/* Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
robbiewdcc848e2003-01-02 21:03:35 +00002 *
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
Wanlong Gaofed96412012-10-24 10:10:29 +080012 * with this program; if not, write the Free Software Foundation, Inc.,
13 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiewdcc848e2003-01-02 21:03:35 +000014 *
15 */
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +010016/*
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010017 * 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
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +010022 */
robbiewdcc848e2003-01-02 21:03:35 +000023
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>
robbiewdcc848e2003-01-02 21:03:35 +000030#include <fcntl.h>
31#include <pwd.h>
32#include <string.h>
robbiewef957512003-03-13 15:33:06 +000033#include <sys/utsname.h>
robbiewdcc848e2003-01-02 21:03:35 +000034#include <signal.h>
35#include "test.h"
36#include "usctest.h"
yaberauneyaa2a05e32009-11-26 14:19:32 +000037#include "linux_syscall_numbers.h"
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010038#include "safe_macros.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010039#include "tst_fs_type.h"
Stanislav Kholmanskikh4bdda042013-08-14 10:01:47 +040040#include "libswapon.h"
robbiewdcc848e2003-01-02 21:03:35 +000041
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +010042static void setup(void);
43static void cleanup(void);
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010044static void setup01(void);
45static void cleanup01(void);
robbiewdcc848e2003-01-02 21:03:35 +000046
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020047char *TCID = "swapon02";
48int TST_TOTAL = 4;
robbiewef957512003-03-13 15:33:06 +000049
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010050static uid_t nobody_uid;
51static int do_swapoff;
Cyril Hrubisb16efb82014-03-06 14:49:17 +010052static long fs_type;
robbiewef957512003-03-13 15:33:06 +000053
subrata_modak56207ce2009-03-23 13:35:39 +000054static int exp_enos[] = { EPERM, EINVAL, ENOENT, EBUSY, 0 };
robbiewdcc848e2003-01-02 21:03:35 +000055
56static struct test_case_t {
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010057 char *err_desc;
58 int exp_errno;
59 char *exp_errval;
60 char *path;
61 void (*setup)(void);
62 void (*cleanup)(void);
63} testcases[] = {
64 {"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL},
65 {"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL},
66 {"Permission denied", EPERM, "EPERM", "./swapfile01",
67 setup01, cleanup01},
68 {"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL},
robbiewdcc848e2003-01-02 21:03:35 +000069};
70
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010071static void verify_swapon(struct test_case_t *test)
72{
73 if (test->setup)
74 test->setup();
75
76 TEST(ltp_syscall(__NR_swapon, test->path, 0));
77
78 if (test->cleanup)
79 test->cleanup();
80
81 if (TEST_RETURN == -1 && TEST_ERRNO == test->exp_errno) {
82 tst_resm(TPASS, "swapon(2) expected failure;"
83 " Got errno - %s : %s",
84 test->exp_errval, test->err_desc);
Cyril Hrubisb16efb82014-03-06 14:49:17 +010085 return;
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010086 }
Cyril Hrubisb16efb82014-03-06 14:49:17 +010087
88 if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) {
89 tst_resm(TCONF, "Swapfile on BTRFS not implemeted");
90 return;
91 }
92
93 tst_resm(TFAIL, "swapon(2) failed to produce expected error:"
94 " %d, errno: %s and got %d.", test->exp_errno,
95 test->exp_errval, TEST_ERRNO);
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010096}
97
subrata_modak56207ce2009-03-23 13:35:39 +000098int main(int ac, char **av)
robbiewdcc848e2003-01-02 21:03:35 +000099{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200100 int lc, i;
101 char *msg;
robbiewdcc848e2003-01-02 21:03:35 +0000102
Garrett Cooper53740502010-12-16 00:04:01 -0800103 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper62383b82010-11-22 12:02:14 -0800104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewdcc848e2003-01-02 21:03:35 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 setup();
subrata_modakdaeac7a2007-05-17 14:31:45 +0000107
subrata_modak56207ce2009-03-23 13:35:39 +0000108 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100110 for (i = 0; i < TST_TOTAL; i++)
111 verify_swapon(testcases + i);
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100112 }
subrata_modakdaeac7a2007-05-17 14:31:45 +0000113
Garrett Cooper62383b82010-11-22 12:02:14 -0800114 cleanup();
115 tst_exit();
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100116}
robbiewdcc848e2003-01-02 21:03:35 +0000117
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100118static void setup01(void)
robbiewdcc848e2003-01-02 21:03:35 +0000119{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100120 SAFE_SETEUID(cleanup, nobody_uid);
robbiewdcc848e2003-01-02 21:03:35 +0000121}
122
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100123static void cleanup01(void)
robbiewdcc848e2003-01-02 21:03:35 +0000124{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100125 SAFE_SETEUID(cleanup, 0);
robbiewdcc848e2003-01-02 21:03:35 +0000126}
127
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100128static void setup(void)
robbiewdcc848e2003-01-02 21:03:35 +0000129{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100130 struct passwd *nobody;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100131
subrata_modak56207ce2009-03-23 13:35:39 +0000132 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiewdcc848e2003-01-02 21:03:35 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 TEST_EXP_ENOS(exp_enos);
robbiewdcc848e2003-01-02 21:03:35 +0000135
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100136 tst_require_root(NULL);
subrata_modak04c009e2007-11-28 09:38:08 +0000137
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100138 nobody = SAFE_GETPWNAM(cleanup, "nobody");
139 nobody_uid = nobody->pw_uid;
140
subrata_modak56207ce2009-03-23 13:35:39 +0000141 tst_tmpdir();
robbiewa3cc71f2003-02-18 20:53:50 +0000142
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100143 switch ((fs_type = tst_fs_type(cleanup, "."))) {
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100144 case TST_NFS_MAGIC:
145 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000146 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100147 "Cannot do swapon on a file on %s filesystem",
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100148 tst_fs_type_name(fs_type));
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100149 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000150 }
151
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100152 SAFE_TOUCH(cleanup, "notswap", 0777, NULL);
153 make_swapfile(cleanup, "swapfile01");
154 make_swapfile(cleanup, "alreadyused");
155
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100156 if (ltp_syscall(__NR_swapon, "alreadyused", 0)) {
157 if (fs_type != TST_BTRFS_MAGIC || errno != EINVAL)
158 tst_resm(TWARN | TERRNO, "swapon(alreadyused) failed");
159 } else {
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100160 do_swapoff = 1;
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100161 }
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100162
subrata_modak56207ce2009-03-23 13:35:39 +0000163 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800164}
robbiewdcc848e2003-01-02 21:03:35 +0000165
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100166void cleanup(void)
robbiewdcc848e2003-01-02 21:03:35 +0000167{
subrata_modak56207ce2009-03-23 13:35:39 +0000168 TEST_CLEANUP;
robbiewdcc848e2003-01-02 21:03:35 +0000169
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100170 if (do_swapoff && ltp_syscall(__NR_swapoff, "alreadyused"))
171 tst_resm(TWARN | TERRNO, "swapoff(alreadyused) failed");
172
subrata_modak56207ce2009-03-23 13:35:39 +0000173 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700174}