xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/swapon/swapon02.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, 2002-2023
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  * This test case checks whether swapon(2) system call returns:
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when the path does not exist
13*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL when the path exists but is invalid
14*49cdfc7eSAndroid Build Coastguard Worker  * - EPERM when user is not a superuser
15*49cdfc7eSAndroid Build Coastguard Worker  * - EBUSY when the specified path is already being used as a swap area
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "libswap.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT	"mntpoint"
25*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE	MNTPOINT"/testswap"
26*49cdfc7eSAndroid Build Coastguard Worker #define NOTSWAP_FILE	MNTPOINT"/notswap"
27*49cdfc7eSAndroid Build Coastguard Worker #define SWAP_FILE	MNTPOINT"/swapfile"
28*49cdfc7eSAndroid Build Coastguard Worker #define USED_FILE	MNTPOINT"/alreadyused"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid;
31*49cdfc7eSAndroid Build Coastguard Worker static int do_swapoff;
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
34*49cdfc7eSAndroid Build Coastguard Worker 	char *err_desc;
35*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
36*49cdfc7eSAndroid Build Coastguard Worker 	char *path;
37*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
38*49cdfc7eSAndroid Build Coastguard Worker 	{"Path does not exist", ENOENT, "./doesnotexist"},
39*49cdfc7eSAndroid Build Coastguard Worker 	{"Invalid path", EINVAL, NOTSWAP_FILE},
40*49cdfc7eSAndroid Build Coastguard Worker 	{"Permission denied", EPERM, SWAP_FILE},
41*49cdfc7eSAndroid Build Coastguard Worker 	{"File already used", EBUSY, USED_FILE},
42*49cdfc7eSAndroid Build Coastguard Worker };
43*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *nobody;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	nobody = SAFE_GETPWNAM("nobody");
49*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = nobody->pw_uid;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	is_swap_supported(TEST_FILE);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(NOTSWAP_FILE, 0777, NULL);
54*49cdfc7eSAndroid Build Coastguard Worker 	MAKE_SMALL_SWAPFILE(SWAP_FILE);
55*49cdfc7eSAndroid Build Coastguard Worker 	MAKE_SMALL_SWAPFILE(USED_FILE);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_syscall(__NR_swapon, USED_FILE, 0))
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "swapon(alreadyused) failed");
59*49cdfc7eSAndroid Build Coastguard Worker 	else
60*49cdfc7eSAndroid Build Coastguard Worker 		do_swapoff = 1;
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)63*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	if (do_swapoff && tst_syscall(__NR_swapoff, USED_FILE))
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN | TERRNO, "swapoff(alreadyused) failed");
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker 
verify_swapon(unsigned int i)69*49cdfc7eSAndroid Build Coastguard Worker static void verify_swapon(unsigned int i)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = tcases + i;
72*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == EPERM)
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(nobody_uid);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(tst_syscall(__NR_swapon, tc->path, 0), tc->exp_errno,
76*49cdfc7eSAndroid Build Coastguard Worker 		     "swapon(2) fail with %s", tc->err_desc);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == EPERM)
79*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(0);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "swapon(2) failed unexpectedly, expected: %s",
83*49cdfc7eSAndroid Build Coastguard Worker 			tst_strerrno(tc->exp_errno));
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
88*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
89*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
90*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
91*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
92*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_swapon,
93*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
94*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
95*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
96*49cdfc7eSAndroid Build Coastguard Worker };
97