1 /*
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
13 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 */
17 /**********************************************************
18 *
19 * TEST IDENTIFIER : iopl02
20 *
21 * EXECUTED BY : superuser
22 *
23 * TEST TITLE : Tests for error conditions
24 *
25 * TEST CASE TOTAL : 2
26 *
27 * AUTHOR : Subhab Biwas <subhabrata.biswas@wipro.com>
28 *
29 * 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
35 * 1) iopl(2) returns -1 and sets errno to EINVAL for privilege
36 * level greater than 3.
37 * 2) iopl(2) returns -1 and sets errno to EPERM if the current
38 * user is not the super-user.
39 *
40 * Setup:
41 * Setup signal handling.
42 * Test caller is superuser
43 * Set expected errnos for logging
44 * Pause for SIGUSR1 if option specified.
45 *
46 * Test:
47 * Loop if the proper options are given.
48 * Execute system call
49 * Check return code and error number, if matching,
50 * Issue PASS message
51 * Otherwise,
52 * Issue FAIL message
53 * Perform testcase specific cleanup (if needed)
54 *
55 * Cleanup:
56 * Print errno log and/or timing stats if options given
57 *
58 * USAGE: <for command-line>
59 * iopl02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
60 * where, -c n : Run n copies concurrently.
61 * -e : Turn on errno logging.
62 * -h : Show help screen
63 * -f : Turn off functional testing
64 * -i n : Execute test n times.
65 * -I x : Execute test for x seconds.
66 * -p : Pause for SIGUSR1 before starting
67 * -P x : Pause for x seconds between iterations.
68 * -t : Turn on syscall timing.
69 *
70 ****************************************************************/
71
72 char *TCID = "iopl02";
73
74 #if defined __i386__ || defined(__x86_64__)
75
76 #include <errno.h>
77 #include <unistd.h>
78 #include <sys/io.h>
79 #include <pwd.h>
80 #include "test.h"
81
82 #define INVALID_LEVEL 4 /* Invalid privilege level */
83 #define EXP_RET_VAL -1
84
85 static void setup();
86 static int setup1(void);
87 static void cleanup1();
88 static void cleanup();
89
90 static char nobody_uid[] = "nobody";
91 struct passwd *ltpuser;
92
93 struct test_cases_t {
94 int level; /* I/O privilege level */
95 char *desc; /* test case description */
96 int exp_errno; /* expected error number */
97 } test_cases[] = {
98 {
99 INVALID_LEVEL, "Invalid privilege level", EINVAL}, {
100 1, "Non super-user", EPERM}
101 };
102
103 int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
104
main(int ac,char ** av)105 int main(int ac, char **av)
106 {
107
108 int lc, i;
109
110 tst_parse_opts(ac, av, NULL, NULL);
111
112 setup();
113
114 for (lc = 0; TEST_LOOPING(lc); lc++) {
115
116 tst_count = 0;
117
118 for (i = 0; i < TST_TOTAL; ++i) {
119
120 if (i == 1) {
121 /* setup Non super-user for second test */
122 if (setup1()) {
123 /* setup1() failed, skip this test */
124 continue;
125 }
126 }
127
128 /*
129 * Call iopl(2)
130 */
131 TEST(iopl(test_cases[i].level));
132
133 if ((TEST_RETURN == EXP_RET_VAL) &&
134 (TEST_ERRNO == test_cases[i].exp_errno)) {
135 tst_resm(TPASS, "Expected failure for %s, "
136 "errno: %d", test_cases[i].desc,
137 TEST_ERRNO);
138 } else {
139 tst_resm(TFAIL, "Unexpected results for %s ; "
140 "returned %ld (expected %d), errno %d "
141 "(expected errno %d)",
142 test_cases[i].desc,
143 TEST_RETURN, EXP_RET_VAL,
144 TEST_ERRNO, test_cases[i].exp_errno);
145 }
146
147 if (i == 1) {
148 /* revert back to super user */
149 cleanup1();
150 }
151
152 }
153 }
154
155 /* cleanup and exit */
156 cleanup();
157
158 tst_exit();
159
160 }
161
162 /* setup1() - set up non-super user for second test case */
setup1(void)163 int setup1(void)
164 {
165 /* switch to "nobody" user */
166 if (seteuid(ltpuser->pw_uid) == -1) {
167 tst_resm(TWARN, "Failed to set effective"
168 "uid to %d", ltpuser->pw_uid);
169 return 1;
170 }
171 return 0;
172 }
173
174 /* cleanup1() - reset to super user for first test case */
cleanup1(void)175 void cleanup1(void)
176 {
177 /* reset user as root */
178 if (seteuid(0) == -1) {
179 tst_brkm(TBROK, NULL, "Failed to set uid as root");
180 }
181 }
182
183 /* setup() - performs all ONE TIME setup for this test */
setup(void)184 void setup(void)
185 {
186 tst_require_root();
187
188 tst_sig(NOFORK, DEF_HANDLER, cleanup);
189
190 /* Check if "nobody" user id exists */
191 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
192 tst_brkm(TBROK, NULL, "\"nobody\" user id doesn't exist");
193 }
194
195 TEST_PAUSE;
196
197 }
198
199 /*
200 *cleanup() - performs all ONE TIME cleanup for this test at
201 * completion or premature exit.
202 */
cleanup(void)203 void cleanup(void)
204 {
205
206 }
207
208 #else /* __i386__ */
209
210 #include "test.h"
211
212 int TST_TOTAL = 0;
213
main(void)214 int main(void)
215 {
216 tst_resm(TPASS,
217 "LSB v1.3 does not specify iopl() for this architecture.");
218 tst_exit();
219 }
220
221 #endif /* __i386__ */
222