xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/swapoff/swapoff02.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  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This test case checks whether swapoff(2) system call  returns
10*49cdfc7eSAndroid Build Coastguard Worker  *  1. EINVAL when the path does not exist
11*49cdfc7eSAndroid Build Coastguard Worker  *  2. ENOENT when the path exists but is invalid
12*49cdfc7eSAndroid Build Coastguard Worker  *  3. EPERM when user is not a superuser
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.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 TEST_FILE	MNTPOINT"/testswap"
23*49cdfc7eSAndroid Build Coastguard Worker #define SWAP_FILE	MNTPOINT"/swapfile"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static int setup01(void);
26*49cdfc7eSAndroid Build Coastguard Worker static void cleanup01(void);
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
31*49cdfc7eSAndroid Build Coastguard Worker 	char *err_desc;
32*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
33*49cdfc7eSAndroid Build Coastguard Worker 	char *exp_errval;
34*49cdfc7eSAndroid Build Coastguard Worker 	char *path;
35*49cdfc7eSAndroid Build Coastguard Worker 	int (*setup)(void);
36*49cdfc7eSAndroid Build Coastguard Worker 	void (*cleanup)(void);
37*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
38*49cdfc7eSAndroid Build Coastguard Worker 	{"path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL},
39*49cdfc7eSAndroid Build Coastguard Worker 	{"Invalid file", EINVAL, "EINVAL", SWAP_FILE, NULL, NULL},
40*49cdfc7eSAndroid Build Coastguard Worker 	{"Permission denied", EPERM, "EPERM", SWAP_FILE, setup01, cleanup01}
41*49cdfc7eSAndroid Build Coastguard Worker };
42*49cdfc7eSAndroid Build Coastguard Worker 
verify_swapoff(unsigned int i)43*49cdfc7eSAndroid Build Coastguard Worker static void verify_swapoff(unsigned int i)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = tcases + i;
46*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->setup)
47*49cdfc7eSAndroid Build Coastguard Worker 		tc->setup();
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_swapoff, tc->path));
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->cleanup)
52*49cdfc7eSAndroid Build Coastguard Worker 		tc->cleanup();
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && (TST_ERR == tc->exp_errno)) {
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "swapoff(2) expected failure;"
56*49cdfc7eSAndroid Build Coastguard Worker 			" Got errno - %s : %s",
57*49cdfc7eSAndroid Build Coastguard Worker 			tc->exp_errval, tc->err_desc);
58*49cdfc7eSAndroid Build Coastguard Worker 	} else {
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "swapoff(2) failed to produce"
60*49cdfc7eSAndroid Build Coastguard Worker 			" expected error; %d, errno"
61*49cdfc7eSAndroid Build Coastguard Worker 			": %s and got %d",
62*49cdfc7eSAndroid Build Coastguard Worker 			tc->exp_errno, tc->exp_errval, TST_ERR);
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 		if ((TST_RET == 0) && (i == 2)) {
65*49cdfc7eSAndroid Build Coastguard Worker 			if (tst_syscall(__NR_swapon, "./swapfile01", 0) != 0)
66*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK | TERRNO, " Failed to turn on swap file");
67*49cdfc7eSAndroid Build Coastguard Worker 		}
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
setup01(void)71*49cdfc7eSAndroid Build Coastguard Worker static int setup01(void)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(nobody_uid);
74*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker 
cleanup01(void)77*49cdfc7eSAndroid Build Coastguard Worker static void cleanup01(void)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(0);
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)82*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *nobody;
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	nobody = SAFE_GETPWNAM("nobody");
87*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = nobody->pw_uid;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	is_swap_supported(TEST_FILE);
90*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MAKE_SMALL_SWAPFILE(SWAP_FILE);
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
94*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
95*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
96*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
97*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
98*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_swapoff,
99*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
100*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup
101*49cdfc7eSAndroid Build Coastguard Worker };
102