blob: 1fd9631ef0c80b10caa1ddbd1c2fe79bfe43fee4 [file] [log] [blame]
subrata_modak413ec142007-08-28 07:17:29 +00001/******************************************************************************
2 *
3 * Copyright (c) International Business Machines Corp., 2007
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak413ec142007-08-28 07:17:29 +000018 *
19 * NAME
20 * swapon03.c
21 *
22 * DESCRIPTION
23 * This test case checks whether swapon(2) system call returns:
24 * - EPERM when there are more than MAX_SWAPFILES already in use.
25 *
26 * Setup:
27 * Setup signal handling.
28 * Pause for SIGUSR1 if option specified.
29 * Create MAX_SWAPFILES - 2 (to support latest kernels) swapfiles
Garrett Cooper2c282152010-12-16 00:55:50 -080030 *
subrata_modak413ec142007-08-28 07:17:29 +000031 * Test:
32 * Loop if the proper options are given.
33 * Execute system call.
34 * Check return code, if system call fails with errno == expected errno
35 * Issue syscall passed with expected errno
subrata_modak4bb656a2009-02-26 12:02:09 +000036 * Otherwise,
subrata_modak413ec142007-08-28 07:17:29 +000037 * Issue syscall failed to produce expected errno
subrata_modak4bb656a2009-02-26 12:02:09 +000038 *
subrata_modak413ec142007-08-28 07:17:29 +000039 * Cleanup:
40 * Do cleanup for the test.
41 *
42 * USAGE: <for command-line>
43 * swapon03 [-e] [-i n] [-I x] [-p x] [-t] [-h] [-f] [-p]
subrata_modak4bb656a2009-02-26 12:02:09 +000044 * where
subrata_modak413ec142007-08-28 07:17:29 +000045 * -e : Turn on errno logging.
46 * -i n : Execute test n times.
47 * -I x : Execute test for x seconds.
48 * -p : Pause for SIGUSR1 before starting
49 * -P x : Pause for x seconds between iterations.
50 * -t : Turn on syscall timing.
51 *
52 * Author
53 * Ricardo Salveti de Araujo <rsalveti@linux.vnet.ibm.com> based on
subrata_modak4bb656a2009-02-26 12:02:09 +000054 * swapon02 created by Aniruddha Marathe
subrata_modak413ec142007-08-28 07:17:29 +000055 *
56 * History
57 * 16/08/2007 Created <rsalveti@linux.vnet.ibm.com>
58 *
59******************************************************************************/
60
yaberauneya89911a12009-11-26 14:11:35 +000061#include <sys/types.h>
subrata_modak413ec142007-08-28 07:17:29 +000062#include <unistd.h>
63#include <errno.h>
64#include <stdlib.h>
subrata_modak413ec142007-08-28 07:17:29 +000065#include <sys/wait.h>
66#include <sys/stat.h>
Garrett Coopera83e7dc2010-02-24 17:05:14 -080067#include <sys/utsname.h>
subrata_modak413ec142007-08-28 07:17:29 +000068#include <fcntl.h>
69#include <pwd.h>
70#include <string.h>
subrata_modak413ec142007-08-28 07:17:29 +000071#include <signal.h>
72#include "test.h"
73#include "usctest.h"
yaberauneya77772ef2009-10-17 06:53:58 +000074#include "config.h"
yaberauneya89911a12009-11-26 14:11:35 +000075#include "linux_syscall_numbers.h"
76#include "swaponoff.h"
subrata_modak413ec142007-08-28 07:17:29 +000077
subrata_modak413ec142007-08-28 07:17:29 +000078void setup();
79void cleanup();
80int setup_swap();
81int clean_swap();
subrata_modak56207ce2009-03-23 13:35:39 +000082int check_and_swapoff(char *filename);
83int create_swapfile(char *swapfile, int bs, int count);
subrata_modak413ec142007-08-28 07:17:29 +000084
85char *TCID = "swapon03"; /* Test program identifier. */
subrata_modak56207ce2009-03-23 13:35:39 +000086int TST_TOTAL = 1; /* Total number of test cases. */
subrata_modak413ec142007-08-28 07:17:29 +000087
88static int exp_enos[] = { EPERM, 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +080089
subrata_modak413ec142007-08-28 07:17:29 +000090static int swapfiles; /* Number of swapfiles turned on */
91
92struct utsname uval;
93char *kmachine;
94
95/* Paths for files that we'll use to test */
96int testfiles = 3;
97static struct swap_testfile_t {
98 char *filename;
99} swap_testfiles[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000100 {
101 "firstswapfile"}, {
102 "secondswapfile"}, {
103 "thirdswapfile"}
subrata_modak413ec142007-08-28 07:17:29 +0000104};
subrata_modakbdbaec52009-02-26 12:14:51 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106int expected_errno = EPERM; /* Expected errno when doing the test */
subrata_modak413ec142007-08-28 07:17:29 +0000107
108int main(int ac, char **av)
109{
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200110 int lc;
111 char *msg;
subrata_modak413ec142007-08-28 07:17:29 +0000112
Garrett Cooper45e285d2010-11-22 12:19:25 -0800113 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800114 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak413ec142007-08-28 07:17:29 +0000115
116 /***************************************************************
117 * perform global setup for test
118 ***************************************************************/
119 uname(&uval);
subrata_modak56207ce2009-03-23 13:35:39 +0000120 kmachine = uval.machine;
121 setup();
subrata_modak413ec142007-08-28 07:17:29 +0000122
123 /***************************************************************
124 * check looping state if -i option given
125 ***************************************************************/
126 for (lc = 0; TEST_LOOPING(lc); lc++) {
127 Tst_count = 0;
128
129 /* do the test setup */
130 if (setup_swap() < 0) {
131 clean_swap();
subrata_modak56207ce2009-03-23 13:35:39 +0000132 tst_brkm(TBROK, cleanup,
133 "Setup failed, quitting the test");
subrata_modak413ec142007-08-28 07:17:29 +0000134 }
135
136 /* Call swapon sys call for the first time */
Jan Stancek359980f2013-02-15 10:16:05 +0100137 TEST(ltp_syscall(__NR_swapon, swap_testfiles[0].filename, 0));
subrata_modak413ec142007-08-28 07:17:29 +0000138
139 /* Check return code */
140 if ((TEST_RETURN == -1) && (TEST_ERRNO == expected_errno)) {
Garrett Cooper1e27c532010-11-22 14:33:28 -0800141 tst_resm(TPASS, "swapon(2) got expected failure (%d),",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 expected_errno);
subrata_modak413ec142007-08-28 07:17:29 +0000143 } else if (TEST_RETURN < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 tst_resm(TFAIL | TTERRNO,
145 "swapon(2) failed to produce expected error "
146 "(%d). System reboot recommended.",
147 expected_errno);
subrata_modak413ec142007-08-28 07:17:29 +0000148 } else {
149 /* Probably the system supports MAX_SWAPFILES > 30,
150 * let's try with MAX_SWAPFILES == 32 */
151
152 /* Call swapon sys call once again for 32
153 * now we can't receive an error */
Jan Stancek359980f2013-02-15 10:16:05 +0100154 TEST(ltp_syscall
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 (__NR_swapon, swap_testfiles[1].filename, 0));
subrata_modak413ec142007-08-28 07:17:29 +0000156
157 /* Check return code (now we're expecting success) */
158 if (TEST_RETURN < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 tst_resm(TFAIL | TTERRNO,
Garrett Cooper1e27c532010-11-22 14:33:28 -0800160 "swapon(2) got an unexpected failure");
subrata_modak56207ce2009-03-23 13:35:39 +0000161 } else {
subrata_modak413ec142007-08-28 07:17:29 +0000162 /* Call swapon sys call once again for 33
163 * now we have to receive an error */
Jan Stancek359980f2013-02-15 10:16:05 +0100164 TEST(ltp_syscall
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 (__NR_swapon, swap_testfiles[2].filename,
166 0));
subrata_modak413ec142007-08-28 07:17:29 +0000167
168 /* Check return code (should be an error) */
subrata_modak56207ce2009-03-23 13:35:39 +0000169 if ((TEST_RETURN == -1)
170 && (TEST_ERRNO == expected_errno)) {
171 tst_resm(TPASS,
172 "swapon(2) got expected failure;"
Garrett Cooperbe5c7882010-08-02 22:01:02 -0700173 " Got errno = %d, probably your"
subrata_modak56207ce2009-03-23 13:35:39 +0000174 " MAX_SWAPFILES is 32",
175 expected_errno);
176 } else {
177 tst_resm(TFAIL,
178 "swapon(2) failed to produce"
179 " expected error: %d, got %s."
180 " System reboot after execution of LTP"
181 " test suite is recommended.",
182 expected_errno,
183 strerror(TEST_ERRNO));
subrata_modak413ec142007-08-28 07:17:29 +0000184 }
185
186 }
187 }
188
189 /* do the clean */
190 if (clean_swap() < 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000191 tst_brkm(TBROK, cleanup,
192 "Cleanup failed, quitting the test");
subrata_modak413ec142007-08-28 07:17:29 +0000193
194 TEST_ERROR_LOG(TEST_ERRNO);
195
Garrett Cooper2c282152010-12-16 00:55:50 -0800196 }
subrata_modak413ec142007-08-28 07:17:29 +0000197
subrata_modak413ec142007-08-28 07:17:29 +0000198 cleanup();
Garrett Cooper1e27c532010-11-22 14:33:28 -0800199 tst_exit();
subrata_modak413ec142007-08-28 07:17:29 +0000200
Garrett Cooper2c282152010-12-16 00:55:50 -0800201}
subrata_modak413ec142007-08-28 07:17:29 +0000202
203/***************************************************************
204 * setup_swap() - Create 33 and activate 30 swapfiles.
205 ***************************************************************/
206int setup_swap()
207{
Garrett Cooper62383b82010-11-22 12:02:14 -0800208 pid_t pid;
subrata_modak56207ce2009-03-23 13:35:39 +0000209 int j, fd; /*j is loop counter, fd is file descriptor */
subrata_modak56207ce2009-03-23 13:35:39 +0000210 int status; /* used for fork */
211 int res = 0, pagesize = getpagesize();
212 int bs, count;
213 char filename[15]; /* array to store new filename */
yaberauneya89911a12009-11-26 14:11:35 +0000214 char buf[BUFSIZ + 1]; /* temp buffer for reading /proc/swaps */
subrata_modak413ec142007-08-28 07:17:29 +0000215
subrata_modak56207ce2009-03-23 13:35:39 +0000216 /* Find out how many swapfiles (1 line per entry) already exist */
subrata_modak413ec142007-08-28 07:17:29 +0000217 swapfiles = 0;
218
yaberauneya89911a12009-11-26 14:11:35 +0000219 if (seteuid(0) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220 tst_brkm(TFAIL | TERRNO, cleanup, "Failed to call seteuid");
yaberauneya89911a12009-11-26 14:11:35 +0000221 }
222
subrata_modak56207ce2009-03-23 13:35:39 +0000223 /* This includes the first (header) line */
224 if ((fd = open("/proc/swaps", O_RDONLY)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 tst_brkm(TFAIL | TERRNO, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000226 "Failed to find out existing number of swap files");
subrata_modak56207ce2009-03-23 13:35:39 +0000227 }
yaberauneya89911a12009-11-26 14:11:35 +0000228 do {
subrata_modak56207ce2009-03-23 13:35:39 +0000229 char *p = buf;
yaberauneya89911a12009-11-26 14:11:35 +0000230 res = read(fd, buf, BUFSIZ);
subrata_modak56207ce2009-03-23 13:35:39 +0000231 if (res < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800232 tst_brkm(TFAIL | TERRNO, cleanup,
Garrett Cooper62383b82010-11-22 12:02:14 -0800233 "Failed to find out existing number of swap "
234 "files");
subrata_modak56207ce2009-03-23 13:35:39 +0000235 }
236 buf[res] = '\0';
237 while ((p = strchr(p, '\n'))) {
238 p++;
239 swapfiles++;
240 }
yaberauneya89911a12009-11-26 14:11:35 +0000241 } while (BUFSIZ <= res);
subrata_modak56207ce2009-03-23 13:35:39 +0000242 close(fd);
243 if (swapfiles)
244 swapfiles--; /* don't count the /proc/swaps header */
subrata_modak413ec142007-08-28 07:17:29 +0000245
subrata_modak56207ce2009-03-23 13:35:39 +0000246 if (swapfiles < 0) {
Garrett Cooper62383b82010-11-22 12:02:14 -0800247 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248 "Failed to find existing number of swapfiles");
subrata_modak56207ce2009-03-23 13:35:39 +0000249 }
subrata_modak413ec142007-08-28 07:17:29 +0000250
subrata_modak56207ce2009-03-23 13:35:39 +0000251 /* Determine how many more files are to be created */
yaberauneya96db7a42009-10-25 06:33:33 +0000252 swapfiles = MAX_SWAPFILES - swapfiles;
253 if (swapfiles > MAX_SWAPFILES) {
254 swapfiles = MAX_SWAPFILES;
subrata_modak56207ce2009-03-23 13:35:39 +0000255 }
subrata_modak413ec142007-08-28 07:17:29 +0000256
subrata_modak56207ce2009-03-23 13:35:39 +0000257 /* args for dd */
258 if ((strncmp(kmachine, "ia64", 4)) == 0) {
259 bs = 1024;
260 count = 1024;
261 } else if (pagesize == 65536) {
262 bs = 1048;
263 count = 655;
264 } else {
265 bs = 1048;
266 count = 40;
267 }
subrata_modak413ec142007-08-28 07:17:29 +0000268
subrata_modak56207ce2009-03-23 13:35:39 +0000269 pid = FORK_OR_VFORK();
270 if (pid == 0) {
271 /*create and turn on remaining swapfiles */
272 for (j = 0; j < swapfiles; j++) {
subrata_modak413ec142007-08-28 07:17:29 +0000273
subrata_modak56207ce2009-03-23 13:35:39 +0000274 /* prepare filename for the iteration */
275 if (sprintf(filename, "swapfile%02d", j + 2) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800276 printf("sprintf() failed to create "
277 "filename");
Garrett Cooper1e27c532010-11-22 14:33:28 -0800278 exit(1);
subrata_modak413ec142007-08-28 07:17:29 +0000279 }
280
subrata_modak56207ce2009-03-23 13:35:39 +0000281 /* Create the swapfile */
282 if (create_swapfile(filename, bs, count) < 0) {
Garrett Cooper62383b82010-11-22 12:02:14 -0800283 printf("Failed to create swapfile");
Garrett Cooper1e27c532010-11-22 14:33:28 -0800284 exit(1);
subrata_modak56207ce2009-03-23 13:35:39 +0000285 }
subrata_modak05fd0c72009-02-12 15:06:05 +0000286
subrata_modak56207ce2009-03-23 13:35:39 +0000287 /* turn on the swap file */
Jan Stancek359980f2013-02-15 10:16:05 +0100288 res = ltp_syscall(__NR_swapon, filename, 0);
289 if (res != 0) {
Garrett Cooperbe5c7882010-08-02 22:01:02 -0700290 if (errno == EPERM) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800291 printf("Successfully created %d "
292 "swapfiles\n", j);
Garrett Cooperbe5c7882010-08-02 22:01:02 -0700293 break;
294 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800295 printf("Failed to create "
296 "swapfile: %s\n", filename);
Garrett Cooperbe5c7882010-08-02 22:01:02 -0700297 exit(1);
298 }
subrata_modak56207ce2009-03-23 13:35:39 +0000299 }
300 }
Garrett Cooper62383b82010-11-22 12:02:14 -0800301 exit(0);
subrata_modak56207ce2009-03-23 13:35:39 +0000302 } else
303 waitpid(pid, &status, 0);
304
305 if (WEXITSTATUS(status)) {
Garrett Cooper1e27c532010-11-22 14:33:28 -0800306 tst_brkm(TFAIL, cleanup, "Failed to setup swaps");
subrata_modak05fd0c72009-02-12 15:06:05 +0000307 }
subrata_modak413ec142007-08-28 07:17:29 +0000308
subrata_modak56207ce2009-03-23 13:35:39 +0000309 /* Create all needed extra swapfiles for testing */
subrata_modak413ec142007-08-28 07:17:29 +0000310 for (j = 0; j < testfiles; j++) {
311 if (create_swapfile(swap_testfiles[j].filename, bs, count) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000312 tst_resm(TWARN,
313 "Failed to create swapfiles for the test");
subrata_modak413ec142007-08-28 07:17:29 +0000314 exit(1);
315 }
316 }
317
318 return 0;
319
320}
321
322/***************************************************************
323 * create_swapfile() - Create a swap file
324 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000325int create_swapfile(char *swapfile, int bs, int count)
subrata_modak413ec142007-08-28 07:17:29 +0000326{
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200327 char cmd_buffer[256];
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800328
subrata_modak413ec142007-08-28 07:17:29 +0000329 /* prepare the path string for dd command */
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200330 if (snprintf(cmd_buffer, sizeof(cmd_buffer),
Wanlong Gao354ebb42012-12-07 10:10:04 +0800331 "dd if=/dev/zero of=%s bs=%d "
332 "count=%d > tmpfile 2>&1", swapfile, bs, count) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000333 tst_resm(TWARN,
334 "sprintf() failed to create the command string");
Chris Dearman37550cf2012-10-17 19:54:01 -0700335
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200336 return -1;
subrata_modak413ec142007-08-28 07:17:29 +0000337 }
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200338
339 if (system(cmd_buffer) != 0) {
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800340 tst_resm(TWARN, "dd command failed to create file via "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800341 "command: %s", cmd_buffer);
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200342 return -1;
subrata_modak413ec142007-08-28 07:17:29 +0000343 }
Chris Dearman37550cf2012-10-17 19:54:01 -0700344
subrata_modak413ec142007-08-28 07:17:29 +0000345 /* make the file swapfile */
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200346 if (snprintf(cmd_buffer, sizeof(cmd_buffer),
Wanlong Gao354ebb42012-12-07 10:10:04 +0800347 "mkswap %s > tmpfile 2>&1", swapfile) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000348 tst_resm(TWARN,
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800349 "snprintf() failed to create mkswap command string");
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200350 return -1;
351 }
Chris Dearman37550cf2012-10-17 19:54:01 -0700352
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200353 if (system(cmd_buffer) != 0) {
yaberauneya225c7f32010-01-31 07:29:25 +0000354 tst_resm(TWARN, "failed to make swap file %s via command %s",
355 swapfile, cmd_buffer);
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200356 return -1;
subrata_modak413ec142007-08-28 07:17:29 +0000357 }
358
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200359 return 0;
subrata_modak413ec142007-08-28 07:17:29 +0000360}
361
362/***************************************************************
363 * clean_swap() - clearing all turned on swapfiles
364 ***************************************************************/
365int clean_swap()
366{
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200367 int j;
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800368 char filename[FILENAME_MAX];
subrata_modak413ec142007-08-28 07:17:29 +0000369
subrata_modak56207ce2009-03-23 13:35:39 +0000370 for (j = 0; j < swapfiles; j++) {
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800371 if (snprintf(filename, sizeof(filename),
Wanlong Gao354ebb42012-12-07 10:10:04 +0800372 "swapfile%02d", j + 2) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000373 tst_resm(TWARN, "sprintf() failed to create filename");
374 tst_resm(TWARN, "Failed to turn off swap files. System"
375 " reboot after execution of LTP test"
376 " suite is recommended");
377 return -1;
378 }
379 if (check_and_swapoff(filename) != 0) {
380 tst_resm(TWARN, "Failed to turn off swap file %s.",
381 filename);
382 return -1;
383 }
384 }
subrata_modak413ec142007-08-28 07:17:29 +0000385
386 for (j = 0; j < testfiles; j++) {
387 if (check_and_swapoff(swap_testfiles[j].filename) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000388 tst_resm(TWARN, "Failed to turn off swap file %s.",
389 swap_testfiles[j].filename);
390 return -1;
391 }
subrata_modak413ec142007-08-28 07:17:29 +0000392 }
393
subrata_modak56207ce2009-03-23 13:35:39 +0000394 return 0;
subrata_modak413ec142007-08-28 07:17:29 +0000395}
396
397/***************************************************************
398 * check_and_swapoff() - check if the file is at /proc/swaps and
399 * remove it giving swapoff
400 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000401int check_and_swapoff(char *filename)
subrata_modak413ec142007-08-28 07:17:29 +0000402{
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200403 char cmd_buffer[256];
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800404 int rc = -1;
subrata_modak413ec142007-08-28 07:17:29 +0000405
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200406 if (snprintf(cmd_buffer, sizeof(cmd_buffer),
Wanlong Gao354ebb42012-12-07 10:10:04 +0800407 "grep -q '%s.*file' /proc/swaps", filename) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000408 tst_resm(TWARN,
409 "sprintf() failed to create the command string");
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800410 } else {
411
412 rc = 0;
413
414 if (system(cmd_buffer) == 0) {
415
416 /* now we need to swapoff the file */
Jan Stancek359980f2013-02-15 10:16:05 +0100417 if (ltp_syscall(__NR_swapoff, filename) != 0) {
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800418
419 tst_resm(TWARN, "Failed to turn off swap "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800420 "file. system reboot after "
421 "execution of LTP test suite "
422 "is recommended");
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800423 rc = -1;
424
Garrett Cooper2c282152010-12-16 00:55:50 -0800425 }
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800426
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200427 }
subrata_modak413ec142007-08-28 07:17:29 +0000428 }
subrata_modak413ec142007-08-28 07:17:29 +0000429
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800430 return rc;
431
subrata_modak413ec142007-08-28 07:17:29 +0000432}
433
434/***************************************************************
435 * setup() - performs all ONE TIME setup for this test
436 ***************************************************************/
437void setup()
438{
Garrett Cooper2c282152010-12-16 00:55:50 -0800439
subrata_modak56207ce2009-03-23 13:35:39 +0000440 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modak413ec142007-08-28 07:17:29 +0000441
subrata_modak56207ce2009-03-23 13:35:39 +0000442 /* set the expected errnos... */
443 TEST_EXP_ENOS(exp_enos);
subrata_modak413ec142007-08-28 07:17:29 +0000444
subrata_modak56207ce2009-03-23 13:35:39 +0000445 /* Check whether we are root */
446 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800447 tst_brkm(TBROK, NULL, "Test must be run as root");
subrata_modak04c009e2007-11-28 09:38:08 +0000448 }
449
subrata_modak56207ce2009-03-23 13:35:39 +0000450 tst_tmpdir();
subrata_modak413ec142007-08-28 07:17:29 +0000451
subrata_modak56207ce2009-03-23 13:35:39 +0000452 if (tst_is_cwd_tmpfs()) {
453 tst_brkm(TCONF, cleanup,
454 "Cannot do swapon on a file located on a tmpfs filesystem");
455 }
subrata_modak413ec142007-08-28 07:17:29 +0000456
subrata_modak56207ce2009-03-23 13:35:39 +0000457 if (tst_is_cwd_nfs()) {
458 tst_brkm(TCONF, cleanup,
459 "Cannot do swapon on a file located on a nfs filesystem");
460 }
461
subrata_modak56207ce2009-03-23 13:35:39 +0000462 TEST_PAUSE;
463
Garrett Cooper2c282152010-12-16 00:55:50 -0800464}
subrata_modak413ec142007-08-28 07:17:29 +0000465
466/***************************************************************
467 * cleanup() - performs all ONE TIME cleanup for this test at
468 * completion or premature exit.
469 ***************************************************************/
470void cleanup()
471{
subrata_modak56207ce2009-03-23 13:35:39 +0000472 /*
473 * print timing stats if that option was specified.
474 * print errno log if that option was specified.
475 */
476 TEST_CLEANUP;
subrata_modak413ec142007-08-28 07:17:29 +0000477
Garrett Coopera83e7dc2010-02-24 17:05:14 -0800478 /* Remove any remaining swap files */
479 clean_swap();
480
subrata_modak56207ce2009-03-23 13:35:39 +0000481 tst_rmdir();
subrata_modak413ec142007-08-28 07:17:29 +0000482
Cyril Hrubisd9235f82012-06-29 14:08:41 +0200483}