Home | History | Annotate | Download | only in munlockall
      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	: munlockall02
     20  *
     21  *    EXECUTED BY	: root / superuser
     22  *
     23  *    TEST TITLE	: test for EPERM error value when run as non superuser
     24  *
     25  *    TEST CASE TOTAL	: 1
     26  *
     27  *    AUTHOR		: sowmya adiga<sowmya.adiga (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 munlockall(2) returns -1 and sets errno to EPERM
     35  *	if the effective userid of the caller is not super-user.
     36  *	$
     37  * 	Setup:
     38  *	  Setup signal handling.
     39  *	  Pause for SIGUSR1 if option specified.
     40  *        Change effective user id to "nobody" user
     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  *	                    (errno set == expected errno)
     47  *	 Issue sys call pass with expected return value and errno.
     48  *	 otherwise,
     49  *	  Issue sys call fails with unexpected errno.
     50  *
     51  *
     52  * 	Cleanup:
     53  *      change effective user id to root
     54  * 	Print errno log and/or timing stats if options given
     55  *
     56  * USAGE:  <for command-line>
     57  *  munlockall02 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
     58  *		where,		-c n : Run n copies concurrently
     59  *				-e   : Turn on errno logging.
     60  *				-h   : Show this help screen
     61  *				-i n : Execute test n times.
     62  *				-I x : Execute test for x seconds.
     63  *				-p   : Pause for SIGUSR1 before starting
     64  *                      	-P x : Pause for x seconds between iterations.
     65  *                       	 t   : Turn on syscall timing.
     66  *
     67  *
     68  *****************************************************************************/
     69 #include <errno.h>
     70 #include <pwd.h>
     71 #include <sys/mman.h>
     72 #include "test.h"
     73 
     74 void setup();
     75 void cleanup();
     76 
     77 char *TCID = "munlockall02";
     78 int TST_TOTAL = 1;
     79 
     80 static char nobody_uid[] = "nobody";
     81 struct passwd *ltpuser;
     82 
     83 #if !defined(UCLINUX)
     84 
     85 int main(int ac, char **av)
     86 {
     87 	int lc;
     88 
     89 	tst_parse_opts(ac, av, NULL, NULL);
     90 
     91 	setup();
     92 
     93 	/* check looping state */
     94 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     95 
     96 		tst_count = 0;
     97 
     98 		TEST(munlockall());
     99 		/* check return code */
    100 		if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
    101 			tst_resm(TPASS, "munlockall() failed"
    102 				 " as expected for non-superuser" ":GOT EPERM");
    103 		} else {
    104 			tst_resm(TCONF, "munlockall() failed to produce "
    105 				 "expected errno :%d Got : %d, %s. ***Some distros, such as Red Hat Enterprise Linux, support non-superuser munlockall calls.***",
    106 				 EPERM, TEST_ERRNO, strerror(TEST_ERRNO));
    107 
    108 		}
    109 	}
    110 
    111 	/* cleanup and exit */
    112 	cleanup();
    113 	tst_exit();
    114 
    115 }
    116 
    117 /* setup() - performs all ONE TIME setup for this test. */
    118 void setup(void)
    119 {
    120 	tst_require_root();
    121 
    122 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    123 
    124 	/* switch to nobody user */
    125 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
    126 		tst_brkm(TBROK, NULL, "\"nobody\"user not present");
    127 	}
    128 
    129 	if (seteuid(ltpuser->pw_uid) == -1) {
    130 		tst_brkm(TBROK, NULL, "seteuid failed to "
    131 			 "to set the effective uid to %d", ltpuser->pw_uid);
    132 	}
    133 
    134 	TEST_PAUSE;
    135 }
    136 
    137 #else
    138 
    139 int main(void)
    140 {
    141 	tst_resm(TINFO, "test is not available on uClinux");
    142 	tst_exit();
    143 }
    144 
    145 #endif /* if !defined(UCLINUX) */
    146 
    147 /*
    148  * cleanup() - performs all ONE TIME cleanup for this test at
    149  *		completion or premature exit.
    150  */
    151 void cleanup(void)
    152 {
    153 	if (seteuid(0) == -1) {
    154 		tst_resm(TWARN, "seteuid failed to "
    155 			 "to set the effective uid to root");
    156 		perror("setuid");
    157 	}
    158 
    159 }
    160