blob: db74ba977bc5ea6cbcd79a31aec8df92c4846030 [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"
yaberauneyaa2a05e32009-11-26 14:19:32 +000036#include "linux_syscall_numbers.h"
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010037#include "safe_macros.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010038#include "tst_fs_type.h"
Stanislav Kholmanskikh4bdda042013-08-14 10:01:47 +040039#include "libswapon.h"
robbiewdcc848e2003-01-02 21:03:35 +000040
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +010041static void setup(void);
42static void cleanup(void);
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010043static void setup01(void);
44static void cleanup01(void);
robbiewdcc848e2003-01-02 21:03:35 +000045
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020046char *TCID = "swapon02";
47int TST_TOTAL = 4;
robbiewef957512003-03-13 15:33:06 +000048
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010049static uid_t nobody_uid;
50static int do_swapoff;
Cyril Hrubisb16efb82014-03-06 14:49:17 +010051static long fs_type;
robbiewef957512003-03-13 15:33:06 +000052
robbiewdcc848e2003-01-02 21:03:35 +000053static struct test_case_t {
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010054 char *err_desc;
55 int exp_errno;
56 char *exp_errval;
57 char *path;
58 void (*setup)(void);
59 void (*cleanup)(void);
60} testcases[] = {
61 {"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL},
62 {"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL},
63 {"Permission denied", EPERM, "EPERM", "./swapfile01",
64 setup01, cleanup01},
65 {"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL},
robbiewdcc848e2003-01-02 21:03:35 +000066};
67
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010068static void verify_swapon(struct test_case_t *test)
69{
70 if (test->setup)
71 test->setup();
72
73 TEST(ltp_syscall(__NR_swapon, test->path, 0));
74
75 if (test->cleanup)
76 test->cleanup();
77
78 if (TEST_RETURN == -1 && TEST_ERRNO == test->exp_errno) {
79 tst_resm(TPASS, "swapon(2) expected failure;"
80 " Got errno - %s : %s",
81 test->exp_errval, test->err_desc);
Cyril Hrubisb16efb82014-03-06 14:49:17 +010082 return;
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010083 }
Cyril Hrubisb16efb82014-03-06 14:49:17 +010084
85 if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) {
86 tst_resm(TCONF, "Swapfile on BTRFS not implemeted");
87 return;
88 }
89
90 tst_resm(TFAIL, "swapon(2) failed to produce expected error:"
91 " %d, errno: %s and got %d.", test->exp_errno,
92 test->exp_errval, TEST_ERRNO);
Cyril Hrubisa432c4d2014-03-06 14:28:30 +010093}
94
subrata_modak56207ce2009-03-23 13:35:39 +000095int main(int ac, char **av)
robbiewdcc848e2003-01-02 21:03:35 +000096{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020097 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020098 const char *msg;
robbiewdcc848e2003-01-02 21:03:35 +000099
Garrett Cooper53740502010-12-16 00:04:01 -0800100 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper62383b82010-11-22 12:02:14 -0800101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewdcc848e2003-01-02 21:03:35 +0000102
subrata_modak56207ce2009-03-23 13:35:39 +0000103 setup();
subrata_modakdaeac7a2007-05-17 14:31:45 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 tst_count = 0;
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100107 for (i = 0; i < TST_TOTAL; i++)
108 verify_swapon(testcases + i);
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100109 }
subrata_modakdaeac7a2007-05-17 14:31:45 +0000110
Garrett Cooper62383b82010-11-22 12:02:14 -0800111 cleanup();
112 tst_exit();
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100113}
robbiewdcc848e2003-01-02 21:03:35 +0000114
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100115static void setup01(void)
robbiewdcc848e2003-01-02 21:03:35 +0000116{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100117 SAFE_SETEUID(cleanup, nobody_uid);
robbiewdcc848e2003-01-02 21:03:35 +0000118}
119
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100120static void cleanup01(void)
robbiewdcc848e2003-01-02 21:03:35 +0000121{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100122 SAFE_SETEUID(cleanup, 0);
robbiewdcc848e2003-01-02 21:03:35 +0000123}
124
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100125static void setup(void)
robbiewdcc848e2003-01-02 21:03:35 +0000126{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100127 struct passwd *nobody;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiewdcc848e2003-01-02 21:03:35 +0000130
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100131 tst_require_root(NULL);
subrata_modak04c009e2007-11-28 09:38:08 +0000132
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100133 nobody = SAFE_GETPWNAM(cleanup, "nobody");
134 nobody_uid = nobody->pw_uid;
135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 tst_tmpdir();
robbiewa3cc71f2003-02-18 20:53:50 +0000137
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100138 switch ((fs_type = tst_fs_type(cleanup, "."))) {
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100139 case TST_NFS_MAGIC:
140 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000141 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100142 "Cannot do swapon on a file on %s filesystem",
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100143 tst_fs_type_name(fs_type));
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100144 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000145 }
146
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100147 SAFE_TOUCH(cleanup, "notswap", 0777, NULL);
148 make_swapfile(cleanup, "swapfile01");
149 make_swapfile(cleanup, "alreadyused");
150
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100151 if (ltp_syscall(__NR_swapon, "alreadyused", 0)) {
152 if (fs_type != TST_BTRFS_MAGIC || errno != EINVAL)
153 tst_resm(TWARN | TERRNO, "swapon(alreadyused) failed");
154 } else {
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100155 do_swapoff = 1;
Cyril Hrubisb16efb82014-03-06 14:49:17 +0100156 }
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100157
subrata_modak56207ce2009-03-23 13:35:39 +0000158 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800159}
robbiewdcc848e2003-01-02 21:03:35 +0000160
Cyril Hrubisf6f4b7f2014-02-26 17:45:31 +0100161void cleanup(void)
robbiewdcc848e2003-01-02 21:03:35 +0000162{
Cyril Hrubisa432c4d2014-03-06 14:28:30 +0100163 if (do_swapoff && ltp_syscall(__NR_swapoff, "alreadyused"))
164 tst_resm(TWARN | TERRNO, "swapoff(alreadyused) failed");
165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700167}