blob: 8270bb779fa1123ee90c8948913afeeb369cdd72 [file] [log] [blame]
robbiewac9784e2003-01-03 17:17:46 +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.
robbiewac9784e2003-01-03 17:17:46 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
19 * TEST IDENTIFIER : swapon1
20 *
robbiew6b01d682003-04-28 20:27:26 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiew6b01d682003-04-28 20:27:26 +000023 * TEST TITLE : Basic test for swapon(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiew6b01d682003-04-28 20:27:26 +000025 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiew6b01d682003-04-28 20:27:26 +000027 * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiewac9784e2003-01-03 17:17:46 +000029 * SIGNALS
robbiew6b01d682003-04-28 20:27:26 +000030 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
robbiewac9784e2003-01-03 17:17:46 +000032 *
33 * DESCRIPTION
34 * This is a Phase I test for the swapon(2) system call.
35 * It is intended to provide a limited exposure of the system call.
subrata_modak4bb656a2009-02-26 12:02:09 +000036 *
robbiew6b01d682003-04-28 20:27:26 +000037 * Setup:
38 * Setup signal handling.
39 * Pause for SIGUSR1 if option specified.
40 * Create a temporary directory.
41 * Create file of size 32Mb .
42 * Make this file as swap file using mkswap(8)
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
robbiew6b01d682003-04-28 20:27:26 +000044 * Test:
45 * Loop if the proper options are given.
46 * Turn on the swapfile to test the system call
47 * Execute system call
48 * Check return code, if system call failed (return=-1)
49 * Log the errno and Issue a FAIL message.
50 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
robbiew6b01d682003-04-28 20:27:26 +000052 * Cleanup:
53 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000054 *
robbiewac9784e2003-01-03 17:17:46 +000055 * USAGE: <for command-line>
56 * swapon01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
57 * where:
robbiew6b01d682003-04-28 20:27:26 +000058 * -c n : run n copies simultaneously.
59 * -e : Turn on errno logging.
60 * -i n : Execute test n times.
61 * -I x : Execute test for x seconds.
62 * -p : Pause for SIGUSR1 before starting
63 * -P x : Pause for x seconds between iterations.
64 * -t : Turn on syscall timing.
robbiewac9784e2003-01-03 17:17:46 +000065 *
66 *RESTRICTIONS:
67 * Not compatible with kernel versions below 2.1.35
robbiew143090a2005-01-17 22:10:09 +000068 *
69 *CHANGES:
70 * 2005/01/01 Add extra check to stop test if insufficient disk space in dir
71 * -Ricky Ng-Adam (rngadam@yahoo.com)
72 * 2005/01/01 Add extra check to stop test if swap file is on tmpfs
73 * -Ricky Ng-Adam (rngadam@yahoo.com)
robbiewac9784e2003-01-03 17:17:46 +000074 *****************************************************************************/
75
76#include <unistd.h>
yaberauneya77772ef2009-10-17 06:53:58 +000077#include <errno.h>
78#include <stdlib.h>
robbiewac9784e2003-01-03 17:17:46 +000079#include "test.h"
80#include "usctest.h"
yaberauneya77772ef2009-10-17 06:53:58 +000081#include "config.h"
yaberauneya89911a12009-11-26 14:11:35 +000082#include "linux_syscall_numbers.h"
83#include "swaponoff.h"
robbiewac9784e2003-01-03 17:17:46 +000084
85static void setup();
86static void cleanup();
87
subrata_modak56207ce2009-03-23 13:35:39 +000088char *TCID = "swapon01"; /* Test program identifier. */
89int TST_TOTAL = 1; /* Total number of test cases. */
robbiewac9784e2003-01-03 17:17:46 +000090
subrata_modak56207ce2009-03-23 13:35:39 +000091int main(int ac, char **av)
robbiewac9784e2003-01-03 17:17:46 +000092{
93
Cyril Hrubis89af32a2012-10-24 16:39:11 +020094 int lc;
95 char *msg;
robbiewac9784e2003-01-03 17:17:46 +000096
Garrett Cooper45e285d2010-11-22 12:19:25 -080097 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper62383b82010-11-22 12:02:14 -080098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewac9784e2003-01-03 17:17:46 +000099
robbiew6b01d682003-04-28 20:27:26 +0000100 setup();
robbiewac9784e2003-01-03 17:17:46 +0000101
robbiew6b01d682003-04-28 20:27:26 +0000102 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewac9784e2003-01-03 17:17:46 +0000103
robbiew6b01d682003-04-28 20:27:26 +0000104 Tst_count = 0;
robbiewac9784e2003-01-03 17:17:46 +0000105
Jan Stancek359980f2013-02-15 10:16:05 +0100106 TEST(ltp_syscall(__NR_swapon, "./swapfile01", 0));
robbiewac9784e2003-01-03 17:17:46 +0000107
robbiew6b01d682003-04-28 20:27:26 +0000108 /* check return code */
109 if (TEST_RETURN == -1) {
110 TEST_ERROR_LOG(TEST_ERRNO);
111 tst_resm(TFAIL, "swapon(2) Failed to turn on"
subrata_modak56207ce2009-03-23 13:35:39 +0000112 " swapfile.");
robbiew6b01d682003-04-28 20:27:26 +0000113 } else {
114 tst_resm(TPASS, "swapon(2) passed and turned on"
subrata_modak56207ce2009-03-23 13:35:39 +0000115 " swapfile");
robbiew6b01d682003-04-28 20:27:26 +0000116 /*we need to turn this swap file off for -i option */
Jan Stancek359980f2013-02-15 10:16:05 +0100117 if (ltp_syscall(__NR_swapoff, "./swapfile01") != 0) {
robbiew6b01d682003-04-28 20:27:26 +0000118 tst_brkm(TBROK, cleanup, "Failed to turn off"
subrata_modak56207ce2009-03-23 13:35:39 +0000119 " swapfile. system"
120 " reboot after"
121 " execution of LTP"
122 " test suite is" " recommended.");
robbiew6b01d682003-04-28 20:27:26 +0000123 }
124 }
subrata_modak56207ce2009-03-23 13:35:39 +0000125 } /*End for TEST_LOOPING */
robbiewac9784e2003-01-03 17:17:46 +0000126
robbiew6b01d682003-04-28 20:27:26 +0000127 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800128 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800129
robbiewac9784e2003-01-03 17:17:46 +0000130}
131
132/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000133void setup()
robbiewac9784e2003-01-03 17:17:46 +0000134{
robbiew143090a2005-01-17 22:10:09 +0000135
robbiew6b01d682003-04-28 20:27:26 +0000136 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiewac9784e2003-01-03 17:17:46 +0000137
subrata_modak56207ce2009-03-23 13:35:39 +0000138 /* Check whether we are root */
robbiew6b01d682003-04-28 20:27:26 +0000139 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800140 tst_brkm(TBROK, NULL, "Test must be run as root");
robbiew6b01d682003-04-28 20:27:26 +0000141 }
robbiewac9784e2003-01-03 17:17:46 +0000142
robbiew6b01d682003-04-28 20:27:26 +0000143 TEST_PAUSE;
robbiewac9784e2003-01-03 17:17:46 +0000144
robbiew6b01d682003-04-28 20:27:26 +0000145 tst_tmpdir();
robbiewac9784e2003-01-03 17:17:46 +0000146
subrata_modak56207ce2009-03-23 13:35:39 +0000147 if (tst_is_cwd_tmpfs()) {
148 tst_brkm(TCONF, cleanup,
149 "Cannot do swapon on a file located on a tmpfs filesystem");
subrata_modak04c009e2007-11-28 09:38:08 +0000150 }
151
subrata_modak56207ce2009-03-23 13:35:39 +0000152 if (tst_is_cwd_nfs()) {
153 tst_brkm(TCONF, cleanup,
154 "Cannot do swapon on a file located on a nfs filesystem");
robbiew143090a2005-01-17 22:10:09 +0000155 }
156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 if (!tst_cwd_has_free(65536)) {
158 tst_brkm(TBROK, cleanup,
159 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000160 }
161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 /*create file */
Garrett Cooper62383b82010-11-22 12:02:14 -0800163 if (system("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 >"
subrata_modak56207ce2009-03-23 13:35:39 +0000164 " tmpfile 2>&1") != 0) {
robbiew6b01d682003-04-28 20:27:26 +0000165 tst_brkm(TBROK, cleanup, "Failed to create file for swap");
166 }
robbiewac9784e2003-01-03 17:17:46 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 /* make above file a swap file */
169 if (system("mkswap swapfile01 > tmpfile 2>&1") != 0) {
robbiew6b01d682003-04-28 20:27:26 +0000170 tst_brkm(TBROK, cleanup, "Failed to make swapfile");
171 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
robbiewac9784e2003-01-03 17:17:46 +0000173
174/*
175 * cleanup() - Performs one time cleanup for this test at
176 * completion or premature exit
177 */
subrata_modak56207ce2009-03-23 13:35:39 +0000178void cleanup()
robbiewac9784e2003-01-03 17:17:46 +0000179{
robbiew6b01d682003-04-28 20:27:26 +0000180 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000181 * print timing stats if that option was specified.
182 * print errno log if that option was specified.
183 */
robbiew6b01d682003-04-28 20:27:26 +0000184 TEST_CLEANUP;
robbiewac9784e2003-01-03 17:17:46 +0000185
robbiew6b01d682003-04-28 20:27:26 +0000186 tst_rmdir();
robbiewac9784e2003-01-03 17:17:46 +0000187
Chris Dearmanec6edca2012-10-17 19:54:01 -0700188}