statfs03.c revision 923b23ff1fd1b77bd895949f9a6b4508c6485f33
1/*
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
18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
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
30 *	 Use a component of the pathname, where search permission
31 *	 is denied for a component of the path prefix of path.
32 *
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>
53#include <fcntl.h>
54#include <errno.h>
55#include <stdio.h>
56#include "test.h"
57#include "usctest.h"
58#include <pwd.h>
59
60extern char *TESTDIR;
61char *TCID = "statfs03";
62int TST_TOTAL = 1;
63int fileHandle = 0;
64extern int Tst_count;
65
66int exp_enos[] = { EACCES, 0 };
67char nobody_uid[] = "nobody";
68struct passwd *ltpuser;
69
70char fname[30] = "testfile";
71char path[50];
72struct statfs buf;
73
74void setup(void);
75void cleanup(void);
76
77int main(int ac, char **av)
78{
79	int lc;			/* loop counter */
80	char *msg;		/* message returned from parse_opts */
81
82	/* parse standard options */
83	if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) {
84		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
85	 /*NOTREACHED*/}
86
87	setup();
88
89	/* set up the expected errnos */
90	TEST_EXP_ENOS(exp_enos);
91
92	/* check looping state if -i option given */
93	for (lc = 0; TEST_LOOPING(lc); lc++) {
94
95		/* reset Tst_count in case we are looping. */
96		Tst_count = 0;
97
98		TEST(statfs(path, &buf));
99
100		if (TEST_RETURN != -1) {
101			tst_resm(TFAIL, "call succeeded unexpectedly");
102
103		} else {
104
105			TEST_ERROR_LOG(TEST_ERRNO);
106
107			if (TEST_ERRNO == EACCES) {
108				tst_resm(TPASS, "expected failure - "
109					 "errno = %d : %s", TEST_ERRNO,
110					 strerror(TEST_ERRNO));
111			} else {
112				tst_resm(TFAIL, "unexpected error - %d : %s - "
113					 "expected %d", TEST_ERRNO,
114					 strerror(TEST_ERRNO), exp_enos[0]);
115			}
116		}
117	}
118	cleanup();
119	 /*NOTREACHED*/ return 0;
120
121}
122
123/*
124 * setup() - performs all ONE TIME setup for this test.
125 */
126void setup()
127{
128
129	/* capture signals */
130	tst_sig(NOFORK, DEF_HANDLER, cleanup);
131
132	/* Pause if that option was specified */
133	TEST_PAUSE;
134
135	/* make a temporary directory and cd to it */
136	tst_tmpdir();
137	if (chmod(TESTDIR, S_IRWXU) == -1)
138		tst_brkm(TBROK, cleanup, "chmod(%s,700) failed; errno %d: %s",
139			 TESTDIR, errno, strerror(errno));
140
141	/* create a test file */
142	sprintf(fname, "%s.%d", fname, getpid());
143	if (mkdir(fname, 0444) == -1) {
144		tst_resm(TFAIL, "creat(2) FAILED to creat temp file");
145	} else {
146		sprintf(path, "%s/%s", fname, fname);
147		if ((fileHandle = creat(path, 0444)) == -1) {
148			tst_resm(TFAIL, "creat (2) FAILED to creat temp file");
149		}
150	}
151
152	/* Switch to nobody user for correct error code collection */
153	if (geteuid() != 0) {
154		tst_brkm(TBROK, tst_exit, "Test must be run as root");
155	}
156
157	ltpuser = getpwnam(nobody_uid);
158	if (seteuid(ltpuser->pw_uid) == -1) {
159		tst_resm(TINFO, "seteuid failed to "
160			 "to set the effective uid to %d", ltpuser->pw_uid);
161		perror("seteuid");
162	}
163
164}
165
166/*
167 * cleanup() - performs all ONE TIME cleanup for this test at
168 *	       completion or premature exit.
169 */
170void cleanup()
171{
172	/* reset the process ID to the saved ID (root) */
173	if (setuid(0) == -1) {
174		tst_resm(TINFO, "setuid(0) failed");
175	}
176
177	/*
178	 * print timing stats if that option was specified.
179	 * print errno log if that option was specified.
180	 */
181	close(fileHandle);
182
183	TEST_CLEANUP;
184
185	/* delete the test directory created in setup() */
186	tst_rmdir();
187
188	/* exit with return code appropriate for results */
189	tst_exit();
190}
191