1 /*
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
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 
18 /*
19  * Check that swapoff() succeeds.
20  */
21 
22 #include <unistd.h>
23 #include "test.h"
24 #include <errno.h>
25 #include <stdlib.h>
26 #include "config.h"
27 #include "lapi/syscalls.h"
28 
29 static void setup(void);
30 static void cleanup(void);
31 static void verify_swapoff(void);
32 
33 char *TCID = "swapoff01";
34 int TST_TOTAL = 1;
35 
36 static long fs_type;
37 
38 int main(int ac, char **av)
39 {
40 	int lc;
41 
42 	tst_parse_opts(ac, av, NULL, NULL);
43 
44 	setup();
45 
46 	for (lc = 0; TEST_LOOPING(lc); lc++) {
47 		tst_count = 0;
48 		verify_swapoff();
49 	}
50 
51 	cleanup();
52 	tst_exit();
53 }
54 
55 static void verify_swapoff(void)
56 {
57 	if (ltp_syscall(__NR_swapon, "./swapfile01", 0) != 0) {
58 		if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) {
59 			tst_brkm(TCONF, cleanup,
60 			         "Swapfiles on BTRFS are not implemented");
61 		}
62 
63 		tst_resm(TBROK, "Failed to turn on the swap file"
64 			 ", skipping test iteration");
65 		return;
66 	}
67 
68 	TEST(ltp_syscall(__NR_swapoff, "./swapfile01"));
69 
70 	if (TEST_RETURN == -1) {
71 		tst_resm(TFAIL | TTERRNO, "Failed to turn off swapfile,"
72 		         " system reboot after execution of LTP "
73 			 "test suite is recommended.");
74 	} else {
75 		tst_resm(TPASS, "Succeeded to turn off swapfile");
76 	}
77 }
78 
79 static void setup(void)
80 {
81 	tst_sig(FORK, DEF_HANDLER, cleanup);
82 
83 	tst_require_root();
84 
85 	TEST_PAUSE;
86 
87 	tst_tmpdir();
88 
89 	switch ((fs_type = tst_fs_type(cleanup, "."))) {
90 	case TST_NFS_MAGIC:
91 	case TST_TMPFS_MAGIC:
92 		tst_brkm(TCONF, cleanup,
93 			 "Cannot do swapoff on a file on %s filesystem",
94 			 tst_fs_type_name(fs_type));
95 	break;
96 	}
97 
98 	if (!tst_fs_has_free(NULL, ".", 64, TST_MB)) {
99 		tst_brkm(TBROK, cleanup,
100 			 "Insufficient disk space to create swap file");
101 	}
102 
103 	if (tst_fill_file("swapfile01", 0x00, 1024, 65536))
104 		tst_brkm(TBROK, cleanup, "Failed to create file for swap");
105 
106 	if (system("mkswap swapfile01 > tmpfile 2>&1") != 0)
107 		tst_brkm(TBROK, cleanup, "Failed to make swapfile");
108 }
109 
110 static void cleanup(void)
111 {
112 	tst_rmdir();
113 }
114