blob: 657054490792cf65bf11f31976b787a10a59e3ea [file] [log] [blame]
robbiew0e9d4582002-12-24 17:06:57 +00001/*
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew0e9d4582002-12-24 17:06:57 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew0e9d4582002-12-24 17:06:57 +000019 * TEST IDENTIFIER : munlockall02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiew0e9d4582002-12-24 17:06:57 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiew0e9d4582002-12-24 17:06:57 +000023 * TEST TITLE : test for EPERM error value when run as non superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiew0e9d4582002-12-24 17:06:57 +000025 * TEST CASE TOTAL : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiew0e9d4582002-12-24 17:06:57 +000027 * AUTHOR : sowmya adiga<sowmya.adiga@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiew0e9d4582002-12-24 17:06:57 +000029 * 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.
subrata_modak56207ce2009-03-23 13:35:39 +000036 * $
robbiew0e9d4582002-12-24 17:06:57 +000037 * Setup:
38 * Setup signal handling.
39 * Pause for SIGUSR1 if option specified.
40 * Change effective user id to "nobody" user
subrata_modak56207ce2009-03-23 13:35:39 +000041 * $
robbiew0e9d4582002-12-24 17:06:57 +000042 * 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.
subrata_modakbdbaec52009-02-26 12:14:51 +000050 *
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
robbiew0e9d4582002-12-24 17:06:57 +000052 * Cleanup:
53 * change effective user id to root
54 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
robbiew0e9d4582002-12-24 17:06:57 +000056 * 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"
robbiew0e9d4582002-12-24 17:06:57 +000073
74void setup();
75void cleanup();
76
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020077char *TCID = "munlockall02";
78int TST_TOTAL = 1;
robbiew0e9d4582002-12-24 17:06:57 +000079
subrata_modak56207ce2009-03-23 13:35:39 +000080static char nobody_uid[] = "nobody";
robbiew0e9d4582002-12-24 17:06:57 +000081struct passwd *ltpuser;
82
robbiewd34d5812005-07-11 22:28:09 +000083#if !defined(UCLINUX)
84
robbiew0e9d4582002-12-24 17:06:57 +000085int main(int ac, char **av)
86{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020087 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020088 const char *msg;
robbiew0e9d4582002-12-24 17:06:57 +000089
Garrett Cooper45e285d2010-11-22 12:19:25 -080090 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
robbiew0e9d4582002-12-24 17:06:57 +000091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080092
robbiew0e9d4582002-12-24 17:06:57 +000093 }
94
robbiew0e9d4582002-12-24 17:06:57 +000095 setup();
96
97 /* check looping state */
98 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
Caspar Zhangd59a6592013-03-07 14:59:12 +0800100 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000101
robbiew0e9d4582002-12-24 17:06:57 +0000102 TEST(munlockall());
robbiew0e9d4582002-12-24 17:06:57 +0000103 /* check return code */
subrata_modak56207ce2009-03-23 13:35:39 +0000104 if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
105 tst_resm(TPASS, "munlockall() failed"
106 " as expected for non-superuser" ":GOT EPERM");
robbiew0e9d4582002-12-24 17:06:57 +0000107 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000108 tst_resm(TCONF, "munlockall() failed to produce "
109 "expected errno :%d Got : %d, %s. ***Some distros, such as Red Hat Enterprise Linux, support non-superuser munlockall calls.***",
110 EPERM, TEST_ERRNO, strerror(TEST_ERRNO));
111
robbiew0e9d4582002-12-24 17:06:57 +0000112 }
subrata_modak56207ce2009-03-23 13:35:39 +0000113 }
robbiew0e9d4582002-12-24 17:06:57 +0000114
115 /* cleanup and exit */
116 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800117 tst_exit();
robbiew0e9d4582002-12-24 17:06:57 +0000118
Garrett Cooper2c282152010-12-16 00:55:50 -0800119}
robbiew0e9d4582002-12-24 17:06:57 +0000120
robbiew0e9d4582002-12-24 17:06:57 +0000121/* setup() - performs all ONE TIME setup for this test. */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400122void setup(void)
robbiew0e9d4582002-12-24 17:06:57 +0000123{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200124 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
robbiew0e9d4582002-12-24 17:06:57 +0000126 tst_sig(NOFORK, DEF_HANDLER, cleanup);
127
subrata_modak56207ce2009-03-23 13:35:39 +0000128 /* switch to nobody user */
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800130 tst_brkm(TBROK, NULL, "\"nobody\"user not present");
subrata_modak56207ce2009-03-23 13:35:39 +0000131 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000132
subrata_modak56207ce2009-03-23 13:35:39 +0000133 if (seteuid(ltpuser->pw_uid) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800134 tst_brkm(TBROK, NULL, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000135 "to set the effective uid to %d", ltpuser->pw_uid);
136 }
robbiew0e9d4582002-12-24 17:06:57 +0000137
robbiew0e9d4582002-12-24 17:06:57 +0000138 TEST_PAUSE;
139}
140
robbiewd34d5812005-07-11 22:28:09 +0000141#else
142
Mike Frysingerc57fba52014-04-09 18:56:30 -0400143int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000144{
vapier81a63072006-02-27 04:29:21 +0000145 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800146 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000147}
148
149#endif /* if !defined(UCLINUX) */
150
robbiew0e9d4582002-12-24 17:06:57 +0000151/*
152 * cleanup() - performs all ONE TIME cleanup for this test at
153 * completion or premature exit.
154 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400155void cleanup(void)
robbiew0e9d4582002-12-24 17:06:57 +0000156{
robbiew0e9d4582002-12-24 17:06:57 +0000157 if (seteuid(0) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000158 tst_resm(TWARN, "seteuid failed to "
159 "to set the effective uid to root");
robbiew0e9d4582002-12-24 17:06:57 +0000160 perror("setuid");
161 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000162
Chris Dearmanec6edca2012-10-17 19:54:01 -0700163}