blob: d178d8df590bcbc3d6a061dc83fbf53998c8906a [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 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
19 * TEST IDENTIFIER : swapoff01
20 *
robbiewadc1c012003-04-29 17:06:59 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewadc1c012003-04-29 17:06:59 +000023 * TEST TITLE : Basic test for swapoff(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiewadc1c012003-04-29 17:06:59 +000025 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiewadc1c012003-04-29 17:06:59 +000027 * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiew0abc81f2003-01-02 16:45:29 +000029 * SIGNALS
robbiewadc1c012003-04-29 17:06:59 +000030 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
robbiew0abc81f2003-01-02 16:45:29 +000032 *
33 * DESCRIPTION
34 * This is a Phase I test for the swapoff(2) system call.
35 * It is intended to provide a limited exposure of the system call.
subrata_modak56207ce2009-03-23 13:35:39 +000036 * $
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
robbiewadc1c012003-04-29 17:06:59 +000038 * Setup:
39 * Setup signal handling.
40 * Pause for SIGUSR1 if option specified.
41 * Create a temporary directory.
42 * Create file of size 40K ( minimum swapfile size).
43 * Make this file as swap file using mkswap(8)
44 * Turn on the swap file.
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
robbiewadc1c012003-04-29 17:06:59 +000046 * Test:
47 * Loop if the proper options are given.
48 * Execute system call
49 * Check return code, if system call failed (return=-1)
50 * Log the errno and Issue a FAIL message.
51 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000052 *
robbiewadc1c012003-04-29 17:06:59 +000053 * Cleanup:
54 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
robbiew0abc81f2003-01-02 16:45:29 +000056 * USAGE: <for command-line>
57 * swapoff01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
58 * where:
robbiewadc1c012003-04-29 17:06:59 +000059 * -c n : Run n copies simultaneously.
60 * -e : Turn on errno logging.
61 * -i n : Execute test n times.
62 * -I x : Execute test for x seconds.
63 * -p : Pause for SIGUSR1 before starting
64 * -P x : Pause for x seconds between iterations.
65 * -t : Turn on syscall timing.
robbiew0abc81f2003-01-02 16:45:29 +000066 *
67 *RESTRICTIONS:
68 * Not compatible with kernel versions below 1.3.2.
robbiew143090a2005-01-17 22:10:09 +000069 *
70 *CHANGES:
71 * 2005/01/01 Add extra check to stop test if insufficient disk space in dir
72 * -Ricky Ng-Adam (rngadam@yahoo.com)
73 * 2005/01/01 Add extra check to stop test if swap file is on tmpfs
74 * -Ricky Ng-Adam (rngadam@yahoo.com)
robbiew0abc81f2003-01-02 16:45:29 +000075 *****************************************************************************/
76
77#include <unistd.h>
78#include "test.h"
79#include "usctest.h"
80#include <errno.h>
robbiew0abc81f2003-01-02 16:45:29 +000081#include <stdlib.h>
yaberauneya77772ef2009-10-17 06:53:58 +000082#include "config.h"
yaberauneyaa2a05e32009-11-26 14:19:32 +000083#include "linux_syscall_numbers.h"
84#include "swaponoff.h"
robbiew0abc81f2003-01-02 16:45:29 +000085
86static void setup();
87static void cleanup();
88
subrata_modak56207ce2009-03-23 13:35:39 +000089char *TCID = "swapoff01"; /* Test program identifier. */
90int TST_TOTAL = 1; /* Total number of test cases. */
robbiew0abc81f2003-01-02 16:45:29 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
robbiew0abc81f2003-01-02 16:45:29 +000093{
94
Cyril Hrubis89af32a2012-10-24 16:39:11 +020095 int lc;
96 char *msg;
robbiew0abc81f2003-01-02 16:45:29 +000097
Garrett Coopera9e49f12010-12-16 10:54:03 -080098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew0abc81f2003-01-02 16:45:29 +0000100
robbiewadc1c012003-04-29 17:06:59 +0000101 setup();
robbiew0abc81f2003-01-02 16:45:29 +0000102
robbiewadc1c012003-04-29 17:06:59 +0000103 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew0abc81f2003-01-02 16:45:29 +0000104
robbiewadc1c012003-04-29 17:06:59 +0000105 Tst_count = 0;
robbiew0abc81f2003-01-02 16:45:29 +0000106
Jan Stancek359980f2013-02-15 10:16:05 +0100107 if (ltp_syscall(__NR_swapon, "./swapfile01", 0) != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000108 tst_resm(TWARN, "Failed to turn on the swap file"
subrata_modak56207ce2009-03-23 13:35:39 +0000109 ", skipping test iteration");
robbiewadc1c012003-04-29 17:06:59 +0000110 continue;
111 }
robbiew0abc81f2003-01-02 16:45:29 +0000112
Jan Stancek359980f2013-02-15 10:16:05 +0100113 TEST(ltp_syscall(__NR_swapoff, "./swapfile01"));
subrata_modak56207ce2009-03-23 13:35:39 +0000114
robbiewadc1c012003-04-29 17:06:59 +0000115 /* check return code */
116 if (TEST_RETURN == -1) {
117 TEST_ERROR_LOG(TEST_ERRNO);
118 tst_resm(TFAIL, "swapoff(2) Failed to turn off"
subrata_modak56207ce2009-03-23 13:35:39 +0000119 " swapfile. System reboot after execution"
120 " of LTP test suite is recommended.");
robbiewadc1c012003-04-29 17:06:59 +0000121 tst_resm(TWARN, "It is recommended not to run"
subrata_modak56207ce2009-03-23 13:35:39 +0000122 " swapon01 and swapon02");
robbiewadc1c012003-04-29 17:06:59 +0000123 } else {
124 tst_resm(TPASS, "swapoff(2) passed and turned off"
subrata_modak56207ce2009-03-23 13:35:39 +0000125 " swapfile.");
robbiewadc1c012003-04-29 17:06:59 +0000126 }
subrata_modak56207ce2009-03-23 13:35:39 +0000127 } /*End for TEST_LOOPING */
robbiew0abc81f2003-01-02 16:45:29 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 /*Clean up and exit */
robbiewadc1c012003-04-29 17:06:59 +0000130 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800131 tst_exit();
robbiew0abc81f2003-01-02 16:45:29 +0000132
robbiew0abc81f2003-01-02 16:45:29 +0000133}
134
135/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000136void setup()
robbiew0abc81f2003-01-02 16:45:29 +0000137{
Garrett Cooper2c282152010-12-16 00:55:50 -0800138
robbiewadc1c012003-04-29 17:06:59 +0000139 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiew0abc81f2003-01-02 16:45:29 +0000140
subrata_modak56207ce2009-03-23 13:35:39 +0000141 /* Check whether we are root */
robbiewadc1c012003-04-29 17:06:59 +0000142 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800143 tst_brkm(TBROK, NULL, "Test must be run as root");
robbiewadc1c012003-04-29 17:06:59 +0000144 }
robbiew0abc81f2003-01-02 16:45:29 +0000145
robbiewadc1c012003-04-29 17:06:59 +0000146 TEST_PAUSE;
robbiew0abc81f2003-01-02 16:45:29 +0000147
robbiewadc1c012003-04-29 17:06:59 +0000148 tst_tmpdir();
robbiew0abc81f2003-01-02 16:45:29 +0000149
subrata_modak56207ce2009-03-23 13:35:39 +0000150 if (tst_is_cwd_tmpfs()) {
151 tst_brkm(TCONF, cleanup,
152 "Cannot do swapon on a file located on a tmpfs filesystem");
subrata_modak04c009e2007-11-28 09:38:08 +0000153 }
154
subrata_modak56207ce2009-03-23 13:35:39 +0000155 if (tst_is_cwd_nfs()) {
156 tst_brkm(TCONF, cleanup,
157 "Cannot do swapon on a file located on a nfs filesystem");
robbiew143090a2005-01-17 22:10:09 +0000158 }
159
subrata_modak56207ce2009-03-23 13:35:39 +0000160 if (!tst_cwd_has_free(65536)) {
161 tst_brkm(TBROK, cleanup,
162 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000163 }
164
subrata_modak56207ce2009-03-23 13:35:39 +0000165 /*create file */
166 if (system
167 ("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 > tmpfile"
168 " 2>&1 ") != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000169 tst_brkm(TBROK, cleanup, "Failed to create file for swap");
170 }
robbiew0abc81f2003-01-02 16:45:29 +0000171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 /* make above file a swap file */
173 if (system("mkswap swapfile01 > tmpfile 2>&1") != 0) {
robbiewadc1c012003-04-29 17:06:59 +0000174 tst_brkm(TBROK, cleanup, "Failed to make swapfile");
175 }
robbiew0abc81f2003-01-02 16:45:29 +0000176
Garrett Cooper2c282152010-12-16 00:55:50 -0800177}
robbiew0abc81f2003-01-02 16:45:29 +0000178
179/*
180 * cleanup() - Performs one time cleanup for this test at
181 * completion or premature exit
182 */
subrata_modak56207ce2009-03-23 13:35:39 +0000183void cleanup()
robbiew0abc81f2003-01-02 16:45:29 +0000184{
robbiewadc1c012003-04-29 17:06:59 +0000185 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000186 * print timing stats if that option was specified.
187 * print errno log if that option was specified.
188 */
robbiewadc1c012003-04-29 17:06:59 +0000189 TEST_CLEANUP;
robbiew0abc81f2003-01-02 16:45:29 +0000190
robbiewadc1c012003-04-29 17:06:59 +0000191 tst_rmdir();
robbiew0abc81f2003-01-02 16:45:29 +0000192
Chris Dearmanec6edca2012-10-17 19:54:01 -0700193}