blob: 61eeef3f44de623d2f17acd0510c4d6a7367d9b2 [file] [log] [blame]
robbiew2db8c7e2003-01-01 20:50:36 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew2db8c7e2003-01-01 20:50:36 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew2db8c7e2003-01-01 20:50:36 +000019 * TEST IDENTIFIER : ioperm02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiew2db8c7e2003-01-01 20:50:36 +000021 * EXECUTED BY : superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiew2db8c7e2003-01-01 20:50:36 +000023 * TEST TITLE : Tests for error conditions
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiew2db8c7e2003-01-01 20:50:36 +000025 * TEST CASE TOTAL : 2
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiew2db8c7e2003-01-01 20:50:36 +000027 * AUTHOR : Subhab Biwas <subhabrata.biswas@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiew2db8c7e2003-01-01 20:50:36 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * Verify that
subrata_modak4bb656a2009-02-26 12:02:09 +000035 * 1) ioperm(2) returns -1 and sets errno to EINVAL for I/O port
36 * address greater than 0x3ff.
robbiew2db8c7e2003-01-01 20:50:36 +000037 * 2) ioperm(2) returns -1 and sets errno to EPERM if the current
subrata_modak4bb656a2009-02-26 12:02:09 +000038 * user is not the super-user.
robbiew2db8c7e2003-01-01 20:50:36 +000039 *
40 * Setup:
41 * Setup signal handling.
42 * Set expected errnos for logging
43 * Pause for SIGUSR1 if option specified.
44 *
45 * Test:
46 * Loop if the proper options are given.
47 * Execute system call
48 * Check return code and error number, if matching,
49 * Issue PASS message
50 * Otherwise,
51 * Issue FAIL message
52 * Perform testcase specific cleanup (if needed)
53 *
54 * Cleanup:
55 * Print errno log and/or timing stats if options given
56 *
57 * USAGE: <for command-line>
58 * ioperm02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
59 * where, -c n : Run n copies concurrently.
60 * -e : Turn on errno logging.
61 * -h : Show help screen
62 * -f : Turn off functional testing
63 * -i n : Execute test n times.
64 * -I x : Execute test for x seconds.
65 * -p : Pause for SIGUSR1 before starting
66 * -P x : Pause for x seconds between iterations.
67 * -t : Turn on syscall timing.
68 *
69 ****************************************************************/
robbiew4d6995c2003-01-10 18:26:33 +000070
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020071char *TCID = "ioperm02";
robbiew4d6995c2003-01-10 18:26:33 +000072
robbiew9e3583b2003-02-12 19:44:48 +000073#if defined __i386__ || defined(__x86_64__)
74
robbiew2db8c7e2003-01-01 20:50:36 +000075#include <errno.h>
76#include <unistd.h>
77#include <sys/io.h>
78#include <pwd.h>
79#include "test.h"
robbiew2db8c7e2003-01-01 20:50:36 +000080
robbiew2db8c7e2003-01-01 20:50:36 +000081#define NUM_BYTES 3
82#define TURN_ON 1
83#define TURN_OFF 0
84#define EXP_RET_VAL -1
mridge5814a0e2005-06-01 20:36:41 +000085#ifndef IO_BITMAP_BITS
subrata_modak56207ce2009-03-23 13:35:39 +000086#define IO_BITMAP_BITS 1024 /* set to default value since some H/W may not support 0x10000 even with a 2.6.8 kernel */
mridgeadab3442006-01-30 22:44:57 +000087#define IO_BITMAP_BITS_16 65536
mridge5814a0e2005-06-01 20:36:41 +000088#endif
robbiew2db8c7e2003-01-01 20:50:36 +000089
90static void setup();
subrata_modak56207ce2009-03-23 13:35:39 +000091static int setup1(void);
robbiew2db8c7e2003-01-01 20:50:36 +000092static void cleanup1();
93static void cleanup();
94
robbiew2db8c7e2003-01-01 20:50:36 +000095static char nobody_uid[] = "nobody";
96struct passwd *ltpuser;
97
98struct test_cases_t {
subrata_modak56207ce2009-03-23 13:35:39 +000099 long from; /* starting port address */
100 long num; /* no. of bytes from starting address */
101 int turn_on;
102 char *desc; /* test case description */
103 int exp_errno; /* expected error number */
subrata_modak4bb656a2009-02-26 12:02:09 +0000104};
robbiew2db8c7e2003-01-01 20:50:36 +0000105
robbiew89e9bf92004-12-20 20:08:18 +0000106int TST_TOTAL = 2;
subrata_modak56207ce2009-03-23 13:35:39 +0000107struct test_cases_t *test_cases;
robbiew2db8c7e2003-01-01 20:50:36 +0000108
robbiew89e9bf92004-12-20 20:08:18 +0000109int main(int ac, char **av)
robbiew2db8c7e2003-01-01 20:50:36 +0000110{
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800111 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200112 const char *msg;
robbiew2db8c7e2003-01-01 20:50:36 +0000113
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800114 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800115 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew2db8c7e2003-01-01 20:50:36 +0000116
robbiew2db8c7e2003-01-01 20:50:36 +0000117 setup();
118
robbiew2db8c7e2003-01-01 20:50:36 +0000119 for (lc = 0; TEST_LOOPING(lc); lc++) {
120
Caspar Zhangd59a6592013-03-07 14:59:12 +0800121 tst_count = 0;
robbiew2db8c7e2003-01-01 20:50:36 +0000122
123 for (i = 0; i < TST_TOTAL; ++i) {
124
125 if (i == 1) {
126 /* setup Non super-user for second test */
127 if (setup1()) {
128 /* setup1() failed, skip this test */
129 continue;
130 }
131 }
132
133 /* Test the system call */
134
135 TEST(ioperm(test_cases[i].from,
subrata_modak56207ce2009-03-23 13:35:39 +0000136 test_cases[i].num, test_cases[i].turn_on));
robbiew2db8c7e2003-01-01 20:50:36 +0000137
138 if ((TEST_RETURN == EXP_RET_VAL) &&
subrata_modak56207ce2009-03-23 13:35:39 +0000139 (TEST_ERRNO == test_cases[i].exp_errno)) {
140 tst_resm(TPASS, "Expected failure for %s, "
141 "errno: %d", test_cases[i].desc,
142 TEST_ERRNO);
robbiew2db8c7e2003-01-01 20:50:36 +0000143 } else {
144 tst_resm(TFAIL, "Unexpected results for %s ; "
subrata_modak358c3ee2009-10-26 14:55:46 +0000145 "returned %ld (expected %d), errno %d "
subrata_modak56207ce2009-03-23 13:35:39 +0000146 "(expected errno %d)",
147 test_cases[i].desc,
148 TEST_RETURN, EXP_RET_VAL,
149 TEST_ERRNO, test_cases[i].exp_errno);
robbiew2db8c7e2003-01-01 20:50:36 +0000150 }
151
robbiew2db8c7e2003-01-01 20:50:36 +0000152 if (i == 1) {
153 /* revert back to super user */
154 cleanup1();
robbiew89e9bf92004-12-20 20:08:18 +0000155 } else {
robbiew2db8c7e2003-01-01 20:50:36 +0000156 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000157 }
robbiew2db8c7e2003-01-01 20:50:36 +0000158
Garrett Cooper2c282152010-12-16 00:55:50 -0800159 }
robbiew2db8c7e2003-01-01 20:50:36 +0000160
161 /* cleanup and exit */
162 cleanup();
163
Garrett Cooper53740502010-12-16 00:04:01 -0800164 tst_exit();
robbiew2db8c7e2003-01-01 20:50:36 +0000165
Garrett Cooper2c282152010-12-16 00:55:50 -0800166}
robbiew2db8c7e2003-01-01 20:50:36 +0000167
168/* setup1() - set up non-super user for second test case */
subrata_modak56207ce2009-03-23 13:35:39 +0000169int setup1(void)
robbiew2db8c7e2003-01-01 20:50:36 +0000170{
171 /* switch to "nobody" user */
172 if (seteuid(ltpuser->pw_uid) == -1) {
173 tst_resm(TWARN, "Failed to set effective"
subrata_modak56207ce2009-03-23 13:35:39 +0000174 "uid to %d", ltpuser->pw_uid);
robbiew2db8c7e2003-01-01 20:50:36 +0000175 return 1;
176 }
177 return 0;
178}
179
robbiew89e9bf92004-12-20 20:08:18 +0000180/* cleanup1() - reset to super user for second test case */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400181void cleanup1(void)
robbiew2db8c7e2003-01-01 20:50:36 +0000182{
183 /* reset user as root */
184 if (seteuid(0) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800185 tst_brkm(TBROK, NULL, "Failed to set uid as root");
robbiew2db8c7e2003-01-01 20:50:36 +0000186 }
187}
188
189/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400190void setup(void)
robbiew2db8c7e2003-01-01 20:50:36 +0000191{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200192 tst_require_root(NULL);
robbiew2db8c7e2003-01-01 20:50:36 +0000193
robbiew2db8c7e2003-01-01 20:50:36 +0000194 tst_sig(NOFORK, DEF_HANDLER, cleanup);
195
robbiew2db8c7e2003-01-01 20:50:36 +0000196 /* Check if "nobody" user id exists */
197 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800198 tst_brkm(TBROK, NULL, "\"nobody\" user id doesn't exist");
robbiew2db8c7e2003-01-01 20:50:36 +0000199 }
200
robbiew89e9bf92004-12-20 20:08:18 +0000201 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000202 * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed
robbiew89e9bf92004-12-20 20:08:18 +0000203 * from kernel 2.6.8 to permit 16-bits (65536) ioperm
204 *
205 * Ricky Ng-Adam, rngadam@yahoo.com
206 * */
Cyril Hrubisd218f342014-09-23 13:14:56 +0200207 test_cases = malloc(sizeof(struct test_cases_t) * 2);
robbiew89e9bf92004-12-20 20:08:18 +0000208 test_cases[0].num = NUM_BYTES;
209 test_cases[0].turn_on = TURN_ON;
210 test_cases[0].desc = "Invalid I/O address";
211 test_cases[0].exp_errno = EINVAL;
212 test_cases[1].num = NUM_BYTES;
213 test_cases[1].turn_on = TURN_ON;
214 test_cases[1].desc = "Non super-user";
215 test_cases[1].exp_errno = EPERM;
subrata_modak56207ce2009-03-23 13:35:39 +0000216 if ((tst_kvercmp(2, 6, 8) < 0) || (tst_kvercmp(2, 6, 9) == 0)) {
217 /*try invalid ioperm on 1022, 1023, 1024 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000218 test_cases[0].from = (IO_BITMAP_BITS - NUM_BYTES) + 1;
robbiew89e9bf92004-12-20 20:08:18 +0000219
subrata_modak56207ce2009-03-23 13:35:39 +0000220 /*try get valid ioperm on 1021, 1022, 1023 */
mridge5814a0e2005-06-01 20:36:41 +0000221 test_cases[1].from = IO_BITMAP_BITS - NUM_BYTES;
subrata_modak56207ce2009-03-23 13:35:39 +0000222 } else {
223 /*try invalid ioperm on 65534, 65535, 65536 */
mridgeadab3442006-01-30 22:44:57 +0000224 test_cases[0].from = (IO_BITMAP_BITS_16 - NUM_BYTES) + 1;
robbiew89e9bf92004-12-20 20:08:18 +0000225
subrata_modak56207ce2009-03-23 13:35:39 +0000226 /*try valid ioperm on 65533, 65534, 65535 */
mridgeadab3442006-01-30 22:44:57 +0000227 test_cases[1].from = IO_BITMAP_BITS_16 - NUM_BYTES;
robbiew89e9bf92004-12-20 20:08:18 +0000228 }
229
robbiew2db8c7e2003-01-01 20:50:36 +0000230 TEST_PAUSE;
231
Garrett Cooper2c282152010-12-16 00:55:50 -0800232}
robbiew2db8c7e2003-01-01 20:50:36 +0000233
Mike Frysingerc57fba52014-04-09 18:56:30 -0400234void cleanup(void)
robbiew2db8c7e2003-01-01 20:50:36 +0000235{
236
Garrett Cooper2c282152010-12-16 00:55:50 -0800237}
robbiew4d6995c2003-01-10 18:26:33 +0000238
239#else /* __i386__ */
240
241#include "test.h"
robbiew4d6995c2003-01-10 18:26:33 +0000242
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200243int TST_TOTAL = 0;
robbiew4d6995c2003-01-10 18:26:33 +0000244
Mike Frysingerc57fba52014-04-09 18:56:30 -0400245int main(void)
robbiew4d6995c2003-01-10 18:26:33 +0000246{
subrata_modak56207ce2009-03-23 13:35:39 +0000247 tst_resm(TPASS,
248 "LSB v1.3 does not specify ioperm() for this architecture.");
robbiew4d6995c2003-01-10 18:26:33 +0000249 tst_exit();
250}
251
Chris Dearmanec6edca2012-10-17 19:54:01 -0700252#endif /* __i386__ */