blob: d69ca6f3f76780d0136dc4d0ee77ccca91ce9bca [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
13 * with this program; if not, write the Free Software Foundation, Inc., 59
14 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
15 *
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
subrata_modak56207ce2009-03-23 13:35:39 +000094 int lc; /* loop counter */
95 char *msg; /* message returned from parse_opts */
robbiewac9784e2003-01-03 17:17:46 +000096
robbiew6b01d682003-04-28 20:27:26 +000097 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -080098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper62383b82010-11-22 12:02:14 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewac9784e2003-01-03 17:17:46 +0000100
robbiew6b01d682003-04-28 20:27:26 +0000101 setup();
robbiewac9784e2003-01-03 17:17:46 +0000102
robbiew6b01d682003-04-28 20:27:26 +0000103 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiewac9784e2003-01-03 17:17:46 +0000104
robbiew6b01d682003-04-28 20:27:26 +0000105 Tst_count = 0;
robbiewac9784e2003-01-03 17:17:46 +0000106
yaberauneya89911a12009-11-26 14:11:35 +0000107 TEST(syscall(__NR_swapon, "./swapfile01", 0));
robbiewac9784e2003-01-03 17:17:46 +0000108
robbiew6b01d682003-04-28 20:27:26 +0000109 /* check return code */
110 if (TEST_RETURN == -1) {
111 TEST_ERROR_LOG(TEST_ERRNO);
112 tst_resm(TFAIL, "swapon(2) Failed to turn on"
subrata_modak56207ce2009-03-23 13:35:39 +0000113 " swapfile.");
robbiew6b01d682003-04-28 20:27:26 +0000114 } else {
115 tst_resm(TPASS, "swapon(2) passed and turned on"
subrata_modak56207ce2009-03-23 13:35:39 +0000116 " swapfile");
robbiew6b01d682003-04-28 20:27:26 +0000117 /*we need to turn this swap file off for -i option */
yaberauneya89911a12009-11-26 14:11:35 +0000118 if (syscall(__NR_swapoff, "./swapfile01") != 0) {
robbiew6b01d682003-04-28 20:27:26 +0000119 tst_brkm(TBROK, cleanup, "Failed to turn off"
subrata_modak56207ce2009-03-23 13:35:39 +0000120 " swapfile. system"
121 " reboot after"
122 " execution of LTP"
123 " test suite is" " recommended.");
robbiew6b01d682003-04-28 20:27:26 +0000124 }
125 }
subrata_modak56207ce2009-03-23 13:35:39 +0000126 } /*End for TEST_LOOPING */
robbiewac9784e2003-01-03 17:17:46 +0000127
robbiew6b01d682003-04-28 20:27:26 +0000128 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800129 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800130
robbiewac9784e2003-01-03 17:17:46 +0000131}
132
133/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000134void setup()
robbiewac9784e2003-01-03 17:17:46 +0000135{
robbiew143090a2005-01-17 22:10:09 +0000136
robbiew6b01d682003-04-28 20:27:26 +0000137 tst_sig(FORK, DEF_HANDLER, cleanup);
robbiewac9784e2003-01-03 17:17:46 +0000138
subrata_modak56207ce2009-03-23 13:35:39 +0000139 /* Check whether we are root */
robbiew6b01d682003-04-28 20:27:26 +0000140 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800141 tst_brkm(TBROK, NULL, "Test must be run as root");
robbiew6b01d682003-04-28 20:27:26 +0000142 }
robbiewac9784e2003-01-03 17:17:46 +0000143
robbiew6b01d682003-04-28 20:27:26 +0000144 TEST_PAUSE;
robbiewac9784e2003-01-03 17:17:46 +0000145
robbiew6b01d682003-04-28 20:27:26 +0000146 tst_tmpdir();
robbiewac9784e2003-01-03 17:17:46 +0000147
subrata_modak56207ce2009-03-23 13:35:39 +0000148 if (tst_is_cwd_tmpfs()) {
149 tst_brkm(TCONF, cleanup,
150 "Cannot do swapon on a file located on a tmpfs filesystem");
subrata_modak04c009e2007-11-28 09:38:08 +0000151 }
152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 if (tst_is_cwd_nfs()) {
154 tst_brkm(TCONF, cleanup,
155 "Cannot do swapon on a file located on a nfs filesystem");
robbiew143090a2005-01-17 22:10:09 +0000156 }
157
subrata_modak56207ce2009-03-23 13:35:39 +0000158 if (!tst_cwd_has_free(65536)) {
159 tst_brkm(TBROK, cleanup,
160 "Insufficient disk space to create swap file");
robbiew143090a2005-01-17 22:10:09 +0000161 }
162
subrata_modak56207ce2009-03-23 13:35:39 +0000163 /*create file */
Garrett Cooper62383b82010-11-22 12:02:14 -0800164 if (system("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 >"
subrata_modak56207ce2009-03-23 13:35:39 +0000165 " tmpfile 2>&1") != 0) {
robbiew6b01d682003-04-28 20:27:26 +0000166 tst_brkm(TBROK, cleanup, "Failed to create file for swap");
167 }
robbiewac9784e2003-01-03 17:17:46 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 /* make above file a swap file */
170 if (system("mkswap swapfile01 > tmpfile 2>&1") != 0) {
robbiew6b01d682003-04-28 20:27:26 +0000171 tst_brkm(TBROK, cleanup, "Failed to make swapfile");
172 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800173}
robbiewac9784e2003-01-03 17:17:46 +0000174
175/*
176 * cleanup() - Performs one time cleanup for this test at
177 * completion or premature exit
178 */
subrata_modak56207ce2009-03-23 13:35:39 +0000179void cleanup()
robbiewac9784e2003-01-03 17:17:46 +0000180{
robbiew6b01d682003-04-28 20:27:26 +0000181 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000182 * print timing stats if that option was specified.
183 * print errno log if that option was specified.
184 */
robbiew6b01d682003-04-28 20:27:26 +0000185 TEST_CLEANUP;
robbiewac9784e2003-01-03 17:17:46 +0000186
robbiew6b01d682003-04-28 20:27:26 +0000187 tst_rmdir();
robbiewac9784e2003-01-03 17:17:46 +0000188
Chris Dearmanec6edca2012-10-17 19:54:01 -0700189}