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@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 #include "safe_macros.h"
74 
75 void setup();
76 void cleanup();
77 
78 char *TCID = "munlockall02";
79 int TST_TOTAL = 1;
80 
81 static char nobody_uid[] = "nobody";
82 struct passwd *ltpuser;
83 
84 #if !defined(UCLINUX)
85 
86 int main(int ac, char **av)
87 {
88 	int lc;
89 
90 	tst_parse_opts(ac, av, NULL, NULL);
91 
92 	setup();
93 
94 	/* check looping state */
95 	for (lc = 0; TEST_LOOPING(lc); lc++) {
96 
97 		tst_count = 0;
98 
99 		TEST(munlockall());
100 		/* check return code */
101 		if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
102 			tst_resm(TPASS, "munlockall() failed"
103 				 " as expected for non-superuser" ":GOT EPERM");
104 		} else {
105 			tst_resm(TCONF, "munlockall() failed to produce "
106 				 "expected errno :%d Got : %d, %s. ***Some distros, such as Red Hat Enterprise Linux, support non-superuser munlockall calls.***",
107 				 EPERM, TEST_ERRNO, strerror(TEST_ERRNO));
108 
109 		}
110 	}
111 
112 	/* cleanup and exit */
113 	cleanup();
114 	tst_exit();
115 
116 }
117 
118 /* setup() - performs all ONE TIME setup for this test. */
119 void setup(void)
120 {
121 	tst_require_root();
122 
123 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
124 
125 	/* switch to nobody user */
126 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
127 		tst_brkm(TBROK, NULL, "\"nobody\"user not present");
128 	}
129 
130 	SAFE_SETEUID(NULL, ltpuser->pw_uid);
131 
132 	TEST_PAUSE;
133 }
134 
135 #else
136 
137 int main(void)
138 {
139 	tst_resm(TINFO, "test is not available on uClinux");
140 	tst_exit();
141 }
142 
143 #endif /* if !defined(UCLINUX) */
144 
145 /*
146  * cleanup() - performs all ONE TIME cleanup for this test at
147  *		completion or premature exit.
148  */
149 void cleanup(void)
150 {
151 	if (seteuid(0) == -1) {
152 		tst_resm(TWARN, "seteuid failed to "
153 			 "to set the effective uid to root");
154 		perror("setuid");
155 	}
156 
157 }
158