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	: iopl01
20  *
21  *    EXECUTED BY	: superuser
22  *
23  *    TEST TITLE	: Basic test for iopl(2)
24  *
25  *    TEST CASE TOTAL	: 4
26  *
27  *    AUTHOR		: Subhab Biswas <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  *	This is a Phase I test for the iopl(2) system call.
35  *	It is intended to provide a limited exposure of the system call.
36  *
37  * 	Setup:
38  * 	  Setup signal handling.
39  *	  Test caller is superuser
40  *	  Pause for SIGUSR1 if option specified.
41  *
42  * 	Test:
43  *	 Loop if the proper options are given.
44  * 	  Execute system call
45  *        Check return code, if system call failed (return=-1)
46  *              Issue FAIL message with errno.
47  *        Otherwise, Issue PASS message.
48  *
49  * 	Cleanup:
50  * 	  Print errno log and/or timing stats if options given
51  *
52  * USAGE:  <for command-line>
53  * iopl01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
54  *			where,  -c n : Run n copies concurrently.
55  *				-e   : Turn on errno logging.
56  *				-h   : Show help screen
57  *				-f   : Turn off functional testing
58  *				-i n : Execute test n times.
59  *				-I x : Execute test for x seconds.
60  *				-p   : Pause for SIGUSR1 before starting
61  *				-P x : Pause for x seconds between iterations.
62  *				-t   : Turn on syscall timing.
63  *
64  ****************************************************************/
65 
66 char *TCID = "iopl01";
67 
68 #if defined __i386__ || defined(__x86_64__)
69 
70 #include <errno.h>
71 #include <unistd.h>
72 #include <sys/io.h>
73 
74 #include "test.h"
75 
76 static void setup();
77 static void cleanup();
78 
79 int TST_TOTAL = 4;
80 
81 int level;			/* I/O privilege level of the process */
82 
83 int main(int ac, char **av)
84 {
85 
86 	int lc;
87 
88 	tst_parse_opts(ac, av, NULL, NULL);
89 
90 	setup();
91 
92 	for (lc = 0; TEST_LOOPING(lc); lc++) {
93 
94 		tst_count = 0;
95 
96 		/*
97 		 * Test the system call for possible privelege levels.
98 		 * As the privelge level for a normal process is 0,
99 		 * start by setting/changing the level to 0.
100 		 */
101 		for (level = 0; level < TST_TOTAL; ++level) {
102 
103 			TEST(iopl(level));
104 
105 			if (TEST_RETURN == -1) {
106 				tst_resm(TFAIL, "iopl() failed for level %d, "
107 					 "errno=%d : %s", level,
108 					 TEST_ERRNO, strerror(TEST_ERRNO));
109 			} else {
110 				tst_resm(TPASS, "iopl() passed for level %d, "
111 					 "returned %ld", level, TEST_RETURN);
112 			}
113 		}
114 	}
115 
116 	/* cleanup and exit */
117 	cleanup();
118 	tst_exit();
119 
120 }
121 
122 /* setup() - performs all ONE TIME setup for this test */
123 void setup(void)
124 {
125 	tst_require_root();
126 
127 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
128 
129 	TEST_PAUSE;
130 
131 }
132 
133 /*
134  *cleanup() -  performs all ONE TIME cleanup for this test at
135  *		completion or premature exit.
136  */
137 void cleanup(void)
138 {
139 
140 	/*
141 	 * back to I/O privilege for normal process.
142 	 */
143 	if (iopl(0) == -1) {
144 		tst_resm(TWARN, "iopl() cleanup failed");
145 	}
146 
147 }
148 
149 #else /* __i386__ */
150 
151 #include "test.h"
152 
153 int TST_TOTAL = 0;
154 
155 int main(void)
156 {
157 	tst_resm(TPASS,
158 		 "LSB v1.3 does not specify iopl() for this architecture.");
159 	tst_exit();
160 }
161 
162 #endif /* __i386__ */
163