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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "safe_macros.h"
58 #include <pwd.h>
59 
60 char *TCID = "statfs03";
61 int TST_TOTAL = 1;
62 int fileHandle = 0;
63 
64 char nobody_uid[] = "nobody";
65 struct passwd *ltpuser;
66 
67 char fname[30] = "testfile";
68 char path[50];
69 struct statfs buf;
70 
71 void setup(void);
72 void cleanup(void);
73 
74 int main(int ac, char **av)
75 {
76 	int lc;
77 
78 	tst_parse_opts(ac, av, NULL, NULL);
79 
80 	setup();
81 
82 	for (lc = 0; TEST_LOOPING(lc); lc++) {
83 
84 		tst_count = 0;
85 
86 		TEST(statfs(path, &buf));
87 
88 		if (TEST_RETURN != -1) {
89 			tst_resm(TFAIL, "call succeeded unexpectedly");
90 
91 		} else {
92 
93 			if (TEST_ERRNO == EACCES) {
94 				tst_resm(TPASS, "expected failure - "
95 					 "errno = %d : %s", TEST_ERRNO,
96 					 strerror(TEST_ERRNO));
97 			} else {
98 				tst_resm(TFAIL, "unexpected error - %d : %s - "
99 					 "expected %d", TEST_ERRNO,
100 					 strerror(TEST_ERRNO), EACCES);
101 			}
102 		}
103 	}
104 
105 	cleanup();
106 	tst_exit();
107 }
108 
109 /*
110  * setup() - performs all ONE TIME setup for this test.
111  */
112 void setup(void)
113 {
114 
115 	tst_require_root();
116 
117 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
118 
119 	TEST_PAUSE;
120 
121 	/* make a temporary directory and cd to it */
122 	tst_tmpdir();
123 	SAFE_CHMOD(cleanup, tst_get_tmpdir(), S_IRWXU);
124 
125 	/* create a test file */
126 	sprintf(fname, "%s.%d", fname, getpid());
127 	if (mkdir(fname, 0444) == -1) {
128 		tst_resm(TFAIL, "creat(2) FAILED to creat temp file");
129 	} else {
130 		sprintf(path, "%s/%s", fname, fname);
131 		if ((fileHandle = creat(path, 0444)) == -1)
132 			tst_brkm(TFAIL | TERRNO, cleanup, "creat failed");
133 	}
134 
135 	ltpuser = getpwnam(nobody_uid);
136 	if (ltpuser == NULL)
137 		tst_brkm(TBROK | TERRNO, cleanup, "getpwnam failed");
138 	if (seteuid(ltpuser->pw_uid) == -1) {
139 		tst_resm(TINFO | TERRNO, "seteuid failed to "
140 			 "to set the effective uid to %d", ltpuser->pw_uid);
141 	}
142 
143 }
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 	/* reset the process ID to the saved ID (root) */
152 	if (setuid(0) == -1) {
153 		tst_resm(TINFO, "setuid(0) failed");
154 	}
155 
156 	/*
157 	 * print timing stats if that option was specified.
158 	 * print errno log if that option was specified.
159 	 */
160 	close(fileHandle);
161 
162 	/* delete the test directory created in setup() */
163 	tst_rmdir();
164 
165 }
166