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