Home | History | Annotate | Download | only in iopl
      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 (at) 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 #include "safe_macros.h"
     82 
     83 #define INVALID_LEVEL 4		/* Invalid privilege level */
     84 #define EXP_RET_VAL -1
     85 
     86 static void setup();
     87 static int setup1(void);
     88 static void cleanup1();
     89 static void cleanup();
     90 
     91 static char nobody_uid[] = "nobody";
     92 struct passwd *ltpuser;
     93 
     94 struct test_cases_t {
     95 	int level;		/* I/O privilege level */
     96 	char *desc;		/* test case description */
     97 	int exp_errno;		/* expected error number */
     98 } test_cases[] = {
     99 	{
    100 	INVALID_LEVEL, "Invalid privilege level", EINVAL}, {
    101 	1, "Non super-user", EPERM}
    102 };
    103 
    104 int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
    105 
    106 int main(int ac, char **av)
    107 {
    108 
    109 	int lc, i;
    110 
    111 	tst_parse_opts(ac, av, NULL, NULL);
    112 
    113 	setup();
    114 
    115 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    116 
    117 		tst_count = 0;
    118 
    119 		for (i = 0; i < TST_TOTAL; ++i) {
    120 
    121 			if (i == 1) {
    122 				/* setup Non super-user for second test */
    123 				if (setup1()) {
    124 					/* setup1() failed, skip this test */
    125 					continue;
    126 				}
    127 			}
    128 
    129 			/*
    130 			 * Call iopl(2)
    131 			 */
    132 			TEST(iopl(test_cases[i].level));
    133 
    134 			if ((TEST_RETURN == EXP_RET_VAL) &&
    135 			    (TEST_ERRNO == test_cases[i].exp_errno)) {
    136 				tst_resm(TPASS, "Expected failure for %s, "
    137 					 "errno: %d", test_cases[i].desc,
    138 					 TEST_ERRNO);
    139 			} else {
    140 				tst_resm(TFAIL, "Unexpected results for %s ; "
    141 					 "returned %ld (expected %d), errno %d "
    142 					 "(expected errno  %d)",
    143 					 test_cases[i].desc,
    144 					 TEST_RETURN, EXP_RET_VAL,
    145 					 TEST_ERRNO, test_cases[i].exp_errno);
    146 			}
    147 
    148 			if (i == 1) {
    149 				/* revert back to super user */
    150 				cleanup1();
    151 			}
    152 
    153 		}
    154 	}
    155 
    156 	/* cleanup and exit */
    157 	cleanup();
    158 
    159 	tst_exit();
    160 
    161 }
    162 
    163 /* setup1() - set up non-super user for second test case */
    164 int setup1(void)
    165 {
    166 	/* switch to "nobody" user */
    167 	if (seteuid(ltpuser->pw_uid) == -1) {
    168 		tst_resm(TWARN, "Failed to set effective"
    169 			 "uid to %d", ltpuser->pw_uid);
    170 		return 1;
    171 	}
    172 	return 0;
    173 }
    174 
    175 /* cleanup1() - reset to super user for first test case */
    176 void cleanup1(void)
    177 {
    178 	/* reset user as root */
    179 	SAFE_SETEUID(NULL, 0);
    180 }
    181 
    182 /* setup() - performs all ONE TIME setup for this test */
    183 void setup(void)
    184 {
    185 	tst_require_root();
    186 
    187 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    188 
    189 	/* Check if "nobody" user id exists */
    190 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
    191 		tst_brkm(TBROK, NULL, "\"nobody\" user id doesn't exist");
    192 	}
    193 
    194 	TEST_PAUSE;
    195 
    196 }
    197 
    198 /*
    199  *cleanup() -  performs all ONE TIME cleanup for this test at
    200  *		completion or premature exit.
    201  */
    202 void cleanup(void)
    203 {
    204 
    205 }
    206 
    207 #else /* __i386__ */
    208 
    209 #include "test.h"
    210 #include "safe_macros.h"
    211 
    212 int TST_TOTAL = 0;
    213 
    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