xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ioperm/ioperm01.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) Linux Test Project, 2020
4*49cdfc7eSAndroid Build Coastguard Worker  *  Copyright (c) Wipro Technologies Ltd, 2002
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * This is a basic test for ioperm(2) system call.
9*49cdfc7eSAndroid Build Coastguard Worker  * It is intended to provide a limited exposure of the system call.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Author: Subhab Biswas <[email protected]>
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #if defined __i386__ || defined(__x86_64__)
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/io.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker unsigned long io_addr;
23*49cdfc7eSAndroid Build Coastguard Worker #define NUM_BYTES 3
24*49cdfc7eSAndroid Build Coastguard Worker #ifndef IO_BITMAP_BITS
25*49cdfc7eSAndroid Build Coastguard Worker #define IO_BITMAP_BITS 1024
26*49cdfc7eSAndroid Build Coastguard Worker #endif
27*49cdfc7eSAndroid Build Coastguard Worker 
verify_ioperm(void)28*49cdfc7eSAndroid Build Coastguard Worker static void verify_ioperm(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	TEST(ioperm(io_addr, NUM_BYTES, 1));
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
33*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "ioperm() failed for port address "
34*49cdfc7eSAndroid Build Coastguard Worker 				"%lu,  errno=%d : %s", io_addr,
35*49cdfc7eSAndroid Build Coastguard Worker 				TST_ERR, tst_strerrno(TST_ERR));
36*49cdfc7eSAndroid Build Coastguard Worker 	} else {
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "ioperm() passed for port "
38*49cdfc7eSAndroid Build Coastguard Worker 				"address %lu, returned %lu",
39*49cdfc7eSAndroid Build Coastguard Worker 				io_addr, TST_RET);
40*49cdfc7eSAndroid Build Coastguard Worker 	}
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)43*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	io_addr = IO_BITMAP_BITS - NUM_BYTES;
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)48*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	/*
51*49cdfc7eSAndroid Build Coastguard Worker 	 * Reset I/O privileges for the specified port.
52*49cdfc7eSAndroid Build Coastguard Worker 	 */
53*49cdfc7eSAndroid Build Coastguard Worker 	if ((ioperm(io_addr, NUM_BYTES, 0)) == -1)
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "ioperm() cleanup failed");
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
58*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_ioperm,
59*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
60*49cdfc7eSAndroid Build Coastguard Worker 	/* ioperm() is restricted under kernel lockdown. */
61*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_lockdown = 1,
62*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
63*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
64*49cdfc7eSAndroid Build Coastguard Worker };
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker #else
67*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("LSB v1.3 does not specify ioperm() for this architecture. (only for i386 or x86_64)");
68*49cdfc7eSAndroid Build Coastguard Worker #endif /* __i386_, __x86_64__*/
69