blob: aedeaf351b892905fddb833b53b1902a03f63e36 [file] [log] [blame]
robbiew2d1b0892002-06-05 15:20:29 +00001/*
2 *
3 * Copyright (C) Bull S.A. 2001
4 * Copyright (c) International Business Machines Corp., 2001
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew2d1b0892002-06-05 15:20:29 +000019 */
20
21/*
22 * NAME
23 * statfs03.c
24 *
25 * DESCRIPTION
26 * Testcase to check that statfs(2) sets errno to EACCES when
27 * search permission is denied for a component of the path prefix of path.
28 *
29 * ALGORITHM
subrata_modak4bb656a2009-02-26 12:02:09 +000030 * Use a component of the pathname, where search permission
31 * is denied for a component of the path prefix of path.
robbiew2d1b0892002-06-05 15:20:29 +000032 *
33 * USAGE: <for command-line>
34 * statfs03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
35 * where, -c n : Run n copies concurrently.
36 * -e : Turn on errno logging.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * HISTORY
43 * 05/2002 Ported by Jacky Malcles
44 *
45 * RESTRICTIONS
46 * NONE
47 *
48 */
49#include <sys/types.h>
50#include <sys/statfs.h>
51#include <sys/stat.h>
52#include <sys/vfs.h>
plars74948ad2002-11-14 16:16:14 +000053#include <fcntl.h>
robbiew2d1b0892002-06-05 15:20:29 +000054#include <errno.h>
55#include <stdio.h>
56#include "test.h"
robbiew2d1b0892002-06-05 15:20:29 +000057#include <pwd.h>
58
robbiew2d1b0892002-06-05 15:20:29 +000059char *TCID = "statfs03";
60int TST_TOTAL = 1;
mridge3db3f032004-08-25 16:26:11 +000061int fileHandle = 0;
robbiew2d1b0892002-06-05 15:20:29 +000062
robbiew2d1b0892002-06-05 15:20:29 +000063char nobody_uid[] = "nobody";
64struct passwd *ltpuser;
65
66char fname[30] = "testfile";
67char path[50];
plars1ad84512002-07-23 13:11:18 +000068struct statfs buf;
robbiew2d1b0892002-06-05 15:20:29 +000069
70void setup(void);
71void cleanup(void);
72
plars74948ad2002-11-14 16:16:14 +000073int main(int ac, char **av)
robbiew2d1b0892002-06-05 15:20:29 +000074{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020075 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020076 const char *msg;
robbiew2d1b0892002-06-05 15:20:29 +000077
Garrett Cooper60fa8012010-11-22 13:50:58 -080078 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper45e285d2010-11-22 12:19:25 -080079 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew2d1b0892002-06-05 15:20:29 +000080
81 setup();
82
robbiew2d1b0892002-06-05 15:20:29 +000083 for (lc = 0; TEST_LOOPING(lc); lc++) {
84
Caspar Zhangd59a6592013-03-07 14:59:12 +080085 tst_count = 0;
robbiew2d1b0892002-06-05 15:20:29 +000086
plars1ad84512002-07-23 13:11:18 +000087 TEST(statfs(path, &buf));
robbiew2d1b0892002-06-05 15:20:29 +000088
89 if (TEST_RETURN != -1) {
90 tst_resm(TFAIL, "call succeeded unexpectedly");
91
92 } else {
93
robbiew2d1b0892002-06-05 15:20:29 +000094 if (TEST_ERRNO == EACCES) {
95 tst_resm(TPASS, "expected failure - "
96 "errno = %d : %s", TEST_ERRNO,
97 strerror(TEST_ERRNO));
98 } else {
99 tst_resm(TFAIL, "unexpected error - %d : %s - "
100 "expected %d", TEST_ERRNO,
Cyril Hrubis605fa332015-02-04 13:11:20 +0100101 strerror(TEST_ERRNO), EACCES);
robbiew2d1b0892002-06-05 15:20:29 +0000102 }
103 }
104 }
Garrett Cooper60fa8012010-11-22 13:50:58 -0800105
robbiew2d1b0892002-06-05 15:20:29 +0000106 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800107 tst_exit();
robbiew2d1b0892002-06-05 15:20:29 +0000108}
subrata_modak56207ce2009-03-23 13:35:39 +0000109
robbiew2d1b0892002-06-05 15:20:29 +0000110/*
111 * setup() - performs all ONE TIME setup for this test.
112 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400113void setup(void)
robbiew2d1b0892002-06-05 15:20:29 +0000114{
115
Garrett Cooper2384bd42010-11-22 14:53:11 -0800116 tst_require_root(NULL);
Garrett Cooper60fa8012010-11-22 13:50:58 -0800117
robbiew2d1b0892002-06-05 15:20:29 +0000118 tst_sig(NOFORK, DEF_HANDLER, cleanup);
119
robbiew2d1b0892002-06-05 15:20:29 +0000120 TEST_PAUSE;
121
122 /* make a temporary directory and cd to it */
123 tst_tmpdir();
Cyril Hrubis9c31ad22014-05-14 17:15:39 +0200124 if (chmod(tst_get_tmpdir(), S_IRWXU) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125 tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 700) failed",
Cyril Hrubis9c31ad22014-05-14 17:15:39 +0200126 tst_get_tmpdir());
subrata_modakbdbaec52009-02-26 12:14:51 +0000127
robbiew2d1b0892002-06-05 15:20:29 +0000128 /* create a test file */
subrata_modak56207ce2009-03-23 13:35:39 +0000129 sprintf(fname, "%s.%d", fname, getpid());
mridge800b8bf2004-09-27 22:10:27 +0000130 if (mkdir(fname, 0444) == -1) {
131 tst_resm(TFAIL, "creat(2) FAILED to creat temp file");
132 } else {
133 sprintf(path, "%s/%s", fname, fname);
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800134 if ((fileHandle = creat(path, 0444)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 tst_brkm(TFAIL | TERRNO, cleanup, "creat failed");
mridge800b8bf2004-09-27 22:10:27 +0000136 }
robbiew2d1b0892002-06-05 15:20:29 +0000137
subrata_modak56207ce2009-03-23 13:35:39 +0000138 ltpuser = getpwnam(nobody_uid);
Garrett Cooper53740502010-12-16 00:04:01 -0800139 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 tst_brkm(TBROK | TERRNO, cleanup, "getpwnam failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000141 if (seteuid(ltpuser->pw_uid) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 tst_resm(TINFO | TERRNO, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000143 "to set the effective uid to %d", ltpuser->pw_uid);
subrata_modak56207ce2009-03-23 13:35:39 +0000144 }
robbiew2d1b0892002-06-05 15:20:29 +0000145
146}
147
robbiew2d1b0892002-06-05 15:20:29 +0000148/*
149 * cleanup() - performs all ONE TIME cleanup for this test at
150 * completion or premature exit.
151 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400152void cleanup(void)
robbiew2d1b0892002-06-05 15:20:29 +0000153{
subrata_modak56207ce2009-03-23 13:35:39 +0000154 /* reset the process ID to the saved ID (root) */
155 if (setuid(0) == -1) {
156 tst_resm(TINFO, "setuid(0) failed");
157 }
robbiew2d1b0892002-06-05 15:20:29 +0000158
159 /*
160 * print timing stats if that option was specified.
161 * print errno log if that option was specified.
162 */
subrata_modak56207ce2009-03-23 13:35:39 +0000163 close(fileHandle);
mridge3db3f032004-08-25 16:26:11 +0000164
robbiew2d1b0892002-06-05 15:20:29 +0000165 /* delete the test directory created in setup() */
166 tst_rmdir();
167
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800168}