swapon01.c revision 359980f68b19c77c698b121b57a071dfe6e3ca31
1ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew/*
2ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *
4ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * This program is free software; you can redistribute it and/or modify it
5ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * under the terms of version 2 of the GNU General Public License as
6ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * published by the Free Software Foundation.
7ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *
8ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * This program is distributed in the hope that it would be useful, but
9ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * WITHOUT ANY WARRANTY; without even the implied warranty of
10ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *
12ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * You should have received a copy of the GNU General Public License along
13fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * with this program; if not, write the Free Software Foundation, Inc.,
14fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *
16ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew */
17ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew/**************************************************************************
184bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
194bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *    TEST IDENTIFIER	: swapon1
204bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
216b01d689606d971217ccb961f3c44db8c2024d08robbiew *    EXECUTED BY	: root / superuser
224bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
236b01d689606d971217ccb961f3c44db8c2024d08robbiew *    TEST TITLE	: Basic test for swapon(2)
244bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
256b01d689606d971217ccb961f3c44db8c2024d08robbiew *    TEST CASE TOTAL	: 1
264bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
276b01d689606d971217ccb961f3c44db8c2024d08robbiew *    AUTHOR		: Aniruddha Marathe <aniruddha.marathe@wipro.com>
284bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
29ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *    SIGNALS
306b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	Uses SIGUSR1 to pause before test if option set.
316b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	(See the parse_opts(3) man page).
32ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *
33ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *    DESCRIPTION
34ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *    This is a Phase I test for the swapon(2) system call.
35ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *    It is intended to provide a limited exposure of the system call.
364bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
376b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	Setup:
386b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Setup signal handling.
396b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Pause for SIGUSR1 if option specified.
406b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Create a temporary directory.
416b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Create file of size 32Mb .
426b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Make this file as swap file using mkswap(8)
434bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
446b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	Test:
456b01d689606d971217ccb961f3c44db8c2024d08robbiew *	 Loop if the proper options are given.
466b01d689606d971217ccb961f3c44db8c2024d08robbiew *	 Turn on the swapfile to test the system call
476b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Execute system call
486b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Check return code, if system call failed (return=-1)
496b01d689606d971217ccb961f3c44db8c2024d08robbiew *		Log the errno and Issue a FAIL message.
506b01d689606d971217ccb961f3c44db8c2024d08robbiew *	  Otherwise, Issue a PASS message.
514bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
526b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	Cleanup:
536b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	  Print errno log and/or timing stats if options given
544bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *
55ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * USAGE:  <for command-line>
56ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * swapon01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
57ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * where:
586b01d689606d971217ccb961f3c44db8c2024d08robbiew * 	-c n : run n copies simultaneously.
596b01d689606d971217ccb961f3c44db8c2024d08robbiew *	-e   : Turn on errno logging.
606b01d689606d971217ccb961f3c44db8c2024d08robbiew *	-i n : Execute test n times.
616b01d689606d971217ccb961f3c44db8c2024d08robbiew *	-I x : Execute test for x seconds.
626b01d689606d971217ccb961f3c44db8c2024d08robbiew *	-p   : Pause for SIGUSR1 before starting
636b01d689606d971217ccb961f3c44db8c2024d08robbiew *	-P x : Pause for x seconds between iterations.
646b01d689606d971217ccb961f3c44db8c2024d08robbiew *	-t   : Turn on syscall timing.
65ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *
66ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *RESTRICTIONS:
67ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * Not compatible with kernel versions below 2.1.35
68143090a9f7b93c011573dc044a58b2eea48e07e5robbiew *
69143090a9f7b93c011573dc044a58b2eea48e07e5robbiew *CHANGES:
70143090a9f7b93c011573dc044a58b2eea48e07e5robbiew * 2005/01/01  Add extra check to stop test if insufficient disk space in dir
71143090a9f7b93c011573dc044a58b2eea48e07e5robbiew *             -Ricky Ng-Adam (rngadam@yahoo.com)
72143090a9f7b93c011573dc044a58b2eea48e07e5robbiew * 2005/01/01  Add extra check to stop test if swap file is on tmpfs
73143090a9f7b93c011573dc044a58b2eea48e07e5robbiew *             -Ricky Ng-Adam (rngadam@yahoo.com)
74ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew *****************************************************************************/
75ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
76ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew#include <unistd.h>
7777772ef57f3c92243d1df2064fdb2b321e40227dyaberauneya#include <errno.h>
7877772ef57f3c92243d1df2064fdb2b321e40227dyaberauneya#include <stdlib.h>
79ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew#include "test.h"
80ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew#include "usctest.h"
8177772ef57f3c92243d1df2064fdb2b321e40227dyaberauneya#include "config.h"
8289911a1fbdf83d83b13218a88432b4c59b01fdf1yaberauneya#include "linux_syscall_numbers.h"
8389911a1fbdf83d83b13218a88432b4c59b01fdf1yaberauneya#include "swaponoff.h"
84ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
85ac9784e4b6099dba38e356f4dde08a8d91846a48robbiewstatic void setup();
86ac9784e4b6099dba38e356f4dde08a8d91846a48robbiewstatic void cleanup();
87ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
8856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakchar *TCID = "swapon01";	/* Test program identifier.    */
8956207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint TST_TOTAL = 1;		/* Total number of test cases. */
90ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
9156207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
92ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew{
93ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
9489af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
9589af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	char *msg;
966b01d689606d971217ccb961f3c44db8c2024d08robbiew
9745e285d46ab47b0ff76c88acb5ba97b0bd5f753dGarrett Cooper	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
9862383b8186495f45d903ca077cfb0c54a6cce00cGarrett Cooper		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
996b01d689606d971217ccb961f3c44db8c2024d08robbiew
1006b01d689606d971217ccb961f3c44db8c2024d08robbiew	setup();
1016b01d689606d971217ccb961f3c44db8c2024d08robbiew
1026b01d689606d971217ccb961f3c44db8c2024d08robbiew	for (lc = 0; TEST_LOOPING(lc); lc++) {
1036b01d689606d971217ccb961f3c44db8c2024d08robbiew
1046b01d689606d971217ccb961f3c44db8c2024d08robbiew		Tst_count = 0;
1056b01d689606d971217ccb961f3c44db8c2024d08robbiew
106359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek		TEST(ltp_syscall(__NR_swapon, "./swapfile01", 0));
1076b01d689606d971217ccb961f3c44db8c2024d08robbiew
1086b01d689606d971217ccb961f3c44db8c2024d08robbiew		/* check return code */
1096b01d689606d971217ccb961f3c44db8c2024d08robbiew		if (TEST_RETURN == -1) {
1106b01d689606d971217ccb961f3c44db8c2024d08robbiew			TEST_ERROR_LOG(TEST_ERRNO);
1116b01d689606d971217ccb961f3c44db8c2024d08robbiew			tst_resm(TFAIL, "swapon(2) Failed to turn on"
11256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 " swapfile.");
1136b01d689606d971217ccb961f3c44db8c2024d08robbiew		} else {
1146b01d689606d971217ccb961f3c44db8c2024d08robbiew			tst_resm(TPASS, "swapon(2) passed and turned on"
11556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				 " swapfile");
1166b01d689606d971217ccb961f3c44db8c2024d08robbiew			/*we need to turn this swap file off for -i option */
117359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek			if (ltp_syscall(__NR_swapoff, "./swapfile01") != 0) {
1186b01d689606d971217ccb961f3c44db8c2024d08robbiew				tst_brkm(TBROK, cleanup, "Failed to turn off"
11956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 " swapfile. system"
12056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 " reboot after"
12156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 " execution of LTP"
12256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 " test suite is" " recommended.");
1236b01d689606d971217ccb961f3c44db8c2024d08robbiew			}
1246b01d689606d971217ccb961f3c44db8c2024d08robbiew		}
12556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	}			/*End for TEST_LOOPING */
1266b01d689606d971217ccb961f3c44db8c2024d08robbiew
1276b01d689606d971217ccb961f3c44db8c2024d08robbiew	cleanup();
1281e6f5a673655551de5734ff31ef48cd63b604e6dGarrett Cooper	tst_exit();
1292c28215423293e443469a07ae7011135d058b671Garrett Cooper
130ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew}
131ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
132ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew/* setup() - performs all ONE TIME setup for this test */
13356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup()
134ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew{
135143090a9f7b93c011573dc044a58b2eea48e07e5robbiew
1366b01d689606d971217ccb961f3c44db8c2024d08robbiew	tst_sig(FORK, DEF_HANDLER, cleanup);
137ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
13856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* Check whether we are root */
1396b01d689606d971217ccb961f3c44db8c2024d08robbiew	if (geteuid() != 0) {
14053740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper		tst_brkm(TBROK, NULL, "Test must be run as root");
1416b01d689606d971217ccb961f3c44db8c2024d08robbiew	}
142ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
1436b01d689606d971217ccb961f3c44db8c2024d08robbiew	TEST_PAUSE;
144ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
1456b01d689606d971217ccb961f3c44db8c2024d08robbiew	tst_tmpdir();
146ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
14756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (tst_is_cwd_tmpfs()) {
14856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_brkm(TCONF, cleanup,
14956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			 "Cannot do swapon on a file located on a tmpfs filesystem");
15004c009ed372517d4a2080052cb1ba41a07f993c7subrata_modak	}
15104c009ed372517d4a2080052cb1ba41a07f993c7subrata_modak
15256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (tst_is_cwd_nfs()) {
15356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_brkm(TCONF, cleanup,
15456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			 "Cannot do swapon on a file located on a nfs filesystem");
155143090a9f7b93c011573dc044a58b2eea48e07e5robbiew	}
156143090a9f7b93c011573dc044a58b2eea48e07e5robbiew
15756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (!tst_cwd_has_free(65536)) {
15856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		tst_brkm(TBROK, cleanup,
15956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			 "Insufficient disk space to create swap file");
160143090a9f7b93c011573dc044a58b2eea48e07e5robbiew	}
161143090a9f7b93c011573dc044a58b2eea48e07e5robbiew
16256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/*create file */
16362383b8186495f45d903ca077cfb0c54a6cce00cGarrett Cooper	if (system("dd if=/dev/zero of=swapfile01 bs=1024 count=65536 >"
16456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		   " tmpfile 2>&1") != 0) {
1656b01d689606d971217ccb961f3c44db8c2024d08robbiew		tst_brkm(TBROK, cleanup, "Failed to create file for swap");
1666b01d689606d971217ccb961f3c44db8c2024d08robbiew	}
167ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
16856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* make above file a swap file */
16956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (system("mkswap swapfile01 > tmpfile 2>&1") != 0) {
1706b01d689606d971217ccb961f3c44db8c2024d08robbiew		tst_brkm(TBROK, cleanup, "Failed to make swapfile");
1716b01d689606d971217ccb961f3c44db8c2024d08robbiew	}
1722c28215423293e443469a07ae7011135d058b671Garrett Cooper}
173ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew
174ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew/*
175ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * cleanup() - Performs one time cleanup for this test at
176ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * completion or premature exit
177ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew */
17856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup()
179ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew{
1806b01d689606d971217ccb961f3c44db8c2024d08robbiew	/*
18156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 * print timing stats if that option was specified.
18256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 * print errno log if that option was specified.
18356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	 */
1846b01d689606d971217ccb961f3c44db8c2024d08robbiew	TEST_CLEANUP;
1856b01d689606d971217ccb961f3c44db8c2024d08robbiew
1866b01d689606d971217ccb961f3c44db8c2024d08robbiew	tst_rmdir();
1876b01d689606d971217ccb961f3c44db8c2024d08robbiew
188ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
189