swapon01.c revision 4bdda040bc045dd8d921d54d3b54db5dabeb9958
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" 844bdda040bc045dd8d921d54d3b54db5dabeb9958Stanislav Kholmanskikh#include "libswapon.h" 85ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew 86ac9784e4b6099dba38e356f4dde08a8d91846a48robbiewstatic void setup(); 87ac9784e4b6099dba38e356f4dde08a8d91846a48robbiewstatic void cleanup(); 88ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew 89fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "swapon01"; 90fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1; 91ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew 9256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av) 93ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew{ 94ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew 9589af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis int lc; 9689af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis char *msg; 976b01d689606d971217ccb961f3c44db8c2024d08robbiew 9845e285d46ab47b0ff76c88acb5ba97b0bd5f753dGarrett Cooper if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 9962383b8186495f45d903ca077cfb0c54a6cce00cGarrett Cooper tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 1006b01d689606d971217ccb961f3c44db8c2024d08robbiew 1016b01d689606d971217ccb961f3c44db8c2024d08robbiew setup(); 1026b01d689606d971217ccb961f3c44db8c2024d08robbiew 1036b01d689606d971217ccb961f3c44db8c2024d08robbiew for (lc = 0; TEST_LOOPING(lc); lc++) { 1046b01d689606d971217ccb961f3c44db8c2024d08robbiew 105d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang tst_count = 0; 1066b01d689606d971217ccb961f3c44db8c2024d08robbiew 107359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek TEST(ltp_syscall(__NR_swapon, "./swapfile01", 0)); 1086b01d689606d971217ccb961f3c44db8c2024d08robbiew 1096b01d689606d971217ccb961f3c44db8c2024d08robbiew /* check return code */ 1106b01d689606d971217ccb961f3c44db8c2024d08robbiew if (TEST_RETURN == -1) { 1116b01d689606d971217ccb961f3c44db8c2024d08robbiew TEST_ERROR_LOG(TEST_ERRNO); 1126b01d689606d971217ccb961f3c44db8c2024d08robbiew tst_resm(TFAIL, "swapon(2) Failed to turn on" 11356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " swapfile."); 1146b01d689606d971217ccb961f3c44db8c2024d08robbiew } else { 1156b01d689606d971217ccb961f3c44db8c2024d08robbiew tst_resm(TPASS, "swapon(2) passed and turned on" 11656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " swapfile"); 1176b01d689606d971217ccb961f3c44db8c2024d08robbiew /*we need to turn this swap file off for -i option */ 118359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek if (ltp_syscall(__NR_swapoff, "./swapfile01") != 0) { 1196b01d689606d971217ccb961f3c44db8c2024d08robbiew tst_brkm(TBROK, cleanup, "Failed to turn off" 12056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " swapfile. system" 12156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " reboot after" 12256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " execution of LTP" 12356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " test suite is" " recommended."); 1246b01d689606d971217ccb961f3c44db8c2024d08robbiew } 1256b01d689606d971217ccb961f3c44db8c2024d08robbiew } 12656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak } /*End for TEST_LOOPING */ 1276b01d689606d971217ccb961f3c44db8c2024d08robbiew 1286b01d689606d971217ccb961f3c44db8c2024d08robbiew cleanup(); 1291e6f5a673655551de5734ff31ef48cd63b604e6dGarrett Cooper tst_exit(); 1302c28215423293e443469a07ae7011135d058b671Garrett Cooper 131ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew} 132ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew 133ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew/* setup() - performs all ONE TIME setup for this test */ 13456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup() 135ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew{ 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 1574bdda040bc045dd8d921d54d3b54db5dabeb9958Stanislav Kholmanskikh make_swapfile(cleanup, "swapfile01"); 1582c28215423293e443469a07ae7011135d058b671Garrett Cooper} 159ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew 160ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew/* 161ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * cleanup() - Performs one time cleanup for this test at 162ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew * completion or premature exit 163ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew */ 16456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup() 165ac9784e4b6099dba38e356f4dde08a8d91846a48robbiew{ 1666b01d689606d971217ccb961f3c44db8c2024d08robbiew /* 16756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak * print timing stats if that option was specified. 16856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak * print errno log if that option was specified. 16956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak */ 1706b01d689606d971217ccb961f3c44db8c2024d08robbiew TEST_CLEANUP; 1716b01d689606d971217ccb961f3c44db8c2024d08robbiew 1726b01d689606d971217ccb961f3c44db8c2024d08robbiew tst_rmdir(); 1736b01d689606d971217ccb961f3c44db8c2024d08robbiew 174ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman} 175