blob: 6a23de1f62baa5d3fe4a260036d8cccf5b8b9747 [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 Hrubis591ddcb2014-02-26 18:06:19 +010017
18/*
19 * Check that swapoff() succeeds.
20 */
robbiew0abc81f2003-01-02 16:45:29 +000021
22#include <unistd.h>
23#include "test.h"
robbiew0abc81f2003-01-02 16:45:29 +000024#include <errno.h>
robbiew0abc81f2003-01-02 16:45:29 +000025#include <stdlib.h>
yaberauneya77772ef2009-10-17 06:53:58 +000026#include "config.h"
yaberauneyaa2a05e32009-11-26 14:19:32 +000027#include "linux_syscall_numbers.h"
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010028#include "tst_fs_type.h"
robbiew0abc81f2003-01-02 16:45:29 +000029
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010030static void setup(void);
31static void cleanup(void);
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010032static void verify_swapoff(void);
robbiew0abc81f2003-01-02 16:45:29 +000033
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020034char *TCID = "swapoff01";
35int TST_TOTAL = 1;
robbiew0abc81f2003-01-02 16:45:29 +000036
Cyril Hrubis72c0a1b2014-03-06 13:40:25 +010037static long fs_type;
38
subrata_modak56207ce2009-03-23 13:35:39 +000039int main(int ac, char **av)
robbiew0abc81f2003-01-02 16:45:29 +000040{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020041 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020042 const char *msg;
robbiew0abc81f2003-01-02 16:45:29 +000043
Garrett Coopera9e49f12010-12-16 10:54:03 -080044 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080045 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew0abc81f2003-01-02 16:45:29 +000046
robbiewadc1c012003-04-29 17:06:59 +000047 setup();
robbiew0abc81f2003-01-02 16:45:29 +000048
robbiewadc1c012003-04-29 17:06:59 +000049 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080050 tst_count = 0;
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010051 verify_swapoff();
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010052 }
robbiew0abc81f2003-01-02 16:45:29 +000053
robbiewadc1c012003-04-29 17:06:59 +000054 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080055 tst_exit();
robbiew0abc81f2003-01-02 16:45:29 +000056}
57
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010058static void verify_swapoff(void)
59{
60 if (ltp_syscall(__NR_swapon, "./swapfile01", 0) != 0) {
Cyril Hrubis72c0a1b2014-03-06 13:40:25 +010061 if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL) {
62 tst_brkm(TCONF, cleanup,
63 "Swapfiles on BTRFS are not implemented");
64 }
65
66 tst_resm(TBROK, "Failed to turn on the swap file"
Cyril Hrubisdfda9f82014-03-06 13:37:21 +010067 ", skipping test iteration");
68 return;
69 }
70
71 TEST(ltp_syscall(__NR_swapoff, "./swapfile01"));
72
73 if (TEST_RETURN == -1) {
74 tst_resm(TFAIL | TTERRNO, "Failed to turn off swapfile,"
75 " system reboot after execution of LTP "
76 "test suite is recommended.");
77 } else {
78 tst_resm(TPASS, "Succeeded to turn off swapfile");
79 }
80}
81
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010082static void setup(void)
robbiew0abc81f2003-01-02 16:45:29 +000083{
robbiewadc1c012003-04-29 17:06:59 +000084 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew0abc81f2003-01-02 16:45:29 +000085
Cyril Hrubis591ddcb2014-02-26 18:06:19 +010086 tst_require_root(NULL);
robbiew0abc81f2003-01-02 16:45:29 +000087
robbiewadc1c012003-04-29 17:06:59 +000088 TEST_PAUSE;
robbiew0abc81f2003-01-02 16:45:29 +000089
robbiewadc1c012003-04-29 17:06:59 +000090 tst_tmpdir();
robbiew0abc81f2003-01-02 16:45:29 +000091
Cyril Hrubis72c0a1b2014-03-06 13:40:25 +010092 switch ((fs_type = tst_fs_type(cleanup, "."))) {
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010093 case TST_NFS_MAGIC:
94 case TST_TMPFS_MAGIC:
subrata_modak56207ce2009-03-23 13:35:39 +000095 tst_brkm(TCONF, cleanup,
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010096 "Cannot do swapoff on a file on %s filesystem",
Cyril Hrubis72c0a1b2014-03-06 13:40:25 +010097 tst_fs_type_name(fs_type));
Cyril Hrubis0f0e3482014-02-27 16:08:04 +010098 break;
robbiew143090a2005-01-17 22:10:09 +000099 }
100
Xiaoguang Wang46941672014-04-25 10:02:07 +0800101 if (!tst_fs_has_free(NULL, ".", 64, TST_MB)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000102 tst_brkm(TBROK, cleanup,
103 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000104 }
105
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100106 if (tst_fill_file("swapfile01", 0x00, 1024, 65536))
robbiewadc1c012003-04-29 17:06:59 +0000107 tst_brkm(TBROK, cleanup, "Failed to create file for swap");
robbiew0abc81f2003-01-02 16:45:29 +0000108
Cyril Hrubisdfda9f82014-03-06 13:37:21 +0100109 if (system("mkswap swapfile01 > tmpfile 2>&1") != 0)
robbiewadc1c012003-04-29 17:06:59 +0000110 tst_brkm(TBROK, cleanup, "Failed to make swapfile");
Garrett Cooper2c282152010-12-16 00:55:50 -0800111}
robbiew0abc81f2003-01-02 16:45:29 +0000112
Cyril Hrubis591ddcb2014-02-26 18:06:19 +0100113static void cleanup(void)
robbiew0abc81f2003-01-02 16:45:29 +0000114{
robbiewadc1c012003-04-29 17:06:59 +0000115 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700116}