blob: b5c6312a141bf56da6318d95e3c010740e1122fb [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"
Richard Palethorpe817d8092017-07-24 14:48:25 +020034#include "lapi/syscalls.h"
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010035#include "safe_macros.h"
robbiew0abc81f2003-01-02 16:45:29 +000036
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010037static void setup(void);
38static void cleanup(void);
39static int setup01(void);
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010040static void cleanup01(void);
robbiew0abc81f2003-01-02 16:45:29 +000041
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020042char *TCID = "swapoff02";
43int TST_TOTAL = 3;
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010044
45static uid_t nobody_uid;
robbiew0abc81f2003-01-02 16:45:29 +000046
robbiew0abc81f2003-01-02 16:45:29 +000047static struct test_case_t {
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010048 char *err_desc;
49 int exp_errno;
50 char *exp_errval;
51 char *path;
52 int (*setup)(void);
53 void (*cleanup)(void);
robbiew0abc81f2003-01-02 16:45:29 +000054} testcase[] = {
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010055 {"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}
robbiew0abc81f2003-01-02 16:45:29 +000058};
59
subrata_modak56207ce2009-03-23 13:35:39 +000060int main(int ac, char **av)
robbiew0abc81f2003-01-02 16:45:29 +000061{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020062 int lc, i;
robbiew0abc81f2003-01-02 16:45:29 +000063
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010064 tst_parse_opts(ac, av, NULL, NULL);
robbiew0abc81f2003-01-02 16:45:29 +000065
robbiewadc1c012003-04-29 17:06:59 +000066 setup();
robbiew0abc81f2003-01-02 16:45:29 +000067
robbiewadc1c012003-04-29 17:06:59 +000068 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew0abc81f2003-01-02 16:45:29 +000069
Caspar Zhangd59a6592013-03-07 14:59:12 +080070 tst_count = 0;
robbiew0abc81f2003-01-02 16:45:29 +000071
subrata_modak56207ce2009-03-23 13:35:39 +000072 for (i = 0; i < TST_TOTAL; i++) {
robbiew0abc81f2003-01-02 16:45:29 +000073
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010074 if (testcase[i].setup)
75 testcase[i].setup();
robbiew0abc81f2003-01-02 16:45:29 +000076
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010077 TEST(ltp_syscall(__NR_swapoff, testcase[i].path));
robbiew0abc81f2003-01-02 16:45:29 +000078
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010079 if (testcase[i].cleanup)
80 testcase[i].cleanup();
81
82 if (TEST_RETURN == -1
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 && (TEST_ERRNO == testcase[i].exp_errno)) {
84 tst_resm(TPASS,
85 "swapoff(2) expected failure;"
subrata_modak56207ce2009-03-23 13:35:39 +000086 " Got errno - %s : %s",
87 testcase[i].exp_errval,
88 testcase[i].err_desc);
89
robbiewadc1c012003-04-29 17:06:59 +000090 } else {
91 tst_resm(TFAIL, "swapoff(2) failed to produce"
subrata_modak56207ce2009-03-23 13:35:39 +000092 " 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)) {
Jan Stancek359980f2013-02-15 10:16:05 +010098 if (ltp_syscall
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 (__NR_swapon, "./swapfile01",
100 0) != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000101 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000102 " Failed to turn on"
103 " swap file");
robbiewadc1c012003-04-29 17:06:59 +0000104 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000105 }
robbiewadc1c012003-04-29 17:06:59 +0000106 }
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100107 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800108 }
robbiew0abc81f2003-01-02 16:45:29 +0000109
robbiewadc1c012003-04-29 17:06:59 +0000110 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800111 tst_exit();
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100112}
robbiew0abc81f2003-01-02 16:45:29 +0000113
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100114static int setup01(void)
robbiew0abc81f2003-01-02 16:45:29 +0000115{
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100116 SAFE_SETEUID(cleanup, nobody_uid);
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100117 return 0;
robbiew0abc81f2003-01-02 16:45:29 +0000118}
119
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100120static void cleanup01(void)
robbiew0abc81f2003-01-02 16:45:29 +0000121{
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100122 SAFE_SETEUID(cleanup, 0);
robbiew0abc81f2003-01-02 16:45:29 +0000123}
124
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100125static void setup(void)
robbiew0abc81f2003-01-02 16:45:29 +0000126{
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100127 long type;
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100128 struct passwd *nobody;
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100129
robbiewadc1c012003-04-29 17:06:59 +0000130 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew0abc81f2003-01-02 16:45:29 +0000131
Cyril Hrubisd1e794d2015-07-30 23:52:51 +0200132 tst_require_root();
robbiew0abc81f2003-01-02 16:45:29 +0000133
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100134 nobody = SAFE_GETPWNAM(NULL, "nobody");
135 nobody_uid = nobody->pw_uid;
136
robbiewadc1c012003-04-29 17:06:59 +0000137 TEST_PAUSE;
robbiew0abc81f2003-01-02 16:45:29 +0000138
robbiewadc1c012003-04-29 17:06:59 +0000139 tst_tmpdir();
robbiew0abc81f2003-01-02 16:45:29 +0000140
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100141 switch ((type = tst_fs_type(cleanup, "."))) {
142 case TST_NFS_MAGIC:
143 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +0000144 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +0100145 "Cannot do swapoff on a file on %s filesystem",
146 tst_fs_type_name(type));
147 break;
robbiew143090a2005-01-17 22:10:09 +0000148 }
149
Xiaoguang Wang46941672014-04-25 10:02:07 +0800150 if (!tst_fs_has_free(NULL, ".", 1, TST_KB)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000151 tst_brkm(TBROK, cleanup,
152 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000153 }
154
Cyril Hrubis912e2642014-02-26 18:11:46 +0100155 if (tst_fill_file("./swapfile01", 0x00, 1024, 1))
156 tst_brkm(TBROK, cleanup, "Failed to create swapfile");
Garrett Cooper2c282152010-12-16 00:55:50 -0800157}
robbiew0abc81f2003-01-02 16:45:29 +0000158
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100159static void cleanup(void)
robbiew0abc81f2003-01-02 16:45:29 +0000160{
robbiewadc1c012003-04-29 17:06:59 +0000161 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700162}