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