xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/swapon/swapon01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4  * Copyright (c) Linux Test Project, 2003-2024
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Checks that swapon() succeds with swapfile.
11  * Testing on all filesystems which support swap file.
12  */
13 
14 #include <unistd.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include "tst_test.h"
18 #include "lapi/syscalls.h"
19 #include "libswap.h"
20 
21 #define MNTPOINT	"mntpoint"
22 #define SWAP_FILE	MNTPOINT"/swapfile01"
23 #define TESTMEM		(1UL<<30)
24 
verify_swapon(void)25 static void verify_swapon(void)
26 {
27 	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
28 
29 	tst_pollute_memory(TESTMEM, 0x41);
30 	tst_res(TINFO, "SwapCached: %ld Kb", SAFE_READ_MEMINFO("SwapCached:"));
31 
32 	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
33 		tst_brk(TBROK | TERRNO,
34 				"Failed to turn off swapfile, system reboot recommended");
35 	}
36 }
37 
setup(void)38 static void setup(void)
39 {
40 	is_swap_supported(SWAP_FILE);
41 	MAKE_SWAPFILE_SIZE(SWAP_FILE, 128);
42 
43 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
44 	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
45 }
46 
47 static struct tst_test test = {
48 	.mntpoint = MNTPOINT,
49 	.mount_device = 1,
50 	.needs_root = 1,
51 	.all_filesystems = 1,
52 	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
53 	.test_all = verify_swapon,
54 	.setup = setup
55 };
56